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