1 // SPDX-License-Identifier: GPL-2.0-only
2 /* xfrm_user.c: User interface to configure xfrm engine.
3 *
4 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5 *
6 * Changes:
7 * Mitsuru KANDA @USAGI
8 * Kazunori MIYAZAWA @USAGI
9 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10 * IPv6 support
11 *
12 */
13
14 #include <linux/compat.h>
15 #include <linux/crypto.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/slab.h>
20 #include <linux/socket.h>
21 #include <linux/string.h>
22 #include <linux/net.h>
23 #include <linux/skbuff.h>
24 #include <linux/pfkeyv2.h>
25 #include <linux/ipsec.h>
26 #include <linux/init.h>
27 #include <linux/security.h>
28 #include <net/sock.h>
29 #include <net/xfrm.h>
30 #include <net/netlink.h>
31 #include <net/ah.h>
32 #include <linux/uaccess.h>
33 #if IS_ENABLED(CONFIG_IPV6)
34 #include <linux/in6.h>
35 #endif
36 #include <linux/unaligned.h>
37
xfrm_net_nlsk(const struct net * net,const struct sk_buff * skb)38 static struct sock *xfrm_net_nlsk(const struct net *net, const struct sk_buff *skb)
39 {
40 /* get the source of this request, see netlink_unicast_kernel */
41 const struct sock *sk = NETLINK_CB(skb).sk;
42
43 /* sk is refcounted, the netns stays alive and nlsk with it */
44 return rcu_dereference_protected(net->xfrm.nlsk, sk->sk_net_refcnt);
45 }
46
verify_one_alg(struct nlattr ** attrs,enum xfrm_attr_type_t type,struct netlink_ext_ack * extack)47 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type,
48 struct netlink_ext_ack *extack)
49 {
50 struct nlattr *rt = attrs[type];
51 struct xfrm_algo *algp;
52
53 if (!rt)
54 return 0;
55
56 algp = nla_data(rt);
57 if (nla_len(rt) < (int)xfrm_alg_len(algp)) {
58 NL_SET_ERR_MSG(extack, "Invalid AUTH/CRYPT/COMP attribute length");
59 return -EINVAL;
60 }
61
62 switch (type) {
63 case XFRMA_ALG_AUTH:
64 case XFRMA_ALG_CRYPT:
65 case XFRMA_ALG_COMP:
66 break;
67
68 default:
69 NL_SET_ERR_MSG(extack, "Invalid algorithm attribute type");
70 return -EINVAL;
71 }
72
73 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
74 return 0;
75 }
76
verify_auth_trunc(struct nlattr ** attrs,struct netlink_ext_ack * extack)77 static int verify_auth_trunc(struct nlattr **attrs,
78 struct netlink_ext_ack *extack)
79 {
80 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
81 struct xfrm_algo_auth *algp;
82
83 if (!rt)
84 return 0;
85
86 algp = nla_data(rt);
87 if (nla_len(rt) < (int)xfrm_alg_auth_len(algp)) {
88 NL_SET_ERR_MSG(extack, "Invalid AUTH_TRUNC attribute length");
89 return -EINVAL;
90 }
91
92 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
93 return 0;
94 }
95
verify_aead(struct nlattr ** attrs,struct netlink_ext_ack * extack)96 static int verify_aead(struct nlattr **attrs, struct netlink_ext_ack *extack)
97 {
98 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
99 struct xfrm_algo_aead *algp;
100
101 if (!rt)
102 return 0;
103
104 algp = nla_data(rt);
105 if (nla_len(rt) < (int)aead_len(algp)) {
106 NL_SET_ERR_MSG(extack, "Invalid AEAD attribute length");
107 return -EINVAL;
108 }
109
110 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
111 return 0;
112 }
113
verify_one_addr(struct nlattr ** attrs,enum xfrm_attr_type_t type,xfrm_address_t ** addrp)114 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
115 xfrm_address_t **addrp)
116 {
117 struct nlattr *rt = attrs[type];
118
119 if (rt && addrp)
120 *addrp = nla_data(rt);
121 }
122
verify_sec_ctx_len(struct nlattr ** attrs,struct netlink_ext_ack * extack)123 static inline int verify_sec_ctx_len(struct nlattr **attrs, struct netlink_ext_ack *extack)
124 {
125 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
126 struct xfrm_user_sec_ctx *uctx;
127
128 if (!rt)
129 return 0;
130
131 uctx = nla_data(rt);
132 if (uctx->len > nla_len(rt) ||
133 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) {
134 NL_SET_ERR_MSG(extack, "Invalid security context length");
135 return -EINVAL;
136 }
137
138 return 0;
139 }
140
verify_replay(struct xfrm_usersa_info * p,struct nlattr ** attrs,u8 sa_dir,struct netlink_ext_ack * extack)141 static inline int verify_replay(struct xfrm_usersa_info *p,
142 struct nlattr **attrs, u8 sa_dir,
143 struct netlink_ext_ack *extack)
144 {
145 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
146 struct xfrm_replay_state_esn *rs;
147
148 if (!rt) {
149 if (p->flags & XFRM_STATE_ESN) {
150 NL_SET_ERR_MSG(extack, "Missing required attribute for ESN");
151 return -EINVAL;
152 }
153 return 0;
154 }
155
156 rs = nla_data(rt);
157
158 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) {
159 NL_SET_ERR_MSG(extack, "ESN bitmap length must be <= 128");
160 return -EINVAL;
161 }
162
163 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
164 nla_len(rt) != sizeof(*rs)) {
165 NL_SET_ERR_MSG(extack, "ESN attribute is too short to fit the full bitmap length");
166 return -EINVAL;
167 }
168
169 /* As only ESP and AH support ESN feature. */
170 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH)) {
171 NL_SET_ERR_MSG(extack, "ESN only supported for ESP and AH");
172 return -EINVAL;
173 }
174
175 if (p->replay_window != 0) {
176 NL_SET_ERR_MSG(extack, "ESN not compatible with legacy replay_window");
177 return -EINVAL;
178 }
179
180 if (sa_dir == XFRM_SA_DIR_OUT) {
181 if (rs->replay_window) {
182 NL_SET_ERR_MSG(extack, "Replay window should be 0 for output SA");
183 return -EINVAL;
184 }
185 if (rs->seq || rs->seq_hi) {
186 NL_SET_ERR_MSG(extack,
187 "Replay seq and seq_hi should be 0 for output SA");
188 return -EINVAL;
189 }
190
191 if (!(p->flags & XFRM_STATE_ESN)) {
192 if (rs->oseq_hi) {
193 NL_SET_ERR_MSG(
194 extack,
195 "Replay oseq_hi should be 0 in non-ESN mode for output SA");
196 return -EINVAL;
197 }
198 if (rs->oseq == U32_MAX) {
199 NL_SET_ERR_MSG(
200 extack,
201 "Replay oseq should be less than 0xFFFFFFFF in non-ESN mode for output SA");
202 return -EINVAL;
203 }
204 } else {
205 if (rs->oseq == U32_MAX && rs->oseq_hi == U32_MAX) {
206 NL_SET_ERR_MSG(
207 extack,
208 "Replay oseq and oseq_hi should be less than 0xFFFFFFFF for output SA");
209 return -EINVAL;
210 }
211 }
212 if (rs->bmp_len) {
213 NL_SET_ERR_MSG(extack, "Replay bmp_len should 0 for output SA");
214 return -EINVAL;
215 }
216 }
217
218 if (sa_dir == XFRM_SA_DIR_IN) {
219 if (rs->oseq || rs->oseq_hi) {
220 NL_SET_ERR_MSG(extack,
221 "Replay oseq and oseq_hi should be 0 for input SA");
222 return -EINVAL;
223 }
224 if (!(p->flags & XFRM_STATE_ESN)) {
225 if (rs->seq_hi) {
226 NL_SET_ERR_MSG(
227 extack,
228 "Replay seq_hi should be 0 in non-ESN mode for input SA");
229 return -EINVAL;
230 }
231
232 if (rs->seq == U32_MAX) {
233 NL_SET_ERR_MSG(
234 extack,
235 "Replay seq should be less than 0xFFFFFFFF in non-ESN mode for input SA");
236 return -EINVAL;
237 }
238 } else {
239 if (rs->seq == U32_MAX && rs->seq_hi == U32_MAX) {
240 NL_SET_ERR_MSG(
241 extack,
242 "Replay seq and seq_hi should be less than 0xFFFFFFFF for input SA");
243 return -EINVAL;
244 }
245 }
246 }
247
248 return 0;
249 }
250
verify_mtimer_thresh(bool has_encap,u8 dir,struct netlink_ext_ack * extack)251 static int verify_mtimer_thresh(bool has_encap, u8 dir,
252 struct netlink_ext_ack *extack)
253 {
254 if (!has_encap) {
255 NL_SET_ERR_MSG(extack,
256 "MTIMER_THRESH requires encapsulation");
257 return -EINVAL;
258 }
259 if (dir == XFRM_SA_DIR_OUT) {
260 NL_SET_ERR_MSG(extack,
261 "MTIMER_THRESH should not be set on output SA");
262 return -EINVAL;
263 }
264 return 0;
265 }
266
verify_xfrm_family(u16 family,struct netlink_ext_ack * extack)267 static int verify_xfrm_family(u16 family, struct netlink_ext_ack *extack)
268 {
269 switch (family) {
270 case AF_INET:
271 return 0;
272 case AF_INET6:
273 #if IS_ENABLED(CONFIG_IPV6)
274 return 0;
275 #else
276 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
277 return -EAFNOSUPPORT;
278 #endif
279 default:
280 NL_SET_ERR_MSG(extack, "Invalid address family");
281 return -EINVAL;
282 }
283 }
284
verify_selector_prefixlen(u16 family,const struct xfrm_selector * sel,struct netlink_ext_ack * extack)285 static int verify_selector_prefixlen(u16 family,
286 const struct xfrm_selector *sel,
287 struct netlink_ext_ack *extack)
288 {
289 switch (family) {
290 case AF_UNSPEC:
291 return 0;
292 case AF_INET:
293 if (sel->prefixlen_d > 32 || sel->prefixlen_s > 32) {
294 NL_SET_ERR_MSG(extack,
295 "Invalid prefix length in selector (must be <= 32 for IPv4)");
296 return -EINVAL;
297 }
298 return 0;
299 case AF_INET6:
300 #if IS_ENABLED(CONFIG_IPV6)
301 if (sel->prefixlen_d > 128 || sel->prefixlen_s > 128) {
302 NL_SET_ERR_MSG(extack,
303 "Invalid prefix length in selector (must be <= 128 for IPv6)");
304 return -EINVAL;
305 }
306 return 0;
307 #else
308 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
309 return -EAFNOSUPPORT;
310 #endif
311 default:
312 NL_SET_ERR_MSG(extack, "Invalid address family in selector");
313 return -EINVAL;
314 }
315 }
316
verify_newsa_info(struct xfrm_usersa_info * p,struct nlattr ** attrs,struct netlink_ext_ack * extack)317 static int verify_newsa_info(struct xfrm_usersa_info *p,
318 struct nlattr **attrs,
319 struct netlink_ext_ack *extack)
320 {
321 int err;
322 u8 sa_dir = nla_get_u8_default(attrs[XFRMA_SA_DIR], 0);
323 u16 family = p->sel.family;
324
325 err = verify_xfrm_family(p->family, extack);
326 if (err)
327 goto out;
328
329 if (!family && !(p->flags & XFRM_STATE_AF_UNSPEC))
330 family = p->family;
331
332 err = verify_selector_prefixlen(family, &p->sel, extack);
333 if (err)
334 goto out;
335
336 err = -EINVAL;
337 switch (p->id.proto) {
338 case IPPROTO_AH:
339 if (!attrs[XFRMA_ALG_AUTH] &&
340 !attrs[XFRMA_ALG_AUTH_TRUNC]) {
341 NL_SET_ERR_MSG(extack, "Missing required attribute for AH: AUTH_TRUNC or AUTH");
342 goto out;
343 }
344
345 if (attrs[XFRMA_ALG_AEAD] ||
346 attrs[XFRMA_ALG_CRYPT] ||
347 attrs[XFRMA_ALG_COMP] ||
348 attrs[XFRMA_TFCPAD]) {
349 NL_SET_ERR_MSG(extack, "Invalid attributes for AH: AEAD, CRYPT, COMP, TFCPAD");
350 goto out;
351 }
352 break;
353
354 case IPPROTO_ESP:
355 if (attrs[XFRMA_ALG_COMP]) {
356 NL_SET_ERR_MSG(extack, "Invalid attribute for ESP: COMP");
357 goto out;
358 }
359
360 if (!attrs[XFRMA_ALG_AUTH] &&
361 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
362 !attrs[XFRMA_ALG_CRYPT] &&
363 !attrs[XFRMA_ALG_AEAD]) {
364 NL_SET_ERR_MSG(extack, "Missing required attribute for ESP: at least one of AUTH, AUTH_TRUNC, CRYPT, AEAD");
365 goto out;
366 }
367
368 if ((attrs[XFRMA_ALG_AUTH] ||
369 attrs[XFRMA_ALG_AUTH_TRUNC] ||
370 attrs[XFRMA_ALG_CRYPT]) &&
371 attrs[XFRMA_ALG_AEAD]) {
372 NL_SET_ERR_MSG(extack, "Invalid attribute combination for ESP: AEAD can't be used with AUTH, AUTH_TRUNC, CRYPT");
373 goto out;
374 }
375
376 if (attrs[XFRMA_TFCPAD] &&
377 p->mode != XFRM_MODE_TUNNEL) {
378 NL_SET_ERR_MSG(extack, "TFC padding can only be used in tunnel mode");
379 goto out;
380 }
381 if ((attrs[XFRMA_IPTFS_DROP_TIME] ||
382 attrs[XFRMA_IPTFS_REORDER_WINDOW] ||
383 attrs[XFRMA_IPTFS_DONT_FRAG] ||
384 attrs[XFRMA_IPTFS_INIT_DELAY] ||
385 attrs[XFRMA_IPTFS_MAX_QSIZE] ||
386 attrs[XFRMA_IPTFS_PKT_SIZE]) &&
387 p->mode != XFRM_MODE_IPTFS) {
388 NL_SET_ERR_MSG(extack, "IP-TFS options can only be used in IP-TFS mode");
389 goto out;
390 }
391 break;
392
393 case IPPROTO_COMP:
394 if (!attrs[XFRMA_ALG_COMP]) {
395 NL_SET_ERR_MSG(extack, "Missing required attribute for COMP: COMP");
396 goto out;
397 }
398
399 if (attrs[XFRMA_ALG_AEAD] ||
400 attrs[XFRMA_ALG_AUTH] ||
401 attrs[XFRMA_ALG_AUTH_TRUNC] ||
402 attrs[XFRMA_ALG_CRYPT] ||
403 attrs[XFRMA_TFCPAD]) {
404 NL_SET_ERR_MSG(extack, "Invalid attributes for COMP: AEAD, AUTH, AUTH_TRUNC, CRYPT, TFCPAD");
405 goto out;
406 }
407
408 if (ntohl(p->id.spi) >= 0x10000) {
409 NL_SET_ERR_MSG(extack, "SPI is too large for COMP (must be < 0x10000)");
410 goto out;
411 }
412 break;
413
414 #if IS_ENABLED(CONFIG_IPV6)
415 case IPPROTO_DSTOPTS:
416 case IPPROTO_ROUTING:
417 if (attrs[XFRMA_ALG_COMP] ||
418 attrs[XFRMA_ALG_AUTH] ||
419 attrs[XFRMA_ALG_AUTH_TRUNC] ||
420 attrs[XFRMA_ALG_AEAD] ||
421 attrs[XFRMA_ALG_CRYPT] ||
422 attrs[XFRMA_ENCAP] ||
423 attrs[XFRMA_SEC_CTX] ||
424 attrs[XFRMA_TFCPAD]) {
425 NL_SET_ERR_MSG(extack, "Invalid attributes for DSTOPTS/ROUTING");
426 goto out;
427 }
428
429 if (!attrs[XFRMA_COADDR]) {
430 NL_SET_ERR_MSG(extack, "Missing required COADDR attribute for DSTOPTS/ROUTING");
431 goto out;
432 }
433 break;
434 #endif
435
436 default:
437 NL_SET_ERR_MSG(extack, "Unsupported protocol");
438 goto out;
439 }
440
441 if ((err = verify_aead(attrs, extack)))
442 goto out;
443 if ((err = verify_auth_trunc(attrs, extack)))
444 goto out;
445 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH, extack)))
446 goto out;
447 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT, extack)))
448 goto out;
449 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP, extack)))
450 goto out;
451 if ((err = verify_sec_ctx_len(attrs, extack)))
452 goto out;
453 if ((err = verify_replay(p, attrs, sa_dir, extack)))
454 goto out;
455
456 err = -EINVAL;
457 switch (p->mode) {
458 case XFRM_MODE_TRANSPORT:
459 case XFRM_MODE_TUNNEL:
460 case XFRM_MODE_ROUTEOPTIMIZATION:
461 case XFRM_MODE_BEET:
462 break;
463 case XFRM_MODE_IPTFS:
464 if (p->id.proto != IPPROTO_ESP) {
465 NL_SET_ERR_MSG(extack, "IP-TFS mode only supported with ESP");
466 goto out;
467 }
468 if (sa_dir == 0) {
469 NL_SET_ERR_MSG(extack, "IP-TFS mode requires in or out direction attribute");
470 goto out;
471 }
472 break;
473
474 default:
475 NL_SET_ERR_MSG(extack, "Unsupported mode");
476 goto out;
477 }
478
479 err = 0;
480
481 if (attrs[XFRMA_MTIMER_THRESH]) {
482 err = verify_mtimer_thresh(!!attrs[XFRMA_ENCAP], sa_dir, extack);
483 if (err)
484 goto out;
485 }
486
487 if (sa_dir == XFRM_SA_DIR_OUT) {
488 if (p->flags & XFRM_STATE_DECAP_DSCP) {
489 NL_SET_ERR_MSG(extack, "Flag DECAP_DSCP should not be set for output SA");
490 err = -EINVAL;
491 goto out;
492 }
493
494 if (p->flags & XFRM_STATE_ICMP) {
495 NL_SET_ERR_MSG(extack, "Flag ICMP should not be set for output SA");
496 err = -EINVAL;
497 goto out;
498 }
499
500 if (p->flags & XFRM_STATE_WILDRECV) {
501 NL_SET_ERR_MSG(extack, "Flag WILDRECV should not be set for output SA");
502 err = -EINVAL;
503 goto out;
504 }
505
506 if (p->replay_window) {
507 NL_SET_ERR_MSG(extack, "Replay window should be 0 for output SA");
508 err = -EINVAL;
509 goto out;
510 }
511
512 if (attrs[XFRMA_IPTFS_DROP_TIME]) {
513 NL_SET_ERR_MSG(extack, "IP-TFS drop time should not be set for output SA");
514 err = -EINVAL;
515 goto out;
516 }
517
518 if (attrs[XFRMA_IPTFS_REORDER_WINDOW]) {
519 NL_SET_ERR_MSG(extack, "IP-TFS reorder window should not be set for output SA");
520 err = -EINVAL;
521 goto out;
522 }
523
524 if (attrs[XFRMA_REPLAY_VAL]) {
525 struct xfrm_replay_state *replay;
526
527 replay = nla_data(attrs[XFRMA_REPLAY_VAL]);
528
529 if (replay->seq || replay->bitmap) {
530 NL_SET_ERR_MSG(extack,
531 "Replay seq and bitmap should be 0 for output SA");
532 err = -EINVAL;
533 goto out;
534 }
535 }
536 }
537
538 if (sa_dir == XFRM_SA_DIR_IN) {
539 if (p->flags & XFRM_STATE_NOPMTUDISC) {
540 NL_SET_ERR_MSG(extack, "Flag NOPMTUDISC should not be set for input SA");
541 err = -EINVAL;
542 goto out;
543 }
544
545 if (attrs[XFRMA_SA_EXTRA_FLAGS]) {
546 u32 xflags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
547
548 if (xflags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP) {
549 NL_SET_ERR_MSG(extack, "Flag DONT_ENCAP_DSCP should not be set for input SA");
550 err = -EINVAL;
551 goto out;
552 }
553
554 if (xflags & XFRM_SA_XFLAG_OSEQ_MAY_WRAP) {
555 NL_SET_ERR_MSG(extack, "Flag OSEQ_MAY_WRAP should not be set for input SA");
556 err = -EINVAL;
557 goto out;
558 }
559
560 }
561
562 if (attrs[XFRMA_IPTFS_DONT_FRAG]) {
563 NL_SET_ERR_MSG(extack, "IP-TFS don't fragment should not be set for input SA");
564 err = -EINVAL;
565 goto out;
566 }
567
568 if (attrs[XFRMA_IPTFS_INIT_DELAY]) {
569 NL_SET_ERR_MSG(extack, "IP-TFS initial delay should not be set for input SA");
570 err = -EINVAL;
571 goto out;
572 }
573
574 if (attrs[XFRMA_IPTFS_MAX_QSIZE]) {
575 NL_SET_ERR_MSG(extack, "IP-TFS max queue size should not be set for input SA");
576 err = -EINVAL;
577 goto out;
578 }
579
580 if (attrs[XFRMA_IPTFS_PKT_SIZE]) {
581 NL_SET_ERR_MSG(extack, "IP-TFS packet size should not be set for input SA");
582 err = -EINVAL;
583 goto out;
584 }
585 }
586
587 if (!sa_dir && attrs[XFRMA_SA_PCPU]) {
588 NL_SET_ERR_MSG(extack, "SA_PCPU only supported with SA_DIR");
589 err = -EINVAL;
590 goto out;
591 }
592
593 out:
594 return err;
595 }
596
attach_one_algo(struct xfrm_algo ** algpp,u8 * props,struct xfrm_algo_desc * (* get_byname)(const char *,int),struct nlattr * rta,struct netlink_ext_ack * extack)597 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
598 struct xfrm_algo_desc *(*get_byname)(const char *, int),
599 struct nlattr *rta, struct netlink_ext_ack *extack)
600 {
601 struct xfrm_algo *p, *ualg;
602 struct xfrm_algo_desc *algo;
603
604 if (!rta)
605 return 0;
606
607 ualg = nla_data(rta);
608
609 algo = get_byname(ualg->alg_name, 1);
610 if (!algo) {
611 NL_SET_ERR_MSG(extack, "Requested COMP algorithm not found");
612 return -ENOSYS;
613 }
614 *props = algo->desc.sadb_alg_id;
615
616 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
617 if (!p)
618 return -ENOMEM;
619
620 strscpy(p->alg_name, algo->name);
621 *algpp = p;
622 return 0;
623 }
624
attach_crypt(struct xfrm_state * x,struct nlattr * rta,struct netlink_ext_ack * extack)625 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta,
626 struct netlink_ext_ack *extack)
627 {
628 struct xfrm_algo *p, *ualg;
629 struct xfrm_algo_desc *algo;
630
631 if (!rta)
632 return 0;
633
634 ualg = nla_data(rta);
635
636 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
637 if (!algo) {
638 NL_SET_ERR_MSG(extack, "Requested CRYPT algorithm not found");
639 return -ENOSYS;
640 }
641 x->props.ealgo = algo->desc.sadb_alg_id;
642
643 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
644 if (!p)
645 return -ENOMEM;
646
647 strscpy(p->alg_name, algo->name);
648 x->ealg = p;
649 x->geniv = algo->uinfo.encr.geniv;
650 return 0;
651 }
652
attach_auth(struct xfrm_algo_auth ** algpp,u8 * props,struct nlattr * rta,struct netlink_ext_ack * extack)653 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
654 struct nlattr *rta, struct netlink_ext_ack *extack)
655 {
656 struct xfrm_algo *ualg;
657 struct xfrm_algo_auth *p;
658 struct xfrm_algo_desc *algo;
659
660 if (!rta)
661 return 0;
662
663 ualg = nla_data(rta);
664
665 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
666 if (!algo) {
667 NL_SET_ERR_MSG(extack, "Requested AUTH algorithm not found");
668 return -ENOSYS;
669 }
670 *props = algo->desc.sadb_alg_id;
671
672 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
673 if (!p)
674 return -ENOMEM;
675
676 strscpy(p->alg_name, algo->name);
677 p->alg_key_len = ualg->alg_key_len;
678 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
679 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
680
681 *algpp = p;
682 return 0;
683 }
684
attach_auth_trunc(struct xfrm_algo_auth ** algpp,u8 * props,struct nlattr * rta,struct netlink_ext_ack * extack)685 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
686 struct nlattr *rta, struct netlink_ext_ack *extack)
687 {
688 struct xfrm_algo_auth *p, *ualg;
689 struct xfrm_algo_desc *algo;
690
691 if (!rta)
692 return 0;
693
694 ualg = nla_data(rta);
695
696 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
697 if (!algo) {
698 NL_SET_ERR_MSG(extack, "Requested AUTH_TRUNC algorithm not found");
699 return -ENOSYS;
700 }
701 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) {
702 NL_SET_ERR_MSG(extack, "Invalid length requested for truncated ICV");
703 return -EINVAL;
704 }
705 *props = algo->desc.sadb_alg_id;
706
707 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
708 if (!p)
709 return -ENOMEM;
710
711 strscpy(p->alg_name, algo->name);
712 if (!p->alg_trunc_len)
713 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
714
715 *algpp = p;
716 return 0;
717 }
718
attach_aead(struct xfrm_state * x,struct nlattr * rta,struct netlink_ext_ack * extack)719 static int attach_aead(struct xfrm_state *x, struct nlattr *rta,
720 struct netlink_ext_ack *extack)
721 {
722 struct xfrm_algo_aead *p, *ualg;
723 struct xfrm_algo_desc *algo;
724
725 if (!rta)
726 return 0;
727
728 ualg = nla_data(rta);
729
730 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
731 if (!algo) {
732 NL_SET_ERR_MSG(extack, "Requested AEAD algorithm not found");
733 return -ENOSYS;
734 }
735 x->props.ealgo = algo->desc.sadb_alg_id;
736
737 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
738 if (!p)
739 return -ENOMEM;
740
741 strscpy(p->alg_name, algo->name);
742 x->aead = p;
743 x->geniv = algo->uinfo.aead.geniv;
744 return 0;
745 }
746
xfrm_replay_verify_len(struct xfrm_replay_state_esn * replay_esn,struct nlattr * rp,struct netlink_ext_ack * extack)747 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
748 struct nlattr *rp,
749 struct netlink_ext_ack *extack)
750 {
751 struct xfrm_replay_state_esn *up;
752 unsigned int ulen;
753
754 if (!replay_esn || !rp)
755 return 0;
756
757 up = nla_data(rp);
758 ulen = xfrm_replay_state_esn_len(up);
759
760 /* Check the overall length and the internal bitmap length to avoid
761 * potential overflow. */
762 if (nla_len(rp) < (int)ulen) {
763 NL_SET_ERR_MSG(extack, "ESN attribute is too short");
764 return -EINVAL;
765 }
766
767 if (xfrm_replay_state_esn_len(replay_esn) != ulen) {
768 NL_SET_ERR_MSG(extack, "New ESN size doesn't match the existing SA's ESN size");
769 return -EINVAL;
770 }
771
772 if (replay_esn->bmp_len != up->bmp_len) {
773 NL_SET_ERR_MSG(extack, "New ESN bitmap size doesn't match the existing SA's ESN bitmap");
774 return -EINVAL;
775 }
776
777 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8) {
778 NL_SET_ERR_MSG(extack, "ESN replay window is longer than the bitmap");
779 return -EINVAL;
780 }
781
782 return 0;
783 }
784
xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn ** replay_esn,struct xfrm_replay_state_esn ** preplay_esn,struct nlattr * rta)785 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
786 struct xfrm_replay_state_esn **preplay_esn,
787 struct nlattr *rta)
788 {
789 struct xfrm_replay_state_esn *p, *pp, *up;
790 unsigned int klen, ulen;
791
792 if (!rta)
793 return 0;
794
795 up = nla_data(rta);
796 klen = xfrm_replay_state_esn_len(up);
797 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
798
799 p = kzalloc(klen, GFP_KERNEL);
800 if (!p)
801 return -ENOMEM;
802
803 pp = kzalloc(klen, GFP_KERNEL);
804 if (!pp) {
805 kfree(p);
806 return -ENOMEM;
807 }
808
809 memcpy(p, up, ulen);
810 memcpy(pp, up, ulen);
811
812 *replay_esn = p;
813 *preplay_esn = pp;
814
815 return 0;
816 }
817
xfrm_user_sec_ctx_size(struct xfrm_sec_ctx * xfrm_ctx)818 static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
819 {
820 unsigned int len = 0;
821
822 if (xfrm_ctx) {
823 len += sizeof(struct xfrm_user_sec_ctx);
824 len += xfrm_ctx->ctx_len;
825 }
826 return len;
827 }
828
copy_from_user_state(struct xfrm_state * x,struct xfrm_usersa_info * p)829 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
830 {
831 memcpy(&x->id, &p->id, sizeof(x->id));
832 memcpy(&x->sel, &p->sel, sizeof(x->sel));
833 memcpy(&x->lft, &p->lft, sizeof(x->lft));
834 x->props.mode = p->mode;
835 x->props.replay_window = min_t(unsigned int, p->replay_window,
836 sizeof(x->replay.bitmap) * 8);
837 x->props.reqid = p->reqid;
838 x->props.family = p->family;
839 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
840 x->props.flags = p->flags;
841
842 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
843 x->sel.family = p->family;
844 }
845
846 /*
847 * someday when pfkey also has support, we could have the code
848 * somehow made shareable and move it to xfrm_state.c - JHS
849 *
850 */
xfrm_update_ae_params(struct xfrm_state * x,struct nlattr ** attrs,int update_esn)851 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
852 int update_esn)
853 {
854 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
855 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
856 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
857 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
858 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
859 struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
860
861 if (re && x->replay_esn && x->preplay_esn) {
862 struct xfrm_replay_state_esn *replay_esn;
863 replay_esn = nla_data(re);
864 memcpy(x->replay_esn, replay_esn,
865 xfrm_replay_state_esn_len(replay_esn));
866 memcpy(x->preplay_esn, replay_esn,
867 xfrm_replay_state_esn_len(replay_esn));
868 }
869
870 if (rp) {
871 struct xfrm_replay_state *replay;
872 replay = nla_data(rp);
873 memcpy(&x->replay, replay, sizeof(*replay));
874 memcpy(&x->preplay, replay, sizeof(*replay));
875 }
876
877 if (lt) {
878 struct xfrm_lifetime_cur *ltime;
879 ltime = nla_data(lt);
880 x->curlft.bytes = ltime->bytes;
881 x->curlft.packets = ltime->packets;
882 x->curlft.add_time = ltime->add_time;
883 x->curlft.use_time = ltime->use_time;
884 }
885
886 if (et)
887 x->replay_maxage = nla_get_u32(et);
888
889 if (rt)
890 x->replay_maxdiff = nla_get_u32(rt);
891
892 if (mt)
893 x->mapping_maxage = nla_get_u32(mt);
894 }
895
xfrm_smark_init(struct nlattr ** attrs,struct xfrm_mark * m)896 static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
897 {
898 if (attrs[XFRMA_SET_MARK]) {
899 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
900 m->m = nla_get_u32_default(attrs[XFRMA_SET_MARK_MASK],
901 0xffffffff);
902 } else {
903 m->v = m->m = 0;
904 }
905 }
906
xfrm_state_construct(struct net * net,struct xfrm_usersa_info * p,struct nlattr ** attrs,int * errp,struct netlink_ext_ack * extack)907 static struct xfrm_state *xfrm_state_construct(struct net *net,
908 struct xfrm_usersa_info *p,
909 struct nlattr **attrs,
910 int *errp,
911 struct netlink_ext_ack *extack)
912 {
913 struct xfrm_state *x = xfrm_state_alloc(net);
914 int err = -ENOMEM;
915
916 if (!x)
917 goto error_no_put;
918
919 copy_from_user_state(x, p);
920
921 if (attrs[XFRMA_ENCAP]) {
922 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
923 sizeof(*x->encap), GFP_KERNEL);
924 if (x->encap == NULL)
925 goto error;
926 }
927
928 if (attrs[XFRMA_COADDR]) {
929 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
930 sizeof(*x->coaddr), GFP_KERNEL);
931 if (x->coaddr == NULL)
932 goto error;
933 }
934
935 if (attrs[XFRMA_SA_EXTRA_FLAGS])
936 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
937
938 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD], extack)))
939 goto error;
940 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
941 attrs[XFRMA_ALG_AUTH_TRUNC], extack)))
942 goto error;
943 if (!x->aalg) {
944 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
945 attrs[XFRMA_ALG_AUTH], extack)))
946 goto error;
947 }
948 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT], extack)))
949 goto error;
950 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
951 xfrm_calg_get_byname,
952 attrs[XFRMA_ALG_COMP], extack)))
953 goto error;
954
955 if (attrs[XFRMA_TFCPAD]) {
956 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
957 if (x->tfcpad > IP_MAX_MTU) {
958 NL_SET_ERR_MSG(extack, "Excessive TFC padding");
959 err = -EINVAL;
960 goto error;
961 }
962 }
963
964 xfrm_mark_get(attrs, &x->mark);
965
966 xfrm_smark_init(attrs, &x->props.smark);
967
968 if (attrs[XFRMA_IF_ID])
969 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
970
971 if (attrs[XFRMA_SA_DIR])
972 x->dir = nla_get_u8(attrs[XFRMA_SA_DIR]);
973
974 if (attrs[XFRMA_NAT_KEEPALIVE_INTERVAL])
975 x->nat_keepalive_interval =
976 nla_get_u32(attrs[XFRMA_NAT_KEEPALIVE_INTERVAL]);
977
978 if (attrs[XFRMA_SA_PCPU]) {
979 x->pcpu_num = nla_get_u32(attrs[XFRMA_SA_PCPU]);
980 if (x->pcpu_num >= num_possible_cpus()) {
981 err = -ERANGE;
982 NL_SET_ERR_MSG(extack, "pCPU number too big");
983 goto error;
984 }
985 }
986
987 err = __xfrm_init_state(x, extack);
988 if (err)
989 goto error;
990
991 if (attrs[XFRMA_SEC_CTX]) {
992 err = security_xfrm_state_alloc(x,
993 nla_data(attrs[XFRMA_SEC_CTX]));
994 if (err)
995 goto error;
996 }
997
998 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
999 attrs[XFRMA_REPLAY_ESN_VAL])))
1000 goto error;
1001
1002 x->km.seq = p->seq;
1003 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
1004 /* sysctl_xfrm_aevent_etime is in 100ms units */
1005 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
1006
1007 if ((err = xfrm_init_replay(x, extack)))
1008 goto error;
1009
1010 /* override default values from above */
1011 xfrm_update_ae_params(x, attrs, 0);
1012
1013 xfrm_set_type_offload(x, attrs[XFRMA_OFFLOAD_DEV]);
1014 /* configure the hardware if offload is requested */
1015 if (attrs[XFRMA_OFFLOAD_DEV]) {
1016 err = xfrm_dev_state_add(net, x,
1017 nla_data(attrs[XFRMA_OFFLOAD_DEV]),
1018 extack);
1019 if (err)
1020 goto error;
1021 }
1022
1023 if (x->mode_cbs && x->mode_cbs->user_init) {
1024 err = x->mode_cbs->user_init(net, x, attrs, extack);
1025 if (err)
1026 goto error;
1027 }
1028
1029 return x;
1030
1031 error:
1032 x->km.state = XFRM_STATE_DEAD;
1033 xfrm_state_put(x);
1034 error_no_put:
1035 *errp = err;
1036 return NULL;
1037 }
1038
xfrm_add_sa(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)1039 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1040 struct nlattr **attrs, struct netlink_ext_ack *extack)
1041 {
1042 struct net *net = sock_net(skb->sk);
1043 struct xfrm_usersa_info *p = nlmsg_data(nlh);
1044 struct xfrm_state *x;
1045 int err;
1046 struct km_event c;
1047
1048 err = verify_newsa_info(p, attrs, extack);
1049 if (err)
1050 return err;
1051
1052 x = xfrm_state_construct(net, p, attrs, &err, extack);
1053 if (!x)
1054 return err;
1055
1056 xfrm_state_hold(x);
1057 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
1058 err = xfrm_state_add(x);
1059 else
1060 err = xfrm_state_update(x);
1061
1062 xfrm_audit_state_add(x, err ? 0 : 1, true);
1063
1064 if (err < 0) {
1065 x->km.state = XFRM_STATE_DEAD;
1066 xfrm_dev_state_delete(x);
1067 __xfrm_state_put(x);
1068 goto out;
1069 }
1070
1071 if (x->km.state == XFRM_STATE_VOID)
1072 x->km.state = XFRM_STATE_VALID;
1073
1074 c.seq = nlh->nlmsg_seq;
1075 c.portid = nlh->nlmsg_pid;
1076 c.event = nlh->nlmsg_type;
1077
1078 km_state_notify(x, &c);
1079 out:
1080 xfrm_state_put(x);
1081 return err;
1082 }
1083
xfrm_user_state_lookup(struct net * net,struct xfrm_usersa_id * p,struct nlattr ** attrs,int * errp)1084 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
1085 struct xfrm_usersa_id *p,
1086 struct nlattr **attrs,
1087 int *errp)
1088 {
1089 struct xfrm_state *x = NULL;
1090 struct xfrm_mark m;
1091 int err;
1092 u32 mark = xfrm_mark_get(attrs, &m);
1093
1094 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
1095 err = -ESRCH;
1096 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
1097 } else {
1098 xfrm_address_t *saddr = NULL;
1099
1100 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
1101 if (!saddr) {
1102 err = -EINVAL;
1103 goto out;
1104 }
1105
1106 err = -ESRCH;
1107 x = xfrm_state_lookup_byaddr(net, mark,
1108 &p->daddr, saddr,
1109 p->proto, p->family);
1110 }
1111
1112 out:
1113 if (!x && errp)
1114 *errp = err;
1115 return x;
1116 }
1117
xfrm_del_sa(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)1118 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1119 struct nlattr **attrs, struct netlink_ext_ack *extack)
1120 {
1121 struct net *net = sock_net(skb->sk);
1122 struct xfrm_state *x;
1123 int err = -ESRCH;
1124 struct km_event c;
1125 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1126
1127 x = xfrm_user_state_lookup(net, p, attrs, &err);
1128 if (x == NULL)
1129 return err;
1130
1131 if ((err = security_xfrm_state_delete(x)) != 0)
1132 goto out;
1133
1134 if (xfrm_state_kern(x)) {
1135 NL_SET_ERR_MSG(extack, "SA is in use by tunnels");
1136 err = -EPERM;
1137 goto out;
1138 }
1139
1140 err = xfrm_state_delete(x);
1141 if (err < 0)
1142 goto out;
1143
1144 c.seq = nlh->nlmsg_seq;
1145 c.portid = nlh->nlmsg_pid;
1146 c.event = nlh->nlmsg_type;
1147 km_state_notify(x, &c);
1148
1149 out:
1150 xfrm_audit_state_delete(x, err ? 0 : 1, true);
1151 xfrm_state_put(x);
1152 return err;
1153 }
1154
copy_to_user_state(struct xfrm_state * x,struct xfrm_usersa_info * p)1155 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
1156 {
1157 memset(p, 0, sizeof(*p));
1158 memcpy(&p->id, &x->id, sizeof(p->id));
1159 memcpy(&p->sel, &x->sel, sizeof(p->sel));
1160 memcpy(&p->lft, &x->lft, sizeof(p->lft));
1161 if (x->xso.dev)
1162 xfrm_dev_state_update_stats(x);
1163 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
1164 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
1165 put_unaligned(x->stats.replay, &p->stats.replay);
1166 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
1167 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
1168 p->mode = x->props.mode;
1169 p->replay_window = x->props.replay_window;
1170 p->reqid = x->props.reqid;
1171 p->family = x->props.family;
1172 p->flags = x->props.flags;
1173 p->seq = x->km.seq;
1174 }
1175
1176 struct xfrm_dump_info {
1177 struct sk_buff *in_skb;
1178 struct sk_buff *out_skb;
1179 u32 nlmsg_seq;
1180 u16 nlmsg_flags;
1181 };
1182
copy_sec_ctx(struct xfrm_sec_ctx * s,struct sk_buff * skb)1183 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
1184 {
1185 struct xfrm_user_sec_ctx *uctx;
1186 struct nlattr *attr;
1187 int ctx_size = sizeof(*uctx) + s->ctx_len;
1188
1189 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
1190 if (attr == NULL)
1191 return -EMSGSIZE;
1192
1193 uctx = nla_data(attr);
1194 uctx->exttype = XFRMA_SEC_CTX;
1195 uctx->len = ctx_size;
1196 uctx->ctx_doi = s->ctx_doi;
1197 uctx->ctx_alg = s->ctx_alg;
1198 uctx->ctx_len = s->ctx_len;
1199 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1200
1201 return 0;
1202 }
1203
xso_to_xuo_ifindex(const struct xfrm_dev_offload * xso,int ifindex,struct xfrm_user_offload * xuo)1204 static void xso_to_xuo_ifindex(const struct xfrm_dev_offload *xso, int ifindex,
1205 struct xfrm_user_offload *xuo)
1206 {
1207 xuo->ifindex = ifindex;
1208 if (xso->dir == XFRM_DEV_OFFLOAD_IN)
1209 xuo->flags = XFRM_OFFLOAD_INBOUND;
1210 if (xso->type == XFRM_DEV_OFFLOAD_PACKET)
1211 xuo->flags |= XFRM_OFFLOAD_PACKET;
1212 }
1213
1214 #ifdef CONFIG_XFRM_MIGRATE
xso_to_xuo(const struct xfrm_dev_offload * xso,struct xfrm_user_offload * xuo)1215 static void xso_to_xuo(const struct xfrm_dev_offload *xso,
1216 struct xfrm_user_offload *xuo)
1217 {
1218 xso_to_xuo_ifindex(xso, xso->dev->ifindex, xuo);
1219 }
1220 #endif
1221
copy_user_offload_ifindex(const struct xfrm_dev_offload * xso,int ifindex,struct sk_buff * skb)1222 static int copy_user_offload_ifindex(const struct xfrm_dev_offload *xso,
1223 int ifindex, struct sk_buff *skb)
1224 {
1225 struct xfrm_user_offload *xuo;
1226 struct nlattr *attr;
1227
1228 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
1229 if (attr == NULL)
1230 return -EMSGSIZE;
1231
1232 xuo = nla_data(attr);
1233 memset(xuo, 0, sizeof(*xuo));
1234 xso_to_xuo_ifindex(xso, ifindex, xuo);
1235
1236 return 0;
1237 }
1238
copy_user_offload(struct xfrm_dev_offload * xso,struct sk_buff * skb)1239 static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb)
1240 {
1241 return copy_user_offload_ifindex(xso, xso->dev->ifindex, skb);
1242 }
1243
copy_user_state_offload(const struct xfrm_dev_offload * xso,struct sk_buff * skb)1244 static int copy_user_state_offload(const struct xfrm_dev_offload *xso,
1245 struct sk_buff *skb)
1246 {
1247 return copy_user_offload_ifindex(xso, READ_ONCE(xso->ifindex), skb);
1248 }
1249
xfrm_redact(void)1250 static bool xfrm_redact(void)
1251 {
1252 return IS_ENABLED(CONFIG_SECURITY) &&
1253 security_locked_down(LOCKDOWN_XFRM_SECRET);
1254 }
1255
copy_to_user_auth(struct xfrm_algo_auth * auth,struct sk_buff * skb)1256 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
1257 {
1258 struct xfrm_algo *algo;
1259 struct xfrm_algo_auth *ap;
1260 struct nlattr *nla;
1261 bool redact_secret = xfrm_redact();
1262
1263 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
1264 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
1265 if (!nla)
1266 return -EMSGSIZE;
1267 algo = nla_data(nla);
1268 strscpy_pad(algo->alg_name, auth->alg_name);
1269
1270 if (redact_secret && auth->alg_key_len)
1271 memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
1272 else
1273 memcpy(algo->alg_key, auth->alg_key,
1274 (auth->alg_key_len + 7) / 8);
1275 algo->alg_key_len = auth->alg_key_len;
1276
1277 nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
1278 if (!nla)
1279 return -EMSGSIZE;
1280 ap = nla_data(nla);
1281 strscpy_pad(ap->alg_name, auth->alg_name);
1282 ap->alg_key_len = auth->alg_key_len;
1283 ap->alg_trunc_len = auth->alg_trunc_len;
1284 if (redact_secret && auth->alg_key_len)
1285 memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
1286 else
1287 memcpy(ap->alg_key, auth->alg_key,
1288 (auth->alg_key_len + 7) / 8);
1289 return 0;
1290 }
1291
copy_to_user_aead(struct xfrm_algo_aead * aead,struct sk_buff * skb)1292 static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
1293 {
1294 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
1295 struct xfrm_algo_aead *ap;
1296 bool redact_secret = xfrm_redact();
1297
1298 if (!nla)
1299 return -EMSGSIZE;
1300
1301 ap = nla_data(nla);
1302 strscpy_pad(ap->alg_name, aead->alg_name);
1303 ap->alg_key_len = aead->alg_key_len;
1304 ap->alg_icv_len = aead->alg_icv_len;
1305
1306 if (redact_secret && aead->alg_key_len)
1307 memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
1308 else
1309 memcpy(ap->alg_key, aead->alg_key,
1310 (aead->alg_key_len + 7) / 8);
1311 return 0;
1312 }
1313
copy_to_user_ealg(struct xfrm_algo * ealg,struct sk_buff * skb)1314 static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
1315 {
1316 struct xfrm_algo *ap;
1317 bool redact_secret = xfrm_redact();
1318 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
1319 xfrm_alg_len(ealg));
1320 if (!nla)
1321 return -EMSGSIZE;
1322
1323 ap = nla_data(nla);
1324 strscpy_pad(ap->alg_name, ealg->alg_name);
1325 ap->alg_key_len = ealg->alg_key_len;
1326
1327 if (redact_secret && ealg->alg_key_len)
1328 memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
1329 else
1330 memcpy(ap->alg_key, ealg->alg_key,
1331 (ealg->alg_key_len + 7) / 8);
1332
1333 return 0;
1334 }
1335
copy_to_user_calg(struct xfrm_algo * calg,struct sk_buff * skb)1336 static int copy_to_user_calg(struct xfrm_algo *calg, struct sk_buff *skb)
1337 {
1338 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_COMP, sizeof(*calg));
1339 struct xfrm_algo *ap;
1340
1341 if (!nla)
1342 return -EMSGSIZE;
1343
1344 ap = nla_data(nla);
1345 strscpy_pad(ap->alg_name, calg->alg_name);
1346 ap->alg_key_len = 0;
1347
1348 return 0;
1349 }
1350
copy_to_user_encap(struct xfrm_encap_tmpl * ep,struct sk_buff * skb)1351 static int copy_to_user_encap(struct xfrm_encap_tmpl *ep, struct sk_buff *skb)
1352 {
1353 struct nlattr *nla = nla_reserve(skb, XFRMA_ENCAP, sizeof(*ep));
1354 struct xfrm_encap_tmpl *uep;
1355
1356 if (!nla)
1357 return -EMSGSIZE;
1358
1359 uep = nla_data(nla);
1360 memset(uep, 0, sizeof(*uep));
1361
1362 uep->encap_type = ep->encap_type;
1363 uep->encap_sport = ep->encap_sport;
1364 uep->encap_dport = ep->encap_dport;
1365 uep->encap_oa = ep->encap_oa;
1366
1367 return 0;
1368 }
1369
xfrm_smark_put(struct sk_buff * skb,const struct xfrm_mark * m)1370 static int xfrm_smark_put(struct sk_buff *skb, const struct xfrm_mark *m)
1371 {
1372 int ret = 0;
1373
1374 if (m->v | m->m) {
1375 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
1376 if (!ret)
1377 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
1378 }
1379 return ret;
1380 }
1381
1382 /* Don't change this without updating xfrm_sa_len! */
copy_to_user_state_extra(struct xfrm_state * x,struct xfrm_usersa_info * p,struct sk_buff * skb)1383 static int copy_to_user_state_extra(struct xfrm_state *x,
1384 struct xfrm_usersa_info *p,
1385 struct sk_buff *skb)
1386 {
1387 int ret = 0;
1388
1389 copy_to_user_state(x, p);
1390
1391 if (x->props.extra_flags) {
1392 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
1393 x->props.extra_flags);
1394 if (ret)
1395 goto out;
1396 }
1397
1398 if (x->coaddr) {
1399 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
1400 if (ret)
1401 goto out;
1402 }
1403 if (x->lastused) {
1404 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
1405 XFRMA_PAD);
1406 if (ret)
1407 goto out;
1408 }
1409 if (x->aead) {
1410 ret = copy_to_user_aead(x->aead, skb);
1411 if (ret)
1412 goto out;
1413 }
1414 if (x->aalg) {
1415 ret = copy_to_user_auth(x->aalg, skb);
1416 if (ret)
1417 goto out;
1418 }
1419 if (x->ealg) {
1420 ret = copy_to_user_ealg(x->ealg, skb);
1421 if (ret)
1422 goto out;
1423 }
1424 if (x->calg) {
1425 ret = copy_to_user_calg(x->calg, skb);
1426 if (ret)
1427 goto out;
1428 }
1429 if (x->encap) {
1430 ret = copy_to_user_encap(x->encap, skb);
1431 if (ret)
1432 goto out;
1433 }
1434 if (x->tfcpad) {
1435 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
1436 if (ret)
1437 goto out;
1438 }
1439 ret = xfrm_mark_put(skb, &x->mark);
1440 if (ret)
1441 goto out;
1442
1443 ret = xfrm_smark_put(skb, &x->props.smark);
1444 if (ret)
1445 goto out;
1446
1447 if (x->replay_esn)
1448 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1449 xfrm_replay_state_esn_len(x->replay_esn),
1450 x->replay_esn);
1451 else
1452 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1453 &x->replay);
1454 if (ret)
1455 goto out;
1456 if (READ_ONCE(x->xso.dev))
1457 ret = copy_user_state_offload(&x->xso, skb);
1458 if (ret)
1459 goto out;
1460 if (x->if_id) {
1461 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1462 if (ret)
1463 goto out;
1464 }
1465 if (x->security) {
1466 ret = copy_sec_ctx(x->security, skb);
1467 if (ret)
1468 goto out;
1469 }
1470 if (x->mode_cbs && x->mode_cbs->copy_to_user)
1471 ret = x->mode_cbs->copy_to_user(x, skb);
1472 if (ret)
1473 goto out;
1474 if (x->mapping_maxage) {
1475 ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
1476 if (ret)
1477 goto out;
1478 }
1479 if (x->pcpu_num != UINT_MAX) {
1480 ret = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
1481 if (ret)
1482 goto out;
1483 }
1484 if (x->dir)
1485 ret = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
1486
1487 if (x->nat_keepalive_interval) {
1488 ret = nla_put_u32(skb, XFRMA_NAT_KEEPALIVE_INTERVAL,
1489 x->nat_keepalive_interval);
1490 if (ret)
1491 goto out;
1492 }
1493 out:
1494 return ret;
1495 }
1496
dump_one_state(struct xfrm_state * x,int count,void * ptr)1497 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
1498 {
1499 struct xfrm_dump_info *sp = ptr;
1500 struct sk_buff *in_skb = sp->in_skb;
1501 struct sk_buff *skb = sp->out_skb;
1502 struct xfrm_translator *xtr;
1503 struct xfrm_usersa_info *p;
1504 struct nlmsghdr *nlh;
1505 int err;
1506
1507 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1508 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
1509 if (nlh == NULL)
1510 return -EMSGSIZE;
1511
1512 p = nlmsg_data(nlh);
1513
1514 err = copy_to_user_state_extra(x, p, skb);
1515 if (err) {
1516 nlmsg_cancel(skb, nlh);
1517 return err;
1518 }
1519 nlmsg_end(skb, nlh);
1520
1521 xtr = xfrm_get_translator();
1522 if (xtr) {
1523 err = xtr->alloc_compat(skb, nlh);
1524
1525 xfrm_put_translator(xtr);
1526 if (err) {
1527 nlmsg_cancel(skb, nlh);
1528 return err;
1529 }
1530 }
1531
1532 return 0;
1533 }
1534
xfrm_dump_sa_done(struct netlink_callback * cb)1535 static int xfrm_dump_sa_done(struct netlink_callback *cb)
1536 {
1537 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1538 struct sock *sk = cb->skb->sk;
1539 struct net *net = sock_net(sk);
1540
1541 if (cb->args[0])
1542 xfrm_state_walk_done(walk, net);
1543 return 0;
1544 }
1545
xfrm_dump_sa(struct sk_buff * skb,struct netlink_callback * cb)1546 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1547 {
1548 struct net *net = sock_net(skb->sk);
1549 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1550 struct xfrm_dump_info info;
1551
1552 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1553 sizeof(cb->args) - sizeof(cb->args[0]));
1554
1555 info.in_skb = cb->skb;
1556 info.out_skb = skb;
1557 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1558 info.nlmsg_flags = NLM_F_MULTI;
1559
1560 if (!cb->args[0]) {
1561 struct nlattr *attrs[XFRMA_MAX+1];
1562 struct xfrm_address_filter *filter = NULL;
1563 u8 proto = 0;
1564 int err;
1565
1566 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1567 xfrma_policy, cb->extack);
1568 if (err < 0)
1569 return err;
1570
1571 if (attrs[XFRMA_ADDRESS_FILTER]) {
1572 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1573 sizeof(*filter), GFP_KERNEL);
1574 if (filter == NULL)
1575 return -ENOMEM;
1576
1577 /* see addr_match(), (prefix length >> 5) << 2
1578 * will be used to compare xfrm_address_t
1579 */
1580 if (filter->splen > (sizeof(xfrm_address_t) << 3) ||
1581 filter->dplen > (sizeof(xfrm_address_t) << 3)) {
1582 kfree(filter);
1583 return -EINVAL;
1584 }
1585 }
1586
1587 if (attrs[XFRMA_PROTO])
1588 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1589
1590 xfrm_state_walk_init(walk, proto, filter);
1591 cb->args[0] = 1;
1592 }
1593
1594 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1595
1596 return skb->len;
1597 }
1598
xfrm_state_netlink(struct sk_buff * in_skb,struct xfrm_state * x,u32 seq)1599 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1600 struct xfrm_state *x, u32 seq)
1601 {
1602 struct xfrm_dump_info info;
1603 struct sk_buff *skb;
1604 int err;
1605
1606 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1607 if (!skb)
1608 return ERR_PTR(-ENOMEM);
1609
1610 info.in_skb = in_skb;
1611 info.out_skb = skb;
1612 info.nlmsg_seq = seq;
1613 info.nlmsg_flags = 0;
1614
1615 err = dump_one_state(x, 0, &info);
1616 if (err) {
1617 kfree_skb(skb);
1618 return ERR_PTR(err);
1619 }
1620
1621 return skb;
1622 }
1623
1624 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1625 * Must be called with RCU read lock.
1626 */
xfrm_nlmsg_multicast(struct net * net,struct sk_buff * skb,u32 pid,unsigned int group)1627 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1628 u32 pid, unsigned int group)
1629 {
1630 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1631 struct xfrm_translator *xtr;
1632
1633 if (!nlsk) {
1634 kfree_skb(skb);
1635 return -EPIPE;
1636 }
1637
1638 xtr = xfrm_get_translator();
1639 if (xtr) {
1640 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1641
1642 xfrm_put_translator(xtr);
1643 if (err) {
1644 kfree_skb(skb);
1645 return err;
1646 }
1647 }
1648
1649 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1650 }
1651
xfrm_spdinfo_msgsize(void)1652 static inline unsigned int xfrm_spdinfo_msgsize(void)
1653 {
1654 return NLMSG_ALIGN(4)
1655 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1656 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1657 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1658 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1659 }
1660
build_spdinfo(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,u32 flags)1661 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1662 u32 portid, u32 seq, u32 flags)
1663 {
1664 struct xfrmk_spdinfo si;
1665 struct xfrmu_spdinfo spc;
1666 struct xfrmu_spdhinfo sph;
1667 struct xfrmu_spdhthresh spt4, spt6;
1668 struct nlmsghdr *nlh;
1669 int err;
1670 u32 *f;
1671 unsigned lseq;
1672
1673 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1674 if (nlh == NULL) /* shouldn't really happen ... */
1675 return -EMSGSIZE;
1676
1677 f = nlmsg_data(nlh);
1678 *f = flags;
1679 xfrm_spd_getinfo(net, &si);
1680 spc.incnt = si.incnt;
1681 spc.outcnt = si.outcnt;
1682 spc.fwdcnt = si.fwdcnt;
1683 spc.inscnt = si.inscnt;
1684 spc.outscnt = si.outscnt;
1685 spc.fwdscnt = si.fwdscnt;
1686 sph.spdhcnt = si.spdhcnt;
1687 sph.spdhmcnt = si.spdhmcnt;
1688
1689 do {
1690 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1691
1692 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1693 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1694 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1695 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1696 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1697
1698 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1699 if (!err)
1700 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1701 if (!err)
1702 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1703 if (!err)
1704 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1705 if (err) {
1706 nlmsg_cancel(skb, nlh);
1707 return err;
1708 }
1709
1710 nlmsg_end(skb, nlh);
1711 return 0;
1712 }
1713
xfrm_set_spdinfo(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)1714 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1715 struct nlattr **attrs,
1716 struct netlink_ext_ack *extack)
1717 {
1718 struct net *net = sock_net(skb->sk);
1719 struct xfrmu_spdhthresh *thresh4 = NULL;
1720 struct xfrmu_spdhthresh *thresh6 = NULL;
1721
1722 /* selector prefixlen thresholds to hash policies */
1723 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1724 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1725
1726 if (nla_len(rta) < sizeof(*thresh4)) {
1727 NL_SET_ERR_MSG(extack, "Invalid SPD_IPV4_HTHRESH attribute length");
1728 return -EINVAL;
1729 }
1730 thresh4 = nla_data(rta);
1731 if (thresh4->lbits > 32 || thresh4->rbits > 32) {
1732 NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 32 for IPv4)");
1733 return -EINVAL;
1734 }
1735 }
1736 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1737 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1738
1739 if (nla_len(rta) < sizeof(*thresh6)) {
1740 NL_SET_ERR_MSG(extack, "Invalid SPD_IPV6_HTHRESH attribute length");
1741 return -EINVAL;
1742 }
1743 thresh6 = nla_data(rta);
1744 if (thresh6->lbits > 128 || thresh6->rbits > 128) {
1745 NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 128 for IPv6)");
1746 return -EINVAL;
1747 }
1748 }
1749
1750 if (thresh4 || thresh6) {
1751 write_seqlock(&net->xfrm.policy_hthresh.lock);
1752 if (thresh4) {
1753 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1754 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1755 }
1756 if (thresh6) {
1757 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1758 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1759 }
1760 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1761
1762 xfrm_policy_hash_rebuild(net);
1763 }
1764
1765 return 0;
1766 }
1767
xfrm_get_spdinfo(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)1768 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1769 struct nlattr **attrs,
1770 struct netlink_ext_ack *extack)
1771 {
1772 struct net *net = sock_net(skb->sk);
1773 struct sk_buff *r_skb;
1774 u32 *flags = nlmsg_data(nlh);
1775 u32 sportid = NETLINK_CB(skb).portid;
1776 u32 seq = nlh->nlmsg_seq;
1777 int err;
1778
1779 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1780 if (r_skb == NULL)
1781 return -ENOMEM;
1782
1783 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1784 BUG_ON(err < 0);
1785
1786 return nlmsg_unicast(xfrm_net_nlsk(net, skb), r_skb, sportid);
1787 }
1788
xfrm_sadinfo_msgsize(void)1789 static inline unsigned int xfrm_sadinfo_msgsize(void)
1790 {
1791 return NLMSG_ALIGN(4)
1792 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1793 + nla_total_size(4); /* XFRMA_SAD_CNT */
1794 }
1795
build_sadinfo(struct sk_buff * skb,struct net * net,u32 portid,u32 seq,u32 flags)1796 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1797 u32 portid, u32 seq, u32 flags)
1798 {
1799 struct xfrmk_sadinfo si;
1800 struct xfrmu_sadhinfo sh;
1801 struct nlmsghdr *nlh;
1802 int err;
1803 u32 *f;
1804
1805 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1806 if (nlh == NULL) /* shouldn't really happen ... */
1807 return -EMSGSIZE;
1808
1809 f = nlmsg_data(nlh);
1810 *f = flags;
1811 xfrm_sad_getinfo(net, &si);
1812
1813 sh.sadhmcnt = si.sadhmcnt;
1814 sh.sadhcnt = si.sadhcnt;
1815
1816 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1817 if (!err)
1818 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1819 if (err) {
1820 nlmsg_cancel(skb, nlh);
1821 return err;
1822 }
1823
1824 nlmsg_end(skb, nlh);
1825 return 0;
1826 }
1827
xfrm_get_sadinfo(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)1828 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1829 struct nlattr **attrs,
1830 struct netlink_ext_ack *extack)
1831 {
1832 struct net *net = sock_net(skb->sk);
1833 struct sk_buff *r_skb;
1834 u32 *flags = nlmsg_data(nlh);
1835 u32 sportid = NETLINK_CB(skb).portid;
1836 u32 seq = nlh->nlmsg_seq;
1837 int err;
1838
1839 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1840 if (r_skb == NULL)
1841 return -ENOMEM;
1842
1843 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1844 BUG_ON(err < 0);
1845
1846 return nlmsg_unicast(xfrm_net_nlsk(net, skb), r_skb, sportid);
1847 }
1848
xfrm_get_sa(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)1849 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1850 struct nlattr **attrs, struct netlink_ext_ack *extack)
1851 {
1852 struct net *net = sock_net(skb->sk);
1853 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1854 struct xfrm_state *x;
1855 struct sk_buff *resp_skb;
1856 int err = -ESRCH;
1857
1858 x = xfrm_user_state_lookup(net, p, attrs, &err);
1859 if (x == NULL)
1860 goto out_noput;
1861
1862 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1863 if (IS_ERR(resp_skb)) {
1864 err = PTR_ERR(resp_skb);
1865 } else {
1866 err = nlmsg_unicast(xfrm_net_nlsk(net, skb), resp_skb, NETLINK_CB(skb).portid);
1867 }
1868 xfrm_state_put(x);
1869 out_noput:
1870 return err;
1871 }
1872
xfrm_alloc_userspi(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)1873 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1874 struct nlattr **attrs,
1875 struct netlink_ext_ack *extack)
1876 {
1877 struct net *net = sock_net(skb->sk);
1878 struct xfrm_state *x;
1879 struct xfrm_userspi_info *p;
1880 struct sk_buff *resp_skb;
1881 xfrm_address_t *daddr;
1882 int family;
1883 int err;
1884 u32 mark;
1885 struct xfrm_mark m;
1886 u32 if_id = 0;
1887 u32 pcpu_num = UINT_MAX;
1888
1889 p = nlmsg_data(nlh);
1890 err = verify_spi_info(p->info.id.proto, p->min, p->max, extack);
1891 if (err)
1892 goto out_noput;
1893
1894 family = p->info.family;
1895 daddr = &p->info.id.daddr;
1896
1897 x = NULL;
1898
1899 mark = xfrm_mark_get(attrs, &m);
1900
1901 if (attrs[XFRMA_IF_ID])
1902 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1903
1904 if (attrs[XFRMA_SA_PCPU]) {
1905 pcpu_num = nla_get_u32(attrs[XFRMA_SA_PCPU]);
1906 if (pcpu_num >= num_possible_cpus()) {
1907 err = -EINVAL;
1908 NL_SET_ERR_MSG(extack, "pCPU number too big");
1909 goto out_noput;
1910 }
1911 }
1912
1913 if (p->info.seq) {
1914 x = xfrm_find_acq_byseq(net, mark, p->info.seq, pcpu_num);
1915 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1916 xfrm_state_put(x);
1917 x = NULL;
1918 }
1919 }
1920
1921 if (!x)
1922 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1923 if_id, pcpu_num, p->info.id.proto, daddr,
1924 &p->info.saddr, 1,
1925 family);
1926 err = -ENOENT;
1927 if (!x) {
1928 NL_SET_ERR_MSG(extack, "Target ACQUIRE not found");
1929 goto out_noput;
1930 }
1931
1932 err = xfrm_alloc_spi(x, p->min, p->max, extack);
1933 if (err)
1934 goto out;
1935
1936 if (attrs[XFRMA_SA_DIR])
1937 x->dir = nla_get_u8(attrs[XFRMA_SA_DIR]);
1938
1939 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1940 if (IS_ERR(resp_skb)) {
1941 err = PTR_ERR(resp_skb);
1942 goto out;
1943 }
1944
1945 err = nlmsg_unicast(xfrm_net_nlsk(net, skb), resp_skb, NETLINK_CB(skb).portid);
1946
1947 out:
1948 xfrm_state_put(x);
1949 out_noput:
1950 return err;
1951 }
1952
verify_policy_dir(u8 dir,struct netlink_ext_ack * extack)1953 static int verify_policy_dir(u8 dir, struct netlink_ext_ack *extack)
1954 {
1955 switch (dir) {
1956 case XFRM_POLICY_IN:
1957 case XFRM_POLICY_OUT:
1958 case XFRM_POLICY_FWD:
1959 break;
1960
1961 default:
1962 NL_SET_ERR_MSG(extack, "Invalid policy direction");
1963 return -EINVAL;
1964 }
1965
1966 return 0;
1967 }
1968
verify_policy_type(u8 type,struct netlink_ext_ack * extack)1969 static int verify_policy_type(u8 type, struct netlink_ext_ack *extack)
1970 {
1971 switch (type) {
1972 case XFRM_POLICY_TYPE_MAIN:
1973 #ifdef CONFIG_XFRM_SUB_POLICY
1974 case XFRM_POLICY_TYPE_SUB:
1975 #endif
1976 break;
1977
1978 default:
1979 NL_SET_ERR_MSG(extack, "Invalid policy type");
1980 return -EINVAL;
1981 }
1982
1983 return 0;
1984 }
1985
verify_newpolicy_info(struct xfrm_userpolicy_info * p,struct netlink_ext_ack * extack)1986 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p,
1987 struct netlink_ext_ack *extack)
1988 {
1989 int ret;
1990
1991 switch (p->share) {
1992 case XFRM_SHARE_ANY:
1993 case XFRM_SHARE_SESSION:
1994 case XFRM_SHARE_USER:
1995 case XFRM_SHARE_UNIQUE:
1996 break;
1997
1998 default:
1999 NL_SET_ERR_MSG(extack, "Invalid policy share");
2000 return -EINVAL;
2001 }
2002
2003 switch (p->action) {
2004 case XFRM_POLICY_ALLOW:
2005 case XFRM_POLICY_BLOCK:
2006 break;
2007
2008 default:
2009 NL_SET_ERR_MSG(extack, "Invalid policy action");
2010 return -EINVAL;
2011 }
2012
2013 switch (p->sel.family) {
2014 case AF_INET:
2015 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
2016 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
2017 return -EINVAL;
2018 }
2019
2020 break;
2021
2022 case AF_INET6:
2023 #if IS_ENABLED(CONFIG_IPV6)
2024 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
2025 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
2026 return -EINVAL;
2027 }
2028
2029 break;
2030 #else
2031 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
2032 return -EAFNOSUPPORT;
2033 #endif
2034
2035 default:
2036 NL_SET_ERR_MSG(extack, "Invalid selector family");
2037 return -EINVAL;
2038 }
2039
2040 ret = verify_policy_dir(p->dir, extack);
2041 if (ret)
2042 return ret;
2043 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) {
2044 NL_SET_ERR_MSG(extack, "Policy index doesn't match direction");
2045 return -EINVAL;
2046 }
2047
2048 return 0;
2049 }
2050
copy_from_user_sec_ctx(struct xfrm_policy * pol,struct nlattr ** attrs)2051 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
2052 {
2053 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2054 struct xfrm_user_sec_ctx *uctx;
2055
2056 if (!rt)
2057 return 0;
2058
2059 uctx = nla_data(rt);
2060 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
2061 }
2062
copy_templates(struct xfrm_policy * xp,struct xfrm_user_tmpl * ut,int nr)2063 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
2064 int nr)
2065 {
2066 int i;
2067
2068 xp->xfrm_nr = nr;
2069 for (i = 0; i < nr; i++, ut++) {
2070 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2071
2072 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
2073 memcpy(&t->saddr, &ut->saddr,
2074 sizeof(xfrm_address_t));
2075 t->reqid = ut->reqid;
2076 t->mode = ut->mode;
2077 t->share = ut->share;
2078 t->optional = ut->optional;
2079 t->aalgos = ut->aalgos;
2080 t->ealgos = ut->ealgos;
2081 t->calgos = ut->calgos;
2082 /* If all masks are ~0, then we allow all algorithms. */
2083 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
2084 t->encap_family = ut->family;
2085 }
2086 }
2087
validate_tmpl(int nr,struct xfrm_user_tmpl * ut,u16 family,int dir,struct netlink_ext_ack * extack)2088 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family,
2089 int dir, struct netlink_ext_ack *extack)
2090 {
2091 u16 prev_family;
2092 int i;
2093
2094 if (nr > XFRM_MAX_DEPTH) {
2095 NL_SET_ERR_MSG(extack, "Template count must be <= XFRM_MAX_DEPTH (" __stringify(XFRM_MAX_DEPTH) ")");
2096 return -EINVAL;
2097 }
2098
2099 prev_family = family;
2100
2101 for (i = 0; i < nr; i++) {
2102 /* We never validated the ut->family value, so many
2103 * applications simply leave it at zero. The check was
2104 * never made and ut->family was ignored because all
2105 * templates could be assumed to have the same family as
2106 * the policy itself. Now that we will have ipv4-in-ipv6
2107 * and ipv6-in-ipv4 tunnels, this is no longer true.
2108 */
2109 if (!ut[i].family)
2110 ut[i].family = family;
2111
2112 switch (ut[i].mode) {
2113 case XFRM_MODE_TUNNEL:
2114 case XFRM_MODE_BEET:
2115 case XFRM_MODE_IPTFS:
2116 if (ut[i].optional && dir == XFRM_POLICY_OUT) {
2117 NL_SET_ERR_MSG(extack, "Mode in optional template not allowed in outbound policy");
2118 return -EINVAL;
2119 }
2120 break;
2121 default:
2122 if (ut[i].family != prev_family) {
2123 NL_SET_ERR_MSG(extack, "Mode in template doesn't support a family change");
2124 return -EINVAL;
2125 }
2126 break;
2127 }
2128 if (ut[i].mode >= XFRM_MODE_MAX) {
2129 NL_SET_ERR_MSG(extack, "Mode in template must be < XFRM_MODE_MAX (" __stringify(XFRM_MODE_MAX) ")");
2130 return -EINVAL;
2131 }
2132
2133 prev_family = ut[i].family;
2134
2135 switch (ut[i].family) {
2136 case AF_INET:
2137 break;
2138 #if IS_ENABLED(CONFIG_IPV6)
2139 case AF_INET6:
2140 break;
2141 #endif
2142 default:
2143 NL_SET_ERR_MSG(extack, "Invalid family in template");
2144 return -EINVAL;
2145 }
2146
2147 if (!xfrm_id_proto_valid(ut[i].id.proto)) {
2148 NL_SET_ERR_MSG(extack, "Invalid XFRM protocol in template");
2149 return -EINVAL;
2150 }
2151 }
2152
2153 return 0;
2154 }
2155
copy_from_user_tmpl(struct xfrm_policy * pol,struct nlattr ** attrs,int dir,struct netlink_ext_ack * extack)2156 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs,
2157 int dir, struct netlink_ext_ack *extack)
2158 {
2159 struct nlattr *rt = attrs[XFRMA_TMPL];
2160
2161 if (!rt) {
2162 pol->xfrm_nr = 0;
2163 } else {
2164 struct xfrm_user_tmpl *utmpl = nla_data(rt);
2165 int nr = nla_len(rt) / sizeof(*utmpl);
2166 int err;
2167
2168 err = validate_tmpl(nr, utmpl, pol->family, dir, extack);
2169 if (err)
2170 return err;
2171
2172 copy_templates(pol, utmpl, nr);
2173 }
2174 return 0;
2175 }
2176
copy_from_user_policy_type(u8 * tp,struct nlattr ** attrs,struct netlink_ext_ack * extack)2177 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs,
2178 struct netlink_ext_ack *extack)
2179 {
2180 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
2181 struct xfrm_userpolicy_type *upt;
2182 u8 type = XFRM_POLICY_TYPE_MAIN;
2183 int err;
2184
2185 if (rt) {
2186 upt = nla_data(rt);
2187 type = upt->type;
2188 }
2189
2190 err = verify_policy_type(type, extack);
2191 if (err)
2192 return err;
2193
2194 *tp = type;
2195 return 0;
2196 }
2197
copy_from_user_policy(struct xfrm_policy * xp,struct xfrm_userpolicy_info * p)2198 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
2199 {
2200 xp->priority = p->priority;
2201 xp->index = p->index;
2202 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
2203 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
2204 xp->action = p->action;
2205 xp->flags = p->flags;
2206 xp->family = p->sel.family;
2207 /* XXX xp->share = p->share; */
2208 }
2209
copy_to_user_policy(struct xfrm_policy * xp,struct xfrm_userpolicy_info * p,int dir)2210 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
2211 {
2212 memset(p, 0, sizeof(*p));
2213 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
2214 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
2215 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
2216 p->priority = xp->priority;
2217 p->index = xp->index;
2218 p->sel.family = xp->family;
2219 p->dir = dir;
2220 p->action = xp->action;
2221 p->flags = xp->flags;
2222 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
2223 }
2224
xfrm_policy_construct(struct net * net,struct xfrm_userpolicy_info * p,struct nlattr ** attrs,int * errp,struct netlink_ext_ack * extack)2225 static struct xfrm_policy *xfrm_policy_construct(struct net *net,
2226 struct xfrm_userpolicy_info *p,
2227 struct nlattr **attrs,
2228 int *errp,
2229 struct netlink_ext_ack *extack)
2230 {
2231 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
2232 int err;
2233
2234 if (!xp) {
2235 *errp = -ENOMEM;
2236 return NULL;
2237 }
2238
2239 copy_from_user_policy(xp, p);
2240
2241 err = copy_from_user_policy_type(&xp->type, attrs, extack);
2242 if (err)
2243 goto error;
2244
2245 if (!(err = copy_from_user_tmpl(xp, attrs, p->dir, extack)))
2246 err = copy_from_user_sec_ctx(xp, attrs);
2247 if (err)
2248 goto error;
2249
2250 xfrm_mark_get(attrs, &xp->mark);
2251
2252 if (attrs[XFRMA_IF_ID])
2253 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2254
2255 /* configure the hardware if offload is requested */
2256 if (attrs[XFRMA_OFFLOAD_DEV]) {
2257 err = xfrm_dev_policy_add(net, xp,
2258 nla_data(attrs[XFRMA_OFFLOAD_DEV]),
2259 p->dir, extack);
2260 if (err)
2261 goto error;
2262 }
2263
2264 return xp;
2265 error:
2266 *errp = err;
2267 xp->walk.dead = 1;
2268 xfrm_policy_destroy(xp);
2269 return NULL;
2270 }
2271
xfrm_add_policy(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)2272 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2273 struct nlattr **attrs,
2274 struct netlink_ext_ack *extack)
2275 {
2276 struct net *net = sock_net(skb->sk);
2277 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
2278 struct xfrm_policy *xp;
2279 struct km_event c;
2280 int err;
2281 int excl;
2282
2283 err = verify_newpolicy_info(p, extack);
2284 if (err)
2285 return err;
2286 err = verify_sec_ctx_len(attrs, extack);
2287 if (err)
2288 return err;
2289
2290 xp = xfrm_policy_construct(net, p, attrs, &err, extack);
2291 if (!xp)
2292 return err;
2293
2294 /* shouldn't excl be based on nlh flags??
2295 * Aha! this is anti-netlink really i.e more pfkey derived
2296 * in netlink excl is a flag and you wouldn't need
2297 * a type XFRM_MSG_UPDPOLICY - JHS */
2298 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
2299 err = xfrm_policy_insert(p->dir, xp, excl);
2300 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
2301
2302 if (err) {
2303 xfrm_dev_policy_delete(xp);
2304 xp->walk.dead = 1;
2305 xfrm_policy_destroy(xp);
2306 return err;
2307 }
2308
2309 c.event = nlh->nlmsg_type;
2310 c.seq = nlh->nlmsg_seq;
2311 c.portid = nlh->nlmsg_pid;
2312 km_policy_notify(xp, p->dir, &c);
2313
2314 xfrm_pol_put(xp);
2315
2316 return 0;
2317 }
2318
copy_to_user_tmpl(struct xfrm_policy * xp,struct sk_buff * skb)2319 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
2320 {
2321 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
2322 int i;
2323
2324 if (xp->xfrm_nr == 0)
2325 return 0;
2326
2327 if (xp->xfrm_nr > XFRM_MAX_DEPTH)
2328 return -ENOBUFS;
2329
2330 for (i = 0; i < xp->xfrm_nr; i++) {
2331 struct xfrm_user_tmpl *up = &vec[i];
2332 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
2333
2334 memset(up, 0, sizeof(*up));
2335 memcpy(&up->id, &kp->id, sizeof(up->id));
2336 up->family = kp->encap_family;
2337 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
2338 up->reqid = kp->reqid;
2339 up->mode = kp->mode;
2340 up->share = kp->share;
2341 up->optional = kp->optional;
2342 up->aalgos = kp->aalgos;
2343 up->ealgos = kp->ealgos;
2344 up->calgos = kp->calgos;
2345 }
2346
2347 return nla_put(skb, XFRMA_TMPL,
2348 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
2349 }
2350
copy_to_user_state_sec_ctx(struct xfrm_state * x,struct sk_buff * skb)2351 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
2352 {
2353 if (x->security) {
2354 return copy_sec_ctx(x->security, skb);
2355 }
2356 return 0;
2357 }
2358
copy_to_user_sec_ctx(struct xfrm_policy * xp,struct sk_buff * skb)2359 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
2360 {
2361 if (xp->security)
2362 return copy_sec_ctx(xp->security, skb);
2363 return 0;
2364 }
userpolicy_type_attrsize(void)2365 static inline unsigned int userpolicy_type_attrsize(void)
2366 {
2367 #ifdef CONFIG_XFRM_SUB_POLICY
2368 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
2369 #else
2370 return 0;
2371 #endif
2372 }
2373
2374 #ifdef CONFIG_XFRM_SUB_POLICY
copy_to_user_policy_type(u8 type,struct sk_buff * skb)2375 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
2376 {
2377 struct xfrm_userpolicy_type upt;
2378
2379 /* Sadly there are two holes in struct xfrm_userpolicy_type */
2380 memset(&upt, 0, sizeof(upt));
2381 upt.type = type;
2382
2383 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
2384 }
2385
2386 #else
copy_to_user_policy_type(u8 type,struct sk_buff * skb)2387 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
2388 {
2389 return 0;
2390 }
2391 #endif
2392
dump_one_policy(struct xfrm_policy * xp,int dir,int count,void * ptr)2393 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
2394 {
2395 struct xfrm_dump_info *sp = ptr;
2396 struct xfrm_userpolicy_info *p;
2397 struct sk_buff *in_skb = sp->in_skb;
2398 struct sk_buff *skb = sp->out_skb;
2399 struct xfrm_translator *xtr;
2400 struct nlmsghdr *nlh;
2401 int err;
2402
2403 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
2404 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
2405 if (nlh == NULL)
2406 return -EMSGSIZE;
2407
2408 p = nlmsg_data(nlh);
2409 copy_to_user_policy(xp, p, dir);
2410 err = copy_to_user_tmpl(xp, skb);
2411 if (!err)
2412 err = copy_to_user_sec_ctx(xp, skb);
2413 if (!err)
2414 err = copy_to_user_policy_type(xp->type, skb);
2415 if (!err)
2416 err = xfrm_mark_put(skb, &xp->mark);
2417 if (!err)
2418 err = xfrm_if_id_put(skb, xp->if_id);
2419 if (!err && xp->xdo.dev)
2420 err = copy_user_offload(&xp->xdo, skb);
2421 if (err) {
2422 nlmsg_cancel(skb, nlh);
2423 return err;
2424 }
2425 nlmsg_end(skb, nlh);
2426
2427 xtr = xfrm_get_translator();
2428 if (xtr) {
2429 err = xtr->alloc_compat(skb, nlh);
2430
2431 xfrm_put_translator(xtr);
2432 if (err) {
2433 nlmsg_cancel(skb, nlh);
2434 return err;
2435 }
2436 }
2437
2438 return 0;
2439 }
2440
xfrm_dump_policy_done(struct netlink_callback * cb)2441 static int xfrm_dump_policy_done(struct netlink_callback *cb)
2442 {
2443 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2444 struct net *net = sock_net(cb->skb->sk);
2445
2446 xfrm_policy_walk_done(walk, net);
2447 return 0;
2448 }
2449
xfrm_dump_policy_start(struct netlink_callback * cb)2450 static int xfrm_dump_policy_start(struct netlink_callback *cb)
2451 {
2452 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2453
2454 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
2455
2456 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
2457 return 0;
2458 }
2459
xfrm_dump_policy(struct sk_buff * skb,struct netlink_callback * cb)2460 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
2461 {
2462 struct net *net = sock_net(skb->sk);
2463 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2464 struct xfrm_dump_info info;
2465
2466 info.in_skb = cb->skb;
2467 info.out_skb = skb;
2468 info.nlmsg_seq = cb->nlh->nlmsg_seq;
2469 info.nlmsg_flags = NLM_F_MULTI;
2470
2471 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
2472
2473 return skb->len;
2474 }
2475
xfrm_policy_netlink(struct sk_buff * in_skb,struct xfrm_policy * xp,int dir,u32 seq)2476 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
2477 struct xfrm_policy *xp,
2478 int dir, u32 seq)
2479 {
2480 struct xfrm_dump_info info;
2481 struct sk_buff *skb;
2482 int err;
2483
2484 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2485 if (!skb)
2486 return ERR_PTR(-ENOMEM);
2487
2488 info.in_skb = in_skb;
2489 info.out_skb = skb;
2490 info.nlmsg_seq = seq;
2491 info.nlmsg_flags = 0;
2492
2493 err = dump_one_policy(xp, dir, 0, &info);
2494 if (err) {
2495 kfree_skb(skb);
2496 return ERR_PTR(err);
2497 }
2498
2499 return skb;
2500 }
2501
xfrm_notify_userpolicy(struct net * net)2502 static int xfrm_notify_userpolicy(struct net *net)
2503 {
2504 struct xfrm_userpolicy_default *up;
2505 int len = NLMSG_ALIGN(sizeof(*up));
2506 struct nlmsghdr *nlh;
2507 struct sk_buff *skb;
2508 int err;
2509
2510 skb = nlmsg_new(len, GFP_ATOMIC);
2511 if (skb == NULL)
2512 return -ENOMEM;
2513
2514 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
2515 if (nlh == NULL) {
2516 kfree_skb(skb);
2517 return -EMSGSIZE;
2518 }
2519
2520 up = nlmsg_data(nlh);
2521 up->in = READ_ONCE(net->xfrm.policy_default[XFRM_POLICY_IN]);
2522 up->fwd = READ_ONCE(net->xfrm.policy_default[XFRM_POLICY_FWD]);
2523 up->out = READ_ONCE(net->xfrm.policy_default[XFRM_POLICY_OUT]);
2524
2525 nlmsg_end(skb, nlh);
2526
2527 rcu_read_lock();
2528 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2529 rcu_read_unlock();
2530
2531 return err;
2532 }
2533
xfrm_userpolicy_is_valid(__u8 policy)2534 static bool xfrm_userpolicy_is_valid(__u8 policy)
2535 {
2536 return policy == XFRM_USERPOLICY_BLOCK ||
2537 policy == XFRM_USERPOLICY_ACCEPT;
2538 }
2539
xfrm_set_default(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)2540 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2541 struct nlattr **attrs, struct netlink_ext_ack *extack)
2542 {
2543 struct net *net = sock_net(skb->sk);
2544 struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
2545
2546 if (xfrm_userpolicy_is_valid(up->in))
2547 WRITE_ONCE(net->xfrm.policy_default[XFRM_POLICY_IN], up->in);
2548
2549 if (xfrm_userpolicy_is_valid(up->fwd))
2550 WRITE_ONCE(net->xfrm.policy_default[XFRM_POLICY_FWD], up->fwd);
2551
2552 if (xfrm_userpolicy_is_valid(up->out))
2553 WRITE_ONCE(net->xfrm.policy_default[XFRM_POLICY_OUT], up->out);
2554
2555 rt_genid_bump_all(net);
2556
2557 xfrm_notify_userpolicy(net);
2558 return 0;
2559 }
2560
xfrm_get_default(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)2561 static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2562 struct nlattr **attrs, struct netlink_ext_ack *extack)
2563 {
2564 struct sk_buff *r_skb;
2565 struct nlmsghdr *r_nlh;
2566 struct net *net = sock_net(skb->sk);
2567 struct xfrm_userpolicy_default *r_up;
2568 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
2569 u32 portid = NETLINK_CB(skb).portid;
2570 u32 seq = nlh->nlmsg_seq;
2571
2572 r_skb = nlmsg_new(len, GFP_ATOMIC);
2573 if (!r_skb)
2574 return -ENOMEM;
2575
2576 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
2577 if (!r_nlh) {
2578 kfree_skb(r_skb);
2579 return -EMSGSIZE;
2580 }
2581
2582 r_up = nlmsg_data(r_nlh);
2583 r_up->in = READ_ONCE(net->xfrm.policy_default[XFRM_POLICY_IN]);
2584 r_up->fwd = READ_ONCE(net->xfrm.policy_default[XFRM_POLICY_FWD]);
2585 r_up->out = READ_ONCE(net->xfrm.policy_default[XFRM_POLICY_OUT]);
2586 nlmsg_end(r_skb, r_nlh);
2587
2588 return nlmsg_unicast(xfrm_net_nlsk(net, skb), r_skb, portid);
2589 }
2590
xfrm_get_policy(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)2591 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2592 struct nlattr **attrs,
2593 struct netlink_ext_ack *extack)
2594 {
2595 struct net *net = sock_net(skb->sk);
2596 struct xfrm_policy *xp;
2597 struct xfrm_userpolicy_id *p;
2598 u8 type = XFRM_POLICY_TYPE_MAIN;
2599 int err;
2600 struct km_event c;
2601 int delete;
2602 struct xfrm_mark m;
2603 u32 if_id = 0;
2604
2605 p = nlmsg_data(nlh);
2606 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
2607
2608 err = copy_from_user_policy_type(&type, attrs, extack);
2609 if (err)
2610 return err;
2611
2612 err = verify_policy_dir(p->dir, extack);
2613 if (err)
2614 return err;
2615
2616 if (attrs[XFRMA_IF_ID])
2617 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2618
2619 xfrm_mark_get(attrs, &m);
2620
2621 if (p->index)
2622 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
2623 p->index, delete, &err);
2624 else {
2625 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2626 struct xfrm_sec_ctx *ctx;
2627
2628 err = verify_sec_ctx_len(attrs, extack);
2629 if (err)
2630 return err;
2631
2632 ctx = NULL;
2633 if (rt) {
2634 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2635
2636 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2637 if (err)
2638 return err;
2639 }
2640 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2641 &p->sel, ctx, delete, &err);
2642 security_xfrm_policy_free(ctx);
2643 }
2644 if (xp == NULL)
2645 return -ENOENT;
2646
2647 if (!delete) {
2648 struct sk_buff *resp_skb;
2649
2650 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
2651 if (IS_ERR(resp_skb)) {
2652 err = PTR_ERR(resp_skb);
2653 } else {
2654 err = nlmsg_unicast(xfrm_net_nlsk(net, skb), resp_skb,
2655 NETLINK_CB(skb).portid);
2656 }
2657 } else {
2658 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2659
2660 if (err != 0)
2661 goto out;
2662
2663 c.data.byid = p->index;
2664 c.event = nlh->nlmsg_type;
2665 c.seq = nlh->nlmsg_seq;
2666 c.portid = nlh->nlmsg_pid;
2667 km_policy_notify(xp, p->dir, &c);
2668 }
2669
2670 out:
2671 xfrm_pol_put(xp);
2672 return err;
2673 }
2674
xfrm_flush_sa(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)2675 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
2676 struct nlattr **attrs,
2677 struct netlink_ext_ack *extack)
2678 {
2679 struct net *net = sock_net(skb->sk);
2680 struct km_event c;
2681 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
2682 int err;
2683
2684 err = xfrm_state_flush(net, p->proto, true);
2685 if (err) {
2686 if (err == -ESRCH) /* empty table */
2687 return 0;
2688 return err;
2689 }
2690 c.data.proto = p->proto;
2691 c.event = nlh->nlmsg_type;
2692 c.seq = nlh->nlmsg_seq;
2693 c.portid = nlh->nlmsg_pid;
2694 c.net = net;
2695 km_state_notify(NULL, &c);
2696
2697 return 0;
2698 }
2699
xfrm_aevent_msgsize(struct xfrm_state * x)2700 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2701 {
2702 unsigned int replay_size = x->replay_esn ?
2703 xfrm_replay_state_esn_len(x->replay_esn) :
2704 sizeof(struct xfrm_replay_state);
2705
2706 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2707 + nla_total_size(replay_size)
2708 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2709 + nla_total_size(sizeof(struct xfrm_mark))
2710 + nla_total_size(4) /* XFRM_AE_RTHR */
2711 + nla_total_size(4) /* XFRM_AE_ETHR */
2712 + nla_total_size(sizeof(x->dir)) /* XFRMA_SA_DIR */
2713 + nla_total_size(4) /* XFRMA_SA_PCPU */
2714 + nla_total_size(sizeof(x->if_id)); /* XFRMA_IF_ID */
2715 }
2716
build_aevent(struct sk_buff * skb,struct xfrm_state * x,const struct km_event * c)2717 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2718 {
2719 struct xfrm_aevent_id *id;
2720 struct nlmsghdr *nlh;
2721 int err;
2722
2723 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2724 if (nlh == NULL)
2725 return -EMSGSIZE;
2726
2727 id = nlmsg_data(nlh);
2728 memset(&id->sa_id, 0, sizeof(id->sa_id));
2729 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2730 id->sa_id.spi = x->id.spi;
2731 id->sa_id.family = x->props.family;
2732 id->sa_id.proto = x->id.proto;
2733 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2734 id->reqid = x->props.reqid;
2735 id->flags = c->data.aevent;
2736
2737 if (x->replay_esn) {
2738 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2739 xfrm_replay_state_esn_len(x->replay_esn),
2740 x->replay_esn);
2741 } else {
2742 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2743 &x->replay);
2744 }
2745 if (err)
2746 goto out_cancel;
2747 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2748 XFRMA_PAD);
2749 if (err)
2750 goto out_cancel;
2751
2752 if (id->flags & XFRM_AE_RTHR) {
2753 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2754 if (err)
2755 goto out_cancel;
2756 }
2757 if (id->flags & XFRM_AE_ETHR) {
2758 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2759 x->replay_maxage * 10 / HZ);
2760 if (err)
2761 goto out_cancel;
2762 }
2763 err = xfrm_mark_put(skb, &x->mark);
2764 if (err)
2765 goto out_cancel;
2766
2767 err = xfrm_if_id_put(skb, x->if_id);
2768 if (err)
2769 goto out_cancel;
2770 if (x->pcpu_num != UINT_MAX) {
2771 err = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
2772 if (err)
2773 goto out_cancel;
2774 }
2775
2776 if (x->dir) {
2777 err = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
2778 if (err)
2779 goto out_cancel;
2780 }
2781
2782 nlmsg_end(skb, nlh);
2783 return 0;
2784
2785 out_cancel:
2786 nlmsg_cancel(skb, nlh);
2787 return err;
2788 }
2789
xfrm_get_ae(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)2790 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2791 struct nlattr **attrs, struct netlink_ext_ack *extack)
2792 {
2793 struct net *net = sock_net(skb->sk);
2794 struct xfrm_state *x;
2795 struct sk_buff *r_skb;
2796 int err;
2797 struct km_event c;
2798 u32 mark;
2799 struct xfrm_mark m;
2800 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2801 struct xfrm_usersa_id *id = &p->sa_id;
2802
2803 mark = xfrm_mark_get(attrs, &m);
2804
2805 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2806 if (x == NULL)
2807 return -ESRCH;
2808
2809 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2810 if (r_skb == NULL) {
2811 xfrm_state_put(x);
2812 return -ENOMEM;
2813 }
2814
2815 /*
2816 * XXX: is this lock really needed - none of the other
2817 * gets lock (the concern is things getting updated
2818 * while we are still reading) - jhs
2819 */
2820 spin_lock_bh(&x->lock);
2821 c.data.aevent = p->flags;
2822 c.seq = nlh->nlmsg_seq;
2823 c.portid = nlh->nlmsg_pid;
2824
2825 err = build_aevent(r_skb, x, &c);
2826 if (err < 0) {
2827 spin_unlock_bh(&x->lock);
2828 xfrm_state_put(x);
2829 kfree_skb(r_skb);
2830 return err;
2831 }
2832
2833 err = nlmsg_unicast(xfrm_net_nlsk(net, skb), r_skb, NETLINK_CB(skb).portid);
2834 spin_unlock_bh(&x->lock);
2835 xfrm_state_put(x);
2836 return err;
2837 }
2838
xfrm_new_ae(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)2839 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2840 struct nlattr **attrs, struct netlink_ext_ack *extack)
2841 {
2842 struct net *net = sock_net(skb->sk);
2843 struct xfrm_state *x;
2844 struct km_event c;
2845 int err = -EINVAL;
2846 u32 mark = 0;
2847 struct xfrm_mark m;
2848 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2849 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2850 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2851 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2852 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2853 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2854
2855 if (!lt && !rp && !re && !et && !rt) {
2856 NL_SET_ERR_MSG(extack, "Missing required attribute for AE");
2857 return err;
2858 }
2859
2860 /* pedantic mode - thou shalt sayeth replaceth */
2861 if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) {
2862 NL_SET_ERR_MSG(extack, "NLM_F_REPLACE flag is required");
2863 return err;
2864 }
2865
2866 mark = xfrm_mark_get(attrs, &m);
2867
2868 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2869 if (x == NULL)
2870 return -ESRCH;
2871
2872 if (x->km.state != XFRM_STATE_VALID) {
2873 NL_SET_ERR_MSG(extack, "SA must be in VALID state");
2874 goto out;
2875 }
2876
2877 err = xfrm_replay_verify_len(x->replay_esn, re, extack);
2878 if (err)
2879 goto out;
2880
2881 spin_lock_bh(&x->lock);
2882 xfrm_update_ae_params(x, attrs, 1);
2883 spin_unlock_bh(&x->lock);
2884
2885 c.event = nlh->nlmsg_type;
2886 c.seq = nlh->nlmsg_seq;
2887 c.portid = nlh->nlmsg_pid;
2888 c.data.aevent = XFRM_AE_CU;
2889 km_state_notify(x, &c);
2890 err = 0;
2891 out:
2892 xfrm_state_put(x);
2893 return err;
2894 }
2895
xfrm_flush_policy(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)2896 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2897 struct nlattr **attrs,
2898 struct netlink_ext_ack *extack)
2899 {
2900 struct net *net = sock_net(skb->sk);
2901 struct km_event c;
2902 u8 type = XFRM_POLICY_TYPE_MAIN;
2903 int err;
2904
2905 err = copy_from_user_policy_type(&type, attrs, extack);
2906 if (err)
2907 return err;
2908
2909 err = xfrm_policy_flush(net, type, true);
2910 if (err) {
2911 if (err == -ESRCH) /* empty table */
2912 return 0;
2913 return err;
2914 }
2915
2916 c.data.type = type;
2917 c.event = nlh->nlmsg_type;
2918 c.seq = nlh->nlmsg_seq;
2919 c.portid = nlh->nlmsg_pid;
2920 c.net = net;
2921 km_policy_notify(NULL, 0, &c);
2922 return 0;
2923 }
2924
xfrm_add_pol_expire(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)2925 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2926 struct nlattr **attrs,
2927 struct netlink_ext_ack *extack)
2928 {
2929 struct net *net = sock_net(skb->sk);
2930 struct xfrm_policy *xp;
2931 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2932 struct xfrm_userpolicy_info *p = &up->pol;
2933 u8 type = XFRM_POLICY_TYPE_MAIN;
2934 int err = -ENOENT;
2935 struct xfrm_mark m;
2936 u32 if_id = 0;
2937
2938 err = copy_from_user_policy_type(&type, attrs, extack);
2939 if (err)
2940 return err;
2941
2942 err = verify_policy_dir(p->dir, extack);
2943 if (err)
2944 return err;
2945
2946 if (attrs[XFRMA_IF_ID])
2947 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2948
2949 xfrm_mark_get(attrs, &m);
2950
2951 if (p->index)
2952 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2953 0, &err);
2954 else {
2955 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2956 struct xfrm_sec_ctx *ctx;
2957
2958 err = verify_sec_ctx_len(attrs, extack);
2959 if (err)
2960 return err;
2961
2962 ctx = NULL;
2963 if (rt) {
2964 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2965
2966 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2967 if (err)
2968 return err;
2969 }
2970 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2971 &p->sel, ctx, 0, &err);
2972 security_xfrm_policy_free(ctx);
2973 }
2974 if (xp == NULL)
2975 return -ENOENT;
2976
2977 if (unlikely(xp->walk.dead))
2978 goto out;
2979
2980 err = 0;
2981 if (up->hard) {
2982 xfrm_policy_delete(xp, p->dir);
2983 xfrm_audit_policy_delete(xp, 1, true);
2984 }
2985 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2986
2987 out:
2988 xfrm_pol_put(xp);
2989 return err;
2990 }
2991
xfrm_add_sa_expire(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)2992 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2993 struct nlattr **attrs,
2994 struct netlink_ext_ack *extack)
2995 {
2996 struct net *net = sock_net(skb->sk);
2997 struct xfrm_state *x;
2998 int err;
2999 struct xfrm_user_expire *ue = nlmsg_data(nlh);
3000 struct xfrm_usersa_info *p = &ue->state;
3001 struct xfrm_mark m;
3002 u32 mark = xfrm_mark_get(attrs, &m);
3003
3004 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
3005
3006 err = -ENOENT;
3007 if (x == NULL)
3008 return err;
3009
3010 spin_lock_bh(&x->lock);
3011 err = -EINVAL;
3012 if (x->km.state != XFRM_STATE_VALID) {
3013 NL_SET_ERR_MSG(extack, "SA must be in VALID state");
3014 goto out;
3015 }
3016
3017 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
3018
3019 if (ue->hard) {
3020 __xfrm_state_delete(x);
3021 xfrm_audit_state_delete(x, 1, true);
3022 }
3023 err = 0;
3024 out:
3025 spin_unlock_bh(&x->lock);
3026 xfrm_state_put(x);
3027 return err;
3028 }
3029
xfrm_add_acquire(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)3030 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
3031 struct nlattr **attrs,
3032 struct netlink_ext_ack *extack)
3033 {
3034 struct net *net = sock_net(skb->sk);
3035 struct xfrm_policy *xp;
3036 struct xfrm_user_tmpl *ut;
3037 int i;
3038 struct nlattr *rt = attrs[XFRMA_TMPL];
3039 struct xfrm_mark mark;
3040
3041 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
3042 struct xfrm_state *x = xfrm_state_alloc(net);
3043 int err = -ENOMEM;
3044
3045 if (!x)
3046 goto nomem;
3047
3048 xfrm_mark_get(attrs, &mark);
3049
3050 if (attrs[XFRMA_SA_PCPU]) {
3051 x->pcpu_num = nla_get_u32(attrs[XFRMA_SA_PCPU]);
3052 err = -EINVAL;
3053 if (x->pcpu_num >= num_possible_cpus()) {
3054 NL_SET_ERR_MSG(extack, "pCPU number too big");
3055 goto free_state;
3056 }
3057 }
3058
3059 err = verify_newpolicy_info(&ua->policy, extack);
3060 if (err)
3061 goto free_state;
3062 err = verify_sec_ctx_len(attrs, extack);
3063 if (err)
3064 goto free_state;
3065
3066 /* build an XP */
3067 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err, extack);
3068 if (!xp)
3069 goto free_state;
3070
3071 memcpy(&x->id, &ua->id, sizeof(ua->id));
3072 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
3073 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
3074 xp->mark.m = x->mark.m = mark.m;
3075 xp->mark.v = x->mark.v = mark.v;
3076 ut = nla_data(rt);
3077 /* extract the templates and for each call km_key */
3078 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
3079 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
3080 memcpy(&x->id, &t->id, sizeof(x->id));
3081 x->props.mode = t->mode;
3082 x->props.reqid = t->reqid;
3083 x->props.family = ut->family;
3084 t->aalgos = ua->aalgos;
3085 t->ealgos = ua->ealgos;
3086 t->calgos = ua->calgos;
3087 err = km_query(x, t, xp);
3088
3089 }
3090
3091 xfrm_state_free(x);
3092 xfrm_dev_policy_delete(xp);
3093 xfrm_dev_policy_free(xp);
3094 security_xfrm_policy_free(xp->security);
3095 kfree(xp);
3096
3097 return 0;
3098
3099 free_state:
3100 xfrm_state_free(x);
3101 nomem:
3102 return err;
3103 }
3104
3105 #ifdef CONFIG_XFRM_MIGRATE
copy_from_user_migrate_state(struct xfrm_migrate * ma,const struct xfrm_user_migrate_state * um)3106 static void copy_from_user_migrate_state(struct xfrm_migrate *ma,
3107 const struct xfrm_user_migrate_state *um)
3108 {
3109 memcpy(&ma->old_daddr, &um->id.daddr, sizeof(ma->old_daddr));
3110 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
3111 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
3112
3113 ma->proto = um->id.proto;
3114 ma->new_reqid = um->new_reqid;
3115
3116 ma->old_family = um->id.family;
3117 ma->new_family = um->new_family;
3118
3119 ma->old_mark = um->old_mark;
3120 ma->flags = um->flags;
3121 ma->new_sel = &um->new_sel;
3122 ma->msg_type = XFRM_MSG_MIGRATE_STATE;
3123 }
3124
copy_from_user_migrate(struct xfrm_migrate * ma,struct xfrm_kmaddress * k,struct nlattr ** attrs,int * num,struct netlink_ext_ack * extack)3125 static int copy_from_user_migrate(struct xfrm_migrate *ma,
3126 struct xfrm_kmaddress *k,
3127 struct nlattr **attrs, int *num,
3128 struct netlink_ext_ack *extack)
3129 {
3130 struct nlattr *rt = attrs[XFRMA_MIGRATE];
3131 struct xfrm_user_migrate *um;
3132 int i, num_migrate;
3133
3134 if (k != NULL) {
3135 struct xfrm_user_kmaddress *uk;
3136
3137 uk = nla_data(attrs[XFRMA_KMADDRESS]);
3138 memcpy(&k->local, &uk->local, sizeof(k->local));
3139 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
3140 k->family = uk->family;
3141 k->reserved = uk->reserved;
3142 }
3143
3144 um = nla_data(rt);
3145 num_migrate = nla_len(rt) / sizeof(*um);
3146
3147 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) {
3148 NL_SET_ERR_MSG(extack, "Invalid number of SAs to migrate, must be 0 < num <= XFRM_MAX_DEPTH (6)");
3149 return -EINVAL;
3150 }
3151
3152 for (i = 0; i < num_migrate; i++, um++, ma++) {
3153 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
3154 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
3155 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
3156 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
3157
3158 ma->proto = um->proto;
3159 ma->mode = um->mode;
3160 ma->old_reqid = um->reqid;
3161
3162 ma->old_family = um->old_family;
3163 ma->new_family = um->new_family;
3164 ma->msg_type = XFRM_MSG_MIGRATE;
3165 }
3166
3167 *num = i;
3168 return 0;
3169 }
3170
xfrm_do_migrate(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)3171 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
3172 struct nlattr **attrs, struct netlink_ext_ack *extack)
3173 {
3174 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
3175 struct xfrm_migrate m[XFRM_MAX_DEPTH] = {};
3176 struct xfrm_kmaddress km, *kmp;
3177 u8 type;
3178 int err;
3179 int n = 0;
3180 struct net *net = sock_net(skb->sk);
3181 struct xfrm_encap_tmpl *encap = NULL;
3182 struct xfrm_user_offload *xuo = NULL;
3183 u32 if_id = 0;
3184
3185 if (!attrs[XFRMA_MIGRATE]) {
3186 NL_SET_ERR_MSG(extack, "Missing required MIGRATE attribute");
3187 return -EINVAL;
3188 }
3189
3190 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
3191
3192 err = copy_from_user_policy_type(&type, attrs, extack);
3193 if (err)
3194 return err;
3195
3196 err = copy_from_user_migrate(m, kmp, attrs, &n, extack);
3197 if (err)
3198 return err;
3199
3200 if (!n)
3201 return 0;
3202
3203 if (attrs[XFRMA_ENCAP]) {
3204 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
3205 sizeof(*encap), GFP_KERNEL);
3206 if (!encap)
3207 return -ENOMEM;
3208 }
3209
3210 if (attrs[XFRMA_IF_ID])
3211 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
3212
3213 if (attrs[XFRMA_OFFLOAD_DEV]) {
3214 xuo = kmemdup(nla_data(attrs[XFRMA_OFFLOAD_DEV]),
3215 sizeof(*xuo), GFP_KERNEL);
3216 if (!xuo) {
3217 err = -ENOMEM;
3218 goto error;
3219 }
3220 }
3221 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap,
3222 if_id, extack, xuo);
3223 error:
3224 kfree(encap);
3225 kfree(xuo);
3226 return err;
3227 }
3228
build_migrate_state(struct sk_buff * skb,const struct xfrm_user_migrate_state * um,const struct xfrm_migrate * m,u8 dir,u32 portid,u32 seq)3229 static int build_migrate_state(struct sk_buff *skb,
3230 const struct xfrm_user_migrate_state *um,
3231 const struct xfrm_migrate *m,
3232 u8 dir, u32 portid, u32 seq)
3233 {
3234 int err;
3235 struct nlmsghdr *nlh;
3236 struct xfrm_user_migrate_state *hdr;
3237
3238 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_MIGRATE_STATE,
3239 sizeof(struct xfrm_user_migrate_state), 0);
3240 if (!nlh)
3241 return -EMSGSIZE;
3242
3243 hdr = nlmsg_data(nlh);
3244 *hdr = *um;
3245 hdr->new_sel = *m->new_sel;
3246
3247 if (m->encap) {
3248 err = nla_put(skb, XFRMA_ENCAP, sizeof(*m->encap), m->encap);
3249 if (err)
3250 goto out_cancel;
3251 }
3252
3253 if (m->xuo) {
3254 err = nla_put(skb, XFRMA_OFFLOAD_DEV, sizeof(*m->xuo), m->xuo);
3255 if (err)
3256 goto out_cancel;
3257 }
3258
3259 if (m->new_mark) {
3260 err = nla_put(skb, XFRMA_MARK, sizeof(*m->new_mark),
3261 m->new_mark);
3262 if (err)
3263 goto out_cancel;
3264 }
3265
3266 err = xfrm_smark_put(skb, &m->smark);
3267 if (err)
3268 goto out_cancel;
3269
3270 if (m->mapping_maxage) {
3271 err = nla_put_u32(skb, XFRMA_MTIMER_THRESH, m->mapping_maxage);
3272 if (err)
3273 goto out_cancel;
3274 }
3275
3276 if (m->nat_keepalive_interval) {
3277 err = nla_put_u32(skb, XFRMA_NAT_KEEPALIVE_INTERVAL,
3278 m->nat_keepalive_interval);
3279 if (err)
3280 goto out_cancel;
3281 }
3282
3283 if (dir) {
3284 err = nla_put_u8(skb, XFRMA_SA_DIR, dir);
3285 if (err)
3286 goto out_cancel;
3287 }
3288
3289 nlmsg_end(skb, nlh);
3290 return 0;
3291
3292 out_cancel:
3293 nlmsg_cancel(skb, nlh);
3294 return err;
3295 }
3296
xfrm_migrate_state_msgsize(const struct xfrm_migrate * m,u8 dir)3297 static unsigned int xfrm_migrate_state_msgsize(const struct xfrm_migrate *m,
3298 u8 dir)
3299 {
3300 return NLMSG_ALIGN(sizeof(struct xfrm_user_migrate_state)) +
3301 (m->encap ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0) +
3302 (m->xuo ? nla_total_size(sizeof(struct xfrm_user_offload)) : 0) +
3303 (m->new_mark ? nla_total_size(sizeof(struct xfrm_mark)) : 0) +
3304 ((m->smark.v | m->smark.m) ? nla_total_size(sizeof(u32)) * 2 : 0) +
3305 (m->mapping_maxage ? nla_total_size(sizeof(u32)) : 0) +
3306 (m->nat_keepalive_interval ? nla_total_size(sizeof(u32)) : 0) +
3307 (dir ? nla_total_size(sizeof(u8)) : 0); /* XFRMA_SA_DIR */
3308 }
3309
xfrm_send_migrate_state(struct net * net,const struct xfrm_user_migrate_state * um,const struct xfrm_migrate * m,u8 dir,u32 portid,u32 seq)3310 static int xfrm_send_migrate_state(struct net *net,
3311 const struct xfrm_user_migrate_state *um,
3312 const struct xfrm_migrate *m,
3313 u8 dir, u32 portid, u32 seq)
3314 {
3315 int err;
3316 struct sk_buff *skb;
3317
3318 skb = nlmsg_new(xfrm_migrate_state_msgsize(m, dir), GFP_ATOMIC);
3319 if (!skb)
3320 return -ENOMEM;
3321
3322 err = build_migrate_state(skb, um, m, dir, portid, seq);
3323 if (err < 0) {
3324 kfree_skb(skb);
3325 return err;
3326 }
3327
3328 rcu_read_lock();
3329 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
3330 rcu_read_unlock();
3331
3332 return err;
3333 }
3334
xfrm_do_migrate_state(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)3335 static int xfrm_do_migrate_state(struct sk_buff *skb, struct nlmsghdr *nlh,
3336 struct nlattr **attrs, struct netlink_ext_ack *extack)
3337 {
3338 struct xfrm_user_migrate_state *um = nlmsg_data(nlh);
3339 struct net *net = sock_net(skb->sk);
3340 struct xfrm_user_offload xuo = {};
3341 struct xfrm_migrate m = {};
3342 struct xfrm_state *xc;
3343 struct xfrm_state *x;
3344 int err;
3345
3346 if (!um->id.spi) {
3347 NL_SET_ERR_MSG(extack, "Invalid SPI 0x0");
3348 return -EINVAL;
3349 }
3350
3351 if (um->reserved) {
3352 NL_SET_ERR_MSG(extack, "Reserved field must be zero");
3353 return -EINVAL;
3354 }
3355
3356 if (um->flags & ~XFRM_MIGRATE_STATE_KNOWN_FLAGS) {
3357 NL_SET_ERR_MSG_FMT(extack, "Unknown flags: 0x%x",
3358 um->flags & ~XFRM_MIGRATE_STATE_KNOWN_FLAGS);
3359 return -EINVAL;
3360 }
3361
3362 err = verify_xfrm_family(um->new_family, extack);
3363 if (err)
3364 return err;
3365
3366 if (!(um->flags & XFRM_MIGRATE_STATE_UPDATE_H2H_SEL)) {
3367 err = verify_selector_prefixlen(um->new_sel.family,
3368 &um->new_sel, extack);
3369 if (err)
3370 return err;
3371 }
3372
3373 copy_from_user_migrate_state(&m, um);
3374
3375 x = xfrm_state_lookup(net, m.old_mark.v & m.old_mark.m,
3376 &um->id.daddr, um->id.spi,
3377 um->id.proto, um->id.family);
3378 if (!x) {
3379 NL_SET_ERR_MSG(extack, "Can not find state");
3380 return -ESRCH;
3381 }
3382
3383 if (um->flags & XFRM_MIGRATE_STATE_UPDATE_H2H_SEL) {
3384 u8 prefixlen = (x->props.family == AF_INET6) ? 128 : 32;
3385
3386 if (x->sel.prefixlen_s != x->sel.prefixlen_d ||
3387 x->sel.prefixlen_d != prefixlen ||
3388 !xfrm_addr_equal(&x->sel.daddr, &x->id.daddr, x->props.family) ||
3389 !xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family)) {
3390 NL_SET_ERR_MSG(extack,
3391 "SA selector is not a single-host match for SA addresses");
3392 err = -EINVAL;
3393 goto out;
3394 }
3395 }
3396
3397 if (attrs[XFRMA_ENCAP]) {
3398 m.encap = nla_data(attrs[XFRMA_ENCAP]);
3399 if (m.encap->encap_type == 0) {
3400 m.encap = NULL; /* sentinel: remove encap */
3401 } else if (m.encap->encap_type != UDP_ENCAP_ESPINUDP) {
3402 NL_SET_ERR_MSG(extack, "Unsupported encapsulation type");
3403 err = -EINVAL;
3404 goto out;
3405 }
3406 } else {
3407 m.encap = x->encap; /* omit-to-inherit */
3408 }
3409
3410 if (attrs[XFRMA_MTIMER_THRESH]) {
3411 err = verify_mtimer_thresh(!!m.encap, x->dir, extack);
3412 if (err)
3413 goto out;
3414 }
3415
3416 if (nla_get_u32_default(attrs[XFRMA_NAT_KEEPALIVE_INTERVAL], 0) && !m.encap) {
3417 NL_SET_ERR_MSG(extack,
3418 "NAT_KEEPALIVE_INTERVAL requires encapsulation");
3419 err = -EINVAL;
3420 goto out;
3421 }
3422
3423 if (attrs[XFRMA_OFFLOAD_DEV]) {
3424 m.xuo = nla_data(attrs[XFRMA_OFFLOAD_DEV]);
3425 } else {
3426 bool inherit_offload = !(um->flags & XFRM_MIGRATE_STATE_CLEAR_OFFLOAD);
3427
3428 if (inherit_offload && x->xso.dev) {
3429 xso_to_xuo(&x->xso, &xuo);
3430 m.xuo = &xuo;
3431 }
3432 }
3433
3434 if (attrs[XFRMA_MARK])
3435 m.new_mark = nla_data(attrs[XFRMA_MARK]);
3436
3437 if (attrs[XFRMA_SET_MARK])
3438 xfrm_smark_init(attrs, &m.smark);
3439 else
3440 m.smark = x->props.smark;
3441
3442 m.mapping_maxage = nla_get_u32_default(attrs[XFRMA_MTIMER_THRESH],
3443 x->mapping_maxage);
3444 m.nat_keepalive_interval = nla_get_u32_default(attrs[XFRMA_NAT_KEEPALIVE_INTERVAL],
3445 x->nat_keepalive_interval);
3446
3447 if (m.new_family != um->id.family ||
3448 !xfrm_addr_equal(&m.new_daddr, &um->id.daddr, um->id.family)) {
3449 u32 new_mark_key = m.new_mark ? m.new_mark->v & m.new_mark->m :
3450 m.old_mark.v & m.old_mark.m;
3451 struct xfrm_state *x_new;
3452
3453 x_new = xfrm_state_lookup(net, new_mark_key, &m.new_daddr,
3454 um->id.spi, um->id.proto, m.new_family);
3455 if (x_new) {
3456 xfrm_state_put(x_new);
3457 NL_SET_ERR_MSG(extack, "New SA tuple already occupied");
3458 err = -EEXIST;
3459 goto out;
3460 }
3461 }
3462
3463 xc = xfrm_state_migrate_create(x, &m, net, extack);
3464 if (!xc) {
3465 NL_SET_ERR_MSG_WEAK(extack, "State migration clone failed");
3466 err = -EINVAL;
3467 goto out;
3468 }
3469
3470 spin_lock_bh(&x->lock);
3471 if (x->km.state != XFRM_STATE_VALID) {
3472 spin_unlock_bh(&x->lock);
3473 NL_SET_ERR_MSG(extack, "State already deleted");
3474 err = -ESRCH;
3475 goto out_xc;
3476 }
3477 xfrm_migrate_sync(xc, x); /* to prevent SN/IV reuse */
3478 __xfrm_state_delete(x);
3479 spin_unlock_bh(&x->lock);
3480
3481 err = xfrm_state_migrate_install(x, xc, &m, extack);
3482 if (err < 0) {
3483 /*
3484 * Should not occur: pre-check above ensures the new tuple is
3485 * free under xfrm_cfg_mutex. Both SAs are gone if it does;
3486 * restoring x would risk SN/IV reuse.
3487 */
3488 goto out;
3489 }
3490
3491 /* Restore encap cleared by sentinel (type=0) during migration. */
3492 if (attrs[XFRMA_ENCAP])
3493 m.encap = nla_data(attrs[XFRMA_ENCAP]);
3494
3495 m.new_sel = &xc->sel;
3496 m.mapping_maxage = xc->mapping_maxage;
3497 m.nat_keepalive_interval = xc->nat_keepalive_interval;
3498
3499 err = xfrm_send_migrate_state(net, um, &m, xc->dir,
3500 nlh->nlmsg_pid, nlh->nlmsg_seq);
3501 if (err < 0) {
3502 NL_SET_ERR_MSG(extack, "Failed to send migration notification");
3503 err = 0;
3504 }
3505
3506 out:
3507 xfrm_state_put(x);
3508 return err;
3509 out_xc:
3510 xc->km.state = XFRM_STATE_DEAD;
3511 xfrm_state_put(xc);
3512 xfrm_state_put(x);
3513 return err;
3514 }
3515
3516 #else
xfrm_do_migrate_state(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)3517 static int xfrm_do_migrate_state(struct sk_buff *skb, struct nlmsghdr *nlh,
3518 struct nlattr **attrs, struct netlink_ext_ack *extack)
3519 {
3520 NL_SET_ERR_MSG(extack, "XFRM_MSG_MIGRATE_STATE is not supported");
3521 return -ENOPROTOOPT;
3522 }
3523
xfrm_do_migrate(struct sk_buff * skb,struct nlmsghdr * nlh,struct nlattr ** attrs,struct netlink_ext_ack * extack)3524 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
3525 struct nlattr **attrs, struct netlink_ext_ack *extack)
3526 {
3527 return -ENOPROTOOPT;
3528 }
3529 #endif
3530
3531 #ifdef CONFIG_XFRM_MIGRATE
copy_to_user_migrate(const struct xfrm_migrate * m,struct sk_buff * skb)3532 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
3533 {
3534 struct xfrm_user_migrate um;
3535
3536 memset(&um, 0, sizeof(um));
3537 um.proto = m->proto;
3538 um.mode = m->mode;
3539 um.reqid = m->old_reqid;
3540 um.old_family = m->old_family;
3541 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
3542 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
3543 um.new_family = m->new_family;
3544 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
3545 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
3546
3547 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
3548 }
3549
copy_to_user_kmaddress(const struct xfrm_kmaddress * k,struct sk_buff * skb)3550 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
3551 {
3552 struct xfrm_user_kmaddress uk;
3553
3554 memset(&uk, 0, sizeof(uk));
3555 uk.family = k->family;
3556 uk.reserved = k->reserved;
3557 memcpy(&uk.local, &k->local, sizeof(uk.local));
3558 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
3559
3560 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
3561 }
3562
xfrm_migrate_msgsize(int num_migrate,int with_kma,int with_encp)3563 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
3564 int with_encp)
3565 {
3566 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
3567 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
3568 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
3569 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
3570 + userpolicy_type_attrsize();
3571 }
3572
build_migrate(struct sk_buff * skb,const struct xfrm_migrate * m,int num_migrate,const struct xfrm_kmaddress * k,const struct xfrm_selector * sel,const struct xfrm_encap_tmpl * encap,u8 dir,u8 type)3573 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
3574 int num_migrate, const struct xfrm_kmaddress *k,
3575 const struct xfrm_selector *sel,
3576 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
3577 {
3578 const struct xfrm_migrate *mp;
3579 struct xfrm_userpolicy_id *pol_id;
3580 struct nlmsghdr *nlh;
3581 int i, err;
3582
3583 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
3584 if (nlh == NULL)
3585 return -EMSGSIZE;
3586
3587 pol_id = nlmsg_data(nlh);
3588 /* copy data from selector, dir, and type to the pol_id */
3589 memset(pol_id, 0, sizeof(*pol_id));
3590 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
3591 pol_id->dir = dir;
3592
3593 if (k != NULL) {
3594 err = copy_to_user_kmaddress(k, skb);
3595 if (err)
3596 goto out_cancel;
3597 }
3598 if (encap) {
3599 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
3600 if (err)
3601 goto out_cancel;
3602 }
3603 err = copy_to_user_policy_type(type, skb);
3604 if (err)
3605 goto out_cancel;
3606 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
3607 err = copy_to_user_migrate(mp, skb);
3608 if (err)
3609 goto out_cancel;
3610 }
3611
3612 nlmsg_end(skb, nlh);
3613 return 0;
3614
3615 out_cancel:
3616 nlmsg_cancel(skb, nlh);
3617 return err;
3618 }
3619
xfrm_send_migrate(const struct xfrm_selector * sel,u8 dir,u8 type,const struct xfrm_migrate * m,int num_migrate,const struct xfrm_kmaddress * k,struct net * net,const struct xfrm_encap_tmpl * encap)3620 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3621 const struct xfrm_migrate *m, int num_migrate,
3622 const struct xfrm_kmaddress *k, struct net *net,
3623 const struct xfrm_encap_tmpl *encap)
3624 {
3625 struct sk_buff *skb;
3626 int err;
3627
3628 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
3629 GFP_ATOMIC);
3630 if (skb == NULL)
3631 return -ENOMEM;
3632
3633 /* build migrate */
3634 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
3635 BUG_ON(err < 0);
3636
3637 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
3638 }
3639 #else
xfrm_send_migrate(const struct xfrm_selector * sel,u8 dir,u8 type,const struct xfrm_migrate * m,int num_migrate,const struct xfrm_kmaddress * k,struct net * net,const struct xfrm_encap_tmpl * encap)3640 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3641 const struct xfrm_migrate *m, int num_migrate,
3642 const struct xfrm_kmaddress *k, struct net *net,
3643 const struct xfrm_encap_tmpl *encap)
3644 {
3645 return -ENOPROTOOPT;
3646 }
3647 #endif
3648
3649 #define XMSGSIZE(type) sizeof(struct type)
3650
3651 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
3652 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
3653 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
3654 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
3655 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
3656 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
3657 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
3658 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
3659 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
3660 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
3661 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
3662 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
3663 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
3664 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
3665 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
3666 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
3667 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
3668 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
3669 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
3670 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
3671 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
3672 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
3673 [XFRM_MSG_MAPPING - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_mapping),
3674 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
3675 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
3676 [XFRM_MSG_MIGRATE_STATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_migrate_state),
3677 };
3678 EXPORT_SYMBOL_GPL(xfrm_msg_min);
3679
3680 #undef XMSGSIZE
3681
3682 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
3683 [XFRMA_UNSPEC] = { .strict_start_type = XFRMA_SA_DIR },
3684 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
3685 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
3686 [XFRMA_LASTUSED] = { .type = NLA_U64},
3687 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
3688 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
3689 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
3690 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
3691 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
3692 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
3693 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
3694 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_user_sec_ctx) },
3695 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
3696 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
3697 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
3698 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
3699 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
3700 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
3701 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
3702 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
3703 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
3704 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
3705 [XFRMA_TFCPAD] = { .type = NLA_U32 },
3706 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
3707 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
3708 [XFRMA_PROTO] = { .type = NLA_U8 },
3709 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
3710 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
3711 [XFRMA_SET_MARK] = { .type = NLA_U32 },
3712 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
3713 [XFRMA_IF_ID] = { .type = NLA_U32 },
3714 [XFRMA_MTIMER_THRESH] = { .type = NLA_U32 },
3715 [XFRMA_SA_DIR] = NLA_POLICY_RANGE(NLA_U8, XFRM_SA_DIR_IN, XFRM_SA_DIR_OUT),
3716 [XFRMA_NAT_KEEPALIVE_INTERVAL] = { .type = NLA_U32 },
3717 [XFRMA_SA_PCPU] = { .type = NLA_U32 },
3718 [XFRMA_IPTFS_DROP_TIME] = { .type = NLA_U32 },
3719 [XFRMA_IPTFS_REORDER_WINDOW] = { .type = NLA_U16 },
3720 [XFRMA_IPTFS_DONT_FRAG] = { .type = NLA_FLAG },
3721 [XFRMA_IPTFS_INIT_DELAY] = { .type = NLA_U32 },
3722 [XFRMA_IPTFS_MAX_QSIZE] = { .type = NLA_U32 },
3723 [XFRMA_IPTFS_PKT_SIZE] = { .type = NLA_U32 },
3724 };
3725 EXPORT_SYMBOL_GPL(xfrma_policy);
3726
3727 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
3728 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
3729 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
3730 };
3731
3732 static const struct xfrm_link {
3733 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **,
3734 struct netlink_ext_ack *);
3735 int (*start)(struct netlink_callback *);
3736 int (*dump)(struct sk_buff *, struct netlink_callback *);
3737 int (*done)(struct netlink_callback *);
3738 const struct nla_policy *nla_pol;
3739 int nla_max;
3740 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
3741 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
3742 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
3743 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
3744 .dump = xfrm_dump_sa,
3745 .done = xfrm_dump_sa_done },
3746 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
3747 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
3748 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
3749 .start = xfrm_dump_policy_start,
3750 .dump = xfrm_dump_policy,
3751 .done = xfrm_dump_policy_done },
3752 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
3753 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
3754 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
3755 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
3756 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
3757 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
3758 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
3759 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
3760 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
3761 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
3762 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
3763 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
3764 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
3765 .nla_pol = xfrma_spd_policy,
3766 .nla_max = XFRMA_SPD_MAX },
3767 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
3768 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
3769 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
3770 [XFRM_MSG_MIGRATE_STATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate_state },
3771 };
3772
xfrm_reject_unused_attr(int type,struct nlattr ** attrs,struct netlink_ext_ack * extack)3773 static int xfrm_reject_unused_attr(int type, struct nlattr **attrs,
3774 struct netlink_ext_ack *extack)
3775 {
3776 if (attrs[XFRMA_SA_DIR]) {
3777 switch (type) {
3778 case XFRM_MSG_NEWSA:
3779 case XFRM_MSG_UPDSA:
3780 case XFRM_MSG_ALLOCSPI:
3781 break;
3782 default:
3783 NL_SET_ERR_MSG(extack, "Invalid attribute SA_DIR");
3784 return -EINVAL;
3785 }
3786 }
3787
3788 if (attrs[XFRMA_SA_PCPU]) {
3789 switch (type) {
3790 case XFRM_MSG_NEWSA:
3791 case XFRM_MSG_UPDSA:
3792 case XFRM_MSG_ALLOCSPI:
3793 case XFRM_MSG_ACQUIRE:
3794
3795 break;
3796 default:
3797 NL_SET_ERR_MSG(extack, "Invalid attribute SA_PCPU");
3798 return -EINVAL;
3799 }
3800 }
3801
3802 if (type == XFRM_MSG_MIGRATE_STATE) {
3803 int i;
3804
3805 for (i = 0; i <= XFRMA_MAX; i++) {
3806 if (!attrs[i])
3807 continue;
3808
3809 switch (i) {
3810 case XFRMA_MARK:
3811 case XFRMA_ENCAP:
3812 case XFRMA_OFFLOAD_DEV:
3813 case XFRMA_SET_MARK:
3814 case XFRMA_SET_MARK_MASK:
3815 case XFRMA_MTIMER_THRESH:
3816 case XFRMA_NAT_KEEPALIVE_INTERVAL:
3817 break;
3818 default:
3819 NL_SET_ERR_MSG_ATTR(extack, attrs[i],
3820 "Unsupported attribute in XFRM_MSG_MIGRATE_STATE");
3821 return -EINVAL;
3822 }
3823 }
3824 }
3825
3826 return 0;
3827 }
3828
xfrm_user_rcv_msg(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3829 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
3830 struct netlink_ext_ack *extack)
3831 {
3832 struct net *net = sock_net(skb->sk);
3833 struct nlattr *attrs[XFRMA_MAX+1];
3834 const struct xfrm_link *link;
3835 struct nlmsghdr *nlh64 = NULL;
3836 int type, err;
3837
3838 type = nlh->nlmsg_type;
3839 if (type > XFRM_MSG_MAX)
3840 return -EINVAL;
3841
3842 type -= XFRM_MSG_BASE;
3843 link = &xfrm_dispatch[type];
3844
3845 /* All operations require privileges, even GET */
3846 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
3847 return -EPERM;
3848
3849 if (IS_ENABLED(CONFIG_COMPAT_FOR_U64_ALIGNMENT) && in_compat_syscall()) {
3850 struct xfrm_translator *xtr = xfrm_get_translator();
3851
3852 if (!xtr)
3853 return -EOPNOTSUPP;
3854
3855 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
3856 link->nla_pol, extack);
3857 xfrm_put_translator(xtr);
3858 if (IS_ERR(nlh64))
3859 return PTR_ERR(nlh64);
3860 if (nlh64)
3861 nlh = nlh64;
3862 }
3863
3864 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
3865 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
3866 (nlh->nlmsg_flags & NLM_F_DUMP)) {
3867 struct netlink_dump_control c = {
3868 .start = link->start,
3869 .dump = link->dump,
3870 .done = link->done,
3871 };
3872
3873 if (link->dump == NULL) {
3874 err = -EINVAL;
3875 goto err;
3876 }
3877
3878 err = netlink_dump_start(xfrm_net_nlsk(net, skb), skb, nlh, &c);
3879 goto err;
3880 }
3881
3882 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
3883 link->nla_max ? : XFRMA_MAX,
3884 link->nla_pol ? : xfrma_policy, extack);
3885 if (err < 0)
3886 goto err;
3887
3888 if (!link->nla_pol || link->nla_pol == xfrma_policy) {
3889 err = xfrm_reject_unused_attr((type + XFRM_MSG_BASE), attrs, extack);
3890 if (err < 0)
3891 goto err;
3892 }
3893
3894 if (link->doit == NULL) {
3895 err = -EINVAL;
3896 goto err;
3897 }
3898
3899 err = link->doit(skb, nlh, attrs, extack);
3900
3901 /* We need to free skb allocated in xfrm_alloc_compat() before
3902 * returning from this function, because consume_skb() won't take
3903 * care of frag_list since netlink destructor sets
3904 * sbk->head to NULL. (see netlink_skb_destructor())
3905 */
3906 if (skb_has_frag_list(skb)) {
3907 kfree_skb(skb_shinfo(skb)->frag_list);
3908 skb_shinfo(skb)->frag_list = NULL;
3909 }
3910
3911 err:
3912 kvfree(nlh64);
3913 return err;
3914 }
3915
xfrm_netlink_rcv(struct sk_buff * skb)3916 static void xfrm_netlink_rcv(struct sk_buff *skb)
3917 {
3918 struct net *net = sock_net(skb->sk);
3919
3920 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
3921 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
3922 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
3923 }
3924
xfrm_expire_msgsize(void)3925 static inline unsigned int xfrm_expire_msgsize(void)
3926 {
3927 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) +
3928 nla_total_size(sizeof(struct xfrm_mark)) +
3929 nla_total_size(sizeof_field(struct xfrm_state, dir)) +
3930 nla_total_size(4); /* XFRMA_SA_PCPU */
3931 }
3932
build_expire(struct sk_buff * skb,struct xfrm_state * x,const struct km_event * c)3933 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
3934 {
3935 struct xfrm_user_expire *ue;
3936 struct nlmsghdr *nlh;
3937 int err;
3938
3939 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
3940 if (nlh == NULL)
3941 return -EMSGSIZE;
3942
3943 ue = nlmsg_data(nlh);
3944 copy_to_user_state(x, &ue->state);
3945 ue->hard = (c->data.hard != 0) ? 1 : 0;
3946 /* clear the padding bytes */
3947 memset_after(ue, 0, hard);
3948
3949 err = xfrm_mark_put(skb, &x->mark);
3950 if (err)
3951 return err;
3952
3953 err = xfrm_if_id_put(skb, x->if_id);
3954 if (err)
3955 return err;
3956 if (x->pcpu_num != UINT_MAX) {
3957 err = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
3958 if (err)
3959 return err;
3960 }
3961
3962 if (x->dir) {
3963 err = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
3964 if (err)
3965 return err;
3966 }
3967
3968 nlmsg_end(skb, nlh);
3969 return 0;
3970 }
3971
xfrm_exp_state_notify(struct xfrm_state * x,const struct km_event * c)3972 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
3973 {
3974 struct net *net = xs_net(x);
3975 struct sk_buff *skb;
3976
3977 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
3978 if (skb == NULL)
3979 return -ENOMEM;
3980
3981 if (build_expire(skb, x, c) < 0) {
3982 kfree_skb(skb);
3983 return -EMSGSIZE;
3984 }
3985
3986 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3987 }
3988
xfrm_aevent_state_notify(struct xfrm_state * x,const struct km_event * c)3989 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
3990 {
3991 struct net *net = xs_net(x);
3992 struct sk_buff *skb;
3993 int err;
3994
3995 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
3996 if (skb == NULL)
3997 return -ENOMEM;
3998
3999 err = build_aevent(skb, x, c);
4000 BUG_ON(err < 0);
4001
4002 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
4003 }
4004
xfrm_notify_sa_flush(const struct km_event * c)4005 static int xfrm_notify_sa_flush(const struct km_event *c)
4006 {
4007 struct net *net = c->net;
4008 struct xfrm_usersa_flush *p;
4009 struct nlmsghdr *nlh;
4010 struct sk_buff *skb;
4011 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
4012
4013 skb = nlmsg_new(len, GFP_ATOMIC);
4014 if (skb == NULL)
4015 return -ENOMEM;
4016
4017 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
4018 if (nlh == NULL) {
4019 kfree_skb(skb);
4020 return -EMSGSIZE;
4021 }
4022
4023 p = nlmsg_data(nlh);
4024 p->proto = c->data.proto;
4025
4026 nlmsg_end(skb, nlh);
4027
4028 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
4029 }
4030
xfrm_sa_len(struct xfrm_state * x)4031 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
4032 {
4033 unsigned int l = 0;
4034 if (x->aead)
4035 l += nla_total_size(aead_len(x->aead));
4036 if (x->aalg) {
4037 l += nla_total_size(sizeof(struct xfrm_algo) +
4038 (x->aalg->alg_key_len + 7) / 8);
4039 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
4040 }
4041 if (x->ealg)
4042 l += nla_total_size(xfrm_alg_len(x->ealg));
4043 if (x->calg)
4044 l += nla_total_size(sizeof(*x->calg));
4045 if (x->encap)
4046 l += nla_total_size(sizeof(*x->encap));
4047 if (x->tfcpad)
4048 l += nla_total_size(sizeof(x->tfcpad));
4049 if (x->replay_esn)
4050 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
4051 else
4052 l += nla_total_size(sizeof(struct xfrm_replay_state));
4053 if (x->security)
4054 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
4055 x->security->ctx_len);
4056 if (x->coaddr)
4057 l += nla_total_size(sizeof(*x->coaddr));
4058 if (x->props.extra_flags)
4059 l += nla_total_size(sizeof(x->props.extra_flags));
4060 if (READ_ONCE(x->xso.dev))
4061 l += nla_total_size(sizeof(struct xfrm_user_offload));
4062 if (x->props.smark.v | x->props.smark.m) {
4063 l += nla_total_size(sizeof(x->props.smark.v));
4064 l += nla_total_size(sizeof(x->props.smark.m));
4065 }
4066 if (x->if_id)
4067 l += nla_total_size(sizeof(x->if_id));
4068 if (x->pcpu_num != UINT_MAX)
4069 l += nla_total_size(sizeof(x->pcpu_num));
4070
4071 /* Must count x->lastused as it may become non-zero behind our back. */
4072 l += nla_total_size_64bit(sizeof(u64));
4073
4074 if (x->mapping_maxage)
4075 l += nla_total_size(sizeof(x->mapping_maxage));
4076
4077 if (x->dir)
4078 l += nla_total_size(sizeof(x->dir));
4079
4080 if (x->nat_keepalive_interval)
4081 l += nla_total_size(sizeof(x->nat_keepalive_interval));
4082
4083 if (x->mode_cbs && x->mode_cbs->sa_len)
4084 l += x->mode_cbs->sa_len(x);
4085
4086 return l;
4087 }
4088
xfrm_notify_sa(struct xfrm_state * x,const struct km_event * c)4089 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
4090 {
4091 struct net *net = xs_net(x);
4092 struct xfrm_usersa_info *p;
4093 struct xfrm_usersa_id *id;
4094 struct nlmsghdr *nlh;
4095 struct sk_buff *skb;
4096 unsigned int len = xfrm_sa_len(x);
4097 unsigned int headlen;
4098 int err;
4099
4100 headlen = sizeof(*p);
4101 if (c->event == XFRM_MSG_DELSA) {
4102 len += nla_total_size(headlen);
4103 headlen = sizeof(*id);
4104 len += nla_total_size(sizeof(struct xfrm_mark));
4105 }
4106 len += NLMSG_ALIGN(headlen);
4107
4108 skb = nlmsg_new(len, GFP_ATOMIC);
4109 if (skb == NULL)
4110 return -ENOMEM;
4111
4112 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
4113 err = -EMSGSIZE;
4114 if (nlh == NULL)
4115 goto out_free_skb;
4116
4117 p = nlmsg_data(nlh);
4118 if (c->event == XFRM_MSG_DELSA) {
4119 struct nlattr *attr;
4120
4121 id = nlmsg_data(nlh);
4122 memset(id, 0, sizeof(*id));
4123 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
4124 id->spi = x->id.spi;
4125 id->family = x->props.family;
4126 id->proto = x->id.proto;
4127
4128 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
4129 err = -EMSGSIZE;
4130 if (attr == NULL)
4131 goto out_free_skb;
4132
4133 p = nla_data(attr);
4134 }
4135 err = copy_to_user_state_extra(x, p, skb);
4136 if (err)
4137 goto out_free_skb;
4138
4139 nlmsg_end(skb, nlh);
4140
4141 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
4142
4143 out_free_skb:
4144 kfree_skb(skb);
4145 return err;
4146 }
4147
xfrm_send_state_notify(struct xfrm_state * x,const struct km_event * c)4148 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
4149 {
4150
4151 switch (c->event) {
4152 case XFRM_MSG_EXPIRE:
4153 return xfrm_exp_state_notify(x, c);
4154 case XFRM_MSG_NEWAE:
4155 return xfrm_aevent_state_notify(x, c);
4156 case XFRM_MSG_DELSA:
4157 case XFRM_MSG_UPDSA:
4158 case XFRM_MSG_NEWSA:
4159 return xfrm_notify_sa(x, c);
4160 case XFRM_MSG_FLUSHSA:
4161 return xfrm_notify_sa_flush(c);
4162 default:
4163 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
4164 c->event);
4165 break;
4166 }
4167
4168 return 0;
4169
4170 }
4171
xfrm_acquire_msgsize(struct xfrm_state * x,struct xfrm_policy * xp)4172 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
4173 struct xfrm_policy *xp)
4174 {
4175 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
4176 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
4177 + nla_total_size(sizeof(struct xfrm_mark))
4178 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
4179 + nla_total_size(4) /* XFRMA_SA_PCPU */
4180 + userpolicy_type_attrsize();
4181 }
4182
build_acquire(struct sk_buff * skb,struct xfrm_state * x,struct xfrm_tmpl * xt,struct xfrm_policy * xp)4183 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
4184 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
4185 {
4186 __u32 seq = xfrm_get_acqseq();
4187 struct xfrm_user_acquire *ua;
4188 struct nlmsghdr *nlh;
4189 int err;
4190
4191 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
4192 if (nlh == NULL)
4193 return -EMSGSIZE;
4194
4195 ua = nlmsg_data(nlh);
4196 memcpy(&ua->id, &x->id, sizeof(ua->id));
4197 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
4198 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
4199 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
4200 ua->aalgos = xt->aalgos;
4201 ua->ealgos = xt->ealgos;
4202 ua->calgos = xt->calgos;
4203 ua->seq = x->km.seq = seq;
4204
4205 err = copy_to_user_tmpl(xp, skb);
4206 if (!err)
4207 err = copy_to_user_state_sec_ctx(x, skb);
4208 if (!err)
4209 err = copy_to_user_policy_type(xp->type, skb);
4210 if (!err)
4211 err = xfrm_mark_put(skb, &xp->mark);
4212 if (!err)
4213 err = xfrm_if_id_put(skb, xp->if_id);
4214 if (!err && xp->xdo.dev)
4215 err = copy_user_offload(&xp->xdo, skb);
4216 if (!err && x->pcpu_num != UINT_MAX)
4217 err = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
4218 if (err) {
4219 nlmsg_cancel(skb, nlh);
4220 return err;
4221 }
4222
4223 nlmsg_end(skb, nlh);
4224 return 0;
4225 }
4226
xfrm_send_acquire(struct xfrm_state * x,struct xfrm_tmpl * xt,struct xfrm_policy * xp)4227 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
4228 struct xfrm_policy *xp)
4229 {
4230 struct net *net = xs_net(x);
4231 struct sk_buff *skb;
4232 int err;
4233
4234 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
4235 if (skb == NULL)
4236 return -ENOMEM;
4237
4238 err = build_acquire(skb, x, xt, xp);
4239 BUG_ON(err < 0);
4240
4241 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
4242 }
4243
4244 /* User gives us xfrm_user_policy_info followed by an array of 0
4245 * or more templates.
4246 */
xfrm_compile_policy(struct sock * sk,int opt,u8 * data,int len,int * dir)4247 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
4248 u8 *data, int len, int *dir)
4249 {
4250 struct net *net = sock_net(sk);
4251 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
4252 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
4253 struct xfrm_policy *xp;
4254 int nr;
4255
4256 switch (sk->sk_family) {
4257 case AF_INET:
4258 if (opt != IP_XFRM_POLICY) {
4259 *dir = -EOPNOTSUPP;
4260 return NULL;
4261 }
4262 break;
4263 #if IS_ENABLED(CONFIG_IPV6)
4264 case AF_INET6:
4265 if (opt != IPV6_XFRM_POLICY) {
4266 *dir = -EOPNOTSUPP;
4267 return NULL;
4268 }
4269 break;
4270 #endif
4271 default:
4272 *dir = -EINVAL;
4273 return NULL;
4274 }
4275
4276 *dir = -EINVAL;
4277
4278 if (len < sizeof(*p) ||
4279 verify_newpolicy_info(p, NULL))
4280 return NULL;
4281
4282 nr = ((len - sizeof(*p)) / sizeof(*ut));
4283 if (validate_tmpl(nr, ut, p->sel.family, p->dir, NULL))
4284 return NULL;
4285
4286 if (p->dir > XFRM_POLICY_OUT)
4287 return NULL;
4288
4289 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
4290 if (xp == NULL) {
4291 *dir = -ENOBUFS;
4292 return NULL;
4293 }
4294
4295 copy_from_user_policy(xp, p);
4296 xp->type = XFRM_POLICY_TYPE_MAIN;
4297 copy_templates(xp, ut, nr);
4298
4299 *dir = p->dir;
4300
4301 return xp;
4302 }
4303
xfrm_polexpire_msgsize(struct xfrm_policy * xp)4304 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
4305 {
4306 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
4307 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
4308 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
4309 + nla_total_size(sizeof(struct xfrm_mark))
4310 + userpolicy_type_attrsize();
4311 }
4312
build_polexpire(struct sk_buff * skb,struct xfrm_policy * xp,int dir,const struct km_event * c)4313 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
4314 int dir, const struct km_event *c)
4315 {
4316 struct xfrm_user_polexpire *upe;
4317 int hard = c->data.hard;
4318 struct nlmsghdr *nlh;
4319 int err;
4320
4321 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
4322 if (nlh == NULL)
4323 return -EMSGSIZE;
4324
4325 upe = nlmsg_data(nlh);
4326 copy_to_user_policy(xp, &upe->pol, dir);
4327 err = copy_to_user_tmpl(xp, skb);
4328 if (!err)
4329 err = copy_to_user_sec_ctx(xp, skb);
4330 if (!err)
4331 err = copy_to_user_policy_type(xp->type, skb);
4332 if (!err)
4333 err = xfrm_mark_put(skb, &xp->mark);
4334 if (!err)
4335 err = xfrm_if_id_put(skb, xp->if_id);
4336 if (!err && xp->xdo.dev)
4337 err = copy_user_offload(&xp->xdo, skb);
4338 if (err) {
4339 nlmsg_cancel(skb, nlh);
4340 return err;
4341 }
4342 upe->hard = !!hard;
4343 /* clear the padding bytes */
4344 memset_after(upe, 0, hard);
4345
4346 nlmsg_end(skb, nlh);
4347 return 0;
4348 }
4349
xfrm_exp_policy_notify(struct xfrm_policy * xp,int dir,const struct km_event * c)4350 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
4351 {
4352 struct net *net = xp_net(xp);
4353 struct sk_buff *skb;
4354 int err;
4355
4356 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
4357 if (skb == NULL)
4358 return -ENOMEM;
4359
4360 err = build_polexpire(skb, xp, dir, c);
4361 BUG_ON(err < 0);
4362
4363 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
4364 }
4365
xfrm_notify_policy(struct xfrm_policy * xp,int dir,const struct km_event * c)4366 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
4367 {
4368 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
4369 struct net *net = xp_net(xp);
4370 struct xfrm_userpolicy_info *p;
4371 struct xfrm_userpolicy_id *id;
4372 struct nlmsghdr *nlh;
4373 struct sk_buff *skb;
4374 unsigned int headlen;
4375 int err;
4376
4377 headlen = sizeof(*p);
4378 if (c->event == XFRM_MSG_DELPOLICY) {
4379 len += nla_total_size(headlen);
4380 headlen = sizeof(*id);
4381 }
4382 len += userpolicy_type_attrsize();
4383 len += nla_total_size(sizeof(struct xfrm_mark));
4384 len += NLMSG_ALIGN(headlen);
4385
4386 skb = nlmsg_new(len, GFP_ATOMIC);
4387 if (skb == NULL)
4388 return -ENOMEM;
4389
4390 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
4391 err = -EMSGSIZE;
4392 if (nlh == NULL)
4393 goto out_free_skb;
4394
4395 p = nlmsg_data(nlh);
4396 if (c->event == XFRM_MSG_DELPOLICY) {
4397 struct nlattr *attr;
4398
4399 id = nlmsg_data(nlh);
4400 memset(id, 0, sizeof(*id));
4401 id->dir = dir;
4402 if (c->data.byid)
4403 id->index = xp->index;
4404 else
4405 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
4406
4407 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
4408 err = -EMSGSIZE;
4409 if (attr == NULL)
4410 goto out_free_skb;
4411
4412 p = nla_data(attr);
4413 }
4414
4415 copy_to_user_policy(xp, p, dir);
4416 err = copy_to_user_tmpl(xp, skb);
4417 if (!err)
4418 err = copy_to_user_policy_type(xp->type, skb);
4419 if (!err)
4420 err = xfrm_mark_put(skb, &xp->mark);
4421 if (!err)
4422 err = xfrm_if_id_put(skb, xp->if_id);
4423 if (!err && xp->xdo.dev)
4424 err = copy_user_offload(&xp->xdo, skb);
4425 if (err)
4426 goto out_free_skb;
4427
4428 nlmsg_end(skb, nlh);
4429
4430 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
4431
4432 out_free_skb:
4433 kfree_skb(skb);
4434 return err;
4435 }
4436
xfrm_notify_policy_flush(const struct km_event * c)4437 static int xfrm_notify_policy_flush(const struct km_event *c)
4438 {
4439 struct net *net = c->net;
4440 struct nlmsghdr *nlh;
4441 struct sk_buff *skb;
4442 int err;
4443
4444 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
4445 if (skb == NULL)
4446 return -ENOMEM;
4447
4448 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
4449 err = -EMSGSIZE;
4450 if (nlh == NULL)
4451 goto out_free_skb;
4452 err = copy_to_user_policy_type(c->data.type, skb);
4453 if (err)
4454 goto out_free_skb;
4455
4456 nlmsg_end(skb, nlh);
4457
4458 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
4459
4460 out_free_skb:
4461 kfree_skb(skb);
4462 return err;
4463 }
4464
xfrm_send_policy_notify(struct xfrm_policy * xp,int dir,const struct km_event * c)4465 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
4466 {
4467
4468 switch (c->event) {
4469 case XFRM_MSG_NEWPOLICY:
4470 case XFRM_MSG_UPDPOLICY:
4471 case XFRM_MSG_DELPOLICY:
4472 return xfrm_notify_policy(xp, dir, c);
4473 case XFRM_MSG_FLUSHPOLICY:
4474 return xfrm_notify_policy_flush(c);
4475 case XFRM_MSG_POLEXPIRE:
4476 return xfrm_exp_policy_notify(xp, dir, c);
4477 default:
4478 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
4479 c->event);
4480 }
4481
4482 return 0;
4483
4484 }
4485
xfrm_report_msgsize(void)4486 static inline unsigned int xfrm_report_msgsize(void)
4487 {
4488 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
4489 }
4490
build_report(struct sk_buff * skb,u8 proto,struct xfrm_selector * sel,xfrm_address_t * addr)4491 static int build_report(struct sk_buff *skb, u8 proto,
4492 struct xfrm_selector *sel, xfrm_address_t *addr)
4493 {
4494 struct xfrm_user_report *ur;
4495 struct nlmsghdr *nlh;
4496
4497 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
4498 if (nlh == NULL)
4499 return -EMSGSIZE;
4500
4501 ur = nlmsg_data(nlh);
4502 memset(ur, 0, sizeof(*ur));
4503 ur->proto = proto;
4504 memcpy(&ur->sel, sel, sizeof(ur->sel));
4505
4506 if (addr) {
4507 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
4508 if (err) {
4509 nlmsg_cancel(skb, nlh);
4510 return err;
4511 }
4512 }
4513 nlmsg_end(skb, nlh);
4514 return 0;
4515 }
4516
xfrm_send_report(struct net * net,u8 proto,struct xfrm_selector * sel,xfrm_address_t * addr)4517 static int xfrm_send_report(struct net *net, u8 proto,
4518 struct xfrm_selector *sel, xfrm_address_t *addr)
4519 {
4520 struct sk_buff *skb;
4521 int err;
4522
4523 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
4524 if (skb == NULL)
4525 return -ENOMEM;
4526
4527 err = build_report(skb, proto, sel, addr);
4528 BUG_ON(err < 0);
4529
4530 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
4531 }
4532
xfrm_mapping_msgsize(void)4533 static inline unsigned int xfrm_mapping_msgsize(void)
4534 {
4535 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
4536 }
4537
build_mapping(struct sk_buff * skb,struct xfrm_state * x,xfrm_address_t * new_saddr,__be16 new_sport)4538 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
4539 xfrm_address_t *new_saddr, __be16 new_sport)
4540 {
4541 struct xfrm_user_mapping *um;
4542 struct nlmsghdr *nlh;
4543
4544 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
4545 if (nlh == NULL)
4546 return -EMSGSIZE;
4547
4548 um = nlmsg_data(nlh);
4549
4550 memset(&um->id, 0, sizeof(um->id));
4551 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
4552 um->id.spi = x->id.spi;
4553 um->id.family = x->props.family;
4554 um->id.proto = x->id.proto;
4555 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
4556 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
4557 um->new_sport = new_sport;
4558 um->old_sport = x->encap->encap_sport;
4559 um->reqid = x->props.reqid;
4560
4561 nlmsg_end(skb, nlh);
4562 return 0;
4563 }
4564
xfrm_send_mapping(struct xfrm_state * x,xfrm_address_t * ipaddr,__be16 sport)4565 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
4566 __be16 sport)
4567 {
4568 struct net *net = xs_net(x);
4569 struct sk_buff *skb;
4570 int err;
4571
4572 if (x->id.proto != IPPROTO_ESP)
4573 return -EINVAL;
4574
4575 if (!x->encap)
4576 return -EINVAL;
4577
4578 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
4579 if (skb == NULL)
4580 return -ENOMEM;
4581
4582 err = build_mapping(skb, x, ipaddr, sport);
4583 BUG_ON(err < 0);
4584
4585 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
4586 }
4587
xfrm_is_alive(const struct km_event * c)4588 static bool xfrm_is_alive(const struct km_event *c)
4589 {
4590 return (bool)xfrm_acquire_is_on(c->net);
4591 }
4592
4593 static struct xfrm_mgr netlink_mgr = {
4594 .notify = xfrm_send_state_notify,
4595 .acquire = xfrm_send_acquire,
4596 .compile_policy = xfrm_compile_policy,
4597 .notify_policy = xfrm_send_policy_notify,
4598 .report = xfrm_send_report,
4599 .migrate = xfrm_send_migrate,
4600 .new_mapping = xfrm_send_mapping,
4601 .is_alive = xfrm_is_alive,
4602 };
4603
xfrm_user_net_init(struct net * net)4604 static int __net_init xfrm_user_net_init(struct net *net)
4605 {
4606 struct sock *nlsk;
4607 struct netlink_kernel_cfg cfg = {
4608 .groups = XFRMNLGRP_MAX,
4609 .input = xfrm_netlink_rcv,
4610 };
4611
4612 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
4613 if (nlsk == NULL)
4614 return -ENOMEM;
4615 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
4616 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
4617 return 0;
4618 }
4619
xfrm_user_net_pre_exit(struct net * net)4620 static void __net_exit xfrm_user_net_pre_exit(struct net *net)
4621 {
4622 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
4623 }
4624
xfrm_user_net_exit(struct list_head * net_exit_list)4625 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
4626 {
4627 struct net *net;
4628
4629 list_for_each_entry(net, net_exit_list, exit_list)
4630 netlink_kernel_release(net->xfrm.nlsk_stash);
4631 }
4632
4633 static struct pernet_operations xfrm_user_net_ops = {
4634 .init = xfrm_user_net_init,
4635 .pre_exit = xfrm_user_net_pre_exit,
4636 .exit_batch = xfrm_user_net_exit,
4637 };
4638
xfrm_user_init(void)4639 static int __init xfrm_user_init(void)
4640 {
4641 int rv;
4642
4643 printk(KERN_INFO "Initializing XFRM netlink socket\n");
4644
4645 rv = register_pernet_subsys(&xfrm_user_net_ops);
4646 if (rv < 0)
4647 return rv;
4648 xfrm_register_km(&netlink_mgr);
4649 return 0;
4650 }
4651
xfrm_user_exit(void)4652 static void __exit xfrm_user_exit(void)
4653 {
4654 xfrm_unregister_km(&netlink_mgr);
4655 unregister_pernet_subsys(&xfrm_user_net_ops);
4656 }
4657
4658 module_init(xfrm_user_init);
4659 module_exit(xfrm_user_exit);
4660 MODULE_DESCRIPTION("XFRM User interface");
4661 MODULE_LICENSE("GPL");
4662 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
4663