2012-11-29 20:14:28 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# Copyright (c) 2012 OpenWrt.org
|
|
|
|
. /lib/functions.sh
|
|
|
|
. /lib/functions/service.sh
|
|
|
|
. /lib/functions/network.sh
|
|
|
|
|
|
|
|
config_load network6
|
2012-12-09 13:54:22 +00:00
|
|
|
local NAT="ip6tables -t nat"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
conf_get() {
|
|
|
|
local __return="$1"
|
|
|
|
local __device="$2"
|
|
|
|
local __option="$3"
|
2012-12-27 14:10:17 +00:00
|
|
|
local __value=$(cat "/proc/sys/net/ipv6/conf/$__device/$__option")
|
2012-11-29 20:14:28 +00:00
|
|
|
eval "$__return=$__value"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
conf_set() {
|
|
|
|
local device="$1"
|
|
|
|
local option="$2"
|
|
|
|
local value="$3"
|
|
|
|
echo "$value" > "/proc/sys/net/ipv6/conf/$device/$option"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stop_service() {
|
|
|
|
local __exe="$1"
|
|
|
|
SERVICE_PID_FILE="$2"
|
|
|
|
local __return="$3"
|
|
|
|
|
|
|
|
service_check "$__exe" && {
|
|
|
|
service_stop "$__exe"
|
|
|
|
[ -n "$__return" ] && eval "$__return=1"
|
|
|
|
}
|
|
|
|
rm -f "$SERVICE_PID_FILE"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
start_service() {
|
|
|
|
local cmd="$1"
|
|
|
|
local pidfile="$2"
|
|
|
|
|
|
|
|
SERVICE_DAEMONIZE=1
|
|
|
|
SERVICE_WRITE_PID=1
|
|
|
|
SERVICE_PID_FILE="$pidfile"
|
|
|
|
service_start $cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resolve_network_add() {
|
|
|
|
local __section="$1"
|
|
|
|
local __device="$2"
|
|
|
|
local __return="$3"
|
|
|
|
local __cdevice
|
|
|
|
network_get_device __cdevice "$__section"
|
|
|
|
[ "$__cdevice" != "$__device" ] && return
|
|
|
|
|
|
|
|
eval "$__return"'="'"$__section"'"'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resolve_network() {
|
|
|
|
local __return="$1"
|
|
|
|
local __device="$2"
|
|
|
|
config_foreach resolve_network_add interface "$__device" "$__return"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-09 13:54:22 +00:00
|
|
|
setup_masquerading() {
|
|
|
|
local cmd="$1"
|
|
|
|
local chain="network6_masquerade_$2"
|
|
|
|
local device="$3"
|
|
|
|
|
|
|
|
$NAT -D POSTROUTING -j "$chain" 2>/dev/null && {
|
|
|
|
$NAT -F "$chain" 2>/dev/null
|
|
|
|
$NAT -X "$chain" 2>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
[ "$cmd" != "stop" ] && {
|
|
|
|
$NAT -N "$chain"
|
|
|
|
$NAT -A "$chain" -o "$device" -j MASQUERADE
|
|
|
|
$NAT -A POSTROUTING -j "$chain"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setup_npt_chain() {
|
|
|
|
local cmd="$1"
|
|
|
|
local network="$2"
|
|
|
|
local chain="network6_npt_$network"
|
|
|
|
|
|
|
|
[ "$cmd" != "start" ] && {
|
|
|
|
$NAT -D POSTROUTING -j "$chain" 2>/dev/null && {
|
|
|
|
$NAT -D PREROUTING -j "$chain" 2>/dev/null
|
|
|
|
$NAT -F "$chain" 2>/dev/null
|
|
|
|
$NAT -X "$chain" 2>/dev/null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[ "$cmd" != "stop" ] && {
|
|
|
|
$NAT -N "$chain" 2>/dev/null && {
|
|
|
|
$NAT -A PREROUTING -j "$chain"
|
|
|
|
$NAT -A POSTROUTING -j "$chain"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-29 20:14:28 +00:00
|
|
|
announce_prefix() {
|
|
|
|
local prefix="$1"
|
|
|
|
local network="$2"
|
2012-12-27 14:10:17 +00:00
|
|
|
local device="$3"
|
|
|
|
local cmd="$4"
|
|
|
|
local type="$5"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
local addr=$(echo "$prefix" | cut -d/ -f1)
|
|
|
|
local rem=$(echo "$prefix" | cut -d/ -f2)
|
|
|
|
local length=$(echo "$rem" | cut -d, -f1)
|
|
|
|
local prefer=""
|
|
|
|
local valid=""
|
|
|
|
|
|
|
|
# If preferred / valid provided
|
|
|
|
[ "$rem" != "$length" ] && {
|
|
|
|
prefer=$(echo "$rem" | cut -d, -f2)
|
|
|
|
valid=$(echo "$rem" | cut -d, -f3)
|
|
|
|
}
|
|
|
|
|
2012-12-09 13:54:22 +00:00
|
|
|
# Get prefix configuration
|
|
|
|
local ula=""
|
|
|
|
local prefix_action=""
|
|
|
|
config_get ula global ula_prefix
|
|
|
|
config_get prefix_action "$network" prefix_action
|
|
|
|
[ -z "$prefix_action" ] && prefix_action="distribute"
|
|
|
|
|
|
|
|
# Always announce the ULA when doing NPT
|
|
|
|
[ "$prefix" == "$ula" -a "$prefix_action" == "npt" ] && prefix_action="distribute"
|
|
|
|
|
2012-12-10 16:46:59 +00:00
|
|
|
[ "$prefix_action" == "distribute" -o "$prefix_action" == "npt" ] && {
|
2012-12-09 13:54:22 +00:00
|
|
|
local msg='{"network": "'"$network"'", "prefix": "'"$addr"'", "length": '"$length"
|
|
|
|
[ -n "$valid" ] && msg="$msg"', "valid": '"$valid"', "preferred": '"$prefer"
|
|
|
|
[ -z "$cmd" ] && cmd=newprefix
|
2012-12-10 16:46:59 +00:00
|
|
|
|
|
|
|
[ "$prefix_action" == "npt" ] && msg="$msg"', "npt": 1'
|
2012-12-11 16:56:37 +00:00
|
|
|
[ "$type" == "secondary" ] && msg="$msg"', "secondary": 1'
|
2012-12-27 14:10:17 +00:00
|
|
|
|
|
|
|
# Detect MTU
|
|
|
|
local mtu
|
|
|
|
conf_get mtu "$device" mtu
|
|
|
|
msg="$msg"', "mtu": '"$mtu"
|
|
|
|
|
2012-12-09 13:54:22 +00:00
|
|
|
ubus call 6distributed "$cmd" "$msg}"
|
|
|
|
}
|
|
|
|
|
|
|
|
[ "$prefix_action" == "npt" ] && {
|
|
|
|
local chain="network6_npt_$network"
|
|
|
|
local ula_addr=$(echo "$ula" | cut -d/ -f1)
|
|
|
|
local ula_rem=$(echo "$ula" | cut -d/ -f2)
|
|
|
|
local ula_length=$(echo "$ula_rem" | cut -d, -f1)
|
|
|
|
local device=""
|
|
|
|
|
|
|
|
network_get_device device "$network"
|
|
|
|
[ "$length" -lt "$ula_length" ] && length="$ula_length"
|
|
|
|
[ "$cmd" == "delprefix" ] && cmd="-D $chain" || cmd="-A $chain"
|
|
|
|
|
|
|
|
local in="-i $device -d $addr/$length -j NETMAP --to $ula_addr/$ula_length"
|
|
|
|
local out="-o $device -s $ula_addr/$ula_length -j NETMAP --to $addr/$length"
|
|
|
|
|
|
|
|
setup_npt_chain start "$network"
|
|
|
|
$NAT $cmd $in
|
|
|
|
$NAT $cmd $out
|
|
|
|
}
|
2012-11-29 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
disable_router() {
|
2012-11-29 20:14:28 +00:00
|
|
|
local network="$1"
|
|
|
|
|
|
|
|
# Notify the address distribution daemon
|
|
|
|
ubus call 6distributed deliface '{"network": "'"$network"'"}'
|
|
|
|
|
2012-12-22 13:00:53 +00:00
|
|
|
|
|
|
|
# Start RD & DHCPv6 service
|
|
|
|
local router_service
|
|
|
|
config_get router_service global router_service
|
|
|
|
|
|
|
|
if [ "$router_service" == "dnsmasq" ]; then
|
|
|
|
rm -f "/var/etc/dnsmasq.d/ipv6-router-$network.conf"
|
|
|
|
/etc/init.d/dnsmasq restart
|
|
|
|
else
|
|
|
|
stop_service /usr/sbin/6relayd "/var/run/ipv6-router-$network.pid"
|
|
|
|
fi
|
2012-11-29 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
restart_relay_slave() {
|
|
|
|
local __section="$1"
|
|
|
|
local __master="$2"
|
|
|
|
|
|
|
|
network_is_up "$__section" || return
|
|
|
|
|
|
|
|
local __device=""
|
|
|
|
network_get_device __device "$__section"
|
|
|
|
|
|
|
|
local __cmaster=""
|
|
|
|
config_get __cmaster "$__section" relay_master
|
|
|
|
|
|
|
|
[ "$__master" == "$__cmaster" ] && {
|
|
|
|
disable_interface "$__section"
|
|
|
|
enable_interface "$__section" "$__device"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
add_relay_slave() {
|
2012-11-29 20:14:28 +00:00
|
|
|
local __section="$1"
|
|
|
|
local __return="$2"
|
|
|
|
local __master="$3"
|
2012-12-03 09:18:53 +00:00
|
|
|
local __mode="$4"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
network_is_up "$__section" || return
|
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
# Get device
|
|
|
|
local __device=""
|
|
|
|
network_get_device __device "$__section"
|
|
|
|
|
2012-11-29 20:14:28 +00:00
|
|
|
# Match master network
|
|
|
|
local __cmaster=""
|
|
|
|
config_get __cmaster "$__section" relay_master
|
2012-12-03 09:18:53 +00:00
|
|
|
[ "$__master" == "$__cmaster" ] || return
|
2012-11-29 20:14:28 +00:00
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
# Test slave mode
|
|
|
|
local __cmode=""
|
|
|
|
config_get __cmode "$__section" mode
|
|
|
|
[ "$__cmode" == "downstream" ] && __cmode="router"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
# Don't start fallback interfaces if we are in forced-relay mode
|
|
|
|
[ "$__cmode" == "relay" -o "$__mode" == "fallback" ] || return
|
|
|
|
|
|
|
|
# Don't make non-relay or non-router interfaces slaves
|
|
|
|
[ "$__cmode" == "relay" -o "$__cmode" == "router" ] || return
|
|
|
|
|
|
|
|
# Disable any active distribution
|
|
|
|
[ "$__cmode" == "router" ] && disable_router "$__section"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
2012-12-06 20:52:40 +00:00
|
|
|
# Configure interface to accept RA and send RS
|
|
|
|
conf_set "$__device" accept_ra 2
|
|
|
|
conf_set "$__device" forwarding 2
|
|
|
|
|
2012-11-29 20:14:28 +00:00
|
|
|
eval "$__return"'="$'"$__return"' '"$__device"'"'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stop_relay() {
|
|
|
|
local network="$1"
|
2012-12-03 09:18:53 +00:00
|
|
|
local pid_fallback="/var/run/ipv6-relay-fallback-$network.pid"
|
|
|
|
local pid_forced="/var/run/ipv6-relay-forced-$network.pid"
|
|
|
|
local was_fallback=""
|
2012-11-29 20:14:28 +00:00
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
stop_service /usr/sbin/6relayd "$pid_fallback" was_fallback
|
|
|
|
stop_service /usr/sbin/6relayd "$pid_forced"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
# Reenable normal distribution on slave interfaces
|
2012-12-03 09:18:53 +00:00
|
|
|
[ -n "$was_fallback" ] && config_foreach restart_relay_slave interface "$network"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
detect_forced_relay_mode() {
|
|
|
|
local __section="$1"
|
|
|
|
local __mode="$2"
|
|
|
|
|
|
|
|
local __cmode
|
|
|
|
config_get __cmode "$__section" mode
|
|
|
|
[ "$__cmode" == "relay" ] && eval "$__mode=forced"
|
2012-11-29 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
restart_relay() {
|
|
|
|
local network="$1"
|
2012-12-03 09:18:53 +00:00
|
|
|
local mode="$2"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
# Stop last active relay
|
|
|
|
stop_relay "$network"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
# Detect if we have a forced-relay
|
|
|
|
[ -z "$mode" ] && config_foreach detect_forced_relay_mode interface mode
|
2012-11-29 20:14:28 +00:00
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
# Don't start without a mode
|
|
|
|
[ -z "$mode" ] && return
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
# Detect master device
|
|
|
|
local device=""
|
2012-12-03 09:18:53 +00:00
|
|
|
network_get_device device "$network"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
# Generate command string
|
2012-12-03 09:18:53 +00:00
|
|
|
local cmd="/usr/sbin/6relayd -A $device"
|
|
|
|
local ifaces=""
|
|
|
|
config_foreach add_relay_slave interface ifaces "$network" "$mode"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
# Start relay
|
2012-12-03 09:18:53 +00:00
|
|
|
local pid="/var/run/ipv6-relay-$mode-$network.pid"
|
|
|
|
[ -n "$ifaces" ] && start_service "$cmd $ifaces" "$pid"
|
|
|
|
|
|
|
|
# There are no slave interface, however indicate that we want to relay
|
|
|
|
[ -z "$ifaces" ] && touch "$pid"
|
2012-11-29 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-09 13:54:22 +00:00
|
|
|
setup_prefix_fallback() {
|
|
|
|
local cmd="$1"
|
|
|
|
local network="$2"
|
|
|
|
local device="$3"
|
|
|
|
|
|
|
|
stop_relay "$network"
|
|
|
|
restart_relay "$network"
|
|
|
|
|
|
|
|
setup_masquerading stop "$network"
|
|
|
|
|
|
|
|
[ "$cmd" != "stop" ] && {
|
|
|
|
local fallback=""
|
|
|
|
config_get fallback "$network" prefix_fallback
|
|
|
|
|
|
|
|
[ "$fallback" == "relay" ] && restart_relay "$network" fallback
|
|
|
|
[ "$fallback" == "masquerade" ] && setup_masquerading start "$network" "$device"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-29 20:14:28 +00:00
|
|
|
restart_master_relay() {
|
|
|
|
local network="$1"
|
2012-12-03 09:18:53 +00:00
|
|
|
local mode="$2"
|
|
|
|
local pid_fallback="/var/run/ipv6-relay-fallback-$network.pid"
|
|
|
|
local pid_forced="/var/run/ipv6-relay-forced-$network.pid"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
# Disable active relaying to this interface
|
|
|
|
config_get relay_master "$network" relay_master
|
2012-12-03 09:18:53 +00:00
|
|
|
[ -z "$relay_master" ] && return
|
|
|
|
network_is_up "$relay_master" || return
|
|
|
|
|
|
|
|
# Detect running mode
|
|
|
|
[ -z "$mode" && -f "$pid_fallback" ] && mode="fallback"
|
|
|
|
[ -z "$mode" && -f "$pid_forced" ] && mode="forced"
|
|
|
|
|
|
|
|
# Restart relay if running or start requested
|
|
|
|
[ -n "$mode" ] && restart_relay "$relay_master" "$mode"
|
2012-11-29 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-28 12:02:22 +00:00
|
|
|
set_site_border() {
|
|
|
|
local network="$1"
|
|
|
|
local device="$2"
|
|
|
|
|
|
|
|
local fwscript="/var/etc/ipv6-firewall.d/site-border-$network.sh"
|
|
|
|
local chain="ipv6-site-border-$network"
|
|
|
|
|
|
|
|
if [ -n "$device" ]; then
|
|
|
|
local site_border
|
2012-12-29 10:12:31 +00:00
|
|
|
config_get_bool site_border "$network" site_border 1
|
2012-12-28 12:02:22 +00:00
|
|
|
[ "$site_border" == "1" ] || return
|
|
|
|
|
|
|
|
mkdir -p $(dirname "$fwscript")
|
|
|
|
echo "ip6tables -N $chain" > "$fwscript"
|
|
|
|
echo "ip6tables -F $chain" >> "$fwscript"
|
|
|
|
echo "ip6tables -A $chain -o $device -j REJECT --reject-with icmp6-no-route" >> "$fwscript"
|
|
|
|
echo "ip6tables -A $chain -i $device -j REJECT --reject-with icmp6-no-route" >> "$fwscript"
|
|
|
|
echo "ip6tables -A ipv6-site-border -j $chain" >> "$fwscript"
|
|
|
|
. "$fwscript"
|
|
|
|
else
|
|
|
|
[ -f "$fwscript" ] || return
|
|
|
|
rm -f "$fwscript"
|
|
|
|
ip6tables -D ipv6-site-border -j "$chain"
|
|
|
|
ip6tables -F "$chain"
|
|
|
|
ip6tables -X "$chain"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-29 20:14:28 +00:00
|
|
|
disable_interface() {
|
|
|
|
local network="$1"
|
|
|
|
|
|
|
|
# Delete all prefixes routed to this interface
|
|
|
|
ubus call 6distributed delprefix '{"network": "'"$network"'"}'
|
|
|
|
|
|
|
|
# Restart Relay
|
|
|
|
restart_master_relay "$network"
|
|
|
|
|
|
|
|
# Disable distribution
|
2012-12-03 09:18:53 +00:00
|
|
|
disable_router "$network"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
2012-12-09 13:54:22 +00:00
|
|
|
# Disable any active relays, masquerading rules and NPT rules
|
2012-11-29 20:14:28 +00:00
|
|
|
stop_relay "$network"
|
2012-12-09 13:54:22 +00:00
|
|
|
setup_masquerading stop "$network"
|
|
|
|
setup_npt_chain stop "$network"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
# Disable DHCPv6 client if enabled, state script will take care
|
2012-12-03 09:18:53 +00:00
|
|
|
stop_service /usr/sbin/odhcp6c "/var/run/ipv6-dhcpv6-$network.pid"
|
2012-12-28 12:02:22 +00:00
|
|
|
|
|
|
|
# Stop site-border
|
|
|
|
set_site_border "$network"
|
2012-11-29 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-09 13:54:22 +00:00
|
|
|
enable_ula_prefix() {
|
2012-11-29 20:14:28 +00:00
|
|
|
local network="$1"
|
2012-12-09 13:54:22 +00:00
|
|
|
local ula="$2"
|
2012-12-27 14:10:17 +00:00
|
|
|
local device="$3"
|
2012-12-09 13:54:22 +00:00
|
|
|
[ -z "$ula" ] && ula="global"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
# ULA-integration
|
|
|
|
local ula_prefix=""
|
2012-12-09 13:54:22 +00:00
|
|
|
config_get ula_prefix "$ula" ula_prefix
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
# ULA auto configuration (first init)
|
|
|
|
[ "$ula_prefix" == "auto" ] && {
|
|
|
|
local r1=""
|
|
|
|
local r2=""
|
|
|
|
local r3=""
|
|
|
|
|
|
|
|
# Sometimes results are empty, therefore try until it works...
|
|
|
|
while [ -z "$r1" -o -z "$r2" -o -z "$r3" ]; do
|
|
|
|
r1=$(printf "%02x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 256)))
|
|
|
|
r2=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
|
|
|
|
r3=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
|
|
|
|
done
|
|
|
|
|
|
|
|
ula_prefix="fd$r1:$r2:$r3::/48"
|
|
|
|
|
|
|
|
# Save prefix so it will be preserved across reboots
|
2012-12-09 13:54:22 +00:00
|
|
|
config_set "$ula" ula_prefix "$ula_prefix"
|
|
|
|
uci_set network6 "$ula" ula_prefix "$ula_prefix"
|
2012-12-06 20:52:40 +00:00
|
|
|
uci_commit network6
|
2012-11-29 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Announce ULA
|
2012-12-27 14:10:17 +00:00
|
|
|
[ -n "$ula_prefix" ] && announce_prefix "$ula_prefix" "$network" "$device" newprefix secondary
|
2012-12-09 13:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enable_static() {
|
|
|
|
local network="$1"
|
|
|
|
local device="$2"
|
|
|
|
|
|
|
|
# Enable global forwarding
|
|
|
|
local global_forward
|
|
|
|
conf_get global_forward all forwarding
|
|
|
|
[ "$global_forward" != "1" ] && conf_set all forwarding 1
|
|
|
|
|
|
|
|
# Configure device
|
|
|
|
conf_set "$device" accept_ra 1
|
|
|
|
conf_set "$device" forwarding 1
|
|
|
|
|
|
|
|
# Enable ULA
|
2012-12-27 14:10:17 +00:00
|
|
|
enable_ula_prefix "$network" global "$device"
|
2012-12-09 13:54:22 +00:00
|
|
|
# Compatibility (deprecated)
|
2012-12-27 14:10:17 +00:00
|
|
|
enable_ula_prefix "$network" "$network" "$device"
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
# Announce all static prefixes
|
2012-12-27 14:10:17 +00:00
|
|
|
config_list_foreach "$network" static_prefix announce_prefix "$network" "$device"
|
2012-12-03 09:18:53 +00:00
|
|
|
|
|
|
|
# start relay if there are forced relay members
|
|
|
|
restart_relay "$network"
|
2012-11-29 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
enable_router() {
|
2012-11-29 20:14:28 +00:00
|
|
|
local network="$1"
|
|
|
|
local device="$2"
|
|
|
|
|
|
|
|
# Get IPv6 prefixes
|
|
|
|
local length
|
|
|
|
config_get length "$network" advertise_prefix
|
|
|
|
[ -z "$length" ] && length=64
|
|
|
|
[ "$length" -ne "0" ] && ubus call 6distributed newiface '{"network": "'"$network"'", "iface": "'"$device"'", "length": '"$length"'}'
|
|
|
|
|
|
|
|
# Start RD & DHCPv6 service
|
2012-12-22 13:00:53 +00:00
|
|
|
local router_service
|
|
|
|
config_get router_service global router_service
|
|
|
|
|
2012-12-28 12:02:22 +00:00
|
|
|
local always_default
|
|
|
|
config_get_bool always_default "$network" always_default 0
|
|
|
|
|
2012-12-22 13:00:53 +00:00
|
|
|
if [ "$router_service" == "dnsmasq" ]; then
|
|
|
|
local dnsmasq_opts
|
2012-12-23 18:22:48 +00:00
|
|
|
config_get dnsmasq_opts "$network" dnsmasq_opts
|
|
|
|
[ -z "$dnsmasq_opts" ] && dnsmasq_opts="ra-names,24h"
|
2012-12-22 13:00:53 +00:00
|
|
|
|
|
|
|
local conf="/var/etc/dnsmasq.d/ipv6-router-$network.conf"
|
|
|
|
mkdir -p $(dirname $conf)
|
2012-12-23 18:22:48 +00:00
|
|
|
echo "dhcp-range=::00ff,::ffff,constructor:$device,$dnsmasq_opts" > $conf
|
2012-12-22 13:00:53 +00:00
|
|
|
echo "enable-ra" >> $conf
|
|
|
|
/etc/init.d/dnsmasq restart
|
|
|
|
else
|
2012-12-28 12:02:22 +00:00
|
|
|
local opts=""
|
|
|
|
[ "$always_default" == "1" ] && opts="-u"
|
|
|
|
|
2012-12-22 13:00:53 +00:00
|
|
|
local pid="/var/run/ipv6-router-$network.pid"
|
2012-12-28 12:02:22 +00:00
|
|
|
start_service "/usr/sbin/6relayd -S $opts . $device" "$pid"
|
2012-12-22 13:00:53 +00:00
|
|
|
fi
|
2012-11-29 20:14:28 +00:00
|
|
|
|
|
|
|
# Try relaying if necessary
|
|
|
|
restart_master_relay "$network"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
enable_dhcpv6() {
|
2012-11-29 20:14:28 +00:00
|
|
|
local network="$1"
|
|
|
|
local device="$2"
|
|
|
|
|
|
|
|
# Configure device
|
|
|
|
conf_set "$device" accept_ra 2
|
|
|
|
conf_set "$device" forwarding 2
|
|
|
|
|
|
|
|
# Trigger RS
|
|
|
|
conf_set "$device" disable_ipv6 1
|
|
|
|
conf_set "$device" disable_ipv6 0
|
|
|
|
|
|
|
|
# Configure DHCPv6-client
|
|
|
|
local dhcp6_opts="$device"
|
|
|
|
|
|
|
|
# Configure DHCPv6-client (e.g. requested prefix)
|
|
|
|
local request_prefix
|
|
|
|
config_get request_prefix "$network" request_prefix
|
|
|
|
[ -z "$request_prefix" ] && request_prefix="auto"
|
|
|
|
[ "$request_prefix" != "no" ] && {
|
|
|
|
[ "$request_prefix" == "auto" ] && request_prefix=0
|
|
|
|
dhcp6_opts="-P$request_prefix $dhcp6_opts"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Start DHCPv6 client
|
2012-12-03 09:18:53 +00:00
|
|
|
local pid="/var/run/ipv6-dhcpv6-$network.pid"
|
2012-11-29 20:14:28 +00:00
|
|
|
start_service "/usr/sbin/odhcp6c -s/lib/ipv6/dhcpv6.sh $dhcp6_opts" "$pid"
|
|
|
|
|
|
|
|
# Refresh RA on all interfaces
|
2012-12-03 09:18:53 +00:00
|
|
|
for pid in /var/run/ipv6-router-*.pid; do
|
2012-11-29 20:14:28 +00:00
|
|
|
kill -SIGUSR1 $(cat "$pid")
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-06 20:52:40 +00:00
|
|
|
enable_6to4() {
|
|
|
|
local network="$1"
|
|
|
|
local device="$2"
|
|
|
|
local mode="$3"
|
|
|
|
|
|
|
|
local prefixlen="48"
|
|
|
|
[ "$mode" == "6rd" ] && {
|
|
|
|
local ip4prefix=$(uci_get network "$network" ip4prefixlen 0)
|
|
|
|
local ip6prefix=$(uci_get network "$network" ip6prefixlen 32)
|
|
|
|
prefixlen=$(($ip6prefix + 32 - $ip4prefix))
|
|
|
|
}
|
|
|
|
|
|
|
|
local prefix=""
|
|
|
|
network_get_ipaddr6 prefix "$network"
|
|
|
|
|
2012-12-27 14:10:17 +00:00
|
|
|
announce_prefix "$prefix/$prefixlen" "$network" "$device"
|
2012-12-06 20:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
enable_interface()
|
|
|
|
{
|
|
|
|
local network="$1"
|
|
|
|
local device="$2"
|
|
|
|
local mode=""
|
2012-12-09 13:54:22 +00:00
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
config_get mode "$network" mode
|
2012-12-09 13:54:22 +00:00
|
|
|
[ -n "$mode" -a "$mode" != "none" ] || return
|
2012-12-03 09:18:53 +00:00
|
|
|
|
|
|
|
# Compatibility with old mode names
|
|
|
|
[ "$mode" == "downstream" ] && mode=router
|
|
|
|
[ "$mode" == "upstream" ] && mode=dhcpv6
|
|
|
|
|
2012-12-28 12:02:22 +00:00
|
|
|
# Enable site-border
|
|
|
|
[ "$mode" == "static" -o "$mode" == "dhcpv6" -o "$mode" == "6to4" -o "$mode" == "6in4" ] && set_site_border "$network" "$device"
|
|
|
|
|
2012-12-03 09:18:53 +00:00
|
|
|
# Run mode startup code
|
2012-12-09 13:54:22 +00:00
|
|
|
enable_static "$network" "$device"
|
2012-12-03 09:18:53 +00:00
|
|
|
[ "$mode" == "dhcpv6" ] && enable_dhcpv6 "$network" "$device"
|
|
|
|
[ "$mode" == "router" ] && enable_router "$network" "$device"
|
2012-12-06 20:52:40 +00:00
|
|
|
[ "$mode" == "6to4" -o "$mode" == "6rd" ] && enable_6to4 "$network" "$device" "$mode"
|
2012-12-03 09:18:53 +00:00
|
|
|
[ "$mode" == "relay" ] && restart_master_relay "$network" forced
|
|
|
|
}
|