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