diff options
author | Roy Marples <roy@marples.name> | 2007-04-05 11:18:42 +0000 |
---|---|---|
committer | Roy Marples <roy@marples.name> | 2007-04-05 11:18:42 +0000 |
commit | 5af58b45146ab5253ca964738f4e45287bf963d4 (patch) | |
tree | 68d3a9a61fa55dd7fe273db776c375f797edaa5b /net.Linux/iproute2.sh |
Rewrite the core parts in C. We now provide librc so other programs can
query runlevels, services and state without using bash. We also provide
libeinfo so other programs can easily use our informational functions.
As such, we have dropped the requirement of using bash as the init script
shell. We now use /bin/sh and have strived to make the scripts as portable
as possible. Shells that work are bash and dash. busybox works provided
you disable s-s-d. If you have WIPE_TMP set to yes in conf.d/bootmisc you
should disable find too.
zsh and ksh do not work at this time.
Networking support is currently being re-vamped also as it was heavily bash
array based. As such, a new config format is available like so
config_eth0="1.2.3.4/24 5.6.7.8/16"
or like so
config_eth0="'1.2.3.4 netmask 255.255.255.0' '5.6.7.8 netmask 255.255.0.0'"
We will still support the old bash array format provided that /bin/sh IS
a link it bash.
ChangeLog for baselayout-1 can be found in our SVN repo.
Diffstat (limited to 'net.Linux/iproute2.sh')
-rw-r--r-- | net.Linux/iproute2.sh | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/net.Linux/iproute2.sh b/net.Linux/iproute2.sh new file mode 100644 index 00000000..1b1b70fa --- /dev/null +++ b/net.Linux/iproute2.sh @@ -0,0 +1,185 @@ +# Copyright 2004-2007 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +iproute2_depend() { + program /sbin/ip + provide interface + after ifconfig +} + +_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 +} + +_up() { + ip link set up dev "${IFACE}" +} + +_down() { + ip link set down dev "${IFACE}" +} + +_exists() { + grep -Eq "^[[:space:]]*${IFACE}:[[:space:]]*" /proc/net/dev +} + +_ifindex() { + local line= i=-2 + while read line ; do + i=$((${i} + 1)) + [ ${i} -lt 1 ] && continue + case "${line}" in + "${IFACE}: "*) echo "${i}"; return 0;; + esac + done < /proc/net/dev + return 1 +} + +_is_wireless() { + # Support new sysfs layout + [ -d /sys/class/net/"${IFACE}"/wireless ] && return 0 + + [ ! -e /proc/net/wireless ] && return 1 + grep -Eq "^[[:space:]]*${IFACE}:[[:space:]]+" /proc/net/wireless +} + +_get_inet_addresses() { + LC_ALL=C ip -family inet addr show "${IFACE}" | \ + sed -n -e 's/.*inet \([^ ]*\).*/\1/p' +} + +_get_inet_address() { + set -- $(_get_inet_addresses) + [ $# = "0" ] && return 1 + echo "$1" +} + +_add_address() { + if [ "$1" = "127.0.0.1/8" -a "${IFACE}" = "lo" ] ; then + ip addr add "$@" dev "${IFACE}" 2>/dev/null + return 0 + fi + + # Convert an ifconfig line to iproute2 + if [ "$2" = "netmask" ] ; then + local one="$1" three="$3" + shift ; shift ; shift + set -- "${one}/$(_netmask2cidr "${three}")" "$@" + fi + + #config=( "${config[@]//pointopoint/peer}" ) + + # Always scope lo addresses as host unless specified otherwise + if [ "${IFACE}" = "lo" ] ; then + set -- "$@" "scope" "host" + fi + + # IPv4 specifics + case "$1" in + *.*.*.*) + case "$@" in + *" brd "*) ;; + *" broadcast "*) ;; + *) set -- "$@" brd + ;; + esac + ;; + esac + + ip addr add dev "${IFACE}" "$@" +} + +_add_route() { + if [ $# -eq 3 ] ; then + set -- "$1" "$2" via "$3" + elif [ "$3" = "gw" ] ; then + local one=$1 two=$2 + shift ; shift; shift + set -- "${one}" "${two}" gw "$@" + 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 ;; + -net) ;; + -A) [ "$2" = "inet6" ] && shift ;; + -host) cmd="${cmd} scope host" ;; + *) cmd="${cmd} $1" ;; + esac + shift + done + + if ! ${have_metric} && [ -n "${metric}" ] ; then + cmd="${cmd} metric ${metric}" + fi + + ip route append ${cmd} dev "${IFACE}" + eend $? +} + +_delete_addresses() { + ip addr flush dev "${IFACE}" scope global 2>/dev/null + ip addr flush dev "${IFACE}" scope site 2>/dev/null + if [ "${IFACE}" != "lo" ] ; then + ip addr flush dev "${IFACE}" scope host 2>/dev/null + fi + return 0 +} + +_has_carrier() { + return 0 +} + +_tunnel() { + ip tunnel "$@" +} + +iproute2_pre_start() { + # MTU support + local mtu= + eval mtu=\$mtu_${IFVAR} + [ -n "${mtu}" ] && ip link set mtu "${mtu}" dev "${IFACE}" + + local tunnel= + eval tunnel=\$iptunnel_${IFVAR} + if [ -n "${tunnel}" ] ; then + # Set our base metric to 1000 + metric=1000 + + ebegin "Creating tunnel ${IFVAR}" + ip tunnel add "${tunnel}" + eend $? || return 1 + fi + + return 0 +} + +iproute2_post_start() { + ip route flush cache dev "${IFACE}" +} + +iproute2_post_stop() { + # Don't delete sit0 as it's a special tunnel + if [ "${IFACE}" != "sit0" ] ; then + if [ -n "$(ip tunnel show "${IFACE}" 2>/dev/null)" ] ; then + ebegin "Destroying tunnel ${IFACE}" + ip tunnel del "${IFACE}" + eend $? + fi + fi +} + +# vim: set ts=4 : |