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