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