]> git.ipfire.org Git - people/stevee/network.git/blob - hooks/zones/bridge.configs/ipv4-static
hostapd: Enable WMM by default.
[people/stevee/network.git] / hooks / zones / 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 . /usr/lib/network/header-config
23
24 HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
25
26 function hook_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 hook_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 # XXX maybe we can add some hashing to identify a configuration again
63 config_write $(zone_dir ${zone})/configs/${HOOK}.$(uuid) ${HOOK_SETTINGS}
64
65 exit ${EXIT_OK}
66 }
67
68 function hook_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
77
78 config_read $(zone_dir ${zone})/configs/${config}
79
80 ip_address_add ${zone} ${ADDRESS}/${PREFIX}
81
82 # Save configuration
83 routing_db_set ${zone} ipv4 type "${HOOK}"
84 routing_db_set ${zone} ipv4 local-ip-address "${ADDRESS}/${PREFIX}"
85 routing_db_set ${zone} ipv4 remote-ip-address "${GATEWAY}"
86 routing_db_set ${zone} ipv4 active 1
87
88 routing_update ${zone} ipv4
89 routing_default_update
90
91 exit ${EXIT_OK}
92 }
93
94 function hook_down() {
95 local zone=${1}
96 local config=${2}
97 shift 2
98
99 if ! device_exists ${zone}; then
100 error "Zone '${zone}' doesn't exist."
101 exit ${EXIT_ERROR}
102 fi
103
104 config_read $(zone_dir ${zone})/configs/${config}
105
106 ip_address_del ${zone} ${ADDRESS}/${PREFIX}
107
108 # Update routing tables.
109 routing_default_update
110
111 exit ${EXIT_OK}
112 }
113
114 function hook_status() {
115 local zone=${1}
116 local config=${2}
117 shift 2
118
119 if ! device_exists ${zone}; then
120 error "Zone '${zone}' doesn't exist."
121 exit ${EXIT_ERROR}
122 fi
123
124 config_read $(zone_dir ${zone})/configs/${config}
125
126 local status
127 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
128 status=${MSG_HOOK_UP}
129 else
130 status=${MSG_HOOK_DOWN}
131 fi
132 cli_statusline 3 "${HOOK}" "${status}"
133
134 cli_print_fmt1 3 "IPv4 address" "${ADDRESS}/${PREFIX}"
135 if [ -n "${GATEWAY}" ]; then
136 cli_print_fmt1 3 "Gateway" "${GATEWAY}"
137 fi
138 cli_space
139
140 exit ${EXIT_OK}
141 }
142
143 function ipv4_mask_to_cidr() {
144 local mask=0
145
146 local field
147 for field in $(tr '.' ' ' <<<${1}); do
148 mask=$(( $(( ${mask} << 8 )) | ${field} ))
149 done
150
151 local cidr=0
152 local x=$(( 128 << 24 )) # 0x80000000
153
154 while [ $(( ${x} & ${mask} )) -ne 0 ]; do
155 [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 ))
156 cidr=$((${cidr} + 1))
157 done
158
159 if [ $(( ${mask} & 2147483647 )) -ne 0 ]; then # 2147483647 = 0x7fffffff
160 echo "Invalid net mask: $1" >&2
161 else
162 echo ${cidr}
163 fi
164 }