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 # Determine the mode based on the IP protocol
154 mode
=$
(ip_tunnel_convert_mode_to_iproute2_mode
"${mode}" "${remote_address_protocol}")
156 log DEBUG
"Creating tunnel device '${device}' (mode=${mode})..."
159 if ! cmd ip link add name
${device} type ${mode} ${cmd_args}; then
160 error
"Could not create tunnel device ${device}"
164 # Disable policy lookups for VTI devices
165 if [ "${mode}" = "vti" ]; then
166 sysctl_set
"net.ipv4.conf.${device}.disable_policy" "1"
176 ip_tunnel_change_keys
() {
180 if ! isset device
; then
181 error
"No device given"
188 while [ $# -gt 0 ]; do
191 ikey
="$(cli_get_val ${1})"
194 okey
="$(cli_get_val ${1})"
197 error
"Invalid argument: ${1}"
204 if ! isset ikey ||
! isset okey
; then
205 error
"You need to set --ikey= and --okey="
209 if ! device_exists
"${device}"; then
210 error
"No such device: ${device}"
214 # Determine the device type
215 local type="$(device_tunnel_get_type ${device})"
217 if ! isoneof
"type" vti vti6
; then
218 log ERROR
"Device type '${type}' is invalid"
222 if ! cmd ip link change dev
"${device}" \
223 type "${type}" ikey "${ikey}" okey "${okey}"; then
224 log ERROR
"Could not change keys of device ${device}"