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