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 IS_ENABLED(CONFIG_IPV6)
32 #include <linux/in6.h>
35 static int verify_one_alg(struct nlattr
**attrs
, enum xfrm_attr_type_t type
)
37 struct nlattr
*rt
= attrs
[type
];
38 struct xfrm_algo
*algp
;
44 if (nla_len(rt
) < xfrm_alg_len(algp
))
57 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
61 static int verify_auth_trunc(struct nlattr
**attrs
)
63 struct nlattr
*rt
= attrs
[XFRMA_ALG_AUTH_TRUNC
];
64 struct xfrm_algo_auth
*algp
;
70 if (nla_len(rt
) < xfrm_alg_auth_len(algp
))
73 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
77 static int verify_aead(struct nlattr
**attrs
)
79 struct nlattr
*rt
= attrs
[XFRMA_ALG_AEAD
];
80 struct xfrm_algo_aead
*algp
;
86 if (nla_len(rt
) < aead_len(algp
))
89 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
93 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
94 xfrm_address_t
**addrp
)
96 struct nlattr
*rt
= attrs
[type
];
99 *addrp
= nla_data(rt
);
102 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
104 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
105 struct xfrm_user_sec_ctx
*uctx
;
111 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
117 static inline int verify_replay(struct xfrm_usersa_info
*p
,
118 struct nlattr
**attrs
)
120 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_ESN_VAL
];
121 struct xfrm_replay_state_esn
*rs
;
123 if (p
->flags
& XFRM_STATE_ESN
) {
129 if (rs
->bmp_len
> XFRMA_REPLAY_ESN_MAX
/ sizeof(rs
->bmp
[0]) / 8)
132 if (nla_len(rt
) < xfrm_replay_state_esn_len(rs
) &&
133 nla_len(rt
) != sizeof(*rs
))
140 /* As only ESP and AH support ESN feature. */
141 if ((p
->id
.proto
!= IPPROTO_ESP
) && (p
->id
.proto
!= IPPROTO_AH
))
144 if (p
->replay_window
!= 0)
150 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
151 struct nlattr
**attrs
)
161 #if IS_ENABLED(CONFIG_IPV6)
173 switch (p
->id
.proto
) {
175 if ((!attrs
[XFRMA_ALG_AUTH
] &&
176 !attrs
[XFRMA_ALG_AUTH_TRUNC
]) ||
177 attrs
[XFRMA_ALG_AEAD
] ||
178 attrs
[XFRMA_ALG_CRYPT
] ||
179 attrs
[XFRMA_ALG_COMP
] ||
185 if (attrs
[XFRMA_ALG_COMP
])
187 if (!attrs
[XFRMA_ALG_AUTH
] &&
188 !attrs
[XFRMA_ALG_AUTH_TRUNC
] &&
189 !attrs
[XFRMA_ALG_CRYPT
] &&
190 !attrs
[XFRMA_ALG_AEAD
])
192 if ((attrs
[XFRMA_ALG_AUTH
] ||
193 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
194 attrs
[XFRMA_ALG_CRYPT
]) &&
195 attrs
[XFRMA_ALG_AEAD
])
197 if (attrs
[XFRMA_TFCPAD
] &&
198 p
->mode
!= XFRM_MODE_TUNNEL
)
203 if (!attrs
[XFRMA_ALG_COMP
] ||
204 attrs
[XFRMA_ALG_AEAD
] ||
205 attrs
[XFRMA_ALG_AUTH
] ||
206 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
207 attrs
[XFRMA_ALG_CRYPT
] ||
208 attrs
[XFRMA_TFCPAD
] ||
209 (ntohl(p
->id
.spi
) >= 0x10000))
213 #if IS_ENABLED(CONFIG_IPV6)
214 case IPPROTO_DSTOPTS
:
215 case IPPROTO_ROUTING
:
216 if (attrs
[XFRMA_ALG_COMP
] ||
217 attrs
[XFRMA_ALG_AUTH
] ||
218 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
219 attrs
[XFRMA_ALG_AEAD
] ||
220 attrs
[XFRMA_ALG_CRYPT
] ||
221 attrs
[XFRMA_ENCAP
] ||
222 attrs
[XFRMA_SEC_CTX
] ||
223 attrs
[XFRMA_TFCPAD
] ||
224 !attrs
[XFRMA_COADDR
])
233 if ((err
= verify_aead(attrs
)))
235 if ((err
= verify_auth_trunc(attrs
)))
237 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
239 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
241 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
243 if ((err
= verify_sec_ctx_len(attrs
)))
245 if ((err
= verify_replay(p
, attrs
)))
250 case XFRM_MODE_TRANSPORT
:
251 case XFRM_MODE_TUNNEL
:
252 case XFRM_MODE_ROUTEOPTIMIZATION
:
266 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
267 struct xfrm_algo_desc
*(*get_byname
)(const char *, int),
270 struct xfrm_algo
*p
, *ualg
;
271 struct xfrm_algo_desc
*algo
;
276 ualg
= nla_data(rta
);
278 algo
= get_byname(ualg
->alg_name
, 1);
281 *props
= algo
->desc
.sadb_alg_id
;
283 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
287 strcpy(p
->alg_name
, algo
->name
);
292 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
295 struct xfrm_algo
*ualg
;
296 struct xfrm_algo_auth
*p
;
297 struct xfrm_algo_desc
*algo
;
302 ualg
= nla_data(rta
);
304 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
307 *props
= algo
->desc
.sadb_alg_id
;
309 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
313 strcpy(p
->alg_name
, algo
->name
);
314 p
->alg_key_len
= ualg
->alg_key_len
;
315 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
316 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
322 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
325 struct xfrm_algo_auth
*p
, *ualg
;
326 struct xfrm_algo_desc
*algo
;
331 ualg
= nla_data(rta
);
333 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
336 if (ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
338 *props
= algo
->desc
.sadb_alg_id
;
340 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
344 strcpy(p
->alg_name
, algo
->name
);
345 if (!p
->alg_trunc_len
)
346 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
352 static int attach_aead(struct xfrm_algo_aead
**algpp
, u8
*props
,
355 struct xfrm_algo_aead
*p
, *ualg
;
356 struct xfrm_algo_desc
*algo
;
361 ualg
= nla_data(rta
);
363 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
366 *props
= algo
->desc
.sadb_alg_id
;
368 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
372 strcpy(p
->alg_name
, algo
->name
);
377 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn
*replay_esn
,
380 struct xfrm_replay_state_esn
*up
;
383 if (!replay_esn
|| !rp
)
387 ulen
= xfrm_replay_state_esn_len(up
);
389 if (nla_len(rp
) < ulen
|| xfrm_replay_state_esn_len(replay_esn
) != ulen
)
395 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn
**replay_esn
,
396 struct xfrm_replay_state_esn
**preplay_esn
,
399 struct xfrm_replay_state_esn
*p
, *pp
, *up
;
406 klen
= xfrm_replay_state_esn_len(up
);
407 ulen
= nla_len(rta
) >= klen
? klen
: sizeof(*up
);
409 p
= kzalloc(klen
, GFP_KERNEL
);
413 pp
= kzalloc(klen
, GFP_KERNEL
);
420 memcpy(pp
, up
, ulen
);
428 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
433 len
+= sizeof(struct xfrm_user_sec_ctx
);
434 len
+= xfrm_ctx
->ctx_len
;
439 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
441 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
442 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
443 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
444 x
->props
.mode
= p
->mode
;
445 x
->props
.replay_window
= min_t(unsigned int, p
->replay_window
,
446 sizeof(x
->replay
.bitmap
) * 8);
447 x
->props
.reqid
= p
->reqid
;
448 x
->props
.family
= p
->family
;
449 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
450 x
->props
.flags
= p
->flags
;
452 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
453 x
->sel
.family
= p
->family
;
457 * someday when pfkey also has support, we could have the code
458 * somehow made shareable and move it to xfrm_state.c - JHS
461 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
,
464 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
465 struct nlattr
*re
= update_esn
? attrs
[XFRMA_REPLAY_ESN_VAL
] : NULL
;
466 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
467 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
468 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
471 struct xfrm_replay_state_esn
*replay_esn
;
472 replay_esn
= nla_data(re
);
473 memcpy(x
->replay_esn
, replay_esn
,
474 xfrm_replay_state_esn_len(replay_esn
));
475 memcpy(x
->preplay_esn
, replay_esn
,
476 xfrm_replay_state_esn_len(replay_esn
));
480 struct xfrm_replay_state
*replay
;
481 replay
= nla_data(rp
);
482 memcpy(&x
->replay
, replay
, sizeof(*replay
));
483 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
487 struct xfrm_lifetime_cur
*ltime
;
488 ltime
= nla_data(lt
);
489 x
->curlft
.bytes
= ltime
->bytes
;
490 x
->curlft
.packets
= ltime
->packets
;
491 x
->curlft
.add_time
= ltime
->add_time
;
492 x
->curlft
.use_time
= ltime
->use_time
;
496 x
->replay_maxage
= nla_get_u32(et
);
499 x
->replay_maxdiff
= nla_get_u32(rt
);
502 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
503 struct xfrm_usersa_info
*p
,
504 struct nlattr
**attrs
,
507 struct xfrm_state
*x
= xfrm_state_alloc(net
);
513 copy_from_user_state(x
, p
);
515 if (attrs
[XFRMA_SA_EXTRA_FLAGS
])
516 x
->props
.extra_flags
= nla_get_u32(attrs
[XFRMA_SA_EXTRA_FLAGS
]);
518 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
519 attrs
[XFRMA_ALG_AEAD
])))
521 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
522 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
524 if (!x
->props
.aalgo
) {
525 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
526 attrs
[XFRMA_ALG_AUTH
])))
529 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
530 xfrm_ealg_get_byname
,
531 attrs
[XFRMA_ALG_CRYPT
])))
533 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
534 xfrm_calg_get_byname
,
535 attrs
[XFRMA_ALG_COMP
])))
538 if (attrs
[XFRMA_ENCAP
]) {
539 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
540 sizeof(*x
->encap
), GFP_KERNEL
);
541 if (x
->encap
== NULL
)
545 if (attrs
[XFRMA_TFCPAD
])
546 x
->tfcpad
= nla_get_u32(attrs
[XFRMA_TFCPAD
]);
548 if (attrs
[XFRMA_COADDR
]) {
549 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
550 sizeof(*x
->coaddr
), GFP_KERNEL
);
551 if (x
->coaddr
== NULL
)
555 xfrm_mark_get(attrs
, &x
->mark
);
557 err
= __xfrm_init_state(x
, false);
561 if (attrs
[XFRMA_SEC_CTX
] &&
562 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
565 if ((err
= xfrm_alloc_replay_state_esn(&x
->replay_esn
, &x
->preplay_esn
,
566 attrs
[XFRMA_REPLAY_ESN_VAL
])))
570 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
571 /* sysctl_xfrm_aevent_etime is in 100ms units */
572 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
574 if ((err
= xfrm_init_replay(x
)))
577 /* override default values from above */
578 xfrm_update_ae_params(x
, attrs
, 0);
583 x
->km
.state
= XFRM_STATE_DEAD
;
590 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
591 struct nlattr
**attrs
)
593 struct net
*net
= sock_net(skb
->sk
);
594 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
595 struct xfrm_state
*x
;
599 err
= verify_newsa_info(p
, attrs
);
603 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
608 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
609 err
= xfrm_state_add(x
);
611 err
= xfrm_state_update(x
);
613 xfrm_audit_state_add(x
, err
? 0 : 1, true);
616 x
->km
.state
= XFRM_STATE_DEAD
;
621 c
.seq
= nlh
->nlmsg_seq
;
622 c
.portid
= nlh
->nlmsg_pid
;
623 c
.event
= nlh
->nlmsg_type
;
625 km_state_notify(x
, &c
);
631 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
632 struct xfrm_usersa_id
*p
,
633 struct nlattr
**attrs
,
636 struct xfrm_state
*x
= NULL
;
639 u32 mark
= xfrm_mark_get(attrs
, &m
);
641 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
643 x
= xfrm_state_lookup(net
, mark
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
645 xfrm_address_t
*saddr
= NULL
;
647 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
654 x
= xfrm_state_lookup_byaddr(net
, mark
,
656 p
->proto
, p
->family
);
665 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
666 struct nlattr
**attrs
)
668 struct net
*net
= sock_net(skb
->sk
);
669 struct xfrm_state
*x
;
672 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
674 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
678 if ((err
= security_xfrm_state_delete(x
)) != 0)
681 if (xfrm_state_kern(x
)) {
686 err
= xfrm_state_delete(x
);
691 c
.seq
= nlh
->nlmsg_seq
;
692 c
.portid
= nlh
->nlmsg_pid
;
693 c
.event
= nlh
->nlmsg_type
;
694 km_state_notify(x
, &c
);
697 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
702 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
704 memset(p
, 0, sizeof(*p
));
705 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
706 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
707 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
708 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
709 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
710 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
711 p
->mode
= x
->props
.mode
;
712 p
->replay_window
= x
->props
.replay_window
;
713 p
->reqid
= x
->props
.reqid
;
714 p
->family
= x
->props
.family
;
715 p
->flags
= x
->props
.flags
;
719 struct xfrm_dump_info
{
720 struct sk_buff
*in_skb
;
721 struct sk_buff
*out_skb
;
726 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
728 struct xfrm_user_sec_ctx
*uctx
;
730 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
732 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
736 uctx
= nla_data(attr
);
737 uctx
->exttype
= XFRMA_SEC_CTX
;
738 uctx
->len
= ctx_size
;
739 uctx
->ctx_doi
= s
->ctx_doi
;
740 uctx
->ctx_alg
= s
->ctx_alg
;
741 uctx
->ctx_len
= s
->ctx_len
;
742 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
747 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
749 struct xfrm_algo
*algo
;
752 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
753 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
757 algo
= nla_data(nla
);
758 strncpy(algo
->alg_name
, auth
->alg_name
, sizeof(algo
->alg_name
));
759 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
760 algo
->alg_key_len
= auth
->alg_key_len
;
765 /* Don't change this without updating xfrm_sa_len! */
766 static int copy_to_user_state_extra(struct xfrm_state
*x
,
767 struct xfrm_usersa_info
*p
,
772 copy_to_user_state(x
, p
);
774 if (x
->props
.extra_flags
) {
775 ret
= nla_put_u32(skb
, XFRMA_SA_EXTRA_FLAGS
,
776 x
->props
.extra_flags
);
782 ret
= nla_put(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
787 ret
= nla_put_u64(skb
, XFRMA_LASTUSED
, x
->lastused
);
792 ret
= nla_put(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
797 ret
= copy_to_user_auth(x
->aalg
, skb
);
799 ret
= nla_put(skb
, XFRMA_ALG_AUTH_TRUNC
,
800 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
805 ret
= nla_put(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
810 ret
= nla_put(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
815 ret
= nla_put(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
820 ret
= nla_put_u32(skb
, XFRMA_TFCPAD
, x
->tfcpad
);
824 ret
= xfrm_mark_put(skb
, &x
->mark
);
828 ret
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
829 xfrm_replay_state_esn_len(x
->replay_esn
),
832 ret
= nla_put(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
),
837 ret
= copy_sec_ctx(x
->security
, skb
);
842 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
844 struct xfrm_dump_info
*sp
= ptr
;
845 struct sk_buff
*in_skb
= sp
->in_skb
;
846 struct sk_buff
*skb
= sp
->out_skb
;
847 struct xfrm_usersa_info
*p
;
848 struct nlmsghdr
*nlh
;
851 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
852 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
858 err
= copy_to_user_state_extra(x
, p
, skb
);
860 nlmsg_cancel(skb
, nlh
);
867 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
869 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
870 struct sock
*sk
= cb
->skb
->sk
;
871 struct net
*net
= sock_net(sk
);
873 xfrm_state_walk_done(walk
, net
);
877 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1];
878 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
880 struct net
*net
= sock_net(skb
->sk
);
881 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
882 struct xfrm_dump_info info
;
884 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
885 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
887 info
.in_skb
= cb
->skb
;
889 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
890 info
.nlmsg_flags
= NLM_F_MULTI
;
893 struct nlattr
*attrs
[XFRMA_MAX
+1];
894 struct xfrm_address_filter
*filter
= NULL
;
900 err
= nlmsg_parse(cb
->nlh
, 0, attrs
, XFRMA_MAX
,
905 if (attrs
[XFRMA_ADDRESS_FILTER
]) {
906 filter
= kmalloc(sizeof(*filter
), GFP_KERNEL
);
910 memcpy(filter
, nla_data(attrs
[XFRMA_ADDRESS_FILTER
]),
914 if (attrs
[XFRMA_PROTO
])
915 proto
= nla_get_u8(attrs
[XFRMA_PROTO
]);
917 xfrm_state_walk_init(walk
, proto
, filter
);
920 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
925 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
926 struct xfrm_state
*x
, u32 seq
)
928 struct xfrm_dump_info info
;
932 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
934 return ERR_PTR(-ENOMEM
);
936 info
.in_skb
= in_skb
;
938 info
.nlmsg_seq
= seq
;
939 info
.nlmsg_flags
= 0;
941 err
= dump_one_state(x
, 0, &info
);
950 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
951 * Must be called with RCU read lock.
953 static inline int xfrm_nlmsg_multicast(struct net
*net
, struct sk_buff
*skb
,
954 u32 pid
, unsigned int group
)
956 struct sock
*nlsk
= rcu_dereference(net
->xfrm
.nlsk
);
959 return nlmsg_multicast(nlsk
, skb
, pid
, group
, GFP_ATOMIC
);
964 static inline size_t xfrm_spdinfo_msgsize(void)
966 return NLMSG_ALIGN(4)
967 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
968 + nla_total_size(sizeof(struct xfrmu_spdhinfo
))
969 + nla_total_size(sizeof(struct xfrmu_spdhthresh
))
970 + nla_total_size(sizeof(struct xfrmu_spdhthresh
));
973 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
974 u32 portid
, u32 seq
, u32 flags
)
976 struct xfrmk_spdinfo si
;
977 struct xfrmu_spdinfo spc
;
978 struct xfrmu_spdhinfo sph
;
979 struct xfrmu_spdhthresh spt4
, spt6
;
980 struct nlmsghdr
*nlh
;
985 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
986 if (nlh
== NULL
) /* shouldn't really happen ... */
991 xfrm_spd_getinfo(net
, &si
);
992 spc
.incnt
= si
.incnt
;
993 spc
.outcnt
= si
.outcnt
;
994 spc
.fwdcnt
= si
.fwdcnt
;
995 spc
.inscnt
= si
.inscnt
;
996 spc
.outscnt
= si
.outscnt
;
997 spc
.fwdscnt
= si
.fwdscnt
;
998 sph
.spdhcnt
= si
.spdhcnt
;
999 sph
.spdhmcnt
= si
.spdhmcnt
;
1002 lseq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
1004 spt4
.lbits
= net
->xfrm
.policy_hthresh
.lbits4
;
1005 spt4
.rbits
= net
->xfrm
.policy_hthresh
.rbits4
;
1006 spt6
.lbits
= net
->xfrm
.policy_hthresh
.lbits6
;
1007 spt6
.rbits
= net
->xfrm
.policy_hthresh
.rbits6
;
1008 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, lseq
));
1010 err
= nla_put(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
1012 err
= nla_put(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
1014 err
= nla_put(skb
, XFRMA_SPD_IPV4_HTHRESH
, sizeof(spt4
), &spt4
);
1016 err
= nla_put(skb
, XFRMA_SPD_IPV6_HTHRESH
, sizeof(spt6
), &spt6
);
1018 nlmsg_cancel(skb
, nlh
);
1022 nlmsg_end(skb
, nlh
);
1026 static int xfrm_set_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1027 struct nlattr
**attrs
)
1029 struct net
*net
= sock_net(skb
->sk
);
1030 struct xfrmu_spdhthresh
*thresh4
= NULL
;
1031 struct xfrmu_spdhthresh
*thresh6
= NULL
;
1033 /* selector prefixlen thresholds to hash policies */
1034 if (attrs
[XFRMA_SPD_IPV4_HTHRESH
]) {
1035 struct nlattr
*rta
= attrs
[XFRMA_SPD_IPV4_HTHRESH
];
1037 if (nla_len(rta
) < sizeof(*thresh4
))
1039 thresh4
= nla_data(rta
);
1040 if (thresh4
->lbits
> 32 || thresh4
->rbits
> 32)
1043 if (attrs
[XFRMA_SPD_IPV6_HTHRESH
]) {
1044 struct nlattr
*rta
= attrs
[XFRMA_SPD_IPV6_HTHRESH
];
1046 if (nla_len(rta
) < sizeof(*thresh6
))
1048 thresh6
= nla_data(rta
);
1049 if (thresh6
->lbits
> 128 || thresh6
->rbits
> 128)
1053 if (thresh4
|| thresh6
) {
1054 write_seqlock(&net
->xfrm
.policy_hthresh
.lock
);
1056 net
->xfrm
.policy_hthresh
.lbits4
= thresh4
->lbits
;
1057 net
->xfrm
.policy_hthresh
.rbits4
= thresh4
->rbits
;
1060 net
->xfrm
.policy_hthresh
.lbits6
= thresh6
->lbits
;
1061 net
->xfrm
.policy_hthresh
.rbits6
= thresh6
->rbits
;
1063 write_sequnlock(&net
->xfrm
.policy_hthresh
.lock
);
1065 xfrm_policy_hash_rebuild(net
);
1071 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1072 struct nlattr
**attrs
)
1074 struct net
*net
= sock_net(skb
->sk
);
1075 struct sk_buff
*r_skb
;
1076 u32
*flags
= nlmsg_data(nlh
);
1077 u32 sportid
= NETLINK_CB(skb
).portid
;
1078 u32 seq
= nlh
->nlmsg_seq
;
1080 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
1084 if (build_spdinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1087 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1090 static inline size_t xfrm_sadinfo_msgsize(void)
1092 return NLMSG_ALIGN(4)
1093 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
1094 + nla_total_size(4); /* XFRMA_SAD_CNT */
1097 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
1098 u32 portid
, u32 seq
, u32 flags
)
1100 struct xfrmk_sadinfo si
;
1101 struct xfrmu_sadhinfo sh
;
1102 struct nlmsghdr
*nlh
;
1106 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
1107 if (nlh
== NULL
) /* shouldn't really happen ... */
1110 f
= nlmsg_data(nlh
);
1112 xfrm_sad_getinfo(net
, &si
);
1114 sh
.sadhmcnt
= si
.sadhmcnt
;
1115 sh
.sadhcnt
= si
.sadhcnt
;
1117 err
= nla_put_u32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
1119 err
= nla_put(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
1121 nlmsg_cancel(skb
, nlh
);
1125 nlmsg_end(skb
, nlh
);
1129 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1130 struct nlattr
**attrs
)
1132 struct net
*net
= sock_net(skb
->sk
);
1133 struct sk_buff
*r_skb
;
1134 u32
*flags
= nlmsg_data(nlh
);
1135 u32 sportid
= NETLINK_CB(skb
).portid
;
1136 u32 seq
= nlh
->nlmsg_seq
;
1138 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
1142 if (build_sadinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1145 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1148 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1149 struct nlattr
**attrs
)
1151 struct net
*net
= sock_net(skb
->sk
);
1152 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
1153 struct xfrm_state
*x
;
1154 struct sk_buff
*resp_skb
;
1157 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
1161 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1162 if (IS_ERR(resp_skb
)) {
1163 err
= PTR_ERR(resp_skb
);
1165 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1172 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1173 struct nlattr
**attrs
)
1175 struct net
*net
= sock_net(skb
->sk
);
1176 struct xfrm_state
*x
;
1177 struct xfrm_userspi_info
*p
;
1178 struct sk_buff
*resp_skb
;
1179 xfrm_address_t
*daddr
;
1185 p
= nlmsg_data(nlh
);
1186 err
= verify_spi_info(p
->info
.id
.proto
, p
->min
, p
->max
);
1190 family
= p
->info
.family
;
1191 daddr
= &p
->info
.id
.daddr
;
1195 mark
= xfrm_mark_get(attrs
, &m
);
1197 x
= xfrm_find_acq_byseq(net
, mark
, p
->info
.seq
);
1198 if (x
&& !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
)) {
1205 x
= xfrm_find_acq(net
, &m
, p
->info
.mode
, p
->info
.reqid
,
1206 p
->info
.id
.proto
, daddr
,
1213 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
1217 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1218 if (IS_ERR(resp_skb
)) {
1219 err
= PTR_ERR(resp_skb
);
1223 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1231 static int verify_policy_dir(u8 dir
)
1234 case XFRM_POLICY_IN
:
1235 case XFRM_POLICY_OUT
:
1236 case XFRM_POLICY_FWD
:
1246 static int verify_policy_type(u8 type
)
1249 case XFRM_POLICY_TYPE_MAIN
:
1250 #ifdef CONFIG_XFRM_SUB_POLICY
1251 case XFRM_POLICY_TYPE_SUB
:
1262 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1267 case XFRM_SHARE_ANY
:
1268 case XFRM_SHARE_SESSION
:
1269 case XFRM_SHARE_USER
:
1270 case XFRM_SHARE_UNIQUE
:
1277 switch (p
->action
) {
1278 case XFRM_POLICY_ALLOW
:
1279 case XFRM_POLICY_BLOCK
:
1286 switch (p
->sel
.family
) {
1291 #if IS_ENABLED(CONFIG_IPV6)
1294 return -EAFNOSUPPORT
;
1301 ret
= verify_policy_dir(p
->dir
);
1304 if (p
->index
&& ((p
->index
& XFRM_POLICY_MAX
) != p
->dir
))
1310 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1312 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1313 struct xfrm_user_sec_ctx
*uctx
;
1318 uctx
= nla_data(rt
);
1319 return security_xfrm_policy_alloc(&pol
->security
, uctx
, GFP_KERNEL
);
1322 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1328 for (i
= 0; i
< nr
; i
++, ut
++) {
1329 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1331 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1332 memcpy(&t
->saddr
, &ut
->saddr
,
1333 sizeof(xfrm_address_t
));
1334 t
->reqid
= ut
->reqid
;
1336 t
->share
= ut
->share
;
1337 t
->optional
= ut
->optional
;
1338 t
->aalgos
= ut
->aalgos
;
1339 t
->ealgos
= ut
->ealgos
;
1340 t
->calgos
= ut
->calgos
;
1341 /* If all masks are ~0, then we allow all algorithms. */
1342 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1343 t
->encap_family
= ut
->family
;
1347 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1351 if (nr
> XFRM_MAX_DEPTH
)
1354 for (i
= 0; i
< nr
; i
++) {
1355 /* We never validated the ut->family value, so many
1356 * applications simply leave it at zero. The check was
1357 * never made and ut->family was ignored because all
1358 * templates could be assumed to have the same family as
1359 * the policy itself. Now that we will have ipv4-in-ipv6
1360 * and ipv6-in-ipv4 tunnels, this is no longer true.
1363 ut
[i
].family
= family
;
1365 switch (ut
[i
].family
) {
1368 #if IS_ENABLED(CONFIG_IPV6)
1380 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1382 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1387 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1388 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1391 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1395 copy_templates(pol
, utmpl
, nr
);
1400 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1402 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1403 struct xfrm_userpolicy_type
*upt
;
1404 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1412 err
= verify_policy_type(type
);
1420 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1422 xp
->priority
= p
->priority
;
1423 xp
->index
= p
->index
;
1424 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1425 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1426 xp
->action
= p
->action
;
1427 xp
->flags
= p
->flags
;
1428 xp
->family
= p
->sel
.family
;
1429 /* XXX xp->share = p->share; */
1432 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1434 memset(p
, 0, sizeof(*p
));
1435 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1436 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1437 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1438 p
->priority
= xp
->priority
;
1439 p
->index
= xp
->index
;
1440 p
->sel
.family
= xp
->family
;
1442 p
->action
= xp
->action
;
1443 p
->flags
= xp
->flags
;
1444 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1447 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1449 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1457 copy_from_user_policy(xp
, p
);
1459 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1463 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1464 err
= copy_from_user_sec_ctx(xp
, attrs
);
1468 xfrm_mark_get(attrs
, &xp
->mark
);
1474 xfrm_policy_destroy(xp
);
1478 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1479 struct nlattr
**attrs
)
1481 struct net
*net
= sock_net(skb
->sk
);
1482 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1483 struct xfrm_policy
*xp
;
1488 err
= verify_newpolicy_info(p
);
1491 err
= verify_sec_ctx_len(attrs
);
1495 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1499 /* shouldn't excl be based on nlh flags??
1500 * Aha! this is anti-netlink really i.e more pfkey derived
1501 * in netlink excl is a flag and you wouldnt need
1502 * a type XFRM_MSG_UPDPOLICY - JHS */
1503 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1504 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1505 xfrm_audit_policy_add(xp
, err
? 0 : 1, true);
1508 security_xfrm_policy_free(xp
->security
);
1513 c
.event
= nlh
->nlmsg_type
;
1514 c
.seq
= nlh
->nlmsg_seq
;
1515 c
.portid
= nlh
->nlmsg_pid
;
1516 km_policy_notify(xp
, p
->dir
, &c
);
1523 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1525 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1528 if (xp
->xfrm_nr
== 0)
1531 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1532 struct xfrm_user_tmpl
*up
= &vec
[i
];
1533 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1535 memset(up
, 0, sizeof(*up
));
1536 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1537 up
->family
= kp
->encap_family
;
1538 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1539 up
->reqid
= kp
->reqid
;
1540 up
->mode
= kp
->mode
;
1541 up
->share
= kp
->share
;
1542 up
->optional
= kp
->optional
;
1543 up
->aalgos
= kp
->aalgos
;
1544 up
->ealgos
= kp
->ealgos
;
1545 up
->calgos
= kp
->calgos
;
1548 return nla_put(skb
, XFRMA_TMPL
,
1549 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1552 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1555 return copy_sec_ctx(x
->security
, skb
);
1560 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1563 return copy_sec_ctx(xp
->security
, skb
);
1566 static inline size_t userpolicy_type_attrsize(void)
1568 #ifdef CONFIG_XFRM_SUB_POLICY
1569 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1575 #ifdef CONFIG_XFRM_SUB_POLICY
1576 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1578 struct xfrm_userpolicy_type upt
= {
1582 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1586 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1592 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1594 struct xfrm_dump_info
*sp
= ptr
;
1595 struct xfrm_userpolicy_info
*p
;
1596 struct sk_buff
*in_skb
= sp
->in_skb
;
1597 struct sk_buff
*skb
= sp
->out_skb
;
1598 struct nlmsghdr
*nlh
;
1601 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
1602 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1606 p
= nlmsg_data(nlh
);
1607 copy_to_user_policy(xp
, p
, dir
);
1608 err
= copy_to_user_tmpl(xp
, skb
);
1610 err
= copy_to_user_sec_ctx(xp
, skb
);
1612 err
= copy_to_user_policy_type(xp
->type
, skb
);
1614 err
= xfrm_mark_put(skb
, &xp
->mark
);
1616 nlmsg_cancel(skb
, nlh
);
1619 nlmsg_end(skb
, nlh
);
1623 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1625 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1626 struct net
*net
= sock_net(cb
->skb
->sk
);
1628 xfrm_policy_walk_done(walk
, net
);
1632 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1634 struct net
*net
= sock_net(skb
->sk
);
1635 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1636 struct xfrm_dump_info info
;
1638 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1639 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1641 info
.in_skb
= cb
->skb
;
1643 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1644 info
.nlmsg_flags
= NLM_F_MULTI
;
1648 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1651 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1656 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1657 struct xfrm_policy
*xp
,
1660 struct xfrm_dump_info info
;
1661 struct sk_buff
*skb
;
1664 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1666 return ERR_PTR(-ENOMEM
);
1668 info
.in_skb
= in_skb
;
1670 info
.nlmsg_seq
= seq
;
1671 info
.nlmsg_flags
= 0;
1673 err
= dump_one_policy(xp
, dir
, 0, &info
);
1676 return ERR_PTR(err
);
1682 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1683 struct nlattr
**attrs
)
1685 struct net
*net
= sock_net(skb
->sk
);
1686 struct xfrm_policy
*xp
;
1687 struct xfrm_userpolicy_id
*p
;
1688 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1693 u32 mark
= xfrm_mark_get(attrs
, &m
);
1695 p
= nlmsg_data(nlh
);
1696 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1698 err
= copy_from_user_policy_type(&type
, attrs
);
1702 err
= verify_policy_dir(p
->dir
);
1707 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, delete, &err
);
1709 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1710 struct xfrm_sec_ctx
*ctx
;
1712 err
= verify_sec_ctx_len(attrs
);
1718 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1720 err
= security_xfrm_policy_alloc(&ctx
, uctx
, GFP_KERNEL
);
1724 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
, &p
->sel
,
1726 security_xfrm_policy_free(ctx
);
1732 struct sk_buff
*resp_skb
;
1734 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1735 if (IS_ERR(resp_skb
)) {
1736 err
= PTR_ERR(resp_skb
);
1738 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1739 NETLINK_CB(skb
).portid
);
1742 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
1747 c
.data
.byid
= p
->index
;
1748 c
.event
= nlh
->nlmsg_type
;
1749 c
.seq
= nlh
->nlmsg_seq
;
1750 c
.portid
= nlh
->nlmsg_pid
;
1751 km_policy_notify(xp
, p
->dir
, &c
);
1756 if (delete && err
== 0)
1757 xfrm_garbage_collect(net
);
1761 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1762 struct nlattr
**attrs
)
1764 struct net
*net
= sock_net(skb
->sk
);
1766 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1769 err
= xfrm_state_flush(net
, p
->proto
, true);
1771 if (err
== -ESRCH
) /* empty table */
1775 c
.data
.proto
= p
->proto
;
1776 c
.event
= nlh
->nlmsg_type
;
1777 c
.seq
= nlh
->nlmsg_seq
;
1778 c
.portid
= nlh
->nlmsg_pid
;
1780 km_state_notify(NULL
, &c
);
1785 static inline size_t xfrm_aevent_msgsize(struct xfrm_state
*x
)
1787 size_t replay_size
= x
->replay_esn
?
1788 xfrm_replay_state_esn_len(x
->replay_esn
) :
1789 sizeof(struct xfrm_replay_state
);
1791 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1792 + nla_total_size(replay_size
)
1793 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1794 + nla_total_size(sizeof(struct xfrm_mark
))
1795 + nla_total_size(4) /* XFRM_AE_RTHR */
1796 + nla_total_size(4); /* XFRM_AE_ETHR */
1799 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
1801 struct xfrm_aevent_id
*id
;
1802 struct nlmsghdr
*nlh
;
1805 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1809 id
= nlmsg_data(nlh
);
1810 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
, sizeof(x
->id
.daddr
));
1811 id
->sa_id
.spi
= x
->id
.spi
;
1812 id
->sa_id
.family
= x
->props
.family
;
1813 id
->sa_id
.proto
= x
->id
.proto
;
1814 memcpy(&id
->saddr
, &x
->props
.saddr
, sizeof(x
->props
.saddr
));
1815 id
->reqid
= x
->props
.reqid
;
1816 id
->flags
= c
->data
.aevent
;
1818 if (x
->replay_esn
) {
1819 err
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
1820 xfrm_replay_state_esn_len(x
->replay_esn
),
1823 err
= nla_put(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
),
1828 err
= nla_put(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1832 if (id
->flags
& XFRM_AE_RTHR
) {
1833 err
= nla_put_u32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1837 if (id
->flags
& XFRM_AE_ETHR
) {
1838 err
= nla_put_u32(skb
, XFRMA_ETIMER_THRESH
,
1839 x
->replay_maxage
* 10 / HZ
);
1843 err
= xfrm_mark_put(skb
, &x
->mark
);
1847 nlmsg_end(skb
, nlh
);
1851 nlmsg_cancel(skb
, nlh
);
1855 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1856 struct nlattr
**attrs
)
1858 struct net
*net
= sock_net(skb
->sk
);
1859 struct xfrm_state
*x
;
1860 struct sk_buff
*r_skb
;
1865 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1866 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1868 mark
= xfrm_mark_get(attrs
, &m
);
1870 x
= xfrm_state_lookup(net
, mark
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1874 r_skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
1875 if (r_skb
== NULL
) {
1881 * XXX: is this lock really needed - none of the other
1882 * gets lock (the concern is things getting updated
1883 * while we are still reading) - jhs
1885 spin_lock_bh(&x
->lock
);
1886 c
.data
.aevent
= p
->flags
;
1887 c
.seq
= nlh
->nlmsg_seq
;
1888 c
.portid
= nlh
->nlmsg_pid
;
1890 if (build_aevent(r_skb
, x
, &c
) < 0)
1892 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).portid
);
1893 spin_unlock_bh(&x
->lock
);
1898 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1899 struct nlattr
**attrs
)
1901 struct net
*net
= sock_net(skb
->sk
);
1902 struct xfrm_state
*x
;
1907 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1908 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1909 struct nlattr
*re
= attrs
[XFRMA_REPLAY_ESN_VAL
];
1910 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1912 if (!lt
&& !rp
&& !re
)
1915 /* pedantic mode - thou shalt sayeth replaceth */
1916 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1919 mark
= xfrm_mark_get(attrs
, &m
);
1921 x
= xfrm_state_lookup(net
, mark
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1925 if (x
->km
.state
!= XFRM_STATE_VALID
)
1928 err
= xfrm_replay_verify_len(x
->replay_esn
, re
);
1932 spin_lock_bh(&x
->lock
);
1933 xfrm_update_ae_params(x
, attrs
, 1);
1934 spin_unlock_bh(&x
->lock
);
1936 c
.event
= nlh
->nlmsg_type
;
1937 c
.seq
= nlh
->nlmsg_seq
;
1938 c
.portid
= nlh
->nlmsg_pid
;
1939 c
.data
.aevent
= XFRM_AE_CU
;
1940 km_state_notify(x
, &c
);
1947 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1948 struct nlattr
**attrs
)
1950 struct net
*net
= sock_net(skb
->sk
);
1952 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1955 err
= copy_from_user_policy_type(&type
, attrs
);
1959 err
= xfrm_policy_flush(net
, type
, true);
1961 if (err
== -ESRCH
) /* empty table */
1967 c
.event
= nlh
->nlmsg_type
;
1968 c
.seq
= nlh
->nlmsg_seq
;
1969 c
.portid
= nlh
->nlmsg_pid
;
1971 km_policy_notify(NULL
, 0, &c
);
1975 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1976 struct nlattr
**attrs
)
1978 struct net
*net
= sock_net(skb
->sk
);
1979 struct xfrm_policy
*xp
;
1980 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1981 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1982 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1985 u32 mark
= xfrm_mark_get(attrs
, &m
);
1987 err
= copy_from_user_policy_type(&type
, attrs
);
1991 err
= verify_policy_dir(p
->dir
);
1996 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, 0, &err
);
1998 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1999 struct xfrm_sec_ctx
*ctx
;
2001 err
= verify_sec_ctx_len(attrs
);
2007 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
2009 err
= security_xfrm_policy_alloc(&ctx
, uctx
, GFP_KERNEL
);
2013 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
,
2014 &p
->sel
, ctx
, 0, &err
);
2015 security_xfrm_policy_free(ctx
);
2020 if (unlikely(xp
->walk
.dead
))
2025 xfrm_policy_delete(xp
, p
->dir
);
2026 xfrm_audit_policy_delete(xp
, 1, true);
2028 // reset the timers here?
2029 WARN(1, "Dont know what to do with soft policy expire\n");
2031 km_policy_expired(xp
, p
->dir
, up
->hard
, nlh
->nlmsg_pid
);
2038 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2039 struct nlattr
**attrs
)
2041 struct net
*net
= sock_net(skb
->sk
);
2042 struct xfrm_state
*x
;
2044 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
2045 struct xfrm_usersa_info
*p
= &ue
->state
;
2047 u32 mark
= xfrm_mark_get(attrs
, &m
);
2049 x
= xfrm_state_lookup(net
, mark
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
2055 spin_lock_bh(&x
->lock
);
2057 if (x
->km
.state
!= XFRM_STATE_VALID
)
2059 km_state_expired(x
, ue
->hard
, nlh
->nlmsg_pid
);
2062 __xfrm_state_delete(x
);
2063 xfrm_audit_state_delete(x
, 1, true);
2067 spin_unlock_bh(&x
->lock
);
2072 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2073 struct nlattr
**attrs
)
2075 struct net
*net
= sock_net(skb
->sk
);
2076 struct xfrm_policy
*xp
;
2077 struct xfrm_user_tmpl
*ut
;
2079 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
2080 struct xfrm_mark mark
;
2082 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
2083 struct xfrm_state
*x
= xfrm_state_alloc(net
);
2089 xfrm_mark_get(attrs
, &mark
);
2091 err
= verify_newpolicy_info(&ua
->policy
);
2096 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
2100 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
2101 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
2102 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
2103 xp
->mark
.m
= x
->mark
.m
= mark
.m
;
2104 xp
->mark
.v
= x
->mark
.v
= mark
.v
;
2106 /* extract the templates and for each call km_key */
2107 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
2108 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
2109 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
2110 x
->props
.mode
= t
->mode
;
2111 x
->props
.reqid
= t
->reqid
;
2112 x
->props
.family
= ut
->family
;
2113 t
->aalgos
= ua
->aalgos
;
2114 t
->ealgos
= ua
->ealgos
;
2115 t
->calgos
= ua
->calgos
;
2116 err
= km_query(x
, t
, xp
);
2126 WARN(1, "BAD policy passed\n");
2133 #ifdef CONFIG_XFRM_MIGRATE
2134 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
2135 struct xfrm_kmaddress
*k
,
2136 struct nlattr
**attrs
, int *num
)
2138 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
2139 struct xfrm_user_migrate
*um
;
2143 struct xfrm_user_kmaddress
*uk
;
2145 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
2146 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
2147 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
2148 k
->family
= uk
->family
;
2149 k
->reserved
= uk
->reserved
;
2153 num_migrate
= nla_len(rt
) / sizeof(*um
);
2155 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
2158 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
2159 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
2160 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
2161 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
2162 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
2164 ma
->proto
= um
->proto
;
2165 ma
->mode
= um
->mode
;
2166 ma
->reqid
= um
->reqid
;
2168 ma
->old_family
= um
->old_family
;
2169 ma
->new_family
= um
->new_family
;
2176 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2177 struct nlattr
**attrs
)
2179 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
2180 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2181 struct xfrm_kmaddress km
, *kmp
;
2185 struct net
*net
= sock_net(skb
->sk
);
2187 if (attrs
[XFRMA_MIGRATE
] == NULL
)
2190 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
2192 err
= copy_from_user_policy_type(&type
, attrs
);
2196 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
2203 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
, net
);
2208 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2209 struct nlattr
**attrs
)
2211 return -ENOPROTOOPT
;
2215 #ifdef CONFIG_XFRM_MIGRATE
2216 static int copy_to_user_migrate(const struct xfrm_migrate
*m
, struct sk_buff
*skb
)
2218 struct xfrm_user_migrate um
;
2220 memset(&um
, 0, sizeof(um
));
2221 um
.proto
= m
->proto
;
2223 um
.reqid
= m
->reqid
;
2224 um
.old_family
= m
->old_family
;
2225 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
2226 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
2227 um
.new_family
= m
->new_family
;
2228 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
2229 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
2231 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
2234 static int copy_to_user_kmaddress(const struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
2236 struct xfrm_user_kmaddress uk
;
2238 memset(&uk
, 0, sizeof(uk
));
2239 uk
.family
= k
->family
;
2240 uk
.reserved
= k
->reserved
;
2241 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
2242 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
2244 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
2247 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
)
2249 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
2250 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
2251 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
2252 + userpolicy_type_attrsize();
2255 static int build_migrate(struct sk_buff
*skb
, const struct xfrm_migrate
*m
,
2256 int num_migrate
, const struct xfrm_kmaddress
*k
,
2257 const struct xfrm_selector
*sel
, u8 dir
, u8 type
)
2259 const struct xfrm_migrate
*mp
;
2260 struct xfrm_userpolicy_id
*pol_id
;
2261 struct nlmsghdr
*nlh
;
2264 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
2268 pol_id
= nlmsg_data(nlh
);
2269 /* copy data from selector, dir, and type to the pol_id */
2270 memset(pol_id
, 0, sizeof(*pol_id
));
2271 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
2275 err
= copy_to_user_kmaddress(k
, skb
);
2279 err
= copy_to_user_policy_type(type
, skb
);
2282 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2283 err
= copy_to_user_migrate(mp
, skb
);
2288 nlmsg_end(skb
, nlh
);
2292 nlmsg_cancel(skb
, nlh
);
2296 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2297 const struct xfrm_migrate
*m
, int num_migrate
,
2298 const struct xfrm_kmaddress
*k
)
2300 struct net
*net
= &init_net
;
2301 struct sk_buff
*skb
;
2303 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
), GFP_ATOMIC
);
2308 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, dir
, type
) < 0)
2311 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_MIGRATE
);
2314 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2315 const struct xfrm_migrate
*m
, int num_migrate
,
2316 const struct xfrm_kmaddress
*k
)
2318 return -ENOPROTOOPT
;
2322 #define XMSGSIZE(type) sizeof(struct type)
2324 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2325 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2326 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2327 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2328 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2329 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2330 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2331 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2332 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2333 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2334 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2335 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2336 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2337 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2338 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2339 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2340 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2341 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2342 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2343 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2344 [XFRM_MSG_NEWSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2345 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2350 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2351 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2352 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2353 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2354 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2355 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2356 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2357 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2358 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2359 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2360 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2361 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2362 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2363 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2364 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2365 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2366 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2367 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2368 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2369 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2370 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2371 [XFRMA_MARK
] = { .len
= sizeof(struct xfrm_mark
) },
2372 [XFRMA_TFCPAD
] = { .type
= NLA_U32
},
2373 [XFRMA_REPLAY_ESN_VAL
] = { .len
= sizeof(struct xfrm_replay_state_esn
) },
2374 [XFRMA_SA_EXTRA_FLAGS
] = { .type
= NLA_U32
},
2375 [XFRMA_PROTO
] = { .type
= NLA_U8
},
2376 [XFRMA_ADDRESS_FILTER
] = { .len
= sizeof(struct xfrm_address_filter
) },
2379 static const struct nla_policy xfrma_spd_policy
[XFRMA_SPD_MAX
+1] = {
2380 [XFRMA_SPD_IPV4_HTHRESH
] = { .len
= sizeof(struct xfrmu_spdhthresh
) },
2381 [XFRMA_SPD_IPV6_HTHRESH
] = { .len
= sizeof(struct xfrmu_spdhthresh
) },
2384 static const struct xfrm_link
{
2385 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2386 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2387 int (*done
)(struct netlink_callback
*);
2388 const struct nla_policy
*nla_pol
;
2390 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2391 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2392 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2393 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2394 .dump
= xfrm_dump_sa
,
2395 .done
= xfrm_dump_sa_done
},
2396 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2397 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2398 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2399 .dump
= xfrm_dump_policy
,
2400 .done
= xfrm_dump_policy_done
},
2401 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2402 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2403 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2404 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2405 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2406 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2407 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2408 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2409 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2410 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2411 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2412 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2413 [XFRM_MSG_NEWSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_set_spdinfo
,
2414 .nla_pol
= xfrma_spd_policy
,
2415 .nla_max
= XFRMA_SPD_MAX
},
2416 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2419 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2421 struct net
*net
= sock_net(skb
->sk
);
2422 struct nlattr
*attrs
[XFRMA_MAX
+1];
2423 const struct xfrm_link
*link
;
2426 #ifdef CONFIG_COMPAT
2427 if (is_compat_task())
2431 type
= nlh
->nlmsg_type
;
2432 if (type
> XFRM_MSG_MAX
)
2435 type
-= XFRM_MSG_BASE
;
2436 link
= &xfrm_dispatch
[type
];
2438 /* All operations require privileges, even GET */
2439 if (!netlink_net_capable(skb
, CAP_NET_ADMIN
))
2442 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2443 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2444 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2445 if (link
->dump
== NULL
)
2449 struct netlink_dump_control c
= {
2453 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, &c
);
2457 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
,
2458 link
->nla_max
? : XFRMA_MAX
,
2459 link
->nla_pol
? : xfrma_policy
);
2463 if (link
->doit
== NULL
)
2466 return link
->doit(skb
, nlh
, attrs
);
2469 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2471 struct net
*net
= sock_net(skb
->sk
);
2473 mutex_lock(&net
->xfrm
.xfrm_cfg_mutex
);
2474 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2475 mutex_unlock(&net
->xfrm
.xfrm_cfg_mutex
);
2478 static inline size_t xfrm_expire_msgsize(void)
2480 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
))
2481 + nla_total_size(sizeof(struct xfrm_mark
));
2484 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
2486 struct xfrm_user_expire
*ue
;
2487 struct nlmsghdr
*nlh
;
2490 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2494 ue
= nlmsg_data(nlh
);
2495 copy_to_user_state(x
, &ue
->state
);
2496 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2498 err
= xfrm_mark_put(skb
, &x
->mark
);
2502 nlmsg_end(skb
, nlh
);
2506 static int xfrm_exp_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2508 struct net
*net
= xs_net(x
);
2509 struct sk_buff
*skb
;
2511 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2515 if (build_expire(skb
, x
, c
) < 0) {
2520 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_EXPIRE
);
2523 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2525 struct net
*net
= xs_net(x
);
2526 struct sk_buff
*skb
;
2528 skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
2532 if (build_aevent(skb
, x
, c
) < 0)
2535 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_AEVENTS
);
2538 static int xfrm_notify_sa_flush(const struct km_event
*c
)
2540 struct net
*net
= c
->net
;
2541 struct xfrm_usersa_flush
*p
;
2542 struct nlmsghdr
*nlh
;
2543 struct sk_buff
*skb
;
2544 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2546 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2550 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2556 p
= nlmsg_data(nlh
);
2557 p
->proto
= c
->data
.proto
;
2559 nlmsg_end(skb
, nlh
);
2561 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_SA
);
2564 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2568 l
+= nla_total_size(aead_len(x
->aead
));
2570 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2571 (x
->aalg
->alg_key_len
+ 7) / 8);
2572 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2575 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2577 l
+= nla_total_size(sizeof(*x
->calg
));
2579 l
+= nla_total_size(sizeof(*x
->encap
));
2581 l
+= nla_total_size(sizeof(x
->tfcpad
));
2583 l
+= nla_total_size(xfrm_replay_state_esn_len(x
->replay_esn
));
2585 l
+= nla_total_size(sizeof(struct xfrm_replay_state
));
2587 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2588 x
->security
->ctx_len
);
2590 l
+= nla_total_size(sizeof(*x
->coaddr
));
2591 if (x
->props
.extra_flags
)
2592 l
+= nla_total_size(sizeof(x
->props
.extra_flags
));
2594 /* Must count x->lastused as it may become non-zero behind our back. */
2595 l
+= nla_total_size(sizeof(u64
));
2600 static int xfrm_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
2602 struct net
*net
= xs_net(x
);
2603 struct xfrm_usersa_info
*p
;
2604 struct xfrm_usersa_id
*id
;
2605 struct nlmsghdr
*nlh
;
2606 struct sk_buff
*skb
;
2607 int len
= xfrm_sa_len(x
);
2610 headlen
= sizeof(*p
);
2611 if (c
->event
== XFRM_MSG_DELSA
) {
2612 len
+= nla_total_size(headlen
);
2613 headlen
= sizeof(*id
);
2614 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2616 len
+= NLMSG_ALIGN(headlen
);
2618 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2622 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2627 p
= nlmsg_data(nlh
);
2628 if (c
->event
== XFRM_MSG_DELSA
) {
2629 struct nlattr
*attr
;
2631 id
= nlmsg_data(nlh
);
2632 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2633 id
->spi
= x
->id
.spi
;
2634 id
->family
= x
->props
.family
;
2635 id
->proto
= x
->id
.proto
;
2637 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2644 err
= copy_to_user_state_extra(x
, p
, skb
);
2648 nlmsg_end(skb
, nlh
);
2650 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_SA
);
2657 static int xfrm_send_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2661 case XFRM_MSG_EXPIRE
:
2662 return xfrm_exp_state_notify(x
, c
);
2663 case XFRM_MSG_NEWAE
:
2664 return xfrm_aevent_state_notify(x
, c
);
2665 case XFRM_MSG_DELSA
:
2666 case XFRM_MSG_UPDSA
:
2667 case XFRM_MSG_NEWSA
:
2668 return xfrm_notify_sa(x
, c
);
2669 case XFRM_MSG_FLUSHSA
:
2670 return xfrm_notify_sa_flush(c
);
2672 printk(KERN_NOTICE
"xfrm_user: Unknown SA event %d\n",
2681 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2682 struct xfrm_policy
*xp
)
2684 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2685 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2686 + nla_total_size(sizeof(struct xfrm_mark
))
2687 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2688 + userpolicy_type_attrsize();
2691 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2692 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
)
2694 __u32 seq
= xfrm_get_acqseq();
2695 struct xfrm_user_acquire
*ua
;
2696 struct nlmsghdr
*nlh
;
2699 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2703 ua
= nlmsg_data(nlh
);
2704 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2705 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2706 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2707 copy_to_user_policy(xp
, &ua
->policy
, XFRM_POLICY_OUT
);
2708 ua
->aalgos
= xt
->aalgos
;
2709 ua
->ealgos
= xt
->ealgos
;
2710 ua
->calgos
= xt
->calgos
;
2711 ua
->seq
= x
->km
.seq
= seq
;
2713 err
= copy_to_user_tmpl(xp
, skb
);
2715 err
= copy_to_user_state_sec_ctx(x
, skb
);
2717 err
= copy_to_user_policy_type(xp
->type
, skb
);
2719 err
= xfrm_mark_put(skb
, &xp
->mark
);
2721 nlmsg_cancel(skb
, nlh
);
2725 nlmsg_end(skb
, nlh
);
2729 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2730 struct xfrm_policy
*xp
)
2732 struct net
*net
= xs_net(x
);
2733 struct sk_buff
*skb
;
2735 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2739 if (build_acquire(skb
, x
, xt
, xp
) < 0)
2742 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_ACQUIRE
);
2745 /* User gives us xfrm_user_policy_info followed by an array of 0
2746 * or more templates.
2748 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2749 u8
*data
, int len
, int *dir
)
2751 struct net
*net
= sock_net(sk
);
2752 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2753 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2754 struct xfrm_policy
*xp
;
2757 switch (sk
->sk_family
) {
2759 if (opt
!= IP_XFRM_POLICY
) {
2764 #if IS_ENABLED(CONFIG_IPV6)
2766 if (opt
!= IPV6_XFRM_POLICY
) {
2779 if (len
< sizeof(*p
) ||
2780 verify_newpolicy_info(p
))
2783 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2784 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2787 if (p
->dir
> XFRM_POLICY_OUT
)
2790 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
2796 copy_from_user_policy(xp
, p
);
2797 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2798 copy_templates(xp
, ut
, nr
);
2805 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2807 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2808 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2809 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2810 + nla_total_size(sizeof(struct xfrm_mark
))
2811 + userpolicy_type_attrsize();
2814 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2815 int dir
, const struct km_event
*c
)
2817 struct xfrm_user_polexpire
*upe
;
2818 int hard
= c
->data
.hard
;
2819 struct nlmsghdr
*nlh
;
2822 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2826 upe
= nlmsg_data(nlh
);
2827 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2828 err
= copy_to_user_tmpl(xp
, skb
);
2830 err
= copy_to_user_sec_ctx(xp
, skb
);
2832 err
= copy_to_user_policy_type(xp
->type
, skb
);
2834 err
= xfrm_mark_put(skb
, &xp
->mark
);
2836 nlmsg_cancel(skb
, nlh
);
2841 nlmsg_end(skb
, nlh
);
2845 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2847 struct net
*net
= xp_net(xp
);
2848 struct sk_buff
*skb
;
2850 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2854 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2857 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_EXPIRE
);
2860 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2862 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2863 struct net
*net
= xp_net(xp
);
2864 struct xfrm_userpolicy_info
*p
;
2865 struct xfrm_userpolicy_id
*id
;
2866 struct nlmsghdr
*nlh
;
2867 struct sk_buff
*skb
;
2870 headlen
= sizeof(*p
);
2871 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2872 len
+= nla_total_size(headlen
);
2873 headlen
= sizeof(*id
);
2875 len
+= userpolicy_type_attrsize();
2876 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2877 len
+= NLMSG_ALIGN(headlen
);
2879 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2883 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2888 p
= nlmsg_data(nlh
);
2889 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2890 struct nlattr
*attr
;
2892 id
= nlmsg_data(nlh
);
2893 memset(id
, 0, sizeof(*id
));
2896 id
->index
= xp
->index
;
2898 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2900 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2908 copy_to_user_policy(xp
, p
, dir
);
2909 err
= copy_to_user_tmpl(xp
, skb
);
2911 err
= copy_to_user_policy_type(xp
->type
, skb
);
2913 err
= xfrm_mark_put(skb
, &xp
->mark
);
2917 nlmsg_end(skb
, nlh
);
2919 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_POLICY
);
2926 static int xfrm_notify_policy_flush(const struct km_event
*c
)
2928 struct net
*net
= c
->net
;
2929 struct nlmsghdr
*nlh
;
2930 struct sk_buff
*skb
;
2933 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2937 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2941 err
= copy_to_user_policy_type(c
->data
.type
, skb
);
2945 nlmsg_end(skb
, nlh
);
2947 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_POLICY
);
2954 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2958 case XFRM_MSG_NEWPOLICY
:
2959 case XFRM_MSG_UPDPOLICY
:
2960 case XFRM_MSG_DELPOLICY
:
2961 return xfrm_notify_policy(xp
, dir
, c
);
2962 case XFRM_MSG_FLUSHPOLICY
:
2963 return xfrm_notify_policy_flush(c
);
2964 case XFRM_MSG_POLEXPIRE
:
2965 return xfrm_exp_policy_notify(xp
, dir
, c
);
2967 printk(KERN_NOTICE
"xfrm_user: Unknown Policy event %d\n",
2975 static inline size_t xfrm_report_msgsize(void)
2977 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2980 static int build_report(struct sk_buff
*skb
, u8 proto
,
2981 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2983 struct xfrm_user_report
*ur
;
2984 struct nlmsghdr
*nlh
;
2986 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2990 ur
= nlmsg_data(nlh
);
2992 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2995 int err
= nla_put(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2997 nlmsg_cancel(skb
, nlh
);
3001 nlmsg_end(skb
, nlh
);
3005 static int xfrm_send_report(struct net
*net
, u8 proto
,
3006 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
3008 struct sk_buff
*skb
;
3010 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
3014 if (build_report(skb
, proto
, sel
, addr
) < 0)
3017 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_REPORT
);
3020 static inline size_t xfrm_mapping_msgsize(void)
3022 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
3025 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
3026 xfrm_address_t
*new_saddr
, __be16 new_sport
)
3028 struct xfrm_user_mapping
*um
;
3029 struct nlmsghdr
*nlh
;
3031 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
3035 um
= nlmsg_data(nlh
);
3037 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
3038 um
->id
.spi
= x
->id
.spi
;
3039 um
->id
.family
= x
->props
.family
;
3040 um
->id
.proto
= x
->id
.proto
;
3041 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
3042 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
3043 um
->new_sport
= new_sport
;
3044 um
->old_sport
= x
->encap
->encap_sport
;
3045 um
->reqid
= x
->props
.reqid
;
3047 nlmsg_end(skb
, nlh
);
3051 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
3054 struct net
*net
= xs_net(x
);
3055 struct sk_buff
*skb
;
3057 if (x
->id
.proto
!= IPPROTO_ESP
)
3063 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
3067 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
3070 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_MAPPING
);
3073 static bool xfrm_is_alive(const struct km_event
*c
)
3075 return (bool)xfrm_acquire_is_on(c
->net
);
3078 static struct xfrm_mgr netlink_mgr
= {
3080 .notify
= xfrm_send_state_notify
,
3081 .acquire
= xfrm_send_acquire
,
3082 .compile_policy
= xfrm_compile_policy
,
3083 .notify_policy
= xfrm_send_policy_notify
,
3084 .report
= xfrm_send_report
,
3085 .migrate
= xfrm_send_migrate
,
3086 .new_mapping
= xfrm_send_mapping
,
3087 .is_alive
= xfrm_is_alive
,
3090 static int __net_init
xfrm_user_net_init(struct net
*net
)
3093 struct netlink_kernel_cfg cfg
= {
3094 .groups
= XFRMNLGRP_MAX
,
3095 .input
= xfrm_netlink_rcv
,
3098 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, &cfg
);
3101 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
3102 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
3106 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
3109 list_for_each_entry(net
, net_exit_list
, exit_list
)
3110 RCU_INIT_POINTER(net
->xfrm
.nlsk
, NULL
);
3112 list_for_each_entry(net
, net_exit_list
, exit_list
)
3113 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
3116 static struct pernet_operations xfrm_user_net_ops
= {
3117 .init
= xfrm_user_net_init
,
3118 .exit_batch
= xfrm_user_net_exit
,
3121 static int __init
xfrm_user_init(void)
3125 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
3127 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
3130 rv
= xfrm_register_km(&netlink_mgr
);
3132 unregister_pernet_subsys(&xfrm_user_net_ops
);
3136 static void __exit
xfrm_user_exit(void)
3138 xfrm_unregister_km(&netlink_mgr
);
3139 unregister_pernet_subsys(&xfrm_user_net_ops
);
3142 module_init(xfrm_user_init
);
3143 module_exit(xfrm_user_exit
);
3144 MODULE_LICENSE("GPL");
3145 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);