-
Notifications
You must be signed in to change notification settings - Fork 12
/
multihomed-routing-ifup
executable file
·100 lines (83 loc) · 3.5 KB
/
multihomed-routing-ifup
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
#!/bin/bash
handle_nm_ifup()
{
echo "$(env)" >> /tmp/nm-env.log
echo "\n" >> /tmp/nm-env.log
for ((i=0; i < IP4_NUM_ADDRESSES; i++))
do
varname="IP4_ADDRESS_$i"
addr_str=$(echo ${!varname} | cut -d " " -f 1)
network_address=$(echo "$addr_str" | cut -d "/" -f 1)
if [ -n "$DHCP4_SUBNET_MASK" ];
then
network_number=$DHCP4_NETWORK_NUMBER
netmask=$DHCP4_SUBNET_MASK;
else
netmask=$(ipcalc $addr_str | grep Netmask | tr -d " " | cut -d ":" -f 2 | cut -d "=" -f 1)
network_number=$(ipcalc "$addr_str" | grep "^Network" | tr -d " " | cut -d ":" -f 2 | cut -d "/" -f 1)
fi
#get route
rt_table=$(/usr/sbin/table_allocator_client -4 -s -a "$network_address" -n "$netmask" -i "$IFACE" -d tas_socket)
if [ "$rt_table" -eq 0 ];
then
rt_table=254;
if_metric=$(/sbin/ip link show dev "$IFACE" | head -1 | cut -d " " -f 1 | cut -d ":" -f 1)
fi
#move the address route created by default
#ip -4 ro delete ${network_number}${netmask:+/$netmask} dev ${IFACE}
ip -4 ro add ${network_number}${netmask:+/$netmask} dev ${IFACE} \
src ${network_address} table ${rt_table} ${if_metric:+metric $if_metric}
#move default route to table
#ip -4 ro delete default via ${IP4_GATEWAY} dev ${IFACE}
ip -4 ro add default via ${IP4_GATEWAY} dev ${IFACE} src ${network_address} \
table ${rt_table} ${if_metric:+metric $if_metric}
#move other provided routes
for ((j=0; j < IP4_NUM_ROUTES; j++))
do
varname="IP4_ROUTE_$i"
route_address=$(echo ${!varname} | cut -d " " -f 1)
route_next_hop=$(echo ${!varname} | cut -d " " -f 2)
route_metric=$(echo ${!varname} | cut -d " " -f 3)
#ip -4 ro delete ${route_address} via ${route_next_hop} dev ${IFACE} \
# ${route_metric:+metric $route_metric}
ip -4 ro add ${route_address} via ${route_next_hop} dev ${IFACE} \
${route_metric:+metric $route_metric} table ${rt_table} \
src ${network_address}
done
done
}
handle_ifup()
{
#get route
rt_table=$(/usr/sbin/table_allocator_client -4 -s -a "$IF_ADDRESS" -n "$IF_NETMASK" -i "$IFACE" -d tas_socket)
if [ "$rt_table" -eq 0 ];
then
rt_table=254;
if_metric=$(/sbin/ip link show dev "$IFACE" | head -1 | cut -d " " -f 1 | cut -d ":" -f 1)
fi
#This is not the nicest way in the world to extract network number, look into
#bash bitwise operators when time
network_number=$(ipcalc "$IF_ADDRESS"/"$IF_NETMASK" | grep "^Network" | tr -d " " |
cut -d ":" -f 2 | cut -d "/" -f 1)
#we are run from up, static configuration is already applied
#move the address route created by default
ip -4 ro delete ${network_number}${IF_NETMASK:+/$IF_NETMASK} dev ${IFACE}
ip -4 ro add ${network_number}${IF_NETMASK:+/$IF_NETMASK} dev ${IFACE} \
src ${IF_ADDRESS} table ${rt_table} ${if_metric:+metric $if_metric}
#move default route to table
ip -4 ro delete default via ${IF_GATEWAY} dev ${IFACE}
ip -4 ro add default via ${IF_GATEWAY} dev ${IFACE} src ${IF_ADDRESS} \
table ${rt_table} ${if_metric:+metric $if_metric}
}
if [ "$ADDRFAM" != "inet" ];
then
exit 0;
fi
if [ "$METHOD" = "NetworkManager" -a "$IP4_GATEWAY" ];
then
handle_nm_ifup
elif [ "$METHOD" = "static" -a -n "$IF_GATEWAY" ];
then
handle_ifup
fi
exit 0