Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | #!/bin/sh |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 2 | # udhcpc script edited by Tim Riker <Tim@Rikers.org> |
| 3 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 4 | RESOLV_CONF="/etc/resolv.conf" |
Denis Vlasenko | 1b47bbd | 2009-04-21 00:17:00 +0000 | [diff] [blame] | 5 | |
Denys Vlasenko | c8ab67c | 2009-05-10 23:27:43 +0200 | [diff] [blame] | 6 | [ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; } |
Denis Vlasenko | 1b47bbd | 2009-04-21 00:17:00 +0000 | [diff] [blame] | 7 | |
Denis Vlasenko | ea4f084 | 2009-04-16 20:04:09 +0000 | [diff] [blame] | 8 | NETMASK="" |
Jiří Prchal | 47839ae | 2018-10-04 09:02:27 +0200 | [diff] [blame] | 9 | if command -v ip >/dev/null; then |
| 10 | [ -n "$subnet" ] && NETMASK="/$subnet" |
| 11 | else |
| 12 | [ -n "$subnet" ] && NETMASK="netmask $subnet" |
| 13 | fi |
Denis Vlasenko | ea4f084 | 2009-04-16 20:04:09 +0000 | [diff] [blame] | 14 | BROADCAST="broadcast +" |
| 15 | [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 16 | |
| 17 | case "$1" in |
| 18 | deconfig) |
Denys Vlasenko | 81a7083 | 2019-09-05 14:58:08 +0200 | [diff] [blame] | 19 | echo "Clearing IP addresses on $interface, upping it" |
Jiří Prchal | 47839ae | 2018-10-04 09:02:27 +0200 | [diff] [blame] | 20 | if command -v ip >/dev/null; then |
Stefan Agner | 62744ef | 2020-06-16 10:06:27 +0200 | [diff] [blame] | 21 | ip -4 addr flush dev $interface |
Denys Vlasenko | 81a7083 | 2019-09-05 14:58:08 +0200 | [diff] [blame] | 22 | ip link set dev $interface up |
Jiří Prchal | 47839ae | 2018-10-04 09:02:27 +0200 | [diff] [blame] | 23 | else |
| 24 | ifconfig $interface 0.0.0.0 |
| 25 | fi |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 26 | ;; |
| 27 | |
| 28 | renew|bound) |
Denis Vlasenko | ea4f084 | 2009-04-16 20:04:09 +0000 | [diff] [blame] | 29 | echo "Setting IP address $ip on $interface" |
Jiří Prchal | 47839ae | 2018-10-04 09:02:27 +0200 | [diff] [blame] | 30 | 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 Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 35 | |
| 36 | if [ -n "$router" ] ; then |
Denis Vlasenko | ea4f084 | 2009-04-16 20:04:09 +0000 | [diff] [blame] | 37 | echo "Deleting routers" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 38 | while route del default gw 0.0.0.0 dev $interface ; do |
| 39 | : |
| 40 | done |
| 41 | |
Eric Andersen | 1aee3ff | 2004-10-13 07:18:05 +0000 | [diff] [blame] | 42 | metric=0 |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 43 | for i in $router ; do |
Denis Vlasenko | ea4f084 | 2009-04-16 20:04:09 +0000 | [diff] [blame] | 44 | echo "Adding router $i" |
Denys Vlasenko | 9c192e7 | 2017-04-06 00:53:43 +0200 | [diff] [blame] | 45 | 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 Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 53 | done |
| 54 | fi |
| 55 | |
Mike Frysinger | 39b8fb4 | 2013-02-27 01:01:43 -0500 | [diff] [blame] | 56 | # If the file is a symlink somewhere (like /etc/resolv.conf |
| 57 | # pointing to /run/resolv.conf), make sure things work. |
Rolf Eike Beer | 7180d9e | 2019-03-28 15:29:29 +0100 | [diff] [blame] | 58 | 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 Frysinger | bca5c55 | 2013-03-12 10:48:09 -0400 | [diff] [blame] | 62 | realconf=$(readlink -f "$RESOLV_CONF" 2>/dev/null || echo "$RESOLV_CONF") |
Rolf Eike Beer | 7f89ebe | 2019-08-13 17:41:56 +0200 | [diff] [blame] | 63 | echo "Recreating $realconf" |
Mike Frysinger | 39b8fb4 | 2013-02-27 01:01:43 -0500 | [diff] [blame] | 64 | tmpfile="$realconf-$$" |
| 65 | > "$tmpfile" |
| 66 | [ -n "$domain" ] && echo "search $domain" >> "$tmpfile" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 67 | for i in $dns ; do |
Denis Vlasenko | ea4f084 | 2009-04-16 20:04:09 +0000 | [diff] [blame] | 68 | echo " Adding DNS server $i" |
Mike Frysinger | 39b8fb4 | 2013-02-27 01:01:43 -0500 | [diff] [blame] | 69 | echo "nameserver $i" >> "$tmpfile" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 70 | done |
Mike Frysinger | 39b8fb4 | 2013-02-27 01:01:43 -0500 | [diff] [blame] | 71 | mv "$tmpfile" "$realconf" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 72 | ;; |
| 73 | esac |
| 74 | |
| 75 | exit 0 |