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