2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2018 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 .
/usr
/lib
/network
/header-port
24 SUPPORTED_IP_TUNNEL_MODES
="gretap"
26 HOOK_SETTINGS
="ADDRESS MARK MODE PEER LOCAL_ADDRESS"
28 hook_check_settings
() {
30 assert isoneof MODE
${SUPPORTED_IP_TUNNEL_MODES}
33 assert mac_is_valid
"${ADDRESS}"
35 # Generate a random mark
37 MARK
="$(( ${RANDOM} & 0xffffffff ))"
41 hook_parse_cmdline
() {
42 while [ $# -gt 0 ]; do
45 ADDRESS
="$(cli_get_val "${1}")"
47 if ! isset ADDRESS ||
! mac_is_valid
"${ADDRESS}"; then
48 error
"Invalid MAC address: ${ADDRESS}"
54 LOCAL_ADDRESS
="$(cli_get_val "${1}")"
58 MODE
="$(cli_get_val "${1}")"
60 # MODE must be on the list of supported protocols
61 if ! isoneof MODE
${SUPPORTED_IP_TUNNEL_MODES}; then
62 error
"Unsupported mode: ${mode}"
68 PEER
="$(cli_get_val "${1}")"
72 error
"Unknown option: ${1}"
79 # Generate a random MAC address if none is set
80 if ! isset ADDRESS
; then
81 ADDRESS
="$(mac_generate)"
84 # If PEER is set, it must be a valid IP address
85 if isset PEER
&& ! ip_is_valid
"${PEER}"; then
86 error
"Peer ${PEER} is not a valid IP address"
90 # If LOCAL_ADDRESS is set, it must be a valid IP address
91 # of the same protocol than PEER is
92 if isset LOCAL_ADDRESS
; then
93 if ! ip_is_valid
"${LOCAL_ADDRESS}"; then
94 error
"Local address ${LOCAL_ADDRESS} is not a valid IP address"
98 if ! ip_protocol_match
"${PEER}" "${LOCAL_ADDRESS}"; then
99 error
"Peer and local address are of different IP protocols"
111 local ${HOOK_SETTINGS}
112 if ! port_settings_read
"${port}" ${HOOK_SETTINGS}; then
113 log ERROR
"Could not read settings for port ${port}"
117 if ! ip_tunnel_add
"${port}" \
119 --address="${ADDRESS}" \
120 --remote-address="${PEER}" \
121 --local-address="${LOCAL_ADDRESS}" \
123 --okey="${MARK}"; then
135 if ! ip_tunnel_del
"${port}"; then
142 hook_hotplug_rename
() {
149 local ${HOOK_SETTINGS}
150 if ! port_settings_read
"${port}" ${HOOK_SETTINGS}; then
151 log ERROR
"Could not read settings for port ${port}"
155 # Get the current MAC address of the device.
156 local address
="$(device_get_address ${device})"
160 if [ "${ADDRESS}" = "${address}" ]; then