large init script cleanup and merge of whiterussian changes, new dnsmasq config handling
[openwrt.git] / package / base-files / default / etc / hotplug.d / net / 10-net
1 #!/bin/sh
2 find_name()
3 {
4 for ifname in lan wan wifi ${ifnames}; do
5 IFTYPE="${ifname}"
6 eval "IFPROTO=\"\${${IFTYPE}_proto}\""
7 eval "IFACE=\"\${${IFTYPE}_ifname}\""
8 case "$IFPROTO" in
9 ""|none);;
10 static|dhcp)
11 [ "${IFACE}" = "$INTERFACE" ] && return 0
12 [ "${IFACE%%[0-9]*}" = "br" ] && {
13 eval "ifs=\"\${${IFTYPE}_ifnames}\""
14 for part in $ifs; do
15 [ "$part" = "$INTERFACE" ] && return 0
16 done
17 }
18 ;;
19 pppoa)
20 [ "$INTERFACE" = "atm0" \
21 -a -x /sbin/ifup.${IFPROTO} ] && return 0
22 ;;
23 *)
24 eval "device=\"\${${IFTYPE}_device}\""
25 [ "$device" = "$INTERFACE" \
26 -a -x /sbin/ifup.${IFPROTO} ] && return 0
27 ;;
28 esac
29 done
30 IFACE=""
31 IFTYPE=""
32 IFPROTO=""
33 return 255
34 }
35
36 do_ifup() {
37 if="$3"
38 eval "if_proto=\"\${${2}_proto}\""
39
40 pidfile=/var/run/${if}.pid
41 [ -f $pidfile ] && $DEBUG kill $(cat $pidfile)
42
43 case "$1" in
44 static)
45 eval "ip=\"\${${2}_ipaddr}\""
46 eval "ip6=\"\${${2}_ip6addr}\""
47 eval "netmask=\"\${${2}_netmask}\""
48 eval "gateway=\"\${${2}_gateway}\""
49 eval "dns=\"\${${2}_dns}\""
50
51 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} broadcast + up
52 [ -n "$ip6" ] && $DEBUG ifconfig $if add $ip6
53
54 ${gateway:+$DEBUG route add default gw $gateway}
55
56 [ -f /tmp/resolv.conf ] || {
57 debug "# --- creating /tmp/resolv.conf ---"
58 for ns in $dns; do
59 echo "nameserver $ns" >> /tmp/resolv.conf
60 done
61 }
62
63 env -i ACTION="ifup" INTERFACE="${2}" PROTO=static /sbin/hotplug "iface" &
64 ;;
65 dhcp)
66 eval "ip=\"\${${2}_ipaddr}\""
67 eval "netmask=\"\${${2}_netmask}\""
68 eval "hostname=\"\${${2}_hostname}\""
69
70 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} broadcast + up
71
72 args="-i $if ${ip:+-r $ip} -b -p $pidfile"
73 hostname="${hostname%%.*}"
74 args="$args${hostname:+ -H $hostname}"
75 [ "$if_proto" = "pptp" ] && args="$args -n -q" || args="$args -R &"
76 ${DEBUG:-eval} "udhcpc $args"
77 # hotplug events are handled by /usr/share/udhcpc/default.script
78 ;;
79 *)
80 if [ -x "/sbin/ifup.$1" ]; then
81 ( $DEBUG . /sbin/ifup.$1 ${2} $3 )
82 fi
83 ;;
84 esac
85 }
86
87 do_register()
88 {
89 [ -z "$IFTYPE" -o -z "$IFPROTO" ] && {
90 find_name || {
91 case "${INTERFACE%%[0-9]*}" in
92 wds)
93 for ifname in lan wifi; do
94 eval "if=\"\${${ifname}_ifname}\""
95 [ -z "$IFPROTO" ] && [ "$if" = "br0" ] && {
96 eval "IFPROTO=\"\${${2}_proto}\""
97 IFTYPE="${tmp}"
98 }
99 done
100 [ -z "$IFPROTO" ] && return 0
101 ;;
102 atm)
103 for tmp in lan wan wifi ${ifnames}; do
104 eval "if_proto=\"\${${2}_proto}\""
105 [ "$if_proto" = "pppoa" ] && {
106 do_ifup "pppoa" "$tmp" "$INTERFACE"
107 return 0
108 }
109 done
110 ;;
111 *)
112 return 0
113 ;;
114 esac
115 }
116 }
117
118 case "${INTERFACE%%[0-9]*}" in
119 ppp|atm);;
120 *)
121 eval "mac=\"\${${IFTYPE}_hwaddr}\""
122 ${mac:+$DEBUG ifconfig $INTERFACE down hw ether $mac}
123 ;;
124 esac
125
126 eval "if=\"\${${IFTYPE}_ifname}\""
127 if [ "${if%%[0-9]}" = "br" ]; then
128 if_valid "$INTERFACE" && {
129 ifconfig "$if" 2>&- >&- || {
130 eval "stp=\"\${${IFTYPE}_stp}\""
131 $DEBUG brctl addbr "$if"
132 $DEBUG brctl setfd "$if" 0
133 $DEBUG brctl stp "$if" "${stp:-0}"
134 }
135
136 if [ "$INTERFACE" != "$if" ]; then
137 $DEBUG ifconfig "$INTERFACE" 0.0.0.0 up
138 $DEBUG brctl addif "$if" "$INTERFACE"
139 $DEBUG ifconfig "$INTERFACE" allmulti
140 else
141 do_ifup "$IFPROTO" "$IFTYPE" "$if"
142 fi
143 }
144 else
145 [ "${INTERFACE%%[0-9]*}" = "ppp" ] || do_ifup "$IFPROTO" "$IFTYPE" "$if"
146 fi
147 }
148
149 do_unregister() {
150 [ -z "$IFTYPE" -o -z "$IFPROTO" ] && {
151 find_name || return 0
152 }
153
154 [ "${IFACE%%[0-9]*}" = "br" ] && {
155 if [ "$INTERFACE" != "$IFACE" ]; then
156 brctl delif "$IFACE" "$INTERFACE" 2>&- >&-
157 else
158 brctl delbr "$IFACE" 2>&- >&-
159 fi
160 }
161
162 case "$IFPROTO" in
163 pppoe|pppoa|pptp)
164 killall pppd 2>&- >&-
165 ;;
166 dhcp)
167 [ -f /var/run/${INTERFACE}.pid ] && kill "$(cat /var/run/${INTERFACE}.pid)" 2>&- >&-
168 ;;
169 esac
170
171 [ "${INTERFACE%%[0-9]*}" = "atm" ] || ifconfig "$INTERFACE" 0.0.0.0 down 2>&-
172 }
173
174 case "$ACTION" in
175 add|register) do_register;;
176 remove|unregister) do_unregister;;
177 esac
This page took 0.057549 seconds and 5 git commands to generate.