xref: /linux/include/uapi/linux/if_packet.h (revision ab1c247094e323177a578b38f0325bf79f0317ac)
16f52b16cSGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2607ca46eSDavid Howells #ifndef __LINUX_IF_PACKET_H
3607ca46eSDavid Howells #define __LINUX_IF_PACKET_H
4607ca46eSDavid Howells 
59c661b0bSTanner Love #include <asm/byteorder.h>
6607ca46eSDavid Howells #include <linux/types.h>
7607ca46eSDavid Howells 
8607ca46eSDavid Howells struct sockaddr_pkt {
9607ca46eSDavid Howells 	unsigned short spkt_family;
10607ca46eSDavid Howells 	unsigned char spkt_device[14];
11607ca46eSDavid Howells 	__be16 spkt_protocol;
12607ca46eSDavid Howells };
13607ca46eSDavid Howells 
14607ca46eSDavid Howells struct sockaddr_ll {
15607ca46eSDavid Howells 	unsigned short	sll_family;
16607ca46eSDavid Howells 	__be16		sll_protocol;
17607ca46eSDavid Howells 	int		sll_ifindex;
18607ca46eSDavid Howells 	unsigned short	sll_hatype;
19607ca46eSDavid Howells 	unsigned char	sll_pkttype;
20607ca46eSDavid Howells 	unsigned char	sll_halen;
21607ca46eSDavid Howells 	unsigned char	sll_addr[8];
22607ca46eSDavid Howells };
23607ca46eSDavid Howells 
24607ca46eSDavid Howells /* Packet types */
25607ca46eSDavid Howells 
26607ca46eSDavid Howells #define PACKET_HOST		0		/* To us		*/
27607ca46eSDavid Howells #define PACKET_BROADCAST	1		/* To all		*/
28607ca46eSDavid Howells #define PACKET_MULTICAST	2		/* To group		*/
29607ca46eSDavid Howells #define PACKET_OTHERHOST	3		/* To someone else 	*/
30607ca46eSDavid Howells #define PACKET_OUTGOING		4		/* Outgoing of any type */
31607ca46eSDavid Howells #define PACKET_LOOPBACK		5		/* MC/BRD frame looped back */
32604d13c9SDaniel Borkmann #define PACKET_USER		6		/* To user space	*/
33604d13c9SDaniel Borkmann #define PACKET_KERNEL		7		/* To kernel space	*/
34604d13c9SDaniel Borkmann /* Unused, PACKET_FASTROUTE and PACKET_LOOPBACK are invisible to user space */
35607ca46eSDavid Howells #define PACKET_FASTROUTE	6		/* Fastrouted frame	*/
36607ca46eSDavid Howells 
37607ca46eSDavid Howells /* Packet socket options */
38607ca46eSDavid Howells 
39607ca46eSDavid Howells #define PACKET_ADD_MEMBERSHIP		1
40607ca46eSDavid Howells #define PACKET_DROP_MEMBERSHIP		2
41607ca46eSDavid Howells #define PACKET_RECV_OUTPUT		3
42607ca46eSDavid Howells /* Value 4 is still used by obsolete turbo-packet. */
43607ca46eSDavid Howells #define PACKET_RX_RING			5
44607ca46eSDavid Howells #define PACKET_STATISTICS		6
45607ca46eSDavid Howells #define PACKET_COPY_THRESH		7
46607ca46eSDavid Howells #define PACKET_AUXDATA			8
47607ca46eSDavid Howells #define PACKET_ORIGDEV			9
48607ca46eSDavid Howells #define PACKET_VERSION			10
49607ca46eSDavid Howells #define PACKET_HDRLEN			11
50607ca46eSDavid Howells #define PACKET_RESERVE			12
51607ca46eSDavid Howells #define PACKET_TX_RING			13
52607ca46eSDavid Howells #define PACKET_LOSS			14
53607ca46eSDavid Howells #define PACKET_VNET_HDR			15
54607ca46eSDavid Howells #define PACKET_TX_TIMESTAMP		16
55607ca46eSDavid Howells #define PACKET_TIMESTAMP		17
56607ca46eSDavid Howells #define PACKET_FANOUT			18
575920cd3aSPaul Chavent #define PACKET_TX_HAS_OFF		19
58d346a3faSDaniel Borkmann #define PACKET_QDISC_BYPASS		20
59a9b63918SWillem de Bruijn #define PACKET_ROLLOVER_STATS		21
6047dceb8eSWillem de Bruijn #define PACKET_FANOUT_DATA		22
61fa788d98SVincent Whitchurch #define PACKET_IGNORE_OUTGOING		23
62*dfc39d40SJianfeng Tan #define PACKET_VNET_HDR_SZ		24
63607ca46eSDavid Howells 
64607ca46eSDavid Howells #define PACKET_FANOUT_HASH		0
65607ca46eSDavid Howells #define PACKET_FANOUT_LB		1
66607ca46eSDavid Howells #define PACKET_FANOUT_CPU		2
6777f65ebdSWillem de Bruijn #define PACKET_FANOUT_ROLLOVER		3
685df0ddfbSDaniel Borkmann #define PACKET_FANOUT_RND		4
692d36097dSNeil Horman #define PACKET_FANOUT_QM		5
7047dceb8eSWillem de Bruijn #define PACKET_FANOUT_CBPF		6
71f2e52095SWillem de Bruijn #define PACKET_FANOUT_EBPF		7
7277f65ebdSWillem de Bruijn #define PACKET_FANOUT_FLAG_ROLLOVER	0x1000
734a69a864SMike Maloney #define PACKET_FANOUT_FLAG_UNIQUEID	0x2000
7458ba4263SWillem de Bruijn #define PACKET_FANOUT_FLAG_IGNORE_OUTGOING     0x4000
75607ca46eSDavid Howells #define PACKET_FANOUT_FLAG_DEFRAG	0x8000
76607ca46eSDavid Howells 
77607ca46eSDavid Howells struct tpacket_stats {
78607ca46eSDavid Howells 	unsigned int	tp_packets;
79607ca46eSDavid Howells 	unsigned int	tp_drops;
80607ca46eSDavid Howells };
81607ca46eSDavid Howells 
82607ca46eSDavid Howells struct tpacket_stats_v3 {
83607ca46eSDavid Howells 	unsigned int	tp_packets;
84607ca46eSDavid Howells 	unsigned int	tp_drops;
85607ca46eSDavid Howells 	unsigned int	tp_freeze_q_cnt;
86607ca46eSDavid Howells };
87607ca46eSDavid Howells 
88a9b63918SWillem de Bruijn struct tpacket_rollover_stats {
89a9b63918SWillem de Bruijn 	__aligned_u64	tp_all;
90a9b63918SWillem de Bruijn 	__aligned_u64	tp_huge;
91a9b63918SWillem de Bruijn 	__aligned_u64	tp_failed;
92a9b63918SWillem de Bruijn };
93a9b63918SWillem de Bruijn 
94607ca46eSDavid Howells union tpacket_stats_u {
95607ca46eSDavid Howells 	struct tpacket_stats stats1;
96607ca46eSDavid Howells 	struct tpacket_stats_v3 stats3;
97607ca46eSDavid Howells };
98607ca46eSDavid Howells 
99607ca46eSDavid Howells struct tpacket_auxdata {
100607ca46eSDavid Howells 	__u32		tp_status;
101607ca46eSDavid Howells 	__u32		tp_len;
102607ca46eSDavid Howells 	__u32		tp_snaplen;
103607ca46eSDavid Howells 	__u16		tp_mac;
104607ca46eSDavid Howells 	__u16		tp_net;
105607ca46eSDavid Howells 	__u16		tp_vlan_tci;
106a0cdfcf3SAtzm Watanabe 	__u16		tp_vlan_tpid;
107607ca46eSDavid Howells };
108607ca46eSDavid Howells 
109607ca46eSDavid Howells /* Rx ring - header status */
1107276d5d7SDaniel Borkmann #define TP_STATUS_KERNEL		      0
1117276d5d7SDaniel Borkmann #define TP_STATUS_USER			(1 << 0)
1127276d5d7SDaniel Borkmann #define TP_STATUS_COPY			(1 << 1)
1137276d5d7SDaniel Borkmann #define TP_STATUS_LOSING		(1 << 2)
1147276d5d7SDaniel Borkmann #define TP_STATUS_CSUMNOTREADY		(1 << 3)
1157276d5d7SDaniel Borkmann #define TP_STATUS_VLAN_VALID		(1 << 4) /* auxdata has valid tp_vlan_tci */
1167276d5d7SDaniel Borkmann #define TP_STATUS_BLK_TMO		(1 << 5)
117a0cdfcf3SAtzm Watanabe #define TP_STATUS_VLAN_TPID_VALID	(1 << 6) /* auxdata has valid tp_vlan_tpid */
118682f048bSAlexander Drozdov #define TP_STATUS_CSUM_VALID		(1 << 7)
1198e08bb75SXin Long #define TP_STATUS_GSO_TCP		(1 << 8)
120607ca46eSDavid Howells 
121607ca46eSDavid Howells /* Tx ring - header status */
1227276d5d7SDaniel Borkmann #define TP_STATUS_AVAILABLE	      0
1237276d5d7SDaniel Borkmann #define TP_STATUS_SEND_REQUEST	(1 << 0)
1247276d5d7SDaniel Borkmann #define TP_STATUS_SENDING	(1 << 1)
1257276d5d7SDaniel Borkmann #define TP_STATUS_WRONG_FORMAT	(1 << 2)
126607ca46eSDavid Howells 
127b9c32fb2SDaniel Borkmann /* Rx and Tx ring - header status */
128b9c32fb2SDaniel Borkmann #define TP_STATUS_TS_SOFTWARE		(1 << 29)
12968a360e8SWillem de Bruijn #define TP_STATUS_TS_SYS_HARDWARE	(1 << 30) /* deprecated, never set */
13079293f49SJiunn Chang #define TP_STATUS_TS_RAW_HARDWARE	(1U << 31)
131b9c32fb2SDaniel Borkmann 
132607ca46eSDavid Howells /* Rx ring - feature request bits */
133607ca46eSDavid Howells #define TP_FT_REQ_FILL_RXHASH	0x1
134607ca46eSDavid Howells 
135607ca46eSDavid Howells struct tpacket_hdr {
136607ca46eSDavid Howells 	unsigned long	tp_status;
137607ca46eSDavid Howells 	unsigned int	tp_len;
138607ca46eSDavid Howells 	unsigned int	tp_snaplen;
139607ca46eSDavid Howells 	unsigned short	tp_mac;
140607ca46eSDavid Howells 	unsigned short	tp_net;
141607ca46eSDavid Howells 	unsigned int	tp_sec;
142607ca46eSDavid Howells 	unsigned int	tp_usec;
143607ca46eSDavid Howells };
144607ca46eSDavid Howells 
145607ca46eSDavid Howells #define TPACKET_ALIGNMENT	16
146607ca46eSDavid Howells #define TPACKET_ALIGN(x)	(((x)+TPACKET_ALIGNMENT-1)&~(TPACKET_ALIGNMENT-1))
147607ca46eSDavid Howells #define TPACKET_HDRLEN		(TPACKET_ALIGN(sizeof(struct tpacket_hdr)) + sizeof(struct sockaddr_ll))
148607ca46eSDavid Howells 
149607ca46eSDavid Howells struct tpacket2_hdr {
150607ca46eSDavid Howells 	__u32		tp_status;
151607ca46eSDavid Howells 	__u32		tp_len;
152607ca46eSDavid Howells 	__u32		tp_snaplen;
153607ca46eSDavid Howells 	__u16		tp_mac;
154607ca46eSDavid Howells 	__u16		tp_net;
155607ca46eSDavid Howells 	__u32		tp_sec;
156607ca46eSDavid Howells 	__u32		tp_nsec;
157607ca46eSDavid Howells 	__u16		tp_vlan_tci;
158a0cdfcf3SAtzm Watanabe 	__u16		tp_vlan_tpid;
159a0cdfcf3SAtzm Watanabe 	__u8		tp_padding[4];
160607ca46eSDavid Howells };
161607ca46eSDavid Howells 
162607ca46eSDavid Howells struct tpacket_hdr_variant1 {
163607ca46eSDavid Howells 	__u32	tp_rxhash;
164607ca46eSDavid Howells 	__u32	tp_vlan_tci;
165a0cdfcf3SAtzm Watanabe 	__u16	tp_vlan_tpid;
166a0cdfcf3SAtzm Watanabe 	__u16	tp_padding;
167607ca46eSDavid Howells };
168607ca46eSDavid Howells 
169607ca46eSDavid Howells struct tpacket3_hdr {
170607ca46eSDavid Howells 	__u32		tp_next_offset;
171607ca46eSDavid Howells 	__u32		tp_sec;
172607ca46eSDavid Howells 	__u32		tp_nsec;
173607ca46eSDavid Howells 	__u32		tp_snaplen;
174607ca46eSDavid Howells 	__u32		tp_len;
175607ca46eSDavid Howells 	__u32		tp_status;
176607ca46eSDavid Howells 	__u16		tp_mac;
177607ca46eSDavid Howells 	__u16		tp_net;
178607ca46eSDavid Howells 	/* pkt_hdr variants */
179607ca46eSDavid Howells 	union {
180607ca46eSDavid Howells 		struct tpacket_hdr_variant1 hv1;
181607ca46eSDavid Howells 	};
182a0cdfcf3SAtzm Watanabe 	__u8		tp_padding[8];
183607ca46eSDavid Howells };
184607ca46eSDavid Howells 
185607ca46eSDavid Howells struct tpacket_bd_ts {
186607ca46eSDavid Howells 	unsigned int ts_sec;
187607ca46eSDavid Howells 	union {
188607ca46eSDavid Howells 		unsigned int ts_usec;
189607ca46eSDavid Howells 		unsigned int ts_nsec;
190607ca46eSDavid Howells 	};
191607ca46eSDavid Howells };
192607ca46eSDavid Howells 
193607ca46eSDavid Howells struct tpacket_hdr_v1 {
194607ca46eSDavid Howells 	__u32	block_status;
195607ca46eSDavid Howells 	__u32	num_pkts;
196607ca46eSDavid Howells 	__u32	offset_to_first_pkt;
197607ca46eSDavid Howells 
198607ca46eSDavid Howells 	/* Number of valid bytes (including padding)
199607ca46eSDavid Howells 	 * blk_len <= tp_block_size
200607ca46eSDavid Howells 	 */
201607ca46eSDavid Howells 	__u32	blk_len;
202607ca46eSDavid Howells 
203607ca46eSDavid Howells 	/*
204607ca46eSDavid Howells 	 * Quite a few uses of sequence number:
205607ca46eSDavid Howells 	 * 1. Make sure cache flush etc worked.
206607ca46eSDavid Howells 	 *    Well, one can argue - why not use the increasing ts below?
207607ca46eSDavid Howells 	 *    But look at 2. below first.
208607ca46eSDavid Howells 	 * 2. When you pass around blocks to other user space decoders,
209607ca46eSDavid Howells 	 *    you can see which blk[s] is[are] outstanding etc.
210607ca46eSDavid Howells 	 * 3. Validate kernel code.
211607ca46eSDavid Howells 	 */
212607ca46eSDavid Howells 	__aligned_u64	seq_num;
213607ca46eSDavid Howells 
214607ca46eSDavid Howells 	/*
215607ca46eSDavid Howells 	 * ts_last_pkt:
216607ca46eSDavid Howells 	 *
217607ca46eSDavid Howells 	 * Case 1.	Block has 'N'(N >=1) packets and TMO'd(timed out)
218607ca46eSDavid Howells 	 *		ts_last_pkt == 'time-stamp of last packet' and NOT the
219607ca46eSDavid Howells 	 *		time when the timer fired and the block was closed.
220607ca46eSDavid Howells 	 *		By providing the ts of the last packet we can absolutely
221607ca46eSDavid Howells 	 *		guarantee that time-stamp wise, the first packet in the
222607ca46eSDavid Howells 	 *		next block will never precede the last packet of the
223607ca46eSDavid Howells 	 *		previous block.
224607ca46eSDavid Howells 	 * Case 2.	Block has zero packets and TMO'd
225607ca46eSDavid Howells 	 *		ts_last_pkt = time when the timer fired and the block
226607ca46eSDavid Howells 	 *		was closed.
227607ca46eSDavid Howells 	 * Case 3.	Block has 'N' packets and NO TMO.
228607ca46eSDavid Howells 	 *		ts_last_pkt = time-stamp of the last pkt in the block.
229607ca46eSDavid Howells 	 *
230607ca46eSDavid Howells 	 * ts_first_pkt:
231607ca46eSDavid Howells 	 *		Is always the time-stamp when the block was opened.
232607ca46eSDavid Howells 	 *		Case a)	ZERO packets
233607ca46eSDavid Howells 	 *			No packets to deal with but atleast you know the
234607ca46eSDavid Howells 	 *			time-interval of this block.
235607ca46eSDavid Howells 	 *		Case b) Non-zero packets
236607ca46eSDavid Howells 	 *			Use the ts of the first packet in the block.
237607ca46eSDavid Howells 	 *
238607ca46eSDavid Howells 	 */
239607ca46eSDavid Howells 	struct tpacket_bd_ts	ts_first_pkt, ts_last_pkt;
240607ca46eSDavid Howells };
241607ca46eSDavid Howells 
242607ca46eSDavid Howells union tpacket_bd_header_u {
243607ca46eSDavid Howells 	struct tpacket_hdr_v1 bh1;
244607ca46eSDavid Howells };
245607ca46eSDavid Howells 
246607ca46eSDavid Howells struct tpacket_block_desc {
247607ca46eSDavid Howells 	__u32 version;
248607ca46eSDavid Howells 	__u32 offset_to_priv;
249607ca46eSDavid Howells 	union tpacket_bd_header_u hdr;
250607ca46eSDavid Howells };
251607ca46eSDavid Howells 
252607ca46eSDavid Howells #define TPACKET2_HDRLEN		(TPACKET_ALIGN(sizeof(struct tpacket2_hdr)) + sizeof(struct sockaddr_ll))
253607ca46eSDavid Howells #define TPACKET3_HDRLEN		(TPACKET_ALIGN(sizeof(struct tpacket3_hdr)) + sizeof(struct sockaddr_ll))
254607ca46eSDavid Howells 
255607ca46eSDavid Howells enum tpacket_versions {
256607ca46eSDavid Howells 	TPACKET_V1,
257607ca46eSDavid Howells 	TPACKET_V2,
258607ca46eSDavid Howells 	TPACKET_V3
259607ca46eSDavid Howells };
260607ca46eSDavid Howells 
261607ca46eSDavid Howells /*
262607ca46eSDavid Howells    Frame structure:
263607ca46eSDavid Howells 
264607ca46eSDavid Howells    - Start. Frame must be aligned to TPACKET_ALIGNMENT=16
265607ca46eSDavid Howells    - struct tpacket_hdr
266607ca46eSDavid Howells    - pad to TPACKET_ALIGNMENT=16
267607ca46eSDavid Howells    - struct sockaddr_ll
268607ca46eSDavid Howells    - Gap, chosen so that packet data (Start+tp_net) alignes to TPACKET_ALIGNMENT=16
269607ca46eSDavid Howells    - Start+tp_mac: [ Optional MAC header ]
270607ca46eSDavid Howells    - Start+tp_net: Packet data, aligned to TPACKET_ALIGNMENT=16.
271607ca46eSDavid Howells    - Pad to align to TPACKET_ALIGNMENT=16
272607ca46eSDavid Howells  */
273607ca46eSDavid Howells 
274607ca46eSDavid Howells struct tpacket_req {
275607ca46eSDavid Howells 	unsigned int	tp_block_size;	/* Minimal size of contiguous block */
276607ca46eSDavid Howells 	unsigned int	tp_block_nr;	/* Number of blocks */
277607ca46eSDavid Howells 	unsigned int	tp_frame_size;	/* Size of frame */
278607ca46eSDavid Howells 	unsigned int	tp_frame_nr;	/* Total number of frames */
279607ca46eSDavid Howells };
280607ca46eSDavid Howells 
281607ca46eSDavid Howells struct tpacket_req3 {
282607ca46eSDavid Howells 	unsigned int	tp_block_size;	/* Minimal size of contiguous block */
283607ca46eSDavid Howells 	unsigned int	tp_block_nr;	/* Number of blocks */
284607ca46eSDavid Howells 	unsigned int	tp_frame_size;	/* Size of frame */
285607ca46eSDavid Howells 	unsigned int	tp_frame_nr;	/* Total number of frames */
286607ca46eSDavid Howells 	unsigned int	tp_retire_blk_tov; /* timeout in msecs */
287607ca46eSDavid Howells 	unsigned int	tp_sizeof_priv; /* offset to private data area */
288607ca46eSDavid Howells 	unsigned int	tp_feature_req_word;
289607ca46eSDavid Howells };
290607ca46eSDavid Howells 
291607ca46eSDavid Howells union tpacket_req_u {
292607ca46eSDavid Howells 	struct tpacket_req	req;
293607ca46eSDavid Howells 	struct tpacket_req3	req3;
294607ca46eSDavid Howells };
295607ca46eSDavid Howells 
296607ca46eSDavid Howells struct packet_mreq {
297607ca46eSDavid Howells 	int		mr_ifindex;
298607ca46eSDavid Howells 	unsigned short	mr_type;
299607ca46eSDavid Howells 	unsigned short	mr_alen;
300607ca46eSDavid Howells 	unsigned char	mr_address[8];
301607ca46eSDavid Howells };
302607ca46eSDavid Howells 
3039c661b0bSTanner Love struct fanout_args {
3049c661b0bSTanner Love #if defined(__LITTLE_ENDIAN_BITFIELD)
3059c661b0bSTanner Love 	__u16		id;
3069c661b0bSTanner Love 	__u16		type_flags;
3079c661b0bSTanner Love #else
3089c661b0bSTanner Love 	__u16		type_flags;
3099c661b0bSTanner Love 	__u16		id;
3109c661b0bSTanner Love #endif
3119c661b0bSTanner Love 	__u32		max_num_members;
3129c661b0bSTanner Love };
3139c661b0bSTanner Love 
314607ca46eSDavid Howells #define PACKET_MR_MULTICAST	0
315607ca46eSDavid Howells #define PACKET_MR_PROMISC	1
316607ca46eSDavid Howells #define PACKET_MR_ALLMULTI	2
317607ca46eSDavid Howells #define PACKET_MR_UNICAST	3
318607ca46eSDavid Howells 
319607ca46eSDavid Howells #endif
320