2829b586db2d5666ee973389c7d650db8a785eca
[vsys-scripts.git] / exec / 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 #   - Followed by options as name=value pairs
11
12 import sys
13 import pwd
14 import re
15 import socket
16 import struct
17 import os
18 import string
19
20 vsys_config_dir = "/etc/planetlab/vsys-attributes"
21
22 if len(sys.argv) != 2: sys.exit(1)
23
24 # VSYS scripts get slicename as $1
25 slicename=sys.argv[1]
26 sliceid = pwd.getpwnam(slicename).pw_uid
27
28 netblock_config=os.path.join(vsys_config_dir,slicename,"vsys_vnet")
29
30 # Read netblock allocation file
31 base = None
32
33 for netblock in open(netblock_config,'r'):
34     base, mask = netblock.split('/')
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 arglines = map(string.strip, sys.stdin.readlines())
43
44 if len(arglines)<3:
45     print >>sys.stderr, "Insufficient argument lines."
46     sys.exit(1)
47
48 vif = arglines[0] # interface name
49 vip = arglines[1] # IP
50 vmask = int(arglines[2]) # netmask as int
51
52 # Create options list
53 if len(arglines)>3:
54     options = arglines[3:]
55 else:
56     options = []
57
58 # Convert network base addr to int format by unpacking as 32bit net-ordered long (!L)
59 base_int = struct.unpack('!L',socket.inet_aton(base))[0]
60 mask = int(mask)
61
62
63 ### Validate args
64 # Validate interface name
65 if len(vif)>16:
66     print >>sys.stderr, "Interface name %s invalid"%(vif)
67     sys.exit(1)
68
69 if re.match(r'(tun|tap)%d-\d+' % sliceid, vif ) is None:
70     print >>sys.stderr, "Interface name %s does not match slice id %d."%(vif, sliceid)
71     sys.exit(1)
72
73
74
75 # Validate requested IP and convert to int format.
76 try:
77     vip_int = struct.unpack('!L',socket.inet_aton(vip))[0]
78 except socket.error:
79     print >>sys.stderr, "Invalid IP: %s" % vip
80     sys.exit(1)
81
82 # Check IP is in netblock
83 if (vip_int>>(32-mask)) != (base_int>>(32-mask)):
84     print >>sys.stderr, "Requested IP %s not in netblock %s/%d" % (vip,base,mask)
85     sys.exit(1)
86
87 # TODO. Check IP is not in use?
88
89 # Validate mask: Check requested mask is sane and within our netblock
90 if vmask>32 or vmask <8:
91     print >>sys.stderr, "Requested netmask /%d is invalid" %(vmask)
92     sys.exit(1)
93     
94 if vmask<mask:
95     print >>sys.stderr, "Requested netmask /%d larger than allocation /%d" %(vmask, mask)
96     sys.exit(1)
97
98
99
100 ### Process options
101
102 opt_txqueuelen = None
103 opt_rp_filter = None
104
105
106 for optionline in options:
107     if len(optionline)==0: continue
108     try:
109         opt, val = optionline.split('=')
110     except:
111         print >>sys.stderr, "Bad option line: \"%s\"" % (optionline)
112         sys.exit(1)
113
114     if opt=="rp_filter":
115         if val=="0":
116             opt_rp_filter="0"
117         elif val=="1":
118             opt_rp_filter="1"
119         else:
120             print >>sys.stderr, "rp_filter value invalid: \"%s\"" % (val)
121             sys.exit(1)
122
123     elif opt=="txqueuelen":
124         intval = int(val)
125         if intval<1 or intval>10000:
126             print >>sys.stderr, "txqueuelen value %s out of range 1-10000" % (val)
127             sys.exit(1)
128         opt_txqueuelen = intval
129
130     else:
131         print >>sys.stderr, "Unknown option: \"%s\"" % (opt)
132         sys.exit(1)
133
134
135         
136 ### Configure interface
137
138 if opt_txqueuelen is None:
139     cmd_ifconfig = "/sbin/ifconfig %s %s/%d" % (vif, vip, vmask)
140 else:
141     cmd_ifconfig = "/sbin/ifconfig %s %s/%d txqueuelen %d" % (vif, vip, vmask, opt_txqueuelen)
142
143 os.system(cmd_ifconfig)
144
145 # Add iptables rules (Clearing old ones first, if they exist)
146 cmd_iptables_in = "/sbin/iptables -A INPUT -i %s -m mark -m state --state NEW ! --mark %d -j DROP" % (vif, sliceid)
147 cmd_iptables_del_in = "/sbin/iptables -D INPUT -i %s -m mark -m state --state NEW ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
148 cmd_iptables_out = "/sbin/iptables -A OUTPUT -o %s -m state --state NEW -m mark ! --mark %d -j DROP" % (vif, sliceid)
149 cmd_iptables_del_out = "/sbin/iptables -D OUTPUT -o %s -m state --state NEW -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
150
151 os.system(cmd_iptables_del_in)
152 os.system(cmd_iptables_in)
153 os.system(cmd_iptables_del_out)
154 os.system(cmd_iptables_out)
155
156 # Process additional options
157 if opt_rp_filter is not None:
158     rp_cmd = "/sbin/sysctl net.ipv4.conf.%s.rp_filter=%s" % (vif, opt_rp_filter)
159     os.system(rp_cmd)