From owner-freebsd-hackers Thu Sep 20 23:58:58 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from whale.sunbay.crimea.ua (whale.sunbay.crimea.ua [212.110.138.65]) by hub.freebsd.org (Postfix) with ESMTP id 438BB37B41B for ; Thu, 20 Sep 2001 23:58:43 -0700 (PDT) Received: (from ru@localhost) by whale.sunbay.crimea.ua (8.11.2/8.11.2) id f8L6v4130766; Fri, 21 Sep 2001 09:57:04 +0300 (EEST) (envelope-from ru) Date: Fri, 21 Sep 2001 09:57:04 +0300 From: Ruslan Ermilov To: David Preece Cc: davidc@acns.ab.ca, sridharv@ufl.edu, bright@mu.org, hackers@FreeBSD.ORG Subject: Re: Raw sockets: Stevens shall provide. Message-ID: <20010921095704.F27714@sunbay.com> References: <01092112052007.36513@development.wgtn.csg.co.nz> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="NzB8fVQJ5HfG6fxh" Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <01092112052007.36513@development.wgtn.csg.co.nz>; from davep@afterswish.com on Fri, Sep 21, 2001 at 12:05:20PM +1200 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG --NzB8fVQJ5HfG6fxh Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Sep 21, 2001 at 12:05:20PM +1200, David Preece wrote: > Thanks for your help, as suggested the combination of IP_HDRINCL and reading > Stevens seems to have me back on the right path. Part of the confusion was > the Linux implementation of IP_HDRINCL appears to need the hardware addresses > writing as well, consequently there are lots of examples of this being done > in order to forge arp replies etc - not what I needed. Or rather, it was > actually. > > Anyway, the code is now refusing to send packets entirely, which is actually > good because it implies a checksum is broken somewhere and that's a bug, not > a misunderstanding about the API. > I didn't follow the thread, but the error may be caused by supplying some header fields in incorrect byte order. Some fields are expected in host byte order, and some are in net byte order, e.g. the ip_id (if non-zero). I wrote the demo program that uses IP_HDRINCL option, attached. Cheers, -- Ruslan Ermilov Oracle Developer/DBA, ru@sunbay.com Sunbay Software AG, ru@FreeBSD.org FreeBSD committer, +380.652.512.251 Simferopol, Ukraine http://www.FreeBSD.org The Power To Serve http://www.oracle.com Enabling The Information Age --NzB8fVQJ5HfG6fxh Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="rawip.c" #include #include #include #include #include #include #include #include #include struct udppacket { struct ip ip; struct udphdr uh; char data[IP_MAXPACKET - sizeof(struct ip) - sizeof(struct udphdr)]; }; /* struct ip { }; struct udphdr { u_short uh_sport; # source port u_short uh_dport; # destination port u_short uh_ulen; # udp length u_short uh_sum; # udp checksum }; */ int main(void) { int s; int hincl = 1; struct udppacket packet; struct sockaddr_in to; bzero(&to, sizeof to); to.sin_len = sizeof to; to.sin_family = AF_INET; to.sin_addr.s_addr = htonl(INADDR_LOOPBACK); to.sin_port = 0; bzero(&packet, offsetof(struct udppacket, data)); packet.ip.ip_v = IPVERSION; packet.ip.ip_hl = sizeof(packet.ip) >> 2; packet.ip.ip_len = sizeof(packet.ip) + sizeof(packet.uh); packet.ip.ip_id = 0; packet.ip.ip_off = 0; packet.ip.ip_ttl = MAXTTL; packet.ip.ip_p = IPPROTO_UDP; packet.ip.ip_src.s_addr = packet.ip.ip_dst.s_addr = htonl(INADDR_LOOPBACK); if ((s = socket(AF_INET, SOCK_RAW, 0)) == -1) err(1, "socket"); if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl)) == -1) err(1, "setsockopt(IP_HDRINCL)"); if (sendto(s, &packet, 28, 0, (struct sockaddr *)&to, sizeof to) == -1) err(1, "sendto"); return (0); } --NzB8fVQJ5HfG6fxh-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message