]> git.ipfire.org Git - people/ms/network.git/blame - src/network
Move cli_device_status_phy() to functions.phy
[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
1c6a4e30 41cli_settings() {
2212045f 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
2212045f 48 network_settings_set "$@"
e9df08ad 49 network_settings_write
9111eb72 50 else
e9df08ad 51 network_settings_print
9111eb72
MT
52 fi
53}
54
1c6a4e30 55cli_device() {
2212045f 56 if cli_help_requested "$@"; then
6c74a64c
MT
57 cli_show_man network-device
58 exit ${EXIT_OK}
59 fi
60
f90dd58c
MT
61 local action="${1}"
62 shift
ec63256a 63
9111eb72 64 case "${action}" in
f90dd58c 65 list)
2212045f 66 cli_device_list "$@"
5cf0edf9 67 ;;
9111eb72 68 *)
f90dd58c
MT
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)
2212045f 82 cli_device_discover ${device} "$@"
f90dd58c 83 ;;
f5ee091e 84 identify)
2212045f 85 device_identify "${device}" "$@"
f5ee091e 86 ;;
f90dd58c 87 monitor)
2212045f 88 cli_device_monitor "${device}" "$@"
f90dd58c
MT
89 ;;
90 status)
91 cli_device_status ${device}
92 ;;
93 unlock)
2212045f 94 cli_device_serial_unlock ${device} "$@"
f90dd58c
MT
95 ;;
96 ussd)
2212045f 97 cli_device_send_ussd_command "${device}" "$@"
f90dd58c
MT
98 ;;
99 *)
100 cli_show_man network-device
101 ;;
102 esac
9111eb72
MT
103 ;;
104 esac
ec63256a
MT
105
106 return ${EXIT_OK}
107}
108
1c6a4e30 109cli_device_status() {
f90dd58c
MT
110 local device="${1}"
111 assert isset device
ec63256a 112
6c74a64c
MT
113 # Disable debugging output here.
114 local log_disable_stdout=${LOG_DISABLE_STDOUT}
115 LOG_DISABLE_STDOUT="true"
116
ec63256a
MT
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
f90dd58c
MT
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
6c74a64c 134
ec63256a
MT
135 # Print the device status.
136 device_is_up ${device} &>/dev/null
137 local status=$?
138
139 case "${status}" in
140 ${EXIT_TRUE})
fcbf6823 141 status="${CLR_GREEN_B}UP${CLR_RESET}"
ec63256a
MT
142 ;;
143 ${EXIT_FALSE})
fcbf6823 144 status="${CLR_RED_B}DOWN${CLR_RESET}"
ec63256a
MT
145 ;;
146 esac
147
148 cli_print_fmt1 1 "Status" "${status}"
149 cli_print_fmt1 1 "Type" "${type}"
a4f7ad26 150
4ad5981f
MT
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
a4f7ad26
MT
157 # Ethernet-compatible?
158 device_is_ethernet_compatible "${device}" &>/dev/null
159 cli_print_fmt1 1 "Ethernet-compatible" "$(cli_print_bool $?)"
160
ec63256a
MT
161 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
162 cli_space
163
164 # Print the link speed for ethernet devices.
245dffc9 165 if device_is_up ${device} &>/dev/null; then
657540d8
MT
166 local link="$(device_get_link_string "${device}")"
167 if isset link; then
168 cli_print_fmt1 1 "Link" "${link}"
169 fi
245dffc9 170 fi
ec63256a
MT
171
172 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
173 cli_space
174
3cb2fc42
MT
175 # Print device statistics.
176 cli_device_stats 2 ${device}
ec63256a
MT
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 $?)"
6529dfaa
MT
184
185 # Supports multiqueue?
186 if device_supports_multiqueue ${device}; then
6655d1bf 187 cli_print_fmt1 1 "Multiqueue" "Supported"
6529dfaa 188 fi
ec63256a
MT
189 cli_space
190
6529dfaa
MT
191 cli_device_show_queues 2 ${device}
192
7951525a
MT
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})"
ec63256a
MT
201 done
202 cli_space
203 fi
204
f90dd58c
MT
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
6c74a64c
MT
217 # Reset the logging level.
218 LOG_DISABLE_STDOUT=${log_disable_stdout}
219}
220
1c6a4e30 221cli_device_status_serial() {
6c74a64c
MT
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
36aeb387
MT
241 # Mobile
242 if modem_is_mobile "${device}"; then
6c74a64c
MT
243 cli_print_fmt1 1 "IMEI" \
244 "$(modem_get_device_imei ${device})"
6c74a64c
MT
245 fi
246 cli_space
36aeb387
MT
247
248 if modem_is_mobile "${device}"; then
249 modem_mobile_network_status "${device}" 2
250 cli_space
251 fi
9111eb72
MT
252}
253
1c6a4e30 254cli_device_discover() {
9111eb72
MT
255 local device=${1}
256 shift
257
5dfc94a8
MT
258 # This can only be executed for ethernet (or compatible) devices
259 if ! device_is_ethernet_compatible "${device}"; then
9111eb72
MT
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
1c6a4e30 325cli_device_serial_unlock() {
2212045f 326 if cli_help_requested "$@"; then
6c74a64c
MT
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
1c6a4e30 411cli_device_send_ussd_command() {
5cf0edf9
MT
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
1c6a4e30 446cli_device_monitor() {
5a38ea84
MT
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
1c6a4e30 459cli_device_list() {
f90dd58c
MT
460 local device
461 for device in $(device_list); do
462 cli_device_status "${device}"
463 done
464
465 exit ${EXIT_OK}
466}
467
1c6a4e30 468cli_hostname() {
2212045f 469 if cli_help_requested "$@"; then
9111eb72
MT
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
1c6a4e30 487cli_port() {
2212045f 488 if cli_help_requested "$@"; then
9111eb72
MT
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
9111eb72 501 case "${action}" in
f5ee091e 502 edit|create|remove|up|down|status|identify)
2212045f 503 port_${action} "${port}" "$@"
9111eb72 504 ;;
8de0a49f 505 color)
2212045f 506 color_cli "port" "${port}" "$@"
8de0a49f 507 ;;
ba348e4b 508 description)
2212045f 509 description_cli "port" "${port}" "$@"
ba348e4b 510 ;;
9111eb72
MT
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
1ba6a2bb 521 new|destroy)
2212045f 522 port_${action} "$@"
9111eb72
MT
523 ;;
524 *)
525 error "Unrecognized argument: ${action}"
526 exit ${EXIT_ERROR}
527 ;;
528 esac
529 fi
530}
531
1c6a4e30 532cli_zone() {
2212045f 533 if cli_help_requested "$@"; then
9111eb72
MT
534 cli_show_man network-zone
535 exit ${EXIT_OK}
536 fi
537
538 local action
539 local zone
540
9ea45130 541 if zone_exists ${1}; then
9111eb72
MT
542 zone=${1}
543 action=${2}
544 shift 2
545
546 # Action aliases
547 case "${action}" in
e6fd23fd 548 start|reload)
9111eb72
MT
549 action="up"
550 ;;
551 stop)
552 action="down"
553 ;;
554 show)
555 action="status"
556 ;;
557 esac
558
559 case "${action}" in
ac694a6a 560 port)
2212045f 561 cli_zone_port "${zone}" "$@"
ac694a6a 562 ;;
2bb20bbd 563 rename)
2212045f 564 cli_zone_rename "${zone}" "$@"
2bb20bbd 565 ;;
f5ee091e 566 config|disable|down|edit|enable|identify|status|up)
2212045f 567 zone_${action} ${zone} "$@"
9111eb72 568 ;;
8de0a49f 569 color)
2212045f 570 color_cli "zone" "${zone}" "$@"
8de0a49f 571 ;;
ba348e4b 572 description)
2212045f 573 description_cli "zone" ${zone} "$@"
ba348e4b 574 ;;
9111eb72
MT
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
cf0fc8ab 586 new)
2212045f 587 cli_zone_new "$@"
9111eb72 588 ;;
cf0fc8ab 589 destroy)
2212045f 590 cli_zone_destroy "$@"
9111eb72 591 ;;
9111eb72
MT
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
2c37b88b 605cli_zone_new() {
2212045f 606 if cli_help_requested "$@" || [ $# -lt 2 ]; then
2c37b88b
MT
607 cli_show_man network-zone-new
608 exit ${EXIT_OK}
609 fi
610
2212045f 611 zone_new "$@"
2c37b88b
MT
612}
613
9111eb72
MT
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.
1c6a4e30 617cli_zone_destroy() {
2212045f 618 if cli_help_requested "$@"; then
9111eb72
MT
619 cli_show_man network-zone
620 exit ${EXIT_OK}
621 fi
622
cf0fc8ab 623 local zone="${1}"
9111eb72 624
165d0d94
MT
625 # Check if the zone exists
626 if ! zone_exists "${zone}"; then
627 error "Zone '${zone}' does not exist"
628 return ${EXIT_ERROR}
9111eb72
MT
629 fi
630
165d0d94
MT
631 echo "Removing zone '${zone}'..."
632 zone_destroy "${zone}" || exit $?
9111eb72
MT
633}
634
1c6a4e30 635cli_zone_port() {
2212045f 636 if cli_help_requested "$@"; then
ac694a6a
MT
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)
2212045f 651 zone_port_edit "${zone}" "${port}" "$@"
ac694a6a
MT
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)
2212045f 664 zone_port_attach "${zone}" "$@"
ac694a6a
MT
665 ;;
666 detach)
2212045f 667 zone_port_detach "${zone}" "$@"
ac694a6a
MT
668 ;;
669 *)
670 error "Unrecognised argument: ${action}"
671 exit ${EXIT_ERROR}
672 ;;
673 esac
674 fi
675
676 exit ${EXIT_OK}
677}
678
2bb20bbd 679cli_zone_rename() {
2212045f 680 if cli_help_requested "$@"; then
2bb20bbd
SS
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
2bb20bbd
SS
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
1c6a4e30 720cli_list_hooks() {
9111eb72
MT
721 local type=${1}
722 shift
723
2212045f 724 if cli_help_requested "$@"; then
9111eb72
MT
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
1c6a4e30 740cli_dhcpd() {
6c07160e
MT
741 local proto=${1}
742 shift
743
2212045f 744 if cli_help_requested "$@"; then
6c07160e
MT
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)
2212045f 754 dhcpd_edit ${proto} "$@"
6c07160e
MT
755 ;;
756 start)
757 dhcpd_start ${proto}
9ff233f0
MT
758
759 # Make this permanent
760 dhcpd_enable ${proto}
6c07160e
MT
761 ;;
762 stop)
763 dhcpd_stop ${proto}
9ff233f0
MT
764
765 # Make this permanent
766 dhcpd_disable ${proto}
6c07160e
MT
767 ;;
768 restart|reload)
769 dhcpd_reload ${proto}
770 ;;
771 subnet)
2212045f 772 cli_dhcpd_subnet ${proto} "$@"
6c07160e
MT
773 ;;
774 show|"")
2212045f 775 cli_dhcpd_show ${proto} "$@"
6c07160e
MT
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
1c6a4e30 788cli_dhcpd_show() {
6c07160e
MT
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
d13929d4 804 cli_print_fmt1 2 "Valid lifetime" "$(format_time ${VALID_LIFETIME})"
6c07160e
MT
805 fi
806
807 if isinteger PREFERRED_LIFETIME; then
d13929d4 808 cli_print_fmt1 2 "Preferred lifetime" "$(format_time ${PREFERRED_LIFETIME})"
6c07160e
MT
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"
d13929d4
MT
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})"
6c07160e
MT
820
821 if isset MIN_LEASE_TIME; then
d13929d4 822 cli_print_fmt1 2 "Min. lease time" "$(format_time ${MIN_LEASE_TIME})"
6c07160e
MT
823 fi
824
825 cli_space
826 ;;
827 esac
828
829 # Read the options.
830 local -A options
6a9a9ea6 831 dhcpd_global_options_read ${proto}
6c07160e
MT
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"
6a9a9ea6
MT
851 local subnet
852 for subnet in ${subnets}; do
853 cli_dhcpd_subnet_show ${proto} ${subnet} 2
6c07160e
MT
854 done
855 fi
856}
857
1c6a4e30 858cli_dhcpd_subnet() {
6a9a9ea6
MT
859 assert [ $# -ge 1 ]
860
6c07160e
MT
861 local proto=${1}
862 shift
863
2212045f 864 if cli_help_requested "$@"; then
6c07160e
MT
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)
2212045f 874 dhcpd_subnet_new ${proto} "$@"
6c07160e
MT
875 ;;
876 remove)
2212045f 877 dhcpd_subnet_remove ${proto} "$@"
6c07160e 878 ;;
6a9a9ea6
MT
879 *:*/*|*.*.*.*/*)
880 local subnet=${action}
6c07160e 881
6a9a9ea6
MT
882 if ! dhcpd_subnet_exists ${proto} ${subnet}; then
883 error "Subnet ${subnet} does not exist"
6c07160e
MT
884 return ${EXIT_ERROR}
885 fi
886
887 # Update the action.
888 action=${1}
889 shift
890
891 case "${action}" in
892 edit)
2212045f 893 dhcpd_subnet_edit ${proto} ${subnet} "$@"
6c07160e
MT
894 local ret=$?
895
896 if [ ${ret} -eq ${EXIT_OK} ]; then
897 dhcpd_reload ${proto}
898 fi
899 exit ${ret}
900 ;;
901 range)
2212045f 902 cli_dhcpd_subnet_range ${proto} ${subnet} "$@"
6c07160e
MT
903 exit $?
904 ;;
905 show)
2212045f 906 cli_dhcpd_subnet_show ${proto} ${subnet} "$@"
6c07160e
MT
907 exit $?
908 ;;
909 options)
2212045f 910 cli_dhcpd_subnet_options ${proto} ${subnet} "$@"
6c07160e
MT
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)
6a9a9ea6
MT
921 local subnet
922 for subnet in $(dhcpd_subnet_list ${proto}); do
923 cli_dhcpd_subnet_show ${proto} ${subnet}
6c07160e
MT
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
1c6a4e30 937cli_dhcpd_subnet_range() {
6a9a9ea6 938 assert [ $# -ge 2 ]
6c07160e 939
6a9a9ea6
MT
940 local proto=${1}
941 local subnet=${2}
942 local action=${3}
943 shift 3
6c07160e
MT
944
945 case "${action}" in
946 new)
2212045f 947 dhcpd_subnet_range_new ${proto} ${subnet} "$@" || exit ${EXIT_ERROR}
6c07160e
MT
948 ;;
949 remove)
2212045f 950 dhcpd_subnet_range_remove ${proto} ${subnet} "$@" || exit ${EXIT_ERROR}
6c07160e
MT
951 ;;
952 *)
953 error "Unrecognized action: ${action}"
954 cli_run_help network dhcpvN subnet range
955 exit ${EXIT_ERROR}
956 ;;
957 esac
90b63fca
MT
958
959 dhcpd_reload ${proto}
960 return ${EXIT_OK}
6c07160e
MT
961}
962
1c6a4e30 963cli_dhcpd_subnet_show() {
6a9a9ea6 964 assert [ $# -ge 2 -a $# -le 3 ]
6c07160e 965
6a9a9ea6
MT
966 local proto=${1}
967 local subnet=${2}
6c07160e
MT
968
969 local level=${3}
970 isset level || level=0
971
972 local $(dhcpd_subnet_settings ${proto})
973
974 # Read in configuration settings.
6a9a9ea6 975 dhcpd_subnet_read ${proto} ${subnet}
6c07160e 976
94784eb9 977 cli_headline $(( ${level} + 1 )) "DHCP Subnet Declaration"
6c07160e
MT
978 cli_print_fmt1 $(( ${level} + 1 )) \
979 "Subnet" "${ADDRESS}/${PREFIX}"
980 cli_space
981
982 # Read the options.
983 local -A options
6a9a9ea6 984 dhcpd_subnet_options_read "${proto}" "${subnet}"
6c07160e
MT
985
986 # Print the options if any.
987 if [ ${#options[*]} -gt 0 ]; then
988 cli_headline $(( ${level} + 2 )) "Options"
989
990 local option
cc02f6be 991 for option in $(dhcpd_subnet_options_list ${proto}); do
6c07160e
MT
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
6a9a9ea6 1003 local ranges=$(dhcpd_subnet_range_list ${proto} ${subnet})
6c07160e 1004 if isset ranges; then
f3ac1159
MT
1005 local range $(dhcpd_subnet_range_settings ${proto})
1006 for range in ${ranges}; do
6a9a9ea6 1007 dhcpd_subnet_range_read ${proto} ${subnet} ${range}
6c07160e 1008
f3ac1159 1009 cli_print $(( ${level} + 2 )) "%s - %s" ${START} ${END}
6c07160e
MT
1010 done
1011 else
1012 cli_print $(( ${level} + 2 )) "No ranges have been defined."
1013 fi
1014
1015 cli_space
1016}
1017
1c6a4e30 1018cli_dhcpd_subnet_options() {
de21f838 1019 assert [ $# -ge 2 ]
6c07160e 1020
6a9a9ea6
MT
1021 local proto=${1}
1022 local subnet=${2}
6c07160e 1023
6c07160e
MT
1024 local key val
1025 while [ $# -gt 0 ]; do
1026 case "${1}" in
1027 *=*)
1028 key=$(cli_get_key ${1})
2212045f 1029 val=$(cli_get_val "${1}")
6c07160e 1030
6a9a9ea6 1031 dhcpd_subnet_option_set ${proto} ${subnet} ${key} ${val}
6c07160e 1032 esac
de21f838 1033 shift
6c07160e
MT
1034 done
1035}
1036
1c6a4e30 1037cli_start() {
2212045f 1038 if cli_help_requested "$@"; then
9111eb72
MT
1039 cli_show_man network
1040 exit ${EXIT_OK}
1041 fi
1042
2212045f 1043 local zones=$(zones_get "$@")
9111eb72
MT
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
1c6a4e30 1053cli_stop() {
2212045f 1054 if cli_help_requested "$@"; then
9111eb72
MT
1055 cli_show_man network
1056 exit ${EXIT_OK}
1057 fi
1058
2212045f 1059 local zones=$(zones_get "$@")
9111eb72
MT
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
1c6a4e30 1069cli_restart() {
2212045f 1070 if cli_help_requested "$@"; then
9111eb72
MT
1071 cli_show_man network
1072 exit ${EXIT_OK}
1073 fi
1074
2212045f 1075 cli_stop "$@"
9111eb72
MT
1076
1077 # Give the system some time to calm down
1078 sleep ${TIMEOUT_RESTART}
1079
2212045f 1080 cli_start "$@"
9111eb72
MT
1081}
1082
1c6a4e30 1083cli_status() {
2212045f 1084 if cli_help_requested "$@"; then
9111eb72
MT
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
3a3eeb1d 1095 local arguments=( $@ )
9111eb72 1096
3a3eeb1d
MT
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
9111eb72
MT
1129 done
1130
1131 # Reset logging.
1132 LOG_DISABLE_STDOUT=${log_disable_stdout}
1133}
1134
1c6a4e30 1135cli_reset() {
2212045f 1136 if cli_help_requested "$@"; then
9111eb72
MT
1137 cli_show_man network
1138 exit ${EXIT_OK}
1139 fi
1140
1141 warning_log "Will reset the whole network configuration!!!"
9111eb72
MT
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
f5f9014d
MT
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
ecac9e2b
JS
1168 local pool
1169 for pool in $(ipsec_list_pools); do
1170 ipsec_pool_destroy "${pool}"
1171 done
1172
f5f9014d
MT
1173 # Stop strongswan
1174 ipsec_strongswan_autostart
1175
3cac4fcd
MT
1176 # Destroy all user-defined security policies
1177 local secpol
1178 for secpol in $(vpn_security_policies_list_user); do
f05fa5d4 1179 vpn_security_policies_destroy "${secpol}"
3cac4fcd
MT
1180 done
1181
9111eb72 1182 local zone
8707df4b 1183 for zone in $(zones_get_all); do
36e1f715 1184 zone_destroy "${zone}"
9111eb72
MT
1185 done
1186
1187 local port
8707df4b 1188 for port in $(ports_get_all); do
1ba6a2bb 1189 port_destroy "${port}"
9111eb72
MT
1190 done
1191
acc9efd5
MT
1192 # Flush all DNS servers.
1193 dns_server_flush
1194
76334a38
MT
1195 # Trigger udev to re-add all physical network devices
1196 cmd_quiet udevadm trigger --action=add --subsystem-match=net
1197
9111eb72
MT
1198 exit ${EXIT_OK}
1199}
1200
85afd775
MT
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.
1c6a4e30 1204cli_help() {
6b7654eb
MT
1205 local cmd=${1}
1206 shift
85afd775 1207
6b7654eb
MT
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
85afd775 1218
6b7654eb
MT
1219 if ! hook_exists ${type} ${hook}; then
1220 error "No hook with name '${hook}' could be found"
1221 exit "${EXIT_ERROR}"
1222 fi
85afd775 1223
6b7654eb
MT
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
85afd775 1233
6b7654eb 1234 return ${EXIT_ERROR}
85afd775
MT
1235}
1236
1c6a4e30 1237cli_dns_server() {
2212045f 1238 if cli_help_requested "$@"; then
6b34112f 1239 cli_show_man network-dns-server
acc9efd5
MT
1240 exit ${EXIT_OK}
1241 fi
1242
1243 # Get the command.
1244 local cmd=${1}; shift
1245 if [ -z "${cmd}" ]; then
6b34112f 1246 cli_show_man network-dns-server
acc9efd5
MT
1247 exit ${EXIT_ERROR}
1248 fi
1249
6f923dac
MT
1250 # Get the new server to process (if any).
1251 local server=${1}
1252 local priority=${2}
1253
acc9efd5
MT
1254 case "${cmd}" in
1255 list)
8d1d2745 1256 dns_server_show
e5efaa6b 1257 exit ${EXIT_OK}
acc9efd5
MT
1258 ;;
1259 add)
e5651e17
MT
1260 if dns_server_exists ${server}; then
1261 error "DNS server '${server}' already exists!"
1262 exit ${EXIT_ERROR}
1263 fi
1264
6f923dac
MT
1265 log INFO "Adding new DNS server: ${server}"
1266 dns_server_add ${server} ${priority}
acc9efd5
MT
1267 ;;
1268 remove)
e5651e17
MT
1269 if ! dns_server_exists ${server}; then
1270 error "DNS server '${server}' does not exist!"
1271 exit ${EXIT_ERROR}
1272 fi
1273
6f923dac
MT
1274 log INFO "Removing DNS server: ${server}"
1275 dns_server_remove ${server} ${priority}
acc9efd5
MT
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.
bf98f6fc 1286 dns_server_update
acc9efd5
MT
1287
1288 exit ${EXIT_OK}
1289}
1290
1c6a4e30 1291cli_raw() {
bae37360
MT
1292 local cmd="${1}"
1293 assert isset cmd
1294 shift
1295
1296 case "${cmd}" in
c041b631
MT
1297 db-dump)
1298 db_dump
1299 ;;
e8253eba
JS
1300 device-get-by-mac-address)
1301 device_get_by_mac_address "$@"
1302 ;;
ac7ef1f9 1303 ipsec-connection-exists)
2212045f 1304 ipsec_connection_exists "$@"
ac7ef1f9 1305 ;;
bae37360
MT
1306 list-devices)
1307 device_list
1308 ;;
cc02f6be 1309 list-dhcpd-ranges-of-subnet)
2212045f 1310 dhcpd_subnet_range_list "$@"
cc02f6be
MT
1311 ;;
1312 list-dhcpd-settings)
2212045f 1313 dhcpd_global_settings_list "$@"
cc02f6be
MT
1314 ;;
1315 list-dhcpd-subnets)
2212045f 1316 dhcpd_subnet_list "$@"
cc02f6be
MT
1317 ;;
1318 list-dhcpd-subnet-options)
2212045f 1319 dhcpd_subnet_options_list "$@"
cc02f6be 1320 ;;
8d1d2745
MT
1321 list-dns-servers)
1322 dns_server_list
1323 ;;
bae37360
MT
1324 list-free-ports)
1325 port_list_free
1326 ;;
1327 list-hooks)
2212045f 1328 hook_list "$@"
bae37360 1329 ;;
c87851b7
JS
1330 list-ipsec-connections)
1331 ipsec_list_connections
1332 ;;
bae37360
MT
1333 list-ports)
1334 port_list
1335 ;;
1336 list-ports-of-zone)
2212045f 1337 zone_get_ports "$@"
bae37360 1338 ;;
1d9c63f7
JS
1339 list-vpn-security-policies-all)
1340 vpn_security_policies_list_all
1341 ;;
bae37360
MT
1342 list-settings)
1343 network_settings_list
1344 ;;
1345 list-zones)
1346 zones_get_all
1347 ;;
1ca5fe9f
JS
1348 list-next-free-zones)
1349 zones_get_next_free
1350 ;;
9da14216 1351 list-zone-config-ids)
2212045f 1352 zone_config_list_ids "$@"
9da14216 1353 ;;
a8be80ca 1354 list-zone-config-hids)
2212045f 1355 zone_config_list_hids "$@"
a8be80ca 1356 ;;
51408ef7 1357 vpn-security-policy-exists)
2212045f 1358 vpn_security_policy_exists "$@"
51408ef7 1359 ;;
1ca5fe9f 1360 zone-name-is-valid)
2212045f 1361 zone_name_is_valid "$@"
1ca5fe9f 1362 ;;
7deab7cc 1363 zone-config-id-is-valid)
2212045f 1364 zone_config_id_is_valid "$@"
7deab7cc 1365 ;;
fbba9abf 1366 zone-config-hid-is-valid)
2212045f 1367 zone_config_hid_is_valid "$@"
fbba9abf 1368 ;;
bae37360
MT
1369 *)
1370 error "No such command: ${cmd}"
1371 exit ${EXIT_ERROR}
1372 ;;
1373 esac
1374
1375 exit ${EXIT_OK}
1376}
1377
1848564d
MT
1378# Process the given action
1379case "${action}" in
b8357295 1380 init)
02a397d3
MT
1381 # Update resolv.conf(5) when initializing the network
1382 dns_generate_resolvconf
1383
eb6b47dc
MT
1384 # Update bird configuration
1385 bird_generate_config
1386
02a397d3
MT
1387 # Also execute all triggers
1388 triggers_execute_all "init"
b8357295
MT
1389 ;;
1390
47feaba1 1391 settings|hostname|port|device|zone|start|stop|restart|status|reset|route|vpn|wireless)
2212045f 1392 cli_${action} "$@"
1848564d
MT
1393 ;;
1394
6c07160e
MT
1395 # DHCP server configuration (automatically detects which protocol to use).
1396 dhcpv6|dhcpv4)
2212045f 1397 cli_dhcpd ${action/dhcp/ip} "$@"
6c07160e
MT
1398 ;;
1399
6b34112f
MT
1400 # DNS server configuration.
1401 dns-server)
2212045f 1402 cli_dns_server "$@"
6b34112f
MT
1403 ;;
1404
fe4555b5 1405 ""|help|--help|-h)
2212045f 1406 cli_help "$@"
1848564d 1407 ;;
fe4555b5 1408
bae37360 1409 raw)
2212045f 1410 cli_raw "$@"
bae37360
MT
1411 ;;
1412
1848564d 1413 *)
fe4555b5 1414 error "Invalid command given: ${action}"
de28a630 1415 cli_usage "network help"
1848564d 1416 exit ${EXIT_CONF_ERROR}
fe4555b5 1417 ;;
1848564d 1418esac
85afd775
MT
1419
1420exit ${EXIT_OK}