vif_up script from Thom Haddow, for bringing up virtual interfaces
[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 netblock_config="vnetblocks.conf"
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 # Read netblock allocation file
27 base = None
28
29 for line in open(netblock_config,'r'):
30     if line.startswith('#'): continue # skip comments
31     slice, netblock = line.split()
32     if slice == slicename: # found slice's alloc
33         base, mask = netblock.split('/')
34         break
35
36 if base is None:
37     print >>sys.stderr, "Could not find entry for slice %s in netblock config file %s" % (slicename, netblock_config)
38     sys.exit(1)
39
40
41 ### Read args from stdin
42 vif = sys.stdin.readline().strip() # interface name
43 vip = sys.stdin.readline().strip() # IP
44 vmask = int(sys.stdin.readline().strip()) # netmask as int
45
46 # TODO further config args? txqueue, nat? Can add these later...
47
48
49
50 # Convert network base addr to int format by unpacking as 32bit net-ordered long (!L)
51 base_int = struct.unpack('!L',socket.inet_aton(base))[0]
52 mask = int(mask)
53
54
55 ### Validate args
56 # Validate interface name
57 if len(vif)>16:
58     print >>sys.stderr, "Interface name %s invalid"%(vif)
59     sys.exit(1)
60
61 if re.match(r'(tun|tap)%d-\d+' % sliceid, vif ) is None:
62     print >>sys.stderr, "Interface name %s does not match slice id %d."%(vif, sliceid)
63     sys.exit(1)
64
65
66
67 # Validate requested IP and convert to int format.
68 try:
69     vip_int = struct.unpack('!L',socket.inet_aton(vip))[0]
70 except socket.error:
71     print >>sys.stderr, "Invalid IP: %s" % vip
72     sys.exit(1)
73
74 # Check IP is in netblock
75 if (vip_int>>(32-mask)) != (base_int>>(32-mask)):
76     print >>sys.stderr, "Requested IP %s not in netblock %s/%d" % (vip,base,mask)
77     sys.exit(1)
78
79 # TODO. Check IP is not in use?
80
81 # Validate mask: Check requested mask is sane and within our netblock
82 if vmask>32 or vmask <8:
83     print >>sys.stderr, "Requested netmask /%d is invalid" %(vmask)
84     sys.exit(1)
85     
86 if vmask<mask:
87     print >>sys.stderr, "Requested netmask /%d larger than allocation /%d" %(vmask, mask)
88     sys.exit(1)
89
90
91
92 ### Configure interface
93
94 cmd_ifconfig = "/sbin/ifconfig %s %s/%d" % (vif, vip, vmask)
95 os.system(cmd_ifconfig)
96
97 # Add iptables rules (Clearing old ones first, if they exist)
98 cmd_iptables_in = "/sbin/iptables -A INPUT -i %s -m mark ! --mark %d -j DROP" % (vif, sliceid)
99 cmd_iptables_del_in = "/sbin/iptables -D INPUT -i %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
100 cmd_iptables_out = "/sbin/iptables -A OUTPUT -o %s -m mark ! --mark %d -j DROP" % (vif, sliceid)
101 cmd_iptables_del_out = "/sbin/iptables -D OUTPUT -o %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
102
103 os.system(cmd_iptables_del_in)
104 os.system(cmd_iptables_in)
105 os.system(cmd_iptables_del_out)
106 os.system(cmd_iptables_out)