2 # VSYS script to ifdown 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>)
15 if len(sys.argv) != 2: sys.exit(1)
17 # VSYS scripts get slicename as $1
19 sliceid = pwd.getpwnam(slicename).pw_uid
23 vif = sys.stdin.readline().strip() # interface name
26 # Validate interface name
28 print >>sys.stderr, "Interface name %s invalid"%(vif)
31 if re.match(r'(tun|tap)%d-\d+' % sliceid, vif ) is None:
32 print >>sys.stderr, "Interface name %s does not match slice id %d."%(vif, sliceid)
37 # Remove iptables rules
38 public_src = os.popen("ifconfig | grep $(ip route | grep default | awk '{print $3}' | awk -F. '{print $1\"[.]\"$2}') | head -1 | awk '{print $2}' | awk -F : '{print $2}'").read().rstrip()
39 cmd_iptables_del_in = "/sbin/iptables -D INPUT -i %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
40 cmd_iptables_del_out = "/sbin/iptables -D OUTPUT -o %s -m mark ! --mark %d -j DROP 2>/dev/null" % (vif, sliceid)
41 cmd_iptables_del_pr = "/sbin/iptables -t nat -D POSTROUTING -s $(ip ro | grep 'dev %s' | head -1 | awk '{print $1}') -j SNAT --to-source %s --random" % (vif, public_src,)
43 os.system(cmd_iptables_del_in)
44 os.system(cmd_iptables_del_out)
45 os.system(cmd_iptables_del_pr)
47 # Bring interface down
48 cmd_ifconfig = "/sbin/ifconfig %s down" % (vif)
49 os.system(cmd_ifconfig)