20 from Queue import Queue
31 tun_path = '/dev/net/tun'
32 hostaddr = socket.gethostbyname(socket.gethostname())
34 usage = "usage: %prog [options]"
36 parser = optparse.OptionParser(usage=usage)
39 "-i", "--iface", dest="tun_name", metavar="DEVICE",
41 help = "TUN/TAP interface to tap into")
43 "-d", "--tun-path", dest="tun_path", metavar="PATH",
44 default = "/dev/net/tun",
45 help = "TUN/TAP device file path or file descriptor number")
47 "-p", "--peer-port", dest="peer_port", metavar="PEER_PORT", type="int",
49 help = "Remote TCP/UDP port to connect to.")
51 "--pass-fd", dest="pass_fd", metavar="UNIX_SOCKET",
53 help = "Path to a unix-domain socket to pass the TUN file descriptor to. "
54 "If given, all other connectivity options are ignored, tun_connect will "
55 "simply wait to be killed after passing the file descriptor, and it will be "
56 "the receiver's responsability to handle the tunneling.")
58 "-m", "--mode", dest="mode", metavar="MODE",
61 "Set mode. One of none, tun, tap, pl-tun, pl-tap, pl-gre-ip, pl-gre-eth. In any mode except none, a TUN/TAP will be created "
62 "by using the proper interface (tunctl for tun/tap, /vsys/fd_tuntap.control for pl-tun/pl-tap), "
63 "and it will be brought up (with ifconfig for tun/tap, with /vsys/vif_up for pl-tun/pl-tap). You have "
64 "to specify an VIF_ADDRESS and VIF_MASK in any case (except for none).")
66 "-t", "--protocol", dest="protocol", metavar="PROTOCOL",
69 "Set protocol. One of tcp, udp, fd, gre. In any mode except none, a TUN/TAP will be created.")
71 "-A", "--vif-address", dest="vif_addr", metavar="VIF_ADDRESS",
74 "See mode. This specifies the VIF_ADDRESS, "
75 "the IP address of the virtual interface.")
77 "-M", "--vif-mask", dest="vif_mask", type="int", metavar="VIF_MASK",
80 "See mode. This specifies the VIF_MASK, "
81 "a number indicating the network type (ie: 24 for a C-class network).")
83 "-P", "--port", dest="port", type="int", metavar="PORT",
86 "This specifies the LOCAL_PORT. This will be the local bind port for UDP/TCP.")
88 "-S", "--vif-snat", dest="vif_snat",
89 action = "store_true",
91 help = "See mode. This specifies whether SNAT will be enabled for the virtual interface. " )
93 "-Z", "--vif-pointopoint", dest="vif_pointopoint", metavar="DST_ADDR",
96 "See mode. This specifies the remote endpoint's virtual address, "
97 "for point-to-point routing configuration. "
98 "Not supported by PlanetLab" )
100 "-Q", "--vif-txqueuelen", dest="vif_txqueuelen", metavar="SIZE", type="int",
103 "See mode. This specifies the interface's transmission queue length. " )
105 "-b", "--bwlimit", dest="bwlimit", metavar="BYTESPERSECOND", type="int",
108 "This specifies the interface's emulated bandwidth in bytes per second." )
110 "-a", "--peer-address", dest="peer_addr", metavar="PEER_ADDRESS",
113 "This specifies the PEER_ADDRESS, "
114 "the IP address of the remote interface.")
116 "-k", "--key", dest="cipher_key", metavar="KEY",
119 "Specify a symmetric encryption key with which to protect packets across "
120 "the tunnel. python-crypto must be installed on the system." )
122 "-K", "--gre-key", dest="gre_key", metavar="KEY", type="string",
125 "Specify a demultiplexing 32-bit numeric key for GRE." )
127 "-C", "--cipher", dest="cipher", metavar="CIPHER",
129 help = "One of PLAIN, AES, Blowfish, DES, DES3. " )
131 "-N", "--no-capture", dest="no_capture",
132 action = "store_true",
134 help = "If specified, packets won't be logged to standard output "
135 "(default is to log them to standard output). " )
137 "-c", "--pcap-capture", dest="pcap_capture", metavar="FILE",
139 help = "If specified, packets won't be logged to standard output, "
140 "but dumped to a pcap-formatted trace in the specified file. " )
142 "--multicast-forwarder", dest="multicast_fwd",
144 help = "If specified, multicast packets will be forwarded to "
145 "the specified unix-domain socket. If the device uses ethernet "
146 "frames, ethernet headers will be stripped and IP packets "
147 "will be forwarded, prefixed with the interface's address." )
149 "--filter", dest="filter_module", metavar="PATH",
151 help = "If specified, it should be either a .py or .so module. "
152 "It will be loaded, and all incoming and outgoing packets "
153 "will be routed through it. The filter will not be responsible "
154 "for buffering, packet queueing is performed in tun_connect "
155 "already, so it should not concern itself with it. It should "
156 "not, however, block in one direction if the other is congested.\n"
158 "Modules are expected to have the following methods:\n"
160 "\t\tIf arguments are given, this method will be called with the\n"
161 "\t\tgiven arguments (as keyword args in python modules, or a single\n"
162 "\t\tstring in c modules).\n"
163 "\taccept_packet(packet, direction):\n"
164 "\t\tDecide whether to drop the packet. Direction is 0 for packets "
165 "coming from the local side to the remote, and 1 is for packets "
166 "coming from the remote side to the local. Return a boolean, "
167 "true if the packet is not to be dropped.\n"
169 "\t\tInitializes a filtering pipe (filter_run). It should "
170 "return two file descriptors to use as a bidirectional "
171 "pipe: local and remote. 'local' is where packets from the "
172 "local side will be written to. After filtering, those packets "
173 "should be written to 'remote', where tun_connect will read "
174 "from, and it will forward them to the remote peer. "
175 "Packets from the remote peer will be written to 'remote', "
176 "where the filter is expected to read from, and eventually "
177 "forward them to the local side. If the file descriptors are "
178 "not nonblocking, they will be set to nonblocking. So it's "
179 "better to set them from the start like that.\n"
180 "\tfilter_run(local, remote):\n"
181 "\t\tIf filter_init is provided, it will be called repeatedly, "
182 "in a separate thread until the process is killed. It should "
183 "sleep at most for a second.\n"
184 "\tfilter_close(local, remote):\n"
185 "\t\tCalled then the process is killed, if filter_init was provided. "
186 "It should, among other things, close the file descriptors.\n"
188 "Python modules are expected to return a tuple in filter_init, "
189 "either of file descriptors or file objects, while native ones "
190 "will receive two int*.\n"
192 "Python modules can additionally contain a custom queue class "
193 "that will replace the FIFO used by default. The class should "
194 "be named 'queueclass' and contain an interface compatible with "
195 "collections.deque. That is, indexing (especiall for q[0]), "
196 "bool(q), popleft, appendleft, pop (right), append (right), "
197 "len(q) and clear. When using a custom queue, queue size will "
198 "have no effect, pass an effective queue size to the module "
199 "by using filter_args" )
201 "--filter-args", dest="filter_args", metavar="FILE",
203 help = "If specified, packets won't be logged to standard output, "
204 "but dumped to a pcap-formatted trace in the specified file. " )
206 (options,args) = parser.parse_args(sys.argv[1:])
212 'blowfish' : 'Blowfish',
214 }[options.cipher.lower()]
216 ETH_P_ALL = 0x00000003
217 ETH_P_IP = 0x00000800
218 TUNSETIFF = 0x400454ca
219 IFF_NO_PI = 0x00001000
222 IFF_VNET_HDR = 0x00004000
223 TUN_PKT_STRIP = 0x00000001
224 IFHWADDRLEN = 0x00000006
225 IFNAMSIZ = 0x00000010
226 IFREQ_SZ = 0x00000028
227 FIONREAD = 0x0000541b
229 class HostLock(object):
230 # This class is used as a lock to prevent concurrency issues with more
231 # than one instance of netns running in the same machine. Both in
232 # different processes or different threads.
234 processcond = threading.Condition()
236 def __init__(self, lockfile):
237 processcond = self.__class__.processcond
239 processcond.acquire()
242 while self.__class__.taken:
244 self.__class__.taken = True
246 processcond.release()
248 self.lockfile = lockfile
252 fcntl.flock(self.lockfile, fcntl.LOCK_EX)
254 except (OSError, IOError), e:
255 if e.args[0] != os.errno.EINTR:
259 processcond = self.__class__.processcond
261 processcond.acquire()
263 if not self.lockfile.closed:
264 fcntl.flock(self.lockfile, fcntl.LOCK_UN)
267 self.__class__.taken = False
270 processcond.release()
273 return x+'\x00'*(IFNAMSIZ-len(x))
275 def ifreq(iface, flags):
277 # char[IFNAMSIZ] : interface name
280 ifreq = ifnam(iface)+struct.pack("H",flags);
281 ifreq += '\x00' * (len(ifreq)-IFREQ_SZ)
284 def tunopen(tun_path, tun_name):
285 if tun_path.isdigit():
287 print >>sys.stderr, "Using tun:", tun_name, "fd", tun_path
288 tun = os.fdopen(int(tun_path), 'r+b', 0)
291 print >>sys.stderr, "Using tun:", tun_name, "at", tun_path
292 tun = open(tun_path, 'r+b', 0)
294 # bind file descriptor to the interface
295 fcntl.ioctl(tun.fileno(), TUNSETIFF, ifreq(tun_name, IFF_NO_PI|IFF_TUN))
299 def tunclose(tun_path, tun_name, tun):
300 if tun_path and tun_path.isdigit():
302 os.close(int(tun_path))
308 def noopen(tun_path, tun_name):
309 print >>sys.stderr, "Using tun:", tun_name
311 def noclose(tun_path, tun_name, tun):
314 def tuntap_alloc(kind, tun_path, tun_name):
320 args.append(tun_name)
321 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
322 out,err = proc.communicate()
324 raise RuntimeError, "Could not allocate %s device" % (kind,)
326 match = re.search(r"Set '(?P<dev>(?:tun|tap)[0-9]*)' persistent and owned by .*", out, re.I)
328 raise RuntimeError, "Could not allocate %s device - tunctl said: %s" % (kind, out)
330 tun_name = match.group("dev")
331 print >>sys.stderr, "Allocated %s device: %s" % (kind, tun_name)
333 return tun_path, tun_name
335 def tuntap_dealloc(tun_path, tun_name):
336 args = ["tunctl", "-d", tun_name]
337 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
338 out,err = proc.communicate()
340 print >> sys.stderr, "WARNING: error deallocating %s device" % (tun_name,)
342 def nmask_to_dot_notation(mask):
343 mask = hex(((1 << mask) - 1) << (32 - mask)) # 24 -> 0xFFFFFF00
344 mask = mask[2:] # strip 0x
345 mask = mask.decode("hex") # to bytes
346 mask = '.'.join(map(str,map(ord,mask))) # to 255.255.255.0
349 def vif_start(tun_path, tun_name):
350 args = ["ifconfig", tun_name, options.vif_addr,
351 "netmask", nmask_to_dot_notation(options.vif_mask),
353 if options.vif_pointopoint:
354 args.extend(["pointopoint",options.vif_pointopoint])
355 if options.vif_txqueuelen is not None:
356 args.extend(["txqueuelen",str(options.vif_txqueuelen)])
358 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
359 out,err = proc.communicate()
361 raise RuntimeError, "Error starting virtual interface"
364 # set up SNAT using iptables
365 # TODO: stop vif on error.
366 # Not so necessary since deallocating the tun/tap device
367 # will forcibly stop it, but it would be tidier
368 args = [ "iptables", "-t", "nat", "-A", "POSTROUTING",
369 "-s", "%s/%d" % (options.vif_addr, options.vif_mask),
371 "--to-source", hostaddr, "--random" ]
372 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
373 out,err = proc.communicate()
375 raise RuntimeError, "Error setting up SNAT"
377 def vif_stop(tun_path, tun_name):
379 # set up SNAT using iptables
380 args = [ "iptables", "-t", "nat", "-D", "POSTROUTING",
381 "-s", "%s/%d" % (options.vif_addr, options.vif_mask),
383 "--to-source", hostaddr, "--random" ]
384 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
385 out,err = proc.communicate()
387 args = ["ifconfig", tun_name, "down"]
388 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
389 out,err = proc.communicate()
391 print >>sys.stderr, "WARNING: error stopping virtual interface"
394 def pl_tuntap_alloc(kind, tun_path, tun_name):
395 tunalloc_so = ctypes.cdll.LoadLibrary("./tunalloc.so")
396 c_tun_name = ctypes.c_char_p("\x00"*IFNAMSIZ) # the string will be mutated!
397 kind = {"tun":IFF_TUN,
399 fd = tunalloc_so.tun_alloc(kind, c_tun_name)
400 name = c_tun_name.value
403 _name_reservation = None
404 def pl_tuntap_namealloc(kind, tun_path, tun_name):
405 global _name_reservation
407 lockfile = open("/tmp/nepi-tun-connect.lock", "a")
408 lock = HostLock(lockfile)
410 # We need to do this, fd_tuntap is the only one who can
411 # tell us our slice id (this script runs as root, so no uid),
412 # and the pattern of device names accepted by vsys scripts
413 tunalloc_so = ctypes.cdll.LoadLibrary("./tunalloc.so")
414 c_tun_name = ctypes.c_char_p("\x00"*IFNAMSIZ) # the string will be mutated!
415 nkind= {"tun":IFF_TUN,
417 fd = tunalloc_so.tun_alloc(nkind, c_tun_name)
418 name = c_tun_name.value
421 base = name[:name.index('-')+1]
422 existing = set(map(str.strip,os.popen("ip a | grep -o '%s[0-9]*'" % (base,)).read().strip().split('\n')))
424 for i in xrange(9000,10000):
426 if name not in existing:
429 raise RuntimeError, "Could not assign interface name"
431 _name_reservation = lock
435 def pl_vif_start(tun_path, tun_name):
436 global _name_reservation
440 out.append(stdout.read())
444 # Serialize access to vsys
445 lockfile = open("/tmp/nepi-tun-connect.lock", "a")
446 lock = _name_reservation or HostLock(lockfile)
447 _name_reservation = None
449 stdin = open("/vsys/vif_up.in","w")
450 stdout = open("/vsys/vif_up.out","r")
452 t = threading.Thread(target=outreader)
455 stdin.write(tun_name+"\n")
456 stdin.write(options.vif_addr+"\n")
457 stdin.write(str(options.vif_mask)+"\n")
459 stdin.write("snat=1\n")
460 if options.vif_pointopoint:
461 stdin.write("pointopoint=%s\n" % (options.vif_pointopoint,))
462 if options.vif_txqueuelen is not None:
463 stdin.write("txqueuelen=%d\n" % (options.vif_txqueuelen,))
464 if options.mode.startswith('pl-gre'):
465 stdin.write("gre=%s\n" % (options.gre_key,))
466 stdin.write("remote=%s\n" % (options.peer_addr,))
472 print >>sys.stderr, out
476 def pl_vif_stop(tun_path, tun_name):
479 out.append(stdout.read())
482 if options.mode.startswith('pl-gre'):
487 for i in xrange(lim):
488 ifaces = set(map(str.strip,os.popen("ip a | grep -o '%s'" % (tun_name,)).read().strip().split('\n')))
489 if tun_name in ifaces:
494 # Serialize access to vsys
495 lockfile = open("/tmp/nepi-tun-connect.lock", "a")
496 lock = HostLock(lockfile)
498 stdin = open("/vsys/vif_down.in","w")
499 stdout = open("/vsys/vif_down.out","r")
501 t = threading.Thread(target=outreader)
504 stdin.write(tun_name+"\n")
510 print >>sys.stderr, out
515 def tun_fwd(tun, remote, reconnect = None, accept_local = None, accept_remote = None, slowlocal = True, bwlimit = None):
518 tunqueue = options.vif_txqueuelen or 1000
521 # in PL mode, we cannot strip PI structs
522 # so we'll have to handle them
523 tunchannel.tun_fwd(tun, remote,
524 with_pi = options.mode.startswith('pl-'),
525 ether_mode = tun_name.startswith('tap'),
526 cipher_key = options.cipher_key,
527 udp = options.protocol == 'udp',
528 TERMINATE = TERMINATE,
530 reconnect = reconnect,
532 tunkqueue = tunkqueue,
533 cipher = options.cipher,
534 accept_local = accept_local,
535 accept_remote = accept_remote,
536 queueclass = queueclass,
537 slowlocal = slowlocal,
543 nop = lambda tun_path, tun_name : (tun_path, tun_name)
545 'none' : dict(alloc=nop,
546 tunopen=tunopen, tunclose=tunclose,
550 'tun' : dict(alloc=functools.partial(tuntap_alloc, "tun"),
551 tunopen=tunopen, tunclose=tunclose,
552 dealloc=tuntap_dealloc,
555 'tap' : dict(alloc=functools.partial(tuntap_alloc, "tap"),
556 tunopen=tunopen, tunclose=tunclose,
557 dealloc=tuntap_dealloc,
560 'pl-tun' : dict(alloc=functools.partial(pl_tuntap_alloc, "tun"),
561 tunopen=tunopen, tunclose=tunclose,
565 'pl-tap' : dict(alloc=functools.partial(pl_tuntap_alloc, "tap"),
566 tunopen=tunopen, tunclose=tunclose,
570 'pl-gre-ip' : dict(alloc=functools.partial(pl_tuntap_namealloc, "tun"),
571 tunopen=noopen, tunclose=tunclose,
575 'pl-gre-eth': dict(alloc=functools.partial(pl_tuntap_namealloc, "tap"),
576 tunopen=noopen, tunclose=noclose,
582 tun_path = options.tun_path
583 tun_name = options.tun_name
585 modeinfo = MODEINFO[options.mode]
587 # Try to load filter module
589 if options.filter_module:
590 print >>sys.stderr, "Loading module", options.filter_module, "with args", options.filter_args
591 if options.filter_module.endswith('.py'):
592 sys.path.append(os.path.dirname(options.filter_module))
593 filter_module = __import__(os.path.basename(options.filter_module).rsplit('.',1)[0])
594 if options.filter_args:
596 filter_args = dict(map(lambda x:x.split('=',1),options.filter_args.split(',')))
597 filter_module.init(**filter_args)
599 traceback.print_exc()
600 elif options.filter_module.endswith('.so'):
601 filter_module = ctypes.cdll.LoadLibrary(options.filter_module)
602 if options.filter_args:
604 filter_module.init(options.filter_args)
606 traceback.print_exc()
608 accept_packet = filter_module.accept_packet
609 print >>sys.stderr, "Installing packet filter (accept_packet)"
614 queueclass = filter_module.queueclass
615 print >>sys.stderr, "Installing custom queue"
620 _filter_init = filter_module.filter_init
621 filter_run = filter_module.filter_run
622 filter_close = filter_module.filter_close
625 filter_local = ctypes.c_int(0)
626 filter_remote = ctypes.c_int(0)
627 _filter_init(filter_local, filter_remote)
628 return filter_local, filter_remote
630 print >>sys.stderr, "Installing packet filter (stream filter)"
642 # install multicast forwarding hook
643 if options.multicast_fwd:
644 print >>sys.stderr, "Connecting to mcast filter"
645 mcfwd_sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
646 #disable nonblocking, cannot handle EWOULDBLOCK
647 #tunchannel.nonblock(mcfwd_sock.fileno())
648 mcfwd_sock.settimeout(0.5) # 500ms tops - packet lost if it blocks more than that
650 # be careful to roll back stuff on exceptions
651 tun_path, tun_name = modeinfo['alloc'](tun_path, tun_name)
653 modeinfo['start'](tun_path, tun_name)
655 tun = modeinfo['tunopen'](tun_path, tun_name)
657 modeinfo['stop'](tun_path, tun_name)
660 modeinfo['dealloc'](tun_path, tun_name)
664 # Trak SIGTERM, and set global termination flag instead of dying
666 def _finalize(sig,frame):
668 TERMINATE.append(None)
669 signal.signal(signal.SIGTERM, _finalize)
676 # install multicast forwarding hook
677 if options.multicast_fwd:
678 print >>sys.stderr, "Installing mcast filter"
681 writev = iovec.writev
686 def writev(fileno, *stuff):
687 os_write(''.join(map_(str_,stuff)))
690 mcfwd_queue = Queue(options.vif_txqueuelen or 500)
693 sockno=mcfwd_sock.fileno(),
694 multicast_fwd = options.multicast_fwd,
695 vif_addr = socket.inet_aton(options.vif_addr),
697 retrycodes=(os.errno.EWOULDBLOCK, os.errno.EAGAIN, os.errno.EINTR),
699 TERMINATE_ = TERMINATE
702 while not TERMINATE_:
704 fwd = mcfwd_queue.get(True, 1)
711 sock.connect(multicast_fwd)
714 traceback.print_exc(file=sys.stderr)
717 writev(sockno, vif_addr,fwd)
719 if e.errno not in retrycodes:
720 traceback.print_exc(file=sys.stderr)
723 writev(sockno, vif_addr,fwd)
725 traceback.print_exc(file=sys.stderr)
726 except socket.timeout:
730 traceback.print_exc(file=sys.stderr)
732 mcfwd_queue.task_done()
733 mcfwd_thread = threading.Thread(target=mcfwd_thread)
736 def accept_packet(packet, direction,
737 _up_accept=accept_packet,
738 etherProto=tunchannel.etherProto,
739 etherStrip=tunchannel.etherStrip,
740 etherMode=tun_name.startswith('tap'),
743 rv = _up_accept(packet, direction)
750 if etherProto(packet)=='\x08\x00':
751 fwd = etherStrip(packet)
756 if fwd is not None and len(fwd) >= 20:
757 if (ord(fwd[16]) & 0xf0) == 0xe0:
758 # Queue for forwarding
760 mcfwd_queue.put_nowait(fwd)
762 print >>sys.stderr, "Multicast packet dropped, forwarder queue full"
766 if options.protocol == 'fd':
767 if accept_packet or filter_init:
768 raise NotImplementedError, "--pass-fd and --filter are not compatible"
770 if options.pass_fd.startswith("base64:"):
771 options.pass_fd = base64.b64decode(
772 options.pass_fd[len("base64:"):])
773 options.pass_fd = os.path.expandvars(options.pass_fd)
775 print >>sys.stderr, "Sending FD to: %r" % (options.pass_fd,)
777 # send FD to whoever wants it
780 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
784 raise OSError, "Killed"
786 sock.connect(options.pass_fd)
789 # wait a while, retry
790 print >>sys.stderr, "%s: Could not connect. Retrying in a sec..." % (time.strftime('%c'),)
791 time.sleep(min(30.0,retrydelay))
794 sock.connect(options.pass_fd)
795 passfd.sendfd(sock, tun.fileno(), '0')
798 def tun_fwd(tun, remote, **kw):
804 elif options.protocol == "gre":
805 if accept_packet or filter_init:
806 raise NotImplementedError, "--mode %s and --filter are not compatible" % (options.mode,)
809 def tun_fwd(tun, remote, **kw):
814 remote = options.peer_addr
815 elif options.protocol == "udp":
816 # connect to remote endpoint
817 if options.peer_addr and options.peer_port:
818 rsock = tunchannel.udp_establish(TERMINATE, hostaddr, options.port,
819 options.peer_addr, options.peer_port)
820 remote = os.fdopen(rsock.fileno(), 'r+b', 0)
822 print >>sys.stderr, "Error: need a remote endpoint in UDP mode"
823 raise AssertionError, "Error: need a remote endpoint in UDP mode"
824 elif options.protocol == "tcp":
825 # connect to remote endpoint
826 if options.peer_addr and options.peer_port:
827 rsock = tunchannel.tcp_establish(TERMINATE, hostaddr, options.port,
828 options.peer_addr, options.peer_port)
829 remote = os.fdopen(rsock.fileno(), 'r+b', 0)
831 print >>sys.stderr, "Error: need a remote endpoint in TCP mode"
832 raise AssertionError, "Error: need a remote endpoint in TCP mode"
834 msg = "Error: Invalid protocol %s" % options.protocol
835 print >>sys.stderr, msg
836 raise AssertionError, msg
839 filter_local, filter_remote = filter_init()
846 remote = filter_remote
849 filter_close(local, remote)
851 filter_thread = threading.Thread(target=filter_loop)
852 filter_thread.start()
854 print >>sys.stderr, "Connected"
856 if not options.no_capture:
857 # Launch a tcpdump subprocess, to capture and dump packets.
858 # Make sure to catch sigterm and kill the tcpdump as well
859 tcpdump = subprocess.Popen(
860 ["tcpdump","-l","-n","-i",tun_name, "-s", "4096"]
861 + ["-w",options.pcap_capture,"-U"] * bool(options.pcap_capture) )
863 # Try to give us high priority
867 # Ignore errors, we might not have enough privileges,
868 # or perhaps there is no os.nice support in the system
873 reconnect = reconnect,
874 accept_local = accept_packet,
875 accept_remote = accept_packet,
876 bwlimit = options.bwlimit,
881 # 1. Forward packets from tun to filter
882 # 2. Forward packets from remote to filter
884 # 1. needs TUN rate-limiting, while
885 # 2. needs reconnection
887 # 1. needs ONLY TUN-side acceptance checks, while
888 # 2. needs ONLY remote-side acceptance checks
889 if isinstance(filter_local, ctypes.c_int):
890 filter_local_fd = filter_local.value
892 filter_local_fd = filter_local
893 if isinstance(filter_remote, ctypes.c_int):
894 filter_remote_fd = filter_remote.value
896 filter_remote_fd = filter_remote
899 tun_fwd(tun, filter_local_fd,
900 accept_local = accept_packet,
904 tun_fwd(filter_remote_fd, remote,
905 reconnect = reconnect,
906 accept_remote = accept_packet,
907 bwlimit = options.bwlimit,
910 localthread = threading.Thread(target=localside)
911 remotethread = threading.Thread(target=remoteside)
919 print >>sys.stderr, "Shutting down..."
921 # In case sys.stderr is broken
924 # tidy shutdown in every case - swallow exceptions
925 TERMINATE.append(None)
941 os.kill(tcpdump.pid, signal.SIGTERM)
947 modeinfo['stop'](tun_path, tun_name)
949 traceback.print_exc()
952 modeinfo['tunclose'](tun_path, tun_name, tun)
954 traceback.print_exc()
957 modeinfo['dealloc'](tun_path, tun_name)
959 traceback.print_exc()
961 print >>sys.stderr, "TERMINATED GRACEFULLY"