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