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
))
122 static inline int verify_replay(struct xfrm_usersa_info
*p
,
123 struct nlattr
**attrs
)
125 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_ESN_VAL
];
130 if (p
->replay_window
!= 0)
136 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
137 struct nlattr
**attrs
)
147 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
159 switch (p
->id
.proto
) {
161 if ((!attrs
[XFRMA_ALG_AUTH
] &&
162 !attrs
[XFRMA_ALG_AUTH_TRUNC
]) ||
163 attrs
[XFRMA_ALG_AEAD
] ||
164 attrs
[XFRMA_ALG_CRYPT
] ||
165 attrs
[XFRMA_ALG_COMP
] ||
171 if (attrs
[XFRMA_ALG_COMP
])
173 if (!attrs
[XFRMA_ALG_AUTH
] &&
174 !attrs
[XFRMA_ALG_AUTH_TRUNC
] &&
175 !attrs
[XFRMA_ALG_CRYPT
] &&
176 !attrs
[XFRMA_ALG_AEAD
])
178 if ((attrs
[XFRMA_ALG_AUTH
] ||
179 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
180 attrs
[XFRMA_ALG_CRYPT
]) &&
181 attrs
[XFRMA_ALG_AEAD
])
183 if (attrs
[XFRMA_TFCPAD
] &&
184 p
->mode
!= XFRM_MODE_TUNNEL
)
189 if (!attrs
[XFRMA_ALG_COMP
] ||
190 attrs
[XFRMA_ALG_AEAD
] ||
191 attrs
[XFRMA_ALG_AUTH
] ||
192 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
193 attrs
[XFRMA_ALG_CRYPT
] ||
198 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
199 case IPPROTO_DSTOPTS
:
200 case IPPROTO_ROUTING
:
201 if (attrs
[XFRMA_ALG_COMP
] ||
202 attrs
[XFRMA_ALG_AUTH
] ||
203 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
204 attrs
[XFRMA_ALG_AEAD
] ||
205 attrs
[XFRMA_ALG_CRYPT
] ||
206 attrs
[XFRMA_ENCAP
] ||
207 attrs
[XFRMA_SEC_CTX
] ||
208 attrs
[XFRMA_TFCPAD
] ||
209 !attrs
[XFRMA_COADDR
])
218 if ((err
= verify_aead(attrs
)))
220 if ((err
= verify_auth_trunc(attrs
)))
222 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
224 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
226 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
228 if ((err
= verify_sec_ctx_len(attrs
)))
230 if ((err
= verify_replay(p
, attrs
)))
235 case XFRM_MODE_TRANSPORT
:
236 case XFRM_MODE_TUNNEL
:
237 case XFRM_MODE_ROUTEOPTIMIZATION
:
251 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
252 struct xfrm_algo_desc
*(*get_byname
)(const char *, int),
255 struct xfrm_algo
*p
, *ualg
;
256 struct xfrm_algo_desc
*algo
;
261 ualg
= nla_data(rta
);
263 algo
= get_byname(ualg
->alg_name
, 1);
266 *props
= algo
->desc
.sadb_alg_id
;
268 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
272 strcpy(p
->alg_name
, algo
->name
);
277 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
280 struct xfrm_algo
*ualg
;
281 struct xfrm_algo_auth
*p
;
282 struct xfrm_algo_desc
*algo
;
287 ualg
= nla_data(rta
);
289 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
292 *props
= algo
->desc
.sadb_alg_id
;
294 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
298 strcpy(p
->alg_name
, algo
->name
);
299 p
->alg_key_len
= ualg
->alg_key_len
;
300 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
301 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
307 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
310 struct xfrm_algo_auth
*p
, *ualg
;
311 struct xfrm_algo_desc
*algo
;
316 ualg
= nla_data(rta
);
318 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
321 if ((ualg
->alg_trunc_len
/ 8) > MAX_AH_AUTH_LEN
||
322 ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
324 *props
= algo
->desc
.sadb_alg_id
;
326 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
330 strcpy(p
->alg_name
, algo
->name
);
331 if (!p
->alg_trunc_len
)
332 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
338 static int attach_aead(struct xfrm_algo_aead
**algpp
, u8
*props
,
341 struct xfrm_algo_aead
*p
, *ualg
;
342 struct xfrm_algo_desc
*algo
;
347 ualg
= nla_data(rta
);
349 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
352 *props
= algo
->desc
.sadb_alg_id
;
354 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
358 strcpy(p
->alg_name
, algo
->name
);
363 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn
**replay_esn
,
364 struct xfrm_replay_state_esn
**preplay_esn
,
367 struct xfrm_replay_state_esn
*p
, *pp
, *up
;
374 p
= kmemdup(up
, xfrm_replay_state_esn_len(up
), GFP_KERNEL
);
378 pp
= kmemdup(up
, xfrm_replay_state_esn_len(up
), GFP_KERNEL
);
390 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
395 len
+= sizeof(struct xfrm_user_sec_ctx
);
396 len
+= xfrm_ctx
->ctx_len
;
401 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
403 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
404 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
405 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
406 x
->props
.mode
= p
->mode
;
407 x
->props
.replay_window
= p
->replay_window
;
408 x
->props
.reqid
= p
->reqid
;
409 x
->props
.family
= p
->family
;
410 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
411 x
->props
.flags
= p
->flags
;
413 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
414 x
->sel
.family
= p
->family
;
418 * someday when pfkey also has support, we could have the code
419 * somehow made shareable and move it to xfrm_state.c - JHS
422 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
)
424 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
425 struct nlattr
*re
= attrs
[XFRMA_REPLAY_ESN_VAL
];
426 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
427 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
428 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
431 struct xfrm_replay_state_esn
*replay_esn
;
432 replay_esn
= nla_data(re
);
433 memcpy(x
->replay_esn
, replay_esn
,
434 xfrm_replay_state_esn_len(replay_esn
));
435 memcpy(x
->preplay_esn
, replay_esn
,
436 xfrm_replay_state_esn_len(replay_esn
));
440 struct xfrm_replay_state
*replay
;
441 replay
= nla_data(rp
);
442 memcpy(&x
->replay
, replay
, sizeof(*replay
));
443 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
447 struct xfrm_lifetime_cur
*ltime
;
448 ltime
= nla_data(lt
);
449 x
->curlft
.bytes
= ltime
->bytes
;
450 x
->curlft
.packets
= ltime
->packets
;
451 x
->curlft
.add_time
= ltime
->add_time
;
452 x
->curlft
.use_time
= ltime
->use_time
;
456 x
->replay_maxage
= nla_get_u32(et
);
459 x
->replay_maxdiff
= nla_get_u32(rt
);
462 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
463 struct xfrm_usersa_info
*p
,
464 struct nlattr
**attrs
,
467 struct xfrm_state
*x
= xfrm_state_alloc(net
);
473 copy_from_user_state(x
, p
);
475 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
476 attrs
[XFRMA_ALG_AEAD
])))
478 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
479 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
481 if (!x
->props
.aalgo
) {
482 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
483 attrs
[XFRMA_ALG_AUTH
])))
486 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
487 xfrm_ealg_get_byname
,
488 attrs
[XFRMA_ALG_CRYPT
])))
490 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
491 xfrm_calg_get_byname
,
492 attrs
[XFRMA_ALG_COMP
])))
495 if (attrs
[XFRMA_ENCAP
]) {
496 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
497 sizeof(*x
->encap
), GFP_KERNEL
);
498 if (x
->encap
== NULL
)
502 if (attrs
[XFRMA_TFCPAD
])
503 x
->tfcpad
= nla_get_u32(attrs
[XFRMA_TFCPAD
]);
505 if (attrs
[XFRMA_COADDR
]) {
506 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
507 sizeof(*x
->coaddr
), GFP_KERNEL
);
508 if (x
->coaddr
== NULL
)
512 xfrm_mark_get(attrs
, &x
->mark
);
514 err
= __xfrm_init_state(x
, false);
518 if (attrs
[XFRMA_SEC_CTX
] &&
519 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
522 if ((err
= xfrm_alloc_replay_state_esn(&x
->replay_esn
, &x
->preplay_esn
,
523 attrs
[XFRMA_REPLAY_ESN_VAL
])))
527 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
528 /* sysctl_xfrm_aevent_etime is in 100ms units */
529 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
531 if ((err
= xfrm_init_replay(x
)))
534 /* override default values from above */
535 xfrm_update_ae_params(x
, attrs
);
540 x
->km
.state
= XFRM_STATE_DEAD
;
547 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
548 struct nlattr
**attrs
)
550 struct net
*net
= sock_net(skb
->sk
);
551 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
552 struct xfrm_state
*x
;
555 uid_t loginuid
= audit_get_loginuid(current
);
556 u32 sessionid
= audit_get_sessionid(current
);
559 err
= verify_newsa_info(p
, attrs
);
563 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
568 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
569 err
= xfrm_state_add(x
);
571 err
= xfrm_state_update(x
);
573 security_task_getsecid(current
, &sid
);
574 xfrm_audit_state_add(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
577 x
->km
.state
= XFRM_STATE_DEAD
;
582 c
.seq
= nlh
->nlmsg_seq
;
583 c
.pid
= nlh
->nlmsg_pid
;
584 c
.event
= nlh
->nlmsg_type
;
586 km_state_notify(x
, &c
);
592 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
593 struct xfrm_usersa_id
*p
,
594 struct nlattr
**attrs
,
597 struct xfrm_state
*x
= NULL
;
600 u32 mark
= xfrm_mark_get(attrs
, &m
);
602 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
604 x
= xfrm_state_lookup(net
, mark
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
606 xfrm_address_t
*saddr
= NULL
;
608 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
615 x
= xfrm_state_lookup_byaddr(net
, mark
,
617 p
->proto
, p
->family
);
626 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
627 struct nlattr
**attrs
)
629 struct net
*net
= sock_net(skb
->sk
);
630 struct xfrm_state
*x
;
633 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
634 uid_t loginuid
= audit_get_loginuid(current
);
635 u32 sessionid
= audit_get_sessionid(current
);
638 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
642 if ((err
= security_xfrm_state_delete(x
)) != 0)
645 if (xfrm_state_kern(x
)) {
650 err
= xfrm_state_delete(x
);
655 c
.seq
= nlh
->nlmsg_seq
;
656 c
.pid
= nlh
->nlmsg_pid
;
657 c
.event
= nlh
->nlmsg_type
;
658 km_state_notify(x
, &c
);
661 security_task_getsecid(current
, &sid
);
662 xfrm_audit_state_delete(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
667 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
669 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
670 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
671 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
672 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
673 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
674 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
675 p
->mode
= x
->props
.mode
;
676 p
->replay_window
= x
->props
.replay_window
;
677 p
->reqid
= x
->props
.reqid
;
678 p
->family
= x
->props
.family
;
679 p
->flags
= x
->props
.flags
;
683 struct xfrm_dump_info
{
684 struct sk_buff
*in_skb
;
685 struct sk_buff
*out_skb
;
690 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
692 struct xfrm_user_sec_ctx
*uctx
;
694 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
696 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
700 uctx
= nla_data(attr
);
701 uctx
->exttype
= XFRMA_SEC_CTX
;
702 uctx
->len
= ctx_size
;
703 uctx
->ctx_doi
= s
->ctx_doi
;
704 uctx
->ctx_alg
= s
->ctx_alg
;
705 uctx
->ctx_len
= s
->ctx_len
;
706 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
711 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
713 struct xfrm_algo
*algo
;
716 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
717 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
721 algo
= nla_data(nla
);
722 strcpy(algo
->alg_name
, auth
->alg_name
);
723 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
724 algo
->alg_key_len
= auth
->alg_key_len
;
729 /* Don't change this without updating xfrm_sa_len! */
730 static int copy_to_user_state_extra(struct xfrm_state
*x
,
731 struct xfrm_usersa_info
*p
,
734 copy_to_user_state(x
, p
);
737 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
740 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
743 NLA_PUT(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
745 if (copy_to_user_auth(x
->aalg
, skb
))
746 goto nla_put_failure
;
748 NLA_PUT(skb
, XFRMA_ALG_AUTH_TRUNC
,
749 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
752 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
754 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
757 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
760 NLA_PUT_U32(skb
, XFRMA_TFCPAD
, x
->tfcpad
);
762 if (xfrm_mark_put(skb
, &x
->mark
))
763 goto nla_put_failure
;
766 NLA_PUT(skb
, XFRMA_REPLAY_ESN_VAL
,
767 xfrm_replay_state_esn_len(x
->replay_esn
), x
->replay_esn
);
769 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
770 goto nla_put_failure
;
778 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
780 struct xfrm_dump_info
*sp
= ptr
;
781 struct sk_buff
*in_skb
= sp
->in_skb
;
782 struct sk_buff
*skb
= sp
->out_skb
;
783 struct xfrm_usersa_info
*p
;
784 struct nlmsghdr
*nlh
;
787 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
788 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
794 err
= copy_to_user_state_extra(x
, p
, skb
);
796 goto nla_put_failure
;
802 nlmsg_cancel(skb
, nlh
);
806 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
808 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
809 xfrm_state_walk_done(walk
);
813 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
815 struct net
*net
= sock_net(skb
->sk
);
816 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
817 struct xfrm_dump_info info
;
819 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
820 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
822 info
.in_skb
= cb
->skb
;
824 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
825 info
.nlmsg_flags
= NLM_F_MULTI
;
829 xfrm_state_walk_init(walk
, 0);
832 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
837 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
838 struct xfrm_state
*x
, u32 seq
)
840 struct xfrm_dump_info info
;
843 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
845 return ERR_PTR(-ENOMEM
);
847 info
.in_skb
= in_skb
;
849 info
.nlmsg_seq
= seq
;
850 info
.nlmsg_flags
= 0;
852 if (dump_one_state(x
, 0, &info
)) {
860 static inline size_t xfrm_spdinfo_msgsize(void)
862 return NLMSG_ALIGN(4)
863 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
864 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
867 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
868 u32 pid
, u32 seq
, u32 flags
)
870 struct xfrmk_spdinfo si
;
871 struct xfrmu_spdinfo spc
;
872 struct xfrmu_spdhinfo sph
;
873 struct nlmsghdr
*nlh
;
876 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
877 if (nlh
== NULL
) /* shouldnt really happen ... */
882 xfrm_spd_getinfo(net
, &si
);
883 spc
.incnt
= si
.incnt
;
884 spc
.outcnt
= si
.outcnt
;
885 spc
.fwdcnt
= si
.fwdcnt
;
886 spc
.inscnt
= si
.inscnt
;
887 spc
.outscnt
= si
.outscnt
;
888 spc
.fwdscnt
= si
.fwdscnt
;
889 sph
.spdhcnt
= si
.spdhcnt
;
890 sph
.spdhmcnt
= si
.spdhmcnt
;
892 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
893 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
895 return nlmsg_end(skb
, nlh
);
898 nlmsg_cancel(skb
, nlh
);
902 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
903 struct nlattr
**attrs
)
905 struct net
*net
= sock_net(skb
->sk
);
906 struct sk_buff
*r_skb
;
907 u32
*flags
= nlmsg_data(nlh
);
908 u32 spid
= NETLINK_CB(skb
).pid
;
909 u32 seq
= nlh
->nlmsg_seq
;
911 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
915 if (build_spdinfo(r_skb
, net
, spid
, seq
, *flags
) < 0)
918 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, spid
);
921 static inline size_t xfrm_sadinfo_msgsize(void)
923 return NLMSG_ALIGN(4)
924 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
925 + nla_total_size(4); /* XFRMA_SAD_CNT */
928 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
929 u32 pid
, u32 seq
, u32 flags
)
931 struct xfrmk_sadinfo si
;
932 struct xfrmu_sadhinfo sh
;
933 struct nlmsghdr
*nlh
;
936 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
937 if (nlh
== NULL
) /* shouldnt really happen ... */
942 xfrm_sad_getinfo(net
, &si
);
944 sh
.sadhmcnt
= si
.sadhmcnt
;
945 sh
.sadhcnt
= si
.sadhcnt
;
947 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
948 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
950 return nlmsg_end(skb
, nlh
);
953 nlmsg_cancel(skb
, nlh
);
957 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
958 struct nlattr
**attrs
)
960 struct net
*net
= sock_net(skb
->sk
);
961 struct sk_buff
*r_skb
;
962 u32
*flags
= nlmsg_data(nlh
);
963 u32 spid
= NETLINK_CB(skb
).pid
;
964 u32 seq
= nlh
->nlmsg_seq
;
966 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
970 if (build_sadinfo(r_skb
, net
, spid
, seq
, *flags
) < 0)
973 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, spid
);
976 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
977 struct nlattr
**attrs
)
979 struct net
*net
= sock_net(skb
->sk
);
980 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
981 struct xfrm_state
*x
;
982 struct sk_buff
*resp_skb
;
985 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
989 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
990 if (IS_ERR(resp_skb
)) {
991 err
= PTR_ERR(resp_skb
);
993 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).pid
);
1000 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
1002 switch (p
->info
.id
.proto
) {
1008 /* IPCOMP spi is 16-bits. */
1009 if (p
->max
>= 0x10000)
1017 if (p
->min
> p
->max
)
1023 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1024 struct nlattr
**attrs
)
1026 struct net
*net
= sock_net(skb
->sk
);
1027 struct xfrm_state
*x
;
1028 struct xfrm_userspi_info
*p
;
1029 struct sk_buff
*resp_skb
;
1030 xfrm_address_t
*daddr
;
1036 p
= nlmsg_data(nlh
);
1037 err
= verify_userspi_info(p
);
1041 family
= p
->info
.family
;
1042 daddr
= &p
->info
.id
.daddr
;
1046 mark
= xfrm_mark_get(attrs
, &m
);
1048 x
= xfrm_find_acq_byseq(net
, mark
, p
->info
.seq
);
1049 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
1056 x
= xfrm_find_acq(net
, &m
, p
->info
.mode
, p
->info
.reqid
,
1057 p
->info
.id
.proto
, daddr
,
1064 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
1068 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1069 if (IS_ERR(resp_skb
)) {
1070 err
= PTR_ERR(resp_skb
);
1074 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).pid
);
1082 static int verify_policy_dir(u8 dir
)
1085 case XFRM_POLICY_IN
:
1086 case XFRM_POLICY_OUT
:
1087 case XFRM_POLICY_FWD
:
1097 static int verify_policy_type(u8 type
)
1100 case XFRM_POLICY_TYPE_MAIN
:
1101 #ifdef CONFIG_XFRM_SUB_POLICY
1102 case XFRM_POLICY_TYPE_SUB
:
1113 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1116 case XFRM_SHARE_ANY
:
1117 case XFRM_SHARE_SESSION
:
1118 case XFRM_SHARE_USER
:
1119 case XFRM_SHARE_UNIQUE
:
1126 switch (p
->action
) {
1127 case XFRM_POLICY_ALLOW
:
1128 case XFRM_POLICY_BLOCK
:
1135 switch (p
->sel
.family
) {
1140 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1143 return -EAFNOSUPPORT
;
1150 return verify_policy_dir(p
->dir
);
1153 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1155 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1156 struct xfrm_user_sec_ctx
*uctx
;
1161 uctx
= nla_data(rt
);
1162 return security_xfrm_policy_alloc(&pol
->security
, uctx
);
1165 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1171 for (i
= 0; i
< nr
; i
++, ut
++) {
1172 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1174 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1175 memcpy(&t
->saddr
, &ut
->saddr
,
1176 sizeof(xfrm_address_t
));
1177 t
->reqid
= ut
->reqid
;
1179 t
->share
= ut
->share
;
1180 t
->optional
= ut
->optional
;
1181 t
->aalgos
= ut
->aalgos
;
1182 t
->ealgos
= ut
->ealgos
;
1183 t
->calgos
= ut
->calgos
;
1184 /* If all masks are ~0, then we allow all algorithms. */
1185 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1186 t
->encap_family
= ut
->family
;
1190 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1194 if (nr
> XFRM_MAX_DEPTH
)
1197 for (i
= 0; i
< nr
; i
++) {
1198 /* We never validated the ut->family value, so many
1199 * applications simply leave it at zero. The check was
1200 * never made and ut->family was ignored because all
1201 * templates could be assumed to have the same family as
1202 * the policy itself. Now that we will have ipv4-in-ipv6
1203 * and ipv6-in-ipv4 tunnels, this is no longer true.
1206 ut
[i
].family
= family
;
1208 switch (ut
[i
].family
) {
1211 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1223 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1225 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1230 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1231 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1234 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1238 copy_templates(pol
, utmpl
, nr
);
1243 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1245 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1246 struct xfrm_userpolicy_type
*upt
;
1247 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1255 err
= verify_policy_type(type
);
1263 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1265 xp
->priority
= p
->priority
;
1266 xp
->index
= p
->index
;
1267 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1268 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1269 xp
->action
= p
->action
;
1270 xp
->flags
= p
->flags
;
1271 xp
->family
= p
->sel
.family
;
1272 /* XXX xp->share = p->share; */
1275 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1277 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1278 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1279 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1280 p
->priority
= xp
->priority
;
1281 p
->index
= xp
->index
;
1282 p
->sel
.family
= xp
->family
;
1284 p
->action
= xp
->action
;
1285 p
->flags
= xp
->flags
;
1286 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1289 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1291 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1299 copy_from_user_policy(xp
, p
);
1301 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1305 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1306 err
= copy_from_user_sec_ctx(xp
, attrs
);
1310 xfrm_mark_get(attrs
, &xp
->mark
);
1316 xfrm_policy_destroy(xp
);
1320 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1321 struct nlattr
**attrs
)
1323 struct net
*net
= sock_net(skb
->sk
);
1324 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1325 struct xfrm_policy
*xp
;
1329 uid_t loginuid
= audit_get_loginuid(current
);
1330 u32 sessionid
= audit_get_sessionid(current
);
1333 err
= verify_newpolicy_info(p
);
1336 err
= verify_sec_ctx_len(attrs
);
1340 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1344 /* shouldnt excl be based on nlh flags??
1345 * Aha! this is anti-netlink really i.e more pfkey derived
1346 * in netlink excl is a flag and you wouldnt need
1347 * a type XFRM_MSG_UPDPOLICY - JHS */
1348 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1349 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1350 security_task_getsecid(current
, &sid
);
1351 xfrm_audit_policy_add(xp
, err
? 0 : 1, loginuid
, sessionid
, sid
);
1354 security_xfrm_policy_free(xp
->security
);
1359 c
.event
= nlh
->nlmsg_type
;
1360 c
.seq
= nlh
->nlmsg_seq
;
1361 c
.pid
= nlh
->nlmsg_pid
;
1362 km_policy_notify(xp
, p
->dir
, &c
);
1369 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1371 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1374 if (xp
->xfrm_nr
== 0)
1377 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1378 struct xfrm_user_tmpl
*up
= &vec
[i
];
1379 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1381 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1382 up
->family
= kp
->encap_family
;
1383 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1384 up
->reqid
= kp
->reqid
;
1385 up
->mode
= kp
->mode
;
1386 up
->share
= kp
->share
;
1387 up
->optional
= kp
->optional
;
1388 up
->aalgos
= kp
->aalgos
;
1389 up
->ealgos
= kp
->ealgos
;
1390 up
->calgos
= kp
->calgos
;
1393 return nla_put(skb
, XFRMA_TMPL
,
1394 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1397 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1400 return copy_sec_ctx(x
->security
, skb
);
1405 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1408 return copy_sec_ctx(xp
->security
, skb
);
1412 static inline size_t userpolicy_type_attrsize(void)
1414 #ifdef CONFIG_XFRM_SUB_POLICY
1415 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1421 #ifdef CONFIG_XFRM_SUB_POLICY
1422 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1424 struct xfrm_userpolicy_type upt
= {
1428 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1432 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1438 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1440 struct xfrm_dump_info
*sp
= ptr
;
1441 struct xfrm_userpolicy_info
*p
;
1442 struct sk_buff
*in_skb
= sp
->in_skb
;
1443 struct sk_buff
*skb
= sp
->out_skb
;
1444 struct nlmsghdr
*nlh
;
1446 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1447 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1451 p
= nlmsg_data(nlh
);
1452 copy_to_user_policy(xp
, p
, dir
);
1453 if (copy_to_user_tmpl(xp
, skb
) < 0)
1455 if (copy_to_user_sec_ctx(xp
, skb
))
1457 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1459 if (xfrm_mark_put(skb
, &xp
->mark
))
1460 goto nla_put_failure
;
1462 nlmsg_end(skb
, nlh
);
1467 nlmsg_cancel(skb
, nlh
);
1471 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1473 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1475 xfrm_policy_walk_done(walk
);
1479 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1481 struct net
*net
= sock_net(skb
->sk
);
1482 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1483 struct xfrm_dump_info info
;
1485 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1486 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1488 info
.in_skb
= cb
->skb
;
1490 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1491 info
.nlmsg_flags
= NLM_F_MULTI
;
1495 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1498 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1503 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1504 struct xfrm_policy
*xp
,
1507 struct xfrm_dump_info info
;
1508 struct sk_buff
*skb
;
1510 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1512 return ERR_PTR(-ENOMEM
);
1514 info
.in_skb
= in_skb
;
1516 info
.nlmsg_seq
= seq
;
1517 info
.nlmsg_flags
= 0;
1519 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1527 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1528 struct nlattr
**attrs
)
1530 struct net
*net
= sock_net(skb
->sk
);
1531 struct xfrm_policy
*xp
;
1532 struct xfrm_userpolicy_id
*p
;
1533 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1538 u32 mark
= xfrm_mark_get(attrs
, &m
);
1540 p
= nlmsg_data(nlh
);
1541 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1543 err
= copy_from_user_policy_type(&type
, attrs
);
1547 err
= verify_policy_dir(p
->dir
);
1552 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, delete, &err
);
1554 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1555 struct xfrm_sec_ctx
*ctx
;
1557 err
= verify_sec_ctx_len(attrs
);
1563 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1565 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1569 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
, &p
->sel
,
1571 security_xfrm_policy_free(ctx
);
1577 struct sk_buff
*resp_skb
;
1579 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1580 if (IS_ERR(resp_skb
)) {
1581 err
= PTR_ERR(resp_skb
);
1583 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1584 NETLINK_CB(skb
).pid
);
1587 uid_t loginuid
= audit_get_loginuid(current
);
1588 u32 sessionid
= audit_get_sessionid(current
);
1591 security_task_getsecid(current
, &sid
);
1592 xfrm_audit_policy_delete(xp
, err
? 0 : 1, loginuid
, sessionid
,
1598 c
.data
.byid
= p
->index
;
1599 c
.event
= nlh
->nlmsg_type
;
1600 c
.seq
= nlh
->nlmsg_seq
;
1601 c
.pid
= nlh
->nlmsg_pid
;
1602 km_policy_notify(xp
, p
->dir
, &c
);
1610 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1611 struct nlattr
**attrs
)
1613 struct net
*net
= sock_net(skb
->sk
);
1615 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1616 struct xfrm_audit audit_info
;
1619 audit_info
.loginuid
= audit_get_loginuid(current
);
1620 audit_info
.sessionid
= audit_get_sessionid(current
);
1621 security_task_getsecid(current
, &audit_info
.secid
);
1622 err
= xfrm_state_flush(net
, p
->proto
, &audit_info
);
1624 if (err
== -ESRCH
) /* empty table */
1628 c
.data
.proto
= p
->proto
;
1629 c
.event
= nlh
->nlmsg_type
;
1630 c
.seq
= nlh
->nlmsg_seq
;
1631 c
.pid
= nlh
->nlmsg_pid
;
1633 km_state_notify(NULL
, &c
);
1638 static inline size_t xfrm_aevent_msgsize(struct xfrm_state
*x
)
1640 size_t replay_size
= x
->replay_esn
?
1641 xfrm_replay_state_esn_len(x
->replay_esn
) :
1642 sizeof(struct xfrm_replay_state
);
1644 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1645 + nla_total_size(replay_size
)
1646 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1647 + nla_total_size(sizeof(struct xfrm_mark
))
1648 + nla_total_size(4) /* XFRM_AE_RTHR */
1649 + nla_total_size(4); /* XFRM_AE_ETHR */
1652 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
1654 struct xfrm_aevent_id
*id
;
1655 struct nlmsghdr
*nlh
;
1657 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1661 id
= nlmsg_data(nlh
);
1662 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1663 id
->sa_id
.spi
= x
->id
.spi
;
1664 id
->sa_id
.family
= x
->props
.family
;
1665 id
->sa_id
.proto
= x
->id
.proto
;
1666 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1667 id
->reqid
= x
->props
.reqid
;
1668 id
->flags
= c
->data
.aevent
;
1671 NLA_PUT(skb
, XFRMA_REPLAY_ESN_VAL
,
1672 xfrm_replay_state_esn_len(x
->replay_esn
),
1675 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1677 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1679 if (id
->flags
& XFRM_AE_RTHR
)
1680 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1682 if (id
->flags
& XFRM_AE_ETHR
)
1683 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1684 x
->replay_maxage
* 10 / HZ
);
1686 if (xfrm_mark_put(skb
, &x
->mark
))
1687 goto nla_put_failure
;
1689 return nlmsg_end(skb
, nlh
);
1692 nlmsg_cancel(skb
, nlh
);
1696 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1697 struct nlattr
**attrs
)
1699 struct net
*net
= sock_net(skb
->sk
);
1700 struct xfrm_state
*x
;
1701 struct sk_buff
*r_skb
;
1706 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1707 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1709 mark
= xfrm_mark_get(attrs
, &m
);
1711 x
= xfrm_state_lookup(net
, mark
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1715 r_skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
1716 if (r_skb
== NULL
) {
1722 * XXX: is this lock really needed - none of the other
1723 * gets lock (the concern is things getting updated
1724 * while we are still reading) - jhs
1726 spin_lock_bh(&x
->lock
);
1727 c
.data
.aevent
= p
->flags
;
1728 c
.seq
= nlh
->nlmsg_seq
;
1729 c
.pid
= nlh
->nlmsg_pid
;
1731 if (build_aevent(r_skb
, x
, &c
) < 0)
1733 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).pid
);
1734 spin_unlock_bh(&x
->lock
);
1739 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1740 struct nlattr
**attrs
)
1742 struct net
*net
= sock_net(skb
->sk
);
1743 struct xfrm_state
*x
;
1748 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1749 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1750 struct nlattr
*re
= attrs
[XFRMA_REPLAY_ESN_VAL
];
1751 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1753 if (!lt
&& !rp
&& !re
)
1756 /* pedantic mode - thou shalt sayeth replaceth */
1757 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1760 mark
= xfrm_mark_get(attrs
, &m
);
1762 x
= xfrm_state_lookup(net
, mark
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1766 if (x
->km
.state
!= XFRM_STATE_VALID
)
1769 spin_lock_bh(&x
->lock
);
1770 xfrm_update_ae_params(x
, attrs
);
1771 spin_unlock_bh(&x
->lock
);
1773 c
.event
= nlh
->nlmsg_type
;
1774 c
.seq
= nlh
->nlmsg_seq
;
1775 c
.pid
= nlh
->nlmsg_pid
;
1776 c
.data
.aevent
= XFRM_AE_CU
;
1777 km_state_notify(x
, &c
);
1784 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1785 struct nlattr
**attrs
)
1787 struct net
*net
= sock_net(skb
->sk
);
1789 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1791 struct xfrm_audit audit_info
;
1793 err
= copy_from_user_policy_type(&type
, attrs
);
1797 audit_info
.loginuid
= audit_get_loginuid(current
);
1798 audit_info
.sessionid
= audit_get_sessionid(current
);
1799 security_task_getsecid(current
, &audit_info
.secid
);
1800 err
= xfrm_policy_flush(net
, type
, &audit_info
);
1802 if (err
== -ESRCH
) /* empty table */
1808 c
.event
= nlh
->nlmsg_type
;
1809 c
.seq
= nlh
->nlmsg_seq
;
1810 c
.pid
= nlh
->nlmsg_pid
;
1812 km_policy_notify(NULL
, 0, &c
);
1816 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1817 struct nlattr
**attrs
)
1819 struct net
*net
= sock_net(skb
->sk
);
1820 struct xfrm_policy
*xp
;
1821 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1822 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1823 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1826 u32 mark
= xfrm_mark_get(attrs
, &m
);
1828 err
= copy_from_user_policy_type(&type
, attrs
);
1832 err
= verify_policy_dir(p
->dir
);
1837 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, 0, &err
);
1839 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1840 struct xfrm_sec_ctx
*ctx
;
1842 err
= verify_sec_ctx_len(attrs
);
1848 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1850 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1854 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
,
1855 &p
->sel
, ctx
, 0, &err
);
1856 security_xfrm_policy_free(ctx
);
1861 if (unlikely(xp
->walk
.dead
))
1866 uid_t loginuid
= audit_get_loginuid(current
);
1867 u32 sessionid
= audit_get_sessionid(current
);
1870 security_task_getsecid(current
, &sid
);
1871 xfrm_policy_delete(xp
, p
->dir
);
1872 xfrm_audit_policy_delete(xp
, 1, loginuid
, sessionid
, sid
);
1875 // reset the timers here?
1876 WARN(1, "Dont know what to do with soft policy expire\n");
1878 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1885 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1886 struct nlattr
**attrs
)
1888 struct net
*net
= sock_net(skb
->sk
);
1889 struct xfrm_state
*x
;
1891 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1892 struct xfrm_usersa_info
*p
= &ue
->state
;
1894 u32 mark
= xfrm_mark_get(attrs
, &m
);
1896 x
= xfrm_state_lookup(net
, mark
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1902 spin_lock_bh(&x
->lock
);
1904 if (x
->km
.state
!= XFRM_STATE_VALID
)
1906 km_state_expired(x
, ue
->hard
, current
->pid
);
1909 uid_t loginuid
= audit_get_loginuid(current
);
1910 u32 sessionid
= audit_get_sessionid(current
);
1913 security_task_getsecid(current
, &sid
);
1914 __xfrm_state_delete(x
);
1915 xfrm_audit_state_delete(x
, 1, loginuid
, sessionid
, sid
);
1919 spin_unlock_bh(&x
->lock
);
1924 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1925 struct nlattr
**attrs
)
1927 struct net
*net
= sock_net(skb
->sk
);
1928 struct xfrm_policy
*xp
;
1929 struct xfrm_user_tmpl
*ut
;
1931 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1932 struct xfrm_mark mark
;
1934 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1935 struct xfrm_state
*x
= xfrm_state_alloc(net
);
1941 xfrm_mark_get(attrs
, &mark
);
1943 err
= verify_newpolicy_info(&ua
->policy
);
1948 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
1952 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1953 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1954 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1955 xp
->mark
.m
= x
->mark
.m
= mark
.m
;
1956 xp
->mark
.v
= x
->mark
.v
= mark
.v
;
1958 /* extract the templates and for each call km_key */
1959 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1960 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1961 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1962 x
->props
.mode
= t
->mode
;
1963 x
->props
.reqid
= t
->reqid
;
1964 x
->props
.family
= ut
->family
;
1965 t
->aalgos
= ua
->aalgos
;
1966 t
->ealgos
= ua
->ealgos
;
1967 t
->calgos
= ua
->calgos
;
1968 err
= km_query(x
, t
, xp
);
1978 WARN(1, "BAD policy passed\n");
1985 #ifdef CONFIG_XFRM_MIGRATE
1986 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1987 struct xfrm_kmaddress
*k
,
1988 struct nlattr
**attrs
, int *num
)
1990 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
1991 struct xfrm_user_migrate
*um
;
1995 struct xfrm_user_kmaddress
*uk
;
1997 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
1998 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
1999 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
2000 k
->family
= uk
->family
;
2001 k
->reserved
= uk
->reserved
;
2005 num_migrate
= nla_len(rt
) / sizeof(*um
);
2007 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
2010 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
2011 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
2012 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
2013 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
2014 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
2016 ma
->proto
= um
->proto
;
2017 ma
->mode
= um
->mode
;
2018 ma
->reqid
= um
->reqid
;
2020 ma
->old_family
= um
->old_family
;
2021 ma
->new_family
= um
->new_family
;
2028 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2029 struct nlattr
**attrs
)
2031 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
2032 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2033 struct xfrm_kmaddress km
, *kmp
;
2038 if (attrs
[XFRMA_MIGRATE
] == NULL
)
2041 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
2043 err
= copy_from_user_policy_type(&type
, attrs
);
2047 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
2054 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
);
2059 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2060 struct nlattr
**attrs
)
2062 return -ENOPROTOOPT
;
2066 #ifdef CONFIG_XFRM_MIGRATE
2067 static int copy_to_user_migrate(const struct xfrm_migrate
*m
, struct sk_buff
*skb
)
2069 struct xfrm_user_migrate um
;
2071 memset(&um
, 0, sizeof(um
));
2072 um
.proto
= m
->proto
;
2074 um
.reqid
= m
->reqid
;
2075 um
.old_family
= m
->old_family
;
2076 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
2077 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
2078 um
.new_family
= m
->new_family
;
2079 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
2080 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
2082 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
2085 static int copy_to_user_kmaddress(const struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
2087 struct xfrm_user_kmaddress uk
;
2089 memset(&uk
, 0, sizeof(uk
));
2090 uk
.family
= k
->family
;
2091 uk
.reserved
= k
->reserved
;
2092 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
2093 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
2095 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
2098 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
)
2100 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
2101 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
2102 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
2103 + userpolicy_type_attrsize();
2106 static int build_migrate(struct sk_buff
*skb
, const struct xfrm_migrate
*m
,
2107 int num_migrate
, const struct xfrm_kmaddress
*k
,
2108 const struct xfrm_selector
*sel
, u8 dir
, u8 type
)
2110 const struct xfrm_migrate
*mp
;
2111 struct xfrm_userpolicy_id
*pol_id
;
2112 struct nlmsghdr
*nlh
;
2115 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
2119 pol_id
= nlmsg_data(nlh
);
2120 /* copy data from selector, dir, and type to the pol_id */
2121 memset(pol_id
, 0, sizeof(*pol_id
));
2122 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
2125 if (k
!= NULL
&& (copy_to_user_kmaddress(k
, skb
) < 0))
2128 if (copy_to_user_policy_type(type
, skb
) < 0)
2131 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2132 if (copy_to_user_migrate(mp
, skb
) < 0)
2136 return nlmsg_end(skb
, nlh
);
2138 nlmsg_cancel(skb
, nlh
);
2142 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2143 const struct xfrm_migrate
*m
, int num_migrate
,
2144 const struct xfrm_kmaddress
*k
)
2146 struct net
*net
= &init_net
;
2147 struct sk_buff
*skb
;
2149 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
), GFP_ATOMIC
);
2154 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, dir
, type
) < 0)
2157 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
2160 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2161 const struct xfrm_migrate
*m
, int num_migrate
,
2162 const struct xfrm_kmaddress
*k
)
2164 return -ENOPROTOOPT
;
2168 #define XMSGSIZE(type) sizeof(struct type)
2170 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2171 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2172 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2173 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2174 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2175 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2176 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2177 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2178 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2179 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2180 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2181 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2182 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2183 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2184 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2185 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2186 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2187 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2188 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2189 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2190 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2195 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2196 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2197 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2198 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2199 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2200 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2201 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2202 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2203 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2204 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2205 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2206 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2207 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2208 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2209 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2210 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2211 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2212 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2213 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2214 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2215 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2216 [XFRMA_MARK
] = { .len
= sizeof(struct xfrm_mark
) },
2217 [XFRMA_TFCPAD
] = { .type
= NLA_U32
},
2218 [XFRMA_REPLAY_ESN_VAL
] = { .len
= sizeof(struct xfrm_replay_state_esn
) },
2221 static struct xfrm_link
{
2222 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2223 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2224 int (*done
)(struct netlink_callback
*);
2225 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2226 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2227 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2228 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2229 .dump
= xfrm_dump_sa
,
2230 .done
= xfrm_dump_sa_done
},
2231 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2232 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2233 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2234 .dump
= xfrm_dump_policy
,
2235 .done
= xfrm_dump_policy_done
},
2236 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2237 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2238 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2239 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2240 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2241 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2242 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2243 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2244 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2245 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2246 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2247 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2248 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2251 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2253 struct net
*net
= sock_net(skb
->sk
);
2254 struct nlattr
*attrs
[XFRMA_MAX
+1];
2255 struct xfrm_link
*link
;
2258 type
= nlh
->nlmsg_type
;
2259 if (type
> XFRM_MSG_MAX
)
2262 type
-= XFRM_MSG_BASE
;
2263 link
= &xfrm_dispatch
[type
];
2265 /* All operations require privileges, even GET */
2266 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
2269 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2270 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2271 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2272 if (link
->dump
== NULL
)
2275 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, link
->dump
, link
->done
);
2278 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
2283 if (link
->doit
== NULL
)
2286 return link
->doit(skb
, nlh
, attrs
);
2289 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2291 mutex_lock(&xfrm_cfg_mutex
);
2292 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2293 mutex_unlock(&xfrm_cfg_mutex
);
2296 static inline size_t xfrm_expire_msgsize(void)
2298 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
))
2299 + nla_total_size(sizeof(struct xfrm_mark
));
2302 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
2304 struct xfrm_user_expire
*ue
;
2305 struct nlmsghdr
*nlh
;
2307 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2311 ue
= nlmsg_data(nlh
);
2312 copy_to_user_state(x
, &ue
->state
);
2313 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2315 if (xfrm_mark_put(skb
, &x
->mark
))
2316 goto nla_put_failure
;
2318 return nlmsg_end(skb
, nlh
);
2324 static int xfrm_exp_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2326 struct net
*net
= xs_net(x
);
2327 struct sk_buff
*skb
;
2329 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2333 if (build_expire(skb
, x
, c
) < 0) {
2338 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2341 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2343 struct net
*net
= xs_net(x
);
2344 struct sk_buff
*skb
;
2346 skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
2350 if (build_aevent(skb
, x
, c
) < 0)
2353 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
2356 static int xfrm_notify_sa_flush(const struct km_event
*c
)
2358 struct net
*net
= c
->net
;
2359 struct xfrm_usersa_flush
*p
;
2360 struct nlmsghdr
*nlh
;
2361 struct sk_buff
*skb
;
2362 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2364 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2368 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2374 p
= nlmsg_data(nlh
);
2375 p
->proto
= c
->data
.proto
;
2377 nlmsg_end(skb
, nlh
);
2379 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2382 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2386 l
+= nla_total_size(aead_len(x
->aead
));
2388 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2389 (x
->aalg
->alg_key_len
+ 7) / 8);
2390 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2393 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2395 l
+= nla_total_size(sizeof(*x
->calg
));
2397 l
+= nla_total_size(sizeof(*x
->encap
));
2399 l
+= nla_total_size(sizeof(x
->tfcpad
));
2401 l
+= nla_total_size(xfrm_replay_state_esn_len(x
->replay_esn
));
2403 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2404 x
->security
->ctx_len
);
2406 l
+= nla_total_size(sizeof(*x
->coaddr
));
2408 /* Must count x->lastused as it may become non-zero behind our back. */
2409 l
+= nla_total_size(sizeof(u64
));
2414 static int xfrm_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
2416 struct net
*net
= xs_net(x
);
2417 struct xfrm_usersa_info
*p
;
2418 struct xfrm_usersa_id
*id
;
2419 struct nlmsghdr
*nlh
;
2420 struct sk_buff
*skb
;
2421 int len
= xfrm_sa_len(x
);
2424 headlen
= sizeof(*p
);
2425 if (c
->event
== XFRM_MSG_DELSA
) {
2426 len
+= nla_total_size(headlen
);
2427 headlen
= sizeof(*id
);
2428 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2430 len
+= NLMSG_ALIGN(headlen
);
2432 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2436 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2438 goto nla_put_failure
;
2440 p
= nlmsg_data(nlh
);
2441 if (c
->event
== XFRM_MSG_DELSA
) {
2442 struct nlattr
*attr
;
2444 id
= nlmsg_data(nlh
);
2445 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2446 id
->spi
= x
->id
.spi
;
2447 id
->family
= x
->props
.family
;
2448 id
->proto
= x
->id
.proto
;
2450 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2452 goto nla_put_failure
;
2457 if (copy_to_user_state_extra(x
, p
, skb
))
2458 goto nla_put_failure
;
2460 nlmsg_end(skb
, nlh
);
2462 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2465 /* Somebody screwed up with xfrm_sa_len! */
2471 static int xfrm_send_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2475 case XFRM_MSG_EXPIRE
:
2476 return xfrm_exp_state_notify(x
, c
);
2477 case XFRM_MSG_NEWAE
:
2478 return xfrm_aevent_state_notify(x
, c
);
2479 case XFRM_MSG_DELSA
:
2480 case XFRM_MSG_UPDSA
:
2481 case XFRM_MSG_NEWSA
:
2482 return xfrm_notify_sa(x
, c
);
2483 case XFRM_MSG_FLUSHSA
:
2484 return xfrm_notify_sa_flush(c
);
2486 printk(KERN_NOTICE
"xfrm_user: Unknown SA event %d\n",
2495 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2496 struct xfrm_policy
*xp
)
2498 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2499 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2500 + nla_total_size(sizeof(struct xfrm_mark
))
2501 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2502 + userpolicy_type_attrsize();
2505 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2506 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2509 struct xfrm_user_acquire
*ua
;
2510 struct nlmsghdr
*nlh
;
2511 __u32 seq
= xfrm_get_acqseq();
2513 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2517 ua
= nlmsg_data(nlh
);
2518 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2519 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2520 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2521 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2522 ua
->aalgos
= xt
->aalgos
;
2523 ua
->ealgos
= xt
->ealgos
;
2524 ua
->calgos
= xt
->calgos
;
2525 ua
->seq
= x
->km
.seq
= seq
;
2527 if (copy_to_user_tmpl(xp
, skb
) < 0)
2529 if (copy_to_user_state_sec_ctx(x
, skb
))
2531 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2533 if (xfrm_mark_put(skb
, &xp
->mark
))
2534 goto nla_put_failure
;
2536 return nlmsg_end(skb
, nlh
);
2540 nlmsg_cancel(skb
, nlh
);
2544 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2545 struct xfrm_policy
*xp
, int dir
)
2547 struct net
*net
= xs_net(x
);
2548 struct sk_buff
*skb
;
2550 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2554 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2557 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2560 /* User gives us xfrm_user_policy_info followed by an array of 0
2561 * or more templates.
2563 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2564 u8
*data
, int len
, int *dir
)
2566 struct net
*net
= sock_net(sk
);
2567 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2568 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2569 struct xfrm_policy
*xp
;
2572 switch (sk
->sk_family
) {
2574 if (opt
!= IP_XFRM_POLICY
) {
2579 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2581 if (opt
!= IPV6_XFRM_POLICY
) {
2594 if (len
< sizeof(*p
) ||
2595 verify_newpolicy_info(p
))
2598 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2599 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2602 if (p
->dir
> XFRM_POLICY_OUT
)
2605 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
2611 copy_from_user_policy(xp
, p
);
2612 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2613 copy_templates(xp
, ut
, nr
);
2620 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2622 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2623 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2624 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2625 + nla_total_size(sizeof(struct xfrm_mark
))
2626 + userpolicy_type_attrsize();
2629 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2630 int dir
, const struct km_event
*c
)
2632 struct xfrm_user_polexpire
*upe
;
2633 struct nlmsghdr
*nlh
;
2634 int hard
= c
->data
.hard
;
2636 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2640 upe
= nlmsg_data(nlh
);
2641 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2642 if (copy_to_user_tmpl(xp
, skb
) < 0)
2644 if (copy_to_user_sec_ctx(xp
, skb
))
2646 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2648 if (xfrm_mark_put(skb
, &xp
->mark
))
2649 goto nla_put_failure
;
2652 return nlmsg_end(skb
, nlh
);
2656 nlmsg_cancel(skb
, nlh
);
2660 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2662 struct net
*net
= xp_net(xp
);
2663 struct sk_buff
*skb
;
2665 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2669 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2672 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2675 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2677 struct net
*net
= xp_net(xp
);
2678 struct xfrm_userpolicy_info
*p
;
2679 struct xfrm_userpolicy_id
*id
;
2680 struct nlmsghdr
*nlh
;
2681 struct sk_buff
*skb
;
2682 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2685 headlen
= sizeof(*p
);
2686 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2687 len
+= nla_total_size(headlen
);
2688 headlen
= sizeof(*id
);
2690 len
+= userpolicy_type_attrsize();
2691 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2692 len
+= NLMSG_ALIGN(headlen
);
2694 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2698 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2702 p
= nlmsg_data(nlh
);
2703 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2704 struct nlattr
*attr
;
2706 id
= nlmsg_data(nlh
);
2707 memset(id
, 0, sizeof(*id
));
2710 id
->index
= xp
->index
;
2712 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2714 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2721 copy_to_user_policy(xp
, p
, dir
);
2722 if (copy_to_user_tmpl(xp
, skb
) < 0)
2724 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2727 if (xfrm_mark_put(skb
, &xp
->mark
))
2728 goto nla_put_failure
;
2730 nlmsg_end(skb
, nlh
);
2732 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2740 static int xfrm_notify_policy_flush(const struct km_event
*c
)
2742 struct net
*net
= c
->net
;
2743 struct nlmsghdr
*nlh
;
2744 struct sk_buff
*skb
;
2746 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2750 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2753 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2756 nlmsg_end(skb
, nlh
);
2758 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2765 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2769 case XFRM_MSG_NEWPOLICY
:
2770 case XFRM_MSG_UPDPOLICY
:
2771 case XFRM_MSG_DELPOLICY
:
2772 return xfrm_notify_policy(xp
, dir
, c
);
2773 case XFRM_MSG_FLUSHPOLICY
:
2774 return xfrm_notify_policy_flush(c
);
2775 case XFRM_MSG_POLEXPIRE
:
2776 return xfrm_exp_policy_notify(xp
, dir
, c
);
2778 printk(KERN_NOTICE
"xfrm_user: Unknown Policy event %d\n",
2786 static inline size_t xfrm_report_msgsize(void)
2788 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2791 static int build_report(struct sk_buff
*skb
, u8 proto
,
2792 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2794 struct xfrm_user_report
*ur
;
2795 struct nlmsghdr
*nlh
;
2797 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2801 ur
= nlmsg_data(nlh
);
2803 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2806 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2808 return nlmsg_end(skb
, nlh
);
2811 nlmsg_cancel(skb
, nlh
);
2815 static int xfrm_send_report(struct net
*net
, u8 proto
,
2816 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2818 struct sk_buff
*skb
;
2820 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2824 if (build_report(skb
, proto
, sel
, addr
) < 0)
2827 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2830 static inline size_t xfrm_mapping_msgsize(void)
2832 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
2835 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
2836 xfrm_address_t
*new_saddr
, __be16 new_sport
)
2838 struct xfrm_user_mapping
*um
;
2839 struct nlmsghdr
*nlh
;
2841 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
2845 um
= nlmsg_data(nlh
);
2847 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
2848 um
->id
.spi
= x
->id
.spi
;
2849 um
->id
.family
= x
->props
.family
;
2850 um
->id
.proto
= x
->id
.proto
;
2851 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
2852 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
2853 um
->new_sport
= new_sport
;
2854 um
->old_sport
= x
->encap
->encap_sport
;
2855 um
->reqid
= x
->props
.reqid
;
2857 return nlmsg_end(skb
, nlh
);
2860 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
2863 struct net
*net
= xs_net(x
);
2864 struct sk_buff
*skb
;
2866 if (x
->id
.proto
!= IPPROTO_ESP
)
2872 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
2876 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
2879 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MAPPING
, GFP_ATOMIC
);
2882 static struct xfrm_mgr netlink_mgr
= {
2884 .notify
= xfrm_send_state_notify
,
2885 .acquire
= xfrm_send_acquire
,
2886 .compile_policy
= xfrm_compile_policy
,
2887 .notify_policy
= xfrm_send_policy_notify
,
2888 .report
= xfrm_send_report
,
2889 .migrate
= xfrm_send_migrate
,
2890 .new_mapping
= xfrm_send_mapping
,
2893 static int __net_init
xfrm_user_net_init(struct net
*net
)
2897 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, XFRMNLGRP_MAX
,
2898 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2901 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
2902 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
2906 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
2909 list_for_each_entry(net
, net_exit_list
, exit_list
)
2910 rcu_assign_pointer(net
->xfrm
.nlsk
, NULL
);
2912 list_for_each_entry(net
, net_exit_list
, exit_list
)
2913 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
2916 static struct pernet_operations xfrm_user_net_ops
= {
2917 .init
= xfrm_user_net_init
,
2918 .exit_batch
= xfrm_user_net_exit
,
2921 static int __init
xfrm_user_init(void)
2925 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2927 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
2930 rv
= xfrm_register_km(&netlink_mgr
);
2932 unregister_pernet_subsys(&xfrm_user_net_ops
);
2936 static void __exit
xfrm_user_exit(void)
2938 xfrm_unregister_km(&netlink_mgr
);
2939 unregister_pernet_subsys(&xfrm_user_net_ops
);
2942 module_init(xfrm_user_init
);
2943 module_exit(xfrm_user_exit
);
2944 MODULE_LICENSE("GPL");
2945 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);