| Red Hat Documentation: man pages: man7: Linux Programmer's Manual (7) |
PACKETSection: Linux Programmer's Manual (7)Updated: 29 Apr 2025 Index NAMEpacket, PF_PACKET - packet interface on device level.SYNOPSIS#include <sys/socket.h> DESCRIPTIONPacket sockets are used to receive or send raw packets at the device driver(OSI Layer 2) level. They allow the user to implement protocol modules in user space on top of the physical layer.
The
socket_type
is either
SOCK_RAW
for raw packets including the link level header or
SOCK_DGRAM
for cooked packets with the link level header removed. The link level
header information is available in a common format in a
sockaddr_ll.
protocol
is the IEEE 802.3 protocol number in network order. See the
<linux/if_ether.h>
include file for a list of allowed protocols. When protocol
is set to
htons(ETH_P_ALL)
then all protocols are received.
All incoming packets of that protocol type will be passed to the packet
socket before they are passed to the protocols implemented in the kernel.
SOCK_RAW packets are passed to and from the device driver without any changes in the packet data. When receiving a packet, the address is still parsed and passed in a standard sockaddr_ll address structure. When transmitting a packet, the user supplied buffer must contain the physical layer header. That packet is then queued unmodified to the network driver of the interface defined by the destination address. SOCK_RAW is similar to but not compatible with the obsolete SOCK_PACKET of Linux 2.0. SOCK_DGRAM operates on a slightly higher level. The physical header is removed before the packet is passed to the user. Packets sent through a SOCK_DGRAM packet socket get a suitable physical layer header based on the information in the sockaddr_ll destination address before they are queued. By default all packets of the specified protocol type are passed to a packet socket. To only get packets from a specific interface use specifying an address in a struct sockaddr_ll to bind the packet to an interface.
ADDRESS TYPESThe sockaddr_ll is a device independent physical layer address.
sll_protocol is the standard ethernet protocol type in network order as defined in the linux/if_ether.h include file. sll_hatype is a ARP type as defined in the linux/if_arp.h include file. sll_pkttype contains the packet type. Valid types are PACKET_HOST for a packet addressed to the local host, PACKET_BROADCAST for a physical layer broadcast packet, PACKET_MULTICAST for a packet sent to a physical layer multicast address, PACKET_OTHERHOST for a packet to some other host that has been caught by a device driver in promiscuous mode, and PACKET_OUTGOING for a packet originated from the local host that is looped back to a packet socket. sll_addr and sll_halen contain the physical layer (e.g. IEEE 802.3) address and its length. The exact interpretation depends on the device. SOCKET OPTIONSPacket sockets can be used to configure physical layer multicasting and promiscuous mode. It works by calling on a packet socket for SOL_PACKET and one of the options PACKET_ADD_MEMBERSHIP to add a binding or PACKET_DROP_MEMBERSHIP to drop it. They both expect a packet_mreq structure as argument:
mr_ifindex contains the interface index for the interface whose status should be changed. The mr_type parameter specifies which action to perform. PACKET_MR_PROMISC enables receiving all packets on a shared medium - often known as ``promiscuous mode'', PACKET_MR_MULTICAST binds the socket to the physical layer multicast group specified in mr_address and mr_alen, and PACKET_MR_ALLMULTI sets the socket up to receive all multicast packets arriving at the interface. In addition the traditional ioctls SIOCSIFFLAGS, SIOCADDMULTI, SIOCDELMULTI can be used for the same purpose.
IOCTLSSIOCGSTAMP can be used to receive the time stamp of the last received packet. Argument is a struct timeval.In addition all standard ioctls defined in and are valid on packet sockets. ERROR HANDLINGPacket sockets do no error handling other than errors occurred while passing the packet to the device driver. They don't have the concept of a pending error.COMPATIBILITYIn Linux 2.0, the only way to get a packet socket was by calling socket(PF_INET, SOCK_PACKET, protocol). This is still supported but strongly deprecated. The main difference between the two methods is that SOCK_PACKET uses the old struct sockaddr_pkt to specify an interface, which doesn't provide physical layer independence.
spkt_family contains the device type, spkt_protocol is the IEEE 802.3 protocol type as defined in <sys/if_ether.h> and spkt_device is the device name as a null terminated string, e.g. eth0. This structure is obsolete and should not be used in new code. NOTESFor portable programs it is suggested to use PF_PACKET via although this only covers a subset of the PF_PACKET features.ERRORS
VERSIONSPF_PACKET is a new feature in Linux 2.2. Earlier Linux versions supported only SOCK_PACKET.BUGSglibc 2.1 does not have a define for SOL_PACKET. The suggested workaround is to use
Socket filters are not documented. CREDITSThis man page was writen by Andi Kleen with help from Matthew Wilcox. PF_PACKET in Linux 2.2 was implemented by Alexey Kuznetsov, based on code by Alan Cox and others.SEE ALSO
The
linux/if_ether.h
include file for physical layer protocols.
Index
This document was created by using the manual pages. Time: 12:14:03 GMT, March 24, 2025 |
|
© 2001 Red Hat, Inc. All rights reserved. About Red Hat : Legal statement : Privacy statement : Y2K statement : Contact Red Hat |