flow.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. *
  4. * Generic internet FLOW.
  5. *
  6. */
  7. #ifndef _NET_FLOW_H
  8. #define _NET_FLOW_H
  9. #include <linux/socket.h>
  10. #include <linux/in6.h>
  11. #include <linux/atomic.h>
  12. #include <net/flow_dissector.h>
  13. #include <linux/uidgid.h>
  14. /*
  15. * ifindex generation is per-net namespace, and loopback is
  16. * always the 1st device in ns (see net_dev_init), thus any
  17. * loopback device should get ifindex 1
  18. */
  19. #define LOOPBACK_IFINDEX 1
  20. struct flowi_tunnel {
  21. __be64 tun_id;
  22. };
  23. struct flowi_common {
  24. int flowic_oif;
  25. int flowic_iif;
  26. __u32 flowic_mark;
  27. __u8 flowic_tos;
  28. __u8 flowic_scope;
  29. __u8 flowic_proto;
  30. __u8 flowic_flags;
  31. #define FLOWI_FLAG_ANYSRC 0x01
  32. #define FLOWI_FLAG_KNOWN_NH 0x02
  33. #define FLOWI_FLAG_SKIP_NH_OIF 0x04
  34. __u32 flowic_secid;
  35. struct flowi_tunnel flowic_tun_key;
  36. kuid_t flowic_uid;
  37. };
  38. union flowi_uli {
  39. struct {
  40. __be16 dport;
  41. __be16 sport;
  42. } ports;
  43. struct {
  44. __u8 type;
  45. __u8 code;
  46. } icmpt;
  47. struct {
  48. __le16 dport;
  49. __le16 sport;
  50. } dnports;
  51. __be32 spi;
  52. __be32 gre_key;
  53. struct {
  54. __u8 type;
  55. } mht;
  56. };
  57. struct flowi4 {
  58. struct flowi_common __fl_common;
  59. #define flowi4_oif __fl_common.flowic_oif
  60. #define flowi4_iif __fl_common.flowic_iif
  61. #define flowi4_mark __fl_common.flowic_mark
  62. #define flowi4_tos __fl_common.flowic_tos
  63. #define flowi4_scope __fl_common.flowic_scope
  64. #define flowi4_proto __fl_common.flowic_proto
  65. #define flowi4_flags __fl_common.flowic_flags
  66. #define flowi4_secid __fl_common.flowic_secid
  67. #define flowi4_tun_key __fl_common.flowic_tun_key
  68. #define flowi4_uid __fl_common.flowic_uid
  69. /* (saddr,daddr) must be grouped, same order as in IP header */
  70. __be32 saddr;
  71. __be32 daddr;
  72. union flowi_uli uli;
  73. #define fl4_sport uli.ports.sport
  74. #define fl4_dport uli.ports.dport
  75. #define fl4_icmp_type uli.icmpt.type
  76. #define fl4_icmp_code uli.icmpt.code
  77. #define fl4_ipsec_spi uli.spi
  78. #define fl4_mh_type uli.mht.type
  79. #define fl4_gre_key uli.gre_key
  80. } __attribute__((__aligned__(BITS_PER_LONG/8)));
  81. static inline void flowi4_init_output(struct flowi4 *fl4, int oif,
  82. __u32 mark, __u8 tos, __u8 scope,
  83. __u8 proto, __u8 flags,
  84. __be32 daddr, __be32 saddr,
  85. __be16 dport, __be16 sport,
  86. kuid_t uid)
  87. {
  88. fl4->flowi4_oif = oif;
  89. fl4->flowi4_iif = LOOPBACK_IFINDEX;
  90. fl4->flowi4_mark = mark;
  91. fl4->flowi4_tos = tos;
  92. fl4->flowi4_scope = scope;
  93. fl4->flowi4_proto = proto;
  94. fl4->flowi4_flags = flags;
  95. fl4->flowi4_secid = 0;
  96. fl4->flowi4_tun_key.tun_id = 0;
  97. fl4->flowi4_uid = uid;
  98. fl4->daddr = daddr;
  99. fl4->saddr = saddr;
  100. fl4->fl4_dport = dport;
  101. fl4->fl4_sport = sport;
  102. }
  103. /* Reset some input parameters after previous lookup */
  104. static inline void flowi4_update_output(struct flowi4 *fl4, int oif, __u8 tos,
  105. __be32 daddr, __be32 saddr)
  106. {
  107. fl4->flowi4_oif = oif;
  108. fl4->flowi4_tos = tos;
  109. fl4->daddr = daddr;
  110. fl4->saddr = saddr;
  111. }
  112. struct flowi6 {
  113. struct flowi_common __fl_common;
  114. #define flowi6_oif __fl_common.flowic_oif
  115. #define flowi6_iif __fl_common.flowic_iif
  116. #define flowi6_mark __fl_common.flowic_mark
  117. #define flowi6_scope __fl_common.flowic_scope
  118. #define flowi6_proto __fl_common.flowic_proto
  119. #define flowi6_flags __fl_common.flowic_flags
  120. #define flowi6_secid __fl_common.flowic_secid
  121. #define flowi6_tun_key __fl_common.flowic_tun_key
  122. #define flowi6_uid __fl_common.flowic_uid
  123. struct in6_addr daddr;
  124. struct in6_addr saddr;
  125. /* Note: flowi6_tos is encoded in flowlabel, too. */
  126. __be32 flowlabel;
  127. union flowi_uli uli;
  128. #define fl6_sport uli.ports.sport
  129. #define fl6_dport uli.ports.dport
  130. #define fl6_icmp_type uli.icmpt.type
  131. #define fl6_icmp_code uli.icmpt.code
  132. #define fl6_ipsec_spi uli.spi
  133. #define fl6_mh_type uli.mht.type
  134. #define fl6_gre_key uli.gre_key
  135. __u32 mp_hash;
  136. } __attribute__((__aligned__(BITS_PER_LONG/8)));
  137. struct flowidn {
  138. struct flowi_common __fl_common;
  139. #define flowidn_oif __fl_common.flowic_oif
  140. #define flowidn_iif __fl_common.flowic_iif
  141. #define flowidn_mark __fl_common.flowic_mark
  142. #define flowidn_scope __fl_common.flowic_scope
  143. #define flowidn_proto __fl_common.flowic_proto
  144. #define flowidn_flags __fl_common.flowic_flags
  145. __le16 daddr;
  146. __le16 saddr;
  147. union flowi_uli uli;
  148. #define fld_sport uli.ports.sport
  149. #define fld_dport uli.ports.dport
  150. } __attribute__((__aligned__(BITS_PER_LONG/8)));
  151. struct flowi {
  152. union {
  153. struct flowi_common __fl_common;
  154. struct flowi4 ip4;
  155. struct flowi6 ip6;
  156. struct flowidn dn;
  157. } u;
  158. #define flowi_oif u.__fl_common.flowic_oif
  159. #define flowi_iif u.__fl_common.flowic_iif
  160. #define flowi_mark u.__fl_common.flowic_mark
  161. #define flowi_tos u.__fl_common.flowic_tos
  162. #define flowi_scope u.__fl_common.flowic_scope
  163. #define flowi_proto u.__fl_common.flowic_proto
  164. #define flowi_flags u.__fl_common.flowic_flags
  165. #define flowi_secid u.__fl_common.flowic_secid
  166. #define flowi_tun_key u.__fl_common.flowic_tun_key
  167. #define flowi_uid u.__fl_common.flowic_uid
  168. } __attribute__((__aligned__(BITS_PER_LONG/8)));
  169. static inline struct flowi *flowi4_to_flowi(struct flowi4 *fl4)
  170. {
  171. return container_of(fl4, struct flowi, u.ip4);
  172. }
  173. static inline struct flowi *flowi6_to_flowi(struct flowi6 *fl6)
  174. {
  175. return container_of(fl6, struct flowi, u.ip6);
  176. }
  177. static inline struct flowi *flowidn_to_flowi(struct flowidn *fldn)
  178. {
  179. return container_of(fldn, struct flowi, u.dn);
  180. }
  181. typedef unsigned long flow_compare_t;
  182. static inline unsigned int flow_key_size(u16 family)
  183. {
  184. switch (family) {
  185. case AF_INET:
  186. BUILD_BUG_ON(sizeof(struct flowi4) % sizeof(flow_compare_t));
  187. return sizeof(struct flowi4) / sizeof(flow_compare_t);
  188. case AF_INET6:
  189. BUILD_BUG_ON(sizeof(struct flowi6) % sizeof(flow_compare_t));
  190. return sizeof(struct flowi6) / sizeof(flow_compare_t);
  191. case AF_DECnet:
  192. BUILD_BUG_ON(sizeof(struct flowidn) % sizeof(flow_compare_t));
  193. return sizeof(struct flowidn) / sizeof(flow_compare_t);
  194. }
  195. return 0;
  196. }
  197. __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys);
  198. #endif