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_crypt(struct xfrm_state
*x
, struct nlattr
*rta
)
294 struct xfrm_algo
*p
, *ualg
;
295 struct xfrm_algo_desc
*algo
;
300 ualg
= nla_data(rta
);
302 algo
= xfrm_ealg_get_byname(ualg
->alg_name
, 1);
305 x
->props
.ealgo
= algo
->desc
.sadb_alg_id
;
307 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
311 strcpy(p
->alg_name
, algo
->name
);
313 x
->geniv
= algo
->uinfo
.encr
.geniv
;
317 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
320 struct xfrm_algo
*ualg
;
321 struct xfrm_algo_auth
*p
;
322 struct xfrm_algo_desc
*algo
;
327 ualg
= nla_data(rta
);
329 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
332 *props
= algo
->desc
.sadb_alg_id
;
334 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
338 strcpy(p
->alg_name
, algo
->name
);
339 p
->alg_key_len
= ualg
->alg_key_len
;
340 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
341 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
347 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
350 struct xfrm_algo_auth
*p
, *ualg
;
351 struct xfrm_algo_desc
*algo
;
356 ualg
= nla_data(rta
);
358 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
361 if (ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
363 *props
= algo
->desc
.sadb_alg_id
;
365 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
369 strcpy(p
->alg_name
, algo
->name
);
370 if (!p
->alg_trunc_len
)
371 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
377 static int attach_aead(struct xfrm_state
*x
, struct nlattr
*rta
)
379 struct xfrm_algo_aead
*p
, *ualg
;
380 struct xfrm_algo_desc
*algo
;
385 ualg
= nla_data(rta
);
387 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
390 x
->props
.ealgo
= algo
->desc
.sadb_alg_id
;
392 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
396 strcpy(p
->alg_name
, algo
->name
);
398 x
->geniv
= algo
->uinfo
.aead
.geniv
;
402 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn
*replay_esn
,
405 struct xfrm_replay_state_esn
*up
;
408 if (!replay_esn
|| !rp
)
412 ulen
= xfrm_replay_state_esn_len(up
);
414 if (nla_len(rp
) < ulen
|| xfrm_replay_state_esn_len(replay_esn
) != ulen
)
420 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn
**replay_esn
,
421 struct xfrm_replay_state_esn
**preplay_esn
,
424 struct xfrm_replay_state_esn
*p
, *pp
, *up
;
431 klen
= xfrm_replay_state_esn_len(up
);
432 ulen
= nla_len(rta
) >= klen
? klen
: sizeof(*up
);
434 p
= kzalloc(klen
, GFP_KERNEL
);
438 pp
= kzalloc(klen
, GFP_KERNEL
);
445 memcpy(pp
, up
, ulen
);
453 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
458 len
+= sizeof(struct xfrm_user_sec_ctx
);
459 len
+= xfrm_ctx
->ctx_len
;
464 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
466 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
467 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
468 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
469 x
->props
.mode
= p
->mode
;
470 x
->props
.replay_window
= min_t(unsigned int, p
->replay_window
,
471 sizeof(x
->replay
.bitmap
) * 8);
472 x
->props
.reqid
= p
->reqid
;
473 x
->props
.family
= p
->family
;
474 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
475 x
->props
.flags
= p
->flags
;
477 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
478 x
->sel
.family
= p
->family
;
482 * someday when pfkey also has support, we could have the code
483 * somehow made shareable and move it to xfrm_state.c - JHS
486 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
,
489 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
490 struct nlattr
*re
= update_esn
? attrs
[XFRMA_REPLAY_ESN_VAL
] : NULL
;
491 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
492 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
493 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
496 struct xfrm_replay_state_esn
*replay_esn
;
497 replay_esn
= nla_data(re
);
498 memcpy(x
->replay_esn
, replay_esn
,
499 xfrm_replay_state_esn_len(replay_esn
));
500 memcpy(x
->preplay_esn
, replay_esn
,
501 xfrm_replay_state_esn_len(replay_esn
));
505 struct xfrm_replay_state
*replay
;
506 replay
= nla_data(rp
);
507 memcpy(&x
->replay
, replay
, sizeof(*replay
));
508 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
512 struct xfrm_lifetime_cur
*ltime
;
513 ltime
= nla_data(lt
);
514 x
->curlft
.bytes
= ltime
->bytes
;
515 x
->curlft
.packets
= ltime
->packets
;
516 x
->curlft
.add_time
= ltime
->add_time
;
517 x
->curlft
.use_time
= ltime
->use_time
;
521 x
->replay_maxage
= nla_get_u32(et
);
524 x
->replay_maxdiff
= nla_get_u32(rt
);
527 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
528 struct xfrm_usersa_info
*p
,
529 struct nlattr
**attrs
,
532 struct xfrm_state
*x
= xfrm_state_alloc(net
);
538 copy_from_user_state(x
, p
);
540 if (attrs
[XFRMA_SA_EXTRA_FLAGS
])
541 x
->props
.extra_flags
= nla_get_u32(attrs
[XFRMA_SA_EXTRA_FLAGS
]);
543 if ((err
= attach_aead(x
, attrs
[XFRMA_ALG_AEAD
])))
545 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
546 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
548 if (!x
->props
.aalgo
) {
549 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
550 attrs
[XFRMA_ALG_AUTH
])))
553 if ((err
= attach_crypt(x
, attrs
[XFRMA_ALG_CRYPT
])))
555 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
556 xfrm_calg_get_byname
,
557 attrs
[XFRMA_ALG_COMP
])))
560 if (attrs
[XFRMA_ENCAP
]) {
561 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
562 sizeof(*x
->encap
), GFP_KERNEL
);
563 if (x
->encap
== NULL
)
567 if (attrs
[XFRMA_TFCPAD
])
568 x
->tfcpad
= nla_get_u32(attrs
[XFRMA_TFCPAD
]);
570 if (attrs
[XFRMA_COADDR
]) {
571 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
572 sizeof(*x
->coaddr
), GFP_KERNEL
);
573 if (x
->coaddr
== NULL
)
577 xfrm_mark_get(attrs
, &x
->mark
);
579 err
= __xfrm_init_state(x
, false);
583 if (attrs
[XFRMA_SEC_CTX
] &&
584 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
587 if ((err
= xfrm_alloc_replay_state_esn(&x
->replay_esn
, &x
->preplay_esn
,
588 attrs
[XFRMA_REPLAY_ESN_VAL
])))
592 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
593 /* sysctl_xfrm_aevent_etime is in 100ms units */
594 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
596 if ((err
= xfrm_init_replay(x
)))
599 /* override default values from above */
600 xfrm_update_ae_params(x
, attrs
, 0);
605 x
->km
.state
= XFRM_STATE_DEAD
;
612 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
613 struct nlattr
**attrs
)
615 struct net
*net
= sock_net(skb
->sk
);
616 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
617 struct xfrm_state
*x
;
621 err
= verify_newsa_info(p
, attrs
);
625 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
630 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
631 err
= xfrm_state_add(x
);
633 err
= xfrm_state_update(x
);
635 xfrm_audit_state_add(x
, err
? 0 : 1, true);
638 x
->km
.state
= XFRM_STATE_DEAD
;
643 c
.seq
= nlh
->nlmsg_seq
;
644 c
.portid
= nlh
->nlmsg_pid
;
645 c
.event
= nlh
->nlmsg_type
;
647 km_state_notify(x
, &c
);
653 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
654 struct xfrm_usersa_id
*p
,
655 struct nlattr
**attrs
,
658 struct xfrm_state
*x
= NULL
;
661 u32 mark
= xfrm_mark_get(attrs
, &m
);
663 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
665 x
= xfrm_state_lookup(net
, mark
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
667 xfrm_address_t
*saddr
= NULL
;
669 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
676 x
= xfrm_state_lookup_byaddr(net
, mark
,
678 p
->proto
, p
->family
);
687 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
688 struct nlattr
**attrs
)
690 struct net
*net
= sock_net(skb
->sk
);
691 struct xfrm_state
*x
;
694 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
696 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
700 if ((err
= security_xfrm_state_delete(x
)) != 0)
703 if (xfrm_state_kern(x
)) {
708 err
= xfrm_state_delete(x
);
713 c
.seq
= nlh
->nlmsg_seq
;
714 c
.portid
= nlh
->nlmsg_pid
;
715 c
.event
= nlh
->nlmsg_type
;
716 km_state_notify(x
, &c
);
719 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
724 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
726 memset(p
, 0, sizeof(*p
));
727 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
728 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
729 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
730 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
731 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
732 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
733 p
->mode
= x
->props
.mode
;
734 p
->replay_window
= x
->props
.replay_window
;
735 p
->reqid
= x
->props
.reqid
;
736 p
->family
= x
->props
.family
;
737 p
->flags
= x
->props
.flags
;
741 struct xfrm_dump_info
{
742 struct sk_buff
*in_skb
;
743 struct sk_buff
*out_skb
;
748 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
750 struct xfrm_user_sec_ctx
*uctx
;
752 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
754 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
758 uctx
= nla_data(attr
);
759 uctx
->exttype
= XFRMA_SEC_CTX
;
760 uctx
->len
= ctx_size
;
761 uctx
->ctx_doi
= s
->ctx_doi
;
762 uctx
->ctx_alg
= s
->ctx_alg
;
763 uctx
->ctx_len
= s
->ctx_len
;
764 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
769 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
771 struct xfrm_algo
*algo
;
774 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
775 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
779 algo
= nla_data(nla
);
780 strncpy(algo
->alg_name
, auth
->alg_name
, sizeof(algo
->alg_name
));
781 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
782 algo
->alg_key_len
= auth
->alg_key_len
;
787 /* Don't change this without updating xfrm_sa_len! */
788 static int copy_to_user_state_extra(struct xfrm_state
*x
,
789 struct xfrm_usersa_info
*p
,
794 copy_to_user_state(x
, p
);
796 if (x
->props
.extra_flags
) {
797 ret
= nla_put_u32(skb
, XFRMA_SA_EXTRA_FLAGS
,
798 x
->props
.extra_flags
);
804 ret
= nla_put(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
809 ret
= nla_put_u64(skb
, XFRMA_LASTUSED
, x
->lastused
);
814 ret
= nla_put(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
819 ret
= copy_to_user_auth(x
->aalg
, skb
);
821 ret
= nla_put(skb
, XFRMA_ALG_AUTH_TRUNC
,
822 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
827 ret
= nla_put(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
832 ret
= nla_put(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
837 ret
= nla_put(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
842 ret
= nla_put_u32(skb
, XFRMA_TFCPAD
, x
->tfcpad
);
846 ret
= xfrm_mark_put(skb
, &x
->mark
);
850 ret
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
851 xfrm_replay_state_esn_len(x
->replay_esn
),
854 ret
= nla_put(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
),
859 ret
= copy_sec_ctx(x
->security
, skb
);
864 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
866 struct xfrm_dump_info
*sp
= ptr
;
867 struct sk_buff
*in_skb
= sp
->in_skb
;
868 struct sk_buff
*skb
= sp
->out_skb
;
869 struct xfrm_usersa_info
*p
;
870 struct nlmsghdr
*nlh
;
873 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
874 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
880 err
= copy_to_user_state_extra(x
, p
, skb
);
882 nlmsg_cancel(skb
, nlh
);
889 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
891 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
892 struct sock
*sk
= cb
->skb
->sk
;
893 struct net
*net
= sock_net(sk
);
895 xfrm_state_walk_done(walk
, net
);
899 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1];
900 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
902 struct net
*net
= sock_net(skb
->sk
);
903 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
904 struct xfrm_dump_info info
;
906 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
907 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
909 info
.in_skb
= cb
->skb
;
911 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
912 info
.nlmsg_flags
= NLM_F_MULTI
;
915 struct nlattr
*attrs
[XFRMA_MAX
+1];
916 struct xfrm_address_filter
*filter
= NULL
;
922 err
= nlmsg_parse(cb
->nlh
, 0, attrs
, XFRMA_MAX
,
927 if (attrs
[XFRMA_ADDRESS_FILTER
]) {
928 filter
= kmemdup(nla_data(attrs
[XFRMA_ADDRESS_FILTER
]),
929 sizeof(*filter
), GFP_KERNEL
);
934 if (attrs
[XFRMA_PROTO
])
935 proto
= nla_get_u8(attrs
[XFRMA_PROTO
]);
937 xfrm_state_walk_init(walk
, proto
, filter
);
940 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
945 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
946 struct xfrm_state
*x
, u32 seq
)
948 struct xfrm_dump_info info
;
952 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
954 return ERR_PTR(-ENOMEM
);
956 info
.in_skb
= in_skb
;
958 info
.nlmsg_seq
= seq
;
959 info
.nlmsg_flags
= 0;
961 err
= dump_one_state(x
, 0, &info
);
970 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
971 * Must be called with RCU read lock.
973 static inline int xfrm_nlmsg_multicast(struct net
*net
, struct sk_buff
*skb
,
974 u32 pid
, unsigned int group
)
976 struct sock
*nlsk
= rcu_dereference(net
->xfrm
.nlsk
);
979 return nlmsg_multicast(nlsk
, skb
, pid
, group
, GFP_ATOMIC
);
984 static inline size_t xfrm_spdinfo_msgsize(void)
986 return NLMSG_ALIGN(4)
987 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
988 + nla_total_size(sizeof(struct xfrmu_spdhinfo
))
989 + nla_total_size(sizeof(struct xfrmu_spdhthresh
))
990 + nla_total_size(sizeof(struct xfrmu_spdhthresh
));
993 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
994 u32 portid
, u32 seq
, u32 flags
)
996 struct xfrmk_spdinfo si
;
997 struct xfrmu_spdinfo spc
;
998 struct xfrmu_spdhinfo sph
;
999 struct xfrmu_spdhthresh spt4
, spt6
;
1000 struct nlmsghdr
*nlh
;
1005 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
1006 if (nlh
== NULL
) /* shouldn't really happen ... */
1009 f
= nlmsg_data(nlh
);
1011 xfrm_spd_getinfo(net
, &si
);
1012 spc
.incnt
= si
.incnt
;
1013 spc
.outcnt
= si
.outcnt
;
1014 spc
.fwdcnt
= si
.fwdcnt
;
1015 spc
.inscnt
= si
.inscnt
;
1016 spc
.outscnt
= si
.outscnt
;
1017 spc
.fwdscnt
= si
.fwdscnt
;
1018 sph
.spdhcnt
= si
.spdhcnt
;
1019 sph
.spdhmcnt
= si
.spdhmcnt
;
1022 lseq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
1024 spt4
.lbits
= net
->xfrm
.policy_hthresh
.lbits4
;
1025 spt4
.rbits
= net
->xfrm
.policy_hthresh
.rbits4
;
1026 spt6
.lbits
= net
->xfrm
.policy_hthresh
.lbits6
;
1027 spt6
.rbits
= net
->xfrm
.policy_hthresh
.rbits6
;
1028 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, lseq
));
1030 err
= nla_put(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
1032 err
= nla_put(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
1034 err
= nla_put(skb
, XFRMA_SPD_IPV4_HTHRESH
, sizeof(spt4
), &spt4
);
1036 err
= nla_put(skb
, XFRMA_SPD_IPV6_HTHRESH
, sizeof(spt6
), &spt6
);
1038 nlmsg_cancel(skb
, nlh
);
1042 nlmsg_end(skb
, nlh
);
1046 static int xfrm_set_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1047 struct nlattr
**attrs
)
1049 struct net
*net
= sock_net(skb
->sk
);
1050 struct xfrmu_spdhthresh
*thresh4
= NULL
;
1051 struct xfrmu_spdhthresh
*thresh6
= NULL
;
1053 /* selector prefixlen thresholds to hash policies */
1054 if (attrs
[XFRMA_SPD_IPV4_HTHRESH
]) {
1055 struct nlattr
*rta
= attrs
[XFRMA_SPD_IPV4_HTHRESH
];
1057 if (nla_len(rta
) < sizeof(*thresh4
))
1059 thresh4
= nla_data(rta
);
1060 if (thresh4
->lbits
> 32 || thresh4
->rbits
> 32)
1063 if (attrs
[XFRMA_SPD_IPV6_HTHRESH
]) {
1064 struct nlattr
*rta
= attrs
[XFRMA_SPD_IPV6_HTHRESH
];
1066 if (nla_len(rta
) < sizeof(*thresh6
))
1068 thresh6
= nla_data(rta
);
1069 if (thresh6
->lbits
> 128 || thresh6
->rbits
> 128)
1073 if (thresh4
|| thresh6
) {
1074 write_seqlock(&net
->xfrm
.policy_hthresh
.lock
);
1076 net
->xfrm
.policy_hthresh
.lbits4
= thresh4
->lbits
;
1077 net
->xfrm
.policy_hthresh
.rbits4
= thresh4
->rbits
;
1080 net
->xfrm
.policy_hthresh
.lbits6
= thresh6
->lbits
;
1081 net
->xfrm
.policy_hthresh
.rbits6
= thresh6
->rbits
;
1083 write_sequnlock(&net
->xfrm
.policy_hthresh
.lock
);
1085 xfrm_policy_hash_rebuild(net
);
1091 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1092 struct nlattr
**attrs
)
1094 struct net
*net
= sock_net(skb
->sk
);
1095 struct sk_buff
*r_skb
;
1096 u32
*flags
= nlmsg_data(nlh
);
1097 u32 sportid
= NETLINK_CB(skb
).portid
;
1098 u32 seq
= nlh
->nlmsg_seq
;
1100 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
1104 if (build_spdinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1107 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1110 static inline size_t xfrm_sadinfo_msgsize(void)
1112 return NLMSG_ALIGN(4)
1113 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
1114 + nla_total_size(4); /* XFRMA_SAD_CNT */
1117 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
1118 u32 portid
, u32 seq
, u32 flags
)
1120 struct xfrmk_sadinfo si
;
1121 struct xfrmu_sadhinfo sh
;
1122 struct nlmsghdr
*nlh
;
1126 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
1127 if (nlh
== NULL
) /* shouldn't really happen ... */
1130 f
= nlmsg_data(nlh
);
1132 xfrm_sad_getinfo(net
, &si
);
1134 sh
.sadhmcnt
= si
.sadhmcnt
;
1135 sh
.sadhcnt
= si
.sadhcnt
;
1137 err
= nla_put_u32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
1139 err
= nla_put(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
1141 nlmsg_cancel(skb
, nlh
);
1145 nlmsg_end(skb
, nlh
);
1149 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1150 struct nlattr
**attrs
)
1152 struct net
*net
= sock_net(skb
->sk
);
1153 struct sk_buff
*r_skb
;
1154 u32
*flags
= nlmsg_data(nlh
);
1155 u32 sportid
= NETLINK_CB(skb
).portid
;
1156 u32 seq
= nlh
->nlmsg_seq
;
1158 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
1162 if (build_sadinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1165 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1168 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1169 struct nlattr
**attrs
)
1171 struct net
*net
= sock_net(skb
->sk
);
1172 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
1173 struct xfrm_state
*x
;
1174 struct sk_buff
*resp_skb
;
1177 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
1181 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1182 if (IS_ERR(resp_skb
)) {
1183 err
= PTR_ERR(resp_skb
);
1185 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1192 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1193 struct nlattr
**attrs
)
1195 struct net
*net
= sock_net(skb
->sk
);
1196 struct xfrm_state
*x
;
1197 struct xfrm_userspi_info
*p
;
1198 struct sk_buff
*resp_skb
;
1199 xfrm_address_t
*daddr
;
1205 p
= nlmsg_data(nlh
);
1206 err
= verify_spi_info(p
->info
.id
.proto
, p
->min
, p
->max
);
1210 family
= p
->info
.family
;
1211 daddr
= &p
->info
.id
.daddr
;
1215 mark
= xfrm_mark_get(attrs
, &m
);
1217 x
= xfrm_find_acq_byseq(net
, mark
, p
->info
.seq
);
1218 if (x
&& !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
)) {
1225 x
= xfrm_find_acq(net
, &m
, p
->info
.mode
, p
->info
.reqid
,
1226 p
->info
.id
.proto
, daddr
,
1233 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
1237 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1238 if (IS_ERR(resp_skb
)) {
1239 err
= PTR_ERR(resp_skb
);
1243 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1251 static int verify_policy_dir(u8 dir
)
1254 case XFRM_POLICY_IN
:
1255 case XFRM_POLICY_OUT
:
1256 case XFRM_POLICY_FWD
:
1266 static int verify_policy_type(u8 type
)
1269 case XFRM_POLICY_TYPE_MAIN
:
1270 #ifdef CONFIG_XFRM_SUB_POLICY
1271 case XFRM_POLICY_TYPE_SUB
:
1282 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1287 case XFRM_SHARE_ANY
:
1288 case XFRM_SHARE_SESSION
:
1289 case XFRM_SHARE_USER
:
1290 case XFRM_SHARE_UNIQUE
:
1297 switch (p
->action
) {
1298 case XFRM_POLICY_ALLOW
:
1299 case XFRM_POLICY_BLOCK
:
1306 switch (p
->sel
.family
) {
1311 #if IS_ENABLED(CONFIG_IPV6)
1314 return -EAFNOSUPPORT
;
1321 ret
= verify_policy_dir(p
->dir
);
1324 if (p
->index
&& ((p
->index
& XFRM_POLICY_MAX
) != p
->dir
))
1330 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1332 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1333 struct xfrm_user_sec_ctx
*uctx
;
1338 uctx
= nla_data(rt
);
1339 return security_xfrm_policy_alloc(&pol
->security
, uctx
, GFP_KERNEL
);
1342 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1348 for (i
= 0; i
< nr
; i
++, ut
++) {
1349 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1351 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1352 memcpy(&t
->saddr
, &ut
->saddr
,
1353 sizeof(xfrm_address_t
));
1354 t
->reqid
= ut
->reqid
;
1356 t
->share
= ut
->share
;
1357 t
->optional
= ut
->optional
;
1358 t
->aalgos
= ut
->aalgos
;
1359 t
->ealgos
= ut
->ealgos
;
1360 t
->calgos
= ut
->calgos
;
1361 /* If all masks are ~0, then we allow all algorithms. */
1362 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1363 t
->encap_family
= ut
->family
;
1367 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1371 if (nr
> XFRM_MAX_DEPTH
)
1374 for (i
= 0; i
< nr
; i
++) {
1375 /* We never validated the ut->family value, so many
1376 * applications simply leave it at zero. The check was
1377 * never made and ut->family was ignored because all
1378 * templates could be assumed to have the same family as
1379 * the policy itself. Now that we will have ipv4-in-ipv6
1380 * and ipv6-in-ipv4 tunnels, this is no longer true.
1383 ut
[i
].family
= family
;
1385 switch (ut
[i
].family
) {
1388 #if IS_ENABLED(CONFIG_IPV6)
1400 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1402 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1407 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1408 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1411 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1415 copy_templates(pol
, utmpl
, nr
);
1420 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1422 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1423 struct xfrm_userpolicy_type
*upt
;
1424 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1432 err
= verify_policy_type(type
);
1440 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1442 xp
->priority
= p
->priority
;
1443 xp
->index
= p
->index
;
1444 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1445 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1446 xp
->action
= p
->action
;
1447 xp
->flags
= p
->flags
;
1448 xp
->family
= p
->sel
.family
;
1449 /* XXX xp->share = p->share; */
1452 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1454 memset(p
, 0, sizeof(*p
));
1455 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1456 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1457 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1458 p
->priority
= xp
->priority
;
1459 p
->index
= xp
->index
;
1460 p
->sel
.family
= xp
->family
;
1462 p
->action
= xp
->action
;
1463 p
->flags
= xp
->flags
;
1464 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1467 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1469 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1477 copy_from_user_policy(xp
, p
);
1479 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1483 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1484 err
= copy_from_user_sec_ctx(xp
, attrs
);
1488 xfrm_mark_get(attrs
, &xp
->mark
);
1494 xfrm_policy_destroy(xp
);
1498 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1499 struct nlattr
**attrs
)
1501 struct net
*net
= sock_net(skb
->sk
);
1502 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1503 struct xfrm_policy
*xp
;
1508 err
= verify_newpolicy_info(p
);
1511 err
= verify_sec_ctx_len(attrs
);
1515 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1519 /* shouldn't excl be based on nlh flags??
1520 * Aha! this is anti-netlink really i.e more pfkey derived
1521 * in netlink excl is a flag and you wouldnt need
1522 * a type XFRM_MSG_UPDPOLICY - JHS */
1523 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1524 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1525 xfrm_audit_policy_add(xp
, err
? 0 : 1, true);
1528 security_xfrm_policy_free(xp
->security
);
1533 c
.event
= nlh
->nlmsg_type
;
1534 c
.seq
= nlh
->nlmsg_seq
;
1535 c
.portid
= nlh
->nlmsg_pid
;
1536 km_policy_notify(xp
, p
->dir
, &c
);
1543 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1545 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1548 if (xp
->xfrm_nr
== 0)
1551 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1552 struct xfrm_user_tmpl
*up
= &vec
[i
];
1553 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1555 memset(up
, 0, sizeof(*up
));
1556 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1557 up
->family
= kp
->encap_family
;
1558 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1559 up
->reqid
= kp
->reqid
;
1560 up
->mode
= kp
->mode
;
1561 up
->share
= kp
->share
;
1562 up
->optional
= kp
->optional
;
1563 up
->aalgos
= kp
->aalgos
;
1564 up
->ealgos
= kp
->ealgos
;
1565 up
->calgos
= kp
->calgos
;
1568 return nla_put(skb
, XFRMA_TMPL
,
1569 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1572 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1575 return copy_sec_ctx(x
->security
, skb
);
1580 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1583 return copy_sec_ctx(xp
->security
, skb
);
1586 static inline size_t userpolicy_type_attrsize(void)
1588 #ifdef CONFIG_XFRM_SUB_POLICY
1589 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1595 #ifdef CONFIG_XFRM_SUB_POLICY
1596 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1598 struct xfrm_userpolicy_type upt
= {
1602 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1606 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1612 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1614 struct xfrm_dump_info
*sp
= ptr
;
1615 struct xfrm_userpolicy_info
*p
;
1616 struct sk_buff
*in_skb
= sp
->in_skb
;
1617 struct sk_buff
*skb
= sp
->out_skb
;
1618 struct nlmsghdr
*nlh
;
1621 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
1622 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1626 p
= nlmsg_data(nlh
);
1627 copy_to_user_policy(xp
, p
, dir
);
1628 err
= copy_to_user_tmpl(xp
, skb
);
1630 err
= copy_to_user_sec_ctx(xp
, skb
);
1632 err
= copy_to_user_policy_type(xp
->type
, skb
);
1634 err
= xfrm_mark_put(skb
, &xp
->mark
);
1636 nlmsg_cancel(skb
, nlh
);
1639 nlmsg_end(skb
, nlh
);
1643 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1645 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1646 struct net
*net
= sock_net(cb
->skb
->sk
);
1648 xfrm_policy_walk_done(walk
, net
);
1652 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1654 struct net
*net
= sock_net(skb
->sk
);
1655 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1656 struct xfrm_dump_info info
;
1658 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1659 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1661 info
.in_skb
= cb
->skb
;
1663 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1664 info
.nlmsg_flags
= NLM_F_MULTI
;
1668 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1671 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1676 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1677 struct xfrm_policy
*xp
,
1680 struct xfrm_dump_info info
;
1681 struct sk_buff
*skb
;
1684 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1686 return ERR_PTR(-ENOMEM
);
1688 info
.in_skb
= in_skb
;
1690 info
.nlmsg_seq
= seq
;
1691 info
.nlmsg_flags
= 0;
1693 err
= dump_one_policy(xp
, dir
, 0, &info
);
1696 return ERR_PTR(err
);
1702 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1703 struct nlattr
**attrs
)
1705 struct net
*net
= sock_net(skb
->sk
);
1706 struct xfrm_policy
*xp
;
1707 struct xfrm_userpolicy_id
*p
;
1708 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1713 u32 mark
= xfrm_mark_get(attrs
, &m
);
1715 p
= nlmsg_data(nlh
);
1716 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1718 err
= copy_from_user_policy_type(&type
, attrs
);
1722 err
= verify_policy_dir(p
->dir
);
1727 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, delete, &err
);
1729 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1730 struct xfrm_sec_ctx
*ctx
;
1732 err
= verify_sec_ctx_len(attrs
);
1738 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1740 err
= security_xfrm_policy_alloc(&ctx
, uctx
, GFP_KERNEL
);
1744 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
, &p
->sel
,
1746 security_xfrm_policy_free(ctx
);
1752 struct sk_buff
*resp_skb
;
1754 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1755 if (IS_ERR(resp_skb
)) {
1756 err
= PTR_ERR(resp_skb
);
1758 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1759 NETLINK_CB(skb
).portid
);
1762 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
1767 c
.data
.byid
= p
->index
;
1768 c
.event
= nlh
->nlmsg_type
;
1769 c
.seq
= nlh
->nlmsg_seq
;
1770 c
.portid
= nlh
->nlmsg_pid
;
1771 km_policy_notify(xp
, p
->dir
, &c
);
1776 if (delete && err
== 0)
1777 xfrm_garbage_collect(net
);
1781 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1782 struct nlattr
**attrs
)
1784 struct net
*net
= sock_net(skb
->sk
);
1786 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1789 err
= xfrm_state_flush(net
, p
->proto
, true);
1791 if (err
== -ESRCH
) /* empty table */
1795 c
.data
.proto
= p
->proto
;
1796 c
.event
= nlh
->nlmsg_type
;
1797 c
.seq
= nlh
->nlmsg_seq
;
1798 c
.portid
= nlh
->nlmsg_pid
;
1800 km_state_notify(NULL
, &c
);
1805 static inline size_t xfrm_aevent_msgsize(struct xfrm_state
*x
)
1807 size_t replay_size
= x
->replay_esn
?
1808 xfrm_replay_state_esn_len(x
->replay_esn
) :
1809 sizeof(struct xfrm_replay_state
);
1811 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1812 + nla_total_size(replay_size
)
1813 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1814 + nla_total_size(sizeof(struct xfrm_mark
))
1815 + nla_total_size(4) /* XFRM_AE_RTHR */
1816 + nla_total_size(4); /* XFRM_AE_ETHR */
1819 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
1821 struct xfrm_aevent_id
*id
;
1822 struct nlmsghdr
*nlh
;
1825 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1829 id
= nlmsg_data(nlh
);
1830 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
, sizeof(x
->id
.daddr
));
1831 id
->sa_id
.spi
= x
->id
.spi
;
1832 id
->sa_id
.family
= x
->props
.family
;
1833 id
->sa_id
.proto
= x
->id
.proto
;
1834 memcpy(&id
->saddr
, &x
->props
.saddr
, sizeof(x
->props
.saddr
));
1835 id
->reqid
= x
->props
.reqid
;
1836 id
->flags
= c
->data
.aevent
;
1838 if (x
->replay_esn
) {
1839 err
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
1840 xfrm_replay_state_esn_len(x
->replay_esn
),
1843 err
= nla_put(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
),
1848 err
= nla_put(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1852 if (id
->flags
& XFRM_AE_RTHR
) {
1853 err
= nla_put_u32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1857 if (id
->flags
& XFRM_AE_ETHR
) {
1858 err
= nla_put_u32(skb
, XFRMA_ETIMER_THRESH
,
1859 x
->replay_maxage
* 10 / HZ
);
1863 err
= xfrm_mark_put(skb
, &x
->mark
);
1867 nlmsg_end(skb
, nlh
);
1871 nlmsg_cancel(skb
, nlh
);
1875 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1876 struct nlattr
**attrs
)
1878 struct net
*net
= sock_net(skb
->sk
);
1879 struct xfrm_state
*x
;
1880 struct sk_buff
*r_skb
;
1885 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1886 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1888 mark
= xfrm_mark_get(attrs
, &m
);
1890 x
= xfrm_state_lookup(net
, mark
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1894 r_skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
1895 if (r_skb
== NULL
) {
1901 * XXX: is this lock really needed - none of the other
1902 * gets lock (the concern is things getting updated
1903 * while we are still reading) - jhs
1905 spin_lock_bh(&x
->lock
);
1906 c
.data
.aevent
= p
->flags
;
1907 c
.seq
= nlh
->nlmsg_seq
;
1908 c
.portid
= nlh
->nlmsg_pid
;
1910 if (build_aevent(r_skb
, x
, &c
) < 0)
1912 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).portid
);
1913 spin_unlock_bh(&x
->lock
);
1918 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1919 struct nlattr
**attrs
)
1921 struct net
*net
= sock_net(skb
->sk
);
1922 struct xfrm_state
*x
;
1927 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1928 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1929 struct nlattr
*re
= attrs
[XFRMA_REPLAY_ESN_VAL
];
1930 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1932 if (!lt
&& !rp
&& !re
)
1935 /* pedantic mode - thou shalt sayeth replaceth */
1936 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1939 mark
= xfrm_mark_get(attrs
, &m
);
1941 x
= xfrm_state_lookup(net
, mark
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1945 if (x
->km
.state
!= XFRM_STATE_VALID
)
1948 err
= xfrm_replay_verify_len(x
->replay_esn
, re
);
1952 spin_lock_bh(&x
->lock
);
1953 xfrm_update_ae_params(x
, attrs
, 1);
1954 spin_unlock_bh(&x
->lock
);
1956 c
.event
= nlh
->nlmsg_type
;
1957 c
.seq
= nlh
->nlmsg_seq
;
1958 c
.portid
= nlh
->nlmsg_pid
;
1959 c
.data
.aevent
= XFRM_AE_CU
;
1960 km_state_notify(x
, &c
);
1967 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1968 struct nlattr
**attrs
)
1970 struct net
*net
= sock_net(skb
->sk
);
1972 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1975 err
= copy_from_user_policy_type(&type
, attrs
);
1979 err
= xfrm_policy_flush(net
, type
, true);
1981 if (err
== -ESRCH
) /* empty table */
1987 c
.event
= nlh
->nlmsg_type
;
1988 c
.seq
= nlh
->nlmsg_seq
;
1989 c
.portid
= nlh
->nlmsg_pid
;
1991 km_policy_notify(NULL
, 0, &c
);
1995 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1996 struct nlattr
**attrs
)
1998 struct net
*net
= sock_net(skb
->sk
);
1999 struct xfrm_policy
*xp
;
2000 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
2001 struct xfrm_userpolicy_info
*p
= &up
->pol
;
2002 u8 type
= XFRM_POLICY_TYPE_MAIN
;
2005 u32 mark
= xfrm_mark_get(attrs
, &m
);
2007 err
= copy_from_user_policy_type(&type
, attrs
);
2011 err
= verify_policy_dir(p
->dir
);
2016 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, 0, &err
);
2018 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
2019 struct xfrm_sec_ctx
*ctx
;
2021 err
= verify_sec_ctx_len(attrs
);
2027 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
2029 err
= security_xfrm_policy_alloc(&ctx
, uctx
, GFP_KERNEL
);
2033 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
,
2034 &p
->sel
, ctx
, 0, &err
);
2035 security_xfrm_policy_free(ctx
);
2040 if (unlikely(xp
->walk
.dead
))
2045 xfrm_policy_delete(xp
, p
->dir
);
2046 xfrm_audit_policy_delete(xp
, 1, true);
2048 // reset the timers here?
2049 WARN(1, "Don't know what to do with soft policy expire\n");
2051 km_policy_expired(xp
, p
->dir
, up
->hard
, nlh
->nlmsg_pid
);
2058 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2059 struct nlattr
**attrs
)
2061 struct net
*net
= sock_net(skb
->sk
);
2062 struct xfrm_state
*x
;
2064 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
2065 struct xfrm_usersa_info
*p
= &ue
->state
;
2067 u32 mark
= xfrm_mark_get(attrs
, &m
);
2069 x
= xfrm_state_lookup(net
, mark
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
2075 spin_lock_bh(&x
->lock
);
2077 if (x
->km
.state
!= XFRM_STATE_VALID
)
2079 km_state_expired(x
, ue
->hard
, nlh
->nlmsg_pid
);
2082 __xfrm_state_delete(x
);
2083 xfrm_audit_state_delete(x
, 1, true);
2087 spin_unlock_bh(&x
->lock
);
2092 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2093 struct nlattr
**attrs
)
2095 struct net
*net
= sock_net(skb
->sk
);
2096 struct xfrm_policy
*xp
;
2097 struct xfrm_user_tmpl
*ut
;
2099 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
2100 struct xfrm_mark mark
;
2102 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
2103 struct xfrm_state
*x
= xfrm_state_alloc(net
);
2109 xfrm_mark_get(attrs
, &mark
);
2111 err
= verify_newpolicy_info(&ua
->policy
);
2116 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
2120 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
2121 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
2122 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
2123 xp
->mark
.m
= x
->mark
.m
= mark
.m
;
2124 xp
->mark
.v
= x
->mark
.v
= mark
.v
;
2126 /* extract the templates and for each call km_key */
2127 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
2128 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
2129 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
2130 x
->props
.mode
= t
->mode
;
2131 x
->props
.reqid
= t
->reqid
;
2132 x
->props
.family
= ut
->family
;
2133 t
->aalgos
= ua
->aalgos
;
2134 t
->ealgos
= ua
->ealgos
;
2135 t
->calgos
= ua
->calgos
;
2136 err
= km_query(x
, t
, xp
);
2146 WARN(1, "BAD policy passed\n");
2153 #ifdef CONFIG_XFRM_MIGRATE
2154 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
2155 struct xfrm_kmaddress
*k
,
2156 struct nlattr
**attrs
, int *num
)
2158 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
2159 struct xfrm_user_migrate
*um
;
2163 struct xfrm_user_kmaddress
*uk
;
2165 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
2166 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
2167 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
2168 k
->family
= uk
->family
;
2169 k
->reserved
= uk
->reserved
;
2173 num_migrate
= nla_len(rt
) / sizeof(*um
);
2175 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
2178 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
2179 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
2180 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
2181 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
2182 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
2184 ma
->proto
= um
->proto
;
2185 ma
->mode
= um
->mode
;
2186 ma
->reqid
= um
->reqid
;
2188 ma
->old_family
= um
->old_family
;
2189 ma
->new_family
= um
->new_family
;
2196 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2197 struct nlattr
**attrs
)
2199 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
2200 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2201 struct xfrm_kmaddress km
, *kmp
;
2205 struct net
*net
= sock_net(skb
->sk
);
2207 if (attrs
[XFRMA_MIGRATE
] == NULL
)
2210 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
2212 err
= copy_from_user_policy_type(&type
, attrs
);
2216 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
2223 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
, net
);
2228 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2229 struct nlattr
**attrs
)
2231 return -ENOPROTOOPT
;
2235 #ifdef CONFIG_XFRM_MIGRATE
2236 static int copy_to_user_migrate(const struct xfrm_migrate
*m
, struct sk_buff
*skb
)
2238 struct xfrm_user_migrate um
;
2240 memset(&um
, 0, sizeof(um
));
2241 um
.proto
= m
->proto
;
2243 um
.reqid
= m
->reqid
;
2244 um
.old_family
= m
->old_family
;
2245 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
2246 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
2247 um
.new_family
= m
->new_family
;
2248 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
2249 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
2251 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
2254 static int copy_to_user_kmaddress(const struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
2256 struct xfrm_user_kmaddress uk
;
2258 memset(&uk
, 0, sizeof(uk
));
2259 uk
.family
= k
->family
;
2260 uk
.reserved
= k
->reserved
;
2261 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
2262 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
2264 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
2267 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
)
2269 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
2270 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
2271 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
2272 + userpolicy_type_attrsize();
2275 static int build_migrate(struct sk_buff
*skb
, const struct xfrm_migrate
*m
,
2276 int num_migrate
, const struct xfrm_kmaddress
*k
,
2277 const struct xfrm_selector
*sel
, u8 dir
, u8 type
)
2279 const struct xfrm_migrate
*mp
;
2280 struct xfrm_userpolicy_id
*pol_id
;
2281 struct nlmsghdr
*nlh
;
2284 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
2288 pol_id
= nlmsg_data(nlh
);
2289 /* copy data from selector, dir, and type to the pol_id */
2290 memset(pol_id
, 0, sizeof(*pol_id
));
2291 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
2295 err
= copy_to_user_kmaddress(k
, skb
);
2299 err
= copy_to_user_policy_type(type
, skb
);
2302 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2303 err
= copy_to_user_migrate(mp
, skb
);
2308 nlmsg_end(skb
, nlh
);
2312 nlmsg_cancel(skb
, nlh
);
2316 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2317 const struct xfrm_migrate
*m
, int num_migrate
,
2318 const struct xfrm_kmaddress
*k
)
2320 struct net
*net
= &init_net
;
2321 struct sk_buff
*skb
;
2323 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
), GFP_ATOMIC
);
2328 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, dir
, type
) < 0)
2331 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_MIGRATE
);
2334 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2335 const struct xfrm_migrate
*m
, int num_migrate
,
2336 const struct xfrm_kmaddress
*k
)
2338 return -ENOPROTOOPT
;
2342 #define XMSGSIZE(type) sizeof(struct type)
2344 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2345 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2346 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2347 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2348 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2349 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2350 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2351 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2352 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2353 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2354 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2355 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2356 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2357 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2358 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2359 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2360 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2361 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2362 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2363 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2364 [XFRM_MSG_NEWSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2365 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2370 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2371 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2372 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2373 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2374 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2375 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2376 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2377 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2378 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2379 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2380 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2381 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2382 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2383 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2384 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2385 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2386 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2387 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2388 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2389 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2390 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2391 [XFRMA_MARK
] = { .len
= sizeof(struct xfrm_mark
) },
2392 [XFRMA_TFCPAD
] = { .type
= NLA_U32
},
2393 [XFRMA_REPLAY_ESN_VAL
] = { .len
= sizeof(struct xfrm_replay_state_esn
) },
2394 [XFRMA_SA_EXTRA_FLAGS
] = { .type
= NLA_U32
},
2395 [XFRMA_PROTO
] = { .type
= NLA_U8
},
2396 [XFRMA_ADDRESS_FILTER
] = { .len
= sizeof(struct xfrm_address_filter
) },
2399 static const struct nla_policy xfrma_spd_policy
[XFRMA_SPD_MAX
+1] = {
2400 [XFRMA_SPD_IPV4_HTHRESH
] = { .len
= sizeof(struct xfrmu_spdhthresh
) },
2401 [XFRMA_SPD_IPV6_HTHRESH
] = { .len
= sizeof(struct xfrmu_spdhthresh
) },
2404 static const struct xfrm_link
{
2405 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2406 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2407 int (*done
)(struct netlink_callback
*);
2408 const struct nla_policy
*nla_pol
;
2410 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2411 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2412 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2413 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2414 .dump
= xfrm_dump_sa
,
2415 .done
= xfrm_dump_sa_done
},
2416 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2417 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2418 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2419 .dump
= xfrm_dump_policy
,
2420 .done
= xfrm_dump_policy_done
},
2421 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2422 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2423 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2424 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2425 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2426 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2427 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2428 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2429 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2430 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2431 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2432 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2433 [XFRM_MSG_NEWSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_set_spdinfo
,
2434 .nla_pol
= xfrma_spd_policy
,
2435 .nla_max
= XFRMA_SPD_MAX
},
2436 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2439 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2441 struct net
*net
= sock_net(skb
->sk
);
2442 struct nlattr
*attrs
[XFRMA_MAX
+1];
2443 const struct xfrm_link
*link
;
2446 #ifdef CONFIG_COMPAT
2447 if (is_compat_task())
2451 type
= nlh
->nlmsg_type
;
2452 if (type
> XFRM_MSG_MAX
)
2455 type
-= XFRM_MSG_BASE
;
2456 link
= &xfrm_dispatch
[type
];
2458 /* All operations require privileges, even GET */
2459 if (!netlink_net_capable(skb
, CAP_NET_ADMIN
))
2462 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2463 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2464 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2465 if (link
->dump
== NULL
)
2469 struct netlink_dump_control c
= {
2473 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, &c
);
2477 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
,
2478 link
->nla_max
? : XFRMA_MAX
,
2479 link
->nla_pol
? : xfrma_policy
);
2483 if (link
->doit
== NULL
)
2486 return link
->doit(skb
, nlh
, attrs
);
2489 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2491 struct net
*net
= sock_net(skb
->sk
);
2493 mutex_lock(&net
->xfrm
.xfrm_cfg_mutex
);
2494 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2495 mutex_unlock(&net
->xfrm
.xfrm_cfg_mutex
);
2498 static inline size_t xfrm_expire_msgsize(void)
2500 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
))
2501 + nla_total_size(sizeof(struct xfrm_mark
));
2504 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
2506 struct xfrm_user_expire
*ue
;
2507 struct nlmsghdr
*nlh
;
2510 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2514 ue
= nlmsg_data(nlh
);
2515 copy_to_user_state(x
, &ue
->state
);
2516 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2518 err
= xfrm_mark_put(skb
, &x
->mark
);
2522 nlmsg_end(skb
, nlh
);
2526 static int xfrm_exp_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2528 struct net
*net
= xs_net(x
);
2529 struct sk_buff
*skb
;
2531 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2535 if (build_expire(skb
, x
, c
) < 0) {
2540 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_EXPIRE
);
2543 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2545 struct net
*net
= xs_net(x
);
2546 struct sk_buff
*skb
;
2548 skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
2552 if (build_aevent(skb
, x
, c
) < 0)
2555 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_AEVENTS
);
2558 static int xfrm_notify_sa_flush(const struct km_event
*c
)
2560 struct net
*net
= c
->net
;
2561 struct xfrm_usersa_flush
*p
;
2562 struct nlmsghdr
*nlh
;
2563 struct sk_buff
*skb
;
2564 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2566 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2570 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2576 p
= nlmsg_data(nlh
);
2577 p
->proto
= c
->data
.proto
;
2579 nlmsg_end(skb
, nlh
);
2581 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_SA
);
2584 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2588 l
+= nla_total_size(aead_len(x
->aead
));
2590 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2591 (x
->aalg
->alg_key_len
+ 7) / 8);
2592 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2595 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2597 l
+= nla_total_size(sizeof(*x
->calg
));
2599 l
+= nla_total_size(sizeof(*x
->encap
));
2601 l
+= nla_total_size(sizeof(x
->tfcpad
));
2603 l
+= nla_total_size(xfrm_replay_state_esn_len(x
->replay_esn
));
2605 l
+= nla_total_size(sizeof(struct xfrm_replay_state
));
2607 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2608 x
->security
->ctx_len
);
2610 l
+= nla_total_size(sizeof(*x
->coaddr
));
2611 if (x
->props
.extra_flags
)
2612 l
+= nla_total_size(sizeof(x
->props
.extra_flags
));
2614 /* Must count x->lastused as it may become non-zero behind our back. */
2615 l
+= nla_total_size(sizeof(u64
));
2620 static int xfrm_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
2622 struct net
*net
= xs_net(x
);
2623 struct xfrm_usersa_info
*p
;
2624 struct xfrm_usersa_id
*id
;
2625 struct nlmsghdr
*nlh
;
2626 struct sk_buff
*skb
;
2627 int len
= xfrm_sa_len(x
);
2630 headlen
= sizeof(*p
);
2631 if (c
->event
== XFRM_MSG_DELSA
) {
2632 len
+= nla_total_size(headlen
);
2633 headlen
= sizeof(*id
);
2634 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2636 len
+= NLMSG_ALIGN(headlen
);
2638 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2642 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2647 p
= nlmsg_data(nlh
);
2648 if (c
->event
== XFRM_MSG_DELSA
) {
2649 struct nlattr
*attr
;
2651 id
= nlmsg_data(nlh
);
2652 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2653 id
->spi
= x
->id
.spi
;
2654 id
->family
= x
->props
.family
;
2655 id
->proto
= x
->id
.proto
;
2657 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2664 err
= copy_to_user_state_extra(x
, p
, skb
);
2668 nlmsg_end(skb
, nlh
);
2670 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_SA
);
2677 static int xfrm_send_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2681 case XFRM_MSG_EXPIRE
:
2682 return xfrm_exp_state_notify(x
, c
);
2683 case XFRM_MSG_NEWAE
:
2684 return xfrm_aevent_state_notify(x
, c
);
2685 case XFRM_MSG_DELSA
:
2686 case XFRM_MSG_UPDSA
:
2687 case XFRM_MSG_NEWSA
:
2688 return xfrm_notify_sa(x
, c
);
2689 case XFRM_MSG_FLUSHSA
:
2690 return xfrm_notify_sa_flush(c
);
2692 printk(KERN_NOTICE
"xfrm_user: Unknown SA event %d\n",
2701 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2702 struct xfrm_policy
*xp
)
2704 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2705 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2706 + nla_total_size(sizeof(struct xfrm_mark
))
2707 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2708 + userpolicy_type_attrsize();
2711 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2712 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
)
2714 __u32 seq
= xfrm_get_acqseq();
2715 struct xfrm_user_acquire
*ua
;
2716 struct nlmsghdr
*nlh
;
2719 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2723 ua
= nlmsg_data(nlh
);
2724 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2725 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2726 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2727 copy_to_user_policy(xp
, &ua
->policy
, XFRM_POLICY_OUT
);
2728 ua
->aalgos
= xt
->aalgos
;
2729 ua
->ealgos
= xt
->ealgos
;
2730 ua
->calgos
= xt
->calgos
;
2731 ua
->seq
= x
->km
.seq
= seq
;
2733 err
= copy_to_user_tmpl(xp
, skb
);
2735 err
= copy_to_user_state_sec_ctx(x
, skb
);
2737 err
= copy_to_user_policy_type(xp
->type
, skb
);
2739 err
= xfrm_mark_put(skb
, &xp
->mark
);
2741 nlmsg_cancel(skb
, nlh
);
2745 nlmsg_end(skb
, nlh
);
2749 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2750 struct xfrm_policy
*xp
)
2752 struct net
*net
= xs_net(x
);
2753 struct sk_buff
*skb
;
2755 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2759 if (build_acquire(skb
, x
, xt
, xp
) < 0)
2762 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_ACQUIRE
);
2765 /* User gives us xfrm_user_policy_info followed by an array of 0
2766 * or more templates.
2768 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2769 u8
*data
, int len
, int *dir
)
2771 struct net
*net
= sock_net(sk
);
2772 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2773 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2774 struct xfrm_policy
*xp
;
2777 switch (sk
->sk_family
) {
2779 if (opt
!= IP_XFRM_POLICY
) {
2784 #if IS_ENABLED(CONFIG_IPV6)
2786 if (opt
!= IPV6_XFRM_POLICY
) {
2799 if (len
< sizeof(*p
) ||
2800 verify_newpolicy_info(p
))
2803 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2804 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2807 if (p
->dir
> XFRM_POLICY_OUT
)
2810 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
2816 copy_from_user_policy(xp
, p
);
2817 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2818 copy_templates(xp
, ut
, nr
);
2825 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2827 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2828 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2829 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2830 + nla_total_size(sizeof(struct xfrm_mark
))
2831 + userpolicy_type_attrsize();
2834 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2835 int dir
, const struct km_event
*c
)
2837 struct xfrm_user_polexpire
*upe
;
2838 int hard
= c
->data
.hard
;
2839 struct nlmsghdr
*nlh
;
2842 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2846 upe
= nlmsg_data(nlh
);
2847 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2848 err
= copy_to_user_tmpl(xp
, skb
);
2850 err
= copy_to_user_sec_ctx(xp
, skb
);
2852 err
= copy_to_user_policy_type(xp
->type
, skb
);
2854 err
= xfrm_mark_put(skb
, &xp
->mark
);
2856 nlmsg_cancel(skb
, nlh
);
2861 nlmsg_end(skb
, nlh
);
2865 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2867 struct net
*net
= xp_net(xp
);
2868 struct sk_buff
*skb
;
2870 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2874 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2877 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_EXPIRE
);
2880 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2882 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2883 struct net
*net
= xp_net(xp
);
2884 struct xfrm_userpolicy_info
*p
;
2885 struct xfrm_userpolicy_id
*id
;
2886 struct nlmsghdr
*nlh
;
2887 struct sk_buff
*skb
;
2890 headlen
= sizeof(*p
);
2891 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2892 len
+= nla_total_size(headlen
);
2893 headlen
= sizeof(*id
);
2895 len
+= userpolicy_type_attrsize();
2896 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2897 len
+= NLMSG_ALIGN(headlen
);
2899 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2903 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2908 p
= nlmsg_data(nlh
);
2909 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2910 struct nlattr
*attr
;
2912 id
= nlmsg_data(nlh
);
2913 memset(id
, 0, sizeof(*id
));
2916 id
->index
= xp
->index
;
2918 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2920 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2928 copy_to_user_policy(xp
, p
, dir
);
2929 err
= copy_to_user_tmpl(xp
, skb
);
2931 err
= copy_to_user_policy_type(xp
->type
, skb
);
2933 err
= xfrm_mark_put(skb
, &xp
->mark
);
2937 nlmsg_end(skb
, nlh
);
2939 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_POLICY
);
2946 static int xfrm_notify_policy_flush(const struct km_event
*c
)
2948 struct net
*net
= c
->net
;
2949 struct nlmsghdr
*nlh
;
2950 struct sk_buff
*skb
;
2953 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2957 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2961 err
= copy_to_user_policy_type(c
->data
.type
, skb
);
2965 nlmsg_end(skb
, nlh
);
2967 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_POLICY
);
2974 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2978 case XFRM_MSG_NEWPOLICY
:
2979 case XFRM_MSG_UPDPOLICY
:
2980 case XFRM_MSG_DELPOLICY
:
2981 return xfrm_notify_policy(xp
, dir
, c
);
2982 case XFRM_MSG_FLUSHPOLICY
:
2983 return xfrm_notify_policy_flush(c
);
2984 case XFRM_MSG_POLEXPIRE
:
2985 return xfrm_exp_policy_notify(xp
, dir
, c
);
2987 printk(KERN_NOTICE
"xfrm_user: Unknown Policy event %d\n",
2995 static inline size_t xfrm_report_msgsize(void)
2997 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
3000 static int build_report(struct sk_buff
*skb
, u8 proto
,
3001 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
3003 struct xfrm_user_report
*ur
;
3004 struct nlmsghdr
*nlh
;
3006 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
3010 ur
= nlmsg_data(nlh
);
3012 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
3015 int err
= nla_put(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
3017 nlmsg_cancel(skb
, nlh
);
3021 nlmsg_end(skb
, nlh
);
3025 static int xfrm_send_report(struct net
*net
, u8 proto
,
3026 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
3028 struct sk_buff
*skb
;
3030 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
3034 if (build_report(skb
, proto
, sel
, addr
) < 0)
3037 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_REPORT
);
3040 static inline size_t xfrm_mapping_msgsize(void)
3042 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
3045 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
3046 xfrm_address_t
*new_saddr
, __be16 new_sport
)
3048 struct xfrm_user_mapping
*um
;
3049 struct nlmsghdr
*nlh
;
3051 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
3055 um
= nlmsg_data(nlh
);
3057 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
3058 um
->id
.spi
= x
->id
.spi
;
3059 um
->id
.family
= x
->props
.family
;
3060 um
->id
.proto
= x
->id
.proto
;
3061 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
3062 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
3063 um
->new_sport
= new_sport
;
3064 um
->old_sport
= x
->encap
->encap_sport
;
3065 um
->reqid
= x
->props
.reqid
;
3067 nlmsg_end(skb
, nlh
);
3071 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
3074 struct net
*net
= xs_net(x
);
3075 struct sk_buff
*skb
;
3077 if (x
->id
.proto
!= IPPROTO_ESP
)
3083 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
3087 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
3090 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_MAPPING
);
3093 static bool xfrm_is_alive(const struct km_event
*c
)
3095 return (bool)xfrm_acquire_is_on(c
->net
);
3098 static struct xfrm_mgr netlink_mgr
= {
3100 .notify
= xfrm_send_state_notify
,
3101 .acquire
= xfrm_send_acquire
,
3102 .compile_policy
= xfrm_compile_policy
,
3103 .notify_policy
= xfrm_send_policy_notify
,
3104 .report
= xfrm_send_report
,
3105 .migrate
= xfrm_send_migrate
,
3106 .new_mapping
= xfrm_send_mapping
,
3107 .is_alive
= xfrm_is_alive
,
3110 static int __net_init
xfrm_user_net_init(struct net
*net
)
3113 struct netlink_kernel_cfg cfg
= {
3114 .groups
= XFRMNLGRP_MAX
,
3115 .input
= xfrm_netlink_rcv
,
3118 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, &cfg
);
3121 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
3122 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
3126 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
3129 list_for_each_entry(net
, net_exit_list
, exit_list
)
3130 RCU_INIT_POINTER(net
->xfrm
.nlsk
, NULL
);
3132 list_for_each_entry(net
, net_exit_list
, exit_list
)
3133 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
3136 static struct pernet_operations xfrm_user_net_ops
= {
3137 .init
= xfrm_user_net_init
,
3138 .exit_batch
= xfrm_user_net_exit
,
3141 static int __init
xfrm_user_init(void)
3145 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
3147 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
3150 rv
= xfrm_register_km(&netlink_mgr
);
3152 unregister_pernet_subsys(&xfrm_user_net_ops
);
3156 static void __exit
xfrm_user_exit(void)
3158 xfrm_unregister_km(&netlink_mgr
);
3159 unregister_pernet_subsys(&xfrm_user_net_ops
);
3162 module_init(xfrm_user_init
);
3163 module_exit(xfrm_user_exit
);
3164 MODULE_LICENSE("GPL");
3165 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);