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