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