xref: /linux/include/net/xfrm.h (revision bb65a0da99bf4861dce01149ba64e875e47ae5a3)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_XFRM_H
3 #define _NET_XFRM_H
4 
5 #include <linux/compiler.h>
6 #include <linux/xfrm.h>
7 #include <linux/spinlock.h>
8 #include <linux/list.h>
9 #include <linux/skbuff.h>
10 #include <linux/socket.h>
11 #include <linux/pfkeyv2.h>
12 #include <linux/ipsec.h>
13 #include <linux/in6.h>
14 #include <linux/mutex.h>
15 #include <linux/audit.h>
16 #include <linux/slab.h>
17 #include <linux/refcount.h>
18 #include <linux/sockptr.h>
19 
20 #include <net/sock.h>
21 #include <net/dst.h>
22 #include <net/inet_dscp.h>
23 #include <net/ip.h>
24 #include <net/route.h>
25 #include <net/ipv6.h>
26 #include <net/ip6_fib.h>
27 #include <net/flow.h>
28 #include <net/gro_cells.h>
29 
30 #include <linux/interrupt.h>
31 
32 #ifdef CONFIG_XFRM_STATISTICS
33 #include <net/snmp.h>
34 #endif
35 
36 #define XFRM_PROTO_ESP		50
37 #define XFRM_PROTO_AH		51
38 #define XFRM_PROTO_COMP		108
39 #define XFRM_PROTO_IPIP		4
40 #define XFRM_PROTO_IPV6		41
41 #define XFRM_PROTO_IPTFS	IPPROTO_AGGFRAG
42 #define XFRM_PROTO_ROUTING	IPPROTO_ROUTING
43 #define XFRM_PROTO_DSTOPTS	IPPROTO_DSTOPTS
44 
45 #define XFRM_ALIGN4(len)	(((len) + 3) & ~3)
46 #define XFRM_ALIGN8(len)	(((len) + 7) & ~7)
47 #define MODULE_ALIAS_XFRM_MODE(family, encap) \
48 	MODULE_ALIAS("xfrm-mode-" __stringify(family) "-" __stringify(encap))
49 #define MODULE_ALIAS_XFRM_TYPE(family, proto) \
50 	MODULE_ALIAS("xfrm-type-" __stringify(family) "-" __stringify(proto))
51 #define MODULE_ALIAS_XFRM_OFFLOAD_TYPE(family, proto) \
52 	MODULE_ALIAS("xfrm-offload-" __stringify(family) "-" __stringify(proto))
53 
54 #ifdef CONFIG_XFRM_STATISTICS
55 #define XFRM_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.xfrm_statistics, field)
56 #define XFRM_ADD_STATS(net, field, val) SNMP_ADD_STATS((net)->mib.xfrm_statistics, field, val)
57 #else
58 #define XFRM_INC_STATS(net, field)	((void)(net))
59 #define XFRM_ADD_STATS(net, field, val) ((void)(net))
60 #endif
61 
62 
63 /* Organization of SPD aka "XFRM rules"
64    ------------------------------------
65 
66    Basic objects:
67    - policy rule, struct xfrm_policy (=SPD entry)
68    - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle)
69    - instance of a transformer, struct xfrm_state (=SA)
70    - template to clone xfrm_state, struct xfrm_tmpl
71 
72    SPD is organized as hash table (for policies that meet minimum address prefix
73    length setting, net->xfrm.policy_hthresh).  Other policies are stored in
74    lists, sorted into rbtree ordered by destination and source address networks.
75    See net/xfrm/xfrm_policy.c for details.
76 
77    (To be compatible with existing pfkeyv2 implementations,
78    many rules with priority of 0x7fffffff are allowed to exist and
79    such rules are ordered in an unpredictable way, thanks to bsd folks.)
80 
81    If "action" is "block", then we prohibit the flow, otherwise:
82    if "xfrms_nr" is zero, the flow passes untransformed. Otherwise,
83    policy entry has list of up to XFRM_MAX_DEPTH transformations,
84    described by templates xfrm_tmpl. Each template is resolved
85    to a complete xfrm_state (see below) and we pack bundle of transformations
86    to a dst_entry returned to requester.
87 
88    dst -. xfrm  .-> xfrm_state #1
89     |---. child .-> dst -. xfrm .-> xfrm_state #2
90                      |---. child .-> dst -. xfrm .-> xfrm_state #3
91                                       |---. child .-> NULL
92 
93 
94    Resolution of xrfm_tmpl
95    -----------------------
96    Template contains:
97    1. ->mode		Mode: transport or tunnel
98    2. ->id.proto	Protocol: AH/ESP/IPCOMP
99    3. ->id.daddr	Remote tunnel endpoint, ignored for transport mode.
100       Q: allow to resolve security gateway?
101    4. ->id.spi          If not zero, static SPI.
102    5. ->saddr		Local tunnel endpoint, ignored for transport mode.
103    6. ->algos		List of allowed algos. Plain bitmask now.
104       Q: ealgos, aalgos, calgos. What a mess...
105    7. ->share		Sharing mode.
106       Q: how to implement private sharing mode? To add struct sock* to
107       flow id?
108 
109    Having this template we search through SAD searching for entries
110    with appropriate mode/proto/algo, permitted by selector.
111    If no appropriate entry found, it is requested from key manager.
112 
113    PROBLEMS:
114    Q: How to find all the bundles referring to a physical path for
115       PMTU discovery? Seems, dst should contain list of all parents...
116       and enter to infinite locking hierarchy disaster.
117       No! It is easier, we will not search for them, let them find us.
118       We add genid to each dst plus pointer to genid of raw IP route,
119       pmtu disc will update pmtu on raw IP route and increase its genid.
120       dst_check() will see this for top level and trigger resyncing
121       metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
122  */
123 
124 struct xfrm_state_walk {
125 	struct list_head	all;
126 	u8			state;
127 	u8			dying;
128 	u8			proto;
129 	u32			seq;
130 	struct xfrm_address_filter *filter;
131 };
132 
133 #define XFRM_STATE_WALK_BATCH_SIZE	32
134 
135 struct xfrm_state_walk_batch {
136 	struct xfrm_state	*states[XFRM_STATE_WALK_BATCH_SIZE];
137 	unsigned int		count;
138 };
139 
140 enum {
141 	XFRM_DEV_OFFLOAD_IN = 1,
142 	XFRM_DEV_OFFLOAD_OUT,
143 	XFRM_DEV_OFFLOAD_FWD,
144 };
145 
146 enum {
147 	XFRM_DEV_OFFLOAD_UNSPECIFIED,
148 	XFRM_DEV_OFFLOAD_CRYPTO,
149 	XFRM_DEV_OFFLOAD_PACKET,
150 };
151 
152 enum {
153 	XFRM_DEV_OFFLOAD_FLAG_ACQ = 1,
154 };
155 
156 struct xfrm_dev_offload {
157 	/* The device for this offload.
158 	 * Device drivers should not use this directly, as that will prevent
159 	 * them from working with bonding device. Instead, the device passed
160 	 * to the add/delete callbacks should be used.
161 	 */
162 	struct net_device	*dev;
163 	netdevice_tracker	dev_tracker;
164 	/* This is a private pointer used by the bonding driver (and eventually
165 	 * should be moved there). Device drivers should not use it.
166 	 * Protected by xfrm_state.lock AND bond.ipsec_lock in most cases,
167 	 * except in the .xdo_dev_state_del() flow, where only xfrm_state.lock
168 	 * is held.
169 	 */
170 	struct net_device	*real_dev;
171 	unsigned long		offload_handle;
172 	/* Snapshot the attached device index for dump paths. */
173 	int			ifindex;
174 	u8			dir : 2;
175 	u8			type : 2;
176 	u8			flags : 2;
177 };
178 
179 struct xfrm_mode {
180 	u8 encap;
181 	u8 family;
182 	u8 flags;
183 };
184 
185 /* Flags for xfrm_mode. */
186 enum {
187 	XFRM_MODE_FLAG_TUNNEL = 1,
188 };
189 
190 enum xfrm_replay_mode {
191 	XFRM_REPLAY_MODE_LEGACY,
192 	XFRM_REPLAY_MODE_BMP,
193 	XFRM_REPLAY_MODE_ESN,
194 };
195 
196 /* Full description of state of transformer. */
197 struct xfrm_state {
198 	possible_net_t		xs_net;
199 	struct hlist_node	bydst;
200 	struct hlist_node	bysrc;
201 	struct hlist_node	gclist;
202 	union {
203 		struct hlist_node	dev_gclist;
204 		struct hlist_node	byseq;
205 	};
206 	struct hlist_node	byspi;
207 	struct hlist_node	state_cache;
208 	struct hlist_node	state_cache_input;
209 
210 	refcount_t		refcnt;
211 	spinlock_t		lock;
212 
213 	u32			pcpu_num;
214 	struct xfrm_id		id;
215 	struct xfrm_selector	sel;
216 	struct xfrm_mark	mark;
217 	u32			if_id;
218 	u32			tfcpad;
219 
220 	u32			genid;
221 
222 	/* Key manager bits */
223 	struct xfrm_state_walk	km;
224 
225 	/* Parameters of this state. */
226 	struct {
227 		u32		reqid;
228 		u8		mode;
229 		u8		replay_window;
230 		u8		aalgo, ealgo, calgo;
231 		u8		flags;
232 		u16		family;
233 		xfrm_address_t	saddr;
234 		int		header_len;
235 		int		enc_hdr_len;
236 		int		trailer_len;
237 		u32		extra_flags;
238 		struct xfrm_mark	smark;
239 	} props;
240 
241 	struct xfrm_lifetime_cfg lft;
242 
243 	/* Data for transformer */
244 	struct xfrm_algo_auth	*aalg;
245 	struct xfrm_algo	*ealg;
246 	struct xfrm_algo	*calg;
247 	struct xfrm_algo_aead	*aead;
248 	const char		*geniv;
249 
250 	/* mapping change rate limiting */
251 	__be16 new_mapping_sport;
252 	u32 new_mapping;	/* seconds */
253 	u32 mapping_maxage;	/* seconds for input SA */
254 
255 	/* Data for encapsulator */
256 	struct xfrm_encap_tmpl	*encap;
257 
258 	/* NAT keepalive */
259 	u32			nat_keepalive_interval; /* seconds */
260 	time64_t		nat_keepalive_expiration;
261 
262 	/* Data for care-of address */
263 	xfrm_address_t	*coaddr;
264 
265 	/* IPComp needs an IPIP tunnel for handling uncompressed packets */
266 	struct xfrm_state	*tunnel;
267 
268 	/* If a tunnel, number of users + 1 */
269 	atomic_t		tunnel_users;
270 
271 	/* State for replay detection */
272 	struct xfrm_replay_state replay;
273 	struct xfrm_replay_state_esn *replay_esn;
274 
275 	/* Replay detection state at the time we sent the last notification */
276 	struct xfrm_replay_state preplay;
277 	struct xfrm_replay_state_esn *preplay_esn;
278 
279 	/* replay detection mode */
280 	enum xfrm_replay_mode    repl_mode;
281 	/* internal flag that only holds state for delayed aevent at the
282 	 * moment
283 	*/
284 	u32			xflags;
285 
286 	/* Replay detection notification settings */
287 	u32			replay_maxage;
288 	u32			replay_maxdiff;
289 
290 	/* Replay detection notification timer */
291 	struct timer_list	rtimer;
292 
293 	/* Statistics */
294 	struct xfrm_stats	stats;
295 
296 	struct xfrm_lifetime_cur curlft;
297 	struct hrtimer		mtimer;
298 
299 	struct xfrm_dev_offload xso;
300 
301 	/* used to fix curlft->add_time when changing date */
302 	long		saved_tmo;
303 
304 	/* Last used time */
305 	time64_t		lastused;
306 
307 	struct page_frag xfrag;
308 
309 	/* Reference to data common to all the instances of this
310 	 * transformer. */
311 	const struct xfrm_type	*type;
312 	struct xfrm_mode	inner_mode;
313 	struct xfrm_mode	inner_mode_iaf;
314 	struct xfrm_mode	outer_mode;
315 
316 	const struct xfrm_type_offload	*type_offload;
317 
318 	/* Security context */
319 	struct xfrm_sec_ctx	*security;
320 
321 	/* Private data of this transformer, format is opaque,
322 	 * interpreted by xfrm_type methods. */
323 	void			*data;
324 	u8			dir;
325 
326 	const struct xfrm_mode_cbs	*mode_cbs;
327 	void				*mode_data;
328 };
329 
330 static inline struct net *xs_net(struct xfrm_state *x)
331 {
332 	return read_pnet(&x->xs_net);
333 }
334 
335 /* xflags - make enum if more show up */
336 #define XFRM_TIME_DEFER	1
337 #define XFRM_SOFT_EXPIRE 2
338 
339 enum {
340 	XFRM_STATE_VOID,
341 	XFRM_STATE_ACQ,
342 	XFRM_STATE_VALID,
343 	XFRM_STATE_ERROR,
344 	XFRM_STATE_EXPIRED,
345 	XFRM_STATE_DEAD
346 };
347 
348 /* callback structure passed from either netlink or pfkey */
349 struct km_event {
350 	union {
351 		u32 hard;
352 		u32 proto;
353 		u32 byid;
354 		u32 aevent;
355 		u32 type;
356 	} data;
357 
358 	u32	seq;
359 	u32	portid;
360 	u32	event;
361 	struct net *net;
362 };
363 
364 struct xfrm_if_decode_session_result {
365 	struct net *net;
366 	u32 if_id;
367 };
368 
369 struct xfrm_if_cb {
370 	bool (*decode_session)(struct sk_buff *skb,
371 			       unsigned short family,
372 			       struct xfrm_if_decode_session_result *res);
373 };
374 
375 void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb);
376 void xfrm_if_unregister_cb(void);
377 
378 struct xfrm_dst_lookup_params {
379 	struct net *net;
380 	dscp_t dscp;
381 	int oif;
382 	xfrm_address_t *saddr;
383 	xfrm_address_t *daddr;
384 	u32 mark;
385 	__u8 ipproto;
386 	union flowi_uli uli;
387 };
388 
389 struct net_device;
390 struct xfrm_type;
391 struct xfrm_dst;
392 struct xfrm_policy_afinfo {
393 	struct dst_ops		*dst_ops;
394 	struct dst_entry	*(*dst_lookup)(const struct xfrm_dst_lookup_params *params);
395 	int			(*get_saddr)(xfrm_address_t *saddr,
396 					     const struct xfrm_dst_lookup_params *params);
397 	int			(*fill_dst)(struct xfrm_dst *xdst,
398 					    struct net_device *dev,
399 					    const struct flowi *fl);
400 	struct dst_entry	*(*blackhole_route)(struct net *net, struct dst_entry *orig);
401 };
402 
403 int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family);
404 void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo);
405 void km_policy_notify(struct xfrm_policy *xp, int dir,
406 		      const struct km_event *c);
407 void km_state_notify(struct xfrm_state *x, const struct km_event *c);
408 
409 struct xfrm_tmpl;
410 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t,
411 	     struct xfrm_policy *pol);
412 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
413 int __xfrm_state_delete(struct xfrm_state *x);
414 
415 struct xfrm_state_afinfo {
416 	u8				family;
417 	u8				proto;
418 
419 	const struct xfrm_type_offload *type_offload_esp;
420 
421 	const struct xfrm_type		*type_esp;
422 	const struct xfrm_type		*type_ipip;
423 	const struct xfrm_type		*type_ipip6;
424 	const struct xfrm_type		*type_comp;
425 	const struct xfrm_type		*type_ah;
426 	const struct xfrm_type		*type_routing;
427 	const struct xfrm_type		*type_dstopts;
428 
429 	int			(*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
430 	int			(*transport_finish)(struct sk_buff *skb,
431 						    int async);
432 	void			(*local_error)(struct sk_buff *skb, u32 mtu);
433 };
434 
435 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
436 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
437 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
438 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family);
439 
440 struct xfrm_input_afinfo {
441 	u8			family;
442 	bool			is_ipip;
443 	int			(*callback)(struct sk_buff *skb, u8 protocol,
444 					    int err);
445 };
446 
447 int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo);
448 int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo);
449 
450 void xfrm_flush_gc(void);
451 
452 struct xfrm_type {
453 	struct module		*owner;
454 	u8			proto;
455 	u8			flags;
456 #define XFRM_TYPE_NON_FRAGMENT	1
457 #define XFRM_TYPE_REPLAY_PROT	2
458 #define XFRM_TYPE_LOCAL_COADDR	4
459 #define XFRM_TYPE_REMOTE_COADDR	8
460 
461 	int			(*init_state)(struct xfrm_state *x,
462 					      struct netlink_ext_ack *extack);
463 	void			(*destructor)(struct xfrm_state *);
464 	int			(*input)(struct xfrm_state *, struct sk_buff *skb);
465 	int			(*output)(struct xfrm_state *, struct sk_buff *pskb);
466 	int			(*reject)(struct xfrm_state *, struct sk_buff *,
467 					  const struct flowi *);
468 };
469 
470 int xfrm_register_type(const struct xfrm_type *type, unsigned short family);
471 void xfrm_unregister_type(const struct xfrm_type *type, unsigned short family);
472 
473 struct xfrm_type_offload {
474 	struct module	*owner;
475 	u8		proto;
476 	void		(*encap)(struct xfrm_state *, struct sk_buff *pskb);
477 	int		(*input_tail)(struct xfrm_state *x, struct sk_buff *skb);
478 	int		(*xmit)(struct xfrm_state *, struct sk_buff *pskb, netdev_features_t features);
479 };
480 
481 int xfrm_register_type_offload(const struct xfrm_type_offload *type, unsigned short family);
482 void xfrm_unregister_type_offload(const struct xfrm_type_offload *type, unsigned short family);
483 void xfrm_set_type_offload(struct xfrm_state *x, bool try_load);
484 static inline void xfrm_unset_type_offload(struct xfrm_state *x)
485 {
486 	if (!x->type_offload)
487 		return;
488 
489 	module_put(x->type_offload->owner);
490 	x->type_offload = NULL;
491 }
492 
493 /**
494  * struct xfrm_mode_cbs - XFRM mode callbacks
495  * @owner: module owner or NULL
496  * @init_state: Add/init mode specific state in `xfrm_state *x`
497  * @clone_state: Copy mode specific values from `orig` to new state `x`
498  * @destroy_state: Cleanup mode specific state from `xfrm_state *x`
499  * @user_init: Process mode specific netlink attributes from user
500  * @copy_to_user: Add netlink attributes to `attrs` based on state in `x`
501  * @sa_len: Return space required to store mode specific netlink attributes
502  * @get_inner_mtu: Return avail payload space after removing encap overhead
503  * @input: Process received packet from SA using mode
504  * @output: Output given packet using mode
505  * @prepare_output: Add mode specific encapsulation to packet in skb. On return
506  *	`transport_header` should point at ESP header, `network_header` should
507  *	point at outer IP header and `mac_header` should opint at the
508  *	protocol/nexthdr field of the outer IP.
509  *
510  * One should examine and understand the specific uses of these callbacks in
511  * xfrm for further detail on how and when these functions are called. RTSL.
512  */
513 struct xfrm_mode_cbs {
514 	struct module	*owner;
515 	int	(*init_state)(struct xfrm_state *x);
516 	int	(*clone_state)(struct xfrm_state *x, struct xfrm_state *orig);
517 	void	(*destroy_state)(struct xfrm_state *x);
518 	int	(*user_init)(struct net *net, struct xfrm_state *x,
519 			     struct nlattr **attrs,
520 			     struct netlink_ext_ack *extack);
521 	int	(*copy_to_user)(struct xfrm_state *x, struct sk_buff *skb);
522 	unsigned int (*sa_len)(const struct xfrm_state *x);
523 	u32	(*get_inner_mtu)(struct xfrm_state *x, int outer_mtu);
524 	int	(*input)(struct xfrm_state *x, struct sk_buff *skb);
525 	int	(*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
526 	int	(*prepare_output)(struct xfrm_state *x, struct sk_buff *skb);
527 };
528 
529 int xfrm_register_mode_cbs(u8 mode, const struct xfrm_mode_cbs *mode_cbs);
530 void xfrm_unregister_mode_cbs(u8 mode);
531 
532 static inline int xfrm_af2proto(unsigned int family)
533 {
534 	switch(family) {
535 	case AF_INET:
536 		return IPPROTO_IPIP;
537 	case AF_INET6:
538 		return IPPROTO_IPV6;
539 	default:
540 		return 0;
541 	}
542 }
543 
544 static inline const struct xfrm_mode *xfrm_ip2inner_mode(struct xfrm_state *x, int ipproto)
545 {
546 	if ((x->sel.family != AF_UNSPEC) ||
547 	    (ipproto == IPPROTO_IPIP && x->props.family == AF_INET) ||
548 	    (ipproto == IPPROTO_IPV6 && x->props.family == AF_INET6))
549 		return &x->inner_mode;
550 	else
551 		return &x->inner_mode_iaf;
552 }
553 
554 struct xfrm_tmpl {
555 /* id in template is interpreted as:
556  * daddr - destination of tunnel, may be zero for transport mode.
557  * spi   - zero to acquire spi. Not zero if spi is static, then
558  *	   daddr must be fixed too.
559  * proto - AH/ESP/IPCOMP
560  */
561 	struct xfrm_id		id;
562 
563 /* Source address of tunnel. Ignored, if it is not a tunnel. */
564 	xfrm_address_t		saddr;
565 
566 	unsigned short		encap_family;
567 
568 	u32			reqid;
569 
570 /* Mode: transport, tunnel etc. */
571 	u8			mode;
572 
573 /* Sharing mode: unique, this session only, this user only etc. */
574 	u8			share;
575 
576 /* May skip this transfomration if no SA is found */
577 	u8			optional;
578 
579 /* Skip aalgos/ealgos/calgos checks. */
580 	u8			allalgs;
581 
582 /* Bit mask of algos allowed for acquisition */
583 	u32			aalgos;
584 	u32			ealgos;
585 	u32			calgos;
586 };
587 
588 #define XFRM_MAX_DEPTH		6
589 #define XFRM_MAX_OFFLOAD_DEPTH	1
590 
591 struct xfrm_policy_walk_entry {
592 	struct list_head	all;
593 	u8			dead;
594 };
595 
596 struct xfrm_policy_walk {
597 	struct xfrm_policy_walk_entry walk;
598 	u8 type;
599 	u32 seq;
600 };
601 
602 struct xfrm_policy_queue {
603 	struct sk_buff_head	hold_queue;
604 	struct timer_list	hold_timer;
605 	unsigned long		timeout;
606 };
607 
608 /**
609  *	struct xfrm_policy - xfrm policy
610  *	@xp_net: network namespace the policy lives in
611  *	@bydst: hlist node for SPD hash table or rbtree list
612  *	@byidx: hlist node for index hash table
613  *	@state_cache_list: hlist head for policy cached xfrm states
614  *	@lock: serialize changes to policy structure members
615  *	@refcnt: reference count, freed once it reaches 0
616  *	@pos: kernel internal tie-breaker to determine age of policy
617  *	@timer: timer
618  *	@genid: generation, used to invalidate old policies
619  *	@priority: priority, set by userspace
620  *	@index:  policy index (autogenerated)
621  *	@if_id: virtual xfrm interface id
622  *	@mark: packet mark
623  *	@selector: selector
624  *	@lft: liftime configuration data
625  *	@curlft: liftime state
626  *	@walk: list head on pernet policy list
627  *	@polq: queue to hold packets while aqcuire operaion in progress
628  *	@bydst_reinsert: policy tree node needs to be merged
629  *	@type: XFRM_POLICY_TYPE_MAIN or _SUB
630  *	@action: XFRM_POLICY_ALLOW or _BLOCK
631  *	@flags: XFRM_POLICY_LOCALOK, XFRM_POLICY_ICMP
632  *	@xfrm_nr: number of used templates in @xfrm_vec
633  *	@family: protocol family
634  *	@security: SELinux security label
635  *	@xfrm_vec: array of templates to resolve state
636  *	@rcu: rcu head, used to defer memory release
637  *	@xdo: hardware offload state
638  */
639 struct xfrm_policy {
640 	possible_net_t		xp_net;
641 	struct hlist_node	bydst;
642 	struct hlist_node	byidx;
643 
644 	struct hlist_head	state_cache_list;
645 
646 	/* This lock only affects elements except for entry. */
647 	rwlock_t		lock;
648 	refcount_t		refcnt;
649 	u32			pos;
650 	struct timer_list	timer;
651 
652 	atomic_t		genid;
653 	u32			priority;
654 	u32			index;
655 	u32			if_id;
656 	struct xfrm_mark	mark;
657 	struct xfrm_selector	selector;
658 	struct xfrm_lifetime_cfg lft;
659 	struct xfrm_lifetime_cur curlft;
660 	struct xfrm_policy_walk_entry walk;
661 	struct xfrm_policy_queue polq;
662 	bool                    bydst_reinsert;
663 	u8			type;
664 	u8			action;
665 	u8			flags;
666 	u8			xfrm_nr;
667 	u16			family;
668 	struct xfrm_sec_ctx	*security;
669 	struct xfrm_tmpl       	xfrm_vec[XFRM_MAX_DEPTH];
670 	struct rcu_head		rcu;
671 
672 	struct xfrm_dev_offload xdo;
673 };
674 
675 static inline struct net *xp_net(const struct xfrm_policy *xp)
676 {
677 	return read_pnet(&xp->xp_net);
678 }
679 
680 struct xfrm_kmaddress {
681 	xfrm_address_t          local;
682 	xfrm_address_t          remote;
683 	u32			reserved;
684 	u16			family;
685 };
686 
687 struct xfrm_migrate {
688 	xfrm_address_t		old_daddr;
689 	xfrm_address_t		old_saddr;
690 	xfrm_address_t		new_daddr;
691 	xfrm_address_t		new_saddr;
692 	struct xfrm_encap_tmpl *encap;
693 	struct xfrm_user_offload *xuo;
694 	struct xfrm_mark        old_mark;
695 	const struct xfrm_mark *new_mark;
696 	struct xfrm_mark        smark;
697 	u8			proto;
698 	u8			mode;
699 	u16			msg_type; /* XFRM_MSG_MIGRATE or XFRM_MSG_MIGRATE_STATE */
700 	u32			flags;
701 	u32			old_reqid;
702 	u32			new_reqid;
703 	u32			nat_keepalive_interval;
704 	u32			mapping_maxage;
705 	u16			old_family;
706 	u16			new_family;
707 	const struct xfrm_selector *new_sel;
708 };
709 
710 #define XFRM_KM_TIMEOUT                30
711 /* what happened */
712 #define XFRM_REPLAY_UPDATE	XFRM_AE_CR
713 #define XFRM_REPLAY_TIMEOUT	XFRM_AE_CE
714 
715 /* default aevent timeout in units of 100ms */
716 #define XFRM_AE_ETIME			10
717 /* Async Event timer multiplier */
718 #define XFRM_AE_ETH_M			10
719 /* default seq threshold size */
720 #define XFRM_AE_SEQT_SIZE		2
721 
722 struct xfrm_mgr {
723 	struct list_head	list;
724 	int			(*notify)(struct xfrm_state *x, const struct km_event *c);
725 	int			(*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp);
726 	struct xfrm_policy	*(*compile_policy)(struct sock *sk, int opt, u8 *data, int len, int *dir);
727 	int			(*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
728 	int			(*notify_policy)(struct xfrm_policy *x, int dir, const struct km_event *c);
729 	int			(*report)(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr);
730 	int			(*migrate)(const struct xfrm_selector *sel,
731 					   u8 dir, u8 type,
732 					   const struct xfrm_migrate *m,
733 					   int num_bundles,
734 					   const struct xfrm_kmaddress *k,
735 					   struct net *net,
736 					   const struct xfrm_encap_tmpl *encap);
737 	bool			(*is_alive)(const struct km_event *c);
738 };
739 
740 void xfrm_register_km(struct xfrm_mgr *km);
741 void xfrm_unregister_km(struct xfrm_mgr *km);
742 
743 struct xfrm_tunnel_skb_cb {
744 	union {
745 		struct inet_skb_parm h4;
746 		struct inet6_skb_parm h6;
747 	} header;
748 
749 	union {
750 		struct ip_tunnel *ip4;
751 		struct ip6_tnl *ip6;
752 	} tunnel;
753 };
754 
755 #define XFRM_TUNNEL_SKB_CB(__skb) ((struct xfrm_tunnel_skb_cb *)&((__skb)->cb[0]))
756 
757 /*
758  * This structure is used for the duration where packets are being
759  * transformed by IPsec.  As soon as the packet leaves IPsec the
760  * area beyond the generic IP part may be overwritten.
761  */
762 struct xfrm_skb_cb {
763 	struct xfrm_tunnel_skb_cb header;
764 
765         /* Sequence number for replay protection. */
766 	union {
767 		struct {
768 			__u32 low;
769 			__u32 hi;
770 		} output;
771 		struct {
772 			__be32 low;
773 			__be32 hi;
774 		} input;
775 	} seq;
776 };
777 
778 #define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0]))
779 
780 /*
781  * This structure is used by the afinfo prepare_input/prepare_output functions
782  * to transmit header information to the mode input/output functions.
783  */
784 struct xfrm_mode_skb_cb {
785 	struct xfrm_tunnel_skb_cb header;
786 
787 	/* Copied from header for IPv4, always set to zero and DF for IPv6. */
788 	__be16 id;
789 	__be16 frag_off;
790 
791 	/* IP header length (excluding options or extension headers). */
792 	u8 ihl;
793 
794 	/* TOS for IPv4, class for IPv6. */
795 	u8 tos;
796 
797 	/* TTL for IPv4, hop limitfor IPv6. */
798 	u8 ttl;
799 
800 	/* Protocol for IPv4, NH for IPv6. */
801 	u8 protocol;
802 
803 	/* Option length for IPv4, zero for IPv6. */
804 	u8 optlen;
805 
806 	/* Used by IPv6 only, zero for IPv4. */
807 	u8 flow_lbl[3];
808 };
809 
810 #define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0]))
811 
812 /*
813  * This structure is used by the input processing to locate the SPI and
814  * related information.
815  */
816 struct xfrm_spi_skb_cb {
817 	struct xfrm_tunnel_skb_cb header;
818 
819 	unsigned int daddroff;
820 	unsigned int family;
821 	__be32 seq;
822 };
823 
824 #define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0]))
825 
826 #ifdef CONFIG_AUDITSYSCALL
827 static inline struct audit_buffer *xfrm_audit_start(const char *op)
828 {
829 	struct audit_buffer *audit_buf = NULL;
830 
831 	if (audit_enabled == AUDIT_OFF)
832 		return NULL;
833 	audit_buf = audit_log_start(audit_context(), GFP_ATOMIC,
834 				    AUDIT_MAC_IPSEC_EVENT);
835 	if (audit_buf == NULL)
836 		return NULL;
837 	audit_log_format(audit_buf, "op=%s", op);
838 	return audit_buf;
839 }
840 
841 static inline void xfrm_audit_helper_usrinfo(bool task_valid,
842 					     struct audit_buffer *audit_buf)
843 {
844 	const unsigned int auid = from_kuid(&init_user_ns, task_valid ?
845 					    audit_get_loginuid(current) :
846 					    INVALID_UID);
847 	const unsigned int ses = task_valid ? audit_get_sessionid(current) :
848 		AUDIT_SID_UNSET;
849 
850 	audit_log_format(audit_buf, " auid=%u ses=%u", auid, ses);
851 	audit_log_task_context(audit_buf);
852 }
853 
854 void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid);
855 void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
856 			      bool task_valid);
857 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid);
858 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid);
859 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
860 				      struct sk_buff *skb);
861 void xfrm_audit_state_replay(struct xfrm_state *x, struct sk_buff *skb,
862 			     __be32 net_seq);
863 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family);
864 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, __be32 net_spi,
865 			       __be32 net_seq);
866 void xfrm_audit_state_icvfail(struct xfrm_state *x, struct sk_buff *skb,
867 			      u8 proto);
868 #else
869 
870 static inline void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
871 					 bool task_valid)
872 {
873 }
874 
875 static inline void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
876 					    bool task_valid)
877 {
878 }
879 
880 static inline void xfrm_audit_state_add(struct xfrm_state *x, int result,
881 					bool task_valid)
882 {
883 }
884 
885 static inline void xfrm_audit_state_delete(struct xfrm_state *x, int result,
886 					   bool task_valid)
887 {
888 }
889 
890 static inline void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
891 					     struct sk_buff *skb)
892 {
893 }
894 
895 static inline void xfrm_audit_state_replay(struct xfrm_state *x,
896 					   struct sk_buff *skb, __be32 net_seq)
897 {
898 }
899 
900 static inline void xfrm_audit_state_notfound_simple(struct sk_buff *skb,
901 				      u16 family)
902 {
903 }
904 
905 static inline void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
906 				      __be32 net_spi, __be32 net_seq)
907 {
908 }
909 
910 static inline void xfrm_audit_state_icvfail(struct xfrm_state *x,
911 				     struct sk_buff *skb, u8 proto)
912 {
913 }
914 #endif /* CONFIG_AUDITSYSCALL */
915 
916 static inline void xfrm_pol_hold(struct xfrm_policy *policy)
917 {
918 	if (likely(policy != NULL))
919 		refcount_inc(&policy->refcnt);
920 }
921 
922 void xfrm_policy_destroy(struct xfrm_policy *policy);
923 
924 static inline void xfrm_pol_put(struct xfrm_policy *policy)
925 {
926 	if (refcount_dec_and_test(&policy->refcnt))
927 		xfrm_policy_destroy(policy);
928 }
929 
930 static inline void xfrm_pols_put(struct xfrm_policy **pols, int npols)
931 {
932 	int i;
933 	for (i = npols - 1; i >= 0; --i)
934 		xfrm_pol_put(pols[i]);
935 }
936 
937 void __xfrm_state_destroy(struct xfrm_state *);
938 
939 static inline void __xfrm_state_put(struct xfrm_state *x)
940 {
941 	refcount_dec(&x->refcnt);
942 }
943 
944 static inline void xfrm_state_put(struct xfrm_state *x)
945 {
946 	if (refcount_dec_and_test(&x->refcnt))
947 		__xfrm_state_destroy(x);
948 }
949 
950 static inline void xfrm_state_hold(struct xfrm_state *x)
951 {
952 	refcount_inc(&x->refcnt);
953 }
954 
955 static inline bool addr_match(const void *token1, const void *token2,
956 			      unsigned int prefixlen)
957 {
958 	const __be32 *a1 = token1;
959 	const __be32 *a2 = token2;
960 	unsigned int pdw;
961 	unsigned int pbi;
962 
963 	if (prefixlen > 128)
964 		return false;
965 
966 	pdw = prefixlen >> 5;	  /* num of whole u32 in prefix */
967 	pbi = prefixlen &  0x1f;  /* num of bits in incomplete u32 in prefix */
968 
969 	if (pdw)
970 		if (memcmp(a1, a2, pdw << 2))
971 			return false;
972 
973 	if (pbi) {
974 		__be32 mask;
975 
976 		mask = htonl((0xffffffff) << (32 - pbi));
977 
978 		if ((a1[pdw] ^ a2[pdw]) & mask)
979 			return false;
980 	}
981 
982 	return true;
983 }
984 
985 static inline bool addr4_match(__be32 a1, __be32 a2, u8 prefixlen)
986 {
987 	/* C99 6.5.7 (3): u32 << 32 is undefined behaviour */
988 	if (sizeof(long) == 4 && prefixlen == 0)
989 		return true;
990 
991 	if (prefixlen > 32)
992 		return false;
993 
994 	return !((a1 ^ a2) & htonl(~0UL << (32 - prefixlen)));
995 }
996 
997 static __inline__
998 __be16 xfrm_flowi_sport(const struct flowi *fl, const union flowi_uli *uli)
999 {
1000 	__be16 port;
1001 	switch(fl->flowi_proto) {
1002 	case IPPROTO_TCP:
1003 	case IPPROTO_UDP:
1004 	case IPPROTO_UDPLITE:
1005 	case IPPROTO_SCTP:
1006 		port = uli->ports.sport;
1007 		break;
1008 	case IPPROTO_ICMP:
1009 	case IPPROTO_ICMPV6:
1010 		port = htons(uli->icmpt.type);
1011 		break;
1012 	case IPPROTO_MH:
1013 		port = htons(uli->mht.type);
1014 		break;
1015 	case IPPROTO_GRE:
1016 		port = htons(ntohl(uli->gre_key) >> 16);
1017 		break;
1018 	default:
1019 		port = 0;	/*XXX*/
1020 	}
1021 	return port;
1022 }
1023 
1024 static __inline__
1025 __be16 xfrm_flowi_dport(const struct flowi *fl, const union flowi_uli *uli)
1026 {
1027 	__be16 port;
1028 	switch(fl->flowi_proto) {
1029 	case IPPROTO_TCP:
1030 	case IPPROTO_UDP:
1031 	case IPPROTO_UDPLITE:
1032 	case IPPROTO_SCTP:
1033 		port = uli->ports.dport;
1034 		break;
1035 	case IPPROTO_ICMP:
1036 	case IPPROTO_ICMPV6:
1037 		port = htons(uli->icmpt.code);
1038 		break;
1039 	case IPPROTO_GRE:
1040 		port = htons(ntohl(uli->gre_key) & 0xffff);
1041 		break;
1042 	default:
1043 		port = 0;	/*XXX*/
1044 	}
1045 	return port;
1046 }
1047 
1048 bool xfrm_selector_match(const struct xfrm_selector *sel,
1049 			 const struct flowi *fl, unsigned short family);
1050 
1051 #ifdef CONFIG_SECURITY_NETWORK_XFRM
1052 /*	If neither has a context --> match
1053  * 	Otherwise, both must have a context and the sids, doi, alg must match
1054  */
1055 static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
1056 {
1057 	return ((!s1 && !s2) ||
1058 		(s1 && s2 &&
1059 		 (s1->ctx_sid == s2->ctx_sid) &&
1060 		 (s1->ctx_doi == s2->ctx_doi) &&
1061 		 (s1->ctx_alg == s2->ctx_alg)));
1062 }
1063 #else
1064 static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
1065 {
1066 	return true;
1067 }
1068 #endif
1069 
1070 /* A struct encoding bundle of transformations to apply to some set of flow.
1071  *
1072  * xdst->child points to the next element of bundle.
1073  * dst->xfrm  points to an instanse of transformer.
1074  *
1075  * Due to unfortunate limitations of current routing cache, which we
1076  * have no time to fix, it mirrors struct rtable and bound to the same
1077  * routing key, including saddr,daddr. However, we can have many of
1078  * bundles differing by session id. All the bundles grow from a parent
1079  * policy rule.
1080  */
1081 struct xfrm_dst {
1082 	union {
1083 		struct dst_entry	dst;
1084 		struct rtable		rt;
1085 		struct rt6_info		rt6;
1086 	} u;
1087 	struct dst_entry *route;
1088 	struct dst_entry *child;
1089 	struct dst_entry *path;
1090 	struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1091 	int num_pols, num_xfrms;
1092 	u32 xfrm_genid;
1093 	u32 policy_genid;
1094 	u32 route_mtu_cached;
1095 	u32 child_mtu_cached;
1096 	u32 route_cookie;
1097 	u32 path_cookie;
1098 };
1099 
1100 static inline struct dst_entry *xfrm_dst_path(const struct dst_entry *dst)
1101 {
1102 #ifdef CONFIG_XFRM
1103 	if (dst->xfrm || (dst->flags & DST_XFRM_QUEUE)) {
1104 		const struct xfrm_dst *xdst = (const struct xfrm_dst *) dst;
1105 
1106 		return xdst->path;
1107 	}
1108 #endif
1109 	return (struct dst_entry *) dst;
1110 }
1111 
1112 static inline struct dst_entry *xfrm_dst_child(const struct dst_entry *dst)
1113 {
1114 #ifdef CONFIG_XFRM
1115 	if (dst->xfrm || (dst->flags & DST_XFRM_QUEUE)) {
1116 		struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1117 		return xdst->child;
1118 	}
1119 #endif
1120 	return NULL;
1121 }
1122 
1123 #ifdef CONFIG_XFRM
1124 static inline void xfrm_dst_set_child(struct xfrm_dst *xdst, struct dst_entry *child)
1125 {
1126 	xdst->child = child;
1127 }
1128 
1129 static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)
1130 {
1131 	xfrm_pols_put(xdst->pols, xdst->num_pols);
1132 	dst_release(xdst->route);
1133 	if (likely(xdst->u.dst.xfrm))
1134 		xfrm_state_put(xdst->u.dst.xfrm);
1135 }
1136 #endif
1137 
1138 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
1139 
1140 struct xfrm_if_parms {
1141 	int link;		/* ifindex of underlying L2 interface */
1142 	u32 if_id;		/* interface identifier */
1143 	bool collect_md;
1144 };
1145 
1146 struct xfrm_if {
1147 	struct xfrm_if __rcu *next;	/* next interface in list */
1148 	struct net_device *dev;		/* virtual device associated with interface */
1149 	struct net *net;		/* netns for packet i/o */
1150 	struct xfrm_if_parms p;		/* interface parms */
1151 
1152 	struct gro_cells gro_cells;
1153 };
1154 
1155 struct xfrm_offload {
1156 	/* Output sequence number for replay protection on offloading. */
1157 	struct {
1158 		__u32 low;
1159 		__u32 hi;
1160 	} seq;
1161 
1162 	__u32			flags;
1163 #define	SA_DELETE_REQ		1
1164 #define	CRYPTO_DONE		2
1165 #define	CRYPTO_NEXT_DONE	4
1166 #define	CRYPTO_FALLBACK		8
1167 #define	XFRM_GSO_SEGMENT	16
1168 #define	XFRM_GRO		32
1169 /* 64 is free */
1170 #define	XFRM_DEV_RESUME		128
1171 #define	XFRM_XMIT		256
1172 
1173 	__u32			status;
1174 #define CRYPTO_SUCCESS				1
1175 #define CRYPTO_GENERIC_ERROR			2
1176 #define CRYPTO_TRANSPORT_AH_AUTH_FAILED		4
1177 #define CRYPTO_TRANSPORT_ESP_AUTH_FAILED	8
1178 #define CRYPTO_TUNNEL_AH_AUTH_FAILED		16
1179 #define CRYPTO_TUNNEL_ESP_AUTH_FAILED		32
1180 #define CRYPTO_INVALID_PACKET_SYNTAX		64
1181 #define CRYPTO_INVALID_PROTOCOL			128
1182 
1183 	/* Used to keep whole l2 header for transport mode GRO */
1184 	__u16			orig_mac_len;
1185 
1186 	__u8			proto;
1187 	__u8			inner_ipproto;
1188 };
1189 
1190 struct sec_path {
1191 	struct xfrm_state	*xvec[XFRM_MAX_DEPTH];
1192 	struct xfrm_offload	ovec[XFRM_MAX_OFFLOAD_DEPTH];
1193 
1194 	u8			len;
1195 	u8			olen;
1196 	u8			verified_cnt;
1197 };
1198 
1199 struct sec_path *secpath_set(struct sk_buff *skb);
1200 
1201 static inline void
1202 secpath_reset(struct sk_buff *skb)
1203 {
1204 #ifdef CONFIG_XFRM
1205 	skb_ext_del(skb, SKB_EXT_SEC_PATH);
1206 #endif
1207 }
1208 
1209 static inline bool
1210 secpath_equal(const struct sk_buff *skb1, const struct sk_buff *skb2)
1211 {
1212 	const struct sec_path *sp1 = skb_sec_path(skb1);
1213 	const struct sec_path *sp2 = skb_sec_path(skb2);
1214 	int i;
1215 
1216 	if (!sp1 || !sp2)
1217 		return sp1 == sp2;
1218 	if (sp1->len != sp2->len)
1219 		return false;
1220 	for (i = 0; i < sp1->len; i++)
1221 		if (sp1->xvec[i] != sp2->xvec[i])
1222 			return false;
1223 	return true;
1224 }
1225 
1226 static inline int
1227 xfrm_addr_any(const xfrm_address_t *addr, unsigned short family)
1228 {
1229 	switch (family) {
1230 	case AF_INET:
1231 		return addr->a4 == 0;
1232 	case AF_INET6:
1233 		return ipv6_addr_any(&addr->in6);
1234 	}
1235 	return 0;
1236 }
1237 
1238 static inline int
1239 __xfrm4_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
1240 {
1241 	return	(tmpl->saddr.a4 &&
1242 		 tmpl->saddr.a4 != x->props.saddr.a4);
1243 }
1244 
1245 static inline int
1246 __xfrm6_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
1247 {
1248 	return	(!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
1249 		 !ipv6_addr_equal((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
1250 }
1251 
1252 static inline int
1253 xfrm_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, unsigned short family)
1254 {
1255 	switch (family) {
1256 	case AF_INET:
1257 		return __xfrm4_state_addr_cmp(tmpl, x);
1258 	case AF_INET6:
1259 		return __xfrm6_state_addr_cmp(tmpl, x);
1260 	}
1261 	return !0;
1262 }
1263 
1264 #ifdef CONFIG_XFRM
1265 static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb)
1266 {
1267 	struct sec_path *sp = skb_sec_path(skb);
1268 
1269 	return sp->xvec[sp->len - 1];
1270 }
1271 #endif
1272 
1273 static inline struct xfrm_offload *xfrm_offload(struct sk_buff *skb)
1274 {
1275 #ifdef CONFIG_XFRM
1276 	struct sec_path *sp = skb_sec_path(skb);
1277 
1278 	if (!sp || !sp->olen || sp->len != sp->olen)
1279 		return NULL;
1280 
1281 	return &sp->ovec[sp->olen - 1];
1282 #else
1283 	return NULL;
1284 #endif
1285 }
1286 
1287 #ifdef CONFIG_XFRM
1288 int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb,
1289 			unsigned short family);
1290 
1291 static inline bool __xfrm_check_nopolicy(struct net *net, struct sk_buff *skb,
1292 					 int dir)
1293 {
1294 	if (!READ_ONCE(net->xfrm.policy_count[dir]) && !secpath_exists(skb))
1295 		return READ_ONCE(net->xfrm.policy_default[dir]) == XFRM_USERPOLICY_ACCEPT;
1296 
1297 	return false;
1298 }
1299 
1300 static inline bool __xfrm_check_dev_nopolicy(struct sk_buff *skb,
1301 					     int dir, unsigned short family)
1302 {
1303 	if (dir != XFRM_POLICY_OUT && family == AF_INET) {
1304 		/* same dst may be used for traffic originating from
1305 		 * devices with different policy settings.
1306 		 */
1307 		return IPCB(skb)->flags & IPSKB_NOPOLICY;
1308 	}
1309 	return skb_dst(skb) && (skb_dst(skb)->flags & DST_NOPOLICY);
1310 }
1311 
1312 static inline int __xfrm_policy_check2(struct sock *sk, int dir,
1313 				       struct sk_buff *skb,
1314 				       unsigned int family, int reverse)
1315 {
1316 	struct net *net = dev_net(skb->dev);
1317 	int ndir = dir | (reverse ? XFRM_POLICY_MASK + 1 : 0);
1318 	struct xfrm_offload *xo = xfrm_offload(skb);
1319 	struct xfrm_state *x;
1320 
1321 	if (sk && sk->sk_policy[XFRM_POLICY_IN])
1322 		return __xfrm_policy_check(sk, ndir, skb, family);
1323 
1324 	if (xo) {
1325 		x = xfrm_input_state(skb);
1326 		if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET) {
1327 			bool check = (xo->flags & CRYPTO_DONE) &&
1328 				     (xo->status & CRYPTO_SUCCESS);
1329 
1330 			/* The packets here are plain ones and secpath was
1331 			 * needed to indicate that hardware already handled
1332 			 * them and there is no need to do nothing in addition.
1333 			 *
1334 			 * Consume secpath which was set by drivers.
1335 			 */
1336 			secpath_reset(skb);
1337 			return check;
1338 		}
1339 	}
1340 
1341 	return __xfrm_check_nopolicy(net, skb, dir) ||
1342 	       __xfrm_check_dev_nopolicy(skb, dir, family) ||
1343 	       __xfrm_policy_check(sk, ndir, skb, family);
1344 }
1345 
1346 static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
1347 {
1348 	return __xfrm_policy_check2(sk, dir, skb, family, 0);
1349 }
1350 
1351 static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
1352 {
1353 	return xfrm_policy_check(sk, dir, skb, AF_INET);
1354 }
1355 
1356 static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
1357 {
1358 	return xfrm_policy_check(sk, dir, skb, AF_INET6);
1359 }
1360 
1361 static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1362 					     struct sk_buff *skb)
1363 {
1364 	return __xfrm_policy_check2(sk, dir, skb, AF_INET, 1);
1365 }
1366 
1367 static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1368 					     struct sk_buff *skb)
1369 {
1370 	return __xfrm_policy_check2(sk, dir, skb, AF_INET6, 1);
1371 }
1372 
1373 int __xfrm_decode_session(struct net *net, struct sk_buff *skb, struct flowi *fl,
1374 			  unsigned int family, int reverse);
1375 
1376 static inline int xfrm_decode_session(struct net *net, struct sk_buff *skb, struct flowi *fl,
1377 				      unsigned int family)
1378 {
1379 	return __xfrm_decode_session(net, skb, fl, family, 0);
1380 }
1381 
1382 static inline int xfrm_decode_session_reverse(struct net *net, struct sk_buff *skb,
1383 					      struct flowi *fl,
1384 					      unsigned int family)
1385 {
1386 	return __xfrm_decode_session(net, skb, fl, family, 1);
1387 }
1388 
1389 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
1390 
1391 static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1392 {
1393 	struct net *net = dev_net(skb->dev);
1394 
1395 	if (!READ_ONCE(net->xfrm.policy_count[XFRM_POLICY_OUT]) &&
1396 	    READ_ONCE(net->xfrm.policy_default[XFRM_POLICY_OUT]) == XFRM_USERPOLICY_ACCEPT)
1397 		return true;
1398 
1399 	return (skb_dst(skb)->flags & DST_NOXFRM) ||
1400 	       __xfrm_route_forward(skb, family);
1401 }
1402 
1403 static inline int xfrm4_route_forward(struct sk_buff *skb)
1404 {
1405 	return xfrm_route_forward(skb, AF_INET);
1406 }
1407 
1408 static inline int xfrm6_route_forward(struct sk_buff *skb)
1409 {
1410 	return xfrm_route_forward(skb, AF_INET6);
1411 }
1412 
1413 int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk);
1414 
1415 static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
1416 {
1417 	if (!sk_fullsock(osk))
1418 		return 0;
1419 	sk->sk_policy[0] = NULL;
1420 	sk->sk_policy[1] = NULL;
1421 	if (unlikely(osk->sk_policy[0] || osk->sk_policy[1]))
1422 		return __xfrm_sk_clone_policy(sk, osk);
1423 	return 0;
1424 }
1425 
1426 int xfrm_policy_delete(struct xfrm_policy *pol, int dir);
1427 
1428 static inline void xfrm_sk_free_policy(struct sock *sk)
1429 {
1430 	struct xfrm_policy *pol;
1431 
1432 	pol = rcu_dereference_protected(sk->sk_policy[0], 1);
1433 	if (unlikely(pol != NULL)) {
1434 		xfrm_policy_delete(pol, XFRM_POLICY_MAX);
1435 		sk->sk_policy[0] = NULL;
1436 	}
1437 	pol = rcu_dereference_protected(sk->sk_policy[1], 1);
1438 	if (unlikely(pol != NULL)) {
1439 		xfrm_policy_delete(pol, XFRM_POLICY_MAX+1);
1440 		sk->sk_policy[1] = NULL;
1441 	}
1442 }
1443 
1444 #else
1445 
1446 static inline void xfrm_sk_free_policy(struct sock *sk) {}
1447 static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) { return 0; }
1448 static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }
1449 static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; }
1450 static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
1451 {
1452 	return 1;
1453 }
1454 static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
1455 {
1456 	return 1;
1457 }
1458 static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
1459 {
1460 	return 1;
1461 }
1462 static inline int xfrm_decode_session_reverse(struct net *net, struct sk_buff *skb,
1463 					      struct flowi *fl,
1464 					      unsigned int family)
1465 {
1466 	return -ENOSYS;
1467 }
1468 static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1469 					     struct sk_buff *skb)
1470 {
1471 	return 1;
1472 }
1473 static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1474 					     struct sk_buff *skb)
1475 {
1476 	return 1;
1477 }
1478 #endif
1479 
1480 static __inline__
1481 xfrm_address_t *xfrm_flowi_daddr(const struct flowi *fl, unsigned short family)
1482 {
1483 	switch (family){
1484 	case AF_INET:
1485 		return (xfrm_address_t *)&fl->u.ip4.daddr;
1486 	case AF_INET6:
1487 		return (xfrm_address_t *)&fl->u.ip6.daddr;
1488 	}
1489 	return NULL;
1490 }
1491 
1492 static __inline__
1493 xfrm_address_t *xfrm_flowi_saddr(const struct flowi *fl, unsigned short family)
1494 {
1495 	switch (family){
1496 	case AF_INET:
1497 		return (xfrm_address_t *)&fl->u.ip4.saddr;
1498 	case AF_INET6:
1499 		return (xfrm_address_t *)&fl->u.ip6.saddr;
1500 	}
1501 	return NULL;
1502 }
1503 
1504 static __inline__
1505 void xfrm_flowi_addr_get(const struct flowi *fl,
1506 			 xfrm_address_t *saddr, xfrm_address_t *daddr,
1507 			 unsigned short family)
1508 {
1509 	switch(family) {
1510 	case AF_INET:
1511 		memcpy(&saddr->a4, &fl->u.ip4.saddr, sizeof(saddr->a4));
1512 		memcpy(&daddr->a4, &fl->u.ip4.daddr, sizeof(daddr->a4));
1513 		break;
1514 	case AF_INET6:
1515 		saddr->in6 = fl->u.ip6.saddr;
1516 		daddr->in6 = fl->u.ip6.daddr;
1517 		break;
1518 	}
1519 }
1520 
1521 static __inline__ int
1522 __xfrm4_state_addr_check(const struct xfrm_state *x,
1523 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
1524 {
1525 	if (daddr->a4 == x->id.daddr.a4 &&
1526 	    (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
1527 		return 1;
1528 	return 0;
1529 }
1530 
1531 static __inline__ int
1532 __xfrm6_state_addr_check(const struct xfrm_state *x,
1533 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
1534 {
1535 	if (ipv6_addr_equal((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
1536 	    (ipv6_addr_equal((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr) ||
1537 	     ipv6_addr_any((struct in6_addr *)saddr) ||
1538 	     ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
1539 		return 1;
1540 	return 0;
1541 }
1542 
1543 static __inline__ int
1544 xfrm_state_addr_check(const struct xfrm_state *x,
1545 		      const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1546 		      unsigned short family)
1547 {
1548 	switch (family) {
1549 	case AF_INET:
1550 		return __xfrm4_state_addr_check(x, daddr, saddr);
1551 	case AF_INET6:
1552 		return __xfrm6_state_addr_check(x, daddr, saddr);
1553 	}
1554 	return 0;
1555 }
1556 
1557 static __inline__ int
1558 xfrm_state_addr_flow_check(const struct xfrm_state *x, const struct flowi *fl,
1559 			   unsigned short family)
1560 {
1561 	switch (family) {
1562 	case AF_INET:
1563 		return __xfrm4_state_addr_check(x,
1564 						(const xfrm_address_t *)&fl->u.ip4.daddr,
1565 						(const xfrm_address_t *)&fl->u.ip4.saddr);
1566 	case AF_INET6:
1567 		return __xfrm6_state_addr_check(x,
1568 						(const xfrm_address_t *)&fl->u.ip6.daddr,
1569 						(const xfrm_address_t *)&fl->u.ip6.saddr);
1570 	}
1571 	return 0;
1572 }
1573 
1574 static inline int xfrm_state_kern(const struct xfrm_state *x)
1575 {
1576 	return atomic_read(&x->tunnel_users);
1577 }
1578 
1579 static inline bool xfrm_id_proto_valid(u8 proto)
1580 {
1581 	switch (proto) {
1582 	case IPPROTO_AH:
1583 	case IPPROTO_ESP:
1584 	case IPPROTO_COMP:
1585 #if IS_ENABLED(CONFIG_IPV6)
1586 	case IPPROTO_ROUTING:
1587 	case IPPROTO_DSTOPTS:
1588 #endif
1589 		return true;
1590 	default:
1591 		return false;
1592 	}
1593 }
1594 
1595 /* IPSEC_PROTO_ANY only matches 3 IPsec protocols, 0 could match all. */
1596 static inline int xfrm_id_proto_match(u8 proto, u8 userproto)
1597 {
1598 	return (!userproto || proto == userproto ||
1599 		(userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH ||
1600 						  proto == IPPROTO_ESP ||
1601 						  proto == IPPROTO_COMP)));
1602 }
1603 
1604 /*
1605  * xfrm algorithm information
1606  */
1607 struct xfrm_algo_aead_info {
1608 	char *geniv;
1609 	u16 icv_truncbits;
1610 };
1611 
1612 struct xfrm_algo_auth_info {
1613 	u16 icv_truncbits;
1614 	u16 icv_fullbits;
1615 };
1616 
1617 struct xfrm_algo_encr_info {
1618 	char *geniv;
1619 	u16 blockbits;
1620 	u16 defkeybits;
1621 };
1622 
1623 struct xfrm_algo_comp_info {
1624 	u16 threshold;
1625 };
1626 
1627 struct xfrm_algo_desc {
1628 	char *name;
1629 	char *compat;
1630 	u8 available:1;
1631 	u8 pfkey_supported:1;
1632 	union {
1633 		struct xfrm_algo_aead_info aead;
1634 		struct xfrm_algo_auth_info auth;
1635 		struct xfrm_algo_encr_info encr;
1636 		struct xfrm_algo_comp_info comp;
1637 	} uinfo;
1638 	struct sadb_alg desc;
1639 };
1640 
1641 /* XFRM protocol handlers.  */
1642 struct xfrm4_protocol {
1643 	int (*handler)(struct sk_buff *skb);
1644 	int (*input_handler)(struct sk_buff *skb, int nexthdr, __be32 spi,
1645 			     int encap_type);
1646 	int (*cb_handler)(struct sk_buff *skb, int err);
1647 	int (*err_handler)(struct sk_buff *skb, u32 info);
1648 
1649 	struct xfrm4_protocol __rcu *next;
1650 	int priority;
1651 };
1652 
1653 struct xfrm6_protocol {
1654 	int (*handler)(struct sk_buff *skb);
1655 	int (*input_handler)(struct sk_buff *skb, int nexthdr, __be32 spi,
1656 			     int encap_type);
1657 	int (*cb_handler)(struct sk_buff *skb, int err);
1658 	int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
1659 			   u8 type, u8 code, int offset, __be32 info);
1660 
1661 	struct xfrm6_protocol __rcu *next;
1662 	int priority;
1663 };
1664 
1665 /* XFRM tunnel handlers.  */
1666 struct xfrm_tunnel {
1667 	int (*handler)(struct sk_buff *skb);
1668 	int (*cb_handler)(struct sk_buff *skb, int err);
1669 	int (*err_handler)(struct sk_buff *skb, u32 info);
1670 
1671 	struct xfrm_tunnel __rcu *next;
1672 	int priority;
1673 };
1674 
1675 struct xfrm6_tunnel {
1676 	int (*handler)(struct sk_buff *skb);
1677 	int (*cb_handler)(struct sk_buff *skb, int err);
1678 	int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
1679 			   u8 type, u8 code, int offset, __be32 info);
1680 	struct xfrm6_tunnel __rcu *next;
1681 	int priority;
1682 };
1683 
1684 void xfrm_init(void);
1685 void xfrm4_init(void);
1686 int xfrm_state_init(struct net *net);
1687 void xfrm_state_fini(struct net *net);
1688 void xfrm4_state_init(void);
1689 void xfrm4_protocol_init(void);
1690 #ifdef CONFIG_XFRM
1691 int xfrm6_init(void);
1692 void xfrm6_fini(void);
1693 int xfrm6_state_init(void);
1694 void xfrm6_state_fini(void);
1695 int xfrm6_protocol_init(void);
1696 void xfrm6_protocol_fini(void);
1697 #else
1698 static inline int xfrm6_init(void)
1699 {
1700 	return 0;
1701 }
1702 static inline void xfrm6_fini(void)
1703 {
1704 	;
1705 }
1706 #endif
1707 
1708 #ifdef CONFIG_XFRM_STATISTICS
1709 int xfrm_proc_init(struct net *net);
1710 void xfrm_proc_fini(struct net *net);
1711 #endif
1712 
1713 int xfrm_sysctl_init(struct net *net);
1714 #ifdef CONFIG_SYSCTL
1715 void xfrm_sysctl_fini(struct net *net);
1716 #else
1717 static inline void xfrm_sysctl_fini(struct net *net)
1718 {
1719 }
1720 #endif
1721 
1722 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
1723 			  struct xfrm_address_filter *filter);
1724 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1725 		    int (*func)(struct xfrm_state *, int, void*), void *);
1726 bool xfrm_state_walk_batch(struct net *net, struct xfrm_state_walk *walk,
1727 			   struct xfrm_state_walk_batch *batch);
1728 bool xfrm_state_walk_rewind(struct xfrm_state_walk *walk, struct xfrm_state *x);
1729 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net);
1730 struct xfrm_state *xfrm_state_alloc(struct net *net);
1731 void xfrm_state_free(struct xfrm_state *x);
1732 struct xfrm_state *xfrm_state_find(const xfrm_address_t *daddr,
1733 				   const xfrm_address_t *saddr,
1734 				   const struct flowi *fl,
1735 				   struct xfrm_tmpl *tmpl,
1736 				   struct xfrm_policy *pol, int *err,
1737 				   unsigned short family, u32 if_id);
1738 struct xfrm_state *xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
1739 				       xfrm_address_t *daddr,
1740 				       xfrm_address_t *saddr,
1741 				       unsigned short family,
1742 				       u8 mode, u8 proto, u32 reqid);
1743 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1744 					      unsigned short family);
1745 int xfrm_state_check_expire(struct xfrm_state *x);
1746 void xfrm_state_update_stats(struct net *net);
1747 #ifdef CONFIG_XFRM_OFFLOAD
1748 static inline void xfrm_dev_state_update_stats(struct xfrm_state *x)
1749 {
1750 	struct xfrm_dev_offload *xdo = &x->xso;
1751 	struct net_device *dev = READ_ONCE(xdo->dev);
1752 
1753 	lockdep_assert_held(&x->lock);
1754 
1755 	if (dev && dev->xfrmdev_ops &&
1756 	    dev->xfrmdev_ops->xdo_dev_state_update_stats)
1757 		dev->xfrmdev_ops->xdo_dev_state_update_stats(x);
1758 
1759 }
1760 #else
1761 static inline void xfrm_dev_state_update_stats(struct xfrm_state *x) {}
1762 #endif
1763 void xfrm_state_insert(struct xfrm_state *x);
1764 int xfrm_state_add(struct xfrm_state *x);
1765 int xfrm_state_update(struct xfrm_state *x);
1766 struct xfrm_state *xfrm_state_lookup(struct net *net, u32 mark,
1767 				     const xfrm_address_t *daddr, __be32 spi,
1768 				     u8 proto, unsigned short family);
1769 struct xfrm_state *xfrm_input_state_lookup(struct net *net, u32 mark,
1770 					   const xfrm_address_t *daddr,
1771 					   __be32 spi, u8 proto,
1772 					   unsigned short family);
1773 struct xfrm_state *xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1774 					    const xfrm_address_t *daddr,
1775 					    const xfrm_address_t *saddr,
1776 					    u8 proto,
1777 					    unsigned short family);
1778 struct xfrm_state *xfrm_state_lookup_exact(struct net *net, const struct xfrm_mark *mark,
1779 					   const xfrm_address_t *daddr, __be32 spi,
1780 					   u8 proto, unsigned short family);
1781 bool xfrm_state_mark_collides(struct net *net, u32 mark,
1782 			      const xfrm_address_t *daddr, __be32 spi,
1783 			      u8 proto, unsigned short family,
1784 			      const struct xfrm_state *self);
1785 #ifdef CONFIG_XFRM_SUB_POLICY
1786 void xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1787 		    unsigned short family);
1788 void xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1789 		     unsigned short family);
1790 #else
1791 static inline void xfrm_tmpl_sort(struct xfrm_tmpl **d, struct xfrm_tmpl **s,
1792 				  int n, unsigned short family)
1793 {
1794 }
1795 
1796 static inline void xfrm_state_sort(struct xfrm_state **d, struct xfrm_state **s,
1797 				   int n, unsigned short family)
1798 {
1799 }
1800 #endif
1801 
1802 struct xfrmk_sadinfo {
1803 	u32 sadhcnt; /* current hash bkts */
1804 	u32 sadhmcnt; /* max allowed hash bkts */
1805 	u32 sadcnt; /* current running count */
1806 };
1807 
1808 struct xfrmk_spdinfo {
1809 	u32 incnt;
1810 	u32 outcnt;
1811 	u32 fwdcnt;
1812 	u32 inscnt;
1813 	u32 outscnt;
1814 	u32 fwdscnt;
1815 	u32 spdhcnt;
1816 	u32 spdhmcnt;
1817 };
1818 
1819 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq, u32 pcpu_num);
1820 int xfrm_state_delete(struct xfrm_state *x);
1821 int xfrm_state_flush(struct net *net, u8 proto, bool task_valid);
1822 int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid);
1823 int xfrm_dev_policy_flush(struct net *net, struct net_device *dev,
1824 			  bool task_valid);
1825 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si);
1826 void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si);
1827 u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq);
1828 int xfrm_init_replay(struct xfrm_state *x, struct netlink_ext_ack *extack);
1829 u32 xfrm_state_mtu(struct xfrm_state *x, int mtu);
1830 int __xfrm_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack);
1831 int xfrm_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack);
1832 int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type);
1833 int xfrm_input_resume(struct sk_buff *skb, int nexthdr);
1834 int xfrm_trans_queue_net(struct net *net, struct sk_buff *skb,
1835 			 int (*finish)(struct net *, struct sock *,
1836 				       struct sk_buff *));
1837 int xfrm_trans_queue(struct sk_buff *skb,
1838 		     int (*finish)(struct net *, struct sock *,
1839 				   struct sk_buff *));
1840 int xfrm_output_resume(struct sock *sk, struct sk_buff *skb, int err);
1841 int xfrm_output(struct sock *sk, struct sk_buff *skb);
1842 int xfrm4_tunnel_check_size(struct sk_buff *skb);
1843 #if IS_ENABLED(CONFIG_IPV6)
1844 int xfrm6_tunnel_check_size(struct sk_buff *skb);
1845 #else
1846 static inline int xfrm6_tunnel_check_size(struct sk_buff *skb)
1847 {
1848 	return -EMSGSIZE;
1849 }
1850 #endif
1851 
1852 #if IS_ENABLED(CONFIG_NET_PKTGEN)
1853 int pktgen_xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb);
1854 #endif
1855 
1856 void xfrm_local_error(struct sk_buff *skb, int mtu);
1857 int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
1858 		    int encap_type);
1859 int xfrm4_transport_finish(struct sk_buff *skb, int async);
1860 int xfrm4_rcv(struct sk_buff *skb);
1861 
1862 static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
1863 {
1864 	XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
1865 	XFRM_SPI_SKB_CB(skb)->family = AF_INET;
1866 	XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
1867 	return xfrm_input(skb, nexthdr, spi, 0);
1868 }
1869 
1870 int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb);
1871 int xfrm4_protocol_register(struct xfrm4_protocol *handler, unsigned char protocol);
1872 int xfrm4_protocol_deregister(struct xfrm4_protocol *handler, unsigned char protocol);
1873 int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family);
1874 int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family);
1875 void xfrm4_local_error(struct sk_buff *skb, u32 mtu);
1876 int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
1877 		  struct ip6_tnl *t);
1878 int xfrm6_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
1879 		    int encap_type);
1880 int xfrm6_transport_finish(struct sk_buff *skb, int async);
1881 int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t);
1882 int xfrm6_rcv(struct sk_buff *skb);
1883 int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
1884 		     xfrm_address_t *saddr, u8 proto);
1885 void xfrm6_local_error(struct sk_buff *skb, u32 mtu);
1886 int xfrm6_protocol_register(struct xfrm6_protocol *handler, unsigned char protocol);
1887 int xfrm6_protocol_deregister(struct xfrm6_protocol *handler, unsigned char protocol);
1888 int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family);
1889 int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family);
1890 __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr);
1891 __be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr);
1892 int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb);
1893 
1894 #ifdef CONFIG_XFRM
1895 void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu);
1896 int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb);
1897 int xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb);
1898 struct sk_buff *xfrm4_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
1899 					struct sk_buff *skb);
1900 struct sk_buff *xfrm6_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
1901 					struct sk_buff *skb);
1902 int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval,
1903 		     int optlen);
1904 #else
1905 static inline int xfrm_user_policy(struct sock *sk, int optname,
1906 				   sockptr_t optval, int optlen)
1907 {
1908  	return -ENOPROTOOPT;
1909 }
1910 #endif
1911 
1912 struct dst_entry *__xfrm_dst_lookup(int family, const struct xfrm_dst_lookup_params *params);
1913 
1914 struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp);
1915 
1916 void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type);
1917 int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
1918 		     int (*func)(struct xfrm_policy *, int, int, void*),
1919 		     void *);
1920 void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net);
1921 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
1922 struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net,
1923 					  const struct xfrm_mark *mark,
1924 					  u32 if_id, u8 type, int dir,
1925 					  struct xfrm_selector *sel,
1926 					  struct xfrm_sec_ctx *ctx, int delete,
1927 					  int *err);
1928 struct xfrm_policy *xfrm_policy_byid(struct net *net,
1929 				     const struct xfrm_mark *mark, u32 if_id,
1930 				     u8 type, int dir, u32 id, int delete,
1931 				     int *err);
1932 int xfrm_policy_flush(struct net *net, u8 type, bool task_valid);
1933 void xfrm_policy_hash_rebuild(struct net *net);
1934 u32 xfrm_get_acqseq(void);
1935 int verify_spi_info(u8 proto, u32 min, u32 max, struct netlink_ext_ack *extack);
1936 int xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi,
1937 		   struct netlink_ext_ack *extack);
1938 struct xfrm_state *xfrm_find_acq(struct net *net, const struct xfrm_mark *mark,
1939 				 u8 mode, u32 reqid, u32 if_id, u32 pcpu_num, u8 proto,
1940 				 const xfrm_address_t *daddr,
1941 				 const xfrm_address_t *saddr, int create,
1942 				 unsigned short family);
1943 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
1944 
1945 #ifdef CONFIG_XFRM_MIGRATE
1946 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1947 	       const struct xfrm_migrate *m, int num_bundles,
1948 	       const struct xfrm_kmaddress *k, struct net *net,
1949 	       const struct xfrm_encap_tmpl *encap);
1950 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net,
1951 						u32 if_id);
1952 struct xfrm_state *xfrm_state_migrate_create(struct xfrm_state *x,
1953 					     const struct xfrm_migrate *m,
1954 					     struct net *net,
1955 					     struct netlink_ext_ack *extack);
1956 int xfrm_state_migrate_install(const struct xfrm_state *x,
1957 			       struct xfrm_state *xc,
1958 			       const struct xfrm_migrate *m,
1959 			       struct netlink_ext_ack *extack);
1960 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1961 				      struct xfrm_migrate *m,
1962 				      struct net *net,
1963 				      struct netlink_ext_ack *extack);
1964 int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1965 		 struct xfrm_migrate *m, int num_bundles,
1966 		 struct xfrm_kmaddress *k, struct net *net,
1967 		 struct xfrm_encap_tmpl *encap, u32 if_id,
1968 		 struct netlink_ext_ack *extack,
1969 		 struct xfrm_user_offload *xuo);
1970 #endif
1971 
1972 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
1973 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid);
1974 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel,
1975 	      xfrm_address_t *addr);
1976 
1977 void xfrm_input_init(void);
1978 int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
1979 
1980 void xfrm_probe_algs(void);
1981 int xfrm_count_pfkey_auth_supported(void);
1982 int xfrm_count_pfkey_enc_supported(void);
1983 struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx);
1984 struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx);
1985 struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id);
1986 struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id);
1987 struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id);
1988 struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe);
1989 struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe);
1990 struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe);
1991 struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len,
1992 					    int probe);
1993 
1994 static inline bool xfrm6_addr_equal(const xfrm_address_t *a,
1995 				    const xfrm_address_t *b)
1996 {
1997 	return ipv6_addr_equal((const struct in6_addr *)a,
1998 			       (const struct in6_addr *)b);
1999 }
2000 
2001 static inline bool xfrm_addr_equal(const xfrm_address_t *a,
2002 				   const xfrm_address_t *b,
2003 				   sa_family_t family)
2004 {
2005 	switch (family) {
2006 	default:
2007 	case AF_INET:
2008 		return ((__force u32)a->a4 ^ (__force u32)b->a4) == 0;
2009 	case AF_INET6:
2010 		return xfrm6_addr_equal(a, b);
2011 	}
2012 }
2013 
2014 static inline int xfrm_policy_id2dir(u32 index)
2015 {
2016 	return index & 7;
2017 }
2018 
2019 #ifdef CONFIG_XFRM
2020 void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq);
2021 int xfrm_replay_check(struct xfrm_state *x, struct sk_buff *skb, __be32 net_seq);
2022 void xfrm_replay_notify(struct xfrm_state *x, int event);
2023 int xfrm_replay_overflow(struct xfrm_state *x, struct sk_buff *skb);
2024 int xfrm_replay_recheck(struct xfrm_state *x, struct sk_buff *skb, __be32 net_seq);
2025 
2026 static inline int xfrm_aevent_is_on(struct net *net)
2027 {
2028 	struct sock *nlsk;
2029 	int ret = 0;
2030 
2031 	rcu_read_lock();
2032 	nlsk = rcu_dereference(net->xfrm.nlsk);
2033 	if (nlsk)
2034 		ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS);
2035 	rcu_read_unlock();
2036 	return ret;
2037 }
2038 
2039 static inline int xfrm_acquire_is_on(struct net *net)
2040 {
2041 	struct sock *nlsk;
2042 	int ret = 0;
2043 
2044 	rcu_read_lock();
2045 	nlsk = rcu_dereference(net->xfrm.nlsk);
2046 	if (nlsk)
2047 		ret = netlink_has_listeners(nlsk, XFRMNLGRP_ACQUIRE);
2048 	rcu_read_unlock();
2049 
2050 	return ret;
2051 }
2052 #endif
2053 
2054 static inline unsigned int aead_len(struct xfrm_algo_aead *alg)
2055 {
2056 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
2057 }
2058 
2059 static inline unsigned int xfrm_alg_len(const struct xfrm_algo *alg)
2060 {
2061 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
2062 }
2063 
2064 static inline unsigned int xfrm_alg_auth_len(const struct xfrm_algo_auth *alg)
2065 {
2066 	return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
2067 }
2068 
2069 static inline unsigned int xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn)
2070 {
2071 	return sizeof(*replay_esn) + replay_esn->bmp_len * sizeof(__u32);
2072 }
2073 
2074 #ifdef CONFIG_XFRM_MIGRATE
2075 static inline int xfrm_replay_clone(struct xfrm_state *x,
2076 				    const struct xfrm_state *orig)
2077 {
2078 	/* Counters synced later in xfrm_replay_sync() */
2079 
2080 	x->replay = orig->replay;
2081 	x->preplay = orig->preplay;
2082 
2083 	if (orig->replay_esn) {
2084 		x->replay_esn = kmemdup(orig->replay_esn,
2085 				xfrm_replay_state_esn_len(orig->replay_esn),
2086 				GFP_KERNEL);
2087 		if (!x->replay_esn)
2088 			return -ENOMEM;
2089 		x->preplay_esn = kmemdup(orig->preplay_esn,
2090 				xfrm_replay_state_esn_len(orig->preplay_esn),
2091 				GFP_KERNEL);
2092 		if (!x->preplay_esn)
2093 			return -ENOMEM;
2094 	}
2095 
2096 	return 0;
2097 }
2098 
2099 static inline void xfrm_replay_sync(struct xfrm_state *x, const struct xfrm_state *orig)
2100 {
2101 	x->replay = orig->replay;
2102 	x->preplay = orig->preplay;
2103 
2104 	if (orig->replay_esn) {
2105 		memcpy(x->replay_esn, orig->replay_esn,
2106 				xfrm_replay_state_esn_len(orig->replay_esn));
2107 
2108 		memcpy(x->preplay_esn, orig->preplay_esn,
2109 				xfrm_replay_state_esn_len(orig->preplay_esn));
2110 	}
2111 }
2112 
2113 static inline void xfrm_migrate_sync(struct xfrm_state *x,
2114 					  const struct xfrm_state *orig)
2115 {
2116 	/* called under lock so no race conditions or mallocs allowed */
2117 	memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
2118 	xfrm_replay_sync(x, orig);
2119 }
2120 
2121 static inline struct xfrm_algo_aead *xfrm_algo_aead_clone(struct xfrm_algo_aead *orig)
2122 {
2123 	return kmemdup(orig, aead_len(orig), GFP_KERNEL);
2124 }
2125 
2126 
2127 static inline struct xfrm_algo *xfrm_algo_clone(struct xfrm_algo *orig)
2128 {
2129 	return kmemdup(orig, xfrm_alg_len(orig), GFP_KERNEL);
2130 }
2131 
2132 static inline struct xfrm_algo_auth *xfrm_algo_auth_clone(struct xfrm_algo_auth *orig)
2133 {
2134 	return kmemdup(orig, xfrm_alg_auth_len(orig), GFP_KERNEL);
2135 }
2136 
2137 static inline void xfrm_states_put(struct xfrm_state **states, int n)
2138 {
2139 	int i;
2140 	for (i = 0; i < n; i++)
2141 		xfrm_state_put(*(states + i));
2142 }
2143 
2144 static inline void xfrm_states_delete(struct xfrm_state **states, int n)
2145 {
2146 	int i;
2147 	for (i = 0; i < n; i++)
2148 		xfrm_state_delete(*(states + i));
2149 }
2150 #endif
2151 
2152 void __init xfrm_dev_init(void);
2153 
2154 #ifdef CONFIG_XFRM_OFFLOAD
2155 void xfrm_dev_resume(struct sk_buff *skb);
2156 void xfrm_dev_backlog(struct softnet_data *sd);
2157 struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again);
2158 int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
2159 		       const struct xfrm_user_offload *xuo,
2160 		       struct netlink_ext_ack *extack);
2161 int xfrm_dev_policy_add(struct net *net, struct xfrm_policy *xp,
2162 			struct xfrm_user_offload *xuo, u8 dir,
2163 			struct netlink_ext_ack *extack);
2164 bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x);
2165 void xfrm_dev_state_delete(struct xfrm_state *x);
2166 void xfrm_dev_state_free(struct xfrm_state *x);
2167 
2168 static inline void xfrm_dev_state_advance_esn(struct xfrm_state *x)
2169 {
2170 	struct xfrm_dev_offload *xso = &x->xso;
2171 	struct net_device *dev = READ_ONCE(xso->dev);
2172 
2173 	if (dev && dev->xfrmdev_ops->xdo_dev_state_advance_esn)
2174 		dev->xfrmdev_ops->xdo_dev_state_advance_esn(x);
2175 }
2176 
2177 static inline bool xfrm_dst_offload_ok(struct dst_entry *dst)
2178 {
2179 	struct xfrm_state *x = dst->xfrm;
2180 	struct xfrm_dst *xdst;
2181 
2182 	if (!x || !x->type_offload)
2183 		return false;
2184 
2185 	xdst = (struct xfrm_dst *) dst;
2186 	if (!x->xso.offload_handle && !xdst->child->xfrm)
2187 		return true;
2188 	if (x->xso.offload_handle && (x->xso.dev == xfrm_dst_path(dst)->dev) &&
2189 	    !xdst->child->xfrm)
2190 		return true;
2191 
2192 	return false;
2193 }
2194 
2195 static inline void xfrm_dev_policy_delete(struct xfrm_policy *x)
2196 {
2197 	struct xfrm_dev_offload *xdo = &x->xdo;
2198 	struct net_device *dev = xdo->dev;
2199 
2200 	if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_policy_delete)
2201 		dev->xfrmdev_ops->xdo_dev_policy_delete(x);
2202 }
2203 
2204 static inline void xfrm_dev_policy_free(struct xfrm_policy *x)
2205 {
2206 	struct xfrm_dev_offload *xdo = &x->xdo;
2207 	struct net_device *dev = xdo->dev;
2208 
2209 	if (dev && dev->xfrmdev_ops) {
2210 		if (dev->xfrmdev_ops->xdo_dev_policy_free)
2211 			dev->xfrmdev_ops->xdo_dev_policy_free(x);
2212 		xdo->dev = NULL;
2213 		netdev_put(dev, &xdo->dev_tracker);
2214 	}
2215 }
2216 #else
2217 static inline void xfrm_dev_resume(struct sk_buff *skb)
2218 {
2219 }
2220 
2221 static inline void xfrm_dev_backlog(struct softnet_data *sd)
2222 {
2223 }
2224 
2225 static inline struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again)
2226 {
2227 	return skb;
2228 }
2229 
2230 static inline int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
2231 				     const struct xfrm_user_offload *xuo,
2232 				     struct netlink_ext_ack *extack)
2233 {
2234 	return 0;
2235 }
2236 
2237 static inline void xfrm_dev_state_delete(struct xfrm_state *x)
2238 {
2239 }
2240 
2241 static inline void xfrm_dev_state_free(struct xfrm_state *x)
2242 {
2243 }
2244 
2245 static inline int xfrm_dev_policy_add(struct net *net, struct xfrm_policy *xp,
2246 				      struct xfrm_user_offload *xuo, u8 dir,
2247 				      struct netlink_ext_ack *extack)
2248 {
2249 	return 0;
2250 }
2251 
2252 static inline void xfrm_dev_policy_delete(struct xfrm_policy *x)
2253 {
2254 }
2255 
2256 static inline void xfrm_dev_policy_free(struct xfrm_policy *x)
2257 {
2258 }
2259 
2260 static inline bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
2261 {
2262 	return false;
2263 }
2264 
2265 static inline void xfrm_dev_state_advance_esn(struct xfrm_state *x)
2266 {
2267 }
2268 
2269 static inline bool xfrm_dst_offload_ok(struct dst_entry *dst)
2270 {
2271 	return false;
2272 }
2273 #endif
2274 
2275 static inline int xfrm_mark_get(struct nlattr **attrs, struct xfrm_mark *m)
2276 {
2277 	if (attrs[XFRMA_MARK])
2278 		memcpy(m, nla_data(attrs[XFRMA_MARK]), sizeof(struct xfrm_mark));
2279 	else
2280 		m->v = m->m = 0;
2281 
2282 	return m->v & m->m;
2283 }
2284 
2285 static inline int xfrm_mark_put(struct sk_buff *skb, const struct xfrm_mark *m)
2286 {
2287 	int ret = 0;
2288 
2289 	if (m->m | m->v)
2290 		ret = nla_put(skb, XFRMA_MARK, sizeof(struct xfrm_mark), m);
2291 	return ret;
2292 }
2293 
2294 static inline __u32 xfrm_smark_get(__u32 mark, struct xfrm_state *x)
2295 {
2296 	struct xfrm_mark *m = &x->props.smark;
2297 
2298 	return (m->v & m->m) | (mark & ~m->m);
2299 }
2300 
2301 static inline int xfrm_if_id_put(struct sk_buff *skb, __u32 if_id)
2302 {
2303 	int ret = 0;
2304 
2305 	if (if_id)
2306 		ret = nla_put_u32(skb, XFRMA_IF_ID, if_id);
2307 	return ret;
2308 }
2309 
2310 static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,
2311 				    unsigned int family)
2312 {
2313 	bool tunnel = false;
2314 
2315 	switch(family) {
2316 	case AF_INET:
2317 		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
2318 			tunnel = true;
2319 		break;
2320 	case AF_INET6:
2321 		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
2322 			tunnel = true;
2323 		break;
2324 	}
2325 	if (tunnel && !(x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL))
2326 		return -EINVAL;
2327 
2328 	return 0;
2329 }
2330 
2331 extern const int xfrm_msg_min[XFRM_NR_MSGTYPES];
2332 extern const struct nla_policy xfrma_policy[XFRMA_MAX+1];
2333 
2334 struct xfrm_translator {
2335 	/* Allocate frag_list and put compat translation there */
2336 	int (*alloc_compat)(struct sk_buff *skb, const struct nlmsghdr *src);
2337 
2338 	/* Allocate nlmsg with 64-bit translaton of received 32-bit message */
2339 	struct nlmsghdr *(*rcv_msg_compat)(const struct nlmsghdr *nlh,
2340 			int maxtype, const struct nla_policy *policy,
2341 			struct netlink_ext_ack *extack);
2342 
2343 	/* Translate 32-bit user_policy from sockptr */
2344 	int (*xlate_user_policy_sockptr)(u8 **pdata32, int *optlen);
2345 
2346 	struct module *owner;
2347 };
2348 
2349 #if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2350 extern int xfrm_register_translator(struct xfrm_translator *xtr);
2351 extern int xfrm_unregister_translator(struct xfrm_translator *xtr);
2352 extern struct xfrm_translator *xfrm_get_translator(void);
2353 extern void xfrm_put_translator(struct xfrm_translator *xtr);
2354 #else
2355 static inline struct xfrm_translator *xfrm_get_translator(void)
2356 {
2357 	return NULL;
2358 }
2359 static inline void xfrm_put_translator(struct xfrm_translator *xtr)
2360 {
2361 }
2362 #endif
2363 
2364 #if IS_ENABLED(CONFIG_IPV6)
2365 static inline bool xfrm6_local_dontfrag(const struct sock *sk)
2366 {
2367 	int proto;
2368 
2369 	if (!sk || sk->sk_family != AF_INET6)
2370 		return false;
2371 
2372 	proto = sk->sk_protocol;
2373 	if (proto == IPPROTO_UDP || proto == IPPROTO_RAW)
2374 		return inet6_test_bit(DONTFRAG, sk);
2375 
2376 	return false;
2377 }
2378 #endif
2379 
2380 #if (IS_BUILTIN(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \
2381     (IS_MODULE(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
2382 
2383 int register_xfrm_interface_bpf(void);
2384 
2385 #else
2386 
2387 static inline int register_xfrm_interface_bpf(void)
2388 {
2389 	return 0;
2390 }
2391 
2392 #endif
2393 
2394 #if IS_ENABLED(CONFIG_DEBUG_INFO_BTF)
2395 int register_xfrm_state_bpf(void);
2396 #else
2397 static inline int register_xfrm_state_bpf(void)
2398 {
2399 	return 0;
2400 }
2401 #endif
2402 
2403 int xfrm_nat_keepalive_init(unsigned short family);
2404 void xfrm_nat_keepalive_fini(unsigned short family);
2405 int xfrm_nat_keepalive_net_init(struct net *net);
2406 int xfrm_nat_keepalive_net_fini(struct net *net);
2407 void xfrm_nat_keepalive_state_updated(struct xfrm_state *x);
2408 
2409 #endif	/* _NET_XFRM_H */
2410