flow.h 6.0 KB

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