1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/socket.h>
19 #include <linux/string.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
22 #include <linux/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
28 #include <net/netlink.h>
30 #include <asm/uaccess.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <linux/in6.h>
35 static int verify_one_alg(struct nlattr
**attrs
, enum xfrm_attr_type_t type
)
37 struct nlattr
*rt
= attrs
[type
];
38 struct xfrm_algo
*algp
;
44 if (nla_len(rt
) < xfrm_alg_len(algp
))
57 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
61 static int verify_auth_trunc(struct nlattr
**attrs
)
63 struct nlattr
*rt
= attrs
[XFRMA_ALG_AUTH_TRUNC
];
64 struct xfrm_algo_auth
*algp
;
70 if (nla_len(rt
) < xfrm_alg_auth_len(algp
))
73 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
77 static int verify_aead(struct nlattr
**attrs
)
79 struct nlattr
*rt
= attrs
[XFRMA_ALG_AEAD
];
80 struct xfrm_algo_aead
*algp
;
86 if (nla_len(rt
) < aead_len(algp
))
89 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
93 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
94 xfrm_address_t
**addrp
)
96 struct nlattr
*rt
= attrs
[type
];
99 *addrp
= nla_data(rt
);
102 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
104 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
105 struct xfrm_user_sec_ctx
*uctx
;
111 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
117 static inline int verify_replay(struct xfrm_usersa_info
*p
,
118 struct nlattr
**attrs
)
120 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_ESN_VAL
];
121 struct xfrm_replay_state_esn
*rs
;
123 if (p
->flags
& XFRM_STATE_ESN
) {
129 if (rs
->bmp_len
> XFRMA_REPLAY_ESN_MAX
/ sizeof(rs
->bmp
[0]) / 8)
132 if (nla_len(rt
) < xfrm_replay_state_esn_len(rs
) &&
133 nla_len(rt
) != sizeof(*rs
))
140 /* As only ESP and AH support ESN feature. */
141 if ((p
->id
.proto
!= IPPROTO_ESP
) && (p
->id
.proto
!= IPPROTO_AH
))
144 if (p
->replay_window
!= 0)
150 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
151 struct nlattr
**attrs
)
161 #if IS_ENABLED(CONFIG_IPV6)
173 switch (p
->id
.proto
) {
175 if ((!attrs
[XFRMA_ALG_AUTH
] &&
176 !attrs
[XFRMA_ALG_AUTH_TRUNC
]) ||
177 attrs
[XFRMA_ALG_AEAD
] ||
178 attrs
[XFRMA_ALG_CRYPT
] ||
179 attrs
[XFRMA_ALG_COMP
] ||
185 if (attrs
[XFRMA_ALG_COMP
])
187 if (!attrs
[XFRMA_ALG_AUTH
] &&
188 !attrs
[XFRMA_ALG_AUTH_TRUNC
] &&
189 !attrs
[XFRMA_ALG_CRYPT
] &&
190 !attrs
[XFRMA_ALG_AEAD
])
192 if ((attrs
[XFRMA_ALG_AUTH
] ||
193 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
194 attrs
[XFRMA_ALG_CRYPT
]) &&
195 attrs
[XFRMA_ALG_AEAD
])
197 if (attrs
[XFRMA_TFCPAD
] &&
198 p
->mode
!= XFRM_MODE_TUNNEL
)
203 if (!attrs
[XFRMA_ALG_COMP
] ||
204 attrs
[XFRMA_ALG_AEAD
] ||
205 attrs
[XFRMA_ALG_AUTH
] ||
206 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
207 attrs
[XFRMA_ALG_CRYPT
] ||
208 attrs
[XFRMA_TFCPAD
] ||
209 (ntohl(p
->id
.spi
) >= 0x10000))
213 #if IS_ENABLED(CONFIG_IPV6)
214 case IPPROTO_DSTOPTS
:
215 case IPPROTO_ROUTING
:
216 if (attrs
[XFRMA_ALG_COMP
] ||
217 attrs
[XFRMA_ALG_AUTH
] ||
218 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
219 attrs
[XFRMA_ALG_AEAD
] ||
220 attrs
[XFRMA_ALG_CRYPT
] ||
221 attrs
[XFRMA_ENCAP
] ||
222 attrs
[XFRMA_SEC_CTX
] ||
223 attrs
[XFRMA_TFCPAD
] ||
224 !attrs
[XFRMA_COADDR
])
233 if ((err
= verify_aead(attrs
)))
235 if ((err
= verify_auth_trunc(attrs
)))
237 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
239 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
241 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
243 if ((err
= verify_sec_ctx_len(attrs
)))
245 if ((err
= verify_replay(p
, attrs
)))
250 case XFRM_MODE_TRANSPORT
:
251 case XFRM_MODE_TUNNEL
:
252 case XFRM_MODE_ROUTEOPTIMIZATION
:
266 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
267 struct xfrm_algo_desc
*(*get_byname
)(const char *, int),
270 struct xfrm_algo
*p
, *ualg
;
271 struct xfrm_algo_desc
*algo
;
276 ualg
= nla_data(rta
);
278 algo
= get_byname(ualg
->alg_name
, 1);
281 *props
= algo
->desc
.sadb_alg_id
;
283 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
287 strcpy(p
->alg_name
, algo
->name
);
292 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
295 struct xfrm_algo
*ualg
;
296 struct xfrm_algo_auth
*p
;
297 struct xfrm_algo_desc
*algo
;
302 ualg
= nla_data(rta
);
304 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
307 *props
= algo
->desc
.sadb_alg_id
;
309 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
313 strcpy(p
->alg_name
, algo
->name
);
314 p
->alg_key_len
= ualg
->alg_key_len
;
315 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
316 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
322 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
325 struct xfrm_algo_auth
*p
, *ualg
;
326 struct xfrm_algo_desc
*algo
;
331 ualg
= nla_data(rta
);
333 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
336 if ((ualg
->alg_trunc_len
/ 8) > MAX_AH_AUTH_LEN
||
337 ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
339 *props
= algo
->desc
.sadb_alg_id
;
341 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
345 strcpy(p
->alg_name
, algo
->name
);
346 if (!p
->alg_trunc_len
)
347 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
353 static int attach_aead(struct xfrm_algo_aead
**algpp
, u8
*props
,
356 struct xfrm_algo_aead
*p
, *ualg
;
357 struct xfrm_algo_desc
*algo
;
362 ualg
= nla_data(rta
);
364 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
367 *props
= algo
->desc
.sadb_alg_id
;
369 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
373 strcpy(p
->alg_name
, algo
->name
);
378 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn
*replay_esn
,
381 struct xfrm_replay_state_esn
*up
;
384 if (!replay_esn
|| !rp
)
388 ulen
= xfrm_replay_state_esn_len(up
);
390 if (nla_len(rp
) < ulen
|| xfrm_replay_state_esn_len(replay_esn
) != ulen
)
396 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn
**replay_esn
,
397 struct xfrm_replay_state_esn
**preplay_esn
,
400 struct xfrm_replay_state_esn
*p
, *pp
, *up
;
407 klen
= xfrm_replay_state_esn_len(up
);
408 ulen
= nla_len(rta
) >= klen
? klen
: sizeof(*up
);
410 p
= kzalloc(klen
, GFP_KERNEL
);
414 pp
= kzalloc(klen
, GFP_KERNEL
);
421 memcpy(pp
, up
, ulen
);
429 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
434 len
+= sizeof(struct xfrm_user_sec_ctx
);
435 len
+= xfrm_ctx
->ctx_len
;
440 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
442 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
443 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
444 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
445 x
->props
.mode
= p
->mode
;
446 x
->props
.replay_window
= min_t(unsigned int, p
->replay_window
,
447 sizeof(x
->replay
.bitmap
) * 8);
448 x
->props
.reqid
= p
->reqid
;
449 x
->props
.family
= p
->family
;
450 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
451 x
->props
.flags
= p
->flags
;
453 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
454 x
->sel
.family
= p
->family
;
458 * someday when pfkey also has support, we could have the code
459 * somehow made shareable and move it to xfrm_state.c - JHS
462 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
,
465 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
466 struct nlattr
*re
= update_esn
? attrs
[XFRMA_REPLAY_ESN_VAL
] : NULL
;
467 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
468 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
469 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
472 struct xfrm_replay_state_esn
*replay_esn
;
473 replay_esn
= nla_data(re
);
474 memcpy(x
->replay_esn
, replay_esn
,
475 xfrm_replay_state_esn_len(replay_esn
));
476 memcpy(x
->preplay_esn
, replay_esn
,
477 xfrm_replay_state_esn_len(replay_esn
));
481 struct xfrm_replay_state
*replay
;
482 replay
= nla_data(rp
);
483 memcpy(&x
->replay
, replay
, sizeof(*replay
));
484 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
488 struct xfrm_lifetime_cur
*ltime
;
489 ltime
= nla_data(lt
);
490 x
->curlft
.bytes
= ltime
->bytes
;
491 x
->curlft
.packets
= ltime
->packets
;
492 x
->curlft
.add_time
= ltime
->add_time
;
493 x
->curlft
.use_time
= ltime
->use_time
;
497 x
->replay_maxage
= nla_get_u32(et
);
500 x
->replay_maxdiff
= nla_get_u32(rt
);
503 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
504 struct xfrm_usersa_info
*p
,
505 struct nlattr
**attrs
,
508 struct xfrm_state
*x
= xfrm_state_alloc(net
);
514 copy_from_user_state(x
, p
);
516 if (attrs
[XFRMA_SA_EXTRA_FLAGS
])
517 x
->props
.extra_flags
= nla_get_u32(attrs
[XFRMA_SA_EXTRA_FLAGS
]);
519 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
520 attrs
[XFRMA_ALG_AEAD
])))
522 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
523 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
525 if (!x
->props
.aalgo
) {
526 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
527 attrs
[XFRMA_ALG_AUTH
])))
530 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
531 xfrm_ealg_get_byname
,
532 attrs
[XFRMA_ALG_CRYPT
])))
534 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
535 xfrm_calg_get_byname
,
536 attrs
[XFRMA_ALG_COMP
])))
539 if (attrs
[XFRMA_ENCAP
]) {
540 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
541 sizeof(*x
->encap
), GFP_KERNEL
);
542 if (x
->encap
== NULL
)
546 if (attrs
[XFRMA_TFCPAD
])
547 x
->tfcpad
= nla_get_u32(attrs
[XFRMA_TFCPAD
]);
549 if (attrs
[XFRMA_COADDR
]) {
550 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
551 sizeof(*x
->coaddr
), GFP_KERNEL
);
552 if (x
->coaddr
== NULL
)
556 xfrm_mark_get(attrs
, &x
->mark
);
558 err
= __xfrm_init_state(x
, false);
562 if (attrs
[XFRMA_SEC_CTX
] &&
563 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
566 if ((err
= xfrm_alloc_replay_state_esn(&x
->replay_esn
, &x
->preplay_esn
,
567 attrs
[XFRMA_REPLAY_ESN_VAL
])))
571 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
572 /* sysctl_xfrm_aevent_etime is in 100ms units */
573 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
575 if ((err
= xfrm_init_replay(x
)))
578 /* override default values from above */
579 xfrm_update_ae_params(x
, attrs
, 0);
584 x
->km
.state
= XFRM_STATE_DEAD
;
591 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
592 struct nlattr
**attrs
)
594 struct net
*net
= sock_net(skb
->sk
);
595 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
596 struct xfrm_state
*x
;
600 err
= verify_newsa_info(p
, attrs
);
604 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
609 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
610 err
= xfrm_state_add(x
);
612 err
= xfrm_state_update(x
);
614 xfrm_audit_state_add(x
, err
? 0 : 1, true);
617 x
->km
.state
= XFRM_STATE_DEAD
;
622 c
.seq
= nlh
->nlmsg_seq
;
623 c
.portid
= nlh
->nlmsg_pid
;
624 c
.event
= nlh
->nlmsg_type
;
626 km_state_notify(x
, &c
);
632 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
633 struct xfrm_usersa_id
*p
,
634 struct nlattr
**attrs
,
637 struct xfrm_state
*x
= NULL
;
640 u32 mark
= xfrm_mark_get(attrs
, &m
);
642 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
644 x
= xfrm_state_lookup(net
, mark
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
646 xfrm_address_t
*saddr
= NULL
;
648 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
655 x
= xfrm_state_lookup_byaddr(net
, mark
,
657 p
->proto
, p
->family
);
666 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
667 struct nlattr
**attrs
)
669 struct net
*net
= sock_net(skb
->sk
);
670 struct xfrm_state
*x
;
673 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
675 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
679 if ((err
= security_xfrm_state_delete(x
)) != 0)
682 if (xfrm_state_kern(x
)) {
687 err
= xfrm_state_delete(x
);
692 c
.seq
= nlh
->nlmsg_seq
;
693 c
.portid
= nlh
->nlmsg_pid
;
694 c
.event
= nlh
->nlmsg_type
;
695 km_state_notify(x
, &c
);
698 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
703 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
705 memset(p
, 0, sizeof(*p
));
706 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
707 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
708 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
709 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
710 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
711 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
712 p
->mode
= x
->props
.mode
;
713 p
->replay_window
= x
->props
.replay_window
;
714 p
->reqid
= x
->props
.reqid
;
715 p
->family
= x
->props
.family
;
716 p
->flags
= x
->props
.flags
;
720 struct xfrm_dump_info
{
721 struct sk_buff
*in_skb
;
722 struct sk_buff
*out_skb
;
727 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
729 struct xfrm_user_sec_ctx
*uctx
;
731 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
733 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
737 uctx
= nla_data(attr
);
738 uctx
->exttype
= XFRMA_SEC_CTX
;
739 uctx
->len
= ctx_size
;
740 uctx
->ctx_doi
= s
->ctx_doi
;
741 uctx
->ctx_alg
= s
->ctx_alg
;
742 uctx
->ctx_len
= s
->ctx_len
;
743 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
748 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
750 struct xfrm_algo
*algo
;
753 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
754 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
758 algo
= nla_data(nla
);
759 strncpy(algo
->alg_name
, auth
->alg_name
, sizeof(algo
->alg_name
));
760 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
761 algo
->alg_key_len
= auth
->alg_key_len
;
766 /* Don't change this without updating xfrm_sa_len! */
767 static int copy_to_user_state_extra(struct xfrm_state
*x
,
768 struct xfrm_usersa_info
*p
,
773 copy_to_user_state(x
, p
);
775 if (x
->props
.extra_flags
) {
776 ret
= nla_put_u32(skb
, XFRMA_SA_EXTRA_FLAGS
,
777 x
->props
.extra_flags
);
783 ret
= nla_put(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
788 ret
= nla_put_u64(skb
, XFRMA_LASTUSED
, x
->lastused
);
793 ret
= nla_put(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
798 ret
= copy_to_user_auth(x
->aalg
, skb
);
800 ret
= nla_put(skb
, XFRMA_ALG_AUTH_TRUNC
,
801 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
806 ret
= nla_put(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
811 ret
= nla_put(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
816 ret
= nla_put(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
821 ret
= nla_put_u32(skb
, XFRMA_TFCPAD
, x
->tfcpad
);
825 ret
= xfrm_mark_put(skb
, &x
->mark
);
829 ret
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
830 xfrm_replay_state_esn_len(x
->replay_esn
),
836 ret
= copy_sec_ctx(x
->security
, skb
);
841 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
843 struct xfrm_dump_info
*sp
= ptr
;
844 struct sk_buff
*in_skb
= sp
->in_skb
;
845 struct sk_buff
*skb
= sp
->out_skb
;
846 struct xfrm_usersa_info
*p
;
847 struct nlmsghdr
*nlh
;
850 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
851 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
857 err
= copy_to_user_state_extra(x
, p
, skb
);
859 nlmsg_cancel(skb
, nlh
);
866 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
868 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
869 struct sock
*sk
= cb
->skb
->sk
;
870 struct net
*net
= sock_net(sk
);
872 xfrm_state_walk_done(walk
, net
);
876 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1];
877 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
879 struct net
*net
= sock_net(skb
->sk
);
880 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
881 struct xfrm_dump_info info
;
883 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
884 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
886 info
.in_skb
= cb
->skb
;
888 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
889 info
.nlmsg_flags
= NLM_F_MULTI
;
892 struct nlattr
*attrs
[XFRMA_MAX
+1];
893 struct xfrm_address_filter
*filter
= NULL
;
899 err
= nlmsg_parse(cb
->nlh
, 0, attrs
, XFRMA_MAX
,
904 if (attrs
[XFRMA_ADDRESS_FILTER
]) {
905 filter
= kmalloc(sizeof(*filter
), GFP_KERNEL
);
909 memcpy(filter
, nla_data(attrs
[XFRMA_ADDRESS_FILTER
]),
913 if (attrs
[XFRMA_PROTO
])
914 proto
= nla_get_u8(attrs
[XFRMA_PROTO
]);
916 xfrm_state_walk_init(walk
, proto
, filter
);
919 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
924 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
925 struct xfrm_state
*x
, u32 seq
)
927 struct xfrm_dump_info info
;
931 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
933 return ERR_PTR(-ENOMEM
);
935 info
.in_skb
= in_skb
;
937 info
.nlmsg_seq
= seq
;
938 info
.nlmsg_flags
= 0;
940 err
= dump_one_state(x
, 0, &info
);
949 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
950 * Must be called with RCU read lock.
952 static inline int xfrm_nlmsg_multicast(struct net
*net
, struct sk_buff
*skb
,
953 u32 pid
, unsigned int group
)
955 struct sock
*nlsk
= rcu_dereference(net
->xfrm
.nlsk
);
958 return nlmsg_multicast(nlsk
, skb
, pid
, group
, GFP_ATOMIC
);
963 static inline size_t xfrm_spdinfo_msgsize(void)
965 return NLMSG_ALIGN(4)
966 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
967 + nla_total_size(sizeof(struct xfrmu_spdhinfo
))
968 + nla_total_size(sizeof(struct xfrmu_spdhthresh
))
969 + nla_total_size(sizeof(struct xfrmu_spdhthresh
));
972 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
973 u32 portid
, u32 seq
, u32 flags
)
975 struct xfrmk_spdinfo si
;
976 struct xfrmu_spdinfo spc
;
977 struct xfrmu_spdhinfo sph
;
978 struct xfrmu_spdhthresh spt4
, spt6
;
979 struct nlmsghdr
*nlh
;
984 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
985 if (nlh
== NULL
) /* shouldn't really happen ... */
990 xfrm_spd_getinfo(net
, &si
);
991 spc
.incnt
= si
.incnt
;
992 spc
.outcnt
= si
.outcnt
;
993 spc
.fwdcnt
= si
.fwdcnt
;
994 spc
.inscnt
= si
.inscnt
;
995 spc
.outscnt
= si
.outscnt
;
996 spc
.fwdscnt
= si
.fwdscnt
;
997 sph
.spdhcnt
= si
.spdhcnt
;
998 sph
.spdhmcnt
= si
.spdhmcnt
;
1001 lseq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
1003 spt4
.lbits
= net
->xfrm
.policy_hthresh
.lbits4
;
1004 spt4
.rbits
= net
->xfrm
.policy_hthresh
.rbits4
;
1005 spt6
.lbits
= net
->xfrm
.policy_hthresh
.lbits6
;
1006 spt6
.rbits
= net
->xfrm
.policy_hthresh
.rbits6
;
1007 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, lseq
));
1009 err
= nla_put(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
1011 err
= nla_put(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
1013 err
= nla_put(skb
, XFRMA_SPD_IPV4_HTHRESH
, sizeof(spt4
), &spt4
);
1015 err
= nla_put(skb
, XFRMA_SPD_IPV6_HTHRESH
, sizeof(spt6
), &spt6
);
1017 nlmsg_cancel(skb
, nlh
);
1021 return nlmsg_end(skb
, nlh
);
1024 static int xfrm_set_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1025 struct nlattr
**attrs
)
1027 struct net
*net
= sock_net(skb
->sk
);
1028 struct xfrmu_spdhthresh
*thresh4
= NULL
;
1029 struct xfrmu_spdhthresh
*thresh6
= NULL
;
1031 /* selector prefixlen thresholds to hash policies */
1032 if (attrs
[XFRMA_SPD_IPV4_HTHRESH
]) {
1033 struct nlattr
*rta
= attrs
[XFRMA_SPD_IPV4_HTHRESH
];
1035 if (nla_len(rta
) < sizeof(*thresh4
))
1037 thresh4
= nla_data(rta
);
1038 if (thresh4
->lbits
> 32 || thresh4
->rbits
> 32)
1041 if (attrs
[XFRMA_SPD_IPV6_HTHRESH
]) {
1042 struct nlattr
*rta
= attrs
[XFRMA_SPD_IPV6_HTHRESH
];
1044 if (nla_len(rta
) < sizeof(*thresh6
))
1046 thresh6
= nla_data(rta
);
1047 if (thresh6
->lbits
> 128 || thresh6
->rbits
> 128)
1051 if (thresh4
|| thresh6
) {
1052 write_seqlock(&net
->xfrm
.policy_hthresh
.lock
);
1054 net
->xfrm
.policy_hthresh
.lbits4
= thresh4
->lbits
;
1055 net
->xfrm
.policy_hthresh
.rbits4
= thresh4
->rbits
;
1058 net
->xfrm
.policy_hthresh
.lbits6
= thresh6
->lbits
;
1059 net
->xfrm
.policy_hthresh
.rbits6
= thresh6
->rbits
;
1061 write_sequnlock(&net
->xfrm
.policy_hthresh
.lock
);
1063 xfrm_policy_hash_rebuild(net
);
1069 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1070 struct nlattr
**attrs
)
1072 struct net
*net
= sock_net(skb
->sk
);
1073 struct sk_buff
*r_skb
;
1074 u32
*flags
= nlmsg_data(nlh
);
1075 u32 sportid
= NETLINK_CB(skb
).portid
;
1076 u32 seq
= nlh
->nlmsg_seq
;
1078 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
1082 if (build_spdinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1085 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1088 static inline size_t xfrm_sadinfo_msgsize(void)
1090 return NLMSG_ALIGN(4)
1091 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
1092 + nla_total_size(4); /* XFRMA_SAD_CNT */
1095 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
1096 u32 portid
, u32 seq
, u32 flags
)
1098 struct xfrmk_sadinfo si
;
1099 struct xfrmu_sadhinfo sh
;
1100 struct nlmsghdr
*nlh
;
1104 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
1105 if (nlh
== NULL
) /* shouldn't really happen ... */
1108 f
= nlmsg_data(nlh
);
1110 xfrm_sad_getinfo(net
, &si
);
1112 sh
.sadhmcnt
= si
.sadhmcnt
;
1113 sh
.sadhcnt
= si
.sadhcnt
;
1115 err
= nla_put_u32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
1117 err
= nla_put(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
1119 nlmsg_cancel(skb
, nlh
);
1123 return nlmsg_end(skb
, nlh
);
1126 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1127 struct nlattr
**attrs
)
1129 struct net
*net
= sock_net(skb
->sk
);
1130 struct sk_buff
*r_skb
;
1131 u32
*flags
= nlmsg_data(nlh
);
1132 u32 sportid
= NETLINK_CB(skb
).portid
;
1133 u32 seq
= nlh
->nlmsg_seq
;
1135 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
1139 if (build_sadinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1142 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1145 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1146 struct nlattr
**attrs
)
1148 struct net
*net
= sock_net(skb
->sk
);
1149 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
1150 struct xfrm_state
*x
;
1151 struct sk_buff
*resp_skb
;
1154 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
1158 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1159 if (IS_ERR(resp_skb
)) {
1160 err
= PTR_ERR(resp_skb
);
1162 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1169 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1170 struct nlattr
**attrs
)
1172 struct net
*net
= sock_net(skb
->sk
);
1173 struct xfrm_state
*x
;
1174 struct xfrm_userspi_info
*p
;
1175 struct sk_buff
*resp_skb
;
1176 xfrm_address_t
*daddr
;
1182 p
= nlmsg_data(nlh
);
1183 err
= verify_spi_info(p
->info
.id
.proto
, p
->min
, p
->max
);
1187 family
= p
->info
.family
;
1188 daddr
= &p
->info
.id
.daddr
;
1192 mark
= xfrm_mark_get(attrs
, &m
);
1194 x
= xfrm_find_acq_byseq(net
, mark
, p
->info
.seq
);
1195 if (x
&& !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
)) {
1202 x
= xfrm_find_acq(net
, &m
, p
->info
.mode
, p
->info
.reqid
,
1203 p
->info
.id
.proto
, daddr
,
1210 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
1214 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1215 if (IS_ERR(resp_skb
)) {
1216 err
= PTR_ERR(resp_skb
);
1220 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1228 static int verify_policy_dir(u8 dir
)
1231 case XFRM_POLICY_IN
:
1232 case XFRM_POLICY_OUT
:
1233 case XFRM_POLICY_FWD
:
1243 static int verify_policy_type(u8 type
)
1246 case XFRM_POLICY_TYPE_MAIN
:
1247 #ifdef CONFIG_XFRM_SUB_POLICY
1248 case XFRM_POLICY_TYPE_SUB
:
1259 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1264 case XFRM_SHARE_ANY
:
1265 case XFRM_SHARE_SESSION
:
1266 case XFRM_SHARE_USER
:
1267 case XFRM_SHARE_UNIQUE
:
1274 switch (p
->action
) {
1275 case XFRM_POLICY_ALLOW
:
1276 case XFRM_POLICY_BLOCK
:
1283 switch (p
->sel
.family
) {
1288 #if IS_ENABLED(CONFIG_IPV6)
1291 return -EAFNOSUPPORT
;
1298 ret
= verify_policy_dir(p
->dir
);
1301 if (p
->index
&& ((p
->index
& XFRM_POLICY_MAX
) != p
->dir
))
1307 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1309 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1310 struct xfrm_user_sec_ctx
*uctx
;
1315 uctx
= nla_data(rt
);
1316 return security_xfrm_policy_alloc(&pol
->security
, uctx
, GFP_KERNEL
);
1319 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1325 for (i
= 0; i
< nr
; i
++, ut
++) {
1326 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1328 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1329 memcpy(&t
->saddr
, &ut
->saddr
,
1330 sizeof(xfrm_address_t
));
1331 t
->reqid
= ut
->reqid
;
1333 t
->share
= ut
->share
;
1334 t
->optional
= ut
->optional
;
1335 t
->aalgos
= ut
->aalgos
;
1336 t
->ealgos
= ut
->ealgos
;
1337 t
->calgos
= ut
->calgos
;
1338 /* If all masks are ~0, then we allow all algorithms. */
1339 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1340 t
->encap_family
= ut
->family
;
1344 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1348 if (nr
> XFRM_MAX_DEPTH
)
1351 for (i
= 0; i
< nr
; i
++) {
1352 /* We never validated the ut->family value, so many
1353 * applications simply leave it at zero. The check was
1354 * never made and ut->family was ignored because all
1355 * templates could be assumed to have the same family as
1356 * the policy itself. Now that we will have ipv4-in-ipv6
1357 * and ipv6-in-ipv4 tunnels, this is no longer true.
1360 ut
[i
].family
= family
;
1362 switch (ut
[i
].family
) {
1365 #if IS_ENABLED(CONFIG_IPV6)
1377 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1379 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1384 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1385 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1388 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1392 copy_templates(pol
, utmpl
, nr
);
1397 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1399 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1400 struct xfrm_userpolicy_type
*upt
;
1401 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1409 err
= verify_policy_type(type
);
1417 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1419 xp
->priority
= p
->priority
;
1420 xp
->index
= p
->index
;
1421 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1422 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1423 xp
->action
= p
->action
;
1424 xp
->flags
= p
->flags
;
1425 xp
->family
= p
->sel
.family
;
1426 /* XXX xp->share = p->share; */
1429 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1431 memset(p
, 0, sizeof(*p
));
1432 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1433 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1434 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1435 p
->priority
= xp
->priority
;
1436 p
->index
= xp
->index
;
1437 p
->sel
.family
= xp
->family
;
1439 p
->action
= xp
->action
;
1440 p
->flags
= xp
->flags
;
1441 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1444 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1446 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1454 copy_from_user_policy(xp
, p
);
1456 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1460 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1461 err
= copy_from_user_sec_ctx(xp
, attrs
);
1465 xfrm_mark_get(attrs
, &xp
->mark
);
1471 xfrm_policy_destroy(xp
);
1475 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1476 struct nlattr
**attrs
)
1478 struct net
*net
= sock_net(skb
->sk
);
1479 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1480 struct xfrm_policy
*xp
;
1485 err
= verify_newpolicy_info(p
);
1488 err
= verify_sec_ctx_len(attrs
);
1492 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1496 /* shouldn't excl be based on nlh flags??
1497 * Aha! this is anti-netlink really i.e more pfkey derived
1498 * in netlink excl is a flag and you wouldnt need
1499 * a type XFRM_MSG_UPDPOLICY - JHS */
1500 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1501 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1502 xfrm_audit_policy_add(xp
, err
? 0 : 1, true);
1505 security_xfrm_policy_free(xp
->security
);
1510 c
.event
= nlh
->nlmsg_type
;
1511 c
.seq
= nlh
->nlmsg_seq
;
1512 c
.portid
= nlh
->nlmsg_pid
;
1513 km_policy_notify(xp
, p
->dir
, &c
);
1520 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1522 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1525 if (xp
->xfrm_nr
== 0)
1528 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1529 struct xfrm_user_tmpl
*up
= &vec
[i
];
1530 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1532 memset(up
, 0, sizeof(*up
));
1533 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1534 up
->family
= kp
->encap_family
;
1535 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1536 up
->reqid
= kp
->reqid
;
1537 up
->mode
= kp
->mode
;
1538 up
->share
= kp
->share
;
1539 up
->optional
= kp
->optional
;
1540 up
->aalgos
= kp
->aalgos
;
1541 up
->ealgos
= kp
->ealgos
;
1542 up
->calgos
= kp
->calgos
;
1545 return nla_put(skb
, XFRMA_TMPL
,
1546 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1549 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1552 return copy_sec_ctx(x
->security
, skb
);
1557 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1560 return copy_sec_ctx(xp
->security
, skb
);
1563 static inline size_t userpolicy_type_attrsize(void)
1565 #ifdef CONFIG_XFRM_SUB_POLICY
1566 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1572 #ifdef CONFIG_XFRM_SUB_POLICY
1573 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1575 struct xfrm_userpolicy_type upt
= {
1579 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1583 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1589 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1591 struct xfrm_dump_info
*sp
= ptr
;
1592 struct xfrm_userpolicy_info
*p
;
1593 struct sk_buff
*in_skb
= sp
->in_skb
;
1594 struct sk_buff
*skb
= sp
->out_skb
;
1595 struct nlmsghdr
*nlh
;
1598 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
1599 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1603 p
= nlmsg_data(nlh
);
1604 copy_to_user_policy(xp
, p
, dir
);
1605 err
= copy_to_user_tmpl(xp
, skb
);
1607 err
= copy_to_user_sec_ctx(xp
, skb
);
1609 err
= copy_to_user_policy_type(xp
->type
, skb
);
1611 err
= xfrm_mark_put(skb
, &xp
->mark
);
1613 nlmsg_cancel(skb
, nlh
);
1616 nlmsg_end(skb
, nlh
);
1620 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1622 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1623 struct net
*net
= sock_net(cb
->skb
->sk
);
1625 xfrm_policy_walk_done(walk
, net
);
1629 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1631 struct net
*net
= sock_net(skb
->sk
);
1632 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1633 struct xfrm_dump_info info
;
1635 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1636 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1638 info
.in_skb
= cb
->skb
;
1640 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1641 info
.nlmsg_flags
= NLM_F_MULTI
;
1645 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1648 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1653 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1654 struct xfrm_policy
*xp
,
1657 struct xfrm_dump_info info
;
1658 struct sk_buff
*skb
;
1661 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1663 return ERR_PTR(-ENOMEM
);
1665 info
.in_skb
= in_skb
;
1667 info
.nlmsg_seq
= seq
;
1668 info
.nlmsg_flags
= 0;
1670 err
= dump_one_policy(xp
, dir
, 0, &info
);
1673 return ERR_PTR(err
);
1679 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1680 struct nlattr
**attrs
)
1682 struct net
*net
= sock_net(skb
->sk
);
1683 struct xfrm_policy
*xp
;
1684 struct xfrm_userpolicy_id
*p
;
1685 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1690 u32 mark
= xfrm_mark_get(attrs
, &m
);
1692 p
= nlmsg_data(nlh
);
1693 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1695 err
= copy_from_user_policy_type(&type
, attrs
);
1699 err
= verify_policy_dir(p
->dir
);
1704 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, delete, &err
);
1706 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1707 struct xfrm_sec_ctx
*ctx
;
1709 err
= verify_sec_ctx_len(attrs
);
1715 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1717 err
= security_xfrm_policy_alloc(&ctx
, uctx
, GFP_KERNEL
);
1721 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
, &p
->sel
,
1723 security_xfrm_policy_free(ctx
);
1729 struct sk_buff
*resp_skb
;
1731 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1732 if (IS_ERR(resp_skb
)) {
1733 err
= PTR_ERR(resp_skb
);
1735 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1736 NETLINK_CB(skb
).portid
);
1739 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
1744 c
.data
.byid
= p
->index
;
1745 c
.event
= nlh
->nlmsg_type
;
1746 c
.seq
= nlh
->nlmsg_seq
;
1747 c
.portid
= nlh
->nlmsg_pid
;
1748 km_policy_notify(xp
, p
->dir
, &c
);
1753 if (delete && err
== 0)
1754 xfrm_garbage_collect(net
);
1758 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1759 struct nlattr
**attrs
)
1761 struct net
*net
= sock_net(skb
->sk
);
1763 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1766 err
= xfrm_state_flush(net
, p
->proto
, true);
1768 if (err
== -ESRCH
) /* empty table */
1772 c
.data
.proto
= p
->proto
;
1773 c
.event
= nlh
->nlmsg_type
;
1774 c
.seq
= nlh
->nlmsg_seq
;
1775 c
.portid
= nlh
->nlmsg_pid
;
1777 km_state_notify(NULL
, &c
);
1782 static inline size_t xfrm_aevent_msgsize(struct xfrm_state
*x
)
1784 size_t replay_size
= x
->replay_esn
?
1785 xfrm_replay_state_esn_len(x
->replay_esn
) :
1786 sizeof(struct xfrm_replay_state
);
1788 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1789 + nla_total_size(replay_size
)
1790 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1791 + nla_total_size(sizeof(struct xfrm_mark
))
1792 + nla_total_size(4) /* XFRM_AE_RTHR */
1793 + nla_total_size(4); /* XFRM_AE_ETHR */
1796 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
1798 struct xfrm_aevent_id
*id
;
1799 struct nlmsghdr
*nlh
;
1802 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1806 id
= nlmsg_data(nlh
);
1807 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
, sizeof(x
->id
.daddr
));
1808 id
->sa_id
.spi
= x
->id
.spi
;
1809 id
->sa_id
.family
= x
->props
.family
;
1810 id
->sa_id
.proto
= x
->id
.proto
;
1811 memcpy(&id
->saddr
, &x
->props
.saddr
, sizeof(x
->props
.saddr
));
1812 id
->reqid
= x
->props
.reqid
;
1813 id
->flags
= c
->data
.aevent
;
1815 if (x
->replay_esn
) {
1816 err
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
1817 xfrm_replay_state_esn_len(x
->replay_esn
),
1820 err
= nla_put(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
),
1825 err
= nla_put(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1829 if (id
->flags
& XFRM_AE_RTHR
) {
1830 err
= nla_put_u32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1834 if (id
->flags
& XFRM_AE_ETHR
) {
1835 err
= nla_put_u32(skb
, XFRMA_ETIMER_THRESH
,
1836 x
->replay_maxage
* 10 / HZ
);
1840 err
= xfrm_mark_put(skb
, &x
->mark
);
1844 return nlmsg_end(skb
, nlh
);
1847 nlmsg_cancel(skb
, nlh
);
1851 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1852 struct nlattr
**attrs
)
1854 struct net
*net
= sock_net(skb
->sk
);
1855 struct xfrm_state
*x
;
1856 struct sk_buff
*r_skb
;
1861 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1862 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1864 mark
= xfrm_mark_get(attrs
, &m
);
1866 x
= xfrm_state_lookup(net
, mark
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1870 r_skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
1871 if (r_skb
== NULL
) {
1877 * XXX: is this lock really needed - none of the other
1878 * gets lock (the concern is things getting updated
1879 * while we are still reading) - jhs
1881 spin_lock_bh(&x
->lock
);
1882 c
.data
.aevent
= p
->flags
;
1883 c
.seq
= nlh
->nlmsg_seq
;
1884 c
.portid
= nlh
->nlmsg_pid
;
1886 if (build_aevent(r_skb
, x
, &c
) < 0)
1888 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).portid
);
1889 spin_unlock_bh(&x
->lock
);
1894 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1895 struct nlattr
**attrs
)
1897 struct net
*net
= sock_net(skb
->sk
);
1898 struct xfrm_state
*x
;
1903 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1904 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1905 struct nlattr
*re
= attrs
[XFRMA_REPLAY_ESN_VAL
];
1906 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1908 if (!lt
&& !rp
&& !re
)
1911 /* pedantic mode - thou shalt sayeth replaceth */
1912 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1915 mark
= xfrm_mark_get(attrs
, &m
);
1917 x
= xfrm_state_lookup(net
, mark
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1921 if (x
->km
.state
!= XFRM_STATE_VALID
)
1924 err
= xfrm_replay_verify_len(x
->replay_esn
, re
);
1928 spin_lock_bh(&x
->lock
);
1929 xfrm_update_ae_params(x
, attrs
, 1);
1930 spin_unlock_bh(&x
->lock
);
1932 c
.event
= nlh
->nlmsg_type
;
1933 c
.seq
= nlh
->nlmsg_seq
;
1934 c
.portid
= nlh
->nlmsg_pid
;
1935 c
.data
.aevent
= XFRM_AE_CU
;
1936 km_state_notify(x
, &c
);
1943 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1944 struct nlattr
**attrs
)
1946 struct net
*net
= sock_net(skb
->sk
);
1948 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1951 err
= copy_from_user_policy_type(&type
, attrs
);
1955 err
= xfrm_policy_flush(net
, type
, true);
1957 if (err
== -ESRCH
) /* empty table */
1963 c
.event
= nlh
->nlmsg_type
;
1964 c
.seq
= nlh
->nlmsg_seq
;
1965 c
.portid
= nlh
->nlmsg_pid
;
1967 km_policy_notify(NULL
, 0, &c
);
1971 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1972 struct nlattr
**attrs
)
1974 struct net
*net
= sock_net(skb
->sk
);
1975 struct xfrm_policy
*xp
;
1976 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1977 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1978 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1981 u32 mark
= xfrm_mark_get(attrs
, &m
);
1983 err
= copy_from_user_policy_type(&type
, attrs
);
1987 err
= verify_policy_dir(p
->dir
);
1992 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, 0, &err
);
1994 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1995 struct xfrm_sec_ctx
*ctx
;
1997 err
= verify_sec_ctx_len(attrs
);
2003 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
2005 err
= security_xfrm_policy_alloc(&ctx
, uctx
, GFP_KERNEL
);
2009 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
,
2010 &p
->sel
, ctx
, 0, &err
);
2011 security_xfrm_policy_free(ctx
);
2016 if (unlikely(xp
->walk
.dead
))
2021 xfrm_policy_delete(xp
, p
->dir
);
2022 xfrm_audit_policy_delete(xp
, 1, true);
2024 // reset the timers here?
2025 WARN(1, "Dont know what to do with soft policy expire\n");
2027 km_policy_expired(xp
, p
->dir
, up
->hard
, nlh
->nlmsg_pid
);
2034 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2035 struct nlattr
**attrs
)
2037 struct net
*net
= sock_net(skb
->sk
);
2038 struct xfrm_state
*x
;
2040 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
2041 struct xfrm_usersa_info
*p
= &ue
->state
;
2043 u32 mark
= xfrm_mark_get(attrs
, &m
);
2045 x
= xfrm_state_lookup(net
, mark
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
2051 spin_lock_bh(&x
->lock
);
2053 if (x
->km
.state
!= XFRM_STATE_VALID
)
2055 km_state_expired(x
, ue
->hard
, nlh
->nlmsg_pid
);
2058 __xfrm_state_delete(x
);
2059 xfrm_audit_state_delete(x
, 1, true);
2063 spin_unlock_bh(&x
->lock
);
2068 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2069 struct nlattr
**attrs
)
2071 struct net
*net
= sock_net(skb
->sk
);
2072 struct xfrm_policy
*xp
;
2073 struct xfrm_user_tmpl
*ut
;
2075 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
2076 struct xfrm_mark mark
;
2078 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
2079 struct xfrm_state
*x
= xfrm_state_alloc(net
);
2085 xfrm_mark_get(attrs
, &mark
);
2087 err
= verify_newpolicy_info(&ua
->policy
);
2092 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
2096 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
2097 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
2098 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
2099 xp
->mark
.m
= x
->mark
.m
= mark
.m
;
2100 xp
->mark
.v
= x
->mark
.v
= mark
.v
;
2102 /* extract the templates and for each call km_key */
2103 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
2104 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
2105 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
2106 x
->props
.mode
= t
->mode
;
2107 x
->props
.reqid
= t
->reqid
;
2108 x
->props
.family
= ut
->family
;
2109 t
->aalgos
= ua
->aalgos
;
2110 t
->ealgos
= ua
->ealgos
;
2111 t
->calgos
= ua
->calgos
;
2112 err
= km_query(x
, t
, xp
);
2122 WARN(1, "BAD policy passed\n");
2129 #ifdef CONFIG_XFRM_MIGRATE
2130 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
2131 struct xfrm_kmaddress
*k
,
2132 struct nlattr
**attrs
, int *num
)
2134 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
2135 struct xfrm_user_migrate
*um
;
2139 struct xfrm_user_kmaddress
*uk
;
2141 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
2142 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
2143 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
2144 k
->family
= uk
->family
;
2145 k
->reserved
= uk
->reserved
;
2149 num_migrate
= nla_len(rt
) / sizeof(*um
);
2151 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
2154 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
2155 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
2156 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
2157 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
2158 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
2160 ma
->proto
= um
->proto
;
2161 ma
->mode
= um
->mode
;
2162 ma
->reqid
= um
->reqid
;
2164 ma
->old_family
= um
->old_family
;
2165 ma
->new_family
= um
->new_family
;
2172 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2173 struct nlattr
**attrs
)
2175 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
2176 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2177 struct xfrm_kmaddress km
, *kmp
;
2181 struct net
*net
= sock_net(skb
->sk
);
2183 if (attrs
[XFRMA_MIGRATE
] == NULL
)
2186 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
2188 err
= copy_from_user_policy_type(&type
, attrs
);
2192 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
2199 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
, net
);
2204 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2205 struct nlattr
**attrs
)
2207 return -ENOPROTOOPT
;
2211 #ifdef CONFIG_XFRM_MIGRATE
2212 static int copy_to_user_migrate(const struct xfrm_migrate
*m
, struct sk_buff
*skb
)
2214 struct xfrm_user_migrate um
;
2216 memset(&um
, 0, sizeof(um
));
2217 um
.proto
= m
->proto
;
2219 um
.reqid
= m
->reqid
;
2220 um
.old_family
= m
->old_family
;
2221 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
2222 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
2223 um
.new_family
= m
->new_family
;
2224 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
2225 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
2227 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
2230 static int copy_to_user_kmaddress(const struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
2232 struct xfrm_user_kmaddress uk
;
2234 memset(&uk
, 0, sizeof(uk
));
2235 uk
.family
= k
->family
;
2236 uk
.reserved
= k
->reserved
;
2237 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
2238 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
2240 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
2243 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
)
2245 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
2246 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
2247 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
2248 + userpolicy_type_attrsize();
2251 static int build_migrate(struct sk_buff
*skb
, const struct xfrm_migrate
*m
,
2252 int num_migrate
, const struct xfrm_kmaddress
*k
,
2253 const struct xfrm_selector
*sel
, u8 dir
, u8 type
)
2255 const struct xfrm_migrate
*mp
;
2256 struct xfrm_userpolicy_id
*pol_id
;
2257 struct nlmsghdr
*nlh
;
2260 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
2264 pol_id
= nlmsg_data(nlh
);
2265 /* copy data from selector, dir, and type to the pol_id */
2266 memset(pol_id
, 0, sizeof(*pol_id
));
2267 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
2271 err
= copy_to_user_kmaddress(k
, skb
);
2275 err
= copy_to_user_policy_type(type
, skb
);
2278 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2279 err
= copy_to_user_migrate(mp
, skb
);
2284 return nlmsg_end(skb
, nlh
);
2287 nlmsg_cancel(skb
, nlh
);
2291 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2292 const struct xfrm_migrate
*m
, int num_migrate
,
2293 const struct xfrm_kmaddress
*k
)
2295 struct net
*net
= &init_net
;
2296 struct sk_buff
*skb
;
2298 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
), GFP_ATOMIC
);
2303 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, dir
, type
) < 0)
2306 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_MIGRATE
);
2309 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2310 const struct xfrm_migrate
*m
, int num_migrate
,
2311 const struct xfrm_kmaddress
*k
)
2313 return -ENOPROTOOPT
;
2317 #define XMSGSIZE(type) sizeof(struct type)
2319 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2320 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2321 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2322 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2323 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2324 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2325 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2326 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2327 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2328 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2329 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2330 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2331 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2332 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2333 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2334 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2335 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2336 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2337 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2338 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2339 [XFRM_MSG_NEWSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2340 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2345 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2346 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2347 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2348 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2349 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2350 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2351 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2352 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2353 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2354 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2355 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2356 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2357 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2358 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2359 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2360 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2361 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2362 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2363 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2364 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2365 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2366 [XFRMA_MARK
] = { .len
= sizeof(struct xfrm_mark
) },
2367 [XFRMA_TFCPAD
] = { .type
= NLA_U32
},
2368 [XFRMA_REPLAY_ESN_VAL
] = { .len
= sizeof(struct xfrm_replay_state_esn
) },
2369 [XFRMA_SA_EXTRA_FLAGS
] = { .type
= NLA_U32
},
2370 [XFRMA_PROTO
] = { .type
= NLA_U8
},
2371 [XFRMA_ADDRESS_FILTER
] = { .len
= sizeof(struct xfrm_address_filter
) },
2374 static const struct nla_policy xfrma_spd_policy
[XFRMA_SPD_MAX
+1] = {
2375 [XFRMA_SPD_IPV4_HTHRESH
] = { .len
= sizeof(struct xfrmu_spdhthresh
) },
2376 [XFRMA_SPD_IPV6_HTHRESH
] = { .len
= sizeof(struct xfrmu_spdhthresh
) },
2379 static const struct xfrm_link
{
2380 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2381 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2382 int (*done
)(struct netlink_callback
*);
2383 const struct nla_policy
*nla_pol
;
2385 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2386 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2387 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2388 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2389 .dump
= xfrm_dump_sa
,
2390 .done
= xfrm_dump_sa_done
},
2391 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2392 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2393 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2394 .dump
= xfrm_dump_policy
,
2395 .done
= xfrm_dump_policy_done
},
2396 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2397 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2398 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2399 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2400 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2401 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2402 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2403 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2404 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2405 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2406 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2407 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2408 [XFRM_MSG_NEWSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_set_spdinfo
,
2409 .nla_pol
= xfrma_spd_policy
,
2410 .nla_max
= XFRMA_SPD_MAX
},
2411 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2414 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2416 struct net
*net
= sock_net(skb
->sk
);
2417 struct nlattr
*attrs
[XFRMA_MAX
+1];
2418 const struct xfrm_link
*link
;
2421 type
= nlh
->nlmsg_type
;
2422 if (type
> XFRM_MSG_MAX
)
2425 type
-= XFRM_MSG_BASE
;
2426 link
= &xfrm_dispatch
[type
];
2428 /* All operations require privileges, even GET */
2429 if (!netlink_net_capable(skb
, CAP_NET_ADMIN
))
2432 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2433 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2434 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2435 if (link
->dump
== NULL
)
2439 struct netlink_dump_control c
= {
2443 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, &c
);
2447 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
,
2448 link
->nla_max
? : XFRMA_MAX
,
2449 link
->nla_pol
? : xfrma_policy
);
2453 if (link
->doit
== NULL
)
2456 return link
->doit(skb
, nlh
, attrs
);
2459 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2461 struct net
*net
= sock_net(skb
->sk
);
2463 mutex_lock(&net
->xfrm
.xfrm_cfg_mutex
);
2464 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2465 mutex_unlock(&net
->xfrm
.xfrm_cfg_mutex
);
2468 static inline size_t xfrm_expire_msgsize(void)
2470 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
))
2471 + nla_total_size(sizeof(struct xfrm_mark
));
2474 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
2476 struct xfrm_user_expire
*ue
;
2477 struct nlmsghdr
*nlh
;
2480 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2484 ue
= nlmsg_data(nlh
);
2485 copy_to_user_state(x
, &ue
->state
);
2486 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2488 err
= xfrm_mark_put(skb
, &x
->mark
);
2492 return nlmsg_end(skb
, nlh
);
2495 static int xfrm_exp_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2497 struct net
*net
= xs_net(x
);
2498 struct sk_buff
*skb
;
2500 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2504 if (build_expire(skb
, x
, c
) < 0) {
2509 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_EXPIRE
);
2512 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2514 struct net
*net
= xs_net(x
);
2515 struct sk_buff
*skb
;
2517 skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
2521 if (build_aevent(skb
, x
, c
) < 0)
2524 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_AEVENTS
);
2527 static int xfrm_notify_sa_flush(const struct km_event
*c
)
2529 struct net
*net
= c
->net
;
2530 struct xfrm_usersa_flush
*p
;
2531 struct nlmsghdr
*nlh
;
2532 struct sk_buff
*skb
;
2533 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2535 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2539 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2545 p
= nlmsg_data(nlh
);
2546 p
->proto
= c
->data
.proto
;
2548 nlmsg_end(skb
, nlh
);
2550 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_SA
);
2553 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2557 l
+= nla_total_size(aead_len(x
->aead
));
2559 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2560 (x
->aalg
->alg_key_len
+ 7) / 8);
2561 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2564 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2566 l
+= nla_total_size(sizeof(*x
->calg
));
2568 l
+= nla_total_size(sizeof(*x
->encap
));
2570 l
+= nla_total_size(sizeof(x
->tfcpad
));
2572 l
+= nla_total_size(xfrm_replay_state_esn_len(x
->replay_esn
));
2574 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2575 x
->security
->ctx_len
);
2577 l
+= nla_total_size(sizeof(*x
->coaddr
));
2578 if (x
->props
.extra_flags
)
2579 l
+= nla_total_size(sizeof(x
->props
.extra_flags
));
2581 /* Must count x->lastused as it may become non-zero behind our back. */
2582 l
+= nla_total_size(sizeof(u64
));
2587 static int xfrm_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
2589 struct net
*net
= xs_net(x
);
2590 struct xfrm_usersa_info
*p
;
2591 struct xfrm_usersa_id
*id
;
2592 struct nlmsghdr
*nlh
;
2593 struct sk_buff
*skb
;
2594 int len
= xfrm_sa_len(x
);
2597 headlen
= sizeof(*p
);
2598 if (c
->event
== XFRM_MSG_DELSA
) {
2599 len
+= nla_total_size(headlen
);
2600 headlen
= sizeof(*id
);
2601 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2603 len
+= NLMSG_ALIGN(headlen
);
2605 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2609 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2614 p
= nlmsg_data(nlh
);
2615 if (c
->event
== XFRM_MSG_DELSA
) {
2616 struct nlattr
*attr
;
2618 id
= nlmsg_data(nlh
);
2619 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2620 id
->spi
= x
->id
.spi
;
2621 id
->family
= x
->props
.family
;
2622 id
->proto
= x
->id
.proto
;
2624 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2631 err
= copy_to_user_state_extra(x
, p
, skb
);
2635 nlmsg_end(skb
, nlh
);
2637 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_SA
);
2644 static int xfrm_send_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2648 case XFRM_MSG_EXPIRE
:
2649 return xfrm_exp_state_notify(x
, c
);
2650 case XFRM_MSG_NEWAE
:
2651 return xfrm_aevent_state_notify(x
, c
);
2652 case XFRM_MSG_DELSA
:
2653 case XFRM_MSG_UPDSA
:
2654 case XFRM_MSG_NEWSA
:
2655 return xfrm_notify_sa(x
, c
);
2656 case XFRM_MSG_FLUSHSA
:
2657 return xfrm_notify_sa_flush(c
);
2659 printk(KERN_NOTICE
"xfrm_user: Unknown SA event %d\n",
2668 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2669 struct xfrm_policy
*xp
)
2671 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2672 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2673 + nla_total_size(sizeof(struct xfrm_mark
))
2674 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2675 + userpolicy_type_attrsize();
2678 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2679 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
)
2681 __u32 seq
= xfrm_get_acqseq();
2682 struct xfrm_user_acquire
*ua
;
2683 struct nlmsghdr
*nlh
;
2686 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2690 ua
= nlmsg_data(nlh
);
2691 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2692 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2693 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2694 copy_to_user_policy(xp
, &ua
->policy
, XFRM_POLICY_OUT
);
2695 ua
->aalgos
= xt
->aalgos
;
2696 ua
->ealgos
= xt
->ealgos
;
2697 ua
->calgos
= xt
->calgos
;
2698 ua
->seq
= x
->km
.seq
= seq
;
2700 err
= copy_to_user_tmpl(xp
, skb
);
2702 err
= copy_to_user_state_sec_ctx(x
, skb
);
2704 err
= copy_to_user_policy_type(xp
->type
, skb
);
2706 err
= xfrm_mark_put(skb
, &xp
->mark
);
2708 nlmsg_cancel(skb
, nlh
);
2712 return nlmsg_end(skb
, nlh
);
2715 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2716 struct xfrm_policy
*xp
)
2718 struct net
*net
= xs_net(x
);
2719 struct sk_buff
*skb
;
2721 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2725 if (build_acquire(skb
, x
, xt
, xp
) < 0)
2728 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_ACQUIRE
);
2731 /* User gives us xfrm_user_policy_info followed by an array of 0
2732 * or more templates.
2734 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2735 u8
*data
, int len
, int *dir
)
2737 struct net
*net
= sock_net(sk
);
2738 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2739 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2740 struct xfrm_policy
*xp
;
2743 switch (sk
->sk_family
) {
2745 if (opt
!= IP_XFRM_POLICY
) {
2750 #if IS_ENABLED(CONFIG_IPV6)
2752 if (opt
!= IPV6_XFRM_POLICY
) {
2765 if (len
< sizeof(*p
) ||
2766 verify_newpolicy_info(p
))
2769 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2770 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2773 if (p
->dir
> XFRM_POLICY_OUT
)
2776 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
2782 copy_from_user_policy(xp
, p
);
2783 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2784 copy_templates(xp
, ut
, nr
);
2791 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2793 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2794 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2795 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2796 + nla_total_size(sizeof(struct xfrm_mark
))
2797 + userpolicy_type_attrsize();
2800 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2801 int dir
, const struct km_event
*c
)
2803 struct xfrm_user_polexpire
*upe
;
2804 int hard
= c
->data
.hard
;
2805 struct nlmsghdr
*nlh
;
2808 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2812 upe
= nlmsg_data(nlh
);
2813 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2814 err
= copy_to_user_tmpl(xp
, skb
);
2816 err
= copy_to_user_sec_ctx(xp
, skb
);
2818 err
= copy_to_user_policy_type(xp
->type
, skb
);
2820 err
= xfrm_mark_put(skb
, &xp
->mark
);
2822 nlmsg_cancel(skb
, nlh
);
2827 return nlmsg_end(skb
, nlh
);
2830 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2832 struct net
*net
= xp_net(xp
);
2833 struct sk_buff
*skb
;
2835 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2839 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2842 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_EXPIRE
);
2845 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2847 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2848 struct net
*net
= xp_net(xp
);
2849 struct xfrm_userpolicy_info
*p
;
2850 struct xfrm_userpolicy_id
*id
;
2851 struct nlmsghdr
*nlh
;
2852 struct sk_buff
*skb
;
2855 headlen
= sizeof(*p
);
2856 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2857 len
+= nla_total_size(headlen
);
2858 headlen
= sizeof(*id
);
2860 len
+= userpolicy_type_attrsize();
2861 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2862 len
+= NLMSG_ALIGN(headlen
);
2864 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2868 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2873 p
= nlmsg_data(nlh
);
2874 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2875 struct nlattr
*attr
;
2877 id
= nlmsg_data(nlh
);
2878 memset(id
, 0, sizeof(*id
));
2881 id
->index
= xp
->index
;
2883 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2885 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2893 copy_to_user_policy(xp
, p
, dir
);
2894 err
= copy_to_user_tmpl(xp
, skb
);
2896 err
= copy_to_user_policy_type(xp
->type
, skb
);
2898 err
= xfrm_mark_put(skb
, &xp
->mark
);
2902 nlmsg_end(skb
, nlh
);
2904 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_POLICY
);
2911 static int xfrm_notify_policy_flush(const struct km_event
*c
)
2913 struct net
*net
= c
->net
;
2914 struct nlmsghdr
*nlh
;
2915 struct sk_buff
*skb
;
2918 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2922 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2926 err
= copy_to_user_policy_type(c
->data
.type
, skb
);
2930 nlmsg_end(skb
, nlh
);
2932 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_POLICY
);
2939 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2943 case XFRM_MSG_NEWPOLICY
:
2944 case XFRM_MSG_UPDPOLICY
:
2945 case XFRM_MSG_DELPOLICY
:
2946 return xfrm_notify_policy(xp
, dir
, c
);
2947 case XFRM_MSG_FLUSHPOLICY
:
2948 return xfrm_notify_policy_flush(c
);
2949 case XFRM_MSG_POLEXPIRE
:
2950 return xfrm_exp_policy_notify(xp
, dir
, c
);
2952 printk(KERN_NOTICE
"xfrm_user: Unknown Policy event %d\n",
2960 static inline size_t xfrm_report_msgsize(void)
2962 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2965 static int build_report(struct sk_buff
*skb
, u8 proto
,
2966 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2968 struct xfrm_user_report
*ur
;
2969 struct nlmsghdr
*nlh
;
2971 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2975 ur
= nlmsg_data(nlh
);
2977 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2980 int err
= nla_put(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2982 nlmsg_cancel(skb
, nlh
);
2986 return nlmsg_end(skb
, nlh
);
2989 static int xfrm_send_report(struct net
*net
, u8 proto
,
2990 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2992 struct sk_buff
*skb
;
2994 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2998 if (build_report(skb
, proto
, sel
, addr
) < 0)
3001 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_REPORT
);
3004 static inline size_t xfrm_mapping_msgsize(void)
3006 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
3009 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
3010 xfrm_address_t
*new_saddr
, __be16 new_sport
)
3012 struct xfrm_user_mapping
*um
;
3013 struct nlmsghdr
*nlh
;
3015 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
3019 um
= nlmsg_data(nlh
);
3021 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
3022 um
->id
.spi
= x
->id
.spi
;
3023 um
->id
.family
= x
->props
.family
;
3024 um
->id
.proto
= x
->id
.proto
;
3025 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
3026 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
3027 um
->new_sport
= new_sport
;
3028 um
->old_sport
= x
->encap
->encap_sport
;
3029 um
->reqid
= x
->props
.reqid
;
3031 return nlmsg_end(skb
, nlh
);
3034 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
3037 struct net
*net
= xs_net(x
);
3038 struct sk_buff
*skb
;
3040 if (x
->id
.proto
!= IPPROTO_ESP
)
3046 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
3050 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
3053 return xfrm_nlmsg_multicast(net
, skb
, 0, XFRMNLGRP_MAPPING
);
3056 static bool xfrm_is_alive(const struct km_event
*c
)
3058 return (bool)xfrm_acquire_is_on(c
->net
);
3061 static struct xfrm_mgr netlink_mgr
= {
3063 .notify
= xfrm_send_state_notify
,
3064 .acquire
= xfrm_send_acquire
,
3065 .compile_policy
= xfrm_compile_policy
,
3066 .notify_policy
= xfrm_send_policy_notify
,
3067 .report
= xfrm_send_report
,
3068 .migrate
= xfrm_send_migrate
,
3069 .new_mapping
= xfrm_send_mapping
,
3070 .is_alive
= xfrm_is_alive
,
3073 static int __net_init
xfrm_user_net_init(struct net
*net
)
3076 struct netlink_kernel_cfg cfg
= {
3077 .groups
= XFRMNLGRP_MAX
,
3078 .input
= xfrm_netlink_rcv
,
3081 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, &cfg
);
3084 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
3085 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
3089 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
3092 list_for_each_entry(net
, net_exit_list
, exit_list
)
3093 RCU_INIT_POINTER(net
->xfrm
.nlsk
, NULL
);
3095 list_for_each_entry(net
, net_exit_list
, exit_list
)
3096 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
3099 static struct pernet_operations xfrm_user_net_ops
= {
3100 .init
= xfrm_user_net_init
,
3101 .exit_batch
= xfrm_user_net_exit
,
3104 static int __init
xfrm_user_init(void)
3108 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
3110 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
3113 rv
= xfrm_register_km(&netlink_mgr
);
3115 unregister_pernet_subsys(&xfrm_user_net_ops
);
3119 static void __exit
xfrm_user_exit(void)
3121 xfrm_unregister_km(&netlink_mgr
);
3122 unregister_pernet_subsys(&xfrm_user_net_ops
);
3125 module_init(xfrm_user_init
);
3126 module_exit(xfrm_user_exit
);
3127 MODULE_LICENSE("GPL");
3128 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);