Subversion Repositories configs

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 - 1
# network-functions-ipv6
2
#
3
# Taken from: network-functions-ipv6
4
# (P) & (C) 1997-2005 by Peter Bieringer <pb@bieringer.de>
5
#
6
#  You will find more information on the initscripts-ipv6 homepage at
7
#   http://www.deepspace6.net/projects/initscripts-ipv6.html
8
#
9
# Version: 2006-08-03
10
#
11
#
12
 
13
 
14
 
15
 
16
 
17
 
18
##### Logging function
19
#  $1: <message> : message string
20
#  $2: [stdout|stderr].[err|warn[ing]|inf[o]|notice] : log level with optional channel, default is "stdout.notice"
21
#      [syslog.[facility.].err|warn[ing]|inf[o]|notice : syslog channel, default is "syslog.user.notice"
22
#  $3: <function name> : name of function which calls this log function, can be empty using ""
23
# return code: 0=ok 1=argument error  3=major problem
24
ipv6_log() {
25
	local message="$1"
26
	local level="$2"
27
	local name="$3"
28
 
29
	if [ -z "$message" ]; then
30
		echo $"ERROR: [ipv6_log] Missing 'message' (arg 1)" >/dev/stderr
31
		return 1
32
	fi
33
	if [ -z "$level" ]; then
34
		local level="stdout.notice"
35
	fi
36
 
37
 
38
	# Map loglevel now
39
	local fn=1
40
	local fnawk="print \$$fn"
41
	local t="$(echo $level | awk -F. "{ $fnawk }")"
42
 
43
	# Check channel, if given
44
	case $t in
45
	    'stdout'|'stderr'|'syslog')
46
		local channel="$t"
47
		local fn=$(($fn + 1))
48
		;;
49
	    *)
50
		local channel="stdout"
51
		;;
52
	esac
53
 
54
	# Check syslog facilty, if given
55
	if [ "$channel" = "syslog" ]; then
56
		local fnawk="print \$$fn"
57
		local t="$(echo $level | awk -F. "{ $fnawk }")"
58
		case $t in
59
		    'local0'|'local1'|'local2'|'local3'|'local4'|'local5'|'local6'|'local7'|'daemon')
60
			local facility="$t"
61
			local fn=$(($fn + 1))
62
			;;
63
		    *)
64
			local facility="user"
65
			;;
66
		esac
67
	fi
68
 
69
	local fnawk="print \$$fn"
70
	local t="$(echo $level | awk -F. "{ $fnawk }")"
71
 
72
	# Map priority
73
	[ "$t" = "inf"      ] && local t="info"
74
	[ "$t" = "deb"      ] && local t="debug"
75
	[ "$t" = "warning"  ] && local t="warn"
76
	[ "$t" = "error"    ] && local t="err"
77
	[ "$t" = "critical" ] && local t="crit"
78
 
79
	# Check priority, if given
80
	case $t in
81
	    'info'|'debug'|'notice'|'warn'|'err'|'crit')
82
			local priority="$t"
83
			local fn=$(($fn + 1))
84
			;;
85
		    *)
86
			local priority="notice"
87
			;;
88
	esac
89
 
90
	local fnawk="print \$$fn"
91
	local t="$(echo $level | awk -F. "{ $fnawk }")"
92
	if [ -n "$t" ]; then
93
		echo $"ERROR: [ipv6_log] Loglevel isn't valid '$level' (arg 2)" >/dev/stderr
94
		return 1
95
	fi
96
 
97
	# Generate function text
98
	if [ -z "$name" ]; then
99
		local txt_name=""
100
	else
101
		local txt_name="[$name]"
102
	fi
103
 
104
	# Log message
105
	case $channel in
106
	    'stdout'|'stderr')
107
		# Generate level text
108
		case $priority in
109
		    'debug')
110
			local txt_level=$"DEBUG    "
111
			;;
112
		    'err')
113
			local txt_level=$"ERROR    "
114
			;;
115
		    'warn')
116
			local txt_level=$"WARN     "
117
			;;
118
		    'crit')
119
			local txt_level=$"CRITICAL "
120
			;;
121
		    'info')
122
			local txt_level=$"INFO     "
123
			;;
124
		    'notice')
125
			local txt_level=$"NOTICE   "
126
			;;
127
		esac
128
 
129
		[ -n "$txt_name" ] && local txt_name="$txt_name "
130
 
131
		if [ "$channel" = "stderr" ]; then
132
			echo "$txt_level: ${txt_name}${message}" >/dev/stderr
133
		elif [ "$channel" = "stdout" ]; then
134
			echo "$txt_level: ${txt_name}${message}"
135
		fi
136
		;;
137
	    'syslog')
138
		# note: logger resides in /usr/bin, but not used by default
139
		if ! [ -x /usr/bin/logger ]; then
140
			echo $"ERROR: [ipv6_log] Syslog is chosen, but binary 'logger' doesn't exist or isn't executable" >/dev/stderr
141
			return 3
142
		fi
143
		if [ -z "$txt_name" ]; then
144
			/usr/bin/logger -p $facility.$priority $message
145
		else
146
			/usr/bin/logger -p $facility.$priority -t "$txt_name" "$message"
147
		fi
148
		;;
149
	    *)
150
		echo $"ERROR: [ipv6_log] Cannot log to channel '$channel'" >/dev/stderr
151
		return 3
152
		;;
153
	esac
154
 
155
	return 0
156
}
157
 
158
 
159
###### Beginning of main code here, always executed on "source|. network-functions-ipv6"
160
 
161
 
162
 
163
###### End of main code here
164
 
165
 
166
##### Test for IPv6 capabilites
167
# $1: (optional) testflag: currently supported: "testonly" (do not load a module)
168
# return code: 0=ok 2=IPv6 test fails
169
ipv6_test() {
170
	local fn="ipv6_test"
171
 
172
	local testflag=$1
173
 
174
	if ! [ -f /proc/net/if_inet6 ]; then
175
		if [ "$testflag" = "testonly" ]; then
176
			return 2
177
		else
178
			modprobe ipv6
179
 
180
			if ! [ -f /proc/net/if_inet6 ]; then
181
				#	ipv6_log $"Kernel is not compiled with IPv6 support" crit $fn
182
				return 2
183
			fi
184
		fi
185
	fi
186
 
187
	if ! [ -d /proc/sys/net/ipv6/conf/ ]; then
188
		return 2
189
	fi
190
 
191
	return 0
192
}
193
 
194
##### Static IPv6 route configuration
195
 
196
# Set static IPv6 route
197
#  $1: <IPv6 network> : to route
198
#  $2: <IPv6 gateway> : over which $1 should be routed (if "::", gw will be skipped)
199
#  $3: [<Interface>] : (optional)
200
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem adding route
201
ipv6_add_route() {
202
	local fn="ipv6_add_route"
203
 
204
	local networkipv6=$1
205
	local gatewayipv6=$2
206
	local device=$3		# maybe empty
207
 
208
	if [ -z "$networkipv6" ]; then
209
		ipv6_log $"Missing parameter 'IPv6-network' (arg 1)" err $fn
210
		return 1
211
	fi
212
 
213
	if [ -z "$gatewayipv6" ]; then
214
		ipv6_log $"Missing parameter 'IPv6-gateway' (arg 2)" err $fn
215
		return 1
216
	fi
217
 
218
	ipv6_test || return 2
219
 
220
	ipv6_test_ipv6_addr_valid $networkipv6 || return 2
221
	ipv6_test_ipv6_addr_valid $gatewayipv6 || return 2
222
 
223
	if [ -z "$device" ]; then
224
		local returntxt="$(/sbin/ip -6 route add $networkipv6 via $gatewayipv6 metric 1 2>&1)"
225
	else
226
		if [ "$gatewayipv6" = "::" ]; then
227
			local returntxt="$(/sbin/ip -6 route add $networkipv6 dev $device metric 1 2>&1)"
228
		else
229
			local returntxt="$(/sbin/ip -6 route add $networkipv6 via $gatewayipv6 dev $device metric 1 2>&1)"
230
		fi
231
	fi
232
 
233
	if [ -n "$returntxt" ]; then
234
		if echo $returntxt | LC_ALL=C grep -q "File exists"; then
235
			# Netlink: "File exists"
236
			true
237
		elif echo $returntxt | LC_ALL=C grep -q "No route to host"; then
238
			# Netlink: "No route to host"
239
			ipv6_log $"'No route to host' adding route '$networkipv6' via gateway '$gatewayipv6' through device '$device'" warn $fn
240
			return 3
241
		else
242
			ipv6_log $"Unknown error" warn $fn
243
			return 3
244
		fi
245
	fi
246
 
247
	return 0
248
}
249
 
250
##### automatic tunneling configuration
251
 
252
## Configure automatic tunneling up
253
# return code: 0=ok 2=IPv6 test fails 3=major problem
254
ipv6_enable_autotunnel() {
255
	local fn="ipv6_enable_autotunnel"
256
 
257
	ipv6_test || return 2
258
 
259
	# enable IPv6-over-IPv4 tunnels
260
	if ipv6_test_device_status sit0; then
261
		true
262
	else
263
		# bring up basic tunnel device
264
		/sbin/ip link set sit0 up
265
 
266
			if ! ipv6_test_device_status sit0; then
267
				ipv6_log $"Tunnel device 'sit0' enabling didn't work" err $fn
268
				return 3
269
			fi
270
 
271
		# Set sysctls proper (regardless "default")
272
		/sbin/sysctl -e -w net.ipv6.conf.sit0.forwarding=1 >/dev/null 2>&1
273
		/sbin/sysctl -e -w net.ipv6.conf.sit0.accept_ra=0 >/dev/null 2>&1
274
		/sbin/sysctl -e -w net.ipv6.conf.sit0.accept_redirects=0 >/dev/null 2>&1
275
	fi
276
 
277
	return 0
278
}
279
 
280
##### Interface configuration
281
 
282
## Add an IPv6 address for given interface
283
#  $1: <Interface>
284
#  $2: <IPv6 address[/prefix]>
285
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
286
ipv6_add_addr_on_device() {
287
	local fn="ipv6_add_addr_on_device"
288
 
289
	local device=$1
290
	local address=$2
291
 
292
	if [ -z "$device" ]; then
293
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
294
		return 1
295
	fi
296
 
297
	if [ -z "$address" ]; then
298
		ipv6_log $"Missing parameter 'IPv6-address' (arg 2)" err $fn
299
		return 1
300
	fi
301
 
302
	ipv6_test || return 2
303
 
304
	ipv6_test_ipv6_addr_valid $address || return 1
305
 
306
	ipv6_test_device_status $device
307
	local result=$?
308
 
309
	if [ "$result" = "0" ]; then
310
		true
311
	elif [ "$result" != "11" ]; then
312
		ipv6_log $"Device '$device' doesn't exist" err $fn
313
		return 3
314
	else
315
		/sbin/ip link set $device up
316
 
317
			if ! ipv6_test_device_status $device; then
318
				ipv6_log $"Device '$device' enabling didn't work" err $fn
319
				return 3
320
			fi
321
	fi
322
 
323
	# Extract address parts
324
	local prefixlength_implicit="$(echo $address | awk -F/ '{ print $2 }')"
325
	local address_implicit="$(echo $address | awk -F/ '{ print $1 }')"
326
 
327
	# Check prefix length and using '64' as default
328
	if [ -z "$prefixlength_implicit" ]; then
329
		local prefixlength_implicit="64"
330
		local address="$address_implicit/$prefixlength_implicit"
331
	fi
332
 
333
	/sbin/ip -6 addr add $address dev $device
334
	local result=$?
335
 
336
	if [ $result -eq 2 ]; then
337
		return 0
338
	elif [ $result -ne 0 ]; then
339
		ipv6_log $"Cannot add IPv6 address '$address' on dev '$device'" err $fn
340
		return 3
341
	fi
342
 
343
	return 0
344
}
345
 
346
 
347
## Remove all IPv6 routes and addresses on given interface (cleanup to prevent kernel crashes)
348
#  $1: <Interface>
349
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
350
ipv6_cleanup_device() {
351
	local fn="ipv6_cleanup_device"
352
 
353
	local device=$1
354
 
355
	if [ -z "$device" ]; then
356
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
357
		return 1
358
	fi
359
 
360
	ipv6_test testonly || return 2
361
 
362
	# Remove all IPv6 routes through this device (but not "lo")
363
	if [ "$device" != "lo" ]; then
364
		/sbin/ip -6 route flush dev $device scope global >/dev/null 2>&1
365
		/sbin/ip -6 route flush dev $device scope site   >/dev/null 2>&1
366
	fi
367
 
368
	# Remove all IPv6 addresses on this interface
369
	/sbin/ip -6 addr flush dev $device scope global >/dev/null 2>&1
370
	/sbin/ip -6 addr flush dev $device scope site   >/dev/null 2>&1
371
 
372
	return 0
373
}
374
 
375
 
376
## Remove all IPv6 6to4 related routes and addresses on given interface
377
#  $1: <Interface>
378
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
379
ipv6_cleanup_6to4_device() {
380
	local fn="ipv6_cleanup_6to4_device"
381
 
382
	local device=$1
383
 
384
	if [ -z "$device" ]; then
385
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
386
		return 1
387
	fi
388
 
389
	ipv6_test testonly || return 2
390
 
391
	# Cleanup 6to4 addresses on this device
392
	/sbin/ip -6 addr show dev $dev scope global permanent | LC_ALL=C grep -w inet6 | awk '{ print $2}' | LC_ALL=C grep "^2002:" | while read addr; do
393
		/sbin/ip -6 addr del ${addr} dev ${dev}
394
	done
395
 
396
	# Get all IPv6 routes through given interface related to 6to4 and remove them
397
	/sbin/ip -6 route show dev $device | LC_ALL=C grep "^2002:" | while read ipv6net dummy; do
398
		/sbin/ip -6 route del $ipv6net dev $device
399
	done
400
 
401
	return 0
402
}
403
 
404
 
405
##### Some address test functions
406
 
407
## Test a given IPv6 address for validity
408
#  $1: <IPv6 address>
409
# return code: 0=ok 1=not valid
410
ipv6_test_ipv6_addr_valid() {
411
	ipcalc -cs6 $1
412
}
413
 
414
 
415
## Test a given IPv4 address for validity
416
#  $1: <IPv4 address>
417
# return code: 0=ok 1=not valid
418
ipv6_test_ipv4_addr_valid() {
419
	ipcalc -cs4 $1
420
}
421
 
422
 
423
## Test a given IPv4 address for not a private but unicast one
424
#  $1: <IPv4 address>
425
# return code: 0=ok 1=argument error 10=private or not unicast
426
ipv6_test_ipv4_addr_global_usable() {
427
	local fn="ipv6_test_ipv4_addr_global_usable"
428
 
429
	local testipv4addr_globalusable=$1
430
 
431
 
432
	if [ -z "$testipv4addr_globalusable" ]; then
433
		return 1
434
	fi
435
 
436
	# Test for a globally usable IPv4 address now
437
		# test 0.0.0.0/8
438
		/bin/ipcalc --network $testipv4addr_globalusable 255.0.0.0   | LC_ALL=C grep -q "NETWORK=0\.0\.0\.0"     && return 10
439
		# test 10.0.0.0/8     (RFC 1918 / private)
440
		/bin/ipcalc --network $testipv4addr_globalusable 255.0.0.0   | LC_ALL=C grep -q "NETWORK=10\.0\.0\.0"    && return 10
441
		# test 127.0.0.0/8    (loopback)
442
		/bin/ipcalc --network $testipv4addr_globalusable 255.0.0.0   | LC_ALL=C grep -q "NETWORK=127\.0\.0\.0"   && return 10
443
		# test 169.254.0.0/16 (APIPA / DHCP link local)
444
		/bin/ipcalc --network $testipv4addr_globalusable 255.255.0.0 | LC_ALL=C grep -q "NETWORK=169\.254\.0\.0" && return 10
445
		# test 172.16.0.0/12  (RFC 1918 / private)
446
		/bin/ipcalc --network $testipv4addr_globalusable 255.240.0.0 | LC_ALL=C grep -q "NETWORK=172\.16\.0\.0"  && return 10
447
		# test 192.168.0.0/16 (RFC 1918 / private)
448
		/bin/ipcalc --network $testipv4addr_globalusable 255.255.0.0 | LC_ALL=C grep -q "NETWORK=192\.168\.0\.0" && return 10
449
		# test 224.0.0.0/3    (multicast and reserved, broadcast)
450
		/bin/ipcalc --network $testipv4addr_globalusable 224.0.0.0   | LC_ALL=C grep -q "NETWORK=224\.0\.0\.0"   && return 10
451
 
452
	return 0
453
}
454
 
455
 
456
## Test a given device for status
457
#  $1: <Interface>
458
# return code: 0=ok 1=argument error 10=not exists 11=down
459
ipv6_test_device_status() {
460
	local fn="ipv6_test_device_status"
461
 
462
	local device=$1
463
 
464
	if [ -z "$device" ]; then
465
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
466
		return 1
467
	fi
468
 
469
	# Test if device exists
470
	if [ ! -d "/sys/class/net/${device}" ]; then
471
		# not exists
472
		return 10
473
	fi
474
 
475
	# Test if device is up
476
	if /sbin/ip link show dev $device 2>/dev/null | LC_ALL=C grep -q "UP"; then
477
		# up
478
		return 0
479
	else
480
		# down
481
		return 11
482
	fi
483
}
484
 
485
 
486
## Create 6to4 prefix
487
#  $1: <IPv4 address>
488
# stdout: <6to4address>
489
# return code: 0=ok 1=argument error
490
ipv6_create_6to4_prefix() {
491
	local fn="ipv6_create_6to4_prefix"
492
 
493
	local ipv4addr=$1
494
 
495
	if [ -z "$ipv4addr" ]; then
496
		ipv6_log $"Missing parameter 'IPv4 address' (arg 1)" stderr.err $fn
497
	fi
498
 
499
	local major1="$(echo $ipv4addr | awk -F. '{ print $1 }')"
500
	local minor1="$(echo $ipv4addr | awk -F. '{ print $2 }')"
501
	local major2="$(echo $ipv4addr | awk -F. '{ print $3 }')"
502
	local minor2="$(echo $ipv4addr | awk -F. '{ print $4 }')"
503
 
504
	if [ -z "$major1" -o -z "$minor1" -o -z "$major2" -o -z "$minor2" ]; then
505
		return 1
506
	fi
507
 
508
	if [ $major1 -eq 0 ]; then
509
		local block1="$(printf "%x" $minor1)"
510
	else
511
		local block1="$(printf "%x%02x" $major1 $minor1)"
512
	fi
513
	if [ $major2 -eq 0 ]; then
514
		local block2="$(printf "%x" $minor2)"
515
	else
516
		local block2="$(printf "%x%02x" $major2 $minor2)"
517
	fi
518
 
519
	local prefix6to4="2002:$block1:$block2"
520
 
521
	echo "$prefix6to4"
522
	return 0
523
}
524
 
525
 
526
## Check and create 6to4 tunnel relay address
527
#  $1: <IPv4 address|IPv6to4 address>
528
# stdout: <tunnel relay address>
529
# return code: 0=ok 1=argument error
530
ipv6_create_6to4_relay_address() {
531
	local fn="ipv6_create_6to4_relay_address"
532
 
533
	local addr=$1
534
 
535
	if [ -z "$addr" ]; then
536
		ipv6_log $"Missing parameter 'address' (arg 1)" stderr.err $fn
537
		return 1
538
	fi
539
 
540
	# Check
541
	if ipv6_test_ipv4_addr_valid $addr ; then
542
		# ok, a IPv4 one
543
		if ipv6_test_ipv4_addr_global_usable $addr; then
544
			# IPv4 globally usable
545
			local ipv6to4_relay="::$addr"
546
		else
547
			ipv6_log $"Given address '$addr' is not a global IPv4 one (arg 1)" stderr.err $fn
548
			return 1
549
		fi
550
	else
551
		ipv6_log $"Given address '$addr' is not a valid IPv4 one (arg 1)" stderr.err $fn
552
		return 1
553
	fi
554
 
555
	echo "$ipv6to4_relay"
556
 
557
	return 0
558
}
559
 
560
 
561
##### 6to4 tunneling setup
562
 
563
## Configure 6to4 tunneling up
564
#  $1: <Interface> : only "tun6to4" is supported
565
#  $2: <IPv4 address> : global IPv4 address of interface (will be used to generate 6to4 prefix)
566
#  $3: [<IPv6 suffix>] : for 6to4 prefix (optional, default is "::1")
567
#  $4: [<MTU>] : MTU of tunnel device (optional, default is automatic)
568
#  $5: [<IPv4 address>] : local IPv4 address of tunnel interface (required in case of 6to4 behind NAT)
569
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
570
ipv6_add_6to4_tunnel() {
571
	local fn="ipv6_add_6to4_tunnel"
572
 
573
	local device=$1
574
	local globalipv4=$2
575
	local globalipv6to4suffix=$3
576
	local mtu=$4
577
	local localipv4=$5
578
 
579
	if [ -z "$device" ]; then
580
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
581
		return 1
582
	fi
583
 
584
	if [ -z "$globalipv4" ]; then
585
		ipv6_log $"Missing parameter 'global IPv4 address' (arg 2)" err $fn
586
		return 1
587
	fi
588
 
589
	# Check device
590
	if [ "$device" != "tun6to4" ]; then
591
		ipv6_log $"Given device '$device' is not supported (arg 1)" err $fn
592
		return 1
593
	fi
594
 
595
	# Copy global IPv4 address to local if last one is not given
596
	if [ -z "$localipv4" ]; then
597
		localipv4="$globalipv4"
598
	fi
599
 
600
	ipv6_test || return 2
601
 
602
	# Generate 6to4 address
603
	local prefix6to4="$(ipv6_create_6to4_prefix $globalipv4)"
604
	if [ $? -ne 0 -o -z "$prefix6to4" ]; then
605
		return 3
606
	fi
607
 
608
	if [ -z "$globalipv6to4suffix" ]; then
609
		local address6to4="${prefix6to4}::1/16"
610
	else
611
		local address6to4="${prefix6to4}::${globalipv6to4suffix}/16"
612
	fi
613
 
614
		ipv6_add_tunnel_device tun6to4 0.0.0.0 $address6to4 $localipv4
615
		if [ $? -ne 0 ]; then
616
			local retval=3
617
		else
618
			local retval=0
619
		fi
620
 
621
		# Add unspecific unreachable route for local 6to4 address space
622
		/sbin/ip route add unreach ${prefix6to4}::/48
623
 
624
	# Set MTU, if given
625
	if [ -n "$mtu" ]; then
626
		ipv6_set_mtu $device $mtu
627
	fi
628
 
629
	return $retval
630
}
631
 
632
 
633
## Configure all 6to4 tunneling down
634
#  $1: <Interface> : only "tun6to4" is supported
635
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
636
ipv6_cleanup_6to4_tunnels() {
637
	local fn="ipv6_cleanup_6to4_tunnels"
638
 
639
	local device=$1
640
 
641
	if [ -z "$device" ]; then
642
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
643
		return 1
644
	fi
645
 
646
	# Check device
647
	if [ "$device" != "tun6to4" ]; then
648
		ipv6_log $"Given device '$device' is not supported (arg 1)" err $fn
649
		return 1
650
	fi
651
 
652
	ipv6_test testonly || return 2
653
 
654
		ipv6_del_tunnel_device tun6to4
655
 
656
		# Remove all unspecific unreachable routes for local 6to4 address space
657
		/sbin/ip -6 route | LC_ALL=C grep "^unreachable 2002:" | LC_ALL=C grep "/48 dev lo" | while read token net rest; do
658
			/sbin/ip route del unreach $net
659
		done
660
 
661
	return 0
662
}
663
 
664
 
665
## Configure 6to4 tunneling down
666
#  $1: <Interface> : only "tun6to4" is supported
667
#  $2: <IPv4 address> : global address of local interface
668
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
669
ipv6_del_6to4_tunnel() {
670
	local fn="ipv6_del_6to4_tunnel"
671
 
672
	local device=$1
673
	local localipv4=$2
674
 
675
	if [ -z "$device" ]; then
676
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
677
		return 1
678
	fi
679
 
680
	if [ -z "$localipv4" ]; then
681
		ipv6_log $"Missing parameter 'local IPv4 address' (arg 2)" err $fn
682
		return 1
683
	fi
684
 
685
	# Check device
686
	if [ "$device" != "tun6to4" ]; then
687
		ipv6_log $"Given device '$device' is not supported (arg 1)" err $fn
688
		return 1
689
	fi
690
 
691
	ipv6_test || return 2
692
 
693
		ipv6_del_tunnel_device tun6to4
694
		local retval=$?
695
 
696
		# Remove unspecific unreachable route for local 6to4 address space
697
		/sbin/ip route del unreach ${prefix6to4}::/48
698
 
699
	return $retval
700
}
701
 
702
 
703
## Configure a static tunnel device up
704
#  $1: <Interface>
705
#  $2: <IPv4 address> : of foreign tunnel
706
#  $3: [<IPv6 address>] : local one of a P-t-P tunnel (optional)
707
#  $4: [<IPv4 address>] : local one of tunnel (optional)
708
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
709
ipv6_add_tunnel_device() {
710
	local fn="ipv6_add_tunnel_device"
711
 
712
	local device=$1
713
	local addressipv4tunnel=$2
714
	local addressipv6local=$3
715
	local addressipv4tunnellocal=$4
716
 
717
	if [ -z "$device" ]; then
718
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
719
		return 1
720
	fi
721
 
722
	if [ -z "$addressipv4tunnel" ]; then
723
		ipv6_log $"Missing parameter 'IPv4-tunnel address' (arg 2)" err $fn
724
		return 1
725
	fi
726
 
727
	if [ -z "$addressipv4tunnellocal" ]; then
728
		local addressipv4tunnellocal="any"
729
	fi
730
 
731
	ipv6_test || return 2
732
 
733
	if ! ipv6_test_device_status $device; then
734
		local ttldefault="$(/sbin/sysctl -e net.ipv4.ip_default_ttl | awk '{ print $3 }')"
735
		if [ -z "$ttldefault" ]; then
736
			local ttldefault=64
737
		fi
738
 
739
		# Test whether remote IPv4 address was already applied to another tunnel
740
		if [ "$addressipv4tunnel" != "0.0.0.0" -a "$addressipv4tunnel" != "any" ]; then
741
			/sbin/ip tunnel show remote $addressipv4tunnel 2>/dev/null | LC_ALL=C grep -w "ipv6/ip" | while IFS=":" read devnew rest; do
742
				if [ "$devnew" != "$device" ]; then
743
					ipv6_log $"Given remote address '$addressipv4tunnel' on tunnel device '$device' is already configured on device '$devnew'" err $fn
744
					return 3
745
				fi
746
			done
747
		fi
748
 
749
		/sbin/ip tunnel add $device mode sit ttl $ttldefault remote $addressipv4tunnel local $addressipv4tunnellocal
750
		if [ $? -ne 0 ]; then
751
			return 3
752
		fi
753
 
754
		# Test, whether "ip tunnel show" reports valid content
755
		if ! /sbin/ip tunnel show $device 2>/dev/null | LC_ALL=C grep -q -w "remote"; then
756
			ipv6_log $"Tunnel device '$device' creation didn't work" err $fn
757
			return 3
758
		fi
759
 
760
		/sbin/ip link set $device up
761
 
762
		if ! ipv6_test_device_status $device; then
763
			ipv6_log $"Tunnel device '$device' bringing up didn't work" err $fn
764
			return 3
765
		fi
766
 
767
		# Set sysctls proper (regardless "default")
768
		/sbin/sysctl -e -w net.ipv6.conf.$SYSCTLDEVICE.forwarding=1 >/dev/null 2>&1
769
		/sbin/sysctl -e -w net.ipv6.conf.$SYSCTLDEVICE.accept_ra=0 >/dev/null 2>&1
770
		/sbin/sysctl -e -w net.ipv6.conf.$SYSCTLDEVICE.accept_redirects=0 >/dev/null 2>&1
771
 
772
		if [ -n "$addressipv6local" ]; then
773
			# Setup P-t-P address
774
			ipv6_add_addr_on_device $device $addressipv6local
775
			if [ $? -ne 0 ]; then
776
				return 3
777
			fi
778
		fi
779
	else
780
		false
781
	fi
782
 
783
	return 0
784
}
785
 
786
 
787
## Configure a static tunnel device down
788
#  $1: <Interface>
789
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
790
ipv6_del_tunnel_device() {
791
	local fn="ipv6_del_tunnel_device"
792
 
793
	local device=$1
794
 
795
	if [ -z "$device" ]; then
796
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
797
		return 1
798
	fi
799
 
800
	ipv6_test testonly || return 2
801
 
802
	if ipv6_test_device_status $device; then
803
		ipv6_cleanup_device $device
804
	else
805
		if [ "$device" != "sit0" ]; then
806
			false
807
		fi
808
	fi
809
 
810
	if [ "$device" != "sit0" ]; then
811
		if /sbin/ip tunnel show $device 2>/dev/null | LC_ALL=C grep -q -w "ipv6/ip"; then
812
			/sbin/ip tunnel del $device
813
 
814
				if ipv6_test_device_status $device; then
815
					return 3
816
				fi
817
		else
818
			false
819
		fi
820
	fi
821
 
822
	return 0
823
}
824
 
825
 
826
## Cleanup all dedicated tunnel devices
827
ipv6_cleanup_tunnel_devices() {
828
	local fn="ipv6_cleanup_tunnel_devices"
829
 
830
	ipv6_test testonly || return 2
831
 
832
	# Find still existing tunnel devices and shutdown and delete them
833
 
834
	/sbin/ip tunnel show | LC_ALL=C grep -w "ipv6/ip" | awk -F: '{ print $1 }' | while read device; do
835
		ipv6_del_tunnel_device $device
836
	done
837
 
838
	return 0
839
}
840
 
841
 
842
## Get address of a dedicated tunnel
843
#  $1: <Interface>
844
#  $2: local|remote : local or remote address
845
# stdout: <IPv4 address> if available
846
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
847
ipv6_get_ipv4addr_of_tunnel() {
848
	local fn="ipv6_get_local_ipv4_of_tunnel"
849
 
850
	local device=$1
851
	local selection=$2
852
 
853
	if [ -z "$device" ]; then
854
		ipv6_log $"Missing parameter 'device' (arg 1)" stderr.err $fn
855
		return 1
856
	fi
857
 
858
	if [ -z "$selection" ]; then
859
		ipv6_log $"Missing parameter 'selection' (arg 2)" stderr.err $fn
860
		return 1
861
	fi
862
	if [ "$selection" != "local" -a "$selection" != "remote" ]; then
863
		ipv6_log $"Unsupported selection '$selection' specified (arg 2)" stderr.err $fn
864
		return 1
865
	fi
866
 
867
	ipv6_test testonly || return 2
868
 
869
	ipv6_test_device_status $device
870
 
871
	if [ $? != 0 -a $? != 11 ]; then
872
		# Device doesn't exist
873
		return 3
874
	fi
875
 
876
	# Device exists, retrieve address
877
	if [ "$selection" = "local" ]; then
878
		local tunnel_local_ipv4addr="$(/sbin/ip tunnel show $device | awk '{ print $6 }')"
879
	elif [ "$selection" = "remote" ]; then
880
		local tunnel_local_ipv4addr="$(/sbin/ip tunnel show $device | awk '{ print $4 }')"
881
	fi
882
 
883
	if [ $? != 0 ]; then
884
		return 3
885
	fi
886
 
887
	if [ "$tunnel_local_ipv4addr" = "any" ]; then
888
		local tunnel_local_ipv4addr="0.0.0.0"
889
	fi
890
 
891
	echo "$tunnel_local_ipv4addr"
892
 
893
	return 0
894
}
895
 
896
 
897
## Get IPv4 address of a device
898
#  $1: <Interface>
899
# stdout: <IPv4 address> if available
900
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem (more than one IPv4 address applied)
901
ipv6_get_ipv4addr_of_device() {
902
	local fn="ipv6_get_ipv4addr_of_device"
903
 
904
	local device=$1
905
 
906
	if [ -z "$device" ]; then
907
		ipv6_log $"Missing parameter 'device' (arg 1)" stderr.err $fn
908
		return 1
909
	fi
910
 
911
	ipv6_test_device_status $device
912
 
913
	if [ $? != 0 -a $? != 11 ]; then
914
		# Device doesn't exist
915
		return 3
916
	fi
917
 
918
	# Device exists, retrieve the first address only
919
	local ipv4addr="$(/sbin/ip -o -4 addr show dev $device | awk '{ print $4 }' | awk -F/ '{ print $1; exit }')"
920
 
921
	if [ $? != 0 ]; then
922
		return 3
923
	fi
924
 
925
	if [ "$ipv4addr" = "any" ]; then
926
		local ipv4addr="0.0.0.0"
927
	fi
928
 
929
	echo "$ipv4addr"
930
 
931
	return 0
932
}
933
 
934
 
935
## Set IPv6 MTU for a device
936
#  $1: <Interface>
937
#  $2: <IPv6 MTU>
938
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
939
ipv6_set_mtu() {
940
	local fn="ipv6_set_mtu"
941
 
942
	local device=$1
943
	local ipv6_mtu=$2
944
 
945
	if [ -z "$device" ]; then
946
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
947
		return 1
948
	fi
949
 
950
	if [ -z "$ipv6_mtu" ]; then
951
		ipv6_log $"Missing parameter 'IPv6 MTU' (arg 2)" err $fn
952
		return 1
953
	fi
954
 
955
	# Check range
956
	if [ $ipv6_mtu -lt 1280 -o $ipv6_mtu -gt 65535 ]; then
957
		ipv6_log $"Given IPv6 MTU '$ipv6_mtu' is out of range" err $fn
958
		return 1
959
	fi
960
 
961
	ipv6_test testonly || return 2
962
 
963
	# Set value
964
	/sbin/ip link set dev $device mtu $ipv6_mtu
965
 
966
	return 0
967
}
968
 
969
 
970
## Set a default route
971
#  $1: <IPv6 address> : gateway, can also contain scope suffix (device name), cause a warning if not matching with $2 (but will have precedence)
972
#  $2: <gateway device>: gateway device (optional in case of $1 is a global address or $1 contains scope suffix)
973
#  $3: <check device>: (optional) device to check scope and gateway device against (setup is skipped, if not matching)
974
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
975
ipv6_set_default_route() {
976
	local fn="ipv6_set_default_route"
977
 
978
	local address=$1
979
	local device=$2
980
	local device_check=$3
981
 
982
	ipv6_test testonly || return 2
983
 
984
	# Map the unspecified address to nothing
985
	if [ "$address" = "::" ]; then
986
		local address=""
987
	fi
988
 
989
	if [ -n "$address" ]; then
990
		local addressgw=$(echo $address | awk -F% '{ print $1 }')
991
		local device_scope=$(echo $address | awk -F% '{ print $2 }')
992
 
993
		if [ -z "$addressgw" ]; then
994
			ipv6_log $"Given IPv6 default gateway '$address' is not in proper format" err $fn
995
			return 3
996
		fi
997
 
998
		# Scope device has precedence
999
		if [ -n "$device_scope" -a -n "$device" -a "$device_scope" != "$device" ]; then
1000
			ipv6_log $"Given IPv6 default gateway '$address' has scope '$device_scope' defined, given default gateway device '$device' will be not used" inf $fn
1001
			local device=""
1002
		fi
1003
 
1004
		# Link local addresses require a device
1005
		if echo $addressgw | LC_ALL=C grep -qi "^fe80:"; then
1006
			if [ -z "$device_scope" ]; then
1007
				if [ -z "$device" ]; then
1008
					ipv6_log $"Given IPv6 default gateway '$address' is link-local, but no scope or gateway device is specified" err $fn
1009
					return 3
1010
				fi
1011
			fi
1012
		fi
1013
 
1014
		# Check whether the route belongs to the specific given interface
1015
		if [ -n "$device_check" ]; then
1016
			# Check whether scope device matches given check device
1017
			if [ -n "$device_scope" -a "$device_check" != "$device_scope" ]; then
1018
				# scope device != specific given -> skip
1019
				return 0
1020
			elif [ -n "$device" -a "$device_check" != "$device" ]; then
1021
				# gateway device != specific given -> skip
1022
				return 0
1023
			fi
1024
		fi
1025
 
1026
		# Set device now, if not given
1027
		if [ -z "$device" ]; then
1028
			local device="$device_scope"
1029
		fi
1030
 
1031
		if [ -z "$device" ]; then
1032
			# Note: this can cause a warning and a not installed route, if given address is not reachable on the link
1033
			ipv6_add_route ::/0 $addressgw
1034
		else
1035
			ipv6_add_route ::/0 $addressgw $device
1036
		fi
1037
	elif [ -n "$device" ]; then
1038
		# Check whether the route belongs to the specific given interface
1039
		if [ -n "$device_check" -a "$device_check" != "$device" ]; then
1040
			# gateway device != specific given -> skip
1041
			return 0
1042
		fi
1043
 
1044
		ipv6_test_route_requires_next_hop $device
1045
		local result=$?
1046
 
1047
		if [ $result = 0 ]; then
1048
			ipv6_log $"Given IPv6 default device '$device' requires an explicit nexthop" err $fn
1049
			return 3
1050
		elif [ $result != 10 ]; then
1051
			ipv6_log $"Given IPv6 default device '$device' doesn't exist or isn't up" err $fn
1052
			return 3
1053
		fi
1054
 
1055
		ipv6_add_route ::/0 :: $device
1056
	else
1057
		ipv6_log $"No parameters given to setup a default route" err $fn
1058
		return 3
1059
	fi
1060
 
1061
	return 0
1062
}
1063
 
1064
 
1065
## Resolve need of explicit next hop for an interface
1066
#  $1: <Interface>
1067
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem 10=needs no explicit hop
1068
ipv6_test_route_requires_next_hop() {
1069
	local fn="ipv6_test_route_requires_next_hop"
1070
 
1071
	local device=$1
1072
 
1073
	if [ -z "$device" ]; then
1074
		ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
1075
		return 1
1076
	fi
1077
 
1078
	ipv6_test testonly || return 2
1079
 
1080
	ipv6_test_device_status $device
1081
 
1082
	if [ $? != 0 ]; then
1083
		return 3
1084
	fi
1085
 
1086
	if [ "$device" = "sit0" ]; then
1087
		return 10
1088
	fi
1089
 
1090
	if /sbin/ip -o link show $device 2>/dev/null |  LC_ALL=C grep -q "POINTOPOINT"; then
1091
		return 10
1092
	fi
1093
 
1094
	return 0
1095
}
1096
 
1097
 
1098
## Trigger radvd
1099
#  $1: up|down : device reason for triggering (coming up or going down)
1100
#  $2: [startstop|restart|reload|SIGHUP] : triger mechanism (default is "SIGHUP")
1101
#        "startstop" : reason=up -> start, reason=down -> stop
1102
#  $3: [<filename>] : alternative pid file  [optional]
1103
# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
1104
ipv6_trigger_radvd() {
1105
	local fn="ipv6_trigger_radvd"
1106
 
1107
	local reason=$1
1108
	local mechanism=$2
1109
	local pidfile=$3
1110
 
1111
	if [ -z "$reason" ]; then
1112
		ipv6_log $"No reason given for sending trigger to radvd" err $fn
1113
		return 1
1114
	fi
1115
 
1116
	if [ "$reason" != "up" -a "$reason" != "down" ]; then
1117
		ipv6_log $"Unsupported reason '$reason' for sending trigger to radvd" err $fn
1118
		return 1
1119
	fi
1120
 
1121
	if [ -z "$mechanism" ]; then
1122
		# Take default
1123
		local mechanism="SIGHUP"
1124
	fi
1125
 
1126
	if [ -z "$pidfile" ]; then
1127
		local pidfile="/var/run/radvd/radvd.pid"
1128
	fi
1129
 
1130
	# Print message and select action
1131
	case $mechanism in
1132
	    'startstop')
1133
		case $reason in
1134
		    up)
1135
			local action="start"
1136
			;;
1137
		    down)
1138
			local action="stop"
1139
			;;
1140
		esac
1141
		;;
1142
	    'reload'|'restart'|'SIGHUP')
1143
		local action="$mechanism"
1144
		;;
1145
	    *)
1146
		ipv6_log $"Unsupported mechanism '$mechanism' for sending trigger to radvd" err $fn
1147
		return 3
1148
		;;
1149
	esac
1150
 
1151
	# PID file needed?
1152
	if [ "$action" = "SIGHUP" ]; then
1153
		if ! [ -f "$pidfile" ]; then
1154
			if [ "$reason" = "down" ]; then
1155
				# be quiet because triggering may have been disabled
1156
				true
1157
			else
1158
				ipv6_log $"Given pidfile '$pidfile' doesn't exist, cannot send trigger to radvd" err $fn
1159
			fi
1160
			return 3
1161
		fi
1162
 
1163
		# Get PID
1164
		local pid="$(cat $pidfile)"
1165
		if [ -z "$pid" ]; then
1166
			# pidfile empty - strange
1167
			ipv6_log $"Pidfile '$pidfile' is empty, cannot send trigger to radvd" err $fn
1168
			return 3
1169
		fi
1170
	fi
1171
 
1172
 
1173
	# Do action
1174
	case $action in
1175
	    'SIGHUP')
1176
		kill -HUP $pid
1177
		;;
1178
	    'reload'|'restart'|'stop'|'start')
1179
			if ! /sbin/chkconfig --list radvd >/dev/null 2>&1; then
1180
				if [ "$reason" = "down" ]; then
1181
					# be quiet because triggering may have been disabled
1182
					true
1183
				else
1184
					ipv6_log $"radvd not (properly) installed, triggering failed" err $fn
1185
				fi
1186
				return 3
1187
			else
1188
				/sbin/service radvd $action >/dev/null 2>&1
1189
			fi
1190
		;;
1191
	    *)
1192
		# Normally not reached, "action" is set above to proper value
1193
		;;
1194
	esac
1195
 
1196
	return 0
1197
}