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