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