1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
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>
28 #include <net/netlink.h>
29 #include <asm/uaccess.h>
30 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31 #include <linux/in6.h>
34 static inline int aead_len(struct xfrm_algo_aead
*alg
)
36 return sizeof(*alg
) + ((alg
->alg_key_len
+ 7) / 8);
39 static int verify_one_alg(struct nlattr
**attrs
, enum xfrm_attr_type_t type
)
41 struct nlattr
*rt
= attrs
[type
];
42 struct xfrm_algo
*algp
;
48 if (nla_len(rt
) < xfrm_alg_len(algp
))
61 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
65 static int verify_auth_trunc(struct nlattr
**attrs
)
67 struct nlattr
*rt
= attrs
[XFRMA_ALG_AUTH_TRUNC
];
68 struct xfrm_algo_auth
*algp
;
74 if (nla_len(rt
) < xfrm_alg_auth_len(algp
))
77 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
81 static int verify_aead(struct nlattr
**attrs
)
83 struct nlattr
*rt
= attrs
[XFRMA_ALG_AEAD
];
84 struct xfrm_algo_aead
*algp
;
90 if (nla_len(rt
) < aead_len(algp
))
93 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
97 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
98 xfrm_address_t
**addrp
)
100 struct nlattr
*rt
= attrs
[type
];
103 *addrp
= nla_data(rt
);
106 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
108 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
109 struct xfrm_user_sec_ctx
*uctx
;
115 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
122 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
123 struct nlattr
**attrs
)
133 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
145 switch (p
->id
.proto
) {
147 if ((!attrs
[XFRMA_ALG_AUTH
] &&
148 !attrs
[XFRMA_ALG_AUTH_TRUNC
]) ||
149 attrs
[XFRMA_ALG_AEAD
] ||
150 attrs
[XFRMA_ALG_CRYPT
] ||
151 attrs
[XFRMA_ALG_COMP
])
156 if (attrs
[XFRMA_ALG_COMP
])
158 if (!attrs
[XFRMA_ALG_AUTH
] &&
159 !attrs
[XFRMA_ALG_AUTH_TRUNC
] &&
160 !attrs
[XFRMA_ALG_CRYPT
] &&
161 !attrs
[XFRMA_ALG_AEAD
])
163 if ((attrs
[XFRMA_ALG_AUTH
] ||
164 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
165 attrs
[XFRMA_ALG_CRYPT
]) &&
166 attrs
[XFRMA_ALG_AEAD
])
171 if (!attrs
[XFRMA_ALG_COMP
] ||
172 attrs
[XFRMA_ALG_AEAD
] ||
173 attrs
[XFRMA_ALG_AUTH
] ||
174 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
175 attrs
[XFRMA_ALG_CRYPT
])
179 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
180 case IPPROTO_DSTOPTS
:
181 case IPPROTO_ROUTING
:
182 if (attrs
[XFRMA_ALG_COMP
] ||
183 attrs
[XFRMA_ALG_AUTH
] ||
184 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
185 attrs
[XFRMA_ALG_AEAD
] ||
186 attrs
[XFRMA_ALG_CRYPT
] ||
187 attrs
[XFRMA_ENCAP
] ||
188 attrs
[XFRMA_SEC_CTX
] ||
189 !attrs
[XFRMA_COADDR
])
198 if ((err
= verify_aead(attrs
)))
200 if ((err
= verify_auth_trunc(attrs
)))
202 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
204 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
206 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
208 if ((err
= verify_sec_ctx_len(attrs
)))
213 case XFRM_MODE_TRANSPORT
:
214 case XFRM_MODE_TUNNEL
:
215 case XFRM_MODE_ROUTEOPTIMIZATION
:
229 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
230 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
233 struct xfrm_algo
*p
, *ualg
;
234 struct xfrm_algo_desc
*algo
;
239 ualg
= nla_data(rta
);
241 algo
= get_byname(ualg
->alg_name
, 1);
244 *props
= algo
->desc
.sadb_alg_id
;
246 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
250 strcpy(p
->alg_name
, algo
->name
);
255 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
258 struct xfrm_algo
*ualg
;
259 struct xfrm_algo_auth
*p
;
260 struct xfrm_algo_desc
*algo
;
265 ualg
= nla_data(rta
);
267 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
270 *props
= algo
->desc
.sadb_alg_id
;
272 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
276 strcpy(p
->alg_name
, algo
->name
);
277 p
->alg_key_len
= ualg
->alg_key_len
;
278 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
279 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
285 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
288 struct xfrm_algo_auth
*p
, *ualg
;
289 struct xfrm_algo_desc
*algo
;
294 ualg
= nla_data(rta
);
296 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
299 if (ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
301 *props
= algo
->desc
.sadb_alg_id
;
303 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
307 strcpy(p
->alg_name
, algo
->name
);
308 if (!p
->alg_trunc_len
)
309 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
315 static int attach_aead(struct xfrm_algo_aead
**algpp
, u8
*props
,
318 struct xfrm_algo_aead
*p
, *ualg
;
319 struct xfrm_algo_desc
*algo
;
324 ualg
= nla_data(rta
);
326 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
329 *props
= algo
->desc
.sadb_alg_id
;
331 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
335 strcpy(p
->alg_name
, algo
->name
);
340 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
345 len
+= sizeof(struct xfrm_user_sec_ctx
);
346 len
+= xfrm_ctx
->ctx_len
;
351 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
353 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
354 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
355 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
356 x
->props
.mode
= p
->mode
;
357 x
->props
.replay_window
= p
->replay_window
;
358 x
->props
.reqid
= p
->reqid
;
359 x
->props
.family
= p
->family
;
360 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
361 x
->props
.flags
= p
->flags
;
363 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
364 x
->sel
.family
= p
->family
;
368 * someday when pfkey also has support, we could have the code
369 * somehow made shareable and move it to xfrm_state.c - JHS
372 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
)
374 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
375 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
376 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
377 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
380 struct xfrm_replay_state
*replay
;
381 replay
= nla_data(rp
);
382 memcpy(&x
->replay
, replay
, sizeof(*replay
));
383 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
387 struct xfrm_lifetime_cur
*ltime
;
388 ltime
= nla_data(lt
);
389 x
->curlft
.bytes
= ltime
->bytes
;
390 x
->curlft
.packets
= ltime
->packets
;
391 x
->curlft
.add_time
= ltime
->add_time
;
392 x
->curlft
.use_time
= ltime
->use_time
;
396 x
->replay_maxage
= nla_get_u32(et
);
399 x
->replay_maxdiff
= nla_get_u32(rt
);
402 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
403 struct xfrm_usersa_info
*p
,
404 struct nlattr
**attrs
,
407 struct xfrm_state
*x
= xfrm_state_alloc(net
);
413 copy_from_user_state(x
, p
);
415 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
416 attrs
[XFRMA_ALG_AEAD
])))
418 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
419 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
421 if (!x
->props
.aalgo
) {
422 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
423 attrs
[XFRMA_ALG_AUTH
])))
426 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
427 xfrm_ealg_get_byname
,
428 attrs
[XFRMA_ALG_CRYPT
])))
430 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
431 xfrm_calg_get_byname
,
432 attrs
[XFRMA_ALG_COMP
])))
435 if (attrs
[XFRMA_ENCAP
]) {
436 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
437 sizeof(*x
->encap
), GFP_KERNEL
);
438 if (x
->encap
== NULL
)
442 if (attrs
[XFRMA_COADDR
]) {
443 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
444 sizeof(*x
->coaddr
), GFP_KERNEL
);
445 if (x
->coaddr
== NULL
)
449 xfrm_mark_get(attrs
, &x
->mark
);
451 err
= xfrm_init_state(x
);
455 if (attrs
[XFRMA_SEC_CTX
] &&
456 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
460 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
461 /* sysctl_xfrm_aevent_etime is in 100ms units */
462 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
463 x
->preplay
.bitmap
= 0;
464 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
465 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
467 /* override default values from above */
469 xfrm_update_ae_params(x
, attrs
);
474 x
->km
.state
= XFRM_STATE_DEAD
;
481 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
482 struct nlattr
**attrs
)
484 struct net
*net
= sock_net(skb
->sk
);
485 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
486 struct xfrm_state
*x
;
489 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
490 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
491 u32 sid
= NETLINK_CB(skb
).sid
;
493 err
= verify_newsa_info(p
, attrs
);
497 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
502 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
503 err
= xfrm_state_add(x
);
505 err
= xfrm_state_update(x
);
507 xfrm_audit_state_add(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
510 x
->km
.state
= XFRM_STATE_DEAD
;
515 c
.seq
= nlh
->nlmsg_seq
;
516 c
.pid
= nlh
->nlmsg_pid
;
517 c
.event
= nlh
->nlmsg_type
;
519 km_state_notify(x
, &c
);
525 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
526 struct xfrm_usersa_id
*p
,
527 struct nlattr
**attrs
,
530 struct xfrm_state
*x
= NULL
;
533 u32 mark
= xfrm_mark_get(attrs
, &m
);
535 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
537 x
= xfrm_state_lookup(net
, mark
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
539 xfrm_address_t
*saddr
= NULL
;
541 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
548 x
= xfrm_state_lookup_byaddr(net
, mark
,
550 p
->proto
, p
->family
);
559 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
560 struct nlattr
**attrs
)
562 struct net
*net
= sock_net(skb
->sk
);
563 struct xfrm_state
*x
;
566 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
567 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
568 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
569 u32 sid
= NETLINK_CB(skb
).sid
;
571 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
575 if ((err
= security_xfrm_state_delete(x
)) != 0)
578 if (xfrm_state_kern(x
)) {
583 err
= xfrm_state_delete(x
);
588 c
.seq
= nlh
->nlmsg_seq
;
589 c
.pid
= nlh
->nlmsg_pid
;
590 c
.event
= nlh
->nlmsg_type
;
591 km_state_notify(x
, &c
);
594 xfrm_audit_state_delete(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
599 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
601 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
602 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
603 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
604 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
605 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
606 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
607 p
->mode
= x
->props
.mode
;
608 p
->replay_window
= x
->props
.replay_window
;
609 p
->reqid
= x
->props
.reqid
;
610 p
->family
= x
->props
.family
;
611 p
->flags
= x
->props
.flags
;
615 struct xfrm_dump_info
{
616 struct sk_buff
*in_skb
;
617 struct sk_buff
*out_skb
;
622 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
624 struct xfrm_user_sec_ctx
*uctx
;
626 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
628 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
632 uctx
= nla_data(attr
);
633 uctx
->exttype
= XFRMA_SEC_CTX
;
634 uctx
->len
= ctx_size
;
635 uctx
->ctx_doi
= s
->ctx_doi
;
636 uctx
->ctx_alg
= s
->ctx_alg
;
637 uctx
->ctx_len
= s
->ctx_len
;
638 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
643 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
645 struct xfrm_algo
*algo
;
648 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
649 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
653 algo
= nla_data(nla
);
654 strcpy(algo
->alg_name
, auth
->alg_name
);
655 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
656 algo
->alg_key_len
= auth
->alg_key_len
;
661 /* Don't change this without updating xfrm_sa_len! */
662 static int copy_to_user_state_extra(struct xfrm_state
*x
,
663 struct xfrm_usersa_info
*p
,
666 copy_to_user_state(x
, p
);
669 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
672 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
675 NLA_PUT(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
677 if (copy_to_user_auth(x
->aalg
, skb
))
678 goto nla_put_failure
;
680 NLA_PUT(skb
, XFRMA_ALG_AUTH_TRUNC
,
681 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
684 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
686 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
689 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
691 if (xfrm_mark_put(skb
, &x
->mark
))
692 goto nla_put_failure
;
694 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
695 goto nla_put_failure
;
703 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
705 struct xfrm_dump_info
*sp
= ptr
;
706 struct sk_buff
*in_skb
= sp
->in_skb
;
707 struct sk_buff
*skb
= sp
->out_skb
;
708 struct xfrm_usersa_info
*p
;
709 struct nlmsghdr
*nlh
;
712 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
713 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
719 err
= copy_to_user_state_extra(x
, p
, skb
);
721 goto nla_put_failure
;
727 nlmsg_cancel(skb
, nlh
);
731 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
733 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
734 xfrm_state_walk_done(walk
);
738 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
740 struct net
*net
= sock_net(skb
->sk
);
741 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
742 struct xfrm_dump_info info
;
744 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
745 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
747 info
.in_skb
= cb
->skb
;
749 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
750 info
.nlmsg_flags
= NLM_F_MULTI
;
754 xfrm_state_walk_init(walk
, 0);
757 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
762 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
763 struct xfrm_state
*x
, u32 seq
)
765 struct xfrm_dump_info info
;
768 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
770 return ERR_PTR(-ENOMEM
);
772 info
.in_skb
= in_skb
;
774 info
.nlmsg_seq
= seq
;
775 info
.nlmsg_flags
= 0;
777 if (dump_one_state(x
, 0, &info
)) {
785 static inline size_t xfrm_spdinfo_msgsize(void)
787 return NLMSG_ALIGN(4)
788 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
789 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
792 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
793 u32 pid
, u32 seq
, u32 flags
)
795 struct xfrmk_spdinfo si
;
796 struct xfrmu_spdinfo spc
;
797 struct xfrmu_spdhinfo sph
;
798 struct nlmsghdr
*nlh
;
801 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
802 if (nlh
== NULL
) /* shouldnt really happen ... */
807 xfrm_spd_getinfo(net
, &si
);
808 spc
.incnt
= si
.incnt
;
809 spc
.outcnt
= si
.outcnt
;
810 spc
.fwdcnt
= si
.fwdcnt
;
811 spc
.inscnt
= si
.inscnt
;
812 spc
.outscnt
= si
.outscnt
;
813 spc
.fwdscnt
= si
.fwdscnt
;
814 sph
.spdhcnt
= si
.spdhcnt
;
815 sph
.spdhmcnt
= si
.spdhmcnt
;
817 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
818 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
820 return nlmsg_end(skb
, nlh
);
823 nlmsg_cancel(skb
, nlh
);
827 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
828 struct nlattr
**attrs
)
830 struct net
*net
= sock_net(skb
->sk
);
831 struct sk_buff
*r_skb
;
832 u32
*flags
= nlmsg_data(nlh
);
833 u32 spid
= NETLINK_CB(skb
).pid
;
834 u32 seq
= nlh
->nlmsg_seq
;
836 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
840 if (build_spdinfo(r_skb
, net
, spid
, seq
, *flags
) < 0)
843 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, spid
);
846 static inline size_t xfrm_sadinfo_msgsize(void)
848 return NLMSG_ALIGN(4)
849 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
850 + nla_total_size(4); /* XFRMA_SAD_CNT */
853 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
854 u32 pid
, u32 seq
, u32 flags
)
856 struct xfrmk_sadinfo si
;
857 struct xfrmu_sadhinfo sh
;
858 struct nlmsghdr
*nlh
;
861 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
862 if (nlh
== NULL
) /* shouldnt really happen ... */
867 xfrm_sad_getinfo(net
, &si
);
869 sh
.sadhmcnt
= si
.sadhmcnt
;
870 sh
.sadhcnt
= si
.sadhcnt
;
872 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
873 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
875 return nlmsg_end(skb
, nlh
);
878 nlmsg_cancel(skb
, nlh
);
882 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
883 struct nlattr
**attrs
)
885 struct net
*net
= sock_net(skb
->sk
);
886 struct sk_buff
*r_skb
;
887 u32
*flags
= nlmsg_data(nlh
);
888 u32 spid
= NETLINK_CB(skb
).pid
;
889 u32 seq
= nlh
->nlmsg_seq
;
891 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
895 if (build_sadinfo(r_skb
, net
, spid
, seq
, *flags
) < 0)
898 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, spid
);
901 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
902 struct nlattr
**attrs
)
904 struct net
*net
= sock_net(skb
->sk
);
905 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
906 struct xfrm_state
*x
;
907 struct sk_buff
*resp_skb
;
910 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
914 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
915 if (IS_ERR(resp_skb
)) {
916 err
= PTR_ERR(resp_skb
);
918 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).pid
);
925 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
927 switch (p
->info
.id
.proto
) {
933 /* IPCOMP spi is 16-bits. */
934 if (p
->max
>= 0x10000)
948 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
949 struct nlattr
**attrs
)
951 struct net
*net
= sock_net(skb
->sk
);
952 struct xfrm_state
*x
;
953 struct xfrm_userspi_info
*p
;
954 struct sk_buff
*resp_skb
;
955 xfrm_address_t
*daddr
;
962 err
= verify_userspi_info(p
);
966 family
= p
->info
.family
;
967 daddr
= &p
->info
.id
.daddr
;
971 mark
= xfrm_mark_get(attrs
, &m
);
973 x
= xfrm_find_acq_byseq(net
, mark
, p
->info
.seq
);
974 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
981 x
= xfrm_find_acq(net
, &m
, p
->info
.mode
, p
->info
.reqid
,
982 p
->info
.id
.proto
, daddr
,
989 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
993 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
994 if (IS_ERR(resp_skb
)) {
995 err
= PTR_ERR(resp_skb
);
999 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).pid
);
1007 static int verify_policy_dir(u8 dir
)
1010 case XFRM_POLICY_IN
:
1011 case XFRM_POLICY_OUT
:
1012 case XFRM_POLICY_FWD
:
1022 static int verify_policy_type(u8 type
)
1025 case XFRM_POLICY_TYPE_MAIN
:
1026 #ifdef CONFIG_XFRM_SUB_POLICY
1027 case XFRM_POLICY_TYPE_SUB
:
1038 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1041 case XFRM_SHARE_ANY
:
1042 case XFRM_SHARE_SESSION
:
1043 case XFRM_SHARE_USER
:
1044 case XFRM_SHARE_UNIQUE
:
1051 switch (p
->action
) {
1052 case XFRM_POLICY_ALLOW
:
1053 case XFRM_POLICY_BLOCK
:
1060 switch (p
->sel
.family
) {
1065 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1068 return -EAFNOSUPPORT
;
1075 return verify_policy_dir(p
->dir
);
1078 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1080 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1081 struct xfrm_user_sec_ctx
*uctx
;
1086 uctx
= nla_data(rt
);
1087 return security_xfrm_policy_alloc(&pol
->security
, uctx
);
1090 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1096 for (i
= 0; i
< nr
; i
++, ut
++) {
1097 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1099 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1100 memcpy(&t
->saddr
, &ut
->saddr
,
1101 sizeof(xfrm_address_t
));
1102 t
->reqid
= ut
->reqid
;
1104 t
->share
= ut
->share
;
1105 t
->optional
= ut
->optional
;
1106 t
->aalgos
= ut
->aalgos
;
1107 t
->ealgos
= ut
->ealgos
;
1108 t
->calgos
= ut
->calgos
;
1109 /* If all masks are ~0, then we allow all algorithms. */
1110 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1111 t
->encap_family
= ut
->family
;
1115 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1119 if (nr
> XFRM_MAX_DEPTH
)
1122 for (i
= 0; i
< nr
; i
++) {
1123 /* We never validated the ut->family value, so many
1124 * applications simply leave it at zero. The check was
1125 * never made and ut->family was ignored because all
1126 * templates could be assumed to have the same family as
1127 * the policy itself. Now that we will have ipv4-in-ipv6
1128 * and ipv6-in-ipv4 tunnels, this is no longer true.
1131 ut
[i
].family
= family
;
1133 switch (ut
[i
].family
) {
1136 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1148 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1150 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1155 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1156 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1159 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1163 copy_templates(pol
, utmpl
, nr
);
1168 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1170 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1171 struct xfrm_userpolicy_type
*upt
;
1172 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1180 err
= verify_policy_type(type
);
1188 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1190 xp
->priority
= p
->priority
;
1191 xp
->index
= p
->index
;
1192 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1193 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1194 xp
->action
= p
->action
;
1195 xp
->flags
= p
->flags
;
1196 xp
->family
= p
->sel
.family
;
1197 /* XXX xp->share = p->share; */
1200 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1202 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1203 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1204 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1205 p
->priority
= xp
->priority
;
1206 p
->index
= xp
->index
;
1207 p
->sel
.family
= xp
->family
;
1209 p
->action
= xp
->action
;
1210 p
->flags
= xp
->flags
;
1211 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1214 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1216 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1224 copy_from_user_policy(xp
, p
);
1226 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1230 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1231 err
= copy_from_user_sec_ctx(xp
, attrs
);
1235 xfrm_mark_get(attrs
, &xp
->mark
);
1241 xfrm_policy_destroy(xp
);
1245 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1246 struct nlattr
**attrs
)
1248 struct net
*net
= sock_net(skb
->sk
);
1249 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1250 struct xfrm_policy
*xp
;
1254 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1255 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
1256 u32 sid
= NETLINK_CB(skb
).sid
;
1258 err
= verify_newpolicy_info(p
);
1261 err
= verify_sec_ctx_len(attrs
);
1265 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1269 /* shouldnt excl be based on nlh flags??
1270 * Aha! this is anti-netlink really i.e more pfkey derived
1271 * in netlink excl is a flag and you wouldnt need
1272 * a type XFRM_MSG_UPDPOLICY - JHS */
1273 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1274 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1275 xfrm_audit_policy_add(xp
, err
? 0 : 1, loginuid
, sessionid
, sid
);
1278 security_xfrm_policy_free(xp
->security
);
1283 c
.event
= nlh
->nlmsg_type
;
1284 c
.seq
= nlh
->nlmsg_seq
;
1285 c
.pid
= nlh
->nlmsg_pid
;
1286 km_policy_notify(xp
, p
->dir
, &c
);
1293 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1295 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1298 if (xp
->xfrm_nr
== 0)
1301 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1302 struct xfrm_user_tmpl
*up
= &vec
[i
];
1303 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1305 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1306 up
->family
= kp
->encap_family
;
1307 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1308 up
->reqid
= kp
->reqid
;
1309 up
->mode
= kp
->mode
;
1310 up
->share
= kp
->share
;
1311 up
->optional
= kp
->optional
;
1312 up
->aalgos
= kp
->aalgos
;
1313 up
->ealgos
= kp
->ealgos
;
1314 up
->calgos
= kp
->calgos
;
1317 return nla_put(skb
, XFRMA_TMPL
,
1318 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1321 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1324 return copy_sec_ctx(x
->security
, skb
);
1329 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1332 return copy_sec_ctx(xp
->security
, skb
);
1336 static inline size_t userpolicy_type_attrsize(void)
1338 #ifdef CONFIG_XFRM_SUB_POLICY
1339 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1345 #ifdef CONFIG_XFRM_SUB_POLICY
1346 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1348 struct xfrm_userpolicy_type upt
= {
1352 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1356 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1362 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1364 struct xfrm_dump_info
*sp
= ptr
;
1365 struct xfrm_userpolicy_info
*p
;
1366 struct sk_buff
*in_skb
= sp
->in_skb
;
1367 struct sk_buff
*skb
= sp
->out_skb
;
1368 struct nlmsghdr
*nlh
;
1370 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1371 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1375 p
= nlmsg_data(nlh
);
1376 copy_to_user_policy(xp
, p
, dir
);
1377 if (copy_to_user_tmpl(xp
, skb
) < 0)
1379 if (copy_to_user_sec_ctx(xp
, skb
))
1381 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1383 if (xfrm_mark_put(skb
, &xp
->mark
))
1384 goto nla_put_failure
;
1386 nlmsg_end(skb
, nlh
);
1391 nlmsg_cancel(skb
, nlh
);
1395 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1397 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1399 xfrm_policy_walk_done(walk
);
1403 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1405 struct net
*net
= sock_net(skb
->sk
);
1406 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1407 struct xfrm_dump_info info
;
1409 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1410 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1412 info
.in_skb
= cb
->skb
;
1414 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1415 info
.nlmsg_flags
= NLM_F_MULTI
;
1419 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1422 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1427 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1428 struct xfrm_policy
*xp
,
1431 struct xfrm_dump_info info
;
1432 struct sk_buff
*skb
;
1434 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1436 return ERR_PTR(-ENOMEM
);
1438 info
.in_skb
= in_skb
;
1440 info
.nlmsg_seq
= seq
;
1441 info
.nlmsg_flags
= 0;
1443 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1451 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1452 struct nlattr
**attrs
)
1454 struct net
*net
= sock_net(skb
->sk
);
1455 struct xfrm_policy
*xp
;
1456 struct xfrm_userpolicy_id
*p
;
1457 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1462 u32 mark
= xfrm_mark_get(attrs
, &m
);
1464 p
= nlmsg_data(nlh
);
1465 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1467 err
= copy_from_user_policy_type(&type
, attrs
);
1471 err
= verify_policy_dir(p
->dir
);
1476 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, delete, &err
);
1478 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1479 struct xfrm_sec_ctx
*ctx
;
1481 err
= verify_sec_ctx_len(attrs
);
1487 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1489 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1493 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
, &p
->sel
,
1495 security_xfrm_policy_free(ctx
);
1501 struct sk_buff
*resp_skb
;
1503 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1504 if (IS_ERR(resp_skb
)) {
1505 err
= PTR_ERR(resp_skb
);
1507 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1508 NETLINK_CB(skb
).pid
);
1511 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1512 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
1513 u32 sid
= NETLINK_CB(skb
).sid
;
1515 xfrm_audit_policy_delete(xp
, err
? 0 : 1, loginuid
, sessionid
,
1521 c
.data
.byid
= p
->index
;
1522 c
.event
= nlh
->nlmsg_type
;
1523 c
.seq
= nlh
->nlmsg_seq
;
1524 c
.pid
= nlh
->nlmsg_pid
;
1525 km_policy_notify(xp
, p
->dir
, &c
);
1533 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1534 struct nlattr
**attrs
)
1536 struct net
*net
= sock_net(skb
->sk
);
1538 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1539 struct xfrm_audit audit_info
;
1542 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1543 audit_info
.sessionid
= NETLINK_CB(skb
).sessionid
;
1544 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1545 err
= xfrm_state_flush(net
, p
->proto
, &audit_info
);
1547 if (err
== -ESRCH
) /* empty table */
1551 c
.data
.proto
= p
->proto
;
1552 c
.event
= nlh
->nlmsg_type
;
1553 c
.seq
= nlh
->nlmsg_seq
;
1554 c
.pid
= nlh
->nlmsg_pid
;
1556 km_state_notify(NULL
, &c
);
1561 static inline size_t xfrm_aevent_msgsize(void)
1563 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1564 + nla_total_size(sizeof(struct xfrm_replay_state
))
1565 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1566 + nla_total_size(sizeof(struct xfrm_mark
))
1567 + nla_total_size(4) /* XFRM_AE_RTHR */
1568 + nla_total_size(4); /* XFRM_AE_ETHR */
1571 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1573 struct xfrm_aevent_id
*id
;
1574 struct nlmsghdr
*nlh
;
1576 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1580 id
= nlmsg_data(nlh
);
1581 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1582 id
->sa_id
.spi
= x
->id
.spi
;
1583 id
->sa_id
.family
= x
->props
.family
;
1584 id
->sa_id
.proto
= x
->id
.proto
;
1585 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1586 id
->reqid
= x
->props
.reqid
;
1587 id
->flags
= c
->data
.aevent
;
1589 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1590 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1592 if (id
->flags
& XFRM_AE_RTHR
)
1593 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1595 if (id
->flags
& XFRM_AE_ETHR
)
1596 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1597 x
->replay_maxage
* 10 / HZ
);
1599 if (xfrm_mark_put(skb
, &x
->mark
))
1600 goto nla_put_failure
;
1602 return nlmsg_end(skb
, nlh
);
1605 nlmsg_cancel(skb
, nlh
);
1609 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1610 struct nlattr
**attrs
)
1612 struct net
*net
= sock_net(skb
->sk
);
1613 struct xfrm_state
*x
;
1614 struct sk_buff
*r_skb
;
1619 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1620 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1622 r_skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1626 mark
= xfrm_mark_get(attrs
, &m
);
1628 x
= xfrm_state_lookup(net
, mark
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1635 * XXX: is this lock really needed - none of the other
1636 * gets lock (the concern is things getting updated
1637 * while we are still reading) - jhs
1639 spin_lock_bh(&x
->lock
);
1640 c
.data
.aevent
= p
->flags
;
1641 c
.seq
= nlh
->nlmsg_seq
;
1642 c
.pid
= nlh
->nlmsg_pid
;
1644 if (build_aevent(r_skb
, x
, &c
) < 0)
1646 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).pid
);
1647 spin_unlock_bh(&x
->lock
);
1652 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1653 struct nlattr
**attrs
)
1655 struct net
*net
= sock_net(skb
->sk
);
1656 struct xfrm_state
*x
;
1661 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1662 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1663 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1668 /* pedantic mode - thou shalt sayeth replaceth */
1669 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1672 mark
= xfrm_mark_get(attrs
, &m
);
1674 x
= xfrm_state_lookup(net
, mark
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1678 if (x
->km
.state
!= XFRM_STATE_VALID
)
1681 spin_lock_bh(&x
->lock
);
1682 xfrm_update_ae_params(x
, attrs
);
1683 spin_unlock_bh(&x
->lock
);
1685 c
.event
= nlh
->nlmsg_type
;
1686 c
.seq
= nlh
->nlmsg_seq
;
1687 c
.pid
= nlh
->nlmsg_pid
;
1688 c
.data
.aevent
= XFRM_AE_CU
;
1689 km_state_notify(x
, &c
);
1696 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1697 struct nlattr
**attrs
)
1699 struct net
*net
= sock_net(skb
->sk
);
1701 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1703 struct xfrm_audit audit_info
;
1705 err
= copy_from_user_policy_type(&type
, attrs
);
1709 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1710 audit_info
.sessionid
= NETLINK_CB(skb
).sessionid
;
1711 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1712 err
= xfrm_policy_flush(net
, type
, &audit_info
);
1714 if (err
== -ESRCH
) /* empty table */
1720 c
.event
= nlh
->nlmsg_type
;
1721 c
.seq
= nlh
->nlmsg_seq
;
1722 c
.pid
= nlh
->nlmsg_pid
;
1724 km_policy_notify(NULL
, 0, &c
);
1728 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1729 struct nlattr
**attrs
)
1731 struct net
*net
= sock_net(skb
->sk
);
1732 struct xfrm_policy
*xp
;
1733 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1734 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1735 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1738 u32 mark
= xfrm_mark_get(attrs
, &m
);
1740 err
= copy_from_user_policy_type(&type
, attrs
);
1744 err
= verify_policy_dir(p
->dir
);
1749 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, 0, &err
);
1751 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1752 struct xfrm_sec_ctx
*ctx
;
1754 err
= verify_sec_ctx_len(attrs
);
1760 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1762 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1766 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
,
1767 &p
->sel
, ctx
, 0, &err
);
1768 security_xfrm_policy_free(ctx
);
1773 if (unlikely(xp
->walk
.dead
))
1778 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1779 uid_t sessionid
= NETLINK_CB(skb
).sessionid
;
1780 u32 sid
= NETLINK_CB(skb
).sid
;
1781 xfrm_policy_delete(xp
, p
->dir
);
1782 xfrm_audit_policy_delete(xp
, 1, loginuid
, sessionid
, sid
);
1785 // reset the timers here?
1786 WARN(1, "Dont know what to do with soft policy expire\n");
1788 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1795 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1796 struct nlattr
**attrs
)
1798 struct net
*net
= sock_net(skb
->sk
);
1799 struct xfrm_state
*x
;
1801 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1802 struct xfrm_usersa_info
*p
= &ue
->state
;
1804 u32 mark
= xfrm_mark_get(attrs
, &m
);
1806 x
= xfrm_state_lookup(net
, mark
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1812 spin_lock_bh(&x
->lock
);
1814 if (x
->km
.state
!= XFRM_STATE_VALID
)
1816 km_state_expired(x
, ue
->hard
, current
->pid
);
1819 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1820 uid_t sessionid
= NETLINK_CB(skb
).sessionid
;
1821 u32 sid
= NETLINK_CB(skb
).sid
;
1822 __xfrm_state_delete(x
);
1823 xfrm_audit_state_delete(x
, 1, loginuid
, sessionid
, sid
);
1827 spin_unlock_bh(&x
->lock
);
1832 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1833 struct nlattr
**attrs
)
1835 struct net
*net
= sock_net(skb
->sk
);
1836 struct xfrm_policy
*xp
;
1837 struct xfrm_user_tmpl
*ut
;
1839 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1840 struct xfrm_mark mark
;
1842 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1843 struct xfrm_state
*x
= xfrm_state_alloc(net
);
1849 xfrm_mark_get(attrs
, &mark
);
1851 err
= verify_newpolicy_info(&ua
->policy
);
1856 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
1860 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1861 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1862 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1863 xp
->mark
.m
= x
->mark
.m
= mark
.m
;
1864 xp
->mark
.v
= x
->mark
.v
= mark
.v
;
1866 /* extract the templates and for each call km_key */
1867 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1868 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1869 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1870 x
->props
.mode
= t
->mode
;
1871 x
->props
.reqid
= t
->reqid
;
1872 x
->props
.family
= ut
->family
;
1873 t
->aalgos
= ua
->aalgos
;
1874 t
->ealgos
= ua
->ealgos
;
1875 t
->calgos
= ua
->calgos
;
1876 err
= km_query(x
, t
, xp
);
1886 WARN(1, "BAD policy passed\n");
1893 #ifdef CONFIG_XFRM_MIGRATE
1894 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1895 struct xfrm_kmaddress
*k
,
1896 struct nlattr
**attrs
, int *num
)
1898 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
1899 struct xfrm_user_migrate
*um
;
1903 struct xfrm_user_kmaddress
*uk
;
1905 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
1906 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
1907 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
1908 k
->family
= uk
->family
;
1909 k
->reserved
= uk
->reserved
;
1913 num_migrate
= nla_len(rt
) / sizeof(*um
);
1915 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
1918 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
1919 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
1920 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
1921 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
1922 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
1924 ma
->proto
= um
->proto
;
1925 ma
->mode
= um
->mode
;
1926 ma
->reqid
= um
->reqid
;
1928 ma
->old_family
= um
->old_family
;
1929 ma
->new_family
= um
->new_family
;
1936 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1937 struct nlattr
**attrs
)
1939 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
1940 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
1941 struct xfrm_kmaddress km
, *kmp
;
1946 if (attrs
[XFRMA_MIGRATE
] == NULL
)
1949 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
1951 err
= copy_from_user_policy_type(&type
, attrs
);
1955 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
1962 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
);
1967 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1968 struct nlattr
**attrs
)
1970 return -ENOPROTOOPT
;
1974 #ifdef CONFIG_XFRM_MIGRATE
1975 static int copy_to_user_migrate(struct xfrm_migrate
*m
, struct sk_buff
*skb
)
1977 struct xfrm_user_migrate um
;
1979 memset(&um
, 0, sizeof(um
));
1980 um
.proto
= m
->proto
;
1982 um
.reqid
= m
->reqid
;
1983 um
.old_family
= m
->old_family
;
1984 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
1985 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
1986 um
.new_family
= m
->new_family
;
1987 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
1988 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
1990 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
1993 static int copy_to_user_kmaddress(struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
1995 struct xfrm_user_kmaddress uk
;
1997 memset(&uk
, 0, sizeof(uk
));
1998 uk
.family
= k
->family
;
1999 uk
.reserved
= k
->reserved
;
2000 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
2001 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
2003 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
2006 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
)
2008 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
2009 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
2010 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
2011 + userpolicy_type_attrsize();
2014 static int build_migrate(struct sk_buff
*skb
, struct xfrm_migrate
*m
,
2015 int num_migrate
, struct xfrm_kmaddress
*k
,
2016 struct xfrm_selector
*sel
, u8 dir
, u8 type
)
2018 struct xfrm_migrate
*mp
;
2019 struct xfrm_userpolicy_id
*pol_id
;
2020 struct nlmsghdr
*nlh
;
2023 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
2027 pol_id
= nlmsg_data(nlh
);
2028 /* copy data from selector, dir, and type to the pol_id */
2029 memset(pol_id
, 0, sizeof(*pol_id
));
2030 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
2033 if (k
!= NULL
&& (copy_to_user_kmaddress(k
, skb
) < 0))
2036 if (copy_to_user_policy_type(type
, skb
) < 0)
2039 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2040 if (copy_to_user_migrate(mp
, skb
) < 0)
2044 return nlmsg_end(skb
, nlh
);
2046 nlmsg_cancel(skb
, nlh
);
2050 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2051 struct xfrm_migrate
*m
, int num_migrate
,
2052 struct xfrm_kmaddress
*k
)
2054 struct net
*net
= &init_net
;
2055 struct sk_buff
*skb
;
2057 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
), GFP_ATOMIC
);
2062 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, dir
, type
) < 0)
2065 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
2068 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2069 struct xfrm_migrate
*m
, int num_migrate
,
2070 struct xfrm_kmaddress
*k
)
2072 return -ENOPROTOOPT
;
2076 #define XMSGSIZE(type) sizeof(struct type)
2078 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2079 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2080 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2081 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2082 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2083 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2084 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2085 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2086 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2087 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2088 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2089 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2090 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2091 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2092 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2093 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2094 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2095 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2096 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2097 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2098 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2103 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2104 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2105 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2106 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2107 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2108 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2109 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2110 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2111 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2112 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2113 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2114 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2115 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2116 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2117 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2118 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2119 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2120 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2121 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2122 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2123 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2124 [XFRMA_MARK
] = { .len
= sizeof(struct xfrm_mark
) },
2127 static struct xfrm_link
{
2128 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2129 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2130 int (*done
)(struct netlink_callback
*);
2131 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2132 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2133 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2134 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2135 .dump
= xfrm_dump_sa
,
2136 .done
= xfrm_dump_sa_done
},
2137 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2138 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2139 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2140 .dump
= xfrm_dump_policy
,
2141 .done
= xfrm_dump_policy_done
},
2142 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2143 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2144 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2145 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2146 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2147 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2148 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2149 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2150 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2151 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2152 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2153 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2154 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2157 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2159 struct net
*net
= sock_net(skb
->sk
);
2160 struct nlattr
*attrs
[XFRMA_MAX
+1];
2161 struct xfrm_link
*link
;
2164 type
= nlh
->nlmsg_type
;
2165 if (type
> XFRM_MSG_MAX
)
2168 type
-= XFRM_MSG_BASE
;
2169 link
= &xfrm_dispatch
[type
];
2171 /* All operations require privileges, even GET */
2172 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
2175 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2176 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2177 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2178 if (link
->dump
== NULL
)
2181 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, link
->dump
, link
->done
);
2184 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
2189 if (link
->doit
== NULL
)
2192 return link
->doit(skb
, nlh
, attrs
);
2195 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2197 mutex_lock(&xfrm_cfg_mutex
);
2198 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2199 mutex_unlock(&xfrm_cfg_mutex
);
2202 static inline size_t xfrm_expire_msgsize(void)
2204 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
))
2205 + nla_total_size(sizeof(struct xfrm_mark
));
2208 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
2210 struct xfrm_user_expire
*ue
;
2211 struct nlmsghdr
*nlh
;
2213 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2217 ue
= nlmsg_data(nlh
);
2218 copy_to_user_state(x
, &ue
->state
);
2219 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2221 if (xfrm_mark_put(skb
, &x
->mark
))
2222 goto nla_put_failure
;
2224 return nlmsg_end(skb
, nlh
);
2230 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2232 struct net
*net
= xs_net(x
);
2233 struct sk_buff
*skb
;
2235 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2239 if (build_expire(skb
, x
, c
) < 0) {
2244 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2247 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2249 struct net
*net
= xs_net(x
);
2250 struct sk_buff
*skb
;
2252 skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
2256 if (build_aevent(skb
, x
, c
) < 0)
2259 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
2262 static int xfrm_notify_sa_flush(struct km_event
*c
)
2264 struct net
*net
= c
->net
;
2265 struct xfrm_usersa_flush
*p
;
2266 struct nlmsghdr
*nlh
;
2267 struct sk_buff
*skb
;
2268 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2270 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2274 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2280 p
= nlmsg_data(nlh
);
2281 p
->proto
= c
->data
.proto
;
2283 nlmsg_end(skb
, nlh
);
2285 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2288 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2292 l
+= nla_total_size(aead_len(x
->aead
));
2294 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2295 (x
->aalg
->alg_key_len
+ 7) / 8);
2296 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2299 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2301 l
+= nla_total_size(sizeof(*x
->calg
));
2303 l
+= nla_total_size(sizeof(*x
->encap
));
2305 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2306 x
->security
->ctx_len
);
2308 l
+= nla_total_size(sizeof(*x
->coaddr
));
2310 /* Must count x->lastused as it may become non-zero behind our back. */
2311 l
+= nla_total_size(sizeof(u64
));
2316 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
2318 struct net
*net
= xs_net(x
);
2319 struct xfrm_usersa_info
*p
;
2320 struct xfrm_usersa_id
*id
;
2321 struct nlmsghdr
*nlh
;
2322 struct sk_buff
*skb
;
2323 int len
= xfrm_sa_len(x
);
2326 headlen
= sizeof(*p
);
2327 if (c
->event
== XFRM_MSG_DELSA
) {
2328 len
+= nla_total_size(headlen
);
2329 headlen
= sizeof(*id
);
2330 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2332 len
+= NLMSG_ALIGN(headlen
);
2334 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2338 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2340 goto nla_put_failure
;
2342 p
= nlmsg_data(nlh
);
2343 if (c
->event
== XFRM_MSG_DELSA
) {
2344 struct nlattr
*attr
;
2346 id
= nlmsg_data(nlh
);
2347 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2348 id
->spi
= x
->id
.spi
;
2349 id
->family
= x
->props
.family
;
2350 id
->proto
= x
->id
.proto
;
2352 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2354 goto nla_put_failure
;
2359 if (copy_to_user_state_extra(x
, p
, skb
))
2360 goto nla_put_failure
;
2362 nlmsg_end(skb
, nlh
);
2364 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2367 /* Somebody screwed up with xfrm_sa_len! */
2373 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2377 case XFRM_MSG_EXPIRE
:
2378 return xfrm_exp_state_notify(x
, c
);
2379 case XFRM_MSG_NEWAE
:
2380 return xfrm_aevent_state_notify(x
, c
);
2381 case XFRM_MSG_DELSA
:
2382 case XFRM_MSG_UPDSA
:
2383 case XFRM_MSG_NEWSA
:
2384 return xfrm_notify_sa(x
, c
);
2385 case XFRM_MSG_FLUSHSA
:
2386 return xfrm_notify_sa_flush(c
);
2388 printk(KERN_NOTICE
"xfrm_user: Unknown SA event %d\n",
2397 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2398 struct xfrm_policy
*xp
)
2400 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2401 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2402 + nla_total_size(sizeof(struct xfrm_mark
))
2403 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2404 + userpolicy_type_attrsize();
2407 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2408 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2411 struct xfrm_user_acquire
*ua
;
2412 struct nlmsghdr
*nlh
;
2413 __u32 seq
= xfrm_get_acqseq();
2415 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2419 ua
= nlmsg_data(nlh
);
2420 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2421 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2422 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2423 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2424 ua
->aalgos
= xt
->aalgos
;
2425 ua
->ealgos
= xt
->ealgos
;
2426 ua
->calgos
= xt
->calgos
;
2427 ua
->seq
= x
->km
.seq
= seq
;
2429 if (copy_to_user_tmpl(xp
, skb
) < 0)
2431 if (copy_to_user_state_sec_ctx(x
, skb
))
2433 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2435 if (xfrm_mark_put(skb
, &xp
->mark
))
2436 goto nla_put_failure
;
2438 return nlmsg_end(skb
, nlh
);
2442 nlmsg_cancel(skb
, nlh
);
2446 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2447 struct xfrm_policy
*xp
, int dir
)
2449 struct net
*net
= xs_net(x
);
2450 struct sk_buff
*skb
;
2452 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2456 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2459 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2462 /* User gives us xfrm_user_policy_info followed by an array of 0
2463 * or more templates.
2465 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2466 u8
*data
, int len
, int *dir
)
2468 struct net
*net
= sock_net(sk
);
2469 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2470 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2471 struct xfrm_policy
*xp
;
2474 switch (sk
->sk_family
) {
2476 if (opt
!= IP_XFRM_POLICY
) {
2481 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2483 if (opt
!= IPV6_XFRM_POLICY
) {
2496 if (len
< sizeof(*p
) ||
2497 verify_newpolicy_info(p
))
2500 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2501 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2504 if (p
->dir
> XFRM_POLICY_OUT
)
2507 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
2513 copy_from_user_policy(xp
, p
);
2514 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2515 copy_templates(xp
, ut
, nr
);
2522 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2524 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2525 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2526 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2527 + nla_total_size(sizeof(struct xfrm_mark
))
2528 + userpolicy_type_attrsize();
2531 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2532 int dir
, struct km_event
*c
)
2534 struct xfrm_user_polexpire
*upe
;
2535 struct nlmsghdr
*nlh
;
2536 int hard
= c
->data
.hard
;
2538 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2542 upe
= nlmsg_data(nlh
);
2543 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2544 if (copy_to_user_tmpl(xp
, skb
) < 0)
2546 if (copy_to_user_sec_ctx(xp
, skb
))
2548 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2550 if (xfrm_mark_put(skb
, &xp
->mark
))
2551 goto nla_put_failure
;
2554 return nlmsg_end(skb
, nlh
);
2558 nlmsg_cancel(skb
, nlh
);
2562 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2564 struct net
*net
= xp_net(xp
);
2565 struct sk_buff
*skb
;
2567 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2571 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2574 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2577 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2579 struct net
*net
= xp_net(xp
);
2580 struct xfrm_userpolicy_info
*p
;
2581 struct xfrm_userpolicy_id
*id
;
2582 struct nlmsghdr
*nlh
;
2583 struct sk_buff
*skb
;
2584 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2587 headlen
= sizeof(*p
);
2588 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2589 len
+= nla_total_size(headlen
);
2590 headlen
= sizeof(*id
);
2592 len
+= userpolicy_type_attrsize();
2593 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2594 len
+= NLMSG_ALIGN(headlen
);
2596 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2600 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2604 p
= nlmsg_data(nlh
);
2605 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2606 struct nlattr
*attr
;
2608 id
= nlmsg_data(nlh
);
2609 memset(id
, 0, sizeof(*id
));
2612 id
->index
= xp
->index
;
2614 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2616 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2623 copy_to_user_policy(xp
, p
, dir
);
2624 if (copy_to_user_tmpl(xp
, skb
) < 0)
2626 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2629 if (xfrm_mark_put(skb
, &xp
->mark
))
2630 goto nla_put_failure
;
2632 nlmsg_end(skb
, nlh
);
2634 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2642 static int xfrm_notify_policy_flush(struct km_event
*c
)
2644 struct net
*net
= c
->net
;
2645 struct nlmsghdr
*nlh
;
2646 struct sk_buff
*skb
;
2648 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2652 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2655 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2658 nlmsg_end(skb
, nlh
);
2660 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2667 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2671 case XFRM_MSG_NEWPOLICY
:
2672 case XFRM_MSG_UPDPOLICY
:
2673 case XFRM_MSG_DELPOLICY
:
2674 return xfrm_notify_policy(xp
, dir
, c
);
2675 case XFRM_MSG_FLUSHPOLICY
:
2676 return xfrm_notify_policy_flush(c
);
2677 case XFRM_MSG_POLEXPIRE
:
2678 return xfrm_exp_policy_notify(xp
, dir
, c
);
2680 printk(KERN_NOTICE
"xfrm_user: Unknown Policy event %d\n",
2688 static inline size_t xfrm_report_msgsize(void)
2690 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2693 static int build_report(struct sk_buff
*skb
, u8 proto
,
2694 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2696 struct xfrm_user_report
*ur
;
2697 struct nlmsghdr
*nlh
;
2699 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2703 ur
= nlmsg_data(nlh
);
2705 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2708 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2710 return nlmsg_end(skb
, nlh
);
2713 nlmsg_cancel(skb
, nlh
);
2717 static int xfrm_send_report(struct net
*net
, u8 proto
,
2718 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2720 struct sk_buff
*skb
;
2722 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2726 if (build_report(skb
, proto
, sel
, addr
) < 0)
2729 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2732 static inline size_t xfrm_mapping_msgsize(void)
2734 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
2737 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
2738 xfrm_address_t
*new_saddr
, __be16 new_sport
)
2740 struct xfrm_user_mapping
*um
;
2741 struct nlmsghdr
*nlh
;
2743 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
2747 um
= nlmsg_data(nlh
);
2749 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
2750 um
->id
.spi
= x
->id
.spi
;
2751 um
->id
.family
= x
->props
.family
;
2752 um
->id
.proto
= x
->id
.proto
;
2753 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
2754 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
2755 um
->new_sport
= new_sport
;
2756 um
->old_sport
= x
->encap
->encap_sport
;
2757 um
->reqid
= x
->props
.reqid
;
2759 return nlmsg_end(skb
, nlh
);
2762 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
2765 struct net
*net
= xs_net(x
);
2766 struct sk_buff
*skb
;
2768 if (x
->id
.proto
!= IPPROTO_ESP
)
2774 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
2778 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
2781 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MAPPING
, GFP_ATOMIC
);
2784 static struct xfrm_mgr netlink_mgr
= {
2786 .notify
= xfrm_send_state_notify
,
2787 .acquire
= xfrm_send_acquire
,
2788 .compile_policy
= xfrm_compile_policy
,
2789 .notify_policy
= xfrm_send_policy_notify
,
2790 .report
= xfrm_send_report
,
2791 .migrate
= xfrm_send_migrate
,
2792 .new_mapping
= xfrm_send_mapping
,
2795 static int __net_init
xfrm_user_net_init(struct net
*net
)
2799 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, XFRMNLGRP_MAX
,
2800 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2803 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
2804 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
2808 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
2811 list_for_each_entry(net
, net_exit_list
, exit_list
)
2812 rcu_assign_pointer(net
->xfrm
.nlsk
, NULL
);
2814 list_for_each_entry(net
, net_exit_list
, exit_list
)
2815 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
2818 static struct pernet_operations xfrm_user_net_ops
= {
2819 .init
= xfrm_user_net_init
,
2820 .exit_batch
= xfrm_user_net_exit
,
2823 static int __init
xfrm_user_init(void)
2827 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2829 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
2832 rv
= xfrm_register_km(&netlink_mgr
);
2834 unregister_pernet_subsys(&xfrm_user_net_ops
);
2838 static void __exit
xfrm_user_exit(void)
2840 xfrm_unregister_km(&netlink_mgr
);
2841 unregister_pernet_subsys(&xfrm_user_net_ops
);
2844 module_init(xfrm_user_init
);
2845 module_exit(xfrm_user_exit
);
2846 MODULE_LICENSE("GPL");
2847 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);