]> git.ipfire.org Git - people/stevee/network.git/blame - src/hooks/configs/ipv4-static
Update all config hooks according to the new naming convention
[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
b6d9bf2b 38hook_new() {
ea699552 39 local zone="${1}"
ccbc0dd4 40 assert zone_exists "${zone}"
1848564d
MT
41 shift
42
ccbc0dd4
MT
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}"
1848564d 56 ;;
ccbc0dd4
MT
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}"
1848564d 65 ;;
ccbc0dd4
MT
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}"
1848564d 74 ;;
ccbc0dd4
MT
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}
1848564d
MT
91 ;;
92 esac
ccbc0dd4 93 done <<< "$(args $@)"
1848564d 94
ccbc0dd4
MT
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"
1848564d
MT
107 fi
108
a5ebb169 109 # XXX maybe we can add some hashing to identify a configuration again
b6d9bf2b 110 zone_config_settings_write "${zone}" "${HOOK}.$(uuid)"
1848564d
MT
111
112 exit ${EXIT_OK}
113}
114
1c6a4e30 115hook_up() {
1848564d
MT
116 local zone=${1}
117 local config=${2}
118 shift 2
119
120 if ! device_exists ${zone}; then
121 error "Zone '${zone}' doesn't exist."
122 exit ${EXIT_ERROR}
123 fi
a5ebb169 124
b6d9bf2b 125 zone_config_settings_read "${zone}" "${config}"
1848564d 126
38f61548 127 ip_address_add ${zone} ${ADDRESS}/${PREFIX}
1848564d 128
b368da2f 129 # Save configuration
c041b631
MT
130 db_set "${zone}/ipv4/type" "${HOOK}"
131 db_set "${zone}/ipv4/local-ip-address" "${ADDRESS}/${PREFIX}"
132 db_set "${zone}/ipv4/remote-ip-address" "${GATEWAY}"
133 db_set "${zone}/ipv4/active" 1
b368da2f
MT
134
135 routing_update ${zone} ipv4
2741ce55 136 routing_default_update
1848564d
MT
137
138 exit ${EXIT_OK}
139}
140
1c6a4e30 141hook_down() {
1848564d
MT
142 local zone=${1}
143 local config=${2}
144 shift 2
145
146 if ! device_exists ${zone}; then
147 error "Zone '${zone}' doesn't exist."
148 exit ${EXIT_ERROR}
149 fi
150
b6d9bf2b 151 zone_config_settings_read "${zone}" "${config}"
1848564d 152
38f61548 153 ip_address_del ${zone} ${ADDRESS}/${PREFIX}
1848564d 154
2741ce55
MT
155 # Update routing tables.
156 routing_default_update
157
1848564d
MT
158 exit ${EXIT_OK}
159}
160
1c6a4e30 161hook_status() {
2472e0ea
MT
162 local zone="${1}"
163 assert isset zone
164
165 local config="${2}"
166 assert isset config
167
ae1def39
MT
168 shift 2
169
170 if ! device_exists ${zone}; then
171 error "Zone '${zone}' doesn't exist."
172 exit ${EXIT_ERROR}
173 fi
e9df08ad 174
b6d9bf2b 175 zone_config_settings_read "${zone}" "${config}"
ae1def39 176
8e3508ac 177 local status
38f61548 178 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
8e3508ac 179 status=${MSG_HOOK_UP}
ae1def39 180 else
8e3508ac 181 status=${MSG_HOOK_DOWN}
ae1def39 182 fi
8e3508ac 183 cli_statusline 3 "${HOOK}" "${status}"
ae1def39 184
8e3508ac 185 cli_print_fmt1 3 "IPv4 address" "${ADDRESS}/${PREFIX}"
ae1def39 186 if [ -n "${GATEWAY}" ]; then
8e3508ac 187 cli_print_fmt1 3 "Gateway" "${GATEWAY}"
ae1def39 188 fi
50250b79 189 cli_space
ae1def39
MT
190
191 exit ${EXIT_OK}
192}