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