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