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