📄 net_init.c
字号:
/* netdrv_init.c: Initialization for network devices. */
/*
Written 1993,1994,1995 by Donald Becker.
The author may be reached as becker@cesdis.gsfc.nasa.gov or
C/O Center of Excellence in Space Data and Information Sciences
Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
This file contains the initialization for the "pl14+" style ethernet
drivers. It should eventually replace most of drivers/net/Space.c.
It's primary advantage is that it's able to allocate low-memory buffers.
A secondary advantage is that the dangerous NE*000 netcards can reserve
their I/O port region before the SCSI probes start.
Modifications/additions by Bjorn Ekwall <bj0rn@blox.se>:
ethdev_index[MAX_ETH_CARDS]
register_netdev() / unregister_netdev()
Modifications by Wolfgang Walter
Use dev_close cleanly so we always shut things down tidily.
Changed 29/10/95, Alan Cox to pass sockaddr's around for mac addresses.
14/06/96 - Paul Gortmaker: Add generic eth_change_mtu() function.
24/09/96 - Paul Norton: Add token-ring variants of the netdev functions.
*/
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/malloc.h>
#include <linux/if_ether.h>
#include <linux/string.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/fddidevice.h>
#include <linux/hippidevice.h>
#include <linux/trdevice.h>
#include <linux/fcdevice.h>
#include <linux/if_arp.h>
#include <linux/if_ltalk.h>
#include <linux/rtnetlink.h>
#include <net/neighbour.h>
/* The network devices currently exist only in the socket namespace, so these
entries are unused. The only ones that make sense are
open start the ethercard
close stop the ethercard
ioctl To get statistics, perhaps set the interface port (AUI, BNC, etc.)
One can also imagine getting raw packets using
read & write
but this is probably better handled by a raw packet socket.
Given that almost all of these functions are handled in the current
socket-based scheme, putting ethercard devices in /dev/ seems pointless.
[Removed all support for /dev network devices. When someone adds
streams then by magic we get them, but otherwise they are un-needed
and a space waste]
*/
/* The list of used and available "eth" slots (for "eth0", "eth1", etc.) */
#define MAX_ETH_CARDS 16
static struct device *ethdev_index[MAX_ETH_CARDS];
/* Fill in the fields of the device structure with ethernet-generic values.
If no device structure is passed, a new one is constructed, complete with
a SIZEOF_PRIVATE private data area.
If an empty string area is passed as dev->name, or a new structure is made,
a new name string is constructed. The passed string area should be 8 bytes
long.
*/
struct device *
init_etherdev(struct device *dev, int sizeof_priv)
{
int new_device = 0;
int i;
/* Use an existing correctly named device in Space.c:dev_base. */
if (dev == NULL) {
int alloc_size = sizeof(struct device) + sizeof("eth%d ")
+ sizeof_priv + 3;
struct device *cur_dev;
char pname[8]; /* Putative name for the device. */
for (i = 0; i < MAX_ETH_CARDS; ++i)
if (ethdev_index[i] == NULL) {
sprintf(pname, "eth%d", i);
for (cur_dev = dev_base; cur_dev; cur_dev = cur_dev->next)
if (strcmp(pname, cur_dev->name) == 0) {
dev = cur_dev;
dev->init = NULL;
sizeof_priv = (sizeof_priv + 3) & ~3;
dev->priv = sizeof_priv
? kmalloc(sizeof_priv, GFP_KERNEL)
: NULL;
if (dev->priv) memset(dev->priv, 0, sizeof_priv);
goto found;
}
}
alloc_size &= ~3; /* Round to dword boundary. */
dev = (struct device *)kmalloc(alloc_size, GFP_KERNEL);
if(dev==NULL)
return NULL;
memset(dev, 0, alloc_size);
if (sizeof_priv)
dev->priv = (void *) (dev + 1);
dev->name = sizeof_priv + (char *)(dev + 1);
new_device = 1;
}
found: /* From the double loop above. */
if (dev->name &&
((dev->name[0] == '\0') || (dev->name[0] == ' '))) {
for (i = 0; i < MAX_ETH_CARDS; ++i)
if (ethdev_index[i] == NULL) {
sprintf(dev->name, "eth%d", i);
ethdev_index[i] = dev;
break;
}
}
ether_setup(dev); /* Hmmm, should this be called here? */
if (new_device)
register_netdevice(dev);
return dev;
}
static int eth_mac_addr(struct device *dev, void *p)
{
struct sockaddr *addr=p;
if(dev->start)
return -EBUSY;
memcpy(dev->dev_addr, addr->sa_data,dev->addr_len);
return 0;
}
static int eth_change_mtu(struct device *dev, int new_mtu)
{
if ((new_mtu < 68) || (new_mtu > 1500))
return -EINVAL;
dev->mtu = new_mtu;
return 0;
}
#ifdef CONFIG_FDDI
static int fddi_change_mtu(struct device *dev, int new_mtu)
{
if ((new_mtu < FDDI_K_SNAP_HLEN) || (new_mtu > FDDI_K_SNAP_DLEN))
return(-EINVAL);
dev->mtu = new_mtu;
return(0);
}
#endif
#ifdef CONFIG_HIPPI
#define MAX_HIP_CARDS 4
static struct device *hipdev_index[MAX_HIP_CARDS];
static int hippi_change_mtu(struct device *dev, int new_mtu)
{
/*
* HIPPI's got these nice large MTUs.
*/
if ((new_mtu < 68) || (new_mtu > 65280))
return -EINVAL;
dev->mtu = new_mtu;
return(0);
}
/*
* For HIPPI we will actually use the lower 4 bytes of the hardware
* address as the I-FIELD rather than the actual hardware address.
*/
static int hippi_mac_addr(struct device *dev, void *p)
{
struct sockaddr *addr = p;
if(dev->start)
return -EBUSY;
memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
return 0;
}
struct device *init_hippi_dev(struct device *dev, int sizeof_priv)
{
int new_device = 0;
int i;
/* Use an existing correctly named device in Space.c:dev_base. */
if (dev == NULL) {
int alloc_size = sizeof(struct device) + sizeof("hip%d ")
+ sizeof_priv + 3;
struct device *cur_dev;
char pname[8];
for (i = 0; i < MAX_HIP_CARDS; ++i)
if (hipdev_index[i] == NULL) {
sprintf(pname, "hip%d", i);
for (cur_dev = dev_base; cur_dev; cur_dev = cur_dev->next)
if (strcmp(pname, cur_dev->name) == 0) {
dev = cur_dev;
dev->init = NULL;
sizeof_priv = (sizeof_priv + 3) & ~3;
dev->priv = sizeof_priv
? kmalloc(sizeof_priv, GFP_KERNEL)
: NULL;
if (dev->priv) memset(dev->priv, 0, sizeof_priv);
goto hipfound;
}
}
alloc_size &= ~3; /* Round to dword boundary. */
dev = (struct device *)kmalloc(alloc_size, GFP_KERNEL);
if(dev==NULL)
return NULL;
memset(dev, 0, alloc_size);
if (sizeof_priv)
dev->priv = (void *) (dev + 1);
dev->name = sizeof_priv + (char *)(dev + 1);
new_device = 1;
}
hipfound: /* From the double loop above. */
if (dev->name &&
((dev->name[0] == '\0') || (dev->name[0] == ' '))) {
for (i = 0; i < MAX_HIP_CARDS; ++i)
if (hipdev_index[i] == NULL) {
sprintf(dev->name, "hip%d", i);
hipdev_index[i] = dev;
break;
}
}
hippi_setup(dev);
if (new_device)
register_netdevice(dev);
return dev;
}
void unregister_hipdev(struct device *dev)
{
int i;
rtnl_lock();
unregister_netdevice(dev);
for (i = 0; i < MAX_HIP_CARDS; ++i) {
if (hipdev_index[i] == dev) {
hipdev_index[i] = NULL;
break;
}
}
rtnl_unlock();
}
static int hippi_neigh_setup_dev(struct device *dev, struct neigh_parms *p)
{
/* Never send broadcast/multicast ARP messages */
p->mcast_probes = 0;
/* In IPv6 unicast probes are valid even on NBMA,
* because they are encapsulated in normal IPv6 protocol.
* Should be a generic flag.
*/
if (p->tbl->family != AF_INET6)
p->ucast_probes = 0;
return 0;
}
#endif
void ether_setup(struct device *dev)
{
int i;
/* Fill in the fields of the device structure with ethernet-generic values.
This should be in a common file instead of per-driver. */
/* register boot-defined "eth" devices */
if (dev->name && (strncmp(dev->name, "eth", 3) == 0)) {
i = simple_strtoul(dev->name + 3, NULL, 0);
if (ethdev_index[i] == NULL) {
ethdev_index[i] = dev;
}
else if (dev != ethdev_index[i]) {
/* Really shouldn't happen! */
printk("ether_setup: Ouch! Someone else took %s\n",
dev->name);
}
}
dev->change_mtu = eth_change_mtu;
dev->hard_header = eth_header;
dev->rebuild_header = eth_rebuild_header;
dev->set_mac_address = eth_mac_addr;
dev->hard_header_cache = eth_header_cache;
dev->header_cache_update= eth_header_cache_update;
dev->hard_header_parse = eth_header_parse;
dev->type = ARPHRD_ETHER;
dev->hard_header_len = ETH_HLEN;
dev->mtu = 1500; /* eth_mtu */
dev->addr_len = ETH_ALEN;
dev->tx_queue_len = 100; /* Ethernet wants good queues */
memset(dev->broadcast,0xFF, ETH_ALEN);
/* New-style flags. */
dev->flags = IFF_BROADCAST|IFF_MULTICAST;
dev_init_buffers(dev);
}
#ifdef CONFIG_FDDI
void fddi_setup(struct device *dev)
{
/*
* Fill in the fields of the device structure with FDDI-generic values.
* This should be in a common file instead of per-driver.
*/
dev->change_mtu = fddi_change_mtu;
dev->hard_header = fddi_header;
dev->rebuild_header = fddi_rebuild_header;
dev->type = ARPHRD_FDDI;
dev->hard_header_len = FDDI_K_SNAP_HLEN+3; /* Assume 802.2 SNAP hdr len + 3 pad bytes */
dev->mtu = FDDI_K_SNAP_DLEN; /* Assume max payload of 802.2 SNAP frame */
dev->addr_len = FDDI_K_ALEN;
dev->tx_queue_len = 100; /* Long queues on FDDI */
memset(dev->broadcast, 0xFF, FDDI_K_ALEN);
/* New-style flags */
dev->flags = IFF_BROADCAST | IFF_MULTICAST;
dev_init_buffers(dev);
return;
}
#endif
#ifdef CONFIG_HIPPI
void hippi_setup(struct device *dev)
{
int i;
if (dev->name && (strncmp(dev->name, "hip", 3) == 0)) {
i = simple_strtoul(dev->name + 3, NULL, 0);
if (hipdev_index[i] == NULL) {
hipdev_index[i] = dev;
}
else if (dev != hipdev_index[i]) {
printk("hippi_setup: Ouch! Someone else took %s\n",
dev->name);
}
}
dev->set_multicast_list = NULL;
dev->change_mtu = hippi_change_mtu;
dev->hard_header = hippi_header;
dev->rebuild_header = hippi_rebuild_header;
dev->set_mac_address = hippi_mac_addr;
dev->hard_header_parse = NULL;
dev->hard_header_cache = NULL;
dev->header_cache_update = NULL;
dev->neigh_setup = hippi_neigh_setup_dev;
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -