Debian Codegraph @

src/freebsd-virtualbox-guest-wlan-workaround

raw
1 #!/usr/bin/env python3
2 # Work-around for VirtualBox FreeBSD guest issue with IPv6 over WiFi
3 # See https://www.virtualbox.org/ticket/22153
4
5 import ipaddress
6 import netifaces
7 import subprocess
8
9
10 queryDefaultRoute = subprocess.run('netstat -rn | grep "^default .* fe80::" | sed -r "s/[ ]+/ /g" | head -n1', shell=True, check=True, capture_output=True)
11 route = queryDefaultRoute.stdout.decode('ascii').strip().split(' ')
12 if len(route) != 4:
13 sys.stdout.write('ERROR: Did not find link-local IPv6 address of route!\n')
14 sys.exit(1)
15
16 defaultInterface = route[3]
17 defaultGatewayNetwork = ipaddress.IPv6Network(route[1] + '/64', strict = False)
18 defaultGatewayAddress = ipaddress.IPv6Address(route[1])
19 defaultGatewayAddressGlobalAddress = None
20
21 addresses = netifaces.ifaddresses(defaultInterface)
22 if netifaces.AF_INET6 in addresses:
23 for address in addresses[netifaces.AF_INET6]:
24 if ('addr' in address) and ('netmask' in address):
25 m = ipaddress.IPv6Network(address['netmask'])
26 a = ipaddress.IPv6Network(str(address['addr']) + '/' + str(m.prefixlen), strict = False)
27 if not a.is_link_local:
28 defaultGatewayAddressGlobalAddress = \
29 ipaddress.IPv6Address(
30 int(a.network_address) + (int(defaultGatewayAddress) -
31 int(defaultGatewayNetwork.network_address)) )
32
33 if defaultGatewayAddressGlobalAddress == None:
34 sys.stdout.write('ERROR: Did not find global IPv6 address of router!\n')
35 sys.exit(1)
36
37 print("Global router address: " + str(defaultGatewayAddressGlobalAddress))
38 try:
39 subprocess.run([ 'ping', str(defaultGatewayAddressGlobalAddress) ])
40 except:
41 pass