Merge net.OS into net

This commit is contained in:
Roy Marples
2008-03-26 14:18:01 +00:00
parent 55858eca2e
commit ccf238f852
29 changed files with 19 additions and 19 deletions

View File

@@ -1,5 +1,6 @@
DIR= ${PREFIX}/${RC_LIB}/net
INC= dhclient.sh dhcpcd.sh macchanger.sh macnet.sh \
SRCS= ifconfig.sh.in
INC= dhclient.sh dhcpcd.sh ifconfig.sh macchanger.sh macnet.sh \
ssidnet.sh system.sh wpa_supplicant.sh
MK= ../mk

View File

@@ -0,0 +1,6 @@
SRCS+= iwconfig.sh.in
INC+= iwconfig.sh
.SUFFIXES: .sh.BSD.in
.sh.BSD.in.sh:
sed ${SED_REPLACE} ${SED_EXTRA} $< > $@

View File

@@ -0,0 +1,8 @@
SRCS+= iwconfig.sh.in
INC+= adsl.sh apipa.sh arping.sh bonding.sh br2684ctl.sh bridge.sh \
ccwgroup.sh iproute2.sh ifplugd.sh ip6to4.sh ipppd.sh \
iwconfig.sh netplugd.sh pppd.sh tuntap.sh udhcpc.sh vlan.sh
.SUFFIXES: .sh.Linux.in
.sh.Linux.in.sh:
sed ${SED_REPLACE} ${SED_EXTRA} $< > $@

View File

@@ -1 +1,3 @@
INC+= ifwatchd.sh
.SUFFIXES: .sh.BSD.in
.sh.BSD.in.sh:
sed ${SED_REPLACE} ${SED_EXTRA} $< > $@

74
net/adsl.sh Normal file
View File

@@ -0,0 +1,74 @@
# Copyright 2004-2007 Gentoo Foundation
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
adsl_depend()
{
program /usr/sbin/adsl-start /usr/sbin/pppoe-start
before dhcp
}
adsl_setup_vars()
{
local startstop="$1" cfgexe=
if [ -x /usr/sbin/pppoe-start ]; then
exe="/usr/sbin/pppoe-${startstop}"
cfgexe=pppoe-setup
else
exe="/usr/sbin/adsl-${startstop}"
cfgexe=adsl-setup
fi
# Decide which configuration to use. Hopefully there is an
# interface-specific one
cfgfile="/etc/ppp/pppoe-${IFACE}.conf"
[ -f "${cfgfile}" ] || cfgfile="/etc/ppp/pppoe.conf"
if [ ! -f "${cfgfile}" ]; then
eerror "no pppoe.conf file found!"
eerror "Please run ${cfgexe} to create one"
return 1
fi
return 0
}
adsl_start()
{
local exe= cfgfile= user=
adsl_setup_vars start || return 1
# Might or might not be set in conf.d/net
eval user=\$adsl_user_${IFVAR}
# Start ADSL with the cfgfile, but override ETH and PIDFILE
einfo "Starting ADSL for ${IFACE}"
(
cat "${cfgfile}";
echo "ETH=${IFACE}";
echo "PIDFILE=/var/run/rp-pppoe-${IFACE}.pid";
[ -n "${user}" ] && echo "USER=${user}";
) | ${exe} >/dev/null
eend $?
}
adsl_stop()
{
local exe= cfgfile=
[ ! -f /var/run/rp-pppoe-"${IFACE}".pid ] && return 0
adsl_setup_vars stop || return 1
einfo "Stopping ADSL for ${IFACE}"
(
cat "${cfgfile}";
echo "ETH=${IFACE}";
echo "PIDFILE=/var/run/rp-pppoe-${IFACE}.pid";
) | ${exe} >/dev/null
eend $?
return 0
}

48
net/apipa.sh Normal file
View File

@@ -0,0 +1,48 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
apipa_depend()
{
program /sbin/arping
}
_random()
{
local r=${RANDOM}
if [ -n "${r}" ]; then
echo "${r}"
else
uuidgen | sed -n -e 's/[^[:digit:]]//g' -e 's/\(^.\{1,7\}\).*/\1/p'
fi
}
apipa_start()
{
local iface="$1" i1= i2= addr= i=0
_exists true || return 1
einfo "Searching for free addresses in 169.254.0.0/16"
eindent
while [ ${i} -lt 64516 ]; do
i1=$((($(_random) % 255) + 1))
i2=$((($(_random) % 255) + 1))
addr="169.254.${i1}.${i2}"
vebegin "${addr}/16"
if ! arping_address "${addr}"; then
eval config_${config_index}="\"${addr}/16 broadcast 169.254.255.255\""
config_index=$((${config_index} - 1))
veend 0
eoutdent
return 0
fi
i=$((${i} + 1))
done
eerror "No free address found!"
eoutdent
return 1
}

131
net/arping.sh Normal file
View File

@@ -0,0 +1,131 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
arping_depend()
{
program /sbin/arping /usr/sbin/arping2
before interface
}
arping_address()
{
local ip=${1%%/*} mac="$2" spoof="$3" foundmac= i= w= opts=
# We only handle IPv4 addresses
case "${ip}" in
0.0.0.0|0) return 1;;
*.*.*.*);;
*) return 1;;
esac
# We need to bring the interface up to test
_exists "${iface}" || return 1
_up "${iface}"
eval w=\$arping_wait_${IFVAR}
[ -z "${w}" ] && w=${arping_wait:-5}
if type arping2 >/dev/null 2>&1; then
if [ -n "${spoof}" ]; then
opts="${opts} -S ${spoof}"
else
[ -z "$(_get_inet_address)" ] && opts="${opts} -0"
fi
while [ ${w} -gt 0 -a -z "${foundmac}" ]; do
foundmac="$(arping2 ${opts} -r -c 1 -i "${IFACE}" "${ip}" 2>/dev/null | \
sed -e 'y/abcdef/ABCDEF/')"
w=$((${w} - 1))
done
else
[ -z "$(_get_inet_address)" ] && opts="${opts} -D"
foundmac="$(arping -w "${w}" ${opts} -f -I "${IFACE}" "${ip}" 2>/dev/null | \
sed -n -e 'y/abcdef/ABCDEF/' -e 's/.*\[\([^]]*\)\].*/\1/p')"
fi
[ -z "${foundmac}" ] && return 1
if [ -n "${mac}" ]; then
if [ "${mac}" != "${foundmac}" ]; then
vewarn "Found ${ip} but MAC ${foundmac} does not match"
return 1
fi
fi
return 0
}
_arping_in_config()
{
_get_array "config_${IFVAR}" | while read i; do
[ "${i}" = "arping" ] && return 1
done
return 1
}
arping_start()
{
local gateways= x= conf= i=
einfo "Pinging gateways on ${IFACE} for configuration"
eval gateways=\$gateways_${IFVAR}
if [ -z "${gateways}" ]; then
eerror "No gateways have been defined (gateways_${IFVAR}=\"...\")"
return 1
fi
eindent
for x in ${gateways}; do
local IFS=,
set -- ${x}
local ip=$1 mac=$2 spoof=$3 extra=
unset IFS
if [ -n "${mac}" ]; then
mac="$(echo "${mac}" | tr '[:lower:]' '[:upper:]')"
extra="(MAC ${mac})"
fi
vebegin "${ip} ${extra}"
if arping_address "${ip}" "${mac}" "${spoof}"; then
local IFS=.
for i in ${ip}; do
if [ "${#i}" = "2" ]; then
conf="${conf}0${i}"
elif [ "${#i}" = "1" ]; then
conf="${conf}00${i}"
else
conf="${conf}${i}"
fi
done
unset IFS
[ -n "${mac}" ] && conf="${conf}_$(echo "${mac}" | sed -e 's/://g')"
eend 0
eoutdent
veinfo "Configuring ${IFACE} for ${ip} ${extra}"
_configure_variables ${conf}
# Call the system module as we've aleady passed it by ....
# And it *has* to be pre_start for other things to work correctly
system_pre_start
# Ensure that we have a valid config - ie arping is no longer there
local IFS="$__IFS"
for i in $(_get_array "config_${IFVAR}"); do
if [ "${i}" = "arping" ]; then
eend 1 "No config found for ${ip} (config_${conf}=\"...\")"
continue 2
fi
done
unset IFS
_load_config
return 0
fi
veend 1
done
eoutdent
return 1
}

101
net/bonding.sh Normal file
View File

@@ -0,0 +1,101 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
bonding_depend()
{
before interface macchanger
program /sbin/ifenslave
}
_config_vars="$_config_vars slaves"
_is_bond()
{
[ -f "/proc/net/bonding/${IFACE}" ]
}
bonding_pre_start()
{
local s= slaves="$(_get_array "slaves_${IFVAR}")"
[ -z "${slaves}" ] && return 0
# Load the kernel module if required
if [ ! -d /proc/net/bonding ]; then
if ! modprobe bonding; then
eerror "Cannot load the bonding module"
return 1
fi
fi
# We can create the interface name we like now, but this
# requires sysfs
if ! _exists && [ -d /sys/class/net ]; then
echo "+${IFACE}" > /sys/class/net/bonding_masters
fi
_exists true || return 1
if ! _is_bond; then
eerror "${IFACE} is not capable of bonding"
return 1
fi
ebegin "Adding slaves to ${IFACE}"
eindent
einfo "${slaves}"
# Check that our slaves exist
(
for IFACE in ${slaves}; do
_exists true || return 1
done
# Must force the slaves to a particular state before adding them
for IFACE in ${slaves}; do
_delete_addresses
_up
done
)
# now force the master to up
_up
# finally add in slaves
eoutdent
/sbin/ifenslave "${IFACE}" ${slaves} >/dev/null
eend $?
return 0 #important
}
bonding_stop()
{
_is_bond || return 0
local slaves= s=
slaves=$( \
sed -n -e 's/^Slave Interface: //p' "/proc/net/bonding/${IFACE}" \
| tr '\n' ' ' \
)
[ -z "${slaves}" ] && return 0
# remove all slaves
ebegin "Removing slaves from ${IFACE}"
eindent
einfo "${slaves}"
eoutdent
/sbin/ifenslave -d "${IFACE}" ${slaves}
# reset all slaves
(
for IFACE in ${slaves}; do
if _exists; then
_delete_addresses
_down
fi
done
)
eend 0
return 0
}

59
net/br2684ctl.sh Normal file
View File

@@ -0,0 +1,59 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
_br2684ctl()
{
if [ -x /usr/sbin/br2684ctl ]; then
echo /usr/sbin/br2684ctl
else
echo /sbin/br2684ctl
fi
}
br2684ctl_depend()
{
before ppp
program start $(_br2684ctl)
}
_config_vars="$_config_vars bridge bridge_add brctl"
br2684ctl_pre_start()
{
local opts=
eval opts=\$br2684ctl_${IFVAR}
[ -z "${opts}" ] && return 0
if [ "${IFACE#nas[0-9]*}" = "${IFACE}" ]; then
eerror "Interface must be called nas[0-9] for RFC 2684 Bridging"
return 1
fi
case " ${opts} " in
*" -b "*|*" -c "*)
eerror "The -b and -c options are not allowed for br2684ctl_${IVAR}"
return 1
;;
*" -a "*);;
*)
eerror "-a option (VPI and VCI) is required in br2684ctl_${IFVAR}"
return 1
;;
esac
einfo "Starting RFC 2684 Bridge control on ${IFACE}"
start-stop-daemon --start --exec $(_br2684ctl) --background \
--make-pidfile --pidfile "/var/run/br2684ctl-${IFACE}.pid" \
-- -c "${IFACE#nas*}" ${opts}
eend $?
}
br2684ctl_post_stop()
{
local pidfile="/var/run/br2684ctl-${IFACE}.pid"
[ -e "${pidfile}" ] || return 0
einfo "Stopping RFC 2684 Bridge control on ${IFACE}"
start-stop-daemon --stop --quiet --pidfile "${pidfile}"
eend $?
}

123
net/bridge.sh Normal file
View File

@@ -0,0 +1,123 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
bridge_depend()
{
before interface macnet
program /sbin/brctl
}
_config_vars="$_config_vars bridge bridge_add brctl"
_is_bridge()
{
brctl show 2>/dev/null | grep -q "^${IFACE}[[:space:]]"
}
bridge_pre_start()
{
local ports= brif= iface="${IFACE}" e= x=
local ports="$(_get_array "bridge_${IFVAR}")"
local opts="$(_get_array "brctl_${IFVAR}")"
eval brif=\$bridge_add_${IFVAR}
[ -z "${ports}" -a -z "${brif}" -a -z "${opts}" ] && return 0
[ -n "${ports}" ] && bridge_post_stop
(
if [ -z "${ports}" -a -n "${brif}" ]; then
ports="${IFACE}"
IFACE="${brif}"
else
ports="${ports}"
metric=1000
fi
if ! _is_bridge; then
ebegin "Creating bridge ${IFACE}"
if ! brctl addbr "${IFACE}"; then
eend 1
return 1
fi
fi
local IFS="$__IFS"
for x in ${opts}; do
unset IFS
set -- ${x}
x=$1
shift
set -- "${x}" "${IFACE}" "$@"
brctl "$@"
done
unset IFS
if [ -n "${ports}" ]; then
einfo "Adding ports to ${IFACE}"
eindent
local OIFACE="${IFACE}"
for x in ${ports}; do
ebegin "${x}"
local IFACE="${x}"
_set_flag promisc
_up
if ! brctl addif "${OIFACE}" "${x}"; then
_set_flag -promisc
eend 1
return 1
fi
eend 0
done
eoutdent
fi
) || return 1
# Bring up the bridge
_up
}
bridge_post_stop()
{
local port= ports= delete=false extra=
if _is_bridge; then
ebegin "Destroying bridge ${IFACE}"
_down
ports="$(brctl show 2>/dev/null | \
sed -n -e '/^'"${IFACE}"'[[:space:]]/,/^\S/ { /^\('"${IFACE}"'[[:space:]]\|\t\)/s/^.*\t//p }')"
delete=true
iface=${IFACE}
eindent
else
# Work out if we're added to a bridge for removal or not
eval set -- $(brctl show 2>/dev/null | sed -e "s/'/'\\\\''/g" -e "s/$/'/g" -e "s/^/'/g")
local line=
for line; do
set -- ${line}
if [ "$3" = "${IFACE}" ]; then
iface=$1
break
fi
done
[ -z "${iface}" ] && return 0
extra=" from ${iface}"
fi
for port in ${ports}; do
ebegin "Removing port ${port}${extra}"
local IFACE="${port}"
_set_flag -promisc
brctl delif "${iface}" "${port}"
eend $?
done
if ${delete}; then
eoutdent
brctl delbr "${iface}"
eend $?
fi
return 0
}

67
net/ccwgroup.sh Normal file
View File

@@ -0,0 +1,67 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
_config_vars="$_config_vars ccwgroup"
ccwgroup_depend()
{
before interface
}
ccwgroup_pre_start()
{
local ccwgroup="$(_get_array "ccwgroup_${IFVAR}")"
[ -z "${ccwgroup}" ] && return 0
if [ ! -d /sys/bus/ccwgroup ]; then
modprobe qeth
if [ ! -d /sys/bus/ccwgroup ]; then
eerror "ccwgroup support missing in kernel"
return 1
fi
fi
einfo "Enabling ccwgroup on ${IFACE}"
local x= ccw= first= layer2=
for x in ${ccwgroup}; do
[ -z "${first}" ] && first=${x}
ccw="${ccw}${ccw:+,}${x}"
done
if [ -e /sys/devices/qeth/"${first}" ]; then
echo "0" >/sys/devices/qeth/"${first}"/online
else
echo "${ccw}" >/sys/bus/ccwgroup/drivers/qeth/group
fi
eval layer2=\$qeth_layer2_${IFVAR}
echo "${layer2:-0}" > /sys/devices/qeth/"${first}"/layer2
echo "1" >/sys/devices/qeth/"${first}"/online
eend $?
}
ccwgroup_pre_stop()
{
# Erase any existing ccwgroup to be safe
service_set_value ccwgroup_device ""
[ ! -L /sys/class/net/"${FACE}"/driver ] && return 0
local driver="$(readlink /sys/class/net/"${IFACE}"/driver)"
case "${diver}" in
*/bus/ccwgroup/*);;
*) return 0;;
esac
local device="$(readlink /sys/class/net/"${IFACE}"/device)"
device=${device##*/}
service_set_value ccwgroup_device "${device}"
}
ccwgroup_post_stop()
{
local device="$(service_get_value ccwgroup_device)"
[ -z "${device}" ] && return 0
einfo "Disabling ccwgroup on ${iface}"
echo "0" >/sys/devices/qeth/"${device}"/online
echo "1" >/sys/devices/qeth/"${device}"/ungroup
eend $?
}

221
net/clip.sh Normal file
View File

@@ -0,0 +1,221 @@
# Copyright 2005-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
clip_depend()
{
program /usr/sbin/atmsigd
before interface
}
_config_vars="$_config_vars clip"
# This starts a service. Albeit atmsigd, ilmid and atmarpd do allow for back-
# grounding through the -b option, its usage causes them to be sensible to
# SIGHUP, which is sent to all daemons when console detaches right after
# startup. This is probably due to the fact that these programs don't detach
# themself from the controlling terminal when backgrounding... The only way I
# see to overcame this is to use the --background option in start-stop-daemon,
# which is reported as a "last resort" method, but it acts correctly about this.
atmclip_svc_start()
{
ebegin "Starting $2 Daemon ($1)"
start-stop-daemon --start \
--background \
--make-pidfile --pidfile "/var/run/$1.pid" \
--exec "/usr/sbin/$1" -- -l syslog
eend $?
}
atmclip_svcs_start()
{
einfo "First CLIP instance: starting ATM CLIP daemons"
eindent
if yesno ${clip_full:-yes}; then
atmclip_svc_start atmsigd "Signaling" && \
atmclip_svc_start ilmid "Integrated Local Management Interface" && \
atmclip_svc_start atmarpd "Address Resolution Protocol"
else
atmclip_svc_start atmarpd "Address Resolution Protocol"
fi
local r=$?
eoutdent
return ${r}
}
atmclip_svc_stop()
{
ebegin "Stopping $2 Daemon ($1)"
start-stop-daemon --stop --quiet \
--pidfile "/var/run/$1.pid" \
--exec "/usr/sbin/$1"
eend $?
}
atmclip_svcs_stop()
{
einfo "Last CLIP instance: stopping ATM CLIP daemons"
eindent
# Heartake operation!
sync
atmclip_svc_stop atmarpd "Address Resolution Protocol"
if yesno ${clip_full:-yes}; then
atmclip_svc_stop ilmid "Integrated Local Management Interface"
atmclip_svc_stop atmsigd "Signaling"
fi
eoutdent
}
are_atmclip_svcs_running()
{
start-stop-daemon --quiet --test --stop --pidfile /var/run/atmarpd.pid || return 1
if yesno ${clip_full:-yes}; then
start-stop-daemon --quiet --test --stop --pidfile /var/run/ilmid.pid || return 1
start-stop-daemon --quiet --test --stop --pidfile /var/run/atmsigd.pid || return 1
fi
return 0
}
clip_pre_start()
{
local clip=
eval clip=\$clip_${IFVAR}
[ -z "${clip}" ] && return 0
if [ ! -r /proc/net/atm/arp ]; then
modprobe clip && sleep 2
if [ ! -r /proc/net/atm/arp ]; then
eerror "You need first to enable kernel support for ATM CLIP"
return 1
fi
fi
local started_here=
if ! are_atmclip_svcs_running; then
atmclip_svcs_start || return 1
started_here=1
fi
if ! _exists; then
ebegin "Creating CLIP interface ${IFACE}"
atmarp -c "${IFACE}"
if ! eend $?; then
[ -z "${started_here}" ] && atmclip_svcs_stop
return 1
fi
fi
return 0
}
clip_post_start()
{
local clip="$(_get_array "clip_${IFVAR}")"
[ -z "${clip}" ] && return 0
are_atmclip_svcs_running || return 1
# The atm tools (atmarpd?) are silly enough that they would not work with
# iproute2 interface setup as opposed to the ifconfig one.
# The workaround is to temporarily toggle the interface state from up
# to down and then up again, without touching its address. This (should)
# work with both iproute2 and ifconfig.
_down
_up
# Now the real thing: create a PVC with our peer(s).
# There are cases in which the ATM interface is not yet
# ready to establish new VCCs. In that cases, atmarp would
# fail. Here we allow 10 retries to happen every 2 seconds before
# reporting problems. Also, when no defined VC can be established,
# we stop the ATM daemons.
local has_failures= i=
for i in ${clip}; do
local IFS=","
set -- ${i}
unset IFS
local peerip="$1"; shift
local ifvpivci="$1"; shift
ebegin "Creating PVC ${ifvpivci} for peer ${peerip}"
local nleftretries=10 emsg= ecode=
while [ ${nleftretries} -gt 0 ]; do
nleftretries=$((${nleftretries} - 1))
emsg="$(atmarp -s "${peerip}" "${ifvpivci}" "$@" 2>&1)"
ecode=$? && break
sleep 2
done
if ! eend ${ecode}; then
eerror "Creation failed for PVC ${ifvpivci}: ${emsg}"
has_failures=1
fi
done
if [ -n "${has_failures}" ]; then
clip_pre_stop "${iface}"
clip_post_stop "${iface}"
return 1
else
return 0
fi
}
clip_pre_stop()
{
are_atmclip_svcs_running || return 0
# We remove all the PVCs which may have been created by
# clip_post_start for this interface. This shouldn't be
# needed by the ATM stack, but sometimes I got a panic
# killing CLIP daemons without previously vacuuming
# every active CLIP PVCs.
# The linux 2.6's ATM stack is really a mess...
local itf= t= encp= idle= ipaddr= left=
einfo "Removing PVCs on this interface"
eindent
{
read left && \
while read itf t encp idle ipaddr left; do
if [ "${itf}" = "${IFACE}" ]; then
ebegin "Removing PVC to ${ipaddr}"
atmarp -d "${ipaddr}"
eend $?
fi
done
} < /proc/net/atm/arp
eoutdent
}
# Here we should teorically delete the interface previously created in the
# clip_pre_start function, but there is no way to "undo" an interface creation.
# We can just leave the interface down. "ifconfig -a" will still list it...
# Also, here we can stop the ATM CLIP daemons if there is no other CLIP PVC
# outstanding. We check this condition by inspecting the /proc/net/atm/arp file.
clip_post_stop()
{
are_atmclip_svcs_running || return 0
local itf= left= hasothers=
{
read left && \
while read itf left; do
if [ "${itf}" != "${IFACE}" ]; then
hasothers=1
break
fi
done
} < /proc/net/atm/arp
if [ -z "${hasothers}" ]; then
atmclip_svcs_stop || return 1
fi
}

241
net/ifconfig.sh.BSD.in Normal file
View File

@@ -0,0 +1,241 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
ifconfig_depend()
{
program /sbin/ifconfig
provide interface
}
_exists()
{
# Only FreeBSD sees to have /dev/net .... is there something
# other than ifconfig we can use for the others?
if [ -d /dev/net ]; then
[ -e /dev/net/"${IFACE}" ]
else
ifconfig "${IFACE}" >/dev/null 2>&1
fi
}
_up()
{
ifconfig "${IFACE}" up
}
_down()
{
ifconfig "${IFACE}" down
}
_ifindex()
{
local x= i=1
case "${RC_UNAME}" in
FreeBSD|DragonFly)
for x in /dev/net[0-9]*; do
if [ "${x}" -ef /dev/net/"${IFACE}" ]; then
echo "${x#/dev/net}"
return 0
fi
i=$((${i} + 1))
done
;;
default)
for x in $(ifconfig -l); do
if [ "${x}" = "${IFACE}" ]; then
echo "${i}"
return 0
fi
i=$((${i} + 1))
done
;;
esac
# Return the next available index
echo "${i}"
return 1
}
_ifconfig_ent()
{
LC_ALL=C ifconfig "${IFACE}" 2>/dev/null | while read ent rest; do
case "${ent}" in
$1) echo "${rest}";;
esac
done
}
_get_mac_address()
{
local ent="ether"
case "${RC_UNAME}" in
NetBSD|OpenBSD) ent="address:";;
esac
case $(_ifconfig_ent "${ent}") in
00:00:00:00:00:00);;
44:44:44:44:44:44);;
FF:FF:FF:FF:FF:FF);;
"") return 1;;
*) echo "${address}";;
esac
return 0;
}
_is_wireless()
{
case "$(_ifconfig_ent "media:")" in
IEEE802.11*|"IEEE 802.11 Wireless"*) return 0;;
*) return 1;;
esac
}
_get_inet_address()
{
local inet= address= n= netmask= rest=
LC_ALL=C ifconfig "${IFACE}" | while read inet address n netmask rest; do
if [ "${inet}" = "inet" ]; then
echo "${address}/$(_netmask2cidr "${netmask}")"
return 0
fi
done
}
_add_address()
{
local inet6=
case "$@" in
*:*) inet6=inet6;;
esac
if [ "${metric:-0}" != "0" ]; then
set -- "$@" metric ${metric}
fi
# ifconfig doesn't like CIDR addresses
case "${RC_UNAME}" in
OpenBSD)
local ip="${1%%/*}" cidr="${1##*/}" netmask=
if [ -n "${cidr}" -a "${cidr}" != "${ip}" ]; then
netmask="$(_cidr2netmask "${cidr}")"
shift
set -- "${ip}" netmask "${netmask}" "$@"
fi
;;
esac
ifconfig "${IFACE}" ${inet6} "$@" alias
}
_add_route()
{
if [ $# -gt 3 ]; then
if [ "$3" = "gw" -o "$3" = "via" ]; then
local one=$1 two=$2
shift; shift; shift
set -- "${one}" "${two}" "$@"
fi
fi
case "$@" in
*:*) route add -inet6 "$@";;
*) route add "$@";;
esac
}
_delete_addresses()
{
einfo "Removing addresses"
eindent
LC_ALL=C ifconfig "${IFACE}" | while read inet address rest; do
case "${inet}" in
inet|inet6)
case "${address}" in
*"%${IFACE}"|::1) continue;;
127.0.0.1) [ "${IFACE}" = "lo0" ] && continue;;
esac
einfo "${address}"
ifconfig "${IFACE}" "${inet}" "${address}" -alias
eend $?
;;
esac
done
eoutdent
return 0
}
_show_address()
{
einfo "received address $(_get_inet_address "${IFACE}")"
}
_has_carrier()
{
case "$(_ifconfig_ent "status:")" in
""|active|associated) return 0;;
*) return 1;;
esac
}
ifconfig_pre_start()
{
local config="$(_get_array "ifconfig_${IFVAR}")" conf= arg= args=
local IFS="$__IFS"
[ -z "${config}" ] && return 0
veinfo "Running ifconfig commands"
eindent
for conf in ${config}; do
unset IFS
args=
for arg in ${conf}; do
case ${arg} in
[Dd][Hh][Cc][Pp]);;
[Nn][Oo][Aa][Uu][Tt][Oo]);;
[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]);;
[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]);;
[Ww][Pp][Aa]);;
*) args="${args} ${arg}";;
esac
done
[ -z "${args}" ] && continue
vebegin "ifconfig${args}"
eval ifconfig "${IFACE}" "${args}"
veend $?
done
eoutdent
return 0
}
_ifconfig_ipv6_tentative()
{
local inet= address= rest=
LC_ALL=C ifconfig "${IFACE}" | while read inet address rest; do
case "${inet}" in
inet6)
case "${rest}" in
*" "tentative*) return 2;;
esac
;;
esac
done
[ $? = 2 ]
}
ifconfig_post_start()
{
if _ifconfig_ipv6_tentative; then
ebegin "Waiting for IPv6 addresses"
while true; do
_ifconfig_ipv6_tentative || break
done
eend 0
fi
}

291
net/ifconfig.sh.Linux.in Normal file
View File

@@ -0,0 +1,291 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
ifconfig_depend()
{
program /sbin/ifconfig
provide interface
}
_up()
{
ifconfig "${IFACE}" up
}
_down()
{
ifconfig "${IFACE}" down
}
_exists()
{
grep -Eq "^[[:space:]]*${IFACE}:" /proc/net/dev
}
_ifindex()
{
local line= i=-2
while read line; do
i=$((${i} + 1))
[ ${i} -lt 1 ] && continue
case "${line}" in
"${IFACE}:"*) echo "${i}"; return 0;;
esac
done < /proc/net/dev
# Return the next available index
i=$((${i} + 1))
echo "${i}"
return 1
}
_is_wireless()
{
# Support new sysfs layout
[ -d /sys/class/net/"${IFACE}"/wireless ] && return 0
[ ! -e /proc/net/wireless ] && return 1
grep -Eq "^[[:space:]]*${IFACE}:" /proc/net/wireless
}
_set_flag()
{
ifconfig "${IFACE}" "$1"
}
_get_mac_address()
{
local mac=$(LC_ALL=C ifconfig "${IFACE}" | \
sed -n -e 's/.* HWaddr \(..:..:..:..:..:..\).*/\1/p')
case "${mac}" in
00:00:00:00:00:00);;
44:44:44:44:44:44);;
FF:FF:FF:FF:FF:FF);;
"");;
*) echo "${mac}"; return 0;;
esac
return 1
}
_set_mac_address()
{
ifconfig "${IFACE}" hw ether "$1"
}
_get_inet_address()
{
set -- $(LC_ALL=C ifconfig "${IFACE}" |
sed -n -e 's/.*inet addr:\([^ ]*\).*Mask:\([^ ]*\).*/\1 \2/p')
[ -z "$1" ] && return 1
echo -n "$1"
shift
echo "/$(_netmask2cidr "$1")"
}
_get_inet_addresses()
{
local iface=${IFACE} i=0
local addrs="$(_get_inet_address)"
while true; do
local IFACE="${iface}:${i}"
_exists || break
local addr="$(_get_inet_address)"
[ -n "${addr}" ] && addrs="${addrs}${addrs:+ }${addr}"
i=$((${i} + 1))
done
echo "${addrs}"
}
_cidr2netmask()
{
local cidr="$1" netmask="" done=0 i=0 sum=0 cur=128
local octets= frac=
local octets=$((${cidr} / 8))
local frac=$((${cidr} % 8))
while [ ${octets} -gt 0 ]; do
netmask="${netmask}.255"
octets=$((${octets} - 1))
done=$((${done} + 1))
done
if [ ${done} -lt 4 ]; then
while [ ${i} -lt ${frac} ]; do
sum=$((${sum} + ${cur}))
cur=$((${cur} / 2))
i=$((${i} + 1))
done
netmask="${netmask}.${sum}"
done=$((${done} + 1))
while [ ${done} -lt 4 ]; do
netmask="${netmask}.0"
done=$((${done} + 1))
done
fi
echo "${netmask#.*}"
}
_add_address()
{
if [ "$1" = "127.0.0.1/8" -a "${IFACE}" = "lo" ]; then
ifconfig "${IFACE}" "$@" 2>/dev/null
return 0
fi
case "$1" in
*:*) ifconfig "${IFACE}" inet6 add "$@"; return $?;;
esac
# IPv4 is tricky - ifconfig requires an aliased device
# for multiple addresses
local iface="${IFACE}"
if LC_ALL=C ifconfig "${iface}" | grep -Eq "\<inet addr:.*"; then
# Get the last alias made for the interface and add 1 to it
i=$(ifconfig | sed '1!G;h;$!d' | grep -m 1 -o "^${iface}:[0-9]*" \
| sed -n -e 's/'"${iface}"'://p')
i=$((${i:-0} + 1))
iface="${iface}:${i}"
fi
# ifconfig doesn't like CIDR addresses
local ip="${1%%/*}" cidr="${1##*/}" netmask=
if [ -n "${cidr}" -a "${cidr}" != "${ip}" ]; then
netmask="$(_cidr2netmask "${cidr}")"
shift
set -- "${ip}" netmask "${netmask}" "$@"
fi
local arg= cmd=
while [ -n "$1" ]; do
case "$1" in
brd)
if [ "$2" = "+" ]; then
shift
else
cmd="${cmd} broadcast"
fi
;;
peer) cmd="${cmd} pointtopoint";;
*) cmd="${cmd} $1";;
esac
shift
done
ifconfig "${iface}" ${cmd}
}
_add_route()
{
local inet6=
if [ -n "${metric}" ]; then
set -- "$@" metric ${metric}
fi
if [ $# -eq 3 ]; then
set -- "$1" "$2" gw "$3"
elif [ "$3" = "via" ]; then
local one=$1 two=$2
shift; shift; shift
set -- "${one}" "${two}" gw "$@"
fi
case "$@" in
*:*)
inet6="-A inet6"
[ "$1" = "-net" ] && shift
;;
esac
route ${inet6} add "$@" dev "${IFACE}"
}
_delete_addresses()
{
# We don't remove addresses from aliases
case "${IFACE}" in
*:*) return 0;;
esac
einfo "Removing addresses"
eindent
# iproute2 can add many addresses to an iface unlike ifconfig ...
# iproute2 added addresses cause problems for ifconfig
# as we delete an address, a new one appears, so we have to
# keep polling
while true; do
local addr=$(_get_inet_address)
[ -z "${addr}" ] && break
if [ "${addr}" = "127.0.0.1/8" ]; then
# Don't delete the loopback address
[ "${IFACE}" = "lo" -o "${IFACE}" = "lo0" ] && break
fi
einfo "${addr}"
ifconfig "${IFACE}" 0.0.0.0 || break
done
# Remove IPv6 addresses
local addr=
for addr in $(LC_ALL=C ifconfig "${IFACE}" | \
sed -n -e 's/^.*inet6 addr: \([^ ]*\) Scope:[^L].*/\1/p'); do
[ "${addr}" = "::1/128" -a "${IFACE}" = "lo" ] && continue
einfo "${addr}"
ifconfig "${IFACE}" inet6 del "${addr}"
done
return 0
}
_has_carrier()
{
return 0
}
_tunnel()
{
iptunnel "$@"
}
ifconfig_pre_start()
{
# MTU support
local mtu=
eval mtu=\$mtu_${IFVAR}
[ -n "${mtu}" ] && ifconfig "${IFACE}" mtu "${mtu}"
# TX Queue Length support
local len=
eval len=\$txqueuelen_${IFVAR}
[ -n "${len}" ] && ifconfig "${IFACE}" txqueuelen "${len}"
local tunnel=
eval tunnel=\$iptunnel_${IFVAR}
[ -z "${tunnel}" ] && return 0
# Set our base metric to 1000
metric=1000
ebegin "Creating tunnel ${IFVAR}"
iptunnel add "${tunnel}"
eend $?
}
ifconfig_post_stop()
{
# Don't delete sit0 as it's a special tunnel
[ "${IFACE}" = "sit0" ] && return 0
[ -z "$(iptunnel show "${IFACE}" 2>/dev/null)" ] && return 0
ebegin "Destroying tunnel ${IFACE}"
iptunnel del "${IFACE}"
eend $?
}

92
net/ifplugd.sh Normal file
View File

@@ -0,0 +1,92 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
_config_vars="$_config_vars plug_timeout"
ifplugd_depend()
{
program start /usr/sbin/ifplugd
after macnet rename
before interface
provide plug
}
ifplugd_pre_start()
{
local pidfile="/var/run/ifplugd.${IFACE}.pid" timeout= args=
# We don't start ifplugd if we're being called from the background
yesno ${IN_BACKGROUND} && return 0
_exists || return 0
# We need a valid MAC address
# It's a basic test to ensure it's not a virtual interface
if ! _get_mac_address >/dev/null 2>&1; then
vewarn "ifplugd only works on interfaces with a valid MAC address"
return 0
fi
# We don't work on bonded, bridges, tun/tap, vlan or wireless
for f in bond bridge tuntap vlan wireless; do
if type "_is_${f}" >/dev/null 2>&1; then
if _is_${f}; then
veinfo "netplug does not work with" "${f}"
return 0
fi
fi
done
ebegin "Starting ifplugd on" "${IFACE}"
eval args=\$ifplugd_${IFVAR}
# Mark the us as inactive so netplug can restart us
mark_service_inactive
# Start ifplugd
eval start-stop-daemon --start --exec /usr/sbin/ifplugd \
--pidfile "${pidfile}" -- "${args}" --iface="${IFACE}"
eend "$?" || return 1
eindent
eval timeout=\$plug_timeout_${IFVAR}
[ -z "${timeout}" ] && timeout=-1
if [ ${timeout} -eq 0 ]; then
ewarn "WARNING: infinite timeout set for ${IFACE} to come up"
elif [ ${timeout} -lt 0 ]; then
einfo "Backgrounding ..."
exit 1
fi
veinfo "Waiting for ${IFACE} to be marked as started"
local i=0
while true; do
if service_started; then
_show_address
exit 0
fi
sleep 1
[ ${timeout} -eq 0 ] && continue
i=$((${i} + 1))
[ ${i} -ge ${timeout} ] && break
done
eend 1 "Failed to configure ${IFACE} in the background"
exit 1
}
ifplugd_stop()
{
yesno ${IN_BACKGROUND} && return 0
local pidfile="/var/run/ifplugd.${IFACE}.pid"
[ ! -e "${pidfile}" ] && return 0
ebegin "Stopping ifplugd on" "${IFACE}"
start-stop-daemon --stop --quiet --exec /usr/sbin/ifplugd \
--pidfile "${pidfile}" --signal QUIT
eend $?
}

99
net/ip6to4.sh Normal file
View File

@@ -0,0 +1,99 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
_config_vars="$_config_vars link suffix relay"
ip6to4_depend()
{
after interface
}
ip6to4_start()
{
case " ${MODULES} " in
*" ifconfig "*)
if [ "${IFACE}" != "sit0" ]; then
eerror "ip6to4 can only work on the sit0 interface using ifconfig"
eerror "emerge sys-apps/iproute2 to use other interfaces"
return 1
fi
esac
local host= suffix= relay= addr= iface=${IFACE} new= localip=
eval host=\$link_${IFVAR}
if [ -z "${host}" ]; then
eerror "link_${IFVAR} not set"
return 1
fi
eval suffix=\${suffix_${IFVAR}:-1}
eval relay=\${relay_${IFVAR}:-192.88.99.1}
IFACE=${host}
addrs=$(_get_inet_addresses)
IFACE=${iface}
if [ -z "${addrs}" ]; then
eerror "${host} is not configured with an IPv4 address"
return 1
fi
for addr in ${addrs}; do
# Strip the subnet
local ip="${addr%/*}" subnet="${addr#*/}"
# We don't work on private IPv4 addresses
case "${ip}" in
127.*) continue;;
10.*) continue;;
192.168.*) continue;;
172.*)
local i=16
while [ ${i} -lt 32 ]; do
case "${ip}" in
172.${i}.*) break;;
esac
i=$((${i} + 1))
done
[ ${i} -lt 32 ] && continue
;;
esac
veinfo "IPv4 address on ${host}: ${ip}/${subnet}"
local ipa= ip6= IFS="${IFS}."
for i in ${ip}; do
ipa="${ipa} ${i}"
done
unset IFS
eval ip6="$(printf "2002:%02x%02x:%02x%02x::%s" ${ipa} ${suffix})"
veinfo "Derived IPv6 address: ${ip6}"
# Now apply our IPv6 address to our config
new="${new}${new:+ }${ip6}/16"
if [ -n "${localip}" ]; then
localip="any"
else
localip="${ip}"
fi
done
if [ -z "${new}" ]; then
eerror "No global IPv4 addresses found on interface ${host}"
return 1
fi
if [ "${IFACE}" != "sit0" ]; then
ebegin "Creating 6to4 tunnel on ${IFACE}"
_tunnel add "${IFACE}" mode sit ttl 255 remote any local "${localip}"
eend $? || return 1
_up
fi
# Now apply our config
eval config_${config_index}=\'"${new}"\'
config_index=$((${config_index} - 1))
# Add a route for us, ensuring we don't delete anything else
local routes="$(_get_array "routes_${IFVAR}")
2003::/3 via ::${relay} metric 2147483647"
eval routes_${IFVAR}=\$routes
}

48
net/ipppd.sh Normal file
View File

@@ -0,0 +1,48 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
ipppd_depend()
{
program start /usr/sbin/ipppd
after macnet
before interface
provide isdn
}
_config_vars="$_config_vars ipppd"
ipppd_pre_start()
{
local opts= pidfile="/var/run/ipppd-${IFACE}.pid"
# Check that we are a valid ippp interface
case "${IFACE}" in
ippp[0-9]*);;
*) return 0;;
esac
# Check that the interface exists
_exists || return 1
# Might or might not be set in conf.d/net
eval opts=\$ipppd_${IFVAR}
einfo "Starting ipppd for ${IFACE}"
start-stop-daemon --start --exec /usr/sbin/ipppd \
--pidfile "${pidfile}" \
-- ${opts} pidfile "${pidfile}" \
file "/etc/ppp/options.${IFACE}" >/dev/null
eend $?
}
ipppd_post_stop()
{
local pidfile="/var/run/ipppd-${IFACE}.pid"
[ ! -f "${pidfile}" ] && return 0
einfo "Stopping ipppd for ${IFACE}"
start-stop-daemon --stop --quiet --exec /usr/sbin/ipppd \
--pidfile "${pidfile}"
eend $?
}

241
net/iproute2.sh Normal file
View File

@@ -0,0 +1,241 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
iproute2_depend()
{
program /sbin/ip
provide interface
after ifconfig
}
_up()
{
ip link set up dev "${IFACE}"
}
_down()
{
ip link set down dev "${IFACE}"
}
_exists()
{
grep -Eq "^[[:space:]]*${IFACE}:" /proc/net/dev
}
_ifindex()
{
local line= i=-2
while read line; do
i=$((${i} + 1))
[ ${i} -lt 1 ] && continue
case "${line}" in
"${IFACE}:"*) echo "${i}"; return 0;;
esac
done < /proc/net/dev
# Return the next available index
i=$((${i} + 1))
echo "${i}"
return 1
}
_is_wireless()
{
# Support new sysfs layout
[ -d /sys/class/net/"${IFACE}"/wireless ] && return 0
[ ! -e /proc/net/wireless ] && return 1
grep -Eq "^[[:space:]]*${IFACE}:" /proc/net/wireless
}
_set_flag()
{
local flag=$1 opt="on"
if [ "${flag#-}" != "${flag}" ]; then
flag=${flag#-}
opt="off"
fi
ip link set "${IFACE}" "${flag}" "${opt}"
}
_get_mac_address()
{
local mac=$(LC_ALL=C ip link show "${IFACE}" | sed -n \
-e 'y/abcdef/ABCDEF/' \
-e '/link\// s/^.*\<\(..:..:..:..:..:..\)\>.*/\1/p')
case "${mac}" in
00:00:00:00:00:00);;
44:44:44:44:44:44);;
FF:FF:FF:FF:FF:FF);;
"");;
*) echo "${mac}"; return 0;;
esac
return 1
}
_set_mac_address()
{
ip link set address "$1" dev "${IFACE}"
}
_get_inet_addresses()
{
LC_ALL=C ip -family inet addr show "${IFACE}" | \
sed -n -e 's/.*inet \([^ ]*\).*/\1/p'
}
_get_inet_address()
{
set -- $(_get_inet_addresses)
[ $# = "0" ] && return 1
echo "$1"
}
_add_address()
{
if [ "$1" = "127.0.0.1/8" -a "${IFACE}" = "lo" ]; then
ip addr add "$@" dev "${IFACE}" 2>/dev/null
return 0
fi
# Convert an ifconfig line to iproute2
if [ "$2" = "netmask" ]; then
local one="$1" three="$3"
shift; shift; shift
set -- "${one}/$(_netmask2cidr "${three}")" "$@"
fi
#config=( "${config[@]//pointopoint/peer}" )
# Always scope lo addresses as host unless specified otherwise
if [ "${IFACE}" = "lo" ]; then
set -- "$@" "scope" "host"
fi
# IPv4 specifics
case "$1" in
*.*.*.*)
case "$@" in
*" brd "*);;
*" broadcast "*);;
*) set -- "$@" brd +;;
esac
;;
esac
ip addr add dev "${IFACE}" "$@"
}
_add_route()
{
if [ $# -eq 3 ]; then
set -- "$1" "$2" via "$3"
elif [ "$3" = "gw" ]; then
local one=$1 two=$2
shift; shift; shift
set -- "${one}" "${two}" via "$@"
fi
local cmd= have_metric=false
while [ -n "$1" ]; do
case "$1" in
metric) cmd="${cmd} $1"; have_metric=true;;
netmask) cmd="${cmd}/$(_netmask2cidr "$2")"; shift;;
-host|-net);;
-A) [ "$2" = "inet6" ] && shift;;
*) cmd="${cmd} $1";;
esac
shift
done
if ! ${have_metric} && [ -n "${metric}" ]; then
cmd="${cmd} metric ${metric}"
fi
ip route append ${cmd} dev "${IFACE}"
eend $?
}
_delete_addresses()
{
ip addr flush dev "${IFACE}" scope global 2>/dev/null
ip addr flush dev "${IFACE}" scope site 2>/dev/null
if [ "${IFACE}" != "lo" ]; then
ip addr flush dev "${IFACE}" scope host 2>/dev/null
fi
return 0
}
_has_carrier()
{
return 0
}
_tunnel()
{
ip tunnel "$@"
}
iproute2_pre_start()
{
# MTU support
local mtu=
eval mtu=\$mtu_${IFVAR}
[ -n "${mtu}" ] && ip link set mtu "${mtu}" dev "${IFACE}"
# TX Queue Length support
local len=
eval len=\$txqueuelen_${IFVAR}
[ -n "${len}" ] && ip link set txqueuelen "${len}" dev "${IFACE}"
local tunnel=
eval tunnel=\$iptunnel_${IFVAR}
if [ -n "${tunnel}" ]; then
# Set our base metric to 1000
metric=1000
ebegin "Creating tunnel ${IFVAR}"
ip tunnel add ${tunnel} name "${IFACE}"
eend $? || return 1
_up
fi
return 0
}
_iproute2_ipv6_tentative()
{
LC_ALL=C ip addr show dev "${IFACE}" | \
grep -q "^[[:space:]]*inet6 .* tentative"
}
iproute2_post_start()
{
# Kernel may not have IP built in
if [ -e /proc/net/route ]; then
ip route flush table cache dev "${IFACE}"
fi
if _iproute2_ipv6_tentative; then
ebegin "Waiting for IPv6 addresses"
while true; do
_iproute2_ipv6_tentative || break
done
eend 0
fi
}
iproute2_post_stop()
{
# Don't delete sit0 as it's a special tunnel
if [ "${IFACE}" != "sit0" ]; then
if [ -n "$(ip tunnel show "${IFACE}" 2>/dev/null)" ]; then
ebegin "Destroying tunnel ${IFACE}"
ip tunnel del "${IFACE}"
eend $?
fi
fi
}

592
net/iwconfig.sh.BSD.in Normal file
View File

@@ -0,0 +1,592 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
_config_vars="$_config_vars ssid mode associate_timeout preferred_aps"
_config_vars="$_config_vars blacklist_aps"
iwconfig_depend()
{
program /sbin/ifconfig
after plug
before interface
provide wireless
}
iwconfig_get_wep_status()
{
local status="disabled"
local mode=$(LC_ALL=C ifconfig "${IFACE}" \
| sed -n -e 's/^[[:space:]]*authmode \([^ ]*\) privacy ON .*/\1/p')
if [ -n "${mode}" ] ; then
status="enabled - ${mode}"
fi
echo "(WEP ${status})"
}
_iwconfig_get()
{
LC_ALL=C ifconfig "${IFACE}" | \
sed -n -e "s/^[[:space:]]*ssid \(.*\) channel \([0-9]*\).* bssid \(..:..:..:..:..:..\)\$/\\$1/p"
}
_get_ssid()
{
local ssid="$(_iwconfig_get 1)"
# If the ssid has a space then it's wrapped in quotes. This is a
# problem if the real ssid has a quote at the start or the end :/
ssid=${ssid#\"}
ssid=${ssid%\"}
echo "${ssid}"
}
_get_ap_mac_address()
{
_iwconfig_get 3
}
_get_channel()
{
_iwconfig_get 2
}
iwconfig_report()
{
local m="connected to"
local ssid="$(_get_ssid)"
local mac="$(_get_ap_mac_address "${iface}")"
[ -n "${mac}" ] && mac=" at ${mac}"
local wep_status="$(iwconfig_get_wep_status "${iface}")"
local channel="$(_get_channel)"
[ -n "${channel}" ] && channel="on channel ${channel} "
eindent
einfo "${IFACE} ${m} \"${ssid}\"${mac}"
einfo "${channel}${wep_status}"
eoutdent
}
iwconfig_get_wep_key()
{
local mac="$1" key=
[ -n "${mac}" ] && mac="$(echo "${mac}" | sed -e 's/://g')"
eval key=\$mac_key_${mac}
[ -z "${key}" ] && eval key=\$key_${SSIDVAR}
echo "${key:--}"
}
iwconfig_user_config()
{
local conf=
eval set -- \$ifconfig_${SSIDVAR}
for conf in "$@" ; do
ifconfig "${IFACE}" ${conf}
done
}
iwconfig_set_mode()
{
local x= opt= unopt="hostap adhoc"
case "$1" in
master|hostap) unopt="adhoc" opt="hostap" ;;
ad-hoc|adhoc) unopt="hostap" opt="adhoc" ;;
esac
for x in ${unopt} ; do
ifconfig "${IFACE}" -mediaopt ${x}
done
for x in ${opt} ; do
ifconfig "${IFACE}" mediaopt ${x}
done
}
iwconfig_setup_specific()
{
local mode="${1:-master}" channel=
if [ -z "${SSID}" ]; then
eerror "${IFACE} requires an SSID to be set to operate in ${mode} mode"
eerror "adjust the ssid_${IFVAR} setting in /etc/conf.d/net"
return 1
fi
iwconfig_set_mode "${mode}" || return 1
SSIDVAR=$(shell_var "${SSID}")
local key=$(iwconfig_get_wep_key)
# Now set the key
ifconfig "${IFACE}" wepkey "${key}"
ifconfig "${IFACE}" ssid "${SSID}" || return 1
eval channel=\$channel_${IFVAR}
# We default the channel to 3
ifconfig "${IFACE}" channel "${channel:-3}" || return 1
iwconfig_user_config
iwconfig_report "${iface}"
return 0
}
iwconfig_associate()
{
local mac="$1" channel="$2" caps="$3"
local mode= w="(WEP Disabled)" key=
SSIDVAR=$(shell_var "${SSID}")
key=$(iwconfig_get_wep_key "${mac}")
case "${caps}" in
[EI]P*)
if [ "${key}" = "-" ] ; then
ewarn "WEP key is not set for \"${SSID}\"; not connecting"
return 1
fi
;;
"") ;;
*)
if [ "${key}" != "-" ] ; then
key="-"
ewarn "\"${SSID}\" is not WEP enabled; ignoring setting"
fi
;;
esac
# Set mode accordingly
case "${caps}" in
*E*)
mode="managed"
if LC_ALL=C ifconfig "${IFACE}" | \
grep -q "^[[:space:]]*media: .*adhoc" ; then
ifconfig "${IFACE}" down -mediaopt adhoc up
fi
;;
*I*)
mode="adhoc"
if ! LC_ALL=C ifconfig "${IFACE}" | \
grep -q "^[[:space:]]*media: .*adhoc" ; then
ifconfig "${IFACE}" down mediaopt adhoc up
fi
;;
*)
if LC_ALL=C ifconfig "${IFACE}" \
| grep -q "^[[:space:]]*media: .*adhoc" ; then
mode="adhoc"
else
mode="managed"
fi
;;
esac
if [ "${key}" = "-" ] ; then
ifconfig "${IFACE}" wepmode off
else
ifconfig "${IFACE}" wepmode on
ifconfig "${IFACE}" deftxkey 1
w=$(iwconfig_get_wep_status)
fi
ebegin "Connecting to \"${SSID}\" in ${mode} mode ${w}"
if ! ifconfig "${IFACE}" wepkey "${key}" ; then
eerror "Invalid WEP key ${key}"
return 1
fi
ifconfig "${IFACE}" ssid "${SSID}" || return 1
iwconfig_user_config
if [ "${SSID}" != "any" ] && type preassociate >/dev/null 2>/dev/null ; then
veinfo "Running preassociate function"
veindent
( preassociate )
local e=$?
veoutdent
if [ ${e} -eq 0 ] ; then
veend 1 "preassociate \"${SSID}\" on ${IFACE} failed"
return 1
fi
fi
local timeout= i=0
eval timeout=\$associate_timeout_${IFVAR}
timeout=${timeout:-10}
[ ${timeout} -eq 0 ] \
&& vewarn "WARNING: infinite timeout set for association on ${IFACE}"
while true; do
_has_carrier && break
sleep 1
[ ${timeout} -eq 0 ] && continue
i=$((${i} + 1))
[ ${i} -ge ${timeout} ] && { eend 1; return 1; }
done
_has_carrier || { eend 1; return 1; }
eend 0
if [ "${SSID}" = "any" ]; then
SSID="$(_get_ssid)"
iwconfig_associate
return $?
fi
iwconfig_report
if type postassociate >/dev/null 2>/dev/null ; then
veinfo "Running postassociate function"
veindent
( postassociate )
veoutdent
fi
return 0
}
iwconfig_scan()
{
local x= i=0 scan= quality=
einfo "Scanning for access points"
eindent
scan="$(LC_ALL=C ifconfig -v "${IFACE}" list scan 2>/dev/null | sed -e "1 d" -e "s/$/'/g" -e "s/^/'/g")"
while [ ${i} -lt 3 -a -z "${scan}" ] ; do
scan="${scan}${scan:+ }$(LC_ALL=C ifconfig -v "${IFACE}" scan 2>/dev/null | sed -e "1 d" -e "s/$/'/g" -e "s/^/'/g")"
i=$((${i} + 1))
done
APS=-1
eval set -- ${scan}
for line in "$@" ; do
APS=$((${APS} + 1))
set -- ${line}
while true ; do
case "$1" in
*:*:*:*:*:*) break ;;
esac
eval SSID_${APS}="\"\${SSID_${APS}}\${SSID_${APS}:+ }$1\""
shift
done
eval MAC_${APS}="$(echo "$1" | tr '[:lower:]' '[:upper:]')"
eval CHAN_${APS}="$2"
quality=${4%:*}
shift ; shift ; shift ; shift ; shift
eval CAPS_${APS}="\"$*\""
# Add 1000 for managed nodes as we prefer them to adhoc
set -- $*
case "$1" in
*E*) eval QUAL_${APS}=$((${quality} + 1000)) ;;
*) eval QUAL_${APS}=\$quality ;;
esac
done
if [ -z "${MAC_0}" ]; then
ewarn "no access points found"
eoutdent
return 1
fi
# Sort based on quality
local i=0 k=1 a= b= x= t=
while [ ${i} -lt ${APS} ] ; do
k=$((${i} + 1))
while [ ${k} -le ${APS} ] ; do
eval a=\$QUALITY_${i}
[ -z "${a}" ] && break
eval b=\$QUALITY_${k}
if [ -n "${b}" -a "${a}" -lt "${b}" ] ; then
for x in MAC SSID CHAN QUALITY CAPS ; do
eval t=\$${x}_${i}
eval ${x}_${i}=\$${x}_${k}
eval ${x}_${k}=\$t
done
fi
k=$((${k} + 1))
done
i=$((${i} + 1))
done
# Strip any duplicates
local i=0 k=1 a= b=
while [ ${i} -lt ${APS} ] ; do
k=$((${i} + 1))
while [ ${k} -le ${APS} ] ; do
eval a=\$MAC_${i}
eval b=\$MAC_${k}
if [ "${a}" = "${b}" ] ; then
eval a=\$QUALITY_${i}
eval b=\$QUALITY_${k}
if [ -n "${a}" -a -n "${b}" ] ; then
if [ ${a} -ge ${b} ] ; then
unset MAC_${k} SSID_${k} CHAN_${k} QUALITY_${k} CAPS_${k}
else
unset MAC_${i} SSID_${i} CHAN_${i} QUALITY_${i} CAPS_${i}
fi
else
unset MAC_${k} SSID_${k} CHAN_${k} QUALITY_${k} CAPS_${k}
fi
fi
k=$((${k} + 1))
done
i=$((${i} + 1))
done
local i=0 e= m= s=
while [ ${i} -le ${APS} ] ; do
eval x=\$MAC_${i}
if [ -z "${x}" ] ; then
i=$((${i} + 1))
continue
fi
eval m=\$MODE_${i}
[ -n "${m}" ] && m=", ${m}"
eval s=\$SSID_${i}
eval q=\$QUALITY_${i}
eval e=\$CAPS_${i}
case "${e}" in
[EI]P*) e=", encrypted" ;;
*) e="" ;;
esac
if [ -z "${s}" ] ; then
einfo "Found ${x}${m}${e}"
else
einfo "Found \"${s}\" at ${x}${m}${e}"
fi
x="$(echo "${x}" | sed -e 's/://g')"
eval x=\$mac_ssid_${x}
if [ -n "${x}" ] ; then
eval SSID_${i}=\$x
s=${x}
eindent
einfo "mapping to \"${x}\""
eoutdent
fi
eval set -- $(_flatten_array "blacklist_aps_${IFVAR}")
[ $# = 0 ] && eval set -- $(_flatten_array "blacklist_aps")
for x; do
if [ "${x}" = "${s}" ] ; then
ewarn "${s} has been blacklisted - not connecting"
unset SSID_${i} MAC_${i} CHAN_${i} QUALITY_${i} CAPS_${i}
fi
done
i=$((${i} + 1))
done
eoutdent
return 0
}
iwconfig_force_preferred()
{
eval set -- $(_flatten_array "preferred_aps_${IFVAR}")
[ $# = 0 ] && eval set -- $(_flatten_array "preferred_aps")
[ $# = 0 ] && return 1
ewarn "Trying to force preferred in case they are hidden"
local ssid=
for ssid; do
local found_AP=false i=0 e=
while [ ${i} -le ${APS:--1} ] ; do
eval e=\$SSID_${i}
if [ "${e}" = "${ssid}" ] ; then
found_AP=true
break
fi
i=$((${i} + 1))
done
if ! ${found_AP} ; then
SSID=${ssid}
iwconfig_associate && return 0
fi
done
ewarn "Failed to associate with any preferred access points on ${IFACE}"
return 1
}
iwconfig_connect_preferred()
{
eval set -- $(_flatten_array "preferred_aps_${IFVAR}")
[ $# = 0 ] && eval set -- $(_flatten_array "preferred_aps")
[ $# = 0 ] && return 1
local ssid= i=0 mode= mac= caps= freq= chan=
for ssid; do
while [ ${i} -le ${APS} ] ; do
eval e=\$SSID_${i}
if [ "${e}" = "${ssid}" ] ; then
SSID=${e}
eval mac=\$MAC_${i}
eval caps=\$CAPS_${i}
eval freq=\$FREQ_${i}
eval chan=\$CHAN_${i}
iwconfig_associate "${mac}" \
"${chan}" "${caps}" && return 0
fi
i=$((${i} + 1))
done
done
return 1
}
iwconfig_connect_not_preferred()
{
local ssid= i=0 mode= mac= caps= freq= chan= pref=
while [ ${i} -le ${APS} ] ; do
eval e=\$SSID_${i}
if [ -z "${e}" ] ; then
i=$((${i} + 1))
continue
fi
eval set -- $(_flatten_array "preferred_aps_${IFVAR}")
[ $# = 0 ] && eval set -- $(_flatten_array "preferred_aps")
pref=false
for ssid; do
if [ "${e}" = "${ssid}" ] ; then
pref=true
break
fi
done
if ! ${pref} ; then
SSID=${e}
eval mac=\$MAC_${i}
eval caps=\$CAPS_${i}
eval freq=\$FREQ_${i}
eval chan=\$CHAN_${i}
iwconfig_associate "${mac}" \
"${chan}" "${caps}" && return 0
fi
i=$((${i} + 1))
done
return 1
}
iwconfig_defaults()
{
# Set some defaults
#ifconfig "${iface}" txpower 100 2>/dev/null
ifconfig "${IFACE}" bssid -
ifconfig "${IFACE}" ssid -
ifconfig "${IFACE}" wepkey 1:- wepkey 2:- wepkey 3:- wepkey 4:-
ifconfig "${IFACE}" authmode open
ifconfig "${IFACE}" -mediaopt adhoc
ifconfig "${IFACE}" -mediaopt hostap
}
iwconfig_configure()
{
local x APS
eval SSID=\$ssid_${IFVAR}
# Setup ad-hoc mode?
eval x=\$mode_${IFVAR}
x=${x:-managed}
case "${x}" in
ad-hoc|adhoc|hostap|master) iwconfig_setup_specific "${x}" ;;
esac
if [ "${x}" != "managed" -a "${x}" != "auto" -a "${x}" != "ad-hoc" -a "${x}" != "adhoc" -a ${x} != "master" ] ; then
eerror "Only managed, ad-hoc, master and auto modes are supported"
return 1
fi
# Has an SSID been forced?
if [ -n "${SSID}" ]; then
iwconfig_set_mode "${x}"
iwconfig_associate && return 0
[ "${SSID}" = "any" ] && iwconfig_force_preferred && return 0
eval SSID=\$adhoc_ssid_${IFVAR}
if [ -n "${SSID}" ]; then
iwconfig_setup_specific adhoc
return $?
fi
return 1
fi
# Are we forcing preferred only?
eval x=\$associate_order_${IFVAR}
[ -n "${x}" ] && associate_order=${x}
associate_order=${associate_order:-any}
if [ "${associate_order}" = "forcepreferredonly" ]; then
iwconfig_force_preferred && return 0
else
iwconfig_scan || return 1
iwconfig_connect_preferred && return 0
[ "${associate_order}" = "forcepreferred" ] || \
[ "${associate_order}" = "forceany" ] && \
iwconfig_force_preferred && return 0
[ "${associate_order}" = "any" ] || \
[ "${associate_order}" = "forceany" ] && \
iwconfig_connect_not_preferred && return 0
fi
e="associate with"
[ -z "${MAC_0}" ] && e="find"
[ "${preferred_aps}" = "force" ] || \
[ "${preferred_aps}" = "forceonly" ] && \
e="force"
e="Couldn't ${e} any access points on ${IFACE}"
eval SSID=\$adhoc_ssid_${IFVAR}
if [ -n "${SSID}" ]; then
ewarn "${e}"
iwconfig_setup_specific adhoc
return $?
fi
eerror "${e}"
return 1
}
iwconfig_pre_start()
{
# We don't configure wireless if we're being called from
# the background
yesno ${IN_BACKGROUND} && return 0
service_set_value "SSID" ""
_exists || return 0
if ! _is_wireless ; then
veinfo "${IFACE} is not wireless"
return 0
fi
iwconfig_defaults
iwconfig_user_config
# Set the base metric to be 2000
metric=2000
einfo "Configuring wireless network for ${IFACE}"
if iwconfig_configure ; then
service_set_value "SSID" "${SSID}"
return 0
fi
eerror "Failed to configure wireless for ${IFACE}"
iwconfig_defaults
#iwconfig "${IFACE}" txpower 0 2>/dev/null
unset SSID SSIDVAR
_down
return 1
}
iwconfig_post_stop()
{
yesno ${IN_BACKGROUND} && return 0
_is_wireless || return 0
iwconfig_defaults
#iwconfig "${IFACE}" txpower 0 2>/dev/null
}

761
net/iwconfig.sh.Linux.in Normal file
View File

@@ -0,0 +1,761 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
_config_vars="$_config_vars ssid mode associate_timeout sleep_scan"
_config_vars="$_config_vars preferred_aps blacklist_aps"
iwconfig_depend()
{
program /sbin/iwconfig
after plug
before interface
provide wireless
}
iwconfig_get_wep_status()
{
local mode= status="disabled"
# No easy way of doing this grep in bash regex :/
if LC_ALL=C iwconfig "${IFACE}" | \
grep -qE "^ +Encryption key:[*0-9,A-F]"; then
status="enabled"
mode=$(LC_ALL=C iwconfig "${IFACE}" | \
sed -n -e 's/^.*Security mode:\(.*[^ ]\).*/\1/p')
[ -n "${mode}" ] && mode=" - ${mode}"
fi
echo "(WEP ${status}${mode})"
}
_get_ssid()
{
local i=5 ssid=
while [ ${i} -gt 0 ]; do
ssid=$(iwgetid --raw "${IFACE}")
if [ -n "${ssid}" ]; then
echo "${ssid}"
return 0
fi
sleep 1
i=$((${i} + 1))
done
return 1
}
_get_ap_mac_address()
{
local mac="$(iwgetid --raw --ap "${IFACE}")"
case "${mac}" in
"00:00:00:00:00:00") return 1;;
"44:44:44:44:44:44") return 1;;
"FF:00:00:00:00:00") return 1;;
"FF:FF:FF:FF:FF:FF") return 1;;
*) echo "${mac}";;
esac
}
iwconfig_get_mode()
{
LC_ALL=C iwgetid --mode "${IFACE}" | \
sed -n -e 's/^.*Mode:\(.*\)/\1/p' | \
tr '[:upper:]' '[:lower:]'
}
iwconfig_set_mode()
{
local mode="$1"
[ "${mode}" = "$(iwconfig_get_mode)" ] && return 0
# Devicescape stack requires the interface to be down
_down
iwconfig "${IFACE}" mode "${mode}" || return 1
_up
}
iwconfig_get_type()
{
LC_ALL=C iwconfig "${IFACE}" | \
sed -n -e 's/^'"$1"' *\([^ ]* [^ ]*\).*/\1/p'
}
iwconfig_report()
{
local mac= m="connected to"
local ssid="$(_get_ssid)"
local wep_status="$(iwconfig_get_wep_status)"
local channel="$(iwgetid --raw --channel "${iface}")"
[ -n "${channel}" ] && channel="on channel ${channel} "
local mode="$(iwconfig_get_mode)"
if [ "${mode}" = "master" ]; then
m="configured as"
else
mac="$(_get_ap_mac_address)"
[ -n "${mac}" ] && mac=" at ${mac}"
fi
eindent
einfo "${IFACE} ${m} SSID \"${SSID}\"${mac}"
einfo "in ${mode} mode ${channel}${wep_status}"
eoutdent
}
iwconfig_get_wep_key()
{
local mac="$1" key=
[ -n "${mac}" ] && mac="$(echo "${mac}" | sed -e 's/://g')"
eval key=\$mac_key_${mac}
[ -z "${key}" ] && eval key=\$key_${SSIDVAR}
if [ -z "${key}" ]; then
echo "off"
else
set -- ${key}
local x= e=false
for x; do
if [ "${x}" = "enc" ]; then
e=true
break
fi
done
${e} || key="${key} enc open"
echo "${key}"
fi
}
iwconfig_user_config()
{
local conf= var=${SSIDVAR} config=
[ -z "${var}" ] && var=${IFVAR}
config="$(_get_array "iwconfig_${var}")"
local IFS="$__IFS"
for conf in ${config}; do
unset IFS
if ! eval iwconfig "${IFACE}" "${conf}"; then
ewarn "${IFACE} does not support the following configuration commands"
ewarn " ${conf}"
fi
done
unset IFS
config="$(_get_array "iwpriv_${var}")"
local IFS="$__IFS"
for conf in ${config}; do
unset IFS
if ! eval iwpriv "${IFACE}" "${conf}"; then
ewarn "${IFACE} does not support the following private ioctls"
ewarn " ${conf}"
fi
done
}
iwconfig_setup_specific()
{
local mode="$1" channel=
if [ -z "${SSID}" ]; then
eerror "${IFACE} requires an SSID to be set to operate in ${mode} mode"
eerror "adjust the ssid_${IFVAR} setting in /etc/conf.d/net"
return 1
fi
SSIDVAR=$(shell_var "${SSID}")
local key=$(iwconfig_get_wep_key)
iwconfig_set_mode "${mode}"
# Now set the key
if ! eval iwconfig "${IFACE}" key "${key}"; then
if [ "${key}" != "off" ]; then
ewarn "${IFACE} does not support setting keys"
ewarn "or the parameter \"mac_key_${SSIDVAR}\" or \"key_${SSIDVAR}\" is incorrect"
fi
fi
# Then set the SSID
if ! iwconfig "${IFACE}" essid "${SSID}"; then
eerror "${IFACE} does not support setting SSID to \"${SSID}\""
return 1
fi
eval channel=\$channel_${SSIDVAR}
[ -z "${channel}" ] && eval channel=\$channel_${IFVAR}
# We default the channel to 3
if ! iwconfig "${IFACE}" channel "${channel:-3}"; then
ewarn "${IFACE} does not support setting the channel to \"${channel:-3}\""
return 1
fi
# Finally apply the user Config
iwconfig_user_config
iwconfig_report
return 0
}
iwconfig_wait_for_association()
{
local timeout= i=0
eval timeout=\$associate_timeout_${IFVAR}
timeout=${timeout:-10}
[ ${timeout} -eq 0 ] \
&& vewarn "WARNING: infinite timeout set for association on ${IFACE}"
while true; do
# Use sysfs if we can
if [ -e /sys/class/net/"${IFACE}"/carrier ]; then
if [ "$(cat /sys/class/net/"${IFACE}"/carrier)" = "1" ]; then
# Double check we have an ssid. This is mainly for buggy
# prism54 drivers that always set their carrier on :/
[ -n "$(iwgetid --raw "${IFACE}")" ] && return 0
fi
else
local atest=
eval atest=\$associate_test_${IFVAR}
atest=${atest:-mac}
if [ "${atest}" = "mac" -o "${atest}" = "all" ]; then
[ -n "$(_get_ap_mac_address)" ] && return 0
fi
if [ "${atest}" = "quality" -o "${atest}" = "all" ]; then
[ "$(sed -n -e 's/^.*'"${IFACE}"': *[0-9]* *\([0-9]*\).*/\1/p' \
/proc/net/wireless)" != "0" ] && return 0
fi
fi
sleep 1
[ ${timeout} -eq 0 ] && continue
i=$((${i} + 1))
[ ${i} -ge ${timeout} ] && return 1
done
return 1
}
iwconfig_associate()
{
local mode="${1:-managed}" mac="$2" wep_required="$3"
local freq="$4" chan="$5"
local w="(WEP Disabled)" key=
iwconfig_set_mode "${mode}"
if [ "${SSID}" = "any" ]; then
iwconfig "${IFACE}" ap any 2>/dev/null
unset SSIDVAR
else
SSIDVAR=$(shell_var "${SSID}")
key="$(iwconfig_get_wep_key "${mac}")"
if [ "${wep_required}" = "on" -a "${key}" = "off" ]; then
ewarn "WEP key is not set for \"${SSID}\" - not connecting"
return 1
fi
if [ "${wep_required}" = "off" -a "${key}" != "off" ]; then
key="off"
ewarn "\"${SSID}\" is not WEP enabled - ignoring setting"
fi
if ! eval iwconfig "${IFACE}" key "${key}"; then
if [ "${key}" != "off" ]; then
ewarn "${IFACE} does not support setting keys"
ewarn "or the parameter \"mac_key_${SSIDVAR}\" or \"key_${SSIDVAR}\" is incorrect"
return 1
fi
fi
[ "${key}" != "off" ] && w="$(iwconfig_get_wep_status "${iface}")"
fi
if ! iwconfig "${IFACE}" essid "${SSID}"; then
if [ "${SSID}" != "any" ]; then
ewarn "${IFACE} does not support setting SSID to \"${SSID}\""
fi
fi
# Only use channel or frequency
if [ -n "${chan}" ]; then
iwconfig "${IFACE}" channel "${chan}"
elif [ -n "${freq}" ]; then
iwconfig "${IFACE}" freq "${freq}"
fi
[ -n "${mac}" ] && iwconfig "${IFACE}" ap "${mac}"
# Finally apply the user Config
iwconfig_user_config
ebegin "Connecting to \"${SSID}\" in ${mode} mode ${w}"
if [ "${SSID}" != "any" ] && type preassociate >/dev/null 2>&1; then
veinfo "Running preassociate function"
veindent
( preassociate )
local e=$?
veoutdent
if [ ${e} -eq 0 ]; then
veend 1 "preassociate \"${SSID}\" on ${IFACE} failed"
return 1
fi
fi
if ! iwconfig_wait_for_association; then
eend 1
return 1
fi
eend 0
if [ "${SSID}" = "any" ]; then
SSID="$(_get_ssid)"
iwconfig_associate
return $?
fi
iwconfig_report
if type postassociate >/dev/null 2>&1; then
veinfo "Running postassociate function"
veindent
( postassociate )
veoutdent
fi
return 0
}
iwconfig_scan()
{
local x= i=0 scan=
einfo "Scanning for access points"
eindent
# Sleep if required
eval x=\$sleep_scan_${IFVAR}
[ -n "${x}" ] && sleep "${x}"
while [ ${i} -lt 3 ]; do
local scan="${scan}${scan:+ }$(LC_ALL=C iwlist "${IFACE}" scan 2>/dev/null | sed -e "s/'/'\\\\''/g" -e "s/$/'/g" -e "s/^/'/g")"
# If this is the first pass and txpower as off and we have no
# results then we need to wait for at least 2 seconds whilst
# the interface does an initial scan.
if [ "${i}" = "0" -a "${txpowerwasoff}" = "0" ]; then
case "${scan}" in
"'${IFACE} "*"No scan results"*)
sleep 2
txpowerwasoff=1
continue
;;
esac
fi
i=$((${i} + 1))
done
if [ -z "${scan}" ]; then
ewarn "${iface} does not support scanning"
eoutdent
eval x=\$adhoc_ssid_${IFVAR}
[ -n "${x}" ] && return 0
if [ -n "${preferred_aps}" ]; then
[ "${associate_order}" = "forcepreferred" ] || \
[ "${associate_order}" = "forcepreferredonly" ] && return 0
fi
eerror "You either need to set a preferred_aps list in /etc/conf.d/wireless"
eerror " preferred_aps=\"SSID1 SSID2\""
eerror " and set associate_order_${IFVAR}=\"forcepreferred\""
eerror " or set associate_order_${IFVAR}=\"forcepreferredonly\""
eerror "or hardcode the SSID to \"any\" and let the driver find an Access Point"
eerror " ssid_${IFVAR}=\"any\""
eerror "or configure defaulting to Ad-Hoc when Managed fails"
eerror " adhoc_ssid_${IFVAR}=\"WLAN\""
eerror "or hardcode the SSID against the interface (not recommended)"
eerror " ssid_${IFVAR}=\"SSID\""
return 1
fi
APS=-1
eval set -- ${scan}
for line; do
case "${line}" in
*Address:*)
APS=$((${APS} + 1))
eval MAC_${APS}="\""$(echo "${line#*: }" | tr '[:lower:]' '[:upper:]')"\""
eval QUALITY_${APS}=0
;;
*ESSID:*)
x=${line#*\"}
x=${x%*\"}
eval SSID_${APS}=\$x
;;
*Mode:*)
x="$(echo "${line#*:}" | tr '[:upper:]' '[:lower:]')"
if [ "${x}" = "master" ]; then
eval MODE_${APS}=managed
else
eval MODE_${APS}=\$x
fi
;;
*'Encryption key:'*)
x=${line#*:}
eval ENC_${APS}=\$x
;;
#*Frequency:*)
# freq[i]="${line#*:}"
# x="${freq[i]#* }"
# freq[i]="${freq[i]%% *}${x:0:1}"
# ;;
*Channel:*)
x=${line#*:}
x=${x%% *}
eval CHAN_${APS}=\$x
;;
*Quality*)
x=${line#*:}
x=${x%/*}
x="$(echo "${x}" | sed -e 's/[^[:digit:]]//g')"
x=${x:-0}
eval QUALITY_${APS}=\$x
;;
esac
done
if [ -z "${MAC_0}" ]; then
ewarn "no access points found"
eoutdent
return 1
fi
# Sort based on quality
local i=0 k=1 a= b= x= t=
while [ ${i} -lt ${APS} ]; do
k=$((${i} + 1))
while [ ${k} -le ${APS} ]; do
eval a=\$QUALITY_${i}
[ -z "${a}" ] && break
eval b=\$QUALITY_${k}
if [ -n "${b}" -a "${a}" -lt "${b}" ]; then
for x in MAC SSID CHAN QUALITY ENC; do
eval t=\$${x}_${i}
eval ${x}_${i}=\$${x}_${k}
eval ${x}_${k}=\$t
done
fi
k=$((${k} + 1))
done
i=$((${i} + 1))
done
# Strip any duplicates
local i=0 k=1 a= b=
while [ ${i} -lt ${APS} ]; do
k=$((${i} + 1))
while [ ${k} -le ${APS} ]; do
eval a=\$MAC_${i}
eval b=\$MAC_${k}
if [ "${a}" = "${b}" ]; then
eval a=\$QUALITY_${i}
eval b=\$QUALITY_${k}
local u=${k}
# We need to split this into two tests, otherwise bash errors
[ -n "${a}" -a -n "${b}" ] && [ "${a}" -lt "${b}" ] && u=${i}
unset MAC_${u} SSID_${u} CHAN_${u} QUALITY_${u} ENC_${u}
fi
k=$((${k} + 1))
done
i=$((${i} + 1))
done
local i=0 e= m= s=
while [ ${i} -le ${APS} ]; do
eval x=\$MAC_${i}
if [ -z "${x}" ]; then
i=$((${i} + 1))
continue
fi
eval m=\$MODE_${i}
eval s=\$SSID_${i}
eval q=\$QUALITY_${i}
eval e=\$ENC_${i}
if [ -n "${e}" -a "${e}" != "off" ]; then
e=", encrypted"
else
e=""
fi
if [ -z "${s}" ]; then
einfo "Found ${x}, ${m}${e}"
else
einfo "Found \"${s}\" at ${x}, ${m}${e}"
fi
x="$(echo "${x}" | sed -e 's/://g')"
eval x=\$mac_ssid_${x}
if [ -n "${x}" ]; then
eval SSID_${i}=\$x
s=${x}
eindent
einfo "mapping to \"${x}\""
eoutdent
fi
eval set -- $(_flatten_array "blacklist_aps_${IFVAR}")
[ $# = 0 ] && eval set -- $(_flatten_array "blacklist_aps")
for x; do
if [ "${x}" = "${s}" ]; then
ewarn "${s} has been blacklisted - not connecting"
unset SSID_${i} MAC_${i} CHAN_${i} QUALITY_${i} ENC_${i}
fi
done
i=$((${i} + 1))
done
eoutdent
}
iwconfig_force_preferred()
{
eval set -- $(_flatten_array "preferred_aps_${IFVAR}")
[ $# = 0 ] && eval set -- $(_flatten_array "preferred_aps")
[ $# = 0 ] && return 1
ewarn "Trying to force preferred in case they are hidden"
for ssid; do
local found_AP=false i=0 e=
while [ ${i} -le ${APS} ]; do
eval e=\$SSID_${i}
if [ "${e}" = "${ssid}" ]; then
found_AP=true
break
fi
i=$((${i} + 1))
done
if ! ${found_AP}; then
SSID=${e}
iwconfig_associate && return 0
fi
done
ewarn "Failed to associate with any preferred access points on ${IFACE}"
return 1
}
iwconfig_connect_preferred()
{
local ssid= i= mode= mac= enc= freq= chan=
eval set -- $(_flatten_array "preferred_aps_${IFVAR}")
[ $# = 0 ] && eval set -- $(_flatten_array "preferred_aps")
for ssid; do
unset IFS
i=0
while [ ${i} -le ${APS} ]; do
eval e=\$SSID_${i}
if [ "${e}" = "${ssid}" ]; then
SSID=${e}
eval mode=\$MODE_${i}
eval mac=\$MAC_${i}
eval enc=\$ENC_${i}
eval freq=\$FREQ_${i}
eval chan=\$CHAN_${i}
iwconfig_associate "${mode}" "${mac}" "${enc}" "${freq}" \
"${chan}" && return 0
fi
i=$((${i} + 1))
done
done
return 1
}
iwconfig_connect_not_preferred()
{
local ssid= i=0 mode= mac= enc= freq= chan= pref=false
while [ ${i} -le ${APS} ]; do
eval e=\$SSID_${i}
if [ -n "${e}" ]; then
eval set -- $(_flatten_array "preferred_aps_${IFVAR}")
[ $# = 0 ] && eval set -- $(_flatten_array "preferred_aps")
for ssid; do
if [ "${e}" = "${ssid}" ]; then
pref=true
break
fi
done
if ! ${pref}; then
SSID=${e}
eval mode=\$MODE_${i}
eval mac=\$MAC_${i}
eval enc=\$ENC_${i}
eval freq=\$FREQ_${i}
eval chan=\$CHAN_${i}
iwconfig_associate "${mode}" "${mac}" "${enc}" "${freq}" \
"${chan}" && return 0
fi
fi
i=$((${i} + 1))
done
return 1
}
iwconfig_defaults()
{
local x=
for x in txpower rate rts frag; do
iwconfig "${IFACE}" "${x}" auto 2>/dev/null
done
# Release the AP forced
# Must do ap and then ssid otherwise scanning borks
iwconfig "${IFACE}" ap off 2>/dev/null
iwconfig "${IFACE}" essid off 2>/dev/null
}
iwconfig_configure()
{
local x= APS=
eval SSID=\$ssid_${IFVAR}
# Support old variable
[ -z "${SSID}" ] && eval SSID=\$essid_${IFVAR}
# Setup ad-hoc mode?
eval x=\$mode_${IFVAR}
x=${x:-managed}
if [ "${x}" = "ad-hoc" -o "${x}" = "master" ]; then
iwconfig_setup_specific "${x}"
return $?
fi
if [ "${x}" != "managed" -a "${x}" != "auto" ]; then
eerror "Only managed, ad-hoc, master and auto modes are supported"
return 1
fi
# Has an SSID been forced?
if [ -n "${SSID}" ]; then
iwconfig_set_mode "${x}"
iwconfig_associate && return 0
[ "${SSID}" = "any" ] && iwconfig_force_preferred && return 0
eval SSID=\$adhoc_ssid_${IFVAR}
if [ -n "${SSID}" ]; then
iwconfig_setup_specific ad-hoc
return $?
fi
return 1
fi
eval x=\$preferred_aps_${IFVAR}
[ -n "${x}" ] && preferred_aps=${x}
eval x=\$blacklist_aps_${IFVAR}
[ -n "${x}" ] && blacklist_aps=${x}
eval x=\$associate_order_${IFVAR}
[ -n "${x}" ] && associate_order=${x}
associate_order=${associate_order:-any}
if [ "${associate_order}" = "forcepreferredonly" ]; then
iwconfig_force_preferred && return 0
else
iwconfig_scan || return 1
iwconfig_connect_preferred && return 0
[ "${associate_order}" = "forcepreferred" ] || \
[ "${associate_order}" = "forceany" ] && \
iwconfig_force_preferred && return 0
[ "${associate_order}" = "any" ] || \
[ "${associate_order}" = "forceany" ] && \
iwconfig_connect_not_preferred && return 0
fi
e="associate with"
[ -z "${MAC_0}" ] && e="find"
[ "${preferred_aps}" = "force" ] || \
[ "${preferred_aps}" = "forceonly" ] && \
e="force"
e="Couldn't ${e} any access points on ${IFACE}"
eval SSID=\$adhoc_ssid_${IFVAR}
if [ -n "${SSID}" ]; then
ewarn "${e}"
iwconfig_setup_specific ad-hoc
return $?
fi
eerror "${e}"
return 1
}
iwconfig_pre_start()
{
# We don't configure wireless if we're being called from
# the background
yesno ${IN_BACKGROUND} && return 0
service_set_value "SSID" ""
_exists || return 0
if ! _is_wireless; then
veinfo "Wireless extensions not found for ${IFACE}"
return 0
fi
# Warn about old file - we want to punt it really
if [ -e /etc/conf.d/wireless ]; then
ewarn "/etc/conf.d/wireless is deprecated"
ewarn "Please put all settings into /etc/conf.d/net"
. /etc/conf.d/wireless
fi
# Store the fact that tx-power was off so we default to a longer
# wait if our scan returns nothing
LC_ALL=C iwconfig "${IFACE}" | sed -e '1d' | grep -q "Tx-Power=off"
local txpowerwasoff=$?
iwconfig_defaults
iwconfig_user_config
# Set the base metric to be 2000
metric=2000
# Check for rf_kill - only ipw supports this at present, but other
# cards may in the future.
if [ -e /sys/class/net/"${IFACE}"/device/rf_kill ]; then
if [ $(cat /sys/class/net/"${IFACE}"/device/rf_kill) != "0" ]; then
eerror "Wireless radio has been killed for interface ${IFACE}"
return 1
fi
fi
einfo "Configuring wireless network for ${IFACE}"
# Are we a proper IEEE device?
# Most devices reutrn IEEE 802.11b/g - but intel cards return IEEE
# in lower case and RA cards return RAPCI or similar
# which really sucks :(
# For the time being, we will test prism54 not loading firmware
# which reports NOT READY!
x="$(iwconfig_get_type)"
if [ "${x}" = "NOT READY!" ]; then
eerror "Looks like there was a probem loading the firmware for ${IFACE}"
return 1
fi
if iwconfig_configure; then
service_set_value "SSID" "${SSID}"
return 0
fi
eerror "Failed to configure wireless for ${IFACE}"
iwconfig_defaults
iwconfig "${IFACE}" txpower off 2>/dev/null
unset SSID SSIDVAR
_down
return 1
}
iwconfig_post_stop()
{
yesno ${IN_BACKGROUND} && return 0
_exists || return 0
iwconfig_defaults
iwconfig "${IFACE}" txpower off 2>/dev/null
}

94
net/netplugd.sh Normal file
View File

@@ -0,0 +1,94 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
_config_vars="$_config_vars plug_timeout"
netplugd_depend()
{
program start /sbin/netplugd
after macnet rename
before interface
provide plug
# Prefer ifplugd
before ifplugd
}
netplugd_pre_start()
{
local pidfile="/var/run/netplugd-${IFACE}.pid" timeout=
# We don't start netplug if we're being called from the background
yesno ${IN_BACKGROUND} && return 0
_exists || return 0
# We need a valid MAC address
# It's a basic test to ensure it's not a virtual interface
if ! _get_mac_address >/dev/null 2>&1; then
vewarn "netplug only works on interfaces with a valid MAC address"
return 0
fi
# We don't work on bonded, bridges, tun/tap, vlan or wireless
for f in bond bridge tuntap vlan wireless; do
if type "_is_${f}" >/dev/null 2>&1; then
if _is_${f}; then
veinfo "netplug does not work with" "${f}"
return 0
fi
fi
done
ebegin "Starting netplug on" "${IFACE}"
# Mark the us as inactive so netplug can restart us
mark_service_inactive
# Start netplug
start-stop-daemon --start --exec /sbin/netplugd \
--pidfile "${pidfile}" \
-- -i "${IFACE}" -P -p "${pidfile}" -c /dev/null
eend "$?" || return 1
eindent
eval timeout=\$plug_timeout_${IFVAR}
[ -z "${timeout}" ] && timeout=-1
if [ ${timeout} -eq 0 ]; then
ewarn "WARNING: infinite timeout set for ${IFACE} to come up"
elif [ ${timeout} -lt 0 ]; then
einfo "Backgrounding ..."
exit 1
fi
veinfo "Waiting for ${IFACE} to be marked as started"
local i=0
while true; do
if service_started; then
_show_address
exit 0
fi
sleep 1
[ ${timeout} -eq 0 ] && continue
i=$((${i} + 1))
[ ${i} -ge ${timeout} ] && break
done
eend 1 "Failed to configure ${IFACE} in the background"
exit 1
}
netplugd_stop()
{
yesno ${IN_BACKGROUND} && return 0
local pidfile="/var/run/netplugd-${IFACE}.pid"
[ ! -e "${pidfile}" ] && return 0
ebegin "Stopping netplug on" "${IFACE}"
start-stop-daemon --stop --quiet --exec /sbin/netplugd \
--pidfile "${pidfile}"
eend $?
}

243
net/pppd.sh Normal file
View File

@@ -0,0 +1,243 @@
# Copyright 2005-2007 Gentoo Foundation
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
pppd_depend()
{
program /usr/sbin/pppd
after interface
before dhcp
provide ppp
}
is_ppp()
{
[ -e /var/run/ppp-"${IFACE}".pid ]
}
requote()
{
printf "'%s' " "$@"
}
pppd_pre_start()
{
# Interface has to be called ppp
[ "${IFACE%%[0-9]*}" = "ppp" ] || return 0
# Set our base metric
metric=4000
if yesno ${IN_BACKGROUND}; then
local config=
eval config=\$config_${IFVAR}
# If no config for ppp then don't default to DHCP
if [ -z "${config}" ]; then
eval config_${IFVAR}=null
fi
return 0
fi
local link= i= unit="${IFACE#ppp}" opts=
# PPP requires a link to communicate over - normally a serial port
# PPPoE communicates over Ethernet
# PPPoA communicates over ATM
# In all cases, the link needs to be available before we start PPP
eval link=\$link_${IFVAR}
[ -n "${link}" ] || return 0
case "${link}" in
/*)
if [ ! -e "${link}" ]; then
eerror "${link} does not exist"
eerror "Please verify hardware or kernel module (driver)"
return 1
fi
;;
esac
if [ -z "${unit}" ]; then
eerror "PPP requires a unit - use net.ppp[0-9] instead of net.ppp"
return 1
fi
# We need to flatten the useless array
set -- $(_get_array "pppd_${IFVAR}")
opts="$@"
local mtu= hasmtu=false hasmru=false hasmaxfail=false haspersist=false
local hasupdetach=false hasdefaultmetric=false
for i in ${opts}; do
case "${i}" in
unit|nodetach|linkname)
eerror "The option \"${i}\" is not allowed in pppd_${IFVAR}"
return 1
;;
defaultmetric) hasdefaultmetric=true;;
mtu) hasmtu=true;;
mru) hasmru=true;;
maxfail) hasmaxfail=true;;
persist) haspersist=true;;
updetach) hasupdetach=true;;
esac
done
# Might be set in conf.d/net
local username= password= passwordset=
eval username=\$username_${IFVAR}
eval password=\$password_${IFVAR}
eval passwordset=\$\{password_${IFVAR}-x\}
if [ -n "${username}" ] \
&& [ -n "${password}" -o -z "${passwordset}" ]; then
opts="${opts} plugin passwordfd.so passwordfd 0"
fi
if ! ${hasdefaultmetric}; then
local m=
eval m=\$metric_${IFVAR}
[ -z "${m}" ] && m=$((${metric} + $(_ifindex)))
opts="${opts} defaultmetric ${m}"
fi
if [ -n "${mtu}" ]; then
${hasmtu} || opts="${opts} mtu ${mtu}"
${hasmru} || opts="${opts} mru ${mtu}"
fi
${hasmaxfail} || opts="${opts} maxfail 0"
${haspersist} || opts="${opts} persist"
# Set linkname because we need /var/run/ppp-${linkname}.pid
# This pidfile has the advantage of being there,
# even if ${IFACE} interface was never started
opts="linkname ${IFACE} ${opts}"
# Setup auth info
if [ -n "${username}" ]; then
opts="user '${username}' remotename ${IFACE} ${opts}"
fi
# Load a custom interface configuration file if it exists
[ -f "/etc/ppp/options.${IFACE}" ] \
&& opts="${opts} file '/etc/ppp/options.${IFACE}'"
# Set unit
opts="unit ${unit} ${opts}"
# Setup connect script
local chatprog="/usr/sbin/chat -e -E -v" phone=
eval phone=\$phone_number_${IFVAR}
set -- ${phone}
[ -n "$1" ] && chatprog="${chatprog} -T '$1'"
[ -n "$2" ] && chatprog="${chatprog} -U '$2'"
# We need to flatten the useless array
set -- $(_get_array "chat_${IFVAR}")
if [ $# != 0 ]; then
opts="${opts} connect '$(echo ${chatprog} $@ | sed -e "s:':'\\\\'':g")'"
fi
# Add plugins
local haspppoa=false haspppoe=false plugins="$(_get_array "plugins_${IFVAR}")"
local IFS="$__IFS"
for i in ${plugins}; do
unset IFS
set -- ${i}
case "$1" in
passwordfd) continue;;
pppoa) shift; set -- "pppoatm" "$@";;
pppoe) shift; set -- "rp-pppoe" "$@";;
capi) shift; set -- "capiplugin" "$@";;
esac
case "$1" in
rp-pppoe) haspppoe=true;;
pppoatm) haspppoa=true;;
esac
if [ "$1" = "rp-pppoe" ] || [ "$1" = "pppoatm" -a "${link}" != "/dev/null" ]; then
opts="${opts} connect true"
set -- "$@" "${link}"
fi
opts="${opts} plugin $1.so"
shift
opts="${opts} $@"
done
unset IFS
#Specialized stuff. Insert here actions particular to connection type (pppoe,pppoa,capi)
local insert_link_in_opts=1
if ${haspppoe}; then
if [ ! -e /proc/net/pppoe ]; then
# Load the PPPoE kernel module
if ! modprobe pppoe; then
eerror "kernel does not support PPPoE"
return 1
fi
fi
# Ensure that the link exists and is up
( IFACE="${link}"; _exists true && _up ) || return 1
insert_link_in_opts=0
fi
if ${haspppoa}; then
if [ ! -d /proc/net/atm ]; then
# Load the PPPoA kernel module
if ! modprobe pppoatm; then
eerror "kernel does not support PPPoATM"
return 1
fi
fi
if [ "${link}" != "/dev/null" ]; then
insert_link_in_opts=0
else
ewarn "WARNING: An [itf.]vpi.vci ATM address was expected in link_${IFVAR}"
fi
fi
[ "${insert_link_in_opts}" = "0" ] || opts="${link} ${opts}"
ebegin "Starting pppd in ${IFACE}"
mark_service_inactive
if [ -n "${username}" ] \
&& [ -n "${password}" -o -z "${passwordset}" ]; then
printf "%s" "${password}" | \
eval start-stop-daemon --start --exec /usr/sbin/pppd \
--pidfile "/var/run/ppp-${IFACE}.pid" -- "${opts}" >/dev/null
else
eval start-stop-daemon --start --exec /usr/sbin/pppd \
--pidfile "/var/run/ppp-${IFACE}.pid" -- "${opts}" >/dev/null
fi
if ! eend $? "Failed to start PPP"; then
mark_service_stopped
return 1
fi
if ${hasupdetach}; then
_show_address
else
einfo "Backgrounding ..."
fi
# pppd will re-call us when we bring the interface up
exit 0
}
# Dummy function for users that still have config_ppp0="ppp"
pppd_start()
{
return 0
}
pppd_stop()
{
yesno ${IN_BACKGROUND} && return 0
local pidfile="/var/run/ppp-${IFACE}.pid"
[ ! -s "${pidfile}" ] && return 0
# Give pppd at least 30 seconds do die, #147490
einfo "Stopping pppd on ${IFACE}"
start-stop-daemon --stop --quiet --exec /usr/sbin/pppd \
--pidfile "${pidfile}" --retry 30
eend $?
}

59
net/pump.sh Normal file
View File

@@ -0,0 +1,59 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
pump_depend()
{
program /sbin/pump
after interface
provide dhcp
}
_config_vars="$_config_vars dhcp pump"
pump_start()
{
local args= opt= opts=
# Get our options
eval opts=\$dhcp_${IFVAR}
[ -z "${opts}" ] && opts=${dhcp}
# Map some generic options to dhcpcd
for opt in ${opts}; do
case "${opt}" in
nodns) args="${args} --no-dns";;
nontp) args="${args} --no-ntp";;
nogateway) args="${args} --no-gateway";;
esac
done
# Add our route metric
[ "${metric:-0}" != "0" ] && args="${args} --route-metric ${metric}"
args="${args} --win-client-ident"
args="${args} --keep-up --interface ${IFACE}"
ebegin "Running pump"
eval pump "${args}"
eend $? || return 1
_show_address
return 0
}
pump_stop()
{
# We check for a pump process first as querying for status
# causes pump to spawn a process
start-stop-daemon --quiet --test --stop --exec /sbin/pump || return 0
# Check that pump is running on the interface
if ! pump --status --interface "${IFACE}" >/dev/null 2>&1; then
return 0
fi
# Pump always releases the lease
ebegin "Stopping pump on ${IFACE}"
pump --release --interface "${IFACE}"
eend $? "Failed to stop pump"
}

83
net/tuntap.sh Normal file
View File

@@ -0,0 +1,83 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
tuntap_depend()
{
before bridge interface macchanger
}
_config_vars="$_config_vars tunctl"
_is_tuntap()
{
[ -n "$(export RC_SVCNAME="net.${IFACE}"; service_get_value tuntap)" ]
}
tuntap_pre_start()
{
local tuntap=
eval tuntap=\$tuntap_${IFVAR}
[ -z "${tuntap}" ] && return 0
if [ ! -e /dev/net/tun ]; then
if ! modprobe tun; then
eerror "TUN/TAP support is not present in this kernel"
return 1
fi
vebegin "Waiting for /dev/net/tun"
# /dev/net/tun can take it's time to appear
local timeout=10
while [ ! -e /dev/net/tun -a ${timeout} -gt 0 ]; do
sleep 1
timeout=$((${timeout} - 1))
done
if [ ! -e /dev/net/tun ]; then
eerror "TUN/TAP support present but /dev/net/tun is not"
return 1
fi
veend 0
fi
ebegin "Creating Tun/Tap interface ${IFACE}"
# Set the base metric to 1000
metric=1000
local o_opts= t_opts= do_openvpn=false do_tunctl=false
eval o_opts=\$openvpn_${IFVAR}
eval t_opts=\$tunctl_${IFVAR}
if [ -n "${o_opts}" ] && type openvpn >/dev/null 2>&1; then
do_openvpn=true
elif [ -n "${t_opts}" ] && type tunctl >/dev/null 2>&1; then
do_tunctl=true
elif type openvpn >/dev/null 2>&1; then
do_openvpn=true
else
do_tunctl=true
fi
if ${do_openvpn}; then
openvpn --mktun --dev-type "${tuntap}" --dev "${IFACE}" \
${o_opts} >/dev/null
else
tunctl ${t_opts} -t "${IFACE}" >/dev/null
fi
eend $? && _up && service_set_value tuntap "${tuntap}"
}
tuntap_post_stop()
{
_is_tuntap || return 0
ebegin "Destroying Tun/Tap interface ${IFACE}"
if type tunctl >/dev/null 2>&1; then
tunctl -d "${IFACE}" >/dev/null
else
openvpn --rmtun \
--dev-type "$(service_get_value tuntap)" \
--dev "${IFACE}" >/dev/null
fi
eend $?
}

105
net/udhcpc.sh Normal file
View File

@@ -0,0 +1,105 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
udhcpc_depend()
{
program start /sbin/udhcpc
after interface
provide dhcp
}
_config_vars="$_config_vars dhcp udhcpc"
# WARNING:- The relies heavily on Gentoo patches and scripts for udhcpc
udhcpc_start()
{
local args= opt= opts= pidfile="/var/run/udhcpc-${IFACE}.pid"
local sendhost=true cachefile="/var/cache/udhcpc-${IFACE}.lease"
eval args=\$udhcpc_${IFVAR}
# Get our options
eval opts=\$dhcp_${IFVAR}
[ -z "${opts}" ] && opts=${dhcp}
# Map some generic options to dhcpcd
for opt in ${opts}; do
case "${opt}" in
nodns) args="${args} --env PEER_DNS=no";;
nontp) args="${args} --env PEER_NTP=no";;
nogateway) args="${args} --env PEER_ROUTERS=no";;
nosendhost) sendhost=false;
esac
done
[ "${metric:-0}" != "0" ] && args="${args} --env IF_METRIC=${metric}"
ebegin "Running udhcpc"
# Try and load the cache if it exists
if [ -f "${cachefile}" ]; then
case "$ {args} " in
*" --request="*|*" -r "*);;
*)
local x=$(cat "${cachefile}")
# Check for a valid ip
case "${x}" in
*.*.*.*) args="${args} --request=${x}";;
esac
;;
esac
fi
case " ${args} " in
*" --quit "*|*" -q "*) x="/sbin/udhcpc";;
*) x="start-stop-daemon --start --exec /sbin/udhcpc \
--pidfile \"${pidfile}\" --";;
esac
case " ${args} " in
*" --hosname="*|*" -h "*|*" -H "*);;
*)
if ${sendhost}; then
local hname="$(hostname)"
if [ "${hname}" != "(none)" ] && [ "${hname}" != "localhost" ]; then
args="${args} --hostname='${hname}'"
fi
fi
;;
esac
local script="${RC_LIBDIR}"/sh/udhcpc.sh
[ -x "${script}" ] || script=/lib/rcscripts/sh/udhcpc.sh
eval "${x}" "${args}" --interface="${IFACE}" --now \
--script="${script}" \
--pidfile="${pidfile}" >/dev/null
eend $? || return 1
_show_address
return 0
}
udhcpc_stop()
{
local pidfile="/var/run/udhcpc-${IFACE}.pid" opts=
[ ! -f "${pidfile}" ] && return 0
# Get our options
eval opts=\$dhcp_${IFVAR}
[ -z "${opts}" ] && opts=${dhcp}
ebegin "Stopping udhcpc on ${IFACE}"
case " ${opts} " in
*" release "*)
start-stop-daemon --stop --quiet --oknodo --signal USR2 \
--exec /sbin/udhcpc --pidfile "${pidfile}"
if [ -f /var/cache/udhcpc-"${IFACE}".lease ]; then
rm -f /var/cache/udhcpc-"${IFACE}".lease
fi
;;
esac
start-stop-daemon --stop --exec /sbin/udhcpc --pidfile "${pidfile}"
eend $?
}

118
net/vlan.sh Normal file
View File

@@ -0,0 +1,118 @@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
vlan_depend()
{
program /sbin/vconfig
after interface
before dhcp
}
_config_vars="$_config_vars vlans"
_is_vlan()
{
[ ! -d /proc/net/vlan ] && return 1
[ -e /proc/net/vlan/"${IFACE}" ] && return 0
grep -Eq "^${IFACE}[[:space:]]+" /proc/net/vlan/config
}
_get_vlans()
{
[ -e /proc/net/vlan/config ] || return 1
sed -n -e 's/^\(.*[0-9]\) \(.* \) .*'"${IFACE}"'$/\1/p' /proc/net/vlan/config
}
_check_vlan()
{
if [ ! -d /proc/net/vlan ]; then
modprobe 8021q
if [ ! -d /proc/net/vlan ]; then
eerror "VLAN (802.1q) support is not present in this kernel"
return 1
fi
fi
}
vlan_pre_start()
{
local vc="$(_get_array "vconfig_${IFVAR}")"
[ -z "${vc}" ] && return 0
_check_vlan || return 1
_exists || return 1
local v= x= e=
local IFS="$__IFS"
for v in ${vc}; do
unset IFS
case "${v}" in
set_name_type" "*) x=${v};;
*)
set -- ${v}
x="$1 ${IFACE}"
shift
x="${x} $@"
;;
esac
e="$(vconfig ${x} 2>&1 1>/dev/null)"
[ -z "${e}" ] && continue
eerror "${e}"
return 1
done
}
vlan_post_start()
{
local vlans=
eval vlans=\$vlans_${IFACE}
[ -z "${vlans}" ] && return 0
_check_vlan || return 1
_exists || return 1
local vlan= e= s=
for vlan in ${vlans}; do
einfo "Adding VLAN ${vlan} to ${IFACE}"
e="$(vconfig add "${IFACE}" "${vlan}" 2>&1 1>/dev/null)"
if [ -n "${e}" ]; then
eend 1 "${e}"
continue
fi
# We may not want to start the vlan ourselves
eval s=\$vlan_start_${IFVAR}
yesno ${s:-yes} || continue
# We need to work out the interface name of our new vlan id
local ifname="$( \
sed -n -e 's/^\([^[:space:]]*\) *| '"${vlan}"' *| .*'"${iface}"'$/\1/p' \
/proc/net/vlan/config )"
mark_service_started "net.${ifname}"
(
export RC_SVCNAME="net.${ifname}"
start
) || mark_service_stopped "net.${ifname}"
done
return 0
}
vlan_post_stop()
{
local vlan=
for vlan in $(_get_vlans); do
einfo "Removing VLAN ${vlan##*.} from ${IFACE}"
(
export RC_SVCNAME="net.${vlan}"
stop
) && {
mark_service_stopped "net.${vlan}"
vconfig rem "${vlan}" >/dev/null
}
done
return 0
}