Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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 <asm/uaccess.h>
 30 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 31 #include <linux/in6.h>
 32 #endif
 33 
 34 static inline int aead_len(struct xfrm_algo_aead *alg)
 35 {
 36         return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
 37 }
 38 
 39 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
 40 {
 41         struct nlattr *rt = attrs[type];
 42         struct xfrm_algo *algp;
 43 
 44         if (!rt)
 45                 return 0;
 46 
 47         algp = nla_data(rt);
 48         if (nla_len(rt) < xfrm_alg_len(algp))
 49                 return -EINVAL;
 50 
 51         switch (type) {
 52         case XFRMA_ALG_AUTH:
 53                 if (!algp->alg_key_len &&
 54                     strcmp(algp->alg_name, "digest_null") != 0)
 55                         return -EINVAL;
 56                 break;
 57 
 58         case XFRMA_ALG_CRYPT:
 59                 if (!algp->alg_key_len &&
 60                     strcmp(algp->alg_name, "cipher_null") != 0)
 61                         return -EINVAL;
 62                 break;
 63 
 64         case XFRMA_ALG_COMP:
 65                 /* Zero length keys are legal.  */
 66                 break;
 67 
 68         default:
 69                 return -EINVAL;
 70         }
 71 
 72         algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
 73         return 0;
 74 }
 75 
 76 static int verify_aead(struct nlattr **attrs)
 77 {
 78         struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
 79         struct xfrm_algo_aead *algp;
 80 
 81         if (!rt)
 82                 return 0;
 83 
 84         algp = nla_data(rt);
 85         if (nla_len(rt) < aead_len(algp))
 86                 return -EINVAL;
 87 
 88         algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
 89         return 0;
 90 }
 91 
 92 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
 93                            xfrm_address_t **addrp)
 94 {
 95         struct nlattr *rt = attrs[type];
 96 
 97         if (rt && addrp)
 98                 *addrp = nla_data(rt);
 99 }
100 
101 static inline int verify_sec_ctx_len(struct nlattr **attrs)
102 {
103         struct nlattr *rt = attrs[XFRMA_SEC_CTX];
104         struct xfrm_user_sec_ctx *uctx;
105 
106         if (!rt)
107                 return 0;
108 
109         uctx = nla_data(rt);
110         if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
111                 return -EINVAL;
112 
113         return 0;
114 }
115 
116 
117 static int verify_newsa_info(struct xfrm_usersa_info *p,
118                              struct nlattr **attrs)
119 {
120         int err;
121 
122         err = -EINVAL;
123         switch (p->family) {
124         case AF_INET:
125                 break;
126 
127         case AF_INET6:
128 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
129                 break;
130 #else
131                 err = -EAFNOSUPPORT;
132                 goto out;
133 #endif
134 
135         default:
136                 goto out;
137         }
138 
139         err = -EINVAL;
140         switch (p->id.proto) {
141         case IPPROTO_AH:
142                 if (!attrs[XFRMA_ALG_AUTH]      ||
143                     attrs[XFRMA_ALG_AEAD]       ||
144                     attrs[XFRMA_ALG_CRYPT]      ||
145                     attrs[XFRMA_ALG_COMP])
146                         goto out;
147                 break;
148 
149         case IPPROTO_ESP:
150                 if (attrs[XFRMA_ALG_COMP])
151                         goto out;
152                 if (!attrs[XFRMA_ALG_AUTH] &&
153                     !attrs[XFRMA_ALG_CRYPT] &&
154                     !attrs[XFRMA_ALG_AEAD])
155                         goto out;
156                 if ((attrs[XFRMA_ALG_AUTH] ||
157                      attrs[XFRMA_ALG_CRYPT]) &&
158                     attrs[XFRMA_ALG_AEAD])
159                         goto out;
160                 break;
161 
162         case IPPROTO_COMP:
163                 if (!attrs[XFRMA_ALG_COMP]      ||
164                     attrs[XFRMA_ALG_AEAD]       ||
165                     attrs[XFRMA_ALG_AUTH]       ||
166                     attrs[XFRMA_ALG_CRYPT])
167                         goto out;
168                 break;
169 
170 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171         case IPPROTO_DSTOPTS:
172         case IPPROTO_ROUTING:
173                 if (attrs[XFRMA_ALG_COMP]       ||
174                     attrs[XFRMA_ALG_AUTH]       ||
175                     attrs[XFRMA_ALG_AEAD]       ||
176                     attrs[XFRMA_ALG_CRYPT]      ||
177                     attrs[XFRMA_ENCAP]          ||
178                     attrs[XFRMA_SEC_CTX]        ||
179                     !attrs[XFRMA_COADDR])
180                         goto out;
181                 break;
182 #endif
183 
184         default:
185                 goto out;
186         }
187 
188         if ((err = verify_aead(attrs)))
189                 goto out;
190         if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
191                 goto out;
192         if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
193                 goto out;
194         if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
195                 goto out;
196         if ((err = verify_sec_ctx_len(attrs)))
197                 goto out;
198 
199         err = -EINVAL;
200         switch (p->mode) {
201         case XFRM_MODE_TRANSPORT:
202         case XFRM_MODE_TUNNEL:
203         case XFRM_MODE_ROUTEOPTIMIZATION:
204         case XFRM_MODE_BEET:
205                 break;
206 
207         default:
208                 goto out;
209         }
210 
211         err = 0;
212 
213 out:
214         return err;
215 }
216 
217 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
218                            struct xfrm_algo_desc *(*get_byname)(char *, int),
219                            struct nlattr *rta)
220 {
221         struct xfrm_algo *p, *ualg;
222         struct xfrm_algo_desc *algo;
223 
224         if (!rta)
225                 return 0;
226 
227         ualg = nla_data(rta);
228 
229         algo = get_byname(ualg->alg_name, 1);
230         if (!algo)
231                 return -ENOSYS;
232         *props = algo->desc.sadb_alg_id;
233 
234         p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
235         if (!p)
236                 return -ENOMEM;
237 
238         strcpy(p->alg_name, algo->name);
239         *algpp = p;
240         return 0;
241 }
242 
243 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
244                        struct nlattr *rta)
245 {
246         struct xfrm_algo_aead *p, *ualg;
247         struct xfrm_algo_desc *algo;
248 
249         if (!rta)
250                 return 0;
251 
252         ualg = nla_data(rta);
253 
254         algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
255         if (!algo)
256                 return -ENOSYS;
257         *props = algo->desc.sadb_alg_id;
258 
259         p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
260         if (!p)
261                 return -ENOMEM;
262 
263         strcpy(p->alg_name, algo->name);
264         *algpp = p;
265         return 0;
266 }
267 
268 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
269 {
270         int len = 0;
271 
272         if (xfrm_ctx) {
273                 len += sizeof(struct xfrm_user_sec_ctx);
274                 len += xfrm_ctx->ctx_len;
275         }
276         return len;
277 }
278 
279 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
280 {
281         memcpy(&x->id, &p->id, sizeof(x->id));
282         memcpy(&x->sel, &p->sel, sizeof(x->sel));
283         memcpy(&x->lft, &p->lft, sizeof(x->lft));
284         x->props.mode = p->mode;
285         x->props.replay_window = p->replay_window;
286         x->props.reqid = p->reqid;
287         x->props.family = p->family;
288         memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
289         x->props.flags = p->flags;
290 
291         if (!x->sel.family)
292                 x->sel.family = p->family;
293 
294 }
295 
296 /*
297  * someday when pfkey also has support, we could have the code
298  * somehow made shareable and move it to xfrm_state.c - JHS
299  *
300 */
301 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
302 {
303         struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
304         struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
305         struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
306         struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
307 
308         if (rp) {
309                 struct xfrm_replay_state *replay;
310                 replay = nla_data(rp);
311                 memcpy(&x->replay, replay, sizeof(*replay));
312                 memcpy(&x->preplay, replay, sizeof(*replay));
313         }
314 
315         if (lt) {
316                 struct xfrm_lifetime_cur *ltime;
317                 ltime = nla_data(lt);
318                 x->curlft.bytes = ltime->bytes;
319                 x->curlft.packets = ltime->packets;
320                 x->curlft.add_time = ltime->add_time;
321                 x->curlft.use_time = ltime->use_time;
322         }
323 
324         if (et)
325                 x->replay_maxage = nla_get_u32(et);
326 
327         if (rt)
328                 x->replay_maxdiff = nla_get_u32(rt);
329 }
330 
331 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
332                                                struct nlattr **attrs,
333                                                int *errp)
334 {
335         struct xfrm_state *x = xfrm_state_alloc();
336         int err = -ENOMEM;
337 
338         if (!x)
339                 goto error_no_put;
340 
341         copy_from_user_state(x, p);
342 
343         if ((err = attach_aead(&x->aead, &x->props.ealgo,
344                                attrs[XFRMA_ALG_AEAD])))
345                 goto error;
346         if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
347                                    xfrm_aalg_get_byname,
348                                    attrs[XFRMA_ALG_AUTH])))
349                 goto error;
350         if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
351                                    xfrm_ealg_get_byname,
352                                    attrs[XFRMA_ALG_CRYPT])))
353                 goto error;
354         if ((err = attach_one_algo(&x->calg, &x->props.calgo,
355                                    xfrm_calg_get_byname,
356                                    attrs[XFRMA_ALG_COMP])))
357                 goto error;
358 
359         if (attrs[XFRMA_ENCAP]) {
360                 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
361                                    sizeof(*x->encap), GFP_KERNEL);
362                 if (x->encap == NULL)
363                         goto error;
364         }
365 
366         if (attrs[XFRMA_COADDR]) {
367                 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
368                                     sizeof(*x->coaddr), GFP_KERNEL);
369                 if (x->coaddr == NULL)
370                         goto error;
371         }
372 
373         err = xfrm_init_state(x);
374         if (err)
375                 goto error;
376 
377         if (attrs[XFRMA_SEC_CTX] &&
378             security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
379                 goto error;
380 
381         x->km.seq = p->seq;
382         x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
383         /* sysctl_xfrm_aevent_etime is in 100ms units */
384         x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
385         x->preplay.bitmap = 0;
386         x->preplay.seq = x->replay.seq+x->replay_maxdiff;
387         x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
388 
389         /* override default values from above */
390 
391         xfrm_update_ae_params(x, attrs);
392 
393         return x;
394 
395 error:
396         x->km.state = XFRM_STATE_DEAD;
397         xfrm_state_put(x);
398 error_no_put:
399         *errp = err;
400         return NULL;
401 }
402 
403 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
404                 struct nlattr **attrs)
405 {
406         struct xfrm_usersa_info *p = nlmsg_data(nlh);
407         struct xfrm_state *x;
408         int err;
409         struct km_event c;
410 
411         err = verify_newsa_info(p, attrs);
412         if (err)
413                 return err;
414 
415         x = xfrm_state_construct(p, attrs, &err);
416         if (!x)
417                 return err;
418 
419         xfrm_state_hold(x);
420         if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
421                 err = xfrm_state_add(x);
422         else
423                 err = xfrm_state_update(x);
424 
425         xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
426                              NETLINK_CB(skb).sid);
427 
428         if (err < 0) {
429                 x->km.state = XFRM_STATE_DEAD;
430                 __xfrm_state_put(x);
431                 goto out;
432         }
433 
434         c.seq = nlh->nlmsg_seq;
435         c.pid = nlh->nlmsg_pid;
436         c.event = nlh->nlmsg_type;
437 
438         km_state_notify(x, &c);
439 out:
440         xfrm_state_put(x);
441         return err;
442 }
443 
444 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
445                                                  struct nlattr **attrs,
446                                                  int *errp)
447 {
448         struct xfrm_state *x = NULL;
449         int err;
450 
451         if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
452                 err = -ESRCH;
453                 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
454         } else {
455                 xfrm_address_t *saddr = NULL;
456 
457                 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
458                 if (!saddr) {
459                         err = -EINVAL;
460                         goto out;
461                 }
462 
463                 err = -ESRCH;
464                 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
465                                              p->family);
466         }
467 
468  out:
469         if (!x && errp)
470                 *errp = err;
471         return x;
472 }
473 
474 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
475                 struct nlattr **attrs)
476 {
477         struct xfrm_state *x;
478         int err = -ESRCH;
479         struct km_event c;
480         struct xfrm_usersa_id *p = nlmsg_data(nlh);
481 
482         x = xfrm_user_state_lookup(p, attrs, &err);
483         if (x == NULL)
484                 return err;
485 
486         if ((err = security_xfrm_state_delete(x)) != 0)
487                 goto out;
488 
489         if (xfrm_state_kern(x)) {
490                 err = -EPERM;
491                 goto out;
492         }
493 
494         err = xfrm_state_delete(x);
495 
496         if (err < 0)
497                 goto out;
498 
499         c.seq = nlh->nlmsg_seq;
500         c.pid = nlh->nlmsg_pid;
501         c.event = nlh->nlmsg_type;
502         km_state_notify(x, &c);
503 
504 out:
505         xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
506                                 NETLINK_CB(skb).sid);
507         xfrm_state_put(x);
508         return err;
509 }
510 
511 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
512 {
513         memcpy(&p->id, &x->id, sizeof(p->id));
514         memcpy(&p->sel, &x->sel, sizeof(p->sel));
515         memcpy(&p->lft, &x->lft, sizeof(p->lft));
516         memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
517         memcpy(&p->stats, &x->stats, sizeof(p->stats));
518         memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
519         p->mode = x->props.mode;
520         p->replay_window = x->props.replay_window;
521         p->reqid = x->props.reqid;
522         p->family = x->props.family;
523         p->flags = x->props.flags;
524         p->seq = x->km.seq;
525 }
526 
527 struct xfrm_dump_info {
528         struct sk_buff *in_skb;
529         struct sk_buff *out_skb;
530         u32 nlmsg_seq;
531         u16 nlmsg_flags;
532         int start_idx;
533         int this_idx;
534 };
535 
536 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
537 {
538         struct xfrm_user_sec_ctx *uctx;
539         struct nlattr *attr;
540         int ctx_size = sizeof(*uctx) + s->ctx_len;
541 
542         attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
543         if (attr == NULL)
544                 return -EMSGSIZE;
545 
546         uctx = nla_data(attr);
547         uctx->exttype = XFRMA_SEC_CTX;
548         uctx->len = ctx_size;
549         uctx->ctx_doi = s->ctx_doi;
550         uctx->ctx_alg = s->ctx_alg;
551         uctx->ctx_len = s->ctx_len;
552         memcpy(uctx + 1, s->ctx_str, s->ctx_len);
553 
554         return 0;
555 }
556 
557 /* Don't change this without updating xfrm_sa_len! */
558 static int copy_to_user_state_extra(struct xfrm_state *x,
559                                     struct xfrm_usersa_info *p,
560                                     struct sk_buff *skb)
561 {
562         copy_to_user_state(x, p);
563 
564         if (x->coaddr)
565                 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
566 
567         if (x->lastused)
568                 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
569 
570         if (x->aead)
571                 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
572         if (x->aalg)
573                 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
574         if (x->ealg)
575                 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
576         if (x->calg)
577                 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
578 
579         if (x->encap)
580                 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
581 
582         if (x->security && copy_sec_ctx(x->security, skb) < 0)
583                 goto nla_put_failure;
584 
585         return 0;
586 
587 nla_put_failure:
588         return -EMSGSIZE;
589 }
590 
591 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
592 {
593         struct xfrm_dump_info *sp = ptr;
594         struct sk_buff *in_skb = sp->in_skb;
595         struct sk_buff *skb = sp->out_skb;
596         struct xfrm_usersa_info *p;
597         struct nlmsghdr *nlh;
598         int err;
599 
600         if (sp->this_idx < sp->start_idx)
601                 goto out;
602 
603         nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
604                         XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
605         if (nlh == NULL)
606                 return -EMSGSIZE;
607 
608         p = nlmsg_data(nlh);
609 
610         err = copy_to_user_state_extra(x, p, skb);
611         if (err)
612                 goto nla_put_failure;
613 
614         nlmsg_end(skb, nlh);
615 out:
616         sp->this_idx++;
617         return 0;
618 
619 nla_put_failure:
620         nlmsg_cancel(skb, nlh);
621         return err;
622 }
623 
624 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
625 {
626         struct xfrm_dump_info info;
627 
628         info.in_skb = cb->skb;
629         info.out_skb = skb;
630         info.nlmsg_seq = cb->nlh->nlmsg_seq;
631         info.nlmsg_flags = NLM_F_MULTI;
632         info.this_idx = 0;
633         info.start_idx = cb->args[0];
634         (void) xfrm_state_walk(0, dump_one_state, &info);
635         cb->args[0] = info.this_idx;
636 
637         return skb->len;
638 }
639 
640 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
641                                           struct xfrm_state *x, u32 seq)
642 {
643         struct xfrm_dump_info info;
644         struct sk_buff *skb;
645 
646         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
647         if (!skb)
648                 return ERR_PTR(-ENOMEM);
649 
650         info.in_skb = in_skb;
651         info.out_skb = skb;
652         info.nlmsg_seq = seq;
653         info.nlmsg_flags = 0;
654         info.this_idx = info.start_idx = 0;
655 
656         if (dump_one_state(x, 0, &info)) {
657                 kfree_skb(skb);
658                 return NULL;
659         }
660 
661         return skb;
662 }
663 
664 static inline size_t xfrm_spdinfo_msgsize(void)
665 {
666         return NLMSG_ALIGN(4)
667                + nla_total_size(sizeof(struct xfrmu_spdinfo))
668                + nla_total_size(sizeof(struct xfrmu_spdhinfo));
669 }
670 
671 static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
672 {
673         struct xfrmk_spdinfo si;
674         struct xfrmu_spdinfo spc;
675         struct xfrmu_spdhinfo sph;
676         struct nlmsghdr *nlh;
677         u32 *f;
678 
679         nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
680         if (nlh == NULL) /* shouldnt really happen ... */
681                 return -EMSGSIZE;
682 
683         f = nlmsg_data(nlh);
684         *f = flags;
685         xfrm_spd_getinfo(&si);
686         spc.incnt = si.incnt;
687         spc.outcnt = si.outcnt;
688         spc.fwdcnt = si.fwdcnt;
689         spc.inscnt = si.inscnt;
690         spc.outscnt = si.outscnt;
691         spc.fwdscnt = si.fwdscnt;
692         sph.spdhcnt = si.spdhcnt;
693         sph.spdhmcnt = si.spdhmcnt;
694 
695         NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
696         NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
697 
698         return nlmsg_end(skb, nlh);
699 
700 nla_put_failure:
701         nlmsg_cancel(skb, nlh);
702         return -EMSGSIZE;
703 }
704 
705 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
706                 struct nlattr **attrs)
707 {
708         struct sk_buff *r_skb;
709         u32 *flags = nlmsg_data(nlh);
710         u32 spid = NETLINK_CB(skb).pid;
711         u32 seq = nlh->nlmsg_seq;
712 
713         r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
714         if (r_skb == NULL)
715                 return -ENOMEM;
716 
717         if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
718                 BUG();
719 
720         return nlmsg_unicast(xfrm_nl, r_skb, spid);
721 }
722 
723 static inline size_t xfrm_sadinfo_msgsize(void)
724 {
725         return NLMSG_ALIGN(4)
726                + nla_total_size(sizeof(struct xfrmu_sadhinfo))
727                + nla_total_size(4); /* XFRMA_SAD_CNT */
728 }
729 
730 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
731 {
732         struct xfrmk_sadinfo si;
733         struct xfrmu_sadhinfo sh;
734         struct nlmsghdr *nlh;
735         u32 *f;
736 
737         nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
738         if (nlh == NULL) /* shouldnt really happen ... */
739                 return -EMSGSIZE;
740 
741         f = nlmsg_data(nlh);
742         *f = flags;
743         xfrm_sad_getinfo(&si);
744 
745         sh.sadhmcnt = si.sadhmcnt;
746         sh.sadhcnt = si.sadhcnt;
747 
748         NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
749         NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
750 
751         return nlmsg_end(skb, nlh);
752 
753 nla_put_failure:
754         nlmsg_cancel(skb, nlh);
755         return -EMSGSIZE;
756 }
757 
758 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
759                 struct nlattr **attrs)
760 {
761         struct sk_buff *r_skb;
762         u32 *flags = nlmsg_data(nlh);
763         u32 spid = NETLINK_CB(skb).pid;
764         u32 seq = nlh->nlmsg_seq;
765 
766         r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
767         if (r_skb == NULL)
768                 return -ENOMEM;
769 
770         if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
771                 BUG();
772 
773         return nlmsg_unicast(xfrm_nl, r_skb, spid);
774 }
775 
776 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
777                 struct nlattr **attrs)
778 {
779         struct xfrm_usersa_id *p = nlmsg_data(nlh);
780         struct xfrm_state *x;
781         struct sk_buff *resp_skb;
782         int err = -ESRCH;
783 
784         x = xfrm_user_state_lookup(p, attrs, &err);
785         if (x == NULL)
786                 goto out_noput;
787 
788         resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
789         if (IS_ERR(resp_skb)) {
790                 err = PTR_ERR(resp_skb);
791         } else {
792                 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
793         }
794         xfrm_state_put(x);
795 out_noput:
796         return err;
797 }
798 
799 static int verify_userspi_info(struct xfrm_userspi_info *p)
800 {
801         switch (p->info.id.proto) {
802         case IPPROTO_AH:
803         case IPPROTO_ESP:
804                 break;
805 
806         case IPPROTO_COMP:
807                 /* IPCOMP spi is 16-bits. */
808                 if (p->max >= 0x10000)
809                         return -EINVAL;
810                 break;
811 
812         default:
813                 return -EINVAL;
814         }
815 
816         if (p->min > p->max)
817                 return -EINVAL;
818 
819         return 0;
820 }
821 
822 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
823                 struct nlattr **attrs)
824 {
825         struct xfrm_state *x;
826         struct xfrm_userspi_info *p;
827         struct sk_buff *resp_skb;
828         xfrm_address_t *daddr;
829         int family;
830         int err;
831 
832         p = nlmsg_data(nlh);
833         err = verify_userspi_info(p);
834         if (err)
835                 goto out_noput;
836 
837         family = p->info.family;
838         daddr = &p->info.id.daddr;
839 
840         x = NULL;
841         if (p->info.seq) {
842                 x = xfrm_find_acq_byseq(p->info.seq);
843                 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
844                         xfrm_state_put(x);
845                         x = NULL;
846                 }
847         }
848 
849         if (!x)
850                 x = xfrm_find_acq(p->info.mode, p->info.reqid,
851                                   p->info.id.proto, daddr,
852                                   &p->info.saddr, 1,
853                                   family);
854         err = -ENOENT;
855         if (x == NULL)
856                 goto out_noput;
857 
858         err = xfrm_alloc_spi(x, p->min, p->max);
859         if (err)
860                 goto out;
861 
862         resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
863         if (IS_ERR(resp_skb)) {
864                 err = PTR_ERR(resp_skb);
865                 goto out;
866         }
867 
868         err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
869 
870 out:
871         xfrm_state_put(x);
872 out_noput:
873         return err;
874 }
875 
876 static int verify_policy_dir(u8 dir)
877 {
878         switch (dir) {
879         case XFRM_POLICY_IN:
880         case XFRM_POLICY_OUT:
881         case XFRM_POLICY_FWD:
882                 break;
883 
884         default:
885                 return -EINVAL;
886         }
887 
888         return 0;
889 }
890 
891 static int verify_policy_type(u8 type)
892 {
893         switch (type) {
894         case XFRM_POLICY_TYPE_MAIN:
895 #ifdef CONFIG_XFRM_SUB_POLICY
896         case XFRM_POLICY_TYPE_SUB:
897 #endif
898                 break;
899 
900         default:
901                 return -EINVAL;
902         }
903 
904         return 0;
905 }
906 
907 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
908 {
909         switch (p->share) {
910         case XFRM_SHARE_ANY:
911         case XFRM_SHARE_SESSION:
912         case XFRM_SHARE_USER:
913         case XFRM_SHARE_UNIQUE:
914                 break;
915 
916         default:
917                 return -EINVAL;
918         }
919 
920         switch (p->action) {
921         case XFRM_POLICY_ALLOW:
922         case XFRM_POLICY_BLOCK:
923                 break;
924 
925         default:
926                 return -EINVAL;
927         }
928 
929         switch (p->sel.family) {
930         case AF_INET:
931                 break;
932 
933         case AF_INET6:
934 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
935                 break;
936 #else
937                 return  -EAFNOSUPPORT;
938 #endif
939 
940         default:
941                 return -EINVAL;
942         }
943 
944         return verify_policy_dir(p->dir);
945 }
946 
947 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
948 {
949         struct nlattr *rt = attrs[XFRMA_SEC_CTX];
950         struct xfrm_user_sec_ctx *uctx;
951 
952         if (!rt)
953                 return 0;
954 
955         uctx = nla_data(rt);
956         return security_xfrm_policy_alloc(pol, uctx);
957 }
958 
959 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
960                            int nr)
961 {
962         int i;
963 
964         xp->xfrm_nr = nr;
965         for (i = 0; i < nr; i++, ut++) {
966                 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
967 
968                 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
969                 memcpy(&t->saddr, &ut->saddr,
970                        sizeof(xfrm_address_t));
971                 t->reqid = ut->reqid;
972                 t->mode = ut->mode;
973                 t->share = ut->share;
974                 t->optional = ut->optional;
975                 t->aalgos = ut->aalgos;
976                 t->ealgos = ut->ealgos;
977                 t->calgos = ut->calgos;
978                 /* If all masks are ~0, then we allow all algorithms. */
979                 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
980                 t->encap_family = ut->family;
981         }
982 }
983 
984 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
985 {
986         int i;
987 
988         if (nr > XFRM_MAX_DEPTH)
989                 return -EINVAL;
990 
991         for (i = 0; i < nr; i++) {
992                 /* We never validated the ut->family value, so many
993                  * applications simply leave it at zero.  The check was
994                  * never made and ut->family was ignored because all
995                  * templates could be assumed to have the same family as
996                  * the policy itself.  Now that we will have ipv4-in-ipv6
997                  * and ipv6-in-ipv4 tunnels, this is no longer true.
998                  */
999                 if (!ut[i].family)
1000                         ut[i].family = family;
1001 
1002                 switch (ut[i].family) {
1003                 case AF_INET:
1004                         break;
1005 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1006                 case AF_INET6:
1007                         break;
1008 #endif
1009                 default:
1010                         return -EINVAL;
1011                 }
1012         }
1013 
1014         return 0;
1015 }
1016 
1017 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1018 {
1019         struct nlattr *rt = attrs[XFRMA_TMPL];
1020 
1021         if (!rt) {
1022                 pol->xfrm_nr = 0;
1023         } else {
1024                 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1025                 int nr = nla_len(rt) / sizeof(*utmpl);
1026                 int err;
1027 
1028                 err = validate_tmpl(nr, utmpl, pol->family);
1029                 if (err)
1030                         return err;
1031 
1032                 copy_templates(pol, utmpl, nr);
1033         }
1034         return 0;
1035 }
1036 
1037 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1038 {
1039         struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1040         struct xfrm_userpolicy_type *upt;
1041         u8 type = XFRM_POLICY_TYPE_MAIN;
1042         int err;
1043 
1044         if (rt) {
1045                 upt = nla_data(rt);
1046                 type = upt->type;
1047         }
1048 
1049         err = verify_policy_type(type);
1050         if (err)
1051                 return err;
1052 
1053         *tp = type;
1054         return 0;
1055 }
1056 
1057 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1058 {
1059         xp->priority = p->priority;
1060         xp->index = p->index;
1061         memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1062         memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1063         xp->action = p->action;
1064         xp->flags = p->flags;
1065         xp->family = p->sel.family;
1066         /* XXX xp->share = p->share; */
1067 }
1068 
1069 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1070 {
1071         memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1072         memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1073         memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1074         p->priority = xp->priority;
1075         p->index = xp->index;
1076         p->sel.family = xp->family;
1077         p->dir = dir;
1078         p->action = xp->action;
1079         p->flags = xp->flags;
1080         p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1081 }
1082 
1083 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1084 {
1085         struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1086         int err;
1087 
1088         if (!xp) {
1089                 *errp = -ENOMEM;
1090                 return NULL;
1091         }
1092 
1093         copy_from_user_policy(xp, p);
1094 
1095         err = copy_from_user_policy_type(&xp->type, attrs);
1096         if (err)
1097                 goto error;
1098 
1099         if (!(err = copy_from_user_tmpl(xp, attrs)))
1100                 err = copy_from_user_sec_ctx(xp, attrs);
1101         if (err)
1102                 goto error;
1103 
1104         return xp;
1105  error:
1106         *errp = err;
1107         xp->dead = 1;
1108         xfrm_policy_destroy(xp);
1109         return NULL;
1110 }
1111 
1112 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1113                 struct nlattr **attrs)
1114 {
1115         struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1116         struct xfrm_policy *xp;
1117         struct km_event c;
1118         int err;
1119         int excl;
1120 
1121         err = verify_newpolicy_info(p);
1122         if (err)
1123                 return err;
1124         err = verify_sec_ctx_len(attrs);
1125         if (err)
1126                 return err;
1127 
1128         xp = xfrm_policy_construct(p, attrs, &err);
1129         if (!xp)
1130                 return err;
1131 
1132         /* shouldnt excl be based on nlh flags??
1133          * Aha! this is anti-netlink really i.e  more pfkey derived
1134          * in netlink excl is a flag and you wouldnt need
1135          * a type XFRM_MSG_UPDPOLICY - JHS */
1136         excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1137         err = xfrm_policy_insert(p->dir, xp, excl);
1138         xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1139                               NETLINK_CB(skb).sid);
1140 
1141         if (err) {
1142                 security_xfrm_policy_free(xp);
1143                 kfree(xp);
1144                 return err;
1145         }
1146 
1147         c.event = nlh->nlmsg_type;
1148         c.seq = nlh->nlmsg_seq;
1149         c.pid = nlh->nlmsg_pid;
1150         km_policy_notify(xp, p->dir, &c);
1151 
1152         xfrm_pol_put(xp);
1153 
1154         return 0;
1155 }
1156 
1157 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1158 {
1159         struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1160         int i;
1161 
1162         if (xp->xfrm_nr == 0)
1163                 return 0;
1164 
1165         for (i = 0; i < xp->xfrm_nr; i++) {
1166                 struct xfrm_user_tmpl *up = &vec[i];
1167                 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1168 
1169                 memcpy(&up->id, &kp->id, sizeof(up->id));
1170                 up->family = kp->encap_family;
1171                 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1172                 up->reqid = kp->reqid;
1173                 up->mode = kp->mode;
1174                 up->share = kp->share;
1175                 up->optional = kp->optional;
1176                 up->aalgos = kp->aalgos;
1177                 up->ealgos = kp->ealgos;
1178                 up->calgos = kp->calgos;
1179         }
1180 
1181         return nla_put(skb, XFRMA_TMPL,
1182                        sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1183 }
1184 
1185 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1186 {
1187         if (x->security) {
1188                 return copy_sec_ctx(x->security, skb);
1189         }
1190         return 0;
1191 }
1192 
1193 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1194 {
1195         if (xp->security) {
1196                 return copy_sec_ctx(xp->security, skb);
1197         }
1198         return 0;
1199 }
1200 static inline size_t userpolicy_type_attrsize(void)
1201 {
1202 #ifdef CONFIG_XFRM_SUB_POLICY
1203         return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1204 #else
1205         return 0;
1206 #endif
1207 }
1208 
1209 #ifdef CONFIG_XFRM_SUB_POLICY
1210 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1211 {
1212         struct xfrm_userpolicy_type upt = {
1213                 .type = type,
1214         };
1215 
1216         return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1217 }
1218 
1219 #else
1220 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1221 {
1222         return 0;
1223 }
1224 #endif
1225 
1226 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1227 {
1228         struct xfrm_dump_info *sp = ptr;
1229         struct xfrm_userpolicy_info *p;
1230         struct sk_buff *in_skb = sp->in_skb;
1231         struct sk_buff *skb = sp->out_skb;
1232         struct nlmsghdr *nlh;
1233 
1234         if (sp->this_idx < sp->start_idx)
1235                 goto out;
1236 
1237         nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1238                         XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1239         if (nlh == NULL)
1240                 return -EMSGSIZE;
1241 
1242         p = nlmsg_data(nlh);
1243         copy_to_user_policy(xp, p, dir);
1244         if (copy_to_user_tmpl(xp, skb) < 0)
1245                 goto nlmsg_failure;
1246         if (copy_to_user_sec_ctx(xp, skb))
1247                 goto nlmsg_failure;
1248         if (copy_to_user_policy_type(xp->type, skb) < 0)
1249                 goto nlmsg_failure;
1250 
1251         nlmsg_end(skb, nlh);
1252 out:
1253         sp->this_idx++;
1254         return 0;
1255 
1256 nlmsg_failure:
1257         nlmsg_cancel(skb, nlh);
1258         return -EMSGSIZE;
1259 }
1260 
1261 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1262 {
1263         struct xfrm_dump_info info;
1264 
1265         info.in_skb = cb->skb;
1266         info.out_skb = skb;
1267         info.nlmsg_seq = cb->nlh->nlmsg_seq;
1268         info.nlmsg_flags = NLM_F_MULTI;
1269         info.this_idx = 0;
1270         info.start_idx = cb->args[0];
1271         (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1272 #ifdef CONFIG_XFRM_SUB_POLICY
1273         (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1274 #endif
1275         cb->args[0] = info.this_idx;
1276 
1277         return skb->len;
1278 }
1279 
1280 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1281                                           struct xfrm_policy *xp,
1282                                           int dir, u32 seq)
1283 {
1284         struct xfrm_dump_info info;
1285         struct sk_buff *skb;
1286 
1287         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1288         if (!skb)
1289                 return ERR_PTR(-ENOMEM);
1290 
1291         info.in_skb = in_skb;
1292         info.out_skb = skb;
1293         info.nlmsg_seq = seq;
1294         info.nlmsg_flags = 0;
1295         info.this_idx = info.start_idx = 0;
1296 
1297         if (dump_one_policy(xp, dir, 0, &info) < 0) {
1298                 kfree_skb(skb);
1299                 return NULL;
1300         }
1301 
1302         return skb;
1303 }
1304 
1305 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1306                 struct nlattr **attrs)
1307 {
1308         struct xfrm_policy *xp;
1309         struct xfrm_userpolicy_id *p;
1310         u8 type = XFRM_POLICY_TYPE_MAIN;
1311         int err;
1312         struct km_event c;
1313         int delete;
1314 
1315         p = nlmsg_data(nlh);
1316         delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1317 
1318         err = copy_from_user_policy_type(&type, attrs);
1319         if (err)
1320                 return err;
1321 
1322         err = verify_policy_dir(p->dir);
1323         if (err)
1324                 return err;
1325 
1326         if (p->index)
1327                 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
1328         else {
1329                 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1330                 struct xfrm_policy tmp;
1331 
1332                 err = verify_sec_ctx_len(attrs);
1333                 if (err)
1334                         return err;
1335 
1336                 memset(&tmp, 0, sizeof(struct xfrm_policy));
1337                 if (rt) {
1338                         struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1339 
1340                         if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1341                                 return err;
1342                 }
1343                 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1344                                            delete, &err);
1345                 security_xfrm_policy_free(&tmp);
1346         }
1347         if (xp == NULL)
1348                 return -ENOENT;
1349 
1350         if (!delete) {
1351                 struct sk_buff *resp_skb;
1352 
1353                 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1354                 if (IS_ERR(resp_skb)) {
1355                         err = PTR_ERR(resp_skb);
1356                 } else {
1357                         err = nlmsg_unicast(xfrm_nl, resp_skb,
1358                                             NETLINK_CB(skb).pid);
1359                 }
1360         } else {
1361                 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1362                                          NETLINK_CB(skb).loginuid,
1363                                          NETLINK_CB(skb).sid);
1364 
1365                 if (err != 0)
1366                         goto out;
1367 
1368                 c.data.byid = p->index;
1369                 c.event = nlh->nlmsg_type;
1370                 c.seq = nlh->nlmsg_seq;
1371                 c.pid = nlh->nlmsg_pid;
1372                 km_policy_notify(xp, p->dir, &c);
1373         }
1374 
1375 out:
1376         xfrm_pol_put(xp);
1377         return err;
1378 }
1379 
1380 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1381                 struct nlattr **attrs)
1382 {
1383         struct km_event c;
1384         struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1385         struct xfrm_audit audit_info;
1386         int err;
1387 
1388         audit_info.loginuid = NETLINK_CB(skb).loginuid;
1389         audit_info.secid = NETLINK_CB(skb).sid;
1390         err = xfrm_state_flush(p->proto, &audit_info);
1391         if (err)
1392                 return err;
1393         c.data.proto = p->proto;
1394         c.event = nlh->nlmsg_type;
1395         c.seq = nlh->nlmsg_seq;
1396         c.pid = nlh->nlmsg_pid;
1397         km_state_notify(NULL, &c);
1398 
1399         return 0;
1400 }
1401 
1402 static inline size_t xfrm_aevent_msgsize(void)
1403 {
1404         return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1405                + nla_total_size(sizeof(struct xfrm_replay_state))
1406                + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1407                + nla_total_size(4) /* XFRM_AE_RTHR */
1408                + nla_total_size(4); /* XFRM_AE_ETHR */
1409 }
1410 
1411 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1412 {
1413         struct xfrm_aevent_id *id;
1414         struct nlmsghdr *nlh;
1415 
1416         nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1417         if (nlh == NULL)
1418                 return -EMSGSIZE;
1419 
1420         id = nlmsg_data(nlh);
1421         memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1422         id->sa_id.spi = x->id.spi;
1423         id->sa_id.family = x->props.family;
1424         id->sa_id.proto = x->id.proto;
1425         memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1426         id->reqid = x->props.reqid;
1427         id->flags = c->data.aevent;
1428 
1429         NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1430         NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1431 
1432         if (id->flags & XFRM_AE_RTHR)
1433                 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1434 
1435         if (id->flags & XFRM_AE_ETHR)
1436                 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1437                             x->replay_maxage * 10 / HZ);
1438 
1439         return nlmsg_end(skb, nlh);
1440 
1441 nla_put_failure:
1442         nlmsg_cancel(skb, nlh);
1443         return -EMSGSIZE;
1444 }
1445 
1446 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1447                 struct nlattr **attrs)
1448 {
1449         struct xfrm_state *x;
1450         struct sk_buff *r_skb;
1451         int err;
1452         struct km_event c;
1453         struct xfrm_aevent_id *p = nlmsg_data(nlh);
1454         struct xfrm_usersa_id *id = &p->sa_id;
1455 
1456         r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1457         if (r_skb == NULL)
1458                 return -ENOMEM;
1459 
1460         x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1461         if (x == NULL) {
1462                 kfree_skb(r_skb);
1463                 return -ESRCH;
1464         }
1465 
1466         /*
1467          * XXX: is this lock really needed - none of the other
1468          * gets lock (the concern is things getting updated
1469          * while we are still reading) - jhs
1470         */
1471         spin_lock_bh(&x->lock);
1472         c.data.aevent = p->flags;
1473         c.seq = nlh->nlmsg_seq;
1474         c.pid = nlh->nlmsg_pid;
1475 
1476         if (build_aevent(r_skb, x, &c) < 0)
1477                 BUG();
1478         err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
1479         spin_unlock_bh(&x->lock);
1480         xfrm_state_put(x);
1481         return err;
1482 }
1483 
1484 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1485                 struct nlattr **attrs)
1486 {
1487         struct xfrm_state *x;
1488         struct km_event c;
1489         int err = - EINVAL;
1490         struct xfrm_aevent_id *p = nlmsg_data(nlh);
1491         struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1492         struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1493 
1494         if (!lt && !rp)
1495                 return err;
1496 
1497         /* pedantic mode - thou shalt sayeth replaceth */
1498         if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1499                 return err;
1500 
1501         x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1502         if (x == NULL)
1503                 return -ESRCH;
1504 
1505         if (x->km.state != XFRM_STATE_VALID)
1506                 goto out;
1507 
1508         spin_lock_bh(&x->lock);
1509         xfrm_update_ae_params(x, attrs);
1510         spin_unlock_bh(&x->lock);
1511 
1512         c.event = nlh->nlmsg_type;
1513         c.seq = nlh->nlmsg_seq;
1514         c.pid = nlh->nlmsg_pid;
1515         c.data.aevent = XFRM_AE_CU;
1516         km_state_notify(x, &c);
1517         err = 0;
1518 out:
1519         xfrm_state_put(x);
1520         return err;
1521 }
1522 
1523 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1524                 struct nlattr **attrs)
1525 {
1526         struct km_event c;
1527         u8 type = XFRM_POLICY_TYPE_MAIN;
1528         int err;
1529         struct xfrm_audit audit_info;
1530 
1531         err = copy_from_user_policy_type(&type, attrs);
1532         if (err)
1533                 return err;
1534 
1535         audit_info.loginuid = NETLINK_CB(skb).loginuid;
1536         audit_info.secid = NETLINK_CB(skb).sid;
1537         err = xfrm_policy_flush(type, &audit_info);
1538         if (err)
1539                 return err;
1540         c.data.type = type;
1541         c.event = nlh->nlmsg_type;
1542         c.seq = nlh->nlmsg_seq;
1543         c.pid = nlh->nlmsg_pid;
1544         km_policy_notify(NULL, 0, &c);
1545         return 0;
1546 }
1547 
1548 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1549                 struct nlattr **attrs)
1550 {
1551         struct xfrm_policy *xp;
1552         struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1553         struct xfrm_userpolicy_info *p = &up->pol;
1554         u8 type = XFRM_POLICY_TYPE_MAIN;
1555         int err = -ENOENT;
1556 
1557         err = copy_from_user_policy_type(&type, attrs);
1558         if (err)
1559                 return err;
1560 
1561         if (p->index)
1562                 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
1563         else {
1564                 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1565                 struct xfrm_policy tmp;
1566 
1567                 err = verify_sec_ctx_len(attrs);
1568                 if (err)
1569                         return err;
1570 
1571                 memset(&tmp, 0, sizeof(struct xfrm_policy));
1572                 if (rt) {
1573                         struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1574 
1575                         if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1576                                 return err;
1577                 }
1578                 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1579                                            0, &err);
1580                 security_xfrm_policy_free(&tmp);
1581         }
1582 
1583         if (xp == NULL)
1584                 return -ENOENT;
1585         read_lock(&xp->lock);
1586         if (xp->dead) {
1587                 read_unlock(&xp->lock);
1588                 goto out;
1589         }
1590 
1591         read_unlock(&xp->lock);
1592         err = 0;
1593         if (up->hard) {
1594                 xfrm_policy_delete(xp, p->dir);
1595                 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1596                                          NETLINK_CB(skb).sid);
1597 
1598         } else {
1599                 // reset the timers here?
1600                 printk("Dont know what to do with soft policy expire\n");
1601         }
1602         km_policy_expired(xp, p->dir, up->hard, current->pid);
1603 
1604 out:
1605         xfrm_pol_put(xp);
1606         return err;
1607 }
1608 
1609 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1610                 struct nlattr **attrs)
1611 {
1612         struct xfrm_state *x;
1613         int err;
1614         struct xfrm_user_expire *ue = nlmsg_data(nlh);
1615         struct xfrm_usersa_info *p = &ue->state;
1616 
1617         x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1618 
1619         err = -ENOENT;
1620         if (x == NULL)
1621                 return err;
1622 
1623         spin_lock_bh(&x->lock);
1624         err = -EINVAL;
1625         if (x->km.state != XFRM_STATE_VALID)
1626                 goto out;
1627         km_state_expired(x, ue->hard, current->pid);
1628 
1629         if (ue->hard) {
1630                 __xfrm_state_delete(x);
1631                 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1632                                         NETLINK_CB(skb).sid);
1633         }
1634         err = 0;
1635 out:
1636         spin_unlock_bh(&x->lock);
1637         xfrm_state_put(x);
1638         return err;
1639 }
1640 
1641 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1642                 struct nlattr **attrs)
1643 {
1644         struct xfrm_policy *xp;
1645         struct xfrm_user_tmpl *ut;
1646         int i;
1647         struct nlattr *rt = attrs[XFRMA_TMPL];
1648 
1649         struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1650         struct xfrm_state *x = xfrm_state_alloc();
1651         int err = -ENOMEM;
1652 
1653         if (!x)
1654                 return err;
1655 
1656         err = verify_newpolicy_info(&ua->policy);
1657         if (err) {
1658                 printk("BAD policy passed\n");
1659                 kfree(x);
1660                 return err;
1661         }
1662 
1663         /*   build an XP */
1664         xp = xfrm_policy_construct(&ua->policy, attrs, &err);
1665         if (!xp) {
1666                 kfree(x);
1667                 return err;
1668         }
1669 
1670         memcpy(&x->id, &ua->id, sizeof(ua->id));
1671         memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1672         memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1673 
1674         ut = nla_data(rt);
1675         /* extract the templates and for each call km_key */
1676         for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1677                 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1678                 memcpy(&x->id, &t->id, sizeof(x->id));
1679                 x->props.mode = t->mode;
1680                 x->props.reqid = t->reqid;
1681                 x->props.family = ut->family;
1682                 t->aalgos = ua->aalgos;
1683                 t->ealgos = ua->ealgos;
1684                 t->calgos = ua->calgos;
1685                 err = km_query(x, t, xp);
1686 
1687         }
1688 
1689         kfree(x);
1690         kfree(xp);
1691 
1692         return 0;
1693 }
1694 
1695 #ifdef CONFIG_XFRM_MIGRATE
1696 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1697                                   struct nlattr **attrs, int *num)
1698 {
1699         struct nlattr *rt = attrs[XFRMA_MIGRATE];
1700         struct xfrm_user_migrate *um;
1701         int i, num_migrate;
1702 
1703         um = nla_data(rt);
1704         num_migrate = nla_len(rt) / sizeof(*um);
1705 
1706         if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1707                 return -EINVAL;
1708 
1709         for (i = 0; i < num_migrate; i++, um++, ma++) {
1710                 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1711                 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1712                 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1713                 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1714 
1715                 ma->proto = um->proto;
1716                 ma->mode = um->mode;
1717                 ma->reqid = um->reqid;
1718 
1719                 ma->old_family = um->old_family;
1720                 ma->new_family = um->new_family;
1721         }
1722 
1723         *num = i;
1724         return 0;
1725 }
1726 
1727 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1728                            struct nlattr **attrs)
1729 {
1730         struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1731         struct xfrm_migrate m[XFRM_MAX_DEPTH];
1732         u8 type;
1733         int err;
1734         int n = 0;
1735 
1736         if (attrs[XFRMA_MIGRATE] == NULL)
1737                 return -EINVAL;
1738 
1739         err = copy_from_user_policy_type(&type, attrs);
1740         if (err)
1741                 return err;
1742 
1743         err = copy_from_user_migrate((struct xfrm_migrate *)m,
1744                                      attrs, &n);
1745         if (err)
1746                 return err;
1747 
1748         if (!n)
1749                 return 0;
1750 
1751         xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1752 
1753         return 0;
1754 }
1755 #else
1756 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1757                            struct nlattr **attrs)
1758 {
1759         return -ENOPROTOOPT;
1760 }
1761 #endif
1762 
1763 #ifdef CONFIG_XFRM_MIGRATE
1764 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1765 {
1766         struct xfrm_user_migrate um;
1767 
1768         memset(&um, 0, sizeof(um));
1769         um.proto = m->proto;
1770         um.mode = m->mode;
1771         um.reqid = m->reqid;
1772         um.old_family = m->old_family;
1773         memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1774         memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1775         um.new_family = m->new_family;
1776         memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1777         memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1778 
1779         return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1780 }
1781 
1782 static inline size_t xfrm_migrate_msgsize(int num_migrate)
1783 {
1784         return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1785                + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1786                + userpolicy_type_attrsize();
1787 }
1788 
1789 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1790                          int num_migrate, struct xfrm_selector *sel,
1791                          u8 dir, u8 type)
1792 {
1793         struct xfrm_migrate *mp;
1794         struct xfrm_userpolicy_id *pol_id;
1795         struct nlmsghdr *nlh;
1796         int i;
1797 
1798         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1799         if (nlh == NULL)
1800                 return -EMSGSIZE;
1801 
1802         pol_id = nlmsg_data(nlh);
1803         /* copy data from selector, dir, and type to the pol_id */
1804         memset(pol_id, 0, sizeof(*pol_id));
1805         memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1806         pol_id->dir = dir;
1807 
1808         if (copy_to_user_policy_type(type, skb) < 0)
1809                 goto nlmsg_failure;
1810 
1811         for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1812                 if (copy_to_user_migrate(mp, skb) < 0)
1813                         goto nlmsg_failure;
1814         }
1815 
1816         return nlmsg_end(skb, nlh);
1817 nlmsg_failure:
1818         nlmsg_cancel(skb, nlh);
1819         return -EMSGSIZE;
1820 }
1821 
1822 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1823                              struct xfrm_migrate *m, int num_migrate)
1824 {
1825         struct sk_buff *skb;
1826 
1827         skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
1828         if (skb == NULL)
1829                 return -ENOMEM;
1830 
1831         /* build migrate */
1832         if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1833                 BUG();
1834 
1835         return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
1836 }
1837 #else
1838 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1839                              struct xfrm_migrate *m, int num_migrate)
1840 {
1841         return -ENOPROTOOPT;
1842 }
1843 #endif
1844 
1845 #define XMSGSIZE(type) sizeof(struct type)
1846 
1847 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1848         [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1849         [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1850         [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1851         [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1852         [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1853         [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1854         [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1855         [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1856         [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1857         [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1858         [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1859         [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1860         [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1861         [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
1862         [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1863         [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1864         [XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1865         [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1866         [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
1867         [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
1868 };
1869 
1870 #undef XMSGSIZE
1871 
1872 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1873         [XFRMA_ALG_AEAD]        = { .len = sizeof(struct xfrm_algo_aead) },
1874         [XFRMA_ALG_AUTH]        = { .len = sizeof(struct xfrm_algo) },
1875         [XFRMA_ALG_CRYPT]       = { .len = sizeof(struct xfrm_algo) },
1876         [XFRMA_ALG_COMP]        = { .len = sizeof(struct xfrm_algo) },
1877         [XFRMA_ENCAP]           = { .len = sizeof(struct xfrm_encap_tmpl) },
1878         [XFRMA_TMPL]            = { .len = sizeof(struct xfrm_user_tmpl) },
1879         [XFRMA_SEC_CTX]         = { .len = sizeof(struct xfrm_sec_ctx) },
1880         [XFRMA_LTIME_VAL]       = { .len = sizeof(struct xfrm_lifetime_cur) },
1881         [XFRMA_REPLAY_VAL]      = { .len = sizeof(struct xfrm_replay_state) },
1882         [XFRMA_REPLAY_THRESH]   = { .type = NLA_U32 },
1883         [XFRMA_ETIMER_THRESH]   = { .type = NLA_U32 },
1884         [XFRMA_SRCADDR]         = { .len = sizeof(xfrm_address_t) },
1885         [XFRMA_COADDR]          = { .len = sizeof(xfrm_address_t) },
1886         [XFRMA_POLICY_TYPE]     = { .len = sizeof(struct xfrm_userpolicy_type)},
1887         [XFRMA_MIGRATE]         = { .len = sizeof(struct xfrm_user_migrate) },
1888 };
1889 
1890 static struct xfrm_link {
1891         int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
1892         int (*dump)(struct sk_buff *, struct netlink_callback *);
1893 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1894         [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
1895         [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
1896         [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1897                                                    .dump = xfrm_dump_sa       },
1898         [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
1899         [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
1900         [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1901                                                    .dump = xfrm_dump_policy   },
1902         [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1903         [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
1904         [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1905         [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
1906         [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
1907         [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1908         [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
1909         [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
1910         [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
1911         [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
1912         [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
1913         [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
1914         [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
1915 };
1916 
1917 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1918 {
1919         struct nlattr *attrs[XFRMA_MAX+1];
1920         struct xfrm_link *link;
1921         int type, err;
1922 
1923         type = nlh->nlmsg_type;
1924         if (type > XFRM_MSG_MAX)
1925                 return -EINVAL;
1926 
1927         type -= XFRM_MSG_BASE;
1928         link = &xfrm_dispatch[type];
1929 
1930         /* All operations require privileges, even GET */
1931         if (security_netlink_recv(skb, CAP_NET_ADMIN))
1932                 return -EPERM;
1933 
1934         if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1935              type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1936             (nlh->nlmsg_flags & NLM_F_DUMP)) {
1937                 if (link->dump == NULL)
1938                         return -EINVAL;
1939 
1940                 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
1941         }
1942 
1943         err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
1944                           xfrma_policy);
1945         if (err < 0)
1946                 return err;
1947 
1948         if (link->doit == NULL)
1949                 return -EINVAL;
1950 
1951         return link->doit(skb, nlh, attrs);
1952 }
1953 
1954 static void xfrm_netlink_rcv(struct sk_buff *skb)
1955 {
1956         mutex_lock(&xfrm_cfg_mutex);
1957         netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1958         mutex_unlock(&xfrm_cfg_mutex);
1959 }
1960 
1961 static inline size_t xfrm_expire_msgsize(void)
1962 {
1963         return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1964 }
1965 
1966 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1967 {
1968         struct xfrm_user_expire *ue;
1969         struct nlmsghdr *nlh;
1970 
1971         nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1972         if (nlh == NULL)
1973                 return -EMSGSIZE;
1974 
1975         ue = nlmsg_data(nlh);
1976         copy_to_user_state(x, &ue->state);
1977         ue->hard = (c->data.hard != 0) ? 1 : 0;
1978 
1979         return nlmsg_end(skb, nlh);
1980 }
1981 
1982 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1983 {
1984         struct sk_buff *skb;
1985 
1986         skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
1987         if (skb == NULL)
1988                 return -ENOMEM;
1989 
1990         if (build_expire(skb, x, c) < 0)
1991                 BUG();
1992 
1993         return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1994 }
1995 
1996 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1997 {
1998         struct sk_buff *skb;
1999 
2000         skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
2001         if (skb == NULL)
2002                 return -ENOMEM;
2003 
2004         if (build_aevent(skb, x, c) < 0)
2005                 BUG();
2006 
2007         return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2008 }
2009 
2010 static int xfrm_notify_sa_flush(struct km_event *c)
2011 {
2012         struct xfrm_usersa_flush *p;
2013         struct nlmsghdr *nlh;
2014         struct sk_buff *skb;
2015         int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2016 
2017         skb = nlmsg_new(len, GFP_ATOMIC);
2018         if (skb == NULL)
2019                 return -ENOMEM;
2020 
2021         nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2022         if (nlh == NULL) {
2023                 kfree_skb(skb);
2024                 return -EMSGSIZE;
2025         }
2026 
2027         p = nlmsg_data(nlh);
2028         p->proto = c->data.proto;
2029 
2030         nlmsg_end(skb, nlh);
2031 
2032         return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2033 }
2034 
2035 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2036 {
2037         size_t l = 0;
2038         if (x->aead)
2039                 l += nla_total_size(aead_len(x->aead));
2040         if (x->aalg)
2041                 l += nla_total_size(xfrm_alg_len(x->aalg));
2042         if (x->ealg)
2043                 l += nla_total_size(xfrm_alg_len(x->ealg));
2044         if (x->calg)
2045                 l += nla_total_size(sizeof(*x->calg));
2046         if (x->encap)
2047                 l += nla_total_size(sizeof(*x->encap));
2048         if (x->security)
2049                 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2050                                     x->security->ctx_len);
2051         if (x->coaddr)
2052                 l += nla_total_size(sizeof(*x->coaddr));
2053 
2054         /* Must count x->lastused as it may become non-zero behind our back. */
2055         l += nla_total_size(sizeof(u64));
2056 
2057         return l;
2058 }
2059 
2060 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2061 {
2062         struct xfrm_usersa_info *p;
2063         struct xfrm_usersa_id *id;
2064         struct nlmsghdr *nlh;
2065         struct sk_buff *skb;
2066         int len = xfrm_sa_len(x);
2067         int headlen;
2068 
2069         headlen = sizeof(*p);
2070         if (c->event == XFRM_MSG_DELSA) {
2071                 len += nla_total_size(headlen);
2072                 headlen = sizeof(*id);
2073         }
2074         len += NLMSG_ALIGN(headlen);
2075 
2076         skb = nlmsg_new(len, GFP_ATOMIC);
2077         if (skb == NULL)
2078                 return -ENOMEM;
2079 
2080         nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2081         if (nlh == NULL)
2082                 goto nla_put_failure;
2083 
2084         p = nlmsg_data(nlh);
2085         if (c->event == XFRM_MSG_DELSA) {
2086                 struct nlattr *attr;
2087 
2088                 id = nlmsg_data(nlh);
2089                 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2090                 id->spi = x->id.spi;
2091                 id->family = x->props.family;
2092                 id->proto = x->id.proto;
2093 
2094                 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2095                 if (attr == NULL)
2096                         goto nla_put_failure;
2097 
2098                 p = nla_data(attr);
2099         }
2100 
2101         if (copy_to_user_state_extra(x, p, skb))
2102                 goto nla_put_failure;
2103 
2104         nlmsg_end(skb, nlh);
2105 
2106         return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2107 
2108 nla_put_failure:
2109         /* Somebody screwed up with xfrm_sa_len! */
2110         WARN_ON(1);
2111         kfree_skb(skb);
2112         return -1;
2113 }
2114 
2115 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2116 {
2117 
2118         switch (c->event) {
2119         case XFRM_MSG_EXPIRE:
2120                 return xfrm_exp_state_notify(x, c);
2121         case XFRM_MSG_NEWAE:
2122                 return xfrm_aevent_state_notify(x, c);
2123         case XFRM_MSG_DELSA:
2124         case XFRM_MSG_UPDSA:
2125         case XFRM_MSG_NEWSA:
2126                 return xfrm_notify_sa(x, c);
2127         case XFRM_MSG_FLUSHSA:
2128                 return xfrm_notify_sa_flush(c);
2129         default:
2130                  printk("xfrm_user: Unknown SA event %d\n", c->event);
2131                  break;
2132         }
2133 
2134         return 0;
2135 
2136 }
2137 
2138 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2139                                           struct xfrm_policy *xp)
2140 {
2141         return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2142                + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2143                + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2144                + userpolicy_type_attrsize();
2145 }
2146 
2147 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2148                          struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2149                          int dir)
2150 {
2151         struct xfrm_user_acquire *ua;
2152         struct nlmsghdr *nlh;
2153         __u32 seq = xfrm_get_acqseq();
2154 
2155         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2156         if (nlh == NULL)
2157                 return -EMSGSIZE;
2158 
2159         ua = nlmsg_data(nlh);
2160         memcpy(&ua->id, &x->id, sizeof(ua->id));
2161         memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2162         memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2163         copy_to_user_policy(xp, &ua->policy, dir);
2164         ua->aalgos = xt->aalgos;
2165         ua->ealgos = xt->ealgos;
2166         ua->calgos = xt->calgos;
2167         ua->seq = x->km.seq = seq;
2168 
2169         if (copy_to_user_tmpl(xp, skb) < 0)
2170                 goto nlmsg_failure;
2171         if (copy_to_user_state_sec_ctx(x, skb))
2172                 goto nlmsg_failure;
2173         if (copy_to_user_policy_type(xp->type, skb) < 0)
2174                 goto nlmsg_failure;
2175 
2176         return nlmsg_end(skb, nlh);
2177 
2178 nlmsg_failure:
2179         nlmsg_cancel(skb, nlh);
2180         return -EMSGSIZE;
2181 }
2182 
2183 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2184                              struct xfrm_policy *xp, int dir)
2185 {
2186         struct sk_buff *skb;
2187 
2188         skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2189         if (skb == NULL)
2190                 return -ENOMEM;
2191 
2192         if (build_acquire(skb, x, xt, xp, dir) < 0)
2193                 BUG();
2194 
2195         return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2196 }
2197 
2198 /* User gives us xfrm_user_policy_info followed by an array of 0
2199  * or more templates.
2200  */
2201 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2202                                                u8 *data, int len, int *dir)
2203 {
2204         struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2205         struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2206         struct xfrm_policy *xp;
2207         int nr;
2208 
2209         switch (sk->sk_family) {
2210         case AF_INET:
2211                 if (opt != IP_XFRM_POLICY) {
2212                         *dir = -EOPNOTSUPP;
2213                         return NULL;
2214                 }
2215                 break;
2216 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2217         case AF_INET6:
2218                 if (opt != IPV6_XFRM_POLICY) {
2219                         *dir = -EOPNOTSUPP;
2220                         return NULL;
2221                 }
2222                 break;
2223 #endif
2224         default:
2225                 *dir = -EINVAL;
2226                 return NULL;
2227         }
2228 
2229         *dir = -EINVAL;
2230 
2231         if (len < sizeof(*p) ||
2232             verify_newpolicy_info(p))
2233                 return NULL;
2234 
2235         nr = ((len - sizeof(*p)) / sizeof(*ut));
2236         if (validate_tmpl(nr, ut, p->sel.family))
2237                 return NULL;
2238 
2239         if (p->dir > XFRM_POLICY_OUT)
2240                 return NULL;
2241 
2242         xp = xfrm_policy_alloc(GFP_KERNEL);
2243         if (xp == NULL) {
2244                 *dir = -ENOBUFS;
2245                 return NULL;
2246         }
2247 
2248         copy_from_user_policy(xp, p);
2249         xp->type = XFRM_POLICY_TYPE_MAIN;
2250         copy_templates(xp, ut, nr);
2251 
2252         *dir = p->dir;
2253 
2254         return xp;
2255 }
2256 
2257 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2258 {
2259         return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2260                + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2261                + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2262                + userpolicy_type_attrsize();
2263 }
2264 
2265 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2266                            int dir, struct km_event *c)
2267 {
2268         struct xfrm_user_polexpire *upe;
2269         struct nlmsghdr *nlh;
2270         int hard = c->data.hard;
2271 
2272         nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2273         if (nlh == NULL)
2274                 return -EMSGSIZE;
2275 
2276         upe = nlmsg_data(nlh);
2277         copy_to_user_policy(xp, &upe->pol, dir);
2278         if (copy_to_user_tmpl(xp, skb) < 0)
2279                 goto nlmsg_failure;
2280         if (copy_to_user_sec_ctx(xp, skb))
2281                 goto nlmsg_failure;
2282         if (copy_to_user_policy_type(xp->type, skb) < 0)
2283                 goto nlmsg_failure;
2284         upe->hard = !!hard;
2285 
2286         return nlmsg_end(skb, nlh);
2287 
2288 nlmsg_failure:
2289         nlmsg_cancel(skb, nlh);
2290         return -EMSGSIZE;
2291 }
2292 
2293 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2294 {
2295         struct sk_buff *skb;
2296 
2297         skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2298         if (skb == NULL)
2299                 return -ENOMEM;
2300 
2301         if (build_polexpire(skb, xp, dir, c) < 0)
2302                 BUG();
2303 
2304         return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2305 }
2306 
2307 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2308 {
2309         struct xfrm_userpolicy_info *p;
2310         struct xfrm_userpolicy_id *id;
2311         struct nlmsghdr *nlh;
2312         struct sk_buff *skb;
2313         int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2314         int headlen;
2315 
2316         headlen = sizeof(*p);
2317         if (c->event == XFRM_MSG_DELPOLICY) {
2318                 len += nla_total_size(headlen);
2319                 headlen = sizeof(*id);
2320         }
2321         len += userpolicy_type_attrsize();
2322         len += NLMSG_ALIGN(headlen);
2323 
2324         skb = nlmsg_new(len, GFP_ATOMIC);
2325         if (skb == NULL)
2326                 return -ENOMEM;
2327 
2328         nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2329         if (nlh == NULL)
2330                 goto nlmsg_failure;
2331 
2332         p = nlmsg_data(nlh);
2333         if (c->event == XFRM_MSG_DELPOLICY) {
2334                 struct nlattr *attr;
2335 
2336                 id = nlmsg_data(nlh);
2337                 memset(id, 0, sizeof(*id));
2338                 id->dir = dir;
2339                 if (c->data.byid)
2340                         id->index = xp->index;
2341                 else
2342                         memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2343 
2344                 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2345                 if (attr == NULL)
2346                         goto nlmsg_failure;
2347 
2348                 p = nla_data(attr);
2349         }
2350 
2351         copy_to_user_policy(xp, p, dir);
2352         if (copy_to_user_tmpl(xp, skb) < 0)
2353                 goto nlmsg_failure;
2354         if (copy_to_user_policy_type(xp->type, skb) < 0)
2355                 goto nlmsg_failure;
2356 
2357         nlmsg_end(skb, nlh);
2358 
2359         return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2360 
2361 nlmsg_failure:
2362         kfree_skb(skb);
2363         return -1;
2364 }
2365 
2366 static int xfrm_notify_policy_flush(struct km_event *c)
2367 {
2368         struct nlmsghdr *nlh;
2369         struct sk_buff *skb;
2370 
2371         skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2372         if (skb == NULL)
2373                 return -ENOMEM;
2374 
2375         nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2376         if (nlh == NULL)
2377                 goto nlmsg_failure;
2378         if (copy_to_user_policy_type(c->data.type, skb) < 0)
2379                 goto nlmsg_failure;
2380 
2381         nlmsg_end(skb, nlh);
2382 
2383         return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2384 
2385 nlmsg_failure:
2386         kfree_skb(skb);
2387         return -1;
2388 }
2389 
2390 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2391 {
2392 
2393         switch (c->event) {
2394         case XFRM_MSG_NEWPOLICY:
2395         case XFRM_MSG_UPDPOLICY:
2396         case XFRM_MSG_DELPOLICY:
2397                 return xfrm_notify_policy(xp, dir, c);
2398         case XFRM_MSG_FLUSHPOLICY:
2399                 return xfrm_notify_policy_flush(c);
2400         case XFRM_MSG_POLEXPIRE:
2401                 return xfrm_exp_policy_notify(xp, dir, c);
2402         default:
2403                 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2404         }
2405 
2406         return 0;
2407 
2408 }
2409 
2410 static inline size_t xfrm_report_msgsize(void)
2411 {
2412         return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2413 }
2414 
2415 static int build_report(struct sk_buff *skb, u8 proto,
2416                         struct xfrm_selector *sel, xfrm_address_t *addr)
2417 {
2418         struct xfrm_user_report *ur;
2419         struct nlmsghdr *nlh;
2420 
2421         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2422         if (nlh == NULL)
2423                 return -EMSGSIZE;
2424 
2425         ur = nlmsg_data(nlh);
2426         ur->proto = proto;
2427         memcpy(&ur->sel, sel, sizeof(ur->sel));
2428 
2429         if (addr)
2430                 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2431 
2432         return nlmsg_end(skb, nlh);
2433 
2434 nla_put_failure:
2435         nlmsg_cancel(skb, nlh);
2436         return -EMSGSIZE;
2437 }
2438 
2439 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2440                             xfrm_address_t *addr)
2441 {
2442         struct sk_buff *skb;
2443 
2444         skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2445         if (skb == NULL)
2446                 return -ENOMEM;
2447 
2448         if (build_report(skb, proto, sel, addr) < 0)
2449                 BUG();
2450 
2451         return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2452 }
2453 
2454 static struct xfrm_mgr netlink_mgr = {
2455         .id             = "netlink",
2456         .notify         = xfrm_send_state_notify,
2457         .acquire        = xfrm_send_acquire,
2458         .compile_policy = xfrm_compile_policy,
2459         .notify_policy  = xfrm_send_policy_notify,
2460         .report         = xfrm_send_report,
2461         .migrate        = xfrm_send_migrate,
2462 };
2463 
2464 static int __init xfrm_user_init(void)
2465 {
2466         struct sock *nlsk;
2467 
2468         printk(KERN_INFO "Initializing XFRM netlink socket\n");
2469 
2470         nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
2471                                      xfrm_netlink_rcv, NULL, THIS_MODULE);
2472         if (nlsk == NULL)
2473                 return -ENOMEM;
2474         rcu_assign_pointer(xfrm_nl, nlsk);
2475 
2476         xfrm_register_km(&netlink_mgr);
2477 
2478         return 0;
2479 }
2480 
2481 static void __exit xfrm_user_exit(void)
2482 {
2483         struct sock *nlsk = xfrm_nl;
2484 
2485         xfrm_unregister_km(&netlink_mgr);
2486         rcu_assign_pointer(xfrm_nl, NULL);
2487         synchronize_rcu();
2488         netlink_kernel_release(nlsk);
2489 }
2490 
2491 module_init(xfrm_user_init);
2492 module_exit(xfrm_user_exit);
2493 MODULE_LICENSE("GPL");
2494 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
2495 
2496 
  This page was automatically generated by the LXR engine.