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