]>
Commit | Line | Data |
---|---|---|
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 | |
24 | HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY" | |
25 | ||
2181765d | 26 | function hook_check() { |
1848564d MT |
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 | ||
2181765d | 36 | function hook_create() { |
1848564d MT |
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 | 62 | # XXX maybe we can add some hashing to identify a configuration again |
e9df08ad | 63 | zone_config_settings_write "${zone}" "${HOOK}.$(uuid)" ${HOOK_SETTINGS} |
1848564d MT |
64 | |
65 | exit ${EXIT_OK} | |
66 | } | |
67 | ||
2181765d | 68 | function hook_up() { |
1848564d MT |
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 | 77 | |
e9df08ad | 78 | zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS} |
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 | |
2741ce55 | 89 | routing_default_update |
1848564d MT |
90 | |
91 | exit ${EXIT_OK} | |
92 | } | |
93 | ||
2181765d | 94 | function hook_down() { |
1848564d MT |
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 | ||
e9df08ad | 104 | zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS} |
1848564d | 105 | |
38f61548 | 106 | ip_address_del ${zone} ${ADDRESS}/${PREFIX} |
1848564d | 107 | |
2741ce55 MT |
108 | # Update routing tables. |
109 | routing_default_update | |
110 | ||
1848564d MT |
111 | exit ${EXIT_OK} |
112 | } | |
113 | ||
2181765d | 114 | function hook_status() { |
ae1def39 MT |
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 | |
e9df08ad MT |
123 | |
124 | zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS} | |
ae1def39 | 125 | |
8e3508ac | 126 | local status |
38f61548 | 127 | if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then |
8e3508ac | 128 | status=${MSG_HOOK_UP} |
ae1def39 | 129 | else |
8e3508ac | 130 | status=${MSG_HOOK_DOWN} |
ae1def39 | 131 | fi |
8e3508ac | 132 | cli_statusline 3 "${HOOK}" "${status}" |
ae1def39 | 133 | |
8e3508ac | 134 | cli_print_fmt1 3 "IPv4 address" "${ADDRESS}/${PREFIX}" |
ae1def39 | 135 | if [ -n "${GATEWAY}" ]; then |
8e3508ac | 136 | cli_print_fmt1 3 "Gateway" "${GATEWAY}" |
ae1def39 | 137 | fi |
50250b79 | 138 | cli_space |
ae1def39 MT |
139 | |
140 | exit ${EXIT_OK} | |
141 | } | |
142 | ||
1848564d MT |
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 | } |