next up previous contents
Next: Example: ifcfg Up: ip-cref Previous: How to only translate   Contents


Example: minimal host setup

The following script gives an example of a fault safe setup of IP (and IPv6, if it is compiled into the kernel) in the common case of a node attached to a single broadcast network. A more advanced script, which may be used both on multihomed hosts and on routers, is described in the following section.

The utilities used in the script may be found in the directory ftp://ftp.inr.ac.ru/ip-routing/:

  1. ip -- package iproute2.
  2. arping -- package iputils.
  3. rdisc -- package iputils.
1cm NB. It also refers to a DHCP client, dhcpcd. I should refrain from recommending a good DHCP client to use. All that I can say is that ISC dhcp-2.0b1pl6 patched with the patch that can be found in the dhcp.bootp.rarp subdirectory of the same ftp site does work, at least on Ethernet and Token Ring.

#! /bin/bash
# Usage: ifone ADDRESS[/PREFIX-LENGTH] [DEVICE]
# Parameters:
# $1 -- Static IP address, optionally followed by prefix length.
# $2 -- Device name. If it is missing, eth0 is asssumed.
# F.e. ifone 193.233.7.90
dev=$2
: ${dev:=eth0}
ipaddr=
# Parse IP address, splitting prefix length.
if [ "$1" != "" ]; then
  ipaddr=${1%/*}
  if [ "$1" != "$ipaddr" ]; then
    pfxlen=${1#*/}
  fi
  : ${pfxlen:=24}
fi
pfx="${ipaddr}/${pfxlen}"

# Step 0 -- enable loopback.
#
# This step is necessary on any networked box before attempt
# to configure any other device.
ip link set up dev lo
ip addr add 127.0.0.1/8 dev lo brd + scope host
# IPv6 autoconfigure themself on loopback.
#
# If user gave loopback as device, we add the address as alias and exit.
if [ "$dev" = "lo" ]; then
  if [ "$ipaddr" != "" -a  "$ipaddr" != "127.0.0.1" ]; then
    ip address add $ipaddr dev $dev
    exit $?
  fi
  exit 0
fi

# Step 1 -- enable device $dev

if ! ip link set up dev $dev ; then
  echo "Cannot enable interface $dev. Aborting." 1>&2
  exit 1
fi
# The interface is UP. IPv6 started stateless autoconfiguration itself,
# and its configuration finishes here. However,
# IP still needs some static preconfigured address.
if [ "$ipaddr" = "" ]; then
  echo "No address for $dev is configured, trying DHCP..." 1>&2
  dhcpcd
  exit $?
fi

# Step 2 -- IP Duplicate Address Detection [9].
# Send two probes and wait for result for 3 seconds.
# If the interface opens slower f.e. due to long media detection,
# you want to increase the timeout.
if ! arping -q -c 2 -w 3 -D -I $dev $ipaddr ; then
  echo "Address $ipaddr is busy, trying DHCP..." 1>&2
  dhcpcd
  exit $?
fi
# OK, the address is unique, we may add it on the interface.
#
# Step 3 -- Configure the address on the interface.

if ! ip address add $pfx brd + dev $dev; then
  echo "Failed to add $pfx on $dev, trying DHCP..." 1>&2
  dhcpcd
  exit $?
fi

# Step 4 -- Announce our presence on the link.

arping -A -c 1 -I $dev $ipaddr
noarp=$?
( sleep 2;
  arping -U -c 1 -I $dev $ipaddr ) >& /dev/null </dev/null &

# Step 5 (optional) -- Add some control routes.
#
# 1. Prohibit link local multicast addresses.
# 2. Prohibit link local (alias, limited) broadcast.
# 3. Add default multicast route.
ip route add unreachable 224.0.0.0/24 
ip route add unreachable 255.255.255.255
if [ `ip link ls $dev | grep -c MULTICAST` -ge 1 ]; then
  ip route add 224.0.0.0/4 dev $dev scope global
fi

# Step 6 -- Add fallback default route with huge metric.
# If a proxy ARP server is present on the interface, we will be
# able to talk to all the Internet without further configuration.
# It is not so cheap though and we still hope that this route
# will be overridden by more correct one by rdisc.
# Do not make this step if the device is not ARPable,
# because dead nexthop detection does not work on them.
if [ "$noarp" = "0" ]; then
  ip ro add default dev $dev metric 30000 scope global
fi

# Step 7 -- Restart router discovery and exit.
killall -HUP rdisc || rdisc -fs
exit 0


next up previous contents
Next: Example: ifcfg Up: ip-cref Previous: How to only translate   Contents