packetのヘルプ・マニュアル
日本語 英語
packet --help
man packet
PACKET(7) Linux Programmer’s Manual PACKET(7)
名前
packet, AF_PACKET - デバイスレベルのパケットインターフェース
書式
#include
#include
#include /* the L2 protocols */
packet_socket = socket(AF_PACKET, int socket_type, int protocol);
説明
packet ソケットは、デバイスドライバ (OSI レイヤ 2) レベルで生のパケット
(raw packet) を送受信するために用いられる。 packet ソケットを使うと、ユ
ーザー空間で物理層の上にプロトコルモジュールを実装することができる。
socket_type には SOCK_RAW と SOCK_DGRAM のいずれかを指定する。 SOCK_RAW
はリンクレベルヘッダを含む raw パケットを、 SOCK_DGRAM はリンクレベルヘ
ッ ダが削除された加工済みパケットを示す。リンクレベルヘッダ情報は sock-
addr_ll で共通のフォーマットで入手できる。 protocol には IEEE 802.3 プ
ロ トコル番号をネットワークバイトオーダーで指定する。指定できるプロトコ
ルのリストは、インクルードファイル を参照。プロト コ
ル を htons(ETH_P_ALL) にすると、全てのプロトコルが受信される。外部から
来たパケットのうち指定したプロトコルのものは、カーネルに実装されてい る
プロトコルに渡される前の段階で、 packet ソケットに渡される。
packet ソケットをオープンできるのは、実効ユーザーID が 0 のプロセスか、
CAP_NET_RAW ケーパビリティを持つプロセスだけである。
SOCK_RAW パケットでは、パケットをデバイスドライバと受け渡しする際、パケ
ッ トデータに変更が行われることはない。パケットの受信時には、アドレスの
解析だけは行われ、標準的な sockaddr_ll アドレス構造体に渡される。パケッ
ト の送信時には、ユーザが指定するバッファに物理層のヘッダが含まれている
必要がある。パケットはそのまま修正を受けずに、行き先アドレスから決定 さ
れ るインターフェースのネットワークドライバにキューイングされる。デバイ
スドライバによっては、他のヘッダを常に追加するものもある。 SOCK_RAW は
Linux 2.0 の obosolete な AF_INET/SOCK_PACKET と似ているが、互換性があ
るわけではない。
SOCK_DGRAM はやや高位のレベルで動作する。物理ヘッダは、パケットがユーザ
ー に渡される前に削除される。 SOCK_DGRAM の packet ソケットを通して送ら
れるパケットは、 sockaddr_ll の行き先アドレスの情報に基づき、適切な物理
層のヘッダが付加されてから、キューに送られる。
デ フォルトでは、指定したプロトコル型のパケットはすべて packet ソケット
に送られる。特定のインターフェースからのパケットだけを取得したい場合 に
は、 struct sockaddr_ll にアドレスを指定して bind(2) を呼び、 packet ソ
ケットをそのインターフェースに結び付ける (バインドする)。バインドの際に
は、アドレスフィールドのうち sll_protocol と sll_ifindex だけが用いられ
る。
connect(2) 操作は packet ソケットではサポートされていない。
MSG_TRUNC フラグが recvmsg(2), recv(2), recvfrom(2) に渡されると、 ( バ
ッ ファサイズより大きかったとしても) 常に実際に通信されたパケットの長さ
が返される。
アドレスのタイプ
sockaddr_ll はデバイスに依存しない物理層のアドレスである。
struct sockaddr_ll {
unsigned short sll_family; /* 常に AF_PACKET */
unsigned short sll_protocol; /* 物理層のプロトコル */
int sll_ifindex; /* インターフェース番号 */
unsigned short sll_hatype; /* ヘッダ種別 */
unsigned char sll_pkttype; /* パケット種別 */
unsigned char sll_halen; /* アドレスの長さ */
unsigned char sll_addr[8]; /* 物理層のアドレス */
};
sll_protocol は標準的なイーサネットプロトコルのタイプで、ネットワークバ
イ トオーダーで記述する。インクルードファイル で定義
されている。これがこのソケットのプロトコルのデフォルトとなる。
sll_ifindex はそのインターフェースの interface index で あ る (netde-
vice(7) を参照)。 0 は (バインドが許可されている) 任意のインターフェー
スにマッチする。
sll_hatype は、インクルードファイル で定義されて い る
ARP 種別である。
sll_pkttype はパケット種別である。指定できる種別は以下のいずれかである:
PACKET_HOST (ローカルホスト向けのパケット)、 PACKET_BORADCAST (物理層の
ブ ロードキャストパケット)、 PACKET_MULTICAST (物理層のマルチキャストア
ドレスに送るパケット)、 PACKET_OTHERHOST (他のホストに向けられたパケ ッ
ト のうち、無差別モード (promiscuous mode: 後述) のデバイスドライバによ
り補足されたもの)、 PACKET_OUTGOING (ローカルホストか ら 発 信 さ れ 、
packet ソケットにループバックしてきたパケット)。これらの種別が意味を持
つのは受信時のみである。
sll_addr と sll_halen は、物理層の (つまり IEEE 802.3 の) アドレスと そ
の長さである。厳密な解釈はデバイスに依存する。
パ ケットを送る場合は、 sll_family, sll_addr, sll_halen, sll_ifindex を
指定すれば十分である。その他のフィールドは 0 にしておくべき で あ る 。
sll_hatype と sll_pkttype には受信したパケットの情報が設定される。バイ
ンドの際には、 sll_protocol と sll_ifindex だけが使用される。
ソケットオプション
packet ソケットは、物理層のマルチキャストや無差別モ ー ド (promiscuous
mode) を設定して使うことができる。これには SOL_PACKET と以下のオプショ
ンのいずれかを指定して setsockopt(2) を呼べばよい。バインドを追加する場
合 は PACKET_ADD_MEMBERSHIP であり、取り去る場合は PACKET_DROP_MEMBER-
SHIP である。これらはいずれも packet_mreq 構造体を引き数に取る。
struct packet_mreq {
int mr_ifindex; /* インターフェース番号 */
unsigned short mr_type; /* 動作 */
unsigned short mr_alen; /* アドレスの長さ */
unsigned char mr_address[8]; /* 物理層のアドレス */
};
mr_ifindex は、ステータスを変更したいインターフェースのインターフェース
番 号 で あ る 。 mr_type パ ラ メ ー タ は 実 行する動作を指定する:
PACKET_MR_PROMISC は、共有している媒体からの全てのパケットを受信でき る
よ う に する (しばしば "無差別モード (promiscuous mode)" と呼ばれる)。
PACKET_MR_MULTICAST は、そのソケットを、 mr_address と mr_alen で指定さ
れ る 物理層のマルチキャストブループにバインドする。 PACKET_MR_ALLMULTI
は socket を up にして、そのインターフェースに到達したすべてのマルチ キ
ャストパケットを受信できるようにする。
昔 からある ioctl だけでなく、 SIOCSIFFLAGS, SIOCADDMULTI, SIOCDELMULTI
を同じ目的に用いることができる。
ioctl
SIOCGSTAMP を用いると、最後に受信したパケットのタイムスタンプを得ること
ができる。引き数は struct timeval である。
さ らに、 netdevice(7) および socket(7) で定義されている標準の ioctl は
いずれも packet ソケットに指定可能である。
エラー処理
packet ソケットは、パケットをデバイスドライバに渡すときに起きたエラーし
か処理しない。遅延エラー (pending error) に関する概念は持っていない。
エラー
EADDRNOTAVAIL
不明なマルチキャストグループアドレスが渡された。
EFAULT ユーザが渡したメモリアドレスが不正。
EINVAL 引き数が不正。
EMSGSIZE
パケットがインターフェースの MTU より大きい。
ENETDOWN
インターフェースが up でない。
ENOBUFS
パケットに割り当てるメモリが足りない。
ENODEV デバイス名が不明。あるいはインターフェースアドレスで指定されたイ
ンターフェースインデックスが不明。
ENOENT パケットを一つも受信していない。
ENOTCONN
インターフェースアドレスが渡されなかった。
ENXIO インターフェースアドレスに不正なインターフェースインデックスが含
まれている。
EPERM この操作を行うのに必要な権限をユーザが持っていない。
上記以外のエラーが、低レベルのドライバで生成されることがある。
バージョン
AF_PACKET は Linux 2.2 の新機能である。これより古いバージョンの Linux
では SOCK_PACKET のみをサポートしていた。
インクルードファイル が存在するのは glibc 2.1 以降
である。それ以前のシステムでは以下のようにする必要がある:
#include
#include
#include /* The L2 protocols */
注意
移 植性の必要なプログラムでは、 pcap(3) 経由で AF_PACKET を用いることを
お薦めする。ただし、この方法では AF_PACKET の機能すべてを利用することは
できない。
SOCK_DGRAM packet ソケットは、IEEE 802.3 フレームの IEEE 802.2 LLC ヘッ
ダの生成や解析を行おうとしない。 ETH_P_802_3 が送信プロトコルに指定され
ると、カーネルは 802.3 フレームを生成して length フィールドに書き込む。
完全に準拠したパケットを得るためにはユーザーが LLC ヘッダを与える必要が
ある。到着した 802.3 パケットでは、 DSAP/SSAP protocol の各フィールドは
多重化 (multiplex) されていない。代わりにこれらは LLC ヘッダが前置さ れ
た ETH_P_802_2 プロトコルとして与えられる。したがって、 ETH_P_802_3 に
バインドすることはできない。かわりに ETH_P_802_2 にバインドし、自分自身
で プロトコルの多重化を行うこと。送信のデフォルトは、プロトコルフィール
ドを持つ標準の Ethernet DIX encapsulation である。
packet ソケットは入出力の firewall chain に影響をうけない。
移植性
Linux 2.0 では、 packet ソ ケ ッ ト を 得 る 方 法 は socket(AF_INET,
SOCK_PACKET, protocol) を呼ぶやり方しかなかった。この方法はまだサポート
されているが、用いないことを強く推奨する。現在の方法との主な違 い は 、
SOCK_PACKET ではインターフェースの指定に古い struct sockaddr_pkt を用い
る点である。これには物理層からの独立性がない。
struct sockaddr_pkt {
unsigned short spkt_family;
unsigned char spkt_device[14];
unsigned short spkt_protocol;
};
spkt_family はデバイスのタイプ、 spkt_protocol は で定
義されている IEEE 802.3 プロトコルタイプ、 spkt_device はデバイスの名前
を NULL 終端された文字列で与えたもの (例: eth0) である。
この構造体は obsolete であり、新しくコードを書く時には用いるべきでな い
。
バグ
glibc 2.1 には SOL_PACKET の定義がない。回避策としては、以下のようにす
るとよい。
#ifndef SOL_PACKET
#define SOL_PACKET 263
#endif
この問題は新しいバージョンの glibc では修正されている。 libc5 のシス テ
ムにはこの問題はない。
IEEE 802.2/803.3 の LLC の扱い方は、バグと考えても良いだろう。
ソケットフィルターについて記載されていない。
MSG_TRUNC recvmsg(2) 拡張は非常にまずい対処であり、制御メッセージで置き
換えるべきである。今のところ SOCK_DGRAM 経由でパケットについていた宛 先
アドレスを得る方法がない。
関連項目
socket(2), pcap(3), capabilities(7), ip(7), raw(7), socket(7)
標準 IP Ethernet encapsulation に関する情報は RFC 894 にある。
IEEE 802.3 IP encapsulation に関する情報は RFC 1700 にある。
物 理層のプロトコルに関する記述は インクルードファイ
ルにある。
Linux 2008-08-08 PACKET(7)
PACKET(7) Linux Programmer’s Manual PACKET(7)
NAME
packet, AF_PACKET - packet interface on device level.
SYNOPSIS
#include
#include
#include /* 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.
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 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.
Only processes with effective UID 0 or the CAP_NET_RAW capability may
open packet sockets.
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 should contain the
physical layer header. That packet is then queued unmodified to the
network driver of the interface defined by the destination address.
Some device drivers always add other headers. SOCK_RAW is similar to
but not compatible with the obsolete AF_INET/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
bind(2) specifying an address in a struct sockaddr_ll to bind the
packet socket to an interface. Only the sll_protocol and the
sll_ifindex address fields are used for purposes of binding.
The connect(2) operation is not supported on packet sockets.
When the MSG_TRUNC flag is passed to recvmsg(2), recv(2), recvfrom(2)
the real length of the packet on the wire is always returned, even when
it is longer than the buffer.
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; /* Header type */
unsigned char sll_pkttype; /* Packet type */
unsigned char sll_halen; /* Length of address */
unsigned char sll_addr[8]; /* Physical layer address */
};
sll_protocol is the standard ethernet protocol type in network order as
defined in the include file. It defaults to the
socket’s protocol. sll_ifindex is the interface index of the interface
(see netdevice(7)); 0 matches any interface (only permitted for bind-
ing). sll_hatype is a ARP type as defined in the
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. These types make
only sense for receiving. sll_addr and sll_halen contain the physical
layer (e.g., IEEE 802.3) address and its length. The exact interpreta-
tion depends on the device.
When you send packets it is enough to specify sll_family, sll_addr,
sll_halen, sll_ifindex. The other fields should be 0. sll_hatype and
sll_pkttype are set on received packets for your information. For bind
only sll_protocol and sll_ifindex are used.
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 */
};
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, SIOCDEL-
MULTI can be used for the same purpose.
Ioctls
SIOCGSTAMP can be used to receive the timestamp of the last received
packet. Argument is a struct timeval.
In addition all standard ioctls defined in netdevice(7) and socket(7)
are valid on packet sockets.
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.
In addition other errors may be generated by the low-level
driver.
VERSIONS
AF_PACKET is a new feature in Linux 2.2. Earlier Linux versions sup-
ported only SOCK_PACKET.
The include file is present since glibc 2.1.
Older systems need:
#include
#include
#include /* The L2 protocols */
NOTES
For portable programs it is suggested to use AF_PACKET via pcap(3);
although this only covers a subset of the AF_PACKET features.
The SOCK_DGRAM packet sockets make no attempt to create or parse the
IEEE 802.2 LLC header for a IEEE 802.3 frame. When ETH_P_802_3 is
specified as protocol for sending the kernel creates the 802.3 frame
and fills out the length field; the user has to supply the LLC header
to get a fully conforming packet. Incoming 802.3 packets are not mul-
tiplexed on the DSAP/SSAP protocol fields; instead they are supplied to
the user as protocol ETH_P_802_2 with the LLC header prepended. It is
thus not possible to bind to ETH_P_802_3; bind to ETH_P_802_2 instead
and do the protocol multiplex yourself. The default for sending is the
standard Ethernet DIX encapsulation with the protocol filled in.
Packet sockets are not subject to the input or output firewall chains.
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 inter-
face, which doesn’t provide physical layer independence.
struct sockaddr_pkt {
unsigned short spkt_family;
unsigned char spkt_device[14];
unsigned short spkt_protocol;
};
spkt_family contains the device type, spkt_protocol is the IEEE 802.3
protocol type as defined in and spkt_device is the
device name as a null-terminated string, for example, eth0.
This structure is obsolete and should not be used in new code.
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)
RFC 894 for the standard IP Ethernet encapsulation.
RFC 1700 for the IEEE 802.3 IP encapsulation.
The include file for physical layer protocols.
COLOPHON
This page is part of release 3.22 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/.
Linux 2008-08-08 PACKET(7)