add hotplug stuff to trunk/
[openwrt.git] / openwrt / package / base-files / default / etc / functions.sh
1 #!/bin/ash
2 . /etc/nvram.sh
3
4 alias debug=${DEBUG:-:}
5
6 # valid interface?
7 if_valid () (
8 ifconfig "$1" >&- 2>&- ||
9 [ "${1%%[0-9]}" = "br" ] ||
10 {
11 [ "${1%%[0-9]}" = "vlan" ] && (
12 i=${1#vlan}
13 hwname=$(nvram get vlan${i}hwname)
14 hwaddr=$(nvram get ${hwname}macaddr)
15 [ -z "$hwaddr" ] && return 1
16
17 vif=$(ifconfig -a | awk '/^eth.*'$hwaddr'/ {print $1; exit}' IGNORECASE=1)
18 debug "# vlan$i => $vif"
19
20 $DEBUG ifconfig $vif up
21 $DEBUG vconfig add $vif $i 2>&-
22 )
23 } ||
24 { debug "# missing interface '$1' ignored"; false; }
25 )
26
27 do_ifup() {
28 if_proto=$(nvram get ${2}_proto)
29 if=$(nvram get ${2}_ifname)
30 [ "${if%%[0-9]}" = "ppp" ] && if=$(nvram get ${if_proto}_ifname)
31
32 pidfile=/var/run/${if}.pid
33 [ -f $pidfile ] && $DEBUG kill $(cat $pidfile)
34
35 case "$1" in
36 static)
37 ip=$(nvram get ${2}_ipaddr)
38 netmask=$(nvram get ${2}_netmask)
39 gateway=$(nvram get ${2}_gateway)
40
41 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} broadcast + up
42 ${gateway:+$DEBUG route add default gw $gateway}
43
44 [ -f /etc/resolv.conf ] || {
45 debug "# --- creating /etc/resolv.conf ---"
46 for dns in $(nvram get ${2}_dns); do
47 echo "nameserver $dns" >> /etc/resolv.conf
48 done
49 }
50
51 env -i ACTION="ifup" INTERFACE="${2}" PROTO=static /sbin/hotplug "iface" &
52 ;;
53 dhcp)
54 DHCP_IP=$(nvram get ${2}_ipaddr)
55 DHCP_NETMASK=$(nvram get ${2}_netmask)
56 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} broadcast + up
57
58 DHCP_ARGS="-i $if ${DHCP_IP:+-r $DHCP_IP} -b -p $pidfile"
59 DHCP_HOSTNAME=$(nvram get ${2}_hostname)
60 DHCP_HOSTNAME=${DHCP_HOSTNAME%%.*}
61 [ -z $DHCP_HOSTNAME ] || DHCP_ARGS="$DHCP_ARGS -H $DHCP_HOSTNAME"
62 [ "$if_proto" = "pptp" ] && DHCP_ARGS="$DHCP_ARGS -n -q" || DHCP_ARGS="$DHCP_ARGS -R &"
63 oldpid=$(cat $pidfile)
64 ${DEBUG:-eval} "udhcpc $DHCP_ARGS"
65 pidof udhcpc | grep "$oldpid" >&- 2>&- && {
66 sleep 1
67 kill -9 $oldpid
68 }
69 # hotplug events are handled by /usr/share/udhcpc/default.script
70 ;;
71 none|"")
72 ;;
73 *)
74 [ -x "/sbin/ifup.$1" ] && { $DEBUG /sbin/ifup.$1 ${2}; exit; }
75 echo "### ifup ${2}: ignored ${2}_proto=\"$1\" (not supported)"
76 ;;
77 esac
78 }
79
80 bitcount () {
81 local c=$1
82 echo $((
83 c=((c>> 1)&0x55555555)+(c&0x55555555),
84 c=((c>> 2)&0x33333333)+(c&0x33333333),
85 c=((c>> 4)&0x0f0f0f0f)+(c&0x0f0f0f0f),
86 c=((c>> 8)&0x00ff00ff)+(c&0x00ff00ff),
87 c=((c>>16)&0x0000ffff)+(c&0x0000ffff)
88 ))
89 }
90
91 valid_netmask () {
92 return $((-($1)&~$1))
93 }
94
95 ip2int () (
96 set $(echo $1 | tr '\.' ' ')
97 echo $(($1<<24|$2<<16|$3<<8|$4))
98 )
99
100 int2ip () {
101 echo $(($1>>24&255)).$(($1>>16&255)).$(($1>>8&255)).$(($1&255))
102 }
This page took 0.055978 seconds and 5 git commands to generate.