Add support for static routes per interface. You can specify one or more routes,...
[openwrt/svn-archive/archive.git] / openwrt / package / base-files / default / etc / functions.sh
1 #!/bin/ash
2
3 alias debug=${DEBUG:-:}
4
5 # valid interface?
6 if_valid () (
7 ifconfig "$1" >&- 2>&- ||
8 [ "${1%%[0-9]}" = "br" ] ||
9 {
10 [ "${1%%[0-9]}" = "vlan" ] && (
11 i=${1#vlan}
12 hwname=$(nvram get vlan${i}hwname)
13 hwaddr=$(nvram get ${hwname}macaddr)
14 [ -z "$hwaddr" ] && return 1
15
16 vif=$(ifconfig -a | awk '/^eth.*'$hwaddr'/ {print $1; exit}' IGNORECASE=1)
17 debug "# vlan$i => $vif"
18
19 $DEBUG ifconfig $vif up
20 $DEBUG vconfig add $vif $i 2>&-
21 )
22 } ||
23 { debug "# missing interface '$1' ignored"; false; }
24 )
25
26 do_ifup() {
27 if_proto=$(nvram get ${2}_proto)
28 if=$(nvram get ${2}_ifname)
29 [ "${if%%[0-9]}" = "ppp" ] && if=$(nvram get ${2}_device)
30
31 pidfile=/var/run/${if}.pid
32 [ -f $pidfile ] && $DEBUG kill $(cat $pidfile)
33
34 case "$1" in
35 static)
36 ip=$(nvram get ${2}_ipaddr)
37 netmask=$(nvram get ${2}_netmask)
38 gateway=$(nvram get ${2}_gateway)
39 mtu=$(nvram get ${2}_mtu)
40 static_route=$(nvram get ${2}_static_route)
41
42 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} ${mtu:+mtu $(($mtu))} broadcast + up
43 ${gateway:+$DEBUG route add default gw $gateway}
44
45 [ -n "$static_route" ] && {
46 for route in $static_route; do
47 if [ "$(echo $route | cut -d \/ -f2)" != "32" ];
48 then
49 route add -net $(echo $route | cut -d \/ -f1) netmask $(echo $route | cut -d \/ -f1) dev $if
50 else
51 route add -host $(echo $route | cut -d \/ -f1) dev $if
52 fi
53 done
54 }
55
56 [ -f /etc/resolv.conf ] || {
57 debug "# --- creating /etc/resolv.conf ---"
58 for dns in $(nvram get ${2}_dns); do
59 echo "nameserver $dns" >> /etc/resolv.conf
60 done
61 }
62
63 env -i ACTION="ifup" INTERFACE="${2}" PROTO=static /sbin/hotplug "iface" &
64 ;;
65 dhcp)
66 DHCP_IP=$(nvram get ${2}_ipaddr)
67 DHCP_NETMASK=$(nvram get ${2}_netmask)
68 mtu=$(nvram get ${2}_mtu)
69 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} ${mtu:+mtu $(($mtu))} broadcast + up
70
71 DHCP_ARGS="-i $if ${DHCP_IP:+-r $DHCP_IP} -b -p $pidfile"
72 DHCP_HOSTNAME=$(nvram get ${2}_hostname)
73 DHCP_HOSTNAME=${DHCP_HOSTNAME%%.*}
74 [ -z $DHCP_HOSTNAME ] || DHCP_ARGS="$DHCP_ARGS -H $DHCP_HOSTNAME"
75 [ "$if_proto" = "pptp" ] && DHCP_ARGS="$DHCP_ARGS -n -q" || DHCP_ARGS="$DHCP_ARGS -R &"
76 [ -r $pidfile ] && oldpid=$(cat $pidfile 2>&-)
77 ${DEBUG:-eval} "udhcpc $DHCP_ARGS"
78 [ -n "$oldpid" ] && pidof udhcpc | grep "$oldpid" >&- 2>&- && {
79 sleep 1
80 kill -9 $oldpid
81 }
82 # hotplug events are handled by /usr/share/udhcpc/default.script
83 ;;
84 none|"")
85 ;;
86 *)
87 [ -x "/sbin/ifup.$1" ] && { $DEBUG /sbin/ifup.$1 ${2}; exit; }
88 echo "### ifup ${2}: ignored ${2}_proto=\"$1\" (not supported)"
89 ;;
90 esac
91 }