blob: f079ec8b849fed1414adc70f49f971ce897c15ff [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001#!/bin/sh
Russ Dill61fb4892002-10-14 21:41:28 +00002# udhcpc script edited by Tim Riker <Tim@Rikers.org>
3
Russ Dill61fb4892002-10-14 21:41:28 +00004RESOLV_CONF="/etc/resolv.conf"
Denis Vlasenko1b47bbd2009-04-21 00:17:00 +00005
Denys Vlasenkoc8ab67c2009-05-10 23:27:43 +02006[ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; }
Denis Vlasenko1b47bbd2009-04-21 00:17:00 +00007
Denis Vlasenkoea4f0842009-04-16 20:04:09 +00008NETMASK=""
Jiří Prchal47839ae2018-10-04 09:02:27 +02009if command -v ip >/dev/null; then
10 [ -n "$subnet" ] && NETMASK="/$subnet"
11else
12 [ -n "$subnet" ] && NETMASK="netmask $subnet"
13fi
Denis Vlasenkoea4f0842009-04-16 20:04:09 +000014BROADCAST="broadcast +"
15[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
Russ Dill61fb4892002-10-14 21:41:28 +000016
17case "$1" in
18 deconfig)
Denys Vlasenko81a70832019-09-05 14:58:08 +020019 echo "Clearing IP addresses on $interface, upping it"
Jiří Prchal47839ae2018-10-04 09:02:27 +020020 if command -v ip >/dev/null; then
Stefan Agner62744ef2020-06-16 10:06:27 +020021 ip -4 addr flush dev $interface
Denys Vlasenko81a70832019-09-05 14:58:08 +020022 ip link set dev $interface up
Jiří Prchal47839ae2018-10-04 09:02:27 +020023 else
24 ifconfig $interface 0.0.0.0
25 fi
Russ Dill61fb4892002-10-14 21:41:28 +000026 ;;
27
28 renew|bound)
Denis Vlasenkoea4f0842009-04-16 20:04:09 +000029 echo "Setting IP address $ip on $interface"
Jiří Prchal47839ae2018-10-04 09:02:27 +020030 if command -v ip >/dev/null; then
31 ip addr add $ip$NETMASK $BROADCAST dev $interface
32 else
33 ifconfig $interface $ip $NETMASK $BROADCAST
34 fi
Russ Dill61fb4892002-10-14 21:41:28 +000035
36 if [ -n "$router" ] ; then
Denis Vlasenkoea4f0842009-04-16 20:04:09 +000037 echo "Deleting routers"
Russ Dill61fb4892002-10-14 21:41:28 +000038 while route del default gw 0.0.0.0 dev $interface ; do
39 :
40 done
41
Eric Andersen1aee3ff2004-10-13 07:18:05 +000042 metric=0
Russ Dill61fb4892002-10-14 21:41:28 +000043 for i in $router ; do
Denis Vlasenkoea4f0842009-04-16 20:04:09 +000044 echo "Adding router $i"
Denys Vlasenko9c192e72017-04-06 00:53:43 +020045 if [ "$subnet" = "255.255.255.255" ]; then
46 # special case for /32 subnets:
47 # /32 instructs kernel to always use routing for all outgoing packets
48 # (they can never be sent to local subnet - there is no local subnet for /32).
49 # Used in datacenters, avoids the need for private ip-addresses between two hops.
50 ip route add $i dev $interface
51 fi
52 route add default gw $i dev $interface metric $((metric++))
Russ Dill61fb4892002-10-14 21:41:28 +000053 done
54 fi
55
Mike Frysinger39b8fb42013-02-27 01:01:43 -050056 # If the file is a symlink somewhere (like /etc/resolv.conf
57 # pointing to /run/resolv.conf), make sure things work.
Rolf Eike Beer7180d9e2019-03-28 15:29:29 +010058 if test -L "$RESOLV_CONF"; then
59 # If it's a dangling symlink, try to create the target.
60 test -e "$RESOLV_CONF" || touch "$RESOLV_CONF"
61 fi
Mike Frysingerbca5c552013-03-12 10:48:09 -040062 realconf=$(readlink -f "$RESOLV_CONF" 2>/dev/null || echo "$RESOLV_CONF")
Rolf Eike Beer7f89ebe2019-08-13 17:41:56 +020063 echo "Recreating $realconf"
Mike Frysinger39b8fb42013-02-27 01:01:43 -050064 tmpfile="$realconf-$$"
65 > "$tmpfile"
66 [ -n "$domain" ] && echo "search $domain" >> "$tmpfile"
Russ Dill61fb4892002-10-14 21:41:28 +000067 for i in $dns ; do
Denis Vlasenkoea4f0842009-04-16 20:04:09 +000068 echo " Adding DNS server $i"
Mike Frysinger39b8fb42013-02-27 01:01:43 -050069 echo "nameserver $i" >> "$tmpfile"
Russ Dill61fb4892002-10-14 21:41:28 +000070 done
Mike Frysinger39b8fb42013-02-27 01:01:43 -050071 mv "$tmpfile" "$realconf"
Russ Dill61fb4892002-10-14 21:41:28 +000072 ;;
73esac
74
75exit 0