]> git.ipfire.org Git - people/ms/network.git/blob - hooks/bridge.configs/ipv4-static
network: Update codebase.
[people/ms/network.git] / hooks / bridge.configs / ipv4-static
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 . /lib/network/header-port
23
24 HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
25
26 function _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
36 function _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
62 config_write $(zone_dir ${zone})/config.${HOOK}.$(uuid) ${HOOK_SETTINGS}
63
64 exit ${EXIT_OK}
65 }
66
67 function _up() {
68 local zone=${1}
69 local config=${2}
70 shift 2
71
72 if ! device_exists ${zone}; then
73 error "Zone '${zone}' doesn't exist."
74 exit ${EXIT_ERROR}
75 fi
76
77 config_read $(zone_dir ${zone})/${config}
78
79 if ! zone_has_ipv4 ${zone} ${ADDRESS}/${PREFIX}; then
80 ip addr add ${ADDRESS}/${PREFIX} dev ${zone}
81 else
82 warning "Do not set IPv4 address '${ADDRESS}/${PREFIX}' because it was already configured on zone '${zone}'."
83 fi
84
85 warning "XXX Gateway stuff has to be done."
86 if [ -n "${GATEWAY}" ]; then
87 : # What do we do with the gateway?
88 fi
89
90 exit ${EXIT_OK}
91 }
92
93 function _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
103 config_read $(zone_dir ${zone})/${config}
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
112 function ipv4_mask_to_cidr() {
113 local mask=0
114
115 local field
116 for field in $(tr '.' ' ' <<<${1}); do
117 mask=$(( $(( ${mask} << 8 )) | ${field} ))
118 done
119
120 local cidr=0
121 local x=$(( 128 << 24 )) # 0x80000000
122
123 while [ $(( ${x} & ${mask} )) -ne 0 ]; do
124 [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 ))
125 cidr=$((${cidr} + 1))
126 done
127
128 if [ $(( ${mask} & 2147483647 )) -ne 0 ]; then # 2147483647 = 0x7fffffff
129 echo "Invalid net mask: $1" >&2
130 else
131 echo ${cidr}
132 fi
133 }
134
135 run $@