2006-07-30 03:09:09 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# Copyright (C) 2006 OpenWrt.org
|
|
|
|
|
|
|
|
# DEBUG="echo"
|
|
|
|
|
|
|
|
find_config() {
|
2006-08-27 10:05:58 +00:00
|
|
|
local iftype device iface ifaces ifn
|
2006-07-30 03:09:09 +00:00
|
|
|
for ifn in $interfaces; do
|
2006-07-30 13:21:18 +00:00
|
|
|
config_get iftype "$ifn" type
|
2006-07-30 03:09:09 +00:00
|
|
|
config_get iface "$ifn" ifname
|
2006-08-27 10:05:58 +00:00
|
|
|
case "$iftype" in
|
|
|
|
bridge) config_get ifaces "$ifn" ifnames;;
|
|
|
|
esac
|
2006-08-04 09:50:11 +00:00
|
|
|
config_get device "$ifn" device
|
2006-08-27 10:05:58 +00:00
|
|
|
for ifc in $device $iface $ifaces; do
|
2007-06-15 16:07:09 +00:00
|
|
|
[ ."$ifc" = ."$1" ] && {
|
2006-07-30 03:09:09 +00:00
|
|
|
echo "$ifn"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
scan_interfaces() {
|
2007-03-19 22:06:31 +00:00
|
|
|
local cfgfile="$1"
|
2006-08-24 13:46:47 +00:00
|
|
|
local mode iftype iface ifname device
|
2006-07-30 03:09:09 +00:00
|
|
|
interfaces=
|
|
|
|
config_cb() {
|
2006-11-03 21:57:35 +00:00
|
|
|
case "$1" in
|
|
|
|
interface)
|
|
|
|
config_set "$2" auto 1
|
|
|
|
;;
|
|
|
|
esac
|
2006-07-30 13:21:18 +00:00
|
|
|
config_get iftype "$CONFIG_SECTION" TYPE
|
|
|
|
case "$iftype" in
|
2006-07-30 03:09:09 +00:00
|
|
|
interface)
|
2006-08-23 18:47:31 +00:00
|
|
|
config_get proto "$CONFIG_SECTION" proto
|
2006-07-30 03:09:09 +00:00
|
|
|
append interfaces "$CONFIG_SECTION"
|
2006-08-27 11:11:55 +00:00
|
|
|
config_get iftype "$CONFIG_SECTION" type
|
2006-10-09 12:35:31 +00:00
|
|
|
config_get ifname "$CONFIG_SECTION" ifname
|
2008-04-02 21:27:28 +00:00
|
|
|
config_get device "$CONFIG_SECTION" device
|
|
|
|
config_set "$CONFIG_SECTION" device "${device:-$ifname}"
|
2006-08-24 13:46:47 +00:00
|
|
|
case "$iftype" in
|
|
|
|
bridge)
|
2008-04-02 21:27:28 +00:00
|
|
|
config_set "$CONFIG_SECTION" ifnames "${device:-$ifname}"
|
2006-08-24 13:46:47 +00:00
|
|
|
config_set "$CONFIG_SECTION" ifname br-"$CONFIG_SECTION"
|
|
|
|
;;
|
|
|
|
esac
|
2006-08-23 18:47:31 +00:00
|
|
|
( type "scan_$proto" ) >/dev/null 2>/dev/null && eval "scan_$proto '$CONFIG_SECTION'"
|
2006-07-30 03:09:09 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
2007-03-19 22:06:31 +00:00
|
|
|
config_load "${cfgfile:-network}"
|
2006-07-30 03:09:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add_vlan() {
|
|
|
|
local vif="${1%\.*}"
|
|
|
|
|
|
|
|
[ "$1" = "$vif" ] || ifconfig "$1" >/dev/null 2>/dev/null || {
|
|
|
|
ifconfig "$vif" up 2>/dev/null >/dev/null || add_vlan "$vif"
|
|
|
|
$DEBUG vconfig add "$vif" "${1##*\.}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-30 05:22:02 +00:00
|
|
|
# sort the device list, drop duplicates
|
|
|
|
sort_list() {
|
|
|
|
local arg="$*"
|
|
|
|
(
|
|
|
|
for item in $arg; do
|
|
|
|
echo "$item"
|
|
|
|
done
|
|
|
|
) | sort -u
|
|
|
|
}
|
|
|
|
|
2007-02-25 12:52:02 +00:00
|
|
|
# Create the interface, if necessary.
|
|
|
|
# Return status 0 indicates that the setup_interface() call should continue
|
|
|
|
# Return status 1 means that everything is set up already.
|
|
|
|
|
|
|
|
prepare_interface() {
|
2006-07-30 03:09:09 +00:00
|
|
|
local iface="$1"
|
|
|
|
local config="$2"
|
|
|
|
|
2007-02-25 12:52:02 +00:00
|
|
|
# if we're called for the bridge interface itself, don't bother trying
|
|
|
|
# to create any interfaces here. The scripts have already done that, otherwise
|
|
|
|
# the bridge interface wouldn't exist.
|
2007-11-30 09:43:05 +00:00
|
|
|
[ "br-$config" = "$iface" -o -e "$iface" ] && return 0;
|
2006-07-30 03:09:09 +00:00
|
|
|
|
2006-10-15 15:50:02 +00:00
|
|
|
ifconfig "$iface" 2>/dev/null >/dev/null && {
|
2006-12-04 23:22:06 +00:00
|
|
|
# make sure the interface is removed from any existing bridge and brought down
|
|
|
|
ifconfig "$iface" down
|
2006-10-15 15:50:02 +00:00
|
|
|
unbridge "$iface"
|
|
|
|
}
|
|
|
|
|
2006-07-30 03:09:09 +00:00
|
|
|
# Setup VLAN interfaces
|
|
|
|
add_vlan "$iface"
|
2007-04-02 09:41:56 +00:00
|
|
|
ifconfig "$iface" 2>/dev/null >/dev/null || return 0
|
2006-07-30 03:09:09 +00:00
|
|
|
|
|
|
|
# Setup bridging
|
2007-02-25 12:52:02 +00:00
|
|
|
config_get iftype "$config" type
|
2007-10-18 13:07:28 +00:00
|
|
|
config_get stp "$config" stp
|
2006-07-30 03:09:09 +00:00
|
|
|
case "$iftype" in
|
|
|
|
bridge)
|
2007-01-20 15:28:47 +00:00
|
|
|
[ -x /usr/sbin/brctl ] && {
|
|
|
|
ifconfig "br-$config" 2>/dev/null >/dev/null && {
|
2008-07-30 05:22:02 +00:00
|
|
|
local newdevs=
|
|
|
|
|
|
|
|
config_get devices "$config" device
|
|
|
|
for dev in $(sort_list "$devices" "$iface"); do
|
|
|
|
append newdevs "$dev"
|
|
|
|
done
|
|
|
|
uci_set_state network "$config" device "$newdevs"
|
2007-01-20 15:28:47 +00:00
|
|
|
$DEBUG brctl addif "br-$config" "$iface"
|
2007-02-25 12:52:02 +00:00
|
|
|
# Bridge existed already. No further processing necesary
|
2007-01-20 15:28:47 +00:00
|
|
|
} || {
|
|
|
|
$DEBUG brctl addbr "br-$config"
|
|
|
|
$DEBUG brctl setfd "br-$config" 0
|
2007-07-04 05:05:46 +00:00
|
|
|
$DEBUG ifconfig "br-$config" up
|
2007-01-20 15:28:47 +00:00
|
|
|
$DEBUG brctl addif "br-$config" "$iface"
|
2007-10-18 13:20:02 +00:00
|
|
|
$DEBUG brctl stp "br-$config" ${stp:-off}
|
2007-02-25 12:52:02 +00:00
|
|
|
# Creating the bridge here will have triggered a hotplug event, which will
|
|
|
|
# result in another setup_interface() call, so we simply stop processing
|
|
|
|
# the current event at this point.
|
2007-01-20 15:28:47 +00:00
|
|
|
}
|
2007-07-08 12:51:30 +00:00
|
|
|
ifconfig "$iface" up 2>/dev/null >/dev/null
|
2007-02-25 12:52:02 +00:00
|
|
|
return 1
|
2006-07-30 03:09:09 +00:00
|
|
|
}
|
|
|
|
;;
|
|
|
|
esac
|
2007-02-25 12:52:02 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2008-04-02 21:27:28 +00:00
|
|
|
set_interface_ifname() {
|
|
|
|
local config="$1"
|
|
|
|
local ifname="$2"
|
|
|
|
|
|
|
|
config_get device "$1" device
|
2008-04-07 16:02:50 +00:00
|
|
|
uci_set_state network "$config" ifname "$ifname"
|
|
|
|
uci_set_state network "$config" device "$device"
|
2008-04-02 21:27:28 +00:00
|
|
|
}
|
|
|
|
|
2007-02-25 12:52:02 +00:00
|
|
|
setup_interface() {
|
|
|
|
local iface="$1"
|
|
|
|
local config="$2"
|
|
|
|
local proto
|
|
|
|
local macaddr
|
|
|
|
|
|
|
|
[ -n "$config" ] || {
|
|
|
|
config=$(find_config "$iface")
|
|
|
|
[ "$?" = 0 ] || return 1
|
|
|
|
}
|
|
|
|
proto="${3:-$(config_get "$config" proto)}"
|
|
|
|
|
|
|
|
prepare_interface "$iface" "$config" || return 0
|
|
|
|
|
|
|
|
[ "$iface" = "br-$config" ] && {
|
|
|
|
# need to bring up the bridge and wait a second for
|
|
|
|
# it to switch to the 'forwarding' state, otherwise
|
|
|
|
# it will lose its routes...
|
|
|
|
ifconfig "$iface" up
|
|
|
|
sleep 1
|
|
|
|
}
|
2006-07-30 03:09:09 +00:00
|
|
|
|
|
|
|
# Interface settings
|
|
|
|
config_get mtu "$config" mtu
|
2006-12-08 11:25:10 +00:00
|
|
|
config_get macaddr "$config" macaddr
|
2007-11-30 09:43:05 +00:00
|
|
|
grep "$iface:" /proc/net/dev > /dev/null && \
|
|
|
|
$DEBUG ifconfig "$iface" ${macaddr:+hw ether "$macaddr"} ${mtu:+mtu $mtu} up
|
2008-04-02 21:27:28 +00:00
|
|
|
set_interface_ifname "$config" "$iface"
|
2006-07-30 03:09:09 +00:00
|
|
|
|
|
|
|
pidfile="/var/run/$iface.pid"
|
|
|
|
case "$proto" in
|
|
|
|
static)
|
|
|
|
config_get ipaddr "$config" ipaddr
|
|
|
|
config_get netmask "$config" netmask
|
|
|
|
config_get ip6addr "$config" ip6addr
|
2007-11-12 16:29:08 +00:00
|
|
|
[ -z "$ipaddr" -o -z "$netmask" ] && [ -z "$ip6addr" ] && return 1
|
|
|
|
|
2006-07-30 03:09:09 +00:00
|
|
|
config_get gateway "$config" gateway
|
2007-11-16 07:27:59 +00:00
|
|
|
config_get ip6gw "$config" ip6gw
|
2006-07-30 03:09:09 +00:00
|
|
|
config_get dns "$config" dns
|
2007-01-27 21:58:03 +00:00
|
|
|
config_get bcast "$config" broadcast
|
2006-07-30 03:09:09 +00:00
|
|
|
|
2007-06-28 23:07:29 +00:00
|
|
|
[ -z "$ipaddr" ] || $DEBUG ifconfig "$iface" "$ipaddr" netmask "$netmask" broadcast "${bcast:-+}"
|
2007-05-06 16:52:46 +00:00
|
|
|
[ -z "$ip6addr" ] || $DEBUG ifconfig "$iface" add "$ip6addr"
|
2008-07-30 22:49:25 +00:00
|
|
|
[ -z "$gateway" ] || $DEBUG route add default gw "$gateway" dev "$iface"
|
|
|
|
[ -z "$ip6gw" ] || $DEBUG route -A inet6 add default gw "$ip6gw" dev "$iface"
|
2007-06-16 00:01:33 +00:00
|
|
|
[ -z "$dns" ] || {
|
2006-07-30 03:09:09 +00:00
|
|
|
for ns in $dns; do
|
2007-06-16 00:01:33 +00:00
|
|
|
grep "$ns" /tmp/resolv.conf.auto 2>/dev/null >/dev/null || {
|
|
|
|
echo "nameserver $ns" >> /tmp/resolv.conf.auto
|
|
|
|
}
|
2006-07-30 03:09:09 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2007-03-04 20:31:53 +00:00
|
|
|
env -i ACTION="ifup" INTERFACE="$config" DEVICE="$iface" PROTO=static /sbin/hotplug-call "iface" &
|
2006-07-30 03:09:09 +00:00
|
|
|
;;
|
|
|
|
dhcp)
|
2006-10-24 23:59:08 +00:00
|
|
|
# prevent udhcpc from starting more than once
|
2006-10-26 01:33:36 +00:00
|
|
|
lock "/var/lock/dhcp-$iface"
|
2006-07-30 03:09:09 +00:00
|
|
|
pid="$(cat "$pidfile" 2>/dev/null)"
|
2006-10-26 01:33:36 +00:00
|
|
|
[ -d "/proc/$pid" ] && grep udhcpc "/proc/${pid}/cmdline" >/dev/null 2>/dev/null && {
|
|
|
|
lock -u "/var/lock/dhcp-$iface"
|
|
|
|
return 0
|
|
|
|
}
|
2006-07-30 03:09:09 +00:00
|
|
|
|
|
|
|
config_get ipaddr "$config" ipaddr
|
|
|
|
config_get netmask "$config" netmask
|
|
|
|
config_get hostname "$config" hostname
|
|
|
|
config_get proto1 "$config" proto
|
2007-10-06 03:12:04 +00:00
|
|
|
config_get clientid "$config" clientid
|
2006-07-30 03:09:09 +00:00
|
|
|
|
|
|
|
[ -z "$ipaddr" ] || \
|
|
|
|
$DEBUG ifconfig "$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
|
|
|
|
|
|
|
|
# don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
|
2007-06-15 16:07:09 +00:00
|
|
|
[ ."$proto1" != ."$proto" ] && dhcpopts="-n -q"
|
2007-10-06 03:12:04 +00:00
|
|
|
$DEBUG eval udhcpc -t 0 -i "$iface" ${ipaddr:+-r $ipaddr} ${hostname:+-H $hostname} ${clientid:+-c $clientid} -b -p "$pidfile" ${dhcpopts:- -R &}
|
2006-10-26 01:33:36 +00:00
|
|
|
lock -u "/var/lock/dhcp-$iface"
|
2006-07-30 03:09:09 +00:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
if ( eval "type setup_interface_$proto" ) >/dev/null 2>/dev/null; then
|
|
|
|
eval "setup_interface_$proto '$iface' '$config' '$proto'"
|
|
|
|
else
|
|
|
|
echo "Interface type $proto not supported."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2006-10-15 15:50:02 +00:00
|
|
|
unbridge() {
|
|
|
|
local dev="$1"
|
|
|
|
local brdev
|
2007-02-27 23:34:14 +00:00
|
|
|
|
|
|
|
[ -x /usr/sbin/brctl ] || return 0
|
2006-10-15 15:50:02 +00:00
|
|
|
brctl show | grep "$dev" >/dev/null && {
|
|
|
|
# interface is still part of a bridge, correct that
|
|
|
|
|
|
|
|
for brdev in $(brctl show | awk '$2 ~ /^[0-9].*\./ { print $1 }'); do
|
|
|
|
brctl delif "$brdev" "$dev" 2>/dev/null >/dev/null
|
|
|
|
done
|
|
|
|
}
|
|
|
|
}
|