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