(1) Make if_index "wrap" at USHRT_MAX instead of going above it so

that other parts of the kernel won't lose gratuitously.  There are
places where it's assumed that it won't grow that large.

(2) Avoid accidental reuse of occupied slots in the ifindex2ifnet[]
table.
This commit is contained in:
atatat 2002-02-09 05:56:34 +00:00
parent 48198ba7ca
commit 9dd8465963

View File

@ -1,4 +1,4 @@
/* $NetBSD: if.c,v 1.101 2001/12/02 19:44:25 abs Exp $ */
/* $NetBSD: if.c,v 1.102 2002/02/09 05:56:34 atatat Exp $ */
/*-
* Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
@ -101,7 +101,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.101 2001/12/02 19:44:25 abs Exp $");
__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.102 2002/02/09 05:56:34 atatat Exp $");
#include "opt_inet.h"
@ -256,7 +256,7 @@ if_nulldrain(ifp)
/* Nothing. */
}
int if_index = 0;
int if_index = 1;
struct ifaddr **ifnet_addrs = NULL;
struct ifnet **ifindex2ifnet = NULL;
@ -354,13 +354,45 @@ void
if_attach(ifp)
struct ifnet *ifp;
{
static size_t if_indexlim = 8;
static size_t if_indexlim = 0;
int indexlim = 0;
if (if_index == 0)
if (if_indexlim == 0) {
TAILQ_INIT(&ifnet);
if_indexlim = 8;
}
TAILQ_INIT(&ifp->if_addrlist);
TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
ifp->if_index = ++if_index;
ifp->if_index = if_index;
if (ifindex2ifnet == 0)
if_index++;
else
while (ifindex2ifnet[ifp->if_index] != NULL) {
++if_index;
if (if_index == 0)
if_index = 1;
/*
* If we hit USHRT_MAX, we skip back to 0 since
* there are a number of places where the value
* of if_index or if_index itself is compared
* to to or stored in an unsigned short. By
* jumping back, we won't botch those assignments
* or comparisons.
*/
else if (if_index == USHRT_MAX) {
/*
* However, if we have to jump back to
* zero *twice* without finding an empty
* slot in ifindex2ifnet[], then there
* there are too many (>65535) interfaces.
*/
if (indexlim++)
panic("too many interfaces");
else
if_index = 1;
}
ifp->if_index = if_index;
}
/*
* We have some arrays that should be indexed by if_index.