From nobody@FreeBSD.org  Mon Oct 21 15:04:42 2002
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 83B1437B401
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 21 Oct 2002 15:04:42 -0700 (PDT)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id D0CA043E4A
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 21 Oct 2002 15:04:41 -0700 (PDT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.12.6/8.12.6) with ESMTP id g9LM4f7R026695
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 21 Oct 2002 15:04:41 -0700 (PDT)
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.6/8.12.6/Submit) id g9LM4fAB026694;
	Mon, 21 Oct 2002 15:04:41 -0700 (PDT)
Message-Id: <200210212204.g9LM4fAB026694@www.freebsd.org>
Date: Mon, 21 Oct 2002 15:04:41 -0700 (PDT)
From: Brian Lai <junwen_lai@yahoo.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: possible raw socket bug
X-Send-Pr-Version: www-1.0

>Number:         44361
>Category:       misc
>Synopsis:       possible raw socket bug
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Oct 21 15:10:02 PDT 2002
>Closed-Date:    Sat Jan 18 14:48:05 PST 2003
>Last-Modified:  Sun Jan 19 08:50:02 PST 2003
>Originator:     Brian Lai
>Release:        4.5
>Organization:
no
>Environment:
FreeBSD 4.5-RELEASE
>Description:
look at rip_output@sys/netinet/raw_ip.c, ip->ip_len, which is 
in network byte order, is compared against m->m_pkthdr.len which is in
host byte order.

This bug is found when I am developing a user level TCP/IP stack.

As far as I know, this bug exits in 4.7 and 5.0-CURRENT.



>How-To-Repeat:
      
>Fix:
add
----------------
NTOHS(ip->ip_len);
NTOHS(ip->ip_off);
----------------
after 
----------------
} else {
		if (m->m_pkthdr.len > IP_MAXPACKET) {
			m_freem(m);
			return(EMSGSIZE);
		}
		ip = mtod(m, struct ip *);

----------------
>Release-Note:
>Audit-Trail:

From: Alfred Perlstein <bright@mu.org>
To: freebsd-gnats-submit@FreeBSD.org
Cc: net@freebsd.org
Subject: Re: misc/44361: possible raw socket bug
Date: Sat, 18 Jan 2003 13:08:14 -0800

 It appears that we expect the ip_len and ip_off feilds to be sent
 in host byte order as the stack will fix it to network byte order
 in ip_output.
 
 Is this a bug or feature? :)
 
 -- 
 -Alfred Perlstein [alfred@freebsd.org]

State-Changed-From-To: open->closed 
State-Changed-By: hsu 
State-Changed-When: Sat Jan 18 14:41:41 PST 2003 
State-Changed-Why:  
This is not a bug.  From Stevens, Unix Network Programming, Vol 1, page 657: 

On Berkeley-derived kernels, all fields are in network byte 
order except ip_len and ip_off, whch are in host byte order. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=44361 

From: Kelly Yancey <kbyanc@posi.net>
To: Alfred Perlstein <bright@mu.org>
Cc: freebsd-gnats-submit@FreeBSD.ORG, <net@FreeBSD.ORG>
Subject: Re: misc/44361: possible raw socket bug
Date: Sat, 18 Jan 2003 21:45:40 -0800 (PST)

 On Sat, 18 Jan 2003, Alfred Perlstein wrote:
 
 > It appears that we expect the ip_len and ip_off feilds to be sent
 > in host byte order as the stack will fix it to network byte order
 > in ip_output.
 >
 > Is this a bug or feature? :)
 >
 > --
 > -Alfred Perlstein [alfred@freebsd.org]
 
   Both, no? :)  It's a bug documented in Stevens TCP/IP Illustrated 2 as
 being around since 4.4BSD, but I would expect that fixing it would break a
 good bit.  On the other hand, it is supposedly fixed in OpenBSD.
 
   Kelly
 
 --
 Kelly Yancey -- kbyanc@{posi.net,FreeBSD.org}
 "The fact that a believer is happier than a skeptic is no more to the point
  than the fact than a drunken man is happier than a sober one."
 	-- George Bernard Shaw
 

From: Hiten Pandya <hiten@unixdaemons.com>
To: Kelly Yancey <kbyanc@posi.net>, bug-followup@FreeBSD.ORG
Cc: alfred@FreeBSD.ORG
Subject: Re: misc/44361: possible raw socket bug
Date: Sun, 19 Jan 2003 15:25:50 +0000

 This is a multi-part message in MIME format.
 --------------030508090504070801080703
 Content-Type: text/plain; charset=us-ascii; format=flowed
 Content-Transfer-Encoding: 7bit
 
 --- Kelly Yancey <kbyanc@posi.net> wrote:
   > On Sat, 18 Jan 2003, Alfred Perlstein wrote:
   >
   > : It appears that we expect the ip_len and ip_off feilds to :
   > be sent in host byte order as the stack will fix it to :
   > network byte order in ip_output. : : Is this a bug or feature?
   > :)
   >
   > Both, no? :)  It's a bug documented in Stevens TCP/IP
   > Illustrated 2 as being around since 4.4BSD, but I would expect
   >  that fixing it would break a good bit.  On the other hand, it
   >  is supposedly fixed in OpenBSD.
 
 We should follow the OpenBSD behaviour [1], which is what the
 submitter has told us (using ntohs() for both ip_len, and
 ip_off).  It was fixed 5 years ago in OpenBSD by deraadt@.
 
 Doing some research shows that NetBSD use HTONS(), because of an
 mbuf optimization they made [2] (it was fixed 5 months ago in
 their raw_ip.c); I.e. they convert m->m_pkthdr.len to host order
    for ip_len, and htons(0) for ip_off:
 
 \begin{verbatim}
 	/* From: NetBSD src/sys/netinet/raw_ip.c */
 	ip->ip_off = htons(0);
 	/* ... */
 	ip->ip_len = htons(m->m_pkthdr.len);
 
 	/* XXX userland passes ip_len and ip_off in host order */
 	if (m->m_pkthdr.len != ip->ip_len) {
 		m_freem(m);
 		return (EINVAL);
 	}
 	HTONS(ip->ip_len);
 	HTONS(ip->ip_off);
 \end{verbatim}
 
 For the reference, two Problem Reports were submitted to NetBSD
 for this same problem. [3]
 
 I have attached a fix to HEAD with this mail.  It should be
 MFC'ed, if possible.  It is also available from:
 http://www.unixdaemons.com/~hiten/work/diffs/netinet+ip_fields-fix.patch
 
 Reference:
 [1] - diff(1) rev. 1.8 and 1.9 of OpenBSD raw_ip.c
 [2] - diff(1) rev. 1.61 and 1.62 of NetBSD raw_ip.c
 [3] - NetBSD PR: 17867 and 10195
 
 Index: sys/netinet/raw_ip.c
 ===================================================================
 RCS file: /home/ncvs/src/sys/netinet/raw_ip.c,v
 retrieving revision 1.108
 diff -u -r1.108 raw_ip.c
 --- sys/netinet/raw_ip.c	2003/01/18 01:10:55	1.108
 +++ sys/netinet/raw_ip.c	2003/01/19 15:15:09
 @@ -302,6 +302,8 @@
   			return(EMSGSIZE);
   		}
   		ip = mtod(m, struct ip *);
 +		ntohs(ip->ip_len);
 +		ntohs(ip->ip_off);
   		/* don't allow both user specified and setsockopt options,
   		   and don't allow packet length sizes that will crash */
   		if (((ip->ip_hl != (sizeof (*ip) >> 2))
 
 Cheers.
 
 -- 
 Hiten Pandya
 http://www.unixdaemons.com/~hiten
 hiten@uk.FreeBSD.org, hiten@unixdaemons.com
 
 
 --------------030508090504070801080703
 Content-Type: text/plain;
  name="netinet+ip_fields-fix.patch"
 Content-Transfer-Encoding: 7bit
 Content-Disposition: inline;
  filename="netinet+ip_fields-fix.patch"
 
 Index: sys/netinet/raw_ip.c
 ===================================================================
 RCS file: /home/ncvs/src/sys/netinet/raw_ip.c,v
 retrieving revision 1.108
 diff -u -r1.108 raw_ip.c
 --- sys/netinet/raw_ip.c	2003/01/18 01:10:55	1.108
 +++ sys/netinet/raw_ip.c	2003/01/19 15:15:09
 @@ -302,6 +302,8 @@
  			return(EMSGSIZE);
  		}
  		ip = mtod(m, struct ip *);
 +		ntohs(ip->ip_len);
 +		ntohs(ip->ip_off);
  		/* don't allow both user specified and setsockopt options,
  		   and don't allow packet length sizes that will crash */
  		if (((ip->ip_hl != (sizeof (*ip) >> 2))
 
 
 --------------030508090504070801080703--
 

From: Hiten Pandya <hiten@unixdaemons.com>
To: bug-followup@FreeBSD.ORG
Cc:  
Subject: Re: misc/44361: possible raw socket bug
Date: Sun, 19 Jan 2003 15:53:12 +0000

 Ignore my request for commit to HEAD and MFC in the previous
 follow-up, as this would not be the best time for it.
 
 Cheers.
 
 -- 
 Hiten Pandya
 http://www.unixdaemons.com/~hiten
 hiten@uk.FreeBSD.org, hiten@unixdaemons.com
 

From: Alexander Langer <alex@big.endian.de>
To: freebsd-gnats-submit@FreeBSD.org, junwen_lai@yahoo.com
Cc:  
Subject: Re: misc/44361: possible raw socket bug
Date: Sun, 19 Jan 2003 17:01:59 +0100

 This particular bug or feature causes more problems.
 
 compare
 http://docs.freebsd.org/cgi/getmsg.cgi?fetch=238175+0+archive/2002/freebsd-hackers/20021215.freebsd-hackers
 
 which is not yet committed.
 
 Alex
 
 

From: Alexander Langer <alex@big.endian.de>
To: freebsd-gnats-submit@FreeBSD.org, junwen_lai@yahoo.com
Cc:  
Subject: Re: misc/44361: possible raw socket bug
Date: Sun, 19 Jan 2003 17:41:54 +0100

 nerver mind, as maxim pointed out, Ian already fixed this.  I must have 
 overlooked that, sorry.
 
 Alex
 
 
>Unformatted:

-------------------

From: Jeffrey Hsu <hsu@freebsd.org>

This is not a bug.  From Stevens, Unix Network Programming, Vol 1, page 657:

	On Berkeley-derived kernels, all fields are in network byte
	order except ip_len and ip_off, whch are in host byte order.
