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