1
2
3
4
5
6
7#ifndef _NET_FLOW_H
8#define _NET_FLOW_H
9
10#include <linux/socket.h>
11#include <linux/in6.h>
12#include <linux/atomic.h>
13#include <net/flow_dissector.h>
14#include <linux/rh_kabi.h>
15#include <net/flow_dissector.h>
16
17struct flowi_tunnel {
18 __be64 tun_id;
19};
20
21struct 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
31#define FLOWI_FLAG_KNOWN_NH 0x04
32#define FLOWI_FLAG_EXTENDED4 0x40
33#define FLOWI_FLAG_EXTENDED6 0x80
34 __u32 flowic_secid;
35};
36
37union flowi_uli {
38 struct {
39 __be16 dport;
40 __be16 sport;
41 } ports;
42
43 struct {
44 __u8 type;
45 __u8 code;
46 } icmpt;
47
48 struct {
49 __le16 dport;
50 __le16 sport;
51 } dnports;
52
53 __be32 spi;
54 __be32 gre_key;
55
56 struct {
57 __u8 type;
58 } mht;
59};
60
61
62struct flowi_extended {
63 struct flowi_tunnel flowic_tun_key;
64
65 u32 rh_reserved[4];
66};
67
68struct flowi4 {
69 struct flowi_common __fl_common;
70#define flowi4_oif __fl_common.flowic_oif
71#define flowi4_iif __fl_common.flowic_iif
72#define flowi4_mark __fl_common.flowic_mark
73#define flowi4_tos __fl_common.flowic_tos
74#define flowi4_scope __fl_common.flowic_scope
75#define flowi4_proto __fl_common.flowic_proto
76#define flowi4_flags __fl_common.flowic_flags
77#define flowi4_secid __fl_common.flowic_secid
78
79
80 __be32 saddr;
81 __be32 daddr;
82
83 union flowi_uli uli;
84#define fl4_sport uli.ports.sport
85#define fl4_dport uli.ports.dport
86#define fl4_icmp_type uli.icmpt.type
87#define fl4_icmp_code uli.icmpt.code
88#define fl4_ipsec_spi uli.spi
89#define fl4_mh_type uli.mht.type
90#define fl4_gre_key uli.gre_key
91
92 RH_KABI_EXTEND(u32 rh_reserved[4])
93 RH_KABI_EXTEND(struct flowi_extended __fl_extended)
94} __attribute__((__aligned__(BITS_PER_LONG/8)));
95
96static inline void flowi4_tun_id_set(struct flowi4 *fl, __be64 tun_id)
97{
98 fl->flowi4_flags |= FLOWI_FLAG_EXTENDED4;
99 fl->__fl_extended.flowic_tun_key.tun_id = tun_id;
100}
101
102static inline void flowi4_init_output(struct flowi4 *fl4, int oif,
103 __u32 mark, __u8 tos, __u8 scope,
104 __u8 proto, __u8 flags,
105 __be32 daddr, __be32 saddr,
106 __be16 dport, __be16 sport)
107{
108 fl4->flowi4_oif = oif;
109 fl4->flowi4_iif = 0;
110 fl4->flowi4_mark = mark;
111 fl4->flowi4_tos = tos;
112 fl4->flowi4_scope = scope;
113 fl4->flowi4_proto = proto;
114 fl4->flowi4_flags = flags;
115 fl4->flowi4_secid = 0;
116 flowi4_tun_id_set(fl4, 0);
117 fl4->daddr = daddr;
118 fl4->saddr = saddr;
119 fl4->fl4_dport = dport;
120 fl4->fl4_sport = sport;
121}
122
123
124static inline void flowi4_update_output(struct flowi4 *fl4, int oif, __u8 tos,
125 __be32 daddr, __be32 saddr)
126{
127 fl4->flowi4_oif = oif;
128 fl4->flowi4_tos = tos;
129 fl4->daddr = daddr;
130 fl4->saddr = saddr;
131}
132
133
134struct flowi6 {
135 struct flowi_common __fl_common;
136#define flowi6_oif __fl_common.flowic_oif
137#define flowi6_iif __fl_common.flowic_iif
138#define flowi6_mark __fl_common.flowic_mark
139#define flowi6_tos __fl_common.flowic_tos
140#define flowi6_scope __fl_common.flowic_scope
141#define flowi6_proto __fl_common.flowic_proto
142#define flowi6_flags __fl_common.flowic_flags
143#define flowi6_secid __fl_common.flowic_secid
144 struct in6_addr daddr;
145 struct in6_addr saddr;
146 __be32 flowlabel;
147 union flowi_uli uli;
148#define fl6_sport uli.ports.sport
149#define fl6_dport uli.ports.dport
150#define fl6_icmp_type uli.icmpt.type
151#define fl6_icmp_code uli.icmpt.code
152#define fl6_ipsec_spi uli.spi
153#define fl6_mh_type uli.mht.type
154#define fl6_gre_key uli.gre_key
155
156 RH_KABI_EXTEND(u32 rh_reserved[4])
157 RH_KABI_EXTEND(struct flowi_extended __fl_extended)
158} __attribute__((__aligned__(BITS_PER_LONG/8)));
159
160static inline void flowi6_tun_id_set(struct flowi6 *fl, __be64 tun_id)
161{
162 fl->flowi6_flags |= FLOWI_FLAG_EXTENDED6;
163 fl->__fl_extended.flowic_tun_key.tun_id = tun_id;
164}
165
166struct flowidn {
167 struct flowi_common __fl_common;
168#define flowidn_oif __fl_common.flowic_oif
169#define flowidn_iif __fl_common.flowic_iif
170#define flowidn_mark __fl_common.flowic_mark
171#define flowidn_scope __fl_common.flowic_scope
172#define flowidn_proto __fl_common.flowic_proto
173#define flowidn_flags __fl_common.flowic_flags
174 __le16 daddr;
175 __le16 saddr;
176 union flowi_uli uli;
177#define fld_sport uli.ports.sport
178#define fld_dport uli.ports.dport
179} __attribute__((__aligned__(BITS_PER_LONG/8)));
180
181struct flowi {
182 union {
183 struct flowi_common __fl_common;
184 struct flowi4 ip4;
185 struct flowi6 ip6;
186 struct flowidn dn;
187 } u;
188#define flowi_oif u.__fl_common.flowic_oif
189#define flowi_iif u.__fl_common.flowic_iif
190#define flowi_mark u.__fl_common.flowic_mark
191#define flowi_tos u.__fl_common.flowic_tos
192#define flowi_scope u.__fl_common.flowic_scope
193#define flowi_proto u.__fl_common.flowic_proto
194#define flowi_flags u.__fl_common.flowic_flags
195#define flowi_secid u.__fl_common.flowic_secid
196} __attribute__((__aligned__(BITS_PER_LONG/8)));
197
198static inline __be64 flowi_tun_id_get(struct flowi *fl)
199{
200 if (fl->flowi_flags & FLOWI_FLAG_EXTENDED4)
201 return fl->u.ip4.__fl_extended.flowic_tun_key.tun_id;
202 else if (fl->flowi_flags & FLOWI_FLAG_EXTENDED6)
203 return fl->u.ip6.__fl_extended.flowic_tun_key.tun_id;
204 else
205 return 0;
206}
207
208static inline struct flowi *flowi4_to_flowi(struct flowi4 *fl4)
209{
210 return container_of(fl4, struct flowi, u.ip4);
211}
212
213static inline struct flowi *flowi6_to_flowi(struct flowi6 *fl6)
214{
215 return container_of(fl6, struct flowi, u.ip6);
216}
217
218static inline struct flowi *flowidn_to_flowi(struct flowidn *fldn)
219{
220 return container_of(fldn, struct flowi, u.dn);
221}
222
223typedef unsigned long flow_compare_t;
224
225static inline size_t flow_key_size(u16 family)
226{
227 switch (family) {
228 case AF_INET:
229 BUILD_BUG_ON(sizeof(struct flowi4) % sizeof(flow_compare_t));
230 return sizeof(struct flowi4) / sizeof(flow_compare_t);
231 case AF_INET6:
232 BUILD_BUG_ON(sizeof(struct flowi6) % sizeof(flow_compare_t));
233 return sizeof(struct flowi6) / sizeof(flow_compare_t);
234 case AF_DECnet:
235 BUILD_BUG_ON(sizeof(struct flowidn) % sizeof(flow_compare_t));
236 return sizeof(struct flowidn) / sizeof(flow_compare_t);
237 }
238 return 0;
239}
240
241#define FLOW_DIR_IN 0
242#define FLOW_DIR_OUT 1
243#define FLOW_DIR_FWD 2
244
245struct net;
246struct sock;
247struct flow_cache_ops;
248
249struct flow_cache_object {
250 const struct flow_cache_ops *ops;
251};
252
253struct flow_cache_ops {
254 struct flow_cache_object *(*get)(struct flow_cache_object *);
255 int (*check)(struct flow_cache_object *);
256 void (*delete)(struct flow_cache_object *);
257};
258
259typedef struct flow_cache_object *(*flow_resolve_t)(
260 struct net *net, const struct flowi *key, u16 family,
261 u8 dir, struct flow_cache_object *oldobj, void *ctx);
262
263struct flow_cache_object *flow_cache_lookup(struct net *net,
264 const struct flowi *key, u16 family,
265 u8 dir, flow_resolve_t resolver,
266 void *ctx);
267int flow_cache_init(struct net *net);
268void flow_cache_fini(struct net *net);
269
270void flow_cache_flush(struct net *net);
271void flow_cache_flush_deferred(struct net *net);
272extern atomic_t flow_cache_genid;
273
274__u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys);
275
276static inline __u32 get_hash_from_flowi6(const struct flowi6 *fl6)
277{
278 struct flow_keys keys;
279
280 return __get_hash_from_flowi6(fl6, &keys);
281}
282
283__u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys);
284
285static inline __u32 get_hash_from_flowi4(const struct flowi4 *fl4)
286{
287 struct flow_keys keys;
288
289 return __get_hash_from_flowi4(fl4, &keys);
290}
291
292#endif
293