2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012-2013 IPFire Network Development Team #
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. #
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. #
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/>. #
20 ###############################################################################
22 IP_TUNNEL_MODES
="gre sit vti"
24 # This function converts our modes into the type
25 # the iproute2 tool uses
26 ip_tunnel_convert_mode_to_iproute2_mode
() {
30 if ! isset mode ||
! isset protocol
; then
31 log ERROR
"Did not get mode and/or protocol"
35 if [[ "${protocol}" = "ipv4" ]]; then
36 # When we use IPv4 we can use our modes
40 if [[ "${protocol}" = "ipv6" ]]; then
41 # When we use IPv6 we have to convert
65 while [ $# -gt 0 ]; do
68 mode
="$(cli_get_val "${1}")"
71 ttl
="$(cli_get_val "${1}")"
74 remote_address
="$(cli_get_val "${1}")"
77 local_address
="$(cli_get_val "${1}")"
82 ikey
="$(cli_get_val "${1}")"
85 okey
="$(cli_get_val "${1}")"
92 error
"--mode= is not set. Must be one of ${IP_TUNNEL_MODES}"
96 if ! isoneof mode
${IP_TUNNEL_MODES}; then
97 error
"Invalid mode: ${mode}"
101 # Detect the IP protocol, which is important to decide which mode we have to use
102 local remote_address_protocol
="$(ip_detect_protocol "${remote_address}")"
104 # If we could not detect the IP protocol something with
105 # ${remote_address} is wrong
106 if ! isset remote_address_protocol
; then
107 log ERROR
"Could not determine remote address IP protocol"
111 # We cannot mix IPv6 and IPv4
112 if [[ "${remote_address_protocol}" != \
113 "$(ip_detect_protocol "${local_address}")" ]] ; then
114 log ERROR
"Local and remote address\
115 are not from the same IP protocol"
119 # ikey and okey must be set for VTI devices
120 if [ "${mode}" = "vti" ] && (! isset ikey ||
! isset okey
); then
121 error
"--ikey= and --okey= must be set for VTI device"
125 # If TTL is set, make sure it is an integer.
126 if isset ttl
&& ! isinteger ttl
; then
127 error
"TTL must be an integer: ${ttl}"
133 # Apply TTL if a value has been set.
135 cmd_args
="${cmd_args} ttl ${ttl}"
138 # Apply local address if a value has been set.
139 if isset local_address
; then
140 cmd_args
="${cmd_args} local ${local_address}"
143 # Apply remote address if a value has been set.
144 if isset remote_address
; then
145 cmd_args
="${cmd_args} remote ${remote_address}"
148 # Add ikey and okey for VTI devices
149 if [ "${mode}" = "vti" ]; then
150 cmd_args
="${cmd_args} ikey ${ikey} okey ${okey}"
153 log DEBUG
"Creating tunnel device '${device}' (mode=${mode})..."
156 if ! cmd ip link add name
${device} type ${mode} ${cmd_args}; then
157 error
"Could not create tunnel device ${device}"
161 # Disable policy lookups for VTI devices
162 if [ "${mode}" = "vti" ]; then
163 sysctl_set
"net.ipv4.conf.${device}.disable_policy" "1"
173 ip_tunnel_change_keys
() {
177 if ! isset device
; then
178 error
"No device given"
185 while [ $# -gt 0 ]; do
188 ikey
="$(cli_get_val ${1})"
191 okey
="$(cli_get_val ${1})"
194 error
"Invalid argument: ${1}"
201 if ! isset ikey ||
! isset okey
; then
202 error
"You need to set --ikey= and --okey="
206 if ! device_exists
"${device}"; then
207 error
"No such device: ${device}"
211 if ! cmd ip link change dev
"${device}" \
212 type vti ikey
"${ikey}" okey
"${okey}"; then
213 log ERROR
"Could not change keys of device ${device}"