]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/configs/ipv4-static
ef74991ec44de20def2a80d3956ce41481bb3029
[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 zone_config_check_same_setting "${zone}" "ipv4-static" "ADDRESS" "${ADDRESS}"; then
103 error "An ipv4-static config with the same IPv4 address is already configured"
104 exit ${EXIT_CONF_ERROR}
105 fi
106
107 if ! isset GATEWAY && zone_is_nonlocal "${zone}"; then
108 warning "You did not configure a gateway for a non-local zone"
109 fi
110 }
111
112 hook_new() {
113 local zone="${1}"
114 shift
115
116 assert zone_exists "${zone}"
117
118 if ! hook_parse_cmdline $@; then
119 # Return an error if the parsing of the cmd line fails
120 return ${EXIT_ERROR}
121 fi
122
123 zone_config_settings_write "${zone}" "${HOOK}"
124
125 exit ${EXIT_OK}
126 }
127
128 hook_up() {
129 local zone=${1}
130 local config=${2}
131 shift 2
132
133 if ! device_exists ${zone}; then
134 error "Zone '${zone}' doesn't exist."
135 exit ${EXIT_ERROR}
136 fi
137
138 zone_config_settings_read "${zone}" "${config}"
139
140 ip_address_add ${zone} ${ADDRESS}/${PREFIX}
141
142 # Save configuration
143 db_set "${zone}/ipv4/type" "${HOOK}"
144 db_set "${zone}/ipv4/local-ip-address" "${ADDRESS}/${PREFIX}"
145 db_set "${zone}/ipv4/remote-ip-address" "${GATEWAY}"
146 db_set "${zone}/ipv4/active" 1
147
148 routing_update ${zone} ipv4
149 routing_default_update
150
151 exit ${EXIT_OK}
152 }
153
154 hook_down() {
155 local zone=${1}
156 local config=${2}
157 shift 2
158
159 if ! device_exists ${zone}; then
160 error "Zone '${zone}' doesn't exist."
161 exit ${EXIT_ERROR}
162 fi
163
164 # Remove routing information from database.
165 db_delete "${zone}/ipv4"
166
167 zone_config_settings_read "${zone}" "${config}"
168
169 ip_address_del ${zone} ${ADDRESS}/${PREFIX}
170
171 # Update routing tables.
172 routing_default_update
173
174 exit ${EXIT_OK}
175 }
176
177 hook_status() {
178 local zone="${1}"
179 assert isset zone
180
181 local config="${2}"
182 assert isset config
183
184 shift 2
185
186 if ! device_exists ${zone}; then
187 error "Zone '${zone}' doesn't exist."
188 exit ${EXIT_ERROR}
189 fi
190
191 zone_config_settings_read "${zone}" "${config}"
192
193 local status
194 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
195 status=${MSG_HOOK_UP}
196 else
197 status=${MSG_HOOK_DOWN}
198 fi
199 cli_statusline 3 "${HOOK}" "${status}"
200
201 cli_print_fmt1 3 "IPv4 address" "${ADDRESS}/${PREFIX}"
202 if [ -n "${GATEWAY}" ]; then
203 cli_print_fmt1 3 "Gateway" "${GATEWAY}"
204 fi
205 cli_space
206
207 exit ${EXIT_OK}
208 }