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