flow.h 5.6 KB

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