]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/configs/ipv4-static
ipv4-static: create hook_parse_cmdline function
[people/stevee/network.git] / src / hooks / configs / ipv4-static
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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
22 . /usr/lib/network/header-config
23
24 HOOK_MANPAGE="network-config-ipv4-static"
25
26 HOOK_CONFIG_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
27
28 hook_check_config_settings() {
29 assert isset ADDRESS
30 assert isinteger PREFIX
31
32 if [ ${PREFIX} -gt 30 ]; then
33 error "PREFIX is greater than 30."
34 exit ${EXIT_ERROR}
35 fi
36 }
37
38 hook_parse_cmdline() {
39 local arg
40
41 while read -r arg; do
42 local key="$(cli_get_key "${arg}")"
43 local val="$(cli_get_val "${arg}")"
44
45 case "${key}" in
46 address)
47 if ! ipv4_is_valid "${val}"; then
48 error "Invalid IPv4 address: ${val}"
49 exit ${EXIT_CONF_ERROR}
50 fi
51
52 ADDRESS="${val}"
53 ;;
54
55 prefix)
56 if ! ipv4_prefix_is_valid "${val}"; then
57 error "Invalid IPv4 prefix: ${val}"
58 exit ${EXIT_CONF_ERROR}
59 fi
60
61 PREFIX="${val}"
62 ;;
63
64 gateway)
65 if ! ipv4_is_valid "${val}"; then
66 error "Invalid IPv4 address for gateway: ${val}"
67 exit ${EXIT_CONF_ERROR}
68 fi
69
70 GATEWAY="${val}"
71 ;;
72
73 # Compatibility switches
74 netmask)
75 if ! ipv4_netmask_is_valid "${val}"; then
76 error "Invalid netmask: ${val}"
77 exit ${EXIT_CONF_ERROR}
78 fi
79
80 # The netmask will be converted into a prefix
81 PREFIX="$(ipv4_netmask2prefix ${val})"
82 ;;
83
84 # Unknown switches
85 *)
86 error "Unhandled argument: ${arg}"
87 exit ${EXIT_CONF_ERROR}
88 ;;
89 esac
90 done <<< "$(args $@)"
91
92 if ! isset ADDRESS; then
93 error "You need to provide an IPv4 address"
94 exit ${EXIT_CONF_ERROR}
95 fi
96
97 if ! isset PREFIX; then
98 error "You need to provide an IPv4 prefix"
99 exit ${EXIT_CONF_ERROR}
100 fi
101
102 if ! isset GATEWAY && zone_is_nonlocal "${zone}"; then
103 warning "You did not configure a gateway for a non-local zone"
104 fi
105 }
106
107 hook_new() {
108 local zone="${1}"
109 shift
110
111 assert zone_exists "${zone}"
112
113 if ! hook_parse_cmdline $@; then
114 # Return an error if the parsing of the cmd line fails
115 return ${EXIT_ERROR}
116 fi
117
118 zone_config_settings_write "${zone}" "${HOOK}"
119
120 exit ${EXIT_OK}
121 }
122
123 hook_up() {
124 local zone=${1}
125 local config=${2}
126 shift 2
127
128 if ! device_exists ${zone}; then
129 error "Zone '${zone}' doesn't exist."
130 exit ${EXIT_ERROR}
131 fi
132
133 zone_config_settings_read "${zone}" "${config}"
134
135 ip_address_add ${zone} ${ADDRESS}/${PREFIX}
136
137 # Save configuration
138 db_set "${zone}/ipv4/type" "${HOOK}"
139 db_set "${zone}/ipv4/local-ip-address" "${ADDRESS}/${PREFIX}"
140 db_set "${zone}/ipv4/remote-ip-address" "${GATEWAY}"
141 db_set "${zone}/ipv4/active" 1
142
143 routing_update ${zone} ipv4
144 routing_default_update
145
146 exit ${EXIT_OK}
147 }
148
149 hook_down() {
150 local zone=${1}
151 local config=${2}
152 shift 2
153
154 if ! device_exists ${zone}; then
155 error "Zone '${zone}' doesn't exist."
156 exit ${EXIT_ERROR}
157 fi
158
159 # Remove routing information from database.
160 db_delete "${zone}/ipv4"
161
162 zone_config_settings_read "${zone}" "${config}"
163
164 ip_address_del ${zone} ${ADDRESS}/${PREFIX}
165
166 # Update routing tables.
167 routing_default_update
168
169 exit ${EXIT_OK}
170 }
171
172 hook_status() {
173 local zone="${1}"
174 assert isset zone
175
176 local config="${2}"
177 assert isset config
178
179 shift 2
180
181 if ! device_exists ${zone}; then
182 error "Zone '${zone}' doesn't exist."
183 exit ${EXIT_ERROR}
184 fi
185
186 zone_config_settings_read "${zone}" "${config}"
187
188 local status
189 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
190 status=${MSG_HOOK_UP}
191 else
192 status=${MSG_HOOK_DOWN}
193 fi
194 cli_statusline 3 "${HOOK}" "${status}"
195
196 cli_print_fmt1 3 "IPv4 address" "${ADDRESS}/${PREFIX}"
197 if [ -n "${GATEWAY}" ]; then
198 cli_print_fmt1 3 "Gateway" "${GATEWAY}"
199 fi
200 cli_space
201
202 exit ${EXIT_OK}
203 }