xref: /openbmc/linux/include/net/flow.h (revision 31cb1fb4)
1 /*
2  *
3  *	Generic internet FLOW.
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/uidgid.h>
15 
16 /*
17  * ifindex generation is per-net namespace, and loopback is
18  * always the 1st device in ns (see net_dev_init), thus any
19  * loopback device should get ifindex 1
20  */
21 
22 #define LOOPBACK_IFINDEX	1
23 
24 struct flowi_tunnel {
25 	__be64			tun_id;
26 };
27 
28 struct flowi_common {
29 	int	flowic_oif;
30 	int	flowic_iif;
31 	__u32	flowic_mark;
32 	__u8	flowic_tos;
33 	__u8	flowic_scope;
34 	__u8	flowic_proto;
35 	__u8	flowic_flags;
36 #define FLOWI_FLAG_ANYSRC		0x01
37 #define FLOWI_FLAG_KNOWN_NH		0x02
38 #define FLOWI_FLAG_SKIP_NH_OIF		0x04
39 	__u32	flowic_secid;
40 	struct flowi_tunnel flowic_tun_key;
41 	kuid_t  flowic_uid;
42 };
43 
44 union flowi_uli {
45 	struct {
46 		__be16	dport;
47 		__be16	sport;
48 	} ports;
49 
50 	struct {
51 		__u8	type;
52 		__u8	code;
53 	} icmpt;
54 
55 	struct {
56 		__le16	dport;
57 		__le16	sport;
58 	} dnports;
59 
60 	__be32		spi;
61 	__be32		gre_key;
62 
63 	struct {
64 		__u8	type;
65 	} mht;
66 };
67 
68 struct 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 #define flowi4_tun_key		__fl_common.flowic_tun_key
79 #define flowi4_uid		__fl_common.flowic_uid
80 
81 	/* (saddr,daddr) must be grouped, same order as in IP header */
82 	__be32			saddr;
83 	__be32			daddr;
84 
85 	union flowi_uli		uli;
86 #define fl4_sport		uli.ports.sport
87 #define fl4_dport		uli.ports.dport
88 #define fl4_icmp_type		uli.icmpt.type
89 #define fl4_icmp_code		uli.icmpt.code
90 #define fl4_ipsec_spi		uli.spi
91 #define fl4_mh_type		uli.mht.type
92 #define fl4_gre_key		uli.gre_key
93 } __attribute__((__aligned__(BITS_PER_LONG/8)));
94 
95 static inline void flowi4_init_output(struct flowi4 *fl4, int oif,
96 				      __u32 mark, __u8 tos, __u8 scope,
97 				      __u8 proto, __u8 flags,
98 				      __be32 daddr, __be32 saddr,
99 				      __be16 dport, __be16 sport,
100 				      kuid_t uid)
101 {
102 	fl4->flowi4_oif = oif;
103 	fl4->flowi4_iif = LOOPBACK_IFINDEX;
104 	fl4->flowi4_mark = mark;
105 	fl4->flowi4_tos = tos;
106 	fl4->flowi4_scope = scope;
107 	fl4->flowi4_proto = proto;
108 	fl4->flowi4_flags = flags;
109 	fl4->flowi4_secid = 0;
110 	fl4->flowi4_tun_key.tun_id = 0;
111 	fl4->flowi4_uid = uid;
112 	fl4->daddr = daddr;
113 	fl4->saddr = saddr;
114 	fl4->fl4_dport = dport;
115 	fl4->fl4_sport = sport;
116 }
117 
118 /* Reset some input parameters after previous lookup */
119 static inline void flowi4_update_output(struct flowi4 *fl4, int oif, __u8 tos,
120 					__be32 daddr, __be32 saddr)
121 {
122 	fl4->flowi4_oif = oif;
123 	fl4->flowi4_tos = tos;
124 	fl4->daddr = daddr;
125 	fl4->saddr = saddr;
126 }
127 
128 
129 struct flowi6 {
130 	struct flowi_common	__fl_common;
131 #define flowi6_oif		__fl_common.flowic_oif
132 #define flowi6_iif		__fl_common.flowic_iif
133 #define flowi6_mark		__fl_common.flowic_mark
134 #define flowi6_scope		__fl_common.flowic_scope
135 #define flowi6_proto		__fl_common.flowic_proto
136 #define flowi6_flags		__fl_common.flowic_flags
137 #define flowi6_secid		__fl_common.flowic_secid
138 #define flowi6_tun_key		__fl_common.flowic_tun_key
139 #define flowi6_uid		__fl_common.flowic_uid
140 	struct in6_addr		daddr;
141 	struct in6_addr		saddr;
142 	/* Note: flowi6_tos is encoded in flowlabel, too. */
143 	__be32			flowlabel;
144 	union flowi_uli		uli;
145 #define fl6_sport		uli.ports.sport
146 #define fl6_dport		uli.ports.dport
147 #define fl6_icmp_type		uli.icmpt.type
148 #define fl6_icmp_code		uli.icmpt.code
149 #define fl6_ipsec_spi		uli.spi
150 #define fl6_mh_type		uli.mht.type
151 #define fl6_gre_key		uli.gre_key
152 } __attribute__((__aligned__(BITS_PER_LONG/8)));
153 
154 struct flowidn {
155 	struct flowi_common	__fl_common;
156 #define flowidn_oif		__fl_common.flowic_oif
157 #define flowidn_iif		__fl_common.flowic_iif
158 #define flowidn_mark		__fl_common.flowic_mark
159 #define flowidn_scope		__fl_common.flowic_scope
160 #define flowidn_proto		__fl_common.flowic_proto
161 #define flowidn_flags		__fl_common.flowic_flags
162 	__le16			daddr;
163 	__le16			saddr;
164 	union flowi_uli		uli;
165 #define fld_sport		uli.ports.sport
166 #define fld_dport		uli.ports.dport
167 } __attribute__((__aligned__(BITS_PER_LONG/8)));
168 
169 struct flowi {
170 	union {
171 		struct flowi_common	__fl_common;
172 		struct flowi4		ip4;
173 		struct flowi6		ip6;
174 		struct flowidn		dn;
175 	} u;
176 #define flowi_oif	u.__fl_common.flowic_oif
177 #define flowi_iif	u.__fl_common.flowic_iif
178 #define flowi_mark	u.__fl_common.flowic_mark
179 #define flowi_tos	u.__fl_common.flowic_tos
180 #define flowi_scope	u.__fl_common.flowic_scope
181 #define flowi_proto	u.__fl_common.flowic_proto
182 #define flowi_flags	u.__fl_common.flowic_flags
183 #define flowi_secid	u.__fl_common.flowic_secid
184 #define flowi_tun_key	u.__fl_common.flowic_tun_key
185 #define flowi_uid	u.__fl_common.flowic_uid
186 } __attribute__((__aligned__(BITS_PER_LONG/8)));
187 
188 static inline struct flowi *flowi4_to_flowi(struct flowi4 *fl4)
189 {
190 	return container_of(fl4, struct flowi, u.ip4);
191 }
192 
193 static inline struct flowi *flowi6_to_flowi(struct flowi6 *fl6)
194 {
195 	return container_of(fl6, struct flowi, u.ip6);
196 }
197 
198 static inline struct flowi *flowidn_to_flowi(struct flowidn *fldn)
199 {
200 	return container_of(fldn, struct flowi, u.dn);
201 }
202 
203 typedef unsigned long flow_compare_t;
204 
205 static inline unsigned int flow_key_size(u16 family)
206 {
207 	switch (family) {
208 	case AF_INET:
209 		BUILD_BUG_ON(sizeof(struct flowi4) % sizeof(flow_compare_t));
210 		return sizeof(struct flowi4) / sizeof(flow_compare_t);
211 	case AF_INET6:
212 		BUILD_BUG_ON(sizeof(struct flowi6) % sizeof(flow_compare_t));
213 		return sizeof(struct flowi6) / sizeof(flow_compare_t);
214 	case AF_DECnet:
215 		BUILD_BUG_ON(sizeof(struct flowidn) % sizeof(flow_compare_t));
216 		return sizeof(struct flowidn) / sizeof(flow_compare_t);
217 	}
218 	return 0;
219 }
220 
221 #define FLOW_DIR_IN	0
222 #define FLOW_DIR_OUT	1
223 #define FLOW_DIR_FWD	2
224 
225 struct net;
226 struct sock;
227 struct flow_cache_ops;
228 
229 struct flow_cache_object {
230 	const struct flow_cache_ops *ops;
231 };
232 
233 struct flow_cache_ops {
234 	struct flow_cache_object *(*get)(struct flow_cache_object *);
235 	int (*check)(struct flow_cache_object *);
236 	void (*delete)(struct flow_cache_object *);
237 };
238 
239 typedef struct flow_cache_object *(*flow_resolve_t)(
240 		struct net *net, const struct flowi *key, u16 family,
241 		u8 dir, struct flow_cache_object *oldobj, void *ctx);
242 
243 struct flow_cache_object *flow_cache_lookup(struct net *net,
244 					    const struct flowi *key, u16 family,
245 					    u8 dir, flow_resolve_t resolver,
246 					    void *ctx);
247 int flow_cache_init(struct net *net);
248 void flow_cache_fini(struct net *net);
249 void flow_cache_hp_init(void);
250 
251 void flow_cache_flush(struct net *net);
252 void flow_cache_flush_deferred(struct net *net);
253 extern atomic_t flow_cache_genid;
254 
255 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys);
256 
257 static inline __u32 get_hash_from_flowi6(const struct flowi6 *fl6)
258 {
259 	struct flow_keys keys;
260 
261 	return __get_hash_from_flowi6(fl6, &keys);
262 }
263 
264 __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys);
265 
266 static inline __u32 get_hash_from_flowi4(const struct flowi4 *fl4)
267 {
268 	struct flow_keys keys;
269 
270 	return __get_hash_from_flowi4(fl4, &keys);
271 }
272 
273 #endif
274