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