File: address

package info (click to toggle)
ifupdown-scripts-zg2 0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 500 kB
  • ctags: 76
  • sloc: sh: 3,606; makefile: 35
file content (126 lines) | stat: -rwxr-xr-x 3,826 bytes parent folder | download
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
#!/bin/bash
# $Header$

# IFACE      = Logical interface name
# MODE       = start | stop
# METHOD     = manual, otherwise exit!
# IF_ADDRESS = address/prefix
# IF_DEVICE  = physical interface name
# IF_SCOPE   = scope of address
# IF_BRD     = broadcast address (all1 default, all0/none allowed)
# IF_FLAGS   = "secondary"
# IF_LABEL   = set label (1 default)
# IF_WAIT_TENTATIVE = wait for non-tentative IP addresses (1 default)
# IF_PREFLFT = preferred lifetime

. /etc/network/ifupdown-scripts-zg2.d/common-functions

# check that an address has been configured for this interface
# TODO: Maybe check if it actually looks like an IP address?
[ -z "${IF_ADDRESS:-}" ] && exit 0

case "$MODE" in
  start)
  
    # build address parameters, parse flags
  
    ADDRPARM="dev $IF_DEVICE"
    if [ "$IF_DEVICE" != "$IFACE" ]; then
      # if logical device name differs from physical, label interface
      if [ "${IF_LABEL:-1}" != "0" ]; then
        ADDRPARM="$ADDRPARM label $IF_DEVICE:$IFACE"
      fi
    fi
    if [ "$(addrtype $IF_ADDRESS)" = "v4" ]; then
      IF_BRD=${IF_BRD:="all1"}
      case "$IF_BRD" in
        none) : ;;
        all1) ADDRPARM="$ADDRPARM broadcast +";;
        all0) ADDRPARM="$ADDRPARM broadcast -";;
        *)    ADDRPARM="$ADDRPARM broadcast $IF_BRD";;
      esac
    fi

    ADDRPARM="$ADDRPARM $IF_ADDRESS"
    ADDRPARM="$ADDRPARM ${IF_SCOPE:+scope $IF_SCOPE}"
    ADDRPARM="$ADDRPARM ${IF_PREFLFT:+preferred_lft $IF_PREFLFT}"

    SECONDARY=""
    if echo $IF_FLAGS | grep -s -q -i "secondary"; then
      SECONDARY="1"
    fi

    # initialize interface

    cmd "ip addr add $ADDRPARM"

    # check if primary or secondary IP
    # primary / secondary IP addresses need to be addresed differently since
    # taking down a primary IP also removes the secondaries. This can have
    # unwanted effects to network connectivity and wrecks our interface state.
    
    if ip addr show dev "$IF_DEVICE" secondary | \
    				grep -s -q "$IF_ADDRESS"; then
      # $IF_ADDRESS has become secondary IP
      if [ -z "${SECONDARY:-}" ]; then
      	# $IF_ADDRESS is not marked as secondary
	# take away IP again
	ip addr del "$ADDRPARM"
	abort "secondary IP not marked as such in /etc/network/interface"
      fi
    elif ip addr show dev "$IF_DEVICE" primary | \
    				grep -s -q "$IF_ADDRESS"; then
      # $IF_ADDRESS has become primary IP
      if [ -n "$SECONDARY" ]; then
        # $IF_ADDRESS is marked as secondary
	# take away IP again
	ip addr del "$ADDRPARM"
	abort "secondary IP came up as primary. Bring up primary before bringing up secondaries."
      fi
    fi

    if [ "${IF_WAIT_TENTATIVE:-1}" != "1" ]; then
      loop=30
      while ip --oneline addr show dev "$IF_DEVICE" | grep --quiet tentative; do
        if [ $loop -eq 30 ]; then
          verbose "waiting for IPv6 address to leave tentative state"
        fi
        if [ $loop -eq 0 ]; then
          verbose "giving up on tentative IPv6 address"
	  cmd "ip --oneline addr show dev $IF_DEVICE"
	  break
        fi
        sleep 1
        loop=$(($loop-1))
      done
    fi

    add_down "address" "addr del $ADDRPARM"
    add_down "ip-address" "$IF_ADDRESS"
    add_down "ip-dev" "$IF_DEVICE"

    ;;
  stop)
    ADDR=$(state_entry ip-address)
    DEV=$(state_entry ip-dev)
    exec_down "ip-address" ""
    exec_down "ip-dev" ""
    if [ -n "$DEV" ]; then
      if ip addr show dev "$DEV" primary | \
                                grep -s -q "$ADDR"; then
        # we are trying to take down a primary IP address
        if ip addr show dev "$DEV" secondary | \
      				grep -s -q inet[^6]; then
 	  # there are still secondary IPs present
 	  abort "take down secondary IPs before taking down primary IPs."
        fi
      fi
    fi

    exec_down "address" "ip"
    ;;
  *)
    ;;
esac

# end of file