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