]> git.ipfire.org Git - people/stevee/network.git/blame - hooks/zones/bridge.configs/ipv4-static
network: Some work on configuration code.
[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
MT
79
80 if ! zone_has_ipv4 ${zone} ${ADDRESS}/${PREFIX}; then
81 ip addr add ${ADDRESS}/${PREFIX} dev ${zone}
82 else
83 warning "Do not set IPv4 address '${ADDRESS}/${PREFIX}' because it was already configured on zone '${zone}'."
84 fi
85
940d7206
MT
86 if zone_is_nonlocal ${zone} && [ -n "${GATEWAY}" ]; then
87 : # XXX to be done
1848564d
MT
88 fi
89
90 exit ${EXIT_OK}
91}
92
93function _down() {
94 local zone=${1}
95 local config=${2}
96 shift 2
97
98 if ! device_exists ${zone}; then
99 error "Zone '${zone}' doesn't exist."
100 exit ${EXIT_ERROR}
101 fi
102
a5ebb169 103 config_read $(zone_dir ${zone})/configs/${config}
1848564d
MT
104
105 if zone_has_ipv4 ${zone} ${ADDRESS}/${PREFIX}; then
106 ip addr del ${ADDRESS}/${PREFIX} dev ${zone}
107 fi
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}"
ae1def39 125 if zone_has_ipv4 ${zone} ${ADDRESS}/${PREFIX}; then
a5ebb169 126 echo -ne "${COLOUR_ENABLED}ENABLED ${COLOUR_NORMAL}"
ae1def39 127 else
a5ebb169 128 echo -ne "${COLOUR_DISABLED}DISABLED${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 $@