/[baselayout]/trunk/net.Linux/ifconfig.sh
Gentoo

Contents of /trunk/net.Linux/ifconfig.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2812 - (show annotations) (download) (as text)
Fri Aug 3 13:53:20 2007 UTC (7 years, 1 month ago) by uberlord
File MIME type: text/x-sh
File size: 5197 byte(s)
Fix static IPv6 routes, #187514.
1 # Copyright 2004-2007 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 ifconfig_depend() {
5 program /sbin/ifconfig
6 provide interface
7 }
8
9 _up() {
10 ifconfig "${IFACE}" up
11 }
12
13 _down() {
14 ifconfig "${IFACE}" down
15 }
16
17 _exists() {
18 grep -Eq "^[[:space:]]*${IFACE}:" /proc/net/dev
19 }
20
21 _ifindex() {
22 local line= i=-2
23 while read line ; do
24 i=$((${i} + 1))
25 [ ${i} -lt 1 ] && continue
26 case "${line}" in
27 "${IFACE}:"*) echo "${i}"; return 0;;
28 esac
29 done < /proc/net/dev
30 return 1
31 }
32
33 _is_wireless() {
34 # Support new sysfs layout
35 [ -d /sys/class/net/"${IFACE}"/wireless ] && return 0
36
37 [ ! -e /proc/net/wireless ] && return 1
38 grep -Eq "^[[:space:]]*${IFACE}:" /proc/net/wireless
39 }
40
41 _get_mac_address() {
42 local mac=$(LC_ALL=C ifconfig "${IFACE}" | \
43 sed -n -e 's/.* HWaddr \(..:..:..:..:..:..\).*/\1/p')
44
45
46 case "${mac}" in
47 00:00:00:00:00:00) ;;
48 44:44:44:44:44:44) ;;
49 FF:FF:FF:FF:FF:FF) ;;
50 "") ;;
51 *) echo "${mac}"; return 0 ;;
52 esac
53
54 return 1
55 }
56
57 _set_mac_address() {
58 ifconfig "${IFACE}" hw ether "$1"
59 }
60
61 _get_inet_address() {
62 set -- $(LC_ALL=C ifconfig "${IFACE}" |
63 sed -n -e 's/.*inet addr:\([^ ]*\).*Mask:\([^ ]*\).*/\1 \2/p')
64 [ -z "$1" ] && return 1
65
66 echo -n "$1"
67 shift
68 echo "/$(_netmask2cidr "$1")"
69 }
70
71 _get_inet_addresses() {
72 local iface=${IFACE} i=0
73 local addrs="$(_get_inet_address)"
74
75 while true ; do
76 local IFACE="${iface}:${i}"
77 _exists || break
78 local addr="$(_get_inet_address)"
79 [ -n "${addr}" ] && addrs="${addrs}${addrs:+ }${addr}"
80 i=$((${i} + 1))
81 done
82 echo "${addrs}"
83 }
84
85 _cidr2netmask() {
86 local cidr="$1" netmask="" done=0 i=0 sum=0 cur=128
87 local octets= frac=
88
89 local octets=$((${cidr} / 8))
90 local frac=$((${cidr} % 8))
91 while [ ${octets} -gt 0 ] ; do
92 netmask="${netmask}.255"
93 octets=$((${octets} - 1))
94 done=$((${done} + 1))
95 done
96
97 if [ ${done} -lt 4 ] ; then
98 while [ ${i} -lt ${frac} ] ; do
99 sum=$((${sum} + ${cur}))
100 cur=$((${cur} / 2))
101 i=$((i + 1))
102 done
103 netmask="${netmask}.${sum}"
104 done=$((${done} + 1))
105
106 while [ ${done} -lt 4 ] ; do
107 netmask="${netmask}.0"
108 done=$((${done} + 1))
109 done
110 fi
111
112 echo "${netmask#.*}"
113 }
114
115 _add_address() {
116 if [ "$1" = "127.0.0.1/8" -a "${IFACE}" = "lo" ] ; then
117 ifconfig "${IFACE}" "$@" 2>/dev/null
118 return 0
119 fi
120
121 case "$1" in
122 *:*) ifconfig "${IFACE}" inet6 add "$@"; return $?;;
123 esac
124
125 # IPv4 is tricky - ifconfig requires an aliased device
126 # for multiple addresses
127 local iface="${IFACE}"
128 if LC_ALL=C ifconfig "${iface}" | grep -Eq "\<inet addr:.*" ; then
129 # Get the last alias made for the interface and add 1 to it
130 i=$(ifconfig | sed '1!G;h;$!d' | grep -m 1 -o "^${iface}:[0-9]*" \
131 | sed -n -e 's/'"${iface}"'://p')
132 i=$((${i:-0} + 1))
133 iface="${iface}:${i}"
134 fi
135
136 # ifconfig doesn't like CIDR addresses
137 local ip="${1%%/*}" cidr="${1##*/}" netmask=
138 if [ -n "${cidr}" -a "${cidr}" != "${ip}" ]; then
139 netmask="$(_cidr2netmask "${cidr}")"
140 shift
141 set -- "${ip}" netmask "${netmask}" "$@"
142 fi
143
144 local arg= cmd=
145 while [ -n "$1" ] ; do
146 case "$1" in
147 brd)
148 if [ "$2" = "+" ] ; then
149 shift
150 else
151 cmd="${cmd} broadcast"
152 fi
153 ;;
154 peer) cmd="${cmd} pointtopoint";;
155 *) cmd="${cmd} $1" ;;
156 esac
157 shift
158 done
159
160 ifconfig "${iface}" ${cmd}
161 }
162
163 _add_route() {
164 local inet6=
165
166 if [ -n "${metric}" ] ; then
167 set -- "$@" metric ${metric}
168 fi
169
170 if [ $# -eq 3 ] ; then
171 set -- "$1" "$2" gw "$3"
172 elif [ "$3" = "via" ] ; then
173 local one=$1 two=$2
174 shift ; shift; shift
175 set -- "${one}" "${two}" gw "$@"
176 fi
177
178 case "$@" in
179 *:*)
180 inet6="-A inet6"
181 [ "$1" = "-net" ] && shift
182 ;;
183 esac
184
185 route ${inet6} add "$@" dev "${IFACE}"
186 }
187
188 _delete_addresses() {
189 # We don't remove addresses from aliases
190 case "${IFACE}" in
191 *:*) return 0 ;;
192 esac
193
194 einfo "Removing addresses"
195 eindent
196 # iproute2 can add many addresses to an iface unlike ifconfig ...
197 # iproute2 added addresses cause problems for ifconfig
198 # as we delete an address, a new one appears, so we have to
199 # keep polling
200 while true ; do
201 local addr=$(_get_inet_address)
202 [ -z "${addr}" ] && break
203
204 if [ "${addr}" = "127.0.0.1/8" ] ; then
205 # Don't delete the loopback address
206 [ "${IFACE}" = "lo" -o "${IFACE}" = "lo0" ] && break
207 fi
208 einfo "${addr}"
209 ifconfig "${IFACE}" 0.0.0.0 || break
210 done
211
212 # Remove IPv6 addresses
213 local addr=
214 for addr in $(LC_ALL=C ifconfig "${IFACE}" | \
215 sed -n -e 's/^.*inet6 addr: \([^ ]*\) Scope:[^L].*/\1/p') ; do
216 [ "${addr}" = "::1/128" -a "${IFACE}" = "lo" ] && continue
217 einfo "${addr}"
218 ifconfig "${IFACE}" inet6 del "${addr}"
219 done
220
221 return 0
222 }
223
224 _has_carrier() {
225 return 0
226 }
227
228 _tunnel() {
229 iptunnel "$@"
230 }
231
232 ifconfig_pre_start() {
233 # MTU support
234 local mtu=
235 eval mtu=\$mtu_${IFVAR}
236 [ -n "${mtu}" ] && ifconfig "${IFACE}" mtu "${mtu}"
237
238 local tunnel=
239
240 eval tunnel=\$iptunnel_${IFVAR}
241 [ -z "${tunnel}" ] && return 0
242
243 # Set our base metric to 1000
244 metric=1000
245
246 ebegin "Creating tunnel ${IFVAR}"
247 iptunnel add "${tunnel}"
248 eend $?
249 }
250
251 ifconfig_post_stop() {
252 # Don't delete sit0 as it's a special tunnel
253 [ "${IFACE}" = "sit0" ] && return 0
254
255 [ -z "$(iptunnel show "${IFACE}" 2>/dev/null)" ] && return 0
256
257 ebegin "Destroying tunnel ${IFACE}"
258 iptunnel del "${IFACE}"
259 eend $?
260 }
261
262 # vim: set ts=4 :

  ViewVC Help
Powered by ViewVC 1.1.20