]> git.ipfire.org Git - people/ms/network.git/blame - src/network
Split port hooks in to (create|remove|up|down) actions
[people/ms/network.git] / src / network
CommitLineData
5b20e43a
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
1848564d 5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
5b20e43a
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
1848564d
MT
22# Parse the command line
23while [ $# -gt 0 ]; do
24 case "${1}" in
25 -d|--debug)
26 DEBUG=1
5b20e43a 27 ;;
1848564d
MT
28 *)
29 action=${1}
5b20e43a 30 ;;
5b20e43a 31 esac
5b20e43a 32 shift
1848564d 33 [ -n "${action}" ] && break
5b20e43a
MT
34done
35
3647b19f
MT
36. /usr/lib/network/functions
37
e9df08ad
MT
38# Read network settings
39network_settings_read
fe52c5e0 40
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
9111eb72 471 case "${action}" in
1ba6a2bb
MT
472 edit|create|remove|up|down|status)
473 port_${action} "${port}" $@
9111eb72
MT
474 ;;
475 *)
476 error "Unrecognized argument: ${action}"
477 exit ${EXIT_ERROR}
478 ;;
479 esac
480 else
481 action=${1}
482 shift
483
484 case "${action}" in
1ba6a2bb 485 new|destroy)
9111eb72
MT
486 port_${action} $@
487 ;;
488 *)
489 error "Unrecognized argument: ${action}"
490 exit ${EXIT_ERROR}
491 ;;
492 esac
493 fi
494}
495
496function cli_zone() {
497 if cli_help_requested $@; then
498 cli_show_man network-zone
499 exit ${EXIT_OK}
500 fi
501
502 local action
503 local zone
504
505 if zone_name_is_valid ${1}; then
506 zone=${1}
507 action=${2}
508 shift 2
509
510 # Action aliases
511 case "${action}" in
e6fd23fd 512 start|reload)
9111eb72
MT
513 action="up"
514 ;;
515 stop)
516 action="down"
517 ;;
518 show)
519 action="status"
520 ;;
521 esac
522
523 case "${action}" in
5c5b8e36 524 config|disable|down|edit|enable|port|status|up)
9111eb72
MT
525 zone_${action} ${zone} $@
526 ;;
527 *)
528 error "Unrecognized argument: ${action}"
529 cli_show_man network-zone
530 exit ${EXIT_ERROR}
531 ;;
532 esac
533 else
534 action=${1}
535 shift
536
537 case "${action}" in
538 create)
539 zone_${action} $@
540 ;;
541 remove)
542 cli_zone_remove $@
543 ;;
544 list-hooks)
545 cli_list_hooks zone $@
546 ;;
547 ""|*)
548 if [ -n "${action}" ]; then
549 error "Unrecognized argument: '${action}'"
550 echo
551 fi
552
553 cli_show_man network-zone
554 exit ${EXIT_ERROR}
555 ;;
556 esac
557 fi
558}
559
560# Removes a zone either immediately, if it is currently down,
561# or adds a tag that the removal will be done when the zone
562# is brought down the next time.
563function cli_zone_remove() {
564 if cli_help_requested $@; then
565 cli_show_man network-zone
566 exit ${EXIT_OK}
567 fi
568
569 local zone=${1}
570 assert zone_exists ${zone}
571
572 if zone_is_up ${zone}; then
573 echo "Zone '${zone}' is up and will be removed when it goes down the next time."
574 zone_remove ${zone}
575 else
576 echo "Removing zone '${zone}' now..."
577 zone_remove_now ${zone}
578 fi
579
580 exit ${EXIT_OK}
581}
582
583function cli_list_hooks() {
584 local type=${1}
585 shift
586
587 if cli_help_requested $@; then
588 cli_show_man network-zone
589 exit ${EXIT_OK}
590 fi
591
592 local hook_dir=$(hook_dir ${type})
593 local hook
594
595 for hook in ${hook_dir}/*; do
596 hook=$(basename ${hook})
597 if hook_exists ${type} ${hook}; then
598 echo "${hook}"
599 fi
600 done | sort -u
601}
602
cb965348
MT
603function cli_route() {
604 if cli_help_requested $@; then
605 cli_show_man network-route
606 exit ${EXIT_OK}
607 fi
608
609 local action=${1}
610 shift
611
612 case "${action}" in
613 # Add a new route.
614 add)
615 route_add $@
616 ;;
617 # Remove an existing route.
618 remove)
619 route_remove $@
620 ;;
621 # List all routes.
622 list)
623 route_list $@
d2021e87 624 return ${EXIT_OK}
cb965348
MT
625 ;;
626 *)
627 error "Unrecognized action: ${action}"
628 cli_run_help network route
629
630 exit ${EXIT_ERROR}
631 ;;
632 esac
633
d2021e87
MT
634 # Applying all routes.
635 route_apply
636
cb965348
MT
637 exit ${EXIT_OK}
638}
639
6c07160e
MT
640function cli_dhcpd() {
641 local proto=${1}
642 shift
643
644 if cli_help_requested $@; then
645 cli_show_man network-dhcp
646 exit ${EXIT_OK}
647 fi
648
649 local action=${1}
650 shift
651
652 case "${action}" in
653 edit)
654 dhcpd_edit ${proto} $@
655 ;;
656 start)
657 dhcpd_start ${proto}
658 ;;
659 stop)
660 dhcpd_stop ${proto}
661 ;;
662 restart|reload)
663 dhcpd_reload ${proto}
664 ;;
665 subnet)
666 cli_dhcpd_subnet ${proto} $@
667 ;;
668 show|"")
669 cli_dhcpd_show ${proto} $@
670 ;;
671 *)
672 error "Unrecognized action: ${action}"
673 cli_run_help network dhcpvN
674
675 exit ${EXIT_ERROR}
676 ;;
677 esac
678
679 exit ${EXIT_OK}
680}
681
682function cli_dhcpd_show() {
683 local proto=${1}
684 assert isset proto
685
686 local settings=$(dhcpd_settings ${proto})
687 assert isset settings
688
689 local ${settings}
690 dhcpd_global_settings_read ${proto}
691
692 cli_headline 1 "Dynamic Host Configuration Protocol Daemon for ${proto/ip/IP}"
693
694 case "${proto}" in
695 ipv6)
696 cli_headline 2 "Lease times"
697 if isinteger VALID_LIFETIME; then
698 cli_print_fmt1 2 "Valid lifetime" "${VALID_LIFETIME}s"
699 fi
700
701 if isinteger PREFERRED_LIFETIME; then
702 cli_print_fmt1 2 "Preferred lifetime" "${PREFERRED_LIFETIME}s"
703 fi
704
705 cli_space
706 ;;
707 ipv4)
708 cli_print_fmt1 1 "Authoritative" $(cli_print_enabled AUTHORITATIVE)
709 cli_space
710
711 cli_headline 2 "Lease times"
712 cli_print_fmt1 2 "Default lease time" "${DEFAULT_LEASE_TIME}s"
713 cli_print_fmt1 2 "Max. lease time" "${MAX_LEASE_TIME}s"
714
715 if isset MIN_LEASE_TIME; then
716 cli_print_fmt1 2 "Min. lease time" "${MIN_LEASE_TIME}s"
717 fi
718
719 cli_space
720 ;;
721 esac
722
723 # Read the options.
724 local -A options
725 dhcpd_global_options_read ${proto} ${subnet_id}
726
727 # Print the options if any.
728 if [ ${#options[*]} -gt 0 ]; then
729 cli_headline 2 "Options"
730
731 local option
732 for option in $(dhcpd_options ${proto}); do
733 [ -n "${options[${option}]}" ] || continue
734
735 cli_print_fmt1 2 \
736 "${option}" "${options[${option}]}"
737 done
738 cli_space
739 fi
740
741 # Subnets.
742 local subnets=$(dhcpd_subnet_list ${proto})
743 if [ -n "${subnets}" ]; then
744 cli_headline 2 "Subnets"
745 local subnet_id
746 for subnet_id in ${subnets}; do
747 cli_dhcpd_subnet_show ${proto} ${subnet_id} 2
748 done
749 fi
750}
751
752function cli_dhcpd_subnet() {
753 local proto=${1}
754 shift
755
756 if cli_help_requested $@; then
757 cli_show_man network-dhcp-subnet
758 exit ${EXIT_OK}
759 fi
760
761 local action=${1}
762 shift
763
764 case "${action}" in
765 new)
766 dhcpd_subnet_new ${proto} $@
767 ;;
768 remove)
769 dhcpd_subnet_remove ${proto} $@
770 ;;
771 [0-9]*)
772 local subnet_id=${action}
773
774 if ! dhcpd_subnet_exists ${proto} ${subnet_id}; then
775 error "The given subnet with ID ${subnet_id} does not exist."
776 return ${EXIT_ERROR}
777 fi
778
779 # Update the action.
780 action=${1}
781 shift
782
783 case "${action}" in
784 edit)
785 dhcpd_subnet_edit ${proto} ${subnet_id} $@
786 local ret=$?
787
788 if [ ${ret} -eq ${EXIT_OK} ]; then
789 dhcpd_reload ${proto}
790 fi
791 exit ${ret}
792 ;;
793 range)
794 cli_dhcpd_subnet_range ${proto} ${subnet_id} $@
795 exit $?
796 ;;
797 show)
798 cli_dhcpd_subnet_show ${proto} ${subnet_id} $@
799 exit $?
800 ;;
801 options)
802 cli_dhcpd_subnet_options ${proto} ${subnet_id} $@
803 exit $?
804 ;;
805 *)
806 error "Unrecognized action: ${action}"
807 cli_run_help network dhcpvN subnet
808 exit ${EXIT_ERROR}
809 ;;
810 esac
811 ;;
812 show)
813 local subnet_id
814 for subnet_id in $(dhcpd_subnet_list ${proto}); do
815 cli_dhcpd_subnet_show ${proto} ${subnet_id}
816 done
817 ;;
818 *)
819 error "Unrecognized action: ${action}"
820 cli_run_help network dhcpvN subnet
821
822 exit ${EXIT_ERROR}
823 ;;
824 esac
825
826 exit ${EXIT_OK}
827}
828
829function cli_dhcpd_subnet_range() {
830 local proto=${1}
831 assert isset proto
832 shift
833
834 local subnet_id=${1}
835 assert isset subnet_id
836 shift
837
838 local action=${1}
839 shift
840
841 case "${action}" in
842 new)
843 dhcpd_subnet_range_new ${proto} ${subnet_id} $@
844 exit $?
845 ;;
846 remove)
847 dhcpd_subnet_range_remove ${proto} ${subnet_id} $@
848 exit $?
849 ;;
850 *)
851 error "Unrecognized action: ${action}"
852 cli_run_help network dhcpvN subnet range
853 exit ${EXIT_ERROR}
854 ;;
855 esac
856}
857
858function cli_dhcpd_subnet_show() {
859 local proto=${1}
860 assert isset proto
861
862 local subnet_id=${2}
863 assert isset subnet_id
864
865 local level=${3}
866 isset level || level=0
867
868 local $(dhcpd_subnet_settings ${proto})
869
870 # Read in configuration settings.
871 dhcpd_subnet_read ${proto} ${subnet_id}
872
873 cli_headline $(( ${level} + 1 )) \
874 "DHCP${proto/ip/} subnet declaration #${subnet_id}"
875 cli_print_fmt1 $(( ${level} + 1 )) \
876 "Subnet" "${ADDRESS}/${PREFIX}"
877 cli_space
878
879 # Read the options.
880 local -A options
881 dhcpd_subnet_options_read ${proto} ${subnet_id}
882
883 # Print the options if any.
884 if [ ${#options[*]} -gt 0 ]; then
885 cli_headline $(( ${level} + 2 )) "Options"
886
887 local option
888 for option in $(dhcpd_subnet_options ${proto}); do
889 [ -n "${options[${option}]}" ] || continue
890
891 cli_print_fmt1 $(( ${level} + 2 )) \
892 "${option}" "${options[${option}]}"
893 done
894 cli_space
895 fi
896
897 # Ranges.
898 cli_headline $(( ${level} + 2 )) "Ranges"
899
900 local ranges=$(dhcpd_subnet_range_list ${proto} ${subnet_id})
901 if isset ranges; then
902 local range_id $(dhcpd_subnet_range_settings ${proto})
903 for range_id in ${ranges}; do
904 dhcpd_subnet_range_read ${proto} ${subnet_id} ${range_id}
905
906 cli_print $(( ${level} + 2 )) \
907 "#%d: %s - %s" ${range_id} ${START} ${END}
908 done
909 else
910 cli_print $(( ${level} + 2 )) "No ranges have been defined."
911 fi
912
913 cli_space
914}
915
916function cli_dhcpd_options() {
917 local proto=${1}
918 assert isset proto
919 shift
920
921 local subnet_id=${1}
922 assert isset subnet_id
923 shift
924
925 local valid_options=$(dhcpd_subnet_options ${proto})
926
927 local key val
928 while [ $# -gt 0 ]; do
929 case "${1}" in
930 *=*)
931 key=$(cli_get_key ${1})
932 val=$(cli_get_val ${1})
933
934 dhcpd_subnet_option_set ${proto} ${subnet_id} ${key} ${val}
935 esac
936 done
937}
938
9111eb72
MT
939function cli_start() {
940 if cli_help_requested $@; then
941 cli_show_man network
942 exit ${EXIT_OK}
943 fi
944
945 local zones=$(zones_get $@)
946
947 local zone
948 for zone in ${zones}; do
949 zone_start ${zone} &
950 done
951
952 wait # until everything is settled
953}
954
955function cli_stop() {
956 if cli_help_requested $@; then
957 cli_show_man network
958 exit ${EXIT_OK}
959 fi
960
961 local zones=$(zones_get $@)
962
963 local zone
964 for zone in ${zones}; do
965 zone_stop ${zone} &
966 done
967
968 wait # until everything is settled
969}
970
971function cli_restart() {
972 if cli_help_requested $@; then
973 cli_show_man network
974 exit ${EXIT_OK}
975 fi
976
977 cli_stop $@
978
979 # Give the system some time to calm down
980 sleep ${TIMEOUT_RESTART}
981
982 cli_start $@
983}
984
985function cli_status() {
986 if cli_help_requested $@; then
987 cli_show_man network
988 exit ${EXIT_OK}
989 fi
990
991 # When dumping status information, the debug
992 # mode clutters the console which is not what we want.
993 # Logging on the console is disabled for a short time.
994 local log_disable_stdout=${LOG_DISABLE_STDOUT}
995 LOG_DISABLE_STDOUT="true"
996
997 local zones=$(zones_get $@)
998
999 local zone
1000 for zone in ${zones}; do
1001 zone_status ${zone}
1002 done
1003
1004 # Reset logging.
1005 LOG_DISABLE_STDOUT=${log_disable_stdout}
1006}
1007
1008function cli_reset() {
1009 if cli_help_requested $@; then
1010 cli_show_man network
1011 exit ${EXIT_OK}
1012 fi
1013
1014 warning_log "Will reset the whole network configuration!!!"
1015
1016 # Force mode is disabled by default
1017 local force=0
1018
1019 while [ $# -gt 0 ]; do
1020 case "${1}" in
1021 --force|-f)
1022 force=1
1023 ;;
1024 esac
1025 shift
1026 done
1027
1028 # If we are not running in force mode, we ask the user if he does know
1029 # what he is doing.
1030 if ! enabled force; then
1031 if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
1032 exit ${EXIT_ERROR}
1033 fi
1034 fi
1035
1036 local zone
1037 for zone in $(zones_get --all); do
1038 zone_remove ${zone}
1039 done
1040
1041 local port
1042 for port in $(ports_get --all); do
1ba6a2bb 1043 port_destroy "${port}"
9111eb72
MT
1044 done
1045
acc9efd5
MT
1046 # Flush all DNS servers.
1047 dns_server_flush
1048
9111eb72
MT
1049 # Re-run the initialization functions
1050 init_run
1051
1052 exit ${EXIT_OK}
1053}
1054
85afd775
MT
1055# Help function: will show the default man page to the user.
1056# Optionally, there are two arguments taken, the type of hook
1057# and which hook should be shown.
1058function cli_help() {
1059 local type=${1}
1060 local what=${2}
1061
1062 # Remove unknown types.
1063 if ! listmatch ${type} zone port config; then
1064 type=""
1065 fi
1066
1067 # If no arguments were given, we will show the default page.
1068 if [ -z "${type}" ]; then
1069 cli_show_man network
1070 return ${EXIT_OK}
1071 fi
1072
1073 if ! hook_exists ${type} ${what}; then
1074 error "Hook of type '${type}' and name '${what}' could not be found."
1075 exit "${EXIT_ERROR}"
1076 fi
1077
1078 hook_exec ${type} ${what} help
1079}
1080
6b34112f 1081function cli_dns_server() {
acc9efd5 1082 if cli_help_requested $@; then
6b34112f 1083 cli_show_man network-dns-server
acc9efd5
MT
1084 exit ${EXIT_OK}
1085 fi
1086
1087 # Get the command.
1088 local cmd=${1}; shift
1089 if [ -z "${cmd}" ]; then
6b34112f 1090 cli_show_man network-dns-server
acc9efd5
MT
1091 exit ${EXIT_ERROR}
1092 fi
1093
6f923dac
MT
1094 # Get the new server to process (if any).
1095 local server=${1}
1096 local priority=${2}
1097
acc9efd5
MT
1098 case "${cmd}" in
1099 list)
acc9efd5 1100 dns_server_list
e5efaa6b 1101 exit ${EXIT_OK}
acc9efd5
MT
1102 ;;
1103 add)
e5651e17
MT
1104 if dns_server_exists ${server}; then
1105 error "DNS server '${server}' already exists!"
1106 exit ${EXIT_ERROR}
1107 fi
1108
6f923dac
MT
1109 log INFO "Adding new DNS server: ${server}"
1110 dns_server_add ${server} ${priority}
acc9efd5
MT
1111 ;;
1112 remove)
e5651e17
MT
1113 if ! dns_server_exists ${server}; then
1114 error "DNS server '${server}' does not exist!"
1115 exit ${EXIT_ERROR}
1116 fi
1117
6f923dac
MT
1118 log INFO "Removing DNS server: ${server}"
1119 dns_server_remove ${server} ${priority}
acc9efd5
MT
1120 ;;
1121 update)
1122 # Just run the update afterwards.
1123 ;;
1124 *)
1125 error "No such command: ${cmd}"
1126 exit ${EXIT_ERROR}
1127 esac
1128
1129 # Update the local DNS configuration after changes have been made.
1130 dns_generate_resolvconf
6f923dac 1131 radvd_update
acc9efd5
MT
1132
1133 exit ${EXIT_OK}
1134}
1135
1848564d
MT
1136# Process the given action
1137case "${action}" in
b8357295
MT
1138 init)
1139 init_run
1140 ;;
1141
e9df08ad 1142 settings|hostname|port|device|zone|start|stop|restart|status|reset|route)
0a79ea02 1143 cli_${action} $@
1848564d
MT
1144 ;;
1145
6c07160e
MT
1146 # DHCP server configuration (automatically detects which protocol to use).
1147 dhcpv6|dhcpv4)
1148 cli_dhcpd ${action/dhcp/ip} $@
1149 ;;
1150
6b34112f
MT
1151 # DNS server configuration.
1152 dns-server)
1153 cli_dns_server $@
1154 ;;
1155
fe4555b5 1156 ""|help|--help|-h)
85afd775 1157 cli_help $@
1848564d 1158 ;;
fe4555b5 1159
1848564d 1160 *)
fe4555b5 1161 error "Invalid command given: ${action}"
de28a630 1162 cli_usage "network help"
1848564d 1163 exit ${EXIT_CONF_ERROR}
fe4555b5 1164 ;;
1848564d 1165esac
85afd775
MT
1166
1167exit ${EXIT_OK}