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