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