PACKET(7) | Linux Programmer's Manual | PACKET(7) |
NAME
packet - packet interface on device level.SYNOPSIS
#include<sys/socket.h>
<netpacket/packet.h>
<net/ethernet.h>
/*
the
L2
protocols
*/
packet_socket = socket(AF_PACKET, int socket_type , int protocol );
DESCRIPTION
Packet 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.Address types
The sockaddr_ll is a device independent physical layer address.struct sockaddr_ll {
unsigned short sll_family; /* Always AF_PACKET */
unsigned short sll_protocol; /* Physical layer protocol */
int sll_ifindex; /* Interface number */
unsigned short sll_hatype; /* ARP hardware type */
unsigned char sll_pkttype; /* Packet type */
unsigned char sll_halen; /* Length of address */
unsigned char sll_addr[8]; /* Physical layer address */
};
Socket options
Packet sockets can be used to configure physical layer multicasting and promiscuous mode. It works by calling setsockopt(2) 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:struct packet_mreq {
int mr_ifindex; /* interface index */
unsigned short mr_type; /* action */
unsigned short mr_alen; /* address length */
unsigned char mr_address[8]; /* physical layer address */
};
Ioctls
SIOCGSTAMP can be used to receive the timestamp of the last received packet. Argument is a struct timeval.Error handling
Packet 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.ERRORS
- EADDRNOTAVAIL
- Unknown multicast group address passed.
- EFAULT
- User passed invalid memory address.
- EINVAL
- Invalid argument.
- EMSGSIZE
- Packet is bigger than interface MTU.
- ENETDOWN
- Interface is not up.
- ENOBUFS
- Not enough memory to allocate the packet.
- ENODEV
- Unknown device name or interface index specified in interface address.
- ENOENT
- No packet received.
- ENOTCONN
- No interface address passed.
- ENXIO
- Interface address contained an invalid interface index.
- EPERM
-
User has insufficient privileges to carry out this operation.
VERSIONS
AF_PACKET is a new feature in Linux 2.2. Earlier Linux versions supported only SOCK_PACKET.The include file <netpacket/packet.h> is present since glibc 2.1. Older systems need:
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h> /* The L2 protocols */
NOTES
For portable programs it is suggested to use AF_PACKET via pcap(3); although this covers only a subset of the AF_PACKET features.Compatibility
In Linux 2.0, the only way to get a packet socket was by calling socket(AF_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.struct sockaddr_pkt {
unsigned short spkt_family;
unsigned char spkt_device[14];
unsigned short spkt_protocol;
};
BUGS
glibc 2.1 does not have a define for SOL_PACKET. The suggested workaround is to use:
#ifndef SOL_PACKET
#define SOL_PACKET 263
#endif
This is fixed in later glibc versions and also does not occur on libc5 systems.
The IEEE 802.2/803.3 LLC handling could be considered as a bug.
Socket filters are not documented.
The MSG_TRUNC recvmsg(2) extension is an ugly hack and should be replaced by a control message. There is currently no way to get the original destination address of packets via SOCK_DGRAM.
SEE ALSO
socket(2), pcap(3), capabilities(7), ip(7), raw(7), socket(7)COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.2012-05-10 | Linux |