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>
35 static struct xfrm_mark dummy_mark
= {0, 0};
37 static inline int aead_len(struct xfrm_algo_aead
*alg
)
39 return sizeof(*alg
) + ((alg
->alg_key_len
+ 7) / 8);
42 static int verify_one_alg(struct nlattr
**attrs
, enum xfrm_attr_type_t type
)
44 struct nlattr
*rt
= attrs
[type
];
45 struct xfrm_algo
*algp
;
51 if (nla_len(rt
) < xfrm_alg_len(algp
))
64 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
68 static int verify_auth_trunc(struct nlattr
**attrs
)
70 struct nlattr
*rt
= attrs
[XFRMA_ALG_AUTH_TRUNC
];
71 struct xfrm_algo_auth
*algp
;
77 if (nla_len(rt
) < xfrm_alg_auth_len(algp
))
80 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
84 static int verify_aead(struct nlattr
**attrs
)
86 struct nlattr
*rt
= attrs
[XFRMA_ALG_AEAD
];
87 struct xfrm_algo_aead
*algp
;
93 if (nla_len(rt
) < aead_len(algp
))
96 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
100 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
101 xfrm_address_t
**addrp
)
103 struct nlattr
*rt
= attrs
[type
];
106 *addrp
= nla_data(rt
);
109 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
111 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
112 struct xfrm_user_sec_ctx
*uctx
;
118 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
125 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
126 struct nlattr
**attrs
)
136 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
148 switch (p
->id
.proto
) {
150 if ((!attrs
[XFRMA_ALG_AUTH
] &&
151 !attrs
[XFRMA_ALG_AUTH_TRUNC
]) ||
152 attrs
[XFRMA_ALG_AEAD
] ||
153 attrs
[XFRMA_ALG_CRYPT
] ||
154 attrs
[XFRMA_ALG_COMP
])
159 if (attrs
[XFRMA_ALG_COMP
])
161 if (!attrs
[XFRMA_ALG_AUTH
] &&
162 !attrs
[XFRMA_ALG_AUTH_TRUNC
] &&
163 !attrs
[XFRMA_ALG_CRYPT
] &&
164 !attrs
[XFRMA_ALG_AEAD
])
166 if ((attrs
[XFRMA_ALG_AUTH
] ||
167 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
168 attrs
[XFRMA_ALG_CRYPT
]) &&
169 attrs
[XFRMA_ALG_AEAD
])
174 if (!attrs
[XFRMA_ALG_COMP
] ||
175 attrs
[XFRMA_ALG_AEAD
] ||
176 attrs
[XFRMA_ALG_AUTH
] ||
177 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
178 attrs
[XFRMA_ALG_CRYPT
])
182 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
183 case IPPROTO_DSTOPTS
:
184 case IPPROTO_ROUTING
:
185 if (attrs
[XFRMA_ALG_COMP
] ||
186 attrs
[XFRMA_ALG_AUTH
] ||
187 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
188 attrs
[XFRMA_ALG_AEAD
] ||
189 attrs
[XFRMA_ALG_CRYPT
] ||
190 attrs
[XFRMA_ENCAP
] ||
191 attrs
[XFRMA_SEC_CTX
] ||
192 !attrs
[XFRMA_COADDR
])
201 if ((err
= verify_aead(attrs
)))
203 if ((err
= verify_auth_trunc(attrs
)))
205 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
207 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
209 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
211 if ((err
= verify_sec_ctx_len(attrs
)))
216 case XFRM_MODE_TRANSPORT
:
217 case XFRM_MODE_TUNNEL
:
218 case XFRM_MODE_ROUTEOPTIMIZATION
:
232 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
233 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
236 struct xfrm_algo
*p
, *ualg
;
237 struct xfrm_algo_desc
*algo
;
242 ualg
= nla_data(rta
);
244 algo
= get_byname(ualg
->alg_name
, 1);
247 *props
= algo
->desc
.sadb_alg_id
;
249 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
253 strcpy(p
->alg_name
, algo
->name
);
258 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
261 struct xfrm_algo
*ualg
;
262 struct xfrm_algo_auth
*p
;
263 struct xfrm_algo_desc
*algo
;
268 ualg
= nla_data(rta
);
270 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
273 *props
= algo
->desc
.sadb_alg_id
;
275 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
279 strcpy(p
->alg_name
, algo
->name
);
280 p
->alg_key_len
= ualg
->alg_key_len
;
281 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
282 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
288 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
291 struct xfrm_algo_auth
*p
, *ualg
;
292 struct xfrm_algo_desc
*algo
;
297 ualg
= nla_data(rta
);
299 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
302 if (ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
304 *props
= algo
->desc
.sadb_alg_id
;
306 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
310 strcpy(p
->alg_name
, algo
->name
);
311 if (!p
->alg_trunc_len
)
312 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
318 static int attach_aead(struct xfrm_algo_aead
**algpp
, u8
*props
,
321 struct xfrm_algo_aead
*p
, *ualg
;
322 struct xfrm_algo_desc
*algo
;
327 ualg
= nla_data(rta
);
329 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
332 *props
= algo
->desc
.sadb_alg_id
;
334 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
338 strcpy(p
->alg_name
, algo
->name
);
343 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
348 len
+= sizeof(struct xfrm_user_sec_ctx
);
349 len
+= xfrm_ctx
->ctx_len
;
354 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
356 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
357 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
358 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
359 x
->props
.mode
= p
->mode
;
360 x
->props
.replay_window
= p
->replay_window
;
361 x
->props
.reqid
= p
->reqid
;
362 x
->props
.family
= p
->family
;
363 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
364 x
->props
.flags
= p
->flags
;
366 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
367 x
->sel
.family
= p
->family
;
371 * someday when pfkey also has support, we could have the code
372 * somehow made shareable and move it to xfrm_state.c - JHS
375 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
)
377 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
378 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
379 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
380 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
383 struct xfrm_replay_state
*replay
;
384 replay
= nla_data(rp
);
385 memcpy(&x
->replay
, replay
, sizeof(*replay
));
386 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
390 struct xfrm_lifetime_cur
*ltime
;
391 ltime
= nla_data(lt
);
392 x
->curlft
.bytes
= ltime
->bytes
;
393 x
->curlft
.packets
= ltime
->packets
;
394 x
->curlft
.add_time
= ltime
->add_time
;
395 x
->curlft
.use_time
= ltime
->use_time
;
399 x
->replay_maxage
= nla_get_u32(et
);
402 x
->replay_maxdiff
= nla_get_u32(rt
);
405 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
406 struct xfrm_usersa_info
*p
,
407 struct nlattr
**attrs
,
410 struct xfrm_state
*x
= xfrm_state_alloc(net
);
416 copy_from_user_state(x
, p
);
418 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
419 attrs
[XFRMA_ALG_AEAD
])))
421 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
422 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
424 if (!x
->props
.aalgo
) {
425 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
426 attrs
[XFRMA_ALG_AUTH
])))
429 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
430 xfrm_ealg_get_byname
,
431 attrs
[XFRMA_ALG_CRYPT
])))
433 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
434 xfrm_calg_get_byname
,
435 attrs
[XFRMA_ALG_COMP
])))
438 if (attrs
[XFRMA_ENCAP
]) {
439 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
440 sizeof(*x
->encap
), GFP_KERNEL
);
441 if (x
->encap
== NULL
)
445 if (attrs
[XFRMA_COADDR
]) {
446 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
447 sizeof(*x
->coaddr
), GFP_KERNEL
);
448 if (x
->coaddr
== NULL
)
452 err
= xfrm_init_state(x
);
456 if (attrs
[XFRMA_SEC_CTX
] &&
457 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
461 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
462 /* sysctl_xfrm_aevent_etime is in 100ms units */
463 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
464 x
->preplay
.bitmap
= 0;
465 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
466 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
468 /* override default values from above */
470 xfrm_update_ae_params(x
, attrs
);
475 x
->km
.state
= XFRM_STATE_DEAD
;
482 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
483 struct nlattr
**attrs
)
485 struct net
*net
= sock_net(skb
->sk
);
486 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
487 struct xfrm_state
*x
;
490 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
491 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
492 u32 sid
= NETLINK_CB(skb
).sid
;
494 err
= verify_newsa_info(p
, attrs
);
498 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
503 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
504 err
= xfrm_state_add(x
);
506 err
= xfrm_state_update(x
);
508 xfrm_audit_state_add(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
511 x
->km
.state
= XFRM_STATE_DEAD
;
516 c
.seq
= nlh
->nlmsg_seq
;
517 c
.pid
= nlh
->nlmsg_pid
;
518 c
.event
= nlh
->nlmsg_type
;
520 km_state_notify(x
, &c
);
526 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
527 struct xfrm_usersa_id
*p
,
528 struct nlattr
**attrs
,
531 struct xfrm_state
*x
= NULL
;
534 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
536 x
= xfrm_state_lookup(net
, DUMMY_MARK
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
538 xfrm_address_t
*saddr
= NULL
;
540 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
547 x
= xfrm_state_lookup_byaddr(net
, DUMMY_MARK
, &p
->daddr
, saddr
,
548 p
->proto
, p
->family
);
557 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
558 struct nlattr
**attrs
)
560 struct net
*net
= sock_net(skb
->sk
);
561 struct xfrm_state
*x
;
564 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
565 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
566 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
567 u32 sid
= NETLINK_CB(skb
).sid
;
569 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
573 if ((err
= security_xfrm_state_delete(x
)) != 0)
576 if (xfrm_state_kern(x
)) {
581 err
= xfrm_state_delete(x
);
586 c
.seq
= nlh
->nlmsg_seq
;
587 c
.pid
= nlh
->nlmsg_pid
;
588 c
.event
= nlh
->nlmsg_type
;
589 km_state_notify(x
, &c
);
592 xfrm_audit_state_delete(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
597 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
599 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
600 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
601 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
602 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
603 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
604 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
605 p
->mode
= x
->props
.mode
;
606 p
->replay_window
= x
->props
.replay_window
;
607 p
->reqid
= x
->props
.reqid
;
608 p
->family
= x
->props
.family
;
609 p
->flags
= x
->props
.flags
;
613 struct xfrm_dump_info
{
614 struct sk_buff
*in_skb
;
615 struct sk_buff
*out_skb
;
620 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
622 struct xfrm_user_sec_ctx
*uctx
;
624 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
626 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
630 uctx
= nla_data(attr
);
631 uctx
->exttype
= XFRMA_SEC_CTX
;
632 uctx
->len
= ctx_size
;
633 uctx
->ctx_doi
= s
->ctx_doi
;
634 uctx
->ctx_alg
= s
->ctx_alg
;
635 uctx
->ctx_len
= s
->ctx_len
;
636 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
641 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
643 struct xfrm_algo
*algo
;
646 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
647 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
651 algo
= nla_data(nla
);
652 strcpy(algo
->alg_name
, auth
->alg_name
);
653 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
654 algo
->alg_key_len
= auth
->alg_key_len
;
659 /* Don't change this without updating xfrm_sa_len! */
660 static int copy_to_user_state_extra(struct xfrm_state
*x
,
661 struct xfrm_usersa_info
*p
,
664 copy_to_user_state(x
, p
);
667 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
670 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
673 NLA_PUT(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
675 if (copy_to_user_auth(x
->aalg
, skb
))
676 goto nla_put_failure
;
678 NLA_PUT(skb
, XFRMA_ALG_AUTH_TRUNC
,
679 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
682 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
684 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
687 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
689 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
690 goto nla_put_failure
;
698 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
700 struct xfrm_dump_info
*sp
= ptr
;
701 struct sk_buff
*in_skb
= sp
->in_skb
;
702 struct sk_buff
*skb
= sp
->out_skb
;
703 struct xfrm_usersa_info
*p
;
704 struct nlmsghdr
*nlh
;
707 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
708 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
714 err
= copy_to_user_state_extra(x
, p
, skb
);
716 goto nla_put_failure
;
722 nlmsg_cancel(skb
, nlh
);
726 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
728 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
729 xfrm_state_walk_done(walk
);
733 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
735 struct net
*net
= sock_net(skb
->sk
);
736 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
737 struct xfrm_dump_info info
;
739 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
740 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
742 info
.in_skb
= cb
->skb
;
744 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
745 info
.nlmsg_flags
= NLM_F_MULTI
;
749 xfrm_state_walk_init(walk
, 0);
752 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
757 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
758 struct xfrm_state
*x
, u32 seq
)
760 struct xfrm_dump_info info
;
763 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
765 return ERR_PTR(-ENOMEM
);
767 info
.in_skb
= in_skb
;
769 info
.nlmsg_seq
= seq
;
770 info
.nlmsg_flags
= 0;
772 if (dump_one_state(x
, 0, &info
)) {
780 static inline size_t xfrm_spdinfo_msgsize(void)
782 return NLMSG_ALIGN(4)
783 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
784 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
787 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
788 u32 pid
, u32 seq
, u32 flags
)
790 struct xfrmk_spdinfo si
;
791 struct xfrmu_spdinfo spc
;
792 struct xfrmu_spdhinfo sph
;
793 struct nlmsghdr
*nlh
;
796 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
797 if (nlh
== NULL
) /* shouldnt really happen ... */
802 xfrm_spd_getinfo(net
, &si
);
803 spc
.incnt
= si
.incnt
;
804 spc
.outcnt
= si
.outcnt
;
805 spc
.fwdcnt
= si
.fwdcnt
;
806 spc
.inscnt
= si
.inscnt
;
807 spc
.outscnt
= si
.outscnt
;
808 spc
.fwdscnt
= si
.fwdscnt
;
809 sph
.spdhcnt
= si
.spdhcnt
;
810 sph
.spdhmcnt
= si
.spdhmcnt
;
812 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
813 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
815 return nlmsg_end(skb
, nlh
);
818 nlmsg_cancel(skb
, nlh
);
822 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
823 struct nlattr
**attrs
)
825 struct net
*net
= sock_net(skb
->sk
);
826 struct sk_buff
*r_skb
;
827 u32
*flags
= nlmsg_data(nlh
);
828 u32 spid
= NETLINK_CB(skb
).pid
;
829 u32 seq
= nlh
->nlmsg_seq
;
831 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
835 if (build_spdinfo(r_skb
, net
, spid
, seq
, *flags
) < 0)
838 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, spid
);
841 static inline size_t xfrm_sadinfo_msgsize(void)
843 return NLMSG_ALIGN(4)
844 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
845 + nla_total_size(4); /* XFRMA_SAD_CNT */
848 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
849 u32 pid
, u32 seq
, u32 flags
)
851 struct xfrmk_sadinfo si
;
852 struct xfrmu_sadhinfo sh
;
853 struct nlmsghdr
*nlh
;
856 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
857 if (nlh
== NULL
) /* shouldnt really happen ... */
862 xfrm_sad_getinfo(net
, &si
);
864 sh
.sadhmcnt
= si
.sadhmcnt
;
865 sh
.sadhcnt
= si
.sadhcnt
;
867 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
868 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
870 return nlmsg_end(skb
, nlh
);
873 nlmsg_cancel(skb
, nlh
);
877 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
878 struct nlattr
**attrs
)
880 struct net
*net
= sock_net(skb
->sk
);
881 struct sk_buff
*r_skb
;
882 u32
*flags
= nlmsg_data(nlh
);
883 u32 spid
= NETLINK_CB(skb
).pid
;
884 u32 seq
= nlh
->nlmsg_seq
;
886 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
890 if (build_sadinfo(r_skb
, net
, spid
, seq
, *flags
) < 0)
893 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, spid
);
896 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
897 struct nlattr
**attrs
)
899 struct net
*net
= sock_net(skb
->sk
);
900 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
901 struct xfrm_state
*x
;
902 struct sk_buff
*resp_skb
;
905 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
909 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
910 if (IS_ERR(resp_skb
)) {
911 err
= PTR_ERR(resp_skb
);
913 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).pid
);
920 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
922 switch (p
->info
.id
.proto
) {
928 /* IPCOMP spi is 16-bits. */
929 if (p
->max
>= 0x10000)
943 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
944 struct nlattr
**attrs
)
946 struct net
*net
= sock_net(skb
->sk
);
947 struct xfrm_state
*x
;
948 struct xfrm_userspi_info
*p
;
949 struct sk_buff
*resp_skb
;
950 xfrm_address_t
*daddr
;
955 err
= verify_userspi_info(p
);
959 family
= p
->info
.family
;
960 daddr
= &p
->info
.id
.daddr
;
964 x
= xfrm_find_acq_byseq(net
, DUMMY_MARK
, p
->info
.seq
);
965 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
972 x
= xfrm_find_acq(net
, &dummy_mark
, p
->info
.mode
, p
->info
.reqid
,
973 p
->info
.id
.proto
, daddr
,
980 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
984 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
985 if (IS_ERR(resp_skb
)) {
986 err
= PTR_ERR(resp_skb
);
990 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).pid
);
998 static int verify_policy_dir(u8 dir
)
1001 case XFRM_POLICY_IN
:
1002 case XFRM_POLICY_OUT
:
1003 case XFRM_POLICY_FWD
:
1013 static int verify_policy_type(u8 type
)
1016 case XFRM_POLICY_TYPE_MAIN
:
1017 #ifdef CONFIG_XFRM_SUB_POLICY
1018 case XFRM_POLICY_TYPE_SUB
:
1029 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1032 case XFRM_SHARE_ANY
:
1033 case XFRM_SHARE_SESSION
:
1034 case XFRM_SHARE_USER
:
1035 case XFRM_SHARE_UNIQUE
:
1042 switch (p
->action
) {
1043 case XFRM_POLICY_ALLOW
:
1044 case XFRM_POLICY_BLOCK
:
1051 switch (p
->sel
.family
) {
1056 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1059 return -EAFNOSUPPORT
;
1066 return verify_policy_dir(p
->dir
);
1069 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1071 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1072 struct xfrm_user_sec_ctx
*uctx
;
1077 uctx
= nla_data(rt
);
1078 return security_xfrm_policy_alloc(&pol
->security
, uctx
);
1081 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1087 for (i
= 0; i
< nr
; i
++, ut
++) {
1088 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1090 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1091 memcpy(&t
->saddr
, &ut
->saddr
,
1092 sizeof(xfrm_address_t
));
1093 t
->reqid
= ut
->reqid
;
1095 t
->share
= ut
->share
;
1096 t
->optional
= ut
->optional
;
1097 t
->aalgos
= ut
->aalgos
;
1098 t
->ealgos
= ut
->ealgos
;
1099 t
->calgos
= ut
->calgos
;
1100 /* If all masks are ~0, then we allow all algorithms. */
1101 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1102 t
->encap_family
= ut
->family
;
1106 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1110 if (nr
> XFRM_MAX_DEPTH
)
1113 for (i
= 0; i
< nr
; i
++) {
1114 /* We never validated the ut->family value, so many
1115 * applications simply leave it at zero. The check was
1116 * never made and ut->family was ignored because all
1117 * templates could be assumed to have the same family as
1118 * the policy itself. Now that we will have ipv4-in-ipv6
1119 * and ipv6-in-ipv4 tunnels, this is no longer true.
1122 ut
[i
].family
= family
;
1124 switch (ut
[i
].family
) {
1127 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1139 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1141 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1146 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1147 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1150 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1154 copy_templates(pol
, utmpl
, nr
);
1159 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1161 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1162 struct xfrm_userpolicy_type
*upt
;
1163 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1171 err
= verify_policy_type(type
);
1179 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1181 xp
->priority
= p
->priority
;
1182 xp
->index
= p
->index
;
1183 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1184 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1185 xp
->action
= p
->action
;
1186 xp
->flags
= p
->flags
;
1187 xp
->family
= p
->sel
.family
;
1188 /* XXX xp->share = p->share; */
1191 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1193 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1194 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1195 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1196 p
->priority
= xp
->priority
;
1197 p
->index
= xp
->index
;
1198 p
->sel
.family
= xp
->family
;
1200 p
->action
= xp
->action
;
1201 p
->flags
= xp
->flags
;
1202 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1205 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1207 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1215 copy_from_user_policy(xp
, p
);
1217 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1221 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1222 err
= copy_from_user_sec_ctx(xp
, attrs
);
1230 xfrm_policy_destroy(xp
);
1234 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1235 struct nlattr
**attrs
)
1237 struct net
*net
= sock_net(skb
->sk
);
1238 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1239 struct xfrm_policy
*xp
;
1243 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1244 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
1245 u32 sid
= NETLINK_CB(skb
).sid
;
1247 err
= verify_newpolicy_info(p
);
1250 err
= verify_sec_ctx_len(attrs
);
1254 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1258 /* shouldnt excl be based on nlh flags??
1259 * Aha! this is anti-netlink really i.e more pfkey derived
1260 * in netlink excl is a flag and you wouldnt need
1261 * a type XFRM_MSG_UPDPOLICY - JHS */
1262 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1263 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1264 xfrm_audit_policy_add(xp
, err
? 0 : 1, loginuid
, sessionid
, sid
);
1267 security_xfrm_policy_free(xp
->security
);
1272 c
.event
= nlh
->nlmsg_type
;
1273 c
.seq
= nlh
->nlmsg_seq
;
1274 c
.pid
= nlh
->nlmsg_pid
;
1275 km_policy_notify(xp
, p
->dir
, &c
);
1282 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1284 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1287 if (xp
->xfrm_nr
== 0)
1290 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1291 struct xfrm_user_tmpl
*up
= &vec
[i
];
1292 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1294 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1295 up
->family
= kp
->encap_family
;
1296 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1297 up
->reqid
= kp
->reqid
;
1298 up
->mode
= kp
->mode
;
1299 up
->share
= kp
->share
;
1300 up
->optional
= kp
->optional
;
1301 up
->aalgos
= kp
->aalgos
;
1302 up
->ealgos
= kp
->ealgos
;
1303 up
->calgos
= kp
->calgos
;
1306 return nla_put(skb
, XFRMA_TMPL
,
1307 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1310 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1313 return copy_sec_ctx(x
->security
, skb
);
1318 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1321 return copy_sec_ctx(xp
->security
, skb
);
1325 static inline size_t userpolicy_type_attrsize(void)
1327 #ifdef CONFIG_XFRM_SUB_POLICY
1328 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1334 #ifdef CONFIG_XFRM_SUB_POLICY
1335 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1337 struct xfrm_userpolicy_type upt
= {
1341 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1345 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1351 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1353 struct xfrm_dump_info
*sp
= ptr
;
1354 struct xfrm_userpolicy_info
*p
;
1355 struct sk_buff
*in_skb
= sp
->in_skb
;
1356 struct sk_buff
*skb
= sp
->out_skb
;
1357 struct nlmsghdr
*nlh
;
1359 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1360 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1364 p
= nlmsg_data(nlh
);
1365 copy_to_user_policy(xp
, p
, dir
);
1366 if (copy_to_user_tmpl(xp
, skb
) < 0)
1368 if (copy_to_user_sec_ctx(xp
, skb
))
1370 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1373 nlmsg_end(skb
, nlh
);
1377 nlmsg_cancel(skb
, nlh
);
1381 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1383 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1385 xfrm_policy_walk_done(walk
);
1389 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1391 struct net
*net
= sock_net(skb
->sk
);
1392 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1393 struct xfrm_dump_info info
;
1395 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1396 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1398 info
.in_skb
= cb
->skb
;
1400 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1401 info
.nlmsg_flags
= NLM_F_MULTI
;
1405 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1408 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1413 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1414 struct xfrm_policy
*xp
,
1417 struct xfrm_dump_info info
;
1418 struct sk_buff
*skb
;
1420 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1422 return ERR_PTR(-ENOMEM
);
1424 info
.in_skb
= in_skb
;
1426 info
.nlmsg_seq
= seq
;
1427 info
.nlmsg_flags
= 0;
1429 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1437 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1438 struct nlattr
**attrs
)
1440 struct net
*net
= sock_net(skb
->sk
);
1441 struct xfrm_policy
*xp
;
1442 struct xfrm_userpolicy_id
*p
;
1443 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1448 p
= nlmsg_data(nlh
);
1449 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1451 err
= copy_from_user_policy_type(&type
, attrs
);
1455 err
= verify_policy_dir(p
->dir
);
1460 xp
= xfrm_policy_byid(net
, type
, p
->dir
, p
->index
, delete, &err
);
1462 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1463 struct xfrm_sec_ctx
*ctx
;
1465 err
= verify_sec_ctx_len(attrs
);
1471 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1473 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1477 xp
= xfrm_policy_bysel_ctx(net
, type
, p
->dir
, &p
->sel
, ctx
,
1479 security_xfrm_policy_free(ctx
);
1485 struct sk_buff
*resp_skb
;
1487 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1488 if (IS_ERR(resp_skb
)) {
1489 err
= PTR_ERR(resp_skb
);
1491 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1492 NETLINK_CB(skb
).pid
);
1495 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1496 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
1497 u32 sid
= NETLINK_CB(skb
).sid
;
1499 xfrm_audit_policy_delete(xp
, err
? 0 : 1, loginuid
, sessionid
,
1505 c
.data
.byid
= p
->index
;
1506 c
.event
= nlh
->nlmsg_type
;
1507 c
.seq
= nlh
->nlmsg_seq
;
1508 c
.pid
= nlh
->nlmsg_pid
;
1509 km_policy_notify(xp
, p
->dir
, &c
);
1517 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1518 struct nlattr
**attrs
)
1520 struct net
*net
= sock_net(skb
->sk
);
1522 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1523 struct xfrm_audit audit_info
;
1526 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1527 audit_info
.sessionid
= NETLINK_CB(skb
).sessionid
;
1528 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1529 err
= xfrm_state_flush(net
, p
->proto
, &audit_info
);
1531 if (err
== -ESRCH
) /* empty table */
1535 c
.data
.proto
= p
->proto
;
1536 c
.event
= nlh
->nlmsg_type
;
1537 c
.seq
= nlh
->nlmsg_seq
;
1538 c
.pid
= nlh
->nlmsg_pid
;
1540 km_state_notify(NULL
, &c
);
1545 static inline size_t xfrm_aevent_msgsize(void)
1547 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1548 + nla_total_size(sizeof(struct xfrm_replay_state
))
1549 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1550 + nla_total_size(4) /* XFRM_AE_RTHR */
1551 + nla_total_size(4); /* XFRM_AE_ETHR */
1554 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1556 struct xfrm_aevent_id
*id
;
1557 struct nlmsghdr
*nlh
;
1559 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1563 id
= nlmsg_data(nlh
);
1564 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1565 id
->sa_id
.spi
= x
->id
.spi
;
1566 id
->sa_id
.family
= x
->props
.family
;
1567 id
->sa_id
.proto
= x
->id
.proto
;
1568 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1569 id
->reqid
= x
->props
.reqid
;
1570 id
->flags
= c
->data
.aevent
;
1572 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1573 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1575 if (id
->flags
& XFRM_AE_RTHR
)
1576 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1578 if (id
->flags
& XFRM_AE_ETHR
)
1579 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1580 x
->replay_maxage
* 10 / HZ
);
1582 return nlmsg_end(skb
, nlh
);
1585 nlmsg_cancel(skb
, nlh
);
1589 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1590 struct nlattr
**attrs
)
1592 struct net
*net
= sock_net(skb
->sk
);
1593 struct xfrm_state
*x
;
1594 struct sk_buff
*r_skb
;
1597 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1598 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1600 r_skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1604 x
= xfrm_state_lookup(net
, DUMMY_MARK
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1611 * XXX: is this lock really needed - none of the other
1612 * gets lock (the concern is things getting updated
1613 * while we are still reading) - jhs
1615 spin_lock_bh(&x
->lock
);
1616 c
.data
.aevent
= p
->flags
;
1617 c
.seq
= nlh
->nlmsg_seq
;
1618 c
.pid
= nlh
->nlmsg_pid
;
1620 if (build_aevent(r_skb
, x
, &c
) < 0)
1622 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).pid
);
1623 spin_unlock_bh(&x
->lock
);
1628 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1629 struct nlattr
**attrs
)
1631 struct net
*net
= sock_net(skb
->sk
);
1632 struct xfrm_state
*x
;
1635 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1636 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1637 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1642 /* pedantic mode - thou shalt sayeth replaceth */
1643 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1646 x
= xfrm_state_lookup(net
, DUMMY_MARK
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1650 if (x
->km
.state
!= XFRM_STATE_VALID
)
1653 spin_lock_bh(&x
->lock
);
1654 xfrm_update_ae_params(x
, attrs
);
1655 spin_unlock_bh(&x
->lock
);
1657 c
.event
= nlh
->nlmsg_type
;
1658 c
.seq
= nlh
->nlmsg_seq
;
1659 c
.pid
= nlh
->nlmsg_pid
;
1660 c
.data
.aevent
= XFRM_AE_CU
;
1661 km_state_notify(x
, &c
);
1668 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1669 struct nlattr
**attrs
)
1671 struct net
*net
= sock_net(skb
->sk
);
1673 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1675 struct xfrm_audit audit_info
;
1677 err
= copy_from_user_policy_type(&type
, attrs
);
1681 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1682 audit_info
.sessionid
= NETLINK_CB(skb
).sessionid
;
1683 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1684 err
= xfrm_policy_flush(net
, type
, &audit_info
);
1686 if (err
== -ESRCH
) /* empty table */
1692 c
.event
= nlh
->nlmsg_type
;
1693 c
.seq
= nlh
->nlmsg_seq
;
1694 c
.pid
= nlh
->nlmsg_pid
;
1696 km_policy_notify(NULL
, 0, &c
);
1700 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1701 struct nlattr
**attrs
)
1703 struct net
*net
= sock_net(skb
->sk
);
1704 struct xfrm_policy
*xp
;
1705 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1706 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1707 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1710 err
= copy_from_user_policy_type(&type
, attrs
);
1715 xp
= xfrm_policy_byid(net
, type
, p
->dir
, p
->index
, 0, &err
);
1717 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1718 struct xfrm_sec_ctx
*ctx
;
1720 err
= verify_sec_ctx_len(attrs
);
1726 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1728 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1732 xp
= xfrm_policy_bysel_ctx(net
, type
, p
->dir
, &p
->sel
, ctx
, 0, &err
);
1733 security_xfrm_policy_free(ctx
);
1738 read_lock(&xp
->lock
);
1739 if (xp
->walk
.dead
) {
1740 read_unlock(&xp
->lock
);
1744 read_unlock(&xp
->lock
);
1747 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1748 uid_t sessionid
= NETLINK_CB(skb
).sessionid
;
1749 u32 sid
= NETLINK_CB(skb
).sid
;
1750 xfrm_policy_delete(xp
, p
->dir
);
1751 xfrm_audit_policy_delete(xp
, 1, loginuid
, sessionid
, sid
);
1754 // reset the timers here?
1755 printk("Dont know what to do with soft policy expire\n");
1757 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1764 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1765 struct nlattr
**attrs
)
1767 struct net
*net
= sock_net(skb
->sk
);
1768 struct xfrm_state
*x
;
1770 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1771 struct xfrm_usersa_info
*p
= &ue
->state
;
1773 x
= xfrm_state_lookup(net
, DUMMY_MARK
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1779 spin_lock_bh(&x
->lock
);
1781 if (x
->km
.state
!= XFRM_STATE_VALID
)
1783 km_state_expired(x
, ue
->hard
, current
->pid
);
1786 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1787 uid_t sessionid
= NETLINK_CB(skb
).sessionid
;
1788 u32 sid
= NETLINK_CB(skb
).sid
;
1789 __xfrm_state_delete(x
);
1790 xfrm_audit_state_delete(x
, 1, loginuid
, sessionid
, sid
);
1794 spin_unlock_bh(&x
->lock
);
1799 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1800 struct nlattr
**attrs
)
1802 struct net
*net
= sock_net(skb
->sk
);
1803 struct xfrm_policy
*xp
;
1804 struct xfrm_user_tmpl
*ut
;
1806 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1808 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1809 struct xfrm_state
*x
= xfrm_state_alloc(net
);
1815 err
= verify_newpolicy_info(&ua
->policy
);
1820 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
1824 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1825 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1826 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1829 /* extract the templates and for each call km_key */
1830 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1831 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1832 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1833 x
->props
.mode
= t
->mode
;
1834 x
->props
.reqid
= t
->reqid
;
1835 x
->props
.family
= ut
->family
;
1836 t
->aalgos
= ua
->aalgos
;
1837 t
->ealgos
= ua
->ealgos
;
1838 t
->calgos
= ua
->calgos
;
1839 err
= km_query(x
, t
, xp
);
1849 printk("BAD policy passed\n");
1856 #ifdef CONFIG_XFRM_MIGRATE
1857 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1858 struct xfrm_kmaddress
*k
,
1859 struct nlattr
**attrs
, int *num
)
1861 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
1862 struct xfrm_user_migrate
*um
;
1866 struct xfrm_user_kmaddress
*uk
;
1868 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
1869 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
1870 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
1871 k
->family
= uk
->family
;
1872 k
->reserved
= uk
->reserved
;
1876 num_migrate
= nla_len(rt
) / sizeof(*um
);
1878 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
1881 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
1882 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
1883 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
1884 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
1885 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
1887 ma
->proto
= um
->proto
;
1888 ma
->mode
= um
->mode
;
1889 ma
->reqid
= um
->reqid
;
1891 ma
->old_family
= um
->old_family
;
1892 ma
->new_family
= um
->new_family
;
1899 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1900 struct nlattr
**attrs
)
1902 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
1903 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
1904 struct xfrm_kmaddress km
, *kmp
;
1909 if (attrs
[XFRMA_MIGRATE
] == NULL
)
1912 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
1914 err
= copy_from_user_policy_type(&type
, attrs
);
1918 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
1925 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
);
1930 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1931 struct nlattr
**attrs
)
1933 return -ENOPROTOOPT
;
1937 #ifdef CONFIG_XFRM_MIGRATE
1938 static int copy_to_user_migrate(struct xfrm_migrate
*m
, struct sk_buff
*skb
)
1940 struct xfrm_user_migrate um
;
1942 memset(&um
, 0, sizeof(um
));
1943 um
.proto
= m
->proto
;
1945 um
.reqid
= m
->reqid
;
1946 um
.old_family
= m
->old_family
;
1947 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
1948 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
1949 um
.new_family
= m
->new_family
;
1950 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
1951 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
1953 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
1956 static int copy_to_user_kmaddress(struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
1958 struct xfrm_user_kmaddress uk
;
1960 memset(&uk
, 0, sizeof(uk
));
1961 uk
.family
= k
->family
;
1962 uk
.reserved
= k
->reserved
;
1963 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
1964 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
1966 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
1969 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
)
1971 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
1972 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
1973 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
1974 + userpolicy_type_attrsize();
1977 static int build_migrate(struct sk_buff
*skb
, struct xfrm_migrate
*m
,
1978 int num_migrate
, struct xfrm_kmaddress
*k
,
1979 struct xfrm_selector
*sel
, u8 dir
, u8 type
)
1981 struct xfrm_migrate
*mp
;
1982 struct xfrm_userpolicy_id
*pol_id
;
1983 struct nlmsghdr
*nlh
;
1986 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
1990 pol_id
= nlmsg_data(nlh
);
1991 /* copy data from selector, dir, and type to the pol_id */
1992 memset(pol_id
, 0, sizeof(*pol_id
));
1993 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
1996 if (k
!= NULL
&& (copy_to_user_kmaddress(k
, skb
) < 0))
1999 if (copy_to_user_policy_type(type
, skb
) < 0)
2002 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2003 if (copy_to_user_migrate(mp
, skb
) < 0)
2007 return nlmsg_end(skb
, nlh
);
2009 nlmsg_cancel(skb
, nlh
);
2013 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2014 struct xfrm_migrate
*m
, int num_migrate
,
2015 struct xfrm_kmaddress
*k
)
2017 struct net
*net
= &init_net
;
2018 struct sk_buff
*skb
;
2020 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
), GFP_ATOMIC
);
2025 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, dir
, type
) < 0)
2028 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
2031 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2032 struct xfrm_migrate
*m
, int num_migrate
,
2033 struct xfrm_kmaddress
*k
)
2035 return -ENOPROTOOPT
;
2039 #define XMSGSIZE(type) sizeof(struct type)
2041 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2042 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2043 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2044 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2045 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2046 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2047 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2048 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2049 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2050 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2051 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2052 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2053 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2054 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2055 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2056 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2057 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2058 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2059 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2060 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2061 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2066 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2067 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2068 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2069 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2070 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2071 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2072 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2073 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2074 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2075 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2076 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2077 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2078 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2079 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2080 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2081 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2082 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2083 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2084 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2085 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2086 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2089 static struct xfrm_link
{
2090 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2091 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2092 int (*done
)(struct netlink_callback
*);
2093 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2094 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2095 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2096 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2097 .dump
= xfrm_dump_sa
,
2098 .done
= xfrm_dump_sa_done
},
2099 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2100 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2101 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2102 .dump
= xfrm_dump_policy
,
2103 .done
= xfrm_dump_policy_done
},
2104 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2105 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2106 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2107 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2108 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2109 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2110 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2111 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2112 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2113 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2114 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2115 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2116 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2119 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2121 struct net
*net
= sock_net(skb
->sk
);
2122 struct nlattr
*attrs
[XFRMA_MAX
+1];
2123 struct xfrm_link
*link
;
2126 type
= nlh
->nlmsg_type
;
2127 if (type
> XFRM_MSG_MAX
)
2130 type
-= XFRM_MSG_BASE
;
2131 link
= &xfrm_dispatch
[type
];
2133 /* All operations require privileges, even GET */
2134 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
2137 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2138 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2139 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2140 if (link
->dump
== NULL
)
2143 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, link
->dump
, link
->done
);
2146 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
2151 if (link
->doit
== NULL
)
2154 return link
->doit(skb
, nlh
, attrs
);
2157 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2159 mutex_lock(&xfrm_cfg_mutex
);
2160 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2161 mutex_unlock(&xfrm_cfg_mutex
);
2164 static inline size_t xfrm_expire_msgsize(void)
2166 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
));
2169 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
2171 struct xfrm_user_expire
*ue
;
2172 struct nlmsghdr
*nlh
;
2174 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2178 ue
= nlmsg_data(nlh
);
2179 copy_to_user_state(x
, &ue
->state
);
2180 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2182 return nlmsg_end(skb
, nlh
);
2185 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2187 struct net
*net
= xs_net(x
);
2188 struct sk_buff
*skb
;
2190 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2194 if (build_expire(skb
, x
, c
) < 0)
2197 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2200 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2202 struct net
*net
= xs_net(x
);
2203 struct sk_buff
*skb
;
2205 skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
2209 if (build_aevent(skb
, x
, c
) < 0)
2212 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
2215 static int xfrm_notify_sa_flush(struct km_event
*c
)
2217 struct net
*net
= c
->net
;
2218 struct xfrm_usersa_flush
*p
;
2219 struct nlmsghdr
*nlh
;
2220 struct sk_buff
*skb
;
2221 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2223 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2227 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2233 p
= nlmsg_data(nlh
);
2234 p
->proto
= c
->data
.proto
;
2236 nlmsg_end(skb
, nlh
);
2238 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2241 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2245 l
+= nla_total_size(aead_len(x
->aead
));
2247 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2248 (x
->aalg
->alg_key_len
+ 7) / 8);
2249 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2252 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2254 l
+= nla_total_size(sizeof(*x
->calg
));
2256 l
+= nla_total_size(sizeof(*x
->encap
));
2258 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2259 x
->security
->ctx_len
);
2261 l
+= nla_total_size(sizeof(*x
->coaddr
));
2263 /* Must count x->lastused as it may become non-zero behind our back. */
2264 l
+= nla_total_size(sizeof(u64
));
2269 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
2271 struct net
*net
= xs_net(x
);
2272 struct xfrm_usersa_info
*p
;
2273 struct xfrm_usersa_id
*id
;
2274 struct nlmsghdr
*nlh
;
2275 struct sk_buff
*skb
;
2276 int len
= xfrm_sa_len(x
);
2279 headlen
= sizeof(*p
);
2280 if (c
->event
== XFRM_MSG_DELSA
) {
2281 len
+= nla_total_size(headlen
);
2282 headlen
= sizeof(*id
);
2284 len
+= NLMSG_ALIGN(headlen
);
2286 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2290 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2292 goto nla_put_failure
;
2294 p
= nlmsg_data(nlh
);
2295 if (c
->event
== XFRM_MSG_DELSA
) {
2296 struct nlattr
*attr
;
2298 id
= nlmsg_data(nlh
);
2299 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2300 id
->spi
= x
->id
.spi
;
2301 id
->family
= x
->props
.family
;
2302 id
->proto
= x
->id
.proto
;
2304 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2306 goto nla_put_failure
;
2311 if (copy_to_user_state_extra(x
, p
, skb
))
2312 goto nla_put_failure
;
2314 nlmsg_end(skb
, nlh
);
2316 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2319 /* Somebody screwed up with xfrm_sa_len! */
2325 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2329 case XFRM_MSG_EXPIRE
:
2330 return xfrm_exp_state_notify(x
, c
);
2331 case XFRM_MSG_NEWAE
:
2332 return xfrm_aevent_state_notify(x
, c
);
2333 case XFRM_MSG_DELSA
:
2334 case XFRM_MSG_UPDSA
:
2335 case XFRM_MSG_NEWSA
:
2336 return xfrm_notify_sa(x
, c
);
2337 case XFRM_MSG_FLUSHSA
:
2338 return xfrm_notify_sa_flush(c
);
2340 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
2348 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2349 struct xfrm_policy
*xp
)
2351 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2352 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2353 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2354 + userpolicy_type_attrsize();
2357 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2358 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2361 struct xfrm_user_acquire
*ua
;
2362 struct nlmsghdr
*nlh
;
2363 __u32 seq
= xfrm_get_acqseq();
2365 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2369 ua
= nlmsg_data(nlh
);
2370 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2371 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2372 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2373 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2374 ua
->aalgos
= xt
->aalgos
;
2375 ua
->ealgos
= xt
->ealgos
;
2376 ua
->calgos
= xt
->calgos
;
2377 ua
->seq
= x
->km
.seq
= seq
;
2379 if (copy_to_user_tmpl(xp
, skb
) < 0)
2381 if (copy_to_user_state_sec_ctx(x
, skb
))
2383 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2386 return nlmsg_end(skb
, nlh
);
2389 nlmsg_cancel(skb
, nlh
);
2393 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2394 struct xfrm_policy
*xp
, int dir
)
2396 struct net
*net
= xs_net(x
);
2397 struct sk_buff
*skb
;
2399 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2403 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2406 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2409 /* User gives us xfrm_user_policy_info followed by an array of 0
2410 * or more templates.
2412 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2413 u8
*data
, int len
, int *dir
)
2415 struct net
*net
= sock_net(sk
);
2416 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2417 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2418 struct xfrm_policy
*xp
;
2421 switch (sk
->sk_family
) {
2423 if (opt
!= IP_XFRM_POLICY
) {
2428 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2430 if (opt
!= IPV6_XFRM_POLICY
) {
2443 if (len
< sizeof(*p
) ||
2444 verify_newpolicy_info(p
))
2447 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2448 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2451 if (p
->dir
> XFRM_POLICY_OUT
)
2454 xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
2460 copy_from_user_policy(xp
, p
);
2461 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2462 copy_templates(xp
, ut
, nr
);
2469 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2471 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2472 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2473 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2474 + userpolicy_type_attrsize();
2477 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2478 int dir
, struct km_event
*c
)
2480 struct xfrm_user_polexpire
*upe
;
2481 struct nlmsghdr
*nlh
;
2482 int hard
= c
->data
.hard
;
2484 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2488 upe
= nlmsg_data(nlh
);
2489 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2490 if (copy_to_user_tmpl(xp
, skb
) < 0)
2492 if (copy_to_user_sec_ctx(xp
, skb
))
2494 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2498 return nlmsg_end(skb
, nlh
);
2501 nlmsg_cancel(skb
, nlh
);
2505 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2507 struct net
*net
= xp_net(xp
);
2508 struct sk_buff
*skb
;
2510 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2514 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2517 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2520 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2522 struct net
*net
= xp_net(xp
);
2523 struct xfrm_userpolicy_info
*p
;
2524 struct xfrm_userpolicy_id
*id
;
2525 struct nlmsghdr
*nlh
;
2526 struct sk_buff
*skb
;
2527 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2530 headlen
= sizeof(*p
);
2531 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2532 len
+= nla_total_size(headlen
);
2533 headlen
= sizeof(*id
);
2535 len
+= userpolicy_type_attrsize();
2536 len
+= NLMSG_ALIGN(headlen
);
2538 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2542 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2546 p
= nlmsg_data(nlh
);
2547 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2548 struct nlattr
*attr
;
2550 id
= nlmsg_data(nlh
);
2551 memset(id
, 0, sizeof(*id
));
2554 id
->index
= xp
->index
;
2556 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2558 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2565 copy_to_user_policy(xp
, p
, dir
);
2566 if (copy_to_user_tmpl(xp
, skb
) < 0)
2568 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2571 nlmsg_end(skb
, nlh
);
2573 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2580 static int xfrm_notify_policy_flush(struct km_event
*c
)
2582 struct net
*net
= c
->net
;
2583 struct nlmsghdr
*nlh
;
2584 struct sk_buff
*skb
;
2586 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2590 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2593 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2596 nlmsg_end(skb
, nlh
);
2598 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2605 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2609 case XFRM_MSG_NEWPOLICY
:
2610 case XFRM_MSG_UPDPOLICY
:
2611 case XFRM_MSG_DELPOLICY
:
2612 return xfrm_notify_policy(xp
, dir
, c
);
2613 case XFRM_MSG_FLUSHPOLICY
:
2614 return xfrm_notify_policy_flush(c
);
2615 case XFRM_MSG_POLEXPIRE
:
2616 return xfrm_exp_policy_notify(xp
, dir
, c
);
2618 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
2625 static inline size_t xfrm_report_msgsize(void)
2627 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2630 static int build_report(struct sk_buff
*skb
, u8 proto
,
2631 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2633 struct xfrm_user_report
*ur
;
2634 struct nlmsghdr
*nlh
;
2636 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2640 ur
= nlmsg_data(nlh
);
2642 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2645 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2647 return nlmsg_end(skb
, nlh
);
2650 nlmsg_cancel(skb
, nlh
);
2654 static int xfrm_send_report(struct net
*net
, u8 proto
,
2655 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2657 struct sk_buff
*skb
;
2659 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2663 if (build_report(skb
, proto
, sel
, addr
) < 0)
2666 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2669 static inline size_t xfrm_mapping_msgsize(void)
2671 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
2674 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
2675 xfrm_address_t
*new_saddr
, __be16 new_sport
)
2677 struct xfrm_user_mapping
*um
;
2678 struct nlmsghdr
*nlh
;
2680 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
2684 um
= nlmsg_data(nlh
);
2686 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
2687 um
->id
.spi
= x
->id
.spi
;
2688 um
->id
.family
= x
->props
.family
;
2689 um
->id
.proto
= x
->id
.proto
;
2690 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
2691 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
2692 um
->new_sport
= new_sport
;
2693 um
->old_sport
= x
->encap
->encap_sport
;
2694 um
->reqid
= x
->props
.reqid
;
2696 return nlmsg_end(skb
, nlh
);
2699 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
2702 struct net
*net
= xs_net(x
);
2703 struct sk_buff
*skb
;
2705 if (x
->id
.proto
!= IPPROTO_ESP
)
2711 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
2715 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
2718 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MAPPING
, GFP_ATOMIC
);
2721 static struct xfrm_mgr netlink_mgr
= {
2723 .notify
= xfrm_send_state_notify
,
2724 .acquire
= xfrm_send_acquire
,
2725 .compile_policy
= xfrm_compile_policy
,
2726 .notify_policy
= xfrm_send_policy_notify
,
2727 .report
= xfrm_send_report
,
2728 .migrate
= xfrm_send_migrate
,
2729 .new_mapping
= xfrm_send_mapping
,
2732 static int __net_init
xfrm_user_net_init(struct net
*net
)
2736 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, XFRMNLGRP_MAX
,
2737 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2740 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
2741 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
2745 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
2748 list_for_each_entry(net
, net_exit_list
, exit_list
)
2749 rcu_assign_pointer(net
->xfrm
.nlsk
, NULL
);
2751 list_for_each_entry(net
, net_exit_list
, exit_list
)
2752 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
2755 static struct pernet_operations xfrm_user_net_ops
= {
2756 .init
= xfrm_user_net_init
,
2757 .exit_batch
= xfrm_user_net_exit
,
2760 static int __init
xfrm_user_init(void)
2764 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2766 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
2769 rv
= xfrm_register_km(&netlink_mgr
);
2771 unregister_pernet_subsys(&xfrm_user_net_ops
);
2775 static void __exit
xfrm_user_exit(void)
2777 xfrm_unregister_km(&netlink_mgr
);
2778 unregister_pernet_subsys(&xfrm_user_net_ops
);
2781 module_init(xfrm_user_init
);
2782 module_exit(xfrm_user_exit
);
2783 MODULE_LICENSE("GPL");
2784 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);