~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/xfrm/xfrm_user.c

Version: ~ [ linux-5.2-rc1 ] ~ [ linux-5.1.2 ] ~ [ linux-5.0.16 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.43 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.119 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.176 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.179 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.139 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.67 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp