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