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