flow.h 6.8 KB

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