]> git.ipfire.org Git - people/ms/network.git/blame - src/network
Cleanup code that deletes ports/zones
[people/ms/network.git] / src / network
CommitLineData
5b20e43a
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
1848564d 5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
5b20e43a
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
1848564d
MT
22# Parse the command line
23while [ $# -gt 0 ]; do
24 case "${1}" in
25 -d|--debug)
26 DEBUG=1
5b20e43a 27 ;;
1848564d
MT
28 *)
29 action=${1}
5b20e43a 30 ;;
5b20e43a 31 esac
5b20e43a 32 shift
1848564d 33 [ -n "${action}" ] && break
5b20e43a
MT
34done
35
3647b19f
MT
36. /usr/lib/network/functions
37
e9df08ad
MT
38# Read network settings
39network_settings_read
fe52c5e0 40
1c6a4e30 41cli_settings() {
2212045f 42 if cli_help_requested "$@"; then
e9df08ad 43 cli_show_man network-settings
9111eb72
MT
44 exit ${EXIT_OK}
45 fi
46
47 if [ -n "${1}" ]; then
2212045f 48 network_settings_set "$@"
e9df08ad 49 network_settings_write
9111eb72 50 else
e9df08ad 51 network_settings_print
9111eb72
MT
52 fi
53}
54
1c6a4e30 55cli_device() {
2212045f 56 if cli_help_requested "$@"; then
6c74a64c
MT
57 cli_show_man network-device
58 exit ${EXIT_OK}
59 fi
60
f90dd58c
MT
61 local action="${1}"
62 shift
ec63256a 63
9111eb72 64 case "${action}" in
f90dd58c 65 list)
2212045f 66 cli_device_list "$@"
5cf0edf9 67 ;;
9111eb72 68 *)
f90dd58c
MT
69 local device="${action}"
70 action="${1}"
71 shift
72
73 if ! isset device; then
74 cli_show_man network-device
75 return ${EXIT_ERROR}
76 fi
77
78 assert device_exists ${device}
79
80 case "${action}" in
81 discover)
2212045f 82 cli_device_discover ${device} "$@"
f90dd58c 83 ;;
f5ee091e 84 identify)
2212045f 85 device_identify "${device}" "$@"
f5ee091e 86 ;;
f90dd58c 87 monitor)
2212045f 88 cli_device_monitor "${device}" "$@"
f90dd58c
MT
89 ;;
90 status)
91 cli_device_status ${device}
92 ;;
93 unlock)
2212045f 94 cli_device_serial_unlock ${device} "$@"
f90dd58c
MT
95 ;;
96 ussd)
2212045f 97 cli_device_send_ussd_command "${device}" "$@"
f90dd58c
MT
98 ;;
99 *)
100 cli_show_man network-device
101 ;;
102 esac
9111eb72
MT
103 ;;
104 esac
ec63256a
MT
105
106 return ${EXIT_OK}
107}
108
1c6a4e30 109cli_device_status() {
f90dd58c
MT
110 local device="${1}"
111 assert isset device
ec63256a 112
6c74a64c
MT
113 # Disable debugging output here.
114 local log_disable_stdout=${LOG_DISABLE_STDOUT}
115 LOG_DISABLE_STDOUT="true"
116
ec63256a
MT
117 # Save the type of the device for later.
118 local type=$(device_get_type ${device})
119
120 cli_headline 1 "Device status: ${device}"
121 cli_print_fmt1 1 "Name" "${device}"
122
f90dd58c
MT
123 # Handle special devices
124 case "${type}" in
125 phy)
126 cli_device_status_phy "${device}"
127 return $?
128 ;;
129 serial)
130 cli_device_status_serial "${device}"
131 return $?
132 ;;
133 esac
6c74a64c 134
ec63256a
MT
135 # Print the device status.
136 device_is_up ${device} &>/dev/null
137 local status=$?
138
139 case "${status}" in
140 ${EXIT_TRUE})
fcbf6823 141 status="${CLR_GREEN_B}UP${CLR_RESET}"
ec63256a
MT
142 ;;
143 ${EXIT_FALSE})
fcbf6823 144 status="${CLR_RED_B}DOWN${CLR_RESET}"
ec63256a
MT
145 ;;
146 esac
147
148 cli_print_fmt1 1 "Status" "${status}"
149 cli_print_fmt1 1 "Type" "${type}"
a4f7ad26 150
4ad5981f
MT
151 # Print the driver name
152 local driver="$(device_get_driver "${device}")"
153 if isset driver; then
154 cli_print_fmt1 1 "Driver" "${driver}"
155 fi
156
a4f7ad26
MT
157 # Ethernet-compatible?
158 device_is_ethernet_compatible "${device}" &>/dev/null
159 cli_print_fmt1 1 "Ethernet-compatible" "$(cli_print_bool $?)"
160
ec63256a
MT
161 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
162 cli_space
163
164 # Print the link speed for ethernet devices.
245dffc9 165 if device_is_up ${device} &>/dev/null; then
657540d8
MT
166 local link="$(device_get_link_string "${device}")"
167 if isset link; then
168 cli_print_fmt1 1 "Link" "${link}"
169 fi
245dffc9 170 fi
ec63256a
MT
171
172 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
173 cli_space
174
3cb2fc42
MT
175 # Print device statistics.
176 cli_device_stats 2 ${device}
ec63256a
MT
177
178 # Print some more information.
179 device_has_carrier ${device} &>/dev/null
180 cli_print_fmt1 1 "Has carrier?" "$(cli_print_bool $?)"
181
182 device_is_promisc ${device} &>/dev/null
183 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
6529dfaa
MT
184
185 # Supports multiqueue?
186 if device_supports_multiqueue ${device}; then
6655d1bf 187 cli_print_fmt1 1 "Multiqueue" "Supported"
6529dfaa 188 fi
ec63256a
MT
189 cli_space
190
6529dfaa
MT
191 cli_device_show_queues 2 ${device}
192
7951525a
MT
193 # Print all vlan devices.
194 local vlans=$(device_get_vlans ${device})
195 if [ -n "${vlans}" ]; then
196 cli_headline 2 "VLAN devices"
197
198 local vlan
199 for vlan in ${vlans}; do
200 cli_print 2 "* %-6s - %s" "${vlan}" "$(device_get_address ${vlan})"
ec63256a
MT
201 done
202 cli_space
203 fi
204
f90dd58c
MT
205 case "${type}" in
206 wireless*)
207 local phy="$(device_get_phy "${device}")"
208 if isset phy; then
209 cli_headline 2 "PHY"
210 cli_print_fmt1 2 "Name" "${phy}"
211 cli_print_fmt1 2 "Address" "$(phy_get_address "${phy}")"
212 cli_space
213 fi
214 ;;
215 esac
216
6c74a64c
MT
217 # Reset the logging level.
218 LOG_DISABLE_STDOUT=${log_disable_stdout}
219}
220
1c6a4e30 221cli_device_status_serial() {
6c74a64c
MT
222 local device=${1}
223 assert device_is_serial ${device}
224
225 serial_is_locked ${device} &>/dev/null
226 local locked=$?
227
228 cli_print_fmt1 1 "Locked" "$(cli_print_bool ${locked})"
229 cli_space
230
231 # Cannot go on when the device is locked.
232 [ ${locked} -eq ${EXIT_TRUE} ] && return ${EXIT_OK}
233
234 cli_print_fmt1 1 "Manufacturer" \
235 "$(modem_get_manufacturer ${device})"
236 cli_print_fmt1 1 "Model" \
237 "$(modem_get_model ${device})"
238 cli_print_fmt1 1 "Software version" \
239 "$(modem_get_software_version ${device})"
240
36aeb387
MT
241 # Mobile
242 if modem_is_mobile "${device}"; then
6c74a64c
MT
243 cli_print_fmt1 1 "IMEI" \
244 "$(modem_get_device_imei ${device})"
6c74a64c
MT
245 fi
246 cli_space
36aeb387
MT
247
248 if modem_is_mobile "${device}"; then
249 modem_mobile_network_status "${device}" 2
250 cli_space
251 fi
9111eb72
MT
252}
253
1c6a4e30 254cli_device_status_phy() {
f90dd58c
MT
255 local phy="${1}"
256 assert phy_exists "${phy}"
257
258 local address="$(phy_get_address "${phy}")"
259 cli_print_fmt1 1 "Address" "${address}"
4ad5981f
MT
260
261 # Show kernel module
262 local driver="$(phy_get_driver "${phy}")"
263 if isset driver; then
264 cli_print_fmt1 1 "Driver" "${driver}"
265 fi
266
f90dd58c
MT
267 cli_space
268
269 local devices="$(phy_get_devices "${phy}")"
270 if isset devices; then
271 cli_headline 2 "Soft interfaces"
272
273 local device
274 for device in ${devices}; do
275 cli_print 2 "* %s" "${device}"
276 done
277 cli_space
278 fi
279
280 return ${EXIT_OK}
281}
282
1c6a4e30 283cli_device_discover() {
9111eb72
MT
284 local device=${1}
285 shift
286
5dfc94a8
MT
287 # This can only be executed for ethernet (or compatible) devices
288 if ! device_is_ethernet_compatible "${device}"; then
9111eb72
MT
289 return ${EXIT_OK}
290 fi
291
292 local raw
293
294 while [ $# -gt 0 ]; do
295 case "${1}" in
296 --raw)
297 raw=1
298 ;;
299 esac
300 shift
301 done
302
303 local up
304 device_is_up ${device} && up=1
305 device_set_up ${device}
306
307 enabled raw || echo "${device}"
308
309 local hook
310 local out
311 local ret
312 for hook in $(hook_zone_get_all); do
313 out=$(hook_zone_exec ${hook} discover ${device})
314 ret=$?
315
316 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
317
318 if enabled raw; then
319 case "${ret}" in
320 ${DISCOVER_OK})
321 echo "${hook}: OK"
322 local line
323 while read line; do
324 echo "${hook}: ${line}"
325 done <<<"${out}"
326 ;;
327
328 ${DISCOVER_ERROR})
329 echo "${hook}: FAILED"
330 ;;
331 esac
332 else
333 case "${ret}" in
334 ${DISCOVER_OK})
335 echo " ${hook} was successful."
336 local line
337 while read line; do
338 echo " ${line}"
339 done <<<"${out}"
340 ;;
341
342 ${DISCOVER_ERROR})
343 echo " ${hook} failed."
344 ;;
345 esac
346 fi
347 done
348
349 echo # New line
350
351 [ "${up}" = "1" ] || device_set_down ${device}
352}
353
1c6a4e30 354cli_device_serial_unlock() {
2212045f 355 if cli_help_requested "$@"; then
6c74a64c
MT
356 cli_show_man network-device
357 exit ${EXIT_OK}
358 fi
359
360 local device=${1}
361 assert isset device
362
363 if ! device_is_serial ${device}; then
364 error "${device} is not a serial device."
365 error "Unlocking is only supported for serial devices."
366 exit ${EXIT_ERROR}
367 fi
368
369 # Read the current state of the SIM card.
370 modem_sim_status ${device} &>/dev/null
371 local sim_status_code=$?
372
373 # If the SIM card is already unlocked, we don't need to do anything.
374 if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
375 print "The SIM card is already unlocked."
376 exit ${EXIT_OK}
377
378 # If the SIM card is in an unknown state, we cannot do anything.
379 elif [ ${sim_status_code} -eq ${EXIT_SIM_UNKNOWN} ]; then
380 error "The SIM card is in an unknown state."
381 exit ${EXIT_ERROR}
382 fi
383
384 # Ask for the code.
385 local code=${2}
386 local require_new_pin="false"
387 local new_pin
388
389 while ! isinteger code; do
390 local message
391 case "${sim_status_code}" in
392 ${EXIT_SIM_PIN})
393 message="Please enter PIN:"
394 ;;
395 ${EXIT_SIM_PUK})
396 message="Please enter PUK:"
397 require_new_pin="true"
398 ;;
399 esac
400 assert isset message
401
402 echo -n "${message} "
403 read -s code
404 echo # Print newline.
405
406 if enabled require_new_pin; then
407 local i new_pin2
408 for i in 0 1; do
409 case "${i}" in
410 0)
411 message="Please enter a new PIN code:"
412 ;;
413 1)
414 message="Please confirm the new PIN code:"
415 ;;
416 esac
417
418 echo -n "${message} "
419 read -s new_pin2
420 echo # Print newline.
421
422 if [ -n "${new_pin}" ]; then
423 if [ "${new_pin}" != "${new_pin2}" ]; then
424 error "The entered PIN codes did not match."
425 exit ${EXIT_ERROR}
426 fi
427 else
428 new_pin=${new_pin2}
429 fi
430 done
431 fi
432 done
433
434 # Trying to unlock the SIM card.
435 modem_sim_unlock ${device} ${code} ${new_pin}
436
437 exit $?
438}
439
1c6a4e30 440cli_device_send_ussd_command() {
5cf0edf9
MT
441 local device="${1}"
442 assert isset device
443 shift
444
445 local command
446 local timeout
447
448 while [ $# -gt 0 ]; do
449 case "${1}" in
450 --timeout=*)
451 timeout="$(cli_get_val "${1}")"
452 ;;
453 *)
454 if isset command; then
455 warning "Unrecognized argument: ${1}"
456 else
457 command="${1}"
458 fi
459 ;;
460 esac
461 shift
462 done
463
464 assert device_is_serial "${device}"
465
466 local args
467 if isset timeout; then
468 args="${args} --timeout=${timeout}"
469 fi
470
471 modem_ussd_send_command "${device}" "${command}" ${args}
472 exit $?
473}
474
1c6a4e30 475cli_device_monitor() {
5a38ea84
MT
476 local device="${1}"
477 assert isset device
478
479 if ! device_is_wireless "${device}"; then
480 error "This action only works with wireless devices. Exiting."
481 exit ${EXIT_ERROR}
482 fi
483
484 wireless_monitor "${device}"
485 exit $?
486}
487
1c6a4e30 488cli_device_list() {
f90dd58c
MT
489 local device
490 for device in $(device_list); do
491 cli_device_status "${device}"
492 done
493
494 exit ${EXIT_OK}
495}
496
1c6a4e30 497cli_hostname() {
2212045f 498 if cli_help_requested "$@"; then
9111eb72
MT
499 cli_show_man network
500 exit ${EXIT_OK}
501 fi
502
503 local hostname=${1}
504
505 if [ -n "${hostname}" ]; then
506 config_hostname ${hostname}
507 log INFO "Hostname was set to '${hostname}'."
508 log INFO "Changes do only take affect after reboot."
509 exit ${EXIT_OK}
510 fi
511
512 echo "$(config_hostname)"
513 exit ${EXIT_OK}
514}
515
1c6a4e30 516cli_port() {
2212045f 517 if cli_help_requested "$@"; then
9111eb72
MT
518 cli_show_man network-port
519 exit ${EXIT_OK}
520 fi
521
522 local action
523 local port
524
525 if port_exists ${1}; then
526 port=${1}
527 action=${2}
528 shift 2
529
9111eb72 530 case "${action}" in
f5ee091e 531 edit|create|remove|up|down|status|identify)
2212045f 532 port_${action} "${port}" "$@"
9111eb72 533 ;;
8de0a49f 534 color)
2212045f 535 color_cli "port" "${port}" "$@"
8de0a49f 536 ;;
ba348e4b 537 description)
2212045f 538 description_cli "port" "${port}" "$@"
ba348e4b 539 ;;
9111eb72
MT
540 *)
541 error "Unrecognized argument: ${action}"
542 exit ${EXIT_ERROR}
543 ;;
544 esac
545 else
546 action=${1}
547 shift
548
549 case "${action}" in
1ba6a2bb 550 new|destroy)
2212045f 551 port_${action} "$@"
9111eb72
MT
552 ;;
553 *)
554 error "Unrecognized argument: ${action}"
555 exit ${EXIT_ERROR}
556 ;;
557 esac
558 fi
559}
560
1c6a4e30 561cli_zone() {
2212045f 562 if cli_help_requested "$@"; then
9111eb72
MT
563 cli_show_man network-zone
564 exit ${EXIT_OK}
565 fi
566
567 local action
568 local zone
569
9ea45130 570 if zone_exists ${1}; then
9111eb72
MT
571 zone=${1}
572 action=${2}
573 shift 2
574
575 # Action aliases
576 case "${action}" in
e6fd23fd 577 start|reload)
9111eb72
MT
578 action="up"
579 ;;
580 stop)
581 action="down"
582 ;;
583 show)
584 action="status"
585 ;;
586 esac
587
588 case "${action}" in
ac694a6a 589 port)
2212045f 590 cli_zone_port "${zone}" "$@"
ac694a6a 591 ;;
2bb20bbd 592 rename)
2212045f 593 cli_zone_rename "${zone}" "$@"
2bb20bbd 594 ;;
f5ee091e 595 config|disable|down|edit|enable|identify|status|up)
2212045f 596 zone_${action} ${zone} "$@"
9111eb72 597 ;;
8de0a49f 598 color)
2212045f 599 color_cli "zone" "${zone}" "$@"
8de0a49f 600 ;;
ba348e4b 601 description)
2212045f 602 description_cli "zone" ${zone} "$@"
ba348e4b 603 ;;
9111eb72
MT
604 *)
605 error "Unrecognized argument: ${action}"
606 cli_show_man network-zone
607 exit ${EXIT_ERROR}
608 ;;
609 esac
610 else
611 action=${1}
612 shift
613
614 case "${action}" in
cf0fc8ab 615 new)
2212045f 616 cli_zone_new "$@"
9111eb72 617 ;;
cf0fc8ab 618 destroy)
2212045f 619 cli_zone_destroy "$@"
9111eb72 620 ;;
9111eb72
MT
621 ""|*)
622 if [ -n "${action}" ]; then
623 error "Unrecognized argument: '${action}'"
624 echo
625 fi
626
627 cli_show_man network-zone
628 exit ${EXIT_ERROR}
629 ;;
630 esac
631 fi
632}
633
2c37b88b 634cli_zone_new() {
2212045f 635 if cli_help_requested "$@" || [ $# -lt 2 ]; then
2c37b88b
MT
636 cli_show_man network-zone-new
637 exit ${EXIT_OK}
638 fi
639
2212045f 640 zone_new "$@"
2c37b88b
MT
641}
642
9111eb72
MT
643# Removes a zone either immediately, if it is currently down,
644# or adds a tag that the removal will be done when the zone
645# is brought down the next time.
1c6a4e30 646cli_zone_destroy() {
2212045f 647 if cli_help_requested "$@"; then
9111eb72
MT
648 cli_show_man network-zone
649 exit ${EXIT_OK}
650 fi
651
cf0fc8ab 652 local zone="${1}"
9111eb72 653
165d0d94
MT
654 # Check if the zone exists
655 if ! zone_exists "${zone}"; then
656 error "Zone '${zone}' does not exist"
657 return ${EXIT_ERROR}
9111eb72
MT
658 fi
659
165d0d94
MT
660 echo "Removing zone '${zone}'..."
661 zone_destroy "${zone}" || exit $?
9111eb72
MT
662}
663
1c6a4e30 664cli_zone_port() {
2212045f 665 if cli_help_requested "$@"; then
ac694a6a
MT
666 cli_show_man network-zone-port
667 exit ${EXIT_OK}
668 fi
669
670 local zone="${1}"
671 assert zone_exists "${zone}"
672
673 if port_exists "${2}"; then
674 local port="${2}"
675 local action="${3}"
676 shift 3
677
678 case "${action}" in
679 edit)
2212045f 680 zone_port_edit "${zone}" "${port}" "$@"
ac694a6a
MT
681 ;;
682 *)
683 error "Unrecognised argument: ${action}"
684 exit ${EXIT_ERROR}
685 ;;
686 esac
687 else
688 local action="${2}"
689 shift 2
690
691 case "${action}" in
692 attach)
2212045f 693 zone_port_attach "${zone}" "$@"
ac694a6a
MT
694 ;;
695 detach)
2212045f 696 zone_port_detach "${zone}" "$@"
ac694a6a
MT
697 ;;
698 *)
699 error "Unrecognised argument: ${action}"
700 exit ${EXIT_ERROR}
701 ;;
702 esac
703 fi
704
705 exit ${EXIT_OK}
706}
707
2bb20bbd 708cli_zone_rename() {
2212045f 709 if cli_help_requested "$@"; then
2bb20bbd
SS
710 cli_show_man network-zone
711 exit ${EXIT_OK}
712 fi
713
714 local zone="${1}"
715 local name="${2}"
716 shift 2
717
718 if ! isset name; then
719 error "You need to pass a new name"
720 exit ${EXIT_ERROR}
721 fi
722
723 if ! zone_name_is_valid "${name}"; then
724 error "Invalid new zone name: ${name}"
725 exit ${EXIT_ERROR}
726 fi
727
728 # Check if the zone exists
729 if ! zone_exists "${zone}"; then
730 error "Zone ${zone} does not exist"
731 exit ${EXIT_ERROR}
732 fi
733
2bb20bbd
SS
734 # Check if a zone with the new name already exists
735 if zone_exists "${name}"; then
736 error "Zone ${name} already exists"
737 exit ${EXIT_ERROR}
738 fi
739
740 # Rename
741 if ! zone_rename "${zone}" "${name}"; then
742 error "Could not rename zone ${zone} to ${name}"
743 exit ${EXIT_ERROR}
744 fi
745
746 exit ${EXIT_OK}
747}
748
1c6a4e30 749cli_list_hooks() {
9111eb72
MT
750 local type=${1}
751 shift
752
2212045f 753 if cli_help_requested "$@"; then
9111eb72
MT
754 cli_show_man network-zone
755 exit ${EXIT_OK}
756 fi
757
758 local hook_dir=$(hook_dir ${type})
759 local hook
760
761 for hook in ${hook_dir}/*; do
762 hook=$(basename ${hook})
763 if hook_exists ${type} ${hook}; then
764 echo "${hook}"
765 fi
766 done | sort -u
767}
768
1c6a4e30 769cli_dhcpd() {
6c07160e
MT
770 local proto=${1}
771 shift
772
2212045f 773 if cli_help_requested "$@"; then
6c07160e
MT
774 cli_show_man network-dhcp
775 exit ${EXIT_OK}
776 fi
777
778 local action=${1}
779 shift
780
781 case "${action}" in
782 edit)
2212045f 783 dhcpd_edit ${proto} "$@"
6c07160e
MT
784 ;;
785 start)
786 dhcpd_start ${proto}
9ff233f0
MT
787
788 # Make this permanent
789 dhcpd_enable ${proto}
6c07160e
MT
790 ;;
791 stop)
792 dhcpd_stop ${proto}
9ff233f0
MT
793
794 # Make this permanent
795 dhcpd_disable ${proto}
6c07160e
MT
796 ;;
797 restart|reload)
798 dhcpd_reload ${proto}
799 ;;
800 subnet)
2212045f 801 cli_dhcpd_subnet ${proto} "$@"
6c07160e
MT
802 ;;
803 show|"")
2212045f 804 cli_dhcpd_show ${proto} "$@"
6c07160e
MT
805 ;;
806 *)
807 error "Unrecognized action: ${action}"
808 cli_run_help network dhcpvN
809
810 exit ${EXIT_ERROR}
811 ;;
812 esac
813
814 exit ${EXIT_OK}
815}
816
1c6a4e30 817cli_dhcpd_show() {
6c07160e
MT
818 local proto=${1}
819 assert isset proto
820
821 local settings=$(dhcpd_settings ${proto})
822 assert isset settings
823
824 local ${settings}
825 dhcpd_global_settings_read ${proto}
826
827 cli_headline 1 "Dynamic Host Configuration Protocol Daemon for ${proto/ip/IP}"
828
829 case "${proto}" in
830 ipv6)
831 cli_headline 2 "Lease times"
832 if isinteger VALID_LIFETIME; then
d13929d4 833 cli_print_fmt1 2 "Valid lifetime" "$(format_time ${VALID_LIFETIME})"
6c07160e
MT
834 fi
835
836 if isinteger PREFERRED_LIFETIME; then
d13929d4 837 cli_print_fmt1 2 "Preferred lifetime" "$(format_time ${PREFERRED_LIFETIME})"
6c07160e
MT
838 fi
839
840 cli_space
841 ;;
842 ipv4)
843 cli_print_fmt1 1 "Authoritative" $(cli_print_enabled AUTHORITATIVE)
844 cli_space
845
846 cli_headline 2 "Lease times"
d13929d4
MT
847 cli_print_fmt1 2 "Default lease time" "$(format_time ${DEFAULT_LEASE_TIME})"
848 cli_print_fmt1 2 "Max. lease time" "$(format_time ${MAX_LEASE_TIME})"
6c07160e
MT
849
850 if isset MIN_LEASE_TIME; then
d13929d4 851 cli_print_fmt1 2 "Min. lease time" "$(format_time ${MIN_LEASE_TIME})"
6c07160e
MT
852 fi
853
854 cli_space
855 ;;
856 esac
857
858 # Read the options.
859 local -A options
6a9a9ea6 860 dhcpd_global_options_read ${proto}
6c07160e
MT
861
862 # Print the options if any.
863 if [ ${#options[*]} -gt 0 ]; then
864 cli_headline 2 "Options"
865
866 local option
867 for option in $(dhcpd_options ${proto}); do
868 [ -n "${options[${option}]}" ] || continue
869
870 cli_print_fmt1 2 \
871 "${option}" "${options[${option}]}"
872 done
873 cli_space
874 fi
875
876 # Subnets.
877 local subnets=$(dhcpd_subnet_list ${proto})
878 if [ -n "${subnets}" ]; then
879 cli_headline 2 "Subnets"
6a9a9ea6
MT
880 local subnet
881 for subnet in ${subnets}; do
882 cli_dhcpd_subnet_show ${proto} ${subnet} 2
6c07160e
MT
883 done
884 fi
885}
886
1c6a4e30 887cli_dhcpd_subnet() {
6a9a9ea6
MT
888 assert [ $# -ge 1 ]
889
6c07160e
MT
890 local proto=${1}
891 shift
892
2212045f 893 if cli_help_requested "$@"; then
6c07160e
MT
894 cli_show_man network-dhcp-subnet
895 exit ${EXIT_OK}
896 fi
897
898 local action=${1}
899 shift
900
901 case "${action}" in
902 new)
2212045f 903 dhcpd_subnet_new ${proto} "$@"
6c07160e
MT
904 ;;
905 remove)
2212045f 906 dhcpd_subnet_remove ${proto} "$@"
6c07160e 907 ;;
6a9a9ea6
MT
908 *:*/*|*.*.*.*/*)
909 local subnet=${action}
6c07160e 910
6a9a9ea6
MT
911 if ! dhcpd_subnet_exists ${proto} ${subnet}; then
912 error "Subnet ${subnet} does not exist"
6c07160e
MT
913 return ${EXIT_ERROR}
914 fi
915
916 # Update the action.
917 action=${1}
918 shift
919
920 case "${action}" in
921 edit)
2212045f 922 dhcpd_subnet_edit ${proto} ${subnet} "$@"
6c07160e
MT
923 local ret=$?
924
925 if [ ${ret} -eq ${EXIT_OK} ]; then
926 dhcpd_reload ${proto}
927 fi
928 exit ${ret}
929 ;;
930 range)
2212045f 931 cli_dhcpd_subnet_range ${proto} ${subnet} "$@"
6c07160e
MT
932 exit $?
933 ;;
934 show)
2212045f 935 cli_dhcpd_subnet_show ${proto} ${subnet} "$@"
6c07160e
MT
936 exit $?
937 ;;
938 options)
2212045f 939 cli_dhcpd_subnet_options ${proto} ${subnet} "$@"
6c07160e
MT
940 exit $?
941 ;;
942 *)
943 error "Unrecognized action: ${action}"
944 cli_run_help network dhcpvN subnet
945 exit ${EXIT_ERROR}
946 ;;
947 esac
948 ;;
949 show)
6a9a9ea6
MT
950 local subnet
951 for subnet in $(dhcpd_subnet_list ${proto}); do
952 cli_dhcpd_subnet_show ${proto} ${subnet}
6c07160e
MT
953 done
954 ;;
955 *)
956 error "Unrecognized action: ${action}"
957 cli_run_help network dhcpvN subnet
958
959 exit ${EXIT_ERROR}
960 ;;
961 esac
962
963 exit ${EXIT_OK}
964}
965
1c6a4e30 966cli_dhcpd_subnet_range() {
6a9a9ea6 967 assert [ $# -ge 2 ]
6c07160e 968
6a9a9ea6
MT
969 local proto=${1}
970 local subnet=${2}
971 local action=${3}
972 shift 3
6c07160e
MT
973
974 case "${action}" in
975 new)
2212045f 976 dhcpd_subnet_range_new ${proto} ${subnet} "$@" || exit ${EXIT_ERROR}
6c07160e
MT
977 ;;
978 remove)
2212045f 979 dhcpd_subnet_range_remove ${proto} ${subnet} "$@" || exit ${EXIT_ERROR}
6c07160e
MT
980 ;;
981 *)
982 error "Unrecognized action: ${action}"
983 cli_run_help network dhcpvN subnet range
984 exit ${EXIT_ERROR}
985 ;;
986 esac
90b63fca
MT
987
988 dhcpd_reload ${proto}
989 return ${EXIT_OK}
6c07160e
MT
990}
991
1c6a4e30 992cli_dhcpd_subnet_show() {
6a9a9ea6 993 assert [ $# -ge 2 -a $# -le 3 ]
6c07160e 994
6a9a9ea6
MT
995 local proto=${1}
996 local subnet=${2}
6c07160e
MT
997
998 local level=${3}
999 isset level || level=0
1000
1001 local $(dhcpd_subnet_settings ${proto})
1002
1003 # Read in configuration settings.
6a9a9ea6 1004 dhcpd_subnet_read ${proto} ${subnet}
6c07160e 1005
94784eb9 1006 cli_headline $(( ${level} + 1 )) "DHCP Subnet Declaration"
6c07160e
MT
1007 cli_print_fmt1 $(( ${level} + 1 )) \
1008 "Subnet" "${ADDRESS}/${PREFIX}"
1009 cli_space
1010
1011 # Read the options.
1012 local -A options
6a9a9ea6 1013 dhcpd_subnet_options_read "${proto}" "${subnet}"
6c07160e
MT
1014
1015 # Print the options if any.
1016 if [ ${#options[*]} -gt 0 ]; then
1017 cli_headline $(( ${level} + 2 )) "Options"
1018
1019 local option
cc02f6be 1020 for option in $(dhcpd_subnet_options_list ${proto}); do
6c07160e
MT
1021 [ -n "${options[${option}]}" ] || continue
1022
1023 cli_print_fmt1 $(( ${level} + 2 )) \
1024 "${option}" "${options[${option}]}"
1025 done
1026 cli_space
1027 fi
1028
1029 # Ranges.
1030 cli_headline $(( ${level} + 2 )) "Ranges"
1031
6a9a9ea6 1032 local ranges=$(dhcpd_subnet_range_list ${proto} ${subnet})
6c07160e 1033 if isset ranges; then
f3ac1159
MT
1034 local range $(dhcpd_subnet_range_settings ${proto})
1035 for range in ${ranges}; do
6a9a9ea6 1036 dhcpd_subnet_range_read ${proto} ${subnet} ${range}
6c07160e 1037
f3ac1159 1038 cli_print $(( ${level} + 2 )) "%s - %s" ${START} ${END}
6c07160e
MT
1039 done
1040 else
1041 cli_print $(( ${level} + 2 )) "No ranges have been defined."
1042 fi
1043
1044 cli_space
1045}
1046
1c6a4e30 1047cli_dhcpd_subnet_options() {
de21f838 1048 assert [ $# -ge 2 ]
6c07160e 1049
6a9a9ea6
MT
1050 local proto=${1}
1051 local subnet=${2}
6c07160e 1052
6c07160e
MT
1053 local key val
1054 while [ $# -gt 0 ]; do
1055 case "${1}" in
1056 *=*)
1057 key=$(cli_get_key ${1})
2212045f 1058 val=$(cli_get_val "${1}")
6c07160e 1059
6a9a9ea6 1060 dhcpd_subnet_option_set ${proto} ${subnet} ${key} ${val}
6c07160e 1061 esac
de21f838 1062 shift
6c07160e
MT
1063 done
1064}
1065
1c6a4e30 1066cli_start() {
2212045f 1067 if cli_help_requested "$@"; then
9111eb72
MT
1068 cli_show_man network
1069 exit ${EXIT_OK}
1070 fi
1071
2212045f 1072 local zones=$(zones_get "$@")
9111eb72
MT
1073
1074 local zone
1075 for zone in ${zones}; do
1076 zone_start ${zone} &
1077 done
1078
1079 wait # until everything is settled
1080}
1081
1c6a4e30 1082cli_stop() {
2212045f 1083 if cli_help_requested "$@"; then
9111eb72
MT
1084 cli_show_man network
1085 exit ${EXIT_OK}
1086 fi
1087
2212045f 1088 local zones=$(zones_get "$@")
9111eb72
MT
1089
1090 local zone
1091 for zone in ${zones}; do
1092 zone_stop ${zone} &
1093 done
1094
1095 wait # until everything is settled
1096}
1097
1c6a4e30 1098cli_restart() {
2212045f 1099 if cli_help_requested "$@"; then
9111eb72
MT
1100 cli_show_man network
1101 exit ${EXIT_OK}
1102 fi
1103
2212045f 1104 cli_stop "$@"
9111eb72
MT
1105
1106 # Give the system some time to calm down
1107 sleep ${TIMEOUT_RESTART}
1108
2212045f 1109 cli_start "$@"
9111eb72
MT
1110}
1111
1c6a4e30 1112cli_status() {
2212045f 1113 if cli_help_requested "$@"; then
9111eb72
MT
1114 cli_show_man network
1115 exit ${EXIT_OK}
1116 fi
1117
1118 # When dumping status information, the debug
1119 # mode clutters the console which is not what we want.
1120 # Logging on the console is disabled for a short time.
1121 local log_disable_stdout=${LOG_DISABLE_STDOUT}
1122 LOG_DISABLE_STDOUT="true"
1123
3a3eeb1d 1124 local arguments=( $@ )
9111eb72 1125
3a3eeb1d
MT
1126 # Show all zones when no arguments are given
1127 if ! isset arguments; then
1128 local zone
1129 for zone in $(zones_get_all); do
1130 zone_status "${zone}"
1131 done
1132
1133 return ${EXIT_OK}
1134 fi
1135
1136 local arg
1137 for arg in ${arguments[@]}; do
1138 # Is this a zone?
1139 if zone_exists "${arg}"; then
1140 zone_status "${arg}"
1141
1142 # Is this a port?
1143 elif port_exists "${arg}"; then
1144 port_status "${arg}"
1145
1146 # Is this a PHY?
1147 elif phy_exists "${arg}"; then
1148 cli_device_status "${arg}"
1149
1150 # Is this a device?
1151 elif device_exists "${arg}"; then
1152 cli_device_status "${arg}"
1153
1154 # Unknown argument
1155 else
1156 error "Unknown argument: ${arg}"
1157 fi
9111eb72
MT
1158 done
1159
1160 # Reset logging.
1161 LOG_DISABLE_STDOUT=${log_disable_stdout}
1162}
1163
1c6a4e30 1164cli_reset() {
2212045f 1165 if cli_help_requested "$@"; then
9111eb72
MT
1166 cli_show_man network
1167 exit ${EXIT_OK}
1168 fi
1169
1170 warning_log "Will reset the whole network configuration!!!"
9111eb72
MT
1171 # Force mode is disabled by default
1172 local force=0
1173
1174 while [ $# -gt 0 ]; do
1175 case "${1}" in
1176 --force|-f)
1177 force=1
1178 ;;
1179 esac
1180 shift
1181 done
1182
1183 # If we are not running in force mode, we ask the user if he does know
1184 # what he is doing.
1185 if ! enabled force; then
1186 if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
1187 exit ${EXIT_ERROR}
1188 fi
1189 fi
1190
f5f9014d
MT
1191 # Destroy all IPsec VPN connections
1192 local connection
1193 for connection in $(ipsec_list_connections); do
1194 ipsec_connection_destroy "${connection}"
1195 done
1196
ecac9e2b
JS
1197 local pool
1198 for pool in $(ipsec_list_pools); do
1199 ipsec_pool_destroy "${pool}"
1200 done
1201
f5f9014d
MT
1202 # Stop strongswan
1203 ipsec_strongswan_autostart
1204
3cac4fcd
MT
1205 # Destroy all user-defined security policies
1206 local secpol
1207 for secpol in $(vpn_security_policies_list_user); do
f05fa5d4 1208 vpn_security_policies_destroy "${secpol}"
3cac4fcd
MT
1209 done
1210
9111eb72 1211 local zone
8707df4b 1212 for zone in $(zones_get_all); do
36e1f715 1213 zone_destroy "${zone}"
9111eb72
MT
1214 done
1215
1216 local port
8707df4b 1217 for port in $(ports_get_all); do
1ba6a2bb 1218 port_destroy "${port}"
9111eb72
MT
1219 done
1220
acc9efd5
MT
1221 # Flush all DNS servers.
1222 dns_server_flush
1223
76334a38
MT
1224 # Trigger udev to re-add all physical network devices
1225 cmd_quiet udevadm trigger --action=add --subsystem-match=net
1226
9111eb72
MT
1227 exit ${EXIT_OK}
1228}
1229
85afd775
MT
1230# Help function: will show the default man page to the user.
1231# Optionally, there are two arguments taken, the type of hook
1232# and which hook should be shown.
1c6a4e30 1233cli_help() {
6b7654eb
MT
1234 local cmd=${1}
1235 shift
85afd775 1236
6b7654eb
MT
1237 case "${cmd}" in
1238 zone|port|config)
1239 local type=${cmd}
1240 local hook=${1}
1241
1242 # List all hooks if requested
1243 if [ "${hook}" = "list-hooks" ]; then
1244 cli_list_hooks ${type}
1245 return ${EXIT_OK}
1246 fi
85afd775 1247
6b7654eb
MT
1248 if ! hook_exists ${type} ${hook}; then
1249 error "No hook with name '${hook}' could be found"
1250 exit "${EXIT_ERROR}"
1251 fi
85afd775 1252
6b7654eb
MT
1253 hook_exec ${type} ${hook} help
1254 ;;
1255
1256 # In all other cases show the default man page
1257 *)
1258 cli_show_man network
1259 return ${EXIT_OK}
1260 ;;
1261 esac
85afd775 1262
6b7654eb 1263 return ${EXIT_ERROR}
85afd775
MT
1264}
1265
1c6a4e30 1266cli_dns_server() {
2212045f 1267 if cli_help_requested "$@"; then
6b34112f 1268 cli_show_man network-dns-server
acc9efd5
MT
1269 exit ${EXIT_OK}
1270 fi
1271
1272 # Get the command.
1273 local cmd=${1}; shift
1274 if [ -z "${cmd}" ]; then
6b34112f 1275 cli_show_man network-dns-server
acc9efd5
MT
1276 exit ${EXIT_ERROR}
1277 fi
1278
6f923dac
MT
1279 # Get the new server to process (if any).
1280 local server=${1}
1281 local priority=${2}
1282
acc9efd5
MT
1283 case "${cmd}" in
1284 list)
8d1d2745 1285 dns_server_show
e5efaa6b 1286 exit ${EXIT_OK}
acc9efd5
MT
1287 ;;
1288 add)
e5651e17
MT
1289 if dns_server_exists ${server}; then
1290 error "DNS server '${server}' already exists!"
1291 exit ${EXIT_ERROR}
1292 fi
1293
6f923dac
MT
1294 log INFO "Adding new DNS server: ${server}"
1295 dns_server_add ${server} ${priority}
acc9efd5
MT
1296 ;;
1297 remove)
e5651e17
MT
1298 if ! dns_server_exists ${server}; then
1299 error "DNS server '${server}' does not exist!"
1300 exit ${EXIT_ERROR}
1301 fi
1302
6f923dac
MT
1303 log INFO "Removing DNS server: ${server}"
1304 dns_server_remove ${server} ${priority}
acc9efd5
MT
1305 ;;
1306 update)
1307 # Just run the update afterwards.
1308 ;;
1309 *)
1310 error "No such command: ${cmd}"
1311 exit ${EXIT_ERROR}
1312 esac
1313
1314 # Update the local DNS configuration after changes have been made.
bf98f6fc 1315 dns_server_update
acc9efd5
MT
1316
1317 exit ${EXIT_OK}
1318}
1319
1c6a4e30 1320cli_raw() {
bae37360
MT
1321 local cmd="${1}"
1322 assert isset cmd
1323 shift
1324
1325 case "${cmd}" in
c041b631
MT
1326 db-dump)
1327 db_dump
1328 ;;
e8253eba
JS
1329 device-get-by-mac-address)
1330 device_get_by_mac_address "$@"
1331 ;;
ac7ef1f9 1332 ipsec-connection-exists)
2212045f 1333 ipsec_connection_exists "$@"
ac7ef1f9 1334 ;;
bae37360
MT
1335 list-devices)
1336 device_list
1337 ;;
cc02f6be 1338 list-dhcpd-ranges-of-subnet)
2212045f 1339 dhcpd_subnet_range_list "$@"
cc02f6be
MT
1340 ;;
1341 list-dhcpd-settings)
2212045f 1342 dhcpd_global_settings_list "$@"
cc02f6be
MT
1343 ;;
1344 list-dhcpd-subnets)
2212045f 1345 dhcpd_subnet_list "$@"
cc02f6be
MT
1346 ;;
1347 list-dhcpd-subnet-options)
2212045f 1348 dhcpd_subnet_options_list "$@"
cc02f6be 1349 ;;
8d1d2745
MT
1350 list-dns-servers)
1351 dns_server_list
1352 ;;
bae37360
MT
1353 list-free-ports)
1354 port_list_free
1355 ;;
1356 list-hooks)
2212045f 1357 hook_list "$@"
bae37360 1358 ;;
c87851b7
JS
1359 list-ipsec-connections)
1360 ipsec_list_connections
1361 ;;
bae37360
MT
1362 list-ports)
1363 port_list
1364 ;;
1365 list-ports-of-zone)
2212045f 1366 zone_get_ports "$@"
bae37360 1367 ;;
1d9c63f7
JS
1368 list-vpn-security-policies-all)
1369 vpn_security_policies_list_all
1370 ;;
bae37360
MT
1371 list-settings)
1372 network_settings_list
1373 ;;
1374 list-zones)
1375 zones_get_all
1376 ;;
1ca5fe9f
JS
1377 list-next-free-zones)
1378 zones_get_next_free
1379 ;;
9da14216 1380 list-zone-config-ids)
2212045f 1381 zone_config_list_ids "$@"
9da14216 1382 ;;
a8be80ca 1383 list-zone-config-hids)
2212045f 1384 zone_config_list_hids "$@"
a8be80ca 1385 ;;
51408ef7 1386 vpn-security-policy-exists)
2212045f 1387 vpn_security_policy_exists "$@"
51408ef7 1388 ;;
1ca5fe9f 1389 zone-name-is-valid)
2212045f 1390 zone_name_is_valid "$@"
1ca5fe9f 1391 ;;
7deab7cc 1392 zone-config-id-is-valid)
2212045f 1393 zone_config_id_is_valid "$@"
7deab7cc 1394 ;;
fbba9abf 1395 zone-config-hid-is-valid)
2212045f 1396 zone_config_hid_is_valid "$@"
fbba9abf 1397 ;;
bae37360
MT
1398 *)
1399 error "No such command: ${cmd}"
1400 exit ${EXIT_ERROR}
1401 ;;
1402 esac
1403
1404 exit ${EXIT_OK}
1405}
1406
1848564d
MT
1407# Process the given action
1408case "${action}" in
b8357295 1409 init)
02a397d3
MT
1410 # Update resolv.conf(5) when initializing the network
1411 dns_generate_resolvconf
1412
1413 # Also execute all triggers
1414 triggers_execute_all "init"
b8357295
MT
1415 ;;
1416
47feaba1 1417 settings|hostname|port|device|zone|start|stop|restart|status|reset|route|vpn|wireless)
2212045f 1418 cli_${action} "$@"
1848564d
MT
1419 ;;
1420
6c07160e
MT
1421 # DHCP server configuration (automatically detects which protocol to use).
1422 dhcpv6|dhcpv4)
2212045f 1423 cli_dhcpd ${action/dhcp/ip} "$@"
6c07160e
MT
1424 ;;
1425
6b34112f
MT
1426 # DNS server configuration.
1427 dns-server)
2212045f 1428 cli_dns_server "$@"
6b34112f
MT
1429 ;;
1430
fe4555b5 1431 ""|help|--help|-h)
2212045f 1432 cli_help "$@"
1848564d 1433 ;;
fe4555b5 1434
bae37360 1435 raw)
2212045f 1436 cli_raw "$@"
bae37360
MT
1437 ;;
1438
1848564d 1439 *)
fe4555b5 1440 error "Invalid command given: ${action}"
de28a630 1441 cli_usage "network help"
1848564d 1442 exit ${EXIT_CONF_ERROR}
fe4555b5 1443 ;;
1848564d 1444esac
85afd775
MT
1445
1446exit ${EXIT_OK}