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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2761 - (show annotations) (download) (as text)
Tue Jul 10 15:07:18 2007 UTC (7 years, 2 months ago) by uberlord
File MIME type: text/x-sh
File size: 5074 byte(s)
Fix ifconfig using some iproute2 commands.
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 if [ $# -eq 3 ] ; then
165 set -- "$1" "$2" gw "$3"
166 elif [ "$3" = "via" ] ; then
167 local one=$1 two=$2
168 shift ; shift; shift
169 set -- "${one}" "${two}" gw "$@"
170 fi
171
172 if [ -n "${metric}" ] ; then
173 set -- "$@" metric ${metric}
174 fi
175
176 route add "$@"
177 }
178
179 _delete_addresses() {
180 # We don't remove addresses from aliases
181 case "${IFACE}" in
182 *:*) return 0 ;;
183 esac
184
185 einfo "Removing addresses"
186 eindent
187 # iproute2 can add many addresses to an iface unlike ifconfig ...
188 # iproute2 added addresses cause problems for ifconfig
189 # as we delete an address, a new one appears, so we have to
190 # keep polling
191 while true ; do
192 local addr=$(_get_inet_address)
193 [ -z "${addr}" ] && break
194
195 if [ "${addr}" = "127.0.0.1/8" ] ; then
196 # Don't delete the loopback address
197 [ "${IFACE}" = "lo" -o "${IFACE}" = "lo0" ] && break
198 fi
199 einfo "${addr}"
200 ifconfig "${IFACE}" 0.0.0.0 || break
201 done
202
203 # Remove IPv6 addresses
204 local addr=
205 for addr in $(LC_ALL=C ifconfig "${IFACE}" | \
206 sed -n -e 's/^.*inet6 addr: \([^ ]*\) Scope:[^L].*/\1/p') ; do
207 [ "${addr}" = "::1/128" -a "${IFACE}" = "lo" ] && continue
208 einfo "${addr}"
209 ifconfig "${IFACE}" inet6 del "${addr}"
210 done
211
212 return 0
213 }
214
215 _has_carrier() {
216 return 0
217 }
218
219 _tunnel() {
220 iptunnel "$@"
221 }
222
223 ifconfig_pre_start() {
224 # MTU support
225 local mtu=
226 eval mtu=\$mtu_${IFVAR}
227 [ -n "${mtu}" ] && ifconfig "${IFACE}" mtu "${mtu}"
228
229 local tunnel=
230
231 eval tunnel=\$iptunnel_${IFVAR}
232 [ -z "${tunnel}" ] && return 0
233
234 # Set our base metric to 1000
235 metric=1000
236
237 ebegin "Creating tunnel ${IFVAR}"
238 iptunnel add "${tunnel}"
239 eend $?
240 }
241
242 ifconfig_post_stop() {
243 # Don't delete sit0 as it's a special tunnel
244 [ "${IFACE}" = "sit0" ] && return 0
245
246 [ -z "$(iptunnel show "${IFACE}" 2>/dev/null)" ] && return 0
247
248 ebegin "Destroying tunnel ${IFACE}"
249 iptunnel del "${IFACE}"
250 eend $?
251 }
252
253 # vim: set ts=4 :

  ViewVC Help
Powered by ViewVC 1.1.20