blob: afa508d92c93fdf6b3a4a96946a223c440514750 [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>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080014#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
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
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -080024extern struct sock *xfrm_nl;
25extern u32 sysctl_xfrm_aevent_etime;
26extern u32 sysctl_xfrm_aevent_rseqth;
27
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080028extern struct mutex xfrm_cfg_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
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. */
92struct 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
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800143 /* Replay detection state at the time we sent the last notification */
144 struct xfrm_replay_state preplay;
145
Jamal Hadi Salim27170962006-04-14 15:03:05 -0700146 /* internal flag that only holds state for delayed aevent at the
147 * moment
148 */
149 u32 xflags;
150
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800151 /* Replay detection notification settings */
152 u32 replay_maxage;
153 u32 replay_maxdiff;
154
155 /* Replay detection notification timer */
156 struct timer_list rtimer;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* Statistics */
159 struct xfrm_stats stats;
160
161 struct xfrm_lifetime_cur curlft;
162 struct timer_list timer;
163
164 /* Reference to data common to all the instances of this
165 * transformer. */
166 struct xfrm_type *type;
167
Trent Jaegerdf718372005-12-13 23:12:27 -0800168 /* Security context */
169 struct xfrm_sec_ctx *security;
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* Private data of this transformer, format is opaque,
172 * interpreted by xfrm_type methods. */
173 void *data;
174};
175
Jamal Hadi Salim27170962006-04-14 15:03:05 -0700176/* xflags - make enum if more show up */
177#define XFRM_TIME_DEFER 1
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179enum {
180 XFRM_STATE_VOID,
181 XFRM_STATE_ACQ,
182 XFRM_STATE_VALID,
183 XFRM_STATE_ERROR,
184 XFRM_STATE_EXPIRED,
185 XFRM_STATE_DEAD
186};
187
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700188/* callback structure passed from either netlink or pfkey */
189struct km_event
190{
Herbert Xubf088672005-06-18 22:44:00 -0700191 union {
192 u32 hard;
193 u32 proto;
194 u32 byid;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800195 u32 aevent;
Herbert Xubf088672005-06-18 22:44:00 -0700196 } data;
197
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700198 u32 seq;
199 u32 pid;
200 u32 event;
201};
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203struct xfrm_type;
204struct xfrm_dst;
205struct xfrm_policy_afinfo {
206 unsigned short family;
207 rwlock_t lock;
208 struct xfrm_type_map *type_map;
209 struct dst_ops *dst_ops;
210 void (*garbage_collect)(void);
211 int (*dst_lookup)(struct xfrm_dst **dst, struct flowi *fl);
212 struct dst_entry *(*find_bundle)(struct flowi *fl, struct xfrm_policy *policy);
213 int (*bundle_create)(struct xfrm_policy *policy,
214 struct xfrm_state **xfrm,
215 int nx,
216 struct flowi *fl,
217 struct dst_entry **dst_p);
218 void (*decode_session)(struct sk_buff *skb,
219 struct flowi *fl);
220};
221
222extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo);
223extern int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700224extern void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c);
225extern void km_state_notify(struct xfrm_state *x, struct km_event *c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#define XFRM_ACQ_EXPIRES 30
227
228struct xfrm_tmpl;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800229extern int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800230extern void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
231extern int __xfrm_state_delete(struct xfrm_state *x);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233struct xfrm_state_afinfo {
234 unsigned short family;
235 rwlock_t lock;
236 struct list_head *state_bydst;
237 struct list_head *state_byspi;
Herbert Xud094cd82005-06-20 13:19:41 -0700238 int (*init_flags)(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 void (*init_tempsel)(struct xfrm_state *x, struct flowi *fl,
240 struct xfrm_tmpl *tmpl,
241 xfrm_address_t *daddr, xfrm_address_t *saddr);
242 struct xfrm_state *(*state_lookup)(xfrm_address_t *daddr, u32 spi, u8 proto);
243 struct xfrm_state *(*find_acq)(u8 mode, u32 reqid, u8 proto,
244 xfrm_address_t *daddr, xfrm_address_t *saddr,
245 int create);
246};
247
248extern int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
249extern int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
250
251extern void xfrm_state_delete_tunnel(struct xfrm_state *x);
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253struct xfrm_type
254{
255 char *description;
256 struct module *owner;
257 __u8 proto;
258
Herbert Xu72cb6962005-06-20 13:18:08 -0700259 int (*init_state)(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 void (*destructor)(struct xfrm_state *);
Herbert Xue6956332006-04-01 00:52:46 -0800261 int (*input)(struct xfrm_state *, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int (*output)(struct xfrm_state *, struct sk_buff *pskb);
263 /* Estimate maximal size of result of transformation of a dgram */
264 u32 (*get_max_size)(struct xfrm_state *, int size);
265};
266
267struct xfrm_type_map {
268 rwlock_t lock;
269 struct xfrm_type *map[256];
270};
271
272extern int xfrm_register_type(struct xfrm_type *type, unsigned short family);
273extern int xfrm_unregister_type(struct xfrm_type *type, unsigned short family);
274extern struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family);
275extern void xfrm_put_type(struct xfrm_type *type);
276
277struct xfrm_tmpl
278{
279/* id in template is interpreted as:
280 * daddr - destination of tunnel, may be zero for transport mode.
281 * spi - zero to acquire spi. Not zero if spi is static, then
282 * daddr must be fixed too.
283 * proto - AH/ESP/IPCOMP
284 */
285 struct xfrm_id id;
286
287/* Source address of tunnel. Ignored, if it is not a tunnel. */
288 xfrm_address_t saddr;
289
290 __u32 reqid;
291
292/* Mode: transport/tunnel */
293 __u8 mode;
294
295/* Sharing mode: unique, this session only, this user only etc. */
296 __u8 share;
297
298/* May skip this transfomration if no SA is found */
299 __u8 optional;
300
301/* Bit mask of algos allowed for acquisition */
302 __u32 aalgos;
303 __u32 ealgos;
304 __u32 calgos;
305};
306
307#define XFRM_MAX_DEPTH 4
308
309struct xfrm_policy
310{
311 struct xfrm_policy *next;
312 struct list_head list;
313
314 /* This lock only affects elements except for entry. */
315 rwlock_t lock;
316 atomic_t refcnt;
317 struct timer_list timer;
318
319 u32 priority;
320 u32 index;
321 struct xfrm_selector selector;
322 struct xfrm_lifetime_cfg lft;
323 struct xfrm_lifetime_cur curlft;
324 struct dst_entry *bundles;
325 __u16 family;
326 __u8 action;
327 __u8 flags;
328 __u8 dead;
329 __u8 xfrm_nr;
Trent Jaegerdf718372005-12-13 23:12:27 -0800330 struct xfrm_sec_ctx *security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH];
332};
333
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800334#define XFRM_KM_TIMEOUT 30
335/* which seqno */
336#define XFRM_REPLAY_SEQ 1
337#define XFRM_REPLAY_OSEQ 2
338#define XFRM_REPLAY_SEQ_MASK 3
339/* what happened */
340#define XFRM_REPLAY_UPDATE XFRM_AE_CR
341#define XFRM_REPLAY_TIMEOUT XFRM_AE_CE
342
343/* default aevent timeout in units of 100ms */
344#define XFRM_AE_ETIME 10
345/* Async Event timer multiplier */
346#define XFRM_AE_ETH_M 10
347/* default seq threshold size */
348#define XFRM_AE_SEQT_SIZE 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350struct xfrm_mgr
351{
352 struct list_head list;
353 char *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700354 int (*notify)(struct xfrm_state *x, struct km_event *c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp, int dir);
356 struct xfrm_policy *(*compile_policy)(u16 family, int opt, u8 *data, int len, int *dir);
357 int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700358 int (*notify_policy)(struct xfrm_policy *x, int dir, struct km_event *c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359};
360
361extern int xfrm_register_km(struct xfrm_mgr *km);
362extern int xfrm_unregister_km(struct xfrm_mgr *km);
363
364
365extern struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2];
366
367static inline void xfrm_pol_hold(struct xfrm_policy *policy)
368{
369 if (likely(policy != NULL))
370 atomic_inc(&policy->refcnt);
371}
372
373extern void __xfrm_policy_destroy(struct xfrm_policy *policy);
374
375static inline void xfrm_pol_put(struct xfrm_policy *policy)
376{
377 if (atomic_dec_and_test(&policy->refcnt))
378 __xfrm_policy_destroy(policy);
379}
380
381#define XFRM_DST_HSIZE 1024
382
383static __inline__
384unsigned __xfrm4_dst_hash(xfrm_address_t *addr)
385{
386 unsigned h;
387 h = ntohl(addr->a4);
388 h = (h ^ (h>>16)) % XFRM_DST_HSIZE;
389 return h;
390}
391
392static __inline__
393unsigned __xfrm6_dst_hash(xfrm_address_t *addr)
394{
395 unsigned h;
396 h = ntohl(addr->a6[2]^addr->a6[3]);
397 h = (h ^ (h>>16)) % XFRM_DST_HSIZE;
398 return h;
399}
400
401static __inline__
402unsigned xfrm_dst_hash(xfrm_address_t *addr, unsigned short family)
403{
404 switch (family) {
405 case AF_INET:
406 return __xfrm4_dst_hash(addr);
407 case AF_INET6:
408 return __xfrm6_dst_hash(addr);
409 }
410 return 0;
411}
412
413static __inline__
414unsigned __xfrm4_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto)
415{
416 unsigned h;
417 h = ntohl(addr->a4^spi^proto);
418 h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE;
419 return h;
420}
421
422static __inline__
423unsigned __xfrm6_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto)
424{
425 unsigned h;
426 h = ntohl(addr->a6[2]^addr->a6[3]^spi^proto);
427 h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE;
428 return h;
429}
430
431static __inline__
432unsigned xfrm_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto, unsigned short family)
433{
434 switch (family) {
435 case AF_INET:
436 return __xfrm4_spi_hash(addr, spi, proto);
437 case AF_INET6:
438 return __xfrm6_spi_hash(addr, spi, proto);
439 }
440 return 0; /*XXX*/
441}
442
443extern void __xfrm_state_destroy(struct xfrm_state *);
444
Herbert Xu21380b82006-02-22 14:47:13 -0800445static inline void __xfrm_state_put(struct xfrm_state *x)
446{
447 atomic_dec(&x->refcnt);
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static inline void xfrm_state_put(struct xfrm_state *x)
451{
452 if (atomic_dec_and_test(&x->refcnt))
453 __xfrm_state_destroy(x);
454}
455
456static inline void xfrm_state_hold(struct xfrm_state *x)
457{
458 atomic_inc(&x->refcnt);
459}
460
461static __inline__ int addr_match(void *token1, void *token2, int prefixlen)
462{
463 __u32 *a1 = token1;
464 __u32 *a2 = token2;
465 int pdw;
466 int pbi;
467
468 pdw = prefixlen >> 5; /* num of whole __u32 in prefix */
469 pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */
470
471 if (pdw)
472 if (memcmp(a1, a2, pdw << 2))
473 return 0;
474
475 if (pbi) {
476 __u32 mask;
477
478 mask = htonl((0xffffffff) << (32 - pbi));
479
480 if ((a1[pdw] ^ a2[pdw]) & mask)
481 return 0;
482 }
483
484 return 1;
485}
486
487static __inline__
488u16 xfrm_flowi_sport(struct flowi *fl)
489{
490 u16 port;
491 switch(fl->proto) {
492 case IPPROTO_TCP:
493 case IPPROTO_UDP:
494 case IPPROTO_SCTP:
495 port = fl->fl_ip_sport;
496 break;
497 case IPPROTO_ICMP:
498 case IPPROTO_ICMPV6:
499 port = htons(fl->fl_icmp_type);
500 break;
501 default:
502 port = 0; /*XXX*/
503 }
504 return port;
505}
506
507static __inline__
508u16 xfrm_flowi_dport(struct flowi *fl)
509{
510 u16 port;
511 switch(fl->proto) {
512 case IPPROTO_TCP:
513 case IPPROTO_UDP:
514 case IPPROTO_SCTP:
515 port = fl->fl_ip_dport;
516 break;
517 case IPPROTO_ICMP:
518 case IPPROTO_ICMPV6:
519 port = htons(fl->fl_icmp_code);
520 break;
521 default:
522 port = 0; /*XXX*/
523 }
524 return port;
525}
526
527static inline int
528__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
529{
530 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
531 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
532 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
533 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
534 (fl->proto == sel->proto || !sel->proto) &&
535 (fl->oif == sel->ifindex || !sel->ifindex);
536}
537
538static inline int
539__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
540{
541 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
542 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
543 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
544 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
545 (fl->proto == sel->proto || !sel->proto) &&
546 (fl->oif == sel->ifindex || !sel->ifindex);
547}
548
549static inline int
550xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
551 unsigned short family)
552{
553 switch (family) {
554 case AF_INET:
555 return __xfrm4_selector_match(sel, fl);
556 case AF_INET6:
557 return __xfrm6_selector_match(sel, fl);
558 }
559 return 0;
560}
561
Trent Jaegerdf718372005-12-13 23:12:27 -0800562#ifdef CONFIG_SECURITY_NETWORK_XFRM
563/* If neither has a context --> match
564 * Otherwise, both must have a context and the sids, doi, alg must match
565 */
566static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
567{
568 return ((!s1 && !s2) ||
569 (s1 && s2 &&
570 (s1->ctx_sid == s2->ctx_sid) &&
571 (s1->ctx_doi == s2->ctx_doi) &&
572 (s1->ctx_alg == s2->ctx_alg)));
573}
574#else
575static inline int xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
576{
577 return 1;
578}
579#endif
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/* A struct encoding bundle of transformations to apply to some set of flow.
582 *
583 * dst->child points to the next element of bundle.
584 * dst->xfrm points to an instanse of transformer.
585 *
586 * Due to unfortunate limitations of current routing cache, which we
587 * have no time to fix, it mirrors struct rtable and bound to the same
588 * routing key, including saddr,daddr. However, we can have many of
589 * bundles differing by session id. All the bundles grow from a parent
590 * policy rule.
591 */
592struct xfrm_dst
593{
594 union {
595 struct xfrm_dst *next;
596 struct dst_entry dst;
597 struct rtable rt;
598 struct rt6_info rt6;
599 } u;
600 struct dst_entry *route;
601 u32 route_mtu_cached;
602 u32 child_mtu_cached;
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -0700603 u32 route_cookie;
604 u32 path_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605};
606
Herbert Xuaabc9762005-05-03 16:27:10 -0700607static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)
608{
609 dst_release(xdst->route);
610 if (likely(xdst->u.dst.xfrm))
611 xfrm_state_put(xdst->u.dst.xfrm);
612}
613
614extern void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616struct sec_path
617{
618 atomic_t refcnt;
619 int len;
Herbert Xudbe5b4a2006-04-01 00:54:16 -0800620 struct xfrm_state *xvec[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621};
622
623static inline struct sec_path *
624secpath_get(struct sec_path *sp)
625{
626 if (sp)
627 atomic_inc(&sp->refcnt);
628 return sp;
629}
630
631extern void __secpath_destroy(struct sec_path *sp);
632
633static inline void
634secpath_put(struct sec_path *sp)
635{
636 if (sp && atomic_dec_and_test(&sp->refcnt))
637 __secpath_destroy(sp);
638}
639
640extern struct sec_path *secpath_dup(struct sec_path *src);
641
642static inline void
643secpath_reset(struct sk_buff *skb)
644{
645#ifdef CONFIG_XFRM
646 secpath_put(skb->sp);
647 skb->sp = NULL;
648#endif
649}
650
651static inline int
652__xfrm4_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x)
653{
654 return (tmpl->saddr.a4 &&
655 tmpl->saddr.a4 != x->props.saddr.a4);
656}
657
658static inline int
659__xfrm6_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x)
660{
661 return (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
662 ipv6_addr_cmp((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
663}
664
665static inline int
666xfrm_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x, unsigned short family)
667{
668 switch (family) {
669 case AF_INET:
670 return __xfrm4_state_addr_cmp(tmpl, x);
671 case AF_INET6:
672 return __xfrm6_state_addr_cmp(tmpl, x);
673 }
674 return !0;
675}
676
677#ifdef CONFIG_XFRM
678
679extern int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb, unsigned short family);
680
681static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
682{
683 if (sk && sk->sk_policy[XFRM_POLICY_IN])
684 return __xfrm_policy_check(sk, dir, skb, family);
685
686 return (!xfrm_policy_list[dir] && !skb->sp) ||
687 (skb->dst->flags & DST_NOPOLICY) ||
688 __xfrm_policy_check(sk, dir, skb, family);
689}
690
691static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
692{
693 return xfrm_policy_check(sk, dir, skb, AF_INET);
694}
695
696static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
697{
698 return xfrm_policy_check(sk, dir, skb, AF_INET6);
699}
700
Patrick McHardy3e3850e2006-01-06 23:04:54 -0800701extern int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702extern int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
703
704static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
705{
706 return !xfrm_policy_list[XFRM_POLICY_OUT] ||
707 (skb->dst->flags & DST_NOXFRM) ||
708 __xfrm_route_forward(skb, family);
709}
710
711static inline int xfrm4_route_forward(struct sk_buff *skb)
712{
713 return xfrm_route_forward(skb, AF_INET);
714}
715
716static inline int xfrm6_route_forward(struct sk_buff *skb)
717{
718 return xfrm_route_forward(skb, AF_INET6);
719}
720
721extern int __xfrm_sk_clone_policy(struct sock *sk);
722
723static inline int xfrm_sk_clone_policy(struct sock *sk)
724{
725 if (unlikely(sk->sk_policy[0] || sk->sk_policy[1]))
726 return __xfrm_sk_clone_policy(sk);
727 return 0;
728}
729
Herbert Xu4666faa2005-06-18 22:43:22 -0700730extern int xfrm_policy_delete(struct xfrm_policy *pol, int dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732static inline void xfrm_sk_free_policy(struct sock *sk)
733{
734 if (unlikely(sk->sk_policy[0] != NULL)) {
735 xfrm_policy_delete(sk->sk_policy[0], XFRM_POLICY_MAX);
736 sk->sk_policy[0] = NULL;
737 }
738 if (unlikely(sk->sk_policy[1] != NULL)) {
739 xfrm_policy_delete(sk->sk_policy[1], XFRM_POLICY_MAX+1);
740 sk->sk_policy[1] = NULL;
741 }
742}
743
744#else
745
746static inline void xfrm_sk_free_policy(struct sock *sk) {}
747static inline int xfrm_sk_clone_policy(struct sock *sk) { return 0; }
748static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }
749static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; }
750static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
751{
752 return 1;
753}
754static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
755{
756 return 1;
757}
758static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
759{
760 return 1;
761}
762#endif
763
764static __inline__
765xfrm_address_t *xfrm_flowi_daddr(struct flowi *fl, unsigned short family)
766{
767 switch (family){
768 case AF_INET:
769 return (xfrm_address_t *)&fl->fl4_dst;
770 case AF_INET6:
771 return (xfrm_address_t *)&fl->fl6_dst;
772 }
773 return NULL;
774}
775
776static __inline__
777xfrm_address_t *xfrm_flowi_saddr(struct flowi *fl, unsigned short family)
778{
779 switch (family){
780 case AF_INET:
781 return (xfrm_address_t *)&fl->fl4_src;
782 case AF_INET6:
783 return (xfrm_address_t *)&fl->fl6_src;
784 }
785 return NULL;
786}
787
788static __inline__ int
789__xfrm4_state_addr_check(struct xfrm_state *x,
790 xfrm_address_t *daddr, xfrm_address_t *saddr)
791{
792 if (daddr->a4 == x->id.daddr.a4 &&
793 (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
794 return 1;
795 return 0;
796}
797
798static __inline__ int
799__xfrm6_state_addr_check(struct xfrm_state *x,
800 xfrm_address_t *daddr, xfrm_address_t *saddr)
801{
802 if (!ipv6_addr_cmp((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
803 (!ipv6_addr_cmp((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr)||
804 ipv6_addr_any((struct in6_addr *)saddr) ||
805 ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
806 return 1;
807 return 0;
808}
809
810static __inline__ int
811xfrm_state_addr_check(struct xfrm_state *x,
812 xfrm_address_t *daddr, xfrm_address_t *saddr,
813 unsigned short family)
814{
815 switch (family) {
816 case AF_INET:
817 return __xfrm4_state_addr_check(x, daddr, saddr);
818 case AF_INET6:
819 return __xfrm6_state_addr_check(x, daddr, saddr);
820 }
821 return 0;
822}
823
824static inline int xfrm_state_kern(struct xfrm_state *x)
825{
826 return atomic_read(&x->tunnel_users);
827}
828
829/*
830 * xfrm algorithm information
831 */
832struct xfrm_algo_auth_info {
833 u16 icv_truncbits;
834 u16 icv_fullbits;
835};
836
837struct xfrm_algo_encr_info {
838 u16 blockbits;
839 u16 defkeybits;
840};
841
842struct xfrm_algo_comp_info {
843 u16 threshold;
844};
845
846struct xfrm_algo_desc {
847 char *name;
848 u8 available:1;
849 union {
850 struct xfrm_algo_auth_info auth;
851 struct xfrm_algo_encr_info encr;
852 struct xfrm_algo_comp_info comp;
853 } uinfo;
854 struct sadb_alg desc;
855};
856
857/* XFRM tunnel handlers. */
858struct xfrm_tunnel {
859 int (*handler)(struct sk_buff *skb);
Herbert Xud2acc342006-03-28 01:12:13 -0800860 int (*err_handler)(struct sk_buff *skb, __u32 info);
861
862 struct xfrm_tunnel *next;
863 int priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864};
865
866struct xfrm6_tunnel {
Herbert Xud2acc342006-03-28 01:12:13 -0800867 int (*handler)(struct sk_buff *skb);
868 int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
869 int type, int code, int offset, __u32 info);
870
871 struct xfrm6_tunnel *next;
872 int priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873};
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);
Herbert Xud2acc342006-03-28 01:12:13 -0800908extern int xfrm6_rcv_spi(struct sk_buff *skb, u32 spi);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800909extern 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);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800960extern void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
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{
Patrick McHardybe336902006-03-20 22:40:54 -08001003 struct sock *nlsk;
1004 int ret = 0;
1005
1006 rcu_read_lock();
1007 nlsk = rcu_dereference(xfrm_nl);
1008 if (nlsk)
1009 ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS);
1010 rcu_read_unlock();
1011 return ret;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001012}
1013
1014static inline void xfrm_aevent_doreplay(struct xfrm_state *x)
1015{
1016 if (xfrm_aevent_is_on())
1017 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
1018}
1019
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021#endif /* _NET_XFRM_H */