]> git.ipfire.org Git - people/stevee/network.git/blame - hooks/zones/bridge.configs/ipv4-static
pppoe-server: Add more information to status output.
[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
f41fa3d7 22. /usr/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
b368da2f
MT
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
1848564d
MT
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 104
38f61548 105 ip_address_del ${zone} ${ADDRESS}/${PREFIX}
1848564d
MT
106
107 exit ${EXIT_OK}
108}
109
ae1def39
MT
110function _status() {
111 local zone=${1}
112 local config=${2}
113 shift 2
114
115 if ! device_exists ${zone}; then
116 error "Zone '${zone}' doesn't exist."
117 exit ${EXIT_ERROR}
118 fi
119
a5ebb169 120 config_read $(zone_dir ${zone})/configs/${config}
ae1def39 121
8e3508ac 122 local status
38f61548 123 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
8e3508ac 124 status=${MSG_HOOK_UP}
ae1def39 125 else
8e3508ac 126 status=${MSG_HOOK_DOWN}
ae1def39 127 fi
8e3508ac 128 cli_statusline 3 "${HOOK}" "${status}"
ae1def39 129
8e3508ac 130 cli_print_fmt1 3 "IPv4 address" "${ADDRESS}/${PREFIX}"
ae1def39 131 if [ -n "${GATEWAY}" ]; then
8e3508ac 132 cli_print_fmt1 3 "Gateway" "${GATEWAY}"
ae1def39
MT
133 fi
134
135 exit ${EXIT_OK}
136}
137
1848564d
MT
138function ipv4_mask_to_cidr() {
139 local mask=0
140
141 local field
142 for field in $(tr '.' ' ' <<<${1}); do
143 mask=$(( $(( ${mask} << 8 )) | ${field} ))
144 done
145
146 local cidr=0
147 local x=$(( 128 << 24 )) # 0x80000000
148
149 while [ $(( ${x} & ${mask} )) -ne 0 ]; do
150 [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 ))
151 cidr=$((${cidr} + 1))
152 done
153
154 if [ $(( ${mask} & 2147483647 )) -ne 0 ]; then # 2147483647 = 0x7fffffff
155 echo "Invalid net mask: $1" >&2
156 else
157 echo ${cidr}
158 fi
159}