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>
30 #include <asm/uaccess.h>
31 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32 #include <linux/in6.h>
35 static inline int aead_len(struct xfrm_algo_aead
*alg
)
37 return sizeof(*alg
) + ((alg
->alg_key_len
+ 7) / 8);
40 static int verify_one_alg(struct nlattr
**attrs
, enum xfrm_attr_type_t type
)
42 struct nlattr
*rt
= attrs
[type
];
43 struct xfrm_algo
*algp
;
49 if (nla_len(rt
) < xfrm_alg_len(algp
))
62 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
66 static int verify_auth_trunc(struct nlattr
**attrs
)
68 struct nlattr
*rt
= attrs
[XFRMA_ALG_AUTH_TRUNC
];
69 struct xfrm_algo_auth
*algp
;
75 if (nla_len(rt
) < xfrm_alg_auth_len(algp
))
78 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
82 static int verify_aead(struct nlattr
**attrs
)
84 struct nlattr
*rt
= attrs
[XFRMA_ALG_AEAD
];
85 struct xfrm_algo_aead
*algp
;
91 if (nla_len(rt
) < aead_len(algp
))
94 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
98 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
99 xfrm_address_t
**addrp
)
101 struct nlattr
*rt
= attrs
[type
];
104 *addrp
= nla_data(rt
);
107 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
109 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
110 struct xfrm_user_sec_ctx
*uctx
;
116 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
123 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
124 struct nlattr
**attrs
)
134 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
146 switch (p
->id
.proto
) {
148 if ((!attrs
[XFRMA_ALG_AUTH
] &&
149 !attrs
[XFRMA_ALG_AUTH_TRUNC
]) ||
150 attrs
[XFRMA_ALG_AEAD
] ||
151 attrs
[XFRMA_ALG_CRYPT
] ||
152 attrs
[XFRMA_ALG_COMP
] ||
158 if (attrs
[XFRMA_ALG_COMP
])
160 if (!attrs
[XFRMA_ALG_AUTH
] &&
161 !attrs
[XFRMA_ALG_AUTH_TRUNC
] &&
162 !attrs
[XFRMA_ALG_CRYPT
] &&
163 !attrs
[XFRMA_ALG_AEAD
])
165 if ((attrs
[XFRMA_ALG_AUTH
] ||
166 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
167 attrs
[XFRMA_ALG_CRYPT
]) &&
168 attrs
[XFRMA_ALG_AEAD
])
170 if (attrs
[XFRMA_TFCPAD
] &&
171 p
->mode
!= XFRM_MODE_TUNNEL
)
176 if (!attrs
[XFRMA_ALG_COMP
] ||
177 attrs
[XFRMA_ALG_AEAD
] ||
178 attrs
[XFRMA_ALG_AUTH
] ||
179 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
180 attrs
[XFRMA_ALG_CRYPT
] ||
185 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
186 case IPPROTO_DSTOPTS
:
187 case IPPROTO_ROUTING
:
188 if (attrs
[XFRMA_ALG_COMP
] ||
189 attrs
[XFRMA_ALG_AUTH
] ||
190 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
191 attrs
[XFRMA_ALG_AEAD
] ||
192 attrs
[XFRMA_ALG_CRYPT
] ||
193 attrs
[XFRMA_ENCAP
] ||
194 attrs
[XFRMA_SEC_CTX
] ||
195 attrs
[XFRMA_TFCPAD
] ||
196 !attrs
[XFRMA_COADDR
])
205 if ((err
= verify_aead(attrs
)))
207 if ((err
= verify_auth_trunc(attrs
)))
209 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
211 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
213 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
215 if ((err
= verify_sec_ctx_len(attrs
)))
220 case XFRM_MODE_TRANSPORT
:
221 case XFRM_MODE_TUNNEL
:
222 case XFRM_MODE_ROUTEOPTIMIZATION
:
236 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
237 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
240 struct xfrm_algo
*p
, *ualg
;
241 struct xfrm_algo_desc
*algo
;
246 ualg
= nla_data(rta
);
248 algo
= get_byname(ualg
->alg_name
, 1);
251 *props
= algo
->desc
.sadb_alg_id
;
253 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
257 strcpy(p
->alg_name
, algo
->name
);
262 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
265 struct xfrm_algo
*ualg
;
266 struct xfrm_algo_auth
*p
;
267 struct xfrm_algo_desc
*algo
;
272 ualg
= nla_data(rta
);
274 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
277 *props
= algo
->desc
.sadb_alg_id
;
279 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
283 strcpy(p
->alg_name
, algo
->name
);
284 p
->alg_key_len
= ualg
->alg_key_len
;
285 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
286 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
292 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
295 struct xfrm_algo_auth
*p
, *ualg
;
296 struct xfrm_algo_desc
*algo
;
301 ualg
= nla_data(rta
);
303 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
306 if ((ualg
->alg_trunc_len
/ 8) > MAX_AH_AUTH_LEN
||
307 ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
309 *props
= algo
->desc
.sadb_alg_id
;
311 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
315 strcpy(p
->alg_name
, algo
->name
);
316 if (!p
->alg_trunc_len
)
317 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
323 static int attach_aead(struct xfrm_algo_aead
**algpp
, u8
*props
,
326 struct xfrm_algo_aead
*p
, *ualg
;
327 struct xfrm_algo_desc
*algo
;
332 ualg
= nla_data(rta
);
334 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
337 *props
= algo
->desc
.sadb_alg_id
;
339 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
343 strcpy(p
->alg_name
, algo
->name
);
348 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
353 len
+= sizeof(struct xfrm_user_sec_ctx
);
354 len
+= xfrm_ctx
->ctx_len
;
359 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
361 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
362 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
363 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
364 x
->props
.mode
= p
->mode
;
365 x
->props
.replay_window
= p
->replay_window
;
366 x
->props
.reqid
= p
->reqid
;
367 x
->props
.family
= p
->family
;
368 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
369 x
->props
.flags
= p
->flags
;
371 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
372 x
->sel
.family
= p
->family
;
376 * someday when pfkey also has support, we could have the code
377 * somehow made shareable and move it to xfrm_state.c - JHS
380 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
)
382 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
383 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
384 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
385 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
388 struct xfrm_replay_state
*replay
;
389 replay
= nla_data(rp
);
390 memcpy(&x
->replay
, replay
, sizeof(*replay
));
391 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
395 struct xfrm_lifetime_cur
*ltime
;
396 ltime
= nla_data(lt
);
397 x
->curlft
.bytes
= ltime
->bytes
;
398 x
->curlft
.packets
= ltime
->packets
;
399 x
->curlft
.add_time
= ltime
->add_time
;
400 x
->curlft
.use_time
= ltime
->use_time
;
404 x
->replay_maxage
= nla_get_u32(et
);
407 x
->replay_maxdiff
= nla_get_u32(rt
);
410 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
411 struct xfrm_usersa_info
*p
,
412 struct nlattr
**attrs
,
415 struct xfrm_state
*x
= xfrm_state_alloc(net
);
421 copy_from_user_state(x
, p
);
423 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
424 attrs
[XFRMA_ALG_AEAD
])))
426 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
427 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
429 if (!x
->props
.aalgo
) {
430 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
431 attrs
[XFRMA_ALG_AUTH
])))
434 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
435 xfrm_ealg_get_byname
,
436 attrs
[XFRMA_ALG_CRYPT
])))
438 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
439 xfrm_calg_get_byname
,
440 attrs
[XFRMA_ALG_COMP
])))
443 if (attrs
[XFRMA_ENCAP
]) {
444 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
445 sizeof(*x
->encap
), GFP_KERNEL
);
446 if (x
->encap
== NULL
)
450 if (attrs
[XFRMA_TFCPAD
])
451 x
->tfcpad
= nla_get_u32(attrs
[XFRMA_TFCPAD
]);
453 if (attrs
[XFRMA_COADDR
]) {
454 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
455 sizeof(*x
->coaddr
), GFP_KERNEL
);
456 if (x
->coaddr
== NULL
)
460 xfrm_mark_get(attrs
, &x
->mark
);
462 err
= xfrm_init_state(x
);
466 if (attrs
[XFRMA_SEC_CTX
] &&
467 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
471 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
472 /* sysctl_xfrm_aevent_etime is in 100ms units */
473 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
474 x
->preplay
.bitmap
= 0;
475 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
476 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
478 /* override default values from above */
480 xfrm_update_ae_params(x
, attrs
);
485 x
->km
.state
= XFRM_STATE_DEAD
;
492 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
493 struct nlattr
**attrs
)
495 struct net
*net
= sock_net(skb
->sk
);
496 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
497 struct xfrm_state
*x
;
500 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
501 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
502 u32 sid
= NETLINK_CB(skb
).sid
;
504 err
= verify_newsa_info(p
, attrs
);
508 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
513 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
514 err
= xfrm_state_add(x
);
516 err
= xfrm_state_update(x
);
518 xfrm_audit_state_add(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
521 x
->km
.state
= XFRM_STATE_DEAD
;
526 c
.seq
= nlh
->nlmsg_seq
;
527 c
.pid
= nlh
->nlmsg_pid
;
528 c
.event
= nlh
->nlmsg_type
;
530 km_state_notify(x
, &c
);
536 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
537 struct xfrm_usersa_id
*p
,
538 struct nlattr
**attrs
,
541 struct xfrm_state
*x
= NULL
;
544 u32 mark
= xfrm_mark_get(attrs
, &m
);
546 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
548 x
= xfrm_state_lookup(net
, mark
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
550 xfrm_address_t
*saddr
= NULL
;
552 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
559 x
= xfrm_state_lookup_byaddr(net
, mark
,
561 p
->proto
, p
->family
);
570 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
571 struct nlattr
**attrs
)
573 struct net
*net
= sock_net(skb
->sk
);
574 struct xfrm_state
*x
;
577 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
578 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
579 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
580 u32 sid
= NETLINK_CB(skb
).sid
;
582 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
586 if ((err
= security_xfrm_state_delete(x
)) != 0)
589 if (xfrm_state_kern(x
)) {
594 err
= xfrm_state_delete(x
);
599 c
.seq
= nlh
->nlmsg_seq
;
600 c
.pid
= nlh
->nlmsg_pid
;
601 c
.event
= nlh
->nlmsg_type
;
602 km_state_notify(x
, &c
);
605 xfrm_audit_state_delete(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
610 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
612 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
613 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
614 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
615 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
616 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
617 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
618 p
->mode
= x
->props
.mode
;
619 p
->replay_window
= x
->props
.replay_window
;
620 p
->reqid
= x
->props
.reqid
;
621 p
->family
= x
->props
.family
;
622 p
->flags
= x
->props
.flags
;
626 struct xfrm_dump_info
{
627 struct sk_buff
*in_skb
;
628 struct sk_buff
*out_skb
;
633 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
635 struct xfrm_user_sec_ctx
*uctx
;
637 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
639 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
643 uctx
= nla_data(attr
);
644 uctx
->exttype
= XFRMA_SEC_CTX
;
645 uctx
->len
= ctx_size
;
646 uctx
->ctx_doi
= s
->ctx_doi
;
647 uctx
->ctx_alg
= s
->ctx_alg
;
648 uctx
->ctx_len
= s
->ctx_len
;
649 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
654 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
656 struct xfrm_algo
*algo
;
659 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
660 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
664 algo
= nla_data(nla
);
665 strcpy(algo
->alg_name
, auth
->alg_name
);
666 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
667 algo
->alg_key_len
= auth
->alg_key_len
;
672 /* Don't change this without updating xfrm_sa_len! */
673 static int copy_to_user_state_extra(struct xfrm_state
*x
,
674 struct xfrm_usersa_info
*p
,
677 copy_to_user_state(x
, p
);
680 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
683 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
686 NLA_PUT(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
688 if (copy_to_user_auth(x
->aalg
, skb
))
689 goto nla_put_failure
;
691 NLA_PUT(skb
, XFRMA_ALG_AUTH_TRUNC
,
692 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
695 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
697 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
700 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
703 NLA_PUT_U32(skb
, XFRMA_TFCPAD
, x
->tfcpad
);
705 if (xfrm_mark_put(skb
, &x
->mark
))
706 goto nla_put_failure
;
708 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
709 goto nla_put_failure
;
717 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
719 struct xfrm_dump_info
*sp
= ptr
;
720 struct sk_buff
*in_skb
= sp
->in_skb
;
721 struct sk_buff
*skb
= sp
->out_skb
;
722 struct xfrm_usersa_info
*p
;
723 struct nlmsghdr
*nlh
;
726 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
727 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
733 err
= copy_to_user_state_extra(x
, p
, skb
);
735 goto nla_put_failure
;
741 nlmsg_cancel(skb
, nlh
);
745 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
747 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
748 xfrm_state_walk_done(walk
);
752 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
754 struct net
*net
= sock_net(skb
->sk
);
755 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
756 struct xfrm_dump_info info
;
758 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
759 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
761 info
.in_skb
= cb
->skb
;
763 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
764 info
.nlmsg_flags
= NLM_F_MULTI
;
768 xfrm_state_walk_init(walk
, 0);
771 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
776 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
777 struct xfrm_state
*x
, u32 seq
)
779 struct xfrm_dump_info info
;
782 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
784 return ERR_PTR(-ENOMEM
);
786 info
.in_skb
= in_skb
;
788 info
.nlmsg_seq
= seq
;
789 info
.nlmsg_flags
= 0;
791 if (dump_one_state(x
, 0, &info
)) {
799 static inline size_t xfrm_spdinfo_msgsize(void)
801 return NLMSG_ALIGN(4)
802 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
803 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
806 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
807 u32 pid
, u32 seq
, u32 flags
)
809 struct xfrmk_spdinfo si
;
810 struct xfrmu_spdinfo spc
;
811 struct xfrmu_spdhinfo sph
;
812 struct nlmsghdr
*nlh
;
815 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
816 if (nlh
== NULL
) /* shouldnt really happen ... */
821 xfrm_spd_getinfo(net
, &si
);
822 spc
.incnt
= si
.incnt
;
823 spc
.outcnt
= si
.outcnt
;
824 spc
.fwdcnt
= si
.fwdcnt
;
825 spc
.inscnt
= si
.inscnt
;
826 spc
.outscnt
= si
.outscnt
;
827 spc
.fwdscnt
= si
.fwdscnt
;
828 sph
.spdhcnt
= si
.spdhcnt
;
829 sph
.spdhmcnt
= si
.spdhmcnt
;
831 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
832 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
834 return nlmsg_end(skb
, nlh
);
837 nlmsg_cancel(skb
, nlh
);
841 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
842 struct nlattr
**attrs
)
844 struct net
*net
= sock_net(skb
->sk
);
845 struct sk_buff
*r_skb
;
846 u32
*flags
= nlmsg_data(nlh
);
847 u32 spid
= NETLINK_CB(skb
).pid
;
848 u32 seq
= nlh
->nlmsg_seq
;
850 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
854 if (build_spdinfo(r_skb
, net
, spid
, seq
, *flags
) < 0)
857 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, spid
);
860 static inline size_t xfrm_sadinfo_msgsize(void)
862 return NLMSG_ALIGN(4)
863 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
864 + nla_total_size(4); /* XFRMA_SAD_CNT */
867 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
868 u32 pid
, u32 seq
, u32 flags
)
870 struct xfrmk_sadinfo si
;
871 struct xfrmu_sadhinfo sh
;
872 struct nlmsghdr
*nlh
;
875 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
876 if (nlh
== NULL
) /* shouldnt really happen ... */
881 xfrm_sad_getinfo(net
, &si
);
883 sh
.sadhmcnt
= si
.sadhmcnt
;
884 sh
.sadhcnt
= si
.sadhcnt
;
886 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
887 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
889 return nlmsg_end(skb
, nlh
);
892 nlmsg_cancel(skb
, nlh
);
896 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
897 struct nlattr
**attrs
)
899 struct net
*net
= sock_net(skb
->sk
);
900 struct sk_buff
*r_skb
;
901 u32
*flags
= nlmsg_data(nlh
);
902 u32 spid
= NETLINK_CB(skb
).pid
;
903 u32 seq
= nlh
->nlmsg_seq
;
905 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
909 if (build_sadinfo(r_skb
, net
, spid
, seq
, *flags
) < 0)
912 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, spid
);
915 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
916 struct nlattr
**attrs
)
918 struct net
*net
= sock_net(skb
->sk
);
919 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
920 struct xfrm_state
*x
;
921 struct sk_buff
*resp_skb
;
924 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
928 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
929 if (IS_ERR(resp_skb
)) {
930 err
= PTR_ERR(resp_skb
);
932 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).pid
);
939 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
941 switch (p
->info
.id
.proto
) {
947 /* IPCOMP spi is 16-bits. */
948 if (p
->max
>= 0x10000)
962 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
963 struct nlattr
**attrs
)
965 struct net
*net
= sock_net(skb
->sk
);
966 struct xfrm_state
*x
;
967 struct xfrm_userspi_info
*p
;
968 struct sk_buff
*resp_skb
;
969 xfrm_address_t
*daddr
;
976 err
= verify_userspi_info(p
);
980 family
= p
->info
.family
;
981 daddr
= &p
->info
.id
.daddr
;
985 mark
= xfrm_mark_get(attrs
, &m
);
987 x
= xfrm_find_acq_byseq(net
, mark
, p
->info
.seq
);
988 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
995 x
= xfrm_find_acq(net
, &m
, p
->info
.mode
, p
->info
.reqid
,
996 p
->info
.id
.proto
, daddr
,
1003 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
1007 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1008 if (IS_ERR(resp_skb
)) {
1009 err
= PTR_ERR(resp_skb
);
1013 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).pid
);
1021 static int verify_policy_dir(u8 dir
)
1024 case XFRM_POLICY_IN
:
1025 case XFRM_POLICY_OUT
:
1026 case XFRM_POLICY_FWD
:
1036 static int verify_policy_type(u8 type
)
1039 case XFRM_POLICY_TYPE_MAIN
:
1040 #ifdef CONFIG_XFRM_SUB_POLICY
1041 case XFRM_POLICY_TYPE_SUB
:
1052 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1055 case XFRM_SHARE_ANY
:
1056 case XFRM_SHARE_SESSION
:
1057 case XFRM_SHARE_USER
:
1058 case XFRM_SHARE_UNIQUE
:
1065 switch (p
->action
) {
1066 case XFRM_POLICY_ALLOW
:
1067 case XFRM_POLICY_BLOCK
:
1074 switch (p
->sel
.family
) {
1079 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1082 return -EAFNOSUPPORT
;
1089 return verify_policy_dir(p
->dir
);
1092 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1094 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1095 struct xfrm_user_sec_ctx
*uctx
;
1100 uctx
= nla_data(rt
);
1101 return security_xfrm_policy_alloc(&pol
->security
, uctx
);
1104 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1110 for (i
= 0; i
< nr
; i
++, ut
++) {
1111 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1113 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1114 memcpy(&t
->saddr
, &ut
->saddr
,
1115 sizeof(xfrm_address_t
));
1116 t
->reqid
= ut
->reqid
;
1118 t
->share
= ut
->share
;
1119 t
->optional
= ut
->optional
;
1120 t
->aalgos
= ut
->aalgos
;
1121 t
->ealgos
= ut
->ealgos
;
1122 t
->calgos
= ut
->calgos
;
1123 /* If all masks are ~0, then we allow all algorithms. */
1124 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1125 t
->encap_family
= ut
->family
;
1129 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1133 if (nr
> XFRM_MAX_DEPTH
)
1136 for (i
= 0; i
< nr
; i
++) {
1137 /* We never validated the ut->family value, so many
1138 * applications simply leave it at zero. The check was
1139 * never made and ut->family was ignored because all
1140 * templates could be assumed to have the same family as
1141 * the policy itself. Now that we will have ipv4-in-ipv6
1142 * and ipv6-in-ipv4 tunnels, this is no longer true.
1145 ut
[i
].family
= family
;
1147 switch (ut
[i
].family
) {
1150 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1162 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1164 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1169 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1170 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1173 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1177 copy_templates(pol
, utmpl
, nr
);
1182 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1184 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1185 struct xfrm_userpolicy_type
*upt
;
1186 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1194 err
= verify_policy_type(type
);
1202 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1204 xp
->priority
= p
->priority
;
1205 xp
->index
= p
->index
;
1206 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1207 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1208 xp
->action
= p
->action
;
1209 xp
->flags
= p
->flags
;
1210 xp
->family
= p
->sel
.family
;
1211 /* XXX xp->share = p->share; */
1214 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1216 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1217 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1218 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1219 p
->priority
= xp
->priority
;
1220 p
->index
= xp
->index
;
1221 p
->sel
.family
= xp
->family
;
1223 p
->action
= xp
->action
;
1224 p
->flags
= xp
->flags
;
1225 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1228 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1230 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1238 copy_from_user_policy(xp
, p
);
1240 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1244 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1245 err
= copy_from_user_sec_ctx(xp
, attrs
);
1249 xfrm_mark_get(attrs
, &xp
->mark
);
1255 xfrm_policy_destroy(xp
);
1259 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1260 struct nlattr
**attrs
)
1262 struct net
*net
= sock_net(skb
->sk
);
1263 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1264 struct xfrm_policy
*xp
;
1268 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1269 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
1270 u32 sid
= NETLINK_CB(skb
).sid
;
1272 err
= verify_newpolicy_info(p
);
1275 err
= verify_sec_ctx_len(attrs
);
1279 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1283 /* shouldnt excl be based on nlh flags??
1284 * Aha! this is anti-netlink really i.e more pfkey derived
1285 * in netlink excl is a flag and you wouldnt need
1286 * a type XFRM_MSG_UPDPOLICY - JHS */
1287 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1288 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1289 xfrm_audit_policy_add(xp
, err
? 0 : 1, loginuid
, sessionid
, sid
);
1292 security_xfrm_policy_free(xp
->security
);
1297 c
.event
= nlh
->nlmsg_type
;
1298 c
.seq
= nlh
->nlmsg_seq
;
1299 c
.pid
= nlh
->nlmsg_pid
;
1300 km_policy_notify(xp
, p
->dir
, &c
);
1307 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1309 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1312 if (xp
->xfrm_nr
== 0)
1315 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1316 struct xfrm_user_tmpl
*up
= &vec
[i
];
1317 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1319 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1320 up
->family
= kp
->encap_family
;
1321 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1322 up
->reqid
= kp
->reqid
;
1323 up
->mode
= kp
->mode
;
1324 up
->share
= kp
->share
;
1325 up
->optional
= kp
->optional
;
1326 up
->aalgos
= kp
->aalgos
;
1327 up
->ealgos
= kp
->ealgos
;
1328 up
->calgos
= kp
->calgos
;
1331 return nla_put(skb
, XFRMA_TMPL
,
1332 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1335 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1338 return copy_sec_ctx(x
->security
, skb
);
1343 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1346 return copy_sec_ctx(xp
->security
, skb
);
1350 static inline size_t userpolicy_type_attrsize(void)
1352 #ifdef CONFIG_XFRM_SUB_POLICY
1353 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1359 #ifdef CONFIG_XFRM_SUB_POLICY
1360 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1362 struct xfrm_userpolicy_type upt
= {
1366 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1370 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1376 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1378 struct xfrm_dump_info
*sp
= ptr
;
1379 struct xfrm_userpolicy_info
*p
;
1380 struct sk_buff
*in_skb
= sp
->in_skb
;
1381 struct sk_buff
*skb
= sp
->out_skb
;
1382 struct nlmsghdr
*nlh
;
1384 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1385 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1389 p
= nlmsg_data(nlh
);
1390 copy_to_user_policy(xp
, p
, dir
);
1391 if (copy_to_user_tmpl(xp
, skb
) < 0)
1393 if (copy_to_user_sec_ctx(xp
, skb
))
1395 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1397 if (xfrm_mark_put(skb
, &xp
->mark
))
1398 goto nla_put_failure
;
1400 nlmsg_end(skb
, nlh
);
1405 nlmsg_cancel(skb
, nlh
);
1409 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1411 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1413 xfrm_policy_walk_done(walk
);
1417 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1419 struct net
*net
= sock_net(skb
->sk
);
1420 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1421 struct xfrm_dump_info info
;
1423 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1424 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1426 info
.in_skb
= cb
->skb
;
1428 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1429 info
.nlmsg_flags
= NLM_F_MULTI
;
1433 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1436 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1441 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1442 struct xfrm_policy
*xp
,
1445 struct xfrm_dump_info info
;
1446 struct sk_buff
*skb
;
1448 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1450 return ERR_PTR(-ENOMEM
);
1452 info
.in_skb
= in_skb
;
1454 info
.nlmsg_seq
= seq
;
1455 info
.nlmsg_flags
= 0;
1457 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1465 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1466 struct nlattr
**attrs
)
1468 struct net
*net
= sock_net(skb
->sk
);
1469 struct xfrm_policy
*xp
;
1470 struct xfrm_userpolicy_id
*p
;
1471 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1476 u32 mark
= xfrm_mark_get(attrs
, &m
);
1478 p
= nlmsg_data(nlh
);
1479 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1481 err
= copy_from_user_policy_type(&type
, attrs
);
1485 err
= verify_policy_dir(p
->dir
);
1490 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, delete, &err
);
1492 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1493 struct xfrm_sec_ctx
*ctx
;
1495 err
= verify_sec_ctx_len(attrs
);
1501 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1503 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1507 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
, &p
->sel
,
1509 security_xfrm_policy_free(ctx
);
1515 struct sk_buff
*resp_skb
;
1517 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1518 if (IS_ERR(resp_skb
)) {
1519 err
= PTR_ERR(resp_skb
);
1521 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1522 NETLINK_CB(skb
).pid
);
1525 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1526 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
1527 u32 sid
= NETLINK_CB(skb
).sid
;
1529 xfrm_audit_policy_delete(xp
, err
? 0 : 1, loginuid
, sessionid
,
1535 c
.data
.byid
= p
->index
;
1536 c
.event
= nlh
->nlmsg_type
;
1537 c
.seq
= nlh
->nlmsg_seq
;
1538 c
.pid
= nlh
->nlmsg_pid
;
1539 km_policy_notify(xp
, p
->dir
, &c
);
1547 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1548 struct nlattr
**attrs
)
1550 struct net
*net
= sock_net(skb
->sk
);
1552 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1553 struct xfrm_audit audit_info
;
1556 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1557 audit_info
.sessionid
= NETLINK_CB(skb
).sessionid
;
1558 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1559 err
= xfrm_state_flush(net
, p
->proto
, &audit_info
);
1561 if (err
== -ESRCH
) /* empty table */
1565 c
.data
.proto
= p
->proto
;
1566 c
.event
= nlh
->nlmsg_type
;
1567 c
.seq
= nlh
->nlmsg_seq
;
1568 c
.pid
= nlh
->nlmsg_pid
;
1570 km_state_notify(NULL
, &c
);
1575 static inline size_t xfrm_aevent_msgsize(void)
1577 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1578 + nla_total_size(sizeof(struct xfrm_replay_state
))
1579 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1580 + nla_total_size(sizeof(struct xfrm_mark
))
1581 + nla_total_size(4) /* XFRM_AE_RTHR */
1582 + nla_total_size(4); /* XFRM_AE_ETHR */
1585 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1587 struct xfrm_aevent_id
*id
;
1588 struct nlmsghdr
*nlh
;
1590 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1594 id
= nlmsg_data(nlh
);
1595 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1596 id
->sa_id
.spi
= x
->id
.spi
;
1597 id
->sa_id
.family
= x
->props
.family
;
1598 id
->sa_id
.proto
= x
->id
.proto
;
1599 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1600 id
->reqid
= x
->props
.reqid
;
1601 id
->flags
= c
->data
.aevent
;
1603 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1604 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1606 if (id
->flags
& XFRM_AE_RTHR
)
1607 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1609 if (id
->flags
& XFRM_AE_ETHR
)
1610 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1611 x
->replay_maxage
* 10 / HZ
);
1613 if (xfrm_mark_put(skb
, &x
->mark
))
1614 goto nla_put_failure
;
1616 return nlmsg_end(skb
, nlh
);
1619 nlmsg_cancel(skb
, nlh
);
1623 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1624 struct nlattr
**attrs
)
1626 struct net
*net
= sock_net(skb
->sk
);
1627 struct xfrm_state
*x
;
1628 struct sk_buff
*r_skb
;
1633 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1634 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1636 r_skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1640 mark
= xfrm_mark_get(attrs
, &m
);
1642 x
= xfrm_state_lookup(net
, mark
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1649 * XXX: is this lock really needed - none of the other
1650 * gets lock (the concern is things getting updated
1651 * while we are still reading) - jhs
1653 spin_lock_bh(&x
->lock
);
1654 c
.data
.aevent
= p
->flags
;
1655 c
.seq
= nlh
->nlmsg_seq
;
1656 c
.pid
= nlh
->nlmsg_pid
;
1658 if (build_aevent(r_skb
, x
, &c
) < 0)
1660 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).pid
);
1661 spin_unlock_bh(&x
->lock
);
1666 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1667 struct nlattr
**attrs
)
1669 struct net
*net
= sock_net(skb
->sk
);
1670 struct xfrm_state
*x
;
1675 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1676 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1677 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1682 /* pedantic mode - thou shalt sayeth replaceth */
1683 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1686 mark
= xfrm_mark_get(attrs
, &m
);
1688 x
= xfrm_state_lookup(net
, mark
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1692 if (x
->km
.state
!= XFRM_STATE_VALID
)
1695 spin_lock_bh(&x
->lock
);
1696 xfrm_update_ae_params(x
, attrs
);
1697 spin_unlock_bh(&x
->lock
);
1699 c
.event
= nlh
->nlmsg_type
;
1700 c
.seq
= nlh
->nlmsg_seq
;
1701 c
.pid
= nlh
->nlmsg_pid
;
1702 c
.data
.aevent
= XFRM_AE_CU
;
1703 km_state_notify(x
, &c
);
1710 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1711 struct nlattr
**attrs
)
1713 struct net
*net
= sock_net(skb
->sk
);
1715 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1717 struct xfrm_audit audit_info
;
1719 err
= copy_from_user_policy_type(&type
, attrs
);
1723 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1724 audit_info
.sessionid
= NETLINK_CB(skb
).sessionid
;
1725 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1726 err
= xfrm_policy_flush(net
, type
, &audit_info
);
1728 if (err
== -ESRCH
) /* empty table */
1734 c
.event
= nlh
->nlmsg_type
;
1735 c
.seq
= nlh
->nlmsg_seq
;
1736 c
.pid
= nlh
->nlmsg_pid
;
1738 km_policy_notify(NULL
, 0, &c
);
1742 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1743 struct nlattr
**attrs
)
1745 struct net
*net
= sock_net(skb
->sk
);
1746 struct xfrm_policy
*xp
;
1747 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1748 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1749 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1752 u32 mark
= xfrm_mark_get(attrs
, &m
);
1754 err
= copy_from_user_policy_type(&type
, attrs
);
1758 err
= verify_policy_dir(p
->dir
);
1763 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, 0, &err
);
1765 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1766 struct xfrm_sec_ctx
*ctx
;
1768 err
= verify_sec_ctx_len(attrs
);
1774 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1776 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1780 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
,
1781 &p
->sel
, ctx
, 0, &err
);
1782 security_xfrm_policy_free(ctx
);
1787 if (unlikely(xp
->walk
.dead
))
1792 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1793 uid_t sessionid
= NETLINK_CB(skb
).sessionid
;
1794 u32 sid
= NETLINK_CB(skb
).sid
;
1795 xfrm_policy_delete(xp
, p
->dir
);
1796 xfrm_audit_policy_delete(xp
, 1, loginuid
, sessionid
, sid
);
1799 // reset the timers here?
1800 WARN(1, "Dont know what to do with soft policy expire\n");
1802 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1809 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1810 struct nlattr
**attrs
)
1812 struct net
*net
= sock_net(skb
->sk
);
1813 struct xfrm_state
*x
;
1815 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1816 struct xfrm_usersa_info
*p
= &ue
->state
;
1818 u32 mark
= xfrm_mark_get(attrs
, &m
);
1820 x
= xfrm_state_lookup(net
, mark
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1826 spin_lock_bh(&x
->lock
);
1828 if (x
->km
.state
!= XFRM_STATE_VALID
)
1830 km_state_expired(x
, ue
->hard
, current
->pid
);
1833 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1834 uid_t sessionid
= NETLINK_CB(skb
).sessionid
;
1835 u32 sid
= NETLINK_CB(skb
).sid
;
1836 __xfrm_state_delete(x
);
1837 xfrm_audit_state_delete(x
, 1, loginuid
, sessionid
, sid
);
1841 spin_unlock_bh(&x
->lock
);
1846 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1847 struct nlattr
**attrs
)
1849 struct net
*net
= sock_net(skb
->sk
);
1850 struct xfrm_policy
*xp
;
1851 struct xfrm_user_tmpl
*ut
;
1853 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1854 struct xfrm_mark mark
;
1856 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1857 struct xfrm_state
*x
= xfrm_state_alloc(net
);
1863 xfrm_mark_get(attrs
, &mark
);
1865 err
= verify_newpolicy_info(&ua
->policy
);
1870 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
1874 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1875 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1876 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1877 xp
->mark
.m
= x
->mark
.m
= mark
.m
;
1878 xp
->mark
.v
= x
->mark
.v
= mark
.v
;
1880 /* extract the templates and for each call km_key */
1881 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1882 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1883 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1884 x
->props
.mode
= t
->mode
;
1885 x
->props
.reqid
= t
->reqid
;
1886 x
->props
.family
= ut
->family
;
1887 t
->aalgos
= ua
->aalgos
;
1888 t
->ealgos
= ua
->ealgos
;
1889 t
->calgos
= ua
->calgos
;
1890 err
= km_query(x
, t
, xp
);
1900 WARN(1, "BAD policy passed\n");
1907 #ifdef CONFIG_XFRM_MIGRATE
1908 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1909 struct xfrm_kmaddress
*k
,
1910 struct nlattr
**attrs
, int *num
)
1912 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
1913 struct xfrm_user_migrate
*um
;
1917 struct xfrm_user_kmaddress
*uk
;
1919 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
1920 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
1921 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
1922 k
->family
= uk
->family
;
1923 k
->reserved
= uk
->reserved
;
1927 num_migrate
= nla_len(rt
) / sizeof(*um
);
1929 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
1932 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
1933 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
1934 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
1935 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
1936 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
1938 ma
->proto
= um
->proto
;
1939 ma
->mode
= um
->mode
;
1940 ma
->reqid
= um
->reqid
;
1942 ma
->old_family
= um
->old_family
;
1943 ma
->new_family
= um
->new_family
;
1950 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1951 struct nlattr
**attrs
)
1953 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
1954 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
1955 struct xfrm_kmaddress km
, *kmp
;
1960 if (attrs
[XFRMA_MIGRATE
] == NULL
)
1963 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
1965 err
= copy_from_user_policy_type(&type
, attrs
);
1969 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
1976 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
);
1981 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1982 struct nlattr
**attrs
)
1984 return -ENOPROTOOPT
;
1988 #ifdef CONFIG_XFRM_MIGRATE
1989 static int copy_to_user_migrate(struct xfrm_migrate
*m
, struct sk_buff
*skb
)
1991 struct xfrm_user_migrate um
;
1993 memset(&um
, 0, sizeof(um
));
1994 um
.proto
= m
->proto
;
1996 um
.reqid
= m
->reqid
;
1997 um
.old_family
= m
->old_family
;
1998 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
1999 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
2000 um
.new_family
= m
->new_family
;
2001 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
2002 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
2004 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
2007 static int copy_to_user_kmaddress(struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
2009 struct xfrm_user_kmaddress uk
;
2011 memset(&uk
, 0, sizeof(uk
));
2012 uk
.family
= k
->family
;
2013 uk
.reserved
= k
->reserved
;
2014 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
2015 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
2017 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
2020 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
)
2022 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
2023 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
2024 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
2025 + userpolicy_type_attrsize();
2028 static int build_migrate(struct sk_buff
*skb
, struct xfrm_migrate
*m
,
2029 int num_migrate
, struct xfrm_kmaddress
*k
,
2030 struct xfrm_selector
*sel
, u8 dir
, u8 type
)
2032 struct xfrm_migrate
*mp
;
2033 struct xfrm_userpolicy_id
*pol_id
;
2034 struct nlmsghdr
*nlh
;
2037 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
2041 pol_id
= nlmsg_data(nlh
);
2042 /* copy data from selector, dir, and type to the pol_id */
2043 memset(pol_id
, 0, sizeof(*pol_id
));
2044 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
2047 if (k
!= NULL
&& (copy_to_user_kmaddress(k
, skb
) < 0))
2050 if (copy_to_user_policy_type(type
, skb
) < 0)
2053 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2054 if (copy_to_user_migrate(mp
, skb
) < 0)
2058 return nlmsg_end(skb
, nlh
);
2060 nlmsg_cancel(skb
, nlh
);
2064 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2065 struct xfrm_migrate
*m
, int num_migrate
,
2066 struct xfrm_kmaddress
*k
)
2068 struct net
*net
= &init_net
;
2069 struct sk_buff
*skb
;
2071 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
), GFP_ATOMIC
);
2076 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, dir
, type
) < 0)
2079 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
2082 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2083 struct xfrm_migrate
*m
, int num_migrate
,
2084 struct xfrm_kmaddress
*k
)
2086 return -ENOPROTOOPT
;
2090 #define XMSGSIZE(type) sizeof(struct type)
2092 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2093 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2094 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2095 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2096 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2097 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2098 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2099 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2100 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2101 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2102 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2103 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2104 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2105 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2106 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2107 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2108 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2109 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2110 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2111 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2112 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2117 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2118 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2119 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2120 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2121 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2122 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2123 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2124 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2125 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2126 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2127 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2128 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2129 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2130 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2131 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2132 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2133 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2134 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2135 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2136 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2137 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2138 [XFRMA_MARK
] = { .len
= sizeof(struct xfrm_mark
) },
2139 [XFRMA_TFCPAD
] = { .type
= NLA_U32
},
2142 static struct xfrm_link
{
2143 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2144 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2145 int (*done
)(struct netlink_callback
*);
2146 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2147 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2148 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2149 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2150 .dump
= xfrm_dump_sa
,
2151 .done
= xfrm_dump_sa_done
},
2152 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2153 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2154 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2155 .dump
= xfrm_dump_policy
,
2156 .done
= xfrm_dump_policy_done
},
2157 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2158 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2159 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2160 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2161 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2162 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2163 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2164 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2165 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2166 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2167 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2168 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2169 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2172 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2174 struct net
*net
= sock_net(skb
->sk
);
2175 struct nlattr
*attrs
[XFRMA_MAX
+1];
2176 struct xfrm_link
*link
;
2179 type
= nlh
->nlmsg_type
;
2180 if (type
> XFRM_MSG_MAX
)
2183 type
-= XFRM_MSG_BASE
;
2184 link
= &xfrm_dispatch
[type
];
2186 /* All operations require privileges, even GET */
2187 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
2190 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2191 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2192 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2193 if (link
->dump
== NULL
)
2196 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, link
->dump
, link
->done
);
2199 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
2204 if (link
->doit
== NULL
)
2207 return link
->doit(skb
, nlh
, attrs
);
2210 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2212 mutex_lock(&xfrm_cfg_mutex
);
2213 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2214 mutex_unlock(&xfrm_cfg_mutex
);
2217 static inline size_t xfrm_expire_msgsize(void)
2219 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
))
2220 + nla_total_size(sizeof(struct xfrm_mark
));
2223 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
2225 struct xfrm_user_expire
*ue
;
2226 struct nlmsghdr
*nlh
;
2228 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2232 ue
= nlmsg_data(nlh
);
2233 copy_to_user_state(x
, &ue
->state
);
2234 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2236 if (xfrm_mark_put(skb
, &x
->mark
))
2237 goto nla_put_failure
;
2239 return nlmsg_end(skb
, nlh
);
2245 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2247 struct net
*net
= xs_net(x
);
2248 struct sk_buff
*skb
;
2250 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2254 if (build_expire(skb
, x
, c
) < 0) {
2259 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2262 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2264 struct net
*net
= xs_net(x
);
2265 struct sk_buff
*skb
;
2267 skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
2271 if (build_aevent(skb
, x
, c
) < 0)
2274 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
2277 static int xfrm_notify_sa_flush(struct km_event
*c
)
2279 struct net
*net
= c
->net
;
2280 struct xfrm_usersa_flush
*p
;
2281 struct nlmsghdr
*nlh
;
2282 struct sk_buff
*skb
;
2283 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2285 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2289 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2295 p
= nlmsg_data(nlh
);
2296 p
->proto
= c
->data
.proto
;
2298 nlmsg_end(skb
, nlh
);
2300 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2303 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2307 l
+= nla_total_size(aead_len(x
->aead
));
2309 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2310 (x
->aalg
->alg_key_len
+ 7) / 8);
2311 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2314 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2316 l
+= nla_total_size(sizeof(*x
->calg
));
2318 l
+= nla_total_size(sizeof(*x
->encap
));
2320 l
+= nla_total_size(sizeof(x
->tfcpad
));
2322 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2323 x
->security
->ctx_len
);
2325 l
+= nla_total_size(sizeof(*x
->coaddr
));
2327 /* Must count x->lastused as it may become non-zero behind our back. */
2328 l
+= nla_total_size(sizeof(u64
));
2333 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
2335 struct net
*net
= xs_net(x
);
2336 struct xfrm_usersa_info
*p
;
2337 struct xfrm_usersa_id
*id
;
2338 struct nlmsghdr
*nlh
;
2339 struct sk_buff
*skb
;
2340 int len
= xfrm_sa_len(x
);
2343 headlen
= sizeof(*p
);
2344 if (c
->event
== XFRM_MSG_DELSA
) {
2345 len
+= nla_total_size(headlen
);
2346 headlen
= sizeof(*id
);
2347 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2349 len
+= NLMSG_ALIGN(headlen
);
2351 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2355 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2357 goto nla_put_failure
;
2359 p
= nlmsg_data(nlh
);
2360 if (c
->event
== XFRM_MSG_DELSA
) {
2361 struct nlattr
*attr
;
2363 id
= nlmsg_data(nlh
);
2364 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2365 id
->spi
= x
->id
.spi
;
2366 id
->family
= x
->props
.family
;
2367 id
->proto
= x
->id
.proto
;
2369 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2371 goto nla_put_failure
;
2376 if (copy_to_user_state_extra(x
, p
, skb
))
2377 goto nla_put_failure
;
2379 nlmsg_end(skb
, nlh
);
2381 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2384 /* Somebody screwed up with xfrm_sa_len! */
2390 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2394 case XFRM_MSG_EXPIRE
:
2395 return xfrm_exp_state_notify(x
, c
);
2396 case XFRM_MSG_NEWAE
:
2397 return xfrm_aevent_state_notify(x
, c
);
2398 case XFRM_MSG_DELSA
:
2399 case XFRM_MSG_UPDSA
:
2400 case XFRM_MSG_NEWSA
:
2401 return xfrm_notify_sa(x
, c
);
2402 case XFRM_MSG_FLUSHSA
:
2403 return xfrm_notify_sa_flush(c
);
2405 printk(KERN_NOTICE
"xfrm_user: Unknown SA event %d\n",
2414 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2415 struct xfrm_policy
*xp
)
2417 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2418 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2419 + nla_total_size(sizeof(struct xfrm_mark
))
2420 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2421 + userpolicy_type_attrsize();
2424 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2425 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2428 struct xfrm_user_acquire
*ua
;
2429 struct nlmsghdr
*nlh
;
2430 __u32 seq
= xfrm_get_acqseq();
2432 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2436 ua
= nlmsg_data(nlh
);
2437 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2438 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2439 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2440 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2441 ua
->aalgos
= xt
->aalgos
;
2442 ua
->ealgos
= xt
->ealgos
;
2443 ua
->calgos
= xt
->calgos
;
2444 ua
->seq
= x
->km
.seq
= seq
;
2446 if (copy_to_user_tmpl(xp
, skb
) < 0)
2448 if (copy_to_user_state_sec_ctx(x
, skb
))
2450 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2452 if (xfrm_mark_put(skb
, &xp
->mark
))
2453 goto nla_put_failure
;
2455 return nlmsg_end(skb
, nlh
);
2459 nlmsg_cancel(skb
, nlh
);
2463 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2464 struct xfrm_policy
*xp
, int dir
)
2466 struct net
*net
= xs_net(x
);
2467 struct sk_buff
*skb
;
2469 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2473 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2476 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2479 /* User gives us xfrm_user_policy_info followed by an array of 0
2480 * or more templates.
2482 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2483 u8
*data
, int len
, int *dir
)
2485 struct net
*net
= sock_net(sk
);
2486 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2487 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2488 struct xfrm_policy
*xp
;
2491 switch (sk
->sk_family
) {
2493 if (opt
!= IP_XFRM_POLICY
) {
2498 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2500 if (opt
!= IPV6_XFRM_POLICY
) {
2513 if (len
< sizeof(*p
) ||
2514 verify_newpolicy_info(p
))
2517 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2518 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2521 if (p
->dir
> XFRM_POLICY_OUT
)
2524 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
2530 copy_from_user_policy(xp
, p
);
2531 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2532 copy_templates(xp
, ut
, nr
);
2539 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2541 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2542 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2543 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2544 + nla_total_size(sizeof(struct xfrm_mark
))
2545 + userpolicy_type_attrsize();
2548 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2549 int dir
, struct km_event
*c
)
2551 struct xfrm_user_polexpire
*upe
;
2552 struct nlmsghdr
*nlh
;
2553 int hard
= c
->data
.hard
;
2555 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2559 upe
= nlmsg_data(nlh
);
2560 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2561 if (copy_to_user_tmpl(xp
, skb
) < 0)
2563 if (copy_to_user_sec_ctx(xp
, skb
))
2565 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2567 if (xfrm_mark_put(skb
, &xp
->mark
))
2568 goto nla_put_failure
;
2571 return nlmsg_end(skb
, nlh
);
2575 nlmsg_cancel(skb
, nlh
);
2579 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2581 struct net
*net
= xp_net(xp
);
2582 struct sk_buff
*skb
;
2584 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2588 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2591 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2594 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2596 struct net
*net
= xp_net(xp
);
2597 struct xfrm_userpolicy_info
*p
;
2598 struct xfrm_userpolicy_id
*id
;
2599 struct nlmsghdr
*nlh
;
2600 struct sk_buff
*skb
;
2601 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2604 headlen
= sizeof(*p
);
2605 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2606 len
+= nla_total_size(headlen
);
2607 headlen
= sizeof(*id
);
2609 len
+= userpolicy_type_attrsize();
2610 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2611 len
+= NLMSG_ALIGN(headlen
);
2613 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2617 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2621 p
= nlmsg_data(nlh
);
2622 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2623 struct nlattr
*attr
;
2625 id
= nlmsg_data(nlh
);
2626 memset(id
, 0, sizeof(*id
));
2629 id
->index
= xp
->index
;
2631 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2633 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2640 copy_to_user_policy(xp
, p
, dir
);
2641 if (copy_to_user_tmpl(xp
, skb
) < 0)
2643 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2646 if (xfrm_mark_put(skb
, &xp
->mark
))
2647 goto nla_put_failure
;
2649 nlmsg_end(skb
, nlh
);
2651 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2659 static int xfrm_notify_policy_flush(struct km_event
*c
)
2661 struct net
*net
= c
->net
;
2662 struct nlmsghdr
*nlh
;
2663 struct sk_buff
*skb
;
2665 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2669 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2672 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2675 nlmsg_end(skb
, nlh
);
2677 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2684 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2688 case XFRM_MSG_NEWPOLICY
:
2689 case XFRM_MSG_UPDPOLICY
:
2690 case XFRM_MSG_DELPOLICY
:
2691 return xfrm_notify_policy(xp
, dir
, c
);
2692 case XFRM_MSG_FLUSHPOLICY
:
2693 return xfrm_notify_policy_flush(c
);
2694 case XFRM_MSG_POLEXPIRE
:
2695 return xfrm_exp_policy_notify(xp
, dir
, c
);
2697 printk(KERN_NOTICE
"xfrm_user: Unknown Policy event %d\n",
2705 static inline size_t xfrm_report_msgsize(void)
2707 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2710 static int build_report(struct sk_buff
*skb
, u8 proto
,
2711 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2713 struct xfrm_user_report
*ur
;
2714 struct nlmsghdr
*nlh
;
2716 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2720 ur
= nlmsg_data(nlh
);
2722 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2725 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2727 return nlmsg_end(skb
, nlh
);
2730 nlmsg_cancel(skb
, nlh
);
2734 static int xfrm_send_report(struct net
*net
, u8 proto
,
2735 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2737 struct sk_buff
*skb
;
2739 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2743 if (build_report(skb
, proto
, sel
, addr
) < 0)
2746 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2749 static inline size_t xfrm_mapping_msgsize(void)
2751 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
2754 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
2755 xfrm_address_t
*new_saddr
, __be16 new_sport
)
2757 struct xfrm_user_mapping
*um
;
2758 struct nlmsghdr
*nlh
;
2760 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
2764 um
= nlmsg_data(nlh
);
2766 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
2767 um
->id
.spi
= x
->id
.spi
;
2768 um
->id
.family
= x
->props
.family
;
2769 um
->id
.proto
= x
->id
.proto
;
2770 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
2771 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
2772 um
->new_sport
= new_sport
;
2773 um
->old_sport
= x
->encap
->encap_sport
;
2774 um
->reqid
= x
->props
.reqid
;
2776 return nlmsg_end(skb
, nlh
);
2779 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
2782 struct net
*net
= xs_net(x
);
2783 struct sk_buff
*skb
;
2785 if (x
->id
.proto
!= IPPROTO_ESP
)
2791 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
2795 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
2798 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MAPPING
, GFP_ATOMIC
);
2801 static struct xfrm_mgr netlink_mgr
= {
2803 .notify
= xfrm_send_state_notify
,
2804 .acquire
= xfrm_send_acquire
,
2805 .compile_policy
= xfrm_compile_policy
,
2806 .notify_policy
= xfrm_send_policy_notify
,
2807 .report
= xfrm_send_report
,
2808 .migrate
= xfrm_send_migrate
,
2809 .new_mapping
= xfrm_send_mapping
,
2812 static int __net_init
xfrm_user_net_init(struct net
*net
)
2816 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, XFRMNLGRP_MAX
,
2817 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2820 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
2821 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
2825 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
2828 list_for_each_entry(net
, net_exit_list
, exit_list
)
2829 rcu_assign_pointer(net
->xfrm
.nlsk
, NULL
);
2831 list_for_each_entry(net
, net_exit_list
, exit_list
)
2832 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
2835 static struct pernet_operations xfrm_user_net_ops
= {
2836 .init
= xfrm_user_net_init
,
2837 .exit_batch
= xfrm_user_net_exit
,
2840 static int __init
xfrm_user_init(void)
2844 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2846 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
2849 rv
= xfrm_register_km(&netlink_mgr
);
2851 unregister_pernet_subsys(&xfrm_user_net_ops
);
2855 static void __exit
xfrm_user_exit(void)
2857 xfrm_unregister_km(&netlink_mgr
);
2858 unregister_pernet_subsys(&xfrm_user_net_ops
);
2861 module_init(xfrm_user_init
);
2862 module_exit(xfrm_user_exit
);
2863 MODULE_LICENSE("GPL");
2864 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);