]> git.ipfire.org Git - people/stevee/network.git/blame - src/hooks/configs/ipv4-static
Rectify config creation
[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
MT
23
24HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
25
2181765d 26function hook_check() {
1848564d
MT
27 assert isset ADDRESS
28 assert isinteger PREFIX
29
30 if [ ${PREFIX} -gt 30 ]; then
31 error "PREFIX is greater than 30."
32 exit ${EXIT_ERROR}
33 fi
34}
35
2181765d 36function hook_create() {
ea699552
MT
37 local zone="${1}"
38 assert isset zone
1848564d
MT
39 shift
40
41 while [ $# -gt 0 ]; do
42 case "${1}" in
43 --address=*)
44 ADDRESS=${1#--address=}
45 ;;
46 --netmask=*)
47 NETMASK=${1#--netmask=}
48 ;;
49 --prefix=*)
50 PREFIX=${1#--prefix=}
51 ;;
52 --gateway=*)
53 GATEWAY=${1#--gateway=}
54 ;;
55 esac
56 shift
57 done
58
59 if [ -z "${PREFIX}" -a -n "${NETMASK}" ]; then
60 PREFIX=$(ipv4_mask_to_cidr ${NETMASK})
61 fi
62
a5ebb169 63 # XXX maybe we can add some hashing to identify a configuration again
e9df08ad 64 zone_config_settings_write "${zone}" "${HOOK}.$(uuid)" ${HOOK_SETTINGS}
1848564d
MT
65
66 exit ${EXIT_OK}
67}
68
2181765d 69function hook_up() {
1848564d
MT
70 local zone=${1}
71 local config=${2}
72 shift 2
73
74 if ! device_exists ${zone}; then
75 error "Zone '${zone}' doesn't exist."
76 exit ${EXIT_ERROR}
77 fi
a5ebb169 78
e9df08ad 79 zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS}
1848564d 80
38f61548 81 ip_address_add ${zone} ${ADDRESS}/${PREFIX}
1848564d 82
b368da2f
MT
83 # Save configuration
84 routing_db_set ${zone} ipv4 type "${HOOK}"
85 routing_db_set ${zone} ipv4 local-ip-address "${ADDRESS}/${PREFIX}"
86 routing_db_set ${zone} ipv4 remote-ip-address "${GATEWAY}"
87 routing_db_set ${zone} ipv4 active 1
88
89 routing_update ${zone} ipv4
2741ce55 90 routing_default_update
1848564d
MT
91
92 exit ${EXIT_OK}
93}
94
2181765d 95function hook_down() {
1848564d
MT
96 local zone=${1}
97 local config=${2}
98 shift 2
99
100 if ! device_exists ${zone}; then
101 error "Zone '${zone}' doesn't exist."
102 exit ${EXIT_ERROR}
103 fi
104
e9df08ad 105 zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS}
1848564d 106
38f61548 107 ip_address_del ${zone} ${ADDRESS}/${PREFIX}
1848564d 108
2741ce55
MT
109 # Update routing tables.
110 routing_default_update
111
1848564d
MT
112 exit ${EXIT_OK}
113}
114
2181765d 115function hook_status() {
ae1def39
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
e9df08ad
MT
124
125 zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS}
ae1def39 126
8e3508ac 127 local status
38f61548 128 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
8e3508ac 129 status=${MSG_HOOK_UP}
ae1def39 130 else
8e3508ac 131 status=${MSG_HOOK_DOWN}
ae1def39 132 fi
8e3508ac 133 cli_statusline 3 "${HOOK}" "${status}"
ae1def39 134
8e3508ac 135 cli_print_fmt1 3 "IPv4 address" "${ADDRESS}/${PREFIX}"
ae1def39 136 if [ -n "${GATEWAY}" ]; then
8e3508ac 137 cli_print_fmt1 3 "Gateway" "${GATEWAY}"
ae1def39 138 fi
50250b79 139 cli_space
ae1def39
MT
140
141 exit ${EXIT_OK}
142}
143
1848564d
MT
144function ipv4_mask_to_cidr() {
145 local mask=0
146
147 local field
148 for field in $(tr '.' ' ' <<<${1}); do
149 mask=$(( $(( ${mask} << 8 )) | ${field} ))
150 done
151
152 local cidr=0
153 local x=$(( 128 << 24 )) # 0x80000000
154
155 while [ $(( ${x} & ${mask} )) -ne 0 ]; do
156 [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 ))
157 cidr=$((${cidr} + 1))
158 done
159
160 if [ $(( ${mask} & 2147483647 )) -ne 0 ]; then # 2147483647 = 0x7fffffff
161 echo "Invalid net mask: $1" >&2
162 else
163 echo ${cidr}
164 fi
165}