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