Checking in some recent tun/tap changes.
[vsys-scripts.git] / vif_up
1 #!/usr/bin/python
2 # VSYS script to configure per-slice virtual network interfaces from the root slice
3 #   Thom Haddow - 06/10/09
4 #
5 # Gets slice name as argv[1]
6 # Takes remaining args on stdin:
7 #   - Interface name (eg [tun|tap]<sliceid>-<n>)
8 #   - IP address (eg 1.2.3.4)
9 #   - Netmask (as int, e.g. 24)
10
11 import sys
12 import pwd
13 import re
14 import socket
15 import struct
16 import os
17
18 vsys_config_dir = "/etc/planetlab/vsys-attributes"
19
20 if len(sys.argv) != 2: sys.exit(1)
21
22 # VSYS scripts get slicename as $1
23 slicename=sys.argv[1]
24 sliceid = pwd.getpwnam(slicename).pw_uid
25
26 netblock_config=os.path.join(vsys_config_dir,slicename,"vsys_vnet")
27
28 # Read netblock allocation file
29 base = None
30
31 for netblock in open(netblock_config,'r'):
32     base, mask = netblock.split('/')
33
34 if base is None:
35     print >>sys.stderr, "Could not find entry for slice %s in netblock config file %s" % (slicename, netblock_config)
36     sys.exit(1)
37
38
39 ### Read args from stdin
40 vif = sys.stdin.readline().strip() # interface name
41 vip = sys.stdin.readline().strip() # IP
42 vmask = int(sys.stdin.readline().strip()) # netmask as int
43
44 # TODO further config args? txqueue, nat? Can add these later...
45
46
47
48 # Convert network base addr to int format by unpacking as 32bit net-ordered long (!L)
49 base_int = struct.unpack('!L',socket.inet_aton(base))[0]
50 mask = int(mask)
51
52
53 ### Validate args
54 # Validate interface name
55 if len(vif)>16:
56     print >>sys.stderr, "Interface name %s invalid"%(vif)
57     sys.exit(1)
58
59 if re.match(r'(tun|tap)%d-\d+' % sliceid, vif ) is None:
60     print >>sys.stderr, "Interface name %s does not match slice id %d."%(vif, sliceid)
61     sys.exit(1)
62
63
64
65 # Validate requested IP and convert to int format.
66 try:
67     vip_int = struct.unpack('!L',socket.inet_aton(vip))[0]
68 except socket.error:
69     print >>sys.stderr, "Invalid IP: %s" % vip
70     sys.exit(1)
71
72 # Check IP is in netblock
73 if (vip_int>>(32-mask)) != (base_int>>(32-mask)):
74     print >>sys.stderr, "Requested IP %s not in netblock %s/%d" % (vip,base,mask)
75     sys.exit(1)
76
77 # TODO. Check IP is not in use?
78
79 # Validate mask: Check requested mask is sane and within our netblock
80 if vmask>32 or vmask <8:
81     print >>sys.stderr, "Requested netmask /%d is invalid" %(vmask)
82     sys.exit(1)
83     
84 if vmask<mask:
85     print >>sys.stderr, "Requested netmask /%d larger than allocation /%d" %(vmask, mask)
86     sys.exit(1)
87
88
89
90 ### Configure interface
91
92 cmd_ifconfig = "/sbin/ifconfig %s %s/%d" % (vif, vip, vmask)
93 os.system(cmd_ifconfig)
94
95 # Add iptables rules (Clearing old ones first, if they exist)
96 cmd_iptables_in = "/sbin/iptables -A INPUT -i %s -m mark ! --mark %d -j DROP" % (vif, sliceid)
97 cmd_iptables_del_in = "/sbin/iptables -D INPUT -i %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
98 cmd_iptables_out = "/sbin/iptables -A OUTPUT -o %s -m mark ! --mark %d -j DROP" % (vif, sliceid)
99 cmd_iptables_del_out = "/sbin/iptables -D OUTPUT -o %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
100
101 os.system(cmd_iptables_del_in)
102 os.system(cmd_iptables_in)
103 os.system(cmd_iptables_del_out)
104 os.system(cmd_iptables_out)