Chapter 12: Networks
❑ The default implementation ofheader_ops->createis ineth_header. This function is used to
generate the network access layer header for the existing packet data.
❑ header_ops->parse(usually implemented byeth_header_parse) obtains the source hardware
address of a given packet.
An ioctl (see Chapter 8) is applied to the file descriptor of a socket to modify the configuration of a net-
work device from userspace. One of the symbolic constants defined in<sockios.h>must be specified
to indicatewhichpart of the configuration is to be changed. For example,SIOCGIFHWADDRis responsi-
ble for setting the hardware address of a network card, but the kernel ultimately delegates this task to
theset_mac_addressfunction of thenet_deviceinstance. Device-specific constants are passed to the
do_ioctlfunction. The implementation is very lengthy because of the many adjustment options but is
not interesting enough for us to discuss it here.
Network devices work in two directions — they send and they receive (these directions are often referred
to asdownstreamandupstream). The kernel sources include two driver skeletons (isa-skeleton.cand
pci-skeleton.cindrivers/net) for use as network driver templates. Below, occasional reference is
made to these drivers when we are primarily interested in their interaction with the hardware but do not
want to restrict ourselves to a specific proprietary card type. More interesting than the programming of
the hardware is the interfaces used by the kernel for communication purposes, which is why I focus on
them below. First, we only need to introduce how network devices are registered within the kernel.
Representation of Network Devices
Each network device is registered in a two-step process:
- alloc_netdevallocates a new instance ofstruct net_device, and a protocol-specific
function fills the structure with typical values. For Ethernet devices, this function is
ether_setup. Other protocols (not considerd in detail) useXXX_setup, where possible
values forXXXincludefddi(fiber distributed data),tr(token ring),ltalk(localtalk),hippi
(high-performance parallel interface), orfc(fiber channel).
Some in-kernel pseudo-devices implementing specific ‘‘interfaces’’ without being bound to
particular hardware also use thenet_deviceframework.ppp_setupinitializes devices for
the PPP protocol, for example. Several moreXXX_setupfunctions can be found across the
kernel sources.
- Oncestruct net_deviceis completely filled in, it needs to be registered with
register_netdevorregister_netdevice. The difference between both functions is
thatregister_netdevallows for working with (limited) format strings for interface names.
The name given innet_device->devcan contain the format specifier%d. When the device
is registered, the kernel selects a unique number that is substituted for%d. Ethernet devices
specifyeth%d, for instance, and the kernel subsequently creates the deviceseth0,eth1...
The convenience functionalloc_etherdev(sizeof_priv)allocates an instance ofstruct net_device
together withsizeof_privbytes for private use — recall thatnet_device->privis a pointer to driver-
specific data associated with the device. Additionally,ether_setupmentioned above is called to set
Ethernet-specific standard values.