From owner-freebsd-net@freebsd.org Wed Mar 21 19:08:31 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 520D8F5E302 for ; Wed, 21 Mar 2018 19:08:31 +0000 (UTC) (envelope-from eugen@grosbein.net) Received: from hz.grosbein.net (hz.grosbein.net [78.47.246.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "hz.grosbein.net", Issuer "hz.grosbein.net" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id DEF5D824A3 for ; Wed, 21 Mar 2018 19:08:30 +0000 (UTC) (envelope-from eugen@grosbein.net) Received: from eg.sd.rdtc.ru (root@eg.sd.rdtc.ru [62.231.161.221] (may be forged)) by hz.grosbein.net (8.15.2/8.15.2) with ESMTPS id w2LJ8NX1090988 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 21 Mar 2018 20:08:24 +0100 (CET) (envelope-from eugen@grosbein.net) X-Envelope-From: eugen@grosbein.net X-Envelope-To: rfg@tristatelogic.com Received: from [10.58.0.4] ([10.58.0.4]) by eg.sd.rdtc.ru (8.15.2/8.15.2) with ESMTPS id w2LJ8KfW058508 (version=TLSv1.2 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT); Thu, 22 Mar 2018 02:08:20 +0700 (+07) (envelope-from eugen@grosbein.net) Subject: Re: Raw Sockets: Two Questions To: "Ronald F. Guilmette" , FreeBSD Net References: <3559.1521655704@segfault.tristatelogic.com> From: Eugene Grosbein Message-ID: <5AB2AD9F.6040600@grosbein.net> Date: Thu, 22 Mar 2018 02:08:15 +0700 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 MIME-Version: 1.0 In-Reply-To: <3559.1521655704@segfault.tristatelogic.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=2.2 required=5.0 tests=BAYES_00, LOCAL_FROM, RDNS_NONE autolearn=no autolearn_force=no version=3.4.1 X-Spam-Report: * -2.3 BAYES_00 BODY: Bayes spam probability is 0 to 1% * [score: 0.0000] * 2.6 LOCAL_FROM From my domains * 1.9 RDNS_NONE Delivered to internal network by a host with no rDNS X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on hz.grosbein.net 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: Wed, 21 Mar 2018 19:08:31 -0000 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]); ... }