ruby - TCP tunneling through Linux tap device -
i've created tap0 device (ip 10.0.0.101), , using zeromq's pgm pub/sub (e.g. pgm://192.168.100.2;234.5.6.7:5555) transport ethernet frames tap zmq subscribers , vice versa. idea create virtual network using pgm. have 2 tap hosts on network: 10.0.0.101, 10.0.0.11. have physical ethernet adapters @ 192.168.106.126, 192.168.106.55.
'the problem ping works, http , ssh protocols not.
wireshark shows successful tcp startup sequence, start see duplicate acks, retransmissions, , curl , ssh hang while , error out.
a snippet wireshark below, followed of (hopefully) relevant ruby source code. using rb_tuntap , ffi-rzmq gems.
no. time source destination protocol length info 7 11:41:45.464867000 10.0.0.11 10.0.0.101 tcp 74 51659 > 3000 [syn] seq=0 win=14600 len=0 mss=1460 sack_perm=1 tsval=1953042 tsecr=0 ws=64 frame 7: 74 bytes on wire (592 bits), 74 bytes captured (592 bits) on interface 0 ethernet ii, src: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f), dst: 56:c8:52:17:31:67 (56:c8:52:17:31:67) internet protocol version 4, src: 10.0.0.11 (10.0.0.11), dst: 10.0.0.101 (10.0.0.101) transmission control protocol, src port: 51659 (51659), dst port: 3000 (3000), seq: 0, len: 0 no. time source destination protocol length info 8 11:41:45.464956000 10.0.0.101 10.0.0.11 tcp 74 3000 > 51659 [syn, ack] seq=0 ack=1 win=28960 len=0 mss=1460 sack_perm=1 tsval=10191992 tsecr=1953042 ws=128 frame 8: 74 bytes on wire (592 bits), 74 bytes captured (592 bits) on interface 0 ethernet ii, src: 56:c8:52:17:31:67 (56:c8:52:17:31:67), dst: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f) internet protocol version 4, src: 10.0.0.101 (10.0.0.101), dst: 10.0.0.11 (10.0.0.11) transmission control protocol, src port: 3000 (3000), dst port: 51659 (51659), seq: 0, ack: 1, len: 0 no. time source destination protocol length info 11 11:41:45.473101000 10.0.0.11 10.0.0.101 tcp 66 51659 > 3000 [ack] seq=1 ack=1 win=14656 len=0 tsval=1953044 tsecr=10191992 frame 11: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0 ethernet ii, src: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f), dst: 56:c8:52:17:31:67 (56:c8:52:17:31:67) internet protocol version 4, src: 10.0.0.11 (10.0.0.11), dst: 10.0.0.101 (10.0.0.101) transmission control protocol, src port: 51659 (51659), dst port: 3000 (3000), seq: 1, ack: 1, len: 0 no. time source destination protocol length info 12 11:41:45.473429000 10.0.0.11 10.0.0.101 http 145 / http/1.1 frame 12: 145 bytes on wire (1160 bits), 145 bytes captured (1160 bits) on interface 0 ethernet ii, src: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f), dst: 56:c8:52:17:31:67 (56:c8:52:17:31:67) internet protocol version 4, src: 10.0.0.11 (10.0.0.11), dst: 10.0.0.101 (10.0.0.101) transmission control protocol, src port: 51659 (51659), dst port: 3000 (3000), seq: 1, ack: 1, len: 79 hypertext transfer protocol no. time source destination protocol length info 13 11:41:45.473460000 10.0.0.101 10.0.0.11 tcp 66 3000 > 51659 [ack] seq=1 ack=80 win=29056 len=0 tsval=10192001 tsecr=1953046 frame 13: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0 ethernet ii, src: 56:c8:52:17:31:67 (56:c8:52:17:31:67), dst: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f) internet protocol version 4, src: 10.0.0.101 (10.0.0.101), dst: 10.0.0.11 (10.0.0.11) transmission control protocol, src port: 3000 (3000), dst port: 51659 (51659), seq: 1, ack: 80, len: 0 no. time source destination protocol length info 15 11:41:45.491555000 10.0.0.101 10.0.0.11 tcp 717 [tcp segment of reassembled pdu] frame 15: 717 bytes on wire (5736 bits), 717 bytes captured (5736 bits) on interface 0 ethernet ii, src: 56:c8:52:17:31:67 (56:c8:52:17:31:67), dst: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f) internet protocol version 4, src: 10.0.0.101 (10.0.0.101), dst: 10.0.0.11 (10.0.0.11) transmission control protocol, src port: 3000 (3000), dst port: 51659 (51659), seq: 1, ack: 80, len: 651 no. time source destination protocol length info 16 11:41:45.491599000 10.0.0.101 10.0.0.11 tcp 1514 [tcp segment of reassembled pdu] frame 16: 1514 bytes on wire (12112 bits), 1514 bytes captured (12112 bits) on interface 0 ethernet ii, src: 56:c8:52:17:31:67 (56:c8:52:17:31:67), dst: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f) internet protocol version 4, src: 10.0.0.101 (10.0.0.101), dst: 10.0.0.11 (10.0.0.11) transmission control protocol, src port: 3000 (3000), dst port: 51659 (51659), seq: 652, ack: 80, len: 1448 no. time source destination protocol length info 21 11:41:45.496998000 10.0.0.11 10.0.0.101 tcp 66 51659 > 3000 [ack] seq=80 ack=652 win=17536 len=0 tsval=1953058 tsecr=10192019 frame 21: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0 ethernet ii, src: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f), dst: 56:c8:52:17:31:67 (56:c8:52:17:31:67) internet protocol version 4, src: 10.0.0.11 (10.0.0.11), dst: 10.0.0.101 (10.0.0.101) transmission control protocol, src port: 51659 (51659), dst port: 3000 (3000), seq: 80, ack: 652, len: 0 no. time source destination protocol length info 22 11:41:45.497026000 10.0.0.101 10.0.0.11 http 231 http/1.1 200 ok (text/html) frame 22: 231 bytes on wire (1848 bits), 231 bytes captured (1848 bits) on interface 0 ethernet ii, src: 56:c8:52:17:31:67 (56:c8:52:17:31:67), dst: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f) internet protocol version 4, src: 10.0.0.101 (10.0.0.101), dst: 10.0.0.11 (10.0.0.11) transmission control protocol, src port: 3000 (3000), dst port: 51659 (51659), seq: 2100, ack: 80, len: 165 [3 reassembled tcp segments (2264 bytes): #15(651), #16(1448), #22(165)] hypertext transfer protocol line-based text data: text/html no. time source destination protocol length info 25 11:41:45.502736000 10.0.0.11 10.0.0.101 tcp 78 [tcp dup ack 21#1] 51659 > 3000 [ack] seq=80 ack=652 win=17536 len=0 tsval=1953061 tsecr=10192019 sle=2100 sre=2265 frame 25: 78 bytes on wire (624 bits), 78 bytes captured (624 bits) on interface 0 ethernet ii, src: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f), dst: 56:c8:52:17:31:67 (56:c8:52:17:31:67) internet protocol version 4, src: 10.0.0.11 (10.0.0.11), dst: 10.0.0.101 (10.0.0.101) transmission control protocol, src port: 51659 (51659), dst port: 3000 (3000), seq: 80, ack: 652, len: 0 no. time source destination protocol length info 26 11:41:45.504245000 10.0.0.101 10.0.0.11 tcp 1514 [tcp retransmission] 3000 > 51659 [ack] seq=652 ack=80 win=29056 len=1448 tsval=10192032 tsecr=1953061[reassembly error, protocol tcp: new fragment overlaps old data (retransmission?)] frame 26: 1514 bytes on wire (12112 bits), 1514 bytes captured (12112 bits) on interface 0 ethernet ii, src: 56:c8:52:17:31:67 (56:c8:52:17:31:67), dst: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f) internet protocol version 4, src: 10.0.0.101 (10.0.0.101), dst: 10.0.0.11 (10.0.0.11) transmission control protocol, src port: 3000 (3000), dst port: 51659 (51659), seq: 652, ack: 80, len: 1448 [reassembly error, protocol tcp: new fragment overlaps old data (retransmission?)] no. time source destination protocol length info 33 11:41:45.711324000 10.0.0.101 10.0.0.11 tcp 1514 [tcp retransmission] 3000 > 51659 [ack] seq=652 ack=80 win=29056 len=1448 tsval=10192239 tsecr=1953061[reassembly error, protocol tcp: new fragment overlaps old data (retransmission?)] frame 33: 1514 bytes on wire (12112 bits), 1514 bytes captured (12112 bits) on interface 0 ethernet ii, src: 56:c8:52:17:31:67 (56:c8:52:17:31:67), dst: 3a:e2:d5:f3:8e:6f (3a:e2:d5:f3:8e:6f) internet protocol version 4, src: 10.0.0.101 (10.0.0.101), dst: 10.0.0.11 (10.0.0.11) transmission control protocol, src port: 3000 (3000), dst port: 51659 (51659), seq: 652, ack: 80, len: 1448 [reassembly error, protocol tcp: new fragment overlaps old data (retransmission?)]
ruby code:
def run stop = false signal.trap("sigint") { stop=true } tap.up #binding.pry pids = [] begin # tap => zmq_pub pids << thread.new while !stop read_and_pub end end # zmq_sub => tap pids << thread.new while !stop sub_and_write end end rescue stop=true end pids.each { |pid| pid.join } tap.down tap.close self.tap = nil zmq_pub.close zmq_sub.close zmq_ctx.terminate end # run def read_and_pub selected = io.select([tap.to_io],nil,nil,1) if !selected.nil? , !selected[0].nil? , selected[0].length>0 msg = tap.to_io.sysread(tap.mtu) if !msg.nil? && msg.length > 0 sent = zmq_pub.send_string(msg) if sent != msg.length puts "**** published #{sent}/#{msg.length} ****" end print_packet msg, tap else puts "io.select returned #{selected} msg #{msg}" end end end def sub_and_write msg = '' zmq_sub.recv_string msg if !msg.nil? && msg.length > 0 sent = tap.to_io.syswrite(msg) if sent != msg.length puts "**** wrote #{sent}/#{msg.length} ****" end print_packet msg, zmq_sub end end
the problem here: tap.to_io.sysread(tap.mtu)
. seems tap.mtu not maximum number of bytes sysread receive, , unread bytes seem disarded tap interface. changed code tap.to_io.sysread(10000)
, http , ssh work expected. largest frame received 1514.
Comments
Post a Comment