flow.h 6.1 KB

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