]> git.ipfire.org Git - people/stevee/network.git/blame - src/hooks/configs/ipv4-static
config hook: prevent two hooks with the same settings
[people/stevee/network.git] / src / hooks / configs / ipv4-static
CommitLineData
1848564d
MT
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
f41fa3d7 22. /usr/lib/network/header-config
1848564d 23
ccbc0dd4
MT
24HOOK_MANPAGE="network-config-ipv4-static"
25
b6d9bf2b 26HOOK_CONFIG_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
1848564d 27
b6d9bf2b 28hook_check_config_settings() {
1848564d
MT
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
4cc76255 38hook_parse_cmdline() {
ccbc0dd4 39 local arg
4cc76255 40
ccbc0dd4
MT
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}"
1848564d 53 ;;
ccbc0dd4
MT
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}"
1848564d 62 ;;
ccbc0dd4
MT
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}"
1848564d 71 ;;
ccbc0dd4
MT
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}
1848564d
MT
88 ;;
89 esac
ccbc0dd4 90 done <<< "$(args $@)"
1848564d 91
ccbc0dd4
MT
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
6727e4be
JS
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
ccbc0dd4
MT
107 if ! isset GATEWAY && zone_is_nonlocal "${zone}"; then
108 warning "You did not configure a gateway for a non-local zone"
1848564d 109 fi
4cc76255
JS
110}
111
112hook_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
1848564d 122
0f8c7941 123 zone_config_settings_write "${zone}" "${HOOK}"
1848564d
MT
124
125 exit ${EXIT_OK}
126}
127
1c6a4e30 128hook_up() {
1848564d
MT
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
a5ebb169 137
b6d9bf2b 138 zone_config_settings_read "${zone}" "${config}"
1848564d 139
38f61548 140 ip_address_add ${zone} ${ADDRESS}/${PREFIX}
1848564d 141
b368da2f 142 # Save configuration
c041b631
MT
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
b368da2f
MT
147
148 routing_update ${zone} ipv4
2741ce55 149 routing_default_update
1848564d
MT
150
151 exit ${EXIT_OK}
152}
153
1c6a4e30 154hook_down() {
1848564d
MT
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
f799b76d
SS
163
164 # Remove routing information from database.
165 db_delete "${zone}/ipv4"
166
b6d9bf2b 167 zone_config_settings_read "${zone}" "${config}"
1848564d 168
38f61548 169 ip_address_del ${zone} ${ADDRESS}/${PREFIX}
1848564d 170
2741ce55
MT
171 # Update routing tables.
172 routing_default_update
173
1848564d
MT
174 exit ${EXIT_OK}
175}
176
1c6a4e30 177hook_status() {
2472e0ea
MT
178 local zone="${1}"
179 assert isset zone
180
181 local config="${2}"
182 assert isset config
183
ae1def39
MT
184 shift 2
185
186 if ! device_exists ${zone}; then
187 error "Zone '${zone}' doesn't exist."
188 exit ${EXIT_ERROR}
189 fi
e9df08ad 190
b6d9bf2b 191 zone_config_settings_read "${zone}" "${config}"
ae1def39 192
8e3508ac 193 local status
38f61548 194 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
8e3508ac 195 status=${MSG_HOOK_UP}
ae1def39 196 else
8e3508ac 197 status=${MSG_HOOK_DOWN}
ae1def39 198 fi
8e3508ac 199 cli_statusline 3 "${HOOK}" "${status}"
ae1def39 200
8e3508ac 201 cli_print_fmt1 3 "IPv4 address" "${ADDRESS}/${PREFIX}"
ae1def39 202 if [ -n "${GATEWAY}" ]; then
8e3508ac 203 cli_print_fmt1 3 "Gateway" "${GATEWAY}"
ae1def39 204 fi
50250b79 205 cli_space
ae1def39
MT
206
207 exit ${EXIT_OK}
208}