root / tools / net-common.in @ bfca72bc
History | View | Annotate | Download (2.6 kB)
1 |
#!/bin/bash |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2011, 2012, 2013 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
@SHELL_ENV_INIT@ |
22 |
|
23 |
function check { |
24 |
|
25 |
if [ -z "$INTERFACE" ]; then |
26 |
echo "No network interface specified" |
27 |
exit 1 |
28 |
fi |
29 |
|
30 |
if [ -z "$MODE" ]; then |
31 |
echo "MODE not specified" |
32 |
exit 1 |
33 |
fi |
34 |
|
35 |
} |
36 |
|
37 |
function fix_mac { |
38 |
|
39 |
# Fix the autogenerated MAC to have the first octet set to "fe" |
40 |
# to discourage the bridge from using the TAP dev's MAC |
41 |
FIXED_MAC=$(ip link show $INTERFACE | \ |
42 |
awk '{if ($1 == "link/ether") printf("fe%s",substr($2,3,15))}') |
43 |
# in case of a vif (xen_netback device) this action is not allowed |
44 |
ip link set $INTERFACE address $FIXED_MAC || true |
45 |
|
46 |
} |
47 |
|
48 |
function setup_bridge { |
49 |
|
50 |
if [ "$MODE" = "bridged" ]; then |
51 |
fix_mac |
52 |
ip link set $INTERFACE up |
53 |
ip link set $INTERFACE mtu $(</sys/class/net/${BRIDGE}/mtu) |
54 |
|
55 |
# Connect the interface to the bridge |
56 |
brctl addif $BRIDGE $INTERFACE |
57 |
fi |
58 |
|
59 |
} |
60 |
|
61 |
function setup_ovs { |
62 |
if [ "$MODE" = "openvswitch" ]; then |
63 |
ovs-vsctl add-port ${LINK} $INTERFACE |
64 |
fi |
65 |
} |
66 |
|
67 |
function setup_route { |
68 |
if [ "$MODE" = "routed" ]; then |
69 |
ip link set $INTERFACE up |
70 |
|
71 |
if [ -z "$IP" ]; then |
72 |
echo "Routed NIC but no IP address specified" |
73 |
exit 1 |
74 |
fi |
75 |
|
76 |
# Route traffic targeted at the IP to the interface |
77 |
if [ -n "$LINK" ]; then |
78 |
while ip rule del dev $INTERFACE; do :; done |
79 |
ip rule add dev $INTERFACE table $LINK |
80 |
ip route replace $IP table $LINK proto static dev $INTERFACE |
81 |
|
82 |
else |
83 |
ip route replace $IP proto static dev $INTERFACE |
84 |
fi |
85 |
|
86 |
# Allow routing and arp proxying, or ndp proxying (IPv6) |
87 |
if [ -d "/proc/sys/net/ipv4/conf/$INTERFACE" ]; then |
88 |
echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp |
89 |
echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding |
90 |
fi |
91 |
|
92 |
if [ -d "/proc/sys/net/ipv6/conf/$INTERFACE" ]; then |
93 |
echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp |
94 |
echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/forwarding |
95 |
fi |
96 |
fi |
97 |
} |