blob: d409c9d5e2931a9be4e6c1fc860316910fe434e0 [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
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -080023extern struct sock *xfrm_nl;
24extern u32 sysctl_xfrm_aevent_etime;
25extern u32 sysctl_xfrm_aevent_rseqth;
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027extern struct semaphore xfrm_cfg_sem;
28
29/* Organization of SPD aka "XFRM rules"
30 ------------------------------------
31
32 Basic objects:
33 - policy rule, struct xfrm_policy (=SPD entry)
34 - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle)
35 - instance of a transformer, struct xfrm_state (=SA)
36 - template to clone xfrm_state, struct xfrm_tmpl
37
38 SPD is plain linear list of xfrm_policy rules, ordered by priority.
39 (To be compatible with existing pfkeyv2 implementations,
40 many rules with priority of 0x7fffffff are allowed to exist and
41 such rules are ordered in an unpredictable way, thanks to bsd folks.)
42
43 Lookup is plain linear search until the first match with selector.
44
45 If "action" is "block", then we prohibit the flow, otherwise:
46 if "xfrms_nr" is zero, the flow passes untransformed. Otherwise,
47 policy entry has list of up to XFRM_MAX_DEPTH transformations,
48 described by templates xfrm_tmpl. Each template is resolved
49 to a complete xfrm_state (see below) and we pack bundle of transformations
50 to a dst_entry returned to requestor.
51
52 dst -. xfrm .-> xfrm_state #1
53 |---. child .-> dst -. xfrm .-> xfrm_state #2
54 |---. child .-> dst -. xfrm .-> xfrm_state #3
55 |---. child .-> NULL
56
57 Bundles are cached at xrfm_policy struct (field ->bundles).
58
59
60 Resolution of xrfm_tmpl
61 -----------------------
62 Template contains:
63 1. ->mode Mode: transport or tunnel
64 2. ->id.proto Protocol: AH/ESP/IPCOMP
65 3. ->id.daddr Remote tunnel endpoint, ignored for transport mode.
66 Q: allow to resolve security gateway?
67 4. ->id.spi If not zero, static SPI.
68 5. ->saddr Local tunnel endpoint, ignored for transport mode.
69 6. ->algos List of allowed algos. Plain bitmask now.
70 Q: ealgos, aalgos, calgos. What a mess...
71 7. ->share Sharing mode.
72 Q: how to implement private sharing mode? To add struct sock* to
73 flow id?
74
75 Having this template we search through SAD searching for entries
76 with appropriate mode/proto/algo, permitted by selector.
77 If no appropriate entry found, it is requested from key manager.
78
79 PROBLEMS:
80 Q: How to find all the bundles referring to a physical path for
81 PMTU discovery? Seems, dst should contain list of all parents...
82 and enter to infinite locking hierarchy disaster.
83 No! It is easier, we will not search for them, let them find us.
84 We add genid to each dst plus pointer to genid of raw IP route,
85 pmtu disc will update pmtu on raw IP route and increase its genid.
86 dst_check() will see this for top level and trigger resyncing
87 metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
88 */
89
90/* Full description of state of transformer. */
91struct xfrm_state
92{
93 /* Note: bydst is re-used during gc */
94 struct list_head bydst;
95 struct list_head byspi;
96
97 atomic_t refcnt;
98 spinlock_t lock;
99
100 struct xfrm_id id;
101 struct xfrm_selector sel;
102
103 /* Key manger bits */
104 struct {
105 u8 state;
106 u8 dying;
107 u32 seq;
108 } km;
109
110 /* Parameters of this state. */
111 struct {
112 u32 reqid;
113 u8 mode;
114 u8 replay_window;
115 u8 aalgo, ealgo, calgo;
116 u8 flags;
117 u16 family;
118 xfrm_address_t saddr;
119 int header_len;
120 int trailer_len;
121 } props;
122
123 struct xfrm_lifetime_cfg lft;
124
125 /* Data for transformer */
126 struct xfrm_algo *aalg;
127 struct xfrm_algo *ealg;
128 struct xfrm_algo *calg;
129
130 /* Data for encapsulator */
131 struct xfrm_encap_tmpl *encap;
132
133 /* IPComp needs an IPIP tunnel for handling uncompressed packets */
134 struct xfrm_state *tunnel;
135
136 /* If a tunnel, number of users + 1 */
137 atomic_t tunnel_users;
138
139 /* State for replay detection */
140 struct xfrm_replay_state replay;
141
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800142 /* Replay detection state at the time we sent the last notification */
143 struct xfrm_replay_state preplay;
144
145 /* Replay detection notification settings */
146 u32 replay_maxage;
147 u32 replay_maxdiff;
148
149 /* Replay detection notification timer */
150 struct timer_list rtimer;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* Statistics */
153 struct xfrm_stats stats;
154
155 struct xfrm_lifetime_cur curlft;
156 struct timer_list timer;
157
158 /* Reference to data common to all the instances of this
159 * transformer. */
160 struct xfrm_type *type;
161
Trent Jaegerdf718372005-12-13 23:12:27 -0800162 /* Security context */
163 struct xfrm_sec_ctx *security;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /* Private data of this transformer, format is opaque,
166 * interpreted by xfrm_type methods. */
167 void *data;
168};
169
170enum {
171 XFRM_STATE_VOID,
172 XFRM_STATE_ACQ,
173 XFRM_STATE_VALID,
174 XFRM_STATE_ERROR,
175 XFRM_STATE_EXPIRED,
176 XFRM_STATE_DEAD
177};
178
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700179/* callback structure passed from either netlink or pfkey */
180struct km_event
181{
Herbert Xubf088672005-06-18 22:44:00 -0700182 union {
183 u32 hard;
184 u32 proto;
185 u32 byid;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800186 u32 aevent;
Herbert Xubf088672005-06-18 22:44:00 -0700187 } data;
188
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700189 u32 seq;
190 u32 pid;
191 u32 event;
192};
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194struct xfrm_type;
195struct xfrm_dst;
196struct xfrm_policy_afinfo {
197 unsigned short family;
198 rwlock_t lock;
199 struct xfrm_type_map *type_map;
200 struct dst_ops *dst_ops;
201 void (*garbage_collect)(void);
202 int (*dst_lookup)(struct xfrm_dst **dst, struct flowi *fl);
203 struct dst_entry *(*find_bundle)(struct flowi *fl, struct xfrm_policy *policy);
204 int (*bundle_create)(struct xfrm_policy *policy,
205 struct xfrm_state **xfrm,
206 int nx,
207 struct flowi *fl,
208 struct dst_entry **dst_p);
209 void (*decode_session)(struct sk_buff *skb,
210 struct flowi *fl);
211};
212
213extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo);
214extern int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700215extern void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c);
216extern void km_state_notify(struct xfrm_state *x, struct km_event *c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define XFRM_ACQ_EXPIRES 30
218
219struct xfrm_tmpl;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800220extern int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800221extern void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
222extern int __xfrm_state_delete(struct xfrm_state *x);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224struct xfrm_state_afinfo {
225 unsigned short family;
226 rwlock_t lock;
227 struct list_head *state_bydst;
228 struct list_head *state_byspi;
Herbert Xud094cd82005-06-20 13:19:41 -0700229 int (*init_flags)(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 void (*init_tempsel)(struct xfrm_state *x, struct flowi *fl,
231 struct xfrm_tmpl *tmpl,
232 xfrm_address_t *daddr, xfrm_address_t *saddr);
233 struct xfrm_state *(*state_lookup)(xfrm_address_t *daddr, u32 spi, u8 proto);
234 struct xfrm_state *(*find_acq)(u8 mode, u32 reqid, u8 proto,
235 xfrm_address_t *daddr, xfrm_address_t *saddr,
236 int create);
237};
238
239extern int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
240extern int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
241
242extern void xfrm_state_delete_tunnel(struct xfrm_state *x);
243
244struct xfrm_decap_state;
245struct xfrm_type
246{
247 char *description;
248 struct module *owner;
249 __u8 proto;
250
Herbert Xu72cb6962005-06-20 13:18:08 -0700251 int (*init_state)(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 void (*destructor)(struct xfrm_state *);
253 int (*input)(struct xfrm_state *, struct xfrm_decap_state *, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 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
259struct xfrm_type_map {
260 rwlock_t lock;
261 struct xfrm_type *map[256];
262};
263
264extern int xfrm_register_type(struct xfrm_type *type, unsigned short family);
265extern int xfrm_unregister_type(struct xfrm_type *type, unsigned short family);
266extern struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family);
267extern void xfrm_put_type(struct xfrm_type *type);
268
269struct 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
301struct 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;
Trent Jaegerdf718372005-12-13 23:12:27 -0800322 struct xfrm_sec_ctx *security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH];
324};
325
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800326#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342struct xfrm_mgr
343{
344 struct list_head list;
345 char *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700346 int (*notify)(struct xfrm_state *x, struct km_event *c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 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);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700350 int (*notify_policy)(struct xfrm_policy *x, int dir, struct km_event *c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351};
352
353extern int xfrm_register_km(struct xfrm_mgr *km);
354extern int xfrm_unregister_km(struct xfrm_mgr *km);
355
356
357extern struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2];
358
359static inline void xfrm_pol_hold(struct xfrm_policy *policy)
360{
361 if (likely(policy != NULL))
362 atomic_inc(&policy->refcnt);
363}
364
365extern void __xfrm_policy_destroy(struct xfrm_policy *policy);
366
367static 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
375static __inline__
376unsigned __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
384static __inline__
385unsigned __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
393static __inline__
394unsigned 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
405static __inline__
406unsigned __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
414static __inline__
415unsigned __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
423static __inline__
424unsigned 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
435extern void __xfrm_state_destroy(struct xfrm_state *);
436
Herbert Xu21380b82006-02-22 14:47:13 -0800437static inline void __xfrm_state_put(struct xfrm_state *x)
438{
439 atomic_dec(&x->refcnt);
440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static 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
448static inline void xfrm_state_hold(struct xfrm_state *x)
449{
450 atomic_inc(&x->refcnt);
451}
452
453static __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
479static __inline__
480u16 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
499static __inline__
500u16 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
519static 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
530static 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
541static inline int
542xfrm_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
Trent Jaegerdf718372005-12-13 23:12:27 -0800554#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 */
558static 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
567static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
568{
569 return 1;
570}
571#endif
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/* 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 */
584struct 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;
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -0700595 u32 route_cookie;
596 u32 path_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597};
598
Herbert Xuaabc9762005-05-03 16:27:10 -0700599static 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
606extern void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/* Decapsulation state, used by the input to store data during
609 * decapsulation procedure, to be used later (during the policy
610 * check
611 */
612struct xfrm_decap_state {
613 char decap_data[20];
614 __u16 decap_type;
615};
616
617struct sec_decap_state {
618 struct xfrm_state *xvec;
619 struct xfrm_decap_state decap;
620};
621
622struct sec_path
623{
624 atomic_t refcnt;
625 int len;
626 struct sec_decap_state x[XFRM_MAX_DEPTH];
627};
628
629static inline struct sec_path *
630secpath_get(struct sec_path *sp)
631{
632 if (sp)
633 atomic_inc(&sp->refcnt);
634 return sp;
635}
636
637extern void __secpath_destroy(struct sec_path *sp);
638
639static inline void
640secpath_put(struct sec_path *sp)
641{
642 if (sp && atomic_dec_and_test(&sp->refcnt))
643 __secpath_destroy(sp);
644}
645
646extern struct sec_path *secpath_dup(struct sec_path *src);
647
648static inline void
649secpath_reset(struct sk_buff *skb)
650{
651#ifdef CONFIG_XFRM
652 secpath_put(skb->sp);
653 skb->sp = NULL;
654#endif
655}
656
657static inline int
658__xfrm4_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x)
659{
660 return (tmpl->saddr.a4 &&
661 tmpl->saddr.a4 != x->props.saddr.a4);
662}
663
664static inline int
665__xfrm6_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x)
666{
667 return (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
668 ipv6_addr_cmp((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
669}
670
671static inline int
672xfrm_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x, unsigned short family)
673{
674 switch (family) {
675 case AF_INET:
676 return __xfrm4_state_addr_cmp(tmpl, x);
677 case AF_INET6:
678 return __xfrm6_state_addr_cmp(tmpl, x);
679 }
680 return !0;
681}
682
683#ifdef CONFIG_XFRM
684
685extern int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb, unsigned short family);
686
687static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
688{
689 if (sk && sk->sk_policy[XFRM_POLICY_IN])
690 return __xfrm_policy_check(sk, dir, skb, family);
691
692 return (!xfrm_policy_list[dir] && !skb->sp) ||
693 (skb->dst->flags & DST_NOPOLICY) ||
694 __xfrm_policy_check(sk, dir, skb, family);
695}
696
697static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
698{
699 return xfrm_policy_check(sk, dir, skb, AF_INET);
700}
701
702static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
703{
704 return xfrm_policy_check(sk, dir, skb, AF_INET6);
705}
706
Patrick McHardy3e3850e2006-01-06 23:04:54 -0800707extern int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708extern int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
709
710static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
711{
712 return !xfrm_policy_list[XFRM_POLICY_OUT] ||
713 (skb->dst->flags & DST_NOXFRM) ||
714 __xfrm_route_forward(skb, family);
715}
716
717static inline int xfrm4_route_forward(struct sk_buff *skb)
718{
719 return xfrm_route_forward(skb, AF_INET);
720}
721
722static inline int xfrm6_route_forward(struct sk_buff *skb)
723{
724 return xfrm_route_forward(skb, AF_INET6);
725}
726
727extern int __xfrm_sk_clone_policy(struct sock *sk);
728
729static inline int xfrm_sk_clone_policy(struct sock *sk)
730{
731 if (unlikely(sk->sk_policy[0] || sk->sk_policy[1]))
732 return __xfrm_sk_clone_policy(sk);
733 return 0;
734}
735
Herbert Xu4666faa2005-06-18 22:43:22 -0700736extern int xfrm_policy_delete(struct xfrm_policy *pol, int dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738static inline void xfrm_sk_free_policy(struct sock *sk)
739{
740 if (unlikely(sk->sk_policy[0] != NULL)) {
741 xfrm_policy_delete(sk->sk_policy[0], XFRM_POLICY_MAX);
742 sk->sk_policy[0] = NULL;
743 }
744 if (unlikely(sk->sk_policy[1] != NULL)) {
745 xfrm_policy_delete(sk->sk_policy[1], XFRM_POLICY_MAX+1);
746 sk->sk_policy[1] = NULL;
747 }
748}
749
750#else
751
752static inline void xfrm_sk_free_policy(struct sock *sk) {}
753static inline int xfrm_sk_clone_policy(struct sock *sk) { return 0; }
754static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }
755static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; }
756static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
757{
758 return 1;
759}
760static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
761{
762 return 1;
763}
764static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
765{
766 return 1;
767}
768#endif
769
770static __inline__
771xfrm_address_t *xfrm_flowi_daddr(struct flowi *fl, unsigned short family)
772{
773 switch (family){
774 case AF_INET:
775 return (xfrm_address_t *)&fl->fl4_dst;
776 case AF_INET6:
777 return (xfrm_address_t *)&fl->fl6_dst;
778 }
779 return NULL;
780}
781
782static __inline__
783xfrm_address_t *xfrm_flowi_saddr(struct flowi *fl, unsigned short family)
784{
785 switch (family){
786 case AF_INET:
787 return (xfrm_address_t *)&fl->fl4_src;
788 case AF_INET6:
789 return (xfrm_address_t *)&fl->fl6_src;
790 }
791 return NULL;
792}
793
794static __inline__ int
795__xfrm4_state_addr_check(struct xfrm_state *x,
796 xfrm_address_t *daddr, xfrm_address_t *saddr)
797{
798 if (daddr->a4 == x->id.daddr.a4 &&
799 (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
800 return 1;
801 return 0;
802}
803
804static __inline__ int
805__xfrm6_state_addr_check(struct xfrm_state *x,
806 xfrm_address_t *daddr, xfrm_address_t *saddr)
807{
808 if (!ipv6_addr_cmp((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
809 (!ipv6_addr_cmp((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr)||
810 ipv6_addr_any((struct in6_addr *)saddr) ||
811 ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
812 return 1;
813 return 0;
814}
815
816static __inline__ int
817xfrm_state_addr_check(struct xfrm_state *x,
818 xfrm_address_t *daddr, xfrm_address_t *saddr,
819 unsigned short family)
820{
821 switch (family) {
822 case AF_INET:
823 return __xfrm4_state_addr_check(x, daddr, saddr);
824 case AF_INET6:
825 return __xfrm6_state_addr_check(x, daddr, saddr);
826 }
827 return 0;
828}
829
830static inline int xfrm_state_kern(struct xfrm_state *x)
831{
832 return atomic_read(&x->tunnel_users);
833}
834
835/*
836 * xfrm algorithm information
837 */
838struct xfrm_algo_auth_info {
839 u16 icv_truncbits;
840 u16 icv_fullbits;
841};
842
843struct xfrm_algo_encr_info {
844 u16 blockbits;
845 u16 defkeybits;
846};
847
848struct xfrm_algo_comp_info {
849 u16 threshold;
850};
851
852struct xfrm_algo_desc {
853 char *name;
854 u8 available:1;
855 union {
856 struct xfrm_algo_auth_info auth;
857 struct xfrm_algo_encr_info encr;
858 struct xfrm_algo_comp_info comp;
859 } uinfo;
860 struct sadb_alg desc;
861};
862
863/* XFRM tunnel handlers. */
864struct xfrm_tunnel {
865 int (*handler)(struct sk_buff *skb);
Patrick McHardy03037702005-07-19 14:03:34 -0700866 void (*err_handler)(struct sk_buff *skb, __u32 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867};
868
869struct xfrm6_tunnel {
Patrick McHardy951dbc82006-01-06 23:02:34 -0800870 int (*handler)(struct sk_buff **pskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 void (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
872 int type, int code, int offset, __u32 info);
873};
874
875extern void xfrm_init(void);
876extern void xfrm4_init(void);
877extern void xfrm6_init(void);
878extern void xfrm6_fini(void);
879extern void xfrm_state_init(void);
880extern void xfrm4_state_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881extern void xfrm6_state_init(void);
882extern void xfrm6_state_fini(void);
883
884extern int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*), void *);
885extern struct xfrm_state *xfrm_state_alloc(void);
886extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
887 struct flowi *fl, struct xfrm_tmpl *tmpl,
888 struct xfrm_policy *pol, int *err,
889 unsigned short family);
890extern int xfrm_state_check_expire(struct xfrm_state *x);
891extern void xfrm_state_insert(struct xfrm_state *x);
892extern int xfrm_state_add(struct xfrm_state *x);
893extern int xfrm_state_update(struct xfrm_state *x);
894extern struct xfrm_state *xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto, unsigned short family);
895extern struct xfrm_state *xfrm_find_acq_byseq(u32 seq);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700896extern int xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897extern void xfrm_state_flush(u8 proto);
898extern int xfrm_replay_check(struct xfrm_state *x, u32 seq);
899extern void xfrm_replay_advance(struct xfrm_state *x, u32 seq);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800900extern void xfrm_replay_notify(struct xfrm_state *x, int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901extern int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb);
902extern int xfrm_state_mtu(struct xfrm_state *x, int mtu);
Herbert Xu72cb6962005-06-20 13:18:08 -0700903extern int xfrm_init_state(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904extern int xfrm4_rcv(struct sk_buff *skb);
905extern int xfrm4_output(struct sk_buff *skb);
906extern int xfrm4_tunnel_register(struct xfrm_tunnel *handler);
907extern int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800908extern int xfrm6_rcv_spi(struct sk_buff **pskb, u32 spi);
909extern int xfrm6_rcv(struct sk_buff **pskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910extern int xfrm6_tunnel_register(struct xfrm6_tunnel *handler);
911extern int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler);
912extern u32 xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr);
913extern void xfrm6_tunnel_free_spi(xfrm_address_t *saddr);
914extern u32 xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr);
915extern int xfrm6_output(struct sk_buff *skb);
916
917#ifdef CONFIG_XFRM
918extern int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type);
919extern int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen);
920extern int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, unsigned short family);
921#else
922static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
923{
924 return -ENOPROTOOPT;
925}
926
927static inline int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
928{
929 /* should not happen */
930 kfree_skb(skb);
931 return 0;
932}
933static inline int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, unsigned short family)
934{
935 return -EINVAL;
936}
937#endif
938
Al Virodd0fc662005-10-07 07:46:04 +0100939struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940extern int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*), void *);
941int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
Trent Jaegerdf718372005-12-13 23:12:27 -0800942struct xfrm_policy *xfrm_policy_bysel_ctx(int dir, struct xfrm_selector *sel,
943 struct xfrm_sec_ctx *ctx, int delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete);
945void xfrm_policy_flush(void);
946u32 xfrm_get_acqseq(void);
947void xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi);
948struct xfrm_state * xfrm_find_acq(u8 mode, u32 reqid, u8 proto,
949 xfrm_address_t *daddr, xfrm_address_t *saddr,
950 int create, unsigned short family);
951extern void xfrm_policy_flush(void);
952extern int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
953extern int xfrm_flush_bundles(void);
David S. Miller399c1802005-12-19 14:23:23 -0800954extern void xfrm_flush_all_bundles(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955extern int xfrm_bundle_ok(struct xfrm_dst *xdst, struct flowi *fl, int family);
956extern void xfrm_init_pmtu(struct dst_entry *dst);
957
958extern wait_queue_head_t km_waitq;
959extern int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport);
960extern void km_policy_expired(struct xfrm_policy *pol, int dir, int hard);
961
962extern void xfrm_input_init(void);
963extern int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, u32 *spi, u32 *seq);
964
965extern void xfrm_probe_algs(void);
966extern int xfrm_count_auth_supported(void);
967extern int xfrm_count_enc_supported(void);
968extern struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx);
969extern struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx);
970extern struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id);
971extern struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id);
972extern struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id);
973extern struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe);
974extern struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe);
975extern struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe);
976
977struct crypto_tfm;
978typedef void (icv_update_fn_t)(struct crypto_tfm *, struct scatterlist *, unsigned int);
979
980extern void skb_icv_walk(const struct sk_buff *skb, struct crypto_tfm *tfm,
981 int offset, int len, icv_update_fn_t icv_update);
982
983static inline int xfrm_addr_cmp(xfrm_address_t *a, xfrm_address_t *b,
984 int family)
985{
986 switch (family) {
987 default:
988 case AF_INET:
989 return a->a4 - b->a4;
990 case AF_INET6:
991 return ipv6_addr_cmp((struct in6_addr *)a,
992 (struct in6_addr *)b);
993 }
994}
995
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700996static inline int xfrm_policy_id2dir(u32 index)
997{
998 return index & 7;
999}
1000
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001001static inline int xfrm_aevent_is_on(void)
1002{
1003 return netlink_has_listeners(xfrm_nl,XFRMNLGRP_AEVENTS);
1004}
1005
1006static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013#endif /* _NET_XFRM_H */