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