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