4579a84704bf6d4166f0d35020ce647a46604efa
[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" ] && 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 ${DEBUG:-eval} "udhcpc $DHCP_ARGS"
79 # hotplug events are handled by /usr/share/udhcpc/default.script
80 ;;
81 *)
82 if [ -x "/sbin/ifup.$1" ]; then
83 ( $DEBUG . /sbin/ifup.$1 ${2} $3 )
84 fi
85 ;;
86 esac
87 }
88
89 do_register()
90 {
91 case "${INTERFACE%%[0-9]*}" in
92 eth) setup_eth;;
93 esac
94
95 [ -z "$IFTYPE" -o -z "$IFPROTO" ] && {
96 find_name || {
97 case "${INTERFACE%%[0-9]*}" in
98 wds)
99 for tmp in lan wifi; do
100 [ -z "$IFPROTO" ] && [ "$(nvram get ${tmp}_ifname)" = "br0" ] && {
101 IFPROTO="$(nvram get ${tmp}_proto)"
102 IFTYPE="${tmp}"
103 }
104 done
105 [ -z "$IFPROTO" ] && return 0
106 ;;
107 atm)
108 for tmp in lan wan wifi $(nvram get ifnames); do
109 [ "$(nvram get ${tmp}_proto)" = "pppoa" ] && {
110 do_ifup "pppoa" "$tmp" "$INTERFACE"
111 return 0
112 }
113 done
114 ;;
115 *)
116 return 0
117 ;;
118 esac
119 }
120 }
121
122 case "${INTERFACE%%[0-9]*}" in
123 ppp|atm);;
124 *)
125 mac=$(nvram get ${IFTYPE}_hwaddr)
126 ${mac:+$DEBUG ifconfig $INTERFACE down hw ether $mac}
127 ;;
128 esac
129
130 if="$(nvram get ${IFTYPE}_ifname)"
131 if [ "${if%%[0-9]}" = "br" ]; then
132 if_valid "$INTERFACE" && {
133 ifconfig "$if" 2>&- >&- || {
134 stp=$(nvram get ${IFTYPE}_stp)
135 $DEBUG brctl addbr "$if"
136 $DEBUG brctl setfd "$if" 0
137 $DEBUG brctl stp "$if" "${stp:-0}"
138 }
139
140 if [ "$INTERFACE" != "$if" ]; then
141 $DEBUG ifconfig "$INTERFACE" 0.0.0.0 up
142 brctl addif "$if" "$INTERFACE"
143 else
144 do_ifup "$IFPROTO" "$IFTYPE" "$if"
145 fi
146 }
147 else
148 [ "${INTERFACE%%[0-9]*}" = "ppp" ] || do_ifup "$IFPROTO" "$IFTYPE" "$if"
149 fi
150 }
151
152 do_unregister() {
153 [ -z "$IFTYPE" -o -z "$IFPROTO" ] && find_name
154 [ -z "$IFTYPE" -o -z "$IFPROTO" ] && return 0
155
156 [ "${IFACE%%[0-9]*}" = "br" ] && {
157 if [ "$INTERFACE" != "$IFACE" ]; then
158 brctl delif "$IFACE" "$INTERFACE" 2>&- >&-
159 else
160 brctl delbr "$IFACE" 2>&- >&-
161 fi
162 }
163
164 case "$IFPROTO" in
165 pppoe|pppoa|pptp)
166 killall pppd 2>&- >&-
167 ;;
168 dhcp)
169 [ -f /var/run/${INTERFACE}.pid ] && kill "$(cat /var/run/${INTERFACE}.pid)" 2>&- >&-
170 ;;
171 esac
172
173 [ "${INTERFACE%%[0-9]*}" = "atm" ] || ifconfig "$INTERFACE" 0.0.0.0 down 2>&-
174 }
175
176 case "$ACTION" in
177 add|register) do_register;;
178 remove|unregister) do_unregister;;
179 esac