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

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

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

~ [ 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