rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * net/key/af_key.c An implementation of PF_KEYv2 sockets. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Maxim Giryaev <gem@asplinux.ru> |
| 10 | * David S. Miller <davem@redhat.com> |
| 11 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
| 12 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
| 13 | * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org> |
| 14 | * Derek Atkins <derek@ihtfp.com> |
| 15 | */ |
| 16 | |
| 17 | #include <linux/capability.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/socket.h> |
| 21 | #include <linux/pfkeyv2.h> |
| 22 | #include <linux/ipsec.h> |
| 23 | #include <linux/skbuff.h> |
| 24 | #include <linux/rtnetlink.h> |
| 25 | #include <linux/in.h> |
| 26 | #include <linux/in6.h> |
| 27 | #include <linux/proc_fs.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <net/net_namespace.h> |
| 31 | #include <net/netns/generic.h> |
| 32 | #include <net/xfrm.h> |
| 33 | |
| 34 | #include <net/sock.h> |
| 35 | |
| 36 | #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x)) |
| 37 | #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x)) |
| 38 | |
| 39 | static unsigned int pfkey_net_id __read_mostly; |
| 40 | struct netns_pfkey { |
| 41 | /* List of all pfkey sockets. */ |
| 42 | struct hlist_head table; |
| 43 | atomic_t socks_nr; |
| 44 | }; |
| 45 | static DEFINE_MUTEX(pfkey_mutex); |
| 46 | |
| 47 | #define DUMMY_MARK 0 |
| 48 | static const struct xfrm_mark dummy_mark = {0, 0}; |
| 49 | struct pfkey_sock { |
| 50 | /* struct sock must be the first member of struct pfkey_sock */ |
| 51 | struct sock sk; |
| 52 | int registered; |
| 53 | int promisc; |
| 54 | |
| 55 | struct { |
| 56 | uint8_t msg_version; |
| 57 | uint32_t msg_portid; |
| 58 | int (*dump)(struct pfkey_sock *sk); |
| 59 | void (*done)(struct pfkey_sock *sk); |
| 60 | union { |
| 61 | struct xfrm_policy_walk policy; |
| 62 | struct xfrm_state_walk state; |
| 63 | } u; |
| 64 | struct sk_buff *skb; |
| 65 | } dump; |
| 66 | struct mutex dump_lock; |
| 67 | }; |
| 68 | |
| 69 | static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len, |
| 70 | xfrm_address_t *saddr, xfrm_address_t *daddr, |
| 71 | u16 *family); |
| 72 | |
| 73 | static inline struct pfkey_sock *pfkey_sk(struct sock *sk) |
| 74 | { |
| 75 | return (struct pfkey_sock *)sk; |
| 76 | } |
| 77 | |
| 78 | static int pfkey_can_dump(const struct sock *sk) |
| 79 | { |
| 80 | if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf) |
| 81 | return 1; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static void pfkey_terminate_dump(struct pfkey_sock *pfk) |
| 86 | { |
| 87 | if (pfk->dump.dump) { |
| 88 | if (pfk->dump.skb) { |
| 89 | kfree_skb(pfk->dump.skb); |
| 90 | pfk->dump.skb = NULL; |
| 91 | } |
| 92 | pfk->dump.done(pfk); |
| 93 | pfk->dump.dump = NULL; |
| 94 | pfk->dump.done = NULL; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static void pfkey_sock_destruct(struct sock *sk) |
| 99 | { |
| 100 | struct net *net = sock_net(sk); |
| 101 | struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); |
| 102 | |
| 103 | pfkey_terminate_dump(pfkey_sk(sk)); |
| 104 | skb_queue_purge(&sk->sk_receive_queue); |
| 105 | |
| 106 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 107 | pr_err("Attempt to release alive pfkey socket: %p\n", sk); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | WARN_ON(atomic_read(&sk->sk_rmem_alloc)); |
| 112 | WARN_ON(refcount_read(&sk->sk_wmem_alloc)); |
| 113 | |
| 114 | atomic_dec(&net_pfkey->socks_nr); |
| 115 | } |
| 116 | |
| 117 | static const struct proto_ops pfkey_ops; |
| 118 | |
| 119 | static void pfkey_insert(struct sock *sk) |
| 120 | { |
| 121 | struct net *net = sock_net(sk); |
| 122 | struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); |
| 123 | |
| 124 | mutex_lock(&pfkey_mutex); |
| 125 | sk_add_node_rcu(sk, &net_pfkey->table); |
| 126 | mutex_unlock(&pfkey_mutex); |
| 127 | } |
| 128 | |
| 129 | static void pfkey_remove(struct sock *sk) |
| 130 | { |
| 131 | mutex_lock(&pfkey_mutex); |
| 132 | sk_del_node_init_rcu(sk); |
| 133 | mutex_unlock(&pfkey_mutex); |
| 134 | } |
| 135 | |
| 136 | static struct proto key_proto = { |
| 137 | .name = "KEY", |
| 138 | .owner = THIS_MODULE, |
| 139 | .obj_size = sizeof(struct pfkey_sock), |
| 140 | }; |
| 141 | |
| 142 | static int pfkey_create(struct net *net, struct socket *sock, int protocol, |
| 143 | int kern) |
| 144 | { |
| 145 | struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); |
| 146 | struct sock *sk; |
| 147 | struct pfkey_sock *pfk; |
| 148 | int err; |
| 149 | |
| 150 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
| 151 | return -EPERM; |
| 152 | if (sock->type != SOCK_RAW) |
| 153 | return -ESOCKTNOSUPPORT; |
| 154 | if (protocol != PF_KEY_V2) |
| 155 | return -EPROTONOSUPPORT; |
| 156 | |
| 157 | err = -ENOMEM; |
| 158 | sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto, kern); |
| 159 | if (sk == NULL) |
| 160 | goto out; |
| 161 | |
| 162 | pfk = pfkey_sk(sk); |
| 163 | mutex_init(&pfk->dump_lock); |
| 164 | |
| 165 | sock->ops = &pfkey_ops; |
| 166 | sock_init_data(sock, sk); |
| 167 | |
| 168 | sk->sk_family = PF_KEY; |
| 169 | sk->sk_destruct = pfkey_sock_destruct; |
| 170 | |
| 171 | atomic_inc(&net_pfkey->socks_nr); |
| 172 | |
| 173 | pfkey_insert(sk); |
| 174 | |
| 175 | return 0; |
| 176 | out: |
| 177 | return err; |
| 178 | } |
| 179 | |
| 180 | static int pfkey_release(struct socket *sock) |
| 181 | { |
| 182 | struct sock *sk = sock->sk; |
| 183 | |
| 184 | if (!sk) |
| 185 | return 0; |
| 186 | |
| 187 | pfkey_remove(sk); |
| 188 | |
| 189 | sock_orphan(sk); |
| 190 | sock->sk = NULL; |
| 191 | skb_queue_purge(&sk->sk_write_queue); |
| 192 | |
| 193 | synchronize_rcu(); |
| 194 | sock_put(sk); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int pfkey_broadcast_one(struct sk_buff *skb, gfp_t allocation, |
| 200 | struct sock *sk) |
| 201 | { |
| 202 | int err = -ENOBUFS; |
| 203 | |
| 204 | if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) |
| 205 | return err; |
| 206 | |
| 207 | skb = skb_clone(skb, allocation); |
| 208 | |
| 209 | if (skb) { |
| 210 | skb_set_owner_r(skb, sk); |
| 211 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 212 | sk->sk_data_ready(sk); |
| 213 | err = 0; |
| 214 | } |
| 215 | return err; |
| 216 | } |
| 217 | |
| 218 | /* Send SKB to all pfkey sockets matching selected criteria. */ |
| 219 | #define BROADCAST_ALL 0 |
| 220 | #define BROADCAST_ONE 1 |
| 221 | #define BROADCAST_REGISTERED 2 |
| 222 | #define BROADCAST_PROMISC_ONLY 4 |
| 223 | static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation, |
| 224 | int broadcast_flags, struct sock *one_sk, |
| 225 | struct net *net) |
| 226 | { |
| 227 | struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); |
| 228 | struct sock *sk; |
| 229 | int err = -ESRCH; |
| 230 | |
| 231 | /* XXX Do we need something like netlink_overrun? I think |
| 232 | * XXX PF_KEY socket apps will not mind current behavior. |
| 233 | */ |
| 234 | if (!skb) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | rcu_read_lock(); |
| 238 | sk_for_each_rcu(sk, &net_pfkey->table) { |
| 239 | struct pfkey_sock *pfk = pfkey_sk(sk); |
| 240 | int err2; |
| 241 | |
| 242 | /* Yes, it means that if you are meant to receive this |
| 243 | * pfkey message you receive it twice as promiscuous |
| 244 | * socket. |
| 245 | */ |
| 246 | if (pfk->promisc) |
| 247 | pfkey_broadcast_one(skb, GFP_ATOMIC, sk); |
| 248 | |
| 249 | /* the exact target will be processed later */ |
| 250 | if (sk == one_sk) |
| 251 | continue; |
| 252 | if (broadcast_flags != BROADCAST_ALL) { |
| 253 | if (broadcast_flags & BROADCAST_PROMISC_ONLY) |
| 254 | continue; |
| 255 | if ((broadcast_flags & BROADCAST_REGISTERED) && |
| 256 | !pfk->registered) |
| 257 | continue; |
| 258 | if (broadcast_flags & BROADCAST_ONE) |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | err2 = pfkey_broadcast_one(skb, GFP_ATOMIC, sk); |
| 263 | |
| 264 | /* Error is cleared after successful sending to at least one |
| 265 | * registered KM */ |
| 266 | if ((broadcast_flags & BROADCAST_REGISTERED) && err) |
| 267 | err = err2; |
| 268 | } |
| 269 | rcu_read_unlock(); |
| 270 | |
| 271 | if (one_sk != NULL) |
| 272 | err = pfkey_broadcast_one(skb, allocation, one_sk); |
| 273 | |
| 274 | kfree_skb(skb); |
| 275 | return err; |
| 276 | } |
| 277 | |
| 278 | static int pfkey_do_dump(struct pfkey_sock *pfk) |
| 279 | { |
| 280 | struct sadb_msg *hdr; |
| 281 | int rc; |
| 282 | |
| 283 | mutex_lock(&pfk->dump_lock); |
| 284 | if (!pfk->dump.dump) { |
| 285 | rc = 0; |
| 286 | goto out; |
| 287 | } |
| 288 | |
| 289 | rc = pfk->dump.dump(pfk); |
| 290 | if (rc == -ENOBUFS) { |
| 291 | rc = 0; |
| 292 | goto out; |
| 293 | } |
| 294 | |
| 295 | if (pfk->dump.skb) { |
| 296 | if (!pfkey_can_dump(&pfk->sk)) { |
| 297 | rc = 0; |
| 298 | goto out; |
| 299 | } |
| 300 | |
| 301 | hdr = (struct sadb_msg *) pfk->dump.skb->data; |
| 302 | hdr->sadb_msg_seq = 0; |
| 303 | hdr->sadb_msg_errno = rc; |
| 304 | pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE, |
| 305 | &pfk->sk, sock_net(&pfk->sk)); |
| 306 | pfk->dump.skb = NULL; |
| 307 | } |
| 308 | |
| 309 | pfkey_terminate_dump(pfk); |
| 310 | |
| 311 | out: |
| 312 | mutex_unlock(&pfk->dump_lock); |
| 313 | return rc; |
| 314 | } |
| 315 | |
| 316 | static inline void pfkey_hdr_dup(struct sadb_msg *new, |
| 317 | const struct sadb_msg *orig) |
| 318 | { |
| 319 | *new = *orig; |
| 320 | } |
| 321 | |
| 322 | static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk) |
| 323 | { |
| 324 | struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL); |
| 325 | struct sadb_msg *hdr; |
| 326 | |
| 327 | if (!skb) |
| 328 | return -ENOBUFS; |
| 329 | |
| 330 | /* Woe be to the platform trying to support PFKEY yet |
| 331 | * having normal errnos outside the 1-255 range, inclusive. |
| 332 | */ |
| 333 | err = -err; |
| 334 | if (err == ERESTARTSYS || |
| 335 | err == ERESTARTNOHAND || |
| 336 | err == ERESTARTNOINTR) |
| 337 | err = EINTR; |
| 338 | if (err >= 512) |
| 339 | err = EINVAL; |
| 340 | BUG_ON(err <= 0 || err >= 256); |
| 341 | |
| 342 | hdr = skb_put(skb, sizeof(struct sadb_msg)); |
| 343 | pfkey_hdr_dup(hdr, orig); |
| 344 | hdr->sadb_msg_errno = (uint8_t) err; |
| 345 | hdr->sadb_msg_len = (sizeof(struct sadb_msg) / |
| 346 | sizeof(uint64_t)); |
| 347 | |
| 348 | pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk)); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static const u8 sadb_ext_min_len[] = { |
| 354 | [SADB_EXT_RESERVED] = (u8) 0, |
| 355 | [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa), |
| 356 | [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime), |
| 357 | [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime), |
| 358 | [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime), |
| 359 | [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address), |
| 360 | [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address), |
| 361 | [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address), |
| 362 | [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key), |
| 363 | [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key), |
| 364 | [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident), |
| 365 | [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident), |
| 366 | [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens), |
| 367 | [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop), |
| 368 | [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported), |
| 369 | [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported), |
| 370 | [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange), |
| 371 | [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate), |
| 372 | [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy), |
| 373 | [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2), |
| 374 | [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type), |
| 375 | [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port), |
| 376 | [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port), |
| 377 | [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address), |
| 378 | [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx), |
| 379 | [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress), |
| 380 | [SADB_X_EXT_FILTER] = (u8) sizeof(struct sadb_x_filter), |
| 381 | }; |
| 382 | |
| 383 | /* Verify sadb_address_{len,prefixlen} against sa_family. */ |
| 384 | static int verify_address_len(const void *p) |
| 385 | { |
| 386 | const struct sadb_address *sp = p; |
| 387 | const struct sockaddr *addr = (const struct sockaddr *)(sp + 1); |
| 388 | const struct sockaddr_in *sin; |
| 389 | #if IS_ENABLED(CONFIG_IPV6) |
| 390 | const struct sockaddr_in6 *sin6; |
| 391 | #endif |
| 392 | int len; |
| 393 | |
| 394 | if (sp->sadb_address_len < |
| 395 | DIV_ROUND_UP(sizeof(*sp) + offsetofend(typeof(*addr), sa_family), |
| 396 | sizeof(uint64_t))) |
| 397 | return -EINVAL; |
| 398 | |
| 399 | switch (addr->sa_family) { |
| 400 | case AF_INET: |
| 401 | len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t)); |
| 402 | if (sp->sadb_address_len != len || |
| 403 | sp->sadb_address_prefixlen > 32) |
| 404 | return -EINVAL; |
| 405 | break; |
| 406 | #if IS_ENABLED(CONFIG_IPV6) |
| 407 | case AF_INET6: |
| 408 | len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t)); |
| 409 | if (sp->sadb_address_len != len || |
| 410 | sp->sadb_address_prefixlen > 128) |
| 411 | return -EINVAL; |
| 412 | break; |
| 413 | #endif |
| 414 | default: |
| 415 | /* It is user using kernel to keep track of security |
| 416 | * associations for another protocol, such as |
| 417 | * OSPF/RSVP/RIPV2/MIP. It is user's job to verify |
| 418 | * lengths. |
| 419 | * |
| 420 | * XXX Actually, association/policy database is not yet |
| 421 | * XXX able to cope with arbitrary sockaddr families. |
| 422 | * XXX When it can, remove this -EINVAL. -DaveM |
| 423 | */ |
| 424 | return -EINVAL; |
| 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static inline int sadb_key_len(const struct sadb_key *key) |
| 431 | { |
| 432 | int key_bytes = DIV_ROUND_UP(key->sadb_key_bits, 8); |
| 433 | |
| 434 | return DIV_ROUND_UP(sizeof(struct sadb_key) + key_bytes, |
| 435 | sizeof(uint64_t)); |
| 436 | } |
| 437 | |
| 438 | static int verify_key_len(const void *p) |
| 439 | { |
| 440 | const struct sadb_key *key = p; |
| 441 | |
| 442 | if (sadb_key_len(key) > key->sadb_key_len) |
| 443 | return -EINVAL; |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx) |
| 449 | { |
| 450 | return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) + |
| 451 | sec_ctx->sadb_x_ctx_len, |
| 452 | sizeof(uint64_t)); |
| 453 | } |
| 454 | |
| 455 | static inline int verify_sec_ctx_len(const void *p) |
| 456 | { |
| 457 | const struct sadb_x_sec_ctx *sec_ctx = p; |
| 458 | int len = sec_ctx->sadb_x_ctx_len; |
| 459 | |
| 460 | if (len > PAGE_SIZE) |
| 461 | return -EINVAL; |
| 462 | |
| 463 | len = pfkey_sec_ctx_len(sec_ctx); |
| 464 | |
| 465 | if (sec_ctx->sadb_x_sec_len != len) |
| 466 | return -EINVAL; |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx, |
| 472 | gfp_t gfp) |
| 473 | { |
| 474 | struct xfrm_user_sec_ctx *uctx = NULL; |
| 475 | int ctx_size = sec_ctx->sadb_x_ctx_len; |
| 476 | |
| 477 | uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp); |
| 478 | |
| 479 | if (!uctx) |
| 480 | return NULL; |
| 481 | |
| 482 | uctx->len = pfkey_sec_ctx_len(sec_ctx); |
| 483 | uctx->exttype = sec_ctx->sadb_x_sec_exttype; |
| 484 | uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi; |
| 485 | uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg; |
| 486 | uctx->ctx_len = sec_ctx->sadb_x_ctx_len; |
| 487 | memcpy(uctx + 1, sec_ctx + 1, |
| 488 | uctx->ctx_len); |
| 489 | |
| 490 | return uctx; |
| 491 | } |
| 492 | |
| 493 | static int present_and_same_family(const struct sadb_address *src, |
| 494 | const struct sadb_address *dst) |
| 495 | { |
| 496 | const struct sockaddr *s_addr, *d_addr; |
| 497 | |
| 498 | if (!src || !dst) |
| 499 | return 0; |
| 500 | |
| 501 | s_addr = (const struct sockaddr *)(src + 1); |
| 502 | d_addr = (const struct sockaddr *)(dst + 1); |
| 503 | if (s_addr->sa_family != d_addr->sa_family) |
| 504 | return 0; |
| 505 | if (s_addr->sa_family != AF_INET |
| 506 | #if IS_ENABLED(CONFIG_IPV6) |
| 507 | && s_addr->sa_family != AF_INET6 |
| 508 | #endif |
| 509 | ) |
| 510 | return 0; |
| 511 | |
| 512 | return 1; |
| 513 | } |
| 514 | |
| 515 | static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs) |
| 516 | { |
| 517 | const char *p = (char *) hdr; |
| 518 | int len = skb->len; |
| 519 | |
| 520 | len -= sizeof(*hdr); |
| 521 | p += sizeof(*hdr); |
| 522 | while (len > 0) { |
| 523 | const struct sadb_ext *ehdr = (const struct sadb_ext *) p; |
| 524 | uint16_t ext_type; |
| 525 | int ext_len; |
| 526 | |
| 527 | if (len < sizeof(*ehdr)) |
| 528 | return -EINVAL; |
| 529 | |
| 530 | ext_len = ehdr->sadb_ext_len; |
| 531 | ext_len *= sizeof(uint64_t); |
| 532 | ext_type = ehdr->sadb_ext_type; |
| 533 | if (ext_len < sizeof(uint64_t) || |
| 534 | ext_len > len || |
| 535 | ext_type == SADB_EXT_RESERVED) |
| 536 | return -EINVAL; |
| 537 | |
| 538 | if (ext_type <= SADB_EXT_MAX) { |
| 539 | int min = (int) sadb_ext_min_len[ext_type]; |
| 540 | if (ext_len < min) |
| 541 | return -EINVAL; |
| 542 | if (ext_hdrs[ext_type-1] != NULL) |
| 543 | return -EINVAL; |
| 544 | switch (ext_type) { |
| 545 | case SADB_EXT_ADDRESS_SRC: |
| 546 | case SADB_EXT_ADDRESS_DST: |
| 547 | case SADB_EXT_ADDRESS_PROXY: |
| 548 | case SADB_X_EXT_NAT_T_OA: |
| 549 | if (verify_address_len(p)) |
| 550 | return -EINVAL; |
| 551 | break; |
| 552 | case SADB_X_EXT_SEC_CTX: |
| 553 | if (verify_sec_ctx_len(p)) |
| 554 | return -EINVAL; |
| 555 | break; |
| 556 | case SADB_EXT_KEY_AUTH: |
| 557 | case SADB_EXT_KEY_ENCRYPT: |
| 558 | if (verify_key_len(p)) |
| 559 | return -EINVAL; |
| 560 | break; |
| 561 | default: |
| 562 | break; |
| 563 | } |
| 564 | ext_hdrs[ext_type-1] = (void *) p; |
| 565 | } |
| 566 | p += ext_len; |
| 567 | len -= ext_len; |
| 568 | } |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static uint16_t |
| 574 | pfkey_satype2proto(uint8_t satype) |
| 575 | { |
| 576 | switch (satype) { |
| 577 | case SADB_SATYPE_UNSPEC: |
| 578 | return IPSEC_PROTO_ANY; |
| 579 | case SADB_SATYPE_AH: |
| 580 | return IPPROTO_AH; |
| 581 | case SADB_SATYPE_ESP: |
| 582 | return IPPROTO_ESP; |
| 583 | case SADB_X_SATYPE_IPCOMP: |
| 584 | return IPPROTO_COMP; |
| 585 | default: |
| 586 | return 0; |
| 587 | } |
| 588 | /* NOTREACHED */ |
| 589 | } |
| 590 | |
| 591 | static uint8_t |
| 592 | pfkey_proto2satype(uint16_t proto) |
| 593 | { |
| 594 | switch (proto) { |
| 595 | case IPPROTO_AH: |
| 596 | return SADB_SATYPE_AH; |
| 597 | case IPPROTO_ESP: |
| 598 | return SADB_SATYPE_ESP; |
| 599 | case IPPROTO_COMP: |
| 600 | return SADB_X_SATYPE_IPCOMP; |
| 601 | default: |
| 602 | return 0; |
| 603 | } |
| 604 | /* NOTREACHED */ |
| 605 | } |
| 606 | |
| 607 | /* BTW, this scheme means that there is no way with PFKEY2 sockets to |
| 608 | * say specifically 'just raw sockets' as we encode them as 255. |
| 609 | */ |
| 610 | |
| 611 | static uint8_t pfkey_proto_to_xfrm(uint8_t proto) |
| 612 | { |
| 613 | return proto == IPSEC_PROTO_ANY ? 0 : proto; |
| 614 | } |
| 615 | |
| 616 | static uint8_t pfkey_proto_from_xfrm(uint8_t proto) |
| 617 | { |
| 618 | return proto ? proto : IPSEC_PROTO_ANY; |
| 619 | } |
| 620 | |
| 621 | static inline int pfkey_sockaddr_len(sa_family_t family) |
| 622 | { |
| 623 | switch (family) { |
| 624 | case AF_INET: |
| 625 | return sizeof(struct sockaddr_in); |
| 626 | #if IS_ENABLED(CONFIG_IPV6) |
| 627 | case AF_INET6: |
| 628 | return sizeof(struct sockaddr_in6); |
| 629 | #endif |
| 630 | } |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static |
| 635 | int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr) |
| 636 | { |
| 637 | switch (sa->sa_family) { |
| 638 | case AF_INET: |
| 639 | xaddr->a4 = |
| 640 | ((struct sockaddr_in *)sa)->sin_addr.s_addr; |
| 641 | return AF_INET; |
| 642 | #if IS_ENABLED(CONFIG_IPV6) |
| 643 | case AF_INET6: |
| 644 | memcpy(xaddr->a6, |
| 645 | &((struct sockaddr_in6 *)sa)->sin6_addr, |
| 646 | sizeof(struct in6_addr)); |
| 647 | return AF_INET6; |
| 648 | #endif |
| 649 | } |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static |
| 654 | int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr) |
| 655 | { |
| 656 | return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1), |
| 657 | xaddr); |
| 658 | } |
| 659 | |
| 660 | static struct xfrm_state *pfkey_xfrm_state_lookup(struct net *net, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 661 | { |
| 662 | const struct sadb_sa *sa; |
| 663 | const struct sadb_address *addr; |
| 664 | uint16_t proto; |
| 665 | unsigned short family; |
| 666 | xfrm_address_t *xaddr; |
| 667 | |
| 668 | sa = ext_hdrs[SADB_EXT_SA - 1]; |
| 669 | if (sa == NULL) |
| 670 | return NULL; |
| 671 | |
| 672 | proto = pfkey_satype2proto(hdr->sadb_msg_satype); |
| 673 | if (proto == 0) |
| 674 | return NULL; |
| 675 | |
| 676 | /* sadb_address_len should be checked by caller */ |
| 677 | addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1]; |
| 678 | if (addr == NULL) |
| 679 | return NULL; |
| 680 | |
| 681 | family = ((const struct sockaddr *)(addr + 1))->sa_family; |
| 682 | switch (family) { |
| 683 | case AF_INET: |
| 684 | xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr; |
| 685 | break; |
| 686 | #if IS_ENABLED(CONFIG_IPV6) |
| 687 | case AF_INET6: |
| 688 | xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr; |
| 689 | break; |
| 690 | #endif |
| 691 | default: |
| 692 | xaddr = NULL; |
| 693 | } |
| 694 | |
| 695 | if (!xaddr) |
| 696 | return NULL; |
| 697 | |
| 698 | return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family); |
| 699 | } |
| 700 | |
| 701 | #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1))) |
| 702 | |
| 703 | static int |
| 704 | pfkey_sockaddr_size(sa_family_t family) |
| 705 | { |
| 706 | return PFKEY_ALIGN8(pfkey_sockaddr_len(family)); |
| 707 | } |
| 708 | |
| 709 | static inline int pfkey_mode_from_xfrm(int mode) |
| 710 | { |
| 711 | switch(mode) { |
| 712 | case XFRM_MODE_TRANSPORT: |
| 713 | return IPSEC_MODE_TRANSPORT; |
| 714 | case XFRM_MODE_TUNNEL: |
| 715 | return IPSEC_MODE_TUNNEL; |
| 716 | case XFRM_MODE_BEET: |
| 717 | return IPSEC_MODE_BEET; |
| 718 | default: |
| 719 | return -1; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | static inline int pfkey_mode_to_xfrm(int mode) |
| 724 | { |
| 725 | switch(mode) { |
| 726 | case IPSEC_MODE_ANY: /*XXX*/ |
| 727 | case IPSEC_MODE_TRANSPORT: |
| 728 | return XFRM_MODE_TRANSPORT; |
| 729 | case IPSEC_MODE_TUNNEL: |
| 730 | return XFRM_MODE_TUNNEL; |
| 731 | case IPSEC_MODE_BEET: |
| 732 | return XFRM_MODE_BEET; |
| 733 | default: |
| 734 | return -1; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port, |
| 739 | struct sockaddr *sa, |
| 740 | unsigned short family) |
| 741 | { |
| 742 | switch (family) { |
| 743 | case AF_INET: |
| 744 | { |
| 745 | struct sockaddr_in *sin = (struct sockaddr_in *)sa; |
| 746 | sin->sin_family = AF_INET; |
| 747 | sin->sin_port = port; |
| 748 | sin->sin_addr.s_addr = xaddr->a4; |
| 749 | memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 750 | return 32; |
| 751 | } |
| 752 | #if IS_ENABLED(CONFIG_IPV6) |
| 753 | case AF_INET6: |
| 754 | { |
| 755 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; |
| 756 | sin6->sin6_family = AF_INET6; |
| 757 | sin6->sin6_port = port; |
| 758 | sin6->sin6_flowinfo = 0; |
| 759 | sin6->sin6_addr = xaddr->in6; |
| 760 | sin6->sin6_scope_id = 0; |
| 761 | return 128; |
| 762 | } |
| 763 | #endif |
| 764 | } |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x, |
| 769 | int add_keys, int hsc) |
| 770 | { |
| 771 | struct sk_buff *skb; |
| 772 | struct sadb_msg *hdr; |
| 773 | struct sadb_sa *sa; |
| 774 | struct sadb_lifetime *lifetime; |
| 775 | struct sadb_address *addr; |
| 776 | struct sadb_key *key; |
| 777 | struct sadb_x_sa2 *sa2; |
| 778 | struct sadb_x_sec_ctx *sec_ctx; |
| 779 | struct xfrm_sec_ctx *xfrm_ctx; |
| 780 | int ctx_size = 0; |
| 781 | int size; |
| 782 | int auth_key_size = 0; |
| 783 | int encrypt_key_size = 0; |
| 784 | int sockaddr_size; |
| 785 | struct xfrm_encap_tmpl *natt = NULL; |
| 786 | int mode; |
| 787 | |
| 788 | /* address family check */ |
| 789 | sockaddr_size = pfkey_sockaddr_size(x->props.family); |
| 790 | if (!sockaddr_size) |
| 791 | return ERR_PTR(-EINVAL); |
| 792 | |
| 793 | /* base, SA, (lifetime (HSC),) address(SD), (address(P),) |
| 794 | key(AE), (identity(SD),) (sensitivity)> */ |
| 795 | size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) + |
| 796 | sizeof(struct sadb_lifetime) + |
| 797 | ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) + |
| 798 | ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) + |
| 799 | sizeof(struct sadb_address)*2 + |
| 800 | sockaddr_size*2 + |
| 801 | sizeof(struct sadb_x_sa2); |
| 802 | |
| 803 | if ((xfrm_ctx = x->security)) { |
| 804 | ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len); |
| 805 | size += sizeof(struct sadb_x_sec_ctx) + ctx_size; |
| 806 | } |
| 807 | |
| 808 | /* identity & sensitivity */ |
| 809 | if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family)) |
| 810 | size += sizeof(struct sadb_address) + sockaddr_size; |
| 811 | |
| 812 | if (add_keys) { |
| 813 | if (x->aalg && x->aalg->alg_key_len) { |
| 814 | auth_key_size = |
| 815 | PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8); |
| 816 | size += sizeof(struct sadb_key) + auth_key_size; |
| 817 | } |
| 818 | if (x->ealg && x->ealg->alg_key_len) { |
| 819 | encrypt_key_size = |
| 820 | PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8); |
| 821 | size += sizeof(struct sadb_key) + encrypt_key_size; |
| 822 | } |
| 823 | } |
| 824 | if (x->encap) |
| 825 | natt = x->encap; |
| 826 | |
| 827 | if (natt && natt->encap_type) { |
| 828 | size += sizeof(struct sadb_x_nat_t_type); |
| 829 | size += sizeof(struct sadb_x_nat_t_port); |
| 830 | size += sizeof(struct sadb_x_nat_t_port); |
| 831 | } |
| 832 | |
| 833 | skb = alloc_skb(size + 16, GFP_ATOMIC); |
| 834 | if (skb == NULL) |
| 835 | return ERR_PTR(-ENOBUFS); |
| 836 | |
| 837 | /* call should fill header later */ |
| 838 | hdr = skb_put(skb, sizeof(struct sadb_msg)); |
| 839 | memset(hdr, 0, size); /* XXX do we need this ? */ |
| 840 | hdr->sadb_msg_len = size / sizeof(uint64_t); |
| 841 | |
| 842 | /* sa */ |
| 843 | sa = skb_put(skb, sizeof(struct sadb_sa)); |
| 844 | sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t); |
| 845 | sa->sadb_sa_exttype = SADB_EXT_SA; |
| 846 | sa->sadb_sa_spi = x->id.spi; |
| 847 | sa->sadb_sa_replay = x->props.replay_window; |
| 848 | switch (x->km.state) { |
| 849 | case XFRM_STATE_VALID: |
| 850 | sa->sadb_sa_state = x->km.dying ? |
| 851 | SADB_SASTATE_DYING : SADB_SASTATE_MATURE; |
| 852 | break; |
| 853 | case XFRM_STATE_ACQ: |
| 854 | sa->sadb_sa_state = SADB_SASTATE_LARVAL; |
| 855 | break; |
| 856 | default: |
| 857 | sa->sadb_sa_state = SADB_SASTATE_DEAD; |
| 858 | break; |
| 859 | } |
| 860 | sa->sadb_sa_auth = 0; |
| 861 | if (x->aalg) { |
| 862 | struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0); |
| 863 | sa->sadb_sa_auth = (a && a->pfkey_supported) ? |
| 864 | a->desc.sadb_alg_id : 0; |
| 865 | } |
| 866 | sa->sadb_sa_encrypt = 0; |
| 867 | BUG_ON(x->ealg && x->calg); |
| 868 | if (x->ealg) { |
| 869 | struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0); |
| 870 | sa->sadb_sa_encrypt = (a && a->pfkey_supported) ? |
| 871 | a->desc.sadb_alg_id : 0; |
| 872 | } |
| 873 | /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */ |
| 874 | if (x->calg) { |
| 875 | struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0); |
| 876 | sa->sadb_sa_encrypt = (a && a->pfkey_supported) ? |
| 877 | a->desc.sadb_alg_id : 0; |
| 878 | } |
| 879 | |
| 880 | sa->sadb_sa_flags = 0; |
| 881 | if (x->props.flags & XFRM_STATE_NOECN) |
| 882 | sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN; |
| 883 | if (x->props.flags & XFRM_STATE_DECAP_DSCP) |
| 884 | sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP; |
| 885 | if (x->props.flags & XFRM_STATE_NOPMTUDISC) |
| 886 | sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC; |
| 887 | |
| 888 | /* hard time */ |
| 889 | if (hsc & 2) { |
| 890 | lifetime = skb_put(skb, sizeof(struct sadb_lifetime)); |
| 891 | lifetime->sadb_lifetime_len = |
| 892 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); |
| 893 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; |
| 894 | lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit); |
| 895 | lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit); |
| 896 | lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds; |
| 897 | lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds; |
| 898 | } |
| 899 | /* soft time */ |
| 900 | if (hsc & 1) { |
| 901 | lifetime = skb_put(skb, sizeof(struct sadb_lifetime)); |
| 902 | lifetime->sadb_lifetime_len = |
| 903 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); |
| 904 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT; |
| 905 | lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit); |
| 906 | lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit); |
| 907 | lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds; |
| 908 | lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds; |
| 909 | } |
| 910 | /* current time */ |
| 911 | lifetime = skb_put(skb, sizeof(struct sadb_lifetime)); |
| 912 | lifetime->sadb_lifetime_len = |
| 913 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); |
| 914 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; |
| 915 | lifetime->sadb_lifetime_allocations = x->curlft.packets; |
| 916 | lifetime->sadb_lifetime_bytes = x->curlft.bytes; |
| 917 | lifetime->sadb_lifetime_addtime = x->curlft.add_time; |
| 918 | lifetime->sadb_lifetime_usetime = x->curlft.use_time; |
| 919 | /* src address */ |
| 920 | addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size); |
| 921 | addr->sadb_address_len = |
| 922 | (sizeof(struct sadb_address)+sockaddr_size)/ |
| 923 | sizeof(uint64_t); |
| 924 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; |
| 925 | /* "if the ports are non-zero, then the sadb_address_proto field, |
| 926 | normally zero, MUST be filled in with the transport |
| 927 | protocol's number." - RFC2367 */ |
| 928 | addr->sadb_address_proto = 0; |
| 929 | addr->sadb_address_reserved = 0; |
| 930 | |
| 931 | addr->sadb_address_prefixlen = |
| 932 | pfkey_sockaddr_fill(&x->props.saddr, 0, |
| 933 | (struct sockaddr *) (addr + 1), |
| 934 | x->props.family); |
| 935 | if (!addr->sadb_address_prefixlen) |
| 936 | BUG(); |
| 937 | |
| 938 | /* dst address */ |
| 939 | addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size); |
| 940 | addr->sadb_address_len = |
| 941 | (sizeof(struct sadb_address)+sockaddr_size)/ |
| 942 | sizeof(uint64_t); |
| 943 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; |
| 944 | addr->sadb_address_proto = 0; |
| 945 | addr->sadb_address_reserved = 0; |
| 946 | |
| 947 | addr->sadb_address_prefixlen = |
| 948 | pfkey_sockaddr_fill(&x->id.daddr, 0, |
| 949 | (struct sockaddr *) (addr + 1), |
| 950 | x->props.family); |
| 951 | if (!addr->sadb_address_prefixlen) |
| 952 | BUG(); |
| 953 | |
| 954 | if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, |
| 955 | x->props.family)) { |
| 956 | addr = skb_put(skb, |
| 957 | sizeof(struct sadb_address) + sockaddr_size); |
| 958 | addr->sadb_address_len = |
| 959 | (sizeof(struct sadb_address)+sockaddr_size)/ |
| 960 | sizeof(uint64_t); |
| 961 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY; |
| 962 | addr->sadb_address_proto = |
| 963 | pfkey_proto_from_xfrm(x->sel.proto); |
| 964 | addr->sadb_address_prefixlen = x->sel.prefixlen_s; |
| 965 | addr->sadb_address_reserved = 0; |
| 966 | |
| 967 | pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport, |
| 968 | (struct sockaddr *) (addr + 1), |
| 969 | x->props.family); |
| 970 | } |
| 971 | |
| 972 | /* auth key */ |
| 973 | if (add_keys && auth_key_size) { |
| 974 | key = skb_put(skb, sizeof(struct sadb_key) + auth_key_size); |
| 975 | key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) / |
| 976 | sizeof(uint64_t); |
| 977 | key->sadb_key_exttype = SADB_EXT_KEY_AUTH; |
| 978 | key->sadb_key_bits = x->aalg->alg_key_len; |
| 979 | key->sadb_key_reserved = 0; |
| 980 | memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8); |
| 981 | } |
| 982 | /* encrypt key */ |
| 983 | if (add_keys && encrypt_key_size) { |
| 984 | key = skb_put(skb, sizeof(struct sadb_key) + encrypt_key_size); |
| 985 | key->sadb_key_len = (sizeof(struct sadb_key) + |
| 986 | encrypt_key_size) / sizeof(uint64_t); |
| 987 | key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT; |
| 988 | key->sadb_key_bits = x->ealg->alg_key_len; |
| 989 | key->sadb_key_reserved = 0; |
| 990 | memcpy(key + 1, x->ealg->alg_key, |
| 991 | (x->ealg->alg_key_len+7)/8); |
| 992 | } |
| 993 | |
| 994 | /* sa */ |
| 995 | sa2 = skb_put(skb, sizeof(struct sadb_x_sa2)); |
| 996 | sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t); |
| 997 | sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2; |
| 998 | if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) { |
| 999 | kfree_skb(skb); |
| 1000 | return ERR_PTR(-EINVAL); |
| 1001 | } |
| 1002 | sa2->sadb_x_sa2_mode = mode; |
| 1003 | sa2->sadb_x_sa2_reserved1 = 0; |
| 1004 | sa2->sadb_x_sa2_reserved2 = 0; |
| 1005 | sa2->sadb_x_sa2_sequence = 0; |
| 1006 | sa2->sadb_x_sa2_reqid = x->props.reqid; |
| 1007 | |
| 1008 | if (natt && natt->encap_type) { |
| 1009 | struct sadb_x_nat_t_type *n_type; |
| 1010 | struct sadb_x_nat_t_port *n_port; |
| 1011 | |
| 1012 | /* type */ |
| 1013 | n_type = skb_put(skb, sizeof(*n_type)); |
| 1014 | n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t); |
| 1015 | n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE; |
| 1016 | n_type->sadb_x_nat_t_type_type = natt->encap_type; |
| 1017 | n_type->sadb_x_nat_t_type_reserved[0] = 0; |
| 1018 | n_type->sadb_x_nat_t_type_reserved[1] = 0; |
| 1019 | n_type->sadb_x_nat_t_type_reserved[2] = 0; |
| 1020 | |
| 1021 | /* source port */ |
| 1022 | n_port = skb_put(skb, sizeof(*n_port)); |
| 1023 | n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); |
| 1024 | n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT; |
| 1025 | n_port->sadb_x_nat_t_port_port = natt->encap_sport; |
| 1026 | n_port->sadb_x_nat_t_port_reserved = 0; |
| 1027 | |
| 1028 | /* dest port */ |
| 1029 | n_port = skb_put(skb, sizeof(*n_port)); |
| 1030 | n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); |
| 1031 | n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT; |
| 1032 | n_port->sadb_x_nat_t_port_port = natt->encap_dport; |
| 1033 | n_port->sadb_x_nat_t_port_reserved = 0; |
| 1034 | } |
| 1035 | |
| 1036 | /* security context */ |
| 1037 | if (xfrm_ctx) { |
| 1038 | sec_ctx = skb_put(skb, |
| 1039 | sizeof(struct sadb_x_sec_ctx) + ctx_size); |
| 1040 | sec_ctx->sadb_x_sec_len = |
| 1041 | (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t); |
| 1042 | sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX; |
| 1043 | sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi; |
| 1044 | sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg; |
| 1045 | sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len; |
| 1046 | memcpy(sec_ctx + 1, xfrm_ctx->ctx_str, |
| 1047 | xfrm_ctx->ctx_len); |
| 1048 | } |
| 1049 | |
| 1050 | return skb; |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x) |
| 1055 | { |
| 1056 | struct sk_buff *skb; |
| 1057 | |
| 1058 | skb = __pfkey_xfrm_state2msg(x, 1, 3); |
| 1059 | |
| 1060 | return skb; |
| 1061 | } |
| 1062 | |
| 1063 | static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x, |
| 1064 | int hsc) |
| 1065 | { |
| 1066 | return __pfkey_xfrm_state2msg(x, 0, hsc); |
| 1067 | } |
| 1068 | |
| 1069 | static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net, |
| 1070 | const struct sadb_msg *hdr, |
| 1071 | void * const *ext_hdrs) |
| 1072 | { |
| 1073 | struct xfrm_state *x; |
| 1074 | const struct sadb_lifetime *lifetime; |
| 1075 | const struct sadb_sa *sa; |
| 1076 | const struct sadb_key *key; |
| 1077 | const struct sadb_x_sec_ctx *sec_ctx; |
| 1078 | uint16_t proto; |
| 1079 | int err; |
| 1080 | |
| 1081 | |
| 1082 | sa = ext_hdrs[SADB_EXT_SA - 1]; |
| 1083 | if (!sa || |
| 1084 | !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], |
| 1085 | ext_hdrs[SADB_EXT_ADDRESS_DST-1])) |
| 1086 | return ERR_PTR(-EINVAL); |
| 1087 | if (hdr->sadb_msg_satype == SADB_SATYPE_ESP && |
| 1088 | !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1]) |
| 1089 | return ERR_PTR(-EINVAL); |
| 1090 | if (hdr->sadb_msg_satype == SADB_SATYPE_AH && |
| 1091 | !ext_hdrs[SADB_EXT_KEY_AUTH-1]) |
| 1092 | return ERR_PTR(-EINVAL); |
| 1093 | if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] != |
| 1094 | !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) |
| 1095 | return ERR_PTR(-EINVAL); |
| 1096 | |
| 1097 | proto = pfkey_satype2proto(hdr->sadb_msg_satype); |
| 1098 | if (proto == 0) |
| 1099 | return ERR_PTR(-EINVAL); |
| 1100 | |
| 1101 | /* default error is no buffer space */ |
| 1102 | err = -ENOBUFS; |
| 1103 | |
| 1104 | /* RFC2367: |
| 1105 | |
| 1106 | Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message. |
| 1107 | SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not |
| 1108 | sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state. |
| 1109 | Therefore, the sadb_sa_state field of all submitted SAs MUST be |
| 1110 | SADB_SASTATE_MATURE and the kernel MUST return an error if this is |
| 1111 | not true. |
| 1112 | |
| 1113 | However, KAME setkey always uses SADB_SASTATE_LARVAL. |
| 1114 | Hence, we have to _ignore_ sadb_sa_state, which is also reasonable. |
| 1115 | */ |
| 1116 | if (sa->sadb_sa_auth > SADB_AALG_MAX || |
| 1117 | (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP && |
| 1118 | sa->sadb_sa_encrypt > SADB_X_CALG_MAX) || |
| 1119 | sa->sadb_sa_encrypt > SADB_EALG_MAX) |
| 1120 | return ERR_PTR(-EINVAL); |
| 1121 | key = ext_hdrs[SADB_EXT_KEY_AUTH - 1]; |
| 1122 | if (key != NULL && |
| 1123 | sa->sadb_sa_auth != SADB_X_AALG_NULL && |
| 1124 | key->sadb_key_bits == 0) |
| 1125 | return ERR_PTR(-EINVAL); |
| 1126 | key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1]; |
| 1127 | if (key != NULL && |
| 1128 | sa->sadb_sa_encrypt != SADB_EALG_NULL && |
| 1129 | key->sadb_key_bits == 0) |
| 1130 | return ERR_PTR(-EINVAL); |
| 1131 | |
| 1132 | x = xfrm_state_alloc(net); |
| 1133 | if (x == NULL) |
| 1134 | return ERR_PTR(-ENOBUFS); |
| 1135 | |
| 1136 | x->id.proto = proto; |
| 1137 | x->id.spi = sa->sadb_sa_spi; |
| 1138 | x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay, |
| 1139 | (sizeof(x->replay.bitmap) * 8)); |
| 1140 | if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN) |
| 1141 | x->props.flags |= XFRM_STATE_NOECN; |
| 1142 | if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP) |
| 1143 | x->props.flags |= XFRM_STATE_DECAP_DSCP; |
| 1144 | if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC) |
| 1145 | x->props.flags |= XFRM_STATE_NOPMTUDISC; |
| 1146 | |
| 1147 | lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1]; |
| 1148 | if (lifetime != NULL) { |
| 1149 | x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); |
| 1150 | x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); |
| 1151 | x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime; |
| 1152 | x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime; |
| 1153 | } |
| 1154 | lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1]; |
| 1155 | if (lifetime != NULL) { |
| 1156 | x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); |
| 1157 | x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); |
| 1158 | x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime; |
| 1159 | x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime; |
| 1160 | } |
| 1161 | |
| 1162 | sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1]; |
| 1163 | if (sec_ctx != NULL) { |
| 1164 | struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL); |
| 1165 | |
| 1166 | if (!uctx) |
| 1167 | goto out; |
| 1168 | |
| 1169 | err = security_xfrm_state_alloc(x, uctx); |
| 1170 | kfree(uctx); |
| 1171 | |
| 1172 | if (err) |
| 1173 | goto out; |
| 1174 | } |
| 1175 | |
| 1176 | err = -ENOBUFS; |
| 1177 | key = ext_hdrs[SADB_EXT_KEY_AUTH - 1]; |
| 1178 | if (sa->sadb_sa_auth) { |
| 1179 | int keysize = 0; |
| 1180 | struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth); |
| 1181 | if (!a || !a->pfkey_supported) { |
| 1182 | err = -ENOSYS; |
| 1183 | goto out; |
| 1184 | } |
| 1185 | if (key) |
| 1186 | keysize = (key->sadb_key_bits + 7) / 8; |
| 1187 | x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL); |
| 1188 | if (!x->aalg) { |
| 1189 | err = -ENOMEM; |
| 1190 | goto out; |
| 1191 | } |
| 1192 | strcpy(x->aalg->alg_name, a->name); |
| 1193 | x->aalg->alg_key_len = 0; |
| 1194 | if (key) { |
| 1195 | x->aalg->alg_key_len = key->sadb_key_bits; |
| 1196 | memcpy(x->aalg->alg_key, key+1, keysize); |
| 1197 | } |
| 1198 | x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits; |
| 1199 | x->props.aalgo = sa->sadb_sa_auth; |
| 1200 | /* x->algo.flags = sa->sadb_sa_flags; */ |
| 1201 | } |
| 1202 | if (sa->sadb_sa_encrypt) { |
| 1203 | if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) { |
| 1204 | struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt); |
| 1205 | if (!a || !a->pfkey_supported) { |
| 1206 | err = -ENOSYS; |
| 1207 | goto out; |
| 1208 | } |
| 1209 | x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL); |
| 1210 | if (!x->calg) { |
| 1211 | err = -ENOMEM; |
| 1212 | goto out; |
| 1213 | } |
| 1214 | strcpy(x->calg->alg_name, a->name); |
| 1215 | x->props.calgo = sa->sadb_sa_encrypt; |
| 1216 | } else { |
| 1217 | int keysize = 0; |
| 1218 | struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt); |
| 1219 | if (!a || !a->pfkey_supported) { |
| 1220 | err = -ENOSYS; |
| 1221 | goto out; |
| 1222 | } |
| 1223 | key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1]; |
| 1224 | if (key) |
| 1225 | keysize = (key->sadb_key_bits + 7) / 8; |
| 1226 | x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL); |
| 1227 | if (!x->ealg) { |
| 1228 | err = -ENOMEM; |
| 1229 | goto out; |
| 1230 | } |
| 1231 | strcpy(x->ealg->alg_name, a->name); |
| 1232 | x->ealg->alg_key_len = 0; |
| 1233 | if (key) { |
| 1234 | x->ealg->alg_key_len = key->sadb_key_bits; |
| 1235 | memcpy(x->ealg->alg_key, key+1, keysize); |
| 1236 | } |
| 1237 | x->props.ealgo = sa->sadb_sa_encrypt; |
| 1238 | x->geniv = a->uinfo.encr.geniv; |
| 1239 | } |
| 1240 | } |
| 1241 | /* x->algo.flags = sa->sadb_sa_flags; */ |
| 1242 | |
| 1243 | x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1], |
| 1244 | &x->props.saddr); |
| 1245 | pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1], |
| 1246 | &x->id.daddr); |
| 1247 | |
| 1248 | if (ext_hdrs[SADB_X_EXT_SA2-1]) { |
| 1249 | const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1]; |
| 1250 | int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode); |
| 1251 | if (mode < 0) { |
| 1252 | err = -EINVAL; |
| 1253 | goto out; |
| 1254 | } |
| 1255 | x->props.mode = mode; |
| 1256 | x->props.reqid = sa2->sadb_x_sa2_reqid; |
| 1257 | } |
| 1258 | |
| 1259 | if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) { |
| 1260 | const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]; |
| 1261 | |
| 1262 | /* Nobody uses this, but we try. */ |
| 1263 | x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr); |
| 1264 | x->sel.prefixlen_s = addr->sadb_address_prefixlen; |
| 1265 | } |
| 1266 | |
| 1267 | if (!x->sel.family) |
| 1268 | x->sel.family = x->props.family; |
| 1269 | |
| 1270 | if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) { |
| 1271 | const struct sadb_x_nat_t_type* n_type; |
| 1272 | struct xfrm_encap_tmpl *natt; |
| 1273 | |
| 1274 | x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL); |
| 1275 | if (!x->encap) { |
| 1276 | err = -ENOMEM; |
| 1277 | goto out; |
| 1278 | } |
| 1279 | |
| 1280 | natt = x->encap; |
| 1281 | n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]; |
| 1282 | natt->encap_type = n_type->sadb_x_nat_t_type_type; |
| 1283 | |
| 1284 | if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) { |
| 1285 | const struct sadb_x_nat_t_port *n_port = |
| 1286 | ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]; |
| 1287 | natt->encap_sport = n_port->sadb_x_nat_t_port_port; |
| 1288 | } |
| 1289 | if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) { |
| 1290 | const struct sadb_x_nat_t_port *n_port = |
| 1291 | ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]; |
| 1292 | natt->encap_dport = n_port->sadb_x_nat_t_port_port; |
| 1293 | } |
| 1294 | memset(&natt->encap_oa, 0, sizeof(natt->encap_oa)); |
| 1295 | } |
| 1296 | |
| 1297 | err = xfrm_init_state(x); |
| 1298 | if (err) |
| 1299 | goto out; |
| 1300 | |
| 1301 | x->km.seq = hdr->sadb_msg_seq; |
| 1302 | return x; |
| 1303 | |
| 1304 | out: |
| 1305 | x->km.state = XFRM_STATE_DEAD; |
| 1306 | xfrm_state_put(x); |
| 1307 | return ERR_PTR(err); |
| 1308 | } |
| 1309 | |
| 1310 | static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 1311 | { |
| 1312 | return -EOPNOTSUPP; |
| 1313 | } |
| 1314 | |
| 1315 | static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 1316 | { |
| 1317 | struct net *net = sock_net(sk); |
| 1318 | struct sk_buff *resp_skb; |
| 1319 | struct sadb_x_sa2 *sa2; |
| 1320 | struct sadb_address *saddr, *daddr; |
| 1321 | struct sadb_msg *out_hdr; |
| 1322 | struct sadb_spirange *range; |
| 1323 | struct xfrm_state *x = NULL; |
| 1324 | int mode; |
| 1325 | int err; |
| 1326 | u32 min_spi, max_spi; |
| 1327 | u32 reqid; |
| 1328 | u8 proto; |
| 1329 | unsigned short family; |
| 1330 | xfrm_address_t *xsaddr = NULL, *xdaddr = NULL; |
| 1331 | |
| 1332 | if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], |
| 1333 | ext_hdrs[SADB_EXT_ADDRESS_DST-1])) |
| 1334 | return -EINVAL; |
| 1335 | |
| 1336 | proto = pfkey_satype2proto(hdr->sadb_msg_satype); |
| 1337 | if (proto == 0) |
| 1338 | return -EINVAL; |
| 1339 | |
| 1340 | if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) { |
| 1341 | mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode); |
| 1342 | if (mode < 0) |
| 1343 | return -EINVAL; |
| 1344 | reqid = sa2->sadb_x_sa2_reqid; |
| 1345 | } else { |
| 1346 | mode = 0; |
| 1347 | reqid = 0; |
| 1348 | } |
| 1349 | |
| 1350 | saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1]; |
| 1351 | daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1]; |
| 1352 | |
| 1353 | family = ((struct sockaddr *)(saddr + 1))->sa_family; |
| 1354 | switch (family) { |
| 1355 | case AF_INET: |
| 1356 | xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr; |
| 1357 | xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr; |
| 1358 | break; |
| 1359 | #if IS_ENABLED(CONFIG_IPV6) |
| 1360 | case AF_INET6: |
| 1361 | xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr; |
| 1362 | xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr; |
| 1363 | break; |
| 1364 | #endif |
| 1365 | } |
| 1366 | |
| 1367 | if (hdr->sadb_msg_seq) { |
| 1368 | x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq); |
| 1369 | if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) { |
| 1370 | xfrm_state_put(x); |
| 1371 | x = NULL; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | if (!x) |
| 1376 | x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family); |
| 1377 | |
| 1378 | if (x == NULL) |
| 1379 | return -ENOENT; |
| 1380 | |
| 1381 | min_spi = 0x100; |
| 1382 | max_spi = 0x0fffffff; |
| 1383 | |
| 1384 | range = ext_hdrs[SADB_EXT_SPIRANGE-1]; |
| 1385 | if (range) { |
| 1386 | min_spi = range->sadb_spirange_min; |
| 1387 | max_spi = range->sadb_spirange_max; |
| 1388 | } |
| 1389 | |
| 1390 | err = verify_spi_info(x->id.proto, min_spi, max_spi); |
| 1391 | if (err) { |
| 1392 | xfrm_state_put(x); |
| 1393 | return err; |
| 1394 | } |
| 1395 | |
| 1396 | err = xfrm_alloc_spi(x, min_spi, max_spi); |
| 1397 | resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x); |
| 1398 | |
| 1399 | if (IS_ERR(resp_skb)) { |
| 1400 | xfrm_state_put(x); |
| 1401 | return PTR_ERR(resp_skb); |
| 1402 | } |
| 1403 | |
| 1404 | out_hdr = (struct sadb_msg *) resp_skb->data; |
| 1405 | out_hdr->sadb_msg_version = hdr->sadb_msg_version; |
| 1406 | out_hdr->sadb_msg_type = SADB_GETSPI; |
| 1407 | out_hdr->sadb_msg_satype = pfkey_proto2satype(proto); |
| 1408 | out_hdr->sadb_msg_errno = 0; |
| 1409 | out_hdr->sadb_msg_reserved = 0; |
| 1410 | out_hdr->sadb_msg_seq = hdr->sadb_msg_seq; |
| 1411 | out_hdr->sadb_msg_pid = hdr->sadb_msg_pid; |
| 1412 | |
| 1413 | xfrm_state_put(x); |
| 1414 | |
| 1415 | pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net); |
| 1416 | |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
| 1420 | static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 1421 | { |
| 1422 | struct net *net = sock_net(sk); |
| 1423 | struct xfrm_state *x; |
| 1424 | |
| 1425 | if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8) |
| 1426 | return -EOPNOTSUPP; |
| 1427 | |
| 1428 | if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0) |
| 1429 | return 0; |
| 1430 | |
| 1431 | x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq); |
| 1432 | if (x == NULL) |
| 1433 | return 0; |
| 1434 | |
| 1435 | spin_lock_bh(&x->lock); |
| 1436 | if (x->km.state == XFRM_STATE_ACQ) |
| 1437 | x->km.state = XFRM_STATE_ERROR; |
| 1438 | |
| 1439 | spin_unlock_bh(&x->lock); |
| 1440 | xfrm_state_put(x); |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | static inline int event2poltype(int event) |
| 1445 | { |
| 1446 | switch (event) { |
| 1447 | case XFRM_MSG_DELPOLICY: |
| 1448 | return SADB_X_SPDDELETE; |
| 1449 | case XFRM_MSG_NEWPOLICY: |
| 1450 | return SADB_X_SPDADD; |
| 1451 | case XFRM_MSG_UPDPOLICY: |
| 1452 | return SADB_X_SPDUPDATE; |
| 1453 | case XFRM_MSG_POLEXPIRE: |
| 1454 | // return SADB_X_SPDEXPIRE; |
| 1455 | default: |
| 1456 | pr_err("pfkey: Unknown policy event %d\n", event); |
| 1457 | break; |
| 1458 | } |
| 1459 | |
| 1460 | return 0; |
| 1461 | } |
| 1462 | |
| 1463 | static inline int event2keytype(int event) |
| 1464 | { |
| 1465 | switch (event) { |
| 1466 | case XFRM_MSG_DELSA: |
| 1467 | return SADB_DELETE; |
| 1468 | case XFRM_MSG_NEWSA: |
| 1469 | return SADB_ADD; |
| 1470 | case XFRM_MSG_UPDSA: |
| 1471 | return SADB_UPDATE; |
| 1472 | case XFRM_MSG_EXPIRE: |
| 1473 | return SADB_EXPIRE; |
| 1474 | default: |
| 1475 | pr_err("pfkey: Unknown SA event %d\n", event); |
| 1476 | break; |
| 1477 | } |
| 1478 | |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | /* ADD/UPD/DEL */ |
| 1483 | static int key_notify_sa(struct xfrm_state *x, const struct km_event *c) |
| 1484 | { |
| 1485 | struct sk_buff *skb; |
| 1486 | struct sadb_msg *hdr; |
| 1487 | |
| 1488 | skb = pfkey_xfrm_state2msg(x); |
| 1489 | |
| 1490 | if (IS_ERR(skb)) |
| 1491 | return PTR_ERR(skb); |
| 1492 | |
| 1493 | hdr = (struct sadb_msg *) skb->data; |
| 1494 | hdr->sadb_msg_version = PF_KEY_V2; |
| 1495 | hdr->sadb_msg_type = event2keytype(c->event); |
| 1496 | hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); |
| 1497 | hdr->sadb_msg_errno = 0; |
| 1498 | hdr->sadb_msg_reserved = 0; |
| 1499 | hdr->sadb_msg_seq = c->seq; |
| 1500 | hdr->sadb_msg_pid = c->portid; |
| 1501 | |
| 1502 | pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x)); |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | static int pfkey_add(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 1508 | { |
| 1509 | struct net *net = sock_net(sk); |
| 1510 | struct xfrm_state *x; |
| 1511 | int err; |
| 1512 | struct km_event c; |
| 1513 | |
| 1514 | x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs); |
| 1515 | if (IS_ERR(x)) |
| 1516 | return PTR_ERR(x); |
| 1517 | |
| 1518 | xfrm_state_hold(x); |
| 1519 | if (hdr->sadb_msg_type == SADB_ADD) |
| 1520 | err = xfrm_state_add(x); |
| 1521 | else |
| 1522 | err = xfrm_state_update(x); |
| 1523 | |
| 1524 | xfrm_audit_state_add(x, err ? 0 : 1, true); |
| 1525 | |
| 1526 | if (err < 0) { |
| 1527 | x->km.state = XFRM_STATE_DEAD; |
| 1528 | __xfrm_state_put(x); |
| 1529 | goto out; |
| 1530 | } |
| 1531 | |
| 1532 | if (hdr->sadb_msg_type == SADB_ADD) |
| 1533 | c.event = XFRM_MSG_NEWSA; |
| 1534 | else |
| 1535 | c.event = XFRM_MSG_UPDSA; |
| 1536 | c.seq = hdr->sadb_msg_seq; |
| 1537 | c.portid = hdr->sadb_msg_pid; |
| 1538 | km_state_notify(x, &c); |
| 1539 | out: |
| 1540 | xfrm_state_put(x); |
| 1541 | return err; |
| 1542 | } |
| 1543 | |
| 1544 | static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 1545 | { |
| 1546 | struct net *net = sock_net(sk); |
| 1547 | struct xfrm_state *x; |
| 1548 | struct km_event c; |
| 1549 | int err; |
| 1550 | |
| 1551 | if (!ext_hdrs[SADB_EXT_SA-1] || |
| 1552 | !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], |
| 1553 | ext_hdrs[SADB_EXT_ADDRESS_DST-1])) |
| 1554 | return -EINVAL; |
| 1555 | |
| 1556 | x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs); |
| 1557 | if (x == NULL) |
| 1558 | return -ESRCH; |
| 1559 | |
| 1560 | if ((err = security_xfrm_state_delete(x))) |
| 1561 | goto out; |
| 1562 | |
| 1563 | if (xfrm_state_kern(x)) { |
| 1564 | err = -EPERM; |
| 1565 | goto out; |
| 1566 | } |
| 1567 | |
| 1568 | err = xfrm_state_delete(x); |
| 1569 | |
| 1570 | if (err < 0) |
| 1571 | goto out; |
| 1572 | |
| 1573 | c.seq = hdr->sadb_msg_seq; |
| 1574 | c.portid = hdr->sadb_msg_pid; |
| 1575 | c.event = XFRM_MSG_DELSA; |
| 1576 | km_state_notify(x, &c); |
| 1577 | out: |
| 1578 | xfrm_audit_state_delete(x, err ? 0 : 1, true); |
| 1579 | xfrm_state_put(x); |
| 1580 | |
| 1581 | return err; |
| 1582 | } |
| 1583 | |
| 1584 | static int pfkey_get(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 1585 | { |
| 1586 | struct net *net = sock_net(sk); |
| 1587 | __u8 proto; |
| 1588 | struct sk_buff *out_skb; |
| 1589 | struct sadb_msg *out_hdr; |
| 1590 | struct xfrm_state *x; |
| 1591 | |
| 1592 | if (!ext_hdrs[SADB_EXT_SA-1] || |
| 1593 | !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], |
| 1594 | ext_hdrs[SADB_EXT_ADDRESS_DST-1])) |
| 1595 | return -EINVAL; |
| 1596 | |
| 1597 | x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs); |
| 1598 | if (x == NULL) |
| 1599 | return -ESRCH; |
| 1600 | |
| 1601 | out_skb = pfkey_xfrm_state2msg(x); |
| 1602 | proto = x->id.proto; |
| 1603 | xfrm_state_put(x); |
| 1604 | if (IS_ERR(out_skb)) |
| 1605 | return PTR_ERR(out_skb); |
| 1606 | |
| 1607 | out_hdr = (struct sadb_msg *) out_skb->data; |
| 1608 | out_hdr->sadb_msg_version = hdr->sadb_msg_version; |
| 1609 | out_hdr->sadb_msg_type = SADB_GET; |
| 1610 | out_hdr->sadb_msg_satype = pfkey_proto2satype(proto); |
| 1611 | out_hdr->sadb_msg_errno = 0; |
| 1612 | out_hdr->sadb_msg_reserved = 0; |
| 1613 | out_hdr->sadb_msg_seq = hdr->sadb_msg_seq; |
| 1614 | out_hdr->sadb_msg_pid = hdr->sadb_msg_pid; |
| 1615 | pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk)); |
| 1616 | |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
| 1620 | static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig, |
| 1621 | gfp_t allocation) |
| 1622 | { |
| 1623 | struct sk_buff *skb; |
| 1624 | struct sadb_msg *hdr; |
| 1625 | int len, auth_len, enc_len, i; |
| 1626 | |
| 1627 | auth_len = xfrm_count_pfkey_auth_supported(); |
| 1628 | if (auth_len) { |
| 1629 | auth_len *= sizeof(struct sadb_alg); |
| 1630 | auth_len += sizeof(struct sadb_supported); |
| 1631 | } |
| 1632 | |
| 1633 | enc_len = xfrm_count_pfkey_enc_supported(); |
| 1634 | if (enc_len) { |
| 1635 | enc_len *= sizeof(struct sadb_alg); |
| 1636 | enc_len += sizeof(struct sadb_supported); |
| 1637 | } |
| 1638 | |
| 1639 | len = enc_len + auth_len + sizeof(struct sadb_msg); |
| 1640 | |
| 1641 | skb = alloc_skb(len + 16, allocation); |
| 1642 | if (!skb) |
| 1643 | goto out_put_algs; |
| 1644 | |
| 1645 | hdr = skb_put(skb, sizeof(*hdr)); |
| 1646 | pfkey_hdr_dup(hdr, orig); |
| 1647 | hdr->sadb_msg_errno = 0; |
| 1648 | hdr->sadb_msg_len = len / sizeof(uint64_t); |
| 1649 | |
| 1650 | if (auth_len) { |
| 1651 | struct sadb_supported *sp; |
| 1652 | struct sadb_alg *ap; |
| 1653 | |
| 1654 | sp = skb_put(skb, auth_len); |
| 1655 | ap = (struct sadb_alg *) (sp + 1); |
| 1656 | |
| 1657 | sp->sadb_supported_len = auth_len / sizeof(uint64_t); |
| 1658 | sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH; |
| 1659 | |
| 1660 | for (i = 0; ; i++) { |
| 1661 | struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i); |
| 1662 | if (!aalg) |
| 1663 | break; |
| 1664 | if (!aalg->pfkey_supported) |
| 1665 | continue; |
| 1666 | if (aalg->available) |
| 1667 | *ap++ = aalg->desc; |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | if (enc_len) { |
| 1672 | struct sadb_supported *sp; |
| 1673 | struct sadb_alg *ap; |
| 1674 | |
| 1675 | sp = skb_put(skb, enc_len); |
| 1676 | ap = (struct sadb_alg *) (sp + 1); |
| 1677 | |
| 1678 | sp->sadb_supported_len = enc_len / sizeof(uint64_t); |
| 1679 | sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT; |
| 1680 | |
| 1681 | for (i = 0; ; i++) { |
| 1682 | struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i); |
| 1683 | if (!ealg) |
| 1684 | break; |
| 1685 | if (!ealg->pfkey_supported) |
| 1686 | continue; |
| 1687 | if (ealg->available) |
| 1688 | *ap++ = ealg->desc; |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | out_put_algs: |
| 1693 | return skb; |
| 1694 | } |
| 1695 | |
| 1696 | static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 1697 | { |
| 1698 | struct pfkey_sock *pfk = pfkey_sk(sk); |
| 1699 | struct sk_buff *supp_skb; |
| 1700 | |
| 1701 | if (hdr->sadb_msg_satype > SADB_SATYPE_MAX) |
| 1702 | return -EINVAL; |
| 1703 | |
| 1704 | if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) { |
| 1705 | if (pfk->registered&(1<<hdr->sadb_msg_satype)) |
| 1706 | return -EEXIST; |
| 1707 | pfk->registered |= (1<<hdr->sadb_msg_satype); |
| 1708 | } |
| 1709 | |
| 1710 | xfrm_probe_algs(); |
| 1711 | |
| 1712 | supp_skb = compose_sadb_supported(hdr, GFP_KERNEL); |
| 1713 | if (!supp_skb) { |
| 1714 | if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) |
| 1715 | pfk->registered &= ~(1<<hdr->sadb_msg_satype); |
| 1716 | |
| 1717 | return -ENOBUFS; |
| 1718 | } |
| 1719 | |
| 1720 | pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk, |
| 1721 | sock_net(sk)); |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
| 1725 | static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr) |
| 1726 | { |
| 1727 | struct sk_buff *skb; |
| 1728 | struct sadb_msg *hdr; |
| 1729 | |
| 1730 | skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC); |
| 1731 | if (!skb) |
| 1732 | return -ENOBUFS; |
| 1733 | |
| 1734 | hdr = skb_put_data(skb, ihdr, sizeof(struct sadb_msg)); |
| 1735 | hdr->sadb_msg_errno = (uint8_t) 0; |
| 1736 | hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t)); |
| 1737 | |
| 1738 | return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk, |
| 1739 | sock_net(sk)); |
| 1740 | } |
| 1741 | |
| 1742 | static int key_notify_sa_flush(const struct km_event *c) |
| 1743 | { |
| 1744 | struct sk_buff *skb; |
| 1745 | struct sadb_msg *hdr; |
| 1746 | |
| 1747 | skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC); |
| 1748 | if (!skb) |
| 1749 | return -ENOBUFS; |
| 1750 | hdr = skb_put(skb, sizeof(struct sadb_msg)); |
| 1751 | hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto); |
| 1752 | hdr->sadb_msg_type = SADB_FLUSH; |
| 1753 | hdr->sadb_msg_seq = c->seq; |
| 1754 | hdr->sadb_msg_pid = c->portid; |
| 1755 | hdr->sadb_msg_version = PF_KEY_V2; |
| 1756 | hdr->sadb_msg_errno = (uint8_t) 0; |
| 1757 | hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t)); |
| 1758 | hdr->sadb_msg_reserved = 0; |
| 1759 | |
| 1760 | pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net); |
| 1761 | |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
| 1765 | static int pfkey_flush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 1766 | { |
| 1767 | struct net *net = sock_net(sk); |
| 1768 | unsigned int proto; |
| 1769 | struct km_event c; |
| 1770 | int err, err2; |
| 1771 | |
| 1772 | proto = pfkey_satype2proto(hdr->sadb_msg_satype); |
| 1773 | if (proto == 0) |
| 1774 | return -EINVAL; |
| 1775 | |
| 1776 | err = xfrm_state_flush(net, proto, true); |
| 1777 | err2 = unicast_flush_resp(sk, hdr); |
| 1778 | if (err || err2) { |
| 1779 | if (err == -ESRCH) /* empty table - go quietly */ |
| 1780 | err = 0; |
| 1781 | return err ? err : err2; |
| 1782 | } |
| 1783 | |
| 1784 | c.data.proto = proto; |
| 1785 | c.seq = hdr->sadb_msg_seq; |
| 1786 | c.portid = hdr->sadb_msg_pid; |
| 1787 | c.event = XFRM_MSG_FLUSHSA; |
| 1788 | c.net = net; |
| 1789 | km_state_notify(NULL, &c); |
| 1790 | |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | static int dump_sa(struct xfrm_state *x, int count, void *ptr) |
| 1795 | { |
| 1796 | struct pfkey_sock *pfk = ptr; |
| 1797 | struct sk_buff *out_skb; |
| 1798 | struct sadb_msg *out_hdr; |
| 1799 | |
| 1800 | if (!pfkey_can_dump(&pfk->sk)) |
| 1801 | return -ENOBUFS; |
| 1802 | |
| 1803 | out_skb = pfkey_xfrm_state2msg(x); |
| 1804 | if (IS_ERR(out_skb)) |
| 1805 | return PTR_ERR(out_skb); |
| 1806 | |
| 1807 | out_hdr = (struct sadb_msg *) out_skb->data; |
| 1808 | out_hdr->sadb_msg_version = pfk->dump.msg_version; |
| 1809 | out_hdr->sadb_msg_type = SADB_DUMP; |
| 1810 | out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); |
| 1811 | out_hdr->sadb_msg_errno = 0; |
| 1812 | out_hdr->sadb_msg_reserved = 0; |
| 1813 | out_hdr->sadb_msg_seq = count + 1; |
| 1814 | out_hdr->sadb_msg_pid = pfk->dump.msg_portid; |
| 1815 | |
| 1816 | if (pfk->dump.skb) |
| 1817 | pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE, |
| 1818 | &pfk->sk, sock_net(&pfk->sk)); |
| 1819 | pfk->dump.skb = out_skb; |
| 1820 | |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
| 1824 | static int pfkey_dump_sa(struct pfkey_sock *pfk) |
| 1825 | { |
| 1826 | struct net *net = sock_net(&pfk->sk); |
| 1827 | return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk); |
| 1828 | } |
| 1829 | |
| 1830 | static void pfkey_dump_sa_done(struct pfkey_sock *pfk) |
| 1831 | { |
| 1832 | struct net *net = sock_net(&pfk->sk); |
| 1833 | |
| 1834 | xfrm_state_walk_done(&pfk->dump.u.state, net); |
| 1835 | } |
| 1836 | |
| 1837 | static int pfkey_dump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 1838 | { |
| 1839 | u8 proto; |
| 1840 | struct xfrm_address_filter *filter = NULL; |
| 1841 | struct pfkey_sock *pfk = pfkey_sk(sk); |
| 1842 | |
| 1843 | mutex_lock(&pfk->dump_lock); |
| 1844 | if (pfk->dump.dump != NULL) { |
| 1845 | mutex_unlock(&pfk->dump_lock); |
| 1846 | return -EBUSY; |
| 1847 | } |
| 1848 | |
| 1849 | proto = pfkey_satype2proto(hdr->sadb_msg_satype); |
| 1850 | if (proto == 0) { |
| 1851 | mutex_unlock(&pfk->dump_lock); |
| 1852 | return -EINVAL; |
| 1853 | } |
| 1854 | |
| 1855 | if (ext_hdrs[SADB_X_EXT_FILTER - 1]) { |
| 1856 | struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1]; |
| 1857 | |
| 1858 | if ((xfilter->sadb_x_filter_splen >= |
| 1859 | (sizeof(xfrm_address_t) << 3)) || |
| 1860 | (xfilter->sadb_x_filter_dplen >= |
| 1861 | (sizeof(xfrm_address_t) << 3))) { |
| 1862 | mutex_unlock(&pfk->dump_lock); |
| 1863 | return -EINVAL; |
| 1864 | } |
| 1865 | filter = kmalloc(sizeof(*filter), GFP_KERNEL); |
| 1866 | if (filter == NULL) { |
| 1867 | mutex_unlock(&pfk->dump_lock); |
| 1868 | return -ENOMEM; |
| 1869 | } |
| 1870 | |
| 1871 | memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr, |
| 1872 | sizeof(xfrm_address_t)); |
| 1873 | memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr, |
| 1874 | sizeof(xfrm_address_t)); |
| 1875 | filter->family = xfilter->sadb_x_filter_family; |
| 1876 | filter->splen = xfilter->sadb_x_filter_splen; |
| 1877 | filter->dplen = xfilter->sadb_x_filter_dplen; |
| 1878 | } |
| 1879 | |
| 1880 | pfk->dump.msg_version = hdr->sadb_msg_version; |
| 1881 | pfk->dump.msg_portid = hdr->sadb_msg_pid; |
| 1882 | pfk->dump.dump = pfkey_dump_sa; |
| 1883 | pfk->dump.done = pfkey_dump_sa_done; |
| 1884 | xfrm_state_walk_init(&pfk->dump.u.state, proto, filter); |
| 1885 | mutex_unlock(&pfk->dump_lock); |
| 1886 | |
| 1887 | return pfkey_do_dump(pfk); |
| 1888 | } |
| 1889 | |
| 1890 | static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 1891 | { |
| 1892 | struct pfkey_sock *pfk = pfkey_sk(sk); |
| 1893 | int satype = hdr->sadb_msg_satype; |
| 1894 | bool reset_errno = false; |
| 1895 | |
| 1896 | if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) { |
| 1897 | reset_errno = true; |
| 1898 | if (satype != 0 && satype != 1) |
| 1899 | return -EINVAL; |
| 1900 | pfk->promisc = satype; |
| 1901 | } |
| 1902 | if (reset_errno && skb_cloned(skb)) |
| 1903 | skb = skb_copy(skb, GFP_KERNEL); |
| 1904 | else |
| 1905 | skb = skb_clone(skb, GFP_KERNEL); |
| 1906 | |
| 1907 | if (reset_errno && skb) { |
| 1908 | struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data; |
| 1909 | new_hdr->sadb_msg_errno = 0; |
| 1910 | } |
| 1911 | |
| 1912 | pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk)); |
| 1913 | return 0; |
| 1914 | } |
| 1915 | |
| 1916 | static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr) |
| 1917 | { |
| 1918 | int i; |
| 1919 | u32 reqid = *(u32*)ptr; |
| 1920 | |
| 1921 | for (i=0; i<xp->xfrm_nr; i++) { |
| 1922 | if (xp->xfrm_vec[i].reqid == reqid) |
| 1923 | return -EEXIST; |
| 1924 | } |
| 1925 | return 0; |
| 1926 | } |
| 1927 | |
| 1928 | static u32 gen_reqid(struct net *net) |
| 1929 | { |
| 1930 | struct xfrm_policy_walk walk; |
| 1931 | u32 start; |
| 1932 | int rc; |
| 1933 | static u32 reqid = IPSEC_MANUAL_REQID_MAX; |
| 1934 | |
| 1935 | start = reqid; |
| 1936 | do { |
| 1937 | ++reqid; |
| 1938 | if (reqid == 0) |
| 1939 | reqid = IPSEC_MANUAL_REQID_MAX+1; |
| 1940 | xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN); |
| 1941 | rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid); |
| 1942 | xfrm_policy_walk_done(&walk, net); |
| 1943 | if (rc != -EEXIST) |
| 1944 | return reqid; |
| 1945 | } while (reqid != start); |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
| 1949 | static int |
| 1950 | parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq) |
| 1951 | { |
| 1952 | struct net *net = xp_net(xp); |
| 1953 | struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr; |
| 1954 | int mode; |
| 1955 | |
| 1956 | if (xp->xfrm_nr >= XFRM_MAX_DEPTH) |
| 1957 | return -ELOOP; |
| 1958 | |
| 1959 | if (rq->sadb_x_ipsecrequest_mode == 0) |
| 1960 | return -EINVAL; |
| 1961 | if (!xfrm_id_proto_valid(rq->sadb_x_ipsecrequest_proto)) |
| 1962 | return -EINVAL; |
| 1963 | |
| 1964 | t->id.proto = rq->sadb_x_ipsecrequest_proto; |
| 1965 | if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0) |
| 1966 | return -EINVAL; |
| 1967 | t->mode = mode; |
| 1968 | if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE) |
| 1969 | t->optional = 1; |
| 1970 | else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) { |
| 1971 | t->reqid = rq->sadb_x_ipsecrequest_reqid; |
| 1972 | if (t->reqid > IPSEC_MANUAL_REQID_MAX) |
| 1973 | t->reqid = 0; |
| 1974 | if (!t->reqid && !(t->reqid = gen_reqid(net))) |
| 1975 | return -ENOBUFS; |
| 1976 | } |
| 1977 | |
| 1978 | /* addresses present only in tunnel mode */ |
| 1979 | if (t->mode == XFRM_MODE_TUNNEL) { |
| 1980 | int err; |
| 1981 | |
| 1982 | err = parse_sockaddr_pair( |
| 1983 | (struct sockaddr *)(rq + 1), |
| 1984 | rq->sadb_x_ipsecrequest_len - sizeof(*rq), |
| 1985 | &t->saddr, &t->id.daddr, &t->encap_family); |
| 1986 | if (err) |
| 1987 | return err; |
| 1988 | } else |
| 1989 | t->encap_family = xp->family; |
| 1990 | |
| 1991 | /* No way to set this via kame pfkey */ |
| 1992 | t->allalgs = 1; |
| 1993 | xp->xfrm_nr++; |
| 1994 | return 0; |
| 1995 | } |
| 1996 | |
| 1997 | static int |
| 1998 | parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol) |
| 1999 | { |
| 2000 | int err; |
| 2001 | int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy); |
| 2002 | struct sadb_x_ipsecrequest *rq = (void*)(pol+1); |
| 2003 | |
| 2004 | if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy)) |
| 2005 | return -EINVAL; |
| 2006 | |
| 2007 | while (len >= sizeof(*rq)) { |
| 2008 | if (len < rq->sadb_x_ipsecrequest_len || |
| 2009 | rq->sadb_x_ipsecrequest_len < sizeof(*rq)) |
| 2010 | return -EINVAL; |
| 2011 | |
| 2012 | if ((err = parse_ipsecrequest(xp, rq)) < 0) |
| 2013 | return err; |
| 2014 | len -= rq->sadb_x_ipsecrequest_len; |
| 2015 | rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len); |
| 2016 | } |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
| 2020 | static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp) |
| 2021 | { |
| 2022 | struct xfrm_sec_ctx *xfrm_ctx = xp->security; |
| 2023 | |
| 2024 | if (xfrm_ctx) { |
| 2025 | int len = sizeof(struct sadb_x_sec_ctx); |
| 2026 | len += xfrm_ctx->ctx_len; |
| 2027 | return PFKEY_ALIGN8(len); |
| 2028 | } |
| 2029 | return 0; |
| 2030 | } |
| 2031 | |
| 2032 | static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp) |
| 2033 | { |
| 2034 | const struct xfrm_tmpl *t; |
| 2035 | int sockaddr_size = pfkey_sockaddr_size(xp->family); |
| 2036 | int socklen = 0; |
| 2037 | int i; |
| 2038 | |
| 2039 | for (i=0; i<xp->xfrm_nr; i++) { |
| 2040 | t = xp->xfrm_vec + i; |
| 2041 | socklen += pfkey_sockaddr_len(t->encap_family); |
| 2042 | } |
| 2043 | |
| 2044 | return sizeof(struct sadb_msg) + |
| 2045 | (sizeof(struct sadb_lifetime) * 3) + |
| 2046 | (sizeof(struct sadb_address) * 2) + |
| 2047 | (sockaddr_size * 2) + |
| 2048 | sizeof(struct sadb_x_policy) + |
| 2049 | (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) + |
| 2050 | (socklen * 2) + |
| 2051 | pfkey_xfrm_policy2sec_ctx_size(xp); |
| 2052 | } |
| 2053 | |
| 2054 | static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp) |
| 2055 | { |
| 2056 | struct sk_buff *skb; |
| 2057 | int size; |
| 2058 | |
| 2059 | size = pfkey_xfrm_policy2msg_size(xp); |
| 2060 | |
| 2061 | skb = alloc_skb(size + 16, GFP_ATOMIC); |
| 2062 | if (skb == NULL) |
| 2063 | return ERR_PTR(-ENOBUFS); |
| 2064 | |
| 2065 | return skb; |
| 2066 | } |
| 2067 | |
| 2068 | static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir) |
| 2069 | { |
| 2070 | struct sadb_msg *hdr; |
| 2071 | struct sadb_address *addr; |
| 2072 | struct sadb_lifetime *lifetime; |
| 2073 | struct sadb_x_policy *pol; |
| 2074 | struct sadb_x_sec_ctx *sec_ctx; |
| 2075 | struct xfrm_sec_ctx *xfrm_ctx; |
| 2076 | int i; |
| 2077 | int size; |
| 2078 | int sockaddr_size = pfkey_sockaddr_size(xp->family); |
| 2079 | int socklen = pfkey_sockaddr_len(xp->family); |
| 2080 | |
| 2081 | size = pfkey_xfrm_policy2msg_size(xp); |
| 2082 | |
| 2083 | /* call should fill header later */ |
| 2084 | hdr = skb_put(skb, sizeof(struct sadb_msg)); |
| 2085 | memset(hdr, 0, size); /* XXX do we need this ? */ |
| 2086 | |
| 2087 | /* src address */ |
| 2088 | addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size); |
| 2089 | addr->sadb_address_len = |
| 2090 | (sizeof(struct sadb_address)+sockaddr_size)/ |
| 2091 | sizeof(uint64_t); |
| 2092 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; |
| 2093 | addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto); |
| 2094 | addr->sadb_address_prefixlen = xp->selector.prefixlen_s; |
| 2095 | addr->sadb_address_reserved = 0; |
| 2096 | if (!pfkey_sockaddr_fill(&xp->selector.saddr, |
| 2097 | xp->selector.sport, |
| 2098 | (struct sockaddr *) (addr + 1), |
| 2099 | xp->family)) |
| 2100 | BUG(); |
| 2101 | |
| 2102 | /* dst address */ |
| 2103 | addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size); |
| 2104 | addr->sadb_address_len = |
| 2105 | (sizeof(struct sadb_address)+sockaddr_size)/ |
| 2106 | sizeof(uint64_t); |
| 2107 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; |
| 2108 | addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto); |
| 2109 | addr->sadb_address_prefixlen = xp->selector.prefixlen_d; |
| 2110 | addr->sadb_address_reserved = 0; |
| 2111 | |
| 2112 | pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport, |
| 2113 | (struct sockaddr *) (addr + 1), |
| 2114 | xp->family); |
| 2115 | |
| 2116 | /* hard time */ |
| 2117 | lifetime = skb_put(skb, sizeof(struct sadb_lifetime)); |
| 2118 | lifetime->sadb_lifetime_len = |
| 2119 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); |
| 2120 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; |
| 2121 | lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit); |
| 2122 | lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit); |
| 2123 | lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds; |
| 2124 | lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds; |
| 2125 | /* soft time */ |
| 2126 | lifetime = skb_put(skb, sizeof(struct sadb_lifetime)); |
| 2127 | lifetime->sadb_lifetime_len = |
| 2128 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); |
| 2129 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT; |
| 2130 | lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit); |
| 2131 | lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit); |
| 2132 | lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds; |
| 2133 | lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds; |
| 2134 | /* current time */ |
| 2135 | lifetime = skb_put(skb, sizeof(struct sadb_lifetime)); |
| 2136 | lifetime->sadb_lifetime_len = |
| 2137 | sizeof(struct sadb_lifetime)/sizeof(uint64_t); |
| 2138 | lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; |
| 2139 | lifetime->sadb_lifetime_allocations = xp->curlft.packets; |
| 2140 | lifetime->sadb_lifetime_bytes = xp->curlft.bytes; |
| 2141 | lifetime->sadb_lifetime_addtime = xp->curlft.add_time; |
| 2142 | lifetime->sadb_lifetime_usetime = xp->curlft.use_time; |
| 2143 | |
| 2144 | pol = skb_put(skb, sizeof(struct sadb_x_policy)); |
| 2145 | pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t); |
| 2146 | pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY; |
| 2147 | pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD; |
| 2148 | if (xp->action == XFRM_POLICY_ALLOW) { |
| 2149 | if (xp->xfrm_nr) |
| 2150 | pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC; |
| 2151 | else |
| 2152 | pol->sadb_x_policy_type = IPSEC_POLICY_NONE; |
| 2153 | } |
| 2154 | pol->sadb_x_policy_dir = dir+1; |
| 2155 | pol->sadb_x_policy_reserved = 0; |
| 2156 | pol->sadb_x_policy_id = xp->index; |
| 2157 | pol->sadb_x_policy_priority = xp->priority; |
| 2158 | |
| 2159 | for (i=0; i<xp->xfrm_nr; i++) { |
| 2160 | const struct xfrm_tmpl *t = xp->xfrm_vec + i; |
| 2161 | struct sadb_x_ipsecrequest *rq; |
| 2162 | int req_size; |
| 2163 | int mode; |
| 2164 | |
| 2165 | req_size = sizeof(struct sadb_x_ipsecrequest); |
| 2166 | if (t->mode == XFRM_MODE_TUNNEL) { |
| 2167 | socklen = pfkey_sockaddr_len(t->encap_family); |
| 2168 | req_size += socklen * 2; |
| 2169 | } else { |
| 2170 | size -= 2*socklen; |
| 2171 | } |
| 2172 | rq = skb_put(skb, req_size); |
| 2173 | pol->sadb_x_policy_len += req_size/8; |
| 2174 | memset(rq, 0, sizeof(*rq)); |
| 2175 | rq->sadb_x_ipsecrequest_len = req_size; |
| 2176 | rq->sadb_x_ipsecrequest_proto = t->id.proto; |
| 2177 | if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0) |
| 2178 | return -EINVAL; |
| 2179 | rq->sadb_x_ipsecrequest_mode = mode; |
| 2180 | rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE; |
| 2181 | if (t->reqid) |
| 2182 | rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE; |
| 2183 | if (t->optional) |
| 2184 | rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE; |
| 2185 | rq->sadb_x_ipsecrequest_reqid = t->reqid; |
| 2186 | |
| 2187 | if (t->mode == XFRM_MODE_TUNNEL) { |
| 2188 | u8 *sa = (void *)(rq + 1); |
| 2189 | pfkey_sockaddr_fill(&t->saddr, 0, |
| 2190 | (struct sockaddr *)sa, |
| 2191 | t->encap_family); |
| 2192 | pfkey_sockaddr_fill(&t->id.daddr, 0, |
| 2193 | (struct sockaddr *) (sa + socklen), |
| 2194 | t->encap_family); |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | /* security context */ |
| 2199 | if ((xfrm_ctx = xp->security)) { |
| 2200 | int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp); |
| 2201 | |
| 2202 | sec_ctx = skb_put(skb, ctx_size); |
| 2203 | sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t); |
| 2204 | sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX; |
| 2205 | sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi; |
| 2206 | sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg; |
| 2207 | sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len; |
| 2208 | memcpy(sec_ctx + 1, xfrm_ctx->ctx_str, |
| 2209 | xfrm_ctx->ctx_len); |
| 2210 | } |
| 2211 | |
| 2212 | hdr->sadb_msg_len = size / sizeof(uint64_t); |
| 2213 | hdr->sadb_msg_reserved = refcount_read(&xp->refcnt); |
| 2214 | |
| 2215 | return 0; |
| 2216 | } |
| 2217 | |
| 2218 | static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c) |
| 2219 | { |
| 2220 | struct sk_buff *out_skb; |
| 2221 | struct sadb_msg *out_hdr; |
| 2222 | int err; |
| 2223 | |
| 2224 | out_skb = pfkey_xfrm_policy2msg_prep(xp); |
| 2225 | if (IS_ERR(out_skb)) |
| 2226 | return PTR_ERR(out_skb); |
| 2227 | |
| 2228 | err = pfkey_xfrm_policy2msg(out_skb, xp, dir); |
| 2229 | if (err < 0) |
| 2230 | return err; |
| 2231 | |
| 2232 | out_hdr = (struct sadb_msg *) out_skb->data; |
| 2233 | out_hdr->sadb_msg_version = PF_KEY_V2; |
| 2234 | |
| 2235 | if (c->data.byid && c->event == XFRM_MSG_DELPOLICY) |
| 2236 | out_hdr->sadb_msg_type = SADB_X_SPDDELETE2; |
| 2237 | else |
| 2238 | out_hdr->sadb_msg_type = event2poltype(c->event); |
| 2239 | out_hdr->sadb_msg_errno = 0; |
| 2240 | out_hdr->sadb_msg_seq = c->seq; |
| 2241 | out_hdr->sadb_msg_pid = c->portid; |
| 2242 | pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp)); |
| 2243 | return 0; |
| 2244 | |
| 2245 | } |
| 2246 | |
| 2247 | static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 2248 | { |
| 2249 | struct net *net = sock_net(sk); |
| 2250 | int err = 0; |
| 2251 | struct sadb_lifetime *lifetime; |
| 2252 | struct sadb_address *sa; |
| 2253 | struct sadb_x_policy *pol; |
| 2254 | struct xfrm_policy *xp; |
| 2255 | struct km_event c; |
| 2256 | struct sadb_x_sec_ctx *sec_ctx; |
| 2257 | |
| 2258 | if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], |
| 2259 | ext_hdrs[SADB_EXT_ADDRESS_DST-1]) || |
| 2260 | !ext_hdrs[SADB_X_EXT_POLICY-1]) |
| 2261 | return -EINVAL; |
| 2262 | |
| 2263 | pol = ext_hdrs[SADB_X_EXT_POLICY-1]; |
| 2264 | if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC) |
| 2265 | return -EINVAL; |
| 2266 | if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) |
| 2267 | return -EINVAL; |
| 2268 | |
| 2269 | xp = xfrm_policy_alloc(net, GFP_KERNEL); |
| 2270 | if (xp == NULL) |
| 2271 | return -ENOBUFS; |
| 2272 | |
| 2273 | xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ? |
| 2274 | XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW); |
| 2275 | xp->priority = pol->sadb_x_policy_priority; |
| 2276 | |
| 2277 | sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1]; |
| 2278 | xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr); |
| 2279 | xp->selector.family = xp->family; |
| 2280 | xp->selector.prefixlen_s = sa->sadb_address_prefixlen; |
| 2281 | xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); |
| 2282 | xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port; |
| 2283 | if (xp->selector.sport) |
| 2284 | xp->selector.sport_mask = htons(0xffff); |
| 2285 | |
| 2286 | sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1]; |
| 2287 | pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr); |
| 2288 | xp->selector.prefixlen_d = sa->sadb_address_prefixlen; |
| 2289 | |
| 2290 | /* Amusing, we set this twice. KAME apps appear to set same value |
| 2291 | * in both addresses. |
| 2292 | */ |
| 2293 | xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); |
| 2294 | |
| 2295 | xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port; |
| 2296 | if (xp->selector.dport) |
| 2297 | xp->selector.dport_mask = htons(0xffff); |
| 2298 | |
| 2299 | sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1]; |
| 2300 | if (sec_ctx != NULL) { |
| 2301 | struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL); |
| 2302 | |
| 2303 | if (!uctx) { |
| 2304 | err = -ENOBUFS; |
| 2305 | goto out; |
| 2306 | } |
| 2307 | |
| 2308 | err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL); |
| 2309 | kfree(uctx); |
| 2310 | |
| 2311 | if (err) |
| 2312 | goto out; |
| 2313 | } |
| 2314 | |
| 2315 | xp->lft.soft_byte_limit = XFRM_INF; |
| 2316 | xp->lft.hard_byte_limit = XFRM_INF; |
| 2317 | xp->lft.soft_packet_limit = XFRM_INF; |
| 2318 | xp->lft.hard_packet_limit = XFRM_INF; |
| 2319 | if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) { |
| 2320 | xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); |
| 2321 | xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); |
| 2322 | xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime; |
| 2323 | xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime; |
| 2324 | } |
| 2325 | if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) { |
| 2326 | xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations); |
| 2327 | xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes); |
| 2328 | xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime; |
| 2329 | xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime; |
| 2330 | } |
| 2331 | xp->xfrm_nr = 0; |
| 2332 | if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC && |
| 2333 | (err = parse_ipsecrequests(xp, pol)) < 0) |
| 2334 | goto out; |
| 2335 | |
| 2336 | err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp, |
| 2337 | hdr->sadb_msg_type != SADB_X_SPDUPDATE); |
| 2338 | |
| 2339 | xfrm_audit_policy_add(xp, err ? 0 : 1, true); |
| 2340 | |
| 2341 | if (err) |
| 2342 | goto out; |
| 2343 | |
| 2344 | if (hdr->sadb_msg_type == SADB_X_SPDUPDATE) |
| 2345 | c.event = XFRM_MSG_UPDPOLICY; |
| 2346 | else |
| 2347 | c.event = XFRM_MSG_NEWPOLICY; |
| 2348 | |
| 2349 | c.seq = hdr->sadb_msg_seq; |
| 2350 | c.portid = hdr->sadb_msg_pid; |
| 2351 | |
| 2352 | km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c); |
| 2353 | xfrm_pol_put(xp); |
| 2354 | return 0; |
| 2355 | |
| 2356 | out: |
| 2357 | xp->walk.dead = 1; |
| 2358 | xfrm_policy_destroy(xp); |
| 2359 | return err; |
| 2360 | } |
| 2361 | |
| 2362 | static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 2363 | { |
| 2364 | struct net *net = sock_net(sk); |
| 2365 | int err; |
| 2366 | struct sadb_address *sa; |
| 2367 | struct sadb_x_policy *pol; |
| 2368 | struct xfrm_policy *xp; |
| 2369 | struct xfrm_selector sel; |
| 2370 | struct km_event c; |
| 2371 | struct sadb_x_sec_ctx *sec_ctx; |
| 2372 | struct xfrm_sec_ctx *pol_ctx = NULL; |
| 2373 | |
| 2374 | if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1], |
| 2375 | ext_hdrs[SADB_EXT_ADDRESS_DST-1]) || |
| 2376 | !ext_hdrs[SADB_X_EXT_POLICY-1]) |
| 2377 | return -EINVAL; |
| 2378 | |
| 2379 | pol = ext_hdrs[SADB_X_EXT_POLICY-1]; |
| 2380 | if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) |
| 2381 | return -EINVAL; |
| 2382 | |
| 2383 | memset(&sel, 0, sizeof(sel)); |
| 2384 | |
| 2385 | sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1]; |
| 2386 | sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr); |
| 2387 | sel.prefixlen_s = sa->sadb_address_prefixlen; |
| 2388 | sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); |
| 2389 | sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port; |
| 2390 | if (sel.sport) |
| 2391 | sel.sport_mask = htons(0xffff); |
| 2392 | |
| 2393 | sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1]; |
| 2394 | pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr); |
| 2395 | sel.prefixlen_d = sa->sadb_address_prefixlen; |
| 2396 | sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); |
| 2397 | sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port; |
| 2398 | if (sel.dport) |
| 2399 | sel.dport_mask = htons(0xffff); |
| 2400 | |
| 2401 | sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1]; |
| 2402 | if (sec_ctx != NULL) { |
| 2403 | struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL); |
| 2404 | |
| 2405 | if (!uctx) |
| 2406 | return -ENOMEM; |
| 2407 | |
| 2408 | err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL); |
| 2409 | kfree(uctx); |
| 2410 | if (err) |
| 2411 | return err; |
| 2412 | } |
| 2413 | |
| 2414 | xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN, |
| 2415 | pol->sadb_x_policy_dir - 1, &sel, pol_ctx, |
| 2416 | 1, &err); |
| 2417 | security_xfrm_policy_free(pol_ctx); |
| 2418 | if (xp == NULL) |
| 2419 | return -ENOENT; |
| 2420 | |
| 2421 | xfrm_audit_policy_delete(xp, err ? 0 : 1, true); |
| 2422 | |
| 2423 | if (err) |
| 2424 | goto out; |
| 2425 | |
| 2426 | c.seq = hdr->sadb_msg_seq; |
| 2427 | c.portid = hdr->sadb_msg_pid; |
| 2428 | c.data.byid = 0; |
| 2429 | c.event = XFRM_MSG_DELPOLICY; |
| 2430 | km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c); |
| 2431 | |
| 2432 | out: |
| 2433 | xfrm_pol_put(xp); |
| 2434 | return err; |
| 2435 | } |
| 2436 | |
| 2437 | static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struct sadb_msg *hdr, int dir) |
| 2438 | { |
| 2439 | int err; |
| 2440 | struct sk_buff *out_skb; |
| 2441 | struct sadb_msg *out_hdr; |
| 2442 | err = 0; |
| 2443 | |
| 2444 | out_skb = pfkey_xfrm_policy2msg_prep(xp); |
| 2445 | if (IS_ERR(out_skb)) { |
| 2446 | err = PTR_ERR(out_skb); |
| 2447 | goto out; |
| 2448 | } |
| 2449 | err = pfkey_xfrm_policy2msg(out_skb, xp, dir); |
| 2450 | if (err < 0) { |
| 2451 | kfree_skb(out_skb); |
| 2452 | goto out; |
| 2453 | } |
| 2454 | |
| 2455 | out_hdr = (struct sadb_msg *) out_skb->data; |
| 2456 | out_hdr->sadb_msg_version = hdr->sadb_msg_version; |
| 2457 | out_hdr->sadb_msg_type = hdr->sadb_msg_type; |
| 2458 | out_hdr->sadb_msg_satype = 0; |
| 2459 | out_hdr->sadb_msg_errno = 0; |
| 2460 | out_hdr->sadb_msg_seq = hdr->sadb_msg_seq; |
| 2461 | out_hdr->sadb_msg_pid = hdr->sadb_msg_pid; |
| 2462 | pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp)); |
| 2463 | err = 0; |
| 2464 | |
| 2465 | out: |
| 2466 | return err; |
| 2467 | } |
| 2468 | |
| 2469 | static int pfkey_sockaddr_pair_size(sa_family_t family) |
| 2470 | { |
| 2471 | return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2); |
| 2472 | } |
| 2473 | |
| 2474 | static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len, |
| 2475 | xfrm_address_t *saddr, xfrm_address_t *daddr, |
| 2476 | u16 *family) |
| 2477 | { |
| 2478 | int af, socklen; |
| 2479 | |
| 2480 | if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family)) |
| 2481 | return -EINVAL; |
| 2482 | |
| 2483 | af = pfkey_sockaddr_extract(sa, saddr); |
| 2484 | if (!af) |
| 2485 | return -EINVAL; |
| 2486 | |
| 2487 | socklen = pfkey_sockaddr_len(af); |
| 2488 | if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen), |
| 2489 | daddr) != af) |
| 2490 | return -EINVAL; |
| 2491 | |
| 2492 | *family = af; |
| 2493 | return 0; |
| 2494 | } |
| 2495 | |
| 2496 | #ifdef CONFIG_NET_KEY_MIGRATE |
| 2497 | static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len, |
| 2498 | struct xfrm_migrate *m) |
| 2499 | { |
| 2500 | int err; |
| 2501 | struct sadb_x_ipsecrequest *rq2; |
| 2502 | int mode; |
| 2503 | |
| 2504 | if (len < sizeof(*rq1) || |
| 2505 | len < rq1->sadb_x_ipsecrequest_len || |
| 2506 | rq1->sadb_x_ipsecrequest_len < sizeof(*rq1)) |
| 2507 | return -EINVAL; |
| 2508 | |
| 2509 | /* old endoints */ |
| 2510 | err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1), |
| 2511 | rq1->sadb_x_ipsecrequest_len - sizeof(*rq1), |
| 2512 | &m->old_saddr, &m->old_daddr, |
| 2513 | &m->old_family); |
| 2514 | if (err) |
| 2515 | return err; |
| 2516 | |
| 2517 | rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len); |
| 2518 | len -= rq1->sadb_x_ipsecrequest_len; |
| 2519 | |
| 2520 | if (len <= sizeof(*rq2) || |
| 2521 | len < rq2->sadb_x_ipsecrequest_len || |
| 2522 | rq2->sadb_x_ipsecrequest_len < sizeof(*rq2)) |
| 2523 | return -EINVAL; |
| 2524 | |
| 2525 | /* new endpoints */ |
| 2526 | err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1), |
| 2527 | rq2->sadb_x_ipsecrequest_len - sizeof(*rq2), |
| 2528 | &m->new_saddr, &m->new_daddr, |
| 2529 | &m->new_family); |
| 2530 | if (err) |
| 2531 | return err; |
| 2532 | |
| 2533 | if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto || |
| 2534 | rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode || |
| 2535 | rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid) |
| 2536 | return -EINVAL; |
| 2537 | |
| 2538 | m->proto = rq1->sadb_x_ipsecrequest_proto; |
| 2539 | if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0) |
| 2540 | return -EINVAL; |
| 2541 | m->mode = mode; |
| 2542 | m->reqid = rq1->sadb_x_ipsecrequest_reqid; |
| 2543 | |
| 2544 | return ((int)(rq1->sadb_x_ipsecrequest_len + |
| 2545 | rq2->sadb_x_ipsecrequest_len)); |
| 2546 | } |
| 2547 | |
| 2548 | static int pfkey_migrate(struct sock *sk, struct sk_buff *skb, |
| 2549 | const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 2550 | { |
| 2551 | int i, len, ret, err = -EINVAL; |
| 2552 | u8 dir; |
| 2553 | struct sadb_address *sa; |
| 2554 | struct sadb_x_kmaddress *kma; |
| 2555 | struct sadb_x_policy *pol; |
| 2556 | struct sadb_x_ipsecrequest *rq; |
| 2557 | struct xfrm_selector sel; |
| 2558 | struct xfrm_migrate m[XFRM_MAX_DEPTH]; |
| 2559 | struct xfrm_kmaddress k; |
| 2560 | struct net *net = sock_net(sk); |
| 2561 | |
| 2562 | if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1], |
| 2563 | ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) || |
| 2564 | !ext_hdrs[SADB_X_EXT_POLICY - 1]) { |
| 2565 | err = -EINVAL; |
| 2566 | goto out; |
| 2567 | } |
| 2568 | |
| 2569 | kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1]; |
| 2570 | pol = ext_hdrs[SADB_X_EXT_POLICY - 1]; |
| 2571 | |
| 2572 | if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) { |
| 2573 | err = -EINVAL; |
| 2574 | goto out; |
| 2575 | } |
| 2576 | |
| 2577 | if (kma) { |
| 2578 | /* convert sadb_x_kmaddress to xfrm_kmaddress */ |
| 2579 | k.reserved = kma->sadb_x_kmaddress_reserved; |
| 2580 | ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1), |
| 2581 | 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma), |
| 2582 | &k.local, &k.remote, &k.family); |
| 2583 | if (ret < 0) { |
| 2584 | err = ret; |
| 2585 | goto out; |
| 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | dir = pol->sadb_x_policy_dir - 1; |
| 2590 | memset(&sel, 0, sizeof(sel)); |
| 2591 | |
| 2592 | /* set source address info of selector */ |
| 2593 | sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1]; |
| 2594 | sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr); |
| 2595 | sel.prefixlen_s = sa->sadb_address_prefixlen; |
| 2596 | sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); |
| 2597 | sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port; |
| 2598 | if (sel.sport) |
| 2599 | sel.sport_mask = htons(0xffff); |
| 2600 | |
| 2601 | /* set destination address info of selector */ |
| 2602 | sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1]; |
| 2603 | pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr); |
| 2604 | sel.prefixlen_d = sa->sadb_address_prefixlen; |
| 2605 | sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto); |
| 2606 | sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port; |
| 2607 | if (sel.dport) |
| 2608 | sel.dport_mask = htons(0xffff); |
| 2609 | |
| 2610 | rq = (struct sadb_x_ipsecrequest *)(pol + 1); |
| 2611 | |
| 2612 | /* extract ipsecrequests */ |
| 2613 | i = 0; |
| 2614 | len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy); |
| 2615 | |
| 2616 | while (len > 0 && i < XFRM_MAX_DEPTH) { |
| 2617 | ret = ipsecrequests_to_migrate(rq, len, &m[i]); |
| 2618 | if (ret < 0) { |
| 2619 | err = ret; |
| 2620 | goto out; |
| 2621 | } else { |
| 2622 | rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret); |
| 2623 | len -= ret; |
| 2624 | i++; |
| 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | if (!i || len > 0) { |
| 2629 | err = -EINVAL; |
| 2630 | goto out; |
| 2631 | } |
| 2632 | |
| 2633 | return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i, |
| 2634 | kma ? &k : NULL, net, NULL); |
| 2635 | |
| 2636 | out: |
| 2637 | return err; |
| 2638 | } |
| 2639 | #else |
| 2640 | static int pfkey_migrate(struct sock *sk, struct sk_buff *skb, |
| 2641 | const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 2642 | { |
| 2643 | return -ENOPROTOOPT; |
| 2644 | } |
| 2645 | #endif |
| 2646 | |
| 2647 | |
| 2648 | static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 2649 | { |
| 2650 | struct net *net = sock_net(sk); |
| 2651 | unsigned int dir; |
| 2652 | int err = 0, delete; |
| 2653 | struct sadb_x_policy *pol; |
| 2654 | struct xfrm_policy *xp; |
| 2655 | struct km_event c; |
| 2656 | |
| 2657 | if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL) |
| 2658 | return -EINVAL; |
| 2659 | |
| 2660 | dir = xfrm_policy_id2dir(pol->sadb_x_policy_id); |
| 2661 | if (dir >= XFRM_POLICY_MAX) |
| 2662 | return -EINVAL; |
| 2663 | |
| 2664 | delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2); |
| 2665 | xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN, |
| 2666 | dir, pol->sadb_x_policy_id, delete, &err); |
| 2667 | if (xp == NULL) |
| 2668 | return -ENOENT; |
| 2669 | |
| 2670 | if (delete) { |
| 2671 | xfrm_audit_policy_delete(xp, err ? 0 : 1, true); |
| 2672 | |
| 2673 | if (err) |
| 2674 | goto out; |
| 2675 | c.seq = hdr->sadb_msg_seq; |
| 2676 | c.portid = hdr->sadb_msg_pid; |
| 2677 | c.data.byid = 1; |
| 2678 | c.event = XFRM_MSG_DELPOLICY; |
| 2679 | km_policy_notify(xp, dir, &c); |
| 2680 | } else { |
| 2681 | err = key_pol_get_resp(sk, xp, hdr, dir); |
| 2682 | } |
| 2683 | |
| 2684 | out: |
| 2685 | xfrm_pol_put(xp); |
| 2686 | return err; |
| 2687 | } |
| 2688 | |
| 2689 | static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr) |
| 2690 | { |
| 2691 | struct pfkey_sock *pfk = ptr; |
| 2692 | struct sk_buff *out_skb; |
| 2693 | struct sadb_msg *out_hdr; |
| 2694 | int err; |
| 2695 | |
| 2696 | if (!pfkey_can_dump(&pfk->sk)) |
| 2697 | return -ENOBUFS; |
| 2698 | |
| 2699 | out_skb = pfkey_xfrm_policy2msg_prep(xp); |
| 2700 | if (IS_ERR(out_skb)) |
| 2701 | return PTR_ERR(out_skb); |
| 2702 | |
| 2703 | err = pfkey_xfrm_policy2msg(out_skb, xp, dir); |
| 2704 | if (err < 0) { |
| 2705 | kfree_skb(out_skb); |
| 2706 | return err; |
| 2707 | } |
| 2708 | |
| 2709 | out_hdr = (struct sadb_msg *) out_skb->data; |
| 2710 | out_hdr->sadb_msg_version = pfk->dump.msg_version; |
| 2711 | out_hdr->sadb_msg_type = SADB_X_SPDDUMP; |
| 2712 | out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC; |
| 2713 | out_hdr->sadb_msg_errno = 0; |
| 2714 | out_hdr->sadb_msg_seq = count + 1; |
| 2715 | out_hdr->sadb_msg_pid = pfk->dump.msg_portid; |
| 2716 | |
| 2717 | if (pfk->dump.skb) |
| 2718 | pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE, |
| 2719 | &pfk->sk, sock_net(&pfk->sk)); |
| 2720 | pfk->dump.skb = out_skb; |
| 2721 | |
| 2722 | return 0; |
| 2723 | } |
| 2724 | |
| 2725 | static int pfkey_dump_sp(struct pfkey_sock *pfk) |
| 2726 | { |
| 2727 | struct net *net = sock_net(&pfk->sk); |
| 2728 | return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk); |
| 2729 | } |
| 2730 | |
| 2731 | static void pfkey_dump_sp_done(struct pfkey_sock *pfk) |
| 2732 | { |
| 2733 | struct net *net = sock_net((struct sock *)pfk); |
| 2734 | |
| 2735 | xfrm_policy_walk_done(&pfk->dump.u.policy, net); |
| 2736 | } |
| 2737 | |
| 2738 | static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 2739 | { |
| 2740 | struct pfkey_sock *pfk = pfkey_sk(sk); |
| 2741 | |
| 2742 | mutex_lock(&pfk->dump_lock); |
| 2743 | if (pfk->dump.dump != NULL) { |
| 2744 | mutex_unlock(&pfk->dump_lock); |
| 2745 | return -EBUSY; |
| 2746 | } |
| 2747 | |
| 2748 | pfk->dump.msg_version = hdr->sadb_msg_version; |
| 2749 | pfk->dump.msg_portid = hdr->sadb_msg_pid; |
| 2750 | pfk->dump.dump = pfkey_dump_sp; |
| 2751 | pfk->dump.done = pfkey_dump_sp_done; |
| 2752 | xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN); |
| 2753 | mutex_unlock(&pfk->dump_lock); |
| 2754 | |
| 2755 | return pfkey_do_dump(pfk); |
| 2756 | } |
| 2757 | |
| 2758 | static int key_notify_policy_flush(const struct km_event *c) |
| 2759 | { |
| 2760 | struct sk_buff *skb_out; |
| 2761 | struct sadb_msg *hdr; |
| 2762 | |
| 2763 | skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC); |
| 2764 | if (!skb_out) |
| 2765 | return -ENOBUFS; |
| 2766 | hdr = skb_put(skb_out, sizeof(struct sadb_msg)); |
| 2767 | hdr->sadb_msg_type = SADB_X_SPDFLUSH; |
| 2768 | hdr->sadb_msg_seq = c->seq; |
| 2769 | hdr->sadb_msg_pid = c->portid; |
| 2770 | hdr->sadb_msg_version = PF_KEY_V2; |
| 2771 | hdr->sadb_msg_errno = (uint8_t) 0; |
| 2772 | hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC; |
| 2773 | hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t)); |
| 2774 | hdr->sadb_msg_reserved = 0; |
| 2775 | pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net); |
| 2776 | return 0; |
| 2777 | |
| 2778 | } |
| 2779 | |
| 2780 | static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs) |
| 2781 | { |
| 2782 | struct net *net = sock_net(sk); |
| 2783 | struct km_event c; |
| 2784 | int err, err2; |
| 2785 | |
| 2786 | err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true); |
| 2787 | err2 = unicast_flush_resp(sk, hdr); |
| 2788 | if (err || err2) { |
| 2789 | if (err == -ESRCH) /* empty table - old silent behavior */ |
| 2790 | return 0; |
| 2791 | return err; |
| 2792 | } |
| 2793 | |
| 2794 | c.data.type = XFRM_POLICY_TYPE_MAIN; |
| 2795 | c.event = XFRM_MSG_FLUSHPOLICY; |
| 2796 | c.portid = hdr->sadb_msg_pid; |
| 2797 | c.seq = hdr->sadb_msg_seq; |
| 2798 | c.net = net; |
| 2799 | km_policy_notify(NULL, 0, &c); |
| 2800 | |
| 2801 | return 0; |
| 2802 | } |
| 2803 | |
| 2804 | typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb, |
| 2805 | const struct sadb_msg *hdr, void * const *ext_hdrs); |
| 2806 | static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = { |
| 2807 | [SADB_RESERVED] = pfkey_reserved, |
| 2808 | [SADB_GETSPI] = pfkey_getspi, |
| 2809 | [SADB_UPDATE] = pfkey_add, |
| 2810 | [SADB_ADD] = pfkey_add, |
| 2811 | [SADB_DELETE] = pfkey_delete, |
| 2812 | [SADB_GET] = pfkey_get, |
| 2813 | [SADB_ACQUIRE] = pfkey_acquire, |
| 2814 | [SADB_REGISTER] = pfkey_register, |
| 2815 | [SADB_EXPIRE] = NULL, |
| 2816 | [SADB_FLUSH] = pfkey_flush, |
| 2817 | [SADB_DUMP] = pfkey_dump, |
| 2818 | [SADB_X_PROMISC] = pfkey_promisc, |
| 2819 | [SADB_X_PCHANGE] = NULL, |
| 2820 | [SADB_X_SPDUPDATE] = pfkey_spdadd, |
| 2821 | [SADB_X_SPDADD] = pfkey_spdadd, |
| 2822 | [SADB_X_SPDDELETE] = pfkey_spddelete, |
| 2823 | [SADB_X_SPDGET] = pfkey_spdget, |
| 2824 | [SADB_X_SPDACQUIRE] = NULL, |
| 2825 | [SADB_X_SPDDUMP] = pfkey_spddump, |
| 2826 | [SADB_X_SPDFLUSH] = pfkey_spdflush, |
| 2827 | [SADB_X_SPDSETIDX] = pfkey_spdadd, |
| 2828 | [SADB_X_SPDDELETE2] = pfkey_spdget, |
| 2829 | [SADB_X_MIGRATE] = pfkey_migrate, |
| 2830 | }; |
| 2831 | |
| 2832 | static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr) |
| 2833 | { |
| 2834 | void *ext_hdrs[SADB_EXT_MAX]; |
| 2835 | int err; |
| 2836 | |
| 2837 | pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, |
| 2838 | BROADCAST_PROMISC_ONLY, NULL, sock_net(sk)); |
| 2839 | |
| 2840 | memset(ext_hdrs, 0, sizeof(ext_hdrs)); |
| 2841 | err = parse_exthdrs(skb, hdr, ext_hdrs); |
| 2842 | if (!err) { |
| 2843 | err = -EOPNOTSUPP; |
| 2844 | if (pfkey_funcs[hdr->sadb_msg_type]) |
| 2845 | err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs); |
| 2846 | } |
| 2847 | return err; |
| 2848 | } |
| 2849 | |
| 2850 | static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp) |
| 2851 | { |
| 2852 | struct sadb_msg *hdr = NULL; |
| 2853 | |
| 2854 | if (skb->len < sizeof(*hdr)) { |
| 2855 | *errp = -EMSGSIZE; |
| 2856 | } else { |
| 2857 | hdr = (struct sadb_msg *) skb->data; |
| 2858 | if (hdr->sadb_msg_version != PF_KEY_V2 || |
| 2859 | hdr->sadb_msg_reserved != 0 || |
| 2860 | (hdr->sadb_msg_type <= SADB_RESERVED || |
| 2861 | hdr->sadb_msg_type > SADB_MAX)) { |
| 2862 | hdr = NULL; |
| 2863 | *errp = -EINVAL; |
| 2864 | } else if (hdr->sadb_msg_len != (skb->len / |
| 2865 | sizeof(uint64_t)) || |
| 2866 | hdr->sadb_msg_len < (sizeof(struct sadb_msg) / |
| 2867 | sizeof(uint64_t))) { |
| 2868 | hdr = NULL; |
| 2869 | *errp = -EMSGSIZE; |
| 2870 | } else { |
| 2871 | *errp = 0; |
| 2872 | } |
| 2873 | } |
| 2874 | return hdr; |
| 2875 | } |
| 2876 | |
| 2877 | static inline int aalg_tmpl_set(const struct xfrm_tmpl *t, |
| 2878 | const struct xfrm_algo_desc *d) |
| 2879 | { |
| 2880 | unsigned int id = d->desc.sadb_alg_id; |
| 2881 | |
| 2882 | if (id >= sizeof(t->aalgos) * 8) |
| 2883 | return 0; |
| 2884 | |
| 2885 | return (t->aalgos >> id) & 1; |
| 2886 | } |
| 2887 | |
| 2888 | static inline int ealg_tmpl_set(const struct xfrm_tmpl *t, |
| 2889 | const struct xfrm_algo_desc *d) |
| 2890 | { |
| 2891 | unsigned int id = d->desc.sadb_alg_id; |
| 2892 | |
| 2893 | if (id >= sizeof(t->ealgos) * 8) |
| 2894 | return 0; |
| 2895 | |
| 2896 | return (t->ealgos >> id) & 1; |
| 2897 | } |
| 2898 | |
| 2899 | static int count_ah_combs(const struct xfrm_tmpl *t) |
| 2900 | { |
| 2901 | int i, sz = 0; |
| 2902 | |
| 2903 | for (i = 0; ; i++) { |
| 2904 | const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i); |
| 2905 | if (!aalg) |
| 2906 | break; |
| 2907 | if (!aalg->pfkey_supported) |
| 2908 | continue; |
| 2909 | if (aalg_tmpl_set(t, aalg) && aalg->available) |
| 2910 | sz += sizeof(struct sadb_comb); |
| 2911 | } |
| 2912 | return sz + sizeof(struct sadb_prop); |
| 2913 | } |
| 2914 | |
| 2915 | static int count_esp_combs(const struct xfrm_tmpl *t) |
| 2916 | { |
| 2917 | int i, k, sz = 0; |
| 2918 | |
| 2919 | for (i = 0; ; i++) { |
| 2920 | const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i); |
| 2921 | if (!ealg) |
| 2922 | break; |
| 2923 | |
| 2924 | if (!ealg->pfkey_supported) |
| 2925 | continue; |
| 2926 | |
| 2927 | if (!(ealg_tmpl_set(t, ealg) && ealg->available)) |
| 2928 | continue; |
| 2929 | |
| 2930 | for (k = 1; ; k++) { |
| 2931 | const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k); |
| 2932 | if (!aalg) |
| 2933 | break; |
| 2934 | |
| 2935 | if (!aalg->pfkey_supported) |
| 2936 | continue; |
| 2937 | |
| 2938 | if (aalg_tmpl_set(t, aalg) && aalg->available) |
| 2939 | sz += sizeof(struct sadb_comb); |
| 2940 | } |
| 2941 | } |
| 2942 | return sz + sizeof(struct sadb_prop); |
| 2943 | } |
| 2944 | |
| 2945 | static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t) |
| 2946 | { |
| 2947 | struct sadb_prop *p; |
| 2948 | int i; |
| 2949 | |
| 2950 | p = skb_put(skb, sizeof(struct sadb_prop)); |
| 2951 | p->sadb_prop_len = sizeof(struct sadb_prop)/8; |
| 2952 | p->sadb_prop_exttype = SADB_EXT_PROPOSAL; |
| 2953 | p->sadb_prop_replay = 32; |
| 2954 | memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved)); |
| 2955 | |
| 2956 | for (i = 0; ; i++) { |
| 2957 | const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i); |
| 2958 | if (!aalg) |
| 2959 | break; |
| 2960 | |
| 2961 | if (!aalg->pfkey_supported) |
| 2962 | continue; |
| 2963 | |
| 2964 | if (aalg_tmpl_set(t, aalg) && aalg->available) { |
| 2965 | struct sadb_comb *c; |
| 2966 | c = skb_put_zero(skb, sizeof(struct sadb_comb)); |
| 2967 | p->sadb_prop_len += sizeof(struct sadb_comb)/8; |
| 2968 | c->sadb_comb_auth = aalg->desc.sadb_alg_id; |
| 2969 | c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits; |
| 2970 | c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits; |
| 2971 | c->sadb_comb_hard_addtime = 24*60*60; |
| 2972 | c->sadb_comb_soft_addtime = 20*60*60; |
| 2973 | c->sadb_comb_hard_usetime = 8*60*60; |
| 2974 | c->sadb_comb_soft_usetime = 7*60*60; |
| 2975 | } |
| 2976 | } |
| 2977 | } |
| 2978 | |
| 2979 | static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t) |
| 2980 | { |
| 2981 | struct sadb_prop *p; |
| 2982 | int i, k; |
| 2983 | |
| 2984 | p = skb_put(skb, sizeof(struct sadb_prop)); |
| 2985 | p->sadb_prop_len = sizeof(struct sadb_prop)/8; |
| 2986 | p->sadb_prop_exttype = SADB_EXT_PROPOSAL; |
| 2987 | p->sadb_prop_replay = 32; |
| 2988 | memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved)); |
| 2989 | |
| 2990 | for (i=0; ; i++) { |
| 2991 | const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i); |
| 2992 | if (!ealg) |
| 2993 | break; |
| 2994 | |
| 2995 | if (!ealg->pfkey_supported) |
| 2996 | continue; |
| 2997 | |
| 2998 | if (!(ealg_tmpl_set(t, ealg) && ealg->available)) |
| 2999 | continue; |
| 3000 | |
| 3001 | for (k = 1; ; k++) { |
| 3002 | struct sadb_comb *c; |
| 3003 | const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k); |
| 3004 | if (!aalg) |
| 3005 | break; |
| 3006 | if (!aalg->pfkey_supported) |
| 3007 | continue; |
| 3008 | if (!(aalg_tmpl_set(t, aalg) && aalg->available)) |
| 3009 | continue; |
| 3010 | c = skb_put(skb, sizeof(struct sadb_comb)); |
| 3011 | memset(c, 0, sizeof(*c)); |
| 3012 | p->sadb_prop_len += sizeof(struct sadb_comb)/8; |
| 3013 | c->sadb_comb_auth = aalg->desc.sadb_alg_id; |
| 3014 | c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits; |
| 3015 | c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits; |
| 3016 | c->sadb_comb_encrypt = ealg->desc.sadb_alg_id; |
| 3017 | c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits; |
| 3018 | c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits; |
| 3019 | c->sadb_comb_hard_addtime = 24*60*60; |
| 3020 | c->sadb_comb_soft_addtime = 20*60*60; |
| 3021 | c->sadb_comb_hard_usetime = 8*60*60; |
| 3022 | c->sadb_comb_soft_usetime = 7*60*60; |
| 3023 | } |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c) |
| 3028 | { |
| 3029 | return 0; |
| 3030 | } |
| 3031 | |
| 3032 | static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c) |
| 3033 | { |
| 3034 | struct sk_buff *out_skb; |
| 3035 | struct sadb_msg *out_hdr; |
| 3036 | int hard; |
| 3037 | int hsc; |
| 3038 | |
| 3039 | hard = c->data.hard; |
| 3040 | if (hard) |
| 3041 | hsc = 2; |
| 3042 | else |
| 3043 | hsc = 1; |
| 3044 | |
| 3045 | out_skb = pfkey_xfrm_state2msg_expire(x, hsc); |
| 3046 | if (IS_ERR(out_skb)) |
| 3047 | return PTR_ERR(out_skb); |
| 3048 | |
| 3049 | out_hdr = (struct sadb_msg *) out_skb->data; |
| 3050 | out_hdr->sadb_msg_version = PF_KEY_V2; |
| 3051 | out_hdr->sadb_msg_type = SADB_EXPIRE; |
| 3052 | out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); |
| 3053 | out_hdr->sadb_msg_errno = 0; |
| 3054 | out_hdr->sadb_msg_reserved = 0; |
| 3055 | out_hdr->sadb_msg_seq = 0; |
| 3056 | out_hdr->sadb_msg_pid = 0; |
| 3057 | |
| 3058 | pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, |
| 3059 | xs_net(x)); |
| 3060 | return 0; |
| 3061 | } |
| 3062 | |
| 3063 | static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c) |
| 3064 | { |
| 3065 | struct net *net = x ? xs_net(x) : c->net; |
| 3066 | struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); |
| 3067 | |
| 3068 | if (atomic_read(&net_pfkey->socks_nr) == 0) |
| 3069 | return 0; |
| 3070 | |
| 3071 | switch (c->event) { |
| 3072 | case XFRM_MSG_EXPIRE: |
| 3073 | return key_notify_sa_expire(x, c); |
| 3074 | case XFRM_MSG_DELSA: |
| 3075 | case XFRM_MSG_NEWSA: |
| 3076 | case XFRM_MSG_UPDSA: |
| 3077 | return key_notify_sa(x, c); |
| 3078 | case XFRM_MSG_FLUSHSA: |
| 3079 | return key_notify_sa_flush(c); |
| 3080 | case XFRM_MSG_NEWAE: /* not yet supported */ |
| 3081 | break; |
| 3082 | default: |
| 3083 | pr_err("pfkey: Unknown SA event %d\n", c->event); |
| 3084 | break; |
| 3085 | } |
| 3086 | |
| 3087 | return 0; |
| 3088 | } |
| 3089 | |
| 3090 | static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) |
| 3091 | { |
| 3092 | if (xp && xp->type != XFRM_POLICY_TYPE_MAIN) |
| 3093 | return 0; |
| 3094 | |
| 3095 | switch (c->event) { |
| 3096 | case XFRM_MSG_POLEXPIRE: |
| 3097 | return key_notify_policy_expire(xp, c); |
| 3098 | case XFRM_MSG_DELPOLICY: |
| 3099 | case XFRM_MSG_NEWPOLICY: |
| 3100 | case XFRM_MSG_UPDPOLICY: |
| 3101 | return key_notify_policy(xp, dir, c); |
| 3102 | case XFRM_MSG_FLUSHPOLICY: |
| 3103 | if (c->data.type != XFRM_POLICY_TYPE_MAIN) |
| 3104 | break; |
| 3105 | return key_notify_policy_flush(c); |
| 3106 | default: |
| 3107 | pr_err("pfkey: Unknown policy event %d\n", c->event); |
| 3108 | break; |
| 3109 | } |
| 3110 | |
| 3111 | return 0; |
| 3112 | } |
| 3113 | |
| 3114 | static u32 get_acqseq(void) |
| 3115 | { |
| 3116 | u32 res; |
| 3117 | static atomic_t acqseq; |
| 3118 | |
| 3119 | do { |
| 3120 | res = atomic_inc_return(&acqseq); |
| 3121 | } while (!res); |
| 3122 | return res; |
| 3123 | } |
| 3124 | |
| 3125 | static bool pfkey_is_alive(const struct km_event *c) |
| 3126 | { |
| 3127 | struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id); |
| 3128 | struct sock *sk; |
| 3129 | bool is_alive = false; |
| 3130 | |
| 3131 | rcu_read_lock(); |
| 3132 | sk_for_each_rcu(sk, &net_pfkey->table) { |
| 3133 | if (pfkey_sk(sk)->registered) { |
| 3134 | is_alive = true; |
| 3135 | break; |
| 3136 | } |
| 3137 | } |
| 3138 | rcu_read_unlock(); |
| 3139 | |
| 3140 | return is_alive; |
| 3141 | } |
| 3142 | |
| 3143 | static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp) |
| 3144 | { |
| 3145 | struct sk_buff *skb; |
| 3146 | struct sadb_msg *hdr; |
| 3147 | struct sadb_address *addr; |
| 3148 | struct sadb_x_policy *pol; |
| 3149 | int sockaddr_size; |
| 3150 | int size; |
| 3151 | struct sadb_x_sec_ctx *sec_ctx; |
| 3152 | struct xfrm_sec_ctx *xfrm_ctx; |
| 3153 | int ctx_size = 0; |
| 3154 | |
| 3155 | sockaddr_size = pfkey_sockaddr_size(x->props.family); |
| 3156 | if (!sockaddr_size) |
| 3157 | return -EINVAL; |
| 3158 | |
| 3159 | size = sizeof(struct sadb_msg) + |
| 3160 | (sizeof(struct sadb_address) * 2) + |
| 3161 | (sockaddr_size * 2) + |
| 3162 | sizeof(struct sadb_x_policy); |
| 3163 | |
| 3164 | if (x->id.proto == IPPROTO_AH) |
| 3165 | size += count_ah_combs(t); |
| 3166 | else if (x->id.proto == IPPROTO_ESP) |
| 3167 | size += count_esp_combs(t); |
| 3168 | |
| 3169 | if ((xfrm_ctx = x->security)) { |
| 3170 | ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len); |
| 3171 | size += sizeof(struct sadb_x_sec_ctx) + ctx_size; |
| 3172 | } |
| 3173 | |
| 3174 | skb = alloc_skb(size + 16, GFP_ATOMIC); |
| 3175 | if (skb == NULL) |
| 3176 | return -ENOMEM; |
| 3177 | |
| 3178 | hdr = skb_put(skb, sizeof(struct sadb_msg)); |
| 3179 | hdr->sadb_msg_version = PF_KEY_V2; |
| 3180 | hdr->sadb_msg_type = SADB_ACQUIRE; |
| 3181 | hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto); |
| 3182 | hdr->sadb_msg_len = size / sizeof(uint64_t); |
| 3183 | hdr->sadb_msg_errno = 0; |
| 3184 | hdr->sadb_msg_reserved = 0; |
| 3185 | hdr->sadb_msg_seq = x->km.seq = get_acqseq(); |
| 3186 | hdr->sadb_msg_pid = 0; |
| 3187 | |
| 3188 | /* src address */ |
| 3189 | addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size); |
| 3190 | addr->sadb_address_len = |
| 3191 | (sizeof(struct sadb_address)+sockaddr_size)/ |
| 3192 | sizeof(uint64_t); |
| 3193 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; |
| 3194 | addr->sadb_address_proto = 0; |
| 3195 | addr->sadb_address_reserved = 0; |
| 3196 | addr->sadb_address_prefixlen = |
| 3197 | pfkey_sockaddr_fill(&x->props.saddr, 0, |
| 3198 | (struct sockaddr *) (addr + 1), |
| 3199 | x->props.family); |
| 3200 | if (!addr->sadb_address_prefixlen) |
| 3201 | BUG(); |
| 3202 | |
| 3203 | /* dst address */ |
| 3204 | addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size); |
| 3205 | addr->sadb_address_len = |
| 3206 | (sizeof(struct sadb_address)+sockaddr_size)/ |
| 3207 | sizeof(uint64_t); |
| 3208 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; |
| 3209 | addr->sadb_address_proto = 0; |
| 3210 | addr->sadb_address_reserved = 0; |
| 3211 | addr->sadb_address_prefixlen = |
| 3212 | pfkey_sockaddr_fill(&x->id.daddr, 0, |
| 3213 | (struct sockaddr *) (addr + 1), |
| 3214 | x->props.family); |
| 3215 | if (!addr->sadb_address_prefixlen) |
| 3216 | BUG(); |
| 3217 | |
| 3218 | pol = skb_put(skb, sizeof(struct sadb_x_policy)); |
| 3219 | pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t); |
| 3220 | pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY; |
| 3221 | pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC; |
| 3222 | pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1; |
| 3223 | pol->sadb_x_policy_reserved = 0; |
| 3224 | pol->sadb_x_policy_id = xp->index; |
| 3225 | pol->sadb_x_policy_priority = xp->priority; |
| 3226 | |
| 3227 | /* Set sadb_comb's. */ |
| 3228 | if (x->id.proto == IPPROTO_AH) |
| 3229 | dump_ah_combs(skb, t); |
| 3230 | else if (x->id.proto == IPPROTO_ESP) |
| 3231 | dump_esp_combs(skb, t); |
| 3232 | |
| 3233 | /* security context */ |
| 3234 | if (xfrm_ctx) { |
| 3235 | sec_ctx = skb_put(skb, |
| 3236 | sizeof(struct sadb_x_sec_ctx) + ctx_size); |
| 3237 | sec_ctx->sadb_x_sec_len = |
| 3238 | (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t); |
| 3239 | sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX; |
| 3240 | sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi; |
| 3241 | sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg; |
| 3242 | sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len; |
| 3243 | memcpy(sec_ctx + 1, xfrm_ctx->ctx_str, |
| 3244 | xfrm_ctx->ctx_len); |
| 3245 | } |
| 3246 | |
| 3247 | return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, |
| 3248 | xs_net(x)); |
| 3249 | } |
| 3250 | |
| 3251 | static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt, |
| 3252 | u8 *data, int len, int *dir) |
| 3253 | { |
| 3254 | struct net *net = sock_net(sk); |
| 3255 | struct xfrm_policy *xp; |
| 3256 | struct sadb_x_policy *pol = (struct sadb_x_policy*)data; |
| 3257 | struct sadb_x_sec_ctx *sec_ctx; |
| 3258 | |
| 3259 | switch (sk->sk_family) { |
| 3260 | case AF_INET: |
| 3261 | if (opt != IP_IPSEC_POLICY) { |
| 3262 | *dir = -EOPNOTSUPP; |
| 3263 | return NULL; |
| 3264 | } |
| 3265 | break; |
| 3266 | #if IS_ENABLED(CONFIG_IPV6) |
| 3267 | case AF_INET6: |
| 3268 | if (opt != IPV6_IPSEC_POLICY) { |
| 3269 | *dir = -EOPNOTSUPP; |
| 3270 | return NULL; |
| 3271 | } |
| 3272 | break; |
| 3273 | #endif |
| 3274 | default: |
| 3275 | *dir = -EINVAL; |
| 3276 | return NULL; |
| 3277 | } |
| 3278 | |
| 3279 | *dir = -EINVAL; |
| 3280 | |
| 3281 | if (len < sizeof(struct sadb_x_policy) || |
| 3282 | pol->sadb_x_policy_len*8 > len || |
| 3283 | pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS || |
| 3284 | (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND)) |
| 3285 | return NULL; |
| 3286 | |
| 3287 | xp = xfrm_policy_alloc(net, GFP_ATOMIC); |
| 3288 | if (xp == NULL) { |
| 3289 | *dir = -ENOBUFS; |
| 3290 | return NULL; |
| 3291 | } |
| 3292 | |
| 3293 | xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ? |
| 3294 | XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW); |
| 3295 | |
| 3296 | xp->lft.soft_byte_limit = XFRM_INF; |
| 3297 | xp->lft.hard_byte_limit = XFRM_INF; |
| 3298 | xp->lft.soft_packet_limit = XFRM_INF; |
| 3299 | xp->lft.hard_packet_limit = XFRM_INF; |
| 3300 | xp->family = sk->sk_family; |
| 3301 | |
| 3302 | xp->xfrm_nr = 0; |
| 3303 | if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC && |
| 3304 | (*dir = parse_ipsecrequests(xp, pol)) < 0) |
| 3305 | goto out; |
| 3306 | |
| 3307 | /* security context too */ |
| 3308 | if (len >= (pol->sadb_x_policy_len*8 + |
| 3309 | sizeof(struct sadb_x_sec_ctx))) { |
| 3310 | char *p = (char *)pol; |
| 3311 | struct xfrm_user_sec_ctx *uctx; |
| 3312 | |
| 3313 | p += pol->sadb_x_policy_len*8; |
| 3314 | sec_ctx = (struct sadb_x_sec_ctx *)p; |
| 3315 | if (len < pol->sadb_x_policy_len*8 + |
| 3316 | sec_ctx->sadb_x_sec_len*8) { |
| 3317 | *dir = -EINVAL; |
| 3318 | goto out; |
| 3319 | } |
| 3320 | if ((*dir = verify_sec_ctx_len(p))) |
| 3321 | goto out; |
| 3322 | uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC); |
| 3323 | *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC); |
| 3324 | kfree(uctx); |
| 3325 | |
| 3326 | if (*dir) |
| 3327 | goto out; |
| 3328 | } |
| 3329 | |
| 3330 | *dir = pol->sadb_x_policy_dir-1; |
| 3331 | return xp; |
| 3332 | |
| 3333 | out: |
| 3334 | xp->walk.dead = 1; |
| 3335 | xfrm_policy_destroy(xp); |
| 3336 | return NULL; |
| 3337 | } |
| 3338 | |
| 3339 | static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport) |
| 3340 | { |
| 3341 | struct sk_buff *skb; |
| 3342 | struct sadb_msg *hdr; |
| 3343 | struct sadb_sa *sa; |
| 3344 | struct sadb_address *addr; |
| 3345 | struct sadb_x_nat_t_port *n_port; |
| 3346 | int sockaddr_size; |
| 3347 | int size; |
| 3348 | __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0); |
| 3349 | struct xfrm_encap_tmpl *natt = NULL; |
| 3350 | |
| 3351 | sockaddr_size = pfkey_sockaddr_size(x->props.family); |
| 3352 | if (!sockaddr_size) |
| 3353 | return -EINVAL; |
| 3354 | |
| 3355 | if (!satype) |
| 3356 | return -EINVAL; |
| 3357 | |
| 3358 | if (!x->encap) |
| 3359 | return -EINVAL; |
| 3360 | |
| 3361 | natt = x->encap; |
| 3362 | |
| 3363 | /* Build an SADB_X_NAT_T_NEW_MAPPING message: |
| 3364 | * |
| 3365 | * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) | |
| 3366 | * ADDRESS_DST (new addr) | NAT_T_DPORT (new port) |
| 3367 | */ |
| 3368 | |
| 3369 | size = sizeof(struct sadb_msg) + |
| 3370 | sizeof(struct sadb_sa) + |
| 3371 | (sizeof(struct sadb_address) * 2) + |
| 3372 | (sockaddr_size * 2) + |
| 3373 | (sizeof(struct sadb_x_nat_t_port) * 2); |
| 3374 | |
| 3375 | skb = alloc_skb(size + 16, GFP_ATOMIC); |
| 3376 | if (skb == NULL) |
| 3377 | return -ENOMEM; |
| 3378 | |
| 3379 | hdr = skb_put(skb, sizeof(struct sadb_msg)); |
| 3380 | hdr->sadb_msg_version = PF_KEY_V2; |
| 3381 | hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING; |
| 3382 | hdr->sadb_msg_satype = satype; |
| 3383 | hdr->sadb_msg_len = size / sizeof(uint64_t); |
| 3384 | hdr->sadb_msg_errno = 0; |
| 3385 | hdr->sadb_msg_reserved = 0; |
| 3386 | hdr->sadb_msg_seq = x->km.seq = get_acqseq(); |
| 3387 | hdr->sadb_msg_pid = 0; |
| 3388 | |
| 3389 | /* SA */ |
| 3390 | sa = skb_put(skb, sizeof(struct sadb_sa)); |
| 3391 | sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t); |
| 3392 | sa->sadb_sa_exttype = SADB_EXT_SA; |
| 3393 | sa->sadb_sa_spi = x->id.spi; |
| 3394 | sa->sadb_sa_replay = 0; |
| 3395 | sa->sadb_sa_state = 0; |
| 3396 | sa->sadb_sa_auth = 0; |
| 3397 | sa->sadb_sa_encrypt = 0; |
| 3398 | sa->sadb_sa_flags = 0; |
| 3399 | |
| 3400 | /* ADDRESS_SRC (old addr) */ |
| 3401 | addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size); |
| 3402 | addr->sadb_address_len = |
| 3403 | (sizeof(struct sadb_address)+sockaddr_size)/ |
| 3404 | sizeof(uint64_t); |
| 3405 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC; |
| 3406 | addr->sadb_address_proto = 0; |
| 3407 | addr->sadb_address_reserved = 0; |
| 3408 | addr->sadb_address_prefixlen = |
| 3409 | pfkey_sockaddr_fill(&x->props.saddr, 0, |
| 3410 | (struct sockaddr *) (addr + 1), |
| 3411 | x->props.family); |
| 3412 | if (!addr->sadb_address_prefixlen) |
| 3413 | BUG(); |
| 3414 | |
| 3415 | /* NAT_T_SPORT (old port) */ |
| 3416 | n_port = skb_put(skb, sizeof(*n_port)); |
| 3417 | n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); |
| 3418 | n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT; |
| 3419 | n_port->sadb_x_nat_t_port_port = natt->encap_sport; |
| 3420 | n_port->sadb_x_nat_t_port_reserved = 0; |
| 3421 | |
| 3422 | /* ADDRESS_DST (new addr) */ |
| 3423 | addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size); |
| 3424 | addr->sadb_address_len = |
| 3425 | (sizeof(struct sadb_address)+sockaddr_size)/ |
| 3426 | sizeof(uint64_t); |
| 3427 | addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST; |
| 3428 | addr->sadb_address_proto = 0; |
| 3429 | addr->sadb_address_reserved = 0; |
| 3430 | addr->sadb_address_prefixlen = |
| 3431 | pfkey_sockaddr_fill(ipaddr, 0, |
| 3432 | (struct sockaddr *) (addr + 1), |
| 3433 | x->props.family); |
| 3434 | if (!addr->sadb_address_prefixlen) |
| 3435 | BUG(); |
| 3436 | |
| 3437 | /* NAT_T_DPORT (new port) */ |
| 3438 | n_port = skb_put(skb, sizeof(*n_port)); |
| 3439 | n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t); |
| 3440 | n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT; |
| 3441 | n_port->sadb_x_nat_t_port_port = sport; |
| 3442 | n_port->sadb_x_nat_t_port_reserved = 0; |
| 3443 | |
| 3444 | return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, |
| 3445 | xs_net(x)); |
| 3446 | } |
| 3447 | |
| 3448 | #ifdef CONFIG_NET_KEY_MIGRATE |
| 3449 | static int set_sadb_address(struct sk_buff *skb, int sasize, int type, |
| 3450 | const struct xfrm_selector *sel) |
| 3451 | { |
| 3452 | struct sadb_address *addr; |
| 3453 | addr = skb_put(skb, sizeof(struct sadb_address) + sasize); |
| 3454 | addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8; |
| 3455 | addr->sadb_address_exttype = type; |
| 3456 | addr->sadb_address_proto = sel->proto; |
| 3457 | addr->sadb_address_reserved = 0; |
| 3458 | |
| 3459 | switch (type) { |
| 3460 | case SADB_EXT_ADDRESS_SRC: |
| 3461 | addr->sadb_address_prefixlen = sel->prefixlen_s; |
| 3462 | pfkey_sockaddr_fill(&sel->saddr, 0, |
| 3463 | (struct sockaddr *)(addr + 1), |
| 3464 | sel->family); |
| 3465 | break; |
| 3466 | case SADB_EXT_ADDRESS_DST: |
| 3467 | addr->sadb_address_prefixlen = sel->prefixlen_d; |
| 3468 | pfkey_sockaddr_fill(&sel->daddr, 0, |
| 3469 | (struct sockaddr *)(addr + 1), |
| 3470 | sel->family); |
| 3471 | break; |
| 3472 | default: |
| 3473 | return -EINVAL; |
| 3474 | } |
| 3475 | |
| 3476 | return 0; |
| 3477 | } |
| 3478 | |
| 3479 | |
| 3480 | static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k) |
| 3481 | { |
| 3482 | struct sadb_x_kmaddress *kma; |
| 3483 | u8 *sa; |
| 3484 | int family = k->family; |
| 3485 | int socklen = pfkey_sockaddr_len(family); |
| 3486 | int size_req; |
| 3487 | |
| 3488 | size_req = (sizeof(struct sadb_x_kmaddress) + |
| 3489 | pfkey_sockaddr_pair_size(family)); |
| 3490 | |
| 3491 | kma = skb_put_zero(skb, size_req); |
| 3492 | kma->sadb_x_kmaddress_len = size_req / 8; |
| 3493 | kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS; |
| 3494 | kma->sadb_x_kmaddress_reserved = k->reserved; |
| 3495 | |
| 3496 | sa = (u8 *)(kma + 1); |
| 3497 | if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) || |
| 3498 | !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family)) |
| 3499 | return -EINVAL; |
| 3500 | |
| 3501 | return 0; |
| 3502 | } |
| 3503 | |
| 3504 | static int set_ipsecrequest(struct sk_buff *skb, |
| 3505 | uint8_t proto, uint8_t mode, int level, |
| 3506 | uint32_t reqid, uint8_t family, |
| 3507 | const xfrm_address_t *src, const xfrm_address_t *dst) |
| 3508 | { |
| 3509 | struct sadb_x_ipsecrequest *rq; |
| 3510 | u8 *sa; |
| 3511 | int socklen = pfkey_sockaddr_len(family); |
| 3512 | int size_req; |
| 3513 | |
| 3514 | size_req = sizeof(struct sadb_x_ipsecrequest) + |
| 3515 | pfkey_sockaddr_pair_size(family); |
| 3516 | |
| 3517 | rq = skb_put_zero(skb, size_req); |
| 3518 | rq->sadb_x_ipsecrequest_len = size_req; |
| 3519 | rq->sadb_x_ipsecrequest_proto = proto; |
| 3520 | rq->sadb_x_ipsecrequest_mode = mode; |
| 3521 | rq->sadb_x_ipsecrequest_level = level; |
| 3522 | rq->sadb_x_ipsecrequest_reqid = reqid; |
| 3523 | |
| 3524 | sa = (u8 *) (rq + 1); |
| 3525 | if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) || |
| 3526 | !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family)) |
| 3527 | return -EINVAL; |
| 3528 | |
| 3529 | return 0; |
| 3530 | } |
| 3531 | #endif |
| 3532 | |
| 3533 | #ifdef CONFIG_NET_KEY_MIGRATE |
| 3534 | static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, |
| 3535 | const struct xfrm_migrate *m, int num_bundles, |
| 3536 | const struct xfrm_kmaddress *k, |
| 3537 | const struct xfrm_encap_tmpl *encap) |
| 3538 | { |
| 3539 | int i; |
| 3540 | int sasize_sel; |
| 3541 | int size = 0; |
| 3542 | int size_pol = 0; |
| 3543 | struct sk_buff *skb; |
| 3544 | struct sadb_msg *hdr; |
| 3545 | struct sadb_x_policy *pol; |
| 3546 | const struct xfrm_migrate *mp; |
| 3547 | |
| 3548 | if (type != XFRM_POLICY_TYPE_MAIN) |
| 3549 | return 0; |
| 3550 | |
| 3551 | if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH) |
| 3552 | return -EINVAL; |
| 3553 | |
| 3554 | if (k != NULL) { |
| 3555 | /* addresses for KM */ |
| 3556 | size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) + |
| 3557 | pfkey_sockaddr_pair_size(k->family)); |
| 3558 | } |
| 3559 | |
| 3560 | /* selector */ |
| 3561 | sasize_sel = pfkey_sockaddr_size(sel->family); |
| 3562 | if (!sasize_sel) |
| 3563 | return -EINVAL; |
| 3564 | size += (sizeof(struct sadb_address) + sasize_sel) * 2; |
| 3565 | |
| 3566 | /* policy info */ |
| 3567 | size_pol += sizeof(struct sadb_x_policy); |
| 3568 | |
| 3569 | /* ipsecrequests */ |
| 3570 | for (i = 0, mp = m; i < num_bundles; i++, mp++) { |
| 3571 | /* old locator pair */ |
| 3572 | size_pol += sizeof(struct sadb_x_ipsecrequest) + |
| 3573 | pfkey_sockaddr_pair_size(mp->old_family); |
| 3574 | /* new locator pair */ |
| 3575 | size_pol += sizeof(struct sadb_x_ipsecrequest) + |
| 3576 | pfkey_sockaddr_pair_size(mp->new_family); |
| 3577 | } |
| 3578 | |
| 3579 | size += sizeof(struct sadb_msg) + size_pol; |
| 3580 | |
| 3581 | /* alloc buffer */ |
| 3582 | skb = alloc_skb(size, GFP_ATOMIC); |
| 3583 | if (skb == NULL) |
| 3584 | return -ENOMEM; |
| 3585 | |
| 3586 | hdr = skb_put(skb, sizeof(struct sadb_msg)); |
| 3587 | hdr->sadb_msg_version = PF_KEY_V2; |
| 3588 | hdr->sadb_msg_type = SADB_X_MIGRATE; |
| 3589 | hdr->sadb_msg_satype = pfkey_proto2satype(m->proto); |
| 3590 | hdr->sadb_msg_len = size / 8; |
| 3591 | hdr->sadb_msg_errno = 0; |
| 3592 | hdr->sadb_msg_reserved = 0; |
| 3593 | hdr->sadb_msg_seq = 0; |
| 3594 | hdr->sadb_msg_pid = 0; |
| 3595 | |
| 3596 | /* Addresses to be used by KM for negotiation, if ext is available */ |
| 3597 | if (k != NULL && (set_sadb_kmaddress(skb, k) < 0)) |
| 3598 | goto err; |
| 3599 | |
| 3600 | /* selector src */ |
| 3601 | set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel); |
| 3602 | |
| 3603 | /* selector dst */ |
| 3604 | set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel); |
| 3605 | |
| 3606 | /* policy information */ |
| 3607 | pol = skb_put(skb, sizeof(struct sadb_x_policy)); |
| 3608 | pol->sadb_x_policy_len = size_pol / 8; |
| 3609 | pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY; |
| 3610 | pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC; |
| 3611 | pol->sadb_x_policy_dir = dir + 1; |
| 3612 | pol->sadb_x_policy_reserved = 0; |
| 3613 | pol->sadb_x_policy_id = 0; |
| 3614 | pol->sadb_x_policy_priority = 0; |
| 3615 | |
| 3616 | for (i = 0, mp = m; i < num_bundles; i++, mp++) { |
| 3617 | /* old ipsecrequest */ |
| 3618 | int mode = pfkey_mode_from_xfrm(mp->mode); |
| 3619 | if (mode < 0) |
| 3620 | goto err; |
| 3621 | if (set_ipsecrequest(skb, mp->proto, mode, |
| 3622 | (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE), |
| 3623 | mp->reqid, mp->old_family, |
| 3624 | &mp->old_saddr, &mp->old_daddr) < 0) |
| 3625 | goto err; |
| 3626 | |
| 3627 | /* new ipsecrequest */ |
| 3628 | if (set_ipsecrequest(skb, mp->proto, mode, |
| 3629 | (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE), |
| 3630 | mp->reqid, mp->new_family, |
| 3631 | &mp->new_saddr, &mp->new_daddr) < 0) |
| 3632 | goto err; |
| 3633 | } |
| 3634 | |
| 3635 | /* broadcast migrate message to sockets */ |
| 3636 | pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net); |
| 3637 | |
| 3638 | return 0; |
| 3639 | |
| 3640 | err: |
| 3641 | kfree_skb(skb); |
| 3642 | return -EINVAL; |
| 3643 | } |
| 3644 | #else |
| 3645 | static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, |
| 3646 | const struct xfrm_migrate *m, int num_bundles, |
| 3647 | const struct xfrm_kmaddress *k, |
| 3648 | const struct xfrm_encap_tmpl *encap) |
| 3649 | { |
| 3650 | return -ENOPROTOOPT; |
| 3651 | } |
| 3652 | #endif |
| 3653 | |
| 3654 | static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
| 3655 | { |
| 3656 | struct sock *sk = sock->sk; |
| 3657 | struct sk_buff *skb = NULL; |
| 3658 | struct sadb_msg *hdr = NULL; |
| 3659 | int err; |
| 3660 | struct net *net = sock_net(sk); |
| 3661 | |
| 3662 | err = -EOPNOTSUPP; |
| 3663 | if (msg->msg_flags & MSG_OOB) |
| 3664 | goto out; |
| 3665 | |
| 3666 | err = -EMSGSIZE; |
| 3667 | if ((unsigned int)len > sk->sk_sndbuf - 32) |
| 3668 | goto out; |
| 3669 | |
| 3670 | err = -ENOBUFS; |
| 3671 | skb = alloc_skb(len, GFP_KERNEL); |
| 3672 | if (skb == NULL) |
| 3673 | goto out; |
| 3674 | |
| 3675 | err = -EFAULT; |
| 3676 | if (memcpy_from_msg(skb_put(skb,len), msg, len)) |
| 3677 | goto out; |
| 3678 | |
| 3679 | hdr = pfkey_get_base_msg(skb, &err); |
| 3680 | if (!hdr) |
| 3681 | goto out; |
| 3682 | |
| 3683 | mutex_lock(&net->xfrm.xfrm_cfg_mutex); |
| 3684 | err = pfkey_process(sk, skb, hdr); |
| 3685 | mutex_unlock(&net->xfrm.xfrm_cfg_mutex); |
| 3686 | |
| 3687 | out: |
| 3688 | if (err && hdr && pfkey_error(hdr, err, sk) == 0) |
| 3689 | err = 0; |
| 3690 | kfree_skb(skb); |
| 3691 | |
| 3692 | return err ? : len; |
| 3693 | } |
| 3694 | |
| 3695 | static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 3696 | int flags) |
| 3697 | { |
| 3698 | struct sock *sk = sock->sk; |
| 3699 | struct pfkey_sock *pfk = pfkey_sk(sk); |
| 3700 | struct sk_buff *skb; |
| 3701 | int copied, err; |
| 3702 | |
| 3703 | err = -EINVAL; |
| 3704 | if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT)) |
| 3705 | goto out; |
| 3706 | |
| 3707 | skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err); |
| 3708 | if (skb == NULL) |
| 3709 | goto out; |
| 3710 | |
| 3711 | copied = skb->len; |
| 3712 | if (copied > len) { |
| 3713 | msg->msg_flags |= MSG_TRUNC; |
| 3714 | copied = len; |
| 3715 | } |
| 3716 | |
| 3717 | skb_reset_transport_header(skb); |
| 3718 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
| 3719 | if (err) |
| 3720 | goto out_free; |
| 3721 | |
| 3722 | sock_recv_ts_and_drops(msg, sk, skb); |
| 3723 | |
| 3724 | err = (flags & MSG_TRUNC) ? skb->len : copied; |
| 3725 | |
| 3726 | if (pfk->dump.dump != NULL && |
| 3727 | 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) |
| 3728 | pfkey_do_dump(pfk); |
| 3729 | |
| 3730 | out_free: |
| 3731 | skb_free_datagram(sk, skb); |
| 3732 | out: |
| 3733 | return err; |
| 3734 | } |
| 3735 | |
| 3736 | static const struct proto_ops pfkey_ops = { |
| 3737 | .family = PF_KEY, |
| 3738 | .owner = THIS_MODULE, |
| 3739 | /* Operations that make no sense on pfkey sockets. */ |
| 3740 | .bind = sock_no_bind, |
| 3741 | .connect = sock_no_connect, |
| 3742 | .socketpair = sock_no_socketpair, |
| 3743 | .accept = sock_no_accept, |
| 3744 | .getname = sock_no_getname, |
| 3745 | .ioctl = sock_no_ioctl, |
| 3746 | .listen = sock_no_listen, |
| 3747 | .shutdown = sock_no_shutdown, |
| 3748 | .setsockopt = sock_no_setsockopt, |
| 3749 | .getsockopt = sock_no_getsockopt, |
| 3750 | .mmap = sock_no_mmap, |
| 3751 | .sendpage = sock_no_sendpage, |
| 3752 | |
| 3753 | /* Now the operations that really occur. */ |
| 3754 | .release = pfkey_release, |
| 3755 | .poll = datagram_poll, |
| 3756 | .sendmsg = pfkey_sendmsg, |
| 3757 | .recvmsg = pfkey_recvmsg, |
| 3758 | }; |
| 3759 | |
| 3760 | static const struct net_proto_family pfkey_family_ops = { |
| 3761 | .family = PF_KEY, |
| 3762 | .create = pfkey_create, |
| 3763 | .owner = THIS_MODULE, |
| 3764 | }; |
| 3765 | |
| 3766 | #ifdef CONFIG_PROC_FS |
| 3767 | static int pfkey_seq_show(struct seq_file *f, void *v) |
| 3768 | { |
| 3769 | struct sock *s = sk_entry(v); |
| 3770 | |
| 3771 | if (v == SEQ_START_TOKEN) |
| 3772 | seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n"); |
| 3773 | else |
| 3774 | seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n", |
| 3775 | s, |
| 3776 | refcount_read(&s->sk_refcnt), |
| 3777 | sk_rmem_alloc_get(s), |
| 3778 | sk_wmem_alloc_get(s), |
| 3779 | from_kuid_munged(seq_user_ns(f), sock_i_uid(s)), |
| 3780 | sock_i_ino(s) |
| 3781 | ); |
| 3782 | return 0; |
| 3783 | } |
| 3784 | |
| 3785 | static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos) |
| 3786 | __acquires(rcu) |
| 3787 | { |
| 3788 | struct net *net = seq_file_net(f); |
| 3789 | struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); |
| 3790 | |
| 3791 | rcu_read_lock(); |
| 3792 | return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos); |
| 3793 | } |
| 3794 | |
| 3795 | static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos) |
| 3796 | { |
| 3797 | struct net *net = seq_file_net(f); |
| 3798 | struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); |
| 3799 | |
| 3800 | return seq_hlist_next_rcu(v, &net_pfkey->table, ppos); |
| 3801 | } |
| 3802 | |
| 3803 | static void pfkey_seq_stop(struct seq_file *f, void *v) |
| 3804 | __releases(rcu) |
| 3805 | { |
| 3806 | rcu_read_unlock(); |
| 3807 | } |
| 3808 | |
| 3809 | static const struct seq_operations pfkey_seq_ops = { |
| 3810 | .start = pfkey_seq_start, |
| 3811 | .next = pfkey_seq_next, |
| 3812 | .stop = pfkey_seq_stop, |
| 3813 | .show = pfkey_seq_show, |
| 3814 | }; |
| 3815 | |
| 3816 | static int pfkey_seq_open(struct inode *inode, struct file *file) |
| 3817 | { |
| 3818 | return seq_open_net(inode, file, &pfkey_seq_ops, |
| 3819 | sizeof(struct seq_net_private)); |
| 3820 | } |
| 3821 | |
| 3822 | static const struct file_operations pfkey_proc_ops = { |
| 3823 | .open = pfkey_seq_open, |
| 3824 | .read = seq_read, |
| 3825 | .llseek = seq_lseek, |
| 3826 | .release = seq_release_net, |
| 3827 | }; |
| 3828 | |
| 3829 | static int __net_init pfkey_init_proc(struct net *net) |
| 3830 | { |
| 3831 | struct proc_dir_entry *e; |
| 3832 | |
| 3833 | e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops); |
| 3834 | if (e == NULL) |
| 3835 | return -ENOMEM; |
| 3836 | |
| 3837 | return 0; |
| 3838 | } |
| 3839 | |
| 3840 | static void __net_exit pfkey_exit_proc(struct net *net) |
| 3841 | { |
| 3842 | remove_proc_entry("pfkey", net->proc_net); |
| 3843 | } |
| 3844 | #else |
| 3845 | static inline int pfkey_init_proc(struct net *net) |
| 3846 | { |
| 3847 | return 0; |
| 3848 | } |
| 3849 | |
| 3850 | static inline void pfkey_exit_proc(struct net *net) |
| 3851 | { |
| 3852 | } |
| 3853 | #endif |
| 3854 | |
| 3855 | static struct xfrm_mgr pfkeyv2_mgr = |
| 3856 | { |
| 3857 | .notify = pfkey_send_notify, |
| 3858 | .acquire = pfkey_send_acquire, |
| 3859 | .compile_policy = pfkey_compile_policy, |
| 3860 | .new_mapping = pfkey_send_new_mapping, |
| 3861 | .notify_policy = pfkey_send_policy_notify, |
| 3862 | .migrate = pfkey_send_migrate, |
| 3863 | .is_alive = pfkey_is_alive, |
| 3864 | }; |
| 3865 | |
| 3866 | static int __net_init pfkey_net_init(struct net *net) |
| 3867 | { |
| 3868 | struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); |
| 3869 | int rv; |
| 3870 | |
| 3871 | INIT_HLIST_HEAD(&net_pfkey->table); |
| 3872 | atomic_set(&net_pfkey->socks_nr, 0); |
| 3873 | |
| 3874 | rv = pfkey_init_proc(net); |
| 3875 | |
| 3876 | return rv; |
| 3877 | } |
| 3878 | |
| 3879 | static void __net_exit pfkey_net_exit(struct net *net) |
| 3880 | { |
| 3881 | struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); |
| 3882 | |
| 3883 | pfkey_exit_proc(net); |
| 3884 | BUG_ON(!hlist_empty(&net_pfkey->table)); |
| 3885 | } |
| 3886 | |
| 3887 | static struct pernet_operations pfkey_net_ops = { |
| 3888 | .init = pfkey_net_init, |
| 3889 | .exit = pfkey_net_exit, |
| 3890 | .id = &pfkey_net_id, |
| 3891 | .size = sizeof(struct netns_pfkey), |
| 3892 | }; |
| 3893 | |
| 3894 | static void __exit ipsec_pfkey_exit(void) |
| 3895 | { |
| 3896 | xfrm_unregister_km(&pfkeyv2_mgr); |
| 3897 | sock_unregister(PF_KEY); |
| 3898 | unregister_pernet_subsys(&pfkey_net_ops); |
| 3899 | proto_unregister(&key_proto); |
| 3900 | } |
| 3901 | |
| 3902 | static int __init ipsec_pfkey_init(void) |
| 3903 | { |
| 3904 | int err = proto_register(&key_proto, 0); |
| 3905 | |
| 3906 | if (err != 0) |
| 3907 | goto out; |
| 3908 | |
| 3909 | err = register_pernet_subsys(&pfkey_net_ops); |
| 3910 | if (err != 0) |
| 3911 | goto out_unregister_key_proto; |
| 3912 | err = sock_register(&pfkey_family_ops); |
| 3913 | if (err != 0) |
| 3914 | goto out_unregister_pernet; |
| 3915 | err = xfrm_register_km(&pfkeyv2_mgr); |
| 3916 | if (err != 0) |
| 3917 | goto out_sock_unregister; |
| 3918 | out: |
| 3919 | return err; |
| 3920 | |
| 3921 | out_sock_unregister: |
| 3922 | sock_unregister(PF_KEY); |
| 3923 | out_unregister_pernet: |
| 3924 | unregister_pernet_subsys(&pfkey_net_ops); |
| 3925 | out_unregister_key_proto: |
| 3926 | proto_unregister(&key_proto); |
| 3927 | goto out; |
| 3928 | } |
| 3929 | |
| 3930 | module_init(ipsec_pfkey_init); |
| 3931 | module_exit(ipsec_pfkey_exit); |
| 3932 | MODULE_LICENSE("GPL"); |
| 3933 | MODULE_ALIAS_NETPROTO(PF_KEY); |