1#!/bin/sh
2#
3# Copyright (c) 2018 Intel Corporation
4# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#    http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18# Setup virtual lan (VLAN) in Linux side that would work with this sample.
19
20if [ -z "$1" ]; then
21    echo "Network interface is missing."
22    echo "This is the network interface where the VLAN will be running."
23    echo "Example: eth0, tap0 etc."
24    exit 1
25fi
26
27if [ `id -u` != 0 ]; then
28    echo "Run this script as a root user!"
29    exit 2
30fi
31
32IFACE="$1"
33
34VLAN_NAME_PREFIX="vlan"
35
36PREFIX_1_IPV6="2001:db8:100"
37PREFIXLEN_1_IPV6="64"
38PREFIX_2_IPV6="2001:db8:200"
39PREFIXLEN_2_IPV6="64"
40
41# From RFC 5737
42PREFIX_1_IPV4="198.51.100"
43PREFIXLEN_1_IPV4="24"
44PREFIX_2_IPV4="203.0.113"
45PREFIXLEN_2_IPV4="24"
46
47ip link add link ${IFACE} name ${VLAN_NAME_PREFIX}.100 type vlan id 100
48ip link add link ${IFACE} name ${VLAN_NAME_PREFIX}.200 type vlan id 200
49
50ip link set ${VLAN_NAME_PREFIX}.100 up
51ip link set ${VLAN_NAME_PREFIX}.200 up
52
53ip -6 addr add ${PREFIX_1_IPV6}::2/${PREFIXLEN_1_IPV6} dev ${VLAN_NAME_PREFIX}.100
54ip -6 route add ${PREFIX_1_IPV6}::/${PREFIXLEN_1_IPV6} \
55   dev ${VLAN_NAME_PREFIX}.100
56
57ip -6 addr add ${PREFIX_2_IPV6}::2/${PREFIXLEN_2_IPV6} dev ${VLAN_NAME_PREFIX}.200
58ip -6 route add ${PREFIX_2_IPV6}::/${PREFIXLEN_2_IPV6} \
59   dev ${VLAN_NAME_PREFIX}.200
60
61ip addr add ${PREFIX_1_IPV4}.2 dev ${VLAN_NAME_PREFIX}.100
62ip route add ${PREFIX_1_IPV4}/${PREFIXLEN_1_IPV4} dev ${VLAN_NAME_PREFIX}.100
63
64ip addr add ${PREFIX_2_IPV4}.2 dev ${VLAN_NAME_PREFIX}.200
65ip route add ${PREFIX_2_IPV4}/${PREFIXLEN_2_IPV4} dev ${VLAN_NAME_PREFIX}.200
66
67
68# You can remove the virtual interface like this
69#    ip link del link eth0 dev vlan.100
70#    ip link del link eth0 dev vlan.200
71
72# If your devices HW MAC address changes when rebooting or flashing,
73# then you can flush the neighbor cache in Linux like this:
74#    ip neigh flush dev vlan.100
75#    ip neigh flush dev vlan.200
76