1
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/rh_kabi.h>
19
20#include <net/sock.h>
21#include <net/dst.h>
22#include <net/ip.h>
23#include <net/route.h>
24#include <net/ipv6.h>
25#include <net/ip6_fib.h>
26#include <net/flow.h>
27#include <net/gro_cells.h>
28
29#include <linux/interrupt.h>
30
31#ifdef CONFIG_XFRM_STATISTICS
32#include <net/snmp.h>
33#endif
34
35#include <linux/rh_kabi.h>
36
37#define XFRM_PROTO_ESP 50
38#define XFRM_PROTO_AH 51
39#define XFRM_PROTO_COMP 108
40#define XFRM_PROTO_IPIP 4
41#define XFRM_PROTO_IPV6 41
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#else
57#define XFRM_INC_STATS(net, field) ((void)(net))
58#endif
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122struct xfrm_state_walk {
123 struct list_head all;
124 u8 state;
125 u8 dying;
126 u8 proto;
127 u32 seq;
128 struct xfrm_address_filter *filter;
129
130 RH_KABI_RESERVE(1)
131};
132
133struct xfrm_state_offload {
134 struct net_device *dev;
135 unsigned long offload_handle;
136 unsigned int num_exthdrs;
137 u8 flags;
138
139 RH_KABI_RESERVE(1)
140 RH_KABI_RESERVE(2)
141 RH_KABI_RESERVE(3)
142 RH_KABI_RESERVE(4)
143};
144
145
146struct tasklet_hrtimer {
147 struct hrtimer timer;
148 struct tasklet_struct tasklet;
149 enum hrtimer_restart (*function)(struct hrtimer *);
150};
151
152
153struct xfrm_state {
154 possible_net_t xs_net;
155 union {
156 struct hlist_node gclist;
157 struct hlist_node bydst;
158 };
159 struct hlist_node bysrc;
160 struct hlist_node byspi;
161
162 refcount_t refcnt;
163 spinlock_t lock;
164
165 struct xfrm_id id;
166 struct xfrm_selector sel;
167 struct xfrm_mark mark;
168 u32 tfcpad;
169
170 u32 genid;
171
172
173 struct xfrm_state_walk km;
174
175
176 struct {
177 u32 reqid;
178 u8 mode;
179 u8 replay_window;
180 u8 aalgo, ealgo, calgo;
181 u8 flags;
182 u16 family;
183 xfrm_address_t saddr;
184 int header_len;
185 int trailer_len;
186 u32 extra_flags;
187 u32 output_mark;
188 } props;
189
190 struct xfrm_lifetime_cfg lft;
191
192
193 struct xfrm_algo_auth *aalg;
194 struct xfrm_algo *ealg;
195 struct xfrm_algo *calg;
196 struct xfrm_algo_aead *aead;
197 const char *geniv;
198
199
200 struct xfrm_encap_tmpl *encap;
201
202
203 xfrm_address_t *coaddr;
204
205
206 struct xfrm_state *tunnel;
207
208
209 atomic_t tunnel_users;
210
211
212 struct xfrm_replay_state replay;
213 struct xfrm_replay_state_esn *replay_esn;
214
215
216 struct xfrm_replay_state preplay;
217 struct xfrm_replay_state_esn *preplay_esn;
218
219
220 const struct xfrm_replay *repl;
221
222
223
224
225 u32 xflags;
226
227
228 u32 replay_maxage;
229 u32 replay_maxdiff;
230
231
232 struct timer_list rtimer;
233
234
235 struct xfrm_stats stats;
236
237 struct xfrm_lifetime_cur curlft;
238 RH_KABI_REPLACE(struct tasklet_hrtimer mtimer, struct hrtimer mtimer)
239
240 struct xfrm_state_offload xso;
241
242
243 long saved_tmo;
244
245
246 RH_KABI_REPLACE(unsigned long lastused, time64_t lastused)
247
248 struct page_frag xfrag;
249
250
251
252 const struct xfrm_type *type;
253 struct xfrm_mode *inner_mode;
254 struct xfrm_mode *inner_mode_iaf;
255 struct xfrm_mode *outer_mode;
256
257 const struct xfrm_type_offload *type_offload;
258
259
260 struct xfrm_sec_ctx *security;
261
262
263
264 void *data;
265
266 RH_KABI_EXTEND(u32 output_mark_mask)
267 RH_KABI_EXTEND(u32 if_id)
268};
269
270static inline struct net *xs_net(struct xfrm_state *x)
271{
272 return read_pnet(&x->xs_net);
273}
274
275
276#define XFRM_TIME_DEFER 1
277#define XFRM_SOFT_EXPIRE 2
278
279enum {
280 XFRM_STATE_VOID,
281 XFRM_STATE_ACQ,
282 XFRM_STATE_VALID,
283 XFRM_STATE_ERROR,
284 XFRM_STATE_EXPIRED,
285 XFRM_STATE_DEAD
286};
287
288
289struct km_event {
290 union {
291 u32 hard;
292 u32 proto;
293 u32 byid;
294 u32 aevent;
295 u32 type;
296 } data;
297
298 u32 seq;
299 u32 portid;
300 u32 event;
301 struct net *net;
302};
303
304struct xfrm_replay {
305 void (*advance)(struct xfrm_state *x, __be32 net_seq);
306 int (*check)(struct xfrm_state *x,
307 struct sk_buff *skb,
308 __be32 net_seq);
309 int (*recheck)(struct xfrm_state *x,
310 struct sk_buff *skb,
311 __be32 net_seq);
312 void (*notify)(struct xfrm_state *x, int event);
313 int (*overflow)(struct xfrm_state *x, struct sk_buff *skb);
314};
315
316struct xfrm_if_cb {
317 struct xfrm_if *(*decode_session)(struct sk_buff *skb,
318 unsigned short family);
319};
320
321void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb);
322void xfrm_if_unregister_cb(void);
323
324struct net_device;
325struct xfrm_type;
326struct xfrm_dst;
327struct xfrm_policy_afinfo {
328 struct dst_ops *dst_ops;
329 struct dst_entry *(*dst_lookup)(struct net *net,
330 int tos, int oif,
331 const xfrm_address_t *saddr,
332 const xfrm_address_t *daddr,
333 u32 mark);
334 int (*get_saddr)(struct net *net, int oif,
335 xfrm_address_t *saddr,
336 xfrm_address_t *daddr,
337 u32 mark);
338 void (*decode_session)(struct sk_buff *skb,
339 struct flowi *fl,
340 int reverse);
341 int (*get_tos)(const struct flowi *fl);
342 int (*init_path)(struct xfrm_dst *path,
343 struct dst_entry *dst,
344 int nfheader_len);
345 int (*fill_dst)(struct xfrm_dst *xdst,
346 struct net_device *dev,
347 const struct flowi *fl);
348 struct dst_entry *(*blackhole_route)(struct net *net, struct dst_entry *orig);
349};
350
351int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family);
352void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo);
353void km_policy_notify(struct xfrm_policy *xp, int dir,
354 const struct km_event *c);
355void km_state_notify(struct xfrm_state *x, const struct km_event *c);
356
357struct xfrm_tmpl;
358int km_query(struct xfrm_state *x, struct xfrm_tmpl *t,
359 struct xfrm_policy *pol);
360void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
361int __xfrm_state_delete(struct xfrm_state *x);
362
363struct xfrm_state_afinfo {
364 unsigned int family;
365 unsigned int proto;
366 __be16 eth_proto;
367 struct module *owner;
368 const struct xfrm_type *type_map[IPPROTO_MAX];
369 const struct xfrm_type_offload *type_offload_map[IPPROTO_MAX];
370 struct xfrm_mode *mode_map[XFRM_MODE_MAX];
371
372 int (*init_flags)(struct xfrm_state *x);
373 void (*init_tempsel)(struct xfrm_selector *sel,
374 const struct flowi *fl);
375 void (*init_temprop)(struct xfrm_state *x,
376 const struct xfrm_tmpl *tmpl,
377 const xfrm_address_t *daddr,
378 const xfrm_address_t *saddr);
379 int (*tmpl_sort)(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n);
380 int (*state_sort)(struct xfrm_state **dst, struct xfrm_state **src, int n);
381 int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
382 int (*output_finish)(struct sock *sk, struct sk_buff *skb);
383 int (*extract_input)(struct xfrm_state *x,
384 struct sk_buff *skb);
385 int (*extract_output)(struct xfrm_state *x,
386 struct sk_buff *skb);
387 int (*transport_finish)(struct sk_buff *skb,
388 int async);
389 void (*local_error)(struct sk_buff *skb, u32 mtu);
390};
391
392int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
393int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
394struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
395struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family);
396
397struct xfrm_input_afinfo {
398 unsigned int family;
399 int (*callback)(struct sk_buff *skb, u8 protocol,
400 int err);
401};
402
403int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo);
404int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo);
405
406void xfrm_flush_gc(void);
407void xfrm_state_delete_tunnel(struct xfrm_state *x);
408
409struct xfrm_type {
410 char *description;
411 struct module *owner;
412 u8 proto;
413 u8 flags;
414#define XFRM_TYPE_NON_FRAGMENT 1
415#define XFRM_TYPE_REPLAY_PROT 2
416#define XFRM_TYPE_LOCAL_COADDR 4
417#define XFRM_TYPE_REMOTE_COADDR 8
418
419 int (*init_state)(struct xfrm_state *x);
420 void (*destructor)(struct xfrm_state *);
421 int (*input)(struct xfrm_state *, struct sk_buff *skb);
422 int (*output)(struct xfrm_state *, struct sk_buff *pskb);
423 int (*reject)(struct xfrm_state *, struct sk_buff *,
424 const struct flowi *);
425 int (*hdr_offset)(struct xfrm_state *, struct sk_buff *, u8 **);
426
427 u32 (*get_mtu)(struct xfrm_state *, int size);
428};
429
430int xfrm_register_type(const struct xfrm_type *type, unsigned short family);
431int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family);
432
433struct xfrm_type_offload {
434 char *description;
435 struct module *owner;
436 u8 proto;
437 void (*encap)(struct xfrm_state *, struct sk_buff *pskb);
438 int (*input_tail)(struct xfrm_state *x, struct sk_buff *skb);
439 int (*xmit)(struct xfrm_state *, struct sk_buff *pskb, netdev_features_t features);
440};
441
442int xfrm_register_type_offload(const struct xfrm_type_offload *type, unsigned short family);
443int xfrm_unregister_type_offload(const struct xfrm_type_offload *type, unsigned short family);
444
445struct xfrm_mode {
446
447
448
449
450
451
452
453
454
455
456
457 int (*input2)(struct xfrm_state *x, struct sk_buff *skb);
458
459
460
461
462
463
464
465
466
467 int (*input)(struct xfrm_state *x, struct sk_buff *skb);
468
469
470
471
472
473
474
475
476
477
478
479
480 int (*output2)(struct xfrm_state *x,struct sk_buff *skb);
481
482
483
484
485
486
487
488
489
490
491 int (*output)(struct xfrm_state *x, struct sk_buff *skb);
492
493
494
495
496 struct sk_buff *(*gso_segment)(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features);
497
498
499
500
501 void (*xmit)(struct xfrm_state *x, struct sk_buff *skb);
502
503 struct xfrm_state_afinfo *afinfo;
504 struct module *owner;
505 unsigned int encap;
506 int flags;
507};
508
509
510enum {
511 XFRM_MODE_FLAG_TUNNEL = 1,
512};
513
514int xfrm_register_mode(struct xfrm_mode *mode, int family);
515int xfrm_unregister_mode(struct xfrm_mode *mode, int family);
516
517static inline int xfrm_af2proto(unsigned int family)
518{
519 switch(family) {
520 case AF_INET:
521 return IPPROTO_IPIP;
522 case AF_INET6:
523 return IPPROTO_IPV6;
524 default:
525 return 0;
526 }
527}
528
529static inline struct xfrm_mode *xfrm_ip2inner_mode(struct xfrm_state *x, int ipproto)
530{
531 if ((ipproto == IPPROTO_IPIP && x->props.family == AF_INET) ||
532 (ipproto == IPPROTO_IPV6 && x->props.family == AF_INET6))
533 return x->inner_mode;
534 else
535 return x->inner_mode_iaf;
536}
537
538struct xfrm_tmpl {
539
540
541
542
543
544
545 struct xfrm_id id;
546
547
548 xfrm_address_t saddr;
549
550 unsigned short encap_family;
551
552 u32 reqid;
553
554
555 u8 mode;
556
557
558 u8 share;
559
560
561 u8 optional;
562
563
564 u8 allalgs;
565
566
567 u32 aalgos;
568 u32 ealgos;
569 u32 calgos;
570};
571
572#define XFRM_MAX_DEPTH 6
573#define XFRM_MAX_OFFLOAD_DEPTH 1
574
575struct xfrm_policy_walk_entry {
576 struct list_head all;
577 u8 dead;
578};
579
580struct xfrm_policy_walk {
581 struct xfrm_policy_walk_entry walk;
582 u8 type;
583 u32 seq;
584};
585
586struct xfrm_policy_queue {
587 struct sk_buff_head hold_queue;
588 struct timer_list hold_timer;
589 unsigned long timeout;
590};
591
592struct xfrm_policy {
593 possible_net_t xp_net;
594 struct hlist_node bydst;
595 struct hlist_node byidx;
596
597
598 rwlock_t lock;
599 refcount_t refcnt;
600 struct timer_list timer;
601
602 atomic_t genid;
603 u32 priority;
604 u32 index;
605 struct xfrm_mark mark;
606 struct xfrm_selector selector;
607 struct xfrm_lifetime_cfg lft;
608 struct xfrm_lifetime_cur curlft;
609 struct xfrm_policy_walk_entry walk;
610 struct xfrm_policy_queue polq;
611 u8 type;
612 u8 action;
613 u8 flags;
614 u8 xfrm_nr;
615 u16 family;
616 struct xfrm_sec_ctx *security;
617 struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH];
618 struct rcu_head rcu;
619 RH_KABI_EXTEND(u32 if_id)
620 RH_KABI_EXTEND(u32 pos)
621 RH_KABI_EXTEND(struct hlist_node bydst_inexact_list)
622 RH_KABI_EXTEND(bool bydst_reinsert)
623};
624
625static inline struct net *xp_net(const struct xfrm_policy *xp)
626{
627 return read_pnet(&xp->xp_net);
628}
629
630struct xfrm_kmaddress {
631 xfrm_address_t local;
632 xfrm_address_t remote;
633 u32 reserved;
634 u16 family;
635};
636
637struct xfrm_migrate {
638 xfrm_address_t old_daddr;
639 xfrm_address_t old_saddr;
640 xfrm_address_t new_daddr;
641 xfrm_address_t new_saddr;
642 u8 proto;
643 u8 mode;
644 u16 reserved;
645 u32 reqid;
646 u16 old_family;
647 u16 new_family;
648};
649
650#define XFRM_KM_TIMEOUT 30
651
652#define XFRM_REPLAY_UPDATE XFRM_AE_CR
653#define XFRM_REPLAY_TIMEOUT XFRM_AE_CE
654
655
656#define XFRM_AE_ETIME 10
657
658#define XFRM_AE_ETH_M 10
659
660#define XFRM_AE_SEQT_SIZE 2
661
662struct xfrm_mgr {
663 struct list_head list;
664 int (*notify)(struct xfrm_state *x, const struct km_event *c);
665 int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp);
666 struct xfrm_policy *(*compile_policy)(struct sock *sk, int opt, u8 *data, int len, int *dir);
667 int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
668 int (*notify_policy)(struct xfrm_policy *x, int dir, const struct km_event *c);
669 int (*report)(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr);
670 int (*migrate)(const struct xfrm_selector *sel,
671 u8 dir, u8 type,
672 const struct xfrm_migrate *m,
673 int num_bundles,
674 const struct xfrm_kmaddress *k,
675 const struct xfrm_encap_tmpl *encap);
676 bool (*is_alive)(const struct km_event *c);
677};
678
679int xfrm_register_km(struct xfrm_mgr *km);
680int xfrm_unregister_km(struct xfrm_mgr *km);
681
682struct xfrm_tunnel_skb_cb {
683 union {
684 struct inet_skb_parm h4;
685 struct inet6_skb_parm h6;
686 } header;
687
688 union {
689 struct ip_tunnel *ip4;
690 struct ip6_tnl *ip6;
691 } tunnel;
692};
693
694#define XFRM_TUNNEL_SKB_CB(__skb) ((struct xfrm_tunnel_skb_cb *)&((__skb)->cb[0]))
695
696
697
698
699
700
701struct xfrm_skb_cb {
702 struct xfrm_tunnel_skb_cb header;
703
704
705 union {
706 struct {
707 __u32 low;
708 __u32 hi;
709 } output;
710 struct {
711 __be32 low;
712 __be32 hi;
713 } input;
714 } seq;
715};
716
717#define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0]))
718
719
720
721
722
723struct xfrm_mode_skb_cb {
724 struct xfrm_tunnel_skb_cb header;
725
726
727 __be16 id;
728 __be16 frag_off;
729
730
731 u8 ihl;
732
733
734 u8 tos;
735
736
737 u8 ttl;
738
739
740 u8 protocol;
741
742
743 u8 optlen;
744
745
746 u8 flow_lbl[3];
747};
748
749#define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0]))
750
751
752
753
754
755struct xfrm_spi_skb_cb {
756 struct xfrm_tunnel_skb_cb header;
757
758 unsigned int daddroff;
759 unsigned int family;
760 __be32 seq;
761};
762
763#define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0]))
764
765#ifdef CONFIG_AUDITSYSCALL
766static inline struct audit_buffer *xfrm_audit_start(const char *op)
767{
768 struct audit_buffer *audit_buf = NULL;
769
770 if (audit_enabled == AUDIT_OFF)
771 return NULL;
772 audit_buf = audit_log_start(audit_context(), GFP_ATOMIC,
773 AUDIT_MAC_IPSEC_EVENT);
774 if (audit_buf == NULL)
775 return NULL;
776 audit_log_format(audit_buf, "op=%s", op);
777 return audit_buf;
778}
779
780static inline void xfrm_audit_helper_usrinfo(bool task_valid,
781 struct audit_buffer *audit_buf)
782{
783 const unsigned int auid = from_kuid(&init_user_ns, task_valid ?
784 audit_get_loginuid(current) :
785 INVALID_UID);
786 const unsigned int ses = task_valid ? audit_get_sessionid(current) :
787 AUDIT_SID_UNSET;
788
789 audit_log_format(audit_buf, " auid=%u ses=%u", auid, ses);
790 audit_log_task_context(audit_buf);
791}
792
793void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid);
794void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
795 bool task_valid);
796void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid);
797void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid);
798void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
799 struct sk_buff *skb);
800void xfrm_audit_state_replay(struct xfrm_state *x, struct sk_buff *skb,
801 __be32 net_seq);
802void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family);
803void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, __be32 net_spi,
804 __be32 net_seq);
805void xfrm_audit_state_icvfail(struct xfrm_state *x, struct sk_buff *skb,
806 u8 proto);
807#else
808
809static inline void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
810 bool task_valid)
811{
812}
813
814static inline void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
815 bool task_valid)
816{
817}
818
819static inline void xfrm_audit_state_add(struct xfrm_state *x, int result,
820 bool task_valid)
821{
822}
823
824static inline void xfrm_audit_state_delete(struct xfrm_state *x, int result,
825 bool task_valid)
826{
827}
828
829static inline void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
830 struct sk_buff *skb)
831{
832}
833
834static inline void xfrm_audit_state_replay(struct xfrm_state *x,
835 struct sk_buff *skb, __be32 net_seq)
836{
837}
838
839static inline void xfrm_audit_state_notfound_simple(struct sk_buff *skb,
840 u16 family)
841{
842}
843
844static inline void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
845 __be32 net_spi, __be32 net_seq)
846{
847}
848
849static inline void xfrm_audit_state_icvfail(struct xfrm_state *x,
850 struct sk_buff *skb, u8 proto)
851{
852}
853#endif
854
855static inline void xfrm_pol_hold(struct xfrm_policy *policy)
856{
857 if (likely(policy != NULL))
858 refcount_inc(&policy->refcnt);
859}
860
861void xfrm_policy_destroy(struct xfrm_policy *policy);
862
863static inline void xfrm_pol_put(struct xfrm_policy *policy)
864{
865 if (refcount_dec_and_test(&policy->refcnt))
866 xfrm_policy_destroy(policy);
867}
868
869static inline void xfrm_pols_put(struct xfrm_policy **pols, int npols)
870{
871 int i;
872 for (i = npols - 1; i >= 0; --i)
873 xfrm_pol_put(pols[i]);
874}
875
876void __xfrm_state_destroy(struct xfrm_state *, bool);
877
878static inline void __xfrm_state_put(struct xfrm_state *x)
879{
880 refcount_dec(&x->refcnt);
881}
882
883static inline void xfrm_state_put(struct xfrm_state *x)
884{
885 if (refcount_dec_and_test(&x->refcnt))
886 __xfrm_state_destroy(x, false);
887}
888
889static inline void xfrm_state_put_sync(struct xfrm_state *x)
890{
891 if (refcount_dec_and_test(&x->refcnt))
892 __xfrm_state_destroy(x, true);
893}
894
895static inline void xfrm_state_hold(struct xfrm_state *x)
896{
897 refcount_inc(&x->refcnt);
898}
899
900static inline bool addr_match(const void *token1, const void *token2,
901 unsigned int prefixlen)
902{
903 const __be32 *a1 = token1;
904 const __be32 *a2 = token2;
905 unsigned int pdw;
906 unsigned int pbi;
907
908 pdw = prefixlen >> 5;
909 pbi = prefixlen & 0x1f;
910
911 if (pdw)
912 if (memcmp(a1, a2, pdw << 2))
913 return false;
914
915 if (pbi) {
916 __be32 mask;
917
918 mask = htonl((0xffffffff) << (32 - pbi));
919
920 if ((a1[pdw] ^ a2[pdw]) & mask)
921 return false;
922 }
923
924 return true;
925}
926
927static inline bool addr4_match(__be32 a1, __be32 a2, u8 prefixlen)
928{
929
930 if (sizeof(long) == 4 && prefixlen == 0)
931 return true;
932 return !((a1 ^ a2) & htonl(~0UL << (32 - prefixlen)));
933}
934
935static __inline__
936__be16 xfrm_flowi_sport(const struct flowi *fl, const union flowi_uli *uli)
937{
938 __be16 port;
939 switch(fl->flowi_proto) {
940 case IPPROTO_TCP:
941 case IPPROTO_UDP:
942 case IPPROTO_UDPLITE:
943 case IPPROTO_SCTP:
944 port = uli->ports.sport;
945 break;
946 case IPPROTO_ICMP:
947 case IPPROTO_ICMPV6:
948 port = htons(uli->icmpt.type);
949 break;
950 case IPPROTO_MH:
951 port = htons(uli->mht.type);
952 break;
953 case IPPROTO_GRE:
954 port = htons(ntohl(uli->gre_key) >> 16);
955 break;
956 default:
957 port = 0;
958 }
959 return port;
960}
961
962static __inline__
963__be16 xfrm_flowi_dport(const struct flowi *fl, const union flowi_uli *uli)
964{
965 __be16 port;
966 switch(fl->flowi_proto) {
967 case IPPROTO_TCP:
968 case IPPROTO_UDP:
969 case IPPROTO_UDPLITE:
970 case IPPROTO_SCTP:
971 port = uli->ports.dport;
972 break;
973 case IPPROTO_ICMP:
974 case IPPROTO_ICMPV6:
975 port = htons(uli->icmpt.code);
976 break;
977 case IPPROTO_GRE:
978 port = htons(ntohl(uli->gre_key) & 0xffff);
979 break;
980 default:
981 port = 0;
982 }
983 return port;
984}
985
986bool xfrm_selector_match(const struct xfrm_selector *sel,
987 const struct flowi *fl, unsigned short family);
988
989#ifdef CONFIG_SECURITY_NETWORK_XFRM
990
991
992
993static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
994{
995 return ((!s1 && !s2) ||
996 (s1 && s2 &&
997 (s1->ctx_sid == s2->ctx_sid) &&
998 (s1->ctx_doi == s2->ctx_doi) &&
999 (s1->ctx_alg == s2->ctx_alg)));
1000}
1001#else
1002static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
1003{
1004 return true;
1005}
1006#endif
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019struct xfrm_dst {
1020 union {
1021 struct dst_entry dst;
1022 struct rtable rt;
1023 struct rt6_info rt6;
1024 } u;
1025 struct dst_entry *route;
1026 struct dst_entry *child;
1027 struct dst_entry *path;
1028 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1029 int num_pols, num_xfrms;
1030 u32 xfrm_genid;
1031 u32 policy_genid;
1032 u32 route_mtu_cached;
1033 u32 child_mtu_cached;
1034 u32 route_cookie;
1035 u32 path_cookie;
1036};
1037
1038static inline struct dst_entry *xfrm_dst_path(const struct dst_entry *dst)
1039{
1040#ifdef CONFIG_XFRM
1041 if (dst->xfrm) {
1042 const struct xfrm_dst *xdst = (const struct xfrm_dst *) dst;
1043
1044 return xdst->path;
1045 }
1046#endif
1047 return (struct dst_entry *) dst;
1048}
1049
1050static inline struct dst_entry *xfrm_dst_child(const struct dst_entry *dst)
1051{
1052#ifdef CONFIG_XFRM
1053 if (dst->xfrm) {
1054 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1055 return xdst->child;
1056 }
1057#endif
1058 return NULL;
1059}
1060
1061#ifdef CONFIG_XFRM
1062static inline void xfrm_dst_set_child(struct xfrm_dst *xdst, struct dst_entry *child)
1063{
1064 xdst->child = child;
1065}
1066
1067static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)
1068{
1069 xfrm_pols_put(xdst->pols, xdst->num_pols);
1070 dst_release(xdst->route);
1071 if (likely(xdst->u.dst.xfrm))
1072 xfrm_state_put(xdst->u.dst.xfrm);
1073}
1074#endif
1075
1076void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
1077
1078struct xfrm_if_parms {
1079 int link;
1080 u32 if_id;
1081};
1082
1083struct xfrm_if {
1084 struct xfrm_if __rcu *next;
1085 struct net_device *dev;
1086 struct net *net;
1087 struct xfrm_if_parms p;
1088
1089 struct gro_cells gro_cells;
1090};
1091
1092struct xfrm_offload {
1093
1094 struct {
1095 __u32 low;
1096 __u32 hi;
1097 } seq;
1098
1099 __u32 flags;
1100#define SA_DELETE_REQ 1
1101#define CRYPTO_DONE 2
1102#define CRYPTO_NEXT_DONE 4
1103#define CRYPTO_FALLBACK 8
1104#define XFRM_GSO_SEGMENT 16
1105#define XFRM_GRO 32
1106#define XFRM_ESP_NO_TRAILER 64
1107#define XFRM_DEV_RESUME 128
1108
1109 __u32 status;
1110#define CRYPTO_SUCCESS 1
1111#define CRYPTO_GENERIC_ERROR 2
1112#define CRYPTO_TRANSPORT_AH_AUTH_FAILED 4
1113#define CRYPTO_TRANSPORT_ESP_AUTH_FAILED 8
1114#define CRYPTO_TUNNEL_AH_AUTH_FAILED 16
1115#define CRYPTO_TUNNEL_ESP_AUTH_FAILED 32
1116#define CRYPTO_INVALID_PACKET_SYNTAX 64
1117#define CRYPTO_INVALID_PROTOCOL 128
1118
1119 __u8 proto;
1120};
1121
1122struct sec_path {
1123
1124
1125
1126
1127
1128 refcount_t refcnt;
1129 int len;
1130 int olen;
1131
1132 struct xfrm_state *xvec[XFRM_MAX_DEPTH];
1133 struct xfrm_offload ovec[XFRM_MAX_OFFLOAD_DEPTH];
1134};
1135
1136static_assert(offsetof(struct sec_path, refcnt) == 0,
1137 "The position of field refcnt in struct sec_path was changed. "
1138 "Please look on its declaration in " __FILE__
1139 " for more details.");
1140
1141static inline int secpath_exists(struct sk_buff *skb)
1142{
1143#ifdef CONFIG_XFRM
1144 return skb->sp != NULL;
1145#else
1146 return 0;
1147#endif
1148}
1149
1150static inline struct sec_path *
1151secpath_get(struct sec_path *sp)
1152{
1153 if (sp)
1154 refcount_inc(&sp->refcnt);
1155 return sp;
1156}
1157
1158void __secpath_destroy(struct sec_path *sp);
1159
1160static inline void
1161secpath_put(struct sec_path *sp)
1162{
1163
1164
1165
1166
1167
1168 if (sp && refcount_dec_and_test(&sp->refcnt))
1169 __secpath_destroy(sp);
1170}
1171
1172struct sec_path *secpath_dup(struct sec_path *src);
1173int secpath_set(struct sk_buff *skb);
1174
1175static inline void
1176secpath_reset(struct sk_buff *skb)
1177{
1178#ifdef CONFIG_XFRM
1179 secpath_put(skb->sp);
1180 skb->sp = NULL;
1181#endif
1182}
1183
1184static inline int
1185xfrm_addr_any(const xfrm_address_t *addr, unsigned short family)
1186{
1187 switch (family) {
1188 case AF_INET:
1189 return addr->a4 == 0;
1190 case AF_INET6:
1191 return ipv6_addr_any(&addr->in6);
1192 }
1193 return 0;
1194}
1195
1196static inline int
1197__xfrm4_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
1198{
1199 return (tmpl->saddr.a4 &&
1200 tmpl->saddr.a4 != x->props.saddr.a4);
1201}
1202
1203static inline int
1204__xfrm6_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
1205{
1206 return (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
1207 !ipv6_addr_equal((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
1208}
1209
1210static inline int
1211xfrm_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, unsigned short family)
1212{
1213 switch (family) {
1214 case AF_INET:
1215 return __xfrm4_state_addr_cmp(tmpl, x);
1216 case AF_INET6:
1217 return __xfrm6_state_addr_cmp(tmpl, x);
1218 }
1219 return !0;
1220}
1221
1222#ifdef CONFIG_XFRM
1223int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb,
1224 unsigned short family);
1225
1226static inline int __xfrm_policy_check2(struct sock *sk, int dir,
1227 struct sk_buff *skb,
1228 unsigned int family, int reverse)
1229{
1230 struct net *net = dev_net(skb->dev);
1231 int ndir = dir | (reverse ? XFRM_POLICY_MASK + 1 : 0);
1232
1233 if (sk && sk->sk_policy[XFRM_POLICY_IN])
1234 return __xfrm_policy_check(sk, ndir, skb, family);
1235
1236 return (!net->xfrm.policy_count[dir] && !skb->sp) ||
1237 (skb_dst(skb)->flags & DST_NOPOLICY) ||
1238 __xfrm_policy_check(sk, ndir, skb, family);
1239}
1240
1241static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
1242{
1243 return __xfrm_policy_check2(sk, dir, skb, family, 0);
1244}
1245
1246static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
1247{
1248 return xfrm_policy_check(sk, dir, skb, AF_INET);
1249}
1250
1251static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
1252{
1253 return xfrm_policy_check(sk, dir, skb, AF_INET6);
1254}
1255
1256static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1257 struct sk_buff *skb)
1258{
1259 return __xfrm_policy_check2(sk, dir, skb, AF_INET, 1);
1260}
1261
1262static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1263 struct sk_buff *skb)
1264{
1265 return __xfrm_policy_check2(sk, dir, skb, AF_INET6, 1);
1266}
1267
1268int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1269 unsigned int family, int reverse);
1270
1271static inline int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1272 unsigned int family)
1273{
1274 return __xfrm_decode_session(skb, fl, family, 0);
1275}
1276
1277static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
1278 struct flowi *fl,
1279 unsigned int family)
1280{
1281 return __xfrm_decode_session(skb, fl, family, 1);
1282}
1283
1284int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
1285
1286static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1287{
1288 struct net *net = dev_net(skb->dev);
1289
1290 return !net->xfrm.policy_count[XFRM_POLICY_OUT] ||
1291 (skb_dst(skb)->flags & DST_NOXFRM) ||
1292 __xfrm_route_forward(skb, family);
1293}
1294
1295static inline int xfrm4_route_forward(struct sk_buff *skb)
1296{
1297 return xfrm_route_forward(skb, AF_INET);
1298}
1299
1300static inline int xfrm6_route_forward(struct sk_buff *skb)
1301{
1302 return xfrm_route_forward(skb, AF_INET6);
1303}
1304
1305int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk);
1306
1307static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
1308{
1309 sk->sk_policy[0] = NULL;
1310 sk->sk_policy[1] = NULL;
1311 if (unlikely(osk->sk_policy[0] || osk->sk_policy[1]))
1312 return __xfrm_sk_clone_policy(sk, osk);
1313 return 0;
1314}
1315
1316int xfrm_policy_delete(struct xfrm_policy *pol, int dir);
1317
1318static inline void xfrm_sk_free_policy(struct sock *sk)
1319{
1320 struct xfrm_policy *pol;
1321
1322 pol = rcu_dereference_protected(sk->sk_policy[0], 1);
1323 if (unlikely(pol != NULL)) {
1324 xfrm_policy_delete(pol, XFRM_POLICY_MAX);
1325 sk->sk_policy[0] = NULL;
1326 }
1327 pol = rcu_dereference_protected(sk->sk_policy[1], 1);
1328 if (unlikely(pol != NULL)) {
1329 xfrm_policy_delete(pol, XFRM_POLICY_MAX+1);
1330 sk->sk_policy[1] = NULL;
1331 }
1332}
1333
1334#else
1335
1336static inline void xfrm_sk_free_policy(struct sock *sk) {}
1337static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) { return 0; }
1338static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }
1339static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; }
1340static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
1341{
1342 return 1;
1343}
1344static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
1345{
1346 return 1;
1347}
1348static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
1349{
1350 return 1;
1351}
1352static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
1353 struct flowi *fl,
1354 unsigned int family)
1355{
1356 return -ENOSYS;
1357}
1358static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
1359 struct sk_buff *skb)
1360{
1361 return 1;
1362}
1363static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
1364 struct sk_buff *skb)
1365{
1366 return 1;
1367}
1368#endif
1369
1370static __inline__
1371xfrm_address_t *xfrm_flowi_daddr(const struct flowi *fl, unsigned short family)
1372{
1373 switch (family){
1374 case AF_INET:
1375 return (xfrm_address_t *)&fl->u.ip4.daddr;
1376 case AF_INET6:
1377 return (xfrm_address_t *)&fl->u.ip6.daddr;
1378 }
1379 return NULL;
1380}
1381
1382static __inline__
1383xfrm_address_t *xfrm_flowi_saddr(const struct flowi *fl, unsigned short family)
1384{
1385 switch (family){
1386 case AF_INET:
1387 return (xfrm_address_t *)&fl->u.ip4.saddr;
1388 case AF_INET6:
1389 return (xfrm_address_t *)&fl->u.ip6.saddr;
1390 }
1391 return NULL;
1392}
1393
1394static __inline__
1395void xfrm_flowi_addr_get(const struct flowi *fl,
1396 xfrm_address_t *saddr, xfrm_address_t *daddr,
1397 unsigned short family)
1398{
1399 switch(family) {
1400 case AF_INET:
1401 memcpy(&saddr->a4, &fl->u.ip4.saddr, sizeof(saddr->a4));
1402 memcpy(&daddr->a4, &fl->u.ip4.daddr, sizeof(daddr->a4));
1403 break;
1404 case AF_INET6:
1405 saddr->in6 = fl->u.ip6.saddr;
1406 daddr->in6 = fl->u.ip6.daddr;
1407 break;
1408 }
1409}
1410
1411static __inline__ int
1412__xfrm4_state_addr_check(const struct xfrm_state *x,
1413 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
1414{
1415 if (daddr->a4 == x->id.daddr.a4 &&
1416 (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
1417 return 1;
1418 return 0;
1419}
1420
1421static __inline__ int
1422__xfrm6_state_addr_check(const struct xfrm_state *x,
1423 const xfrm_address_t *daddr, const xfrm_address_t *saddr)
1424{
1425 if (ipv6_addr_equal((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
1426 (ipv6_addr_equal((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr) ||
1427 ipv6_addr_any((struct in6_addr *)saddr) ||
1428 ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
1429 return 1;
1430 return 0;
1431}
1432
1433static __inline__ int
1434xfrm_state_addr_check(const struct xfrm_state *x,
1435 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1436 unsigned short family)
1437{
1438 switch (family) {
1439 case AF_INET:
1440 return __xfrm4_state_addr_check(x, daddr, saddr);
1441 case AF_INET6:
1442 return __xfrm6_state_addr_check(x, daddr, saddr);
1443 }
1444 return 0;
1445}
1446
1447static __inline__ int
1448xfrm_state_addr_flow_check(const struct xfrm_state *x, const struct flowi *fl,
1449 unsigned short family)
1450{
1451 switch (family) {
1452 case AF_INET:
1453 return __xfrm4_state_addr_check(x,
1454 (const xfrm_address_t *)&fl->u.ip4.daddr,
1455 (const xfrm_address_t *)&fl->u.ip4.saddr);
1456 case AF_INET6:
1457 return __xfrm6_state_addr_check(x,
1458 (const xfrm_address_t *)&fl->u.ip6.daddr,
1459 (const xfrm_address_t *)&fl->u.ip6.saddr);
1460 }
1461 return 0;
1462}
1463
1464static inline int xfrm_state_kern(const struct xfrm_state *x)
1465{
1466 return atomic_read(&x->tunnel_users);
1467}
1468
1469static inline bool xfrm_id_proto_valid(u8 proto)
1470{
1471 switch (proto) {
1472 case IPPROTO_AH:
1473 case IPPROTO_ESP:
1474 case IPPROTO_COMP:
1475#if IS_ENABLED(CONFIG_IPV6)
1476 case IPPROTO_ROUTING:
1477 case IPPROTO_DSTOPTS:
1478#endif
1479 return true;
1480 default:
1481 return false;
1482 }
1483}
1484
1485
1486static inline int xfrm_id_proto_match(u8 proto, u8 userproto)
1487{
1488 return (!userproto || proto == userproto ||
1489 (userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH ||
1490 proto == IPPROTO_ESP ||
1491 proto == IPPROTO_COMP)));
1492}
1493
1494
1495
1496
1497struct xfrm_algo_aead_info {
1498 char *geniv;
1499 u16 icv_truncbits;
1500};
1501
1502struct xfrm_algo_auth_info {
1503 u16 icv_truncbits;
1504 u16 icv_fullbits;
1505};
1506
1507struct xfrm_algo_encr_info {
1508 char *geniv;
1509 u16 blockbits;
1510 u16 defkeybits;
1511};
1512
1513struct xfrm_algo_comp_info {
1514 u16 threshold;
1515};
1516
1517struct xfrm_algo_desc {
1518 char *name;
1519 char *compat;
1520 u8 available:1;
1521 u8 pfkey_supported:1;
1522 union {
1523 struct xfrm_algo_aead_info aead;
1524 struct xfrm_algo_auth_info auth;
1525 struct xfrm_algo_encr_info encr;
1526 struct xfrm_algo_comp_info comp;
1527 } uinfo;
1528 struct sadb_alg desc;
1529};
1530
1531
1532struct xfrm4_protocol {
1533 int (*handler)(struct sk_buff *skb);
1534 int (*input_handler)(struct sk_buff *skb, int nexthdr, __be32 spi,
1535 int encap_type);
1536 int (*cb_handler)(struct sk_buff *skb, int err);
1537 int (*err_handler)(struct sk_buff *skb, u32 info);
1538
1539 struct xfrm4_protocol __rcu *next;
1540 int priority;
1541};
1542
1543struct xfrm6_protocol {
1544 int (*handler)(struct sk_buff *skb);
1545 int (*cb_handler)(struct sk_buff *skb, int err);
1546 int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
1547 u8 type, u8 code, int offset, __be32 info);
1548
1549 struct xfrm6_protocol __rcu *next;
1550 int priority;
1551};
1552
1553
1554struct xfrm_tunnel {
1555 int (*handler)(struct sk_buff *skb);
1556 int (*err_handler)(struct sk_buff *skb, u32 info);
1557
1558 struct xfrm_tunnel __rcu *next;
1559 int priority;
1560};
1561
1562struct xfrm6_tunnel {
1563 int (*handler)(struct sk_buff *skb);
1564 int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
1565 u8 type, u8 code, int offset, __be32 info);
1566 struct xfrm6_tunnel __rcu *next;
1567 int priority;
1568};
1569
1570void xfrm_init(void);
1571void xfrm4_init(void);
1572int xfrm_state_init(struct net *net);
1573void xfrm_state_fini(struct net *net);
1574void xfrm4_state_init(void);
1575void xfrm4_protocol_init(void);
1576#ifdef CONFIG_XFRM
1577int xfrm6_init(void);
1578void xfrm6_fini(void);
1579int xfrm6_state_init(void);
1580void xfrm6_state_fini(void);
1581int xfrm6_protocol_init(void);
1582void xfrm6_protocol_fini(void);
1583#else
1584static inline int xfrm6_init(void)
1585{
1586 return 0;
1587}
1588static inline void xfrm6_fini(void)
1589{
1590 ;
1591}
1592#endif
1593
1594#ifdef CONFIG_XFRM_STATISTICS
1595int xfrm_proc_init(struct net *net);
1596void xfrm_proc_fini(struct net *net);
1597#endif
1598
1599int xfrm_sysctl_init(struct net *net);
1600#ifdef CONFIG_SYSCTL
1601void xfrm_sysctl_fini(struct net *net);
1602#else
1603static inline void xfrm_sysctl_fini(struct net *net)
1604{
1605}
1606#endif
1607
1608void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
1609 struct xfrm_address_filter *filter);
1610int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1611 int (*func)(struct xfrm_state *, int, void*), void *);
1612void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net);
1613struct xfrm_state *xfrm_state_alloc(struct net *net);
1614void xfrm_state_free(struct xfrm_state *x);
1615struct xfrm_state *xfrm_state_find(const xfrm_address_t *daddr,
1616 const xfrm_address_t *saddr,
1617 const struct flowi *fl,
1618 struct xfrm_tmpl *tmpl,
1619 struct xfrm_policy *pol, int *err,
1620 unsigned short family, u32 if_id);
1621struct xfrm_state *xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
1622 xfrm_address_t *daddr,
1623 xfrm_address_t *saddr,
1624 unsigned short family,
1625 u8 mode, u8 proto, u32 reqid);
1626struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1627 unsigned short family);
1628int xfrm_state_check_expire(struct xfrm_state *x);
1629void xfrm_state_insert(struct xfrm_state *x);
1630int xfrm_state_add(struct xfrm_state *x);
1631int xfrm_state_update(struct xfrm_state *x);
1632struct xfrm_state *xfrm_state_lookup(struct net *net, u32 mark,
1633 const xfrm_address_t *daddr, __be32 spi,
1634 u8 proto, unsigned short family);
1635struct xfrm_state *xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1636 const xfrm_address_t *daddr,
1637 const xfrm_address_t *saddr,
1638 u8 proto,
1639 unsigned short family);
1640#ifdef CONFIG_XFRM_SUB_POLICY
1641int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1642 unsigned short family, struct net *net);
1643int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1644 unsigned short family);
1645#else
1646static inline int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src,
1647 int n, unsigned short family, struct net *net)
1648{
1649 return -ENOSYS;
1650}
1651
1652static inline int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src,
1653 int n, unsigned short family)
1654{
1655 return -ENOSYS;
1656}
1657#endif
1658
1659struct xfrmk_sadinfo {
1660 u32 sadhcnt;
1661 u32 sadhmcnt;
1662 u32 sadcnt;
1663};
1664
1665struct xfrmk_spdinfo {
1666 u32 incnt;
1667 u32 outcnt;
1668 u32 fwdcnt;
1669 u32 inscnt;
1670 u32 outscnt;
1671 u32 fwdscnt;
1672 u32 spdhcnt;
1673 u32 spdhmcnt;
1674};
1675
1676struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1677int xfrm_state_delete(struct xfrm_state *x);
1678int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync);
1679int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid);
1680void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si);
1681void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si);
1682u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq);
1683int xfrm_init_replay(struct xfrm_state *x);
1684int xfrm_state_mtu(struct xfrm_state *x, int mtu);
1685int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload);
1686int xfrm_init_state(struct xfrm_state *x);
1687int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb);
1688int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type);
1689int xfrm_input_resume(struct sk_buff *skb, int nexthdr);
1690int xfrm_trans_queue(struct sk_buff *skb,
1691 int (*finish)(struct net *, struct sock *,
1692 struct sk_buff *));
1693int xfrm_output_resume(struct sk_buff *skb, int err);
1694int xfrm_output(struct sock *sk, struct sk_buff *skb);
1695int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1696void xfrm_local_error(struct sk_buff *skb, int mtu);
1697int xfrm4_extract_header(struct sk_buff *skb);
1698int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb);
1699int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
1700 int encap_type);
1701int xfrm4_transport_finish(struct sk_buff *skb, int async);
1702int xfrm4_rcv(struct sk_buff *skb);
1703int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
1704
1705static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
1706{
1707 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
1708 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
1709 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
1710 return xfrm_input(skb, nexthdr, spi, 0);
1711}
1712
1713int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1714int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
1715int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb);
1716int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb);
1717int xfrm4_protocol_register(struct xfrm4_protocol *handler, unsigned char protocol);
1718int xfrm4_protocol_deregister(struct xfrm4_protocol *handler, unsigned char protocol);
1719int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family);
1720int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family);
1721void xfrm4_local_error(struct sk_buff *skb, u32 mtu);
1722int xfrm6_extract_header(struct sk_buff *skb);
1723int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb);
1724int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
1725 struct ip6_tnl *t);
1726int xfrm6_transport_finish(struct sk_buff *skb, int async);
1727int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t);
1728int xfrm6_rcv(struct sk_buff *skb);
1729int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
1730 xfrm_address_t *saddr, u8 proto);
1731void xfrm6_local_error(struct sk_buff *skb, u32 mtu);
1732int xfrm6_protocol_register(struct xfrm6_protocol *handler, unsigned char protocol);
1733int xfrm6_protocol_deregister(struct xfrm6_protocol *handler, unsigned char protocol);
1734int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family);
1735int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family);
1736__be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr);
1737__be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr);
1738int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb);
1739int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
1740int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb);
1741int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb);
1742int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
1743 u8 **prevhdr);
1744
1745#ifdef CONFIG_XFRM
1746int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb);
1747int xfrm_user_policy(struct sock *sk, int optname,
1748 u8 __user *optval, int optlen);
1749#else
1750static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1751{
1752 return -ENOPROTOOPT;
1753}
1754
1755static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
1756{
1757
1758 kfree_skb(skb);
1759 return 0;
1760}
1761#endif
1762
1763struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
1764 const xfrm_address_t *saddr,
1765 const xfrm_address_t *daddr,
1766 int family, u32 mark);
1767
1768struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp);
1769
1770void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type);
1771int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
1772 int (*func)(struct xfrm_policy *, int, int, void*),
1773 void *);
1774void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net);
1775int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
1776struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
1777 u8 type, int dir,
1778 struct xfrm_selector *sel,
1779 struct xfrm_sec_ctx *ctx, int delete,
1780 int *err);
1781struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id, u8,
1782 int dir, u32 id, int delete, int *err);
1783int xfrm_policy_flush(struct net *net, u8 type, bool task_valid);
1784void xfrm_policy_hash_rebuild(struct net *net);
1785u32 xfrm_get_acqseq(void);
1786int verify_spi_info(u8 proto, u32 min, u32 max);
1787int xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi);
1788struct xfrm_state *xfrm_find_acq(struct net *net, const struct xfrm_mark *mark,
1789 u8 mode, u32 reqid, u32 if_id, u8 proto,
1790 const xfrm_address_t *daddr,
1791 const xfrm_address_t *saddr, int create,
1792 unsigned short family);
1793int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
1794
1795#ifdef CONFIG_XFRM_MIGRATE
1796int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1797 const struct xfrm_migrate *m, int num_bundles,
1798 const struct xfrm_kmaddress *k,
1799 const struct xfrm_encap_tmpl *encap);
1800struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net);
1801struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1802 struct xfrm_migrate *m,
1803 struct xfrm_encap_tmpl *encap);
1804int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1805 struct xfrm_migrate *m, int num_bundles,
1806 struct xfrm_kmaddress *k, struct net *net,
1807 struct xfrm_encap_tmpl *encap);
1808#endif
1809
1810int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
1811void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid);
1812int km_report(struct net *net, u8 proto, struct xfrm_selector *sel,
1813 xfrm_address_t *addr);
1814
1815void xfrm_input_init(void);
1816int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
1817
1818void xfrm_probe_algs(void);
1819int xfrm_count_pfkey_auth_supported(void);
1820int xfrm_count_pfkey_enc_supported(void);
1821struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx);
1822struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx);
1823struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id);
1824struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id);
1825struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id);
1826struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe);
1827struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe);
1828struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe);
1829struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len,
1830 int probe);
1831
1832static inline bool xfrm6_addr_equal(const xfrm_address_t *a,
1833 const xfrm_address_t *b)
1834{
1835 return ipv6_addr_equal((const struct in6_addr *)a,
1836 (const struct in6_addr *)b);
1837}
1838
1839static inline bool xfrm_addr_equal(const xfrm_address_t *a,
1840 const xfrm_address_t *b,
1841 sa_family_t family)
1842{
1843 switch (family) {
1844 default:
1845 case AF_INET:
1846 return ((__force u32)a->a4 ^ (__force u32)b->a4) == 0;
1847 case AF_INET6:
1848 return xfrm6_addr_equal(a, b);
1849 }
1850}
1851
1852static inline int xfrm_policy_id2dir(u32 index)
1853{
1854 return index & 7;
1855}
1856
1857#ifdef CONFIG_XFRM
1858static inline int xfrm_aevent_is_on(struct net *net)
1859{
1860 struct sock *nlsk;
1861 int ret = 0;
1862
1863 rcu_read_lock();
1864 nlsk = rcu_dereference(net->xfrm.nlsk);
1865 if (nlsk)
1866 ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS);
1867 rcu_read_unlock();
1868 return ret;
1869}
1870
1871static inline int xfrm_acquire_is_on(struct net *net)
1872{
1873 struct sock *nlsk;
1874 int ret = 0;
1875
1876 rcu_read_lock();
1877 nlsk = rcu_dereference(net->xfrm.nlsk);
1878 if (nlsk)
1879 ret = netlink_has_listeners(nlsk, XFRMNLGRP_ACQUIRE);
1880 rcu_read_unlock();
1881
1882 return ret;
1883}
1884#endif
1885
1886static inline unsigned int aead_len(struct xfrm_algo_aead *alg)
1887{
1888 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
1889}
1890
1891static inline unsigned int xfrm_alg_len(const struct xfrm_algo *alg)
1892{
1893 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
1894}
1895
1896static inline unsigned int xfrm_alg_auth_len(const struct xfrm_algo_auth *alg)
1897{
1898 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
1899}
1900
1901static inline unsigned int xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn)
1902{
1903 return sizeof(*replay_esn) + replay_esn->bmp_len * sizeof(__u32);
1904}
1905
1906#ifdef CONFIG_XFRM_MIGRATE
1907static inline int xfrm_replay_clone(struct xfrm_state *x,
1908 struct xfrm_state *orig)
1909{
1910 x->replay_esn = kzalloc(xfrm_replay_state_esn_len(orig->replay_esn),
1911 GFP_KERNEL);
1912 if (!x->replay_esn)
1913 return -ENOMEM;
1914
1915 x->replay_esn->bmp_len = orig->replay_esn->bmp_len;
1916 x->replay_esn->replay_window = orig->replay_esn->replay_window;
1917
1918 x->preplay_esn = kmemdup(x->replay_esn,
1919 xfrm_replay_state_esn_len(x->replay_esn),
1920 GFP_KERNEL);
1921 if (!x->preplay_esn) {
1922 kfree(x->replay_esn);
1923 return -ENOMEM;
1924 }
1925
1926 return 0;
1927}
1928
1929static inline struct xfrm_algo_aead *xfrm_algo_aead_clone(struct xfrm_algo_aead *orig)
1930{
1931 return kmemdup(orig, aead_len(orig), GFP_KERNEL);
1932}
1933
1934
1935static inline struct xfrm_algo *xfrm_algo_clone(struct xfrm_algo *orig)
1936{
1937 return kmemdup(orig, xfrm_alg_len(orig), GFP_KERNEL);
1938}
1939
1940static inline struct xfrm_algo_auth *xfrm_algo_auth_clone(struct xfrm_algo_auth *orig)
1941{
1942 return kmemdup(orig, xfrm_alg_auth_len(orig), GFP_KERNEL);
1943}
1944
1945static inline void xfrm_states_put(struct xfrm_state **states, int n)
1946{
1947 int i;
1948 for (i = 0; i < n; i++)
1949 xfrm_state_put(*(states + i));
1950}
1951
1952static inline void xfrm_states_delete(struct xfrm_state **states, int n)
1953{
1954 int i;
1955 for (i = 0; i < n; i++)
1956 xfrm_state_delete(*(states + i));
1957}
1958#endif
1959
1960#ifdef CONFIG_XFRM
1961static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb)
1962{
1963 return skb->sp->xvec[skb->sp->len - 1];
1964}
1965#endif
1966
1967static inline struct xfrm_offload *xfrm_offload(struct sk_buff *skb)
1968{
1969#ifdef CONFIG_XFRM
1970 struct sec_path *sp = skb->sp;
1971
1972 if (!sp || !sp->olen || sp->len != sp->olen)
1973 return NULL;
1974
1975 return &sp->ovec[sp->olen - 1];
1976#else
1977 return NULL;
1978#endif
1979}
1980
1981void __init xfrm_dev_init(void);
1982
1983#ifdef CONFIG_XFRM_OFFLOAD
1984void xfrm_dev_resume(struct sk_buff *skb);
1985void xfrm_dev_backlog(struct softnet_data *sd);
1986struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again);
1987int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
1988 struct xfrm_user_offload *xuo);
1989bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x);
1990
1991static inline void xfrm_dev_state_advance_esn(struct xfrm_state *x)
1992{
1993 struct xfrm_state_offload *xso = &x->xso;
1994
1995 if (xso->dev && xso->dev->xfrmdev_ops->xdo_dev_state_advance_esn)
1996 xso->dev->xfrmdev_ops->xdo_dev_state_advance_esn(x);
1997}
1998
1999static inline bool xfrm_dst_offload_ok(struct dst_entry *dst)
2000{
2001 struct xfrm_state *x = dst->xfrm;
2002 struct xfrm_dst *xdst;
2003
2004 if (!x || !x->type_offload)
2005 return false;
2006
2007 xdst = (struct xfrm_dst *) dst;
2008 if (!x->xso.offload_handle && !xdst->child->xfrm)
2009 return true;
2010 if (x->xso.offload_handle && (x->xso.dev == xfrm_dst_path(dst)->dev) &&
2011 !xdst->child->xfrm)
2012 return true;
2013
2014 return false;
2015}
2016
2017static inline void xfrm_dev_state_delete(struct xfrm_state *x)
2018{
2019 struct xfrm_state_offload *xso = &x->xso;
2020
2021 if (xso->dev)
2022 xso->dev->xfrmdev_ops->xdo_dev_state_delete(x);
2023}
2024
2025static inline void xfrm_dev_state_free(struct xfrm_state *x)
2026{
2027 struct xfrm_state_offload *xso = &x->xso;
2028 struct net_device *dev = xso->dev;
2029
2030 if (dev && dev->xfrmdev_ops) {
2031 if (dev->xfrmdev_ops->xdo_dev_state_free)
2032 dev->xfrmdev_ops->xdo_dev_state_free(x);
2033 xso->dev = NULL;
2034 dev_put(dev);
2035 }
2036}
2037#else
2038static inline void xfrm_dev_resume(struct sk_buff *skb)
2039{
2040}
2041
2042static inline void xfrm_dev_backlog(struct softnet_data *sd)
2043{
2044}
2045
2046static inline struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again)
2047{
2048 return skb;
2049}
2050
2051static inline int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, struct xfrm_user_offload *xuo)
2052{
2053 return 0;
2054}
2055
2056static inline void xfrm_dev_state_delete(struct xfrm_state *x)
2057{
2058}
2059
2060static inline void xfrm_dev_state_free(struct xfrm_state *x)
2061{
2062}
2063
2064static inline bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
2065{
2066 return false;
2067}
2068
2069static inline void xfrm_dev_state_advance_esn(struct xfrm_state *x)
2070{
2071}
2072
2073static inline bool xfrm_dst_offload_ok(struct dst_entry *dst)
2074{
2075 return false;
2076}
2077#endif
2078
2079static inline int xfrm_mark_get(struct nlattr **attrs, struct xfrm_mark *m)
2080{
2081 if (attrs[XFRMA_MARK])
2082 memcpy(m, nla_data(attrs[XFRMA_MARK]), sizeof(struct xfrm_mark));
2083 else
2084 m->v = m->m = 0;
2085
2086 return m->v & m->m;
2087}
2088
2089static inline int xfrm_mark_put(struct sk_buff *skb, const struct xfrm_mark *m)
2090{
2091 int ret = 0;
2092
2093 if (m->m | m->v)
2094 ret = nla_put(skb, XFRMA_MARK, sizeof(struct xfrm_mark), m);
2095 return ret;
2096}
2097
2098static inline __u32 xfrm_smark_get(__u32 mark, struct xfrm_state *x)
2099{
2100 u32 m = x->output_mark_mask;
2101 u32 v = x->props.output_mark;
2102
2103 return (v & m) | (mark & ~m);
2104}
2105
2106static inline int xfrm_if_id_put(struct sk_buff *skb, __u32 if_id)
2107{
2108 int ret = 0;
2109
2110 if (if_id)
2111 ret = nla_put_u32(skb, XFRMA_IF_ID, if_id);
2112 return ret;
2113}
2114
2115static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,
2116 unsigned int family)
2117{
2118 bool tunnel = false;
2119
2120 switch(family) {
2121 case AF_INET:
2122 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
2123 tunnel = true;
2124 break;
2125 case AF_INET6:
2126 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
2127 tunnel = true;
2128 break;
2129 }
2130 if (tunnel && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL))
2131 return -EINVAL;
2132
2133 return 0;
2134}
2135#endif
2136