]> git.ipfire.org Git - network.git/blob - src/hooks/configs/ipv6-static
ipv6-static: Remove shell switches to define address and prefix
[network.git] / src / hooks / configs / ipv6-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_CONFIG_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
25
26 hook_check_config_settings() {
27 assert isset ADDRESS
28 assert isinteger PREFIX
29
30 if [ ${PREFIX} -gt 64 ]; then
31 error "PREFIX is greater than 64."
32 exit ${EXIT_ERROR}
33 fi
34 }
35
36 hook_parse_cmdline() {
37 while [ $# -gt 0 ]; do
38 case "${1}" in
39 --gateway=*)
40 GATEWAY=${1#--gateway=}
41 ;;
42 *:*/*)
43 ADDRESS=$(ip_split_prefix "${1}")
44 PREFIX=$(ip_get_prefix "${1}")
45 ;;
46 *)
47 error "Invalid argument: ${1}"
48 return ${EXIT_ERROR}
49 ;;
50 esac
51 shift
52 done
53
54 if ! isset ADDRESS; then
55 error "You need to pass an address"
56 return ${EXIT_ERROR}
57 fi
58
59 if ! isset PREFIX; then
60 error "You need to pass a prefix"
61 return ${EXIT_ERROR}
62 fi
63
64 if ! ipv6_is_valid "${ADDRESS}"; then
65 error "${ADDRESS} is not a valid IPv6 address"
66 return ${EXIT_ERROR}
67 fi
68
69 if ! ipv6_prefix_is_valid "${PREFIX}"; then
70 error "${PREFIX} is not a valid IPv6 prefix"
71 return ${EXIT_ERROR}
72 fi
73
74 if zone_config_check_same_setting "${zone}" "ipv6-static" "ADDRESS" "${ADDRESS}"; then
75 error "An ipv6-static config with the same IPv6 address is already configured"
76 exit ${EXIT_CONF_ERROR}
77 fi
78
79 # Store IPv6 address in small format.
80 ADDRESS=$(ipv6_format "${ADDRESS}")
81
82 if [ -n "${GATEWAY}" ]; then
83 GATEWAY=$(ipv6_format "${GATEWAY}")
84 fi
85 }
86
87 hook_new() {
88 local zone=${1}
89 shift
90
91 if ! hook_parse_cmdline "$@"; then
92 # Return an error if the parsing of the cmd line fails
93 return ${EXIT_ERROR}
94 fi
95
96 zone_config_settings_write "${zone}" "${HOOK}"
97
98 exit ${EXIT_OK}
99 }
100
101 hook_up() {
102 local zone=${1}
103 local config=${2}
104 shift 2
105
106 if ! device_exists ${zone}; then
107 error "Zone '${zone}' doesn't exist."
108 exit ${EXIT_ERROR}
109 fi
110
111 zone_config_settings_read "${zone}" "${config}"
112
113 ip_address_add ${zone} ${ADDRESS}/${PREFIX}
114
115 db_set "${zone}/ipv6/local-ip-address" "${ADDRESS}/${PREFIX}"
116 db_set "${zone}/ipv6/remote-ip-address" "${GATEWAY}"
117 db_set "${zone}/ipv6/active" 1
118
119 routing_default_update
120
121 exit ${EXIT_OK}
122 }
123
124 hook_down() {
125 local zone=${1}
126 local config=${2}
127 shift 2
128
129 if ! device_exists ${zone}; then
130 error "Zone '${zone}' doesn't exist."
131 exit ${EXIT_ERROR}
132 fi
133
134 # Remove routing information from database.
135 db_delete "${zone}/ipv6"
136
137 zone_config_settings_read "${zone}" "${config}"
138
139 ip_address_del ${zone} ${ADDRESS}/${PREFIX}
140
141 # Update routing tables.
142 routing_default_update
143
144 exit ${EXIT_OK}
145 }
146
147 hook_status() {
148 local zone=${1}
149 local config=${2}
150 shift 2
151
152 if ! device_exists ${zone}; then
153 error "Zone '${zone}' doesn't exist."
154 exit ${EXIT_ERROR}
155 fi
156
157 zone_config_settings_read "${zone}" "${config}"
158
159 # Make sure ADDRESS is as short as possible.
160 ADDRESS=$(ipv6_format "${ADDRESS}")
161
162 local status
163 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
164 status=${MSG_HOOK_UP}
165 else
166 status=${MSG_HOOK_DOWN}
167 fi
168 cli_statusline 3 "${HOOK}" "${status}"
169
170 cli_print_fmt1 3 "IPv6 address" "${ADDRESS}/${PREFIX}"
171 if [ -n "${GATEWAY}" ]; then
172 cli_print_fmt1 3 "Gateway" "${GATEWAY}"
173 fi
174 cli_space
175
176 exit ${EXIT_OK}
177 }