]> git.ipfire.org Git - people/stevee/network.git/blame - hooks/zones/bridge.configs/ipv4-static
Bump version to 002.
[people/stevee/network.git] / hooks / zones / bridge.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
a5ebb169 22. /lib/network/header-config
1848564d
MT
23
24HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
25
26function _check() {
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
36function _create() {
37 local zone=${1}
38 shift
39
40 while [ $# -gt 0 ]; do
41 case "${1}" in
42 --address=*)
43 ADDRESS=${1#--address=}
44 ;;
45 --netmask=*)
46 NETMASK=${1#--netmask=}
47 ;;
48 --prefix=*)
49 PREFIX=${1#--prefix=}
50 ;;
51 --gateway=*)
52 GATEWAY=${1#--gateway=}
53 ;;
54 esac
55 shift
56 done
57
58 if [ -z "${PREFIX}" -a -n "${NETMASK}" ]; then
59 PREFIX=$(ipv4_mask_to_cidr ${NETMASK})
60 fi
61
a5ebb169
MT
62 # XXX maybe we can add some hashing to identify a configuration again
63 config_write $(zone_dir ${zone})/configs/${HOOK}.$(uuid) ${HOOK_SETTINGS}
1848564d
MT
64
65 exit ${EXIT_OK}
66}
67
68function _up() {
69 local zone=${1}
70 local config=${2}
71 shift 2
72
73 if ! device_exists ${zone}; then
74 error "Zone '${zone}' doesn't exist."
75 exit ${EXIT_ERROR}
76 fi
a5ebb169
MT
77
78 config_read $(zone_dir ${zone})/configs/${config}
1848564d 79
38f61548 80 ip_address_add ${zone} ${ADDRESS}/${PREFIX}
1848564d 81
b816e04b 82 if [ -n "${GATEWAY}" ]; then
9c71c87c 83 # Save configuration
b816e04b
MT
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
1848564d
MT
90 fi
91
92 exit ${EXIT_OK}
93}
94
95function _down() {
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
a5ebb169 105 config_read $(zone_dir ${zone})/configs/${config}
1848564d 106
38f61548 107 ip_address_del ${zone} ${ADDRESS}/${PREFIX}
1848564d
MT
108
109 exit ${EXIT_OK}
110}
111
ae1def39
MT
112function _status() {
113 local zone=${1}
114 local config=${2}
115 shift 2
116
117 if ! device_exists ${zone}; then
118 error "Zone '${zone}' doesn't exist."
119 exit ${EXIT_ERROR}
120 fi
121
a5ebb169 122 config_read $(zone_dir ${zone})/configs/${config}
ae1def39 123
54a70b3e 124 printf " %10s - " "${HOOK}"
38f61548
MT
125 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
126 echo -ne "${COLOUR_UP} UP ${COLOUR_NORMAL}"
ae1def39 127 else
38f61548 128 echo -ne "${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
ae1def39 129 fi
54a70b3e 130 echo " - ${ADDRESS}/${PREFIX}"
ae1def39
MT
131
132 if [ -n "${GATEWAY}" ]; then
133 echo " Gateway: ${GATEWAY}"
134 fi
135
136 exit ${EXIT_OK}
137}
138
1848564d
MT
139function ipv4_mask_to_cidr() {
140 local mask=0
141
142 local field
143 for field in $(tr '.' ' ' <<<${1}); do
144 mask=$(( $(( ${mask} << 8 )) | ${field} ))
145 done
146
147 local cidr=0
148 local x=$(( 128 << 24 )) # 0x80000000
149
150 while [ $(( ${x} & ${mask} )) -ne 0 ]; do
151 [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 ))
152 cidr=$((${cidr} + 1))
153 done
154
155 if [ $(( ${mask} & 2147483647 )) -ne 0 ]; then # 2147483647 = 0x7fffffff
156 echo "Invalid net mask: $1" >&2
157 else
158 echo ${cidr}
159 fi
160}
161
162run $@