NetBSD/sys/lib/libsa/netif.c

342 lines
7.5 KiB
C
Raw Normal View History

/* $NetBSD: netif.c,v 1.18 2003/03/12 14:49:19 drochner Exp $ */
1994-10-26 08:44:32 +03:00
/*
* Copyright (c) 1993 Adam Glass
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Adam Glass.
* 4. The name of the Author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/cdefs.h>
#include <sys/mount.h>
1999-11-14 00:17:56 +03:00
#ifdef _STANDALONE
#include <lib/libkern/libkern.h>
#else
#include <string.h>
#endif
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include "stand.h"
#include "netif.h"
struct iodesc sockets[SOPEN_MAX];
1995-02-20 03:19:09 +03:00
#ifdef NETIF_DEBUG
int netif_debug = 0;
#endif
/*
* netif_init:
*
* initialize the generic network interface layer
*/
void
netif_init()
{
struct netif_driver *drv;
int d, i;
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("netif_init: called\n");
#endif
for (d = 0; d < n_netif_drivers; d++) {
drv = netif_drivers[d];
for (i = 0; i < drv->netif_nifs; i++)
drv->netif_ifs[i].dif_used = 0;
}
}
1999-02-11 12:10:43 +03:00
int netif_match __P((struct netif *, void *));
int
netif_match(nif, machdep_hint)
struct netif *nif;
void *machdep_hint;
{
struct netif_driver *drv = nif->nif_driver;
#if 0
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("%s%d: netif_match (%d)\n", drv->netif_bname,
nif->nif_unit, nif->nif_sel);
#endif
return drv->netif_match(nif, machdep_hint);
}
struct netif *
netif_select(machdep_hint)
void *machdep_hint;
{
int d, u, unit_done, s;
struct netif_driver *drv;
struct netif cur_if;
static struct netif best_if;
int best_val;
int val;
best_val = 0;
best_if.nif_driver = NULL;
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("netif_select: %d interfaces\n", n_netif_drivers);
#endif
for (d = 0; d < n_netif_drivers; d++) {
cur_if.nif_driver = netif_drivers[d];
drv = cur_if.nif_driver;
for (u = 0; u < drv->netif_nifs; u++) {
cur_if.nif_unit = u;
unit_done = 0;
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("\t%s%d:", drv->netif_bname,
cur_if.nif_unit);
#endif
for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
cur_if.nif_sel = s;
if (drv->netif_ifs[u].dif_used & (1 << s)) {
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf(" [%d used]", s);
#endif
continue;
}
val = netif_match(&cur_if, machdep_hint);
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf(" [%d -> %d]", s, val);
#endif
if (val > best_val) {
best_val = val;
best_if = cur_if;
}
}
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("\n");
#endif
}
}
if (best_if.nif_driver == NULL)
return NULL;
best_if.nif_driver->
netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("netif_select: %s%d(%d) wins\n",
best_if.nif_driver->netif_bname,
best_if.nif_unit, best_if.nif_sel);
#endif
return &best_if;
}
int
netif_probe(nif, machdep_hint)
struct netif *nif;
void *machdep_hint;
{
struct netif_driver *drv = nif->nif_driver;
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
#endif
return drv->netif_probe(nif, machdep_hint);
}
void
netif_attach(nif, desc, machdep_hint)
struct netif *nif;
struct iodesc *desc;
void *machdep_hint;
{
struct netif_driver *drv = nif->nif_driver;
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
#endif
desc->io_netif = nif;
#ifdef PARANOID
if (drv->netif_init == NULL)
panic("%s%d: no netif_init support", drv->netif_bname,
nif->nif_unit);
#endif
drv->netif_init(desc, machdep_hint);
bzero(drv->netif_ifs[nif->nif_unit].dif_stats,
sizeof(struct netif_stats));
}
void
netif_detach(nif)
struct netif *nif;
{
struct netif_driver *drv = nif->nif_driver;
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
#endif
#ifdef PARANOID
if (drv->netif_end == NULL)
panic("%s%d: no netif_end support", drv->netif_bname,
nif->nif_unit);
#endif
drv->netif_end(nif);
}
ssize_t
netif_get(desc, pkt, len, timo)
struct iodesc *desc;
void *pkt;
size_t len;
time_t timo;
{
struct netif *nif = desc->io_netif;
struct netif_driver *drv = nif->nif_driver;
ssize_t rv;
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
#endif
#ifdef PARANOID
if (drv->netif_get == NULL)
panic("%s%d: no netif_get support", drv->netif_bname,
nif->nif_unit);
#endif
rv = drv->netif_get(desc, pkt, len, timo);
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("%s%d: netif_get returning %d\n", drv->netif_bname,
1997-09-06 17:57:14 +04:00
nif->nif_unit, (int)rv);
#endif
return rv;
}
ssize_t
netif_put(desc, pkt, len)
struct iodesc *desc;
void *pkt;
size_t len;
{
struct netif *nif = desc->io_netif;
struct netif_driver *drv = nif->nif_driver;
ssize_t rv;
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
#endif
#ifdef PARANOID
if (drv->netif_put == NULL)
panic("%s%d: no netif_put support", drv->netif_bname,
nif->nif_unit);
#endif
rv = drv->netif_put(desc, pkt, len);
#ifdef NETIF_DEBUG
if (netif_debug)
1996-10-13 06:28:58 +04:00
printf("%s%d: netif_put returning %d\n", drv->netif_bname,
1997-09-06 17:57:14 +04:00
nif->nif_unit, (int)rv);
#endif
return rv;
}
struct iodesc *
socktodesc(sock)
int sock;
{
Make a bunch of backward-compatible changes to the boot blocks which allow size to be reduced substantially. (backward compatibility verified by compiling one of the alpha boot blocks which uses all of the code before and after, diffing the object files, and manually verifying that the differences were 'correct'. some differences were "unavoidable," it wanting to avoid a double-commit, because e.g. local variables which were previously used were no longer used.) a README which describes supported options (or at least the ones mentioned below) is forthcoming. add support for the preprocessor macro LIBSA_NO_TWIDDLE, which causes calls to twiddle() to be omitted if it's defined. add support for the preprocessor macros: LIBSA_NO_FS_CLOSE LIBSA_NO_FS_WRITE LIBSA_NO_FS_SEEK which, if defined, cause the corresponding file system operations in the individual file system implementations to be omitted. (note that all of those macros are not supported by all file systems at this point. comments were added to individual file system files to indicate lack of support, and should be cleaned up later. Backward compatibility options e.g. UFS_NOCLOSE, etc., are supported.) add support for the preprocessor macro LIBSA_NO_FS_SYMLINK, which removes support for symbolic links from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_FS_SINGLECOMPONENT which removes all subdirectory and symlink support from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_NO_FD_CHECKING, which causes code relating to libsa file descriptor checks (e.g. range checking and checking that a file descriptor is valid) to be omitted if it's defined. add support for the preprocessor macro LIBSA_NO_RAW_ACCESS, which causes code relating to raw device access to be omitted if it's defined. change some structure copies to use bcopy() instead. that way use of bcopy vs. memcpy() can easily be selected by LIBSA_USE_MEMCPY. (without changes like these, you could end up having both bcopy() and memcpy() included. eventually, all calls to bcopy should be changed to calls to memcpy() or memmove() as appropriate -- hopefully never the latter -- with an option to use bcopy instead.) add support for the preprocessor macro LIBSA_NO_DISKLABEL_MSGS, which causes disklabel() to return '1' as msg rather than a string. Can be used if the boot blocks don't care about the string, and need to save the space. add support for the preprocessor macro LIBSA_SINGLE_FILESYSTEM, which if defined causes all of the file system switch code to be removed. Its value should be the name of the file system supported by the boot block, e.g. "ufs" for the FFS file system. calls to the file system functions open, close, etc., which were previously done through a function switch are then done via direct invocation of <fs>_open, <fs>_close, etc. (e.g. ufs_open, ...). add support for the preprocessor macro LIBSA_SINGLE_DEVICE, which does the equivalent of LIBSA_SINGLE_FILESYSTEM but for the device switch table. Device entry pointes are expected to be named <dev>foo, e.g. the 'strategy' routine used when LIBSA_SINGLE_DEVICE is set to 'disk' is diskstrategy. make ufs.c f_nindir array be unsigned ints. the fact that it was signed caused ufs.c to require signed division routines (which were otherwise unnecessary for a small boot block).
1999-03-31 05:50:25 +04:00
#if !defined(LIBSA_NO_FD_CHECKING)
if (sock >= SOPEN_MAX) {
errno = EBADF;
return (NULL);
}
Make a bunch of backward-compatible changes to the boot blocks which allow size to be reduced substantially. (backward compatibility verified by compiling one of the alpha boot blocks which uses all of the code before and after, diffing the object files, and manually verifying that the differences were 'correct'. some differences were "unavoidable," it wanting to avoid a double-commit, because e.g. local variables which were previously used were no longer used.) a README which describes supported options (or at least the ones mentioned below) is forthcoming. add support for the preprocessor macro LIBSA_NO_TWIDDLE, which causes calls to twiddle() to be omitted if it's defined. add support for the preprocessor macros: LIBSA_NO_FS_CLOSE LIBSA_NO_FS_WRITE LIBSA_NO_FS_SEEK which, if defined, cause the corresponding file system operations in the individual file system implementations to be omitted. (note that all of those macros are not supported by all file systems at this point. comments were added to individual file system files to indicate lack of support, and should be cleaned up later. Backward compatibility options e.g. UFS_NOCLOSE, etc., are supported.) add support for the preprocessor macro LIBSA_NO_FS_SYMLINK, which removes support for symbolic links from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_FS_SINGLECOMPONENT which removes all subdirectory and symlink support from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_NO_FD_CHECKING, which causes code relating to libsa file descriptor checks (e.g. range checking and checking that a file descriptor is valid) to be omitted if it's defined. add support for the preprocessor macro LIBSA_NO_RAW_ACCESS, which causes code relating to raw device access to be omitted if it's defined. change some structure copies to use bcopy() instead. that way use of bcopy vs. memcpy() can easily be selected by LIBSA_USE_MEMCPY. (without changes like these, you could end up having both bcopy() and memcpy() included. eventually, all calls to bcopy should be changed to calls to memcpy() or memmove() as appropriate -- hopefully never the latter -- with an option to use bcopy instead.) add support for the preprocessor macro LIBSA_NO_DISKLABEL_MSGS, which causes disklabel() to return '1' as msg rather than a string. Can be used if the boot blocks don't care about the string, and need to save the space. add support for the preprocessor macro LIBSA_SINGLE_FILESYSTEM, which if defined causes all of the file system switch code to be removed. Its value should be the name of the file system supported by the boot block, e.g. "ufs" for the FFS file system. calls to the file system functions open, close, etc., which were previously done through a function switch are then done via direct invocation of <fs>_open, <fs>_close, etc. (e.g. ufs_open, ...). add support for the preprocessor macro LIBSA_SINGLE_DEVICE, which does the equivalent of LIBSA_SINGLE_FILESYSTEM but for the device switch table. Device entry pointes are expected to be named <dev>foo, e.g. the 'strategy' routine used when LIBSA_SINGLE_DEVICE is set to 'disk' is diskstrategy. make ufs.c f_nindir array be unsigned ints. the fact that it was signed caused ufs.c to require signed division routines (which were otherwise unnecessary for a small boot block).
1999-03-31 05:50:25 +04:00
#endif
return (&sockets[sock]);
}
int
netif_open(machdep_hint)
void *machdep_hint;
{
int fd;
2000-03-30 16:19:46 +04:00
struct iodesc *s;
struct netif *nif;
/* find a free socket */
for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
if (s->io_netif == (struct netif *)0)
goto fnd;
errno = EMFILE;
return (-1);
fnd:
bzero(s, sizeof(*s));
netif_init();
nif = netif_select(machdep_hint);
if (!nif)
panic("netboot: no interfaces left untried");
if (netif_probe(nif, machdep_hint)) {
1996-10-13 06:28:58 +04:00
printf("netboot: couldn't probe %s%d\n",
1996-10-11 02:46:11 +04:00
nif->nif_driver->netif_bname, nif->nif_unit);
errno = EINVAL;
return(-1);
}
netif_attach(nif, s, machdep_hint);
return(fd);
}
int
netif_close(sock)
int sock;
{
Make a bunch of backward-compatible changes to the boot blocks which allow size to be reduced substantially. (backward compatibility verified by compiling one of the alpha boot blocks which uses all of the code before and after, diffing the object files, and manually verifying that the differences were 'correct'. some differences were "unavoidable," it wanting to avoid a double-commit, because e.g. local variables which were previously used were no longer used.) a README which describes supported options (or at least the ones mentioned below) is forthcoming. add support for the preprocessor macro LIBSA_NO_TWIDDLE, which causes calls to twiddle() to be omitted if it's defined. add support for the preprocessor macros: LIBSA_NO_FS_CLOSE LIBSA_NO_FS_WRITE LIBSA_NO_FS_SEEK which, if defined, cause the corresponding file system operations in the individual file system implementations to be omitted. (note that all of those macros are not supported by all file systems at this point. comments were added to individual file system files to indicate lack of support, and should be cleaned up later. Backward compatibility options e.g. UFS_NOCLOSE, etc., are supported.) add support for the preprocessor macro LIBSA_NO_FS_SYMLINK, which removes support for symbolic links from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_FS_SINGLECOMPONENT which removes all subdirectory and symlink support from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_NO_FD_CHECKING, which causes code relating to libsa file descriptor checks (e.g. range checking and checking that a file descriptor is valid) to be omitted if it's defined. add support for the preprocessor macro LIBSA_NO_RAW_ACCESS, which causes code relating to raw device access to be omitted if it's defined. change some structure copies to use bcopy() instead. that way use of bcopy vs. memcpy() can easily be selected by LIBSA_USE_MEMCPY. (without changes like these, you could end up having both bcopy() and memcpy() included. eventually, all calls to bcopy should be changed to calls to memcpy() or memmove() as appropriate -- hopefully never the latter -- with an option to use bcopy instead.) add support for the preprocessor macro LIBSA_NO_DISKLABEL_MSGS, which causes disklabel() to return '1' as msg rather than a string. Can be used if the boot blocks don't care about the string, and need to save the space. add support for the preprocessor macro LIBSA_SINGLE_FILESYSTEM, which if defined causes all of the file system switch code to be removed. Its value should be the name of the file system supported by the boot block, e.g. "ufs" for the FFS file system. calls to the file system functions open, close, etc., which were previously done through a function switch are then done via direct invocation of <fs>_open, <fs>_close, etc. (e.g. ufs_open, ...). add support for the preprocessor macro LIBSA_SINGLE_DEVICE, which does the equivalent of LIBSA_SINGLE_FILESYSTEM but for the device switch table. Device entry pointes are expected to be named <dev>foo, e.g. the 'strategy' routine used when LIBSA_SINGLE_DEVICE is set to 'disk' is diskstrategy. make ufs.c f_nindir array be unsigned ints. the fact that it was signed caused ufs.c to require signed division routines (which were otherwise unnecessary for a small boot block).
1999-03-31 05:50:25 +04:00
#if !defined(LIBSA_NO_FD_CHECKING)
if (sock >= SOPEN_MAX) {
errno = EBADF;
return(-1);
}
Make a bunch of backward-compatible changes to the boot blocks which allow size to be reduced substantially. (backward compatibility verified by compiling one of the alpha boot blocks which uses all of the code before and after, diffing the object files, and manually verifying that the differences were 'correct'. some differences were "unavoidable," it wanting to avoid a double-commit, because e.g. local variables which were previously used were no longer used.) a README which describes supported options (or at least the ones mentioned below) is forthcoming. add support for the preprocessor macro LIBSA_NO_TWIDDLE, which causes calls to twiddle() to be omitted if it's defined. add support for the preprocessor macros: LIBSA_NO_FS_CLOSE LIBSA_NO_FS_WRITE LIBSA_NO_FS_SEEK which, if defined, cause the corresponding file system operations in the individual file system implementations to be omitted. (note that all of those macros are not supported by all file systems at this point. comments were added to individual file system files to indicate lack of support, and should be cleaned up later. Backward compatibility options e.g. UFS_NOCLOSE, etc., are supported.) add support for the preprocessor macro LIBSA_NO_FS_SYMLINK, which removes support for symbolic links from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_FS_SINGLECOMPONENT which removes all subdirectory and symlink support from the file system support functions. (same notes as for the macros above apply.) add support for the preprocessor macro LIBSA_NO_FD_CHECKING, which causes code relating to libsa file descriptor checks (e.g. range checking and checking that a file descriptor is valid) to be omitted if it's defined. add support for the preprocessor macro LIBSA_NO_RAW_ACCESS, which causes code relating to raw device access to be omitted if it's defined. change some structure copies to use bcopy() instead. that way use of bcopy vs. memcpy() can easily be selected by LIBSA_USE_MEMCPY. (without changes like these, you could end up having both bcopy() and memcpy() included. eventually, all calls to bcopy should be changed to calls to memcpy() or memmove() as appropriate -- hopefully never the latter -- with an option to use bcopy instead.) add support for the preprocessor macro LIBSA_NO_DISKLABEL_MSGS, which causes disklabel() to return '1' as msg rather than a string. Can be used if the boot blocks don't care about the string, and need to save the space. add support for the preprocessor macro LIBSA_SINGLE_FILESYSTEM, which if defined causes all of the file system switch code to be removed. Its value should be the name of the file system supported by the boot block, e.g. "ufs" for the FFS file system. calls to the file system functions open, close, etc., which were previously done through a function switch are then done via direct invocation of <fs>_open, <fs>_close, etc. (e.g. ufs_open, ...). add support for the preprocessor macro LIBSA_SINGLE_DEVICE, which does the equivalent of LIBSA_SINGLE_FILESYSTEM but for the device switch table. Device entry pointes are expected to be named <dev>foo, e.g. the 'strategy' routine used when LIBSA_SINGLE_DEVICE is set to 'disk' is diskstrategy. make ufs.c f_nindir array be unsigned ints. the fact that it was signed caused ufs.c to require signed division routines (which were otherwise unnecessary for a small boot block).
1999-03-31 05:50:25 +04:00
#endif
netif_detach(sockets[sock].io_netif);
sockets[sock].io_netif = (struct netif *)0;
return(0);
}