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