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