]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/zones/6to4-tunnel
Remove the function keyword which is a bashism
[people/stevee/network.git] / src / hooks / zones / 6to4-tunnel
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-zone
23
24 HOOK_SETTINGS="HOOK SERVER_ADDRESS LOCAL_ADDRESS LOCAL_ADDRESS6 TUNNEL_ID"
25 HOOK_SETTINGS="${HOOK_SETTINGS} AUTO_UPDATE_ENDPOINT USERNAME PASSWORD"
26
27 # The IPv4 address of the tunnel endpoint where to connect to.
28 SERVER_ADDRESS=
29
30 # The local IPv4 address of the tunnel endpoint.
31 LOCAL_ADDRESS=
32
33 # The address that is assigned to the tunnel device (with prefix).
34 LOCAL_ADDRESS6=
35
36 # True if the endpoint IP address should be automatically
37 # updated each time the tunnel connects.
38 AUTO_UPDATE_ENDPOINT="false"
39
40 # The ID of the tunnel.
41 TUNNEL_ID=
42
43 # Credentials for the tunnelbroker.net service.
44 USERNAME=
45 PASSWORD=
46
47 hook_check_settings() {
48 assert isset SERVER_ADDRESS
49 assert isset LOCAL_ADDRESS
50 assert isset LOCAL_ADDRESS6
51
52 if enabled AUTO_UPDATE_ENDPOINT; then
53 assert isset TUNNEL_ID
54 assert isset USERNAME
55 assert isset PASSWORD
56 fi
57 }
58
59 hook_parse_cmdline() {
60 local value
61
62 while [ $# -gt 0 ]; do
63 case "${1}" in
64 --server-address=*)
65 SERVER_ADDRESS=$(cli_get_val ${1})
66 ;;
67 --local-ipv4-address=*)
68 LOCAL_ADDRESS=$(cli_get_val ${1})
69 ;;
70 --local-ipv6-address=*)
71 LOCAL_ADDRESS6=$(cli_get_val ${1})
72 ;;
73 --auto-update-endpoint=*)
74 local val="$(cli_get_val ${1})"
75
76 if enabled val; then
77 AUTO_UPDATE_ENDPOINT="true"
78 else
79 AUTO_UPADTE_ENDPOINT="false"
80 fi
81 ;;
82 --tunnel-id=*)
83 TUNNEL_ID="$(cli_get_val ${1})"
84 ;;
85 --username=*)
86 USERNAME="$(cli_get_val ${1})"
87 ;;
88 --password=*)
89 PASSWORD="$(cli_get_val ${1})"
90 ;;
91 *)
92 echo "Unknown option: ${1}" >&2
93 exit ${EXIT_ERROR}
94 ;;
95 esac
96 shift
97 done
98 }
99
100 hook_up() {
101 local zone=${1}
102 assert isset zone
103
104 # Read configuration options.
105 zone_settings_read "${zone}"
106
107 if enabled AUTO_UPDATE_ENDPOINT; then
108 log DEBUG "Updating tunnel endpoint"
109
110 he_tunnelbroker_endpoint_update \
111 --username="${USERNAME}" \
112 --password="${PASSWORD}" \
113 --tunnel-id="${TUNNEL_ID}"
114 fi
115
116 ip_tunnel_add ${zone} --ttl=255 \
117 --remote-address="${SERVER_ADDRESS}" \
118 --local-address="${LOCAL_ADDRESS}"
119
120 # Bring up the device.
121 device_set_up ${zone}
122
123 # Assign IPv6 address.
124 ip_address_add ${zone} ${LOCAL_ADDRESS6}
125
126 # Update routing information.
127 routing_db_set ${zone} ipv6 type "${HOOK}"
128 routing_db_set ${zone} ipv6 local-ip-address "${LOCAL_ADDRESS6}"
129 routing_db_set ${zone} ipv6 active 1
130
131 # Update the routing database.
132 routing_update ${zone} ipv6
133 routing_default_update
134
135 exit ${EXIT_OK}
136 }
137
138 hook_down() {
139 local zone=${1}
140 assert isset zone
141
142 # Remove everything from the routing db.
143 routing_db_remove ${zone} ipv6
144 routing_update ${zone} ipv6
145 routing_default_update
146
147 # Remove the tunnel device.
148 ip_tunnel_del ${zone}
149
150 exit ${EXIT_OK}
151 }
152
153 hook_status() {
154 local zone=${1}
155 assert isset zone
156
157 cli_device_headline ${zone}
158
159 zone_settings_read "${zone}"
160
161 local server_line="${SERVER_ADDRESS}"
162 local server_hostname=$(dns_get_hostname ${SERVER_ADDRESS})
163 if [ -n "${server_hostname}" ]; then
164 server_line="${server_line} (Hostname: ${server_hostname})"
165 fi
166
167 cli_headline 2 "Configuration"
168 cli_print_fmt1 2 "Server" "${server_line}"
169 cli_print_fmt1 2 "Endpoint IPv4 address" "${LOCAL_ADDRESS}"
170 cli_print_fmt1 2 "Endpoint IPv6 address" "${LOCAL_ADDRESS6}"
171 cli_space
172
173 exit ${EXIT_OK}
174 }