blob: da7743eea600033631d49baee59632de7d792e7a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
3 * Generic internet FLOW.
4 *
5 */
6
7#ifndef _NET_FLOW_H
8#define _NET_FLOW_H
9
dpwardaa1c3662011-09-05 16:47:24 +000010#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/in6.h>
Arun Sharma600634972011-07-26 16:09:06 -070012#include <linux/atomic.h>
Lorenzo Colittid403cf42014-03-31 16:23:51 +090013#include <linux/uidgid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Cong Wang6a662712014-04-15 16:25:34 -070015/*
16 * ifindex generation is per-net namespace, and loopback is
17 * always the 1st device in ns (see net_dev_init), thus any
18 * loopback device should get ifindex 1
19 */
20
21#define LOOPBACK_IFINDEX 1
22
David S. Miller806566c2011-03-11 18:22:00 -050023struct flowi_common {
24 int flowic_oif;
25 int flowic_iif;
26 __u32 flowic_mark;
27 __u8 flowic_tos;
28 __u8 flowic_scope;
29 __u8 flowic_proto;
30 __u8 flowic_flags;
David S. Millerfbef0a42011-03-11 15:55:37 -050031#define FLOWI_FLAG_ANYSRC 0x01
Steffen Klassert0e0d44a2013-08-28 08:04:14 +020032#define FLOWI_FLAG_KNOWN_NH 0x02
David S. Miller806566c2011-03-11 18:22:00 -050033 __u32 flowic_secid;
Lorenzo Colittid403cf42014-03-31 16:23:51 +090034 kuid_t flowic_uid;
David S. Miller806566c2011-03-11 18:22:00 -050035};
36
David S. Miller08704bc2011-03-11 18:36:42 -050037union flowi_uli {
38 struct {
David S. Miller08704bc2011-03-11 18:36:42 -050039 __be16 dport;
David S. Miller9b12c752011-03-31 18:03:35 -070040 __be16 sport;
David S. Miller08704bc2011-03-11 18:36:42 -050041 } ports;
42
43 struct {
44 __u8 type;
45 __u8 code;
46 } icmpt;
47
48 struct {
David S. Miller08704bc2011-03-11 18:36:42 -050049 __le16 dport;
David S. Miller9b12c752011-03-31 18:03:35 -070050 __le16 sport;
David S. Miller08704bc2011-03-11 18:36:42 -050051 } dnports;
52
53 __be32 spi;
54 __be32 gre_key;
55
56 struct {
57 __u8 type;
58 } mht;
59};
60
David S. Miller56bb8052011-03-12 00:44:35 -050061struct flowi4 {
David S. Miller806566c2011-03-11 18:22:00 -050062 struct flowi_common __fl_common;
David S. Miller22bd5b92011-03-11 19:54:08 -050063#define flowi4_oif __fl_common.flowic_oif
64#define flowi4_iif __fl_common.flowic_iif
65#define flowi4_mark __fl_common.flowic_mark
66#define flowi4_tos __fl_common.flowic_tos
67#define flowi4_scope __fl_common.flowic_scope
68#define flowi4_proto __fl_common.flowic_proto
69#define flowi4_flags __fl_common.flowic_flags
70#define flowi4_secid __fl_common.flowic_secid
Lorenzo Colittid403cf42014-03-31 16:23:51 +090071#define flowi4_uid __fl_common.flowic_uid
Eric Dumazet84f93072011-11-30 19:00:53 +000072
73 /* (saddr,daddr) must be grouped, same order as in IP header */
David S. Miller56bb8052011-03-12 00:44:35 -050074 __be32 saddr;
Eric Dumazet84f93072011-11-30 19:00:53 +000075 __be32 daddr;
76
David S. Miller56bb8052011-03-12 00:44:35 -050077 union flowi_uli uli;
David S. Miller9cce96d2011-03-12 03:00:33 -050078#define fl4_sport uli.ports.sport
79#define fl4_dport uli.ports.dport
80#define fl4_icmp_type uli.icmpt.type
81#define fl4_icmp_code uli.icmpt.code
82#define fl4_ipsec_spi uli.spi
83#define fl4_mh_type uli.mht.type
84#define fl4_gre_key uli.gre_key
David Ward728871b2011-09-05 16:47:23 +000085} __attribute__((__aligned__(BITS_PER_LONG/8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
David S. Miller83229aa2011-03-31 04:52:14 -070087static inline void flowi4_init_output(struct flowi4 *fl4, int oif,
88 __u32 mark, __u8 tos, __u8 scope,
89 __u8 proto, __u8 flags,
90 __be32 daddr, __be32 saddr,
Lorenzo Colittid403cf42014-03-31 16:23:51 +090091 __be16 dport, __be16 sport,
92 kuid_t uid)
David S. Miller83229aa2011-03-31 04:52:14 -070093{
94 fl4->flowi4_oif = oif;
Cong Wang6a662712014-04-15 16:25:34 -070095 fl4->flowi4_iif = LOOPBACK_IFINDEX;
David S. Miller83229aa2011-03-31 04:52:14 -070096 fl4->flowi4_mark = mark;
97 fl4->flowi4_tos = tos;
98 fl4->flowi4_scope = scope;
99 fl4->flowi4_proto = proto;
100 fl4->flowi4_flags = flags;
101 fl4->flowi4_secid = 0;
Lorenzo Colittid403cf42014-03-31 16:23:51 +0900102 fl4->flowi4_uid = uid;
David S. Miller83229aa2011-03-31 04:52:14 -0700103 fl4->daddr = daddr;
104 fl4->saddr = saddr;
David S. Miller83229aa2011-03-31 04:52:14 -0700105 fl4->fl4_dport = dport;
David S. Miller9b12c752011-03-31 18:03:35 -0700106 fl4->fl4_sport = sport;
David S. Miller83229aa2011-03-31 04:52:14 -0700107}
Julian Anastasove6b45242012-02-04 13:04:46 +0000108
109/* Reset some input parameters after previous lookup */
110static inline void flowi4_update_output(struct flowi4 *fl4, int oif, __u8 tos,
111 __be32 daddr, __be32 saddr)
112{
113 fl4->flowi4_oif = oif;
114 fl4->flowi4_tos = tos;
115 fl4->daddr = daddr;
116 fl4->saddr = saddr;
117}
David S. Miller83229aa2011-03-31 04:52:14 -0700118
119
David S. Miller56bb8052011-03-12 00:44:35 -0500120struct flowi6 {
121 struct flowi_common __fl_common;
David S. Miller20326562011-03-12 02:30:50 -0500122#define flowi6_oif __fl_common.flowic_oif
123#define flowi6_iif __fl_common.flowic_iif
124#define flowi6_mark __fl_common.flowic_mark
125#define flowi6_tos __fl_common.flowic_tos
126#define flowi6_scope __fl_common.flowic_scope
127#define flowi6_proto __fl_common.flowic_proto
128#define flowi6_flags __fl_common.flowic_flags
129#define flowi6_secid __fl_common.flowic_secid
Lorenzo Colittid403cf42014-03-31 16:23:51 +0900130#define flowi6_uid __fl_common.flowic_uid
David S. Miller56bb8052011-03-12 00:44:35 -0500131 struct in6_addr daddr;
132 struct in6_addr saddr;
133 __be32 flowlabel;
134 union flowi_uli uli;
David S. Miller1958b852011-03-12 16:36:19 -0500135#define fl6_sport uli.ports.sport
136#define fl6_dport uli.ports.dport
137#define fl6_icmp_type uli.icmpt.type
138#define fl6_icmp_code uli.icmpt.code
139#define fl6_ipsec_spi uli.spi
140#define fl6_mh_type uli.mht.type
141#define fl6_gre_key uli.gre_key
David Ward728871b2011-09-05 16:47:23 +0000142} __attribute__((__aligned__(BITS_PER_LONG/8)));
David S. Miller56bb8052011-03-12 00:44:35 -0500143
144struct flowidn {
145 struct flowi_common __fl_common;
David S. Millerbef55ae2011-03-12 17:17:10 -0500146#define flowidn_oif __fl_common.flowic_oif
147#define flowidn_iif __fl_common.flowic_iif
148#define flowidn_mark __fl_common.flowic_mark
149#define flowidn_scope __fl_common.flowic_scope
150#define flowidn_proto __fl_common.flowic_proto
151#define flowidn_flags __fl_common.flowic_flags
David S. Miller56bb8052011-03-12 00:44:35 -0500152 __le16 daddr;
153 __le16 saddr;
154 union flowi_uli uli;
David S. Millerbef55ae2011-03-12 17:17:10 -0500155#define fld_sport uli.ports.sport
156#define fld_dport uli.ports.dport
David Ward728871b2011-09-05 16:47:23 +0000157} __attribute__((__aligned__(BITS_PER_LONG/8)));
David S. Miller56bb8052011-03-12 00:44:35 -0500158
159struct flowi {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 union {
David S. Miller56bb8052011-03-12 00:44:35 -0500161 struct flowi_common __fl_common;
162 struct flowi4 ip4;
163 struct flowi6 ip6;
164 struct flowidn dn;
165 } u;
166#define flowi_oif u.__fl_common.flowic_oif
167#define flowi_iif u.__fl_common.flowic_iif
168#define flowi_mark u.__fl_common.flowic_mark
169#define flowi_tos u.__fl_common.flowic_tos
170#define flowi_scope u.__fl_common.flowic_scope
171#define flowi_proto u.__fl_common.flowic_proto
172#define flowi_flags u.__fl_common.flowic_flags
173#define flowi_secid u.__fl_common.flowic_secid
Lorenzo Colittid403cf42014-03-31 16:23:51 +0900174#define flowi_uid u.__fl_common.flowic_uid
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175} __attribute__((__aligned__(BITS_PER_LONG/8)));
176
David S. Miller59b1a942011-03-11 19:23:02 -0500177static inline struct flowi *flowi4_to_flowi(struct flowi4 *fl4)
178{
179 return container_of(fl4, struct flowi, u.ip4);
180}
181
182static inline struct flowi *flowi6_to_flowi(struct flowi6 *fl6)
183{
184 return container_of(fl6, struct flowi, u.ip6);
185}
186
187static inline struct flowi *flowidn_to_flowi(struct flowidn *fldn)
188{
189 return container_of(fldn, struct flowi, u.dn);
190}
191
dpwardaa1c3662011-09-05 16:47:24 +0000192typedef unsigned long flow_compare_t;
193
194static inline size_t flow_key_size(u16 family)
195{
196 switch (family) {
197 case AF_INET:
198 BUILD_BUG_ON(sizeof(struct flowi4) % sizeof(flow_compare_t));
199 return sizeof(struct flowi4) / sizeof(flow_compare_t);
200 case AF_INET6:
201 BUILD_BUG_ON(sizeof(struct flowi6) % sizeof(flow_compare_t));
202 return sizeof(struct flowi6) / sizeof(flow_compare_t);
203 case AF_DECnet:
204 BUILD_BUG_ON(sizeof(struct flowidn) % sizeof(flow_compare_t));
205 return sizeof(struct flowidn) / sizeof(flow_compare_t);
206 }
207 return 0;
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#define FLOW_DIR_IN 0
211#define FLOW_DIR_OUT 1
212#define FLOW_DIR_FWD 2
213
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800214struct net;
Trent Jaegerdf718372005-12-13 23:12:27 -0800215struct sock;
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000216struct flow_cache_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000218struct flow_cache_object {
219 const struct flow_cache_ops *ops;
220};
221
222struct flow_cache_ops {
223 struct flow_cache_object *(*get)(struct flow_cache_object *);
224 int (*check)(struct flow_cache_object *);
225 void (*delete)(struct flow_cache_object *);
226};
227
228typedef struct flow_cache_object *(*flow_resolve_t)(
David S. Millerdee9f4b2011-02-22 18:44:31 -0800229 struct net *net, const struct flowi *key, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000230 u8 dir, struct flow_cache_object *oldobj, void *ctx);
231
Joe Perches47873422013-09-20 11:23:24 -0700232struct flow_cache_object *flow_cache_lookup(struct net *net,
233 const struct flowi *key, u16 family,
234 u8 dir, flow_resolve_t resolver,
235 void *ctx);
Fan Duca925cf2014-01-18 09:55:27 +0800236int flow_cache_init(struct net *net);
Steffen Klassert4a93f502014-03-12 09:43:17 +0100237void flow_cache_fini(struct net *net);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000238
Fan Duca925cf2014-01-18 09:55:27 +0800239void flow_cache_flush(struct net *net);
240void flow_cache_flush_deferred(struct net *net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241extern atomic_t flow_cache_genid;
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#endif