aboutsummaryrefslogtreecommitdiffstats
path: root/sysconfig/network-scripts/ifup-aliases
blob: fbc154706221862c176bc0cbe1ae9e17808914a0 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/bin/bash

#
# configures aliases of device $1
#
# This script goes out of its way to arrive at the configuration of ip
# aliases described in the ifcfg-$DEV:* and ifcfg-$DEV-range* files from
# whatever existing configuration it may be given: existing aliases not
# specified in the configuration will be removed, netmasks and broadcast
# addrs will be updated on existing aliases, and new aliases will be setup.
#
#   range specification files:
#
# One can specify ranges of alised ipaddress using ifcfg-$DEV-range* files.
# Specify multiple ranges using multiple files, such as ifcfg-eth0-range0 and
# ifcfg-eth0-range1, etc. In these files, the following configuration variables 
# specify the range:
#
#    IPADDR_START    -- ipaddr to start range at. eg "192.168.30.1"
#    IPADDR_END      -- ipaddr to end range at. eg "192.168.30.254"
#    CLONENUM_START  -- interface clone number to start using for this range. eg "0"
#
# The above example values create the interfaces eth0:0 through eth0:253 using
# ipaddrs 192.168.30.1 through 192.168.30.254, inclusive.
#
# Other configuration variables such as NETMASK and BROADCAST may be specified
# in the range file and will apply to all of the ipaddresses in the range. Range
# files also inherit configuration from the ifcfg-$DEV file just like normal.
#
# Note that IPADDR_START and IPADR_END are required to be in the same class-c
# block. I.e. IPADDR_START=192.168.30.1 and IPADDR_END=192.168.31.255 is
# not valid.
#
#   speed with large sets of interfaces:
#
# Considerable effort was spent making this script fast. It can efficiently 
# handle a thousand ip aliases on one interface.
# 
# With large sets of ipaddresses the NO_ALIASROUTING=yes configuration is
# highly recommended. (This can be specified in ifcfg-$DEV and inherited.) This
# prevents this script from setting up routing details for the virtual
# interfaces, which I don't think is needed, because outgoing traffic can use the
# main interface. However, make your own conclusions on what you need.
# 
# My test setup of four class C address blocks on a P166 took 25 seconds of 
# which 16 seconds of this was spent in the ifcconfig calls. Without the 
# NO_ALIASROUTING=yes config an additional 12 seconds is spent in route calls.
#
#   notes on internals:
#
# This script uses the bash "eval" command to lookup shell variables with names
# which are generated from other shell variables. This allows us to, in effect,
# create hashes using the shell variable namesspace by just including the hash
# key in the name of the variable.
#
# This script originally written by: David Harris <dharris@drh.net> 
#                                    Principal Engineer, DRH Internet
#                                    June 30, 1999
#
#            modified by: Bill Nottingham <notting@redhat.com>

TEXTDOMAIN=initscripts
TEXTDOMAINDIR=/etc/locale

device=$1
if [ "$device" = "" ]; then
  echo $"usage: ifup-aliases <net-device> [<parent-config>]\n"
  exit 1
fi

PARENTCONFIG=${2:-ifcfg-$device}
parent_device=$device

cd /etc/sysconfig/network-scripts  
. ./network-functions

# Grab the current configuration of any running aliases, place device info
# into variables of the form:
# rdev_<index>_addr = <ip address>
# rdev_<index>_pb = <prefix>_<broadcast>
# rdevip_<ipaddress> = <index>
# Example:
# rdev_0_addr=192.168.1.1
# rdev_0_pb=24_192.16.1.255
# rdevip_192_168_1_1=0
#
# A list of all the devices is created in rdev_LIST.

eval $( ip addr show $device label $device:* | \
    awk 'BEGIN { COUNT=0;LAST_DEV="" } /inet / {
        # Split IP address into address/prefix
        split($2,IPADDR,"/");
        # Create A_B_C_D IP address form
        IP_ADDR=IPADDR[1];
        gsub(/\./,"_",IP_ADDR);
        # Split device into device:index
        split($NF,DEV,":");
        # Update last device
        LAST_DEV=LAST_DEV " " DEV[2];
        printf("rdev_%s_addr=%s\nrdevip_%s=%s\nrdev_%s_pb=%s_%s\nrdev_LIST=\"%s\"\n",
        DEV[2],IPADDR[1],IP_ADDR,DEV[2],DEV[2],IPADDR[2],$4,LAST_DEV);
    } END {
        if(LAST_DEV == "") print "no_devices_are_up=yes"
    }' );

#
# Store configuration of the parent device and network
#

# read from the /etc/sysconfig/network
eval ` (
    . /etc/sysconfig/network;
    echo network_GATEWAY=$GATEWAY\;;
    echo network_GATEWAYDEV=$GATEWAYDEV\;;
) `

# read defaults from the parent config file
[ -f $PARENTCONFIG ] || {
    net_log $"Missing config file $PARENTCONFIG."
    exit 1
}
eval ` (
    . ./$PARENTCONFIG;
    echo default_PREFIX=$PREFIX\;;
    echo default_NETMASK=$NETMASK\;;
    echo default_BROADCAST=$BROADCAST\;;
    echo default_GATEWAY=$GATEWAY\;;
    echo default_NO_ALIASROUTING=$NO_ALIASROUTING\;;
    echo default_ARPCHECK=$ARPCHECK\;;
) `
[ -z "$default_GATEWAY" ] && default_GATEWAY=$network_GATEWAY

function ini_env ()
{
    DEVICE=""
    IPADDR=""
    IPV6ADDR=""
    PREFIX=$default_PREFIX
    NETMASK=$default_NETMASK
    BROADCAST=$default_BROADCAST
    GATEWAY=$default_GATEWAY
    NO_ALIASROUTING=$default_NO_ALIASROUTING
    ONPARENT=""
    ARPCHECK=$default_ARPCHECK
}

function is_default_gateway ()
{
    LC_ALL=C /sbin/ip route ls default scope global \
        | awk '$3 == "'"$1"'" { found = 1; } END { exit found == 0; }'
}

#
# Read the alias configuration files and enable each aliased
# device using new_interface()
#

function new_interface ()
{

    ipa=$IPADDR; ipb=${ipa#*.}; ipc=${ipb#*.};
    IPGLOP="${ipa%%.*}_${ipb%%.*}_${ipc%%.*}_${ipc#*.}";
    DEVNUM=${DEVICE#*:}

    MATCH='^[0-9A-Za-z_]*$'
    if (LC_ALL=C; [[ ! "$DEVNUM" =~ $MATCH ]]); then
        net_log $"error in $FILE: invalid alias number"
        return 1
    fi

    eval "
        ipseen=\$ipseen_${IPGLOP}; devseen=\$devseen_${DEVNUM};
        ipseen_${IPGLOP}=$FILE; devseen_${DEVNUM}=$FILE;
    ";

    if [ -n "$ipseen" ]; then
        net_log $"error in $FILE: already seen ipaddr $IPADDR in $ipseen"
        return 1
    fi

    if [ -n "$devseen" ]; then
        net_log $"error in $FILE: already seen device $parent_device:$DEVNUM in $devseen"
        return 1
    fi

    if [ -z "$DEVICE" -o -z "$IPADDR" ]; then
        if [ -n "$IPV6ADDR" -a -n "$DEVICE" ] && ! is_false "$IPV6INIT"; then
            /etc/sysconfig/network-scripts/ifup-ipv6 ${DEVICE}
            return $?
        fi
        net_log $"error in $FILE: didn't specify device or ipaddr"
        return 1
    fi

    if [ -z "$NETMASK" -a -z "$PREFIX" ]; then
        net_log $"error iN $FILE: didn't specify netmask or prefix"
    fi

    if [ -z "$PREFIX" ]; then
        eval $(/bin/ipcalc --prefix ${IPADDR} ${NETMASK})
    fi

    if [ -z "$BROADCAST" -o "$BROADCAST" = "$default_BROADCAST" ]; then
        eval $(/bin/ipcalc --broadcast ${IPADDR}/${PREFIX})
    fi

    if [ "$no_devices_are_up" = "yes" ]; then
        setup_this=yes
    else

        setup_this=""

        eval "
        rdev_addr=\$rdev_${DEVNUM}_addr;
        rdev_pb=\$rdev_${DEVNUM}_pb;
        rdev_mark=\$rdev_${DEVNUM}_mark;
        rdevip=\$rdevip_${IPGLOP};
        ";

        if [ -n "$rdev_addr" ]; then
            if [ "$rdev_addr" = "${IPADDR}" ]; then
                newmark=keep
                if [ "$rdev_pb" != "${PREFIX}_${BROADCAST}" ]; then
                    setup_this=freshen
                else
                    setup_this=no
                fi
            else
                if [ "$rdev_mark" != "remove" ]; then
                    /sbin/ip addr flush dev $parent_device label $parent_device:${DEVNUM}
                    do_netreport=yes
                fi
                newmark=remove
                setup_this=yes
            fi
            if [ -n "$rdev_mark" -a "$rdev_mark" != "$newmark" ]; then
                net_log $"error in ifcfg-${parent_device}: files"
                return 1
            fi
            eval " rdev_${DEVNUM}_mark=\$newmark ";
        else
            setup_this=yes
        fi

        if [ -n "$rdevip" -a "$rdevip" != "${DEVNUM}" ]; then
            eval " mark_remove=\$rdev_${rdevip}_mark ";
            if [ -n "$mark_remove" -a "$mark_remove" != "remove" ]; then
                net_log $"error in ifcfg-${parent_device}: files"
                return 1
            fi
            if [ "$mark_remove" != "remove" ]; then
                eval " rdev_${rdevip}_mark=remove ";
                /sbin/ip addr flush dev $parent_device label $parent_device:$rdevip
                do_netreport=yes
            fi
        fi

    fi

    if [ "$setup_this" = "freshen" ] ; then
        # we can do the freshen stuff right now
        /sbin/ip addr change ${IPADDR}/${PREFIX} brd ${BROADCAST}
    fi

    if [ "$setup_this" = "yes" ] ; then
        if [ "${parent_device}" != "lo" ] &&  [ "${ARPCHECK}" != "no" ] && \
                is_available ${parent_device} && \
                ( grep -qswi "up" /sys/class/net/${parent_device}/operstate ||  grep -qswi "1" /sys/class/net/${parent_device}/carrier ) ; then
            echo $"Determining if ip address ${IPADDR} is already in use for device ${parent_device}..."
            ARPING=$(/sbin/arping -c 2 -w ${ARPING_WAIT:-3} -D -I ${parent_device} ${IPADDR})
            if [ $? = 1 ]; then
                ARPINGMAC=$(echo $ARPING | sed -ne 's/.*\[\(.*\)\].*/\1/p')
                net_log $"Error, some other host ($ARPINGMAC) already uses address ${IPADDR}."
                return 1
            fi
        fi

        /sbin/ip addr add ${IPADDR}/${PREFIX} brd ${BROADCAST} \
            dev ${parent_device} label ${DEVICE}

        # update ARP cache of neighboring computers:
        if [ "${REALDEVICE}" != "lo" ]; then
            /sbin/arping -q -A -c 1 -I ${parent_device} ${IPADDR}
            ( sleep 2; /sbin/arping -q -U -c 1 -I ${parent_device} ${IPADDR} ) > /dev/null 2>&1 < /dev/null &
        fi

        ! is_false "$IPV6INIT" && \
            /etc/sysconfig/network-scripts/ifup-ipv6 ${DEVICE}

        if [ "$NO_ALIASROUTING" != yes ]; then

            GATEWAYDEV=$network_GATEWAYDEV;

            if [ -n "${GATEWAY}" -a \
                    \( -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${DEVICE}" \) ]; then
                # set up default gateway, if it isn't already there
                if ! is_default_gateway "$GATEWAY"; then
                    ip route replace default ${METRIC:+metric $METRIC} via ${GATEWAY} dev ${DEVICE}
                fi
            fi

            /etc/sysconfig/network-scripts/ifup-routes ${DEVICE} ${NAME}

            do_netreport=yes
            ifuplocal_queue="$ifuplocal_queue $DEVICE"
        fi
    fi
}

if [ "$BASH_VERSINFO" ]; then
    shopt -s nullglob
else
    allow_null_glob_expansion=foo
fi

for FILE in ifcfg-${parent_device}:* ; do
    is_ignored_file "$FILE" && continue
    ini_env
    . ./$FILE
    [ -z "$DEVICE" ] && DEVICE=${FILE##ifcfg-}
    [ "$ONPARENT" != "no" -a "$ONPARENT" != "NO" ] && new_interface
    unset DEVICE
done

for FILE in ifcfg-${parent_device}-range* ; do
    is_ignored_file "$FILE" && continue
    ini_env
    . ./$FILE

    ipaddr_prefix=${IPADDR_START%.*}
    ipaddr_startnum=${IPADDR_START##*.}
    ipaddr_endnum=${IPADDR_END##*.}

    if [ "${IPADDR_START%.*}" != "${IPADDR_END%.*}" ]; then
        net_log $"error in $FILE: IPADDR_START and IPADDR_END don't agree"
        continue
    fi

    if [ $ipaddr_startnum -gt $ipaddr_endnum ]; then
        net_log $"error in $FILE: IPADDR_START greater than IPADDR_END"
        continue
    fi

    ipaddr_num=$ipaddr_startnum
    ipaddr_clonenum=$CLONENUM_START

    while [ $ipaddr_num -le $ipaddr_endnum ]; do
        IPADDR="$ipaddr_prefix.$ipaddr_num"
        DEVICE="$parent_device:$ipaddr_clonenum"
        IPV6INIT="no"
        [ "$ONPARENT" != "no" -a "$ONPARENT" != "NO" ] && new_interface
        ipaddr_num=$(($ipaddr_num+1))
        ipaddr_clonenum=$(($ipaddr_clonenum+1))
    done
done

#
# Remove any devices that should not be around
#
for DEVNUM in $rdev_LIST ; do
    eval " rdev_mark=\$rdev_${DEVNUM}_mark "
    if [ -z "$rdev_mark" ]; then
        /sbin/ip addr flush dev $parent_device label $parent_device:${DEVNUM}
        do_netreport=yes
    fi
done
#
# Notify of new device creation
#

if [ -n "$do_netreport" ]; then
    do_netreport
fi

if [ -x /sbin/ifup-local ]; then
    for DEVICE in $ifuplocal_queue ; do
        /sbin/ifup-local ${DEVICE}
    done
fi