17 tun_path = '/dev/net/tun'
18 hostaddr = socket.gethostbyname(socket.gethostname())
20 usage = "usage: %prog [options] <remote-endpoint>"
22 parser = optparse.OptionParser(usage=usage)
25 "-i", "--iface", dest="tun_name", metavar="DEVICE",
27 help = "TUN/TAP interface to tap into")
29 "-d", "--tun-path", dest="tun_path", metavar="PATH",
30 default = "/dev/net/tun",
31 help = "TUN/TAP device file path or file descriptor number")
33 "-p", "--port", dest="port", metavar="PORT", type="int",
35 help = "Peering TCP port to connect or listen to.")
38 "-m", "--mode", dest="mode", metavar="MODE",
41 "Set mode. One of none, tun, tap, pl-tun, pl-tap. In any mode except none, a TUN/TAP will be created "
42 "by using the proper interface (tunctl for tun/tap, /vsys/fd_tuntap.control for pl-tun/pl-tap), "
43 "and it will be brought up (with ifconfig for tun/tap, with /vsys/vif_up for pl-tun/pl-tap). You have "
44 "to specify an VIF_ADDRESS and VIF_MASK in any case (except for none).")
46 "-A", "--vif-address", dest="vif_addr", metavar="VIF_ADDRESS",
49 "See mode. This specifies the VIF_ADDRESS, "
50 "the IP address of the virtual interface.")
52 "-M", "--vif-mask", dest="vif_mask", type="int", metavar="VIF_MASK",
55 "See mode. This specifies the VIF_MASK, "
56 "a number indicating the network type (ie: 24 for a C-class network).")
58 "-S", "--vif-snat", dest="vif_snat",
59 action = "store_true",
61 help = "See mode. This specifies whether SNAT will be enabled for the virtual interface. " )
63 "-P", "--vif-pointopoint", dest="vif_pointopoint", metavar="DST_ADDR",
66 "See mode. This specifies the remote endpoint's virtual address, "
67 "for point-to-point routing configuration. "
68 "Not supported by PlanetLab" )
70 "-Q", "--vif-txqueuelen", dest="vif_txqueuelen", metavar="SIZE", type="int",
73 "See mode. This specifies the interface's transmission queue length. " )
75 (options, remaining_args) = parser.parse_args(sys.argv[1:])
78 ETH_P_ALL = 0x00000003
80 TUNSETIFF = 0x400454ca
81 IFF_NO_PI = 0x00001000
84 IFF_VNET_HDR = 0x00004000
85 TUN_PKT_STRIP = 0x00000001
86 IFHWADDRLEN = 0x00000006
92 return x+'\x00'*(IFNAMSIZ-len(x))
94 def ifreq(iface, flags):
96 # char[IFNAMSIZ] : interface name
99 ifreq = ifnam(iface)+struct.pack("H",flags);
100 ifreq += '\x00' * (len(ifreq)-IFREQ_SZ)
103 def tunopen(tun_path, tun_name):
104 if tun_path.isdigit():
106 print >>sys.stderr, "Using tun:", tun_name, "fd", tun_path
107 tun = os.fdopen(int(tun_path), 'r+b', 0)
110 print >>sys.stderr, "Using tun:", tun_name, "at", tun_path
111 tun = open(tun_path, 'r+b', 0)
113 # bind file descriptor to the interface
114 fcntl.ioctl(tun.fileno(), TUNSETIFF, ifreq(tun_name, IFF_NO_PI|IFF_TUN))
118 def tunclose(tun_path, tun_name, tun):
119 if tun_path.isdigit():
121 os.close(int(tun_path))
127 def tuntap_alloc(kind, tun_path, tun_name):
133 args.append(tun_name)
134 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
135 out,err = proc.communicate()
137 raise RuntimeError, "Could not allocate %s device" % (kind,)
139 match = re.search(r"Set '(?P<dev>(?:tun|tap)[0-9]*)' persistent and owned by .*", out, re.I)
141 raise RuntimeError, "Could not allocate %s device - tunctl said: %s" % (kind, out)
143 tun_name = match.group("dev")
144 print >>sys.stderr, "Allocated %s device: %s" % (kind, tun_name)
146 return tun_path, tun_name
148 def tuntap_dealloc(tun_path, tun_name):
149 args = ["tunctl", "-d", tun_name]
150 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
151 out,err = proc.communicate()
153 print >> sys.stderr, "WARNING: error deallocating %s device" % (tun_name,)
155 def nmask_to_dot_notation(mask):
156 mask = hex(((1 << mask) - 1) << (32 - mask)) # 24 -> 0xFFFFFF00
157 mask = mask[2:] # strip 0x
158 mask = mask.decode("hex") # to bytes
159 mask = '.'.join(map(str,map(ord,mask))) # to 255.255.255.0
162 def vif_start(tun_path, tun_name):
163 args = ["ifconfig", tun_name, options.vif_addr,
164 "netmask", nmask_to_dot_notation(options.vif_mask),
166 if options.vif_pointopoint:
167 args.extend(["pointopoint",options.vif_pointopoint])
168 if options.vif_txqueuelen is not None:
169 args.extend(["txqueuelen",str(options.vif_txqueuelen)])
171 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
172 out,err = proc.communicate()
174 raise RuntimeError, "Error starting virtual interface"
177 # set up SNAT using iptables
178 # TODO: stop vif on error.
179 # Not so necessary since deallocating the tun/tap device
180 # will forcibly stop it, but it would be tidier
181 args = [ "iptables", "-t", "nat", "-A", "POSTROUTING",
182 "-s", "%s/%d" % (options.vif_addr, options.vif_mask),
184 "--to-source", hostaddr, "--random" ]
185 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
186 out,err = proc.communicate()
188 raise RuntimeError, "Error setting up SNAT"
190 def vif_stop(tun_path, tun_name):
192 # set up SNAT using iptables
193 args = [ "iptables", "-t", "nat", "-D", "POSTROUTING",
194 "-s", "%s/%d" % (options.vif_addr, options.vif_mask),
196 "--to-source", hostaddr, "--random" ]
197 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
198 out,err = proc.communicate()
200 args = ["ifconfig", tun_name, "down"]
201 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
202 out,err = proc.communicate()
204 print >>sys.stderr, "WARNING: error stopping virtual interface"
207 def pl_tuntap_alloc(kind, tun_path, tun_name):
208 tunalloc_so = ctypes.cdll.LoadLibrary("./vsys-scripts/support/tunalloc.so")
209 c_tun_name = ctypes.c_char_p("\x00"*IFNAMSIZ) # the string will be mutated!
210 kind = {"tun":IFF_TUN,
212 fd = tunalloc_so.tun_alloc(kind, c_tun_name)
213 name = c_tun_name.value
216 def pl_vif_start(tun_path, tun_name):
217 stdin = open("/vsys/vif_up.in","w")
218 stdout = open("/vsys/vif_up.out","r")
219 stdin.write(tun_name+"\n")
220 stdin.write(options.vif_addr+"\n")
221 stdin.write(str(options.vif_mask)+"\n")
223 stdin.write("snat=1\n")
224 if options.vif_txqueuelen is not None:
225 stdin.write("txqueuelen=%d\n" % (options.vif_txqueuelen,))
230 print >>sys.stderr, out
234 ipbytes = map(ord,ip.decode("hex"))
235 return '.'.join(map(str,ipbytes))
237 def formatPacket(packet):
238 packet = packet.encode("hex")
240 packet[0:1], #version
241 packet[1:2], #header length
242 packet[2:4], #diffserv/ECN
243 packet[4:8], #total length
245 packet[12:16], #flags/fragment offs
247 packet[18:20], #ip-proto
248 packet[20:24], #checksum
249 ipfmt(packet[24:32]), # src-ip
250 ipfmt(packet[32:40]), # dst-ip
251 packet[40:48] if (int(packet[1]) > 5) else "", # options
252 packet[48:] if (int(packet[1]) > 5) else packet[40:], # payload
255 def packetReady(buf):
258 _,totallen = struct.unpack('HH',buf[:4])
259 totallen = socket.htons(totallen)
260 return len(buf) >= totallen
263 _,totallen = struct.unpack('HH',buf[:4])
264 totallen = socket.htons(totallen)
265 return buf[:totallen], buf[totallen:]
270 if buf[12:14] == '\x08\x10' and buf[16:18] == '\x08\x00':
271 # tagged ethernet frame
273 elif buf[12:14] == '\x08\x00':
274 # untagged ethernet frame
279 def etherWrap(packet):
281 "\x00"*6*2 # bogus src and dst mac
284 +"\x00"*4 # bogus crc
295 "\x00\x00\x08\x00" # PI: 16 bits flags, 16 bits proto
300 def tun_fwd(tun, remote):
303 # in PL mode, we cannot strip PI structs
304 # so we'll have to handle them
305 with_pi = options.mode.startswith('pl-')
306 ether_mode = tun_name.startswith('tap')
308 # Limited frame parsing, to preserve packet boundaries.
309 # Which is needed, since /dev/net/tun is unbuffered
314 if packetReady(bkbuf):
316 if packetReady(fwbuf):
318 rdrdy, wrdy, errs = select.select((tun,remote),wset,(tun,remote),1)
324 # check to see if we can write
325 if remote in wrdy and packetReady(fwbuf):
326 packet, fwbuf = pullPacket(fwbuf)
327 os.write(remote.fileno(), packet)
328 print >>sys.stderr, '>', formatPacket(packet)
332 if tun in wrdy and packetReady(bkbuf):
333 packet, bkbuf = pullPacket(bkbuf)
334 formatted = formatPacket(packet)
336 packet = etherWrap(packet)
338 packet = piWrap(packet)
339 os.write(tun.fileno(), packet)
340 print >>sys.stderr, '<', formatted
342 # check incoming data packets
344 packet = os.read(tun.fileno(),2000) # tun.read blocks until it gets 2k!
346 packet = piStrip(packet)
349 fwbuf = etherStrip(fwbuf)
351 packet = os.read(remote.fileno(),2000) # remote.read blocks until it gets 2k!
356 nop = lambda tun_path, tun_name : (tun_path, tun_name)
358 'none' : dict(alloc=nop,
359 tunopen=tunopen, tunclose=tunclose,
363 'tun' : dict(alloc=functools.partial(tuntap_alloc, "tun"),
364 tunopen=tunopen, tunclose=tunclose,
365 dealloc=tuntap_dealloc,
368 'tap' : dict(alloc=functools.partial(tuntap_alloc, "tap"),
369 tunopen=tunopen, tunclose=tunclose,
370 dealloc=tuntap_dealloc,
373 'pl-tun' : dict(alloc=functools.partial(pl_tuntap_alloc, "tun"),
374 tunopen=tunopen, tunclose=tunclose,
378 'pl-tap' : dict(alloc=functools.partial(pl_tuntap_alloc, "tap"),
379 tunopen=tunopen, tunclose=tunclose,
385 tun_path = options.tun_path
386 tun_name = options.tun_name
388 modeinfo = MODEINFO[options.mode]
390 # be careful to roll back stuff on exceptions
391 tun_path, tun_name = modeinfo['alloc'](tun_path, tun_name)
393 modeinfo['start'](tun_path, tun_name)
395 tun = modeinfo['tunopen'](tun_path, tun_name)
397 modeinfo['stop'](tun_path, tun_name)
400 modeinfo['dealloc'](tun_path, tun_name)
405 # connect to remote endpoint
406 if remaining_args and not remaining_args[0].startswith('-'):
407 print >>sys.stderr, "Connecting to: %s:%d" % (remaining_args[0],options.port)
408 rsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
409 rsock.connect((remaining_args[0],options.port))
411 print >>sys.stderr, "Listening at: %s:%d" % (hostaddr,options.port)
412 lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
413 lsock.bind((hostaddr,options.port))
415 rsock,raddr = lsock.accept()
416 remote = os.fdopen(rsock.fileno(), 'r+b', 0)
418 print >>sys.stderr, "Connected"
423 print >>sys.stderr, "Shutting down..."
425 # In case sys.stderr is broken
428 # tidy shutdown in every case - swallow exceptions
430 modeinfo['tunclose'](tun_path, tun_name, tun)
435 modeinfo['stop'](tun_path, tun_name)
440 modeinfo['dealloc'](tun_path, tun_name)