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