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 <linux/uidgid.h>
14
15 /*
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
23 struct 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;
31 #define FLOWI_FLAG_ANYSRC 0x01
32 #define FLOWI_FLAG_KNOWN_NH 0x02
33 __u32 flowic_secid;
34 kuid_t flowic_uid;
35 };
36
37 union 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 struct flowi4 {
62 struct flowi_common __fl_common;
63 #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
71 #define flowi4_uid __fl_common.flowic_uid
72
73 /* (saddr,daddr) must be grouped, same order as in IP header */
74 __be32 saddr;
75 __be32 daddr;
76
77 union flowi_uli uli;
78 #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
85 } __attribute__((__aligned__(BITS_PER_LONG/8)));
86
flowi4_init_output(struct flowi4 * fl4,int oif,__u32 mark,__u8 tos,__u8 scope,__u8 proto,__u8 flags,__be32 daddr,__be32 saddr,__be16 dport,__be16 sport,kuid_t uid)87 static 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,
91 __be16 dport, __be16 sport,
92 kuid_t uid)
93 {
94 fl4->flowi4_oif = oif;
95 fl4->flowi4_iif = LOOPBACK_IFINDEX;
96 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;
102 fl4->flowi4_uid = uid;
103 fl4->daddr = daddr;
104 fl4->saddr = saddr;
105 fl4->fl4_dport = dport;
106 fl4->fl4_sport = sport;
107 }
108
109 /* Reset some input parameters after previous lookup */
flowi4_update_output(struct flowi4 * fl4,int oif,__u8 tos,__be32 daddr,__be32 saddr)110 static 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 }
118
119
120 struct flowi6 {
121 struct flowi_common __fl_common;
122 #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
130 #define flowi6_uid __fl_common.flowic_uid
131 struct in6_addr daddr;
132 struct in6_addr saddr;
133 __be32 flowlabel;
134 union flowi_uli uli;
135 #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
142 } __attribute__((__aligned__(BITS_PER_LONG/8)));
143
144 struct flowidn {
145 struct flowi_common __fl_common;
146 #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
152 __le16 daddr;
153 __le16 saddr;
154 union flowi_uli uli;
155 #define fld_sport uli.ports.sport
156 #define fld_dport uli.ports.dport
157 } __attribute__((__aligned__(BITS_PER_LONG/8)));
158
159 struct flowi {
160 union {
161 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
174 #define flowi_uid u.__fl_common.flowic_uid
175 } __attribute__((__aligned__(BITS_PER_LONG/8)));
176
flowi4_to_flowi(struct flowi4 * fl4)177 static inline struct flowi *flowi4_to_flowi(struct flowi4 *fl4)
178 {
179 return container_of(fl4, struct flowi, u.ip4);
180 }
181
flowi6_to_flowi(struct flowi6 * fl6)182 static inline struct flowi *flowi6_to_flowi(struct flowi6 *fl6)
183 {
184 return container_of(fl6, struct flowi, u.ip6);
185 }
186
flowidn_to_flowi(struct flowidn * fldn)187 static inline struct flowi *flowidn_to_flowi(struct flowidn *fldn)
188 {
189 return container_of(fldn, struct flowi, u.dn);
190 }
191
192 typedef unsigned long flow_compare_t;
193
flow_key_size(u16 family)194 static 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
210 #define FLOW_DIR_IN 0
211 #define FLOW_DIR_OUT 1
212 #define FLOW_DIR_FWD 2
213
214 struct net;
215 struct sock;
216 struct flow_cache_ops;
217
218 struct flow_cache_object {
219 const struct flow_cache_ops *ops;
220 };
221
222 struct 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
228 typedef struct flow_cache_object *(*flow_resolve_t)(
229 struct net *net, const struct flowi *key, u16 family,
230 u8 dir, struct flow_cache_object *oldobj, void *ctx);
231
232 struct 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);
236 int flow_cache_init(struct net *net);
237 void flow_cache_fini(struct net *net);
238
239 void flow_cache_flush(struct net *net);
240 void flow_cache_flush_deferred(struct net *net);
241 extern atomic_t flow_cache_genid;
242
243 #endif
244