Statistics
| Branch: | Tag: | Revision:

root / common.sh @ aeaafeb9

History | View | Annotate | Download (8.6 kB)

1
#!/bin/bash
2

    
3
function try {
4

    
5
  $1 &>/dev/null || true
6

    
7
}
8

    
9
function clear_routed_setup_ipv4 {
10

    
11
 arptables -D OUTPUT -o $INTERFACE --opcode request -j mangle
12
 while ip rule del dev $INTERFACE; do :; done
13
 iptables -D FORWARD -i $INTERFACE -p udp --dport 67 -j DROP
14

    
15
}
16

    
17
function clear_routed_setup_ipv6 {
18

    
19
 while ip -6 rule del dev $INTERFACE; do :; done
20

    
21
}
22

    
23
function delete_neighbor_proxy {
24

    
25
  get_uplink $LINK "-6"
26
  get_eui64 $MAC $SUBNET6
27
  $SNF_NETWORK_LOG $0 "ip -6 neigh del proxy $EUI64 dev $UPLINK"
28
  ip -6 neigh del proxy $EUI64 dev $UPLINK
29

    
30
}
31

    
32
function clear_routed_setup_firewall {
33

    
34
  for oldchain in protected unprotected limited; do
35
    iptables  -D FORWARD -o $INTERFACE -j $oldchain
36
    ip6tables -D FORWARD -o $INTERFACE -j $oldchain
37
  done
38

    
39
}
40

    
41
function clear_ebtables {
42

    
43
  runlocked $RUNLOCKED_OPTS ebtables -D FORWARD -i $INTERFACE -j $FROM
44
  runlocked $RUNLOCKED_OPTS ebtables -D INPUT -i $INTERFACE -j $FROM
45
  runlocked $RUNLOCKED_OPTS ebtables -D FORWARD -o $INTERFACE -j $TO
46
  runlocked $RUNLOCKED_OPTS ebtables -D OUTPUT -o $INTERFACE -j $TO
47

    
48
  runlocked $RUNLOCKED_OPTS ebtables -X $FROM
49
  runlocked $RUNLOCKED_OPTS ebtables -X $TO
50
}
51

    
52

    
53
function clear_nfdhcpd {
54

    
55
  rm $NFDHCPD_STATE_DIR/$INTERFACE
56

    
57
}
58

    
59

    
60
function routed_setup_ipv4 {
61

    
62
  if [ -z "$INTERFACE" -o -z "$NETWORK_GATEWAY" -o -z "$IP" -o -z "$TABLE" ]
63
  then
64
    return
65
  fi
66

    
67
	# mangle ARPs to come from the gw's IP
68
	arptables -A OUTPUT -o $INTERFACE --opcode request -j mangle --mangle-ip-s    "$NETWORK_GATEWAY"
69

    
70
	# route interface to the proper routing table
71
	ip rule add dev $INTERFACE table $TABLE
72

    
73
	# static route mapping IP -> INTERFACE
74
	ip route replace $IP proto static dev $INTERFACE table $TABLE
75

    
76
	# Enable proxy ARP
77
	echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp
78

    
79
}
80

    
81
function send_garp {
82

    
83
  # Send GARP from host to upstream router
84
  get_uplink $TABLE
85
  echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind
86
  $SNF_NETWORK_LOG $0 "arpsend -U -i $IP -c1 $UPLINK"
87
  arpsend -U -i $IP -c1 $UPLINK
88
  echo 0 > /proc/sys/net/ipv4/ip_nonlocal_bind
89

    
90
}
91

    
92
function routed_setup_ipv6 {
93
	# Add a routing entry for the eui-64
94
  get_uplink $TABLE "-6"
95
  get_eui64 $MAC $NETWORK_SUBNET6
96

    
97
  if [ -z "$EUI64" -o -z "$TABLE" -o -z "$INTERFACE" -o -z "$UPLINK" ]
98
  then
99
    return
100
  fi
101

    
102
	ip -6 rule add dev $INTERFACE table $TABLE
103
	ip -6 ro replace $EUI64/128 dev $INTERFACE table $TABLE
104
	ip -6 neigh add proxy $EUI64 dev $UPLINK
105

    
106
	# disable proxy NDP since we're handling this on userspace
107
	# this should be the default, but better safe than sorry
108
	echo 0 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp
109

    
110
  # Send Unsolicited Neighbor Advertisement
111
  $SNF_NETWORK_LOG $0 "ndsend $EUI64 $UPLINK"
112
  ndsend $EUI64 $UPLINK
113

    
114
}
115

    
116
# pick a firewall profile per NIC, based on tags (and apply it)
117
function routed_setup_firewall {
118
	# for latest ganeti there is no need to check other but uuid
119
	ifprefixindex="synnefo:network:$INTERFACE_INDEX:"
120
	ifprefixname="synnefo:network:$INTERFACE_NAME:"
121
	ifprefixuuid="synnefo:network:$INTERFACE_UUID:"
122
	for tag in $TAGS; do
123
		tag=${tag#$ifprefixindex}
124
		tag=${tag#$ifprefixname}
125
		tag=${tag#$ifprefixuuid}
126
		case $tag in
127
		protected)
128
			chain=protected
129
		;;
130
		unprotected)
131
			chain=unprotected
132
		;;
133
		limited)
134
			chain=limited
135
		;;
136
		esac
137
	done
138

    
139
	if [ "x$chain" != "x" ]; then
140
		iptables  -A FORWARD -o $INTERFACE -j $chain
141
		ip6tables -A FORWARD -o $INTERFACE -j $chain
142
	fi
143
}
144

    
145
function init_ebtables {
146

    
147
  runlocked $RUNLOCKED_OPTS ebtables -N $FROM -P RETURN
148
  runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -i $INTERFACE -j $FROM
149
  # This is needed for multicast packets
150
  runlocked $RUNLOCKED_OPTS ebtables -A INPUT -i $INTERFACE -j $FROM
151

    
152
  runlocked $RUNLOCKED_OPTS ebtables -N $TO -P RETURN
153
  runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -o $INTERFACE -j $TO
154
  # This is needed for multicast packets
155
  runlocked $RUNLOCKED_OPTS ebtables -A OUTPUT -o $INTERFACE -j $TO
156

    
157
}
158

    
159

    
160
function setup_ebtables {
161

    
162
  # do not allow changes in ip-mac pair
163
  if [ -n "$IP" ]; then
164
    :; # runlocked $RUNLOCKED_OPTS ebtables -A $FROM --ip-source \! $IP -p ipv4 -j DROP
165
  fi
166
  runlocked $RUNLOCKED_OPTS ebtables -A $FROM -s \! $MAC -j DROP
167
  # accept dhcp responses from host (nfdhcpd)
168
  # this is actually not needed because nfdhcpd opens a socket and binds is with
169
  # tap interface so dhcp response does not go through bridge
170
  # runlocked $RUNLOCKED_OPTS ebtables -A $TO -s $INDEV_MAC -p ipv4 --ip-protocol=udp  --ip-destination-port=68 -j ACCEPT
171
  # allow only packets from the same mac prefix
172
  runlocked $RUNLOCKED_OPTS ebtables -A $TO -s \! $MAC/$MAC_MASK -j DROP
173
}
174

    
175
function setup_masq {
176

    
177
  # allow packets from/to router (for masquerading)
178
  # runlocked $RUNLOCKED_OPTS ebtables -A $TO -s $NODE_MAC -j ACCEPT
179
  # runlocked $RUNLOCKED_OPTS ebtables -A INPUT -i $INTERFACE -j $FROM
180
  # runlocked $RUNLOCKED_OPTS ebtables -A OUTPUT -o $INTERFACE -j $TO
181
  return
182

    
183
}
184

    
185
function setup_nfdhcpd {
186
	umask 022
187
  FILE=$NFDHCPD_STATE_DIR/$INTERFACE
188
  #IFACE is the interface from which the packet seems to arrive
189
  #needed in bridged mode where the packets seems to arrive from the
190
  #bridge and not from the tap
191
	cat >$FILE <<EOF
192
INDEV=$INDEV
193
IP=$IP
194
MAC=$MAC
195
HOSTNAME=$INSTANCE
196
TAGS="$TAGS"
197
GATEWAY=$NETWORK_GATEWAY
198
SUBNET=$NETWORK_SUBNET
199
GATEWAY6=$NETWORK_GATEWAY6
200
SUBNET6=$NETWORK_SUBNET6
201
EUI64=$($MAC2EUI64 $MAC $NETWORK_SUBNET6 2>/dev/null)
202
EOF
203

    
204
}
205

    
206
function get_uplink {
207

    
208
  local table=$1
209
  local version=$2
210
  UPLINK=$(ip "$version" route list table "$table" | grep "default via" | awk '{print $5}')
211

    
212
}
213

    
214
# Because we do not have IPv6 value in our environment
215
# we caclulate it based on the NIC's MAC and the IPv6 subnet (if any)
216
# first argument MAC second IPv6 subnet
217
# Changes global value EUI64
218
get_eui64 () {
219

    
220
  local mac=$1
221
  local prefix=$2
222

    
223
  if [ -z "$prefix" ]; then
224
    EUI64=
225
  else
226
    EUI64=$($MAC2EUI64 $mac $prefix)
227
  fi
228

    
229
}
230

    
231

    
232
# DDNS related functions
233

    
234
# ommit zone statement
235
# nsupdate  will attempt determine the correct zone to update based on the rest of the input
236
send_command () {
237

    
238
  local command="$1"
239
  $SNF_NETWORK_LOG dnshook "$command"
240
  nsupdate -k $KEYFILE > /dev/null << EOF
241
  server $SERVER
242
  $command
243
  send
244
EOF
245

    
246
}
247

    
248

    
249
update_arecord () {
250

    
251
  local action=$1
252
  local command=
253
  if [ -n "$IP" ]; then
254
    command="update $action $GANETI_INSTANCE_NAME.$FZONE $TTL A $IP"
255
    send_command "$command"
256
  fi
257

    
258
}
259

    
260

    
261
update_aaaarecord () {
262

    
263
  local action=$1
264
  local command=
265
  if [ -n "$EUI64" ]; then
266
    command="update $action $GANETI_INSTANCE_NAME.$FZONE $TTL AAAA $EUI64"
267
    send_command "$command"
268
  fi
269

    
270
}
271

    
272

    
273
update_ptrrecord () {
274

    
275
  local action=$1
276
  local command=
277
  if [ -n "$IP" ]; then
278
    command="update $action $RLPART.$RZONE. $TTL PTR $GANETI_INSTANCE_NAME.$FZONE"
279
    send_command "$command"
280
  fi
281

    
282
}
283

    
284
update_ptr6record () {
285

    
286
  local action=$1
287
  local command=
288
  if [ -n "$EUI64" ]; then
289
    command="update $action $R6LPART$R6ZONE. $TTL PTR $GANETI_INSTANCE_NAME.$FZONE"
290
    send_command "$command"
291
  fi
292

    
293
}
294

    
295
update_all () {
296

    
297
  local action=$1
298
  update_arecord $action
299
  update_aaaarecord $action
300
  update_ptrrecord $action
301
  update_ptr6record $action
302

    
303
}
304

    
305

    
306
# first argument is an eui64 (IPv6)
307
# sets GLOBAL args R6REC, R6ZONE, R6LPART
308
# lets assume eui64=2001:648:2ffc:1::1
309
# the following commands produce:
310
# R6REC=1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.c.f.f.2.8.4.6.0.1.0.0.2.ip6.arpa
311
# R6ZONE=1.0.0.0.c.f.f.2.8.4.6.0.1.0.0.2.ip6.arpa
312
# R6LPART=1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.
313
get_rev6_info () {
314

    
315
  local eui64=$1
316
  if [ -z "$eui64" ]; then
317
    R6REC= ; R6ZONE= ; R6LPART= ;
318
  else
319
    R6REC=$(host $eui64 | egrep -o '([[:alnum:]]\.){32}ip6.arpa' )
320
    R6ZONE=$(echo $R6REC | awk -F. 'BEGIN{rpart="";} { for (i=32;i>16;i=i-1) rpart=$i "." rpart; } END{print rpart "ip6.arpa";}')
321
    R6LPART=$(echo $R6REC | awk -F. 'BEGIN{lpart="";} { for (i=16;i>0;i=i-1) lpart=$i "." lpart; } END{print lpart;}')
322
  fi
323

    
324
}
325

    
326

    
327
# first argument is an ipv4
328
# sets args RZONE, RLPART
329
# lets assume IP=203.0.113.1
330
# RZONE="113.0.203.in-add.arpa"
331
# RLPART="1"
332
get_rev4_info () {
333

    
334
  local ip=$1
335
  if [ -z "$ip" ]; then
336
    RZONE= ; RLPART= ;
337
  else
338
    OLDIFS=$IFS
339
    IFS=". "
340
    set -- $ip
341
    a=$1 ; b=$2; c=$3; d=$4;
342
    IFS=$OLDIFS
343
    RZONE="$c.$b.$a.in-addr.arpa"
344
    RLPART="$d"
345
  fi
346

    
347
}
348

    
349

    
350
# Query nameserver for entries related to the specific instance
351
# An example output is the following:
352
# www.google.com has address 173.194.113.114
353
# www.google.com has address 173.194.113.115
354
# www.google.com has address 173.194.113.116
355
# www.google.com has address 173.194.113.112
356
# www.google.com has address 173.194.113.113
357
# www.google.com has IPv6 address 2a00:1450:4001:80b::1012
358
query_dns () {
359

    
360
  HOSTQ="host -s -R 3 -W 3"
361
  HOST_IP_ALL=$($HOSTQ $GANETI_INSTANCE_NAME.$FZONE $SERVER | sed -n 's/.*has address //p')
362
  HOST_IP6_ALL=$($HOSTQ $GANETI_INSTANCE_NAME.$FZONE $SERVER | sed -n 's/.*has IPv6 address //p')
363

    
364
}