From jin@iss-p1.lbl.gov  Fri Dec 20 11:34:15 1996
Received: from iss-p1.lbl.gov (iss-p1.lbl.gov [131.243.2.47])
          by freefall.freebsd.org (8.8.4/8.8.4) with ESMTP id LAA04766
          for <FreeBSD-gnats-submit@freebsd.org>; Fri, 20 Dec 1996 11:34:14 -0800 (PST)
Received: (from jin@localhost) by iss-p1.lbl.gov (8.8.2/8.8.2) id LAA02356; Fri, 20 Dec 1996 11:34:13 -0800 (PST)
Message-Id: <199612201934.LAA02356@iss-p1.lbl.gov>
Date: Fri, 20 Dec 1996 11:34:13 -0800 (PST)
From: "Jin Guojun[ITG]" <jin@george.lbl.gov>
Reply-To: jin@george.lbl.gov
To: FreeBSD-gnats-submit@freebsd.org
Subject: route add/delete [network] xxx.yyy.zzz.0 my-IP
X-Send-Pr-Version: 3.2

>Number:         2258
>Category:       bin
>Synopsis:       route add/delete [network] xxx.yyy.zzz.0 my-IP
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Dec 20 11:40:01 PST 1996
>Closed-Date:    Tue Jan 25 06:56:21 PST 2000
>Last-Modified:  Tue Jan 25 07:22:56 PST 2000
>Originator:     Jin Guojun[ITG]
>Release:        FreeBSD 2.2-ALPHA i386
>Organization:
>Environment:

	2.2-ALPHA on pentium with 3C509B ethernet cards

>Description:

	correct route add/delete command in 2.1.6

# route add 204.62.155.4 ir50gw 1
old usage of trailing digit, assuming route via gateway
add host 204.62.155.4: gateway ir50gw
# netstat -r
Routing tables

Internet:
Destination        Gateway            Flags     Refs     Use     Netif Expire
default            ir40gw             UGSc        4        1       ep0
localhost          localhost          UH          1       22       lo0
131.243.2/24       link#2             UC          0        0 
ir40gw             aa:0:4:0:86:a5     UHLW        3        0       ep0   1085
... all same as before (omitted)
204.62.155.4       ir50gw             UGHS        0        0       ep0

3# route delete 204.62.155.4       ir50gw
delete host 204.62.155.4: gateway ir50gw
iss-p3# netstat -r
Routing tables

Internet:
Destination        Gateway            Flags     Refs     Use     Netif Expire
default            ir40gw             UGSc        3        1       ep0
localhost          localhost          UH          1       22       lo0
131.243.2/24       link#2             UC          0        0 
ir40gw             aa:0:4:0:86:a5     UHLW        3        0       ep0    641
... all same as before (omitted)

********=================************

	wrong route add/delete command in 2.2-Alpha

# netstat -r
Routing tables

Internet:
Destination        Gateway            Flags     Refs     Use     Netif Expire
default            ir50gw             UGSc        6       68       ep0
localhost          localhost          UH          1       10       lo0
128.3.196/22       link#2             UC          0        0 
ir50gw             0:60:3e:52:6c:83   UHLW        6        0       ep0   1177
iss-p2             0:20:af:ec:32:62   UHLW        1       26       lo0
portnoy-2          8:0:20:80:19:b9    UHLW        8      124       ep0    582
george             8:0:20:1c:7:f5     UHLW        9       90       ep0    816
gracie             8:0:20:7f:15:a1    UHLW        3      227       ep0   1191
128.3.199.255      ff:ff:ff:ff:ff:ff  UHLWb       0        1       ep0
204.62.155         link#6             UC          0        0 
# route add 198.207.143.0 204.62.155.50 1
add net 198.207.143.0: gateway 204.62.155.50
# netstat -r
Routing tables

Internet:
Destination        Gateway            Flags     Refs     Use     Netif Expire
^C		(hanging)
# netstat -rn
Routing tables
		**** add wrong information into routing table *****
Internet:
Destination        Gateway            Flags     Refs     Use     Netif Expire
default            204.62.155.50      UGSc        1        0    znatm0 =>
default            128.3.196.10       UGSc        5       68       ep0
127.0.0.1          127.0.0.1          UH          1       10       lo0
128.3.196/22       link#2             UC          0        0 
128.3.196.10       0:60:3e:52:6c:83   UHLW        5        0       ep0   1168
128.3.196.71       0:20:af:ec:32:62   UHLW        1       26       lo0
128.3.196.85       8:0:20:80:19:b9    UHLW        8      124       ep0    495
128.3.196.93       8:0:20:1c:7:f5     UHLW        9       91       ep0    729
128.3.196.175      8:0:20:7f:15:a1    UHLW        3      430       ep0   1104
128.3.199.255      ff:ff:ff:ff:ff:ff  UHLWb       0        1       ep0
204.62.155         link#6             UC          0        0 
204.62.155.50      0:0:cc:3e:9b:32    UHLW        2        0       lo0
# route delete default            204.62.155.50
delete net default: gateway 204.62.155.50
# netstat -rn
Routing tables
	***** delete wrong router or had wrong info. in the routing table *****
Internet:
Destination        Gateway            Flags     Refs     Use     Netif Expire
default            204.62.155.50      UGSc        1        0    znatm0
127.0.0.1          127.0.0.1          UH          1       10       lo0
128.3.196/22       link#2             UC          0        0 
128.3.196.10       0:60:3e:52:6c:83   UHLW        0        0       ep0   1200
128.3.196.71       0:20:af:ec:32:62   UHLW        1       26       lo0
128.3.196.85       8:0:20:80:19:b9    UHLW        8      124       ep0    421
128.3.196.93       8:0:20:1c:7:f5     UHLW        9       93       ep0    655
128.3.196.175      8:0:20:7f:15:a1    UHLW        3      572       ep0   1030
128.3.199.255      ff:ff:ff:ff:ff:ff  UHLWb       0        1       ep0
204.62.155         link#6             UC          0        0 
204.62.155.50      0:0:cc:3e:9b:32    UHLW        2        0       lo0
# route delete default            204.62.155.50
writing to routing socket: No such process
delete net default: gateway 204.62.155.50: not in table
# # Dec 20 11:28:08 iss-p2 xntpd[98]: sendto(131.243.2.75): No route to host
Dec 20 11:28:08 iss-p2 xntpd[98]: sendto(131.243.2.75): No route to host

routing is screwed up and reboot maybe needed.


>How-To-Repeat:

	just repeat above route add/delete commands under 2.2-Alpha

>Fix:
	
	not clear if this problem is in route.c or in the kernel.
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->wollman 
Responsible-Changed-By: mpp 
Responsible-Changed-When: Tue Feb 18 23:10:56 PST 1997 
Responsible-Changed-Why:  
Networking problem.  Originator states that this problem still exists 
in 2.2-GAMMA and the latest 3.0-SNAP. 

From: Jin Guojun (ITG staff) <jin@george.lbl.gov>
To: freebsd-gnats-submit@freebsd.org
Cc:  Subject: Re: bin/2258 route add/delete [network] xxx.yyy.zzz.0 my-IP
Date: Mon, 23 Mar 1998 17:41:57 -0800 (PST)

 >         If you are still experiencing the problem you reported, it
 > would help the project track the problem if you could upgrade to the
 > latest snapshot of 2.2.6-Beta (located at releng22.freebsd.org) and
 > test your problem again. 
 >  
 >         If you have any feedback regarding this Problem Report,
 > whether you are still experiencing the problem or whether the PR can
 > be closed, please mail your response to
 > freebsd-gnats-submit@freebsd.org. Please do not respond directly to
 > me. I am merely a humble volunteer and have no official connection to
 > the FreeBSD project. Therefore I cannot make any changes to the status
 > of your Problem Report. It is also very important that you include 
 > the category and number of your Problem Report (bin/2258) in the 
 > subject line of your response.
 
 The problem still remains, the adding default setting is wrong.
 It supposes not to set duplicated the default routers, first of all.
 It also deleted a wrong entry. So, the rests are not tested at this time
 due to the broken router. Without a connection to a xterm, it is difficulty
 to copy and send all information on the console to you.
 
 Thanks,
 
 	-Jin
 
 ------------------ screen output for route add/delete ----------------------
 
 adv-pc-1# uname -a
 FreeBSD adv-pc-1.lbl.gov 2.2.6-980322-BETA FreeBSD 2.2.6-980322-BETA #0: Mon Mar 23 17:24:26 PST 1998     root@adv-pc-1.lbl.gov:/usr/src/sys/compile/MinMax  i386
 
 adv-pc-1# netstat -r
 Routing tables
  
 Internet:
 Destination        Gateway            Flags     Refs     Use     Netif Expire
 default            ir50gw             UGSc        2        0       ep0
 localhost          localhost          UH          1       10       lo0
 128.3.196/22       link#3             UC          0        0 
 ...
 
 adv-pc-1# route add default ir40gw 1
 add net default: gateway ir40gw
 adv-pc-1# netstat -r
 Routing tables
  
 Internet:
 Destination        Gateway            Flags     Refs     Use     Netif Expire
  
 ^C		# router is wrong now, so have to use option "-rn"
 adv-pc-1# !!n
 netstat -rn
 Routing tables
  
 Internet:
 Destination        Gateway            Flags     Refs     Use     Netif Expire
 default            131.243.134.1      UGSc        2        1       ep0 =>
 default            128.3.196.10       UGSc        3        0       ep0
 127.0.0.1          127.0.0.1          UH          1       10       lo0
 128.3.196/22       link#3             UC          0        0 
 ....
 
 adv-pc-1# route delete default            131.243.134.1
 delete net default: gateway 131.243.134.1
 adv-pc-1# netstat -r
 Routing tables
  
 Internet:
 Destination        Gateway            Flags     Refs     Use     Netif Expire
 default            131.243.134.1      UGSc        2        1       ep0
 localhost          localhost          UH          1       10       lo0
 128.3.196/22       link#3             UC          0        0 
 ....
State-Changed-From-To: open->closed 
State-Changed-By: sheldonh 
State-Changed-When: Fri Mar 19 02:25:00 PST 1999 
State-Changed-Why:  
Originator not available for feedback on later releases. 
State-Changed-From-To: closed->open 
State-Changed-By: fenner 
State-Changed-When: Sun Aug 15 12:18:10 PDT 1999 
State-Changed-Why:  
Updated originator's address, originator reports problem still exists. 

From: adrian@freebsd.org
To: freebsd-gnats-submit@freebsd.org
Cc: jin@george.lbl.gov
Subject: Re: bin/2258: route add/delete [network] xxx.yyy.zzz.0 my-ip
Date: Sun, 19 Sep 1999 01:47:11 +0800

 What you're seeing here is probably the radix code going potty
 over the fact you have a netmask (in this case 1) invalid.
 
 I haven't seen the old 2.1.6 route, but I'm assuming since it
 caught the '1' at the end, that means it is ignoring it.
 
 The current route does something like this:
 
 route add|delete destination gateway [netmask]
 
 .. which is where I'm guessing part of the problem stems from.
 What did people think the 1 in the PR (in the netmask field
 above) meant? :-)
 
 There isn't any checking in the radix code at *all* to see
 whether the netmask is valid.
 
 I have a patch to route(8) to stop people shooting themselves
 in the foot, but the *real* solution is for someone to get
 dirty in the routing code and stick in some checks to make
 sure the netmask is valid.
 
 Comments?
 
 
 
 Adrian
 
 
 Index: route.c
 ===================================================================
 RCS file: /home/ncvs/src/sbin/route/route.c,v
 retrieving revision 1.32
 diff -u -r1.32 route.c
 --- route.c	1999/08/28 00:14:13	1.32
 +++ route.c	1999/09/18 17:56:00
 @@ -105,6 +105,7 @@
  void	flushroutes(), newroute(), monitor(), sockaddr(), sodump(), bprintf();
  void	print_getmsg(), print_rtmsg(), pmsg_common(), pmsg_addrs(), mask_addr();
  int	getaddr(), rtmsg(), x25_makemask();
 +int	validnetmask();
  extern	char *inet_ntoa(), *iso_ntoa(), *link_ntoa();
  
  void usage __P((const char *)) __dead2;
 @@ -902,6 +903,9 @@
  	}
  	if (((val = inet_addr(s)) != INADDR_NONE) &&
  	    (which != RTA_DST || forcenet == 0)) {
 +		/* Check to make sure the netmask is valid */
 +		if (which == RTA_NETMASK && !validnetmask(ntohl(val)))
 +			errx(EX_DATAERR, "Invalid netmask: %s", s);
  		su->sin.sin_addr.s_addr = val;
  		if (inet_lnaof(su->sin.sin_addr) != INADDR_ANY)
  			return (1);
 @@ -1486,4 +1490,31 @@
  
  	(void) snprintf(buf, sizeof(buf), "%u.%u", ntohs(at.s_net), at.s_node);
  	return(buf);
 +}
 +
 +/*
 + * This returns 1 if the netmask is valid, 0 otherwise.
 + */
 +int
 +validnetmask(unsigned long val)
 +{
 +	int count = 0;
 +
 +	/*
 +	 * Shift right until we get a 1. Then, shift until we've done all
 + 	 * 32 bits in the netmask, and if we get a 0 anywhere, return 0.
 + 	 * otherwise, return 1.
 + 	 */
 +
 +	while ((val & 1) == 0 && count < 32) {
 +		val >>= 1;
 +		count++;
 +	}
 +	while (count < 32) {
 +		if ((val & 1) == 0)
 +			return 0;
 +		val >>= 1;
 +		count++;
 +	}
 +	return 1;
  }
 

From: jin@george.lbl.gov
To: adrian@freebsd.org, freebsd-gnats-submit@freebsd.org
Cc:  
Subject: Re: bin/2258: route add/delete [network] xxx.yyy.zzz.0 my-ip
Date: Sun, 19 Sep 1999 15:12:14 -0700 (PDT)

 > What you're seeing here is probably the radix code going potty
 > over the fact you have a netmask (in this case 1) invalid.
 > 
 > I haven't seen the old 2.1.6 route, but I'm assuming since it
 > caught the '1' at the end, that means it is ignoring it.
 > 
 > The current route does something like this:
 > 
 > route add|delete destination gateway [netmask]
 > 
 > .. which is where I'm guessing part of the problem stems from.
 > What did people think the 1 in the PR (in the netmask field
 > above) meant? :-)
 
 The correct usage is:
 
 	route add|delete destination gateway [hops]
 
 The "1" means 1 hop to/from the gateway. The hop can be zero (0)
 if the gateway is itself. I do not know if people have used more than
 1 hop for specifying a gateway recently, but it was possible in the past.
 One can use a gateway away from local gateway to route packages
 to a specified destination. This was used as one to one routing.
 
 If this field has been changed to netmask in 2.2.x or later, then this
 is the problem. I will look into the code later next week to see if this
 is true.
 
 	-Jin
 
 > There isn't any checking in the radix code at *all* to see
 > whether the netmask is valid.
 > 
 > I have a patch to route(8) to stop people shooting themselves
 > in the foot, but the *real* solution is for someone to get
 > dirty in the routing code and stick in some checks to make
 > sure the netmask is valid.
 > 
 > Comments?
 

From: Sheldon Hearn <sheldonh@uunet.co.za>
To: jin@george.lbl.gov
Cc: adrian@freebsd.org, freebsd-gnats-submit@freebsd.org
Subject: Re: bin/2258: route add/delete
Date: Tue, 21 Dec 1999 14:56:02 +0200

 Greetings,
 
 On Sun, 19 Sep 1999 15:12:14 -0700 (PDT), jin@george.lbl.gov wrote:
 
 > If this field has been changed to netmask in 2.2.x or later, then this
 > is the problem. I will look into the code later next week to see if
 > this is true.
 
 Did you find time to look into this?
 
 Ciao,
 Sheldon.
 
Responsible-Changed-From-To: wollman->freebsd-bugs 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Tue Dec 21 05:31:10 PST 1999 
Responsible-Changed-Why:  
Garrett can't use the PR system, Adrian has a handle on the thing 
and it'll _never_ get closed as it is.  Expect feedback from 
the Originator some time soon. 

From: Adrian Chadd <adrian@freebsd.org>
To: Sheldon Hearn <sheldonh@uunet.co.za>
Cc: jin@george.lbl.gov, adrian@freebsd.org,
	freebsd-gnats-submit@freebsd.org
Subject: Re: bin/2258: route add/delete
Date: Wed, 22 Dec 1999 20:05:41 +0100

 On Tue, Dec 21, 1999, Sheldon Hearn wrote:
 > 
 > Greetings,
 > 
 > On Sun, 19 Sep 1999 15:12:14 -0700 (PDT), jin@george.lbl.gov wrote:
 > 
 > > If this field has been changed to netmask in 2.2.x or later, then this
 > > is the problem. I will look into the code later next week to see if
 > > this is true.
 > 
 > Did you find time to look into this?
 
 Yup, its actually a netmask field being parsed by route, and the route
 code in the kernel messes up non-contiguous netmasks. I suggested that
 we stick a bogon filter in there for this, but garret poked me back saying
 that non-contiguous netmasks were meant to work..
 
 I haven't looked at it much after that, and I'm on holidays. when I'm back,
 and its assigned to me, I'll poke at it more.
 
 
 
 Adrian
 
Responsible-Changed-From-To: freebsd-bugs->adrian 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Thu Dec 23 01:09:07 PST 1999 
Responsible-Changed-Why:  
Ha!  A victim.  Adrian's offered to look at this. :-) 
Responsible-Changed-From-To: adrian->freebsd-bugs 
Responsible-Changed-By: ru 
Responsible-Changed-When: Tue Jan 25 06:52:48 PST 2000 
Responsible-Changed-Why:  
I have a nice explanation and will be closing this PR right now. 
State-Changed-From-To: open->closed 
State-Changed-By: ru 
State-Changed-When: Tue Jan 25 06:56:21 PST 2000 
State-Changed-Why:  
The third argument of the route(8) (in the "old form") is treated 
as a netmask value, so the following command 

# route add 198.207.143.0 127.0.0.1 1 

is, in fact, only a short equivalent of the 

# route add -net 198.207.143.0 -netmask 1 127.0.0.1 

You can see this with the following (non-destructive) command: 

# route -vnt add -net 198.207.143.0 -netmask 1 127.0.0.1 
[...] 
sockaddrs: <DST,GATEWAY,NETMASK> 
198.207.143.0 127.0.0.1 0.0.0.1 
add net 198.207.143.0: gateway 127.0.0.1 

Applying mask of 0.0.0.1 to 198.207.143.0 gives 0.0.0.0/0.0.0.1. 
That is what is actually stored in the kernel routing table. 

To delete this entry, simply execute: 
# route delete 0 -netmask 1 

To dump the entire routing table (note the magic -d option), run: 
# route -vnd flush 

Actually, the only problem is the way netstat(1) reports "default" 
route for (in fact) non-default routes, but this has already been 
fixed in 4.0-CURRENT (see src/sbin/route/route.c,v 1.41). 

I will be MFC'ing this into -STABLE right now. 
>Unformatted:
