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 <linux/uaccess.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <linux/in6.h>
34 #include <asm/unaligned.h>
36 static int verify_one_alg(struct nlattr
**attrs
, enum xfrm_attr_type_t type
)
38 struct nlattr
*rt
= attrs
[type
];
39 struct xfrm_algo
*algp
;
45 if (nla_len(rt
) < xfrm_alg_len(algp
))
58 algp
->alg_name
[sizeof(algp
->alg_name
) - 1] = '\0';
62 static int verify_auth_trunc(struct nlattr
**attrs
)
64 struct nlattr
*rt
= attrs
[XFRMA_ALG_AUTH_TRUNC
];
65 struct xfrm_algo_auth
*algp
;
71 if (nla_len(rt
) < xfrm_alg_auth_len(algp
))
74 algp
->alg_name
[sizeof(algp
->alg_name
) - 1] = '\0';
78 static int verify_aead(struct nlattr
**attrs
)
80 struct nlattr
*rt
= attrs
[XFRMA_ALG_AEAD
];
81 struct xfrm_algo_aead
*algp
;
87 if (nla_len(rt
) < aead_len(algp
))
90 algp
->alg_name
[sizeof(algp
->alg_name
) - 1] = '\0';
94 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
95 xfrm_address_t
**addrp
)
97 struct nlattr
*rt
= attrs
[type
];
100 *addrp
= nla_data(rt
);
103 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
105 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
106 struct xfrm_user_sec_ctx
*uctx
;
112 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
118 static inline int verify_replay(struct xfrm_usersa_info
*p
,
119 struct nlattr
**attrs
)
121 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_ESN_VAL
];
122 struct xfrm_replay_state_esn
*rs
;
125 return (p
->flags
& XFRM_STATE_ESN
) ? -EINVAL
: 0;
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
))
136 /* As only ESP and AH support ESN feature. */
137 if ((p
->id
.proto
!= IPPROTO_ESP
) && (p
->id
.proto
!= IPPROTO_AH
))
140 if (p
->replay_window
!= 0)
146 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
147 struct nlattr
**attrs
)
157 #if IS_ENABLED(CONFIG_IPV6)
168 switch (p
->sel
.family
) {
173 if (p
->sel
.prefixlen_d
> 32 || p
->sel
.prefixlen_s
> 32)
179 #if IS_ENABLED(CONFIG_IPV6)
180 if (p
->sel
.prefixlen_d
> 128 || p
->sel
.prefixlen_s
> 128)
194 switch (p
->id
.proto
) {
196 if ((!attrs
[XFRMA_ALG_AUTH
] &&
197 !attrs
[XFRMA_ALG_AUTH_TRUNC
]) ||
198 attrs
[XFRMA_ALG_AEAD
] ||
199 attrs
[XFRMA_ALG_CRYPT
] ||
200 attrs
[XFRMA_ALG_COMP
] ||
206 if (attrs
[XFRMA_ALG_COMP
])
208 if (!attrs
[XFRMA_ALG_AUTH
] &&
209 !attrs
[XFRMA_ALG_AUTH_TRUNC
] &&
210 !attrs
[XFRMA_ALG_CRYPT
] &&
211 !attrs
[XFRMA_ALG_AEAD
])
213 if ((attrs
[XFRMA_ALG_AUTH
] ||
214 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
215 attrs
[XFRMA_ALG_CRYPT
]) &&
216 attrs
[XFRMA_ALG_AEAD
])
218 if (attrs
[XFRMA_TFCPAD
] &&
219 p
->mode
!= XFRM_MODE_TUNNEL
)
224 if (!attrs
[XFRMA_ALG_COMP
] ||
225 attrs
[XFRMA_ALG_AEAD
] ||
226 attrs
[XFRMA_ALG_AUTH
] ||
227 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
228 attrs
[XFRMA_ALG_CRYPT
] ||
229 attrs
[XFRMA_TFCPAD
] ||
230 (ntohl(p
->id
.spi
) >= 0x10000))
234 #if IS_ENABLED(CONFIG_IPV6)
235 case IPPROTO_DSTOPTS
:
236 case IPPROTO_ROUTING
:
237 if (attrs
[XFRMA_ALG_COMP
] ||
238 attrs
[XFRMA_ALG_AUTH
] ||
239 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
240 attrs
[XFRMA_ALG_AEAD
] ||
241 attrs
[XFRMA_ALG_CRYPT
] ||
242 attrs
[XFRMA_ENCAP
] ||
243 attrs
[XFRMA_SEC_CTX
] ||
244 attrs
[XFRMA_TFCPAD
] ||
245 !attrs
[XFRMA_COADDR
])
254 if ((err
= verify_aead(attrs
)))
256 if ((err
= verify_auth_trunc(attrs
)))
258 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
260 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
262 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
264 if ((err
= verify_sec_ctx_len(attrs
)))
266 if ((err
= verify_replay(p
, attrs
)))
271 case XFRM_MODE_TRANSPORT
:
272 case XFRM_MODE_TUNNEL
:
273 case XFRM_MODE_ROUTEOPTIMIZATION
:
287 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
288 struct xfrm_algo_desc
*(*get_byname
)(const char *, int),
291 struct xfrm_algo
*p
, *ualg
;
292 struct xfrm_algo_desc
*algo
;
297 ualg
= nla_data(rta
);
299 algo
= get_byname(ualg
->alg_name
, 1);
302 *props
= algo
->desc
.sadb_alg_id
;
304 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
308 strcpy(p
->alg_name
, algo
->name
);
313 static int attach_crypt(struct xfrm_state
*x
, struct nlattr
*rta
)
315 struct xfrm_algo
*p
, *ualg
;
316 struct xfrm_algo_desc
*algo
;
321 ualg
= nla_data(rta
);
323 algo
= xfrm_ealg_get_byname(ualg
->alg_name
, 1);
326 x
->props
.ealgo
= algo
->desc
.sadb_alg_id
;
328 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
332 strcpy(p
->alg_name
, algo
->name
);
334 x
->geniv
= algo
->uinfo
.encr
.geniv
;
338 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
341 struct xfrm_algo
*ualg
;
342 struct xfrm_algo_auth
*p
;
343 struct xfrm_algo_desc
*algo
;
348 ualg
= nla_data(rta
);
350 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
353 *props
= algo
->desc
.sadb_alg_id
;
355 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
359 strcpy(p
->alg_name
, algo
->name
);
360 p
->alg_key_len
= ualg
->alg_key_len
;
361 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
362 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
368 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
371 struct xfrm_algo_auth
*p
, *ualg
;
372 struct xfrm_algo_desc
*algo
;
377 ualg
= nla_data(rta
);
379 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
382 if (ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
384 *props
= algo
->desc
.sadb_alg_id
;
386 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
390 strcpy(p
->alg_name
, algo
->name
);
391 if (!p
->alg_trunc_len
)
392 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
398 static int attach_aead(struct xfrm_state
*x
, struct nlattr
*rta
)
400 struct xfrm_algo_aead
*p
, *ualg
;
401 struct xfrm_algo_desc
*algo
;
406 ualg
= nla_data(rta
);
408 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
411 x
->props
.ealgo
= algo
->desc
.sadb_alg_id
;
413 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
417 strcpy(p
->alg_name
, algo
->name
);
419 x
->geniv
= algo
->uinfo
.aead
.geniv
;
423 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn
*replay_esn
,
426 struct xfrm_replay_state_esn
*up
;
429 if (!replay_esn
|| !rp
)
433 ulen
= xfrm_replay_state_esn_len(up
);
435 /* Check the overall length and the internal bitmap length to avoid
436 * potential overflow. */
437 if (nla_len(rp
) < ulen
||
438 xfrm_replay_state_esn_len(replay_esn
) != ulen
||
439 replay_esn
->bmp_len
!= up
->bmp_len
)
442 if (up
->replay_window
> up
->bmp_len
* sizeof(__u32
) * 8)
448 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn
**replay_esn
,
449 struct xfrm_replay_state_esn
**preplay_esn
,
452 struct xfrm_replay_state_esn
*p
, *pp
, *up
;
459 klen
= xfrm_replay_state_esn_len(up
);
460 ulen
= nla_len(rta
) >= klen
? klen
: sizeof(*up
);
462 p
= kzalloc(klen
, GFP_KERNEL
);
466 pp
= kzalloc(klen
, GFP_KERNEL
);
473 memcpy(pp
, up
, ulen
);
481 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
486 len
+= sizeof(struct xfrm_user_sec_ctx
);
487 len
+= xfrm_ctx
->ctx_len
;
492 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
494 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
495 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
496 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
497 x
->props
.mode
= p
->mode
;
498 x
->props
.replay_window
= min_t(unsigned int, p
->replay_window
,
499 sizeof(x
->replay
.bitmap
) * 8);
500 x
->props
.reqid
= p
->reqid
;
501 x
->props
.family
= p
->family
;
502 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
503 x
->props
.flags
= p
->flags
;
505 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
506 x
->sel
.family
= p
->family
;
510 * someday when pfkey also has support, we could have the code
511 * somehow made shareable and move it to xfrm_state.c - JHS
514 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
,
517 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
518 struct nlattr
*re
= update_esn
? attrs
[XFRMA_REPLAY_ESN_VAL
] : NULL
;
519 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
520 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
521 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
524 struct xfrm_replay_state_esn
*replay_esn
;
525 replay_esn
= nla_data(re
);
526 memcpy(x
->replay_esn
, replay_esn
,
527 xfrm_replay_state_esn_len(replay_esn
));
528 memcpy(x
->preplay_esn
, replay_esn
,
529 xfrm_replay_state_esn_len(replay_esn
));
533 struct xfrm_replay_state
*replay
;
534 replay
= nla_data(rp
);
535 memcpy(&x
->replay
, replay
, sizeof(*replay
));
536 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
540 struct xfrm_lifetime_cur
*ltime
;
541 ltime
= nla_data(lt
);
542 x
->curlft
.bytes
= ltime
->bytes
;
543 x
->curlft
.packets
= ltime
->packets
;
544 x
->curlft
.add_time
= ltime
->add_time
;
545 x
->curlft
.use_time
= ltime
->use_time
;
549 x
->replay_maxage
= nla_get_u32(et
);
552 x
->replay_maxdiff
= nla_get_u32(rt
);
555 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
556 struct xfrm_usersa_info
*p
,
557 struct nlattr
**attrs
,
560 struct xfrm_state
*x
= xfrm_state_alloc(net
);
566 copy_from_user_state(x
, p
);
568 if (attrs
[XFRMA_SA_EXTRA_FLAGS
])
569 x
->props
.extra_flags
= nla_get_u32(attrs
[XFRMA_SA_EXTRA_FLAGS
]);
571 if ((err
= attach_aead(x
, attrs
[XFRMA_ALG_AEAD
])))
573 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
574 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
576 if (!x
->props
.aalgo
) {
577 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
578 attrs
[XFRMA_ALG_AUTH
])))
581 if ((err
= attach_crypt(x
, attrs
[XFRMA_ALG_CRYPT
])))
583 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
584 xfrm_calg_get_byname
,
585 attrs
[XFRMA_ALG_COMP
])))
588 if (attrs
[XFRMA_ENCAP
]) {
589 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
590 sizeof(*x
->encap
), GFP_KERNEL
);
591 if (x
->encap
== NULL
)
595 if (attrs
[XFRMA_TFCPAD
])
596 x
->tfcpad
= nla_get_u32(attrs
[XFRMA_TFCPAD
]);
598 if (attrs
[XFRMA_COADDR
]) {
599 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
600 sizeof(*x
->coaddr
), GFP_KERNEL
);
601 if (x
->coaddr
== NULL
)
605 xfrm_mark_get(attrs
, &x
->mark
);
607 if (attrs
[XFRMA_OUTPUT_MARK
])
608 x
->props
.output_mark
= nla_get_u32(attrs
[XFRMA_OUTPUT_MARK
]);
610 err
= __xfrm_init_state(x
, false, attrs
[XFRMA_OFFLOAD_DEV
]);
614 if (attrs
[XFRMA_SEC_CTX
]) {
615 err
= security_xfrm_state_alloc(x
,
616 nla_data(attrs
[XFRMA_SEC_CTX
]));
621 if (attrs
[XFRMA_OFFLOAD_DEV
]) {
622 err
= xfrm_dev_state_add(net
, x
,
623 nla_data(attrs
[XFRMA_OFFLOAD_DEV
]));
628 if ((err
= xfrm_alloc_replay_state_esn(&x
->replay_esn
, &x
->preplay_esn
,
629 attrs
[XFRMA_REPLAY_ESN_VAL
])))
633 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
634 /* sysctl_xfrm_aevent_etime is in 100ms units */
635 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
637 if ((err
= xfrm_init_replay(x
)))
640 /* override default values from above */
641 xfrm_update_ae_params(x
, attrs
, 0);
646 x
->km
.state
= XFRM_STATE_DEAD
;
653 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
654 struct nlattr
**attrs
)
656 struct net
*net
= sock_net(skb
->sk
);
657 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
658 struct xfrm_state
*x
;
662 err
= verify_newsa_info(p
, attrs
);
666 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
671 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
672 err
= xfrm_state_add(x
);
674 err
= xfrm_state_update(x
);
676 xfrm_audit_state_add(x
, err
? 0 : 1, true);
679 x
->km
.state
= XFRM_STATE_DEAD
;
680 xfrm_dev_state_delete(x
);
685 c
.seq
= nlh
->nlmsg_seq
;
686 c
.portid
= nlh
->nlmsg_pid
;
687 c
.event
= nlh
->nlmsg_type
;
689 km_state_notify(x
, &c
);
695 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
696 struct xfrm_usersa_id
*p
,
697 struct nlattr
**attrs
,
700 struct xfrm_state
*x
= NULL
;
703 u32 mark
= xfrm_mark_get(attrs
, &m
);
705 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
707 x
= xfrm_state_lookup(net
, mark
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
709 xfrm_address_t
*saddr
= NULL
;
711 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
718 x
= xfrm_state_lookup_byaddr(net
, mark
,
720 p
->proto
, p
->family
);
729 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
730 struct nlattr
**attrs
)
732 struct net
*net
= sock_net(skb
->sk
);
733 struct xfrm_state
*x
;
736 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
738 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
742 if ((err
= security_xfrm_state_delete(x
)) != 0)
745 if (xfrm_state_kern(x
)) {
750 err
= xfrm_state_delete(x
);
755 c
.seq
= nlh
->nlmsg_seq
;
756 c
.portid
= nlh
->nlmsg_pid
;
757 c
.event
= nlh
->nlmsg_type
;
758 km_state_notify(x
, &c
);
761 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
766 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
768 memset(p
, 0, sizeof(*p
));
769 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
770 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
771 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
772 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
773 put_unaligned(x
->stats
.replay_window
, &p
->stats
.replay_window
);
774 put_unaligned(x
->stats
.replay
, &p
->stats
.replay
);
775 put_unaligned(x
->stats
.integrity_failed
, &p
->stats
.integrity_failed
);
776 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
777 p
->mode
= x
->props
.mode
;
778 p
->replay_window
= x
->props
.replay_window
;
779 p
->reqid
= x
->props
.reqid
;
780 p
->family
= x
->props
.family
;
781 p
->flags
= x
->props
.flags
;
785 struct xfrm_dump_info
{
786 struct sk_buff
*in_skb
;
787 struct sk_buff
*out_skb
;
792 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
794 struct xfrm_user_sec_ctx
*uctx
;
796 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
798 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
802 uctx
= nla_data(attr
);
803 uctx
->exttype
= XFRMA_SEC_CTX
;
804 uctx
->len
= ctx_size
;
805 uctx
->ctx_doi
= s
->ctx_doi
;
806 uctx
->ctx_alg
= s
->ctx_alg
;
807 uctx
->ctx_len
= s
->ctx_len
;
808 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
813 static int copy_user_offload(struct xfrm_state_offload
*xso
, struct sk_buff
*skb
)
815 struct xfrm_user_offload
*xuo
;
818 attr
= nla_reserve(skb
, XFRMA_OFFLOAD_DEV
, sizeof(*xuo
));
822 xuo
= nla_data(attr
);
823 memset(xuo
, 0, sizeof(*xuo
));
824 xuo
->ifindex
= xso
->dev
->ifindex
;
825 xuo
->flags
= xso
->flags
;
830 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
832 struct xfrm_algo
*algo
;
835 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
836 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
840 algo
= nla_data(nla
);
841 strncpy(algo
->alg_name
, auth
->alg_name
, sizeof(algo
->alg_name
));
842 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
843 algo
->alg_key_len
= auth
->alg_key_len
;
848 /* Don't change this without updating xfrm_sa_len! */
849 static int copy_to_user_state_extra(struct xfrm_state
*x
,
850 struct xfrm_usersa_info
*p
,
855 copy_to_user_state(x
, p
);
857 if (x
->props
.extra_flags
) {
858 ret
= nla_put_u32(skb
, XFRMA_SA_EXTRA_FLAGS
,
859 x
->props
.extra_flags
);
865 ret
= nla_put(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
870 ret
= nla_put_u64_64bit(skb
, XFRMA_LASTUSED
, x
->lastused
,
876 ret
= nla_put(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
881 ret
= copy_to_user_auth(x
->aalg
, skb
);
883 ret
= nla_put(skb
, XFRMA_ALG_AUTH_TRUNC
,
884 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
889 ret
= nla_put(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
894 ret
= nla_put(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
899 ret
= nla_put(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
904 ret
= nla_put_u32(skb
, XFRMA_TFCPAD
, x
->tfcpad
);
908 ret
= xfrm_mark_put(skb
, &x
->mark
);
912 ret
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
913 xfrm_replay_state_esn_len(x
->replay_esn
),
916 ret
= nla_put(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
),
921 ret
= copy_user_offload(&x
->xso
, skb
);
924 if (x
->props
.output_mark
) {
925 ret
= nla_put_u32(skb
, XFRMA_OUTPUT_MARK
, x
->props
.output_mark
);
930 ret
= copy_sec_ctx(x
->security
, skb
);
935 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
937 struct xfrm_dump_info
*sp
= ptr
;
938 struct sk_buff
*in_skb
= sp
->in_skb
;
939 struct sk_buff
*skb
= sp
->out_skb
;
940 struct xfrm_usersa_info
*p
;
941 struct nlmsghdr
*nlh
;
944 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
945 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
951 err
= copy_to_user_state_extra(x
, p
, skb
);
953 nlmsg_cancel(skb
, nlh
);
960 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
962 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
963 struct sock
*sk
= cb
->skb
->sk
;
964 struct net
*net
= sock_net(sk
);
967 xfrm_state_walk_done(walk
, net
);
971 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1];
972 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
974 struct net
*net
= sock_net(skb
->sk
);
975 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
976 struct xfrm_dump_info info
;
978 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
979 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
981 info
.in_skb
= cb
->skb
;
983 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
984 info
.nlmsg_flags
= NLM_F_MULTI
;
987 struct nlattr
*attrs
[XFRMA_MAX
+1];
988 struct xfrm_address_filter
*filter
= NULL
;
992 err
= nlmsg_parse(cb
->nlh
, 0, attrs
, XFRMA_MAX
, xfrma_policy
,
997 if (attrs
[XFRMA_ADDRESS_FILTER
]) {
998 filter
= kmemdup(nla_data(attrs
[XFRMA_ADDRESS_FILTER
]),
999 sizeof(*filter
), GFP_KERNEL
);
1004 if (attrs
[XFRMA_PROTO
])
1005 proto
= nla_get_u8(attrs
[XFRMA_PROTO
]);
1007 xfrm_state_walk_init(walk
, proto
, filter
);
1011 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
1016 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
1017 struct xfrm_state
*x
, u32 seq
)
1019 struct xfrm_dump_info info
;
1020 struct sk_buff
*skb
;
1023 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1025 return ERR_PTR(-ENOMEM
);
1027 info
.in_skb
= in_skb
;
1029 info
.nlmsg_seq
= seq
;
1030 info
.nlmsg_flags
= 0;
1032 err
= dump_one_state(x
, 0, &info
);
1035 return ERR_PTR(err
);
1041 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1042 * Must be called with RCU read lock.
1044 static inline int xfrm_nlmsg_multicast(struct net
*net
, struct sk_buff
*skb
,
1045 u32 pid
, unsigned int group
)
1047 struct sock
*nlsk
= rcu_dereference(net
->xfrm
.nlsk
);
1054 return nlmsg_multicast(nlsk
, skb
, pid
, group
, GFP_ATOMIC
);
1057 static inline size_t xfrm_spdinfo_msgsize(void)
1059 return NLMSG_ALIGN(4)
1060 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
1061 + nla_total_size(sizeof(struct xfrmu_spdhinfo
))
1062 + nla_total_size(sizeof(struct xfrmu_spdhthresh
))
1063 + nla_total_size(sizeof(struct xfrmu_spdhthresh
));
1066 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
1067 u32 portid
, u32 seq
, u32 flags
)
1069 struct xfrmk_spdinfo si
;
1070 struct xfrmu_spdinfo spc
;
1071 struct xfrmu_spdhinfo sph
;
1072 struct xfrmu_spdhthresh spt4
, spt6
;
1073 struct nlmsghdr
*nlh
;
1078 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
1079 if (nlh
== NULL
) /* shouldn't really happen ... */
1082 f
= nlmsg_data(nlh
);
1084 xfrm_spd_getinfo(net
, &si
);
1085 spc
.incnt
= si
.incnt
;
1086 spc
.outcnt
= si
.outcnt
;
1087 spc
.fwdcnt
= si
.fwdcnt
;
1088 spc
.inscnt
= si
.inscnt
;
1089 spc
.outscnt
= si
.outscnt
;
1090 spc
.fwdscnt
= si
.fwdscnt
;
1091 sph
.spdhcnt
= si
.spdhcnt
;
1092 sph
.spdhmcnt
= si
.spdhmcnt
;
1095 lseq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
1097 spt4
.lbits
= net
->xfrm
.policy_hthresh
.lbits4
;
1098 spt4
.rbits
= net
->xfrm
.policy_hthresh
.rbits4
;
1099 spt6
.lbits
= net
->xfrm
.policy_hthresh
.lbits6
;
1100 spt6
.rbits
= net
->xfrm
.policy_hthresh
.rbits6
;
1101 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, lseq
));
1103 err
= nla_put(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
1105 err
= nla_put(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
1107 err
= nla_put(skb
, XFRMA_SPD_IPV4_HTHRESH
, sizeof(spt4
), &spt4
);
1109 err
= nla_put(skb
, XFRMA_SPD_IPV6_HTHRESH
, sizeof(spt6
), &spt6
);
1111 nlmsg_cancel(skb
, nlh
);
1115 nlmsg_end(skb
, nlh
);
1119 static int xfrm_set_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1120 struct nlattr
**attrs
)
1122 struct net
*net
= sock_net(skb
->sk
);
1123 struct xfrmu_spdhthresh
*thresh4
= NULL
;
1124 struct xfrmu_spdhthresh
*thresh6
= NULL
;
1126 /* selector prefixlen thresholds to hash policies */
1127 if (attrs
[XFRMA_SPD_IPV4_HTHRESH
]) {
1128 struct nlattr
*rta
= attrs
[XFRMA_SPD_IPV4_HTHRESH
];
1130 if (nla_len(rta
) < sizeof(*thresh4
))
1132 thresh4
= nla_data(rta
);
1133 if (thresh4
->lbits
> 32 || thresh4
->rbits
> 32)
1136 if (attrs
[XFRMA_SPD_IPV6_HTHRESH
]) {
1137 struct nlattr
*rta
= attrs
[XFRMA_SPD_IPV6_HTHRESH
];
1139 if (nla_len(rta
) < sizeof(*thresh6
))
1141 thresh6
= nla_data(rta
);
1142 if (thresh6
->lbits
> 128 || thresh6
->rbits
> 128)
1146 if (thresh4
|| thresh6
) {
1147 write_seqlock(&net
->xfrm
.policy_hthresh
.lock
);
1149 net
->xfrm
.policy_hthresh
.lbits4
= thresh4
->lbits
;
1150 net
->xfrm
.policy_hthresh
.rbits4
= thresh4
->rbits
;
1153 net
->xfrm
.policy_hthresh
.lbits6
= thresh6
->lbits
;
1154 net
->xfrm
.policy_hthresh
.rbits6
= thresh6
->rbits
;
1156 write_sequnlock(&net
->xfrm
.policy_hthresh
.lock
);
1158 xfrm_policy_hash_rebuild(net
);
1164 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1165 struct nlattr
**attrs
)
1167 struct net
*net
= sock_net(skb
->sk
);
1168 struct sk_buff
*r_skb
;
1169 u32
*flags
= nlmsg_data(nlh
);
1170 u32 sportid
= NETLINK_CB(skb
).portid
;
1171 u32 seq
= nlh
->nlmsg_seq
;
1173 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
1177 if (build_spdinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1180 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1183 static inline size_t xfrm_sadinfo_msgsize(void)
1185 return NLMSG_ALIGN(4)
1186 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
1187 + nla_total_size(4); /* XFRMA_SAD_CNT */
1190 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
1191 u32 portid
, u32 seq
, u32 flags
)
1193 struct xfrmk_sadinfo si
;
1194 struct xfrmu_sadhinfo sh
;
1195 struct nlmsghdr
*nlh
;
1199 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
1200 if (nlh
== NULL
) /* shouldn't really happen ... */
1203 f
= nlmsg_data(nlh
);
1205 xfrm_sad_getinfo(net
, &si
);
1207 sh
.sadhmcnt
= si
.sadhmcnt
;
1208 sh
.sadhcnt
= si
.sadhcnt
;
1210 err
= nla_put_u32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
1212 err
= nla_put(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
1214 nlmsg_cancel(skb
, nlh
);
1218 nlmsg_end(skb
, nlh
);
1222 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1223 struct nlattr
**attrs
)
1225 struct net
*net
= sock_net(skb
->sk
);
1226 struct sk_buff
*r_skb
;
1227 u32
*flags
= nlmsg_data(nlh
);
1228 u32 sportid
= NETLINK_CB(skb
).portid
;
1229 u32 seq
= nlh
->nlmsg_seq
;
1231 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
1235 if (build_sadinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1238 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1241 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1242 struct nlattr
**attrs
)
1244 struct net
*net
= sock_net(skb
->sk
);
1245 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
1246 struct xfrm_state
*x
;
1247 struct sk_buff
*resp_skb
;
1250 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
1254 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1255 if (IS_ERR(resp_skb
)) {
1256 err
= PTR_ERR(resp_skb
);
1258 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1265 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1266 struct nlattr
**attrs
)
1268 struct net
*net
= sock_net(skb
->sk
);
1269 struct xfrm_state
*x
;
1270 struct xfrm_userspi_info
*p
;
1271 struct sk_buff
*resp_skb
;
1272 xfrm_address_t
*daddr
;
1278 p
= nlmsg_data(nlh
);
1279 err
= verify_spi_info(p
->info
.id
.proto
, p
->min
, p
->max
);
1283 family
= p
->info
.family
;
1284 daddr
= &p
->info
.id
.daddr
;
1288 mark
= xfrm_mark_get(attrs
, &m
);
1290 x
= xfrm_find_acq_byseq(net
, mark
, p
->info
.seq
);
1291 if (x
&& !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
)) {
1298 x
= xfrm_find_acq(net
, &m
, p
->info
.mode
, p
->info
.reqid
,
1299 p
->info
.id
.proto
, daddr
,
1306 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
1310 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1311 if (IS_ERR(resp_skb
)) {
1312 err
= PTR_ERR(resp_skb
);
1316 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1324 static int verify_policy_dir(u8 dir
)
1327 case XFRM_POLICY_IN
:
1328 case XFRM_POLICY_OUT
:
1329 case XFRM_POLICY_FWD
:
1339 static int verify_policy_type(u8 type
)
1342 case XFRM_POLICY_TYPE_MAIN
:
1343 #ifdef CONFIG_XFRM_SUB_POLICY
1344 case XFRM_POLICY_TYPE_SUB
:
1355 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1360 case XFRM_SHARE_ANY
:
1361 case XFRM_SHARE_SESSION
:
1362 case XFRM_SHARE_USER
:
1363 case XFRM_SHARE_UNIQUE
:
1370 switch (p
->action
) {
1371 case XFRM_POLICY_ALLOW
:
1372 case XFRM_POLICY_BLOCK
:
1379 switch (p
->sel
.family
) {
1381 if (p
->sel
.prefixlen_d
> 32 || p
->sel
.prefixlen_s
> 32)
1387 #if IS_ENABLED(CONFIG_IPV6)
1388 if (p
->sel
.prefixlen_d
> 128 || p
->sel
.prefixlen_s
> 128)
1393 return -EAFNOSUPPORT
;
1400 ret
= verify_policy_dir(p
->dir
);
1403 if (p
->index
&& (xfrm_policy_id2dir(p
->index
) != p
->dir
))
1409 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1411 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1412 struct xfrm_user_sec_ctx
*uctx
;
1417 uctx
= nla_data(rt
);
1418 return security_xfrm_policy_alloc(&pol
->security
, uctx
, GFP_KERNEL
);
1421 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1427 for (i
= 0; i
< nr
; i
++, ut
++) {
1428 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1430 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1431 memcpy(&t
->saddr
, &ut
->saddr
,
1432 sizeof(xfrm_address_t
));
1433 t
->reqid
= ut
->reqid
;
1435 t
->share
= ut
->share
;
1436 t
->optional
= ut
->optional
;
1437 t
->aalgos
= ut
->aalgos
;
1438 t
->ealgos
= ut
->ealgos
;
1439 t
->calgos
= ut
->calgos
;
1440 /* If all masks are ~0, then we allow all algorithms. */
1441 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1442 t
->encap_family
= ut
->family
;
1446 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1451 if (nr
> XFRM_MAX_DEPTH
)
1454 prev_family
= family
;
1456 for (i
= 0; i
< nr
; i
++) {
1457 /* We never validated the ut->family value, so many
1458 * applications simply leave it at zero. The check was
1459 * never made and ut->family was ignored because all
1460 * templates could be assumed to have the same family as
1461 * the policy itself. Now that we will have ipv4-in-ipv6
1462 * and ipv6-in-ipv4 tunnels, this is no longer true.
1465 ut
[i
].family
= family
;
1467 switch (ut
[i
].mode
) {
1468 case XFRM_MODE_TUNNEL
:
1469 case XFRM_MODE_BEET
:
1472 if (ut
[i
].family
!= prev_family
)
1476 if (ut
[i
].mode
>= XFRM_MODE_MAX
)
1479 prev_family
= ut
[i
].family
;
1481 switch (ut
[i
].family
) {
1484 #if IS_ENABLED(CONFIG_IPV6)
1492 if (!xfrm_id_proto_valid(ut
[i
].id
.proto
))
1499 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1501 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1506 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1507 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1510 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1514 copy_templates(pol
, utmpl
, nr
);
1519 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1521 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1522 struct xfrm_userpolicy_type
*upt
;
1523 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1531 err
= verify_policy_type(type
);
1539 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1541 xp
->priority
= p
->priority
;
1542 xp
->index
= p
->index
;
1543 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1544 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1545 xp
->action
= p
->action
;
1546 xp
->flags
= p
->flags
;
1547 xp
->family
= p
->sel
.family
;
1548 /* XXX xp->share = p->share; */
1551 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1553 memset(p
, 0, sizeof(*p
));
1554 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1555 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1556 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1557 p
->priority
= xp
->priority
;
1558 p
->index
= xp
->index
;
1559 p
->sel
.family
= xp
->family
;
1561 p
->action
= xp
->action
;
1562 p
->flags
= xp
->flags
;
1563 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1566 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1568 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1576 copy_from_user_policy(xp
, p
);
1578 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1582 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1583 err
= copy_from_user_sec_ctx(xp
, attrs
);
1587 xfrm_mark_get(attrs
, &xp
->mark
);
1593 xfrm_policy_destroy(xp
);
1597 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1598 struct nlattr
**attrs
)
1600 struct net
*net
= sock_net(skb
->sk
);
1601 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1602 struct xfrm_policy
*xp
;
1607 err
= verify_newpolicy_info(p
);
1610 err
= verify_sec_ctx_len(attrs
);
1614 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1618 /* shouldn't excl be based on nlh flags??
1619 * Aha! this is anti-netlink really i.e more pfkey derived
1620 * in netlink excl is a flag and you wouldnt need
1621 * a type XFRM_MSG_UPDPOLICY - JHS */
1622 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1623 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1624 xfrm_audit_policy_add(xp
, err
? 0 : 1, true);
1627 security_xfrm_policy_free(xp
->security
);
1632 c
.event
= nlh
->nlmsg_type
;
1633 c
.seq
= nlh
->nlmsg_seq
;
1634 c
.portid
= nlh
->nlmsg_pid
;
1635 km_policy_notify(xp
, p
->dir
, &c
);
1642 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1644 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1647 if (xp
->xfrm_nr
== 0)
1650 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1651 struct xfrm_user_tmpl
*up
= &vec
[i
];
1652 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1654 memset(up
, 0, sizeof(*up
));
1655 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1656 up
->family
= kp
->encap_family
;
1657 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1658 up
->reqid
= kp
->reqid
;
1659 up
->mode
= kp
->mode
;
1660 up
->share
= kp
->share
;
1661 up
->optional
= kp
->optional
;
1662 up
->aalgos
= kp
->aalgos
;
1663 up
->ealgos
= kp
->ealgos
;
1664 up
->calgos
= kp
->calgos
;
1667 return nla_put(skb
, XFRMA_TMPL
,
1668 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1671 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1674 return copy_sec_ctx(x
->security
, skb
);
1679 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1682 return copy_sec_ctx(xp
->security
, skb
);
1685 static inline size_t userpolicy_type_attrsize(void)
1687 #ifdef CONFIG_XFRM_SUB_POLICY
1688 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1694 #ifdef CONFIG_XFRM_SUB_POLICY
1695 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1697 struct xfrm_userpolicy_type upt
;
1699 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1700 memset(&upt
, 0, sizeof(upt
));
1703 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1707 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1713 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1715 struct xfrm_dump_info
*sp
= ptr
;
1716 struct xfrm_userpolicy_info
*p
;
1717 struct sk_buff
*in_skb
= sp
->in_skb
;
1718 struct sk_buff
*skb
= sp
->out_skb
;
1719 struct nlmsghdr
*nlh
;
1722 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
1723 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1727 p
= nlmsg_data(nlh
);
1728 copy_to_user_policy(xp
, p
, dir
);
1729 err
= copy_to_user_tmpl(xp
, skb
);
1731 err
= copy_to_user_sec_ctx(xp
, skb
);
1733 err
= copy_to_user_policy_type(xp
->type
, skb
);
1735 err
= xfrm_mark_put(skb
, &xp
->mark
);
1737 nlmsg_cancel(skb
, nlh
);
1740 nlmsg_end(skb
, nlh
);
1744 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1746 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*)cb
->args
;
1747 struct net
*net
= sock_net(cb
->skb
->sk
);
1749 xfrm_policy_walk_done(walk
, net
);
1753 static int xfrm_dump_policy_start(struct netlink_callback
*cb
)
1755 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*)cb
->args
;
1757 BUILD_BUG_ON(sizeof(*walk
) > sizeof(cb
->args
));
1759 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1763 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1765 struct net
*net
= sock_net(skb
->sk
);
1766 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*)cb
->args
;
1767 struct xfrm_dump_info info
;
1769 info
.in_skb
= cb
->skb
;
1771 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1772 info
.nlmsg_flags
= NLM_F_MULTI
;
1774 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1779 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1780 struct xfrm_policy
*xp
,
1783 struct xfrm_dump_info info
;
1784 struct sk_buff
*skb
;
1787 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1789 return ERR_PTR(-ENOMEM
);
1791 info
.in_skb
= in_skb
;
1793 info
.nlmsg_seq
= seq
;
1794 info
.nlmsg_flags
= 0;
1796 err
= dump_one_policy(xp
, dir
, 0, &info
);
1799 return ERR_PTR(err
);
1805 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1806 struct nlattr
**attrs
)
1808 struct net
*net
= sock_net(skb
->sk
);
1809 struct xfrm_policy
*xp
;
1810 struct xfrm_userpolicy_id
*p
;
1811 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1816 u32 mark
= xfrm_mark_get(attrs
, &m
);
1818 p
= nlmsg_data(nlh
);
1819 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1821 err
= copy_from_user_policy_type(&type
, attrs
);
1825 err
= verify_policy_dir(p
->dir
);
1830 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, delete, &err
);
1832 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1833 struct xfrm_sec_ctx
*ctx
;
1835 err
= verify_sec_ctx_len(attrs
);
1841 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1843 err
= security_xfrm_policy_alloc(&ctx
, uctx
, GFP_KERNEL
);
1847 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
, &p
->sel
,
1849 security_xfrm_policy_free(ctx
);
1855 struct sk_buff
*resp_skb
;
1857 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1858 if (IS_ERR(resp_skb
)) {
1859 err
= PTR_ERR(resp_skb
);
1861 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1862 NETLINK_CB(skb
).portid
);
1865 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
1870 c
.data
.byid
= p
->index
;
1871 c
.event
= nlh
->nlmsg_type
;
1872 c
.seq
= nlh
->nlmsg_seq
;
1873 c
.portid
= nlh
->nlmsg_pid
;
1874 km_policy_notify(xp
, p
->dir
, &c
);
1882 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1883 struct nlattr
**attrs
)
1885 struct net
*net
= sock_net(skb
->sk
);
1887 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1890 err
= xfrm_state_flush(net
, p
->proto
, true);
1892 if (err
== -ESRCH
) /* empty table */
1896 c
.data
.proto
= p
->proto
;
1897 c
.event
= nlh
->nlmsg_type
;
1898 c
.seq
= nlh
->nlmsg_seq
;
1899 c
.portid
= nlh
->nlmsg_pid
;
1901 km_state_notify(NULL
, &c
);
1906 static inline size_t xfrm_aevent_msgsize(struct xfrm_state
*x
)
1908 size_t replay_size
= x
->replay_esn
?
1909 xfrm_replay_state_esn_len(x
->replay_esn
) :
1910 sizeof(struct xfrm_replay_state
);
1912 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1913 + nla_total_size(replay_size
)
1914 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur
))
1915 + nla_total_size(sizeof(struct xfrm_mark
))
1916 + nla_total_size(4) /* XFRM_AE_RTHR */
1917 + nla_total_size(4); /* XFRM_AE_ETHR */
1920 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
1922 struct xfrm_aevent_id
*id
;
1923 struct nlmsghdr
*nlh
;
1926 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1930 id
= nlmsg_data(nlh
);
1931 memset(&id
->sa_id
, 0, sizeof(id
->sa_id
));
1932 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
, sizeof(x
->id
.daddr
));
1933 id
->sa_id
.spi
= x
->id
.spi
;
1934 id
->sa_id
.family
= x
->props
.family
;
1935 id
->sa_id
.proto
= x
->id
.proto
;
1936 memcpy(&id
->saddr
, &x
->props
.saddr
, sizeof(x
->props
.saddr
));
1937 id
->reqid
= x
->props
.reqid
;
1938 id
->flags
= c
->data
.aevent
;
1940 if (x
->replay_esn
) {
1941 err
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
1942 xfrm_replay_state_esn_len(x
->replay_esn
),
1945 err
= nla_put(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
),
1950 err
= nla_put_64bit(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
,
1955 if (id
->flags
& XFRM_AE_RTHR
) {
1956 err
= nla_put_u32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1960 if (id
->flags
& XFRM_AE_ETHR
) {
1961 err
= nla_put_u32(skb
, XFRMA_ETIMER_THRESH
,
1962 x
->replay_maxage
* 10 / HZ
);
1966 err
= xfrm_mark_put(skb
, &x
->mark
);
1970 nlmsg_end(skb
, nlh
);
1974 nlmsg_cancel(skb
, nlh
);
1978 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1979 struct nlattr
**attrs
)
1981 struct net
*net
= sock_net(skb
->sk
);
1982 struct xfrm_state
*x
;
1983 struct sk_buff
*r_skb
;
1988 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1989 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1991 mark
= xfrm_mark_get(attrs
, &m
);
1993 x
= xfrm_state_lookup(net
, mark
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1997 r_skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
1998 if (r_skb
== NULL
) {
2004 * XXX: is this lock really needed - none of the other
2005 * gets lock (the concern is things getting updated
2006 * while we are still reading) - jhs
2008 spin_lock_bh(&x
->lock
);
2009 c
.data
.aevent
= p
->flags
;
2010 c
.seq
= nlh
->nlmsg_seq
;
2011 c
.portid
= nlh
->nlmsg_pid
;
2013 if (build_aevent(r_skb
, x
, &c
) < 0)
2015 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).portid
);
2016 spin_unlock_bh(&x
->lock
);
2021 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2022 struct nlattr
**attrs
)
2024 struct net
*net
= sock_net(skb
->sk
);
2025 struct xfrm_state
*x
;
2030 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
2031 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
2032 struct nlattr
*re
= attrs
[XFRMA_REPLAY_ESN_VAL
];
2033 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
2034 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
2035 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
2037 if (!lt
&& !rp
&& !re
&& !et
&& !rt
)
2040 /* pedantic mode - thou shalt sayeth replaceth */
2041 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
2044 mark
= xfrm_mark_get(attrs
, &m
);
2046 x
= xfrm_state_lookup(net
, mark
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
2050 if (x
->km
.state
!= XFRM_STATE_VALID
)
2053 err
= xfrm_replay_verify_len(x
->replay_esn
, re
);
2057 spin_lock_bh(&x
->lock
);
2058 xfrm_update_ae_params(x
, attrs
, 1);
2059 spin_unlock_bh(&x
->lock
);
2061 c
.event
= nlh
->nlmsg_type
;
2062 c
.seq
= nlh
->nlmsg_seq
;
2063 c
.portid
= nlh
->nlmsg_pid
;
2064 c
.data
.aevent
= XFRM_AE_CU
;
2065 km_state_notify(x
, &c
);
2072 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2073 struct nlattr
**attrs
)
2075 struct net
*net
= sock_net(skb
->sk
);
2077 u8 type
= XFRM_POLICY_TYPE_MAIN
;
2080 err
= copy_from_user_policy_type(&type
, attrs
);
2084 err
= xfrm_policy_flush(net
, type
, true);
2086 if (err
== -ESRCH
) /* empty table */
2092 c
.event
= nlh
->nlmsg_type
;
2093 c
.seq
= nlh
->nlmsg_seq
;
2094 c
.portid
= nlh
->nlmsg_pid
;
2096 km_policy_notify(NULL
, 0, &c
);
2100 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2101 struct nlattr
**attrs
)
2103 struct net
*net
= sock_net(skb
->sk
);
2104 struct xfrm_policy
*xp
;
2105 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
2106 struct xfrm_userpolicy_info
*p
= &up
->pol
;
2107 u8 type
= XFRM_POLICY_TYPE_MAIN
;
2110 u32 mark
= xfrm_mark_get(attrs
, &m
);
2112 err
= copy_from_user_policy_type(&type
, attrs
);
2116 err
= verify_policy_dir(p
->dir
);
2121 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, 0, &err
);
2123 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
2124 struct xfrm_sec_ctx
*ctx
;
2126 err
= verify_sec_ctx_len(attrs
);
2132 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
2134 err
= security_xfrm_policy_alloc(&ctx
, uctx
, GFP_KERNEL
);
2138 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
,
2139 &p
->sel
, ctx
, 0, &err
);
2140 security_xfrm_policy_free(ctx
);
2145 if (unlikely(xp
->walk
.dead
))
2150 xfrm_policy_delete(xp
, p
->dir
);
2151 xfrm_audit_policy_delete(xp
, 1, true);
2153 km_policy_expired(xp
, p
->dir
, up
->hard
, nlh
->nlmsg_pid
);
2160 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2161 struct nlattr
**attrs
)
2163 struct net
*net
= sock_net(skb
->sk
);
2164 struct xfrm_state
*x
;
2166 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
2167 struct xfrm_usersa_info
*p
= &ue
->state
;
2169 u32 mark
= xfrm_mark_get(attrs
, &m
);
2171 x
= xfrm_state_lookup(net
, mark
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
2177 spin_lock_bh(&x
->lock
);
2179 if (x
->km
.state
!= XFRM_STATE_VALID
)
2181 km_state_expired(x
, ue
->hard
, nlh
->nlmsg_pid
);
2184 __xfrm_state_delete(x
);
2185 xfrm_audit_state_delete(x
, 1, true);
2189 spin_unlock_bh(&x
->lock
);
2194 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2195 struct nlattr
**attrs
)
2197 struct net
*net
= sock_net(skb
->sk
);
2198 struct xfrm_policy
*xp
;
2199 struct xfrm_user_tmpl
*ut
;
2201 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
2202 struct xfrm_mark mark
;
2204 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
2205 struct xfrm_state
*x
= xfrm_state_alloc(net
);
2211 xfrm_mark_get(attrs
, &mark
);
2213 err
= verify_newpolicy_info(&ua
->policy
);
2218 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
2222 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
2223 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
2224 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
2225 xp
->mark
.m
= x
->mark
.m
= mark
.m
;
2226 xp
->mark
.v
= x
->mark
.v
= mark
.v
;
2228 /* extract the templates and for each call km_key */
2229 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
2230 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
2231 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
2232 x
->props
.mode
= t
->mode
;
2233 x
->props
.reqid
= t
->reqid
;
2234 x
->props
.family
= ut
->family
;
2235 t
->aalgos
= ua
->aalgos
;
2236 t
->ealgos
= ua
->ealgos
;
2237 t
->calgos
= ua
->calgos
;
2238 err
= km_query(x
, t
, xp
);
2253 #ifdef CONFIG_XFRM_MIGRATE
2254 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
2255 struct xfrm_kmaddress
*k
,
2256 struct nlattr
**attrs
, int *num
)
2258 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
2259 struct xfrm_user_migrate
*um
;
2263 struct xfrm_user_kmaddress
*uk
;
2265 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
2266 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
2267 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
2268 k
->family
= uk
->family
;
2269 k
->reserved
= uk
->reserved
;
2273 num_migrate
= nla_len(rt
) / sizeof(*um
);
2275 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
2278 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
2279 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
2280 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
2281 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
2282 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
2284 ma
->proto
= um
->proto
;
2285 ma
->mode
= um
->mode
;
2286 ma
->reqid
= um
->reqid
;
2288 ma
->old_family
= um
->old_family
;
2289 ma
->new_family
= um
->new_family
;
2296 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2297 struct nlattr
**attrs
)
2299 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
2300 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2301 struct xfrm_kmaddress km
, *kmp
;
2305 struct net
*net
= sock_net(skb
->sk
);
2306 struct xfrm_encap_tmpl
*encap
= NULL
;
2308 if (attrs
[XFRMA_MIGRATE
] == NULL
)
2311 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
2313 err
= copy_from_user_policy_type(&type
, attrs
);
2317 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
2324 if (attrs
[XFRMA_ENCAP
]) {
2325 encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
2326 sizeof(*encap
), GFP_KERNEL
);
2331 err
= xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
, net
, encap
);
2338 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2339 struct nlattr
**attrs
)
2341 return -ENOPROTOOPT
;
2345 #ifdef CONFIG_XFRM_MIGRATE
2346 static int copy_to_user_migrate(const struct xfrm_migrate
*m
, struct sk_buff
*skb
)
2348 struct xfrm_user_migrate um
;
2350 memset(&um
, 0, sizeof(um
));
2351 um
.proto
= m
->proto
;
2353 um
.reqid
= m
->reqid
;
2354 um
.old_family
= m
->old_family
;
2355 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
2356 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
2357 um
.new_family
= m
->new_family
;
2358 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
2359 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
2361 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
2364 static int copy_to_user_kmaddress(const struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
2366 struct xfrm_user_kmaddress uk
;
2368 memset(&uk
, 0, sizeof(uk
));
2369 uk
.family
= k
->family
;
2370 uk
.reserved
= k
->reserved
;
2371 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
2372 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
2374 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
2377 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
,
2380 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
2381 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
2382 + (with_encp
? nla_total_size(sizeof(struct xfrm_encap_tmpl
)) : 0)
2383 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
2384 + userpolicy_type_attrsize();
2387 static int build_migrate(struct sk_buff
*skb
, const struct xfrm_migrate
*m
,
2388 int num_migrate
, const struct xfrm_kmaddress
*k
,
2389 const struct xfrm_selector
*sel
,
2390 const struct xfrm_encap_tmpl
*encap
, u8 dir
, u8 type
)
2392 const struct xfrm_migrate
*mp
;
2393 struct xfrm_userpolicy_id
*pol_id
;
2394 struct nlmsghdr
*nlh
;
2397 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
2401 pol_id
= nlmsg_data(nlh
);
2402 /* copy data from selector, dir, and type to the pol_id */
2403 memset(pol_id
, 0, sizeof(*pol_id
));
2404 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
2408 err
= copy_to_user_kmaddress(k
, skb
);
2413 err
= nla_put(skb
, XFRMA_ENCAP
, sizeof(*encap
), encap
);
2417 err
= copy_to_user_policy_type(type
, skb
);
2420 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2421 err
= copy_to_user_migrate(mp
, skb
);
2426 nlmsg_end(skb
, nlh
);
2430 nlmsg_cancel(skb
, nlh
);
2434 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2435 const struct xfrm_migrate
*m
, int num_migrate
,
2436 const struct xfrm_kmaddress
*k
,
2437 const struct xfrm_encap_tmpl
*encap
)
2439 struct net
*net
= &init_net
;
2440 struct sk_buff
*skb
;
2442 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
, !!encap
),
2448 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, encap
, dir
, type
) < 0)
2451 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_MIGRATE
);
2454 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2455 const struct xfrm_migrate
*m
, int num_migrate
,
2456 const struct xfrm_kmaddress
*k
,
2457 const struct xfrm_encap_tmpl
*encap
)
2459 return -ENOPROTOOPT
;
2463 #define XMSGSIZE(type) sizeof(struct type)
2465 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2466 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2467 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2468 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2469 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2470 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2471 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2472 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2473 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2474 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2475 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2476 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2477 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2478 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2479 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2480 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2481 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2482 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2483 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2484 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2485 [XFRM_MSG_NEWSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2486 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2491 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2492 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2493 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2494 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2495 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2496 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2497 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2498 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2499 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2500 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2501 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2502 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2503 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2504 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2505 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2506 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2507 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2508 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2509 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2510 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2511 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2512 [XFRMA_MARK
] = { .len
= sizeof(struct xfrm_mark
) },
2513 [XFRMA_TFCPAD
] = { .type
= NLA_U32
},
2514 [XFRMA_REPLAY_ESN_VAL
] = { .len
= sizeof(struct xfrm_replay_state_esn
) },
2515 [XFRMA_SA_EXTRA_FLAGS
] = { .type
= NLA_U32
},
2516 [XFRMA_PROTO
] = { .type
= NLA_U8
},
2517 [XFRMA_ADDRESS_FILTER
] = { .len
= sizeof(struct xfrm_address_filter
) },
2518 [XFRMA_OFFLOAD_DEV
] = { .len
= sizeof(struct xfrm_user_offload
) },
2519 [XFRMA_OUTPUT_MARK
] = { .len
= NLA_U32
},
2522 static const struct nla_policy xfrma_spd_policy
[XFRMA_SPD_MAX
+1] = {
2523 [XFRMA_SPD_IPV4_HTHRESH
] = { .len
= sizeof(struct xfrmu_spdhthresh
) },
2524 [XFRMA_SPD_IPV6_HTHRESH
] = { .len
= sizeof(struct xfrmu_spdhthresh
) },
2527 static const struct xfrm_link
{
2528 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2529 int (*start
)(struct netlink_callback
*);
2530 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2531 int (*done
)(struct netlink_callback
*);
2532 const struct nla_policy
*nla_pol
;
2534 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2535 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2536 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2537 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2538 .dump
= xfrm_dump_sa
,
2539 .done
= xfrm_dump_sa_done
},
2540 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2541 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2542 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2543 .start
= xfrm_dump_policy_start
,
2544 .dump
= xfrm_dump_policy
,
2545 .done
= xfrm_dump_policy_done
},
2546 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2547 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2548 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2549 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2550 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2551 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2552 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2553 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2554 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2555 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2556 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2557 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2558 [XFRM_MSG_NEWSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_set_spdinfo
,
2559 .nla_pol
= xfrma_spd_policy
,
2560 .nla_max
= XFRMA_SPD_MAX
},
2561 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2564 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2565 struct netlink_ext_ack
*extack
)
2567 struct net
*net
= sock_net(skb
->sk
);
2568 struct nlattr
*attrs
[XFRMA_MAX
+1];
2569 const struct xfrm_link
*link
;
2572 #ifdef CONFIG_COMPAT
2573 if (in_compat_syscall())
2577 type
= nlh
->nlmsg_type
;
2578 if (type
> XFRM_MSG_MAX
)
2581 type
-= XFRM_MSG_BASE
;
2582 link
= &xfrm_dispatch
[type
];
2584 /* All operations require privileges, even GET */
2585 if (!netlink_net_capable(skb
, CAP_NET_ADMIN
))
2588 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2589 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2590 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2591 if (link
->dump
== NULL
)
2595 struct netlink_dump_control c
= {
2596 .start
= link
->start
,
2600 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, &c
);
2604 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
,
2605 link
->nla_max
? : XFRMA_MAX
,
2606 link
->nla_pol
? : xfrma_policy
, extack
);
2610 if (link
->doit
== NULL
)
2613 return link
->doit(skb
, nlh
, attrs
);
2616 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2618 struct net
*net
= sock_net(skb
->sk
);
2620 mutex_lock(&net
->xfrm
.xfrm_cfg_mutex
);
2621 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2622 mutex_unlock(&net
->xfrm
.xfrm_cfg_mutex
);
2625 static inline size_t xfrm_expire_msgsize(void)
2627 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
))
2628 + nla_total_size(sizeof(struct xfrm_mark
));
2631 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
2633 struct xfrm_user_expire
*ue
;
2634 struct nlmsghdr
*nlh
;
2637 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2641 ue
= nlmsg_data(nlh
);
2642 copy_to_user_state(x
, &ue
->state
);
2643 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2644 /* clear the padding bytes */
2645 memset(&ue
->hard
+ 1, 0, sizeof(*ue
) - offsetofend(typeof(*ue
), hard
));
2647 err
= xfrm_mark_put(skb
, &x
->mark
);
2651 nlmsg_end(skb
, nlh
);
2655 static int xfrm_exp_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2657 struct net
*net
= xs_net(x
);
2658 struct sk_buff
*skb
;
2660 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2664 if (build_expire(skb
, x
, c
) < 0) {
2669 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_EXPIRE
);
2672 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2674 struct net
*net
= xs_net(x
);
2675 struct sk_buff
*skb
;
2677 skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
2681 if (build_aevent(skb
, x
, c
) < 0)
2684 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_AEVENTS
);
2687 static int xfrm_notify_sa_flush(const struct km_event
*c
)
2689 struct net
*net
= c
->net
;
2690 struct xfrm_usersa_flush
*p
;
2691 struct nlmsghdr
*nlh
;
2692 struct sk_buff
*skb
;
2693 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2695 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2699 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2705 p
= nlmsg_data(nlh
);
2706 p
->proto
= c
->data
.proto
;
2708 nlmsg_end(skb
, nlh
);
2710 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_SA
);
2713 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2717 l
+= nla_total_size(aead_len(x
->aead
));
2719 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2720 (x
->aalg
->alg_key_len
+ 7) / 8);
2721 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2724 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2726 l
+= nla_total_size(sizeof(*x
->calg
));
2728 l
+= nla_total_size(sizeof(*x
->encap
));
2730 l
+= nla_total_size(sizeof(x
->tfcpad
));
2732 l
+= nla_total_size(xfrm_replay_state_esn_len(x
->replay_esn
));
2734 l
+= nla_total_size(sizeof(struct xfrm_replay_state
));
2736 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2737 x
->security
->ctx_len
);
2739 l
+= nla_total_size(sizeof(*x
->coaddr
));
2740 if (x
->props
.extra_flags
)
2741 l
+= nla_total_size(sizeof(x
->props
.extra_flags
));
2743 l
+= nla_total_size(sizeof(x
->xso
));
2744 if (x
->props
.output_mark
)
2745 l
+= nla_total_size(sizeof(x
->props
.output_mark
));
2747 /* Must count x->lastused as it may become non-zero behind our back. */
2748 l
+= nla_total_size_64bit(sizeof(u64
));
2753 static int xfrm_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
2755 struct net
*net
= xs_net(x
);
2756 struct xfrm_usersa_info
*p
;
2757 struct xfrm_usersa_id
*id
;
2758 struct nlmsghdr
*nlh
;
2759 struct sk_buff
*skb
;
2760 int len
= xfrm_sa_len(x
);
2763 headlen
= sizeof(*p
);
2764 if (c
->event
== XFRM_MSG_DELSA
) {
2765 len
+= nla_total_size(headlen
);
2766 headlen
= sizeof(*id
);
2767 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2769 len
+= NLMSG_ALIGN(headlen
);
2771 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2775 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2780 p
= nlmsg_data(nlh
);
2781 if (c
->event
== XFRM_MSG_DELSA
) {
2782 struct nlattr
*attr
;
2784 id
= nlmsg_data(nlh
);
2785 memset(id
, 0, sizeof(*id
));
2786 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2787 id
->spi
= x
->id
.spi
;
2788 id
->family
= x
->props
.family
;
2789 id
->proto
= x
->id
.proto
;
2791 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2798 err
= copy_to_user_state_extra(x
, p
, skb
);
2802 nlmsg_end(skb
, nlh
);
2804 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_SA
);
2811 static int xfrm_send_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2815 case XFRM_MSG_EXPIRE
:
2816 return xfrm_exp_state_notify(x
, c
);
2817 case XFRM_MSG_NEWAE
:
2818 return xfrm_aevent_state_notify(x
, c
);
2819 case XFRM_MSG_DELSA
:
2820 case XFRM_MSG_UPDSA
:
2821 case XFRM_MSG_NEWSA
:
2822 return xfrm_notify_sa(x
, c
);
2823 case XFRM_MSG_FLUSHSA
:
2824 return xfrm_notify_sa_flush(c
);
2826 printk(KERN_NOTICE
"xfrm_user: Unknown SA event %d\n",
2835 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2836 struct xfrm_policy
*xp
)
2838 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2839 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2840 + nla_total_size(sizeof(struct xfrm_mark
))
2841 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2842 + userpolicy_type_attrsize();
2845 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2846 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
)
2848 __u32 seq
= xfrm_get_acqseq();
2849 struct xfrm_user_acquire
*ua
;
2850 struct nlmsghdr
*nlh
;
2853 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2857 ua
= nlmsg_data(nlh
);
2858 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2859 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2860 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2861 copy_to_user_policy(xp
, &ua
->policy
, XFRM_POLICY_OUT
);
2862 ua
->aalgos
= xt
->aalgos
;
2863 ua
->ealgos
= xt
->ealgos
;
2864 ua
->calgos
= xt
->calgos
;
2865 ua
->seq
= x
->km
.seq
= seq
;
2867 err
= copy_to_user_tmpl(xp
, skb
);
2869 err
= copy_to_user_state_sec_ctx(x
, skb
);
2871 err
= copy_to_user_policy_type(xp
->type
, skb
);
2873 err
= xfrm_mark_put(skb
, &xp
->mark
);
2875 nlmsg_cancel(skb
, nlh
);
2879 nlmsg_end(skb
, nlh
);
2883 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2884 struct xfrm_policy
*xp
)
2886 struct net
*net
= xs_net(x
);
2887 struct sk_buff
*skb
;
2889 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2893 if (build_acquire(skb
, x
, xt
, xp
) < 0)
2896 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_ACQUIRE
);
2899 /* User gives us xfrm_user_policy_info followed by an array of 0
2900 * or more templates.
2902 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2903 u8
*data
, int len
, int *dir
)
2905 struct net
*net
= sock_net(sk
);
2906 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2907 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2908 struct xfrm_policy
*xp
;
2911 switch (sk
->sk_family
) {
2913 if (opt
!= IP_XFRM_POLICY
) {
2918 #if IS_ENABLED(CONFIG_IPV6)
2920 if (opt
!= IPV6_XFRM_POLICY
) {
2933 if (len
< sizeof(*p
) ||
2934 verify_newpolicy_info(p
))
2937 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2938 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2941 if (p
->dir
> XFRM_POLICY_OUT
)
2944 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
2950 copy_from_user_policy(xp
, p
);
2951 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2952 copy_templates(xp
, ut
, nr
);
2959 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2961 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2962 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2963 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2964 + nla_total_size(sizeof(struct xfrm_mark
))
2965 + userpolicy_type_attrsize();
2968 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2969 int dir
, const struct km_event
*c
)
2971 struct xfrm_user_polexpire
*upe
;
2972 int hard
= c
->data
.hard
;
2973 struct nlmsghdr
*nlh
;
2976 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2980 upe
= nlmsg_data(nlh
);
2981 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2982 err
= copy_to_user_tmpl(xp
, skb
);
2984 err
= copy_to_user_sec_ctx(xp
, skb
);
2986 err
= copy_to_user_policy_type(xp
->type
, skb
);
2988 err
= xfrm_mark_put(skb
, &xp
->mark
);
2990 nlmsg_cancel(skb
, nlh
);
2995 nlmsg_end(skb
, nlh
);
2999 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
3001 struct net
*net
= xp_net(xp
);
3002 struct sk_buff
*skb
;
3004 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
3008 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
3011 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_EXPIRE
);
3014 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
3016 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
3017 struct net
*net
= xp_net(xp
);
3018 struct xfrm_userpolicy_info
*p
;
3019 struct xfrm_userpolicy_id
*id
;
3020 struct nlmsghdr
*nlh
;
3021 struct sk_buff
*skb
;
3024 headlen
= sizeof(*p
);
3025 if (c
->event
== XFRM_MSG_DELPOLICY
) {
3026 len
+= nla_total_size(headlen
);
3027 headlen
= sizeof(*id
);
3029 len
+= userpolicy_type_attrsize();
3030 len
+= nla_total_size(sizeof(struct xfrm_mark
));
3031 len
+= NLMSG_ALIGN(headlen
);
3033 skb
= nlmsg_new(len
, GFP_ATOMIC
);
3037 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
3042 p
= nlmsg_data(nlh
);
3043 if (c
->event
== XFRM_MSG_DELPOLICY
) {
3044 struct nlattr
*attr
;
3046 id
= nlmsg_data(nlh
);
3047 memset(id
, 0, sizeof(*id
));
3050 id
->index
= xp
->index
;
3052 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
3054 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
3062 copy_to_user_policy(xp
, p
, dir
);
3063 err
= copy_to_user_tmpl(xp
, skb
);
3065 err
= copy_to_user_policy_type(xp
->type
, skb
);
3067 err
= xfrm_mark_put(skb
, &xp
->mark
);
3071 nlmsg_end(skb
, nlh
);
3073 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_POLICY
);
3080 static int xfrm_notify_policy_flush(const struct km_event
*c
)
3082 struct net
*net
= c
->net
;
3083 struct nlmsghdr
*nlh
;
3084 struct sk_buff
*skb
;
3087 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
3091 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
3095 err
= copy_to_user_policy_type(c
->data
.type
, skb
);
3099 nlmsg_end(skb
, nlh
);
3101 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_POLICY
);
3108 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
3112 case XFRM_MSG_NEWPOLICY
:
3113 case XFRM_MSG_UPDPOLICY
:
3114 case XFRM_MSG_DELPOLICY
:
3115 return xfrm_notify_policy(xp
, dir
, c
);
3116 case XFRM_MSG_FLUSHPOLICY
:
3117 return xfrm_notify_policy_flush(c
);
3118 case XFRM_MSG_POLEXPIRE
:
3119 return xfrm_exp_policy_notify(xp
, dir
, c
);
3121 printk(KERN_NOTICE
"xfrm_user: Unknown Policy event %d\n",
3129 static inline size_t xfrm_report_msgsize(void)
3131 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
3134 static int build_report(struct sk_buff
*skb
, u8 proto
,
3135 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
3137 struct xfrm_user_report
*ur
;
3138 struct nlmsghdr
*nlh
;
3140 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
3144 ur
= nlmsg_data(nlh
);
3146 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
3149 int err
= nla_put(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
3151 nlmsg_cancel(skb
, nlh
);
3155 nlmsg_end(skb
, nlh
);
3159 static int xfrm_send_report(struct net
*net
, u8 proto
,
3160 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
3162 struct sk_buff
*skb
;
3164 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
3168 if (build_report(skb
, proto
, sel
, addr
) < 0)
3171 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_REPORT
);
3174 static inline size_t xfrm_mapping_msgsize(void)
3176 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
3179 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
3180 xfrm_address_t
*new_saddr
, __be16 new_sport
)
3182 struct xfrm_user_mapping
*um
;
3183 struct nlmsghdr
*nlh
;
3185 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
3189 um
= nlmsg_data(nlh
);
3191 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
3192 um
->id
.spi
= x
->id
.spi
;
3193 um
->id
.family
= x
->props
.family
;
3194 um
->id
.proto
= x
->id
.proto
;
3195 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
3196 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
3197 um
->new_sport
= new_sport
;
3198 um
->old_sport
= x
->encap
->encap_sport
;
3199 um
->reqid
= x
->props
.reqid
;
3201 nlmsg_end(skb
, nlh
);
3205 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
3208 struct net
*net
= xs_net(x
);
3209 struct sk_buff
*skb
;
3211 if (x
->id
.proto
!= IPPROTO_ESP
)
3217 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
3221 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
3224 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_MAPPING
);
3227 static bool xfrm_is_alive(const struct km_event
*c
)
3229 return (bool)xfrm_acquire_is_on(c
->net
);
3232 static struct xfrm_mgr netlink_mgr
= {
3233 .notify
= xfrm_send_state_notify
,
3234 .acquire
= xfrm_send_acquire
,
3235 .compile_policy
= xfrm_compile_policy
,
3236 .notify_policy
= xfrm_send_policy_notify
,
3237 .report
= xfrm_send_report
,
3238 .migrate
= xfrm_send_migrate
,
3239 .new_mapping
= xfrm_send_mapping
,
3240 .is_alive
= xfrm_is_alive
,
3243 static int __net_init
xfrm_user_net_init(struct net
*net
)
3246 struct netlink_kernel_cfg cfg
= {
3247 .groups
= XFRMNLGRP_MAX
,
3248 .input
= xfrm_netlink_rcv
,
3251 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, &cfg
);
3254 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
3255 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
3259 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
3262 list_for_each_entry(net
, net_exit_list
, exit_list
)
3263 RCU_INIT_POINTER(net
->xfrm
.nlsk
, NULL
);
3265 list_for_each_entry(net
, net_exit_list
, exit_list
)
3266 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
3269 static struct pernet_operations xfrm_user_net_ops
= {
3270 .init
= xfrm_user_net_init
,
3271 .exit_batch
= xfrm_user_net_exit
,
3274 static int __init
xfrm_user_init(void)
3278 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
3280 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
3283 rv
= xfrm_register_km(&netlink_mgr
);
3285 unregister_pernet_subsys(&xfrm_user_net_ops
);
3289 static void __exit
xfrm_user_exit(void)
3291 xfrm_unregister_km(&netlink_mgr
);
3292 unregister_pernet_subsys(&xfrm_user_net_ops
);
3295 module_init(xfrm_user_init
);
3296 module_exit(xfrm_user_exit
);
3297 MODULE_LICENSE("GPL");
3298 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);