]> Pileus Git - ~andy/linux/blob - net/xfrm/xfrm_user.c
16c84608e81efc881adec9effdacc32f389a11ed
[~andy/linux] / net / xfrm / xfrm_user.c
1 /* xfrm_user.c: User interface to configure xfrm engine.
2  *
3  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4  *
5  * Changes:
6  *      Mitsuru KANDA @USAGI
7  *      Kazunori MIYAZAWA @USAGI
8  *      Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9  *              IPv6 support
10  *
11  */
12
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/socket.h>
19 #include <linux/string.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
22 #include <linux/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
26 #include <net/sock.h>
27 #include <net/xfrm.h>
28 #include <net/netlink.h>
29 #include <net/ah.h>
30 #include <asm/uaccess.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <linux/in6.h>
33 #endif
34
35 static inline int aead_len(struct xfrm_algo_aead *alg)
36 {
37         return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
38 }
39
40 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
41 {
42         struct nlattr *rt = attrs[type];
43         struct xfrm_algo *algp;
44
45         if (!rt)
46                 return 0;
47
48         algp = nla_data(rt);
49         if (nla_len(rt) < xfrm_alg_len(algp))
50                 return -EINVAL;
51
52         switch (type) {
53         case XFRMA_ALG_AUTH:
54         case XFRMA_ALG_CRYPT:
55         case XFRMA_ALG_COMP:
56                 break;
57
58         default:
59                 return -EINVAL;
60         }
61
62         algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
63         return 0;
64 }
65
66 static int verify_auth_trunc(struct nlattr **attrs)
67 {
68         struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
69         struct xfrm_algo_auth *algp;
70
71         if (!rt)
72                 return 0;
73
74         algp = nla_data(rt);
75         if (nla_len(rt) < xfrm_alg_auth_len(algp))
76                 return -EINVAL;
77
78         algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
79         return 0;
80 }
81
82 static int verify_aead(struct nlattr **attrs)
83 {
84         struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
85         struct xfrm_algo_aead *algp;
86
87         if (!rt)
88                 return 0;
89
90         algp = nla_data(rt);
91         if (nla_len(rt) < aead_len(algp))
92                 return -EINVAL;
93
94         algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
95         return 0;
96 }
97
98 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
99                            xfrm_address_t **addrp)
100 {
101         struct nlattr *rt = attrs[type];
102
103         if (rt && addrp)
104                 *addrp = nla_data(rt);
105 }
106
107 static inline int verify_sec_ctx_len(struct nlattr **attrs)
108 {
109         struct nlattr *rt = attrs[XFRMA_SEC_CTX];
110         struct xfrm_user_sec_ctx *uctx;
111
112         if (!rt)
113                 return 0;
114
115         uctx = nla_data(rt);
116         if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
117                 return -EINVAL;
118
119         return 0;
120 }
121
122 static inline int verify_replay(struct xfrm_usersa_info *p,
123                                 struct nlattr **attrs)
124 {
125         struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
126         struct xfrm_replay_state_esn *rs;
127
128         if (p->flags & XFRM_STATE_ESN) {
129                 if (!rt)
130                         return -EINVAL;
131
132                 rs = nla_data(rt);
133
134                 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
135                         return -EINVAL;
136
137                 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
138                     nla_len(rt) != sizeof(*rs))
139                         return -EINVAL;
140         }
141
142         if (!rt)
143                 return 0;
144
145         if (p->id.proto != IPPROTO_ESP)
146                 return -EINVAL;
147
148         if (p->replay_window != 0)
149                 return -EINVAL;
150
151         return 0;
152 }
153
154 static int verify_newsa_info(struct xfrm_usersa_info *p,
155                              struct nlattr **attrs)
156 {
157         int err;
158
159         err = -EINVAL;
160         switch (p->family) {
161         case AF_INET:
162                 break;
163
164         case AF_INET6:
165 #if IS_ENABLED(CONFIG_IPV6)
166                 break;
167 #else
168                 err = -EAFNOSUPPORT;
169                 goto out;
170 #endif
171
172         default:
173                 goto out;
174         }
175
176         err = -EINVAL;
177         switch (p->id.proto) {
178         case IPPROTO_AH:
179                 if ((!attrs[XFRMA_ALG_AUTH]     &&
180                      !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
181                     attrs[XFRMA_ALG_AEAD]       ||
182                     attrs[XFRMA_ALG_CRYPT]      ||
183                     attrs[XFRMA_ALG_COMP]       ||
184                     attrs[XFRMA_TFCPAD])
185                         goto out;
186                 break;
187
188         case IPPROTO_ESP:
189                 if (attrs[XFRMA_ALG_COMP])
190                         goto out;
191                 if (!attrs[XFRMA_ALG_AUTH] &&
192                     !attrs[XFRMA_ALG_AUTH_TRUNC] &&
193                     !attrs[XFRMA_ALG_CRYPT] &&
194                     !attrs[XFRMA_ALG_AEAD])
195                         goto out;
196                 if ((attrs[XFRMA_ALG_AUTH] ||
197                      attrs[XFRMA_ALG_AUTH_TRUNC] ||
198                      attrs[XFRMA_ALG_CRYPT]) &&
199                     attrs[XFRMA_ALG_AEAD])
200                         goto out;
201                 if (attrs[XFRMA_TFCPAD] &&
202                     p->mode != XFRM_MODE_TUNNEL)
203                         goto out;
204                 break;
205
206         case IPPROTO_COMP:
207                 if (!attrs[XFRMA_ALG_COMP]      ||
208                     attrs[XFRMA_ALG_AEAD]       ||
209                     attrs[XFRMA_ALG_AUTH]       ||
210                     attrs[XFRMA_ALG_AUTH_TRUNC] ||
211                     attrs[XFRMA_ALG_CRYPT]      ||
212                     attrs[XFRMA_TFCPAD])
213                         goto out;
214                 break;
215
216 #if IS_ENABLED(CONFIG_IPV6)
217         case IPPROTO_DSTOPTS:
218         case IPPROTO_ROUTING:
219                 if (attrs[XFRMA_ALG_COMP]       ||
220                     attrs[XFRMA_ALG_AUTH]       ||
221                     attrs[XFRMA_ALG_AUTH_TRUNC] ||
222                     attrs[XFRMA_ALG_AEAD]       ||
223                     attrs[XFRMA_ALG_CRYPT]      ||
224                     attrs[XFRMA_ENCAP]          ||
225                     attrs[XFRMA_SEC_CTX]        ||
226                     attrs[XFRMA_TFCPAD]         ||
227                     !attrs[XFRMA_COADDR])
228                         goto out;
229                 break;
230 #endif
231
232         default:
233                 goto out;
234         }
235
236         if ((err = verify_aead(attrs)))
237                 goto out;
238         if ((err = verify_auth_trunc(attrs)))
239                 goto out;
240         if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
241                 goto out;
242         if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
243                 goto out;
244         if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
245                 goto out;
246         if ((err = verify_sec_ctx_len(attrs)))
247                 goto out;
248         if ((err = verify_replay(p, attrs)))
249                 goto out;
250
251         err = -EINVAL;
252         switch (p->mode) {
253         case XFRM_MODE_TRANSPORT:
254         case XFRM_MODE_TUNNEL:
255         case XFRM_MODE_ROUTEOPTIMIZATION:
256         case XFRM_MODE_BEET:
257                 break;
258
259         default:
260                 goto out;
261         }
262
263         err = 0;
264
265 out:
266         return err;
267 }
268
269 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
270                            struct xfrm_algo_desc *(*get_byname)(const char *, int),
271                            struct nlattr *rta)
272 {
273         struct xfrm_algo *p, *ualg;
274         struct xfrm_algo_desc *algo;
275
276         if (!rta)
277                 return 0;
278
279         ualg = nla_data(rta);
280
281         algo = get_byname(ualg->alg_name, 1);
282         if (!algo)
283                 return -ENOSYS;
284         *props = algo->desc.sadb_alg_id;
285
286         p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
287         if (!p)
288                 return -ENOMEM;
289
290         strcpy(p->alg_name, algo->name);
291         *algpp = p;
292         return 0;
293 }
294
295 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
296                        struct nlattr *rta)
297 {
298         struct xfrm_algo *ualg;
299         struct xfrm_algo_auth *p;
300         struct xfrm_algo_desc *algo;
301
302         if (!rta)
303                 return 0;
304
305         ualg = nla_data(rta);
306
307         algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
308         if (!algo)
309                 return -ENOSYS;
310         *props = algo->desc.sadb_alg_id;
311
312         p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
313         if (!p)
314                 return -ENOMEM;
315
316         strcpy(p->alg_name, algo->name);
317         p->alg_key_len = ualg->alg_key_len;
318         p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
319         memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
320
321         *algpp = p;
322         return 0;
323 }
324
325 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
326                              struct nlattr *rta)
327 {
328         struct xfrm_algo_auth *p, *ualg;
329         struct xfrm_algo_desc *algo;
330
331         if (!rta)
332                 return 0;
333
334         ualg = nla_data(rta);
335
336         algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
337         if (!algo)
338                 return -ENOSYS;
339         if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
340             ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
341                 return -EINVAL;
342         *props = algo->desc.sadb_alg_id;
343
344         p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
345         if (!p)
346                 return -ENOMEM;
347
348         strcpy(p->alg_name, algo->name);
349         if (!p->alg_trunc_len)
350                 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
351
352         *algpp = p;
353         return 0;
354 }
355
356 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
357                        struct nlattr *rta)
358 {
359         struct xfrm_algo_aead *p, *ualg;
360         struct xfrm_algo_desc *algo;
361
362         if (!rta)
363                 return 0;
364
365         ualg = nla_data(rta);
366
367         algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
368         if (!algo)
369                 return -ENOSYS;
370         *props = algo->desc.sadb_alg_id;
371
372         p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
373         if (!p)
374                 return -ENOMEM;
375
376         strcpy(p->alg_name, algo->name);
377         *algpp = p;
378         return 0;
379 }
380
381 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
382                                          struct nlattr *rp)
383 {
384         struct xfrm_replay_state_esn *up;
385         int ulen;
386
387         if (!replay_esn || !rp)
388                 return 0;
389
390         up = nla_data(rp);
391         ulen = xfrm_replay_state_esn_len(up);
392
393         if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
394                 return -EINVAL;
395
396         return 0;
397 }
398
399 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
400                                        struct xfrm_replay_state_esn **preplay_esn,
401                                        struct nlattr *rta)
402 {
403         struct xfrm_replay_state_esn *p, *pp, *up;
404         int klen, ulen;
405
406         if (!rta)
407                 return 0;
408
409         up = nla_data(rta);
410         klen = xfrm_replay_state_esn_len(up);
411         ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
412
413         p = kzalloc(klen, GFP_KERNEL);
414         if (!p)
415                 return -ENOMEM;
416
417         pp = kzalloc(klen, GFP_KERNEL);
418         if (!pp) {
419                 kfree(p);
420                 return -ENOMEM;
421         }
422
423         memcpy(p, up, ulen);
424         memcpy(pp, up, ulen);
425
426         *replay_esn = p;
427         *preplay_esn = pp;
428
429         return 0;
430 }
431
432 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
433 {
434         int len = 0;
435
436         if (xfrm_ctx) {
437                 len += sizeof(struct xfrm_user_sec_ctx);
438                 len += xfrm_ctx->ctx_len;
439         }
440         return len;
441 }
442
443 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
444 {
445         memcpy(&x->id, &p->id, sizeof(x->id));
446         memcpy(&x->sel, &p->sel, sizeof(x->sel));
447         memcpy(&x->lft, &p->lft, sizeof(x->lft));
448         x->props.mode = p->mode;
449         x->props.replay_window = min_t(unsigned int, p->replay_window,
450                                         sizeof(x->replay.bitmap) * 8);
451         x->props.reqid = p->reqid;
452         x->props.family = p->family;
453         memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
454         x->props.flags = p->flags;
455
456         if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
457                 x->sel.family = p->family;
458 }
459
460 /*
461  * someday when pfkey also has support, we could have the code
462  * somehow made shareable and move it to xfrm_state.c - JHS
463  *
464 */
465 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
466                                   int update_esn)
467 {
468         struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
469         struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
470         struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
471         struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
472         struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
473
474         if (re) {
475                 struct xfrm_replay_state_esn *replay_esn;
476                 replay_esn = nla_data(re);
477                 memcpy(x->replay_esn, replay_esn,
478                        xfrm_replay_state_esn_len(replay_esn));
479                 memcpy(x->preplay_esn, replay_esn,
480                        xfrm_replay_state_esn_len(replay_esn));
481         }
482
483         if (rp) {
484                 struct xfrm_replay_state *replay;
485                 replay = nla_data(rp);
486                 memcpy(&x->replay, replay, sizeof(*replay));
487                 memcpy(&x->preplay, replay, sizeof(*replay));
488         }
489
490         if (lt) {
491                 struct xfrm_lifetime_cur *ltime;
492                 ltime = nla_data(lt);
493                 x->curlft.bytes = ltime->bytes;
494                 x->curlft.packets = ltime->packets;
495                 x->curlft.add_time = ltime->add_time;
496                 x->curlft.use_time = ltime->use_time;
497         }
498
499         if (et)
500                 x->replay_maxage = nla_get_u32(et);
501
502         if (rt)
503                 x->replay_maxdiff = nla_get_u32(rt);
504 }
505
506 static struct xfrm_state *xfrm_state_construct(struct net *net,
507                                                struct xfrm_usersa_info *p,
508                                                struct nlattr **attrs,
509                                                int *errp)
510 {
511         struct xfrm_state *x = xfrm_state_alloc(net);
512         int err = -ENOMEM;
513
514         if (!x)
515                 goto error_no_put;
516
517         copy_from_user_state(x, p);
518
519         if (attrs[XFRMA_SA_EXTRA_FLAGS])
520                 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
521
522         if ((err = attach_aead(&x->aead, &x->props.ealgo,
523                                attrs[XFRMA_ALG_AEAD])))
524                 goto error;
525         if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
526                                      attrs[XFRMA_ALG_AUTH_TRUNC])))
527                 goto error;
528         if (!x->props.aalgo) {
529                 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
530                                        attrs[XFRMA_ALG_AUTH])))
531                         goto error;
532         }
533         if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
534                                    xfrm_ealg_get_byname,
535                                    attrs[XFRMA_ALG_CRYPT])))
536                 goto error;
537         if ((err = attach_one_algo(&x->calg, &x->props.calgo,
538                                    xfrm_calg_get_byname,
539                                    attrs[XFRMA_ALG_COMP])))
540                 goto error;
541
542         if (attrs[XFRMA_ENCAP]) {
543                 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
544                                    sizeof(*x->encap), GFP_KERNEL);
545                 if (x->encap == NULL)
546                         goto error;
547         }
548
549         if (attrs[XFRMA_TFCPAD])
550                 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
551
552         if (attrs[XFRMA_COADDR]) {
553                 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
554                                     sizeof(*x->coaddr), GFP_KERNEL);
555                 if (x->coaddr == NULL)
556                         goto error;
557         }
558
559         xfrm_mark_get(attrs, &x->mark);
560
561         err = __xfrm_init_state(x, false);
562         if (err)
563                 goto error;
564
565         if (attrs[XFRMA_SEC_CTX] &&
566             security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
567                 goto error;
568
569         if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
570                                                attrs[XFRMA_REPLAY_ESN_VAL])))
571                 goto error;
572
573         x->km.seq = p->seq;
574         x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
575         /* sysctl_xfrm_aevent_etime is in 100ms units */
576         x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
577
578         if ((err = xfrm_init_replay(x)))
579                 goto error;
580
581         /* override default values from above */
582         xfrm_update_ae_params(x, attrs, 0);
583
584         return x;
585
586 error:
587         x->km.state = XFRM_STATE_DEAD;
588         xfrm_state_put(x);
589 error_no_put:
590         *errp = err;
591         return NULL;
592 }
593
594 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
595                 struct nlattr **attrs)
596 {
597         struct net *net = sock_net(skb->sk);
598         struct xfrm_usersa_info *p = nlmsg_data(nlh);
599         struct xfrm_state *x;
600         int err;
601         struct km_event c;
602         kuid_t loginuid = audit_get_loginuid(current);
603         u32 sessionid = audit_get_sessionid(current);
604         u32 sid;
605
606         err = verify_newsa_info(p, attrs);
607         if (err)
608                 return err;
609
610         x = xfrm_state_construct(net, p, attrs, &err);
611         if (!x)
612                 return err;
613
614         xfrm_state_hold(x);
615         if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
616                 err = xfrm_state_add(x);
617         else
618                 err = xfrm_state_update(x);
619
620         security_task_getsecid(current, &sid);
621         xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
622
623         if (err < 0) {
624                 x->km.state = XFRM_STATE_DEAD;
625                 __xfrm_state_put(x);
626                 goto out;
627         }
628
629         c.seq = nlh->nlmsg_seq;
630         c.portid = nlh->nlmsg_pid;
631         c.event = nlh->nlmsg_type;
632
633         km_state_notify(x, &c);
634 out:
635         xfrm_state_put(x);
636         return err;
637 }
638
639 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
640                                                  struct xfrm_usersa_id *p,
641                                                  struct nlattr **attrs,
642                                                  int *errp)
643 {
644         struct xfrm_state *x = NULL;
645         struct xfrm_mark m;
646         int err;
647         u32 mark = xfrm_mark_get(attrs, &m);
648
649         if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
650                 err = -ESRCH;
651                 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
652         } else {
653                 xfrm_address_t *saddr = NULL;
654
655                 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
656                 if (!saddr) {
657                         err = -EINVAL;
658                         goto out;
659                 }
660
661                 err = -ESRCH;
662                 x = xfrm_state_lookup_byaddr(net, mark,
663                                              &p->daddr, saddr,
664                                              p->proto, p->family);
665         }
666
667  out:
668         if (!x && errp)
669                 *errp = err;
670         return x;
671 }
672
673 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
674                 struct nlattr **attrs)
675 {
676         struct net *net = sock_net(skb->sk);
677         struct xfrm_state *x;
678         int err = -ESRCH;
679         struct km_event c;
680         struct xfrm_usersa_id *p = nlmsg_data(nlh);
681         kuid_t loginuid = audit_get_loginuid(current);
682         u32 sessionid = audit_get_sessionid(current);
683         u32 sid;
684
685         x = xfrm_user_state_lookup(net, p, attrs, &err);
686         if (x == NULL)
687                 return err;
688
689         if ((err = security_xfrm_state_delete(x)) != 0)
690                 goto out;
691
692         if (xfrm_state_kern(x)) {
693                 err = -EPERM;
694                 goto out;
695         }
696
697         err = xfrm_state_delete(x);
698
699         if (err < 0)
700                 goto out;
701
702         c.seq = nlh->nlmsg_seq;
703         c.portid = nlh->nlmsg_pid;
704         c.event = nlh->nlmsg_type;
705         km_state_notify(x, &c);
706
707 out:
708         security_task_getsecid(current, &sid);
709         xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
710         xfrm_state_put(x);
711         return err;
712 }
713
714 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
715 {
716         memset(p, 0, sizeof(*p));
717         memcpy(&p->id, &x->id, sizeof(p->id));
718         memcpy(&p->sel, &x->sel, sizeof(p->sel));
719         memcpy(&p->lft, &x->lft, sizeof(p->lft));
720         memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
721         memcpy(&p->stats, &x->stats, sizeof(p->stats));
722         memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
723         p->mode = x->props.mode;
724         p->replay_window = x->props.replay_window;
725         p->reqid = x->props.reqid;
726         p->family = x->props.family;
727         p->flags = x->props.flags;
728         p->seq = x->km.seq;
729 }
730
731 struct xfrm_dump_info {
732         struct sk_buff *in_skb;
733         struct sk_buff *out_skb;
734         u32 nlmsg_seq;
735         u16 nlmsg_flags;
736 };
737
738 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
739 {
740         struct xfrm_user_sec_ctx *uctx;
741         struct nlattr *attr;
742         int ctx_size = sizeof(*uctx) + s->ctx_len;
743
744         attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
745         if (attr == NULL)
746                 return -EMSGSIZE;
747
748         uctx = nla_data(attr);
749         uctx->exttype = XFRMA_SEC_CTX;
750         uctx->len = ctx_size;
751         uctx->ctx_doi = s->ctx_doi;
752         uctx->ctx_alg = s->ctx_alg;
753         uctx->ctx_len = s->ctx_len;
754         memcpy(uctx + 1, s->ctx_str, s->ctx_len);
755
756         return 0;
757 }
758
759 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
760 {
761         struct xfrm_algo *algo;
762         struct nlattr *nla;
763
764         nla = nla_reserve(skb, XFRMA_ALG_AUTH,
765                           sizeof(*algo) + (auth->alg_key_len + 7) / 8);
766         if (!nla)
767                 return -EMSGSIZE;
768
769         algo = nla_data(nla);
770         strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
771         memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
772         algo->alg_key_len = auth->alg_key_len;
773
774         return 0;
775 }
776
777 /* Don't change this without updating xfrm_sa_len! */
778 static int copy_to_user_state_extra(struct xfrm_state *x,
779                                     struct xfrm_usersa_info *p,
780                                     struct sk_buff *skb)
781 {
782         int ret = 0;
783
784         copy_to_user_state(x, p);
785
786         if (x->props.extra_flags) {
787                 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
788                                   x->props.extra_flags);
789                 if (ret)
790                         goto out;
791         }
792
793         if (x->coaddr) {
794                 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
795                 if (ret)
796                         goto out;
797         }
798         if (x->lastused) {
799                 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
800                 if (ret)
801                         goto out;
802         }
803         if (x->aead) {
804                 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
805                 if (ret)
806                         goto out;
807         }
808         if (x->aalg) {
809                 ret = copy_to_user_auth(x->aalg, skb);
810                 if (!ret)
811                         ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
812                                       xfrm_alg_auth_len(x->aalg), x->aalg);
813                 if (ret)
814                         goto out;
815         }
816         if (x->ealg) {
817                 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
818                 if (ret)
819                         goto out;
820         }
821         if (x->calg) {
822                 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
823                 if (ret)
824                         goto out;
825         }
826         if (x->encap) {
827                 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
828                 if (ret)
829                         goto out;
830         }
831         if (x->tfcpad) {
832                 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
833                 if (ret)
834                         goto out;
835         }
836         ret = xfrm_mark_put(skb, &x->mark);
837         if (ret)
838                 goto out;
839         if (x->replay_esn) {
840                 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
841                               xfrm_replay_state_esn_len(x->replay_esn),
842                               x->replay_esn);
843                 if (ret)
844                         goto out;
845         }
846         if (x->security)
847                 ret = copy_sec_ctx(x->security, skb);
848 out:
849         return ret;
850 }
851
852 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
853 {
854         struct xfrm_dump_info *sp = ptr;
855         struct sk_buff *in_skb = sp->in_skb;
856         struct sk_buff *skb = sp->out_skb;
857         struct xfrm_usersa_info *p;
858         struct nlmsghdr *nlh;
859         int err;
860
861         nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
862                         XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
863         if (nlh == NULL)
864                 return -EMSGSIZE;
865
866         p = nlmsg_data(nlh);
867
868         err = copy_to_user_state_extra(x, p, skb);
869         if (err) {
870                 nlmsg_cancel(skb, nlh);
871                 return err;
872         }
873         nlmsg_end(skb, nlh);
874         return 0;
875 }
876
877 static int xfrm_dump_sa_done(struct netlink_callback *cb)
878 {
879         struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
880         struct sock *sk = cb->skb->sk;
881         struct net *net = sock_net(sk);
882
883         xfrm_state_walk_done(walk, net);
884         return 0;
885 }
886
887 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
888 {
889         struct net *net = sock_net(skb->sk);
890         struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
891         struct xfrm_dump_info info;
892
893         BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
894                      sizeof(cb->args) - sizeof(cb->args[0]));
895
896         info.in_skb = cb->skb;
897         info.out_skb = skb;
898         info.nlmsg_seq = cb->nlh->nlmsg_seq;
899         info.nlmsg_flags = NLM_F_MULTI;
900
901         if (!cb->args[0]) {
902                 cb->args[0] = 1;
903                 xfrm_state_walk_init(walk, 0);
904         }
905
906         (void) xfrm_state_walk(net, walk, dump_one_state, &info);
907
908         return skb->len;
909 }
910
911 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
912                                           struct xfrm_state *x, u32 seq)
913 {
914         struct xfrm_dump_info info;
915         struct sk_buff *skb;
916         int err;
917
918         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
919         if (!skb)
920                 return ERR_PTR(-ENOMEM);
921
922         info.in_skb = in_skb;
923         info.out_skb = skb;
924         info.nlmsg_seq = seq;
925         info.nlmsg_flags = 0;
926
927         err = dump_one_state(x, 0, &info);
928         if (err) {
929                 kfree_skb(skb);
930                 return ERR_PTR(err);
931         }
932
933         return skb;
934 }
935
936 static inline size_t xfrm_spdinfo_msgsize(void)
937 {
938         return NLMSG_ALIGN(4)
939                + nla_total_size(sizeof(struct xfrmu_spdinfo))
940                + nla_total_size(sizeof(struct xfrmu_spdhinfo));
941 }
942
943 static int build_spdinfo(struct sk_buff *skb, struct net *net,
944                          u32 portid, u32 seq, u32 flags)
945 {
946         struct xfrmk_spdinfo si;
947         struct xfrmu_spdinfo spc;
948         struct xfrmu_spdhinfo sph;
949         struct nlmsghdr *nlh;
950         int err;
951         u32 *f;
952
953         nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
954         if (nlh == NULL) /* shouldn't really happen ... */
955                 return -EMSGSIZE;
956
957         f = nlmsg_data(nlh);
958         *f = flags;
959         xfrm_spd_getinfo(net, &si);
960         spc.incnt = si.incnt;
961         spc.outcnt = si.outcnt;
962         spc.fwdcnt = si.fwdcnt;
963         spc.inscnt = si.inscnt;
964         spc.outscnt = si.outscnt;
965         spc.fwdscnt = si.fwdscnt;
966         sph.spdhcnt = si.spdhcnt;
967         sph.spdhmcnt = si.spdhmcnt;
968
969         err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
970         if (!err)
971                 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
972         if (err) {
973                 nlmsg_cancel(skb, nlh);
974                 return err;
975         }
976
977         return nlmsg_end(skb, nlh);
978 }
979
980 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
981                 struct nlattr **attrs)
982 {
983         struct net *net = sock_net(skb->sk);
984         struct sk_buff *r_skb;
985         u32 *flags = nlmsg_data(nlh);
986         u32 sportid = NETLINK_CB(skb).portid;
987         u32 seq = nlh->nlmsg_seq;
988
989         r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
990         if (r_skb == NULL)
991                 return -ENOMEM;
992
993         if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
994                 BUG();
995
996         return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
997 }
998
999 static inline size_t xfrm_sadinfo_msgsize(void)
1000 {
1001         return NLMSG_ALIGN(4)
1002                + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1003                + nla_total_size(4); /* XFRMA_SAD_CNT */
1004 }
1005
1006 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1007                          u32 portid, u32 seq, u32 flags)
1008 {
1009         struct xfrmk_sadinfo si;
1010         struct xfrmu_sadhinfo sh;
1011         struct nlmsghdr *nlh;
1012         int err;
1013         u32 *f;
1014
1015         nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1016         if (nlh == NULL) /* shouldn't really happen ... */
1017                 return -EMSGSIZE;
1018
1019         f = nlmsg_data(nlh);
1020         *f = flags;
1021         xfrm_sad_getinfo(net, &si);
1022
1023         sh.sadhmcnt = si.sadhmcnt;
1024         sh.sadhcnt = si.sadhcnt;
1025
1026         err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1027         if (!err)
1028                 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1029         if (err) {
1030                 nlmsg_cancel(skb, nlh);
1031                 return err;
1032         }
1033
1034         return nlmsg_end(skb, nlh);
1035 }
1036
1037 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1038                 struct nlattr **attrs)
1039 {
1040         struct net *net = sock_net(skb->sk);
1041         struct sk_buff *r_skb;
1042         u32 *flags = nlmsg_data(nlh);
1043         u32 sportid = NETLINK_CB(skb).portid;
1044         u32 seq = nlh->nlmsg_seq;
1045
1046         r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1047         if (r_skb == NULL)
1048                 return -ENOMEM;
1049
1050         if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1051                 BUG();
1052
1053         return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1054 }
1055
1056 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1057                 struct nlattr **attrs)
1058 {
1059         struct net *net = sock_net(skb->sk);
1060         struct xfrm_usersa_id *p = nlmsg_data(nlh);
1061         struct xfrm_state *x;
1062         struct sk_buff *resp_skb;
1063         int err = -ESRCH;
1064
1065         x = xfrm_user_state_lookup(net, p, attrs, &err);
1066         if (x == NULL)
1067                 goto out_noput;
1068
1069         resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1070         if (IS_ERR(resp_skb)) {
1071                 err = PTR_ERR(resp_skb);
1072         } else {
1073                 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1074         }
1075         xfrm_state_put(x);
1076 out_noput:
1077         return err;
1078 }
1079
1080 static int verify_userspi_info(struct xfrm_userspi_info *p)
1081 {
1082         switch (p->info.id.proto) {
1083         case IPPROTO_AH:
1084         case IPPROTO_ESP:
1085                 break;
1086
1087         case IPPROTO_COMP:
1088                 /* IPCOMP spi is 16-bits. */
1089                 if (p->max >= 0x10000)
1090                         return -EINVAL;
1091                 break;
1092
1093         default:
1094                 return -EINVAL;
1095         }
1096
1097         if (p->min > p->max)
1098                 return -EINVAL;
1099
1100         return 0;
1101 }
1102
1103 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1104                 struct nlattr **attrs)
1105 {
1106         struct net *net = sock_net(skb->sk);
1107         struct xfrm_state *x;
1108         struct xfrm_userspi_info *p;
1109         struct sk_buff *resp_skb;
1110         xfrm_address_t *daddr;
1111         int family;
1112         int err;
1113         u32 mark;
1114         struct xfrm_mark m;
1115
1116         p = nlmsg_data(nlh);
1117         err = verify_userspi_info(p);
1118         if (err)
1119                 goto out_noput;
1120
1121         family = p->info.family;
1122         daddr = &p->info.id.daddr;
1123
1124         x = NULL;
1125
1126         mark = xfrm_mark_get(attrs, &m);
1127         if (p->info.seq) {
1128                 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1129                 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1130                         xfrm_state_put(x);
1131                         x = NULL;
1132                 }
1133         }
1134
1135         if (!x)
1136                 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1137                                   p->info.id.proto, daddr,
1138                                   &p->info.saddr, 1,
1139                                   family);
1140         err = -ENOENT;
1141         if (x == NULL)
1142                 goto out_noput;
1143
1144         err = xfrm_alloc_spi(x, p->min, p->max);
1145         if (err)
1146                 goto out;
1147
1148         resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1149         if (IS_ERR(resp_skb)) {
1150                 err = PTR_ERR(resp_skb);
1151                 goto out;
1152         }
1153
1154         err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1155
1156 out:
1157         xfrm_state_put(x);
1158 out_noput:
1159         return err;
1160 }
1161
1162 static int verify_policy_dir(u8 dir)
1163 {
1164         switch (dir) {
1165         case XFRM_POLICY_IN:
1166         case XFRM_POLICY_OUT:
1167         case XFRM_POLICY_FWD:
1168                 break;
1169
1170         default:
1171                 return -EINVAL;
1172         }
1173
1174         return 0;
1175 }
1176
1177 static int verify_policy_type(u8 type)
1178 {
1179         switch (type) {
1180         case XFRM_POLICY_TYPE_MAIN:
1181 #ifdef CONFIG_XFRM_SUB_POLICY
1182         case XFRM_POLICY_TYPE_SUB:
1183 #endif
1184                 break;
1185
1186         default:
1187                 return -EINVAL;
1188         }
1189
1190         return 0;
1191 }
1192
1193 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1194 {
1195         int ret;
1196
1197         switch (p->share) {
1198         case XFRM_SHARE_ANY:
1199         case XFRM_SHARE_SESSION:
1200         case XFRM_SHARE_USER:
1201         case XFRM_SHARE_UNIQUE:
1202                 break;
1203
1204         default:
1205                 return -EINVAL;
1206         }
1207
1208         switch (p->action) {
1209         case XFRM_POLICY_ALLOW:
1210         case XFRM_POLICY_BLOCK:
1211                 break;
1212
1213         default:
1214                 return -EINVAL;
1215         }
1216
1217         switch (p->sel.family) {
1218         case AF_INET:
1219                 break;
1220
1221         case AF_INET6:
1222 #if IS_ENABLED(CONFIG_IPV6)
1223                 break;
1224 #else
1225                 return  -EAFNOSUPPORT;
1226 #endif
1227
1228         default:
1229                 return -EINVAL;
1230         }
1231
1232         ret = verify_policy_dir(p->dir);
1233         if (ret)
1234                 return ret;
1235         if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1236                 return -EINVAL;
1237
1238         return 0;
1239 }
1240
1241 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1242 {
1243         struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1244         struct xfrm_user_sec_ctx *uctx;
1245
1246         if (!rt)
1247                 return 0;
1248
1249         uctx = nla_data(rt);
1250         return security_xfrm_policy_alloc(&pol->security, uctx);
1251 }
1252
1253 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1254                            int nr)
1255 {
1256         int i;
1257
1258         xp->xfrm_nr = nr;
1259         for (i = 0; i < nr; i++, ut++) {
1260                 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1261
1262                 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1263                 memcpy(&t->saddr, &ut->saddr,
1264                        sizeof(xfrm_address_t));
1265                 t->reqid = ut->reqid;
1266                 t->mode = ut->mode;
1267                 t->share = ut->share;
1268                 t->optional = ut->optional;
1269                 t->aalgos = ut->aalgos;
1270                 t->ealgos = ut->ealgos;
1271                 t->calgos = ut->calgos;
1272                 /* If all masks are ~0, then we allow all algorithms. */
1273                 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1274                 t->encap_family = ut->family;
1275         }
1276 }
1277
1278 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1279 {
1280         int i;
1281
1282         if (nr > XFRM_MAX_DEPTH)
1283                 return -EINVAL;
1284
1285         for (i = 0; i < nr; i++) {
1286                 /* We never validated the ut->family value, so many
1287                  * applications simply leave it at zero.  The check was
1288                  * never made and ut->family was ignored because all
1289                  * templates could be assumed to have the same family as
1290                  * the policy itself.  Now that we will have ipv4-in-ipv6
1291                  * and ipv6-in-ipv4 tunnels, this is no longer true.
1292                  */
1293                 if (!ut[i].family)
1294                         ut[i].family = family;
1295
1296                 switch (ut[i].family) {
1297                 case AF_INET:
1298                         break;
1299 #if IS_ENABLED(CONFIG_IPV6)
1300                 case AF_INET6:
1301                         break;
1302 #endif
1303                 default:
1304                         return -EINVAL;
1305                 }
1306         }
1307
1308         return 0;
1309 }
1310
1311 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1312 {
1313         struct nlattr *rt = attrs[XFRMA_TMPL];
1314
1315         if (!rt) {
1316                 pol->xfrm_nr = 0;
1317         } else {
1318                 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1319                 int nr = nla_len(rt) / sizeof(*utmpl);
1320                 int err;
1321
1322                 err = validate_tmpl(nr, utmpl, pol->family);
1323                 if (err)
1324                         return err;
1325
1326                 copy_templates(pol, utmpl, nr);
1327         }
1328         return 0;
1329 }
1330
1331 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1332 {
1333         struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1334         struct xfrm_userpolicy_type *upt;
1335         u8 type = XFRM_POLICY_TYPE_MAIN;
1336         int err;
1337
1338         if (rt) {
1339                 upt = nla_data(rt);
1340                 type = upt->type;
1341         }
1342
1343         err = verify_policy_type(type);
1344         if (err)
1345                 return err;
1346
1347         *tp = type;
1348         return 0;
1349 }
1350
1351 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1352 {
1353         xp->priority = p->priority;
1354         xp->index = p->index;
1355         memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1356         memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1357         xp->action = p->action;
1358         xp->flags = p->flags;
1359         xp->family = p->sel.family;
1360         /* XXX xp->share = p->share; */
1361 }
1362
1363 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1364 {
1365         memset(p, 0, sizeof(*p));
1366         memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1367         memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1368         memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1369         p->priority = xp->priority;
1370         p->index = xp->index;
1371         p->sel.family = xp->family;
1372         p->dir = dir;
1373         p->action = xp->action;
1374         p->flags = xp->flags;
1375         p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1376 }
1377
1378 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1379 {
1380         struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1381         int err;
1382
1383         if (!xp) {
1384                 *errp = -ENOMEM;
1385                 return NULL;
1386         }
1387
1388         copy_from_user_policy(xp, p);
1389
1390         err = copy_from_user_policy_type(&xp->type, attrs);
1391         if (err)
1392                 goto error;
1393
1394         if (!(err = copy_from_user_tmpl(xp, attrs)))
1395                 err = copy_from_user_sec_ctx(xp, attrs);
1396         if (err)
1397                 goto error;
1398
1399         xfrm_mark_get(attrs, &xp->mark);
1400
1401         return xp;
1402  error:
1403         *errp = err;
1404         xp->walk.dead = 1;
1405         xfrm_policy_destroy(xp);
1406         return NULL;
1407 }
1408
1409 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1410                 struct nlattr **attrs)
1411 {
1412         struct net *net = sock_net(skb->sk);
1413         struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1414         struct xfrm_policy *xp;
1415         struct km_event c;
1416         int err;
1417         int excl;
1418         kuid_t loginuid = audit_get_loginuid(current);
1419         u32 sessionid = audit_get_sessionid(current);
1420         u32 sid;
1421
1422         err = verify_newpolicy_info(p);
1423         if (err)
1424                 return err;
1425         err = verify_sec_ctx_len(attrs);
1426         if (err)
1427                 return err;
1428
1429         xp = xfrm_policy_construct(net, p, attrs, &err);
1430         if (!xp)
1431                 return err;
1432
1433         /* shouldn't excl be based on nlh flags??
1434          * Aha! this is anti-netlink really i.e  more pfkey derived
1435          * in netlink excl is a flag and you wouldnt need
1436          * a type XFRM_MSG_UPDPOLICY - JHS */
1437         excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1438         err = xfrm_policy_insert(p->dir, xp, excl);
1439         security_task_getsecid(current, &sid);
1440         xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1441
1442         if (err) {
1443                 security_xfrm_policy_free(xp->security);
1444                 kfree(xp);
1445                 return err;
1446         }
1447
1448         c.event = nlh->nlmsg_type;
1449         c.seq = nlh->nlmsg_seq;
1450         c.portid = nlh->nlmsg_pid;
1451         km_policy_notify(xp, p->dir, &c);
1452
1453         xfrm_pol_put(xp);
1454
1455         return 0;
1456 }
1457
1458 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1459 {
1460         struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1461         int i;
1462
1463         if (xp->xfrm_nr == 0)
1464                 return 0;
1465
1466         for (i = 0; i < xp->xfrm_nr; i++) {
1467                 struct xfrm_user_tmpl *up = &vec[i];
1468                 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1469
1470                 memset(up, 0, sizeof(*up));
1471                 memcpy(&up->id, &kp->id, sizeof(up->id));
1472                 up->family = kp->encap_family;
1473                 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1474                 up->reqid = kp->reqid;
1475                 up->mode = kp->mode;
1476                 up->share = kp->share;
1477                 up->optional = kp->optional;
1478                 up->aalgos = kp->aalgos;
1479                 up->ealgos = kp->ealgos;
1480                 up->calgos = kp->calgos;
1481         }
1482
1483         return nla_put(skb, XFRMA_TMPL,
1484                        sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1485 }
1486
1487 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1488 {
1489         if (x->security) {
1490                 return copy_sec_ctx(x->security, skb);
1491         }
1492         return 0;
1493 }
1494
1495 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1496 {
1497         if (xp->security)
1498                 return copy_sec_ctx(xp->security, skb);
1499         return 0;
1500 }
1501 static inline size_t userpolicy_type_attrsize(void)
1502 {
1503 #ifdef CONFIG_XFRM_SUB_POLICY
1504         return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1505 #else
1506         return 0;
1507 #endif
1508 }
1509
1510 #ifdef CONFIG_XFRM_SUB_POLICY
1511 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1512 {
1513         struct xfrm_userpolicy_type upt = {
1514                 .type = type,
1515         };
1516
1517         return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1518 }
1519
1520 #else
1521 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1522 {
1523         return 0;
1524 }
1525 #endif
1526
1527 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1528 {
1529         struct xfrm_dump_info *sp = ptr;
1530         struct xfrm_userpolicy_info *p;
1531         struct sk_buff *in_skb = sp->in_skb;
1532         struct sk_buff *skb = sp->out_skb;
1533         struct nlmsghdr *nlh;
1534         int err;
1535
1536         nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1537                         XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1538         if (nlh == NULL)
1539                 return -EMSGSIZE;
1540
1541         p = nlmsg_data(nlh);
1542         copy_to_user_policy(xp, p, dir);
1543         err = copy_to_user_tmpl(xp, skb);
1544         if (!err)
1545                 err = copy_to_user_sec_ctx(xp, skb);
1546         if (!err)
1547                 err = copy_to_user_policy_type(xp->type, skb);
1548         if (!err)
1549                 err = xfrm_mark_put(skb, &xp->mark);
1550         if (err) {
1551                 nlmsg_cancel(skb, nlh);
1552                 return err;
1553         }
1554         nlmsg_end(skb, nlh);
1555         return 0;
1556 }
1557
1558 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1559 {
1560         struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1561         struct net *net = sock_net(cb->skb->sk);
1562
1563         xfrm_policy_walk_done(walk, net);
1564         return 0;
1565 }
1566
1567 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1568 {
1569         struct net *net = sock_net(skb->sk);
1570         struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1571         struct xfrm_dump_info info;
1572
1573         BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1574                      sizeof(cb->args) - sizeof(cb->args[0]));
1575
1576         info.in_skb = cb->skb;
1577         info.out_skb = skb;
1578         info.nlmsg_seq = cb->nlh->nlmsg_seq;
1579         info.nlmsg_flags = NLM_F_MULTI;
1580
1581         if (!cb->args[0]) {
1582                 cb->args[0] = 1;
1583                 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1584         }
1585
1586         (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1587
1588         return skb->len;
1589 }
1590
1591 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1592                                           struct xfrm_policy *xp,
1593                                           int dir, u32 seq)
1594 {
1595         struct xfrm_dump_info info;
1596         struct sk_buff *skb;
1597         int err;
1598
1599         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1600         if (!skb)
1601                 return ERR_PTR(-ENOMEM);
1602
1603         info.in_skb = in_skb;
1604         info.out_skb = skb;
1605         info.nlmsg_seq = seq;
1606         info.nlmsg_flags = 0;
1607
1608         err = dump_one_policy(xp, dir, 0, &info);
1609         if (err) {
1610                 kfree_skb(skb);
1611                 return ERR_PTR(err);
1612         }
1613
1614         return skb;
1615 }
1616
1617 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1618                 struct nlattr **attrs)
1619 {
1620         struct net *net = sock_net(skb->sk);
1621         struct xfrm_policy *xp;
1622         struct xfrm_userpolicy_id *p;
1623         u8 type = XFRM_POLICY_TYPE_MAIN;
1624         int err;
1625         struct km_event c;
1626         int delete;
1627         struct xfrm_mark m;
1628         u32 mark = xfrm_mark_get(attrs, &m);
1629
1630         p = nlmsg_data(nlh);
1631         delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1632
1633         err = copy_from_user_policy_type(&type, attrs);
1634         if (err)
1635                 return err;
1636
1637         err = verify_policy_dir(p->dir);
1638         if (err)
1639                 return err;
1640
1641         if (p->index)
1642                 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1643         else {
1644                 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1645                 struct xfrm_sec_ctx *ctx;
1646
1647                 err = verify_sec_ctx_len(attrs);
1648                 if (err)
1649                         return err;
1650
1651                 ctx = NULL;
1652                 if (rt) {
1653                         struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1654
1655                         err = security_xfrm_policy_alloc(&ctx, uctx);
1656                         if (err)
1657                                 return err;
1658                 }
1659                 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1660                                            ctx, delete, &err);
1661                 security_xfrm_policy_free(ctx);
1662         }
1663         if (xp == NULL)
1664                 return -ENOENT;
1665
1666         if (!delete) {
1667                 struct sk_buff *resp_skb;
1668
1669                 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1670                 if (IS_ERR(resp_skb)) {
1671                         err = PTR_ERR(resp_skb);
1672                 } else {
1673                         err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1674                                             NETLINK_CB(skb).portid);
1675                 }
1676         } else {
1677                 kuid_t loginuid = audit_get_loginuid(current);
1678                 u32 sessionid = audit_get_sessionid(current);
1679                 u32 sid;
1680
1681                 security_task_getsecid(current, &sid);
1682                 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1683                                          sid);
1684
1685                 if (err != 0)
1686                         goto out;
1687
1688                 c.data.byid = p->index;
1689                 c.event = nlh->nlmsg_type;
1690                 c.seq = nlh->nlmsg_seq;
1691                 c.portid = nlh->nlmsg_pid;
1692                 km_policy_notify(xp, p->dir, &c);
1693         }
1694
1695 out:
1696         xfrm_pol_put(xp);
1697         if (delete && err == 0)
1698                 xfrm_garbage_collect(net);
1699         return err;
1700 }
1701
1702 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1703                 struct nlattr **attrs)
1704 {
1705         struct net *net = sock_net(skb->sk);
1706         struct km_event c;
1707         struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1708         struct xfrm_audit audit_info;
1709         int err;
1710
1711         audit_info.loginuid = audit_get_loginuid(current);
1712         audit_info.sessionid = audit_get_sessionid(current);
1713         security_task_getsecid(current, &audit_info.secid);
1714         err = xfrm_state_flush(net, p->proto, &audit_info);
1715         if (err) {
1716                 if (err == -ESRCH) /* empty table */
1717                         return 0;
1718                 return err;
1719         }
1720         c.data.proto = p->proto;
1721         c.event = nlh->nlmsg_type;
1722         c.seq = nlh->nlmsg_seq;
1723         c.portid = nlh->nlmsg_pid;
1724         c.net = net;
1725         km_state_notify(NULL, &c);
1726
1727         return 0;
1728 }
1729
1730 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1731 {
1732         size_t replay_size = x->replay_esn ?
1733                               xfrm_replay_state_esn_len(x->replay_esn) :
1734                               sizeof(struct xfrm_replay_state);
1735
1736         return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1737                + nla_total_size(replay_size)
1738                + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1739                + nla_total_size(sizeof(struct xfrm_mark))
1740                + nla_total_size(4) /* XFRM_AE_RTHR */
1741                + nla_total_size(4); /* XFRM_AE_ETHR */
1742 }
1743
1744 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1745 {
1746         struct xfrm_aevent_id *id;
1747         struct nlmsghdr *nlh;
1748         int err;
1749
1750         nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1751         if (nlh == NULL)
1752                 return -EMSGSIZE;
1753
1754         id = nlmsg_data(nlh);
1755         memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1756         id->sa_id.spi = x->id.spi;
1757         id->sa_id.family = x->props.family;
1758         id->sa_id.proto = x->id.proto;
1759         memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1760         id->reqid = x->props.reqid;
1761         id->flags = c->data.aevent;
1762
1763         if (x->replay_esn) {
1764                 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1765                               xfrm_replay_state_esn_len(x->replay_esn),
1766                               x->replay_esn);
1767         } else {
1768                 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1769                               &x->replay);
1770         }
1771         if (err)
1772                 goto out_cancel;
1773         err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1774         if (err)
1775                 goto out_cancel;
1776
1777         if (id->flags & XFRM_AE_RTHR) {
1778                 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1779                 if (err)
1780                         goto out_cancel;
1781         }
1782         if (id->flags & XFRM_AE_ETHR) {
1783                 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1784                                   x->replay_maxage * 10 / HZ);
1785                 if (err)
1786                         goto out_cancel;
1787         }
1788         err = xfrm_mark_put(skb, &x->mark);
1789         if (err)
1790                 goto out_cancel;
1791
1792         return nlmsg_end(skb, nlh);
1793
1794 out_cancel:
1795         nlmsg_cancel(skb, nlh);
1796         return err;
1797 }
1798
1799 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1800                 struct nlattr **attrs)
1801 {
1802         struct net *net = sock_net(skb->sk);
1803         struct xfrm_state *x;
1804         struct sk_buff *r_skb;
1805         int err;
1806         struct km_event c;
1807         u32 mark;
1808         struct xfrm_mark m;
1809         struct xfrm_aevent_id *p = nlmsg_data(nlh);
1810         struct xfrm_usersa_id *id = &p->sa_id;
1811
1812         mark = xfrm_mark_get(attrs, &m);
1813
1814         x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1815         if (x == NULL)
1816                 return -ESRCH;
1817
1818         r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1819         if (r_skb == NULL) {
1820                 xfrm_state_put(x);
1821                 return -ENOMEM;
1822         }
1823
1824         /*
1825          * XXX: is this lock really needed - none of the other
1826          * gets lock (the concern is things getting updated
1827          * while we are still reading) - jhs
1828         */
1829         spin_lock_bh(&x->lock);
1830         c.data.aevent = p->flags;
1831         c.seq = nlh->nlmsg_seq;
1832         c.portid = nlh->nlmsg_pid;
1833
1834         if (build_aevent(r_skb, x, &c) < 0)
1835                 BUG();
1836         err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1837         spin_unlock_bh(&x->lock);
1838         xfrm_state_put(x);
1839         return err;
1840 }
1841
1842 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1843                 struct nlattr **attrs)
1844 {
1845         struct net *net = sock_net(skb->sk);
1846         struct xfrm_state *x;
1847         struct km_event c;
1848         int err = - EINVAL;
1849         u32 mark = 0;
1850         struct xfrm_mark m;
1851         struct xfrm_aevent_id *p = nlmsg_data(nlh);
1852         struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1853         struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1854         struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1855
1856         if (!lt && !rp && !re)
1857                 return err;
1858
1859         /* pedantic mode - thou shalt sayeth replaceth */
1860         if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1861                 return err;
1862
1863         mark = xfrm_mark_get(attrs, &m);
1864
1865         x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1866         if (x == NULL)
1867                 return -ESRCH;
1868
1869         if (x->km.state != XFRM_STATE_VALID)
1870                 goto out;
1871
1872         err = xfrm_replay_verify_len(x->replay_esn, re);
1873         if (err)
1874                 goto out;
1875
1876         spin_lock_bh(&x->lock);
1877         xfrm_update_ae_params(x, attrs, 1);
1878         spin_unlock_bh(&x->lock);
1879
1880         c.event = nlh->nlmsg_type;
1881         c.seq = nlh->nlmsg_seq;
1882         c.portid = nlh->nlmsg_pid;
1883         c.data.aevent = XFRM_AE_CU;
1884         km_state_notify(x, &c);
1885         err = 0;
1886 out:
1887         xfrm_state_put(x);
1888         return err;
1889 }
1890
1891 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1892                 struct nlattr **attrs)
1893 {
1894         struct net *net = sock_net(skb->sk);
1895         struct km_event c;
1896         u8 type = XFRM_POLICY_TYPE_MAIN;
1897         int err;
1898         struct xfrm_audit audit_info;
1899
1900         err = copy_from_user_policy_type(&type, attrs);
1901         if (err)
1902                 return err;
1903
1904         audit_info.loginuid = audit_get_loginuid(current);
1905         audit_info.sessionid = audit_get_sessionid(current);
1906         security_task_getsecid(current, &audit_info.secid);
1907         err = xfrm_policy_flush(net, type, &audit_info);
1908         if (err) {
1909                 if (err == -ESRCH) /* empty table */
1910                         return 0;
1911                 return err;
1912         }
1913
1914         c.data.type = type;
1915         c.event = nlh->nlmsg_type;
1916         c.seq = nlh->nlmsg_seq;
1917         c.portid = nlh->nlmsg_pid;
1918         c.net = net;
1919         km_policy_notify(NULL, 0, &c);
1920         return 0;
1921 }
1922
1923 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1924                 struct nlattr **attrs)
1925 {
1926         struct net *net = sock_net(skb->sk);
1927         struct xfrm_policy *xp;
1928         struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1929         struct xfrm_userpolicy_info *p = &up->pol;
1930         u8 type = XFRM_POLICY_TYPE_MAIN;
1931         int err = -ENOENT;
1932         struct xfrm_mark m;
1933         u32 mark = xfrm_mark_get(attrs, &m);
1934
1935         err = copy_from_user_policy_type(&type, attrs);
1936         if (err)
1937                 return err;
1938
1939         err = verify_policy_dir(p->dir);
1940         if (err)
1941                 return err;
1942
1943         if (p->index)
1944                 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1945         else {
1946                 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1947                 struct xfrm_sec_ctx *ctx;
1948
1949                 err = verify_sec_ctx_len(attrs);
1950                 if (err)
1951                         return err;
1952
1953                 ctx = NULL;
1954                 if (rt) {
1955                         struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1956
1957                         err = security_xfrm_policy_alloc(&ctx, uctx);
1958                         if (err)
1959                                 return err;
1960                 }
1961                 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1962                                            &p->sel, ctx, 0, &err);
1963                 security_xfrm_policy_free(ctx);
1964         }
1965         if (xp == NULL)
1966                 return -ENOENT;
1967
1968         if (unlikely(xp->walk.dead))
1969                 goto out;
1970
1971         err = 0;
1972         if (up->hard) {
1973                 kuid_t loginuid = audit_get_loginuid(current);
1974                 u32 sessionid = audit_get_sessionid(current);
1975                 u32 sid;
1976
1977                 security_task_getsecid(current, &sid);
1978                 xfrm_policy_delete(xp, p->dir);
1979                 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1980
1981         } else {
1982                 // reset the timers here?
1983                 WARN(1, "Dont know what to do with soft policy expire\n");
1984         }
1985         km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
1986
1987 out:
1988         xfrm_pol_put(xp);
1989         return err;
1990 }
1991
1992 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1993                 struct nlattr **attrs)
1994 {
1995         struct net *net = sock_net(skb->sk);
1996         struct xfrm_state *x;
1997         int err;
1998         struct xfrm_user_expire *ue = nlmsg_data(nlh);
1999         struct xfrm_usersa_info *p = &ue->state;
2000         struct xfrm_mark m;
2001         u32 mark = xfrm_mark_get(attrs, &m);
2002
2003         x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2004
2005         err = -ENOENT;
2006         if (x == NULL)
2007                 return err;
2008
2009         spin_lock_bh(&x->lock);
2010         err = -EINVAL;
2011         if (x->km.state != XFRM_STATE_VALID)
2012                 goto out;
2013         km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2014
2015         if (ue->hard) {
2016                 kuid_t loginuid = audit_get_loginuid(current);
2017                 u32 sessionid = audit_get_sessionid(current);
2018                 u32 sid;
2019
2020                 security_task_getsecid(current, &sid);
2021                 __xfrm_state_delete(x);
2022                 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
2023         }
2024         err = 0;
2025 out:
2026         spin_unlock_bh(&x->lock);
2027         xfrm_state_put(x);
2028         return err;
2029 }
2030
2031 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2032                 struct nlattr **attrs)
2033 {
2034         struct net *net = sock_net(skb->sk);
2035         struct xfrm_policy *xp;
2036         struct xfrm_user_tmpl *ut;
2037         int i;
2038         struct nlattr *rt = attrs[XFRMA_TMPL];
2039         struct xfrm_mark mark;
2040
2041         struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2042         struct xfrm_state *x = xfrm_state_alloc(net);
2043         int err = -ENOMEM;
2044
2045         if (!x)
2046                 goto nomem;
2047
2048         xfrm_mark_get(attrs, &mark);
2049
2050         err = verify_newpolicy_info(&ua->policy);
2051         if (err)
2052                 goto bad_policy;
2053
2054         /*   build an XP */
2055         xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2056         if (!xp)
2057                 goto free_state;
2058
2059         memcpy(&x->id, &ua->id, sizeof(ua->id));
2060         memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2061         memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2062         xp->mark.m = x->mark.m = mark.m;
2063         xp->mark.v = x->mark.v = mark.v;
2064         ut = nla_data(rt);
2065         /* extract the templates and for each call km_key */
2066         for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2067                 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2068                 memcpy(&x->id, &t->id, sizeof(x->id));
2069                 x->props.mode = t->mode;
2070                 x->props.reqid = t->reqid;
2071                 x->props.family = ut->family;
2072                 t->aalgos = ua->aalgos;
2073                 t->ealgos = ua->ealgos;
2074                 t->calgos = ua->calgos;
2075                 err = km_query(x, t, xp);
2076
2077         }
2078
2079         kfree(x);
2080         kfree(xp);
2081
2082         return 0;
2083
2084 bad_policy:
2085         WARN(1, "BAD policy passed\n");
2086 free_state:
2087         kfree(x);
2088 nomem:
2089         return err;
2090 }
2091
2092 #ifdef CONFIG_XFRM_MIGRATE
2093 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2094                                   struct xfrm_kmaddress *k,
2095                                   struct nlattr **attrs, int *num)
2096 {
2097         struct nlattr *rt = attrs[XFRMA_MIGRATE];
2098         struct xfrm_user_migrate *um;
2099         int i, num_migrate;
2100
2101         if (k != NULL) {
2102                 struct xfrm_user_kmaddress *uk;
2103
2104                 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2105                 memcpy(&k->local, &uk->local, sizeof(k->local));
2106                 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2107                 k->family = uk->family;
2108                 k->reserved = uk->reserved;
2109         }
2110
2111         um = nla_data(rt);
2112         num_migrate = nla_len(rt) / sizeof(*um);
2113
2114         if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2115                 return -EINVAL;
2116
2117         for (i = 0; i < num_migrate; i++, um++, ma++) {
2118                 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2119                 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2120                 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2121                 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2122
2123                 ma->proto = um->proto;
2124                 ma->mode = um->mode;
2125                 ma->reqid = um->reqid;
2126
2127                 ma->old_family = um->old_family;
2128                 ma->new_family = um->new_family;
2129         }
2130
2131         *num = i;
2132         return 0;
2133 }
2134
2135 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2136                            struct nlattr **attrs)
2137 {
2138         struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2139         struct xfrm_migrate m[XFRM_MAX_DEPTH];
2140         struct xfrm_kmaddress km, *kmp;
2141         u8 type;
2142         int err;
2143         int n = 0;
2144         struct net *net = sock_net(skb->sk);
2145
2146         if (attrs[XFRMA_MIGRATE] == NULL)
2147                 return -EINVAL;
2148
2149         kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2150
2151         err = copy_from_user_policy_type(&type, attrs);
2152         if (err)
2153                 return err;
2154
2155         err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2156         if (err)
2157                 return err;
2158
2159         if (!n)
2160                 return 0;
2161
2162         xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2163
2164         return 0;
2165 }
2166 #else
2167 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2168                            struct nlattr **attrs)
2169 {
2170         return -ENOPROTOOPT;
2171 }
2172 #endif
2173
2174 #ifdef CONFIG_XFRM_MIGRATE
2175 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2176 {
2177         struct xfrm_user_migrate um;
2178
2179         memset(&um, 0, sizeof(um));
2180         um.proto = m->proto;
2181         um.mode = m->mode;
2182         um.reqid = m->reqid;
2183         um.old_family = m->old_family;
2184         memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2185         memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2186         um.new_family = m->new_family;
2187         memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2188         memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2189
2190         return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2191 }
2192
2193 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2194 {
2195         struct xfrm_user_kmaddress uk;
2196
2197         memset(&uk, 0, sizeof(uk));
2198         uk.family = k->family;
2199         uk.reserved = k->reserved;
2200         memcpy(&uk.local, &k->local, sizeof(uk.local));
2201         memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2202
2203         return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2204 }
2205
2206 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2207 {
2208         return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2209               + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2210               + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2211               + userpolicy_type_attrsize();
2212 }
2213
2214 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2215                          int num_migrate, const struct xfrm_kmaddress *k,
2216                          const struct xfrm_selector *sel, u8 dir, u8 type)
2217 {
2218         const struct xfrm_migrate *mp;
2219         struct xfrm_userpolicy_id *pol_id;
2220         struct nlmsghdr *nlh;
2221         int i, err;
2222
2223         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2224         if (nlh == NULL)
2225                 return -EMSGSIZE;
2226
2227         pol_id = nlmsg_data(nlh);
2228         /* copy data from selector, dir, and type to the pol_id */
2229         memset(pol_id, 0, sizeof(*pol_id));
2230         memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2231         pol_id->dir = dir;
2232
2233         if (k != NULL) {
2234                 err = copy_to_user_kmaddress(k, skb);
2235                 if (err)
2236                         goto out_cancel;
2237         }
2238         err = copy_to_user_policy_type(type, skb);
2239         if (err)
2240                 goto out_cancel;
2241         for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2242                 err = copy_to_user_migrate(mp, skb);
2243                 if (err)
2244                         goto out_cancel;
2245         }
2246
2247         return nlmsg_end(skb, nlh);
2248
2249 out_cancel:
2250         nlmsg_cancel(skb, nlh);
2251         return err;
2252 }
2253
2254 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2255                              const struct xfrm_migrate *m, int num_migrate,
2256                              const struct xfrm_kmaddress *k)
2257 {
2258         struct net *net = &init_net;
2259         struct sk_buff *skb;
2260
2261         skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2262         if (skb == NULL)
2263                 return -ENOMEM;
2264
2265         /* build migrate */
2266         if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2267                 BUG();
2268
2269         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2270 }
2271 #else
2272 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2273                              const struct xfrm_migrate *m, int num_migrate,
2274                              const struct xfrm_kmaddress *k)
2275 {
2276         return -ENOPROTOOPT;
2277 }
2278 #endif
2279
2280 #define XMSGSIZE(type) sizeof(struct type)
2281
2282 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2283         [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2284         [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2285         [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2286         [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2287         [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2288         [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2289         [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2290         [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2291         [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2292         [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2293         [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2294         [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2295         [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2296         [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2297         [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2298         [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2299         [XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2300         [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2301         [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2302         [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2303 };
2304
2305 #undef XMSGSIZE
2306
2307 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2308         [XFRMA_SA]              = { .len = sizeof(struct xfrm_usersa_info)},
2309         [XFRMA_POLICY]          = { .len = sizeof(struct xfrm_userpolicy_info)},
2310         [XFRMA_LASTUSED]        = { .type = NLA_U64},
2311         [XFRMA_ALG_AUTH_TRUNC]  = { .len = sizeof(struct xfrm_algo_auth)},
2312         [XFRMA_ALG_AEAD]        = { .len = sizeof(struct xfrm_algo_aead) },
2313         [XFRMA_ALG_AUTH]        = { .len = sizeof(struct xfrm_algo) },
2314         [XFRMA_ALG_CRYPT]       = { .len = sizeof(struct xfrm_algo) },
2315         [XFRMA_ALG_COMP]        = { .len = sizeof(struct xfrm_algo) },
2316         [XFRMA_ENCAP]           = { .len = sizeof(struct xfrm_encap_tmpl) },
2317         [XFRMA_TMPL]            = { .len = sizeof(struct xfrm_user_tmpl) },
2318         [XFRMA_SEC_CTX]         = { .len = sizeof(struct xfrm_sec_ctx) },
2319         [XFRMA_LTIME_VAL]       = { .len = sizeof(struct xfrm_lifetime_cur) },
2320         [XFRMA_REPLAY_VAL]      = { .len = sizeof(struct xfrm_replay_state) },
2321         [XFRMA_REPLAY_THRESH]   = { .type = NLA_U32 },
2322         [XFRMA_ETIMER_THRESH]   = { .type = NLA_U32 },
2323         [XFRMA_SRCADDR]         = { .len = sizeof(xfrm_address_t) },
2324         [XFRMA_COADDR]          = { .len = sizeof(xfrm_address_t) },
2325         [XFRMA_POLICY_TYPE]     = { .len = sizeof(struct xfrm_userpolicy_type)},
2326         [XFRMA_MIGRATE]         = { .len = sizeof(struct xfrm_user_migrate) },
2327         [XFRMA_KMADDRESS]       = { .len = sizeof(struct xfrm_user_kmaddress) },
2328         [XFRMA_MARK]            = { .len = sizeof(struct xfrm_mark) },
2329         [XFRMA_TFCPAD]          = { .type = NLA_U32 },
2330         [XFRMA_REPLAY_ESN_VAL]  = { .len = sizeof(struct xfrm_replay_state_esn) },
2331         [XFRMA_SA_EXTRA_FLAGS]  = { .type = NLA_U32 },
2332 };
2333
2334 static const struct xfrm_link {
2335         int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2336         int (*dump)(struct sk_buff *, struct netlink_callback *);
2337         int (*done)(struct netlink_callback *);
2338 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2339         [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2340         [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2341         [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2342                                                    .dump = xfrm_dump_sa,
2343                                                    .done = xfrm_dump_sa_done  },
2344         [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2345         [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2346         [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2347                                                    .dump = xfrm_dump_policy,
2348                                                    .done = xfrm_dump_policy_done },
2349         [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2350         [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
2351         [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2352         [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2353         [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2354         [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2355         [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2356         [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2357         [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2358         [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
2359         [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
2360         [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2361         [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
2362 };
2363
2364 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2365 {
2366         struct net *net = sock_net(skb->sk);
2367         struct nlattr *attrs[XFRMA_MAX+1];
2368         const struct xfrm_link *link;
2369         int type, err;
2370
2371         type = nlh->nlmsg_type;
2372         if (type > XFRM_MSG_MAX)
2373                 return -EINVAL;
2374
2375         type -= XFRM_MSG_BASE;
2376         link = &xfrm_dispatch[type];
2377
2378         /* All operations require privileges, even GET */
2379         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2380                 return -EPERM;
2381
2382         if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2383              type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2384             (nlh->nlmsg_flags & NLM_F_DUMP)) {
2385                 if (link->dump == NULL)
2386                         return -EINVAL;
2387
2388                 {
2389                         struct netlink_dump_control c = {
2390                                 .dump = link->dump,
2391                                 .done = link->done,
2392                         };
2393                         return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2394                 }
2395         }
2396
2397         err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2398                           xfrma_policy);
2399         if (err < 0)
2400                 return err;
2401
2402         if (link->doit == NULL)
2403                 return -EINVAL;
2404
2405         return link->doit(skb, nlh, attrs);
2406 }
2407
2408 static void xfrm_netlink_rcv(struct sk_buff *skb)
2409 {
2410         struct net *net = sock_net(skb->sk);
2411
2412         mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2413         netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2414         mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2415 }
2416
2417 static inline size_t xfrm_expire_msgsize(void)
2418 {
2419         return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2420                + nla_total_size(sizeof(struct xfrm_mark));
2421 }
2422
2423 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2424 {
2425         struct xfrm_user_expire *ue;
2426         struct nlmsghdr *nlh;
2427         int err;
2428
2429         nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2430         if (nlh == NULL)
2431                 return -EMSGSIZE;
2432
2433         ue = nlmsg_data(nlh);
2434         copy_to_user_state(x, &ue->state);
2435         ue->hard = (c->data.hard != 0) ? 1 : 0;
2436
2437         err = xfrm_mark_put(skb, &x->mark);
2438         if (err)
2439                 return err;
2440
2441         return nlmsg_end(skb, nlh);
2442 }
2443
2444 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2445 {
2446         struct net *net = xs_net(x);
2447         struct sk_buff *skb;
2448
2449         skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2450         if (skb == NULL)
2451                 return -ENOMEM;
2452
2453         if (build_expire(skb, x, c) < 0) {
2454                 kfree_skb(skb);
2455                 return -EMSGSIZE;
2456         }
2457
2458         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2459 }
2460
2461 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2462 {
2463         struct net *net = xs_net(x);
2464         struct sk_buff *skb;
2465
2466         skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2467         if (skb == NULL)
2468                 return -ENOMEM;
2469
2470         if (build_aevent(skb, x, c) < 0)
2471                 BUG();
2472
2473         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2474 }
2475
2476 static int xfrm_notify_sa_flush(const struct km_event *c)
2477 {
2478         struct net *net = c->net;
2479         struct xfrm_usersa_flush *p;
2480         struct nlmsghdr *nlh;
2481         struct sk_buff *skb;
2482         int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2483
2484         skb = nlmsg_new(len, GFP_ATOMIC);
2485         if (skb == NULL)
2486                 return -ENOMEM;
2487
2488         nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2489         if (nlh == NULL) {
2490                 kfree_skb(skb);
2491                 return -EMSGSIZE;
2492         }
2493
2494         p = nlmsg_data(nlh);
2495         p->proto = c->data.proto;
2496
2497         nlmsg_end(skb, nlh);
2498
2499         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2500 }
2501
2502 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2503 {
2504         size_t l = 0;
2505         if (x->aead)
2506                 l += nla_total_size(aead_len(x->aead));
2507         if (x->aalg) {
2508                 l += nla_total_size(sizeof(struct xfrm_algo) +
2509                                     (x->aalg->alg_key_len + 7) / 8);
2510                 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2511         }
2512         if (x->ealg)
2513                 l += nla_total_size(xfrm_alg_len(x->ealg));
2514         if (x->calg)
2515                 l += nla_total_size(sizeof(*x->calg));
2516         if (x->encap)
2517                 l += nla_total_size(sizeof(*x->encap));
2518         if (x->tfcpad)
2519                 l += nla_total_size(sizeof(x->tfcpad));
2520         if (x->replay_esn)
2521                 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2522         if (x->security)
2523                 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2524                                     x->security->ctx_len);
2525         if (x->coaddr)
2526                 l += nla_total_size(sizeof(*x->coaddr));
2527         if (x->props.extra_flags)
2528                 l += nla_total_size(sizeof(x->props.extra_flags));
2529
2530         /* Must count x->lastused as it may become non-zero behind our back. */
2531         l += nla_total_size(sizeof(u64));
2532
2533         return l;
2534 }
2535
2536 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2537 {
2538         struct net *net = xs_net(x);
2539         struct xfrm_usersa_info *p;
2540         struct xfrm_usersa_id *id;
2541         struct nlmsghdr *nlh;
2542         struct sk_buff *skb;
2543         int len = xfrm_sa_len(x);
2544         int headlen, err;
2545
2546         headlen = sizeof(*p);
2547         if (c->event == XFRM_MSG_DELSA) {
2548                 len += nla_total_size(headlen);
2549                 headlen = sizeof(*id);
2550                 len += nla_total_size(sizeof(struct xfrm_mark));
2551         }
2552         len += NLMSG_ALIGN(headlen);
2553
2554         skb = nlmsg_new(len, GFP_ATOMIC);
2555         if (skb == NULL)
2556                 return -ENOMEM;
2557
2558         nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2559         err = -EMSGSIZE;
2560         if (nlh == NULL)
2561                 goto out_free_skb;
2562
2563         p = nlmsg_data(nlh);
2564         if (c->event == XFRM_MSG_DELSA) {
2565                 struct nlattr *attr;
2566
2567                 id = nlmsg_data(nlh);
2568                 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2569                 id->spi = x->id.spi;
2570                 id->family = x->props.family;
2571                 id->proto = x->id.proto;
2572
2573                 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2574                 err = -EMSGSIZE;
2575                 if (attr == NULL)
2576                         goto out_free_skb;
2577
2578                 p = nla_data(attr);
2579         }
2580         err = copy_to_user_state_extra(x, p, skb);
2581         if (err)
2582                 goto out_free_skb;
2583
2584         nlmsg_end(skb, nlh);
2585
2586         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2587
2588 out_free_skb:
2589         kfree_skb(skb);
2590         return err;
2591 }
2592
2593 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2594 {
2595
2596         switch (c->event) {
2597         case XFRM_MSG_EXPIRE:
2598                 return xfrm_exp_state_notify(x, c);
2599         case XFRM_MSG_NEWAE:
2600                 return xfrm_aevent_state_notify(x, c);
2601         case XFRM_MSG_DELSA:
2602         case XFRM_MSG_UPDSA:
2603         case XFRM_MSG_NEWSA:
2604                 return xfrm_notify_sa(x, c);
2605         case XFRM_MSG_FLUSHSA:
2606                 return xfrm_notify_sa_flush(c);
2607         default:
2608                 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2609                        c->event);
2610                 break;
2611         }
2612
2613         return 0;
2614
2615 }
2616
2617 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2618                                           struct xfrm_policy *xp)
2619 {
2620         return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2621                + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2622                + nla_total_size(sizeof(struct xfrm_mark))
2623                + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2624                + userpolicy_type_attrsize();
2625 }
2626
2627 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2628                          struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2629 {
2630         __u32 seq = xfrm_get_acqseq();
2631         struct xfrm_user_acquire *ua;
2632         struct nlmsghdr *nlh;
2633         int err;
2634
2635         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2636         if (nlh == NULL)
2637                 return -EMSGSIZE;
2638
2639         ua = nlmsg_data(nlh);
2640         memcpy(&ua->id, &x->id, sizeof(ua->id));
2641         memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2642         memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2643         copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2644         ua->aalgos = xt->aalgos;
2645         ua->ealgos = xt->ealgos;
2646         ua->calgos = xt->calgos;
2647         ua->seq = x->km.seq = seq;
2648
2649         err = copy_to_user_tmpl(xp, skb);
2650         if (!err)
2651                 err = copy_to_user_state_sec_ctx(x, skb);
2652         if (!err)
2653                 err = copy_to_user_policy_type(xp->type, skb);
2654         if (!err)
2655                 err = xfrm_mark_put(skb, &xp->mark);
2656         if (err) {
2657                 nlmsg_cancel(skb, nlh);
2658                 return err;
2659         }
2660
2661         return nlmsg_end(skb, nlh);
2662 }
2663
2664 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2665                              struct xfrm_policy *xp)
2666 {
2667         struct net *net = xs_net(x);
2668         struct sk_buff *skb;
2669
2670         skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2671         if (skb == NULL)
2672                 return -ENOMEM;
2673
2674         if (build_acquire(skb, x, xt, xp) < 0)
2675                 BUG();
2676
2677         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2678 }
2679
2680 /* User gives us xfrm_user_policy_info followed by an array of 0
2681  * or more templates.
2682  */
2683 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2684                                                u8 *data, int len, int *dir)
2685 {
2686         struct net *net = sock_net(sk);
2687         struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2688         struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2689         struct xfrm_policy *xp;
2690         int nr;
2691
2692         switch (sk->sk_family) {
2693         case AF_INET:
2694                 if (opt != IP_XFRM_POLICY) {
2695                         *dir = -EOPNOTSUPP;
2696                         return NULL;
2697                 }
2698                 break;
2699 #if IS_ENABLED(CONFIG_IPV6)
2700         case AF_INET6:
2701                 if (opt != IPV6_XFRM_POLICY) {
2702                         *dir = -EOPNOTSUPP;
2703                         return NULL;
2704                 }
2705                 break;
2706 #endif
2707         default:
2708                 *dir = -EINVAL;
2709                 return NULL;
2710         }
2711
2712         *dir = -EINVAL;
2713
2714         if (len < sizeof(*p) ||
2715             verify_newpolicy_info(p))
2716                 return NULL;
2717
2718         nr = ((len - sizeof(*p)) / sizeof(*ut));
2719         if (validate_tmpl(nr, ut, p->sel.family))
2720                 return NULL;
2721
2722         if (p->dir > XFRM_POLICY_OUT)
2723                 return NULL;
2724
2725         xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2726         if (xp == NULL) {
2727                 *dir = -ENOBUFS;
2728                 return NULL;
2729         }
2730
2731         copy_from_user_policy(xp, p);
2732         xp->type = XFRM_POLICY_TYPE_MAIN;
2733         copy_templates(xp, ut, nr);
2734
2735         *dir = p->dir;
2736
2737         return xp;
2738 }
2739
2740 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2741 {
2742         return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2743                + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2744                + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2745                + nla_total_size(sizeof(struct xfrm_mark))
2746                + userpolicy_type_attrsize();
2747 }
2748
2749 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2750                            int dir, const struct km_event *c)
2751 {
2752         struct xfrm_user_polexpire *upe;
2753         int hard = c->data.hard;
2754         struct nlmsghdr *nlh;
2755         int err;
2756
2757         nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2758         if (nlh == NULL)
2759                 return -EMSGSIZE;
2760
2761         upe = nlmsg_data(nlh);
2762         copy_to_user_policy(xp, &upe->pol, dir);
2763         err = copy_to_user_tmpl(xp, skb);
2764         if (!err)
2765                 err = copy_to_user_sec_ctx(xp, skb);
2766         if (!err)
2767                 err = copy_to_user_policy_type(xp->type, skb);
2768         if (!err)
2769                 err = xfrm_mark_put(skb, &xp->mark);
2770         if (err) {
2771                 nlmsg_cancel(skb, nlh);
2772                 return err;
2773         }
2774         upe->hard = !!hard;
2775
2776         return nlmsg_end(skb, nlh);
2777 }
2778
2779 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2780 {
2781         struct net *net = xp_net(xp);
2782         struct sk_buff *skb;
2783
2784         skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2785         if (skb == NULL)
2786                 return -ENOMEM;
2787
2788         if (build_polexpire(skb, xp, dir, c) < 0)
2789                 BUG();
2790
2791         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2792 }
2793
2794 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2795 {
2796         int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2797         struct net *net = xp_net(xp);
2798         struct xfrm_userpolicy_info *p;
2799         struct xfrm_userpolicy_id *id;
2800         struct nlmsghdr *nlh;
2801         struct sk_buff *skb;
2802         int headlen, err;
2803
2804         headlen = sizeof(*p);
2805         if (c->event == XFRM_MSG_DELPOLICY) {
2806                 len += nla_total_size(headlen);
2807                 headlen = sizeof(*id);
2808         }
2809         len += userpolicy_type_attrsize();
2810         len += nla_total_size(sizeof(struct xfrm_mark));
2811         len += NLMSG_ALIGN(headlen);
2812
2813         skb = nlmsg_new(len, GFP_ATOMIC);
2814         if (skb == NULL)
2815                 return -ENOMEM;
2816
2817         nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2818         err = -EMSGSIZE;
2819         if (nlh == NULL)
2820                 goto out_free_skb;
2821
2822         p = nlmsg_data(nlh);
2823         if (c->event == XFRM_MSG_DELPOLICY) {
2824                 struct nlattr *attr;
2825
2826                 id = nlmsg_data(nlh);
2827                 memset(id, 0, sizeof(*id));
2828                 id->dir = dir;
2829                 if (c->data.byid)
2830                         id->index = xp->index;
2831                 else
2832                         memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2833
2834                 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2835                 err = -EMSGSIZE;
2836                 if (attr == NULL)
2837                         goto out_free_skb;
2838
2839                 p = nla_data(attr);
2840         }
2841
2842         copy_to_user_policy(xp, p, dir);
2843         err = copy_to_user_tmpl(xp, skb);
2844         if (!err)
2845                 err = copy_to_user_policy_type(xp->type, skb);
2846         if (!err)
2847                 err = xfrm_mark_put(skb, &xp->mark);
2848         if (err)
2849                 goto out_free_skb;
2850
2851         nlmsg_end(skb, nlh);
2852
2853         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2854
2855 out_free_skb:
2856         kfree_skb(skb);
2857         return err;
2858 }
2859
2860 static int xfrm_notify_policy_flush(const struct km_event *c)
2861 {
2862         struct net *net = c->net;
2863         struct nlmsghdr *nlh;
2864         struct sk_buff *skb;
2865         int err;
2866
2867         skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2868         if (skb == NULL)
2869                 return -ENOMEM;
2870
2871         nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2872         err = -EMSGSIZE;
2873         if (nlh == NULL)
2874                 goto out_free_skb;
2875         err = copy_to_user_policy_type(c->data.type, skb);
2876         if (err)
2877                 goto out_free_skb;
2878
2879         nlmsg_end(skb, nlh);
2880
2881         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2882
2883 out_free_skb:
2884         kfree_skb(skb);
2885         return err;
2886 }
2887
2888 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2889 {
2890
2891         switch (c->event) {
2892         case XFRM_MSG_NEWPOLICY:
2893         case XFRM_MSG_UPDPOLICY:
2894         case XFRM_MSG_DELPOLICY:
2895                 return xfrm_notify_policy(xp, dir, c);
2896         case XFRM_MSG_FLUSHPOLICY:
2897                 return xfrm_notify_policy_flush(c);
2898         case XFRM_MSG_POLEXPIRE:
2899                 return xfrm_exp_policy_notify(xp, dir, c);
2900         default:
2901                 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2902                        c->event);
2903         }
2904
2905         return 0;
2906
2907 }
2908
2909 static inline size_t xfrm_report_msgsize(void)
2910 {
2911         return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2912 }
2913
2914 static int build_report(struct sk_buff *skb, u8 proto,
2915                         struct xfrm_selector *sel, xfrm_address_t *addr)
2916 {
2917         struct xfrm_user_report *ur;
2918         struct nlmsghdr *nlh;
2919
2920         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2921         if (nlh == NULL)
2922                 return -EMSGSIZE;
2923
2924         ur = nlmsg_data(nlh);
2925         ur->proto = proto;
2926         memcpy(&ur->sel, sel, sizeof(ur->sel));
2927
2928         if (addr) {
2929                 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2930                 if (err) {
2931                         nlmsg_cancel(skb, nlh);
2932                         return err;
2933                 }
2934         }
2935         return nlmsg_end(skb, nlh);
2936 }
2937
2938 static int xfrm_send_report(struct net *net, u8 proto,
2939                             struct xfrm_selector *sel, xfrm_address_t *addr)
2940 {
2941         struct sk_buff *skb;
2942
2943         skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2944         if (skb == NULL)
2945                 return -ENOMEM;
2946
2947         if (build_report(skb, proto, sel, addr) < 0)
2948                 BUG();
2949
2950         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2951 }
2952
2953 static inline size_t xfrm_mapping_msgsize(void)
2954 {
2955         return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2956 }
2957
2958 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2959                          xfrm_address_t *new_saddr, __be16 new_sport)
2960 {
2961         struct xfrm_user_mapping *um;
2962         struct nlmsghdr *nlh;
2963
2964         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2965         if (nlh == NULL)
2966                 return -EMSGSIZE;
2967
2968         um = nlmsg_data(nlh);
2969
2970         memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2971         um->id.spi = x->id.spi;
2972         um->id.family = x->props.family;
2973         um->id.proto = x->id.proto;
2974         memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2975         memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2976         um->new_sport = new_sport;
2977         um->old_sport = x->encap->encap_sport;
2978         um->reqid = x->props.reqid;
2979
2980         return nlmsg_end(skb, nlh);
2981 }
2982
2983 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2984                              __be16 sport)
2985 {
2986         struct net *net = xs_net(x);
2987         struct sk_buff *skb;
2988
2989         if (x->id.proto != IPPROTO_ESP)
2990                 return -EINVAL;
2991
2992         if (!x->encap)
2993                 return -EINVAL;
2994
2995         skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2996         if (skb == NULL)
2997                 return -ENOMEM;
2998
2999         if (build_mapping(skb, x, ipaddr, sport) < 0)
3000                 BUG();
3001
3002         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
3003 }
3004
3005 static struct xfrm_mgr netlink_mgr = {
3006         .id             = "netlink",
3007         .notify         = xfrm_send_state_notify,
3008         .acquire        = xfrm_send_acquire,
3009         .compile_policy = xfrm_compile_policy,
3010         .notify_policy  = xfrm_send_policy_notify,
3011         .report         = xfrm_send_report,
3012         .migrate        = xfrm_send_migrate,
3013         .new_mapping    = xfrm_send_mapping,
3014 };
3015
3016 static int __net_init xfrm_user_net_init(struct net *net)
3017 {
3018         struct sock *nlsk;
3019         struct netlink_kernel_cfg cfg = {
3020                 .groups = XFRMNLGRP_MAX,
3021                 .input  = xfrm_netlink_rcv,
3022         };
3023
3024         nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3025         if (nlsk == NULL)
3026                 return -ENOMEM;
3027         net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3028         rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3029         return 0;
3030 }
3031
3032 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3033 {
3034         struct net *net;
3035         list_for_each_entry(net, net_exit_list, exit_list)
3036                 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3037         synchronize_net();
3038         list_for_each_entry(net, net_exit_list, exit_list)
3039                 netlink_kernel_release(net->xfrm.nlsk_stash);
3040 }
3041
3042 static struct pernet_operations xfrm_user_net_ops = {
3043         .init       = xfrm_user_net_init,
3044         .exit_batch = xfrm_user_net_exit,
3045 };
3046
3047 static int __init xfrm_user_init(void)
3048 {
3049         int rv;
3050
3051         printk(KERN_INFO "Initializing XFRM netlink socket\n");
3052
3053         rv = register_pernet_subsys(&xfrm_user_net_ops);
3054         if (rv < 0)
3055                 return rv;
3056         rv = xfrm_register_km(&netlink_mgr);
3057         if (rv < 0)
3058                 unregister_pernet_subsys(&xfrm_user_net_ops);
3059         return rv;
3060 }
3061
3062 static void __exit xfrm_user_exit(void)
3063 {
3064         xfrm_unregister_km(&netlink_mgr);
3065         unregister_pernet_subsys(&xfrm_user_net_ops);
3066 }
3067
3068 module_init(xfrm_user_init);
3069 module_exit(xfrm_user_exit);
3070 MODULE_LICENSE("GPL");
3071 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3072