Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _NET_XFRM_H |
| 2 | #define _NET_XFRM_H |
| 3 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 4 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 161 | struct xfrm_type; |
| 162 | struct xfrm_dst; |
| 163 | struct xfrm_policy_afinfo { |
| 164 | unsigned short family; |
| 165 | rwlock_t lock; |
| 166 | struct xfrm_type_map *type_map; |
| 167 | struct dst_ops *dst_ops; |
| 168 | void (*garbage_collect)(void); |
| 169 | int (*dst_lookup)(struct xfrm_dst **dst, struct flowi *fl); |
| 170 | struct dst_entry *(*find_bundle)(struct flowi *fl, struct xfrm_policy *policy); |
| 171 | int (*bundle_create)(struct xfrm_policy *policy, |
| 172 | struct xfrm_state **xfrm, |
| 173 | int nx, |
| 174 | struct flowi *fl, |
| 175 | struct dst_entry **dst_p); |
| 176 | void (*decode_session)(struct sk_buff *skb, |
| 177 | struct flowi *fl); |
| 178 | }; |
| 179 | |
| 180 | extern int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo); |
| 181 | extern int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo); |
| 182 | |
| 183 | #define XFRM_ACQ_EXPIRES 30 |
| 184 | |
| 185 | struct xfrm_tmpl; |
| 186 | struct xfrm_state_afinfo { |
| 187 | unsigned short family; |
| 188 | rwlock_t lock; |
| 189 | struct list_head *state_bydst; |
| 190 | struct list_head *state_byspi; |
| 191 | void (*init_tempsel)(struct xfrm_state *x, struct flowi *fl, |
| 192 | struct xfrm_tmpl *tmpl, |
| 193 | xfrm_address_t *daddr, xfrm_address_t *saddr); |
| 194 | struct xfrm_state *(*state_lookup)(xfrm_address_t *daddr, u32 spi, u8 proto); |
| 195 | struct xfrm_state *(*find_acq)(u8 mode, u32 reqid, u8 proto, |
| 196 | xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 197 | int create); |
| 198 | }; |
| 199 | |
| 200 | extern int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo); |
| 201 | extern int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo); |
| 202 | |
| 203 | extern void xfrm_state_delete_tunnel(struct xfrm_state *x); |
| 204 | |
| 205 | struct xfrm_decap_state; |
| 206 | struct xfrm_type |
| 207 | { |
| 208 | char *description; |
| 209 | struct module *owner; |
| 210 | __u8 proto; |
| 211 | |
| 212 | int (*init_state)(struct xfrm_state *x, void *args); |
| 213 | void (*destructor)(struct xfrm_state *); |
| 214 | int (*input)(struct xfrm_state *, struct xfrm_decap_state *, struct sk_buff *skb); |
| 215 | int (*post_input)(struct xfrm_state *, struct xfrm_decap_state *, struct sk_buff *skb); |
| 216 | int (*output)(struct xfrm_state *, struct sk_buff *pskb); |
| 217 | /* Estimate maximal size of result of transformation of a dgram */ |
| 218 | u32 (*get_max_size)(struct xfrm_state *, int size); |
| 219 | }; |
| 220 | |
| 221 | struct xfrm_type_map { |
| 222 | rwlock_t lock; |
| 223 | struct xfrm_type *map[256]; |
| 224 | }; |
| 225 | |
| 226 | extern int xfrm_register_type(struct xfrm_type *type, unsigned short family); |
| 227 | extern int xfrm_unregister_type(struct xfrm_type *type, unsigned short family); |
| 228 | extern struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family); |
| 229 | extern void xfrm_put_type(struct xfrm_type *type); |
| 230 | |
| 231 | struct xfrm_tmpl |
| 232 | { |
| 233 | /* id in template is interpreted as: |
| 234 | * daddr - destination of tunnel, may be zero for transport mode. |
| 235 | * spi - zero to acquire spi. Not zero if spi is static, then |
| 236 | * daddr must be fixed too. |
| 237 | * proto - AH/ESP/IPCOMP |
| 238 | */ |
| 239 | struct xfrm_id id; |
| 240 | |
| 241 | /* Source address of tunnel. Ignored, if it is not a tunnel. */ |
| 242 | xfrm_address_t saddr; |
| 243 | |
| 244 | __u32 reqid; |
| 245 | |
| 246 | /* Mode: transport/tunnel */ |
| 247 | __u8 mode; |
| 248 | |
| 249 | /* Sharing mode: unique, this session only, this user only etc. */ |
| 250 | __u8 share; |
| 251 | |
| 252 | /* May skip this transfomration if no SA is found */ |
| 253 | __u8 optional; |
| 254 | |
| 255 | /* Bit mask of algos allowed for acquisition */ |
| 256 | __u32 aalgos; |
| 257 | __u32 ealgos; |
| 258 | __u32 calgos; |
| 259 | }; |
| 260 | |
| 261 | #define XFRM_MAX_DEPTH 4 |
| 262 | |
| 263 | struct xfrm_policy |
| 264 | { |
| 265 | struct xfrm_policy *next; |
| 266 | struct list_head list; |
| 267 | |
| 268 | /* This lock only affects elements except for entry. */ |
| 269 | rwlock_t lock; |
| 270 | atomic_t refcnt; |
| 271 | struct timer_list timer; |
| 272 | |
| 273 | u32 priority; |
| 274 | u32 index; |
| 275 | struct xfrm_selector selector; |
| 276 | struct xfrm_lifetime_cfg lft; |
| 277 | struct xfrm_lifetime_cur curlft; |
| 278 | struct dst_entry *bundles; |
| 279 | __u16 family; |
| 280 | __u8 action; |
| 281 | __u8 flags; |
| 282 | __u8 dead; |
| 283 | __u8 xfrm_nr; |
| 284 | struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH]; |
| 285 | }; |
| 286 | |
| 287 | #define XFRM_KM_TIMEOUT 30 |
| 288 | |
| 289 | struct xfrm_mgr |
| 290 | { |
| 291 | struct list_head list; |
| 292 | char *id; |
| 293 | int (*notify)(struct xfrm_state *x, int event); |
| 294 | int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp, int dir); |
| 295 | struct xfrm_policy *(*compile_policy)(u16 family, int opt, u8 *data, int len, int *dir); |
| 296 | int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport); |
| 297 | int (*notify_policy)(struct xfrm_policy *x, int dir, int event); |
| 298 | }; |
| 299 | |
| 300 | extern int xfrm_register_km(struct xfrm_mgr *km); |
| 301 | extern int xfrm_unregister_km(struct xfrm_mgr *km); |
| 302 | |
| 303 | |
| 304 | extern struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2]; |
| 305 | |
| 306 | static inline void xfrm_pol_hold(struct xfrm_policy *policy) |
| 307 | { |
| 308 | if (likely(policy != NULL)) |
| 309 | atomic_inc(&policy->refcnt); |
| 310 | } |
| 311 | |
| 312 | extern void __xfrm_policy_destroy(struct xfrm_policy *policy); |
| 313 | |
| 314 | static inline void xfrm_pol_put(struct xfrm_policy *policy) |
| 315 | { |
| 316 | if (atomic_dec_and_test(&policy->refcnt)) |
| 317 | __xfrm_policy_destroy(policy); |
| 318 | } |
| 319 | |
| 320 | #define XFRM_DST_HSIZE 1024 |
| 321 | |
| 322 | static __inline__ |
| 323 | unsigned __xfrm4_dst_hash(xfrm_address_t *addr) |
| 324 | { |
| 325 | unsigned h; |
| 326 | h = ntohl(addr->a4); |
| 327 | h = (h ^ (h>>16)) % XFRM_DST_HSIZE; |
| 328 | return h; |
| 329 | } |
| 330 | |
| 331 | static __inline__ |
| 332 | unsigned __xfrm6_dst_hash(xfrm_address_t *addr) |
| 333 | { |
| 334 | unsigned h; |
| 335 | h = ntohl(addr->a6[2]^addr->a6[3]); |
| 336 | h = (h ^ (h>>16)) % XFRM_DST_HSIZE; |
| 337 | return h; |
| 338 | } |
| 339 | |
| 340 | static __inline__ |
| 341 | unsigned xfrm_dst_hash(xfrm_address_t *addr, unsigned short family) |
| 342 | { |
| 343 | switch (family) { |
| 344 | case AF_INET: |
| 345 | return __xfrm4_dst_hash(addr); |
| 346 | case AF_INET6: |
| 347 | return __xfrm6_dst_hash(addr); |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static __inline__ |
| 353 | unsigned __xfrm4_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto) |
| 354 | { |
| 355 | unsigned h; |
| 356 | h = ntohl(addr->a4^spi^proto); |
| 357 | h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE; |
| 358 | return h; |
| 359 | } |
| 360 | |
| 361 | static __inline__ |
| 362 | unsigned __xfrm6_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto) |
| 363 | { |
| 364 | unsigned h; |
| 365 | h = ntohl(addr->a6[2]^addr->a6[3]^spi^proto); |
| 366 | h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE; |
| 367 | return h; |
| 368 | } |
| 369 | |
| 370 | static __inline__ |
| 371 | unsigned xfrm_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto, unsigned short family) |
| 372 | { |
| 373 | switch (family) { |
| 374 | case AF_INET: |
| 375 | return __xfrm4_spi_hash(addr, spi, proto); |
| 376 | case AF_INET6: |
| 377 | return __xfrm6_spi_hash(addr, spi, proto); |
| 378 | } |
| 379 | return 0; /*XXX*/ |
| 380 | } |
| 381 | |
| 382 | extern void __xfrm_state_destroy(struct xfrm_state *); |
| 383 | |
| 384 | static inline void xfrm_state_put(struct xfrm_state *x) |
| 385 | { |
| 386 | if (atomic_dec_and_test(&x->refcnt)) |
| 387 | __xfrm_state_destroy(x); |
| 388 | } |
| 389 | |
| 390 | static inline void xfrm_state_hold(struct xfrm_state *x) |
| 391 | { |
| 392 | atomic_inc(&x->refcnt); |
| 393 | } |
| 394 | |
| 395 | static __inline__ int addr_match(void *token1, void *token2, int prefixlen) |
| 396 | { |
| 397 | __u32 *a1 = token1; |
| 398 | __u32 *a2 = token2; |
| 399 | int pdw; |
| 400 | int pbi; |
| 401 | |
| 402 | pdw = prefixlen >> 5; /* num of whole __u32 in prefix */ |
| 403 | pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */ |
| 404 | |
| 405 | if (pdw) |
| 406 | if (memcmp(a1, a2, pdw << 2)) |
| 407 | return 0; |
| 408 | |
| 409 | if (pbi) { |
| 410 | __u32 mask; |
| 411 | |
| 412 | mask = htonl((0xffffffff) << (32 - pbi)); |
| 413 | |
| 414 | if ((a1[pdw] ^ a2[pdw]) & mask) |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | static __inline__ |
| 422 | u16 xfrm_flowi_sport(struct flowi *fl) |
| 423 | { |
| 424 | u16 port; |
| 425 | switch(fl->proto) { |
| 426 | case IPPROTO_TCP: |
| 427 | case IPPROTO_UDP: |
| 428 | case IPPROTO_SCTP: |
| 429 | port = fl->fl_ip_sport; |
| 430 | break; |
| 431 | case IPPROTO_ICMP: |
| 432 | case IPPROTO_ICMPV6: |
| 433 | port = htons(fl->fl_icmp_type); |
| 434 | break; |
| 435 | default: |
| 436 | port = 0; /*XXX*/ |
| 437 | } |
| 438 | return port; |
| 439 | } |
| 440 | |
| 441 | static __inline__ |
| 442 | u16 xfrm_flowi_dport(struct flowi *fl) |
| 443 | { |
| 444 | u16 port; |
| 445 | switch(fl->proto) { |
| 446 | case IPPROTO_TCP: |
| 447 | case IPPROTO_UDP: |
| 448 | case IPPROTO_SCTP: |
| 449 | port = fl->fl_ip_dport; |
| 450 | break; |
| 451 | case IPPROTO_ICMP: |
| 452 | case IPPROTO_ICMPV6: |
| 453 | port = htons(fl->fl_icmp_code); |
| 454 | break; |
| 455 | default: |
| 456 | port = 0; /*XXX*/ |
| 457 | } |
| 458 | return port; |
| 459 | } |
| 460 | |
| 461 | static inline int |
| 462 | __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl) |
| 463 | { |
| 464 | return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) && |
| 465 | addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) && |
| 466 | !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) && |
| 467 | !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) && |
| 468 | (fl->proto == sel->proto || !sel->proto) && |
| 469 | (fl->oif == sel->ifindex || !sel->ifindex); |
| 470 | } |
| 471 | |
| 472 | static inline int |
| 473 | __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl) |
| 474 | { |
| 475 | return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) && |
| 476 | addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) && |
| 477 | !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) && |
| 478 | !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) && |
| 479 | (fl->proto == sel->proto || !sel->proto) && |
| 480 | (fl->oif == sel->ifindex || !sel->ifindex); |
| 481 | } |
| 482 | |
| 483 | static inline int |
| 484 | xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl, |
| 485 | unsigned short family) |
| 486 | { |
| 487 | switch (family) { |
| 488 | case AF_INET: |
| 489 | return __xfrm4_selector_match(sel, fl); |
| 490 | case AF_INET6: |
| 491 | return __xfrm6_selector_match(sel, fl); |
| 492 | } |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | /* A struct encoding bundle of transformations to apply to some set of flow. |
| 497 | * |
| 498 | * dst->child points to the next element of bundle. |
| 499 | * dst->xfrm points to an instanse of transformer. |
| 500 | * |
| 501 | * Due to unfortunate limitations of current routing cache, which we |
| 502 | * have no time to fix, it mirrors struct rtable and bound to the same |
| 503 | * routing key, including saddr,daddr. However, we can have many of |
| 504 | * bundles differing by session id. All the bundles grow from a parent |
| 505 | * policy rule. |
| 506 | */ |
| 507 | struct xfrm_dst |
| 508 | { |
| 509 | union { |
| 510 | struct xfrm_dst *next; |
| 511 | struct dst_entry dst; |
| 512 | struct rtable rt; |
| 513 | struct rt6_info rt6; |
| 514 | } u; |
| 515 | struct dst_entry *route; |
| 516 | u32 route_mtu_cached; |
| 517 | u32 child_mtu_cached; |
Hideaki YOSHIFUJI | 92d63dec | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 518 | u32 route_cookie; |
| 519 | u32 path_cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | }; |
| 521 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 522 | static inline void xfrm_dst_destroy(struct xfrm_dst *xdst) |
| 523 | { |
| 524 | dst_release(xdst->route); |
| 525 | if (likely(xdst->u.dst.xfrm)) |
| 526 | xfrm_state_put(xdst->u.dst.xfrm); |
| 527 | } |
| 528 | |
| 529 | extern void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev); |
| 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | /* Decapsulation state, used by the input to store data during |
| 532 | * decapsulation procedure, to be used later (during the policy |
| 533 | * check |
| 534 | */ |
| 535 | struct xfrm_decap_state { |
| 536 | char decap_data[20]; |
| 537 | __u16 decap_type; |
| 538 | }; |
| 539 | |
| 540 | struct sec_decap_state { |
| 541 | struct xfrm_state *xvec; |
| 542 | struct xfrm_decap_state decap; |
| 543 | }; |
| 544 | |
| 545 | struct sec_path |
| 546 | { |
| 547 | atomic_t refcnt; |
| 548 | int len; |
| 549 | struct sec_decap_state x[XFRM_MAX_DEPTH]; |
| 550 | }; |
| 551 | |
| 552 | static inline struct sec_path * |
| 553 | secpath_get(struct sec_path *sp) |
| 554 | { |
| 555 | if (sp) |
| 556 | atomic_inc(&sp->refcnt); |
| 557 | return sp; |
| 558 | } |
| 559 | |
| 560 | extern void __secpath_destroy(struct sec_path *sp); |
| 561 | |
| 562 | static inline void |
| 563 | secpath_put(struct sec_path *sp) |
| 564 | { |
| 565 | if (sp && atomic_dec_and_test(&sp->refcnt)) |
| 566 | __secpath_destroy(sp); |
| 567 | } |
| 568 | |
| 569 | extern struct sec_path *secpath_dup(struct sec_path *src); |
| 570 | |
| 571 | static inline void |
| 572 | secpath_reset(struct sk_buff *skb) |
| 573 | { |
| 574 | #ifdef CONFIG_XFRM |
| 575 | secpath_put(skb->sp); |
| 576 | skb->sp = NULL; |
| 577 | #endif |
| 578 | } |
| 579 | |
| 580 | static inline int |
| 581 | __xfrm4_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x) |
| 582 | { |
| 583 | return (tmpl->saddr.a4 && |
| 584 | tmpl->saddr.a4 != x->props.saddr.a4); |
| 585 | } |
| 586 | |
| 587 | static inline int |
| 588 | __xfrm6_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x) |
| 589 | { |
| 590 | return (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) && |
| 591 | ipv6_addr_cmp((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr)); |
| 592 | } |
| 593 | |
| 594 | static inline int |
| 595 | xfrm_state_addr_cmp(struct xfrm_tmpl *tmpl, struct xfrm_state *x, unsigned short family) |
| 596 | { |
| 597 | switch (family) { |
| 598 | case AF_INET: |
| 599 | return __xfrm4_state_addr_cmp(tmpl, x); |
| 600 | case AF_INET6: |
| 601 | return __xfrm6_state_addr_cmp(tmpl, x); |
| 602 | } |
| 603 | return !0; |
| 604 | } |
| 605 | |
| 606 | #ifdef CONFIG_XFRM |
| 607 | |
| 608 | extern int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb, unsigned short family); |
| 609 | |
| 610 | static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family) |
| 611 | { |
| 612 | if (sk && sk->sk_policy[XFRM_POLICY_IN]) |
| 613 | return __xfrm_policy_check(sk, dir, skb, family); |
| 614 | |
| 615 | return (!xfrm_policy_list[dir] && !skb->sp) || |
| 616 | (skb->dst->flags & DST_NOPOLICY) || |
| 617 | __xfrm_policy_check(sk, dir, skb, family); |
| 618 | } |
| 619 | |
| 620 | static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb) |
| 621 | { |
| 622 | return xfrm_policy_check(sk, dir, skb, AF_INET); |
| 623 | } |
| 624 | |
| 625 | static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb) |
| 626 | { |
| 627 | return xfrm_policy_check(sk, dir, skb, AF_INET6); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | extern int __xfrm_route_forward(struct sk_buff *skb, unsigned short family); |
| 632 | |
| 633 | static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family) |
| 634 | { |
| 635 | return !xfrm_policy_list[XFRM_POLICY_OUT] || |
| 636 | (skb->dst->flags & DST_NOXFRM) || |
| 637 | __xfrm_route_forward(skb, family); |
| 638 | } |
| 639 | |
| 640 | static inline int xfrm4_route_forward(struct sk_buff *skb) |
| 641 | { |
| 642 | return xfrm_route_forward(skb, AF_INET); |
| 643 | } |
| 644 | |
| 645 | static inline int xfrm6_route_forward(struct sk_buff *skb) |
| 646 | { |
| 647 | return xfrm_route_forward(skb, AF_INET6); |
| 648 | } |
| 649 | |
| 650 | extern int __xfrm_sk_clone_policy(struct sock *sk); |
| 651 | |
| 652 | static inline int xfrm_sk_clone_policy(struct sock *sk) |
| 653 | { |
| 654 | if (unlikely(sk->sk_policy[0] || sk->sk_policy[1])) |
| 655 | return __xfrm_sk_clone_policy(sk); |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | extern void xfrm_policy_delete(struct xfrm_policy *pol, int dir); |
| 660 | |
| 661 | static inline void xfrm_sk_free_policy(struct sock *sk) |
| 662 | { |
| 663 | if (unlikely(sk->sk_policy[0] != NULL)) { |
| 664 | xfrm_policy_delete(sk->sk_policy[0], XFRM_POLICY_MAX); |
| 665 | sk->sk_policy[0] = NULL; |
| 666 | } |
| 667 | if (unlikely(sk->sk_policy[1] != NULL)) { |
| 668 | xfrm_policy_delete(sk->sk_policy[1], XFRM_POLICY_MAX+1); |
| 669 | sk->sk_policy[1] = NULL; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | #else |
| 674 | |
| 675 | static inline void xfrm_sk_free_policy(struct sock *sk) {} |
| 676 | static inline int xfrm_sk_clone_policy(struct sock *sk) { return 0; } |
| 677 | static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; } |
| 678 | static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; } |
| 679 | static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb) |
| 680 | { |
| 681 | return 1; |
| 682 | } |
| 683 | static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb) |
| 684 | { |
| 685 | return 1; |
| 686 | } |
| 687 | static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family) |
| 688 | { |
| 689 | return 1; |
| 690 | } |
| 691 | #endif |
| 692 | |
| 693 | static __inline__ |
| 694 | xfrm_address_t *xfrm_flowi_daddr(struct flowi *fl, unsigned short family) |
| 695 | { |
| 696 | switch (family){ |
| 697 | case AF_INET: |
| 698 | return (xfrm_address_t *)&fl->fl4_dst; |
| 699 | case AF_INET6: |
| 700 | return (xfrm_address_t *)&fl->fl6_dst; |
| 701 | } |
| 702 | return NULL; |
| 703 | } |
| 704 | |
| 705 | static __inline__ |
| 706 | xfrm_address_t *xfrm_flowi_saddr(struct flowi *fl, unsigned short family) |
| 707 | { |
| 708 | switch (family){ |
| 709 | case AF_INET: |
| 710 | return (xfrm_address_t *)&fl->fl4_src; |
| 711 | case AF_INET6: |
| 712 | return (xfrm_address_t *)&fl->fl6_src; |
| 713 | } |
| 714 | return NULL; |
| 715 | } |
| 716 | |
| 717 | static __inline__ int |
| 718 | __xfrm4_state_addr_check(struct xfrm_state *x, |
| 719 | xfrm_address_t *daddr, xfrm_address_t *saddr) |
| 720 | { |
| 721 | if (daddr->a4 == x->id.daddr.a4 && |
| 722 | (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4)) |
| 723 | return 1; |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | static __inline__ int |
| 728 | __xfrm6_state_addr_check(struct xfrm_state *x, |
| 729 | xfrm_address_t *daddr, xfrm_address_t *saddr) |
| 730 | { |
| 731 | if (!ipv6_addr_cmp((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) && |
| 732 | (!ipv6_addr_cmp((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr)|| |
| 733 | ipv6_addr_any((struct in6_addr *)saddr) || |
| 734 | ipv6_addr_any((struct in6_addr *)&x->props.saddr))) |
| 735 | return 1; |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static __inline__ int |
| 740 | xfrm_state_addr_check(struct xfrm_state *x, |
| 741 | xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 742 | unsigned short family) |
| 743 | { |
| 744 | switch (family) { |
| 745 | case AF_INET: |
| 746 | return __xfrm4_state_addr_check(x, daddr, saddr); |
| 747 | case AF_INET6: |
| 748 | return __xfrm6_state_addr_check(x, daddr, saddr); |
| 749 | } |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | static inline int xfrm_state_kern(struct xfrm_state *x) |
| 754 | { |
| 755 | return atomic_read(&x->tunnel_users); |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * xfrm algorithm information |
| 760 | */ |
| 761 | struct xfrm_algo_auth_info { |
| 762 | u16 icv_truncbits; |
| 763 | u16 icv_fullbits; |
| 764 | }; |
| 765 | |
| 766 | struct xfrm_algo_encr_info { |
| 767 | u16 blockbits; |
| 768 | u16 defkeybits; |
| 769 | }; |
| 770 | |
| 771 | struct xfrm_algo_comp_info { |
| 772 | u16 threshold; |
| 773 | }; |
| 774 | |
| 775 | struct xfrm_algo_desc { |
| 776 | char *name; |
| 777 | u8 available:1; |
| 778 | union { |
| 779 | struct xfrm_algo_auth_info auth; |
| 780 | struct xfrm_algo_encr_info encr; |
| 781 | struct xfrm_algo_comp_info comp; |
| 782 | } uinfo; |
| 783 | struct sadb_alg desc; |
| 784 | }; |
| 785 | |
| 786 | /* XFRM tunnel handlers. */ |
| 787 | struct xfrm_tunnel { |
| 788 | int (*handler)(struct sk_buff *skb); |
| 789 | void (*err_handler)(struct sk_buff *skb, void *info); |
| 790 | }; |
| 791 | |
| 792 | struct xfrm6_tunnel { |
| 793 | int (*handler)(struct sk_buff **pskb, unsigned int *nhoffp); |
| 794 | void (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, |
| 795 | int type, int code, int offset, __u32 info); |
| 796 | }; |
| 797 | |
| 798 | extern void xfrm_init(void); |
| 799 | extern void xfrm4_init(void); |
| 800 | extern void xfrm6_init(void); |
| 801 | extern void xfrm6_fini(void); |
| 802 | extern void xfrm_state_init(void); |
| 803 | extern void xfrm4_state_init(void); |
| 804 | extern void xfrm4_state_fini(void); |
| 805 | extern void xfrm6_state_init(void); |
| 806 | extern void xfrm6_state_fini(void); |
| 807 | |
| 808 | extern int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*), void *); |
| 809 | extern struct xfrm_state *xfrm_state_alloc(void); |
| 810 | extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 811 | struct flowi *fl, struct xfrm_tmpl *tmpl, |
| 812 | struct xfrm_policy *pol, int *err, |
| 813 | unsigned short family); |
| 814 | extern int xfrm_state_check_expire(struct xfrm_state *x); |
| 815 | extern void xfrm_state_insert(struct xfrm_state *x); |
| 816 | extern int xfrm_state_add(struct xfrm_state *x); |
| 817 | extern int xfrm_state_update(struct xfrm_state *x); |
| 818 | extern struct xfrm_state *xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto, unsigned short family); |
| 819 | extern struct xfrm_state *xfrm_find_acq_byseq(u32 seq); |
| 820 | extern void xfrm_state_delete(struct xfrm_state *x); |
| 821 | extern void xfrm_state_flush(u8 proto); |
| 822 | extern int xfrm_replay_check(struct xfrm_state *x, u32 seq); |
| 823 | extern void xfrm_replay_advance(struct xfrm_state *x, u32 seq); |
| 824 | extern int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb); |
| 825 | extern int xfrm_state_mtu(struct xfrm_state *x, int mtu); |
| 826 | extern int xfrm4_rcv(struct sk_buff *skb); |
| 827 | extern int xfrm4_output(struct sk_buff *skb); |
| 828 | extern int xfrm4_tunnel_register(struct xfrm_tunnel *handler); |
| 829 | extern int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler); |
| 830 | extern int xfrm6_rcv_spi(struct sk_buff **pskb, unsigned int *nhoffp, u32 spi); |
| 831 | extern int xfrm6_rcv(struct sk_buff **pskb, unsigned int *nhoffp); |
| 832 | extern int xfrm6_tunnel_register(struct xfrm6_tunnel *handler); |
| 833 | extern int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler); |
| 834 | extern u32 xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr); |
| 835 | extern void xfrm6_tunnel_free_spi(xfrm_address_t *saddr); |
| 836 | extern u32 xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr); |
| 837 | extern int xfrm6_output(struct sk_buff *skb); |
| 838 | |
| 839 | #ifdef CONFIG_XFRM |
| 840 | extern int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type); |
| 841 | extern int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen); |
| 842 | extern int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, unsigned short family); |
| 843 | #else |
| 844 | static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) |
| 845 | { |
| 846 | return -ENOPROTOOPT; |
| 847 | } |
| 848 | |
| 849 | static inline int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type) |
| 850 | { |
| 851 | /* should not happen */ |
| 852 | kfree_skb(skb); |
| 853 | return 0; |
| 854 | } |
| 855 | static inline int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, unsigned short family) |
| 856 | { |
| 857 | return -EINVAL; |
| 858 | } |
| 859 | #endif |
| 860 | |
| 861 | struct xfrm_policy *xfrm_policy_alloc(int gfp); |
| 862 | extern int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*), void *); |
| 863 | int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl); |
| 864 | struct xfrm_policy *xfrm_policy_bysel(int dir, struct xfrm_selector *sel, |
| 865 | int delete); |
| 866 | struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete); |
| 867 | void xfrm_policy_flush(void); |
| 868 | u32 xfrm_get_acqseq(void); |
| 869 | void xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi); |
| 870 | struct xfrm_state * xfrm_find_acq(u8 mode, u32 reqid, u8 proto, |
| 871 | xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 872 | int create, unsigned short family); |
| 873 | extern void xfrm_policy_flush(void); |
| 874 | extern int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol); |
| 875 | extern int xfrm_flush_bundles(void); |
| 876 | extern int xfrm_bundle_ok(struct xfrm_dst *xdst, struct flowi *fl, int family); |
| 877 | extern void xfrm_init_pmtu(struct dst_entry *dst); |
| 878 | |
| 879 | extern wait_queue_head_t km_waitq; |
| 880 | extern int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport); |
| 881 | extern void km_policy_expired(struct xfrm_policy *pol, int dir, int hard); |
| 882 | |
| 883 | extern void xfrm_input_init(void); |
| 884 | extern int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, u32 *spi, u32 *seq); |
| 885 | |
| 886 | extern void xfrm_probe_algs(void); |
| 887 | extern int xfrm_count_auth_supported(void); |
| 888 | extern int xfrm_count_enc_supported(void); |
| 889 | extern struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx); |
| 890 | extern struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx); |
| 891 | extern struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id); |
| 892 | extern struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id); |
| 893 | extern struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id); |
| 894 | extern struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe); |
| 895 | extern struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe); |
| 896 | extern struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe); |
| 897 | |
| 898 | struct crypto_tfm; |
| 899 | typedef void (icv_update_fn_t)(struct crypto_tfm *, struct scatterlist *, unsigned int); |
| 900 | |
| 901 | extern void skb_icv_walk(const struct sk_buff *skb, struct crypto_tfm *tfm, |
| 902 | int offset, int len, icv_update_fn_t icv_update); |
| 903 | |
| 904 | static inline int xfrm_addr_cmp(xfrm_address_t *a, xfrm_address_t *b, |
| 905 | int family) |
| 906 | { |
| 907 | switch (family) { |
| 908 | default: |
| 909 | case AF_INET: |
| 910 | return a->a4 - b->a4; |
| 911 | case AF_INET6: |
| 912 | return ipv6_addr_cmp((struct in6_addr *)a, |
| 913 | (struct in6_addr *)b); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | #endif /* _NET_XFRM_H */ |