20 tun_path = '/dev/net/tun'
21 hostaddr = socket.gethostbyname(socket.gethostname())
23 usage = "usage: %prog [options] <remote-endpoint>"
25 parser = optparse.OptionParser(usage=usage)
28 "-i", "--iface", dest="tun_name", metavar="DEVICE",
30 help = "TUN/TAP interface to tap into")
32 "-d", "--tun-path", dest="tun_path", metavar="PATH",
33 default = "/dev/net/tun",
34 help = "TUN/TAP device file path or file descriptor number")
36 "-p", "--port", dest="port", metavar="PORT", type="int",
38 help = "Peering TCP port to connect or listen to.")
40 "--pass-fd", dest="pass_fd", metavar="UNIX_SOCKET",
42 help = "Path to a unix-domain socket to pass the TUN file descriptor to. "
43 "If given, all other connectivity options are ignored, tun_connect will "
44 "simply wait to be killed after passing the file descriptor, and it will be "
45 "the receiver's responsability to handle the tunneling.")
48 "-m", "--mode", dest="mode", metavar="MODE",
51 "Set mode. One of none, tun, tap, pl-tun, pl-tap. In any mode except none, a TUN/TAP will be created "
52 "by using the proper interface (tunctl for tun/tap, /vsys/fd_tuntap.control for pl-tun/pl-tap), "
53 "and it will be brought up (with ifconfig for tun/tap, with /vsys/vif_up for pl-tun/pl-tap). You have "
54 "to specify an VIF_ADDRESS and VIF_MASK in any case (except for none).")
56 "-A", "--vif-address", dest="vif_addr", metavar="VIF_ADDRESS",
59 "See mode. This specifies the VIF_ADDRESS, "
60 "the IP address of the virtual interface.")
62 "-M", "--vif-mask", dest="vif_mask", type="int", metavar="VIF_MASK",
65 "See mode. This specifies the VIF_MASK, "
66 "a number indicating the network type (ie: 24 for a C-class network).")
68 "-S", "--vif-snat", dest="vif_snat",
69 action = "store_true",
71 help = "See mode. This specifies whether SNAT will be enabled for the virtual interface. " )
73 "-P", "--vif-pointopoint", dest="vif_pointopoint", metavar="DST_ADDR",
76 "See mode. This specifies the remote endpoint's virtual address, "
77 "for point-to-point routing configuration. "
78 "Not supported by PlanetLab" )
80 "-Q", "--vif-txqueuelen", dest="vif_txqueuelen", metavar="SIZE", type="int",
83 "See mode. This specifies the interface's transmission queue length. " )
85 "-u", "--udp", dest="udp", metavar="PORT", type="int",
88 "Bind to the specified UDP port locally, and send UDP datagrams to the "
89 "remote endpoint, creating a tunnel through UDP rather than TCP." )
91 "-k", "--key", dest="cipher_key", metavar="KEY",
94 "Specify a symmetric encryption key with which to protect packets across "
95 "the tunnel. python-crypto must be installed on the system." )
97 (options, remaining_args) = parser.parse_args(sys.argv[1:])
100 ETH_P_ALL = 0x00000003
101 ETH_P_IP = 0x00000800
102 TUNSETIFF = 0x400454ca
103 IFF_NO_PI = 0x00001000
106 IFF_VNET_HDR = 0x00004000
107 TUN_PKT_STRIP = 0x00000001
108 IFHWADDRLEN = 0x00000006
109 IFNAMSIZ = 0x00000010
110 IFREQ_SZ = 0x00000028
111 FIONREAD = 0x0000541b
114 return x+'\x00'*(IFNAMSIZ-len(x))
116 def ifreq(iface, flags):
118 # char[IFNAMSIZ] : interface name
121 ifreq = ifnam(iface)+struct.pack("H",flags);
122 ifreq += '\x00' * (len(ifreq)-IFREQ_SZ)
125 def tunopen(tun_path, tun_name):
126 if tun_path.isdigit():
128 print >>sys.stderr, "Using tun:", tun_name, "fd", tun_path
129 tun = os.fdopen(int(tun_path), 'r+b', 0)
132 print >>sys.stderr, "Using tun:", tun_name, "at", tun_path
133 tun = open(tun_path, 'r+b', 0)
135 # bind file descriptor to the interface
136 fcntl.ioctl(tun.fileno(), TUNSETIFF, ifreq(tun_name, IFF_NO_PI|IFF_TUN))
140 def tunclose(tun_path, tun_name, tun):
141 if tun_path.isdigit():
143 os.close(int(tun_path))
149 def tuntap_alloc(kind, tun_path, tun_name):
155 args.append(tun_name)
156 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
157 out,err = proc.communicate()
159 raise RuntimeError, "Could not allocate %s device" % (kind,)
161 match = re.search(r"Set '(?P<dev>(?:tun|tap)[0-9]*)' persistent and owned by .*", out, re.I)
163 raise RuntimeError, "Could not allocate %s device - tunctl said: %s" % (kind, out)
165 tun_name = match.group("dev")
166 print >>sys.stderr, "Allocated %s device: %s" % (kind, tun_name)
168 return tun_path, tun_name
170 def tuntap_dealloc(tun_path, tun_name):
171 args = ["tunctl", "-d", tun_name]
172 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
173 out,err = proc.communicate()
175 print >> sys.stderr, "WARNING: error deallocating %s device" % (tun_name,)
177 def nmask_to_dot_notation(mask):
178 mask = hex(((1 << mask) - 1) << (32 - mask)) # 24 -> 0xFFFFFF00
179 mask = mask[2:] # strip 0x
180 mask = mask.decode("hex") # to bytes
181 mask = '.'.join(map(str,map(ord,mask))) # to 255.255.255.0
184 def vif_start(tun_path, tun_name):
185 args = ["ifconfig", tun_name, options.vif_addr,
186 "netmask", nmask_to_dot_notation(options.vif_mask),
188 if options.vif_pointopoint:
189 args.extend(["pointopoint",options.vif_pointopoint])
190 if options.vif_txqueuelen is not None:
191 args.extend(["txqueuelen",str(options.vif_txqueuelen)])
193 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
194 out,err = proc.communicate()
196 raise RuntimeError, "Error starting virtual interface"
199 # set up SNAT using iptables
200 # TODO: stop vif on error.
201 # Not so necessary since deallocating the tun/tap device
202 # will forcibly stop it, but it would be tidier
203 args = [ "iptables", "-t", "nat", "-A", "POSTROUTING",
204 "-s", "%s/%d" % (options.vif_addr, options.vif_mask),
206 "--to-source", hostaddr, "--random" ]
207 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
208 out,err = proc.communicate()
210 raise RuntimeError, "Error setting up SNAT"
212 def vif_stop(tun_path, tun_name):
214 # set up SNAT using iptables
215 args = [ "iptables", "-t", "nat", "-D", "POSTROUTING",
216 "-s", "%s/%d" % (options.vif_addr, options.vif_mask),
218 "--to-source", hostaddr, "--random" ]
219 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
220 out,err = proc.communicate()
222 args = ["ifconfig", tun_name, "down"]
223 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
224 out,err = proc.communicate()
226 print >>sys.stderr, "WARNING: error stopping virtual interface"
229 def pl_tuntap_alloc(kind, tun_path, tun_name):
230 tunalloc_so = ctypes.cdll.LoadLibrary("./tunalloc.so")
231 c_tun_name = ctypes.c_char_p("\x00"*IFNAMSIZ) # the string will be mutated!
232 kind = {"tun":IFF_TUN,
234 fd = tunalloc_so.tun_alloc(kind, c_tun_name)
235 name = c_tun_name.value
238 def pl_vif_start(tun_path, tun_name):
239 stdin = open("/vsys/vif_up.in","w")
240 stdout = open("/vsys/vif_up.out","r")
241 stdin.write(tun_name+"\n")
242 stdin.write(options.vif_addr+"\n")
243 stdin.write(str(options.vif_mask)+"\n")
245 stdin.write("snat=1\n")
246 if options.vif_txqueuelen is not None:
247 stdin.write("txqueuelen=%d\n" % (options.vif_txqueuelen,))
252 print >>sys.stderr, out
256 ipbytes = map(ord,ip.decode("hex"))
257 return '.'.join(map(str,ipbytes))
263 '8863' : 'PPPoE discover',
267 def etherProto(packet):
268 packet = packet.encode("hex")
270 if packet[12:14] == "\x81\x00":
278 def formatPacket(packet, ether_mode):
280 stripped_packet = etherStrip(packet)
281 if not stripped_packet:
282 packet = packet.encode("hex")
284 return "malformed eth " + packet.encode("hex")
286 if packet[24:28] == "8100":
288 ethertype = tagtype.get(packet[32:36], 'eth')
289 return ethertype + " " + ( '-'.join( (
290 packet[0:12], # MAC dest
291 packet[12:24], # MAC src
292 packet[24:32], # VLAN tag
293 packet[32:36], # Ethertype/len
294 packet[36:], # Payload
298 ethertype = tagtype.get(packet[24:28], 'eth')
299 return ethertype + " " + ( '-'.join( (
300 packet[0:12], # MAC dest
301 packet[12:24], # MAC src
302 packet[24:28], # Ethertype/len
303 packet[28:], # Payload
306 packet = stripped_packet
307 packet = packet.encode("hex")
309 return "malformed ip " + packet
311 return "ip " + ( '-'.join( (
312 packet[0:1], #version
313 packet[1:2], #header length
314 packet[2:4], #diffserv/ECN
315 packet[4:8], #total length
317 packet[12:16], #flags/fragment offs
319 packet[18:20], #ip-proto
320 packet[20:24], #checksum
321 ipfmt(packet[24:32]), # src-ip
322 ipfmt(packet[32:40]), # dst-ip
323 packet[40:48] if (int(packet[1],16) > 5) else "", # options
324 packet[48:] if (int(packet[1],16) > 5) else packet[40:], # payload
327 def packetReady(buf, ether_mode):
333 _,totallen = struct.unpack('HH',buf[:4])
334 totallen = socket.htons(totallen)
335 return len(buf) >= totallen
337 def pullPacket(buf, ether_mode):
341 _,totallen = struct.unpack('HH',buf[:4])
342 totallen = socket.htons(totallen)
343 return buf[:totallen], buf[totallen:]
348 if buf[12:14] == '\x08\x10' and buf[16:18] in '\x08\x00':
349 # tagged ethernet frame
351 elif buf[12:14] == '\x08\x00':
352 # untagged ethernet frame
357 def etherWrap(packet):
359 "\x00"*6*2 # bogus src and dst mac
362 +"\x00"*4 # bogus crc
371 def piWrap(buf, ether_mode):
373 proto = etherProto(buf)
377 "\x00\x00" # PI: 16 bits flags
378 +proto # 16 bits proto
382 def encrypt(packet, crypter):
384 padding = crypter.block_size - len(packet) % crypter.block_size
385 packet += chr(padding) * padding
388 return crypter.encrypt(packet)
390 def decrypt(packet, crypter):
392 packet = crypter.decrypt(packet)
395 padding = ord(packet[-1])
396 if not (0 < padding <= crypter.block_size):
398 raise RuntimeError, "Truncated packet"
399 packet = packet[:-padding]
404 def tun_fwd(tun, remote):
407 # in PL mode, we cannot strip PI structs
408 # so we'll have to handle them
409 with_pi = options.mode.startswith('pl-')
410 ether_mode = tun_name.startswith('tap')
414 if options.cipher_key:
415 import Crypto.Cipher.AES
418 hashed_key = hashlib.sha256(options.cipher_key).digest()
419 crypter = Crypto.Cipher.AES.new(
421 Crypto.Cipher.AES.MODE_ECB)
425 traceback.print_exc()
430 print >>sys.stderr, "Packets are transmitted in CIPHER"
432 print >>sys.stderr, "Packets are transmitted in PLAINTEXT"
434 # Limited frame parsing, to preserve packet boundaries.
435 # Which is needed, since /dev/net/tun is unbuffered
440 if packetReady(bkbuf, ether_mode):
442 if packetReady(fwbuf, ether_mode):
444 rdrdy, wrdy, errs = select.select((tun,remote),wset,(tun,remote),1)
450 # check to see if we can write
451 if remote in wrdy and packetReady(fwbuf, ether_mode):
452 packet, fwbuf = pullPacket(fwbuf, ether_mode)
455 enpacket = encrypt(packet, crypter)
458 os.write(remote.fileno(), enpacket)
461 # in UDP mode, we ignore errors - packet loss man...
463 print >>sys.stderr, '>', formatPacket(packet, ether_mode)
464 if tun in wrdy and packetReady(bkbuf, ether_mode):
465 packet, bkbuf = pullPacket(bkbuf, ether_mode)
466 formatted = formatPacket(packet, ether_mode)
468 packet = piWrap(packet, ether_mode)
469 os.write(tun.fileno(), packet)
470 print >>sys.stderr, '<', formatted
472 # check incoming data packets
474 packet = os.read(tun.fileno(),2000) # tun.read blocks until it gets 2k!
476 packet = piStrip(packet)
480 packet = os.read(remote.fileno(),2000) # remote.read blocks until it gets 2k!
482 packet = decrypt(packet, crypter)
485 # in UDP mode, we ignore errors - packet loss man...
491 nop = lambda tun_path, tun_name : (tun_path, tun_name)
493 'none' : dict(alloc=nop,
494 tunopen=tunopen, tunclose=tunclose,
498 'tun' : dict(alloc=functools.partial(tuntap_alloc, "tun"),
499 tunopen=tunopen, tunclose=tunclose,
500 dealloc=tuntap_dealloc,
503 'tap' : dict(alloc=functools.partial(tuntap_alloc, "tap"),
504 tunopen=tunopen, tunclose=tunclose,
505 dealloc=tuntap_dealloc,
508 'pl-tun' : dict(alloc=functools.partial(pl_tuntap_alloc, "tun"),
509 tunopen=tunopen, tunclose=tunclose,
513 'pl-tap' : dict(alloc=functools.partial(pl_tuntap_alloc, "tap"),
514 tunopen=tunopen, tunclose=tunclose,
520 tun_path = options.tun_path
521 tun_name = options.tun_name
523 modeinfo = MODEINFO[options.mode]
525 # be careful to roll back stuff on exceptions
526 tun_path, tun_name = modeinfo['alloc'](tun_path, tun_name)
528 modeinfo['start'](tun_path, tun_name)
530 tun = modeinfo['tunopen'](tun_path, tun_name)
532 modeinfo['stop'](tun_path, tun_name)
535 modeinfo['dealloc'](tun_path, tun_name)
541 if options.pass_fd.startswith("base64:"):
542 options.pass_fd = base64.b64decode(
543 options.pass_fd[len("base64:"):])
544 options.pass_fd = os.path.expandvars(options.pass_fd)
546 print >>sys.stderr, "Sending FD to: %r" % (options.pass_fd,)
548 # send FD to whoever wants it
551 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
553 sock.connect(options.pass_fd)
555 # wait a while, retry
557 sock.connect(options.pass_fd)
558 passfd.sendfd(sock, tun.fileno(), '0')
561 def tun_fwd(tun, remote):
566 # connect to remote endpoint
567 if remaining_args and not remaining_args[0].startswith('-'):
568 print >>sys.stderr, "Listening at: %s:%d" % (hostaddr,options.udp)
569 print >>sys.stderr, "Connecting to: %s:%d" % (remaining_args[0],options.port)
570 rsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
571 rsock.bind((hostaddr,options.udp))
572 rsock.connect((remaining_args[0],options.port))
574 print >>sys.stderr, "Error: need a remote endpoint in UDP mode"
575 raise AssertionError, "Error: need a remote endpoint in UDP mode"
576 remote = os.fdopen(rsock.fileno(), 'r+b', 0)
578 # connect to remote endpoint
579 if remaining_args and not remaining_args[0].startswith('-'):
580 print >>sys.stderr, "Connecting to: %s:%d" % (remaining_args[0],options.port)
581 rsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
582 rsock.connect((remaining_args[0],options.port))
584 print >>sys.stderr, "Listening at: %s:%d" % (hostaddr,options.port)
585 lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
586 lsock.bind((hostaddr,options.port))
588 rsock,raddr = lsock.accept()
589 remote = os.fdopen(rsock.fileno(), 'r+b', 0)
591 print >>sys.stderr, "Connected"
596 print >>sys.stderr, "Shutting down..."
598 # In case sys.stderr is broken
601 # tidy shutdown in every case - swallow exceptions
603 modeinfo['tunclose'](tun_path, tun_name, tun)
608 modeinfo['stop'](tun_path, tun_name)
613 modeinfo['dealloc'](tun_path, tun_name)