From nobody  Mon Nov 24 11:54:07 1997
Received: (from nobody@localhost)
          by hub.freebsd.org (8.8.7/8.8.7) id LAA28861;
          Mon, 24 Nov 1997 11:54:07 -0800 (PST)
          (envelope-from nobody)
Message-Id: <199711241954.LAA28861@hub.freebsd.org>
Date: Mon, 24 Nov 1997 11:54:07 -0800 (PST)
From: Joel.Faedi@esial.u-nancy.fr
To: freebsd-gnats-submit@freebsd.org
Subject: portmap does not find interfaces correctly
X-Send-Pr-Version: www-1.0

>Number:         5139
>Category:       bin
>Synopsis:       portmap does not find interfaces correctly
>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 Nov 24 12:00:00 PST 1997
>Closed-Date:    Wed Aug 9 18:05:42 PDT 2000
>Last-Modified:  Wed Aug 09 18:06:04 PDT 2000
>Originator:     Joel Faedi
>Release:        2.2.5 and 3.0-971123-SNAP
>Organization:
ESIAL - Nancy (France)
>Environment:
FreeBSD meduse.scinfo.u-nancy.fr 2.2.5-RELEASE FreeBSD 2.2.5-RELEASE #15: Tue Nov 18 09:40:12 CET 1997     faedi@meduse.scinfo.u-nancy.fr:/usr/src/sys/compile/MEDUSE  i386
>Description:
when some (the first in the list obtained from SIOCGIFCONF ioctl call) ethernet interfaces are down or have no inet address, future register to portmapper (mountd, nfsd,...) will fail.
>How-To-Repeat:
Put more than one ethernet card in your PC and affect (with ifconfig)
an inet address only to the 2nd detected ethernet card , start portmap,
and try to start mountd. mountd will fail with no active IP address
found.
>Fix:
Fix code of "find_local" function in /usr/src/usr.sbin/portmap/
from_local.c: you can use the code of a similar function in bind
(named). Here is the new code for find_local() (for 2.2.5 and 3.0)
which will fix this problem and correct a potential problem: a inet
address may have more than one entry in the "addrs" array.

=================================================================
/* find_local - find all IP addresses for this host */

int
find_local()
{
    int sock, n, i, found;
    struct ifconf ifc;
    struct ifreq *ifr, *the_end, *ifnext;
    struct ifreq ibuf[MAX_LOCAL], ifreq;
    struct sockaddr_in *sin;

    /* Get list of network interfaces. */

    if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
	perror("socket");
	return (0);
    }
    ifc.ifc_len = sizeof ibuf ;
    ifc.ifc_buf = (caddr_t)ibuf;

    if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0 ||
	    ifc.ifc_len < sizeof(struct ifreq) ) {
	perror("SIOCGIFCONF");
	(void) close(sock);
	return (0);
    }

    /* Get IP address of each active IP network interface. */

    the_end = (struct ifreq *) ((char *)ibuf + ifc.ifc_len);
    num_local = 0;
    for (ifr = ibuf; ifr < the_end; ifr = ifnext) {
      
        n = ifr->ifr_addr.sa_len + sizeof(ifr->ifr_name);
        if (n < sizeof(*ifr))
	  ifnext = ifr + 1;
        else
	  ifnext = (struct ifreq *)((char *)ifr + n);

	if (ifr->ifr_addr.sa_family == AF_INET) {	/* IP net interface */
	    strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
	    if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
		perror("SIOCGIFFLAGS");
	    } else if (ifreq.ifr_flags & IFF_UP) {	/* active interface */
		if (ioctl(sock, SIOCGIFADDR, (char *) &ifreq) < 0) {
		    perror("SIOCGIFADDR");
		} else {
		    sin = (struct sockaddr_in *) & ifreq.ifr_addr;

		    /* due to "alias" addresses, an interface may
		     * appear more than once, with the same IP address
		     */
		    for (found = 0, i = 0; i < num_local && !found; i++)
		      found = (memcmp((char *) &(sin->sin_addr), 
				      (char *) &(addrs[i]),
				      sizeof(struct in_addr)) == 0);
		    if (!found)
		      addrs[num_local++] = sin->sin_addr;
		}
	    }
	}
	if (num_local >= MAX_LOCAL)
	    break;
    }
    (void) close(sock);
    return (num_local);
}


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->suspended 
State-Changed-By: phk 
State-Changed-When: Wed May 27 02:53:49 PDT 1998 
State-Changed-Why:  
Awaiting committer 

From: Sheldon Hearn <sheldonh@iafrica.com>
To: Joel Faedi <Joel.Faedi@esial.u-nancy.fr>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/5139: portmap does not find interfaces correctly
Date: Fri, 19 Mar 1999 15:24:40 +0200

 Hi Joel,
 
 Could you provide some feedback as to whether revision 1.6 of
 src/usr.sbin/portmap/from_local.c fixed the problem you reported 24 Nov
 1997 regarding FreeBSD portmap?
 
 If you've forgotten the details of your report, you can view it at
 http://www.freebsd.org/cgi/query-pr.cgi?pr=5139 . 
 
 Thanks,
 Sheldon.
 

From: "Jol Faedi" <Joel.Faedi@esial.u-nancy.fr>
To: "Sheldon Hearn" <sheldonh@iafrica.com>
Cc:  
Subject: Re: bin/5139: portmap does not find interfaces correctly 
Date: Wed, 24 Mar 1999 12:15:14 +0100

 >
 >Okay, so could you send diffs against 1.7 ? The problem I had was that
 >it was impossible for me to comparre your code against the current code.
 >If you could send a diff, I can test it.
 >
 
 
 With the new code, the patch is very small: 1 line for the problem itself,
 other lines are only for cleaner code in TEST mode (no gcc warning).
 
 *** from_local-1.7-JF.c Wed Mar 24 12:10:01 1999
 --- from_local-1.7.c Fri Mar 19 15:47:39 1999
 ***************
 *** 40,46 ****
   static char sccsid[] = "@(#) from_local.c 1.2 93/11/16 21:50:02";
   #endif
   static const char rcsid[] =
 !  "$Id: from_local.c,v 1.xxx 1999/03/24 11:25:49 xxx Exp $";
   #endif
 
   #include <sys/types.h>
 --- 40,50 ----
   static char sccsid[] = "@(#) from_local.c 1.2 93/11/16 21:50:02";
   #endif
   static const char rcsid[] =
 !  "$Id: from_local.c,v 1.7 1998/08/17 06:05:55 jb Exp $";
 ! #endif
 !
 ! #ifdef TEST
 ! #undef perror
   #endif
 
   #include <sys/types.h>
 ***************
 *** 58,69 ****
   #include <net/if_dl.h>
   #include <netinet/in.h>
 
 - #ifdef TEST
 - #include <arpa/inet.h>
 - #include <string.h>
 - #include <stdio.h>
 - #endif
 -
   #ifndef TRUE
   #define TRUE 1
   #define FALSE 0
 --- 62,67 ----
 ***************
 *** 137,143 ****
       else if (ifr.ifr_flags & IFF_UP)    /* active interface */
         if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
           perror("SIOCGIFADDR");
 !       else if (ifr.ifr_addr.sa_family== AF_INET) { /* IP net interface
 only */
           if (alloced < num_local + 1) {
             alloced += ESTIMATED_LOCAL;
             if (addrs)
 --- 135,141 ----
       else if (ifr.ifr_flags & IFF_UP)    /* active interface */
         if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
           perror("SIOCGIFADDR");
 !       else {
           if (alloced < num_local + 1) {
             alloced += ESTIMATED_LOCAL;
             if (addrs)
 ***************
 *** 180,195 ****
 
   #ifdef TEST
 
 - int
   main()
   {
 !     int     res, i;
 
 !     res = find_local();
 !     printf("results: num_local=%d find_local()=%d\n", num_local, res);
       for (i = 0; i < num_local; i++)
    printf("%s\n", inet_ntoa(addrs[i]));
 -     exit(0);
   }
 
   #endif
 --- 178,191 ----
 
   #ifdef TEST
 
   main()
   {
 !     char   *inet_ntoa();
 !     int     i;
 
 !     find_local();
       for (i = 0; i < num_local; i++)
    printf("%s\n", inet_ntoa(addrs[i]));
   }
 
   #endif
 
 
 
 

From: Sheldon Hearn <sheldonh@iafrica.com>
To: Joel.Faedi@esial.u-nancy.fr
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/5139: portmap does not find interfaces correctly
Date: Wed, 07 Apr 1999 12:07:49 +0200

 Hi Joel,
 
 What sources are you diffing against to come up with the patches you
 submitted? They look horribly broken to me, and they certainly don't
 apply to CURRENT. :-(
 
 Ciao,
 Sheldon.
 
State-Changed-From-To: suspended->feedback 
State-Changed-By: sheldonh 
State-Changed-When: Wed Apr 7 03:10:02 PDT 1999 
State-Changed-Why:  
Broken diffs. 

From: Joel Faedi <Joel.Faedi@esial.u-nancy.fr>
To: sheldonh@iafrica.com
Cc:  
Subject: Re: bin/5139: portmap does not find interfaces correctly
Date: Wed, 7 Apr 1999 17:29:36 +0200 (MET DST)

 Here is a better patch file...
 
 generated with:
 1) ftp ftp://ftp.freebsd.org/pub/FreeBSD/branches/-current/src/usr.sbin/portmap/from_local.c
 2) diff -c from_local.c from_local.c.new > from_local.patch
 
 Here is the file "from_local.patch"
 
 *** from_local.c	Mon Aug 17 12:13:45 1998
 --- from_local.c.new	Wed Apr  7 17:28:26 1999
 ***************
 *** 43,52 ****
   	"$Id: from_local.c,v 1.7 1998/08/17 06:05:55 jb Exp $";
   #endif
   
 - #ifdef TEST
 - #undef perror
 - #endif
 - 
   #include <sys/types.h>
   #include <sys/ioctl.h>
   #include <sys/socket.h>
 --- 43,48 ----
 ***************
 *** 62,67 ****
 --- 58,69 ----
   #include <net/if_dl.h>
   #include <netinet/in.h>
   
 + #ifdef TEST
 + #include <arpa/inet.h>
 + #include <string.h>
 + #include <stdio.h>
 + #endif
 + 
   #ifndef TRUE
   #define	TRUE	1
   #define FALSE	0
 ***************
 *** 135,141 ****
       else if (ifr.ifr_flags & IFF_UP)    /* active interface */
         if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
           perror("SIOCGIFADDR");
 !       else {
           if (alloced < num_local + 1) {
             alloced += ESTIMATED_LOCAL;
             if (addrs)
 --- 137,143 ----
       else if (ifr.ifr_flags & IFF_UP)    /* active interface */
         if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
           perror("SIOCGIFADDR");
 !       else if (ifr.ifr_addr.sa_family == AF_INET) { /* IP net interface only */
           if (alloced < num_local + 1) {
             alloced += ESTIMATED_LOCAL;
             if (addrs)
 ***************
 *** 178,191 ****
   
   #ifdef TEST
   
   main()
   {
 !     char   *inet_ntoa();
 !     int     i;
   
 !     find_local();
       for (i = 0; i < num_local; i++)
   	printf("%s\n", inet_ntoa(addrs[i]));
   }
   
   #endif
 --- 180,194 ----
   
   #ifdef TEST
   
 + int
   main()
   {
 !     int     res, i;
   
 !     res = find_local();
       for (i = 0; i < num_local; i++)
   	printf("%s\n", inet_ntoa(addrs[i]));
 +     exit(0);
   }
   
   #endif
 
 
State-Changed-From-To: feedback->open 
State-Changed-By: sheldonh 
State-Changed-When: Fri May 28 06:15:15 PDT 1999 
State-Changed-Why:  
New diffs need review. 
Responsible-Changed-From-To: freebsd-bugs->obrien 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Mon Jun 21 07:36:43 PDT 1999 
Responsible-Changed-Why:  
I'm hoping that David's interest in AMD might get him involved. Shout 
at me if I'm pushing it. :-) 
Responsible-Changed-From-To: obrien->sheldonh 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Thu Jul 29 12:13:56 PDT 1999 
Responsible-Changed-Why:  
David doesn't have time to look at this one now. I'll take a crack 
at it once I have a second ethernet interface installed. If anyone 
else wants this in the interim, feel free. :-) 
Responsible-Changed-From-To: sheldonh->freebsd-bugs 
Responsible-Changed-By: sheldonh 
Responsible-Changed-When: Thu Dec 9 02:00:23 PST 1999 
Responsible-Changed-Why:  
This one's still on my TODO list, but I'm throwing the PR 
back to the floor in the hopes that some eager PR troll 
will trip over it and take a look. 
State-Changed-From-To: open->closed 
State-Changed-By: brian 
State-Changed-When: Wed Aug 9 18:05:42 PDT 2000 
State-Changed-Why:  
Fixed in -current (and will be MFCd in about 6 days) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=5139 
>Unformatted:
