Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
0002 #ifndef __LINUX_IF_PACKET_H
0003 #define __LINUX_IF_PACKET_H
0004 
0005 #include <asm/byteorder.h>
0006 #include <linux/types.h>
0007 
0008 struct sockaddr_pkt {
0009     unsigned short spkt_family;
0010     unsigned char spkt_device[14];
0011     __be16 spkt_protocol;
0012 };
0013 
0014 struct sockaddr_ll {
0015     unsigned short  sll_family;
0016     __be16      sll_protocol;
0017     int     sll_ifindex;
0018     unsigned short  sll_hatype;
0019     unsigned char   sll_pkttype;
0020     unsigned char   sll_halen;
0021     unsigned char   sll_addr[8];
0022 };
0023 
0024 /* Packet types */
0025 
0026 #define PACKET_HOST     0       /* To us        */
0027 #define PACKET_BROADCAST    1       /* To all       */
0028 #define PACKET_MULTICAST    2       /* To group     */
0029 #define PACKET_OTHERHOST    3       /* To someone else  */
0030 #define PACKET_OUTGOING     4       /* Outgoing of any type */
0031 #define PACKET_LOOPBACK     5       /* MC/BRD frame looped back */
0032 #define PACKET_USER     6       /* To user space    */
0033 #define PACKET_KERNEL       7       /* To kernel space  */
0034 /* Unused, PACKET_FASTROUTE and PACKET_LOOPBACK are invisible to user space */
0035 #define PACKET_FASTROUTE    6       /* Fastrouted frame */
0036 
0037 /* Packet socket options */
0038 
0039 #define PACKET_ADD_MEMBERSHIP       1
0040 #define PACKET_DROP_MEMBERSHIP      2
0041 #define PACKET_RECV_OUTPUT      3
0042 /* Value 4 is still used by obsolete turbo-packet. */
0043 #define PACKET_RX_RING          5
0044 #define PACKET_STATISTICS       6
0045 #define PACKET_COPY_THRESH      7
0046 #define PACKET_AUXDATA          8
0047 #define PACKET_ORIGDEV          9
0048 #define PACKET_VERSION          10
0049 #define PACKET_HDRLEN           11
0050 #define PACKET_RESERVE          12
0051 #define PACKET_TX_RING          13
0052 #define PACKET_LOSS         14
0053 #define PACKET_VNET_HDR         15
0054 #define PACKET_TX_TIMESTAMP     16
0055 #define PACKET_TIMESTAMP        17
0056 #define PACKET_FANOUT           18
0057 #define PACKET_TX_HAS_OFF       19
0058 #define PACKET_QDISC_BYPASS     20
0059 #define PACKET_ROLLOVER_STATS       21
0060 #define PACKET_FANOUT_DATA      22
0061 #define PACKET_IGNORE_OUTGOING      23
0062 
0063 #define PACKET_FANOUT_HASH      0
0064 #define PACKET_FANOUT_LB        1
0065 #define PACKET_FANOUT_CPU       2
0066 #define PACKET_FANOUT_ROLLOVER      3
0067 #define PACKET_FANOUT_RND       4
0068 #define PACKET_FANOUT_QM        5
0069 #define PACKET_FANOUT_CBPF      6
0070 #define PACKET_FANOUT_EBPF      7
0071 #define PACKET_FANOUT_FLAG_ROLLOVER 0x1000
0072 #define PACKET_FANOUT_FLAG_UNIQUEID 0x2000
0073 #define PACKET_FANOUT_FLAG_DEFRAG   0x8000
0074 
0075 struct tpacket_stats {
0076     unsigned int    tp_packets;
0077     unsigned int    tp_drops;
0078 };
0079 
0080 struct tpacket_stats_v3 {
0081     unsigned int    tp_packets;
0082     unsigned int    tp_drops;
0083     unsigned int    tp_freeze_q_cnt;
0084 };
0085 
0086 struct tpacket_rollover_stats {
0087     __aligned_u64   tp_all;
0088     __aligned_u64   tp_huge;
0089     __aligned_u64   tp_failed;
0090 };
0091 
0092 union tpacket_stats_u {
0093     struct tpacket_stats stats1;
0094     struct tpacket_stats_v3 stats3;
0095 };
0096 
0097 struct tpacket_auxdata {
0098     __u32       tp_status;
0099     __u32       tp_len;
0100     __u32       tp_snaplen;
0101     __u16       tp_mac;
0102     __u16       tp_net;
0103     __u16       tp_vlan_tci;
0104     __u16       tp_vlan_tpid;
0105 };
0106 
0107 /* Rx ring - header status */
0108 #define TP_STATUS_KERNEL              0
0109 #define TP_STATUS_USER          (1 << 0)
0110 #define TP_STATUS_COPY          (1 << 1)
0111 #define TP_STATUS_LOSING        (1 << 2)
0112 #define TP_STATUS_CSUMNOTREADY      (1 << 3)
0113 #define TP_STATUS_VLAN_VALID        (1 << 4) /* auxdata has valid tp_vlan_tci */
0114 #define TP_STATUS_BLK_TMO       (1 << 5)
0115 #define TP_STATUS_VLAN_TPID_VALID   (1 << 6) /* auxdata has valid tp_vlan_tpid */
0116 #define TP_STATUS_CSUM_VALID        (1 << 7)
0117 
0118 /* Tx ring - header status */
0119 #define TP_STATUS_AVAILABLE       0
0120 #define TP_STATUS_SEND_REQUEST  (1 << 0)
0121 #define TP_STATUS_SENDING   (1 << 1)
0122 #define TP_STATUS_WRONG_FORMAT  (1 << 2)
0123 
0124 /* Rx and Tx ring - header status */
0125 #define TP_STATUS_TS_SOFTWARE       (1 << 29)
0126 #define TP_STATUS_TS_SYS_HARDWARE   (1 << 30) /* deprecated, never set */
0127 #define TP_STATUS_TS_RAW_HARDWARE   (1U << 31)
0128 
0129 /* Rx ring - feature request bits */
0130 #define TP_FT_REQ_FILL_RXHASH   0x1
0131 
0132 struct tpacket_hdr {
0133     unsigned long   tp_status;
0134     unsigned int    tp_len;
0135     unsigned int    tp_snaplen;
0136     unsigned short  tp_mac;
0137     unsigned short  tp_net;
0138     unsigned int    tp_sec;
0139     unsigned int    tp_usec;
0140 };
0141 
0142 #define TPACKET_ALIGNMENT   16
0143 #define TPACKET_ALIGN(x)    (((x)+TPACKET_ALIGNMENT-1)&~(TPACKET_ALIGNMENT-1))
0144 #define TPACKET_HDRLEN      (TPACKET_ALIGN(sizeof(struct tpacket_hdr)) + sizeof(struct sockaddr_ll))
0145 
0146 struct tpacket2_hdr {
0147     __u32       tp_status;
0148     __u32       tp_len;
0149     __u32       tp_snaplen;
0150     __u16       tp_mac;
0151     __u16       tp_net;
0152     __u32       tp_sec;
0153     __u32       tp_nsec;
0154     __u16       tp_vlan_tci;
0155     __u16       tp_vlan_tpid;
0156     __u8        tp_padding[4];
0157 };
0158 
0159 struct tpacket_hdr_variant1 {
0160     __u32   tp_rxhash;
0161     __u32   tp_vlan_tci;
0162     __u16   tp_vlan_tpid;
0163     __u16   tp_padding;
0164 };
0165 
0166 struct tpacket3_hdr {
0167     __u32       tp_next_offset;
0168     __u32       tp_sec;
0169     __u32       tp_nsec;
0170     __u32       tp_snaplen;
0171     __u32       tp_len;
0172     __u32       tp_status;
0173     __u16       tp_mac;
0174     __u16       tp_net;
0175     /* pkt_hdr variants */
0176     union {
0177         struct tpacket_hdr_variant1 hv1;
0178     };
0179     __u8        tp_padding[8];
0180 };
0181 
0182 struct tpacket_bd_ts {
0183     unsigned int ts_sec;
0184     union {
0185         unsigned int ts_usec;
0186         unsigned int ts_nsec;
0187     };
0188 };
0189 
0190 struct tpacket_hdr_v1 {
0191     __u32   block_status;
0192     __u32   num_pkts;
0193     __u32   offset_to_first_pkt;
0194 
0195     /* Number of valid bytes (including padding)
0196      * blk_len <= tp_block_size
0197      */
0198     __u32   blk_len;
0199 
0200     /*
0201      * Quite a few uses of sequence number:
0202      * 1. Make sure cache flush etc worked.
0203      *    Well, one can argue - why not use the increasing ts below?
0204      *    But look at 2. below first.
0205      * 2. When you pass around blocks to other user space decoders,
0206      *    you can see which blk[s] is[are] outstanding etc.
0207      * 3. Validate kernel code.
0208      */
0209     __aligned_u64   seq_num;
0210 
0211     /*
0212      * ts_last_pkt:
0213      *
0214      * Case 1.  Block has 'N'(N >=1) packets and TMO'd(timed out)
0215      *      ts_last_pkt == 'time-stamp of last packet' and NOT the
0216      *      time when the timer fired and the block was closed.
0217      *      By providing the ts of the last packet we can absolutely
0218      *      guarantee that time-stamp wise, the first packet in the
0219      *      next block will never precede the last packet of the
0220      *      previous block.
0221      * Case 2.  Block has zero packets and TMO'd
0222      *      ts_last_pkt = time when the timer fired and the block
0223      *      was closed.
0224      * Case 3.  Block has 'N' packets and NO TMO.
0225      *      ts_last_pkt = time-stamp of the last pkt in the block.
0226      *
0227      * ts_first_pkt:
0228      *      Is always the time-stamp when the block was opened.
0229      *      Case a) ZERO packets
0230      *          No packets to deal with but atleast you know the
0231      *          time-interval of this block.
0232      *      Case b) Non-zero packets
0233      *          Use the ts of the first packet in the block.
0234      *
0235      */
0236     struct tpacket_bd_ts    ts_first_pkt, ts_last_pkt;
0237 };
0238 
0239 union tpacket_bd_header_u {
0240     struct tpacket_hdr_v1 bh1;
0241 };
0242 
0243 struct tpacket_block_desc {
0244     __u32 version;
0245     __u32 offset_to_priv;
0246     union tpacket_bd_header_u hdr;
0247 };
0248 
0249 #define TPACKET2_HDRLEN     (TPACKET_ALIGN(sizeof(struct tpacket2_hdr)) + sizeof(struct sockaddr_ll))
0250 #define TPACKET3_HDRLEN     (TPACKET_ALIGN(sizeof(struct tpacket3_hdr)) + sizeof(struct sockaddr_ll))
0251 
0252 enum tpacket_versions {
0253     TPACKET_V1,
0254     TPACKET_V2,
0255     TPACKET_V3
0256 };
0257 
0258 /*
0259    Frame structure:
0260 
0261    - Start. Frame must be aligned to TPACKET_ALIGNMENT=16
0262    - struct tpacket_hdr
0263    - pad to TPACKET_ALIGNMENT=16
0264    - struct sockaddr_ll
0265    - Gap, chosen so that packet data (Start+tp_net) alignes to TPACKET_ALIGNMENT=16
0266    - Start+tp_mac: [ Optional MAC header ]
0267    - Start+tp_net: Packet data, aligned to TPACKET_ALIGNMENT=16.
0268    - Pad to align to TPACKET_ALIGNMENT=16
0269  */
0270 
0271 struct tpacket_req {
0272     unsigned int    tp_block_size;  /* Minimal size of contiguous block */
0273     unsigned int    tp_block_nr;    /* Number of blocks */
0274     unsigned int    tp_frame_size;  /* Size of frame */
0275     unsigned int    tp_frame_nr;    /* Total number of frames */
0276 };
0277 
0278 struct tpacket_req3 {
0279     unsigned int    tp_block_size;  /* Minimal size of contiguous block */
0280     unsigned int    tp_block_nr;    /* Number of blocks */
0281     unsigned int    tp_frame_size;  /* Size of frame */
0282     unsigned int    tp_frame_nr;    /* Total number of frames */
0283     unsigned int    tp_retire_blk_tov; /* timeout in msecs */
0284     unsigned int    tp_sizeof_priv; /* offset to private data area */
0285     unsigned int    tp_feature_req_word;
0286 };
0287 
0288 union tpacket_req_u {
0289     struct tpacket_req  req;
0290     struct tpacket_req3 req3;
0291 };
0292 
0293 struct packet_mreq {
0294     int     mr_ifindex;
0295     unsigned short  mr_type;
0296     unsigned short  mr_alen;
0297     unsigned char   mr_address[8];
0298 };
0299 
0300 struct fanout_args {
0301 #if defined(__LITTLE_ENDIAN_BITFIELD)
0302     __u16       id;
0303     __u16       type_flags;
0304 #else
0305     __u16       type_flags;
0306     __u16       id;
0307 #endif
0308     __u32       max_num_members;
0309 };
0310 
0311 #define PACKET_MR_MULTICAST 0
0312 #define PACKET_MR_PROMISC   1
0313 #define PACKET_MR_ALLMULTI  2
0314 #define PACKET_MR_UNICAST   3
0315 
0316 #endif