flow_dissector.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #ifndef _NET_FLOW_DISSECTOR_H
  2. #define _NET_FLOW_DISSECTOR_H
  3. #include <linux/types.h>
  4. #include <linux/skbuff.h>
  5. #include <linux/in6.h>
  6. #include <uapi/linux/if_ether.h>
  7. /**
  8. * struct flow_dissector_key_control:
  9. * @thoff: Transport header offset
  10. */
  11. struct flow_dissector_key_control {
  12. u16 thoff;
  13. u16 addr_type;
  14. };
  15. /**
  16. * struct flow_dissector_key_basic:
  17. * @thoff: Transport header offset
  18. * @n_proto: Network header protocol (eg. IPv4/IPv6)
  19. * @ip_proto: Transport header protocol (eg. TCP/UDP)
  20. */
  21. struct flow_dissector_key_basic {
  22. __be16 n_proto;
  23. u8 ip_proto;
  24. u8 padding;
  25. };
  26. struct flow_dissector_key_tags {
  27. u32 vlan_id:12,
  28. flow_label:20;
  29. };
  30. struct flow_dissector_key_keyid {
  31. __be32 keyid;
  32. };
  33. /**
  34. * struct flow_dissector_key_ipv4_addrs:
  35. * @src: source ip address
  36. * @dst: destination ip address
  37. */
  38. struct flow_dissector_key_ipv4_addrs {
  39. /* (src,dst) must be grouped, in the same way than in IP header */
  40. __be32 src;
  41. __be32 dst;
  42. };
  43. /**
  44. * struct flow_dissector_key_ipv6_addrs:
  45. * @src: source ip address
  46. * @dst: destination ip address
  47. */
  48. struct flow_dissector_key_ipv6_addrs {
  49. /* (src,dst) must be grouped, in the same way than in IP header */
  50. struct in6_addr src;
  51. struct in6_addr dst;
  52. };
  53. /**
  54. * struct flow_dissector_key_tipc_addrs:
  55. * @srcnode: source node address
  56. */
  57. struct flow_dissector_key_tipc_addrs {
  58. __be32 srcnode;
  59. };
  60. /**
  61. * struct flow_dissector_key_addrs:
  62. * @v4addrs: IPv4 addresses
  63. * @v6addrs: IPv6 addresses
  64. */
  65. struct flow_dissector_key_addrs {
  66. union {
  67. struct flow_dissector_key_ipv4_addrs v4addrs;
  68. struct flow_dissector_key_ipv6_addrs v6addrs;
  69. struct flow_dissector_key_tipc_addrs tipcaddrs;
  70. };
  71. };
  72. /**
  73. * flow_dissector_key_tp_ports:
  74. * @ports: port numbers of Transport header
  75. * src: source port number
  76. * dst: destination port number
  77. */
  78. struct flow_dissector_key_ports {
  79. union {
  80. __be32 ports;
  81. struct {
  82. __be16 src;
  83. __be16 dst;
  84. };
  85. };
  86. };
  87. /**
  88. * struct flow_dissector_key_eth_addrs:
  89. * @src: source Ethernet address
  90. * @dst: destination Ethernet address
  91. */
  92. struct flow_dissector_key_eth_addrs {
  93. /* (dst,src) must be grouped, in the same way than in ETH header */
  94. unsigned char dst[ETH_ALEN];
  95. unsigned char src[ETH_ALEN];
  96. };
  97. enum flow_dissector_key_id {
  98. FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
  99. FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
  100. FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
  101. FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
  102. FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
  103. FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
  104. FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
  105. FLOW_DISSECTOR_KEY_VLANID, /* struct flow_dissector_key_flow_tags */
  106. FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
  107. FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
  108. FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
  109. FLOW_DISSECTOR_KEY_MAX,
  110. };
  111. struct flow_dissector_key {
  112. enum flow_dissector_key_id key_id;
  113. size_t offset; /* offset of struct flow_dissector_key_*
  114. in target the struct */
  115. };
  116. struct flow_dissector {
  117. unsigned int used_keys; /* each bit repesents presence of one key id */
  118. unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
  119. };
  120. void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
  121. const struct flow_dissector_key *key,
  122. unsigned int key_count);
  123. bool __skb_flow_dissect(const struct sk_buff *skb,
  124. struct flow_dissector *flow_dissector,
  125. void *target_container,
  126. void *data, __be16 proto, int nhoff, int hlen);
  127. static inline bool skb_flow_dissect(const struct sk_buff *skb,
  128. struct flow_dissector *flow_dissector,
  129. void *target_container)
  130. {
  131. return __skb_flow_dissect(skb, flow_dissector, target_container,
  132. NULL, 0, 0, 0);
  133. }
  134. struct flow_keys {
  135. struct flow_dissector_key_control control;
  136. #define FLOW_KEYS_HASH_START_FIELD basic
  137. struct flow_dissector_key_basic basic;
  138. struct flow_dissector_key_tags tags;
  139. struct flow_dissector_key_keyid keyid;
  140. struct flow_dissector_key_ports ports;
  141. struct flow_dissector_key_addrs addrs;
  142. };
  143. #define FLOW_KEYS_HASH_OFFSET \
  144. offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
  145. __be32 flow_get_u32_src(const struct flow_keys *flow);
  146. __be32 flow_get_u32_dst(const struct flow_keys *flow);
  147. extern struct flow_dissector flow_keys_dissector;
  148. extern struct flow_dissector flow_keys_buf_dissector;
  149. static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
  150. struct flow_keys *flow)
  151. {
  152. memset(flow, 0, sizeof(*flow));
  153. return __skb_flow_dissect(skb, &flow_keys_dissector, flow,
  154. NULL, 0, 0, 0);
  155. }
  156. static inline bool skb_flow_dissect_flow_keys_buf(struct flow_keys *flow,
  157. void *data, __be16 proto,
  158. int nhoff, int hlen)
  159. {
  160. memset(flow, 0, sizeof(*flow));
  161. return __skb_flow_dissect(NULL, &flow_keys_buf_dissector, flow,
  162. data, proto, nhoff, hlen);
  163. }
  164. __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
  165. void *data, int hlen_proto);
  166. static inline __be32 skb_flow_get_ports(const struct sk_buff *skb,
  167. int thoff, u8 ip_proto)
  168. {
  169. return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0);
  170. }
  171. u32 flow_hash_from_keys(struct flow_keys *keys);
  172. void __skb_get_hash(struct sk_buff *skb);
  173. u32 skb_get_poff(const struct sk_buff *skb);
  174. u32 __skb_get_poff(const struct sk_buff *skb, void *data,
  175. const struct flow_keys *keys, int hlen);
  176. /* struct flow_keys_digest:
  177. *
  178. * This structure is used to hold a digest of the full flow keys. This is a
  179. * larger "hash" of a flow to allow definitively matching specific flows where
  180. * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
  181. * that it can by used in CB of skb (see sch_choke for an example).
  182. */
  183. #define FLOW_KEYS_DIGEST_LEN 16
  184. struct flow_keys_digest {
  185. u8 data[FLOW_KEYS_DIGEST_LEN];
  186. };
  187. void make_flow_keys_digest(struct flow_keys_digest *digest,
  188. const struct flow_keys *flow);
  189. #endif