]> git.ipfire.org Git - people/stevee/network.git/blame - src/network
Remove support for macvtap devices
[people/stevee/network.git] / src / network
CommitLineData
5b20e43a
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
1848564d 5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
5b20e43a
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
1848564d
MT
22# Parse the command line
23while [ $# -gt 0 ]; do
24 case "${1}" in
25 -d|--debug)
26 DEBUG=1
5b20e43a 27 ;;
1848564d
MT
28 *)
29 action=${1}
5b20e43a 30 ;;
5b20e43a 31 esac
5b20e43a 32 shift
1848564d 33 [ -n "${action}" ] && break
5b20e43a
MT
34done
35
3647b19f
MT
36. /usr/lib/network/functions
37
e9df08ad
MT
38# Read network settings
39network_settings_read
fe52c5e0 40
e9df08ad 41function cli_settings() {
9111eb72 42 if cli_help_requested $@; then
e9df08ad 43 cli_show_man network-settings
9111eb72
MT
44 exit ${EXIT_OK}
45 fi
46
47 if [ -n "${1}" ]; then
e9df08ad
MT
48 settings_set $@
49 network_settings_write
9111eb72 50 else
e9df08ad 51 network_settings_print
9111eb72
MT
52 fi
53}
54
55function cli_device() {
6c74a64c
MT
56 if cli_help_requested $@; then
57 cli_show_man network-device
58 exit ${EXIT_OK}
59 fi
60
9111eb72
MT
61 local device=${1}
62 local action=${2}
63 shift 2
64
ec63256a
MT
65 if ! isset device; then
66 cli_show_man network-device
9111eb72
MT
67 return ${EXIT_ERROR}
68 fi
69
ec63256a
MT
70 assert device_exists ${device}
71
9111eb72
MT
72 case "${action}" in
73 discover)
9111eb72
MT
74 cli_device_discover ${device} $@
75 ;;
5a38ea84
MT
76 monitor)
77 cli_device_monitor "${device}" $@
78 ;;
ec63256a
MT
79 status)
80 cli_device_status ${device}
9111eb72 81 ;;
6c74a64c
MT
82 unlock)
83 cli_device_serial_unlock ${device} $@
84 ;;
5cf0edf9
MT
85 ussd)
86 cli_device_send_ussd_command "${device}" $@
87 ;;
9111eb72
MT
88 *)
89 cli_show_man network-device
90 ;;
91 esac
ec63256a
MT
92
93 return ${EXIT_OK}
94}
95
96function cli_device_status() {
97 local device=${1}
98 assert device_exists ${device}
99
6c74a64c
MT
100 # Disable debugging output here.
101 local log_disable_stdout=${LOG_DISABLE_STDOUT}
102 LOG_DISABLE_STDOUT="true"
103
ec63256a
MT
104 # Save the type of the device for later.
105 local type=$(device_get_type ${device})
106
107 cli_headline 1 "Device status: ${device}"
108 cli_print_fmt1 1 "Name" "${device}"
109
6c74a64c
MT
110 # Handle serial devices.
111 if [ "${type}" = "serial" ]; then
112 cli_device_status_serial ${device}
113 return $?
114 fi
115
ec63256a
MT
116 # Print the device status.
117 device_is_up ${device} &>/dev/null
118 local status=$?
119
120 case "${status}" in
121 ${EXIT_TRUE})
fcbf6823 122 status="${CLR_GREEN_B}UP${CLR_RESET}"
ec63256a
MT
123 ;;
124 ${EXIT_FALSE})
fcbf6823 125 status="${CLR_RED_B}DOWN${CLR_RESET}"
ec63256a
MT
126 ;;
127 esac
128
129 cli_print_fmt1 1 "Status" "${status}"
130 cli_print_fmt1 1 "Type" "${type}"
131 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
132 cli_space
133
134 # Print the link speed for ethernet devices.
245dffc9 135 if device_is_up ${device} &>/dev/null; then
657540d8
MT
136 local link="$(device_get_link_string "${device}")"
137 if isset link; then
138 cli_print_fmt1 1 "Link" "${link}"
139 fi
245dffc9 140 fi
ec63256a
MT
141
142 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
143 cli_space
144
3cb2fc42
MT
145 # Print device statistics.
146 cli_device_stats 2 ${device}
ec63256a
MT
147
148 # Print some more information.
149 device_has_carrier ${device} &>/dev/null
150 cli_print_fmt1 1 "Has carrier?" "$(cli_print_bool $?)"
151
152 device_is_promisc ${device} &>/dev/null
153 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
154 cli_space
155
7951525a
MT
156 # Print all vlan devices.
157 local vlans=$(device_get_vlans ${device})
158 if [ -n "${vlans}" ]; then
159 cli_headline 2 "VLAN devices"
160
161 local vlan
162 for vlan in ${vlans}; do
163 cli_print 2 "* %-6s - %s" "${vlan}" "$(device_get_address ${vlan})"
ec63256a
MT
164 done
165 cli_space
166 fi
167
6c74a64c
MT
168 # Reset the logging level.
169 LOG_DISABLE_STDOUT=${log_disable_stdout}
170}
171
172function cli_device_status_serial() {
173 local device=${1}
174 assert device_is_serial ${device}
175
176 serial_is_locked ${device} &>/dev/null
177 local locked=$?
178
179 cli_print_fmt1 1 "Locked" "$(cli_print_bool ${locked})"
180 cli_space
181
182 # Cannot go on when the device is locked.
183 [ ${locked} -eq ${EXIT_TRUE} ] && return ${EXIT_OK}
184
185 cli_print_fmt1 1 "Manufacturer" \
186 "$(modem_get_manufacturer ${device})"
187 cli_print_fmt1 1 "Model" \
188 "$(modem_get_model ${device})"
189 cli_print_fmt1 1 "Software version" \
190 "$(modem_get_software_version ${device})"
191
192 if modem_is_mobile ${device}; then
193 cli_print_fmt1 1 "IMEI" \
194 "$(modem_get_device_imei ${device})"
195 cli_space
196
197 cli_headline 2 "Network status"
198 modem_sim_status ${device} &>/dev/null
199 local sim_status_code=$?
200
201 local sim_status="unknown"
202 case "${sim_status_code}" in
203 ${EXIT_SIM_READY})
204 sim_status="SIM ready"
205 ;;
206 ${EXIT_SIM_PIN})
207 sim_status="PIN locked"
208 ;;
209 ${EXIT_SIM_PUK})
210 sim_status="PUK locked"
211 ;;
212 esac
213 cli_print_fmt1 2 "SIM status" "${sim_status}"
214
215 if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
216 cli_print_fmt1 2 "IMSI" \
217 "$(modem_get_sim_imsi ${device})"
218 cli_print_fmt1 2 "Operator" \
219 "$(modem_get_network_operator ${device})"
220 cli_print_fmt1 2 "Mode" \
221 "$(modem_get_network_mode ${device})"
222 cli_print_fmt1 2 "Signal quality" \
223 "$(modem_get_signal_quality ${device}) dBm"
224
225 local ber=$(modem_get_bit_error_rate ${device})
226 isset ber || ber="unknown"
227 cli_print_fmt1 2 "Bit Error Rate" "${ber}"
228 fi
229 fi
230 cli_space
9111eb72
MT
231}
232
233function cli_device_discover() {
234 local device=${1}
235 shift
236
237 local device_type=$(device_get_type ${device})
238 if [ "${device_type}" != "real" ]; then
239 return ${EXIT_OK}
240 fi
241
242 local raw
243
244 while [ $# -gt 0 ]; do
245 case "${1}" in
246 --raw)
247 raw=1
248 ;;
249 esac
250 shift
251 done
252
253 local up
254 device_is_up ${device} && up=1
255 device_set_up ${device}
256
257 enabled raw || echo "${device}"
258
259 local hook
260 local out
261 local ret
262 for hook in $(hook_zone_get_all); do
263 out=$(hook_zone_exec ${hook} discover ${device})
264 ret=$?
265
266 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
267
268 if enabled raw; then
269 case "${ret}" in
270 ${DISCOVER_OK})
271 echo "${hook}: OK"
272 local line
273 while read line; do
274 echo "${hook}: ${line}"
275 done <<<"${out}"
276 ;;
277
278 ${DISCOVER_ERROR})
279 echo "${hook}: FAILED"
280 ;;
281 esac
282 else
283 case "${ret}" in
284 ${DISCOVER_OK})
285 echo " ${hook} was successful."
286 local line
287 while read line; do
288 echo " ${line}"
289 done <<<"${out}"
290 ;;
291
292 ${DISCOVER_ERROR})
293 echo " ${hook} failed."
294 ;;
295 esac
296 fi
297 done
298
299 echo # New line
300
301 [ "${up}" = "1" ] || device_set_down ${device}
302}
303
6c74a64c
MT
304function cli_device_serial_unlock() {
305 if cli_help_requested $@; then
306 cli_show_man network-device
307 exit ${EXIT_OK}
308 fi
309
310 local device=${1}
311 assert isset device
312
313 if ! device_is_serial ${device}; then
314 error "${device} is not a serial device."
315 error "Unlocking is only supported for serial devices."
316 exit ${EXIT_ERROR}
317 fi
318
319 # Read the current state of the SIM card.
320 modem_sim_status ${device} &>/dev/null
321 local sim_status_code=$?
322
323 # If the SIM card is already unlocked, we don't need to do anything.
324 if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
325 print "The SIM card is already unlocked."
326 exit ${EXIT_OK}
327
328 # If the SIM card is in an unknown state, we cannot do anything.
329 elif [ ${sim_status_code} -eq ${EXIT_SIM_UNKNOWN} ]; then
330 error "The SIM card is in an unknown state."
331 exit ${EXIT_ERROR}
332 fi
333
334 # Ask for the code.
335 local code=${2}
336 local require_new_pin="false"
337 local new_pin
338
339 while ! isinteger code; do
340 local message
341 case "${sim_status_code}" in
342 ${EXIT_SIM_PIN})
343 message="Please enter PIN:"
344 ;;
345 ${EXIT_SIM_PUK})
346 message="Please enter PUK:"
347 require_new_pin="true"
348 ;;
349 esac
350 assert isset message
351
352 echo -n "${message} "
353 read -s code
354 echo # Print newline.
355
356 if enabled require_new_pin; then
357 local i new_pin2
358 for i in 0 1; do
359 case "${i}" in
360 0)
361 message="Please enter a new PIN code:"
362 ;;
363 1)
364 message="Please confirm the new PIN code:"
365 ;;
366 esac
367
368 echo -n "${message} "
369 read -s new_pin2
370 echo # Print newline.
371
372 if [ -n "${new_pin}" ]; then
373 if [ "${new_pin}" != "${new_pin2}" ]; then
374 error "The entered PIN codes did not match."
375 exit ${EXIT_ERROR}
376 fi
377 else
378 new_pin=${new_pin2}
379 fi
380 done
381 fi
382 done
383
384 # Trying to unlock the SIM card.
385 modem_sim_unlock ${device} ${code} ${new_pin}
386
387 exit $?
388}
389
5cf0edf9
MT
390function cli_device_send_ussd_command() {
391 local device="${1}"
392 assert isset device
393 shift
394
395 local command
396 local timeout
397
398 while [ $# -gt 0 ]; do
399 case "${1}" in
400 --timeout=*)
401 timeout="$(cli_get_val "${1}")"
402 ;;
403 *)
404 if isset command; then
405 warning "Unrecognized argument: ${1}"
406 else
407 command="${1}"
408 fi
409 ;;
410 esac
411 shift
412 done
413
414 assert device_is_serial "${device}"
415
416 local args
417 if isset timeout; then
418 args="${args} --timeout=${timeout}"
419 fi
420
421 modem_ussd_send_command "${device}" "${command}" ${args}
422 exit $?
423}
424
5a38ea84
MT
425function cli_device_monitor() {
426 local device="${1}"
427 assert isset device
428
429 if ! device_is_wireless "${device}"; then
430 error "This action only works with wireless devices. Exiting."
431 exit ${EXIT_ERROR}
432 fi
433
434 wireless_monitor "${device}"
435 exit $?
436}
437
9111eb72
MT
438function cli_hostname() {
439 if cli_help_requested $@; then
440 cli_show_man network
441 exit ${EXIT_OK}
442 fi
443
444 local hostname=${1}
445
446 if [ -n "${hostname}" ]; then
447 config_hostname ${hostname}
448 log INFO "Hostname was set to '${hostname}'."
449 log INFO "Changes do only take affect after reboot."
450 exit ${EXIT_OK}
451 fi
452
453 echo "$(config_hostname)"
454 exit ${EXIT_OK}
455}
456
457function cli_port() {
458 if cli_help_requested $@; then
459 cli_show_man network-port
460 exit ${EXIT_OK}
461 fi
462
463 local action
464 local port
465
466 if port_exists ${1}; then
467 port=${1}
468 action=${2}
469 shift 2
470
471 # Action aliases
472 case "${action}" in
473 start)
474 action="up"
475 ;;
476 stop)
477 action="down"
478 ;;
479 show)
480 action="status"
481 ;;
482 esac
483
484 case "${action}" in
485 edit|up|down|status)
486 port_${action} ${port} $@
487 ;;
488 *)
489 error "Unrecognized argument: ${action}"
490 exit ${EXIT_ERROR}
491 ;;
492 esac
493 else
494 action=${1}
495 shift
496
497 case "${action}" in
498 create|destroy)
499 port_${action} $@
500 ;;
501 *)
502 error "Unrecognized argument: ${action}"
503 exit ${EXIT_ERROR}
504 ;;
505 esac
506 fi
507}
508
509function cli_zone() {
510 if cli_help_requested $@; then
511 cli_show_man network-zone
512 exit ${EXIT_OK}
513 fi
514
515 local action
516 local zone
517
518 if zone_name_is_valid ${1}; then
519 zone=${1}
520 action=${2}
521 shift 2
522
523 # Action aliases
524 case "${action}" in
e6fd23fd 525 start|reload)
9111eb72
MT
526 action="up"
527 ;;
528 stop)
529 action="down"
530 ;;
531 show)
532 action="status"
533 ;;
534 esac
535
536 case "${action}" in
5c5b8e36 537 config|disable|down|edit|enable|port|status|up)
9111eb72
MT
538 zone_${action} ${zone} $@
539 ;;
540 *)
541 error "Unrecognized argument: ${action}"
542 cli_show_man network-zone
543 exit ${EXIT_ERROR}
544 ;;
545 esac
546 else
547 action=${1}
548 shift
549
550 case "${action}" in
551 create)
552 zone_${action} $@
553 ;;
554 remove)
555 cli_zone_remove $@
556 ;;
557 list-hooks)
558 cli_list_hooks zone $@
559 ;;
560 ""|*)
561 if [ -n "${action}" ]; then
562 error "Unrecognized argument: '${action}'"
563 echo
564 fi
565
566 cli_show_man network-zone
567 exit ${EXIT_ERROR}
568 ;;
569 esac
570 fi
571}
572
573# Removes a zone either immediately, if it is currently down,
574# or adds a tag that the removal will be done when the zone
575# is brought down the next time.
576function cli_zone_remove() {
577 if cli_help_requested $@; then
578 cli_show_man network-zone
579 exit ${EXIT_OK}
580 fi
581
582 local zone=${1}
583 assert zone_exists ${zone}
584
585 if zone_is_up ${zone}; then
586 echo "Zone '${zone}' is up and will be removed when it goes down the next time."
587 zone_remove ${zone}
588 else
589 echo "Removing zone '${zone}' now..."
590 zone_remove_now ${zone}
591 fi
592
593 exit ${EXIT_OK}
594}
595
596function cli_list_hooks() {
597 local type=${1}
598 shift
599
600 if cli_help_requested $@; then
601 cli_show_man network-zone
602 exit ${EXIT_OK}
603 fi
604
605 local hook_dir=$(hook_dir ${type})
606 local hook
607
608 for hook in ${hook_dir}/*; do
609 hook=$(basename ${hook})
610 if hook_exists ${type} ${hook}; then
611 echo "${hook}"
612 fi
613 done | sort -u
614}
615
cb965348
MT
616function cli_route() {
617 if cli_help_requested $@; then
618 cli_show_man network-route
619 exit ${EXIT_OK}
620 fi
621
622 local action=${1}
623 shift
624
625 case "${action}" in
626 # Add a new route.
627 add)
628 route_add $@
629 ;;
630 # Remove an existing route.
631 remove)
632 route_remove $@
633 ;;
634 # List all routes.
635 list)
636 route_list $@
d2021e87 637 return ${EXIT_OK}
cb965348
MT
638 ;;
639 *)
640 error "Unrecognized action: ${action}"
641 cli_run_help network route
642
643 exit ${EXIT_ERROR}
644 ;;
645 esac
646
d2021e87
MT
647 # Applying all routes.
648 route_apply
649
cb965348
MT
650 exit ${EXIT_OK}
651}
652
6c07160e
MT
653function cli_dhcpd() {
654 local proto=${1}
655 shift
656
657 if cli_help_requested $@; then
658 cli_show_man network-dhcp
659 exit ${EXIT_OK}
660 fi
661
662 local action=${1}
663 shift
664
665 case "${action}" in
666 edit)
667 dhcpd_edit ${proto} $@
668 ;;
669 start)
670 dhcpd_start ${proto}
671 ;;
672 stop)
673 dhcpd_stop ${proto}
674 ;;
675 restart|reload)
676 dhcpd_reload ${proto}
677 ;;
678 subnet)
679 cli_dhcpd_subnet ${proto} $@
680 ;;
681 show|"")
682 cli_dhcpd_show ${proto} $@
683 ;;
684 *)
685 error "Unrecognized action: ${action}"
686 cli_run_help network dhcpvN
687
688 exit ${EXIT_ERROR}
689 ;;
690 esac
691
692 exit ${EXIT_OK}
693}
694
695function cli_dhcpd_show() {
696 local proto=${1}
697 assert isset proto
698
699 local settings=$(dhcpd_settings ${proto})
700 assert isset settings
701
702 local ${settings}
703 dhcpd_global_settings_read ${proto}
704
705 cli_headline 1 "Dynamic Host Configuration Protocol Daemon for ${proto/ip/IP}"
706
707 case "${proto}" in
708 ipv6)
709 cli_headline 2 "Lease times"
710 if isinteger VALID_LIFETIME; then
711 cli_print_fmt1 2 "Valid lifetime" "${VALID_LIFETIME}s"
712 fi
713
714 if isinteger PREFERRED_LIFETIME; then
715 cli_print_fmt1 2 "Preferred lifetime" "${PREFERRED_LIFETIME}s"
716 fi
717
718 cli_space
719 ;;
720 ipv4)
721 cli_print_fmt1 1 "Authoritative" $(cli_print_enabled AUTHORITATIVE)
722 cli_space
723
724 cli_headline 2 "Lease times"
725 cli_print_fmt1 2 "Default lease time" "${DEFAULT_LEASE_TIME}s"
726 cli_print_fmt1 2 "Max. lease time" "${MAX_LEASE_TIME}s"
727
728 if isset MIN_LEASE_TIME; then
729 cli_print_fmt1 2 "Min. lease time" "${MIN_LEASE_TIME}s"
730 fi
731
732 cli_space
733 ;;
734 esac
735
736 # Read the options.
737 local -A options
738 dhcpd_global_options_read ${proto} ${subnet_id}
739
740 # Print the options if any.
741 if [ ${#options[*]} -gt 0 ]; then
742 cli_headline 2 "Options"
743
744 local option
745 for option in $(dhcpd_options ${proto}); do
746 [ -n "${options[${option}]}" ] || continue
747
748 cli_print_fmt1 2 \
749 "${option}" "${options[${option}]}"
750 done
751 cli_space
752 fi
753
754 # Subnets.
755 local subnets=$(dhcpd_subnet_list ${proto})
756 if [ -n "${subnets}" ]; then
757 cli_headline 2 "Subnets"
758 local subnet_id
759 for subnet_id in ${subnets}; do
760 cli_dhcpd_subnet_show ${proto} ${subnet_id} 2
761 done
762 fi
763}
764
765function cli_dhcpd_subnet() {
766 local proto=${1}
767 shift
768
769 if cli_help_requested $@; then
770 cli_show_man network-dhcp-subnet
771 exit ${EXIT_OK}
772 fi
773
774 local action=${1}
775 shift
776
777 case "${action}" in
778 new)
779 dhcpd_subnet_new ${proto} $@
780 ;;
781 remove)
782 dhcpd_subnet_remove ${proto} $@
783 ;;
784 [0-9]*)
785 local subnet_id=${action}
786
787 if ! dhcpd_subnet_exists ${proto} ${subnet_id}; then
788 error "The given subnet with ID ${subnet_id} does not exist."
789 return ${EXIT_ERROR}
790 fi
791
792 # Update the action.
793 action=${1}
794 shift
795
796 case "${action}" in
797 edit)
798 dhcpd_subnet_edit ${proto} ${subnet_id} $@
799 local ret=$?
800
801 if [ ${ret} -eq ${EXIT_OK} ]; then
802 dhcpd_reload ${proto}
803 fi
804 exit ${ret}
805 ;;
806 range)
807 cli_dhcpd_subnet_range ${proto} ${subnet_id} $@
808 exit $?
809 ;;
810 show)
811 cli_dhcpd_subnet_show ${proto} ${subnet_id} $@
812 exit $?
813 ;;
814 options)
815 cli_dhcpd_subnet_options ${proto} ${subnet_id} $@
816 exit $?
817 ;;
818 *)
819 error "Unrecognized action: ${action}"
820 cli_run_help network dhcpvN subnet
821 exit ${EXIT_ERROR}
822 ;;
823 esac
824 ;;
825 show)
826 local subnet_id
827 for subnet_id in $(dhcpd_subnet_list ${proto}); do
828 cli_dhcpd_subnet_show ${proto} ${subnet_id}
829 done
830 ;;
831 *)
832 error "Unrecognized action: ${action}"
833 cli_run_help network dhcpvN subnet
834
835 exit ${EXIT_ERROR}
836 ;;
837 esac
838
839 exit ${EXIT_OK}
840}
841
842function cli_dhcpd_subnet_range() {
843 local proto=${1}
844 assert isset proto
845 shift
846
847 local subnet_id=${1}
848 assert isset subnet_id
849 shift
850
851 local action=${1}
852 shift
853
854 case "${action}" in
855 new)
856 dhcpd_subnet_range_new ${proto} ${subnet_id} $@
857 exit $?
858 ;;
859 remove)
860 dhcpd_subnet_range_remove ${proto} ${subnet_id} $@
861 exit $?
862 ;;
863 *)
864 error "Unrecognized action: ${action}"
865 cli_run_help network dhcpvN subnet range
866 exit ${EXIT_ERROR}
867 ;;
868 esac
869}
870
871function cli_dhcpd_subnet_show() {
872 local proto=${1}
873 assert isset proto
874
875 local subnet_id=${2}
876 assert isset subnet_id
877
878 local level=${3}
879 isset level || level=0
880
881 local $(dhcpd_subnet_settings ${proto})
882
883 # Read in configuration settings.
884 dhcpd_subnet_read ${proto} ${subnet_id}
885
886 cli_headline $(( ${level} + 1 )) \
887 "DHCP${proto/ip/} subnet declaration #${subnet_id}"
888 cli_print_fmt1 $(( ${level} + 1 )) \
889 "Subnet" "${ADDRESS}/${PREFIX}"
890 cli_space
891
892 # Read the options.
893 local -A options
894 dhcpd_subnet_options_read ${proto} ${subnet_id}
895
896 # Print the options if any.
897 if [ ${#options[*]} -gt 0 ]; then
898 cli_headline $(( ${level} + 2 )) "Options"
899
900 local option
901 for option in $(dhcpd_subnet_options ${proto}); do
902 [ -n "${options[${option}]}" ] || continue
903
904 cli_print_fmt1 $(( ${level} + 2 )) \
905 "${option}" "${options[${option}]}"
906 done
907 cli_space
908 fi
909
910 # Ranges.
911 cli_headline $(( ${level} + 2 )) "Ranges"
912
913 local ranges=$(dhcpd_subnet_range_list ${proto} ${subnet_id})
914 if isset ranges; then
915 local range_id $(dhcpd_subnet_range_settings ${proto})
916 for range_id in ${ranges}; do
917 dhcpd_subnet_range_read ${proto} ${subnet_id} ${range_id}
918
919 cli_print $(( ${level} + 2 )) \
920 "#%d: %s - %s" ${range_id} ${START} ${END}
921 done
922 else
923 cli_print $(( ${level} + 2 )) "No ranges have been defined."
924 fi
925
926 cli_space
927}
928
929function cli_dhcpd_options() {
930 local proto=${1}
931 assert isset proto
932 shift
933
934 local subnet_id=${1}
935 assert isset subnet_id
936 shift
937
938 local valid_options=$(dhcpd_subnet_options ${proto})
939
940 local key val
941 while [ $# -gt 0 ]; do
942 case "${1}" in
943 *=*)
944 key=$(cli_get_key ${1})
945 val=$(cli_get_val ${1})
946
947 dhcpd_subnet_option_set ${proto} ${subnet_id} ${key} ${val}
948 esac
949 done
950}
951
9111eb72
MT
952function cli_start() {
953 if cli_help_requested $@; then
954 cli_show_man network
955 exit ${EXIT_OK}
956 fi
957
958 local zones=$(zones_get $@)
959
960 local zone
961 for zone in ${zones}; do
962 zone_start ${zone} &
963 done
964
965 wait # until everything is settled
966}
967
968function cli_stop() {
969 if cli_help_requested $@; then
970 cli_show_man network
971 exit ${EXIT_OK}
972 fi
973
974 local zones=$(zones_get $@)
975
976 local zone
977 for zone in ${zones}; do
978 zone_stop ${zone} &
979 done
980
981 wait # until everything is settled
982}
983
984function cli_restart() {
985 if cli_help_requested $@; then
986 cli_show_man network
987 exit ${EXIT_OK}
988 fi
989
990 cli_stop $@
991
992 # Give the system some time to calm down
993 sleep ${TIMEOUT_RESTART}
994
995 cli_start $@
996}
997
998function cli_status() {
999 if cli_help_requested $@; then
1000 cli_show_man network
1001 exit ${EXIT_OK}
1002 fi
1003
1004 # When dumping status information, the debug
1005 # mode clutters the console which is not what we want.
1006 # Logging on the console is disabled for a short time.
1007 local log_disable_stdout=${LOG_DISABLE_STDOUT}
1008 LOG_DISABLE_STDOUT="true"
1009
1010 local zones=$(zones_get $@)
1011
1012 local zone
1013 for zone in ${zones}; do
1014 zone_status ${zone}
1015 done
1016
1017 # Reset logging.
1018 LOG_DISABLE_STDOUT=${log_disable_stdout}
1019}
1020
1021function cli_reset() {
1022 if cli_help_requested $@; then
1023 cli_show_man network
1024 exit ${EXIT_OK}
1025 fi
1026
1027 warning_log "Will reset the whole network configuration!!!"
1028
1029 # Force mode is disabled by default
1030 local force=0
1031
1032 while [ $# -gt 0 ]; do
1033 case "${1}" in
1034 --force|-f)
1035 force=1
1036 ;;
1037 esac
1038 shift
1039 done
1040
1041 # If we are not running in force mode, we ask the user if he does know
1042 # what he is doing.
1043 if ! enabled force; then
1044 if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
1045 exit ${EXIT_ERROR}
1046 fi
1047 fi
1048
1049 local zone
1050 for zone in $(zones_get --all); do
1051 zone_remove ${zone}
1052 done
1053
1054 local port
1055 for port in $(ports_get --all); do
1056 port_remove ${port}
1057 done
1058
acc9efd5
MT
1059 # Flush all DNS servers.
1060 dns_server_flush
1061
9111eb72
MT
1062 # Re-run the initialization functions
1063 init_run
1064
1065 exit ${EXIT_OK}
1066}
1067
85afd775
MT
1068# Help function: will show the default man page to the user.
1069# Optionally, there are two arguments taken, the type of hook
1070# and which hook should be shown.
1071function cli_help() {
1072 local type=${1}
1073 local what=${2}
1074
1075 # Remove unknown types.
1076 if ! listmatch ${type} zone port config; then
1077 type=""
1078 fi
1079
1080 # If no arguments were given, we will show the default page.
1081 if [ -z "${type}" ]; then
1082 cli_show_man network
1083 return ${EXIT_OK}
1084 fi
1085
1086 if ! hook_exists ${type} ${what}; then
1087 error "Hook of type '${type}' and name '${what}' could not be found."
1088 exit "${EXIT_ERROR}"
1089 fi
1090
1091 hook_exec ${type} ${what} help
1092}
1093
6b34112f 1094function cli_dns_server() {
acc9efd5 1095 if cli_help_requested $@; then
6b34112f 1096 cli_show_man network-dns-server
acc9efd5
MT
1097 exit ${EXIT_OK}
1098 fi
1099
1100 # Get the command.
1101 local cmd=${1}; shift
1102 if [ -z "${cmd}" ]; then
6b34112f 1103 cli_show_man network-dns-server
acc9efd5
MT
1104 exit ${EXIT_ERROR}
1105 fi
1106
6f923dac
MT
1107 # Get the new server to process (if any).
1108 local server=${1}
1109 local priority=${2}
1110
acc9efd5
MT
1111 case "${cmd}" in
1112 list)
acc9efd5 1113 dns_server_list
e5efaa6b 1114 exit ${EXIT_OK}
acc9efd5
MT
1115 ;;
1116 add)
e5651e17
MT
1117 if dns_server_exists ${server}; then
1118 error "DNS server '${server}' already exists!"
1119 exit ${EXIT_ERROR}
1120 fi
1121
6f923dac
MT
1122 log INFO "Adding new DNS server: ${server}"
1123 dns_server_add ${server} ${priority}
acc9efd5
MT
1124 ;;
1125 remove)
e5651e17
MT
1126 if ! dns_server_exists ${server}; then
1127 error "DNS server '${server}' does not exist!"
1128 exit ${EXIT_ERROR}
1129 fi
1130
6f923dac
MT
1131 log INFO "Removing DNS server: ${server}"
1132 dns_server_remove ${server} ${priority}
acc9efd5
MT
1133 ;;
1134 update)
1135 # Just run the update afterwards.
1136 ;;
1137 *)
1138 error "No such command: ${cmd}"
1139 exit ${EXIT_ERROR}
1140 esac
1141
1142 # Update the local DNS configuration after changes have been made.
1143 dns_generate_resolvconf
6f923dac 1144 radvd_update
acc9efd5
MT
1145
1146 exit ${EXIT_OK}
1147}
1148
1848564d
MT
1149# Process the given action
1150case "${action}" in
b8357295
MT
1151 init)
1152 init_run
1153 ;;
1154
e9df08ad 1155 settings|hostname|port|device|zone|start|stop|restart|status|reset|route)
0a79ea02 1156 cli_${action} $@
1848564d
MT
1157 ;;
1158
6c07160e
MT
1159 # DHCP server configuration (automatically detects which protocol to use).
1160 dhcpv6|dhcpv4)
1161 cli_dhcpd ${action/dhcp/ip} $@
1162 ;;
1163
6b34112f
MT
1164 # DNS server configuration.
1165 dns-server)
1166 cli_dns_server $@
1167 ;;
1168
fe4555b5 1169 ""|help|--help|-h)
85afd775 1170 cli_help $@
1848564d 1171 ;;
fe4555b5 1172
1848564d 1173 *)
fe4555b5 1174 error "Invalid command given: ${action}"
de28a630 1175 cli_usage "network help"
1848564d 1176 exit ${EXIT_CONF_ERROR}
fe4555b5 1177 ;;
1848564d 1178esac
85afd775
MT
1179
1180exit ${EXIT_OK}