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