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