summaryrefslogtreecommitdiff
path: root/openwrt/package/base-files/default/etc/hotplug.d/net/10-net
blob: 187b937c6080e322e5a208b9ad923647b31a4e2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/sh
setup_eth()
{
	[ -f /proc/net/wl0 ] && {
		lsmod | grep wlcompat >&- || insmod wlcompat
	}
	iwconfig "$INTERFACE" 2>&- | grep -v 'no wireless' >&- && {
		/sbin/wifi
	}
	[ -d /proc/switch ] || {
		insmod switch-core
		insmod switch-robo || insmod switch-adm
	}
	if="$(echo "$INTERFACE" | sed s,eth,et,)"
	ifconfig "$INTERFACE" up 2>&- >&-
	for vlan in $(seq 0 15); do
		[ "$(nvram get vlan${vlan}hwname)" = "$if" ] && {
			$DEBUG vconfig add "$INTERFACE" "$vlan"
			vports="$(nvram get vlan${vlan}ports)"
			[ \! -z "$vports" -a -d "/proc/switch/$INTERFACE" ] && \
				echo "$vports" > "/proc/switch/$INTERFACE/vlan/$vlan/ports"
		}
	done
}

find_name()
{
	pppoa_ifname="atm0" # hack for ppp over atm, which has no ${proto}_ifname
	interfaces="lan wan wifi $(nvram get ifnames)"
	for ifname in $interfaces; do
		IFTYPE="${ifname}"
		IFPROTO="$(nvram get ${IFTYPE}_proto)"
		IFACE="$(nvram get ${IFTYPE}_ifname)"
		case "$IFPROTO" in
			""|none);;
			static|dhcp)
				[ "${IFACE}" = "$INTERFACE" ] && return 0
				[ "${IFACE%%[0-9]*}" = "br" ] && {
					for part in $(nvram get ${IFTYPE}_ifnames); do
						[ "$part" = "$INTERFACE" ] && return 0
					done
				}
			;;
			*)
				[ "$(nvram get ${IFPROTO}_ifname)" = "$INTERFACE" \
				  -a -x /sbin/ifup.${IFPROTO} ] && return 0
			;;
		esac
	done
	IFACE=""
	IFTYPE=""
	IFPROTO=""
	return 255
}

do_ifup() {
	if="$3"
	if_proto="$(nvram get ${2}_proto)"
	
	pidfile=/var/run/${if}.pid
	[ -f $pidfile ] && $DEBUG kill $(cat $pidfile)

	case "$1" in
		static)
			ip=$(nvram get ${2}_ipaddr)
			ip6=$(nvram get ${2}_ip6addr)
			netmask=$(nvram get ${2}_netmask)
			gateway=$(nvram get ${2}_gateway)
	
			$DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} broadcast + up
			[ -n "$ip6" ] && $DEBUG ifconfig $if add $ip6

			${gateway:+$DEBUG route add default gw $gateway}
	
			[ -f /tmp/resolv.conf ] || {
				debug "# --- creating /tmp/resolv.conf ---"
				for dns in $(nvram get ${2}_dns); do
					echo "nameserver $dns" >> /tmp/resolv.conf
				done
			}
			
			env -i ACTION="ifup" INTERFACE="${2}" PROTO=static /sbin/hotplug "iface" &
		;;
		dhcp)
			DHCP_IP=$(nvram get ${2}_ipaddr)
			DHCP_NETMASK=$(nvram get ${2}_netmask)
			$DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} broadcast + up
	
			DHCP_ARGS="-i $if ${DHCP_IP:+-r $DHCP_IP} -b -p $pidfile"
			DHCP_HOSTNAME=$(nvram get ${2}_hostname)
			DHCP_HOSTNAME=${DHCP_HOSTNAME%%.*}
			[ -z $DHCP_HOSTNAME ] || DHCP_ARGS="$DHCP_ARGS -H $DHCP_HOSTNAME"
			[ "$if_proto" = "pptp" ] && DHCP_ARGS="$DHCP_ARGS -n -q" || DHCP_ARGS="$DHCP_ARGS -R &"
			oldpid=$(cat $pidfile)
			${DEBUG:-eval} "udhcpc $DHCP_ARGS"
			pidof udhcpc | grep "$oldpid" >&- 2>&- && {
				sleep 1
				kill -9 $oldpid
			}
			# hotplug events are handled by /usr/share/udhcpc/default.script
		;;
		*)
			if [ -x "/sbin/ifup.$1" ]; then
				( $DEBUG . /sbin/ifup.$1 ${2} $3 )
			fi
		;;
	esac
}

do_register()
{
	case "${INTERFACE%%[0-9]*}" in
		eth) setup_eth;;
	esac

	[ -z "$IFTYPE" -o -z "$IFPROTO" ] && {
		find_name || {
			case "${INTERFACE%%[0-9]*}" in
				wds) 
					for tmp in lan wifi; do
						[ -z "$IFPROTO" ] && [ "$(nvram get ${tmp}_ifname)" = "br0" ] && {
							IFPROTO="$(nvram get ${tmp}_proto)"
							IFTYPE="${tmp}"
						}
					done
					[ -z "$IFPROTO" ] && return 0
				;;
				atm)
					for tmp in lan wan wifi $(nvram get ifnames); do
						[ "$(nvram get ${tmp}_proto)" = "pppoa" ] && {
							do_ifup "pppoa" "$tmp" "$INTERFACE"
							return 0
						}
					done
				;;
				*)
					return 0
				;;
			esac
		}
	}

	case "${INTERFACE%%[0-9]*}" in 
		ppp|atm);;
		*)
			mac=$(nvram get ${IFTYPE}_hwaddr)
			${mac:+$DEBUG ifconfig $INTERFACE down hw ether $mac}
		;;
	esac

	if="$(nvram get ${IFTYPE}_ifname)"
	if [ "${if%%[0-9]}" = "br" ]; then
		if_valid "$INTERFACE" && {
			ifconfig "$if" 2>&- >&- || {
				stp=$(nvram get ${IFTYPE}_stp)
				$DEBUG brctl addbr "$if"
				$DEBUG brctl setfd "$if" 0
				$DEBUG brctl stp "$if" "${stp:-0}"
			}

			if [ "$INTERFACE" != "$if" ]; then
				$DEBUG ifconfig "$INTERFACE" 0.0.0.0 up
				brctl addif "$if" "$INTERFACE"
			else
				do_ifup "$IFPROTO" "$IFTYPE" "$if"
			fi
		}
	else 
		[ "${INTERFACE%%[0-9]*}" = "ppp" ] || do_ifup "$IFPROTO" "$IFTYPE" "$if"
	fi
}

do_unregister() {
	[ -z "$IFTYPE" -o -z "$IFPROTO" ] && find_name
	[ -z "$IFTYPE" -o -z "$IFPROTO" ] && return 0
	
	[ "${IFACE%%[0-9]*}" = "br" ] && {
		if [ "$INTERFACE" != "$IFACE" ]; then
			brctl delif "$IFACE" "$INTERFACE" 2>&- >&-
		else
			brctl delbr "$IFACE" 2>&- >&-
		fi
	}
	
	case "$IFPROTO" in
		pppoe|pppoa|pptp) 
			killall pppd 2>&- >&-
		;;
		dhcp)
			[ -f /var/run/${INTERFACE}.pid ] && kill "$(cat /var/run/${INTERFACE}.pid)" 2>&- >&-
		;;
	esac

	[ "${INTERFACE%%[0-9]*}" = "atm" ] || ifconfig "$INTERFACE" 0.0.0.0 down 2>&-
}

case "$ACTION" in
	add|register) do_register;;
	remove|unregister) do_unregister;;
esac