From owner-freebsd-net@freebsd.org Thu Apr 5 09:53:41 2018 Return-Path: Delivered-To: freebsd-net@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 36B5BF9A1E4 for ; Thu, 5 Apr 2018 09:53:41 +0000 (UTC) (envelope-from julian@freebsd.org) Received: from vps1.elischer.org (vps1.elischer.org [204.109.63.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "vps1.elischer.org", Issuer "CA Cert Signing Authority" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id BB9D473054 for ; Thu, 5 Apr 2018 09:53:40 +0000 (UTC) (envelope-from julian@freebsd.org) Received: from Julian-MBP3.local (115-166-20-68.dyn.iinet.net.au [115.166.20.68]) (authenticated bits=0) by vps1.elischer.org (8.15.2/8.15.2) with ESMTPSA id w359rRbr061820 (version=TLSv1.2 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Thu, 5 Apr 2018 02:53:30 -0700 (PDT) (envelope-from julian@freebsd.org) Subject: Re: Raw Sockets: Two Questions To: Eugene Grosbein , "Ronald F. Guilmette" , FreeBSD Net References: <3559.1521655704@segfault.tristatelogic.com> <5AB2AD9F.6040600@grosbein.net> From: Julian Elischer Message-ID: <4f1c0fe3-ad6e-80d3-9ae2-210c63af355b@freebsd.org> Date: Thu, 5 Apr 2018 17:53:21 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <5AB2AD9F.6040600@grosbein.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 05 Apr 2018 09:53:41 -0000 On 22/3/18 3:08 am, Eugene Grosbein wrote: > 22.03.2018 1:08, Ronald F. Guilmette wrote: > >> OK, so, if I have understood all that has been said in this thread so >> far, then I would assert that, from the perspective of a simple-minded >> and naive end user (e.g. me), the assertion that I originally quoted >> -is- in fact correct, i.e. one -cannot- just simply do sendto/recvfrom >> (and expect to get back responses) if the raw packets that one sends out >> happen to be, for example, well formed TCP or UDP packets. > Not exactly. You can't use raw sockets to receive but that does not mean > you cannot use sendto/recvfrom (or similar calls) at all: there are > libpcap, libdnet and NETGRAPH allowing to send requests and receive answers. > >> If I have correctly understood Matt Joras, there -are- ways to get hold >> of such reply packets, under FreeBSD, but those require getting a bit more >> "under the hood" in order to actually get hold of them... more than just >> a simple recvfrom on the RAW socket. > Why should you concentrate on RAW sockets? > > I have small perl script that sends manually crafted PPPoE frames > and receives replies using simple libpcap interface: > > use Net::Pcap qw(:DEFAULT :functions); > > use constant V8021Q => 0x8100; > use constant ETHERTYPE_PPPOEDISC => 0x8863; > use constant PPPOE_VER => 1; > use constant PPPOE_TYPE => 1; > use constant PADO_CODE => 7; > use constant PADI_CODE => 9; > use constant TAG_END_OF_LIST => 0x0000; > use constant TAG_SERVICE_NAME => 0x0101; > use constant TAG_AC_NAME => 0x0102; > use constant TAG_HOST_UNIQ => 0x0103; > > $packet = > # Ethernet header: dst MAC, src MAC, TYPE > ether_aton('ff:ff:ff:ff:ff:ff') . $bmac . pack('n', ETHERTYPE_PPPOEDISC) . > # PPPoE PADI: VER, TYPE, CODE, SESSION_ID=0 > pack('C', (PPPOE_VER<<4) + PPPOE_TYPE) . pack('C', PADI_CODE) . pack('n', 0) > # LENGTH, tags > pack('n', $tlen) . $tags; > # zero padding upto 60 bytes ethernet frame length (without checksum) > $packet .= pack('a' . (40-$tlen) , '') if $tlen < 40; > > err("cannot open interface $interface: $err") > unless $pcap = pcap_open_live($interface, $snaplen, 0, 0, \$err); > err("could not send PADI") > if pcap_sendpacket($pcap, $packet) != 0; > > $filter = "ether proto " . ETHERTYPE_PPPOEDISC . " and ether dst $mac"; > err("cannot compile filter: $filter") > if pcap_compile($pcap, \$bpf, $filter, 1, 0) < 0; > pcap_setfilter($pcap, $bpf); > > $ec = 0; > while($ec == 0) { > $ec = pcap_loop($pcap, -1, \&callback, undef); > } > pcap_close($pcap); > exit(0); > > sub callback($$$) { > return if $_[1]->{'len'} < 20; # sanity check: short frame > my ($dst, $src, $ftype, $ftag, $fp) = unpack('a6a6na4a*' , $_[2]); > ... > } why not just use the ng-pppoe node? :-) > _______________________________________________ > freebsd-net@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-net > To unsubscribe, send any mail to "freebsd-net-unsubscribe@freebsd.org" >