]> git.ipfire.org Git - people/stevee/network.git/blame - src/hooks/configs/ipv6-static
network fix parameter passing when using ""
[people/stevee/network.git] / src / hooks / configs / ipv6-static
CommitLineData
4231f419
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
b6d9bf2b 22. /usr/lib/network/header-config
4231f419 23
b6d9bf2b 24HOOK_CONFIG_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
4231f419 25
b6d9bf2b 26hook_check_config_settings() {
4231f419
MT
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
465bd07e 36hook_parse_cmdline() {
4231f419
MT
37 while [ $# -gt 0 ]; do
38 case "${1}" in
39 --address=*)
40 ADDRESS=${1#--address=}
41 ;;
42 --prefix=*)
43 PREFIX=${1#--prefix=}
44 ;;
45 --gateway=*)
46 GATEWAY=${1#--gateway=}
47 ;;
48 esac
49 shift
50 done
51
6727e4be
JS
52 if zone_config_check_same_setting "${zone}" "ipv6-static" "ADDRESS" "${ADDRESS}"; then
53 error "An ipv6-static config with the same IPv6 address is already configured"
54 exit ${EXIT_CONF_ERROR}
55 fi
56
b61ca684 57 # Store IPv6 address in small format.
13a6e69f 58 ADDRESS=$(ipv6_format "${ADDRESS}")
4231f419
MT
59
60 if [ -n "${GATEWAY}" ]; then
13a6e69f 61 GATEWAY=$(ipv6_format "${GATEWAY}")
4231f419 62 fi
465bd07e
JS
63}
64
65hook_new() {
66 local zone=${1}
67 shift
68
2212045f 69 if ! hook_parse_cmdline "$@"; then
465bd07e
JS
70 # Return an error if the parsing of the cmd line fails
71 return ${EXIT_ERROR}
72 fi
4231f419 73
0f8c7941 74 zone_config_settings_write "${zone}" "${HOOK}"
4231f419
MT
75
76 exit ${EXIT_OK}
77}
78
1c6a4e30 79hook_up() {
4231f419
MT
80 local zone=${1}
81 local config=${2}
82 shift 2
83
84 if ! device_exists ${zone}; then
85 error "Zone '${zone}' doesn't exist."
86 exit ${EXIT_ERROR}
87 fi
88
b6d9bf2b 89 zone_config_settings_read "${zone}" "${config}"
4231f419 90
38f61548 91 ip_address_add ${zone} ${ADDRESS}/${PREFIX}
4231f419 92
c041b631
MT
93 db_set "${zone}/ipv6/local-ip-address" "${ADDRESS}/${PREFIX}"
94 db_set "${zone}/ipv6/remote-ip-address" "${GATEWAY}"
95 db_set "${zone}/ipv6/active" 1
96
b368da2f 97 routing_default_update
4231f419
MT
98
99 exit ${EXIT_OK}
100}
101
1c6a4e30 102hook_down() {
4231f419
MT
103 local zone=${1}
104 local config=${2}
105 shift 2
106
107 if ! device_exists ${zone}; then
108 error "Zone '${zone}' doesn't exist."
109 exit ${EXIT_ERROR}
110 fi
b0c273ad
MT
111
112 # Remove routing information from database.
c041b631 113 db_delete "${zone}/ipv6"
4231f419 114
b6d9bf2b 115 zone_config_settings_read "${zone}" "${config}"
4231f419 116
38f61548 117 ip_address_del ${zone} ${ADDRESS}/${PREFIX}
4231f419 118
b0c273ad
MT
119 # Update routing tables.
120 routing_default_update
121
4231f419
MT
122 exit ${EXIT_OK}
123}
124
1c6a4e30 125hook_status() {
4231f419
MT
126 local zone=${1}
127 local config=${2}
128 shift 2
129
130 if ! device_exists ${zone}; then
131 error "Zone '${zone}' doesn't exist."
132 exit ${EXIT_ERROR}
133 fi
134
b6d9bf2b 135 zone_config_settings_read "${zone}" "${config}"
4231f419 136
c447aaa8 137 # Make sure ADDRESS is as short as possible.
13a6e69f 138 ADDRESS=$(ipv6_format "${ADDRESS}")
c447aaa8
MT
139
140 local status
38f61548 141 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
c447aaa8 142 status=${MSG_HOOK_UP}
4231f419 143 else
c447aaa8 144 status=${MSG_HOOK_DOWN}
4231f419 145 fi
c447aaa8 146 cli_statusline 3 "${HOOK}" "${status}"
4231f419 147
c447aaa8 148 cli_print_fmt1 3 "IPv6 address" "${ADDRESS}/${PREFIX}"
4231f419 149 if [ -n "${GATEWAY}" ]; then
c447aaa8 150 cli_print_fmt1 3 "Gateway" "${GATEWAY}"
4231f419 151 fi
50250b79 152 cli_space
4231f419
MT
153
154 exit ${EXIT_OK}
155}