]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef _NET_XFRM_H |
3 | #define _NET_XFRM_H | |
4 | ||
aabc9761 | 5 | #include <linux/compiler.h> |
1da177e4 LT |
6 | #include <linux/xfrm.h> |
7 | #include <linux/spinlock.h> | |
8 | #include <linux/list.h> | |
9 | #include <linux/skbuff.h> | |
14c85021 | 10 | #include <linux/socket.h> |
1da177e4 | 11 | #include <linux/pfkeyv2.h> |
5794708f | 12 | #include <linux/ipsec.h> |
1da177e4 | 13 | #include <linux/in6.h> |
4a3e2f71 | 14 | #include <linux/mutex.h> |
ab5f5e8b | 15 | #include <linux/audit.h> |
5a0e3ad6 | 16 | #include <linux/slab.h> |
88755e9c | 17 | #include <linux/refcount.h> |
1da177e4 LT |
18 | |
19 | #include <net/sock.h> | |
20 | #include <net/dst.h> | |
436a0a40 | 21 | #include <net/ip.h> |
1da177e4 LT |
22 | #include <net/route.h> |
23 | #include <net/ipv6.h> | |
24 | #include <net/ip6_fib.h> | |
fe1a5f03 | 25 | #include <net/flow.h> |
f203b76d | 26 | #include <net/gro_cells.h> |
9e0d57fd YP |
27 | |
28 | #include <linux/interrupt.h> | |
29 | ||
558f82ef MN |
30 | #ifdef CONFIG_XFRM_STATISTICS |
31 | #include <net/snmp.h> | |
32 | #endif | |
1da177e4 | 33 | |
d3d6dd3a MN |
34 | #define XFRM_PROTO_ESP 50 |
35 | #define XFRM_PROTO_AH 51 | |
36 | #define XFRM_PROTO_COMP 108 | |
37 | #define XFRM_PROTO_IPIP 4 | |
38 | #define XFRM_PROTO_IPV6 41 | |
39 | #define XFRM_PROTO_ROUTING IPPROTO_ROUTING | |
40 | #define XFRM_PROTO_DSTOPTS IPPROTO_DSTOPTS | |
41 | ||
fa9921e4 | 42 | #define XFRM_ALIGN4(len) (((len) + 3) & ~3) |
1da177e4 | 43 | #define XFRM_ALIGN8(len) (((len) + 7) & ~7) |
b59f45d0 HX |
44 | #define MODULE_ALIAS_XFRM_MODE(family, encap) \ |
45 | MODULE_ALIAS("xfrm-mode-" __stringify(family) "-" __stringify(encap)) | |
d3d6dd3a MN |
46 | #define MODULE_ALIAS_XFRM_TYPE(family, proto) \ |
47 | MODULE_ALIAS("xfrm-type-" __stringify(family) "-" __stringify(proto)) | |
ffdb5211 IT |
48 | #define MODULE_ALIAS_XFRM_OFFLOAD_TYPE(family, proto) \ |
49 | MODULE_ALIAS("xfrm-offload-" __stringify(family) "-" __stringify(proto)) | |
1da177e4 | 50 | |
558f82ef | 51 | #ifdef CONFIG_XFRM_STATISTICS |
59c9940e | 52 | #define XFRM_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.xfrm_statistics, field) |
558f82ef | 53 | #else |
59c9940e | 54 | #define XFRM_INC_STATS(net, field) ((void)(net)) |
558f82ef MN |
55 | #endif |
56 | ||
1da177e4 LT |
57 | |
58 | /* Organization of SPD aka "XFRM rules" | |
59 | ------------------------------------ | |
60 | ||
61 | Basic objects: | |
62 | - policy rule, struct xfrm_policy (=SPD entry) | |
63 | - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle) | |
64 | - instance of a transformer, struct xfrm_state (=SA) | |
65 | - template to clone xfrm_state, struct xfrm_tmpl | |
66 | ||
67 | SPD is plain linear list of xfrm_policy rules, ordered by priority. | |
68 | (To be compatible with existing pfkeyv2 implementations, | |
69 | many rules with priority of 0x7fffffff are allowed to exist and | |
70 | such rules are ordered in an unpredictable way, thanks to bsd folks.) | |
71 | ||
72 | Lookup is plain linear search until the first match with selector. | |
73 | ||
74 | If "action" is "block", then we prohibit the flow, otherwise: | |
75 | if "xfrms_nr" is zero, the flow passes untransformed. Otherwise, | |
76 | policy entry has list of up to XFRM_MAX_DEPTH transformations, | |
77 | described by templates xfrm_tmpl. Each template is resolved | |
78 | to a complete xfrm_state (see below) and we pack bundle of transformations | |
79 | to a dst_entry returned to requestor. | |
80 | ||
81 | dst -. xfrm .-> xfrm_state #1 | |
82 | |---. child .-> dst -. xfrm .-> xfrm_state #2 | |
83 | |---. child .-> dst -. xfrm .-> xfrm_state #3 | |
84 | |---. child .-> NULL | |
85 | ||
86 | Bundles are cached at xrfm_policy struct (field ->bundles). | |
87 | ||
88 | ||
89 | Resolution of xrfm_tmpl | |
90 | ----------------------- | |
91 | Template contains: | |
92 | 1. ->mode Mode: transport or tunnel | |
93 | 2. ->id.proto Protocol: AH/ESP/IPCOMP | |
94 | 3. ->id.daddr Remote tunnel endpoint, ignored for transport mode. | |
95 | Q: allow to resolve security gateway? | |
96 | 4. ->id.spi If not zero, static SPI. | |
97 | 5. ->saddr Local tunnel endpoint, ignored for transport mode. | |
98 | 6. ->algos List of allowed algos. Plain bitmask now. | |
99 | Q: ealgos, aalgos, calgos. What a mess... | |
100 | 7. ->share Sharing mode. | |
101 | Q: how to implement private sharing mode? To add struct sock* to | |
102 | flow id? | |
103 | ||
104 | Having this template we search through SAD searching for entries | |
105 | with appropriate mode/proto/algo, permitted by selector. | |
106 | If no appropriate entry found, it is requested from key manager. | |
107 | ||
108 | PROBLEMS: | |
109 | Q: How to find all the bundles referring to a physical path for | |
110 | PMTU discovery? Seems, dst should contain list of all parents... | |
111 | and enter to infinite locking hierarchy disaster. | |
112 | No! It is easier, we will not search for them, let them find us. | |
113 | We add genid to each dst plus pointer to genid of raw IP route, | |
114 | pmtu disc will update pmtu on raw IP route and increase its genid. | |
115 | dst_check() will see this for top level and trigger resyncing | |
116 | metrics. Plus, it will be made via sk->sk_dst_cache. Solved. | |
117 | */ | |
118 | ||
12a169e7 HX |
119 | struct xfrm_state_walk { |
120 | struct list_head all; | |
121 | u8 state; | |
d3623099 ND |
122 | u8 dying; |
123 | u8 proto; | |
12a169e7 | 124 | u32 seq; |
870a2df4 | 125 | struct xfrm_address_filter *filter; |
12a169e7 HX |
126 | }; |
127 | ||
d77e38e6 SK |
128 | struct xfrm_state_offload { |
129 | struct net_device *dev; | |
130 | unsigned long offload_handle; | |
131 | unsigned int num_exthdrs; | |
132 | u8 flags; | |
133 | }; | |
134 | ||
1da177e4 | 135 | /* Full description of state of transformer. */ |
fd2c3ef7 | 136 | struct xfrm_state { |
0c5c9fb5 | 137 | possible_net_t xs_net; |
abb81c4f | 138 | union { |
12a169e7 | 139 | struct hlist_node gclist; |
abb81c4f HX |
140 | struct hlist_node bydst; |
141 | }; | |
8f126e37 DM |
142 | struct hlist_node bysrc; |
143 | struct hlist_node byspi; | |
1da177e4 | 144 | |
88755e9c | 145 | refcount_t refcnt; |
1da177e4 LT |
146 | spinlock_t lock; |
147 | ||
148 | struct xfrm_id id; | |
149 | struct xfrm_selector sel; | |
bf825f81 | 150 | struct xfrm_mark mark; |
7e652640 | 151 | u32 if_id; |
35d2856b | 152 | u32 tfcpad; |
1da177e4 | 153 | |
9d4a706d DM |
154 | u32 genid; |
155 | ||
12a169e7 HX |
156 | /* Key manager bits */ |
157 | struct xfrm_state_walk km; | |
1da177e4 LT |
158 | |
159 | /* Parameters of this state. */ | |
160 | struct { | |
161 | u32 reqid; | |
162 | u8 mode; | |
163 | u8 replay_window; | |
164 | u8 aalgo, ealgo, calgo; | |
165 | u8 flags; | |
166 | u16 family; | |
167 | xfrm_address_t saddr; | |
168 | int header_len; | |
169 | int trailer_len; | |
a947b0a9 | 170 | u32 extra_flags; |
9b42c1f1 | 171 | struct xfrm_mark smark; |
1da177e4 LT |
172 | } props; |
173 | ||
174 | struct xfrm_lifetime_cfg lft; | |
175 | ||
176 | /* Data for transformer */ | |
4447bb33 | 177 | struct xfrm_algo_auth *aalg; |
1da177e4 LT |
178 | struct xfrm_algo *ealg; |
179 | struct xfrm_algo *calg; | |
1a6509d9 | 180 | struct xfrm_algo_aead *aead; |
69b0137f | 181 | const char *geniv; |
1da177e4 LT |
182 | |
183 | /* Data for encapsulator */ | |
184 | struct xfrm_encap_tmpl *encap; | |
185 | ||
060f02a3 NT |
186 | /* Data for care-of address */ |
187 | xfrm_address_t *coaddr; | |
188 | ||
1da177e4 LT |
189 | /* IPComp needs an IPIP tunnel for handling uncompressed packets */ |
190 | struct xfrm_state *tunnel; | |
191 | ||
192 | /* If a tunnel, number of users + 1 */ | |
193 | atomic_t tunnel_users; | |
194 | ||
195 | /* State for replay detection */ | |
196 | struct xfrm_replay_state replay; | |
9736acf3 | 197 | struct xfrm_replay_state_esn *replay_esn; |
1da177e4 | 198 | |
f8cd5488 JHS |
199 | /* Replay detection state at the time we sent the last notification */ |
200 | struct xfrm_replay_state preplay; | |
9736acf3 | 201 | struct xfrm_replay_state_esn *preplay_esn; |
f8cd5488 | 202 | |
9fdc4883 | 203 | /* The functions for replay detection. */ |
e45a8a9e | 204 | const struct xfrm_replay *repl; |
9fdc4883 | 205 | |
2717096a JHS |
206 | /* internal flag that only holds state for delayed aevent at the |
207 | * moment | |
208 | */ | |
209 | u32 xflags; | |
210 | ||
f8cd5488 JHS |
211 | /* Replay detection notification settings */ |
212 | u32 replay_maxage; | |
213 | u32 replay_maxdiff; | |
214 | ||
215 | /* Replay detection notification timer */ | |
216 | struct timer_list rtimer; | |
217 | ||
1da177e4 LT |
218 | /* Statistics */ |
219 | struct xfrm_stats stats; | |
220 | ||
221 | struct xfrm_lifetime_cur curlft; | |
9e0d57fd | 222 | struct tasklet_hrtimer mtimer; |
1da177e4 | 223 | |
d77e38e6 SK |
224 | struct xfrm_state_offload xso; |
225 | ||
e3c0d047 FD |
226 | /* used to fix curlft->add_time when changing date */ |
227 | long saved_tmo; | |
228 | ||
9afaca05 | 229 | /* Last used time */ |
03dc7a35 | 230 | time64_t lastused; |
9afaca05 | 231 | |
cac2661c SK |
232 | struct page_frag xfrag; |
233 | ||
1da177e4 LT |
234 | /* Reference to data common to all the instances of this |
235 | * transformer. */ | |
533cb5b0 | 236 | const struct xfrm_type *type; |
13996378 | 237 | struct xfrm_mode *inner_mode; |
df9dcb45 | 238 | struct xfrm_mode *inner_mode_iaf; |
13996378 | 239 | struct xfrm_mode *outer_mode; |
1da177e4 | 240 | |
9d389d7f SK |
241 | const struct xfrm_type_offload *type_offload; |
242 | ||
df71837d TJ |
243 | /* Security context */ |
244 | struct xfrm_sec_ctx *security; | |
245 | ||
1da177e4 LT |
246 | /* Private data of this transformer, format is opaque, |
247 | * interpreted by xfrm_type methods. */ | |
248 | void *data; | |
249 | }; | |
250 | ||
673c09be AD |
251 | static inline struct net *xs_net(struct xfrm_state *x) |
252 | { | |
253 | return read_pnet(&x->xs_net); | |
254 | } | |
255 | ||
2717096a JHS |
256 | /* xflags - make enum if more show up */ |
257 | #define XFRM_TIME_DEFER 1 | |
e3c0d047 | 258 | #define XFRM_SOFT_EXPIRE 2 |
2717096a | 259 | |
1da177e4 LT |
260 | enum { |
261 | XFRM_STATE_VOID, | |
262 | XFRM_STATE_ACQ, | |
263 | XFRM_STATE_VALID, | |
264 | XFRM_STATE_ERROR, | |
265 | XFRM_STATE_EXPIRED, | |
266 | XFRM_STATE_DEAD | |
267 | }; | |
268 | ||
26b15dad | 269 | /* callback structure passed from either netlink or pfkey */ |
fd2c3ef7 | 270 | struct km_event { |
bf08867f HX |
271 | union { |
272 | u32 hard; | |
273 | u32 proto; | |
274 | u32 byid; | |
f8cd5488 | 275 | u32 aevent; |
f7b6983f | 276 | u32 type; |
bf08867f HX |
277 | } data; |
278 | ||
26b15dad | 279 | u32 seq; |
15e47304 | 280 | u32 portid; |
26b15dad | 281 | u32 event; |
7067802e | 282 | struct net *net; |
26b15dad JHS |
283 | }; |
284 | ||
9fdc4883 SK |
285 | struct xfrm_replay { |
286 | void (*advance)(struct xfrm_state *x, __be32 net_seq); | |
287 | int (*check)(struct xfrm_state *x, | |
288 | struct sk_buff *skb, | |
289 | __be32 net_seq); | |
3b59df46 SK |
290 | int (*recheck)(struct xfrm_state *x, |
291 | struct sk_buff *skb, | |
292 | __be32 net_seq); | |
9fdc4883 SK |
293 | void (*notify)(struct xfrm_state *x, int event); |
294 | int (*overflow)(struct xfrm_state *x, struct sk_buff *skb); | |
295 | }; | |
296 | ||
f203b76d SK |
297 | struct xfrm_if_cb { |
298 | struct xfrm_if *(*decode_session)(struct sk_buff *skb); | |
299 | }; | |
300 | ||
301 | void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb); | |
302 | void xfrm_if_unregister_cb(void); | |
303 | ||
25ee3286 | 304 | struct net_device; |
1da177e4 LT |
305 | struct xfrm_type; |
306 | struct xfrm_dst; | |
307 | struct xfrm_policy_afinfo { | |
1da177e4 | 308 | struct dst_ops *dst_ops; |
42a7b32b DA |
309 | struct dst_entry *(*dst_lookup)(struct net *net, |
310 | int tos, int oif, | |
5e6b930f | 311 | const xfrm_address_t *saddr, |
077fbac4 LC |
312 | const xfrm_address_t *daddr, |
313 | u32 mark); | |
42a7b32b DA |
314 | int (*get_saddr)(struct net *net, int oif, |
315 | xfrm_address_t *saddr, | |
077fbac4 LC |
316 | xfrm_address_t *daddr, |
317 | u32 mark); | |
1da177e4 | 318 | void (*decode_session)(struct sk_buff *skb, |
d5422efe HX |
319 | struct flowi *fl, |
320 | int reverse); | |
05d84025 | 321 | int (*get_tos)(const struct flowi *fl); |
a1b05140 MN |
322 | int (*init_path)(struct xfrm_dst *path, |
323 | struct dst_entry *dst, | |
324 | int nfheader_len); | |
25ee3286 | 325 | int (*fill_dst)(struct xfrm_dst *xdst, |
87c1e12b | 326 | struct net_device *dev, |
0c7b3eef | 327 | const struct flowi *fl); |
2774c131 | 328 | struct dst_entry *(*blackhole_route)(struct net *net, struct dst_entry *orig); |
1da177e4 LT |
329 | }; |
330 | ||
a2817d8b FW |
331 | int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family); |
332 | void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo); | |
d511337a JP |
333 | void km_policy_notify(struct xfrm_policy *xp, int dir, |
334 | const struct km_event *c); | |
335 | void km_state_notify(struct xfrm_state *x, const struct km_event *c); | |
1da177e4 LT |
336 | |
337 | struct xfrm_tmpl; | |
d511337a JP |
338 | int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, |
339 | struct xfrm_policy *pol); | |
340 | void km_state_expired(struct xfrm_state *x, int hard, u32 portid); | |
341 | int __xfrm_state_delete(struct xfrm_state *x); | |
53bc6b4d | 342 | |
1da177e4 | 343 | struct xfrm_state_afinfo { |
9d389d7f SK |
344 | unsigned int family; |
345 | unsigned int proto; | |
346 | __be16 eth_proto; | |
347 | struct module *owner; | |
348 | const struct xfrm_type *type_map[IPPROTO_MAX]; | |
349 | const struct xfrm_type_offload *type_offload_map[IPPROTO_MAX]; | |
350 | struct xfrm_mode *mode_map[XFRM_MODE_MAX]; | |
351 | ||
d094cd83 | 352 | int (*init_flags)(struct xfrm_state *x); |
73e5ebb2 DM |
353 | void (*init_tempsel)(struct xfrm_selector *sel, |
354 | const struct flowi *fl); | |
19bd6244 DM |
355 | void (*init_temprop)(struct xfrm_state *x, |
356 | const struct xfrm_tmpl *tmpl, | |
357 | const xfrm_address_t *daddr, | |
358 | const xfrm_address_t *saddr); | |
41a49cc3 MN |
359 | int (*tmpl_sort)(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n); |
360 | int (*state_sort)(struct xfrm_state **dst, struct xfrm_state **src, int n); | |
ede2059d | 361 | int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb); |
7026b1dd | 362 | int (*output_finish)(struct sock *sk, struct sk_buff *skb); |
227620e2 HX |
363 | int (*extract_input)(struct xfrm_state *x, |
364 | struct sk_buff *skb); | |
36cf9acf HX |
365 | int (*extract_output)(struct xfrm_state *x, |
366 | struct sk_buff *skb); | |
716062fd HX |
367 | int (*transport_finish)(struct sk_buff *skb, |
368 | int async); | |
628e341f | 369 | void (*local_error)(struct sk_buff *skb, u32 mtu); |
1da177e4 LT |
370 | }; |
371 | ||
d511337a JP |
372 | int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo); |
373 | int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo); | |
374 | struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family); | |
711059b9 | 375 | struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family); |
1da177e4 | 376 | |
2f32b51b SK |
377 | struct xfrm_input_afinfo { |
378 | unsigned int family; | |
2f32b51b SK |
379 | int (*callback)(struct sk_buff *skb, u8 protocol, |
380 | int err); | |
381 | }; | |
382 | ||
960fdfde FW |
383 | int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo); |
384 | int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo); | |
2f32b51b | 385 | |
b48c05ab | 386 | void xfrm_flush_gc(void); |
d511337a | 387 | void xfrm_state_delete_tunnel(struct xfrm_state *x); |
1da177e4 | 388 | |
fd2c3ef7 | 389 | struct xfrm_type { |
1da177e4 LT |
390 | char *description; |
391 | struct module *owner; | |
a6337463 | 392 | u8 proto; |
393 | u8 flags; | |
1b5c2299 | 394 | #define XFRM_TYPE_NON_FRAGMENT 1 |
436a0a40 | 395 | #define XFRM_TYPE_REPLAY_PROT 2 |
f04e7e8d HX |
396 | #define XFRM_TYPE_LOCAL_COADDR 4 |
397 | #define XFRM_TYPE_REMOTE_COADDR 8 | |
1da177e4 | 398 | |
72cb6962 | 399 | int (*init_state)(struct xfrm_state *x); |
1da177e4 | 400 | void (*destructor)(struct xfrm_state *); |
e695633e | 401 | int (*input)(struct xfrm_state *, struct sk_buff *skb); |
1da177e4 | 402 | int (*output)(struct xfrm_state *, struct sk_buff *pskb); |
8f029de2 DM |
403 | int (*reject)(struct xfrm_state *, struct sk_buff *, |
404 | const struct flowi *); | |
aee5adb4 | 405 | int (*hdr_offset)(struct xfrm_state *, struct sk_buff *, u8 **); |
1da177e4 | 406 | /* Estimate maximal size of result of transformation of a dgram */ |
c5c25238 | 407 | u32 (*get_mtu)(struct xfrm_state *, int size); |
1da177e4 LT |
408 | }; |
409 | ||
d511337a JP |
410 | int xfrm_register_type(const struct xfrm_type *type, unsigned short family); |
411 | int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family); | |
1da177e4 | 412 | |
9d389d7f SK |
413 | struct xfrm_type_offload { |
414 | char *description; | |
415 | struct module *owner; | |
416 | u8 proto; | |
417 | void (*encap)(struct xfrm_state *, struct sk_buff *pskb); | |
418 | int (*input_tail)(struct xfrm_state *x, struct sk_buff *skb); | |
419 | int (*xmit)(struct xfrm_state *, struct sk_buff *pskb, netdev_features_t features); | |
420 | }; | |
421 | ||
422 | int xfrm_register_type_offload(const struct xfrm_type_offload *type, unsigned short family); | |
423 | int xfrm_unregister_type_offload(const struct xfrm_type_offload *type, unsigned short family); | |
424 | ||
b59f45d0 | 425 | struct xfrm_mode { |
37fedd3a HX |
426 | /* |
427 | * Add encapsulation header. | |
428 | * | |
429 | * On exit, the transport header will be set to the start of the | |
430 | * encapsulation header to be filled in by x->type->output and | |
431 | * the mac header will be set to the nextheader (protocol for | |
432 | * IPv4) field of the extension header directly preceding the | |
433 | * encapsulation header, or in its absence, that of the top IP | |
434 | * header. The value of the network header will always point | |
435 | * to the top IP header while skb->data will point to the payload. | |
436 | */ | |
36cf9acf HX |
437 | int (*output2)(struct xfrm_state *x,struct sk_buff *skb); |
438 | ||
17c2a42a | 439 | struct xfrm_state_afinfo *afinfo; |
b59f45d0 | 440 | struct module *owner; |
b262a695 FW |
441 | u8 encap; |
442 | u8 family; | |
443 | u8 flags; | |
1bfcb10f HX |
444 | }; |
445 | ||
446 | /* Flags for xfrm_mode. */ | |
447 | enum { | |
448 | XFRM_MODE_FLAG_TUNNEL = 1, | |
b59f45d0 HX |
449 | }; |
450 | ||
b262a695 FW |
451 | int xfrm_register_mode(struct xfrm_mode *mode); |
452 | void xfrm_unregister_mode(struct xfrm_mode *mode); | |
b59f45d0 | 453 | |
df9dcb45 KM |
454 | static inline int xfrm_af2proto(unsigned int family) |
455 | { | |
456 | switch(family) { | |
457 | case AF_INET: | |
458 | return IPPROTO_IPIP; | |
459 | case AF_INET6: | |
460 | return IPPROTO_IPV6; | |
461 | default: | |
462 | return 0; | |
463 | } | |
464 | } | |
465 | ||
466 | static inline struct xfrm_mode *xfrm_ip2inner_mode(struct xfrm_state *x, int ipproto) | |
467 | { | |
468 | if ((ipproto == IPPROTO_IPIP && x->props.family == AF_INET) || | |
469 | (ipproto == IPPROTO_IPV6 && x->props.family == AF_INET6)) | |
470 | return x->inner_mode; | |
471 | else | |
472 | return x->inner_mode_iaf; | |
473 | } | |
474 | ||
fd2c3ef7 | 475 | struct xfrm_tmpl { |
1da177e4 LT |
476 | /* id in template is interpreted as: |
477 | * daddr - destination of tunnel, may be zero for transport mode. | |
478 | * spi - zero to acquire spi. Not zero if spi is static, then | |
479 | * daddr must be fixed too. | |
480 | * proto - AH/ESP/IPCOMP | |
481 | */ | |
482 | struct xfrm_id id; | |
483 | ||
484 | /* Source address of tunnel. Ignored, if it is not a tunnel. */ | |
485 | xfrm_address_t saddr; | |
486 | ||
76b3f055 MK |
487 | unsigned short encap_family; |
488 | ||
a6337463 | 489 | u32 reqid; |
1da177e4 | 490 | |
7e49e6de | 491 | /* Mode: transport, tunnel etc. */ |
a6337463 | 492 | u8 mode; |
1da177e4 LT |
493 | |
494 | /* Sharing mode: unique, this session only, this user only etc. */ | |
a6337463 | 495 | u8 share; |
1da177e4 LT |
496 | |
497 | /* May skip this transfomration if no SA is found */ | |
a6337463 | 498 | u8 optional; |
1da177e4 | 499 | |
c5d18e98 | 500 | /* Skip aalgos/ealgos/calgos checks. */ |
a6337463 | 501 | u8 allalgs; |
c5d18e98 | 502 | |
1da177e4 | 503 | /* Bit mask of algos allowed for acquisition */ |
a6337463 | 504 | u32 aalgos; |
505 | u32 ealgos; | |
506 | u32 calgos; | |
1da177e4 LT |
507 | }; |
508 | ||
622dc828 | 509 | #define XFRM_MAX_DEPTH 6 |
54ef207a | 510 | #define XFRM_MAX_OFFLOAD_DEPTH 1 |
1da177e4 | 511 | |
12a169e7 HX |
512 | struct xfrm_policy_walk_entry { |
513 | struct list_head all; | |
514 | u8 dead; | |
515 | }; | |
516 | ||
517 | struct xfrm_policy_walk { | |
518 | struct xfrm_policy_walk_entry walk; | |
519 | u8 type; | |
520 | u32 seq; | |
521 | }; | |
522 | ||
a0073fe1 SK |
523 | struct xfrm_policy_queue { |
524 | struct sk_buff_head hold_queue; | |
525 | struct timer_list hold_timer; | |
526 | unsigned long timeout; | |
527 | }; | |
528 | ||
fd2c3ef7 | 529 | struct xfrm_policy { |
0c5c9fb5 | 530 | possible_net_t xp_net; |
2518c7c2 DM |
531 | struct hlist_node bydst; |
532 | struct hlist_node byidx; | |
1da177e4 LT |
533 | |
534 | /* This lock only affects elements except for entry. */ | |
535 | rwlock_t lock; | |
850a6212 | 536 | refcount_t refcnt; |
6be3b0db | 537 | u32 pos; |
1da177e4 LT |
538 | struct timer_list timer; |
539 | ||
80c802f3 | 540 | atomic_t genid; |
1da177e4 LT |
541 | u32 priority; |
542 | u32 index; | |
7e652640 | 543 | u32 if_id; |
bf825f81 | 544 | struct xfrm_mark mark; |
1da177e4 LT |
545 | struct xfrm_selector selector; |
546 | struct xfrm_lifetime_cfg lft; | |
547 | struct xfrm_lifetime_cur curlft; | |
12a169e7 | 548 | struct xfrm_policy_walk_entry walk; |
a0073fe1 | 549 | struct xfrm_policy_queue polq; |
9cf545eb | 550 | bool bydst_reinsert; |
46ca5f5d ACM |
551 | u8 type; |
552 | u8 action; | |
553 | u8 flags; | |
46ca5f5d | 554 | u8 xfrm_nr; |
12a169e7 | 555 | u16 family; |
df71837d | 556 | struct xfrm_sec_ctx *security; |
1da177e4 | 557 | struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH]; |
24969fac | 558 | struct hlist_node bydst_inexact_list; |
56f04730 | 559 | struct rcu_head rcu; |
1da177e4 LT |
560 | }; |
561 | ||
63eb23f5 | 562 | static inline struct net *xp_net(const struct xfrm_policy *xp) |
0331b1f3 AD |
563 | { |
564 | return read_pnet(&xp->xp_net); | |
565 | } | |
566 | ||
13c1d189 AE |
567 | struct xfrm_kmaddress { |
568 | xfrm_address_t local; | |
569 | xfrm_address_t remote; | |
570 | u32 reserved; | |
571 | u16 family; | |
572 | }; | |
573 | ||
80c9abaa SS |
574 | struct xfrm_migrate { |
575 | xfrm_address_t old_daddr; | |
576 | xfrm_address_t old_saddr; | |
577 | xfrm_address_t new_daddr; | |
578 | xfrm_address_t new_saddr; | |
579 | u8 proto; | |
580 | u8 mode; | |
581 | u16 reserved; | |
582 | u32 reqid; | |
583 | u16 old_family; | |
584 | u16 new_family; | |
585 | }; | |
586 | ||
f8cd5488 | 587 | #define XFRM_KM_TIMEOUT 30 |
f8cd5488 JHS |
588 | /* what happened */ |
589 | #define XFRM_REPLAY_UPDATE XFRM_AE_CR | |
590 | #define XFRM_REPLAY_TIMEOUT XFRM_AE_CE | |
591 | ||
592 | /* default aevent timeout in units of 100ms */ | |
593 | #define XFRM_AE_ETIME 10 | |
594 | /* Async Event timer multiplier */ | |
595 | #define XFRM_AE_ETH_M 10 | |
596 | /* default seq threshold size */ | |
597 | #define XFRM_AE_SEQT_SIZE 2 | |
1da177e4 | 598 | |
fd2c3ef7 | 599 | struct xfrm_mgr { |
1da177e4 | 600 | struct list_head list; |
214e005b | 601 | int (*notify)(struct xfrm_state *x, const struct km_event *c); |
65e0736b | 602 | int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp); |
cb969f07 | 603 | struct xfrm_policy *(*compile_policy)(struct sock *sk, int opt, u8 *data, int len, int *dir); |
5d36b180 | 604 | int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport); |
214e005b | 605 | int (*notify_policy)(struct xfrm_policy *x, int dir, const struct km_event *c); |
db983c11 | 606 | int (*report)(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr); |
183cad12 DM |
607 | int (*migrate)(const struct xfrm_selector *sel, |
608 | u8 dir, u8 type, | |
609 | const struct xfrm_migrate *m, | |
610 | int num_bundles, | |
8bafd730 AA |
611 | const struct xfrm_kmaddress *k, |
612 | const struct xfrm_encap_tmpl *encap); | |
0f24558e | 613 | bool (*is_alive)(const struct km_event *c); |
1da177e4 LT |
614 | }; |
615 | ||
d511337a JP |
616 | int xfrm_register_km(struct xfrm_mgr *km); |
617 | int xfrm_unregister_km(struct xfrm_mgr *km); | |
1da177e4 | 618 | |
70be6c91 SK |
619 | struct xfrm_tunnel_skb_cb { |
620 | union { | |
621 | struct inet_skb_parm h4; | |
622 | struct inet6_skb_parm h6; | |
623 | } header; | |
624 | ||
625 | union { | |
626 | struct ip_tunnel *ip4; | |
627 | struct ip6_tnl *ip6; | |
628 | } tunnel; | |
629 | }; | |
630 | ||
631 | #define XFRM_TUNNEL_SKB_CB(__skb) ((struct xfrm_tunnel_skb_cb *)&((__skb)->cb[0])) | |
632 | ||
436a0a40 HX |
633 | /* |
634 | * This structure is used for the duration where packets are being | |
635 | * transformed by IPsec. As soon as the packet leaves IPsec the | |
636 | * area beyond the generic IP part may be overwritten. | |
637 | */ | |
638 | struct xfrm_skb_cb { | |
70be6c91 | 639 | struct xfrm_tunnel_skb_cb header; |
436a0a40 HX |
640 | |
641 | /* Sequence number for replay protection. */ | |
b318e0e4 | 642 | union { |
1ce3644a SK |
643 | struct { |
644 | __u32 low; | |
645 | __u32 hi; | |
646 | } output; | |
647 | struct { | |
648 | __be32 low; | |
649 | __be32 hi; | |
650 | } input; | |
b318e0e4 | 651 | } seq; |
436a0a40 HX |
652 | }; |
653 | ||
654 | #define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0])) | |
655 | ||
36cf9acf HX |
656 | /* |
657 | * This structure is used by the afinfo prepare_input/prepare_output functions | |
658 | * to transmit header information to the mode input/output functions. | |
659 | */ | |
660 | struct xfrm_mode_skb_cb { | |
70be6c91 | 661 | struct xfrm_tunnel_skb_cb header; |
36cf9acf HX |
662 | |
663 | /* Copied from header for IPv4, always set to zero and DF for IPv6. */ | |
664 | __be16 id; | |
665 | __be16 frag_off; | |
666 | ||
732c8bd5 HX |
667 | /* IP header length (excluding options or extension headers). */ |
668 | u8 ihl; | |
669 | ||
36cf9acf HX |
670 | /* TOS for IPv4, class for IPv6. */ |
671 | u8 tos; | |
672 | ||
673 | /* TTL for IPv4, hop limitfor IPv6. */ | |
674 | u8 ttl; | |
675 | ||
676 | /* Protocol for IPv4, NH for IPv6. */ | |
677 | u8 protocol; | |
678 | ||
732c8bd5 HX |
679 | /* Option length for IPv4, zero for IPv6. */ |
680 | u8 optlen; | |
681 | ||
36cf9acf HX |
682 | /* Used by IPv6 only, zero for IPv4. */ |
683 | u8 flow_lbl[3]; | |
684 | }; | |
685 | ||
686 | #define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0])) | |
687 | ||
716062fd HX |
688 | /* |
689 | * This structure is used by the input processing to locate the SPI and | |
690 | * related information. | |
691 | */ | |
692 | struct xfrm_spi_skb_cb { | |
70be6c91 | 693 | struct xfrm_tunnel_skb_cb header; |
716062fd | 694 | |
716062fd | 695 | unsigned int daddroff; |
2fcb45b6 | 696 | unsigned int family; |
7785bba2 | 697 | __be32 seq; |
716062fd HX |
698 | }; |
699 | ||
700 | #define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0])) | |
701 | ||
c9204d9c | 702 | #ifdef CONFIG_AUDITSYSCALL |
afeb14b4 | 703 | static inline struct audit_buffer *xfrm_audit_start(const char *op) |
ab5f5e8b JL |
704 | { |
705 | struct audit_buffer *audit_buf = NULL; | |
ab5f5e8b | 706 | |
f7859590 | 707 | if (audit_enabled == AUDIT_OFF) |
afeb14b4 | 708 | return NULL; |
cdfb6b34 | 709 | audit_buf = audit_log_start(audit_context(), GFP_ATOMIC, |
afeb14b4 | 710 | AUDIT_MAC_IPSEC_EVENT); |
ab5f5e8b JL |
711 | if (audit_buf == NULL) |
712 | return NULL; | |
afeb14b4 PM |
713 | audit_log_format(audit_buf, "op=%s", op); |
714 | return audit_buf; | |
715 | } | |
ab5f5e8b | 716 | |
2e71029e | 717 | static inline void xfrm_audit_helper_usrinfo(bool task_valid, |
afeb14b4 PM |
718 | struct audit_buffer *audit_buf) |
719 | { | |
2e71029e TH |
720 | const unsigned int auid = from_kuid(&init_user_ns, task_valid ? |
721 | audit_get_loginuid(current) : | |
722 | INVALID_UID); | |
723 | const unsigned int ses = task_valid ? audit_get_sessionid(current) : | |
f0b75216 | 724 | AUDIT_SID_UNSET; |
2e71029e TH |
725 | |
726 | audit_log_format(audit_buf, " auid=%u ses=%u", auid, ses); | |
f1370cc4 | 727 | audit_log_task_context(audit_buf); |
ab5f5e8b JL |
728 | } |
729 | ||
2e71029e TH |
730 | void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid); |
731 | void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, | |
732 | bool task_valid); | |
733 | void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid); | |
734 | void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid); | |
d511337a JP |
735 | void xfrm_audit_state_replay_overflow(struct xfrm_state *x, |
736 | struct sk_buff *skb); | |
737 | void xfrm_audit_state_replay(struct xfrm_state *x, struct sk_buff *skb, | |
738 | __be32 net_seq); | |
739 | void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family); | |
740 | void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, __be32 net_spi, | |
741 | __be32 net_seq); | |
742 | void xfrm_audit_state_icvfail(struct xfrm_state *x, struct sk_buff *skb, | |
743 | u8 proto); | |
c9204d9c | 744 | #else |
41fef0ee MS |
745 | |
746 | static inline void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, | |
2e71029e | 747 | bool task_valid) |
41fef0ee MS |
748 | { |
749 | } | |
750 | ||
751 | static inline void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, | |
2e71029e | 752 | bool task_valid) |
41fef0ee MS |
753 | { |
754 | } | |
755 | ||
756 | static inline void xfrm_audit_state_add(struct xfrm_state *x, int result, | |
2e71029e | 757 | bool task_valid) |
41fef0ee MS |
758 | { |
759 | } | |
760 | ||
761 | static inline void xfrm_audit_state_delete(struct xfrm_state *x, int result, | |
2e71029e | 762 | bool task_valid) |
41fef0ee MS |
763 | { |
764 | } | |
765 | ||
766 | static inline void xfrm_audit_state_replay_overflow(struct xfrm_state *x, | |
767 | struct sk_buff *skb) | |
768 | { | |
769 | } | |
770 | ||
9fdc4883 SK |
771 | static inline void xfrm_audit_state_replay(struct xfrm_state *x, |
772 | struct sk_buff *skb, __be32 net_seq) | |
773 | { | |
774 | } | |
775 | ||
41fef0ee MS |
776 | static inline void xfrm_audit_state_notfound_simple(struct sk_buff *skb, |
777 | u16 family) | |
778 | { | |
779 | } | |
780 | ||
781 | static inline void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, | |
782 | __be32 net_spi, __be32 net_seq) | |
783 | { | |
784 | } | |
785 | ||
786 | static inline void xfrm_audit_state_icvfail(struct xfrm_state *x, | |
787 | struct sk_buff *skb, u8 proto) | |
788 | { | |
789 | } | |
c9204d9c | 790 | #endif /* CONFIG_AUDITSYSCALL */ |
161a09e7 | 791 | |
1da177e4 LT |
792 | static inline void xfrm_pol_hold(struct xfrm_policy *policy) |
793 | { | |
794 | if (likely(policy != NULL)) | |
850a6212 | 795 | refcount_inc(&policy->refcnt); |
1da177e4 LT |
796 | } |
797 | ||
d511337a | 798 | void xfrm_policy_destroy(struct xfrm_policy *policy); |
1da177e4 LT |
799 | |
800 | static inline void xfrm_pol_put(struct xfrm_policy *policy) | |
801 | { | |
850a6212 | 802 | if (refcount_dec_and_test(&policy->refcnt)) |
64c31b3f | 803 | xfrm_policy_destroy(policy); |
1da177e4 LT |
804 | } |
805 | ||
4e81bb83 MN |
806 | static inline void xfrm_pols_put(struct xfrm_policy **pols, int npols) |
807 | { | |
808 | int i; | |
809 | for (i = npols - 1; i >= 0; --i) | |
810 | xfrm_pol_put(pols[i]); | |
811 | } | |
4e81bb83 | 812 | |
f75a2804 | 813 | void __xfrm_state_destroy(struct xfrm_state *, bool); |
1da177e4 | 814 | |
21380b81 HX |
815 | static inline void __xfrm_state_put(struct xfrm_state *x) |
816 | { | |
88755e9c | 817 | refcount_dec(&x->refcnt); |
21380b81 HX |
818 | } |
819 | ||
1da177e4 LT |
820 | static inline void xfrm_state_put(struct xfrm_state *x) |
821 | { | |
88755e9c | 822 | if (refcount_dec_and_test(&x->refcnt)) |
f75a2804 CW |
823 | __xfrm_state_destroy(x, false); |
824 | } | |
825 | ||
826 | static inline void xfrm_state_put_sync(struct xfrm_state *x) | |
827 | { | |
828 | if (refcount_dec_and_test(&x->refcnt)) | |
829 | __xfrm_state_destroy(x, true); | |
1da177e4 LT |
830 | } |
831 | ||
832 | static inline void xfrm_state_hold(struct xfrm_state *x) | |
833 | { | |
88755e9c | 834 | refcount_inc(&x->refcnt); |
1da177e4 LT |
835 | } |
836 | ||
1744a8fe | 837 | static inline bool addr_match(const void *token1, const void *token2, |
e1b0048e | 838 | unsigned int prefixlen) |
1da177e4 | 839 | { |
1744a8fe DM |
840 | const __be32 *a1 = token1; |
841 | const __be32 *a2 = token2; | |
e1b0048e AD |
842 | unsigned int pdw; |
843 | unsigned int pbi; | |
1da177e4 | 844 | |
a6337463 | 845 | pdw = prefixlen >> 5; /* num of whole u32 in prefix */ |
1da177e4 LT |
846 | pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */ |
847 | ||
848 | if (pdw) | |
849 | if (memcmp(a1, a2, pdw << 2)) | |
1744a8fe | 850 | return false; |
1da177e4 LT |
851 | |
852 | if (pbi) { | |
5f19343f | 853 | __be32 mask; |
1da177e4 LT |
854 | |
855 | mask = htonl((0xffffffff) << (32 - pbi)); | |
856 | ||
857 | if ((a1[pdw] ^ a2[pdw]) & mask) | |
1744a8fe | 858 | return false; |
1da177e4 LT |
859 | } |
860 | ||
1744a8fe | 861 | return true; |
1da177e4 LT |
862 | } |
863 | ||
26bff940 AD |
864 | static inline bool addr4_match(__be32 a1, __be32 a2, u8 prefixlen) |
865 | { | |
866 | /* C99 6.5.7 (3): u32 << 32 is undefined behaviour */ | |
6c786bcb | 867 | if (sizeof(long) == 4 && prefixlen == 0) |
26bff940 | 868 | return true; |
6c786bcb | 869 | return !((a1 ^ a2) & htonl(~0UL << (32 - prefixlen))); |
26bff940 AD |
870 | } |
871 | ||
1da177e4 | 872 | static __inline__ |
6281dcc9 | 873 | __be16 xfrm_flowi_sport(const struct flowi *fl, const union flowi_uli *uli) |
1da177e4 | 874 | { |
f9d07e41 | 875 | __be16 port; |
1d28f42c | 876 | switch(fl->flowi_proto) { |
1da177e4 LT |
877 | case IPPROTO_TCP: |
878 | case IPPROTO_UDP: | |
ba4e58ec | 879 | case IPPROTO_UDPLITE: |
1da177e4 | 880 | case IPPROTO_SCTP: |
6281dcc9 | 881 | port = uli->ports.sport; |
1da177e4 LT |
882 | break; |
883 | case IPPROTO_ICMP: | |
884 | case IPPROTO_ICMPV6: | |
6281dcc9 | 885 | port = htons(uli->icmpt.type); |
1da177e4 | 886 | break; |
2ce4272a | 887 | case IPPROTO_MH: |
6281dcc9 | 888 | port = htons(uli->mht.type); |
2ce4272a | 889 | break; |
cc9ff19d | 890 | case IPPROTO_GRE: |
6281dcc9 | 891 | port = htons(ntohl(uli->gre_key) >> 16); |
cc9ff19d | 892 | break; |
1da177e4 LT |
893 | default: |
894 | port = 0; /*XXX*/ | |
895 | } | |
896 | return port; | |
897 | } | |
898 | ||
899 | static __inline__ | |
6281dcc9 | 900 | __be16 xfrm_flowi_dport(const struct flowi *fl, const union flowi_uli *uli) |
1da177e4 | 901 | { |
f9d07e41 | 902 | __be16 port; |
1d28f42c | 903 | switch(fl->flowi_proto) { |
1da177e4 LT |
904 | case IPPROTO_TCP: |
905 | case IPPROTO_UDP: | |
ba4e58ec | 906 | case IPPROTO_UDPLITE: |
1da177e4 | 907 | case IPPROTO_SCTP: |
6281dcc9 | 908 | port = uli->ports.dport; |
1da177e4 LT |
909 | break; |
910 | case IPPROTO_ICMP: | |
911 | case IPPROTO_ICMPV6: | |
6281dcc9 | 912 | port = htons(uli->icmpt.code); |
1da177e4 | 913 | break; |
cc9ff19d | 914 | case IPPROTO_GRE: |
6281dcc9 | 915 | port = htons(ntohl(uli->gre_key) & 0xffff); |
cc9ff19d | 916 | break; |
1da177e4 LT |
917 | default: |
918 | port = 0; /*XXX*/ | |
919 | } | |
920 | return port; | |
921 | } | |
922 | ||
d511337a JP |
923 | bool xfrm_selector_match(const struct xfrm_selector *sel, |
924 | const struct flowi *fl, unsigned short family); | |
1da177e4 | 925 | |
df71837d TJ |
926 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
927 | /* If neither has a context --> match | |
928 | * Otherwise, both must have a context and the sids, doi, alg must match | |
929 | */ | |
bc9b35ad | 930 | static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2) |
df71837d TJ |
931 | { |
932 | return ((!s1 && !s2) || | |
933 | (s1 && s2 && | |
934 | (s1->ctx_sid == s2->ctx_sid) && | |
935 | (s1->ctx_doi == s2->ctx_doi) && | |
936 | (s1->ctx_alg == s2->ctx_alg))); | |
937 | } | |
938 | #else | |
bc9b35ad | 939 | static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2) |
df71837d | 940 | { |
bc9b35ad | 941 | return true; |
df71837d TJ |
942 | } |
943 | #endif | |
944 | ||
1da177e4 LT |
945 | /* A struct encoding bundle of transformations to apply to some set of flow. |
946 | * | |
b6ca8bd5 | 947 | * xdst->child points to the next element of bundle. |
1da177e4 LT |
948 | * dst->xfrm points to an instanse of transformer. |
949 | * | |
950 | * Due to unfortunate limitations of current routing cache, which we | |
951 | * have no time to fix, it mirrors struct rtable and bound to the same | |
952 | * routing key, including saddr,daddr. However, we can have many of | |
953 | * bundles differing by session id. All the bundles grow from a parent | |
954 | * policy rule. | |
955 | */ | |
fd2c3ef7 | 956 | struct xfrm_dst { |
1da177e4 | 957 | union { |
1da177e4 LT |
958 | struct dst_entry dst; |
959 | struct rtable rt; | |
960 | struct rt6_info rt6; | |
961 | } u; | |
962 | struct dst_entry *route; | |
b6ca8bd5 | 963 | struct dst_entry *child; |
0f6c480f | 964 | struct dst_entry *path; |
80c802f3 TT |
965 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
966 | int num_pols, num_xfrms; | |
80c802f3 TT |
967 | u32 xfrm_genid; |
968 | u32 policy_genid; | |
1da177e4 LT |
969 | u32 route_mtu_cached; |
970 | u32 child_mtu_cached; | |
92d63dec HY |
971 | u32 route_cookie; |
972 | u32 path_cookie; | |
1da177e4 LT |
973 | }; |
974 | ||
0f6c480f DM |
975 | static inline struct dst_entry *xfrm_dst_path(const struct dst_entry *dst) |
976 | { | |
977 | #ifdef CONFIG_XFRM | |
978 | if (dst->xfrm) { | |
979 | const struct xfrm_dst *xdst = (const struct xfrm_dst *) dst; | |
980 | ||
981 | return xdst->path; | |
982 | } | |
983 | #endif | |
984 | return (struct dst_entry *) dst; | |
985 | } | |
986 | ||
b92cf4aa DM |
987 | static inline struct dst_entry *xfrm_dst_child(const struct dst_entry *dst) |
988 | { | |
989 | #ifdef CONFIG_XFRM | |
b6ca8bd5 DM |
990 | if (dst->xfrm) { |
991 | struct xfrm_dst *xdst = (struct xfrm_dst *) dst; | |
992 | return xdst->child; | |
993 | } | |
b92cf4aa DM |
994 | #endif |
995 | return NULL; | |
996 | } | |
997 | ||
def8b4fa | 998 | #ifdef CONFIG_XFRM |
45b018be DM |
999 | static inline void xfrm_dst_set_child(struct xfrm_dst *xdst, struct dst_entry *child) |
1000 | { | |
b6ca8bd5 | 1001 | xdst->child = child; |
45b018be DM |
1002 | } |
1003 | ||
aabc9761 HX |
1004 | static inline void xfrm_dst_destroy(struct xfrm_dst *xdst) |
1005 | { | |
80c802f3 | 1006 | xfrm_pols_put(xdst->pols, xdst->num_pols); |
aabc9761 HX |
1007 | dst_release(xdst->route); |
1008 | if (likely(xdst->u.dst.xfrm)) | |
1009 | xfrm_state_put(xdst->u.dst.xfrm); | |
1010 | } | |
def8b4fa | 1011 | #endif |
aabc9761 | 1012 | |
d511337a | 1013 | void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev); |
aabc9761 | 1014 | |
f203b76d SK |
1015 | struct xfrm_if_parms { |
1016 | char name[IFNAMSIZ]; /* name of XFRM device */ | |
1017 | int link; /* ifindex of underlying L2 interface */ | |
1018 | u32 if_id; /* interface identifyer */ | |
1019 | }; | |
1020 | ||
1021 | struct xfrm_if { | |
1022 | struct xfrm_if __rcu *next; /* next interface in list */ | |
1023 | struct net_device *dev; /* virtual device associated with interface */ | |
1024 | struct net_device *phydev; /* physical device */ | |
1025 | struct net *net; /* netns for packet i/o */ | |
1026 | struct xfrm_if_parms p; /* interface parms */ | |
1027 | ||
1028 | struct gro_cells gro_cells; | |
1029 | }; | |
1030 | ||
54ef207a SK |
1031 | struct xfrm_offload { |
1032 | /* Output sequence number for replay protection on offloading. */ | |
1033 | struct { | |
1034 | __u32 low; | |
1035 | __u32 hi; | |
1036 | } seq; | |
1037 | ||
1038 | __u32 flags; | |
1039 | #define SA_DELETE_REQ 1 | |
1040 | #define CRYPTO_DONE 2 | |
1041 | #define CRYPTO_NEXT_DONE 4 | |
1042 | #define CRYPTO_FALLBACK 8 | |
1043 | #define XFRM_GSO_SEGMENT 16 | |
1044 | #define XFRM_GRO 32 | |
47ebcc0b | 1045 | #define XFRM_ESP_NO_TRAILER 64 |
f53c7239 | 1046 | #define XFRM_DEV_RESUME 128 |
54ef207a SK |
1047 | |
1048 | __u32 status; | |
1049 | #define CRYPTO_SUCCESS 1 | |
1050 | #define CRYPTO_GENERIC_ERROR 2 | |
1051 | #define CRYPTO_TRANSPORT_AH_AUTH_FAILED 4 | |
1052 | #define CRYPTO_TRANSPORT_ESP_AUTH_FAILED 8 | |
1053 | #define CRYPTO_TUNNEL_AH_AUTH_FAILED 16 | |
1054 | #define CRYPTO_TUNNEL_ESP_AUTH_FAILED 32 | |
1055 | #define CRYPTO_INVALID_PACKET_SYNTAX 64 | |
1056 | #define CRYPTO_INVALID_PROTOCOL 128 | |
1057 | ||
1058 | __u8 proto; | |
1059 | }; | |
1060 | ||
fd2c3ef7 | 1061 | struct sec_path { |
1da177e4 | 1062 | int len; |
54ef207a SK |
1063 | int olen; |
1064 | ||
dbe5b4aa | 1065 | struct xfrm_state *xvec[XFRM_MAX_DEPTH]; |
54ef207a | 1066 | struct xfrm_offload ovec[XFRM_MAX_OFFLOAD_DEPTH]; |
1da177e4 LT |
1067 | }; |
1068 | ||
0ca64da1 | 1069 | struct sec_path *secpath_set(struct sk_buff *skb); |
1da177e4 LT |
1070 | |
1071 | static inline void | |
1072 | secpath_reset(struct sk_buff *skb) | |
1073 | { | |
1074 | #ifdef CONFIG_XFRM | |
4165079b | 1075 | skb_ext_del(skb, SKB_EXT_SEC_PATH); |
1da177e4 LT |
1076 | #endif |
1077 | } | |
1078 | ||
a1e59abf | 1079 | static inline int |
6cc32961 | 1080 | xfrm_addr_any(const xfrm_address_t *addr, unsigned short family) |
a1e59abf PM |
1081 | { |
1082 | switch (family) { | |
1083 | case AF_INET: | |
1084 | return addr->a4 == 0; | |
1085 | case AF_INET6: | |
15e318bd | 1086 | return ipv6_addr_any(&addr->in6); |
a1e59abf PM |
1087 | } |
1088 | return 0; | |
1089 | } | |
1090 | ||
1da177e4 | 1091 | static inline int |
21eddb5c | 1092 | __xfrm4_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x) |
1da177e4 LT |
1093 | { |
1094 | return (tmpl->saddr.a4 && | |
1095 | tmpl->saddr.a4 != x->props.saddr.a4); | |
1096 | } | |
1097 | ||
1098 | static inline int | |
21eddb5c | 1099 | __xfrm6_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x) |
1da177e4 LT |
1100 | { |
1101 | return (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) && | |
ff88b30c | 1102 | !ipv6_addr_equal((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr)); |
1da177e4 LT |
1103 | } |
1104 | ||
1105 | static inline int | |
21eddb5c | 1106 | xfrm_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, unsigned short family) |
1da177e4 LT |
1107 | { |
1108 | switch (family) { | |
1109 | case AF_INET: | |
1110 | return __xfrm4_state_addr_cmp(tmpl, x); | |
1111 | case AF_INET6: | |
1112 | return __xfrm6_state_addr_cmp(tmpl, x); | |
1113 | } | |
1114 | return !0; | |
1115 | } | |
1116 | ||
1117 | #ifdef CONFIG_XFRM | |
d511337a JP |
1118 | int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb, |
1119 | unsigned short family); | |
1da177e4 | 1120 | |
d5422efe HX |
1121 | static inline int __xfrm_policy_check2(struct sock *sk, int dir, |
1122 | struct sk_buff *skb, | |
1123 | unsigned int family, int reverse) | |
1da177e4 | 1124 | { |
f6e1e25d | 1125 | struct net *net = dev_net(skb->dev); |
d5422efe HX |
1126 | int ndir = dir | (reverse ? XFRM_POLICY_MASK + 1 : 0); |
1127 | ||
1da177e4 | 1128 | if (sk && sk->sk_policy[XFRM_POLICY_IN]) |
d5422efe | 1129 | return __xfrm_policy_check(sk, ndir, skb, family); |
4e81bb83 | 1130 | |
26912e37 | 1131 | return (!net->xfrm.policy_count[dir] && !secpath_exists(skb)) || |
adf30907 | 1132 | (skb_dst(skb)->flags & DST_NOPOLICY) || |
d5422efe HX |
1133 | __xfrm_policy_check(sk, ndir, skb, family); |
1134 | } | |
1135 | ||
1136 | static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family) | |
1137 | { | |
1138 | return __xfrm_policy_check2(sk, dir, skb, family, 0); | |
1da177e4 LT |
1139 | } |
1140 | ||
1141 | static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb) | |
1142 | { | |
1143 | return xfrm_policy_check(sk, dir, skb, AF_INET); | |
1144 | } | |
1145 | ||
1146 | static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb) | |
1147 | { | |
1148 | return xfrm_policy_check(sk, dir, skb, AF_INET6); | |
1149 | } | |
1150 | ||
d5422efe HX |
1151 | static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir, |
1152 | struct sk_buff *skb) | |
1153 | { | |
1154 | return __xfrm_policy_check2(sk, dir, skb, AF_INET, 1); | |
1155 | } | |
1156 | ||
1157 | static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir, | |
1158 | struct sk_buff *skb) | |
1159 | { | |
1160 | return __xfrm_policy_check2(sk, dir, skb, AF_INET6, 1); | |
1161 | } | |
1162 | ||
d511337a JP |
1163 | int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, |
1164 | unsigned int family, int reverse); | |
d5422efe HX |
1165 | |
1166 | static inline int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, | |
1167 | unsigned int family) | |
1168 | { | |
1169 | return __xfrm_decode_session(skb, fl, family, 0); | |
1170 | } | |
1171 | ||
1172 | static inline int xfrm_decode_session_reverse(struct sk_buff *skb, | |
1173 | struct flowi *fl, | |
1174 | unsigned int family) | |
1175 | { | |
1176 | return __xfrm_decode_session(skb, fl, family, 1); | |
1177 | } | |
1178 | ||
d511337a | 1179 | int __xfrm_route_forward(struct sk_buff *skb, unsigned short family); |
1da177e4 LT |
1180 | |
1181 | static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family) | |
1182 | { | |
99a66657 AD |
1183 | struct net *net = dev_net(skb->dev); |
1184 | ||
1185 | return !net->xfrm.policy_count[XFRM_POLICY_OUT] || | |
adf30907 | 1186 | (skb_dst(skb)->flags & DST_NOXFRM) || |
1da177e4 LT |
1187 | __xfrm_route_forward(skb, family); |
1188 | } | |
1189 | ||
1190 | static inline int xfrm4_route_forward(struct sk_buff *skb) | |
1191 | { | |
1192 | return xfrm_route_forward(skb, AF_INET); | |
1193 | } | |
1194 | ||
1195 | static inline int xfrm6_route_forward(struct sk_buff *skb) | |
1196 | { | |
1197 | return xfrm_route_forward(skb, AF_INET6); | |
1198 | } | |
1199 | ||
d188ba86 | 1200 | int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk); |
1da177e4 | 1201 | |
d188ba86 | 1202 | static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) |
1da177e4 | 1203 | { |
d188ba86 ED |
1204 | sk->sk_policy[0] = NULL; |
1205 | sk->sk_policy[1] = NULL; | |
1206 | if (unlikely(osk->sk_policy[0] || osk->sk_policy[1])) | |
1207 | return __xfrm_sk_clone_policy(sk, osk); | |
1da177e4 LT |
1208 | return 0; |
1209 | } | |
1210 | ||
d511337a | 1211 | int xfrm_policy_delete(struct xfrm_policy *pol, int dir); |
1da177e4 LT |
1212 | |
1213 | static inline void xfrm_sk_free_policy(struct sock *sk) | |
1214 | { | |
d188ba86 ED |
1215 | struct xfrm_policy *pol; |
1216 | ||
1217 | pol = rcu_dereference_protected(sk->sk_policy[0], 1); | |
1218 | if (unlikely(pol != NULL)) { | |
1219 | xfrm_policy_delete(pol, XFRM_POLICY_MAX); | |
1da177e4 LT |
1220 | sk->sk_policy[0] = NULL; |
1221 | } | |
d188ba86 ED |
1222 | pol = rcu_dereference_protected(sk->sk_policy[1], 1); |
1223 | if (unlikely(pol != NULL)) { | |
1224 | xfrm_policy_delete(pol, XFRM_POLICY_MAX+1); | |
1da177e4 LT |
1225 | sk->sk_policy[1] = NULL; |
1226 | } | |
1227 | } | |
1228 | ||
1229 | #else | |
1230 | ||
1231 | static inline void xfrm_sk_free_policy(struct sock *sk) {} | |
d188ba86 | 1232 | static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) { return 0; } |
82695b30 SH |
1233 | static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; } |
1234 | static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; } | |
1da177e4 | 1235 | static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb) |
82695b30 SH |
1236 | { |
1237 | return 1; | |
1238 | } | |
1da177e4 LT |
1239 | static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb) |
1240 | { | |
1241 | return 1; | |
1242 | } | |
1243 | static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family) | |
1244 | { | |
1245 | return 1; | |
1246 | } | |
d5422efe HX |
1247 | static inline int xfrm_decode_session_reverse(struct sk_buff *skb, |
1248 | struct flowi *fl, | |
1249 | unsigned int family) | |
1250 | { | |
1251 | return -ENOSYS; | |
1252 | } | |
1253 | static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir, | |
1254 | struct sk_buff *skb) | |
1255 | { | |
1256 | return 1; | |
1257 | } | |
1258 | static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir, | |
1259 | struct sk_buff *skb) | |
1260 | { | |
1261 | return 1; | |
1262 | } | |
1da177e4 LT |
1263 | #endif |
1264 | ||
1265 | static __inline__ | |
e8a4e377 | 1266 | xfrm_address_t *xfrm_flowi_daddr(const struct flowi *fl, unsigned short family) |
1da177e4 LT |
1267 | { |
1268 | switch (family){ | |
1269 | case AF_INET: | |
7e1dc7b6 | 1270 | return (xfrm_address_t *)&fl->u.ip4.daddr; |
1da177e4 | 1271 | case AF_INET6: |
7e1dc7b6 | 1272 | return (xfrm_address_t *)&fl->u.ip6.daddr; |
1da177e4 LT |
1273 | } |
1274 | return NULL; | |
1275 | } | |
1276 | ||
1277 | static __inline__ | |
e8a4e377 | 1278 | xfrm_address_t *xfrm_flowi_saddr(const struct flowi *fl, unsigned short family) |
1da177e4 LT |
1279 | { |
1280 | switch (family){ | |
1281 | case AF_INET: | |
7e1dc7b6 | 1282 | return (xfrm_address_t *)&fl->u.ip4.saddr; |
1da177e4 | 1283 | case AF_INET6: |
7e1dc7b6 | 1284 | return (xfrm_address_t *)&fl->u.ip6.saddr; |
1da177e4 LT |
1285 | } |
1286 | return NULL; | |
1287 | } | |
1288 | ||
9bb182a7 | 1289 | static __inline__ |
e8a4e377 | 1290 | void xfrm_flowi_addr_get(const struct flowi *fl, |
9bb182a7 YH |
1291 | xfrm_address_t *saddr, xfrm_address_t *daddr, |
1292 | unsigned short family) | |
1293 | { | |
1294 | switch(family) { | |
1295 | case AF_INET: | |
7e1dc7b6 DM |
1296 | memcpy(&saddr->a4, &fl->u.ip4.saddr, sizeof(saddr->a4)); |
1297 | memcpy(&daddr->a4, &fl->u.ip4.daddr, sizeof(daddr->a4)); | |
9bb182a7 YH |
1298 | break; |
1299 | case AF_INET6: | |
15e318bd JB |
1300 | saddr->in6 = fl->u.ip6.saddr; |
1301 | daddr->in6 = fl->u.ip6.daddr; | |
9bb182a7 YH |
1302 | break; |
1303 | } | |
1304 | } | |
1305 | ||
1da177e4 | 1306 | static __inline__ int |
f8848067 DM |
1307 | __xfrm4_state_addr_check(const struct xfrm_state *x, |
1308 | const xfrm_address_t *daddr, const xfrm_address_t *saddr) | |
1da177e4 LT |
1309 | { |
1310 | if (daddr->a4 == x->id.daddr.a4 && | |
1311 | (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4)) | |
1312 | return 1; | |
1313 | return 0; | |
1314 | } | |
1315 | ||
1316 | static __inline__ int | |
f8848067 DM |
1317 | __xfrm6_state_addr_check(const struct xfrm_state *x, |
1318 | const xfrm_address_t *daddr, const xfrm_address_t *saddr) | |
1da177e4 | 1319 | { |
ff88b30c YH |
1320 | if (ipv6_addr_equal((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) && |
1321 | (ipv6_addr_equal((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr) || | |
82695b30 | 1322 | ipv6_addr_any((struct in6_addr *)saddr) || |
1da177e4 LT |
1323 | ipv6_addr_any((struct in6_addr *)&x->props.saddr))) |
1324 | return 1; | |
1325 | return 0; | |
1326 | } | |
1327 | ||
1328 | static __inline__ int | |
f8848067 DM |
1329 | xfrm_state_addr_check(const struct xfrm_state *x, |
1330 | const xfrm_address_t *daddr, const xfrm_address_t *saddr, | |
1da177e4 LT |
1331 | unsigned short family) |
1332 | { | |
1333 | switch (family) { | |
1334 | case AF_INET: | |
1335 | return __xfrm4_state_addr_check(x, daddr, saddr); | |
1336 | case AF_INET6: | |
1337 | return __xfrm6_state_addr_check(x, daddr, saddr); | |
1338 | } | |
1339 | return 0; | |
1340 | } | |
1341 | ||
e53820de | 1342 | static __inline__ int |
f8848067 | 1343 | xfrm_state_addr_flow_check(const struct xfrm_state *x, const struct flowi *fl, |
e53820de MN |
1344 | unsigned short family) |
1345 | { | |
1346 | switch (family) { | |
1347 | case AF_INET: | |
1348 | return __xfrm4_state_addr_check(x, | |
7e1dc7b6 DM |
1349 | (const xfrm_address_t *)&fl->u.ip4.daddr, |
1350 | (const xfrm_address_t *)&fl->u.ip4.saddr); | |
e53820de MN |
1351 | case AF_INET6: |
1352 | return __xfrm6_state_addr_check(x, | |
7e1dc7b6 DM |
1353 | (const xfrm_address_t *)&fl->u.ip6.daddr, |
1354 | (const xfrm_address_t *)&fl->u.ip6.saddr); | |
e53820de MN |
1355 | } |
1356 | return 0; | |
1357 | } | |
1358 | ||
f8848067 | 1359 | static inline int xfrm_state_kern(const struct xfrm_state *x) |
1da177e4 LT |
1360 | { |
1361 | return atomic_read(&x->tunnel_users); | |
1362 | } | |
1363 | ||
5794708f MN |
1364 | static inline int xfrm_id_proto_match(u8 proto, u8 userproto) |
1365 | { | |
dc00a525 MN |
1366 | return (!userproto || proto == userproto || |
1367 | (userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH || | |
1368 | proto == IPPROTO_ESP || | |
1369 | proto == IPPROTO_COMP))); | |
5794708f MN |
1370 | } |
1371 | ||
1da177e4 LT |
1372 | /* |
1373 | * xfrm algorithm information | |
1374 | */ | |
1a6509d9 | 1375 | struct xfrm_algo_aead_info { |
165ecc63 | 1376 | char *geniv; |
1a6509d9 HX |
1377 | u16 icv_truncbits; |
1378 | }; | |
1379 | ||
1da177e4 LT |
1380 | struct xfrm_algo_auth_info { |
1381 | u16 icv_truncbits; | |
1382 | u16 icv_fullbits; | |
1383 | }; | |
1384 | ||
1385 | struct xfrm_algo_encr_info { | |
165ecc63 | 1386 | char *geniv; |
1da177e4 LT |
1387 | u16 blockbits; |
1388 | u16 defkeybits; | |
1389 | }; | |
1390 | ||
1391 | struct xfrm_algo_comp_info { | |
1392 | u16 threshold; | |
1393 | }; | |
1394 | ||
1395 | struct xfrm_algo_desc { | |
1396 | char *name; | |
04ff1260 | 1397 | char *compat; |
1da177e4 | 1398 | u8 available:1; |
7e50f84c | 1399 | u8 pfkey_supported:1; |
1da177e4 | 1400 | union { |
1a6509d9 | 1401 | struct xfrm_algo_aead_info aead; |
1da177e4 LT |
1402 | struct xfrm_algo_auth_info auth; |
1403 | struct xfrm_algo_encr_info encr; | |
1404 | struct xfrm_algo_comp_info comp; | |
1405 | } uinfo; | |
1406 | struct sadb_alg desc; | |
1407 | }; | |
1408 | ||
3328715e SK |
1409 | /* XFRM protocol handlers. */ |
1410 | struct xfrm4_protocol { | |
1411 | int (*handler)(struct sk_buff *skb); | |
1412 | int (*input_handler)(struct sk_buff *skb, int nexthdr, __be32 spi, | |
1413 | int encap_type); | |
1414 | int (*cb_handler)(struct sk_buff *skb, int err); | |
1415 | int (*err_handler)(struct sk_buff *skb, u32 info); | |
1416 | ||
1417 | struct xfrm4_protocol __rcu *next; | |
1418 | int priority; | |
1419 | }; | |
1420 | ||
7e14ea15 | 1421 | struct xfrm6_protocol { |
1da177e4 | 1422 | int (*handler)(struct sk_buff *skb); |
7e14ea15 SK |
1423 | int (*cb_handler)(struct sk_buff *skb, int err); |
1424 | int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
1425 | u8 type, u8 code, int offset, __be32 info); | |
d2acc347 | 1426 | |
7e14ea15 | 1427 | struct xfrm6_protocol __rcu *next; |
d2acc347 | 1428 | int priority; |
1da177e4 LT |
1429 | }; |
1430 | ||
1da177e4 LT |
1431 | /* XFRM tunnel handlers. */ |
1432 | struct xfrm_tunnel { | |
aba82695 | 1433 | int (*handler)(struct sk_buff *skb); |
a6337463 | 1434 | int (*err_handler)(struct sk_buff *skb, u32 info); |
d2acc347 | 1435 | |
b33eab08 | 1436 | struct xfrm_tunnel __rcu *next; |
aba82695 FD |
1437 | int priority; |
1438 | }; | |
1439 | ||
1da177e4 | 1440 | struct xfrm6_tunnel { |
d2acc347 HX |
1441 | int (*handler)(struct sk_buff *skb); |
1442 | int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
d5fdd6ba | 1443 | u8 type, u8 code, int offset, __be32 info); |
6f0bcf15 | 1444 | struct xfrm6_tunnel __rcu *next; |
d2acc347 | 1445 | int priority; |
1da177e4 LT |
1446 | }; |
1447 | ||
d511337a JP |
1448 | void xfrm_init(void); |
1449 | void xfrm4_init(void); | |
1450 | int xfrm_state_init(struct net *net); | |
1451 | void xfrm_state_fini(struct net *net); | |
1452 | void xfrm4_state_init(void); | |
2f32b51b | 1453 | void xfrm4_protocol_init(void); |
c35b7e72 | 1454 | #ifdef CONFIG_XFRM |
d511337a JP |
1455 | int xfrm6_init(void); |
1456 | void xfrm6_fini(void); | |
1457 | int xfrm6_state_init(void); | |
1458 | void xfrm6_state_fini(void); | |
7e14ea15 SK |
1459 | int xfrm6_protocol_init(void); |
1460 | void xfrm6_protocol_fini(void); | |
c35b7e72 DL |
1461 | #else |
1462 | static inline int xfrm6_init(void) | |
1463 | { | |
1464 | return 0; | |
1465 | } | |
1466 | static inline void xfrm6_fini(void) | |
1467 | { | |
1468 | ; | |
1469 | } | |
1470 | #endif | |
1da177e4 | 1471 | |
558f82ef | 1472 | #ifdef CONFIG_XFRM_STATISTICS |
d511337a JP |
1473 | int xfrm_proc_init(struct net *net); |
1474 | void xfrm_proc_fini(struct net *net); | |
558f82ef MN |
1475 | #endif |
1476 | ||
d511337a | 1477 | int xfrm_sysctl_init(struct net *net); |
b27aeadb | 1478 | #ifdef CONFIG_SYSCTL |
d511337a | 1479 | void xfrm_sysctl_fini(struct net *net); |
b27aeadb AD |
1480 | #else |
1481 | static inline void xfrm_sysctl_fini(struct net *net) | |
1482 | { | |
1483 | } | |
1484 | #endif | |
1485 | ||
d3623099 | 1486 | void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto, |
870a2df4 | 1487 | struct xfrm_address_filter *filter); |
d511337a JP |
1488 | int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk, |
1489 | int (*func)(struct xfrm_state *, int, void*), void *); | |
283bc9f3 | 1490 | void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net); |
d511337a | 1491 | struct xfrm_state *xfrm_state_alloc(struct net *net); |
4a135e53 | 1492 | void xfrm_state_free(struct xfrm_state *x); |
d511337a JP |
1493 | struct xfrm_state *xfrm_state_find(const xfrm_address_t *daddr, |
1494 | const xfrm_address_t *saddr, | |
1495 | const struct flowi *fl, | |
1496 | struct xfrm_tmpl *tmpl, | |
1497 | struct xfrm_policy *pol, int *err, | |
bc56b334 | 1498 | unsigned short family, u32 if_id); |
7e652640 | 1499 | struct xfrm_state *xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id, |
d511337a JP |
1500 | xfrm_address_t *daddr, |
1501 | xfrm_address_t *saddr, | |
1502 | unsigned short family, | |
1503 | u8 mode, u8 proto, u32 reqid); | |
c454997e FD |
1504 | struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi, |
1505 | unsigned short family); | |
d511337a JP |
1506 | int xfrm_state_check_expire(struct xfrm_state *x); |
1507 | void xfrm_state_insert(struct xfrm_state *x); | |
1508 | int xfrm_state_add(struct xfrm_state *x); | |
1509 | int xfrm_state_update(struct xfrm_state *x); | |
1510 | struct xfrm_state *xfrm_state_lookup(struct net *net, u32 mark, | |
1511 | const xfrm_address_t *daddr, __be32 spi, | |
1512 | u8 proto, unsigned short family); | |
1513 | struct xfrm_state *xfrm_state_lookup_byaddr(struct net *net, u32 mark, | |
1514 | const xfrm_address_t *daddr, | |
1515 | const xfrm_address_t *saddr, | |
1516 | u8 proto, | |
1517 | unsigned short family); | |
41a49cc3 | 1518 | #ifdef CONFIG_XFRM_SUB_POLICY |
d511337a | 1519 | int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n, |
283bc9f3 | 1520 | unsigned short family, struct net *net); |
d511337a JP |
1521 | int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n, |
1522 | unsigned short family); | |
41a49cc3 MN |
1523 | #else |
1524 | static inline int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, | |
283bc9f3 | 1525 | int n, unsigned short family, struct net *net) |
41a49cc3 MN |
1526 | { |
1527 | return -ENOSYS; | |
1528 | } | |
1529 | ||
1530 | static inline int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, | |
1531 | int n, unsigned short family) | |
1532 | { | |
1533 | return -ENOSYS; | |
1534 | } | |
1535 | #endif | |
af11e316 JHS |
1536 | |
1537 | struct xfrmk_sadinfo { | |
1538 | u32 sadhcnt; /* current hash bkts */ | |
1539 | u32 sadhmcnt; /* max allowed hash bkts */ | |
1540 | u32 sadcnt; /* current running count */ | |
1541 | }; | |
1542 | ||
5a6d3416 JHS |
1543 | struct xfrmk_spdinfo { |
1544 | u32 incnt; | |
1545 | u32 outcnt; | |
1546 | u32 fwdcnt; | |
1547 | u32 inscnt; | |
1548 | u32 outscnt; | |
1549 | u32 fwdscnt; | |
1550 | u32 spdhcnt; | |
1551 | u32 spdhmcnt; | |
1552 | }; | |
1553 | ||
d511337a JP |
1554 | struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq); |
1555 | int xfrm_state_delete(struct xfrm_state *x); | |
f75a2804 | 1556 | int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync); |
d77e38e6 | 1557 | int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid); |
d511337a JP |
1558 | void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si); |
1559 | void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si); | |
1560 | u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq); | |
1561 | int xfrm_init_replay(struct xfrm_state *x); | |
1562 | int xfrm_state_mtu(struct xfrm_state *x, int mtu); | |
ffdb5211 | 1563 | int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload); |
d511337a | 1564 | int xfrm_init_state(struct xfrm_state *x); |
d511337a JP |
1565 | int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type); |
1566 | int xfrm_input_resume(struct sk_buff *skb, int nexthdr); | |
acf568ee HX |
1567 | int xfrm_trans_queue(struct sk_buff *skb, |
1568 | int (*finish)(struct net *, struct sock *, | |
1569 | struct sk_buff *)); | |
d511337a | 1570 | int xfrm_output_resume(struct sk_buff *skb, int err); |
7026b1dd | 1571 | int xfrm_output(struct sock *sk, struct sk_buff *skb); |
0c620e97 FW |
1572 | |
1573 | #if IS_ENABLED(CONFIG_NET_PKTGEN) | |
1574 | int pktgen_xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb); | |
1575 | #endif | |
1576 | ||
d511337a JP |
1577 | void xfrm_local_error(struct sk_buff *skb, int mtu); |
1578 | int xfrm4_extract_header(struct sk_buff *skb); | |
1579 | int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb); | |
1580 | int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi, | |
1581 | int encap_type); | |
1582 | int xfrm4_transport_finish(struct sk_buff *skb, int async); | |
1583 | int xfrm4_rcv(struct sk_buff *skb); | |
1e295370 | 1584 | int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq); |
c4541b41 HX |
1585 | |
1586 | static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi) | |
1587 | { | |
70be6c91 | 1588 | XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL; |
3328715e SK |
1589 | XFRM_SPI_SKB_CB(skb)->family = AF_INET; |
1590 | XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr); | |
1591 | return xfrm_input(skb, nexthdr, spi, 0); | |
c4541b41 HX |
1592 | } |
1593 | ||
d511337a | 1594 | int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb); |
ede2059d | 1595 | int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb); |
7026b1dd | 1596 | int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb); |
3328715e SK |
1597 | int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err); |
1598 | int xfrm4_protocol_register(struct xfrm4_protocol *handler, unsigned char protocol); | |
1599 | int xfrm4_protocol_deregister(struct xfrm4_protocol *handler, unsigned char protocol); | |
d511337a JP |
1600 | int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family); |
1601 | int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family); | |
d511337a JP |
1602 | void xfrm4_local_error(struct sk_buff *skb, u32 mtu); |
1603 | int xfrm6_extract_header(struct sk_buff *skb); | |
1604 | int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb); | |
63c43787 ND |
1605 | int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi, |
1606 | struct ip6_tnl *t); | |
d511337a | 1607 | int xfrm6_transport_finish(struct sk_buff *skb, int async); |
63c43787 | 1608 | int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t); |
d511337a JP |
1609 | int xfrm6_rcv(struct sk_buff *skb); |
1610 | int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr, | |
1611 | xfrm_address_t *saddr, u8 proto); | |
7b77d161 | 1612 | void xfrm6_local_error(struct sk_buff *skb, u32 mtu); |
7e14ea15 SK |
1613 | int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err); |
1614 | int xfrm6_protocol_register(struct xfrm6_protocol *handler, unsigned char protocol); | |
1615 | int xfrm6_protocol_deregister(struct xfrm6_protocol *handler, unsigned char protocol); | |
d511337a | 1616 | int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family); |
7b77d161 | 1617 | int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family); |
d511337a JP |
1618 | __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr); |
1619 | __be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr); | |
1620 | int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb); | |
ede2059d | 1621 | int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb); |
7026b1dd | 1622 | int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb); |
d511337a JP |
1623 | int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb, |
1624 | u8 **prevhdr); | |
1da177e4 LT |
1625 | |
1626 | #ifdef CONFIG_XFRM | |
d511337a JP |
1627 | int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb); |
1628 | int xfrm_user_policy(struct sock *sk, int optname, | |
1629 | u8 __user *optval, int optlen); | |
1da177e4 LT |
1630 | #else |
1631 | static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) | |
1632 | { | |
1633 | return -ENOPROTOOPT; | |
82695b30 | 1634 | } |
1da177e4 | 1635 | |
067b207b | 1636 | static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb) |
1da177e4 LT |
1637 | { |
1638 | /* should not happen */ | |
1639 | kfree_skb(skb); | |
1640 | return 0; | |
1641 | } | |
1da177e4 LT |
1642 | #endif |
1643 | ||
d77e38e6 SK |
1644 | struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif, |
1645 | const xfrm_address_t *saddr, | |
1646 | const xfrm_address_t *daddr, | |
077fbac4 | 1647 | int family, u32 mark); |
d77e38e6 | 1648 | |
0331b1f3 | 1649 | struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp); |
4c563f76 | 1650 | |
d511337a JP |
1651 | void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type); |
1652 | int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk, | |
1653 | int (*func)(struct xfrm_policy *, int, int, void*), | |
1654 | void *); | |
283bc9f3 | 1655 | void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net); |
1da177e4 | 1656 | int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl); |
7e652640 | 1657 | struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id, |
8ca2e93b | 1658 | u8 type, int dir, |
4e81bb83 | 1659 | struct xfrm_selector *sel, |
ef41aaa0 EP |
1660 | struct xfrm_sec_ctx *ctx, int delete, |
1661 | int *err); | |
7e652640 SK |
1662 | struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id, u8, |
1663 | int dir, u32 id, int delete, int *err); | |
2e71029e | 1664 | int xfrm_policy_flush(struct net *net, u8 type, bool task_valid); |
880a6fab | 1665 | void xfrm_policy_hash_rebuild(struct net *net); |
1da177e4 | 1666 | u32 xfrm_get_acqseq(void); |
776e9dd9 | 1667 | int verify_spi_info(u8 proto, u32 min, u32 max); |
d511337a | 1668 | int xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi); |
e473fcb4 | 1669 | struct xfrm_state *xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, |
7e652640 | 1670 | u8 mode, u32 reqid, u32 if_id, u8 proto, |
a70486f0 DM |
1671 | const xfrm_address_t *daddr, |
1672 | const xfrm_address_t *saddr, int create, | |
bd55775c | 1673 | unsigned short family); |
d511337a | 1674 | int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol); |
1da177e4 | 1675 | |
80c9abaa | 1676 | #ifdef CONFIG_XFRM_MIGRATE |
d511337a JP |
1677 | int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, |
1678 | const struct xfrm_migrate *m, int num_bundles, | |
8bafd730 AA |
1679 | const struct xfrm_kmaddress *k, |
1680 | const struct xfrm_encap_tmpl *encap); | |
283bc9f3 | 1681 | struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net); |
d511337a | 1682 | struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x, |
4ab47d47 AA |
1683 | struct xfrm_migrate *m, |
1684 | struct xfrm_encap_tmpl *encap); | |
d511337a JP |
1685 | int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, |
1686 | struct xfrm_migrate *m, int num_bundles, | |
4ab47d47 AA |
1687 | struct xfrm_kmaddress *k, struct net *net, |
1688 | struct xfrm_encap_tmpl *encap); | |
80c9abaa SS |
1689 | #endif |
1690 | ||
d511337a JP |
1691 | int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport); |
1692 | void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid); | |
1693 | int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, | |
1694 | xfrm_address_t *addr); | |
1695 | ||
1696 | void xfrm_input_init(void); | |
1697 | int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq); | |
1698 | ||
1699 | void xfrm_probe_algs(void); | |
1700 | int xfrm_count_pfkey_auth_supported(void); | |
1701 | int xfrm_count_pfkey_enc_supported(void); | |
1702 | struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx); | |
1703 | struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx); | |
1704 | struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id); | |
1705 | struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id); | |
1706 | struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id); | |
1707 | struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe); | |
1708 | struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe); | |
1709 | struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe); | |
1710 | struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len, | |
1711 | int probe); | |
1da177e4 | 1712 | |
70e94e66 YH |
1713 | static inline bool xfrm6_addr_equal(const xfrm_address_t *a, |
1714 | const xfrm_address_t *b) | |
1715 | { | |
1716 | return ipv6_addr_equal((const struct in6_addr *)a, | |
1717 | (const struct in6_addr *)b); | |
1718 | } | |
1719 | ||
1720 | static inline bool xfrm_addr_equal(const xfrm_address_t *a, | |
1721 | const xfrm_address_t *b, | |
1722 | sa_family_t family) | |
1da177e4 LT |
1723 | { |
1724 | switch (family) { | |
1725 | default: | |
1726 | case AF_INET: | |
70e94e66 | 1727 | return ((__force u32)a->a4 ^ (__force u32)b->a4) == 0; |
1da177e4 | 1728 | case AF_INET6: |
70e94e66 | 1729 | return xfrm6_addr_equal(a, b); |
1da177e4 LT |
1730 | } |
1731 | } | |
1732 | ||
77d8d7a6 HX |
1733 | static inline int xfrm_policy_id2dir(u32 index) |
1734 | { | |
1735 | return index & 7; | |
1736 | } | |
1737 | ||
a6483b79 AD |
1738 | #ifdef CONFIG_XFRM |
1739 | static inline int xfrm_aevent_is_on(struct net *net) | |
f8cd5488 | 1740 | { |
be33690d PM |
1741 | struct sock *nlsk; |
1742 | int ret = 0; | |
1743 | ||
1744 | rcu_read_lock(); | |
a6483b79 | 1745 | nlsk = rcu_dereference(net->xfrm.nlsk); |
be33690d PM |
1746 | if (nlsk) |
1747 | ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS); | |
1748 | rcu_read_unlock(); | |
1749 | return ret; | |
f8cd5488 | 1750 | } |
0f24558e HG |
1751 | |
1752 | static inline int xfrm_acquire_is_on(struct net *net) | |
1753 | { | |
1754 | struct sock *nlsk; | |
1755 | int ret = 0; | |
1756 | ||
1757 | rcu_read_lock(); | |
1758 | nlsk = rcu_dereference(net->xfrm.nlsk); | |
1759 | if (nlsk) | |
1760 | ret = netlink_has_listeners(nlsk, XFRMNLGRP_ACQUIRE); | |
1761 | rcu_read_unlock(); | |
1762 | ||
1763 | return ret; | |
1764 | } | |
a6483b79 | 1765 | #endif |
f8cd5488 | 1766 | |
373b8eeb | 1767 | static inline unsigned int aead_len(struct xfrm_algo_aead *alg) |
ee5c2317 SK |
1768 | { |
1769 | return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); | |
1770 | } | |
1771 | ||
06cd22f8 | 1772 | static inline unsigned int xfrm_alg_len(const struct xfrm_algo *alg) |
0f99be0d ED |
1773 | { |
1774 | return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); | |
1775 | } | |
1776 | ||
1bd963a7 | 1777 | static inline unsigned int xfrm_alg_auth_len(const struct xfrm_algo_auth *alg) |
4447bb33 MW |
1778 | { |
1779 | return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); | |
1780 | } | |
1781 | ||
5e708e47 | 1782 | static inline unsigned int xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn) |
9736acf3 SK |
1783 | { |
1784 | return sizeof(*replay_esn) + replay_esn->bmp_len * sizeof(__u32); | |
1785 | } | |
1786 | ||
80c9abaa | 1787 | #ifdef CONFIG_XFRM_MIGRATE |
af2f464e SK |
1788 | static inline int xfrm_replay_clone(struct xfrm_state *x, |
1789 | struct xfrm_state *orig) | |
1790 | { | |
1791 | x->replay_esn = kzalloc(xfrm_replay_state_esn_len(orig->replay_esn), | |
1792 | GFP_KERNEL); | |
1793 | if (!x->replay_esn) | |
1794 | return -ENOMEM; | |
1795 | ||
1796 | x->replay_esn->bmp_len = orig->replay_esn->bmp_len; | |
1797 | x->replay_esn->replay_window = orig->replay_esn->replay_window; | |
1798 | ||
1799 | x->preplay_esn = kmemdup(x->replay_esn, | |
1800 | xfrm_replay_state_esn_len(x->replay_esn), | |
1801 | GFP_KERNEL); | |
1802 | if (!x->preplay_esn) { | |
1803 | kfree(x->replay_esn); | |
1804 | return -ENOMEM; | |
1805 | } | |
1806 | ||
1807 | return 0; | |
1808 | } | |
1809 | ||
ee5c2317 SK |
1810 | static inline struct xfrm_algo_aead *xfrm_algo_aead_clone(struct xfrm_algo_aead *orig) |
1811 | { | |
1812 | return kmemdup(orig, aead_len(orig), GFP_KERNEL); | |
1813 | } | |
1814 | ||
1815 | ||
80c9abaa SS |
1816 | static inline struct xfrm_algo *xfrm_algo_clone(struct xfrm_algo *orig) |
1817 | { | |
0f99be0d | 1818 | return kmemdup(orig, xfrm_alg_len(orig), GFP_KERNEL); |
80c9abaa SS |
1819 | } |
1820 | ||
4447bb33 MW |
1821 | static inline struct xfrm_algo_auth *xfrm_algo_auth_clone(struct xfrm_algo_auth *orig) |
1822 | { | |
1823 | return kmemdup(orig, xfrm_alg_auth_len(orig), GFP_KERNEL); | |
1824 | } | |
1825 | ||
80c9abaa SS |
1826 | static inline void xfrm_states_put(struct xfrm_state **states, int n) |
1827 | { | |
1828 | int i; | |
1829 | for (i = 0; i < n; i++) | |
1830 | xfrm_state_put(*(states + i)); | |
1831 | } | |
1832 | ||
1833 | static inline void xfrm_states_delete(struct xfrm_state **states, int n) | |
1834 | { | |
1835 | int i; | |
1836 | for (i = 0; i < n; i++) | |
1837 | xfrm_state_delete(*(states + i)); | |
1838 | } | |
1839 | #endif | |
f8cd5488 | 1840 | |
def8b4fa | 1841 | #ifdef CONFIG_XFRM |
00501121 HX |
1842 | static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb) |
1843 | { | |
2294be0f FW |
1844 | struct sec_path *sp = skb_sec_path(skb); |
1845 | ||
1846 | return sp->xvec[sp->len - 1]; | |
00501121 | 1847 | } |
f53c7239 SK |
1848 | #endif |
1849 | ||
54ef207a SK |
1850 | static inline struct xfrm_offload *xfrm_offload(struct sk_buff *skb) |
1851 | { | |
f53c7239 | 1852 | #ifdef CONFIG_XFRM |
2294be0f | 1853 | struct sec_path *sp = skb_sec_path(skb); |
54ef207a SK |
1854 | |
1855 | if (!sp || !sp->olen || sp->len != sp->olen) | |
1856 | return NULL; | |
1857 | ||
1858 | return &sp->ovec[sp->olen - 1]; | |
f53c7239 SK |
1859 | #else |
1860 | return NULL; | |
def8b4fa | 1861 | #endif |
f53c7239 | 1862 | } |
00501121 | 1863 | |
e9a441b6 | 1864 | void __init xfrm_dev_init(void); |
b81f884a HL |
1865 | |
1866 | #ifdef CONFIG_XFRM_OFFLOAD | |
f53c7239 SK |
1867 | void xfrm_dev_resume(struct sk_buff *skb); |
1868 | void xfrm_dev_backlog(struct softnet_data *sd); | |
1869 | struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again); | |
d77e38e6 SK |
1870 | int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, |
1871 | struct xfrm_user_offload *xuo); | |
1872 | bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x); | |
1873 | ||
50bd870a YE |
1874 | static inline void xfrm_dev_state_advance_esn(struct xfrm_state *x) |
1875 | { | |
1876 | struct xfrm_state_offload *xso = &x->xso; | |
1877 | ||
1878 | if (xso->dev && xso->dev->xfrmdev_ops->xdo_dev_state_advance_esn) | |
1879 | xso->dev->xfrmdev_ops->xdo_dev_state_advance_esn(x); | |
1880 | } | |
1881 | ||
f70f250a SK |
1882 | static inline bool xfrm_dst_offload_ok(struct dst_entry *dst) |
1883 | { | |
1884 | struct xfrm_state *x = dst->xfrm; | |
b6ca8bd5 | 1885 | struct xfrm_dst *xdst; |
f70f250a SK |
1886 | |
1887 | if (!x || !x->type_offload) | |
1888 | return false; | |
1889 | ||
b6ca8bd5 | 1890 | xdst = (struct xfrm_dst *) dst; |
2271d519 SK |
1891 | if (!x->xso.offload_handle && !xdst->child->xfrm) |
1892 | return true; | |
0f6c480f | 1893 | if (x->xso.offload_handle && (x->xso.dev == xfrm_dst_path(dst)->dev) && |
b6ca8bd5 | 1894 | !xdst->child->xfrm) |
f70f250a SK |
1895 | return true; |
1896 | ||
1897 | return false; | |
1898 | } | |
1899 | ||
d77e38e6 SK |
1900 | static inline void xfrm_dev_state_delete(struct xfrm_state *x) |
1901 | { | |
1902 | struct xfrm_state_offload *xso = &x->xso; | |
1903 | ||
1904 | if (xso->dev) | |
1905 | xso->dev->xfrmdev_ops->xdo_dev_state_delete(x); | |
1906 | } | |
1907 | ||
1908 | static inline void xfrm_dev_state_free(struct xfrm_state *x) | |
1909 | { | |
1910 | struct xfrm_state_offload *xso = &x->xso; | |
77990464 | 1911 | struct net_device *dev = xso->dev; |
d77e38e6 SK |
1912 | |
1913 | if (dev && dev->xfrmdev_ops) { | |
7f05b467 SN |
1914 | if (dev->xfrmdev_ops->xdo_dev_state_free) |
1915 | dev->xfrmdev_ops->xdo_dev_state_free(x); | |
d77e38e6 SK |
1916 | xso->dev = NULL; |
1917 | dev_put(dev); | |
1918 | } | |
1919 | } | |
1920 | #else | |
f53c7239 SK |
1921 | static inline void xfrm_dev_resume(struct sk_buff *skb) |
1922 | { | |
1923 | } | |
1924 | ||
1925 | static inline void xfrm_dev_backlog(struct softnet_data *sd) | |
1926 | { | |
1927 | } | |
1928 | ||
1929 | static inline struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again) | |
f6e27114 | 1930 | { |
3dca3f38 | 1931 | return skb; |
f6e27114 SK |
1932 | } |
1933 | ||
d77e38e6 SK |
1934 | static inline int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, struct xfrm_user_offload *xuo) |
1935 | { | |
1936 | return 0; | |
1937 | } | |
1938 | ||
1939 | static inline void xfrm_dev_state_delete(struct xfrm_state *x) | |
1940 | { | |
1941 | } | |
1942 | ||
1943 | static inline void xfrm_dev_state_free(struct xfrm_state *x) | |
1944 | { | |
1945 | } | |
1946 | ||
1947 | static inline bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x) | |
1948 | { | |
1949 | return false; | |
1950 | } | |
f70f250a | 1951 | |
50bd870a YE |
1952 | static inline void xfrm_dev_state_advance_esn(struct xfrm_state *x) |
1953 | { | |
1954 | } | |
1955 | ||
f70f250a SK |
1956 | static inline bool xfrm_dst_offload_ok(struct dst_entry *dst) |
1957 | { | |
1958 | return false; | |
1959 | } | |
d77e38e6 SK |
1960 | #endif |
1961 | ||
bf825f81 JHS |
1962 | static inline int xfrm_mark_get(struct nlattr **attrs, struct xfrm_mark *m) |
1963 | { | |
1964 | if (attrs[XFRMA_MARK]) | |
4efd7e83 | 1965 | memcpy(m, nla_data(attrs[XFRMA_MARK]), sizeof(struct xfrm_mark)); |
bf825f81 JHS |
1966 | else |
1967 | m->v = m->m = 0; | |
1968 | ||
1969 | return m->v & m->m; | |
1970 | } | |
1971 | ||
e3dfa389 | 1972 | static inline int xfrm_mark_put(struct sk_buff *skb, const struct xfrm_mark *m) |
bf825f81 | 1973 | { |
1d1e34dd | 1974 | int ret = 0; |
bf825f81 | 1975 | |
1d1e34dd DM |
1976 | if (m->m | m->v) |
1977 | ret = nla_put(skb, XFRMA_MARK, sizeof(struct xfrm_mark), m); | |
1978 | return ret; | |
bf825f81 JHS |
1979 | } |
1980 | ||
9b42c1f1 SK |
1981 | static inline __u32 xfrm_smark_get(__u32 mark, struct xfrm_state *x) |
1982 | { | |
1983 | struct xfrm_mark *m = &x->props.smark; | |
1984 | ||
1985 | return (m->v & m->m) | (mark & ~m->m); | |
1986 | } | |
1987 | ||
7e652640 SK |
1988 | static inline int xfrm_if_id_put(struct sk_buff *skb, __u32 if_id) |
1989 | { | |
1990 | int ret = 0; | |
1991 | ||
1992 | if (if_id) | |
1993 | ret = nla_put_u32(skb, XFRMA_IF_ID, if_id); | |
1994 | return ret; | |
1995 | } | |
1996 | ||
70be6c91 SK |
1997 | static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x, |
1998 | unsigned int family) | |
1999 | { | |
2000 | bool tunnel = false; | |
2001 | ||
2002 | switch(family) { | |
2003 | case AF_INET: | |
2004 | if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4) | |
2005 | tunnel = true; | |
2006 | break; | |
2007 | case AF_INET6: | |
2008 | if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6) | |
2009 | tunnel = true; | |
2010 | break; | |
2011 | } | |
2012 | if (tunnel && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL)) | |
2013 | return -EINVAL; | |
2014 | ||
2015 | return 0; | |
2016 | } | |
1da177e4 | 2017 | #endif /* _NET_XFRM_H */ |