18 tun_path = '/dev/net/tun'
19 hostaddr = socket.gethostbyname(socket.gethostname())
21 usage = "usage: %prog [options] <remote-endpoint>"
23 parser = optparse.OptionParser(usage=usage)
26 "-i", "--iface", dest="tun_name", metavar="DEVICE",
28 help = "TUN/TAP interface to tap into")
30 "-d", "--tun-path", dest="tun_path", metavar="PATH",
31 default = "/dev/net/tun",
32 help = "TUN/TAP device file path or file descriptor number")
34 "-p", "--port", dest="port", metavar="PORT", type="int",
36 help = "Peering TCP port to connect or listen to.")
38 "--pass-fd", dest="pass_fd", metavar="UNIX_SOCKET",
40 help = "Path to a unix-domain socket to pass the TUN file descriptor to. "
41 "If given, all other connectivity options are ignored, tun_connect will "
42 "simply wait to be killed after passing the file descriptor, and it will be "
43 "the receiver's responsability to handle the tunneling.")
46 "-m", "--mode", dest="mode", metavar="MODE",
49 "Set mode. One of none, tun, tap, pl-tun, pl-tap. In any mode except none, a TUN/TAP will be created "
50 "by using the proper interface (tunctl for tun/tap, /vsys/fd_tuntap.control for pl-tun/pl-tap), "
51 "and it will be brought up (with ifconfig for tun/tap, with /vsys/vif_up for pl-tun/pl-tap). You have "
52 "to specify an VIF_ADDRESS and VIF_MASK in any case (except for none).")
54 "-A", "--vif-address", dest="vif_addr", metavar="VIF_ADDRESS",
57 "See mode. This specifies the VIF_ADDRESS, "
58 "the IP address of the virtual interface.")
60 "-M", "--vif-mask", dest="vif_mask", type="int", metavar="VIF_MASK",
63 "See mode. This specifies the VIF_MASK, "
64 "a number indicating the network type (ie: 24 for a C-class network).")
66 "-S", "--vif-snat", dest="vif_snat",
67 action = "store_true",
69 help = "See mode. This specifies whether SNAT will be enabled for the virtual interface. " )
71 "-P", "--vif-pointopoint", dest="vif_pointopoint", metavar="DST_ADDR",
74 "See mode. This specifies the remote endpoint's virtual address, "
75 "for point-to-point routing configuration. "
76 "Not supported by PlanetLab" )
78 "-Q", "--vif-txqueuelen", dest="vif_txqueuelen", metavar="SIZE", type="int",
81 "See mode. This specifies the interface's transmission queue length. " )
83 "-u", "--udp", dest="udp", metavar="PORT", type="int",
86 "Bind to the specified UDP port locally, and send UDP datagrams to the "
87 "remote endpoint, creating a tunnel through UDP rather than TCP." )
89 "-k", "--key", dest="cipher_key", metavar="KEY",
92 "Specify a symmetric encryption key with which to protect packets across "
93 "the tunnel. python-crypto must be installed on the system." )
95 (options, remaining_args) = parser.parse_args(sys.argv[1:])
98 ETH_P_ALL = 0x00000003
100 TUNSETIFF = 0x400454ca
101 IFF_NO_PI = 0x00001000
104 IFF_VNET_HDR = 0x00004000
105 TUN_PKT_STRIP = 0x00000001
106 IFHWADDRLEN = 0x00000006
107 IFNAMSIZ = 0x00000010
108 IFREQ_SZ = 0x00000028
109 FIONREAD = 0x0000541b
112 return x+'\x00'*(IFNAMSIZ-len(x))
114 def ifreq(iface, flags):
116 # char[IFNAMSIZ] : interface name
119 ifreq = ifnam(iface)+struct.pack("H",flags);
120 ifreq += '\x00' * (len(ifreq)-IFREQ_SZ)
123 def tunopen(tun_path, tun_name):
124 if tun_path.isdigit():
126 print >>sys.stderr, "Using tun:", tun_name, "fd", tun_path
127 tun = os.fdopen(int(tun_path), 'r+b', 0)
130 print >>sys.stderr, "Using tun:", tun_name, "at", tun_path
131 tun = open(tun_path, 'r+b', 0)
133 # bind file descriptor to the interface
134 fcntl.ioctl(tun.fileno(), TUNSETIFF, ifreq(tun_name, IFF_NO_PI|IFF_TUN))
138 def tunclose(tun_path, tun_name, tun):
139 if tun_path.isdigit():
141 os.close(int(tun_path))
147 def tuntap_alloc(kind, tun_path, tun_name):
153 args.append(tun_name)
154 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
155 out,err = proc.communicate()
157 raise RuntimeError, "Could not allocate %s device" % (kind,)
159 match = re.search(r"Set '(?P<dev>(?:tun|tap)[0-9]*)' persistent and owned by .*", out, re.I)
161 raise RuntimeError, "Could not allocate %s device - tunctl said: %s" % (kind, out)
163 tun_name = match.group("dev")
164 print >>sys.stderr, "Allocated %s device: %s" % (kind, tun_name)
166 return tun_path, tun_name
168 def tuntap_dealloc(tun_path, tun_name):
169 args = ["tunctl", "-d", tun_name]
170 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
171 out,err = proc.communicate()
173 print >> sys.stderr, "WARNING: error deallocating %s device" % (tun_name,)
175 def nmask_to_dot_notation(mask):
176 mask = hex(((1 << mask) - 1) << (32 - mask)) # 24 -> 0xFFFFFF00
177 mask = mask[2:] # strip 0x
178 mask = mask.decode("hex") # to bytes
179 mask = '.'.join(map(str,map(ord,mask))) # to 255.255.255.0
182 def vif_start(tun_path, tun_name):
183 args = ["ifconfig", tun_name, options.vif_addr,
184 "netmask", nmask_to_dot_notation(options.vif_mask),
186 if options.vif_pointopoint:
187 args.extend(["pointopoint",options.vif_pointopoint])
188 if options.vif_txqueuelen is not None:
189 args.extend(["txqueuelen",str(options.vif_txqueuelen)])
191 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
192 out,err = proc.communicate()
194 raise RuntimeError, "Error starting virtual interface"
197 # set up SNAT using iptables
198 # TODO: stop vif on error.
199 # Not so necessary since deallocating the tun/tap device
200 # will forcibly stop it, but it would be tidier
201 args = [ "iptables", "-t", "nat", "-A", "POSTROUTING",
202 "-s", "%s/%d" % (options.vif_addr, options.vif_mask),
204 "--to-source", hostaddr, "--random" ]
205 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
206 out,err = proc.communicate()
208 raise RuntimeError, "Error setting up SNAT"
210 def vif_stop(tun_path, tun_name):
212 # set up SNAT using iptables
213 args = [ "iptables", "-t", "nat", "-D", "POSTROUTING",
214 "-s", "%s/%d" % (options.vif_addr, options.vif_mask),
216 "--to-source", hostaddr, "--random" ]
217 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
218 out,err = proc.communicate()
220 args = ["ifconfig", tun_name, "down"]
221 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
222 out,err = proc.communicate()
224 print >>sys.stderr, "WARNING: error stopping virtual interface"
227 def pl_tuntap_alloc(kind, tun_path, tun_name):
228 tunalloc_so = ctypes.cdll.LoadLibrary("./tunalloc.so")
229 c_tun_name = ctypes.c_char_p("\x00"*IFNAMSIZ) # the string will be mutated!
230 kind = {"tun":IFF_TUN,
232 fd = tunalloc_so.tun_alloc(kind, c_tun_name)
233 name = c_tun_name.value
236 def pl_vif_start(tun_path, tun_name):
237 stdin = open("/vsys/vif_up.in","w")
238 stdout = open("/vsys/vif_up.out","r")
239 stdin.write(tun_name+"\n")
240 stdin.write(options.vif_addr+"\n")
241 stdin.write(str(options.vif_mask)+"\n")
243 stdin.write("snat=1\n")
244 if options.vif_txqueuelen is not None:
245 stdin.write("txqueuelen=%d\n" % (options.vif_txqueuelen,))
250 print >>sys.stderr, out
254 ipbytes = map(ord,ip.decode("hex"))
255 return '.'.join(map(str,ipbytes))
261 '8863' : 'PPPoE discover ',
264 def etherProto(packet):
265 packet = packet.encode("hex")
267 if packet[12:14] == "\x81\x00":
275 def formatPacket(packet, ether_mode):
277 stripped_packet = etherStrip(packet)
278 if not stripped_packet:
279 packet = packet.encode("hex")
281 return "malformed eth " + packet.encode("hex")
283 if packet[24:28] == "8100":
285 ethertype = tagtype.get(packet[32:36], 'eth')
286 return ethertype + " " + ( '-'.join( (
287 packet[0:12], # MAC dest
288 packet[12:24], # MAC src
289 packet[24:32], # VLAN tag
290 packet[32:36], # Ethertype/len
291 packet[36:], # Payload
295 ethertype = tagtype.get(packet[24:28], 'eth')
296 return ethertype + " " + ( '-'.join( (
297 packet[0:12], # MAC dest
298 packet[12:24], # MAC src
299 packet[24:28], # Ethertype/len
300 packet[28:], # Payload
303 packet = stripped_packet
304 packet = packet.encode("hex")
306 return "malformed ip " + packet
308 return "ip " + ( '-'.join( (
309 packet[0:1], #version
310 packet[1:2], #header length
311 packet[2:4], #diffserv/ECN
312 packet[4:8], #total length
314 packet[12:16], #flags/fragment offs
316 packet[18:20], #ip-proto
317 packet[20:24], #checksum
318 ipfmt(packet[24:32]), # src-ip
319 ipfmt(packet[32:40]), # dst-ip
320 packet[40:48] if (int(packet[1],16) > 5) else "", # options
321 packet[48:] if (int(packet[1],16) > 5) else packet[40:], # payload
324 def packetReady(buf, ether_mode):
330 _,totallen = struct.unpack('HH',buf[:4])
331 totallen = socket.htons(totallen)
332 return len(buf) >= totallen
334 def pullPacket(buf, ether_mode):
338 _,totallen = struct.unpack('HH',buf[:4])
339 totallen = socket.htons(totallen)
340 return buf[:totallen], buf[totallen:]
345 if buf[12:14] == '\x08\x10' and buf[16:18] == '\x08\x00':
346 # tagged ethernet frame
348 elif buf[12:14] == '\x08\x00':
349 # untagged ethernet frame
354 def etherWrap(packet):
356 "\x00"*6*2 # bogus src and dst mac
359 +"\x00"*4 # bogus crc
368 def piWrap(buf, ether_mode):
370 proto = etherProto(buf)
374 "\x00\x00" # PI: 16 bits flags
375 +proto # 16 bits proto
379 def encrypt(packet, crypter):
381 padding = crypter.block_size - len(packet) % crypter.block_size
382 packet += chr(padding) * padding
385 return crypter.encrypt(packet)
387 def decrypt(packet, crypter):
389 packet = crypter.decrypt(packet)
392 padding = ord(packet[-1])
393 if not (0 < padding <= crypter.block_size):
395 raise RuntimeError, "Truncated packet"
396 packet = packet[:-padding]
401 def tun_fwd(tun, remote):
404 # in PL mode, we cannot strip PI structs
405 # so we'll have to handle them
406 with_pi = options.mode.startswith('pl-')
407 ether_mode = tun_name.startswith('tap')
411 if options.cipher_key:
412 import Crypto.Cipher.AES
415 hashed_key = hashlib.sha256(options.cipher_key).digest()
416 crypter = Crypto.Cipher.AES.new(
418 Crypto.Cipher.AES.MODE_ECB)
422 traceback.print_exc()
427 print >>sys.stderr, "Packets are transmitted in CIPHER"
429 print >>sys.stderr, "Packets are transmitted in PLAINTEXT"
431 # Limited frame parsing, to preserve packet boundaries.
432 # Which is needed, since /dev/net/tun is unbuffered
437 if packetReady(bkbuf, ether_mode):
439 if packetReady(fwbuf, ether_mode):
441 rdrdy, wrdy, errs = select.select((tun,remote),wset,(tun,remote),1)
447 # check to see if we can write
448 if remote in wrdy and packetReady(fwbuf, ether_mode):
449 packet, fwbuf = pullPacket(fwbuf, ether_mode)
452 enpacket = encrypt(packet, crypter)
455 os.write(remote.fileno(), enpacket)
458 # in UDP mode, we ignore errors - packet loss man...
460 print >>sys.stderr, '>', formatPacket(packet, ether_mode)
461 if tun in wrdy and packetReady(bkbuf, ether_mode):
462 packet, bkbuf = pullPacket(bkbuf, ether_mode)
463 formatted = formatPacket(packet, ether_mode)
465 packet = piWrap(packet, ether_mode)
466 os.write(tun.fileno(), packet)
467 print >>sys.stderr, '<', formatted
469 # check incoming data packets
471 packet = os.read(tun.fileno(),2000) # tun.read blocks until it gets 2k!
473 packet = piStrip(packet)
477 packet = os.read(remote.fileno(),2000) # remote.read blocks until it gets 2k!
479 packet = decrypt(packet, crypter)
482 # in UDP mode, we ignore errors - packet loss man...
488 nop = lambda tun_path, tun_name : (tun_path, tun_name)
490 'none' : dict(alloc=nop,
491 tunopen=tunopen, tunclose=tunclose,
495 'tun' : dict(alloc=functools.partial(tuntap_alloc, "tun"),
496 tunopen=tunopen, tunclose=tunclose,
497 dealloc=tuntap_dealloc,
500 'tap' : dict(alloc=functools.partial(tuntap_alloc, "tap"),
501 tunopen=tunopen, tunclose=tunclose,
502 dealloc=tuntap_dealloc,
505 'pl-tun' : dict(alloc=functools.partial(pl_tuntap_alloc, "tun"),
506 tunopen=tunopen, tunclose=tunclose,
510 'pl-tap' : dict(alloc=functools.partial(pl_tuntap_alloc, "tap"),
511 tunopen=tunopen, tunclose=tunclose,
517 tun_path = options.tun_path
518 tun_name = options.tun_name
520 modeinfo = MODEINFO[options.mode]
522 # be careful to roll back stuff on exceptions
523 tun_path, tun_name = modeinfo['alloc'](tun_path, tun_name)
525 modeinfo['start'](tun_path, tun_name)
527 tun = modeinfo['tunopen'](tun_path, tun_name)
529 modeinfo['stop'](tun_path, tun_name)
532 modeinfo['dealloc'](tun_path, tun_name)
538 # send FD to whoever wants it
541 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
542 sock.connect(options.pass_fd)
543 passfd.sendfd(sock, tun.fileno(), '0')
546 def tun_fwd(tun, remote):
550 # connect to remote endpoint
551 if remaining_args and not remaining_args[0].startswith('-'):
552 print >>sys.stderr, "Listening at: %s:%d" % (hostaddr,options.udp)
553 print >>sys.stderr, "Connecting to: %s:%d" % (remaining_args[0],options.port)
554 rsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
555 rsock.bind((hostaddr,options.udp))
556 rsock.connect((remaining_args[0],options.port))
558 print >>sys.stderr, "Error: need a remote endpoint in UDP mode"
559 raise AssertionError, "Error: need a remote endpoint in UDP mode"
560 remote = os.fdopen(rsock.fileno(), 'r+b', 0)
562 # connect to remote endpoint
563 if remaining_args and not remaining_args[0].startswith('-'):
564 print >>sys.stderr, "Connecting to: %s:%d" % (remaining_args[0],options.port)
565 rsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
566 rsock.connect((remaining_args[0],options.port))
568 print >>sys.stderr, "Listening at: %s:%d" % (hostaddr,options.port)
569 lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
570 lsock.bind((hostaddr,options.port))
572 rsock,raddr = lsock.accept()
573 remote = os.fdopen(rsock.fileno(), 'r+b', 0)
575 print >>sys.stderr, "Connected"
580 print >>sys.stderr, "Shutting down..."
582 # In case sys.stderr is broken
585 # tidy shutdown in every case - swallow exceptions
587 modeinfo['tunclose'](tun_path, tun_name, tun)
592 modeinfo['stop'](tun_path, tun_name)
597 modeinfo['dealloc'](tun_path, tun_name)