flow.h 6.2 KB

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