Statistics
| Branch: | Tag: | Revision:

root / common.sh @ 838c1835

History | View | Annotate | Download (8.2 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 FORWARD -o $INTERFACE -j $TO
45
  #runlocked $RUNLOCKED_OPTS ebtables -D OUTPUT -o $INTERFACE -j $TO
46

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

    
51

    
52
function clear_nfdhcpd {
53

    
54
  rm $NFDHCPD_STATE_DIR/$INTERFACE
55

    
56
}
57

    
58

    
59
function routed_setup_ipv4 {
60

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

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

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

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

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

    
78
}
79

    
80
function send_garp {
81

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

    
91
}
92

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

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

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

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

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

    
115
}
116

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

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

    
146
function init_ebtables {
147

    
148
  runlocked $RUNLOCKED_OPTS ebtables -N $FROM
149
  runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -i $INTERFACE -j $FROM
150
  runlocked $RUNLOCKED_OPTS ebtables -N $TO
151
  runlocked $RUNLOCKED_OPTS ebtables -A FORWARD -o $INTERFACE -j $TO
152

    
153
}
154

    
155

    
156
function setup_ebtables {
157

    
158
  # do not allow changes in ip-mac pair
159
  if [ -n "$IP"]; then
160
    runlocked $RUNLOCKED_OPTS ebtables -A $FROM --ip-source \! $IP -p ipv4 -j DROP
161
  fi
162
  runlocked $RUNLOCKED_OPTS ebtables -A $FROM -s \! $MAC -j DROP
163
  #accept dhcp responses from host (nfdhcpd)
164
  runlocked $RUNLOCKED_OPTS ebtables -A $TO -p ipv4 --ip-protocol=udp  --ip-destination-port=68 -j ACCEPT
165
  # allow only packets from the same mac prefix
166
  runlocked $RUNLOCKED_OPTS ebtables -A $TO -s \! $MAC/$MAC_MASK -j DROP
167
}
168

    
169
function setup_masq {
170

    
171
  # allow packets from/to router (for masquerading)
172
  # runlocked $RUNLOCKED_OPTS ebtables -A $TO -s $NODE_MAC -j ACCEPT
173
  # runlocked $RUNLOCKED_OPTS ebtables -A INPUT -i $INTERFACE -j $FROM
174
  # runlocked $RUNLOCKED_OPTS ebtables -A OUTPUT -o $INTERFACE -j $TO
175
  return
176

    
177
}
178

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

    
198
}
199

    
200
function get_uplink {
201

    
202
  local table=$1
203
  local version=$2
204
  UPLINK=$(ip "$version" route list table "$table" | grep "default via" | awk '{print $5}')
205

    
206
}
207

    
208
# Because we do not have IPv6 value in our environment
209
# we caclulate it based on the NIC's MAC and the IPv6 subnet (if any)
210
# first argument MAC second IPv6 subnet
211
# Changes global value EUI64
212
get_eui64 () {
213

    
214
  local mac=$1
215
  local prefix=$2
216

    
217
  if [ -z "$prefix" ]; then
218
    EUI64=
219
  else
220
    EUI64=$($MAC2EUI64 $mac $prefix)
221
  fi
222

    
223
}
224

    
225

    
226
# DDNS related functions
227

    
228
# ommit zone statement
229
# nsupdate  will attempt determine the correct zone to update based on the rest of the input
230
send_command () {
231

    
232
  local command="$1"
233
  $SNF_NETWORK_LOG $0 "$command"
234
  nsupdate -k $KEYFILE > /dev/null << EOF
235
  server $SERVER
236
  $command
237
  send
238
EOF
239

    
240
}
241

    
242

    
243
update_arecord () {
244

    
245
  local action=$1
246
  local command=
247
  if [ -n "$IP" ]; then
248
    command="update $action $GANETI_INSTANCE_NAME.$FZONE $TTL A $IP"
249
    send_command "$command"
250
  fi
251

    
252
}
253

    
254

    
255
update_aaaarecord () {
256

    
257
  local action=$1
258
  local command=
259
  if [ -n "$EUI64" ]; then
260
    command="update $action $GANETI_INSTANCE_NAME.$FZONE $TTL AAAA $EUI64"
261
    send_command "$command"
262
  fi
263

    
264
}
265

    
266

    
267
update_ptrrecord () {
268

    
269
  local action=$1
270
  local command=
271
  if [ -n "$IP" ]; then
272
    command="update $action $RLPART.$RZONE. $TTL PTR $GANETI_INSTANCE_NAME.$FZONE"
273
    send_command "$command"
274
  fi
275

    
276
}
277

    
278
update_ptr6record () {
279

    
280
  local action=$1
281
  local command=
282
  if [ -n "$EUI64" ]; then
283
    command="update $action $R6LPART$R6ZONE. $TTL PTR $GANETI_INSTANCE_NAME.$FZONE"
284
    send_command "$command"
285
  fi
286

    
287
}
288

    
289
update_all () {
290

    
291
  local action=$1
292
  update_arecord $action
293
  update_aaaarecord $action
294
  update_ptrrecord $action
295
  update_ptr6record $action
296

    
297
}
298

    
299

    
300
# first argument is an eui64 (IPv6)
301
# sets GLOBAL args R6REC, R6ZONE, R6LPART
302
# lets assume eui64=2001:648:2ffc:1::1
303
# the following commands produce:
304
# 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
305
# R6ZONE=1.0.0.0.c.f.f.2.8.4.6.0.1.0.0.2.ip6.arpa
306
# R6LPART=1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.
307
get_rev6_info () {
308

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

    
318
}
319

    
320

    
321
# first argument is an ipv4
322
# sets args RZONE, RLPART
323
# lets assume IP=203.0.113.1
324
# RZONE="113.0.203.in-add.arpa"
325
# RLPART="1"
326
get_rev4_info () {
327

    
328
  local ip=$1
329
  if [ -z "$ip" ]; then
330
    RZONE= ; RLPART= ;
331
  else
332
    OLDIFS=$IFS
333
    IFS=". "
334
    set -- $ip
335
    a=$1 ; b=$2; c=$3; d=$4;
336
    IFS=$OLDIFS
337
    RZONE="$c.$b.$a.in-addr.arpa"
338
    RLPART="$d"
339
  fi
340

    
341
}
342

    
343

    
344
# Query nameserver for entries related to the specific instance
345
# An example output is the following:
346
# www.google.com has address 173.194.113.114
347
# www.google.com has address 173.194.113.115
348
# www.google.com has address 173.194.113.116
349
# www.google.com has address 173.194.113.112
350
# www.google.com has address 173.194.113.113
351
# www.google.com has IPv6 address 2a00:1450:4001:80b::1012
352
query_dns () {
353

    
354
  HOSTQ="host -s -R 3 -W 3"
355
  HOST_IP_ALL=$($HOSTQ $GANETI_INSTANCE_NAME.$FZONE $SERVER | sed -n 's/.*has address //p')
356
  HOST_IP6_ALL=$($HOSTQ $GANETI_INSTANCE_NAME.$FZONE $SERVER | sed -n 's/.*has IPv6 address //p')
357

    
358
}