2 # VSYS script to configure per-slice virtual network interfaces from the root slice
3 # Thom Haddow - 06/10/09
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
20 vsys_config_dir = "/etc/planetlab/vsys-attributes"
22 if len(sys.argv) != 2: sys.exit(1)
24 # VSYS scripts get slicename as $1
26 sliceid = pwd.getpwnam(slicename).pw_uid
28 netblock_config=os.path.join(vsys_config_dir,slicename,"vsys_vnet")
30 # Read netblock allocation file
33 for netblock in open(netblock_config,'r'):
34 base, mask = netblock.split('/')
37 print >>sys.stderr, "Could not find entry for slice %s in netblock config file %s" % (slicename, netblock_config)
41 ### Read args from stdin
42 arglines = map(string.strip, sys.stdin.readlines())
45 print >>sys.stderr, "Insufficient argument lines."
48 vif = arglines[0] # interface name
49 vip = arglines[1] # IP
50 vmask = int(arglines[2]) # netmask as int
54 options = arglines[3:]
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]
64 # Validate interface name
66 print >>sys.stderr, "Interface name %s invalid"%(vif)
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)
75 # Validate requested IP and convert to int format.
77 vip_int = struct.unpack('!L',socket.inet_aton(vip))[0]
79 print >>sys.stderr, "Invalid IP: %s" % vip
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)
87 # TODO. Check IP is not in use?
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)
95 print >>sys.stderr, "Requested netmask /%d larger than allocation /%d" %(vmask, mask)
102 opt_txqueuelen = None
106 for optionline in options:
107 if len(optionline)==0: continue
109 opt, val = optionline.split('=')
111 print >>sys.stderr, "Bad option line: \"%s\"" % (optionline)
120 print >>sys.stderr, "rp_filter value invalid: \"%s\"" % (val)
123 elif opt=="txqueuelen":
125 if intval<1 or intval>10000:
126 print >>sys.stderr, "txqueuelen value %s out of range 1-10000" % (val)
128 opt_txqueuelen = intval
131 print >>sys.stderr, "Unknown option: \"%s\"" % (opt)
136 ### Configure interface
138 if opt_txqueuelen is None:
139 cmd_ifconfig = "/sbin/ifconfig %s %s/%d" % (vif, vip, vmask)
141 cmd_ifconfig = "/sbin/ifconfig %s %s/%d txqueuelen %d" % (vif, vip, vmask, opt_txqueuelen)
143 os.system(cmd_ifconfig)
145 # Add iptables rules (Clearing old ones first, if they exist)
146 cmd_iptables_in = "/sbin/iptables -A INPUT -i %s -m mark ! --mark %d -j DROP" % (vif, sliceid)
147 cmd_iptables_del_in = "/sbin/iptables -D INPUT -i %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
148 cmd_iptables_out = "/sbin/iptables -A OUTPUT -o %s -m mark ! --mark %d -j DROP" % (vif, sliceid)
149 cmd_iptables_del_out = "/sbin/iptables -D OUTPUT -o %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
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)
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)