2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
22 # Parse the command line
23 while [ $# -gt 0 ]; do
33 [ -n "${action}" ] && break
36 .
/usr
/lib
/network
/functions
38 # Read network settings
42 if cli_help_requested $@
; then
43 cli_show_man network-settings
47 if [ -n "${1}" ]; then
49 network_settings_write
51 network_settings_print
56 if cli_help_requested $@
; then
57 cli_show_man network-device
69 local device
="${action}"
73 if ! isset device
; then
74 cli_show_man network-device
78 assert device_exists
${device}
82 cli_device_discover
${device} $@
85 device_identify
"${device}" $@
88 cli_device_monitor
"${device}" $@
91 cli_device_status
${device}
94 cli_device_serial_unlock
${device} $@
97 cli_device_send_ussd_command
"${device}" $@
100 cli_show_man network-device
109 cli_device_status
() {
113 # Disable debugging output here.
114 local log_disable_stdout
=${LOG_DISABLE_STDOUT}
115 LOG_DISABLE_STDOUT
="true"
117 # Save the type of the device for later.
118 local type=$
(device_get_type
${device})
120 cli_headline
1 "Device status: ${device}"
121 cli_print_fmt1
1 "Name" "${device}"
123 # Handle special devices
126 cli_device_status_phy
"${device}"
130 cli_device_status_serial
"${device}"
135 # Print the device status.
136 device_is_up
${device} &>/dev
/null
141 status
="${CLR_GREEN_B}UP${CLR_RESET}"
144 status
="${CLR_RED_B}DOWN${CLR_RESET}"
148 cli_print_fmt1
1 "Status" "${status}"
149 cli_print_fmt1
1 "Type" "${type}"
151 # Ethernet-compatible?
152 device_is_ethernet_compatible
"${device}" &>/dev
/null
153 cli_print_fmt1
1 "Ethernet-compatible" "$(cli_print_bool $?)"
155 cli_print_fmt1
1 "Address" "$(device_get_address ${device})"
158 # Print the link speed for ethernet devices.
159 if device_is_up
${device} &>/dev
/null
; then
160 local link
="$(device_get_link_string "${device}")"
162 cli_print_fmt1
1 "Link" "${link}"
166 cli_print_fmt1
1 "MTU" "$(device_get_mtu ${device})"
169 # Print device statistics.
170 cli_device_stats
2 ${device}
172 # Print some more information.
173 device_has_carrier
${device} &>/dev
/null
174 cli_print_fmt1
1 "Has carrier?" "$(cli_print_bool $?)"
176 device_is_promisc
${device} &>/dev
/null
177 cli_print_fmt1
1 "Promisc" "$(cli_print_bool $?)"
180 # Print all vlan devices.
181 local vlans
=$
(device_get_vlans
${device})
182 if [ -n "${vlans}" ]; then
183 cli_headline
2 "VLAN devices"
186 for vlan
in ${vlans}; do
187 cli_print
2 "* %-6s - %s" "${vlan}" "$(device_get_address ${vlan})"
194 local phy
="$(device_get_phy "${device}")"
197 cli_print_fmt1
2 "Name" "${phy}"
198 cli_print_fmt1
2 "Address" "$(phy_get_address "${phy}")"
204 # Reset the logging level.
205 LOG_DISABLE_STDOUT
=${log_disable_stdout}
208 cli_device_status_serial
() {
210 assert device_is_serial
${device}
212 serial_is_locked
${device} &>/dev
/null
215 cli_print_fmt1
1 "Locked" "$(cli_print_bool ${locked})"
218 # Cannot go on when the device is locked.
219 [ ${locked} -eq ${EXIT_TRUE} ] && return ${EXIT_OK}
221 cli_print_fmt1
1 "Manufacturer" \
222 "$(modem_get_manufacturer ${device})"
223 cli_print_fmt1
1 "Model" \
224 "$(modem_get_model ${device})"
225 cli_print_fmt1
1 "Software version" \
226 "$(modem_get_software_version ${device})"
228 if modem_is_mobile
${device}; then
229 cli_print_fmt1
1 "IMEI" \
230 "$(modem_get_device_imei ${device})"
233 cli_headline
2 "Network status"
234 modem_sim_status
${device} &>/dev
/null
235 local sim_status_code
=$?
237 local sim_status
="unknown"
238 case "${sim_status_code}" in
240 sim_status
="SIM ready"
243 sim_status
="PIN locked"
246 sim_status
="PUK locked"
249 cli_print_fmt1
2 "SIM status" "${sim_status}"
251 if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
252 cli_print_fmt1
2 "IMSI" \
253 "$(modem_get_sim_imsi ${device})"
254 cli_print_fmt1
2 "Operator" \
255 "$(modem_get_network_operator ${device})"
256 cli_print_fmt1
2 "Mode" \
257 "$(modem_get_network_mode ${device})"
258 cli_print_fmt1
2 "Signal quality" \
259 "$(modem_get_signal_quality ${device}) dBm"
261 local ber
=$
(modem_get_bit_error_rate
${device})
262 isset ber || ber
="unknown"
263 cli_print_fmt1
2 "Bit Error Rate" "${ber}"
269 cli_device_status_phy
() {
271 assert phy_exists
"${phy}"
273 local address
="$(phy_get_address "${phy}")"
274 cli_print_fmt1
1 "Address" "${address}"
277 local devices
="$(phy_get_devices "${phy}")"
278 if isset devices
; then
279 cli_headline
2 "Soft interfaces"
282 for device
in ${devices}; do
283 cli_print
2 "* %s" "${device}"
291 cli_device_discover
() {
295 # This can only be executed for ethernet (or compatible) devices
296 if ! device_is_ethernet_compatible
"${device}"; then
302 while [ $# -gt 0 ]; do
312 device_is_up
${device} && up
=1
313 device_set_up
${device}
315 enabled raw ||
echo "${device}"
320 for hook
in $
(hook_zone_get_all
); do
321 out
=$
(hook_zone_exec
${hook} discover
${device})
324 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
332 echo "${hook}: ${line}"
337 echo "${hook}: FAILED"
343 echo " ${hook} was successful."
351 echo " ${hook} failed."
359 [ "${up}" = "1" ] || device_set_down
${device}
362 cli_device_serial_unlock
() {
363 if cli_help_requested $@
; then
364 cli_show_man network-device
371 if ! device_is_serial
${device}; then
372 error
"${device} is not a serial device."
373 error
"Unlocking is only supported for serial devices."
377 # Read the current state of the SIM card.
378 modem_sim_status
${device} &>/dev
/null
379 local sim_status_code
=$?
381 # If the SIM card is already unlocked, we don't need to do anything.
382 if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
383 print
"The SIM card is already unlocked."
386 # If the SIM card is in an unknown state, we cannot do anything.
387 elif [ ${sim_status_code} -eq ${EXIT_SIM_UNKNOWN} ]; then
388 error
"The SIM card is in an unknown state."
394 local require_new_pin
="false"
397 while ! isinteger code
; do
399 case "${sim_status_code}" in
401 message
="Please enter PIN:"
404 message
="Please enter PUK:"
405 require_new_pin
="true"
410 echo -n "${message} "
412 echo # Print newline.
414 if enabled require_new_pin
; then
419 message
="Please enter a new PIN code:"
422 message
="Please confirm the new PIN code:"
426 echo -n "${message} "
428 echo # Print newline.
430 if [ -n "${new_pin}" ]; then
431 if [ "${new_pin}" != "${new_pin2}" ]; then
432 error
"The entered PIN codes did not match."
442 # Trying to unlock the SIM card.
443 modem_sim_unlock
${device} ${code} ${new_pin}
448 cli_device_send_ussd_command
() {
456 while [ $# -gt 0 ]; do
459 timeout
="$(cli_get_val "${1}")"
462 if isset
command; then
463 warning
"Unrecognized argument: ${1}"
472 assert device_is_serial
"${device}"
475 if isset timeout
; then
476 args
="${args} --timeout=${timeout}"
479 modem_ussd_send_command
"${device}" "${command}" ${args}
483 cli_device_monitor() {
487 if ! device_is_wireless "${device}"; then
488 error "This action only works with wireless devices. Exiting.
"
492 wireless_monitor "${device}"
498 for device in $(device_list); do
499 cli_device_status "${device}"
506 if cli_help_requested $@; then
513 if [ -n "${hostname}" ]; then
514 config_hostname ${hostname}
515 log INFO "Hostname was
set to
'${hostname}'.
"
516 log INFO "Changes
do only take affect after reboot.
"
520 echo "$
(config_hostname
)"
525 if cli_help_requested $@; then
526 cli_show_man network-port
533 if port_exists ${1}; then
539 edit|create|remove|up|down|status|identify)
540 port_${action} "${port}" $@
543 error "Unrecognized argument
: ${action}"
556 error "Unrecognized argument
: ${action}"
564 if cli_help_requested $@; then
565 cli_show_man network-zone
572 if zone_name_is_valid ${1}; then
592 cli_zone_port "${zone}" $@
595 cli_zone_rename "${zone}" $@
597 config|disable|down|edit|enable|identify|status|up)
598 zone_${action} ${zone} $@
601 error "Unrecognized argument
: ${action}"
602 cli_show_man network-zone
618 cli_list_hooks zone $@
621 if [ -n "${action}" ]; then
622 error "Unrecognized argument
: '${action}'"
626 cli_show_man network-zone
634 if cli_help_requested $@ || [ $# -lt 2 ]; then
635 cli_show_man network-zone-new
642 # Removes a zone either immediately, if it is currently down,
643 # or adds a tag that the removal will be done when the zone
644 # is brought down the next time.
646 if cli_help_requested $@; then
647 cli_show_man network-zone
652 assert zone_exists "${zone}"
654 if zone_is_up ${zone}; then
655 echo "Zone
'${zone}' is up and will be removed when it goes down the next
time.
"
656 zone_destroy "${zone}"
658 echo "Removing zone
'${zone}' now...
"
659 zone_destroy_now "${zone}"
666 if cli_help_requested $@; then
667 cli_show_man network-zone-port
672 assert zone_exists "${zone}"
674 if port_exists "${2}"; then
681 zone_port_edit "${zone}" "${port}" $@
684 error "Unrecognised argument
: ${action}"
694 zone_port_attach "${zone}" $@
697 zone_port_detach "${zone}" $@
700 error "Unrecognised argument
: ${action}"
710 if cli_help_requested $@; then
711 cli_show_man network-zone
719 if ! isset name; then
720 error "You need to pass a new name
"
724 if ! zone_name_is_valid "${name}"; then
725 error "Invalid new zone name
: ${name}"
729 # Check if the zone exists
730 if ! zone_exists "${zone}"; then
731 error "Zone
${zone} does not exist
"
735 # Destroyed zones cannot be renamed
736 if zone_has_destroy_tag "${zone}"; then
737 error "Zone
${zone} is about to be destroyed and cannot be renamed
"
741 # Check if a zone with the new name already exists
742 if zone_exists "${name}"; then
743 error "Zone
${name} already exists
"
748 if ! zone_rename "${zone}" "${name}"; then
749 error "Could not rename zone
${zone} to
${name}"
760 if cli_help_requested $@; then
761 cli_show_man network-zone
765 local hook_dir=$(hook_dir ${type})
768 for hook in ${hook_dir}/*; do
769 hook=$(basename ${hook})
770 if hook_exists ${type} ${hook}; then
777 if cli_help_requested $@; then
778 cli_show_man network-route
790 # Remove an existing route.
800 error "Unrecognized action
: ${action}"
801 cli_run_help network route
807 # Applying all routes.
817 if cli_help_requested $@; then
818 cli_show_man network-dhcp
827 dhcpd_edit ${proto} $@
836 dhcpd_reload ${proto}
839 cli_dhcpd_subnet ${proto} $@
842 cli_dhcpd_show ${proto} $@
845 error "Unrecognized action
: ${action}"
846 cli_run_help network dhcpvN
859 local settings=$(dhcpd_settings ${proto})
860 assert isset settings
863 dhcpd_global_settings_read ${proto}
865 cli_headline 1 "Dynamic Host Configuration Protocol Daemon
for ${proto/ip/IP}"
869 cli_headline 2 "Lease
times"
870 if isinteger VALID_LIFETIME; then
871 cli_print_fmt1 2 "Valid lifetime
" "${VALID_LIFETIME}s
"
874 if isinteger PREFERRED_LIFETIME; then
875 cli_print_fmt1 2 "Preferred lifetime
" "${PREFERRED_LIFETIME}s
"
881 cli_print_fmt1 1 "Authoritative
" $(cli_print_enabled AUTHORITATIVE)
884 cli_headline 2 "Lease
times"
885 cli_print_fmt1 2 "Default lease
time" "${DEFAULT_LEASE_TIME}s
"
886 cli_print_fmt1 2 "Max. lease
time" "${MAX_LEASE_TIME}s
"
888 if isset MIN_LEASE_TIME; then
889 cli_print_fmt1 2 "Min. lease
time" "${MIN_LEASE_TIME}s
"
898 dhcpd_global_options_read ${proto} ${subnet_id}
900 # Print the options if any.
901 if [ ${#options[*]} -gt 0 ]; then
902 cli_headline 2 "Options
"
905 for option in $(dhcpd_options ${proto}); do
906 [ -n "${options[${option}]}" ] || continue
909 "${option}" "${options[${option}]}"
915 local subnets=$(dhcpd_subnet_list ${proto})
916 if [ -n "${subnets}" ]; then
917 cli_headline 2 "Subnets
"
919 for subnet_id in ${subnets}; do
920 cli_dhcpd_subnet_show ${proto} ${subnet_id} 2
929 if cli_help_requested $@; then
930 cli_show_man network-dhcp-subnet
939 dhcpd_subnet_new ${proto} $@
942 dhcpd_subnet_remove ${proto} $@
945 local subnet_id=${action}
947 if ! dhcpd_subnet_exists ${proto} ${subnet_id}; then
948 error "The given subnet with ID
${subnet_id} does not exist.
"
958 dhcpd_subnet_edit ${proto} ${subnet_id} $@
961 if [ ${ret} -eq ${EXIT_OK} ]; then
962 dhcpd_reload ${proto}
967 cli_dhcpd_subnet_range ${proto} ${subnet_id} $@
971 cli_dhcpd_subnet_show ${proto} ${subnet_id} $@
975 cli_dhcpd_subnet_options ${proto} ${subnet_id} $@
979 error "Unrecognized action
: ${action}"
980 cli_run_help network dhcpvN subnet
987 for subnet_id in $(dhcpd_subnet_list ${proto}); do
988 cli_dhcpd_subnet_show ${proto} ${subnet_id}
992 error "Unrecognized action
: ${action}"
993 cli_run_help network dhcpvN subnet
1002 cli_dhcpd_subnet_range() {
1007 local subnet_id=${1}
1008 assert isset subnet_id
1016 dhcpd_subnet_range_new ${proto} ${subnet_id} $@
1020 dhcpd_subnet_range_remove ${proto} ${subnet_id} $@
1024 error "Unrecognized action
: ${action}"
1025 cli_run_help network dhcpvN subnet range
1031 cli_dhcpd_subnet_show() {
1035 local subnet_id=${2}
1036 assert isset subnet_id
1039 isset level || level=0
1041 local $(dhcpd_subnet_settings ${proto})
1043 # Read in configuration settings.
1044 dhcpd_subnet_read ${proto} ${subnet_id}
1046 cli_headline $(( ${level} + 1 )) \
1047 "DHCP
${proto/ip/} subnet declaration
#${subnet_id}"
1048 cli_print_fmt1 $
(( ${level} + 1 )) \
1049 "Subnet" "${ADDRESS}/${PREFIX}"
1054 dhcpd_subnet_options_read
"${proto}" "${subnet_id}"
1056 # Print the options if any.
1057 if [ ${#options[*]} -gt 0 ]; then
1058 cli_headline $
(( ${level} + 2 )) "Options"
1061 for option
in $
(dhcpd_subnet_options_list
${proto}); do
1062 [ -n "${options[${option}]}" ] ||
continue
1064 cli_print_fmt1 $
(( ${level} + 2 )) \
1065 "${option}" "${options[${option}]}"
1071 cli_headline $
(( ${level} + 2 )) "Ranges"
1073 local ranges
=$
(dhcpd_subnet_range_list
${proto} ${subnet_id})
1074 if isset ranges
; then
1075 local range_id $
(dhcpd_subnet_range_settings
${proto})
1076 for range_id
in ${ranges}; do
1077 dhcpd_subnet_range_read
${proto} ${subnet_id} ${range_id}
1079 cli_print $
(( ${level} + 2 )) \
1080 "#%d: %s - %s" ${range_id} ${START} ${END}
1083 cli_print $
(( ${level} + 2 )) "No ranges have been defined."
1089 cli_dhcpd_subnet_options
() {
1094 local subnet_id
=${1}
1095 assert isset subnet_id
1099 while [ $# -gt 0 ]; do
1102 key
=$
(cli_get_key
${1})
1103 val
=$
(cli_get_val
${1})
1105 dhcpd_subnet_option_set
${proto} ${subnet_id} ${key} ${val}
1111 if cli_help_requested $@
; then
1112 cli_show_man network
1116 local zones
=$
(zones_get $@
)
1119 for zone
in ${zones}; do
1120 zone_start
${zone} &
1123 wait # until everything is settled
1127 if cli_help_requested $@
; then
1128 cli_show_man network
1132 local zones
=$
(zones_get $@
)
1135 for zone
in ${zones}; do
1139 wait # until everything is settled
1143 if cli_help_requested $@
; then
1144 cli_show_man network
1150 # Give the system some time to calm down
1151 sleep ${TIMEOUT_RESTART}
1157 if cli_help_requested $@
; then
1158 cli_show_man network
1162 # When dumping status information, the debug
1163 # mode clutters the console which is not what we want.
1164 # Logging on the console is disabled for a short time.
1165 local log_disable_stdout
=${LOG_DISABLE_STDOUT}
1166 LOG_DISABLE_STDOUT
="true"
1168 local zones
=$
(zones_get $@
)
1171 for zone
in ${zones}; do
1176 LOG_DISABLE_STDOUT
=${log_disable_stdout}
1180 if cli_help_requested $@
; then
1181 cli_show_man network
1185 warning_log
"Will reset the whole network configuration!!!"
1187 # Force mode is disabled by default
1190 while [ $# -gt 0 ]; do
1199 # If we are not running in force mode, we ask the user if he does know
1201 if ! enabled force
; then
1202 if ! cli_yesno
"Do you really want to reset the whole network configuration?"; then
1208 for zone
in $
(zones_get
--all); do
1209 zone_destroy_now
"${zone}"
1213 for port
in $
(ports_get
--all); do
1214 port_destroy
"${port}"
1217 # Flush all DNS servers.
1220 # Re-run the initialization functions
1226 # Help function: will show the default man page to the user.
1227 # Optionally, there are two arguments taken, the type of hook
1228 # and which hook should be shown.
1233 # Remove unknown types.
1234 if ! listmatch
${type} zone port config
; then
1238 # If no arguments were given, we will show the default page.
1239 if [ -z "${type}" ]; then
1240 cli_show_man network
1244 if ! hook_exists
${type} ${what}; then
1245 error
"Hook of type '${type}' and name '${what}' could not be found."
1246 exit "${EXIT_ERROR}"
1249 hook_exec
${type} ${what} help
1253 if cli_help_requested $@
; then
1254 cli_show_man network-dns-server
1259 local cmd
=${1}; shift
1260 if [ -z "${cmd}" ]; then
1261 cli_show_man network-dns-server
1265 # Get the new server to process (if any).
1275 if dns_server_exists
${server}; then
1276 error
"DNS server '${server}' already exists!"
1280 log INFO
"Adding new DNS server: ${server}"
1281 dns_server_add
${server} ${priority}
1284 if ! dns_server_exists
${server}; then
1285 error
"DNS server '${server}' does not exist!"
1289 log INFO
"Removing DNS server: ${server}"
1290 dns_server_remove
${server} ${priority}
1293 # Just run the update afterwards.
1296 error
"No such command: ${cmd}"
1300 # Update the local DNS configuration after changes have been made.
1318 list-dhcpd-ranges-of-subnet
)
1319 dhcpd_subnet_range_list $@
1321 list-dhcpd-settings
)
1322 dhcpd_global_settings_list $@
1325 dhcpd_subnet_list $@
1327 list-dhcpd-subnet-options
)
1328 dhcpd_subnet_options_list $@
1346 network_settings_list
1352 error
"No such command: ${cmd}"
1360 # Process the given action
1366 settings|hostname|port|device|zone|start|stop|restart|status|
reset|route
)
1370 # DHCP server configuration (automatically detects which protocol to use).
1372 cli_dhcpd
${action/dhcp/ip} $@
1375 # DNS server configuration.
1389 error
"Invalid command given: ${action}"
1390 cli_usage
"network help"
1391 exit ${EXIT_CONF_ERROR}