diff options
author | William Hubbs <w.d.hubbs@gmail.com> | 2013-03-18 17:10:58 -0500 |
---|---|---|
committer | William Hubbs <w.d.hubbs@gmail.com> | 2013-04-06 17:26:10 -0500 |
commit | 1e6cef540789ce8b781eb3037ff7058c1a07c07d (patch) | |
tree | ce43b4782513deea473fc9aa3348c146307dda4f /net | |
download | netifrc-1e6cef540789ce8b781eb3037ff7058c1a07c07d.tar.gz netifrc-1e6cef540789ce8b781eb3037ff7058c1a07c07d.tar.bz2 netifrc-1e6cef540789ce8b781eb3037ff7058c1a07c07d.zip |
initial commit ported from OpenRc
Diffstat (limited to 'net')
36 files changed, 5447 insertions, 0 deletions
diff --git a/net/.gitignore b/net/.gitignore new file mode 100644 index 0000000..7adf813 --- /dev/null +++ b/net/.gitignore @@ -0,0 +1,4 @@ +ifconfig.sh +ifwatchd.sh +iwconfig.sh +udhcpc.sh diff --git a/net/Makefile b/net/Makefile new file mode 100644 index 0000000..a03c171 --- /dev/null +++ b/net/Makefile @@ -0,0 +1,24 @@ +DIR= ${LIBEXECDIR}/net +SRCS= ifconfig.sh.in ${SRCS-${OS}} +INC= dhclient.sh dhcpcd.sh ifconfig.sh macchanger.sh macnet.sh \ + ssidnet.sh system.sh wpa_supplicant.sh ${INC-${OS}} + +MK= ../mk +include ${MK}/os.mk + +SRCS-FreeBSD= iwconfig.sh.in +INC-FreeBSD= iwconfig.sh + +SRCS-Linux= iwconfig.sh.in udhcpc.sh.in +INC-Linux= adsl.sh apipa.sh arping.sh bonding.sh br2684ctl.sh bridge.sh \ + ccwgroup.sh clip.sh ethtool.sh iproute2.sh ifplugd.sh ip6to4.sh \ + ipppd.sh iwconfig.sh netplugd.sh pppd.sh pump.sh tuntap.sh udhcpc.sh \ + vlan.sh macvlan.sh ip6rd.sh firewalld.sh + +SRCS-NetBSD= ifwatchd.sh.in +INC-NetBSD= ifwatchd.sh + +%.sh.in: %.sh${SFX} + ${CP} $< $@ + +include ${MK}/scripts.mk diff --git a/net/adsl.sh b/net/adsl.sh new file mode 100644 index 0000000..744ebc2 --- /dev/null +++ b/net/adsl.sh @@ -0,0 +1,74 @@ +# Copyright (c) 2004-2007 Gentoo Foundation +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# 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 +} diff --git a/net/apipa.sh b/net/apipa.sh new file mode 100644 index 0000000..a4aee8a --- /dev/null +++ b/net/apipa.sh @@ -0,0 +1,48 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +apipa_depend() +{ + program /sbin/arping +} + +_random() +{ + local r=${RANDOM} # checkbashisms: false positive, we handle it AFTERWARDS + 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 -= 1 )) + veend 0 + eoutdent + return 0 + fi + + : $(( i += 1 )) + done + + eerror "No free address found!" + eoutdent + return 1 +} diff --git a/net/arping.sh b/net/arping.sh new file mode 100644 index 0000000..83360d7 --- /dev/null +++ b/net/arping.sh @@ -0,0 +1,131 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# 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 -= 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 +} diff --git a/net/bonding.sh b/net/bonding.sh new file mode 100644 index 0000000..fb00825 --- /dev/null +++ b/net/bonding.sh @@ -0,0 +1,223 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +bonding_depend() +{ + before interface macchanger + program /sbin/ifconfig /bin/ifconfig + # If you do not have sysfs, you MUST have this binary instead for ioctl + # Also you will loose some functionality that cannot be done via sysfs: + if [ ! -d /sys/class/net ]; then + program /sbin/ifenslave + fi +} + +_config_vars="$_config_vars slaves" + +_is_bond() +{ + [ -f "/proc/net/bonding/${IFACE}" ] +} + +bonding_pre_start() +{ + local x= s= n= slaves= primary= + + slaves="$(_get_array "slaves_${IFVAR}")" + unset slaves_${IFVAR} + + eval primary="\$primary_${IFVAR}" + unset primary_${IFVAR} + + eval subsume="\$subsume_${IFVAR}" + unset subsume_${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 + + if [ ! -d /sys/class/net ]; then + ewarn "sysfs is not available! You will be unable to create new bonds, or change dynamic parameters!" + 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 + + # Interface must be down in order to configure + _down + + # Configure the bond mode & link monitoring, then we can reloop to ensure + # we configure all other options + # mode needs to be done before all other options. + # miimon needs to be done BEFORE downdelay + [ -d /sys/class/net ] && for n in mode miimon; do + x=/sys/class/net/"${IFACE}"/bonding/$n + [ -f "${x}" ] || continue + eval s=\$${n}_${IFVAR} + if [ -n "${s}" ]; then + einfo "Setting ${n}: ${s}" + echo "${s}" >"${x}" || \ + eerror "Failed to configure $n (${n}_${IFVAR})" + fi + done + # Nice and dynamic for remaining options:) + [ -d /sys/class/net ] && for x in /sys/class/net/"${IFACE}"/bonding/*; do + [ -f "${x}" ] || continue + n=${x##*/} + eval s=\$${n}_${IFVAR} + # skip mode and miimon + [ "${n}" == "mode" -o "${n}" == "miimon" ] && continue + if [ -n "${s}" ]; then + einfo "Setting ${n}: ${s}" + echo "${s}" >"${x}" || \ + eerror "Failed to configure $n (${n}_${IFVAR})" + fi + done + + ebegin "Adding slaves to ${IFACE}" + eindent + einfo "${slaves}" + + # Check that our slaves exist + ( + for IFACE in ${slaves}; do + _exists true || return 1 + done + + # Unless we are subsuming an existing interface (NFS root), we down + # slave interfaces to work around bugs supposedly in some chipsets + # that cause failure to enslave from other states. + if [ -z "${subsume}" ]; then + for IFACE in ${slaves}; do + _delete_addresses + _down + done + fi + ) + + # Now force the master to up + # - First test for interface subsume request (required for NFS root) + if [ -n "${subsume}" ]; then + einfo "Subsuming ${subsume} interface characteristics." + eindent + local oiface=${IFACE} + IFACE=${subsume} + local addr="$(_get_inet_address)" + einfo "address: ${addr}" + IFACE=${oiface} + unset oiface + eoutdent + # subsume (presumably kernel auto-)configured IP + ifconfig ${IFACE} ${addr} up + else + # warn if root on nfs and no subsume interface supplied + local root_fs_type=$(mountinfo -s /) + if [ "${root_fs_type}" = "nfs" ]; then + warn_nfs=1 + ewarn "NFS root detected!!!" + ewarn " If your system crashes here, /etc/conf.d/net needs" + ewarn " subsume_${IFACE}=\"<iface>\" ... where <iface> is the" + ewarn " existing, (usually kernel auto-)configured interface." + fi + # up the interface + _up + fi + + # finally add in slaves + # things needed in the process, and if they are done by ifenslave, openrc, and/or the kernel. + # down new slave interface: ifenslave, openrc + # set mtu: ifenslave, kernel + # set slave MAC: ifenslave, kernel + eoutdent + if [ -d /sys/class/net ]; then + sys_bonding_path=/sys/class/net/"${IFACE}"/bonding + local oiface + oiface=$IFACE + if [ -n "${primary}" ]; then + IFACE=$primary + _down + IFACE=$oiface + echo "+${primary}" >$sys_bonding_path/slaves + echo "${primary}" >$sys_bonding_path/primary + fi + for s in ${slaves}; do + [ "${s}" = "${primary}" ] && continue + if ! grep -q ${s} $sys_bonding_path/slaves; then + IFACE=$s + _down + IFACE=$oiface + echo "+${s}" >$sys_bonding_path/slaves + fi + done + else + ifenslave "${IFACE}" ${slaves} >/dev/null + fi + eend $? + + return 0 #important +} + +bonding_stop() +{ + _is_bond || return 0 + + # Wipe subsumed interface + if [ -n "${subsume}" ]; then + ifconfig ${subsume} 0.0.0.0 + fi + + 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 + if [ -d /sys/class/net ]; then + for s in ${slaves}; do + echo -"${s}" > /sys/class/net/"${IFACE}"/bonding/slaves + done + else + ifenslave -d "${IFACE}" ${slaves} + fi + + # reset all slaves + ( + for IFACE in ${slaves}; do + if _exists; then + _delete_addresses + _down + fi + done + ) + + _down + + if [ -d /sys/class/net ]; then + echo "-${IFACE}" > /sys/class/net/bonding_masters + fi + + eend 0 + return 0 +} diff --git a/net/br2684ctl.sh b/net/br2684ctl.sh new file mode 100644 index 0000000..b3f6119 --- /dev/null +++ b/net/br2684ctl.sh @@ -0,0 +1,50 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +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 $(_which 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 $? +} diff --git a/net/bridge.sh b/net/bridge.sh new file mode 100644 index 0000000..60d3eeb --- /dev/null +++ b/net/bridge.sh @@ -0,0 +1,190 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +bridge_depend() +{ + before interface macnet + program brctl +} + +_config_vars="$_config_vars bridge bridge_add brctl" + +_is_bridge() +{ + [ -d /sys/class/net/"${1:-${IFACE}}"/bridge ] + return $? +} + +_is_bridge_port() +{ + [ -d /sys/class/net/"${1:-${IFACE}}"/brport ] + return $? +} + +_bridge_ports() +{ + for x in /sys/class/net/"${1:-${IFACE}}"/brif/*; do + n=${x##*/} + echo $n + done +} + +bridge_pre_start() +{ + local brif= oiface="${IFACE}" e= x= + # ports is for static add + local ports="$(_get_array "bridge_${IFVAR}")" + # old config options + local opts="$(_get_array "brctl_${IFVAR}")" + # brif is used for dynamic add + eval brif=\$bridge_add_${IFVAR} + + # we need a way to if the bridge exists in a variable name, not just the + # contents of a variable. Eg if somebody has only bridge_add_eth0='br0', + # with no other lines mentioning br0. + eval bridge_unset=\${bridge_${IFVAR}-y\} + eval brctl_unset=\${brctl_${IFVAR}-y\} + + if [ -z "${brif}" -a "${brctl_unset}" = 'y' ]; then + if [ -z "${ports}" -a "${bridge_unset}" = "y" ]; then + #eerror "Misconfigured static bridge detected (see net.example)" + return 0 + fi + fi + + # If the bridge was already up, we should clear it + [ "${bridge_unset}" != "y" ] && bridge_post_stop + + ( + # Normalize order of variables + if [ -z "${ports}" -a -n "${brif}" ]; then + # Dynamic mode detected + ports="${IFACE}" + IFACE="${brif}" + IFVAR=$(shell_var "${IFACE}") + else + # Static mode detected + ports="${ports}" + metric=1000 + fi + + if ! _is_bridge ; then + ebegin "Creating bridge ${IFACE}" + if ! brctl addbr "${IFACE}"; then + eend 1 + return 1 + fi + fi + + # TODO: does this reset the bridge every time we add a interface to the + # bridge? We should probably NOT do that. + + # Old configuration set mechanism + # Only a very limited subset of the options are available in the old + # configuration method. The sysfs interface is in the next block instead. + local IFS="$__IFS" + for x in ${opts}; do + unset IFS + set -- ${x} + x=$1 + shift + set -- "${x}" "${IFACE}" "$@" + brctl "$@" + done + unset IFS + + # New configuration set mechanism, matches bonding + for x in /sys/class/net/"${IFACE}"/bridge/*; do + [ -f "${x}" ] || continue + n=${x##*/} + eval s=\$${n}_${IFVAR} + if [ -n "${s}" ]; then + einfo "Setting ${n}: ${s}" + echo "${s}" >"${x}" || \ + eerror "Failed to configure $n (${n}_${IFVAR})" + fi + done + + if [ -n "${ports}" ]; then + einfo "Adding ports to ${IFACE}" + eindent + + local BR_IFACE="${IFACE}" + for x in ${ports}; do + ebegin "${x}" + local IFACE="${x}" + local IFVAR=$(shell_var "${IFACE}") + if ! _exists "${IFACE}" ; then + eerror "Cannot add non-existent interface ${IFACE} to ${BR_IFACE}" + return 1 + fi + # The interface is known to exist now + _up + if ! brctl addif "${BR_IFACE}" "${x}"; then + eend 1 + return 1 + fi + # Per-interface bridge settings + for x in /sys/class/net/"${IFACE}"/brport/*; do + [ -f "${x}" ] || continue + n=${x##*/} + eval s=\$${n}_${IFVAR} + if [ -n "${s}" ]; then + einfo "Setting ${n}@${IFACE}: ${s}" + echo "${s}" >"${x}" || \ + eerror "Failed to configure $n (${n}_${IFVAR})" + fi + done + eend 0 + done + eoutdent + fi + ) || return 1 + + # Bring up the bridge + _set_flag promisc + _up +} + +bridge_post_stop() +{ + local port= ports= delete=false extra= + + if _is_bridge "${IFACE}"; then + ebegin "Destroying bridge ${IFACE}" + _down + for x in /sys/class/net/"${IFACE}"/brif/*; do + [ -s $x ] || continue + n=${x##*/} + ports="${ports} ${n}" + done + delete=true + iface=${IFACE} + eindent + else + # We are taking down an interface that is part of a bridge maybe + ports="${IFACE}" + local brport_dir="/sys/class/net/${IFACE}/brport" + [ -d ${brport_dir} ] || return 0 + iface=$(readlink ${brport_dir}/bridge) + iface=${iface##*/} + [ -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 +} diff --git a/net/ccwgroup.sh b/net/ccwgroup.sh new file mode 100644 index 0000000..aaf7de6 --- /dev/null +++ b/net/ccwgroup.sh @@ -0,0 +1,106 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +_config_vars="$_config_vars ccwgroup" + +ccwgroup_depend() +{ + before interface +} + +ccwgroup_load_modules() +{ + # make sure we have ccwgroup support or this is a crap shoot + if [ ! -d /sys/bus/ccwgroup ] ; then + modprobe -q ccwgroup + if [ ! -d /sys/bus/ccwgroup ] ; then + eerror "ccwgroup support missing in kernel" + return 1 + fi + fi + + # verify the specific interface is supported + if [ ! -d /sys/bus/ccwgroup/drivers/$1 ] ; then + modprobe $1 >/dev/null 2>&1 + if [ ! -d /sys/bus/ccwgroup/drivers/$1 ] ; then + eerror "$1 support missing in kernel" + return 1 + fi + fi + + return 0 +} + +ccwgroup_pre_start() +{ + local ccwgroup="$(_get_array "ccwgroup_${IFVAR}")" + [ -z "${ccwgroup}" ] && return 0 + + local ccw_type + eval ccw_type=\${ccwgroup_type_${IFVAR}:-qeth} + + ccwgroup_load_modules ${ccw_type} || return 1 + + einfo "Enabling ccwgroup/${ccw_type} on ${IFACE}" + + set -- ${ccwgroup} + local first=$1; shift + if [ -e /sys/devices/${ccw_type}/${first}/online ]; then + echo "0" >/sys/devices/${ccw_type}/${first}/online + else + echo "${first}$(printf ',%s' "$@")" >/sys/bus/ccwgroup/drivers/${ccw_type}/group + fi + + local var val + for var in $(_get_array "ccwgroup_opts_${IFVAR}") online=1 ; do + val=${var#*=} + var=${var%%=*} + echo "${val}" > /sys/devices/${ccw_type}/${first}/${var} + done + eend $? + + # Now that we've properly configured the device, we can run + # bring the interface up. Common code tried to do this already, + # but it failed because we didn't setup sysfs yet. + _up +} + +ccwgroup_pre_stop() +{ + local path="/sys/class/net/${IFACE}" + + # Erase any existing ccwgroup to be safe + service_set_value ccwgroup_device "" + service_set_value ccwgroup_type "" + + [ ! -L "${path}"/device/driver ] && return 0 + case "$(readlink "${path}"/device/driver)" in + */bus/ccwgroup/*) ;; + *) return 0;; + esac + + local device + device="$(readlink "${path}"/device)" + device=${device##*/} + service_set_value ccwgroup_device "${device}" + device="$(readlink "${path}"/device/driver)" + device=${device##*/} + service_set_value ccwgroup_type "${device}" +} + +ccwgroup_post_stop() +{ + local device="$(service_get_value ccwgroup_device)" + [ -z "${device}" ] && return 0 + local ccw_type="$(service_get_value ccwgroup_type)" + local path="/sys/devices/${ccw_type}/${device}" + + einfo "Disabling ccwgroup/${ccw_type} on ${IFACE}" + if echo "0" >"${path}"/online && + echo "1" >"${path}"/ungroup ; then + # The device doesn't disappear right away which breaks + # restart, or a quick start up, so wait around. + while [ -e "${path}" ] ; do :; done + fi + eend $? +} diff --git a/net/clip.sh b/net/clip.sh new file mode 100644 index 0000000..2fc290f --- /dev/null +++ b/net/clip.sh @@ -0,0 +1,221 @@ +# Copyright (c) 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 -= 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 +} diff --git a/net/dhclient.sh b/net/dhclient.sh new file mode 100644 index 0000000..8c100dd --- /dev/null +++ b/net/dhclient.sh @@ -0,0 +1,76 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +dhclient_depend() +{ + after interface + program start /sbin/dhclient + provide dhcp +} + +_config_vars="$_config_vars dhcp dhcpcd" + +dhclient_start() +{ + local args= opt= opts= pidfile="/var/run/dhclient-${IFACE}.pid" + local sendhost=true dconf= + + # Get our options + # These options only work in Gentoo, and maybe RedHat + eval args=\$dhclient_${IFVAR} + eval opts=\$dhcp_${IFVAR} + [ -z "${opts}" ] && opts=${dhcp} + + for opt in ${opts}; do + case "${opt}" in + nodns) args="${args} -e PEER_DNS=no";; + nontp) args="${args} -e PEER_NTP=no";; + nogateway) args="${args} -e PEER_ROUTERS=no";; + nosendhost) sendhost=false;; + esac + done + + # Add our route metric + [ "${metric:-0}" != "0" ] && args="${args} -e IF_METRIC=${metric}" + + if ${sendhost}; then + local hname="$(hostname)" + if [ "${hname}" != "(none)" -a "${hname}" != "localhost" ]; then + dhconf="${dhconf} interface \"${IFACE}\" {" + dhconf="${dhconf} send host-name \"${hname}\";" + dhconf="${dhconf}}" + fi + fi + + # Bring up DHCP for this interface + ebegin "Running dhclient" + echo "${dhconf}" | start-stop-daemon --start --exec /sbin/dhclient \ + --pidfile "${pidfile}" \ + -- ${args} -q -1 -pf "${pidfile}" "${IFACE}" + eend $? || return 1 + + _show_address + return 0 +} + +dhclient_stop() +{ + local pidfile="/var/run/dhclient-${IFACE}.pid" opts= + [ ! -f "${pidfile}" ] && return 0 + + # Get our options + if [ -x /sbin/dhclient ]; then + eval opts=\$dhcp_${IFVAR} + [ -z "${opts}" ] && opts=${dhcp} + fi + + ebegin "Stopping dhclient on ${IFACE}" + case " ${opts} " in + *" release "*) dhclient -q -r -pf "${pidfile}" "${IFACE}";; + *) + start-stop-daemon --stop --quiet \ + --exec /sbin/dhclient --pidfile "${pidfile}" + ;; + esac + eend $? +} diff --git a/net/dhcpcd.sh b/net/dhcpcd.sh new file mode 100644 index 0000000..2c0f919 --- /dev/null +++ b/net/dhcpcd.sh @@ -0,0 +1,88 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +dhcpcd_depend() +{ + after interface + program start dhcpcd + provide dhcp + + # We prefer dhcpcd over the others + after dhclient pump udhcpc +} + +_config_vars="$_config_vars dhcp dhcpcd" + +dhcpcd_start() +{ + local args= opt= opts= pidfile="/var/run/dhcpcd-${IFACE}.pid" new=true + eval args=\$dhcpcd_${IFVAR} + [ -z "${args}" ] && args=${dhcpcd} + + # Get our options + eval opts=\$dhcp_${IFVAR} + [ -z "${opts}" ] && opts=${dhcp} + + case "$(dhcpcd --version)" in + "dhcpcd "[123]*) new=false;; + esac + + # Map some generic options to dhcpcd + for opt in ${opts}; do + case "${opt}" in + nodns) + if ${new}; then + args="${args} -C resolv.conf" + else + args="${args} -R" + fi + ;; + nontp) + if ${new}; then + args="${args} -C ntp.conf" + else + args="${args} -N" + fi + ;; + nonis) + if ${new}; then + args="${args} -C yp.conf" + else + args="${args} -Y" + fi + ;; + nogateway) args="${args} -G";; + nosendhost) args="${args} -h ''"; + esac + done + + # Add our route metric if not given + case " $args " in + *" -m "*) ;; + *) [ "${metric:-0}" != 0 ] && args="$args -m $metric";; + esac + + # Bring up DHCP for this interface + ebegin "Running dhcpcd" + + eval dhcpcd "${args}" "${IFACE}" + eend $? || return 1 + + _show_address + return 0 +} + +dhcpcd_stop() +{ + local pidfile="/var/run/dhcpcd-${IFACE}.pid" opts= sig=SIGTERM + [ ! -f "${pidfile}" ] && return 0 + + ebegin "Stopping dhcpcd on ${IFACE}" + eval opts=\$dhcp_${IFVAR} + [ -z "${opts}" ] && opts=${dhcp} + case " ${opts} " in + *" release "*) sig=SIGHUP;; + esac + start-stop-daemon --stop --quiet --signal ${sig} --pidfile "${pidfile}" + eend $? +} diff --git a/net/ethtool.sh b/net/ethtool.sh new file mode 100644 index 0000000..6023d74 --- /dev/null +++ b/net/ethtool.sh @@ -0,0 +1,52 @@ +# Copyright (c) 2011 by Gentoo Foundation +# Released under the 2-clause BSD license. + +ethtool_depend() +{ + program ethtool + before interface +} + +# This is just to trim whitespace, do not add any quoting! +_trim() { + echo $* +} + +ethtool_pre_start() { + local order opt OFS="${OIFS}" + eval order=\$ethtool_order_${IFVAR} + [ -z "${order}" ] && eval order=\$ethtool_order + [ -z "${order}" ] && order="flash change-eeprom change pause coalesce ring offload identify nfc rxfh-indir ntuple" + # ethtool options not used: --driver, --register-dump, --eeprom-dump, --negotiate, --test, --statistics + eindent + for opt in ${order} ; do + local args + eval args=\$ethtool_$(echo $opt | tr - _)_${IFVAR} + + # Skip everything if no arguments + [ -z "${args}" ] && continue + + # Split on \n + OIFS="${IFS}" + local IFS="$__IFS" + + for p in ${args} ; do + IFS="${OIFS}" + local args_pretty="$(_trim "${p}")" + # Do nothing if empty + [ -z "${args_pretty}" ] && continue + [ "${opt}" = "ring" ] && opt="set-ring" + args_pretty="--${opt} $IFACE ${args_pretty}" + args="--${opt} $IFACE ${args}" + ebegin "ethtool ${args_pretty}" + ethtool ${args} + rc=$? + eend $rc "ethtool exit code $rc" + # TODO: ethtool has MANY different exit codes, with no + # documentation as to which ones are fatal or not. For now we + # simply print the exit code and don't stop the start sequence. + done + IFS="${OIFS}" + done + eoutdent +} diff --git a/net/firewalld.sh b/net/firewalld.sh new file mode 100644 index 0000000..ae83c57 --- /dev/null +++ b/net/firewalld.sh @@ -0,0 +1,38 @@ +# Copyright (c) 2012 Doug Goldstein <cardoe@cardoe.com> +# Released under the 2-clause BSD license. + +firewalld_depend() +{ + after interface + before dhcp + program firewall-cmd + [ "$IFACE" != "lo" ] && need firewalld +} + +_config_vars="$_config_vars firewalld_zone" + +firewalld_post_start() +{ + local firewalld_zone= + eval firewalld_zone=\$firewalld_zone_${IFVAR} + + _exists || return 0 + + if [ "${IFACE}" != "lo" ]; then + firewall-cmd --zone="${firewalld_zone}" \ + --change-interface="${IFACE}" > /dev/null 2>&1 + fi + + return 0 +} + +firewalld_pre_stop() +{ + _exists || return 0 + + if [ "${IFACE}" != "lo" ]; then + firewall-cmd --remove-interface="${IFACE}" > /dev/null 2>&1 + fi + + return 0 +} diff --git a/net/ifconfig.sh.BSD.in b/net/ifconfig.sh.BSD.in new file mode 100644 index 0000000..c7e98a2 --- /dev/null +++ b/net/ifconfig.sh.BSD.in @@ -0,0 +1,264 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# 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 += 1 )) + done + ;; + default) + for x in $(ifconfig -l); do + if [ "${x}" = "${IFACE}" ]; then + echo "${i}" + return 0 + fi + : $(( 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 ali rest; do + case "${inet}" in + inet|inet6) + if [ "${address}" = "alias" ]; then + address="${ali}" + fi + 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 +} + +# Is the interface administratively/operationally up? +# The 'UP' status in ifconfig is the administrative status +# Operational state does not seem to be available in BSD? +# 0: up +# 1: down +# 2: invalid arguments +is_admin_up() +{ + local iface="$1" + [ -z "$iface" ] && iface="$IFACE" + ifconfig "${iface}" | \ + sed -n '1,1{ /flags=.*[<,]UP[,>]/{ q 0 }}; q 1; ' +} + +is_oper_up() +{ + eerror "TODO: is_oper_up not available on BSD" + return 2 +} diff --git a/net/ifconfig.sh.Linux.in b/net/ifconfig.sh.Linux.in new file mode 100644 index 0000000..2afa66c --- /dev/null +++ b/net/ifconfig.sh.Linux.in @@ -0,0 +1,328 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +ifconfig_depend() +{ + program /sbin/ifconfig /bin/ifconfig + provide interface +} + +_up() +{ + ifconfig "${IFACE}" up +} + +_down() +{ + ifconfig "${IFACE}" down +} + +_exists() +{ + [ -e /sys/class/net/"$IFACE" ] +} + +_ifindex() +{ + local index=-1 + local f v + if [ -e /sys/class/net/"${IFACE}"/ifindex ]; then + index=$(cat /sys/class/net/"${IFACE}"/ifindex) + else + for f in /sys/class/net/*/ifindex ; do + v=$(cat $f) + [ $v -gt $index ] && index=$v + done + : $(( index += 1 )) + fi + echo "${index}" + return 0 +} + +_is_wireless() +{ + # Support new sysfs layout + [ -d /sys/class/net/"${IFACE}"/wireless -o \ + -d /sys/class/net/"${IFACE}"/phy80211 ] && 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\|ether\) \(..:..:..:..:..:..\).*/\2/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:\|inet \)\([^ ]*\).*\(Mask:\|netmask \)\([^ ]*\).*/\2 \4/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 += 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 -= 1 )) + : $(( done += 1 )) + done + + if [ ${done} -lt 4 ]; then + while [ ${i} -lt ${frac} ]; do + : $(( sum += cur )) + : $(( cur /= 2 )) + : $(( i += 1 )) + done + netmask="${netmask}.${sum}" + : $(( done += 1 )) + + while [ ${done} -lt 4 ]; do + netmask="${netmask}.0" + : $(( 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} pointopoint";; + *) cmd="${cmd} $1";; + esac + shift + done + + ifconfig "${iface}" ${cmd} +} + +_add_route() +{ + local inet6= family= + + if [ "$1" = "-A" -o "$1" = "-f" -o "$1" = "-family" ]; then + family="-A $2" + shift; shift + elif [ "$1" = "-4" ]; then + family="-A inet" + shift + elif [ "$1" = "-6" ]; then + family="-A inet6" + shift + fi + + 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 + *:*|default*) [ "$1" = "-net" -o "$1" = "-host" ] && shift;; + esac + + route ${family} 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:\|inet6\) \([^ ]*\) .*\(Scope:[^L]\|scopeid [^<]*<[^l]\).*/\2/p'); do + if [ "${IFACE}" = "lo" ]; then + case "${addr}" in + "::1/128"|"/128") continue;; + esac + fi + einfo "${addr}" + ifconfig "${IFACE}" inet6 del "${addr}" + done + + return 0 +} + +_has_carrier() +{ + return 0 +} + +_tunnel() +{ + iptunnel "$@" +} + +ifconfig_pre_start() +{ + local tunnel= + eval tunnel=\$iptunnel_${IFVAR} + if [ -n "${tunnel}" ]; then + # Set our base metric to 1000 + metric=1000 + ebegin "Creating tunnel ${IFVAR}" + iptunnel add ${tunnel} + eend $? || return 1 + _up + fi + + # 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}" + + return 0 +} + +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 $? +} + +# Is the interface administratively/operationally up? +# The 'UP' status in ifconfig/iproute2 is the administrative status +# Operational state is available in iproute2 output as 'state UP', or the +# operstate sysfs variable. +# 0: up +# 1: down +# 2: invalid arguments +is_admin_up() +{ + local iface="$1" + [ -z "$iface" ] && iface="$IFACE" + ifconfig "${iface}" | \ + sed -n '1,1{ /flags=.*[<,]UP[,>]/{ q 0 }}; q 1; ' +} + +is_oper_up() +{ + local iface="$1" + [ -z "$iface" ] && iface="$IFACE" + read state </sys/class/net/"${iface}"/operstate + [ "x$state" = "up" ] +} diff --git a/net/ifplugd.sh b/net/ifplugd.sh new file mode 100644 index 0000000..0bcde2f --- /dev/null +++ b/net/ifplugd.sh @@ -0,0 +1,94 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# 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 "ifplugd 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 + + # IFACE-specific, then global, then default + eval timeout=\$plug_timeout_${IFVAR} + [ -z "${timeout}" ] && timeout=$plug_timeout + [ -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 += 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 $? +} diff --git a/net/ifwatchd.sh.BSD.in b/net/ifwatchd.sh.BSD.in new file mode 100644 index 0000000..289b120 --- /dev/null +++ b/net/ifwatchd.sh.BSD.in @@ -0,0 +1,59 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +_config_vars="$_config_vars plug_timeout" + +ifwatchd_depend() +{ + program start /usr/sbin/ifwatchd + after macnet rename wireless + before interface + provide plug +} + +ifwatchd_pre_start() +{ + # We don't start ifwatchd 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 "ifwatchd only works on interfaces with a valid MAC address" + return 0 + fi + + ebegin "Starting ifwatchd on ${IFACE}" + + # Mark the us as inactive so ifwatchd can restart us + mark_service_inactive + + # Start ifwatchd + IN_BACKGROUND=yes ; export IN_BACKGROUND + start-stop-daemon --start --exec /usr/sbin/ifwatchd \ + -- -c "@LIBEXECDIR@/sh/ifwatchd-carrier.sh" \ + -n "@LIBEXECDIR@/sh/ifwatchd-nocarrier.sh" "${IFACE}" + unset IN_BACKGROUND + eend "$?" || return 1 + + einfo "Backgrounding ..." + exit 1 +} + +ifwatchd_stop() +{ + yesno ${IN_BACKGROUND} && return 0 + + start-stop-daemon --test --quiet --stop --exec /usr/sbin/ifwatchd \ + -- -c "@LIBEXECDIR@/sh/ifwatchd-carrier.sh" \ + -n "@LIBEXECDIR@/sh/ifwatchd-nocarrier.sh" "${IFACE}" \ + || return 0 + + ebegin "Stopping ifwatchd on" "${IFACE}" + start-stop-daemon --stop --exec /usr/sbin/ifwatchd \ + -- -c "@LIBEXECDIR@/sh/ifwatchd-carrier.sh" \ + -n "@LIBEXECDIR@/sh/ifwatchd-nocarrier.sh" "${IFACE}" + eend $? +} diff --git a/net/ip6rd.sh b/net/ip6rd.sh new file mode 100644 index 0000000..a35e2b7 --- /dev/null +++ b/net/ip6rd.sh @@ -0,0 +1,175 @@ +# Copyright (c) 2011 by Gentoo Foundation +# Released under the 2-clause BSD license. + +_config_vars="$_config_vars link prefix suffix ipv4mask relay" + +ip6rd_depend() +{ + program ip + after interface +} + +ip6rd_pre_start() +{ + # ALL interfaces run pre_start blocks, not just those with something + # assigned, so we must check if we need to run on this interface before we + # do so. + local config + eval config=\$config_${IFVAR} + [ "$config" = "ip6rd" ] || return 0 + + case "${MODULES}" in + *" ifconfig "*) + eerror "ifconfig is not supported for 6rd" + eerror "Please emerge sys-apps/iproute2" + return 1 + ;; + esac + + local host= suffix= relay= addr= iface=${IFACE} config_ip6rd= localip= ipv4mask= + eval host=\$link_${IFVAR} + if [ -z "${host}" ]; then + eerror "link_${IFVAR} not set" + return 1 + fi + + eval host=\${link_${IFVAR}} + eval ipv4mask=\${ipv4mask_${IFVAR}:-0} + eval suffix=\${suffix_${IFVAR}:-1} + eval relay=\${relay_${IFVAR}} + eval prefix=\${prefix_${IFVAR}} + + IFACE=${host} + addrs=$(_get_inet_addresses) + IFACE=${iface} + if [ -z "${addrs}" ]; then + eerror "${host} is not configured with an IPv4 address" + return 1 + fi + # TODO: Get this settings from DHCP (Option 212) + if [ -z "${prefix}" ]; then + eerror "prefix_${IFVAR} not set" + return 1 + fi + if [ -z "${relay}" ]; then + eerror "relay_${IFVAR} not set" + return 1 + fi + for addr in ${addrs}; do + # Strip the subnet + local ip="${addr%/*}" subnet="${addr#*/}" + # We don't work on private IPv4 addresses + if _ip6rd_inet_is_private_network "${ip}" + then + continue + fi + + local ip6= ip6_prefix="${prefix%::/*}" ip6_subnet="${prefix#*/}" + ip6_subnet=$((ip6_subnet + (32-ipv4mask))) + eval ip6="$(printf "${ip6_prefix}:%s::%s" \ + $(_ip6rd_prefix_shave_bits ${ip} ${ipv4mask}) ${suffix})" + veinfo "Derived IPv6 address: ${ip6}" + + # Now apply our IPv6 address to our config + config_ip6rd="${config_ip6rd}${config_ip6rd:+ }${ip6}/${ip6_subnet}" + + if [ -n "${localip}" ]; then + localip="any" + else + localip="${ip}" + fi + done + + if [ -z "${config_ip6rd}" ]; then + eerror "No global IPv4 addresses found on interface ${host}" + return 1 + fi + + ebegin "Creating 6rd tunnel ${IFACE}" + if [ "${IFACE}" != "sit0" ]; then + _tunnel add "${IFACE}" mode sit ttl 255 remote any local "${localip}" + fi + _tunnel 6rd dev "${IFACE}" 6rd-prefix "${prefix}" + eend $? || return 1 + _up + + routes_ip6rd="2003::/3 via ::${relay} metric 2147483647" + service_set_value "config_ip6rd_$IFVAR" "$config_ip6rd" + service_set_value "routes_ip6rd_$IFVAR" "$routes_ip6rd" +} + +ip6rd_start() +{ + local config_ip6rd=$(service_get_value "config_ip6rd_$IFVAR") + local routes_ip6rd=$(service_get_value "routes_ip6rd_$IFVAR") + + # Now apply our config + eval config_${config_index}=\'"${config_ip6rd}"\' + : $(( config_index -= 1 )) + + # Add a route for us, ensuring we don't delete anything else + local routes="$(_get_array "routes_${IFVAR}") +$routes_ip6rd" + eval routes_${IFVAR}=\$routes +} + +_ip6rd_inet_atoi() +{ + local IFS="${IFS}." ipi=0 j=3 + for i in $1 ; do + # post-decrement isn't valid + ipi=$(( ipi | (i << (8*j)) )) + j=$(( j - 1 )) + done + echo ${ipi} +} + +_ip6rd_inet_itoa() +{ + local ipi=$1 bitmask v + bitmask=$(( (1 << 24)-1 )) + for i in 0 1 2 3; do + v=$(( (ipi & ~bitmask) >> 24 )) + ipi=$(( (ipi & bitmask) << 8 )) + if [ $i != 3 ] ; then + printf "%d." $v + else + printf "%d\n" $v + fi + done +} + +_ip6rd_inet_get_network() +{ + local a=$(_ip6rd_inet_atoi $1) + local net=$(( a & ( (1<<$2)-1 ) )) + local cidr=$(( 32 - $2 )) + echo $(_ip6rd_inet_itoa $(( (net << cidr ) )) ) +} + +_ip6rd_inet_is_private_network() +{ + if [ "$(_ip6rd_inet_get_network $1 16)" = "192.168.0.0" ]\ + || [ "$(_ip6rd_inet_get_network $1 8)" = "10.0.0.0" ]\ + || [ "$(_ip6rd_inet_get_network $1 12)" = "172.16.0.0" ]\ + || [ "$(_ip6rd_inet_get_network $1 16)" = "169.254.0.0" ] + then + return 0; + fi + return 1; +} + +_ip6rd_prefix_shave_bits() +{ + local ipi= + ipi=$(( ($(_ip6rd_inet_atoi $1) & (1<<(32-$2))-1) << $2)) + if [ $2 -le 16 ] + then + printf "%04x:%0$(( (16-$2>>2)+(($2%4)?1:0) ))x" \ + $((ipi >> 16)) $((ipi & (1<<(16-$2))-1)) + elif [ $2 -lt 32 ] + then + printf "%0$(( (32-$2>>2)+(($2%4)?1:0) ))x" \ + $((ipi >> 16)) + fi +} diff --git a/net/ip6to4.sh b/net/ip6to4.sh new file mode 100644 index 0000000..51b3858 --- /dev/null +++ b/net/ip6to4.sh @@ -0,0 +1,116 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +_config_vars="$_config_vars link suffix relay" + +ip6to4_depend() +{ + after interface + program ip +} + +ip6to4_pre_start() +{ + # ALL interfaces run pre_start blocks, not just those with something + # assigned, so we must check if we need to run on this interface before we + # do so. + local config + eval config=\$config_${IFVAR} + [ "$config" = "ip6to4" ] || return 0 + + 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} config_ip6to4= 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 += 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 + config_ip6to4="${config_ip6to4}${config_ip6to4:+ }${ip6}/48" + + if [ -n "${localip}" ]; then + localip="any" + else + localip="${ip}" + fi + done + + if [ -z "${config_ip6to4}" ]; 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 + routes_ip6to4="2003::/3 via ::${relay} metric 2147483647" + service_set_value "config_ip6to4_$IFVAR" "$config_ip6to4" + service_set_value "routes_ip6to4_$IFVAR" "$routes_ip6to4" +} + +ip6to4_start() +{ + local config_ip6to4=$(service_get_value "config_ip6to4_$IFVAR") + local routes_ip6to4=$(service_get_value "routes_ip6to4_$IFVAR") + + # Now apply our config + eval config_${config_index}=\'"${config_ip6to4}"\' + : $(( config_index -= 1 )) + + # Add a route for us, ensuring we don't delete anything else + local routes="$(_get_array "routes_${IFVAR}") +$routes_ip6to4" + eval routes_${IFVAR}=\$routes +} diff --git a/net/ipppd.sh b/net/ipppd.sh new file mode 100644 index 0000000..537065a --- /dev/null +++ b/net/ipppd.sh @@ -0,0 +1,48 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# 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 $? +} diff --git a/net/iproute2.sh b/net/iproute2.sh new file mode 100644 index 0000000..3bab7b7 --- /dev/null +++ b/net/iproute2.sh @@ -0,0 +1,404 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +iproute2_depend() +{ + program ip + provide interface + after ifconfig +} + +_up() +{ + ip link set dev "${IFACE}" up +} + +_down() +{ + ip link set dev "${IFACE}" down +} + +_exists() +{ + [ -e /sys/class/net/"$IFACE" ] +} + +_ifindex() +{ + local index=-1 + local f v + if [ -e /sys/class/net/"${IFACE}"/ifindex ]; then + index=$(cat /sys/class/net/"${IFACE}"/ifindex) + else + for f in /sys/class/net/*/ifindex ; do + v=$(cat $f) + [ $v -gt $index ] && index=$v + done + : $(( index += 1 )) + fi + echo "${index}" + return 0 +} + +_is_wireless() +{ + # Support new sysfs layout + [ -d /sys/class/net/"${IFACE}"/wireless -o \ + -d /sys/class/net/"${IFACE}"/phy80211 ] && 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 dev "${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 dev "${IFACE}" address "$1" +} + +_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 + local x + local address netmask broadcast peer anycast label scope + local valid_lft preferred_lft home nodad + local confflaglist + address="$1" ; shift + while [ -n "$*" ]; do + x=$1 ; shift + case "$x" in + netmask|ne*) + netmask="/$(_netmask2cidr "$1")" ; shift ;; + broadcast|brd|br*) + broadcast="$1" ; shift ;; + pointopoint|pointtopoint|peer|po*|pe*) + peer="$1" ; shift ;; + anycast|label|scope|valid_lft|preferred_lft|a*|l*|s*|v*|pr*) + case $x in + a*) x=anycast ;; + l*) x=label ;; + s*) x=scope ;; + v*) x=valid_lft ;; + pr*) x=preferred_lft ;; + esac + eval "$x=$1" ; shift ;; + home|nodad|h*|no*) + case $x in h*) x=home ;; n*) x=nodad ;; esac + # FIXME: If we need to reorder these, this will take more code + confflaglist="${confflaglist} $x" ; ;; + *) + ewarn "Unknown argument to config_$IFACE: $x" + esac + done + + # Always scope lo addresses as host unless specified otherwise + if [ "${IFACE}" = "lo" ]; then + [ -z "$scope" ] && scope="host" + fi + + # figure out the broadcast address if it is not specified + # This must NOT be set for IPv6 addresses + if [ "${address#*:}" = "${address}" ]; then + [ -z "$broadcast" ] && broadcast="+" + elif [ -n "$broadcast" ]; then + eerror "Broadcast keywords are not valid with IPv6 addresses" + return 1 + fi + + # This must appear on a single line, continuations cannot be used + set -- "${address}${netmask}" ${peer:+peer} ${peer} ${broadcast:+broadcast} ${broadcast} ${anycast:+anycast} ${anycast} ${label:+label} ${label} ${scope:+scope} ${scope} dev "${IFACE}" ${valid_lft:+valid_lft} $valid_lft ${preferred_lft:+preferred_lft} $preferred_lft $confflaglist + veinfo ip addr add "$@" + ip addr add "$@" +} + +_add_route() +{ + local family= + + if [ "$1" = "-A" -o "$1" = "-f" -o "$1" = "-family" ]; then + family="-f $2" + shift; shift + elif [ "$1" = "-4" ]; then + family="-f inet" + shift + elif [ "$1" = "-6" ]; then + family="-f inet6" + shift + fi + + 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);; + *) cmd="${cmd} $1";; + esac + shift + done + + # We cannot use a metric if we're using a nexthop + if ! ${have_metric} && \ + [ -n "${metric}" -a \ + "${cmd##* nexthop }" = "$cmd" ] + then + cmd="${cmd} metric ${metric}" + fi + + veinfo ip ${family} route append ${cmd} dev "${IFACE}" + ip ${family} 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() +{ + LC_ALL=C ip link show dev "${IFACE}" | grep -q "LOWER_UP" +} + +_tunnel() +{ + ip tunnel "$@" +} + +# This is just to trim whitespace, do not add any quoting! +_trim() { + echo $* +} + +# This is our interface to Routing Policy Database RPDB +# This allows for advanced routing tricks +_ip_rule_runner() { + local cmd rules OIFS="${IFS}" family + if [ "$1" = "-4" -o "$1" = "-6" ]; then + family="$1" + shift + else + family="-4" + fi + cmd="$1" + rules="$2" + veindent + local IFS="$__IFS" + for ru in $rules ; do + unset IFS + ruN="$(_trim "${ru}")" + [ -z "${ruN}" ] && continue + vebegin "${cmd} ${ruN}" + ip $family rule ${cmd} ${ru} + veend $? + local IFS="$__IFS" + done + IFS="${OIFS}" + veoutdent +} + +iproute2_pre_start() +{ + local tunnel= + eval tunnel=\$iptunnel_${IFVAR} + if [ -n "${tunnel}" ]; then + # Set our base metric to 1000 + metric=1000 + # Bug#347657: If the mode is 'ipip6' or 'ip6ip6', the -6 must be passed + # to iproute2 during tunnel creation. + local ipproto='' + [ "${tunnel##mode ipip6}" != "${tunnel}" ] && ipproto='-6' + [ "${tunnel##mode ip6ip6}" != "${tunnel}" ] && ipproto='-6' + + ebegin "Creating tunnel ${IFVAR}" + ip ${ipproto} tunnel add ${tunnel} name "${IFACE}" + eend $? || return 1 + _up + fi + + # MTU support + local mtu= + eval mtu=\$mtu_${IFVAR} + [ -n "${mtu}" ] && ip link set dev "${IFACE}" mtu "${mtu}" + + # TX Queue Length support + local len= + eval len=\$txqueuelen_${IFVAR} + [ -n "${len}" ] && ip link set dev "${IFACE}" txqueuelen "${len}" + + return 0 +} + +_iproute2_ipv6_tentative() +{ + # Only check tentative when we have a carrier. + _has_carrier || return 1 + LC_ALL=C ip addr show dev "${IFACE}" | \ + grep -q "^[[:space:]]*inet6 .* tentative" +} + +iproute2_post_start() +{ + local n=5 + + # Kernel may not have IP built in + if [ -e /proc/net/route ]; then + local rules="$(_get_array "rules_${IFVAR}")" + if [ -n "${rules}" ]; then + if ! ip -4 rule list | grep -q "^"; then + eerror "IP Policy Routing (CONFIG_IP_MULTIPLE_TABLES) needed for ip rule" + else + service_set_value "ip_rule" "${rules}" + einfo "Adding IPv4 RPDB rules" + _ip_rule_runner -4 add "${rules}" + fi + fi + ip -4 route flush table cache dev "${IFACE}" + fi + + # Kernel may not have IPv6 built in + if [ -e /proc/net/ipv6_route ]; then + local rules="$(_get_array "rules6_${IFVAR}")" + if [ -n "${rules}" ]; then + if ! ip -6 rule list | grep -q "^"; then + eerror "IPv6 Policy Routing (CONFIG_IPV6_MULTIPLE_TABLES) needed for ip rule" + else + service_set_value "ip6_rule" "${rules}" + einfo "Adding IPv6 RPDB rules" + _ip_rule_runner -6 add "${rules}" + fi + fi + ip -6 route flush table cache dev "${IFACE}" + fi + + if _iproute2_ipv6_tentative; then + ebegin "Waiting for IPv6 addresses" + while [ $n -ge 0 ]; do + _iproute2_ipv6_tentative || break + sleep 1 + : $(( n -= 1 )) + done + [ $n -ge 0 ] + eend $? + fi + + return 0 +} + +iproute2_post_stop() +{ + # Kernel may not have IP built in + if [ -e /proc/net/route ]; then + local rules="$(service_get_value "ip_rule")" + if [ -n "${rules}" ]; then + einfo "Removing IPv4 RPDB rules" + _ip_rule_runner -4 del "${rules}" + fi + + # Only do something if the interface actually exist + if _exists; then + ip -4 route flush table cache dev "${IFACE}" + fi + fi + + # Kernel may not have IPv6 built in + if [ -e /proc/net/ipv6_route ]; then + local rules="$(service_get_value "ip6_rule")" + if [ -n "${rules}" ]; then + einfo "Removing IPv6 RPDB rules" + _ip_rule_runner -6 del "${rules}" + fi + + # Only do something if the interface actually exist + if _exists; then + ip -6 route flush table cache dev "${IFACE}" + fi + fi + + # 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 +} + +# Is the interface administratively/operationally up? +# The 'UP' status in ifconfig/iproute2 is the administrative status +# Operational state is available in iproute2 output as 'state UP', or the +# operstate sysfs variable. +# 0: up +# 1: down +# 2: invalid arguments +is_admin_up() +{ + local iface="$1" + [ -z "$iface" ] && iface="$IFACE" + ip link show dev $iface | \ + sed -n '1,1{ /[<,]UP[,>]/{ q 0 }}; q 1; ' +} + +is_oper_up() +{ + local iface="$1" + [ -z "$iface" ] && iface="$IFACE" + read state </sys/class/net/"${iface}"/operstate + [ "x$state" = "up" ] +} diff --git a/net/iwconfig.sh.BSD.in b/net/iwconfig.sh.BSD.in new file mode 100644 index 0000000..3acaf66 --- /dev/null +++ b/net/iwconfig.sh.BSD.in @@ -0,0 +1,593 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# 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}\"" + return 1 + fi + ;; + "") ;; + *) + if [ "${key}" != "-" ] ; then + key="-" + ewarn "\"${SSID}\" is not WEP enabled" + 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 += 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 += 1 )) + done + + APS=-1 + eval set -- ${scan} + for line in "$@" ; do + : $(( 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 += 1 )) + done + : $(( 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 += 1 )) + done + : $(( i += 1 )) + done + + local i=0 e= m= s= + + while [ ${i} -le ${APS} ] ; do + eval x=\$MAC_${i} + if [ -z "${x}" ] ; then + : $(( 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 += 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 += 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= mode= mac= caps= freq= chan= + for ssid; do + i=0 + 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 += 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 += 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 += 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=-1 + 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 +} diff --git a/net/iwconfig.sh.Linux.in b/net/iwconfig.sh.Linux.in new file mode 100644 index 0000000..293bc1f --- /dev/null +++ b/net/iwconfig.sh.Linux.in @@ -0,0 +1,763 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# 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 -= 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 and a non-zero + # mac address. This is mainly for buggy + # prism54 drivers that always set their + # carrier on or buggy madwifi drivers that + # sometimes have carrier on and ssid set + # without being associated. :/ + [ -n "$(iwgetid --raw "${IFACE}")" ] && [ "$(iwgetid --ap --raw "${IFACE}")" != "00:00:00:00:00:00" ] && 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 += 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}\"" + return 1 + fi + if [ "${wep_required}" = "off" -a "${key}" != "off" ]; then + key="off" + ewarn "\"${SSID}\" is not WEP enabled" + 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 += 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 += 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 MODE CHAN QUALITY ENC; do + eval t=\$${x}_${i} + eval ${x}_${i}=\$${x}_${k} + eval ${x}_${k}=\$t + done + fi + : $(( k += 1 )) + done + : $(( 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} MODE_${u} CHAN_${u} QUALITY_${u} ENC_${u} + fi + : $(( k += 1 )) + done + : $(( i += 1 )) + done + + local i=0 e= m= s= + + while [ ${i} -le ${APS} ]; do + eval x=\$MAC_${i} + if [ -z "${x}" ]; then + : $(( 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} ${MODE}_${i} CHAN_${i} QUALITY_${i} ENC_${i} + fi + done + : $(( 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 += 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() +{ + 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 += 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 += 1 )) + done + + return 1 +} + +iwconfig_defaults() +{ + # Turn on the radio + iwconfig "${IFACE}" txpower on 2>/dev/null + + # 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=-1 + 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 problem 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 +} diff --git a/net/macchanger.sh b/net/macchanger.sh new file mode 100644 index 0000000..4e535ec --- /dev/null +++ b/net/macchanger.sh @@ -0,0 +1,92 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +macchanger_depend() +{ + before macnet + # no program 'macchanger', as we have partial functionality without it +} + +_config_vars="$_config_vars mac" + +macchanger_pre_start() +{ + # We don't change MAC addresses from background + yesno ${IN_BACKGROUND} && return 0 + + local mac= opts= + + eval mac=\$mac_${IFVAR} + [ -z "${mac}" ] && return 0 + + _exists true || return 1 + + ebegin "Changing MAC address of ${IFACE}" + + # The interface needs to be up for macchanger to work most of the time + _down + + mac=$(echo "${mac}" | tr '[:upper:]' '[:lower:]') + local hex="[0-9a-f][0-9a-f]" + case "${mac}" in + # specific mac-addr + ${hex}:${hex}:${hex}:${hex}:${hex}:${hex}) + # We don't need macchanger to change to a specific + # mac address + _set_mac_address "${mac}" + if eend "$?"; then + mac=$(_get_mac_address) + eindent + einfo "changed to ${mac}" + eoutdent + _up + return 0 + fi + ;; + + # increment MAC address, default macchanger behavior + increment) opts="${opts}";; + + # randomize just the ending bytes + random-ending) opts="${opts} -e";; + + # keep the same kind of physical layer (eg fibre, copper) + random-samekind) opts="${opts} -a";; + + # randomize to any known vendor of any physical layer type + random-anykind) opts="${opts} -A";; + + # fully random bytes + random-full|random) opts="${opts} -r";; + + # default case is just to pass on all the options + *) opts="${opts} ${mac}";; + esac + + if [ ! -x /sbin/macchanger ]; then + eerror "For changing MAC addresses, emerge net-analyzer/macchanger" + return 1 + fi + + mac=$(/sbin/macchanger ${opts} "${IFACE}" \ + | sed -n -e 's/^Faked MAC:.*\<\(..:..:..:..:..:..\)\>.*/\U\1/p' ) + _up + + # Sometimes the interface needs to be up .... + if [ -z "${mac}" ]; then + mac=$(/sbin/macchanger ${opts} "${IFACE}" \ + | sed -n -e 's/^Faked MAC:.*\<\(..:..:..:..:..:..\)\>.*/\U\1/p' ) + fi + + if [ -z "${mac}" ]; then + eend 1 "Failed to set MAC address" + return 1 + fi + + eend 0 + eindent + einfo "changed to" "${mac}" + eoutdent + + return 0 +} diff --git a/net/macnet.sh b/net/macnet.sh new file mode 100644 index 0000000..1ec2ad7 --- /dev/null +++ b/net/macnet.sh @@ -0,0 +1,19 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +macnet_depend() +{ + before rename interface wireless + after macchanger +} + +macnet_pre_start() +{ + local mac=$(_get_mac_address 2>/dev/null) + [ -z "${mac}" ] && return 0 + + vebegin "Configuring ${IFACE} for MAC address ${mac}" + mac=$(echo "${mac}" | sed -e 's/://g') + _configure_variables "${mac}" + veend 0 +} diff --git a/net/macvlan.sh b/net/macvlan.sh new file mode 100644 index 0000000..1b95f5c --- /dev/null +++ b/net/macvlan.sh @@ -0,0 +1,53 @@ +# 2011-09-22 Stef Simoens <stef@bgs.org> +# based on vlan.sh & tuntap.sh +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# All rights reserved. Released under the 2-clause BSD license. + +macvlan_depend() +{ + program ip + after interface + before dhcp macchanger +} + +_is_macvlan() +{ + [ -n "$(RC_SVCNAME="net.${IFACE}"; export RC_SVCNAME ; service_get_value macvlan)" ] +} + +macvlan_pre_start() +{ + # MAC-VLAN needs an existing interface to link to + local macvlan= + eval macvlan=\$macvlan_${IFVAR} + [ -z "${macvlan}" ] && return 0 + + case " ${MODULES} " in + *" ifconfig "*) + eerror "sys-apps/iproute2 is required to configure MACVLANs" + return 1 ;; + esac + + # optional mode, default to "private" + local mode= + eval mode=\$mode_${IFVAR} + [ -z "${mode}" ] && mode="private" + + ebegin "Creating MAC-VLAN ${IFACE} to ${macvlan}" + e="$(ip link add link "${macvlan}" name "${IFACE}" type macvlan mode "${mode}" 2>&1 1>/dev/null)" + if [ -n "${e}" ]; then + eend 1 "${e}" + else + eend 0 && _up && service_set_value macvlan "${macvlan}" + fi +} + + +macvlan_post_stop() +{ + _is_macvlan || return 0 + + ebegin "Removing MAC-VLAN ${IFACE}" + ip link delete "${IFACE}" type macvlan >/dev/null + eend $? +} diff --git a/net/netplugd.sh b/net/netplugd.sh new file mode 100644 index 0000000..f43a5e6 --- /dev/null +++ b/net/netplugd.sh @@ -0,0 +1,96 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# 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 + + # IFACE-specific, then global, then default + eval timeout=\$plug_timeout_${IFVAR} + [ -z "${timeout}" ] && timeout=$plug_timeout + [ -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 += 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 $? +} diff --git a/net/pppd.sh b/net/pppd.sh new file mode 100644 index 0000000..97bd3ee --- /dev/null +++ b/net/pppd.sh @@ -0,0 +1,243 @@ +# Copyright (c) 2005-2007 Gentoo Foundation +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# 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="plugin passwordfd.so ${opts} 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="plugin $1.so ${opts}" + 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 $? +} diff --git a/net/pump.sh b/net/pump.sh new file mode 100644 index 0000000..ddd454c --- /dev/null +++ b/net/pump.sh @@ -0,0 +1,59 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# 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" +} diff --git a/net/ssidnet.sh b/net/ssidnet.sh new file mode 100644 index 0000000..b0eed56 --- /dev/null +++ b/net/ssidnet.sh @@ -0,0 +1,24 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +ssidnet_depend() +{ + before interface system + after wireless +} + +ssidnet_pre_start() +{ + [ -z "${SSID}" -a -z "${SSIDVAR}" ] && return 0 + + local mac=$(_get_ap_mac_address | sed -e 's/://g') x= + + vebegin "Configuring ${IFACE} for SSID ${SSID}" + _configure_variables "${mac}" "${SSIDVAR}" + + # Backwards compat for old gateway var + eval x=\$gateway_${SSIDVAR} + [ -n "${x}" ] && gateway=${x} + + veend 0 +} diff --git a/net/system.sh b/net/system.sh new file mode 100644 index 0000000..98017d5 --- /dev/null +++ b/net/system.sh @@ -0,0 +1,122 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +_config_vars="$_config_vars dns_servers dns_domain dns_search" +_config_vars="$_config_vars dns_sortlist dns_options" +_config_vars="$_config_vars ntp_servers nis_servers nis_domain" + +system_depend() +{ + after interface + before dhcp +} + +_system_dns() +{ + local servers= domain= search= sortlist= options= x= imetric= + + eval servers=\$dns_servers_${IFVAR} + [ -z "${servers}" ] && servers=${dns_servers} + + eval domain=\$dns_domain_${IFVAR} + [ -z "${domain}" ] && domain=${dns_domain} + + eval search=\$dns_search_${IFVAR} + [ -z "${search}" ] && search=${dns_search} + + eval sortlist=\$dns_sortlist_${IFVAR} + [ -z "${sortlist}" ] && sortlist=${dns_sortlist} + + eval options=\$dns_options_${IFVAR} + [ -z "${options}" ] && options=${dns_options} + + [ -z "${servers}" -a -z "${domain}" -a -z "${search}" \ + -a -z "${sortlist}" -a -z "${options}" ] && return 0 + + local buffer="# Generated by net-scripts for interface ${IFACE}\n" + [ -n "${domain}" ] && buffer="${buffer}domain ${domain}\n" + [ -n "${search}" ] && buffer="${buffer}search ${search}\n" + + for x in ${servers}; do + buffer="${buffer}nameserver ${x}\n" + done + + [ -n "${sortlist}" ] && buffer="${buffer}sortlist ${sortlist}\n" + [ -n "${options}" ] && buffer="${buffer}options ${options}\n" + + # Support resolvconf if we have it. + if [ -x /sbin/resolvconf ]; then + x="-a ${IFACE}" + eval imetric=\${metric_${IFVAR}} + if [ -n "${imetric}" ]; then + x="${x} -m ${imetric}" + fi + printf "${buffer}" | resolvconf ${x} + else + printf "${buffer}" > /etc/resolv.conf + chmod 644 /etc/resolv.conf + fi +} + +_system_ntp() +{ + local servers= buffer= x= + + eval servers=\$ntp_servers_${IFVAR} + [ -z "${servers}" ] && servers=${ntp_servers} + [ -z "${servers}" ] && return 0 + + buffer="# Generated by net-scripts for interface ${IFACE}\n" + buffer="${buffer}restrict default noquery notrust nomodify\n" + buffer="${buffer}restrict 127.0.0.1\n" + + for x in ${servers}; do + buffer="${buffer}restrict ${x} nomodify notrap noquery\n" + buffer="${buffer}server ${x}\n" + done + + printf "${buffer}" > /etc/ntp.conf + chmod 644 /etc/ntp.conf +} + +_system_nis() +{ + local servers= domain= x= buffer= + + eval servers=\$nis_servers_${IFVAR} + [ -z "${servers}" ] && servers=${nis_servers} + + eval domain=\$nis_domain_${IFVAR} + [ -z "${domain}" ] && domain=${nis_domain} + + [ -z "${servers}" -a -z "${domain}" ] && return 0 + + buffer="# Generated by net-scripts for interface ${iface}\n" + + if [ -n "${domain}" ]; then + hostname -y "${domain}" + if [ -n "${servers}" ]; then + for x in ${servers}; do + buffer="${buffer}domain ${domain} server ${x}\n" + done + else + buffer="${buffer}domain ${domain} broadcast\n" + fi + else + for x in ${servers}; do + buffer="${buffer}ypserver ${x}\n" + done + fi + + printf "${buffer}" > /etc/yp.conf + chmod 644 /etc/yp.conf +} + +system_pre_start() +{ + _system_dns + _system_ntp + _system_nis + + return 0 +} diff --git a/net/tuntap.sh b/net/tuntap.sh new file mode 100644 index 0000000..331fd83 --- /dev/null +++ b/net/tuntap.sh @@ -0,0 +1,102 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +tuntap_depend() +{ + before bridge interface macchanger + program ip openvpn tunctl +} + +_config_vars="$_config_vars iproute2 openvpn tunctl" + +_is_tuntap() +{ + [ -n "$(RC_SVCNAME="net.${IFACE}"; export RC_SVCNAME ; service_get_value tuntap)" ] +} + +tuntap_pre_start() +{ + local tuntap= + local rc= + 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 its time to appear + local timeout=10 + while [ ! -e /dev/net/tun -a ${timeout} -gt 0 ]; do + sleep 1 + : $(( 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 i_opts= o_opts= t_opts= + local do_iproute2=false do_openvpn=false do_tunctl=false + eval i_opts=\$iproute2_${IFVAR} + eval o_opts=\$openvpn_${IFVAR} + eval t_opts=\$tunctl_${IFVAR} + + if [ -n "${i_opts}" ] && type ip >/dev/null 2>&1; then + do_iproute2=true + elif [ -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 ip >/dev/null 2>&1; then + do_iproute2=true + elif type openvpn >/dev/null 2>&1; then + do_openvpn=true + elif type tunctl >/dev/null 2>&1; then + do_tunctl=true + fi + + if ${do_iproute2}; then + ip tuntap add dev "${IFACE}" mode "${tuntap}" ${i_opts} + rc=$? + elif ${do_openvpn}; then + openvpn --mktun --dev-type "${tuntap}" --dev "${IFACE}" \ + ${o_opts} >/dev/null + rc=$? + elif ${do_tunctl}; then + tunctl ${t_opts} -t "${IFACE}" >/dev/null + rc=$? + else + eerror "Neither iproute2, openvpn nor tunctl has been found, please install" + eerror "either \"iproute2\" \"openvpn\" or \"usermode-utilities\"." + rc=1 + fi + eend $rc && _up && service_set_value tuntap "${tuntap}" +} + +tuntap_post_stop() +{ + _is_tuntap || return 0 + + ebegin "Destroying Tun/Tap interface ${IFACE}" + if type ip > /dev/null 2>&1; then + ip tuntap del dev ${IFACE} mode $(service_get_value tuntap) + elif 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 $? +} diff --git a/net/udhcpc.sh.Linux.in b/net/udhcpc.sh.Linux.in new file mode 100644 index 0000000..bb502e5 --- /dev/null +++ b/net/udhcpc.sh.Linux.in @@ -0,0 +1,110 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +udhcpc_depend() +{ + program start /bin/busybox + after interface + provide dhcp +} + +_config_vars="$_config_vars dhcp 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} + + # This omits the Gentoo specific patch to busybox, + # but it creates temporary files. + # We can move this stuff to udhcpc hook script to avoid that, should we do? + local conf="/var/run/udhcpc-${IFACE}.conf" + echo -n >"$conf" + # Map some generic options to dhcpcd + for opt in ${opts}; do + case "${opt}" in + nodns) echo "PEER_DNS=no" >>"$conf" ;; + nontp) echo "PEER_NTP=no" >>"$conf" ;; + nogateway) echo "PEER_ROUTERS=no" >>"$conf" ;; + nosendhost) sendhost=false; + esac + done + + [ "${metric:-0}" != "0" ] && echo "IF_METRIC=${metric}" >>"$conf" + + 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="/bin/busybox udhcpc";; + *) x="start-stop-daemon --start --exec /bin/busybox \ + --pidfile \"${pidfile}\" -- udhcpc";; + esac + + case " ${args} " in + *" --hostname="*|*" -h "*|*" -H "*);; + *) + if ${sendhost}; then + local hname="$(hostname)" + if [ "${hname}" != "(none)" ] && [ "${hname}" != "localhost" ]; then + args="${args} -x hostname:'${hname}'" + fi + fi + ;; + esac + + eval "${x}" "${args}" --interface="${IFACE}" --now \ + --script="@LIBEXECDIR@/sh/udhcpc-hook.sh" \ + --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 --signal USR2 \ + --exec /bin/busybox --pidfile "${pidfile}" + if [ -f /var/cache/udhcpc-"${IFACE}".lease ]; then + rm -f /var/cache/udhcpc-"${IFACE}".lease + fi + ;; + esac + + start-stop-daemon --stop --exec /bin/busybox --pidfile "${pidfile}" + eend $? + + if [ -e "/var/run/udhcpc-${IFACE}.conf" ]; then + rm -f "/var/run/udhcpc-${IFACE}.conf" + fi +} diff --git a/net/vlan.sh b/net/vlan.sh new file mode 100644 index 0000000..10040af --- /dev/null +++ b/net/vlan.sh @@ -0,0 +1,146 @@ +# Copyright (c) 2007-2008 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +vlan_depend() +{ + program ip + 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/^\W*\([^ ]*\) \(.* \) .*'"${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 vconfig + eval vconfig=\$vconfig_${IFVAR} + if [ -n "${vconfig}" ]; then + eerror "You must convert your vconfig_ VLAN entries to vlan${N} entries." + return 1 + fi + local vlans= + eval vlans=\$vlans_${IFVAR} + [ -z "$vlans" ] && return 0 + case " ${MODULES} " in + *" ifconfig "*) + eerror "sys-apps/iproute2 is required to configure VLANs" + return 1 ;; + esac +} + +vlan_post_start() +{ + local vlans= + eval vlans=\$vlans_${IFVAR} + [ -z "${vlans}" ] && return 0 + + _check_vlan || return 1 + _exists || return 1 + + local vlan= e= s= vname= vflags= vingress= vegress= + for vlan in ${vlans}; do + einfo "Adding VLAN ${vlan} to ${IFACE}" + # We need to gather all interface configuration options + # 1) naming. Default to the standard "${IFACE}.${vlan}" but it can be anything + eval vname=\$${IFACE}_vlan${vlan}_name + [ -z "${vname}" ] && eval vname=\$vlan${vlan}_name + [ -z "${vname}" ] && vname="${IFACE}.${vlan}" + # 2) flags + eval vflags=\$${IFACE}_vlan${vlan}_flags + [ -z "${vflags}" ] && eval vflags=\$vlan${vlan}_flags + # 3) ingress/egress map + eval vingress=\$${IFACE}_vlan${vlan}_ingress + [ -z "${vingress}" ] && eval vingress=\$vlan${vlan}_ingress + [ -z "${vingress}" ] || vingress="ingress-qos-map ${vingress}" + eval vegress=\$${IFACE}_vlan${vlan}_egress + [ -z "${vegress}" ] && eval vegress=\$vlan${vlan}_egress + [ -z "${vegress}" ] || vegress="egress-qos-map ${vegress}" + + # txqueue + local txqueuelen= + eval txqueuelen=\$txqueuelen_${IFACE}_vlan${vlan} + [ -z "${txqueuelen}" ] && eval txqueuelen=\$txqueuelen_vlan${vlan} + # mac + local mac= + eval mac=\$mac_${IFACE}_vlan${vlan} + [ -z "${mac}" ] && eval mac=\$mac_vlan${vlan} + # broadcast + local broadcast= + eval broadcast=\$broadcast_${IFACE}_vlan${vlan} + [ -z "${broadcast}" ] && eval broadcast=\$broadcast_vlan${vlan} + # mtu + local mtu= + eval mtu=\$mtu_${IFACE}_vlan${vlan} + [ -z "${mtu}" ] && eval mtu=\$mtu_vlan${vlan} + + # combine it all + local opts="${txqueuelen:+txqueuelen} ${txqueuelen} ${mac:+address} ${mac} ${broadcast:+broadcast} ${broadcast} ${mtu:+mtu} ${mtu}" + + veinfo "ip link add link \"${IFACE}\" name \"${vname}\" ${opts} type vlan id \"${vlan}\" ${vflags} ${vingress} ${vegress}" + e="$(ip link add link "${IFACE}" name "${vname}" ${opts} type vlan id "${vlan}" ${vflags} ${vingress} ${vegress} 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}" + ( + RC_SVCNAME="net.${ifname}" ; export RC_SVCNAME + start + ) || mark_service_stopped "net.${ifname}" + done + + return 0 +} + +vlan_pre_stop() +{ + local vlan= + + _exists || return 0 + + for vlan in $(_get_vlans); do + einfo "Removing VLAN ${vlan##*.} from ${IFACE}" + ( + RC_SVCNAME="net.${vlan}" ; export RC_SVCNAME + stop + ) && { + mark_service_stopped "net.${vlan}" + ip link delete "${vlan}" type vlan >/dev/null + } + done + + return 0 +} diff --git a/net/wpa_supplicant.sh b/net/wpa_supplicant.sh new file mode 100644 index 0000000..53b0256 --- /dev/null +++ b/net/wpa_supplicant.sh @@ -0,0 +1,212 @@ +# Copyright (c) 2007-2009 Roy Marples <roy@marples.name> +# Released under the 2-clause BSD license. + +wpa_supplicant_depend() +{ + wpas=/usr/sbin/wpa_supplicant + [ -x ${wpas} ] || wpas=/sbin/wpa_supplicant + if [ -x ${wpas} ]; then + program start ${wpas} + # bug 345281: if wpa_supplicant is built w/ USE=dbus, we need to start + # dbus before we can start wpa_supplicant. + ${wpas} -h |grep DBus -sq + [ $? -eq 0 ] && need dbus + fi + after macnet plug + before interface + provide wireless + + # Prefer us over iwconfig + after iwconfig +} + +# Only set these functions if not set already +# IE, prefer to use iwconfig +if ! type _get_ssid >/dev/null 2>&1; then +_get_ssid() +{ + local timeout=5 ssid= + + while [ ${timeout} -gt 0 ]; do + ssid=$(wpa_cli -i"${IFACE}" status | sed -n -e 's/^ssid=//p') + if [ -n "${ssid}" ]; then + echo "${ssid}" + return 0 + fi + sleep 1 + : $(( timeout -= 1 )) + done + + return 1 +} + +_get_ap_mac_address() +{ + wpa_cli -i"${IFACE}" status | sed -n -e 's/^bssid=\(.*\)$/\1/p' \ + | tr '[:lower:]' '[:upper:]' +} +fi + +wpa_supplicant_pre_start() +{ + local opts= cliopts= cfgfile= ctrl_dir= wireless=true + local wpas=/usr/sbin/wpa_supplicant wpac=/usr/bin/wpa_cli + local actfile=/etc/wpa_supplicant/wpa_cli.sh + + if [ ! -x "${wpas}" ]; then + wpas=/sbin/wpa_supplicant + wpac=/bin/wpa_cli + fi + [ "${RC_UNAME}" = "Linux" ] || unset wpac + [ -e "${actfile}" ] || unset wpac + + eval opts=\$wpa_supplicant_${IFVAR} + eval cliopts=\$wpa_cli_${IFVAR} + [ -z "${cliopts}" ] && cliopts=${wpa_cli} + case " ${opts} " in + *" -Dwired "*) wireless=false;; + *) _is_wireless || return 0;; + esac + + # We don't configure wireless if we're being called from + # the background unless we're not currently running + if yesno ${IN_BACKGROUND}; then + if ${wireless} && \ + service_started_daemon "${RC_SVCNAME}" "${wpas}"; then + SSID=$(_get_ssid "${IFACE}") + SSIDVAR=$(shell_var "${SSID}") + service_set_value "SSID" "${SSID}" + metric=2000 + fi + return 0 + fi + + service_set_value "SSID" "" + ebegin "Starting wpa_supplicant on ${IFVAR}" + + if type iwconfig_defaults >/dev/null 2>&1; then + iwconfig_defaults + iwconfig_user_config + fi + + cfgfile=${opts##* -c} + if [ -n "${cfgfile}" -a "${cfgfile}" != "${opts}" ]; then + case "${cfgfile}" in + " "*) cfgfile=${cfgfile# *};; + esac + cfgfile=${cfgfile%% *} + else + # Support new and old style locations + cfgfile="/etc/wpa_supplicant/wpa_supplicant-${IFACE}.conf" + [ ! -e "${cfgfile}" ] \ + && cfgfile="/etc/wpa_supplicant/wpa_supplicant.conf" + [ ! -e ${cfgfile} ] \ + && cfgfile="/etc/wpa_supplicant.conf" + opts="${opts} -c ${cfgfile}" + fi + + if [ ! -f ${cfgfile} ]; then + eend 1 "/etc/wpa_supplicant/wpa_supplicant.conf not found" + return 1 + fi + + # Work out where the ctrl_interface dir is if it's not specified + local ctrl_dir=$(sed -e 's/^ *//' \ + -e '/^ctrl_interface=/!d' \ + -e 's/^ctrl_interface=//' \ + -e 's/^ *//' \ + -e 's/^DIR=//' \ + -e 's/^ *//' \ + -e 's/GROUP=.*//' \ + -e 's/ *$//' \ + "${cfgfile}") + if [ -z "${ctrl_dir}" ]; then + ctrl_dir=${opts##* -C} + if [ -n "${ctrl_dir}" -a "${ctrl_dir}" != "${opts}" ]; then + case "${ctrl_dir}" in + " "*) ctrl_dir=${ctrl_dir# *};; + esac + ctrl_dir=${ctrl_dir%% *} + else + ctrl_dir="/var/run/wpa_supplicant" + opts="${opts} -C ${ctrl_dir}" + fi + fi + service_set_value ctrl_dir "${ctrl_dir}" + + if [ -n "${wpac}" ]; then + opts="${opts} -W" + elif service_started devd; then + mark_service_inactive + fi + start-stop-daemon --start --exec "${wpas}" \ + --pidfile "/var/run/wpa_supplicant-${IFACE}.pid" \ + -- ${opts} -B -i "${IFACE}" \ + -P "/var/run/wpa_supplicant-${IFACE}.pid" + eend $? || return 1 + + # If we don't have a working wpa_cli and action file continue + if [ -z "${wpac}" ]; then + if service_started devd; then + ebegin "Backgrounding ..." + exit 1 + fi + return 0 + fi + + # Starting wpa_supplication-0.4.0, we can get wpa_cli to + # start/stop our scripts from wpa_supplicant messages + local inact=false + service_inactive && inact=true + mark_service_inactive + + ebegin "Starting wpa_cli on" "${IFACE}" + start-stop-daemon --start --exec "${wpac}" \ + --pidfile "/var/run/wpa_cli-${IFACE}.pid" \ + -- ${cliopts} -a "${actfile}" -p "${ctrl_dir}" -i "${IFACE}" \ + -P "/var/run/wpa_cli-${IFACE}.pid" -B + if eend $?; then + ebegin "Backgrounding ..." + exit 1 + fi + + # wpa_cli failed to start? OK, error here + start-stop-daemon --quiet --stop --exec "${wpas}" \ + --pidfile "/var/run/wpa_supplicant-${IFACE}.pid" + ${inact} || mark_service_stopped + return 1 +} + +wpa_supplicant_post_stop() +{ + local wpas=/usr/sbin/wpa_supplicant wpac=/usr/bin/wpa_cli + + if [ ! -x "${wpas}" ]; then + wpas=/sbin/wpa_supplicant + wpac=/bin/wpa_cli + fi + + if yesno "${IN_BACKGROUND}"; then + # Only stop wpa_supplicant if it's not the controlling daemon + ! service_started_daemon "${RC_SVCNAME}" "${wpas}" 1 + fi + [ $? != 0 ] && return 0 + + local pidfile="/var/run/wpa_cli-${IFACE}.pid" + if [ -f ${pidfile} ]; then + ebegin "Stopping wpa_cli on ${IFACE}" + start-stop-daemon --stop --exec "${wpac}" --pidfile "${pidfile}" + eend $? + fi + + pidfile="/var/run/wpa_supplicant-${IFACE}.pid" + if [ -f ${pidfile} ]; then + ebegin "Stopping wpa_supplicant on ${IFACE}" + start-stop-daemon --stop --exec "${wpas}" --pidfile "${pidfile}" + eend $? + fi + + # If wpa_supplicant exits uncleanly, we need to remove the stale dir + [ -S "/var/run/wpa_supplicant/${IFACE}" ] \ + && rm -f "/var/run/wpa_supplicant/${IFACE}" +} |