blob: d6111a2f0a23d07bb25afe53e3806594f538f02d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _NET_XFRM_H
2#define _NET_XFRM_H
3
Herbert Xuaabc9762005-05-03 16:27:10 -07004#include <linux/compiler.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -02005#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/xfrm.h>
7#include <linux/spinlock.h>
8#include <linux/list.h>
9#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020010#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/crypto.h>
12#include <linux/pfkeyv2.h>
13#include <linux/in6.h>
14
15#include <net/sock.h>
16#include <net/dst.h>
17#include <net/route.h>
18#include <net/ipv6.h>
19#include <net/ip6_fib.h>
20
21#define XFRM_ALIGN8(len) (((len) + 7) & ~7)
22
23extern struct semaphore xfrm_cfg_sem;
24
25/* Organization of SPD aka "XFRM rules"
26 ------------------------------------
27
28 Basic objects:
29 - policy rule, struct xfrm_policy (=SPD entry)
30 - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle)
31 - instance of a transformer, struct xfrm_state (=SA)
32 - template to clone xfrm_state, struct xfrm_tmpl
33
34 SPD is plain linear list of xfrm_policy rules, ordered by priority.
35 (To be compatible with existing pfkeyv2 implementations,
36 many rules with priority of 0x7fffffff are allowed to exist and
37 such rules are ordered in an unpredictable way, thanks to bsd folks.)
38
39 Lookup is plain linear search until the first match with selector.
40
41 If "action" is "block", then we prohibit the flow, otherwise:
42 if "xfrms_nr" is zero, the flow passes untransformed. Otherwise,
43 policy entry has list of up to XFRM_MAX_DEPTH transformations,
44 described by templates xfrm_tmpl. Each template is resolved
45 to a complete xfrm_state (see below) and we pack bundle of transformations
46 to a dst_entry returned to requestor.
47
48 dst -. xfrm .-> xfrm_state #1
49 |---. child .-> dst -. xfrm .-> xfrm_state #2
50 |---. child .-> dst -. xfrm .-> xfrm_state #3
51 |---. child .-> NULL
52
53 Bundles are cached at xrfm_policy struct (field ->bundles).
54
55
56 Resolution of xrfm_tmpl
57 -----------------------
58 Template contains:
59 1. ->mode Mode: transport or tunnel
60 2. ->id.proto Protocol: AH/ESP/IPCOMP
61 3. ->id.daddr Remote tunnel endpoint, ignored for transport mode.
62 Q: allow to resolve security gateway?
63 4. ->id.spi If not zero, static SPI.
64 5. ->saddr Local tunnel endpoint, ignored for transport mode.
65 6. ->algos List of allowed algos. Plain bitmask now.
66 Q: ealgos, aalgos, calgos. What a mess...
67 7. ->share Sharing mode.
68 Q: how to implement private sharing mode? To add struct sock* to
69 flow id?
70
71 Having this template we search through SAD searching for entries
72 with appropriate mode/proto/algo, permitted by selector.
73 If no appropriate entry found, it is requested from key manager.
74
75 PROBLEMS:
76 Q: How to find all the bundles referring to a physical path for
77 PMTU discovery? Seems, dst should contain list of all parents...
78 and enter to infinite locking hierarchy disaster.
79 No! It is easier, we will not search for them, let them find us.
80 We add genid to each dst plus pointer to genid of raw IP route,
81 pmtu disc will update pmtu on raw IP route and increase its genid.
82 dst_check() will see this for top level and trigger resyncing
83 metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
84 */
85
86/* Full description of state of transformer. */
87struct xfrm_state
88{
89 /* Note: bydst is re-used during gc */
90 struct list_head bydst;
91 struct list_head byspi;
92
93 atomic_t refcnt;
94 spinlock_t lock;
95
96 struct xfrm_id id;
97 struct xfrm_selector sel;
98
99 /* Key manger bits */
100 struct {
101 u8 state;
102 u8 dying;
103 u32 seq;
104 } km;
105
106 /* Parameters of this state. */
107 struct {
108 u32 reqid;
109 u8 mode;
110 u8 replay_window;
111 u8 aalgo, ealgo, calgo;
112 u8 flags;
113 u16 family;
114 xfrm_address_t saddr;
115 int header_len;
116 int trailer_len;
117 } props;
118
119 struct xfrm_lifetime_cfg lft;
120
121 /* Data for transformer */
122 struct xfrm_algo *aalg;
123 struct xfrm_algo *ealg;
124 struct xfrm_algo *calg;
125
126 /* Data for encapsulator */
127 struct xfrm_encap_tmpl *encap;
128
129 /* IPComp needs an IPIP tunnel for handling uncompressed packets */
130 struct xfrm_state *tunnel;
131
132 /* If a tunnel, number of users + 1 */
133 atomic_t tunnel_users;
134
135 /* State for replay detection */
136 struct xfrm_replay_state replay;
137
138 /* Statistics */
139 struct xfrm_stats stats;
140
141 struct xfrm_lifetime_cur curlft;
142 struct timer_list timer;
143
144 /* Reference to data common to all the instances of this
145 * transformer. */
146 struct xfrm_type *type;
147
Trent Jaegerdf718372005-12-13 23:12:27 -0800148 /* Security context */
149 struct xfrm_sec_ctx *security;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* Private data of this transformer, format is opaque,
152 * interpreted by xfrm_type methods. */
153 void *data;
154};
155
156enum {
157 XFRM_STATE_VOID,
158 XFRM_STATE_ACQ,
159 XFRM_STATE_VALID,
160 XFRM_STATE_ERROR,
161 XFRM_STATE_EXPIRED,
162 XFRM_STATE_DEAD
163};
164
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700165/* callback structure passed from either netlink or pfkey */
166struct km_event
167{
Herbert Xubf088672005-06-18 22:44:00 -0700168 union {
169 u32 hard;
170 u32 proto;
171 u32 byid;
172 } data;
173
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700174 u32 seq;
175 u32 pid;
176 u32 event;
177};
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179struct xfrm_type;
180struct xfrm_dst;
181struct xfrm_policy_afinfo {
182 unsigned short family;
183 rwlock_t lock;
184 struct xfrm_type_map *type_map;
185 struct dst_ops *dst_ops;
186 void (*garbage_collect)(void);
187 int (*dst_lookup)(struct xfrm_dst **dst, struct flowi *fl);
188 struct dst_entry *(*find_bundle)(struct flowi *fl, struct xfrm_policy *policy);
189 int (*bundle_create)(struct xfrm_policy *policy,
190 struct xfrm_state **xfrm,
191 int nx,
192 struct flowi *fl,
193 struct dst_entry **dst_p);
194 void (*decode_session)(struct sk_buff *skb,
195 struct flowi *fl);
196};
197
198extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo);
199extern int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700200extern void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c);
201extern void km_state_notify(struct xfrm_state *x, struct km_event *c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203#define XFRM_ACQ_EXPIRES 30
204
205struct xfrm_tmpl;
206struct xfrm_state_afinfo {
207 unsigned short family;
208 rwlock_t lock;
209 struct list_head *state_bydst;
210 struct list_head *state_byspi;
Herbert Xud094cd82005-06-20 13:19:41 -0700211 int (*init_flags)(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 void (*init_tempsel)(struct xfrm_state *x, struct flowi *fl,
213 struct xfrm_tmpl *tmpl,
214 xfrm_address_t *daddr, xfrm_address_t *saddr);
215 struct xfrm_state *(*state_lookup)(xfrm_address_t *daddr, u32 spi, u8 proto);
216 struct xfrm_state *(*find_acq)(u8 mode, u32 reqid, u8 proto,
217 xfrm_address_t *daddr, xfrm_address_t *saddr,
218 int create);
219};
220
221extern int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
222extern int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
223
224extern void xfrm_state_delete_tunnel(struct xfrm_state *x);
225
226struct xfrm_decap_state;
227struct xfrm_type
228{
229 char *description;
230 struct module *owner;
231 __u8 proto;
232
Herbert Xu72cb6962005-06-20 13:18:08 -0700233 int (*init_state)(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 void (*destructor)(struct xfrm_state *);
235 int (*input)(struct xfrm_state *, struct xfrm_decap_state *, struct sk_buff *skb);
236 int (*post_input)(struct xfrm_state *, struct xfrm_decap_state *, struct sk_buff *skb);
237 int (*output)(struct xfrm_state *, struct sk_buff *pskb);
238 /* Estimate maximal size of result of transformation of a dgram */
239 u32 (*get_max_size)(struct xfrm_state *, int size);
240};
241
242struct xfrm_type_map {
243 rwlock_t lock;
244 struct xfrm_type *map[256];
245};
246
247extern int xfrm_register_type(struct xfrm_type *type, unsigned short family);
248extern int xfrm_unregister_type(struct xfrm_type *type, unsigned short family);
249extern struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family);
250extern void xfrm_put_type(struct xfrm_type *type);
251
252struct xfrm_tmpl
253{
254/* id in template is interpreted as:
255 * daddr - destination of tunnel, may be zero for transport mode.
256 * spi - zero to acquire spi. Not zero if spi is static, then
257 * daddr must be fixed too.
258 * proto - AH/ESP/IPCOMP
259 */
260 struct xfrm_id id;
261
262/* Source address of tunnel. Ignored, if it is not a tunnel. */
263 xfrm_address_t saddr;
264
265 __u32 reqid;
266
267/* Mode: transport/tunnel */
268 __u8 mode;
269
270/* Sharing mode: unique, this session only, this user only etc. */
271 __u8 share;
272
273/* May skip this transfomration if no SA is found */
274 __u8 optional;
275
276/* Bit mask of algos allowed for acquisition */
277 __u32 aalgos;
278 __u32 ealgos;
279 __u32 calgos;
280};
281
282#define XFRM_MAX_DEPTH 4
283
284struct xfrm_policy
285{
286 struct xfrm_policy *next;
287 struct list_head list;
288
289 /* This lock only affects elements except for entry. */
290 rwlock_t lock;
291 atomic_t refcnt;
292 struct timer_list timer;
293
294 u32 priority;
295 u32 index;
296 struct xfrm_selector selector;
297 struct xfrm_lifetime_cfg lft;
298 struct xfrm_lifetime_cur curlft;
299 struct dst_entry *bundles;
300 __u16 family;
301 __u8 action;
302 __u8 flags;
303 __u8 dead;
304 __u8 xfrm_nr;
Trent Jaegerdf718372005-12-13 23:12:27 -0800305 struct xfrm_sec_ctx *security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH];
307};
308
309#define XFRM_KM_TIMEOUT 30
310
311struct xfrm_mgr
312{
313 struct list_head list;
314 char *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700315 int (*notify)(struct xfrm_state *x, struct km_event *c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp, int dir);
317 struct xfrm_policy *(*compile_policy)(u16 family, int opt, u8 *data, int len, int *dir);
318 int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700319 int (*notify_policy)(struct xfrm_policy *x, int dir, struct km_event *c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
322extern int xfrm_register_km(struct xfrm_mgr *km);
323extern int xfrm_unregister_km(struct xfrm_mgr *km);
324
325
326extern struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2];
327
328static inline void xfrm_pol_hold(struct xfrm_policy *policy)
329{
330 if (likely(policy != NULL))
331 atomic_inc(&policy->refcnt);
332}
333
334extern void __xfrm_policy_destroy(struct xfrm_policy *policy);
335
336static inline void xfrm_pol_put(struct xfrm_policy *policy)
337{
338 if (atomic_dec_and_test(&policy->refcnt))
339 __xfrm_policy_destroy(policy);
340}
341
342#define XFRM_DST_HSIZE 1024
343
344static __inline__
345unsigned __xfrm4_dst_hash(xfrm_address_t *addr)
346{
347 unsigned h;
348 h = ntohl(addr->a4);
349 h = (h ^ (h>>16)) % XFRM_DST_HSIZE;
350 return h;
351}
352
353static __inline__
354unsigned __xfrm6_dst_hash(xfrm_address_t *addr)
355{
356 unsigned h;
357 h = ntohl(addr->a6[2]^addr->a6[3]);
358 h = (h ^ (h>>16)) % XFRM_DST_HSIZE;
359 return h;
360}
361
362static __inline__
363unsigned xfrm_dst_hash(xfrm_address_t *addr, unsigned short family)
364{
365 switch (family) {
366 case AF_INET:
367 return __xfrm4_dst_hash(addr);
368 case AF_INET6:
369 return __xfrm6_dst_hash(addr);
370 }
371 return 0;
372}
373
374static __inline__
375unsigned __xfrm4_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto)
376{
377 unsigned h;
378 h = ntohl(addr->a4^spi^proto);
379 h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE;
380 return h;
381}
382
383static __inline__
384unsigned __xfrm6_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto)
385{
386 unsigned h;
387 h = ntohl(addr->a6[2]^addr->a6[3]^spi^proto);
388 h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE;
389 return h;
390}
391
392static __inline__
393unsigned xfrm_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto, unsigned short family)
394{
395 switch (family) {
396 case AF_INET:
397 return __xfrm4_spi_hash(addr, spi, proto);
398 case AF_INET6:
399 return __xfrm6_spi_hash(addr, spi, proto);
400 }
401 return 0; /*XXX*/
402}
403
404extern void __xfrm_state_destroy(struct xfrm_state *);
405
406static inline void xfrm_state_put(struct xfrm_state *x)
407{
408 if (atomic_dec_and_test(&x->refcnt))
409 __xfrm_state_destroy(x);
410}
411
412static inline void xfrm_state_hold(struct xfrm_state *x)
413{
414 atomic_inc(&x->refcnt);
415}
416
417static __inline__ int addr_match(void *token1, void *token2, int prefixlen)
418{
419 __u32 *a1 = token1;
420 __u32 *a2 = token2;
421 int pdw;
422 int pbi;
423
424 pdw = prefixlen >> 5; /* num of whole __u32 in prefix */
425 pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */
426
427 if (pdw)
428 if (memcmp(a1, a2, pdw << 2))
429 return 0;
430
431 if (pbi) {
432 __u32 mask;
433
434 mask = htonl((0xffffffff) << (32 - pbi));
435
436 if ((a1[pdw] ^ a2[pdw]) & mask)
437 return 0;
438 }
439
440 return 1;
441}
442
443static __inline__
444u16 xfrm_flowi_sport(struct flowi *fl)
445{
446 u16 port;
447 switch(fl->proto) {
448 case IPPROTO_TCP:
449 case IPPROTO_UDP:
450 case IPPROTO_SCTP:
451 port = fl->fl_ip_sport;
452 break;
453 case IPPROTO_ICMP:
454 case IPPROTO_ICMPV6:
455 port = htons(fl->fl_icmp_type);
456 break;
457 default:
458 port = 0; /*XXX*/
459 }
460 return port;
461}
462
463static __inline__
464u16 xfrm_flowi_dport(struct flowi *fl)
465{
466 u16 port;
467 switch(fl->proto) {
468 case IPPROTO_TCP:
469 case IPPROTO_UDP:
470 case IPPROTO_SCTP:
471 port = fl->fl_ip_dport;
472 break;
473 case IPPROTO_ICMP:
474 case IPPROTO_ICMPV6:
475 port = htons(fl->fl_icmp_code);
476 break;
477 default:
478 port = 0; /*XXX*/
479 }
480 return port;
481}
482
483static inline int
484__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
485{
486 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
487 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
488 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
489 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
490 (fl->proto == sel->proto || !sel->proto) &&
491 (fl->oif == sel->ifindex || !sel->ifindex);
492}
493
494static inline int
495__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
496{
497 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
498 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
499 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
500 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
501 (fl->proto == sel->proto || !sel->proto) &&
502 (fl->oif == sel->ifindex || !sel->ifindex);
503}
504
505static inline int
506xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
507 unsigned short family)
508{
509 switch (family) {
510 case AF_INET:
511 return __xfrm4_selector_match(sel, fl);
512 case AF_INET6:
513 return __xfrm6_selector_match(sel, fl);
514 }
515 return 0;
516}
517
Trent Jaegerdf718372005-12-13 23:12:27 -0800518#ifdef CONFIG_SECURITY_NETWORK_XFRM
519/* If neither has a context --> match
520 * Otherwise, both must have a context and the sids, doi, alg must match
521 */
522static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
523{
524 return ((!s1 && !s2) ||
525 (s1 && s2 &&
526 (s1->ctx_sid == s2->ctx_sid) &&
527 (s1->ctx_doi == s2->ctx_doi) &&
528 (s1->ctx_alg == s2->ctx_alg)));
529}
530#else
531static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
532{
533 return 1;
534}
535#endif
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/* A struct encoding bundle of transformations to apply to some set of flow.
538 *
539 * dst->child points to the next element of bundle.
540 * dst->xfrm points to an instanse of transformer.
541 *
542 * Due to unfortunate limitations of current routing cache, which we
543 * have no time to fix, it mirrors struct rtable and bound to the same
544 * routing key, including saddr,daddr. However, we can have many of
545 * bundles differing by session id. All the bundles grow from a parent
546 * policy rule.
547 */
548struct xfrm_dst
549{
550 union {
551 struct xfrm_dst *next;
552 struct dst_entry dst;
553 struct rtable rt;
554 struct rt6_info rt6;
555 } u;
556 struct dst_entry *route;
557 u32 route_mtu_cached;
558 u32 child_mtu_cached;
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700559 u32 route_cookie;
560 u32 path_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561};
562
Herbert Xuaabc9762005-05-03 16:27:10 -0700563static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)
564{
565 dst_release(xdst->route);
566 if (likely(xdst->u.dst.xfrm))
567 xfrm_state_put(xdst->u.dst.xfrm);
568}
569
570extern void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/* Decapsulation state, used by the input to store data during
573 * decapsulation procedure, to be used later (during the policy
574 * check
575 */
576struct xfrm_decap_state {
577 char decap_data[20];
578 __u16 decap_type;
579};
580
581struct sec_decap_state {
582 struct xfrm_state *xvec;
583 struct xfrm_decap_state decap;
584};
585
586struct sec_path
587{
588 atomic_t refcnt;
589 int len;
590 struct sec_decap_state x[XFRM_MAX_DEPTH];
591};
592
593static inline struct sec_path *
594secpath_get(struct sec_path *sp)
595{
596 if (sp)
597 atomic_inc(&sp->refcnt);
598 return sp;
599}
600
601extern void __secpath_destroy(struct sec_path *sp);
602
603static inline void
604secpath_put(struct sec_path *sp)
605{
606 if (sp && atomic_dec_and_test(&sp->refcnt))
607 __secpath_destroy(sp);
608}
609
610extern struct sec_path *secpath_dup(struct sec_path *src);
611
612static inline void
613secpath_reset(struct sk_buff *skb)
614{
615#ifdef CONFIG_XFRM
616 secpath_put(skb->sp);
617 skb->sp = NULL;
618#endif
619}
620
621static inline int
622__xfrm4_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x)
623{
624 return (tmpl->saddr.a4 &&
625 tmpl->saddr.a4 != x->props.saddr.a4);
626}
627
628static inline int
629__xfrm6_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x)
630{
631 return (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
632 ipv6_addr_cmp((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
633}
634
635static inline int
636xfrm_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x, unsigned short family)
637{
638 switch (family) {
639 case AF_INET:
640 return __xfrm4_state_addr_cmp(tmpl, x);
641 case AF_INET6:
642 return __xfrm6_state_addr_cmp(tmpl, x);
643 }
644 return !0;
645}
646
647#ifdef CONFIG_XFRM
648
649extern int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb, unsigned short family);
650
651static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
652{
653 if (sk && sk->sk_policy[XFRM_POLICY_IN])
654 return __xfrm_policy_check(sk, dir, skb, family);
655
656 return (!xfrm_policy_list[dir] && !skb->sp) ||
657 (skb->dst->flags & DST_NOPOLICY) ||
658 __xfrm_policy_check(sk, dir, skb, family);
659}
660
661static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
662{
663 return xfrm_policy_check(sk, dir, skb, AF_INET);
664}
665
666static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
667{
668 return xfrm_policy_check(sk, dir, skb, AF_INET6);
669}
670
Patrick McHardy3e3850e2006-01-06 23:04:54 -0800671extern int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672extern int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
673
674static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
675{
676 return !xfrm_policy_list[XFRM_POLICY_OUT] ||
677 (skb->dst->flags & DST_NOXFRM) ||
678 __xfrm_route_forward(skb, family);
679}
680
681static inline int xfrm4_route_forward(struct sk_buff *skb)
682{
683 return xfrm_route_forward(skb, AF_INET);
684}
685
686static inline int xfrm6_route_forward(struct sk_buff *skb)
687{
688 return xfrm_route_forward(skb, AF_INET6);
689}
690
691extern int __xfrm_sk_clone_policy(struct sock *sk);
692
693static inline int xfrm_sk_clone_policy(struct sock *sk)
694{
695 if (unlikely(sk->sk_policy[0] || sk->sk_policy[1]))
696 return __xfrm_sk_clone_policy(sk);
697 return 0;
698}
699
Herbert Xu4666faa2005-06-18 22:43:22 -0700700extern int xfrm_policy_delete(struct xfrm_policy *pol, int dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702static inline void xfrm_sk_free_policy(struct sock *sk)
703{
704 if (unlikely(sk->sk_policy[0] != NULL)) {
705 xfrm_policy_delete(sk->sk_policy[0], XFRM_POLICY_MAX);
706 sk->sk_policy[0] = NULL;
707 }
708 if (unlikely(sk->sk_policy[1] != NULL)) {
709 xfrm_policy_delete(sk->sk_policy[1], XFRM_POLICY_MAX+1);
710 sk->sk_policy[1] = NULL;
711 }
712}
713
714#else
715
716static inline void xfrm_sk_free_policy(struct sock *sk) {}
717static inline int xfrm_sk_clone_policy(struct sock *sk) { return 0; }
718static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }
719static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; }
720static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
721{
722 return 1;
723}
724static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
725{
726 return 1;
727}
728static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
729{
730 return 1;
731}
732#endif
733
734static __inline__
735xfrm_address_t *xfrm_flowi_daddr(struct flowi *fl, unsigned short family)
736{
737 switch (family){
738 case AF_INET:
739 return (xfrm_address_t *)&fl->fl4_dst;
740 case AF_INET6:
741 return (xfrm_address_t *)&fl->fl6_dst;
742 }
743 return NULL;
744}
745
746static __inline__
747xfrm_address_t *xfrm_flowi_saddr(struct flowi *fl, unsigned short family)
748{
749 switch (family){
750 case AF_INET:
751 return (xfrm_address_t *)&fl->fl4_src;
752 case AF_INET6:
753 return (xfrm_address_t *)&fl->fl6_src;
754 }
755 return NULL;
756}
757
758static __inline__ int
759__xfrm4_state_addr_check(struct xfrm_state *x,
760 xfrm_address_t *daddr, xfrm_address_t *saddr)
761{
762 if (daddr->a4 == x->id.daddr.a4 &&
763 (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
764 return 1;
765 return 0;
766}
767
768static __inline__ int
769__xfrm6_state_addr_check(struct xfrm_state *x,
770 xfrm_address_t *daddr, xfrm_address_t *saddr)
771{
772 if (!ipv6_addr_cmp((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
773 (!ipv6_addr_cmp((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr)||
774 ipv6_addr_any((struct in6_addr *)saddr) ||
775 ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
776 return 1;
777 return 0;
778}
779
780static __inline__ int
781xfrm_state_addr_check(struct xfrm_state *x,
782 xfrm_address_t *daddr, xfrm_address_t *saddr,
783 unsigned short family)
784{
785 switch (family) {
786 case AF_INET:
787 return __xfrm4_state_addr_check(x, daddr, saddr);
788 case AF_INET6:
789 return __xfrm6_state_addr_check(x, daddr, saddr);
790 }
791 return 0;
792}
793
794static inline int xfrm_state_kern(struct xfrm_state *x)
795{
796 return atomic_read(&x->tunnel_users);
797}
798
799/*
800 * xfrm algorithm information
801 */
802struct xfrm_algo_auth_info {
803 u16 icv_truncbits;
804 u16 icv_fullbits;
805};
806
807struct xfrm_algo_encr_info {
808 u16 blockbits;
809 u16 defkeybits;
810};
811
812struct xfrm_algo_comp_info {
813 u16 threshold;
814};
815
816struct xfrm_algo_desc {
817 char *name;
818 u8 available:1;
819 union {
820 struct xfrm_algo_auth_info auth;
821 struct xfrm_algo_encr_info encr;
822 struct xfrm_algo_comp_info comp;
823 } uinfo;
824 struct sadb_alg desc;
825};
826
827/* XFRM tunnel handlers. */
828struct xfrm_tunnel {
829 int (*handler)(struct sk_buff *skb);
Patrick McHardy03037702005-07-19 14:03:34 -0700830 void (*err_handler)(struct sk_buff *skb, __u32 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831};
832
833struct xfrm6_tunnel {
Patrick McHardy951dbc82006-01-06 23:02:34 -0800834 int (*handler)(struct sk_buff **pskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 void (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
836 int type, int code, int offset, __u32 info);
837};
838
839extern void xfrm_init(void);
840extern void xfrm4_init(void);
841extern void xfrm6_init(void);
842extern void xfrm6_fini(void);
843extern void xfrm_state_init(void);
844extern void xfrm4_state_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845extern void xfrm6_state_init(void);
846extern void xfrm6_state_fini(void);
847
848extern int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*), void *);
849extern struct xfrm_state *xfrm_state_alloc(void);
850extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
851 struct flowi *fl, struct xfrm_tmpl *tmpl,
852 struct xfrm_policy *pol, int *err,
853 unsigned short family);
854extern int xfrm_state_check_expire(struct xfrm_state *x);
855extern void xfrm_state_insert(struct xfrm_state *x);
856extern int xfrm_state_add(struct xfrm_state *x);
857extern int xfrm_state_update(struct xfrm_state *x);
858extern struct xfrm_state *xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto, unsigned short family);
859extern struct xfrm_state *xfrm_find_acq_byseq(u32 seq);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700860extern int xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861extern void xfrm_state_flush(u8 proto);
862extern int xfrm_replay_check(struct xfrm_state *x, u32 seq);
863extern void xfrm_replay_advance(struct xfrm_state *x, u32 seq);
864extern int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb);
865extern int xfrm_state_mtu(struct xfrm_state *x, int mtu);
Herbert Xu72cb6962005-06-20 13:18:08 -0700866extern int xfrm_init_state(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867extern int xfrm4_rcv(struct sk_buff *skb);
868extern int xfrm4_output(struct sk_buff *skb);
869extern int xfrm4_tunnel_register(struct xfrm_tunnel *handler);
870extern int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800871extern int xfrm6_rcv_spi(struct sk_buff **pskb, u32 spi);
872extern int xfrm6_rcv(struct sk_buff **pskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873extern int xfrm6_tunnel_register(struct xfrm6_tunnel *handler);
874extern int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler);
875extern u32 xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr);
876extern void xfrm6_tunnel_free_spi(xfrm_address_t *saddr);
877extern u32 xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr);
878extern int xfrm6_output(struct sk_buff *skb);
879
880#ifdef CONFIG_XFRM
881extern int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type);
882extern int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen);
883extern int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, unsigned short family);
884#else
885static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
886{
887 return -ENOPROTOOPT;
888}
889
890static inline int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
891{
892 /* should not happen */
893 kfree_skb(skb);
894 return 0;
895}
896static inline int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, unsigned short family)
897{
898 return -EINVAL;
899}
900#endif
901
Al Virodd0fc662005-10-07 07:46:04 +0100902struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903extern int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*), void *);
904int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
Trent Jaegerdf718372005-12-13 23:12:27 -0800905struct xfrm_policy *xfrm_policy_bysel_ctx(int dir, struct xfrm_selector *sel,
906 struct xfrm_sec_ctx *ctx, int delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete);
908void xfrm_policy_flush(void);
909u32 xfrm_get_acqseq(void);
910void xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi);
911struct xfrm_state * xfrm_find_acq(u8 mode, u32 reqid, u8 proto,
912 xfrm_address_t *daddr, xfrm_address_t *saddr,
913 int create, unsigned short family);
914extern void xfrm_policy_flush(void);
915extern int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
916extern int xfrm_flush_bundles(void);
David S. Miller399c1802005-12-19 14:23:23 -0800917extern void xfrm_flush_all_bundles(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918extern int xfrm_bundle_ok(struct xfrm_dst *xdst, struct flowi *fl, int family);
919extern void xfrm_init_pmtu(struct dst_entry *dst);
920
921extern wait_queue_head_t km_waitq;
922extern int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport);
923extern void km_policy_expired(struct xfrm_policy *pol, int dir, int hard);
924
925extern void xfrm_input_init(void);
926extern int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, u32 *spi, u32 *seq);
927
928extern void xfrm_probe_algs(void);
929extern int xfrm_count_auth_supported(void);
930extern int xfrm_count_enc_supported(void);
931extern struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx);
932extern struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx);
933extern struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id);
934extern struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id);
935extern struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id);
936extern struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe);
937extern struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe);
938extern struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe);
939
940struct crypto_tfm;
941typedef void (icv_update_fn_t)(struct crypto_tfm *, struct scatterlist *, unsigned int);
942
943extern void skb_icv_walk(const struct sk_buff *skb, struct crypto_tfm *tfm,
944 int offset, int len, icv_update_fn_t icv_update);
945
946static inline int xfrm_addr_cmp(xfrm_address_t *a, xfrm_address_t *b,
947 int family)
948{
949 switch (family) {
950 default:
951 case AF_INET:
952 return a->a4 - b->a4;
953 case AF_INET6:
954 return ipv6_addr_cmp((struct in6_addr *)a,
955 (struct in6_addr *)b);
956 }
957}
958
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700959static inline int xfrm_policy_id2dir(u32 index)
960{
961 return index & 7;
962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964#endif /* _NET_XFRM_H */