aboutsummaryrefslogtreecommitdiffstats
path: root/main/vlan/vlan.pre-up
blob: be66cff5f1ab0ef8f4d4c8d033eff12ac5ee6a0c (plain) (blame)
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
#!/bin/sh
do_vlan_config() {
    local device="$1"
    local id="$2"
    local newname="$3"
    local nametype="$4"
    if ! [ -d /proc/net/vlan ]; then
        modprobe 8021q
    fi
    vconfig set_name_type ${nametype}
    vconfig add ${device} ${id}
}


# Preferred method:
# Autoguess construction from #520147
case "$IFACE" in
	  *#*) exit 0 ;;
	  *:*) exit 0 ;;
	  vlan*.*) exit 0 ;;
	  vlan*) VLAN_ID="${IFACE#vlan}" NAME1=VLAN;;
	  *.*) RAW_DEVICE="${IFACE%.*}"; VLAN_ID="${IFACE##*.}" NAME1=DEV;;
	  *) [ -n "${IF_VLAN_ID}" -o -n "${IF_VLAN_RAW_DEVICE}" ]  || exit 0 ;;
esac
case "$VLAN_ID" in
	[1-9]*) NAME2=VID_NO_PAD;;
	0???) NAME2=VID;;
	0*) NAME2=VID_NO_PAD;; # Failsafe fallback in case of backward compatability
esac
if [ -n "$IF_VLAN_RAW_DEVICE" ]; then
  RAW_DEVICE="$IF_VLAN_RAW_DEVICE"
fi

# Strip leading 0s
VLAN_ID="${VLAN_ID#0}"
VLAN_ID="${VLAN_ID#0}"
VLAN_ID="${VLAN_ID#0}"
VLAN_ID="${VLAN_ID#0}"
VLAN_ID="$(($VLAN_ID))"
if [ -n "$IF_VLAN_ID" ]; then
  VLAN_ID="$IF_VLAN_ID"
fi

if [ -z "$VLAN_ID" -o -z "$RAW_DEVICE"  ]; then
  exit 1
fi

if [ -n "$RAW_DEVICE" ] && [ -n "$VLAN_ID" ]; then
    if ! ip link show dev "$RAW_DEVICE" > /dev/null; then
        echo "$RAW_DEVICE does not exist, unable to create $IFACE"
        exit 1
    fi
    ip link set up dev "$RAW_DEVICE"
    do_vlan_config "$RAW_DEVICE" "$VLAN_ID" "$IFACE" "${NAME1}_PLUS_${NAME2}"
    if ! ip link show dev "$IFACE" > /dev/null; then
        echo "Failed to create vlan device $IFACE on device $RAW_DEVICE with tag $VLAN_ID"
        exit 1
    fi
fi