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