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