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 inline int aead_len(struct xfrm_algo_aead
*alg
)
37 return sizeof(*alg
) + ((alg
->alg_key_len
+ 7) / 8);
40 static int verify_one_alg(struct nlattr
**attrs
, enum xfrm_attr_type_t type
)
42 struct nlattr
*rt
= attrs
[type
];
43 struct xfrm_algo
*algp
;
49 if (nla_len(rt
) < xfrm_alg_len(algp
))
62 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
66 static int verify_auth_trunc(struct nlattr
**attrs
)
68 struct nlattr
*rt
= attrs
[XFRMA_ALG_AUTH_TRUNC
];
69 struct xfrm_algo_auth
*algp
;
75 if (nla_len(rt
) < xfrm_alg_auth_len(algp
))
78 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
82 static int verify_aead(struct nlattr
**attrs
)
84 struct nlattr
*rt
= attrs
[XFRMA_ALG_AEAD
];
85 struct xfrm_algo_aead
*algp
;
91 if (nla_len(rt
) < aead_len(algp
))
94 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
98 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
99 xfrm_address_t
**addrp
)
101 struct nlattr
*rt
= attrs
[type
];
104 *addrp
= nla_data(rt
);
107 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
109 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
110 struct xfrm_user_sec_ctx
*uctx
;
116 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
122 static inline int verify_replay(struct xfrm_usersa_info
*p
,
123 struct nlattr
**attrs
)
125 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_ESN_VAL
];
126 struct xfrm_replay_state_esn
*rs
;
128 if (p
->flags
& XFRM_STATE_ESN
) {
134 if (rs
->bmp_len
> XFRMA_REPLAY_ESN_MAX
/ sizeof(rs
->bmp
[0]) / 8)
137 if (nla_len(rt
) < xfrm_replay_state_esn_len(rs
) &&
138 nla_len(rt
) != sizeof(*rs
))
145 /* As only ESP and AH support ESN feature. */
146 if ((p
->id
.proto
!= IPPROTO_ESP
) && (p
->id
.proto
!= IPPROTO_AH
))
149 if (p
->replay_window
!= 0)
155 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
156 struct nlattr
**attrs
)
166 #if IS_ENABLED(CONFIG_IPV6)
178 switch (p
->id
.proto
) {
180 if ((!attrs
[XFRMA_ALG_AUTH
] &&
181 !attrs
[XFRMA_ALG_AUTH_TRUNC
]) ||
182 attrs
[XFRMA_ALG_AEAD
] ||
183 attrs
[XFRMA_ALG_CRYPT
] ||
184 attrs
[XFRMA_ALG_COMP
] ||
185 attrs
[XFRMA_TFCPAD
] ||
186 (ntohl(p
->id
.spi
) >= 0x10000))
192 if (attrs
[XFRMA_ALG_COMP
])
194 if (!attrs
[XFRMA_ALG_AUTH
] &&
195 !attrs
[XFRMA_ALG_AUTH_TRUNC
] &&
196 !attrs
[XFRMA_ALG_CRYPT
] &&
197 !attrs
[XFRMA_ALG_AEAD
])
199 if ((attrs
[XFRMA_ALG_AUTH
] ||
200 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
201 attrs
[XFRMA_ALG_CRYPT
]) &&
202 attrs
[XFRMA_ALG_AEAD
])
204 if (attrs
[XFRMA_TFCPAD
] &&
205 p
->mode
!= XFRM_MODE_TUNNEL
)
210 if (!attrs
[XFRMA_ALG_COMP
] ||
211 attrs
[XFRMA_ALG_AEAD
] ||
212 attrs
[XFRMA_ALG_AUTH
] ||
213 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
214 attrs
[XFRMA_ALG_CRYPT
] ||
219 #if IS_ENABLED(CONFIG_IPV6)
220 case IPPROTO_DSTOPTS
:
221 case IPPROTO_ROUTING
:
222 if (attrs
[XFRMA_ALG_COMP
] ||
223 attrs
[XFRMA_ALG_AUTH
] ||
224 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
225 attrs
[XFRMA_ALG_AEAD
] ||
226 attrs
[XFRMA_ALG_CRYPT
] ||
227 attrs
[XFRMA_ENCAP
] ||
228 attrs
[XFRMA_SEC_CTX
] ||
229 attrs
[XFRMA_TFCPAD
] ||
230 !attrs
[XFRMA_COADDR
])
239 if ((err
= verify_aead(attrs
)))
241 if ((err
= verify_auth_trunc(attrs
)))
243 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
245 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
247 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
249 if ((err
= verify_sec_ctx_len(attrs
)))
251 if ((err
= verify_replay(p
, attrs
)))
256 case XFRM_MODE_TRANSPORT
:
257 case XFRM_MODE_TUNNEL
:
258 case XFRM_MODE_ROUTEOPTIMIZATION
:
272 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
273 struct xfrm_algo_desc
*(*get_byname
)(const char *, int),
276 struct xfrm_algo
*p
, *ualg
;
277 struct xfrm_algo_desc
*algo
;
282 ualg
= nla_data(rta
);
284 algo
= get_byname(ualg
->alg_name
, 1);
287 *props
= algo
->desc
.sadb_alg_id
;
289 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
293 strcpy(p
->alg_name
, algo
->name
);
298 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
301 struct xfrm_algo
*ualg
;
302 struct xfrm_algo_auth
*p
;
303 struct xfrm_algo_desc
*algo
;
308 ualg
= nla_data(rta
);
310 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
313 *props
= algo
->desc
.sadb_alg_id
;
315 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
319 strcpy(p
->alg_name
, algo
->name
);
320 p
->alg_key_len
= ualg
->alg_key_len
;
321 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
322 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
328 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
331 struct xfrm_algo_auth
*p
, *ualg
;
332 struct xfrm_algo_desc
*algo
;
337 ualg
= nla_data(rta
);
339 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
342 if ((ualg
->alg_trunc_len
/ 8) > MAX_AH_AUTH_LEN
||
343 ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
345 *props
= algo
->desc
.sadb_alg_id
;
347 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
351 strcpy(p
->alg_name
, algo
->name
);
352 if (!p
->alg_trunc_len
)
353 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
359 static int attach_aead(struct xfrm_algo_aead
**algpp
, u8
*props
,
362 struct xfrm_algo_aead
*p
, *ualg
;
363 struct xfrm_algo_desc
*algo
;
368 ualg
= nla_data(rta
);
370 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
373 *props
= algo
->desc
.sadb_alg_id
;
375 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
379 strcpy(p
->alg_name
, algo
->name
);
384 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn
*replay_esn
,
387 struct xfrm_replay_state_esn
*up
;
390 if (!replay_esn
|| !rp
)
394 ulen
= xfrm_replay_state_esn_len(up
);
396 if (nla_len(rp
) < ulen
|| xfrm_replay_state_esn_len(replay_esn
) != ulen
)
402 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn
**replay_esn
,
403 struct xfrm_replay_state_esn
**preplay_esn
,
406 struct xfrm_replay_state_esn
*p
, *pp
, *up
;
413 klen
= xfrm_replay_state_esn_len(up
);
414 ulen
= nla_len(rta
) >= klen
? klen
: sizeof(*up
);
416 p
= kzalloc(klen
, GFP_KERNEL
);
420 pp
= kzalloc(klen
, GFP_KERNEL
);
427 memcpy(pp
, up
, ulen
);
435 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
440 len
+= sizeof(struct xfrm_user_sec_ctx
);
441 len
+= xfrm_ctx
->ctx_len
;
446 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
448 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
449 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
450 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
451 x
->props
.mode
= p
->mode
;
452 x
->props
.replay_window
= min_t(unsigned int, p
->replay_window
,
453 sizeof(x
->replay
.bitmap
) * 8);
454 x
->props
.reqid
= p
->reqid
;
455 x
->props
.family
= p
->family
;
456 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
457 x
->props
.flags
= p
->flags
;
459 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
460 x
->sel
.family
= p
->family
;
464 * someday when pfkey also has support, we could have the code
465 * somehow made shareable and move it to xfrm_state.c - JHS
468 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
,
471 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
472 struct nlattr
*re
= update_esn
? attrs
[XFRMA_REPLAY_ESN_VAL
] : NULL
;
473 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
474 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
475 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
478 struct xfrm_replay_state_esn
*replay_esn
;
479 replay_esn
= nla_data(re
);
480 memcpy(x
->replay_esn
, replay_esn
,
481 xfrm_replay_state_esn_len(replay_esn
));
482 memcpy(x
->preplay_esn
, replay_esn
,
483 xfrm_replay_state_esn_len(replay_esn
));
487 struct xfrm_replay_state
*replay
;
488 replay
= nla_data(rp
);
489 memcpy(&x
->replay
, replay
, sizeof(*replay
));
490 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
494 struct xfrm_lifetime_cur
*ltime
;
495 ltime
= nla_data(lt
);
496 x
->curlft
.bytes
= ltime
->bytes
;
497 x
->curlft
.packets
= ltime
->packets
;
498 x
->curlft
.add_time
= ltime
->add_time
;
499 x
->curlft
.use_time
= ltime
->use_time
;
503 x
->replay_maxage
= nla_get_u32(et
);
506 x
->replay_maxdiff
= nla_get_u32(rt
);
509 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
510 struct xfrm_usersa_info
*p
,
511 struct nlattr
**attrs
,
514 struct xfrm_state
*x
= xfrm_state_alloc(net
);
520 copy_from_user_state(x
, p
);
522 if (attrs
[XFRMA_SA_EXTRA_FLAGS
])
523 x
->props
.extra_flags
= nla_get_u32(attrs
[XFRMA_SA_EXTRA_FLAGS
]);
525 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
526 attrs
[XFRMA_ALG_AEAD
])))
528 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
529 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
531 if (!x
->props
.aalgo
) {
532 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
533 attrs
[XFRMA_ALG_AUTH
])))
536 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
537 xfrm_ealg_get_byname
,
538 attrs
[XFRMA_ALG_CRYPT
])))
540 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
541 xfrm_calg_get_byname
,
542 attrs
[XFRMA_ALG_COMP
])))
545 if (attrs
[XFRMA_ENCAP
]) {
546 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
547 sizeof(*x
->encap
), GFP_KERNEL
);
548 if (x
->encap
== NULL
)
552 if (attrs
[XFRMA_TFCPAD
])
553 x
->tfcpad
= nla_get_u32(attrs
[XFRMA_TFCPAD
]);
555 if (attrs
[XFRMA_COADDR
]) {
556 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
557 sizeof(*x
->coaddr
), GFP_KERNEL
);
558 if (x
->coaddr
== NULL
)
562 xfrm_mark_get(attrs
, &x
->mark
);
564 err
= __xfrm_init_state(x
, false);
568 if (attrs
[XFRMA_SEC_CTX
] &&
569 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
572 if ((err
= xfrm_alloc_replay_state_esn(&x
->replay_esn
, &x
->preplay_esn
,
573 attrs
[XFRMA_REPLAY_ESN_VAL
])))
577 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
578 /* sysctl_xfrm_aevent_etime is in 100ms units */
579 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
581 if ((err
= xfrm_init_replay(x
)))
584 /* override default values from above */
585 xfrm_update_ae_params(x
, attrs
, 0);
590 x
->km
.state
= XFRM_STATE_DEAD
;
597 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
598 struct nlattr
**attrs
)
600 struct net
*net
= sock_net(skb
->sk
);
601 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
602 struct xfrm_state
*x
;
605 kuid_t loginuid
= audit_get_loginuid(current
);
606 unsigned int sessionid
= audit_get_sessionid(current
);
609 err
= verify_newsa_info(p
, attrs
);
613 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
618 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
619 err
= xfrm_state_add(x
);
621 err
= xfrm_state_update(x
);
623 security_task_getsecid(current
, &sid
);
624 xfrm_audit_state_add(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
627 x
->km
.state
= XFRM_STATE_DEAD
;
632 c
.seq
= nlh
->nlmsg_seq
;
633 c
.portid
= nlh
->nlmsg_pid
;
634 c
.event
= nlh
->nlmsg_type
;
636 km_state_notify(x
, &c
);
642 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
643 struct xfrm_usersa_id
*p
,
644 struct nlattr
**attrs
,
647 struct xfrm_state
*x
= NULL
;
650 u32 mark
= xfrm_mark_get(attrs
, &m
);
652 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
654 x
= xfrm_state_lookup(net
, mark
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
656 xfrm_address_t
*saddr
= NULL
;
658 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
665 x
= xfrm_state_lookup_byaddr(net
, mark
,
667 p
->proto
, p
->family
);
676 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
677 struct nlattr
**attrs
)
679 struct net
*net
= sock_net(skb
->sk
);
680 struct xfrm_state
*x
;
683 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
684 kuid_t loginuid
= audit_get_loginuid(current
);
685 unsigned int sessionid
= audit_get_sessionid(current
);
688 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
692 if ((err
= security_xfrm_state_delete(x
)) != 0)
695 if (xfrm_state_kern(x
)) {
700 err
= xfrm_state_delete(x
);
705 c
.seq
= nlh
->nlmsg_seq
;
706 c
.portid
= nlh
->nlmsg_pid
;
707 c
.event
= nlh
->nlmsg_type
;
708 km_state_notify(x
, &c
);
711 security_task_getsecid(current
, &sid
);
712 xfrm_audit_state_delete(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
717 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
719 memset(p
, 0, sizeof(*p
));
720 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
721 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
722 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
723 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
724 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
725 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
726 p
->mode
= x
->props
.mode
;
727 p
->replay_window
= x
->props
.replay_window
;
728 p
->reqid
= x
->props
.reqid
;
729 p
->family
= x
->props
.family
;
730 p
->flags
= x
->props
.flags
;
734 struct xfrm_dump_info
{
735 struct sk_buff
*in_skb
;
736 struct sk_buff
*out_skb
;
741 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
743 struct xfrm_user_sec_ctx
*uctx
;
745 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
747 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
751 uctx
= nla_data(attr
);
752 uctx
->exttype
= XFRMA_SEC_CTX
;
753 uctx
->len
= ctx_size
;
754 uctx
->ctx_doi
= s
->ctx_doi
;
755 uctx
->ctx_alg
= s
->ctx_alg
;
756 uctx
->ctx_len
= s
->ctx_len
;
757 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
762 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
764 struct xfrm_algo
*algo
;
767 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
768 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
772 algo
= nla_data(nla
);
773 strncpy(algo
->alg_name
, auth
->alg_name
, sizeof(algo
->alg_name
));
774 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
775 algo
->alg_key_len
= auth
->alg_key_len
;
780 /* Don't change this without updating xfrm_sa_len! */
781 static int copy_to_user_state_extra(struct xfrm_state
*x
,
782 struct xfrm_usersa_info
*p
,
787 copy_to_user_state(x
, p
);
789 if (x
->props
.extra_flags
) {
790 ret
= nla_put_u32(skb
, XFRMA_SA_EXTRA_FLAGS
,
791 x
->props
.extra_flags
);
797 ret
= nla_put(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
802 ret
= nla_put_u64(skb
, XFRMA_LASTUSED
, x
->lastused
);
807 ret
= nla_put(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
812 ret
= copy_to_user_auth(x
->aalg
, skb
);
814 ret
= nla_put(skb
, XFRMA_ALG_AUTH_TRUNC
,
815 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
820 ret
= nla_put(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
825 ret
= nla_put(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
830 ret
= nla_put(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
835 ret
= nla_put_u32(skb
, XFRMA_TFCPAD
, x
->tfcpad
);
839 ret
= xfrm_mark_put(skb
, &x
->mark
);
843 ret
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
844 xfrm_replay_state_esn_len(x
->replay_esn
),
850 ret
= copy_sec_ctx(x
->security
, skb
);
855 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
857 struct xfrm_dump_info
*sp
= ptr
;
858 struct sk_buff
*in_skb
= sp
->in_skb
;
859 struct sk_buff
*skb
= sp
->out_skb
;
860 struct xfrm_usersa_info
*p
;
861 struct nlmsghdr
*nlh
;
864 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
865 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
871 err
= copy_to_user_state_extra(x
, p
, skb
);
873 nlmsg_cancel(skb
, nlh
);
880 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
882 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
883 struct sock
*sk
= cb
->skb
->sk
;
884 struct net
*net
= sock_net(sk
);
886 xfrm_state_walk_done(walk
, net
);
890 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1];
891 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
893 struct net
*net
= sock_net(skb
->sk
);
894 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
895 struct xfrm_dump_info info
;
897 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
898 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
900 info
.in_skb
= cb
->skb
;
902 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
903 info
.nlmsg_flags
= NLM_F_MULTI
;
906 struct nlattr
*attrs
[XFRMA_MAX
+1];
907 struct xfrm_address_filter
*filter
= NULL
;
913 err
= nlmsg_parse(cb
->nlh
, 0, attrs
, XFRMA_MAX
,
918 if (attrs
[XFRMA_ADDRESS_FILTER
]) {
919 filter
= kmalloc(sizeof(*filter
), GFP_KERNEL
);
923 memcpy(filter
, nla_data(attrs
[XFRMA_ADDRESS_FILTER
]),
927 if (attrs
[XFRMA_PROTO
])
928 proto
= nla_get_u8(attrs
[XFRMA_PROTO
]);
930 xfrm_state_walk_init(walk
, proto
, filter
);
933 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
938 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
939 struct xfrm_state
*x
, u32 seq
)
941 struct xfrm_dump_info info
;
945 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
947 return ERR_PTR(-ENOMEM
);
949 info
.in_skb
= in_skb
;
951 info
.nlmsg_seq
= seq
;
952 info
.nlmsg_flags
= 0;
954 err
= dump_one_state(x
, 0, &info
);
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
));
970 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
971 u32 portid
, u32 seq
, u32 flags
)
973 struct xfrmk_spdinfo si
;
974 struct xfrmu_spdinfo spc
;
975 struct xfrmu_spdhinfo sph
;
976 struct nlmsghdr
*nlh
;
980 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
981 if (nlh
== NULL
) /* shouldn't really happen ... */
986 xfrm_spd_getinfo(net
, &si
);
987 spc
.incnt
= si
.incnt
;
988 spc
.outcnt
= si
.outcnt
;
989 spc
.fwdcnt
= si
.fwdcnt
;
990 spc
.inscnt
= si
.inscnt
;
991 spc
.outscnt
= si
.outscnt
;
992 spc
.fwdscnt
= si
.fwdscnt
;
993 sph
.spdhcnt
= si
.spdhcnt
;
994 sph
.spdhmcnt
= si
.spdhmcnt
;
996 err
= nla_put(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
998 err
= nla_put(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
1000 nlmsg_cancel(skb
, nlh
);
1004 return nlmsg_end(skb
, nlh
);
1007 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1008 struct nlattr
**attrs
)
1010 struct net
*net
= sock_net(skb
->sk
);
1011 struct sk_buff
*r_skb
;
1012 u32
*flags
= nlmsg_data(nlh
);
1013 u32 sportid
= NETLINK_CB(skb
).portid
;
1014 u32 seq
= nlh
->nlmsg_seq
;
1016 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
1020 if (build_spdinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1023 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1026 static inline size_t xfrm_sadinfo_msgsize(void)
1028 return NLMSG_ALIGN(4)
1029 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
1030 + nla_total_size(4); /* XFRMA_SAD_CNT */
1033 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
1034 u32 portid
, u32 seq
, u32 flags
)
1036 struct xfrmk_sadinfo si
;
1037 struct xfrmu_sadhinfo sh
;
1038 struct nlmsghdr
*nlh
;
1042 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
1043 if (nlh
== NULL
) /* shouldn't really happen ... */
1046 f
= nlmsg_data(nlh
);
1048 xfrm_sad_getinfo(net
, &si
);
1050 sh
.sadhmcnt
= si
.sadhmcnt
;
1051 sh
.sadhcnt
= si
.sadhcnt
;
1053 err
= nla_put_u32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
1055 err
= nla_put(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
1057 nlmsg_cancel(skb
, nlh
);
1061 return nlmsg_end(skb
, nlh
);
1064 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1065 struct nlattr
**attrs
)
1067 struct net
*net
= sock_net(skb
->sk
);
1068 struct sk_buff
*r_skb
;
1069 u32
*flags
= nlmsg_data(nlh
);
1070 u32 sportid
= NETLINK_CB(skb
).portid
;
1071 u32 seq
= nlh
->nlmsg_seq
;
1073 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
1077 if (build_sadinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1080 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1083 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1084 struct nlattr
**attrs
)
1086 struct net
*net
= sock_net(skb
->sk
);
1087 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
1088 struct xfrm_state
*x
;
1089 struct sk_buff
*resp_skb
;
1092 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
1096 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1097 if (IS_ERR(resp_skb
)) {
1098 err
= PTR_ERR(resp_skb
);
1100 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1107 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1108 struct nlattr
**attrs
)
1110 struct net
*net
= sock_net(skb
->sk
);
1111 struct xfrm_state
*x
;
1112 struct xfrm_userspi_info
*p
;
1113 struct sk_buff
*resp_skb
;
1114 xfrm_address_t
*daddr
;
1120 p
= nlmsg_data(nlh
);
1121 err
= verify_spi_info(p
->info
.id
.proto
, p
->min
, p
->max
);
1125 family
= p
->info
.family
;
1126 daddr
= &p
->info
.id
.daddr
;
1130 mark
= xfrm_mark_get(attrs
, &m
);
1132 x
= xfrm_find_acq_byseq(net
, mark
, p
->info
.seq
);
1133 if (x
&& !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
)) {
1140 x
= xfrm_find_acq(net
, &m
, p
->info
.mode
, p
->info
.reqid
,
1141 p
->info
.id
.proto
, daddr
,
1148 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
1152 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1153 if (IS_ERR(resp_skb
)) {
1154 err
= PTR_ERR(resp_skb
);
1158 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1166 static int verify_policy_dir(u8 dir
)
1169 case XFRM_POLICY_IN
:
1170 case XFRM_POLICY_OUT
:
1171 case XFRM_POLICY_FWD
:
1181 static int verify_policy_type(u8 type
)
1184 case XFRM_POLICY_TYPE_MAIN
:
1185 #ifdef CONFIG_XFRM_SUB_POLICY
1186 case XFRM_POLICY_TYPE_SUB
:
1197 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1202 case XFRM_SHARE_ANY
:
1203 case XFRM_SHARE_SESSION
:
1204 case XFRM_SHARE_USER
:
1205 case XFRM_SHARE_UNIQUE
:
1212 switch (p
->action
) {
1213 case XFRM_POLICY_ALLOW
:
1214 case XFRM_POLICY_BLOCK
:
1221 switch (p
->sel
.family
) {
1226 #if IS_ENABLED(CONFIG_IPV6)
1229 return -EAFNOSUPPORT
;
1236 ret
= verify_policy_dir(p
->dir
);
1239 if (p
->index
&& ((p
->index
& XFRM_POLICY_MAX
) != p
->dir
))
1245 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1247 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1248 struct xfrm_user_sec_ctx
*uctx
;
1253 uctx
= nla_data(rt
);
1254 return security_xfrm_policy_alloc(&pol
->security
, uctx
);
1257 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1263 for (i
= 0; i
< nr
; i
++, ut
++) {
1264 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1266 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1267 memcpy(&t
->saddr
, &ut
->saddr
,
1268 sizeof(xfrm_address_t
));
1269 t
->reqid
= ut
->reqid
;
1271 t
->share
= ut
->share
;
1272 t
->optional
= ut
->optional
;
1273 t
->aalgos
= ut
->aalgos
;
1274 t
->ealgos
= ut
->ealgos
;
1275 t
->calgos
= ut
->calgos
;
1276 /* If all masks are ~0, then we allow all algorithms. */
1277 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1278 t
->encap_family
= ut
->family
;
1282 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1286 if (nr
> XFRM_MAX_DEPTH
)
1289 for (i
= 0; i
< nr
; i
++) {
1290 /* We never validated the ut->family value, so many
1291 * applications simply leave it at zero. The check was
1292 * never made and ut->family was ignored because all
1293 * templates could be assumed to have the same family as
1294 * the policy itself. Now that we will have ipv4-in-ipv6
1295 * and ipv6-in-ipv4 tunnels, this is no longer true.
1298 ut
[i
].family
= family
;
1300 switch (ut
[i
].family
) {
1303 #if IS_ENABLED(CONFIG_IPV6)
1315 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1317 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1322 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1323 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1326 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1330 copy_templates(pol
, utmpl
, nr
);
1335 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1337 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1338 struct xfrm_userpolicy_type
*upt
;
1339 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1347 err
= verify_policy_type(type
);
1355 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1357 xp
->priority
= p
->priority
;
1358 xp
->index
= p
->index
;
1359 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1360 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1361 xp
->action
= p
->action
;
1362 xp
->flags
= p
->flags
;
1363 xp
->family
= p
->sel
.family
;
1364 /* XXX xp->share = p->share; */
1367 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1369 memset(p
, 0, sizeof(*p
));
1370 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1371 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1372 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1373 p
->priority
= xp
->priority
;
1374 p
->index
= xp
->index
;
1375 p
->sel
.family
= xp
->family
;
1377 p
->action
= xp
->action
;
1378 p
->flags
= xp
->flags
;
1379 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1382 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1384 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1392 copy_from_user_policy(xp
, p
);
1394 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1398 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1399 err
= copy_from_user_sec_ctx(xp
, attrs
);
1403 xfrm_mark_get(attrs
, &xp
->mark
);
1409 xfrm_policy_destroy(xp
);
1413 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1414 struct nlattr
**attrs
)
1416 struct net
*net
= sock_net(skb
->sk
);
1417 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1418 struct xfrm_policy
*xp
;
1422 kuid_t loginuid
= audit_get_loginuid(current
);
1423 unsigned int sessionid
= audit_get_sessionid(current
);
1426 err
= verify_newpolicy_info(p
);
1429 err
= verify_sec_ctx_len(attrs
);
1433 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1437 /* shouldn't excl be based on nlh flags??
1438 * Aha! this is anti-netlink really i.e more pfkey derived
1439 * in netlink excl is a flag and you wouldnt need
1440 * a type XFRM_MSG_UPDPOLICY - JHS */
1441 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1442 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1443 security_task_getsecid(current
, &sid
);
1444 xfrm_audit_policy_add(xp
, err
? 0 : 1, loginuid
, sessionid
, sid
);
1447 security_xfrm_policy_free(xp
->security
);
1452 c
.event
= nlh
->nlmsg_type
;
1453 c
.seq
= nlh
->nlmsg_seq
;
1454 c
.portid
= nlh
->nlmsg_pid
;
1455 km_policy_notify(xp
, p
->dir
, &c
);
1462 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1464 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1467 if (xp
->xfrm_nr
== 0)
1470 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1471 struct xfrm_user_tmpl
*up
= &vec
[i
];
1472 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1474 memset(up
, 0, sizeof(*up
));
1475 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1476 up
->family
= kp
->encap_family
;
1477 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1478 up
->reqid
= kp
->reqid
;
1479 up
->mode
= kp
->mode
;
1480 up
->share
= kp
->share
;
1481 up
->optional
= kp
->optional
;
1482 up
->aalgos
= kp
->aalgos
;
1483 up
->ealgos
= kp
->ealgos
;
1484 up
->calgos
= kp
->calgos
;
1487 return nla_put(skb
, XFRMA_TMPL
,
1488 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1491 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1494 return copy_sec_ctx(x
->security
, skb
);
1499 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1502 return copy_sec_ctx(xp
->security
, skb
);
1505 static inline size_t userpolicy_type_attrsize(void)
1507 #ifdef CONFIG_XFRM_SUB_POLICY
1508 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1514 #ifdef CONFIG_XFRM_SUB_POLICY
1515 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1517 struct xfrm_userpolicy_type upt
= {
1521 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1525 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1531 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1533 struct xfrm_dump_info
*sp
= ptr
;
1534 struct xfrm_userpolicy_info
*p
;
1535 struct sk_buff
*in_skb
= sp
->in_skb
;
1536 struct sk_buff
*skb
= sp
->out_skb
;
1537 struct nlmsghdr
*nlh
;
1540 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
1541 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1545 p
= nlmsg_data(nlh
);
1546 copy_to_user_policy(xp
, p
, dir
);
1547 err
= copy_to_user_tmpl(xp
, skb
);
1549 err
= copy_to_user_sec_ctx(xp
, skb
);
1551 err
= copy_to_user_policy_type(xp
->type
, skb
);
1553 err
= xfrm_mark_put(skb
, &xp
->mark
);
1555 nlmsg_cancel(skb
, nlh
);
1558 nlmsg_end(skb
, nlh
);
1562 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1564 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1565 struct net
*net
= sock_net(cb
->skb
->sk
);
1567 xfrm_policy_walk_done(walk
, net
);
1571 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1573 struct net
*net
= sock_net(skb
->sk
);
1574 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1575 struct xfrm_dump_info info
;
1577 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1578 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1580 info
.in_skb
= cb
->skb
;
1582 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1583 info
.nlmsg_flags
= NLM_F_MULTI
;
1587 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1590 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1595 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1596 struct xfrm_policy
*xp
,
1599 struct xfrm_dump_info info
;
1600 struct sk_buff
*skb
;
1603 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1605 return ERR_PTR(-ENOMEM
);
1607 info
.in_skb
= in_skb
;
1609 info
.nlmsg_seq
= seq
;
1610 info
.nlmsg_flags
= 0;
1612 err
= dump_one_policy(xp
, dir
, 0, &info
);
1615 return ERR_PTR(err
);
1621 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1622 struct nlattr
**attrs
)
1624 struct net
*net
= sock_net(skb
->sk
);
1625 struct xfrm_policy
*xp
;
1626 struct xfrm_userpolicy_id
*p
;
1627 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1632 u32 mark
= xfrm_mark_get(attrs
, &m
);
1634 p
= nlmsg_data(nlh
);
1635 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1637 err
= copy_from_user_policy_type(&type
, attrs
);
1641 err
= verify_policy_dir(p
->dir
);
1646 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, delete, &err
);
1648 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1649 struct xfrm_sec_ctx
*ctx
;
1651 err
= verify_sec_ctx_len(attrs
);
1657 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1659 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1663 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
, &p
->sel
,
1665 security_xfrm_policy_free(ctx
);
1671 struct sk_buff
*resp_skb
;
1673 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1674 if (IS_ERR(resp_skb
)) {
1675 err
= PTR_ERR(resp_skb
);
1677 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1678 NETLINK_CB(skb
).portid
);
1681 kuid_t loginuid
= audit_get_loginuid(current
);
1682 unsigned int sessionid
= audit_get_sessionid(current
);
1685 security_task_getsecid(current
, &sid
);
1686 xfrm_audit_policy_delete(xp
, err
? 0 : 1, loginuid
, sessionid
,
1692 c
.data
.byid
= p
->index
;
1693 c
.event
= nlh
->nlmsg_type
;
1694 c
.seq
= nlh
->nlmsg_seq
;
1695 c
.portid
= nlh
->nlmsg_pid
;
1696 km_policy_notify(xp
, p
->dir
, &c
);
1701 if (delete && err
== 0)
1702 xfrm_garbage_collect(net
);
1706 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1707 struct nlattr
**attrs
)
1709 struct net
*net
= sock_net(skb
->sk
);
1711 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1712 struct xfrm_audit audit_info
;
1715 audit_info
.loginuid
= audit_get_loginuid(current
);
1716 audit_info
.sessionid
= audit_get_sessionid(current
);
1717 security_task_getsecid(current
, &audit_info
.secid
);
1718 err
= xfrm_state_flush(net
, p
->proto
, &audit_info
);
1720 if (err
== -ESRCH
) /* empty table */
1724 c
.data
.proto
= p
->proto
;
1725 c
.event
= nlh
->nlmsg_type
;
1726 c
.seq
= nlh
->nlmsg_seq
;
1727 c
.portid
= nlh
->nlmsg_pid
;
1729 km_state_notify(NULL
, &c
);
1734 static inline size_t xfrm_aevent_msgsize(struct xfrm_state
*x
)
1736 size_t replay_size
= x
->replay_esn
?
1737 xfrm_replay_state_esn_len(x
->replay_esn
) :
1738 sizeof(struct xfrm_replay_state
);
1740 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1741 + nla_total_size(replay_size
)
1742 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1743 + nla_total_size(sizeof(struct xfrm_mark
))
1744 + nla_total_size(4) /* XFRM_AE_RTHR */
1745 + nla_total_size(4); /* XFRM_AE_ETHR */
1748 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
1750 struct xfrm_aevent_id
*id
;
1751 struct nlmsghdr
*nlh
;
1754 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1758 id
= nlmsg_data(nlh
);
1759 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
, sizeof(x
->id
.daddr
));
1760 id
->sa_id
.spi
= x
->id
.spi
;
1761 id
->sa_id
.family
= x
->props
.family
;
1762 id
->sa_id
.proto
= x
->id
.proto
;
1763 memcpy(&id
->saddr
, &x
->props
.saddr
, sizeof(x
->props
.saddr
));
1764 id
->reqid
= x
->props
.reqid
;
1765 id
->flags
= c
->data
.aevent
;
1767 if (x
->replay_esn
) {
1768 err
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
1769 xfrm_replay_state_esn_len(x
->replay_esn
),
1772 err
= nla_put(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
),
1777 err
= nla_put(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1781 if (id
->flags
& XFRM_AE_RTHR
) {
1782 err
= nla_put_u32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1786 if (id
->flags
& XFRM_AE_ETHR
) {
1787 err
= nla_put_u32(skb
, XFRMA_ETIMER_THRESH
,
1788 x
->replay_maxage
* 10 / HZ
);
1792 err
= xfrm_mark_put(skb
, &x
->mark
);
1796 return nlmsg_end(skb
, nlh
);
1799 nlmsg_cancel(skb
, nlh
);
1803 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1804 struct nlattr
**attrs
)
1806 struct net
*net
= sock_net(skb
->sk
);
1807 struct xfrm_state
*x
;
1808 struct sk_buff
*r_skb
;
1813 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1814 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1816 mark
= xfrm_mark_get(attrs
, &m
);
1818 x
= xfrm_state_lookup(net
, mark
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1822 r_skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
1823 if (r_skb
== NULL
) {
1829 * XXX: is this lock really needed - none of the other
1830 * gets lock (the concern is things getting updated
1831 * while we are still reading) - jhs
1833 spin_lock_bh(&x
->lock
);
1834 c
.data
.aevent
= p
->flags
;
1835 c
.seq
= nlh
->nlmsg_seq
;
1836 c
.portid
= nlh
->nlmsg_pid
;
1838 if (build_aevent(r_skb
, x
, &c
) < 0)
1840 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).portid
);
1841 spin_unlock_bh(&x
->lock
);
1846 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1847 struct nlattr
**attrs
)
1849 struct net
*net
= sock_net(skb
->sk
);
1850 struct xfrm_state
*x
;
1855 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1856 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1857 struct nlattr
*re
= attrs
[XFRMA_REPLAY_ESN_VAL
];
1858 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1860 if (!lt
&& !rp
&& !re
)
1863 /* pedantic mode - thou shalt sayeth replaceth */
1864 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1867 mark
= xfrm_mark_get(attrs
, &m
);
1869 x
= xfrm_state_lookup(net
, mark
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1873 if (x
->km
.state
!= XFRM_STATE_VALID
)
1876 err
= xfrm_replay_verify_len(x
->replay_esn
, re
);
1880 spin_lock_bh(&x
->lock
);
1881 xfrm_update_ae_params(x
, attrs
, 1);
1882 spin_unlock_bh(&x
->lock
);
1884 c
.event
= nlh
->nlmsg_type
;
1885 c
.seq
= nlh
->nlmsg_seq
;
1886 c
.portid
= nlh
->nlmsg_pid
;
1887 c
.data
.aevent
= XFRM_AE_CU
;
1888 km_state_notify(x
, &c
);
1895 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1896 struct nlattr
**attrs
)
1898 struct net
*net
= sock_net(skb
->sk
);
1900 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1902 struct xfrm_audit audit_info
;
1904 err
= copy_from_user_policy_type(&type
, attrs
);
1908 audit_info
.loginuid
= audit_get_loginuid(current
);
1909 audit_info
.sessionid
= audit_get_sessionid(current
);
1910 security_task_getsecid(current
, &audit_info
.secid
);
1911 err
= xfrm_policy_flush(net
, type
, &audit_info
);
1913 if (err
== -ESRCH
) /* empty table */
1919 c
.event
= nlh
->nlmsg_type
;
1920 c
.seq
= nlh
->nlmsg_seq
;
1921 c
.portid
= nlh
->nlmsg_pid
;
1923 km_policy_notify(NULL
, 0, &c
);
1927 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1928 struct nlattr
**attrs
)
1930 struct net
*net
= sock_net(skb
->sk
);
1931 struct xfrm_policy
*xp
;
1932 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1933 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1934 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1937 u32 mark
= xfrm_mark_get(attrs
, &m
);
1939 err
= copy_from_user_policy_type(&type
, attrs
);
1943 err
= verify_policy_dir(p
->dir
);
1948 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, 0, &err
);
1950 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1951 struct xfrm_sec_ctx
*ctx
;
1953 err
= verify_sec_ctx_len(attrs
);
1959 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1961 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1965 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
,
1966 &p
->sel
, ctx
, 0, &err
);
1967 security_xfrm_policy_free(ctx
);
1972 if (unlikely(xp
->walk
.dead
))
1977 kuid_t loginuid
= audit_get_loginuid(current
);
1978 unsigned int sessionid
= audit_get_sessionid(current
);
1981 security_task_getsecid(current
, &sid
);
1982 xfrm_policy_delete(xp
, p
->dir
);
1983 xfrm_audit_policy_delete(xp
, 1, loginuid
, sessionid
, sid
);
1986 // reset the timers here?
1987 WARN(1, "Dont know what to do with soft policy expire\n");
1989 km_policy_expired(xp
, p
->dir
, up
->hard
, nlh
->nlmsg_pid
);
1996 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1997 struct nlattr
**attrs
)
1999 struct net
*net
= sock_net(skb
->sk
);
2000 struct xfrm_state
*x
;
2002 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
2003 struct xfrm_usersa_info
*p
= &ue
->state
;
2005 u32 mark
= xfrm_mark_get(attrs
, &m
);
2007 x
= xfrm_state_lookup(net
, mark
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
2013 spin_lock_bh(&x
->lock
);
2015 if (x
->km
.state
!= XFRM_STATE_VALID
)
2017 km_state_expired(x
, ue
->hard
, nlh
->nlmsg_pid
);
2020 kuid_t loginuid
= audit_get_loginuid(current
);
2021 unsigned int sessionid
= audit_get_sessionid(current
);
2024 security_task_getsecid(current
, &sid
);
2025 __xfrm_state_delete(x
);
2026 xfrm_audit_state_delete(x
, 1, loginuid
, sessionid
, sid
);
2030 spin_unlock_bh(&x
->lock
);
2035 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2036 struct nlattr
**attrs
)
2038 struct net
*net
= sock_net(skb
->sk
);
2039 struct xfrm_policy
*xp
;
2040 struct xfrm_user_tmpl
*ut
;
2042 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
2043 struct xfrm_mark mark
;
2045 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
2046 struct xfrm_state
*x
= xfrm_state_alloc(net
);
2052 xfrm_mark_get(attrs
, &mark
);
2054 err
= verify_newpolicy_info(&ua
->policy
);
2059 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
2063 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
2064 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
2065 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
2066 xp
->mark
.m
= x
->mark
.m
= mark
.m
;
2067 xp
->mark
.v
= x
->mark
.v
= mark
.v
;
2069 /* extract the templates and for each call km_key */
2070 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
2071 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
2072 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
2073 x
->props
.mode
= t
->mode
;
2074 x
->props
.reqid
= t
->reqid
;
2075 x
->props
.family
= ut
->family
;
2076 t
->aalgos
= ua
->aalgos
;
2077 t
->ealgos
= ua
->ealgos
;
2078 t
->calgos
= ua
->calgos
;
2079 err
= km_query(x
, t
, xp
);
2089 WARN(1, "BAD policy passed\n");
2096 #ifdef CONFIG_XFRM_MIGRATE
2097 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
2098 struct xfrm_kmaddress
*k
,
2099 struct nlattr
**attrs
, int *num
)
2101 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
2102 struct xfrm_user_migrate
*um
;
2106 struct xfrm_user_kmaddress
*uk
;
2108 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
2109 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
2110 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
2111 k
->family
= uk
->family
;
2112 k
->reserved
= uk
->reserved
;
2116 num_migrate
= nla_len(rt
) / sizeof(*um
);
2118 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
2121 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
2122 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
2123 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
2124 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
2125 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
2127 ma
->proto
= um
->proto
;
2128 ma
->mode
= um
->mode
;
2129 ma
->reqid
= um
->reqid
;
2131 ma
->old_family
= um
->old_family
;
2132 ma
->new_family
= um
->new_family
;
2139 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2140 struct nlattr
**attrs
)
2142 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
2143 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2144 struct xfrm_kmaddress km
, *kmp
;
2148 struct net
*net
= sock_net(skb
->sk
);
2150 if (attrs
[XFRMA_MIGRATE
] == NULL
)
2153 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
2155 err
= copy_from_user_policy_type(&type
, attrs
);
2159 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
2166 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
, net
);
2171 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2172 struct nlattr
**attrs
)
2174 return -ENOPROTOOPT
;
2178 #ifdef CONFIG_XFRM_MIGRATE
2179 static int copy_to_user_migrate(const struct xfrm_migrate
*m
, struct sk_buff
*skb
)
2181 struct xfrm_user_migrate um
;
2183 memset(&um
, 0, sizeof(um
));
2184 um
.proto
= m
->proto
;
2186 um
.reqid
= m
->reqid
;
2187 um
.old_family
= m
->old_family
;
2188 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
2189 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
2190 um
.new_family
= m
->new_family
;
2191 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
2192 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
2194 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
2197 static int copy_to_user_kmaddress(const struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
2199 struct xfrm_user_kmaddress uk
;
2201 memset(&uk
, 0, sizeof(uk
));
2202 uk
.family
= k
->family
;
2203 uk
.reserved
= k
->reserved
;
2204 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
2205 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
2207 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
2210 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
)
2212 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
2213 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
2214 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
2215 + userpolicy_type_attrsize();
2218 static int build_migrate(struct sk_buff
*skb
, const struct xfrm_migrate
*m
,
2219 int num_migrate
, const struct xfrm_kmaddress
*k
,
2220 const struct xfrm_selector
*sel
, u8 dir
, u8 type
)
2222 const struct xfrm_migrate
*mp
;
2223 struct xfrm_userpolicy_id
*pol_id
;
2224 struct nlmsghdr
*nlh
;
2227 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
2231 pol_id
= nlmsg_data(nlh
);
2232 /* copy data from selector, dir, and type to the pol_id */
2233 memset(pol_id
, 0, sizeof(*pol_id
));
2234 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
2238 err
= copy_to_user_kmaddress(k
, skb
);
2242 err
= copy_to_user_policy_type(type
, skb
);
2245 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2246 err
= copy_to_user_migrate(mp
, skb
);
2251 return nlmsg_end(skb
, nlh
);
2254 nlmsg_cancel(skb
, nlh
);
2258 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2259 const struct xfrm_migrate
*m
, int num_migrate
,
2260 const struct xfrm_kmaddress
*k
)
2262 struct net
*net
= &init_net
;
2263 struct sk_buff
*skb
;
2265 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
), GFP_ATOMIC
);
2270 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, dir
, type
) < 0)
2273 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
2276 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2277 const struct xfrm_migrate
*m
, int num_migrate
,
2278 const struct xfrm_kmaddress
*k
)
2280 return -ENOPROTOOPT
;
2284 #define XMSGSIZE(type) sizeof(struct type)
2286 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2287 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2288 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2289 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2290 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2291 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2292 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2293 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2294 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2295 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2296 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2297 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2298 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2299 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2300 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2301 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2302 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2303 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2304 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2305 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2306 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2311 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2312 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2313 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2314 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2315 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2316 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2317 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2318 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2319 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2320 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2321 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2322 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2323 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2324 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2325 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2326 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2327 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2328 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2329 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2330 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2331 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2332 [XFRMA_MARK
] = { .len
= sizeof(struct xfrm_mark
) },
2333 [XFRMA_TFCPAD
] = { .type
= NLA_U32
},
2334 [XFRMA_REPLAY_ESN_VAL
] = { .len
= sizeof(struct xfrm_replay_state_esn
) },
2335 [XFRMA_SA_EXTRA_FLAGS
] = { .type
= NLA_U32
},
2336 [XFRMA_PROTO
] = { .type
= NLA_U8
},
2337 [XFRMA_ADDRESS_FILTER
] = { .len
= sizeof(struct xfrm_address_filter
) },
2340 static const struct xfrm_link
{
2341 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2342 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2343 int (*done
)(struct netlink_callback
*);
2344 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2345 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2346 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2347 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2348 .dump
= xfrm_dump_sa
,
2349 .done
= xfrm_dump_sa_done
},
2350 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2351 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2352 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2353 .dump
= xfrm_dump_policy
,
2354 .done
= xfrm_dump_policy_done
},
2355 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2356 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2357 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2358 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2359 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2360 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2361 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2362 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2363 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2364 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2365 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2366 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2367 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2370 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2372 struct net
*net
= sock_net(skb
->sk
);
2373 struct nlattr
*attrs
[XFRMA_MAX
+1];
2374 const struct xfrm_link
*link
;
2377 type
= nlh
->nlmsg_type
;
2378 if (type
> XFRM_MSG_MAX
)
2381 type
-= XFRM_MSG_BASE
;
2382 link
= &xfrm_dispatch
[type
];
2384 /* All operations require privileges, even GET */
2385 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2388 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2389 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2390 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2391 if (link
->dump
== NULL
)
2395 struct netlink_dump_control c
= {
2399 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, &c
);
2403 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
2408 if (link
->doit
== NULL
)
2411 return link
->doit(skb
, nlh
, attrs
);
2414 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2416 struct net
*net
= sock_net(skb
->sk
);
2418 mutex_lock(&net
->xfrm
.xfrm_cfg_mutex
);
2419 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2420 mutex_unlock(&net
->xfrm
.xfrm_cfg_mutex
);
2423 static inline size_t xfrm_expire_msgsize(void)
2425 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
))
2426 + nla_total_size(sizeof(struct xfrm_mark
));
2429 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
2431 struct xfrm_user_expire
*ue
;
2432 struct nlmsghdr
*nlh
;
2435 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2439 ue
= nlmsg_data(nlh
);
2440 copy_to_user_state(x
, &ue
->state
);
2441 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2443 err
= xfrm_mark_put(skb
, &x
->mark
);
2447 return nlmsg_end(skb
, nlh
);
2450 static int xfrm_exp_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2452 struct net
*net
= xs_net(x
);
2453 struct sk_buff
*skb
;
2455 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2459 if (build_expire(skb
, x
, c
) < 0) {
2464 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2467 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2469 struct net
*net
= xs_net(x
);
2470 struct sk_buff
*skb
;
2472 skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
2476 if (build_aevent(skb
, x
, c
) < 0)
2479 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
2482 static int xfrm_notify_sa_flush(const struct km_event
*c
)
2484 struct net
*net
= c
->net
;
2485 struct xfrm_usersa_flush
*p
;
2486 struct nlmsghdr
*nlh
;
2487 struct sk_buff
*skb
;
2488 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2490 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2494 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2500 p
= nlmsg_data(nlh
);
2501 p
->proto
= c
->data
.proto
;
2503 nlmsg_end(skb
, nlh
);
2505 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2508 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2512 l
+= nla_total_size(aead_len(x
->aead
));
2514 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2515 (x
->aalg
->alg_key_len
+ 7) / 8);
2516 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2519 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2521 l
+= nla_total_size(sizeof(*x
->calg
));
2523 l
+= nla_total_size(sizeof(*x
->encap
));
2525 l
+= nla_total_size(sizeof(x
->tfcpad
));
2527 l
+= nla_total_size(xfrm_replay_state_esn_len(x
->replay_esn
));
2529 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2530 x
->security
->ctx_len
);
2532 l
+= nla_total_size(sizeof(*x
->coaddr
));
2533 if (x
->props
.extra_flags
)
2534 l
+= nla_total_size(sizeof(x
->props
.extra_flags
));
2536 /* Must count x->lastused as it may become non-zero behind our back. */
2537 l
+= nla_total_size(sizeof(u64
));
2542 static int xfrm_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
2544 struct net
*net
= xs_net(x
);
2545 struct xfrm_usersa_info
*p
;
2546 struct xfrm_usersa_id
*id
;
2547 struct nlmsghdr
*nlh
;
2548 struct sk_buff
*skb
;
2549 int len
= xfrm_sa_len(x
);
2552 headlen
= sizeof(*p
);
2553 if (c
->event
== XFRM_MSG_DELSA
) {
2554 len
+= nla_total_size(headlen
);
2555 headlen
= sizeof(*id
);
2556 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2558 len
+= NLMSG_ALIGN(headlen
);
2560 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2564 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2569 p
= nlmsg_data(nlh
);
2570 if (c
->event
== XFRM_MSG_DELSA
) {
2571 struct nlattr
*attr
;
2573 id
= nlmsg_data(nlh
);
2574 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2575 id
->spi
= x
->id
.spi
;
2576 id
->family
= x
->props
.family
;
2577 id
->proto
= x
->id
.proto
;
2579 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2586 err
= copy_to_user_state_extra(x
, p
, skb
);
2590 nlmsg_end(skb
, nlh
);
2592 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2599 static int xfrm_send_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2603 case XFRM_MSG_EXPIRE
:
2604 return xfrm_exp_state_notify(x
, c
);
2605 case XFRM_MSG_NEWAE
:
2606 return xfrm_aevent_state_notify(x
, c
);
2607 case XFRM_MSG_DELSA
:
2608 case XFRM_MSG_UPDSA
:
2609 case XFRM_MSG_NEWSA
:
2610 return xfrm_notify_sa(x
, c
);
2611 case XFRM_MSG_FLUSHSA
:
2612 return xfrm_notify_sa_flush(c
);
2614 printk(KERN_NOTICE
"xfrm_user: Unknown SA event %d\n",
2623 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2624 struct xfrm_policy
*xp
)
2626 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2627 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2628 + nla_total_size(sizeof(struct xfrm_mark
))
2629 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2630 + userpolicy_type_attrsize();
2633 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2634 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
)
2636 __u32 seq
= xfrm_get_acqseq();
2637 struct xfrm_user_acquire
*ua
;
2638 struct nlmsghdr
*nlh
;
2641 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2645 ua
= nlmsg_data(nlh
);
2646 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2647 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2648 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2649 copy_to_user_policy(xp
, &ua
->policy
, XFRM_POLICY_OUT
);
2650 ua
->aalgos
= xt
->aalgos
;
2651 ua
->ealgos
= xt
->ealgos
;
2652 ua
->calgos
= xt
->calgos
;
2653 ua
->seq
= x
->km
.seq
= seq
;
2655 err
= copy_to_user_tmpl(xp
, skb
);
2657 err
= copy_to_user_state_sec_ctx(x
, skb
);
2659 err
= copy_to_user_policy_type(xp
->type
, skb
);
2661 err
= xfrm_mark_put(skb
, &xp
->mark
);
2663 nlmsg_cancel(skb
, nlh
);
2667 return nlmsg_end(skb
, nlh
);
2670 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2671 struct xfrm_policy
*xp
)
2673 struct net
*net
= xs_net(x
);
2674 struct sk_buff
*skb
;
2676 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2680 if (build_acquire(skb
, x
, xt
, xp
) < 0)
2683 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2686 /* User gives us xfrm_user_policy_info followed by an array of 0
2687 * or more templates.
2689 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2690 u8
*data
, int len
, int *dir
)
2692 struct net
*net
= sock_net(sk
);
2693 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2694 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2695 struct xfrm_policy
*xp
;
2698 switch (sk
->sk_family
) {
2700 if (opt
!= IP_XFRM_POLICY
) {
2705 #if IS_ENABLED(CONFIG_IPV6)
2707 if (opt
!= IPV6_XFRM_POLICY
) {
2720 if (len
< sizeof(*p
) ||
2721 verify_newpolicy_info(p
))
2724 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2725 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2728 if (p
->dir
> XFRM_POLICY_OUT
)
2731 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
2737 copy_from_user_policy(xp
, p
);
2738 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2739 copy_templates(xp
, ut
, nr
);
2746 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2748 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2749 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2750 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2751 + nla_total_size(sizeof(struct xfrm_mark
))
2752 + userpolicy_type_attrsize();
2755 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2756 int dir
, const struct km_event
*c
)
2758 struct xfrm_user_polexpire
*upe
;
2759 int hard
= c
->data
.hard
;
2760 struct nlmsghdr
*nlh
;
2763 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2767 upe
= nlmsg_data(nlh
);
2768 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2769 err
= copy_to_user_tmpl(xp
, skb
);
2771 err
= copy_to_user_sec_ctx(xp
, skb
);
2773 err
= copy_to_user_policy_type(xp
->type
, skb
);
2775 err
= xfrm_mark_put(skb
, &xp
->mark
);
2777 nlmsg_cancel(skb
, nlh
);
2782 return nlmsg_end(skb
, nlh
);
2785 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2787 struct net
*net
= xp_net(xp
);
2788 struct sk_buff
*skb
;
2790 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2794 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2797 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2800 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2802 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2803 struct net
*net
= xp_net(xp
);
2804 struct xfrm_userpolicy_info
*p
;
2805 struct xfrm_userpolicy_id
*id
;
2806 struct nlmsghdr
*nlh
;
2807 struct sk_buff
*skb
;
2810 headlen
= sizeof(*p
);
2811 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2812 len
+= nla_total_size(headlen
);
2813 headlen
= sizeof(*id
);
2815 len
+= userpolicy_type_attrsize();
2816 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2817 len
+= NLMSG_ALIGN(headlen
);
2819 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2823 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2828 p
= nlmsg_data(nlh
);
2829 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2830 struct nlattr
*attr
;
2832 id
= nlmsg_data(nlh
);
2833 memset(id
, 0, sizeof(*id
));
2836 id
->index
= xp
->index
;
2838 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2840 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2848 copy_to_user_policy(xp
, p
, dir
);
2849 err
= copy_to_user_tmpl(xp
, skb
);
2851 err
= copy_to_user_policy_type(xp
->type
, skb
);
2853 err
= xfrm_mark_put(skb
, &xp
->mark
);
2857 nlmsg_end(skb
, nlh
);
2859 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2866 static int xfrm_notify_policy_flush(const struct km_event
*c
)
2868 struct net
*net
= c
->net
;
2869 struct nlmsghdr
*nlh
;
2870 struct sk_buff
*skb
;
2873 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2877 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2881 err
= copy_to_user_policy_type(c
->data
.type
, skb
);
2885 nlmsg_end(skb
, nlh
);
2887 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2894 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2898 case XFRM_MSG_NEWPOLICY
:
2899 case XFRM_MSG_UPDPOLICY
:
2900 case XFRM_MSG_DELPOLICY
:
2901 return xfrm_notify_policy(xp
, dir
, c
);
2902 case XFRM_MSG_FLUSHPOLICY
:
2903 return xfrm_notify_policy_flush(c
);
2904 case XFRM_MSG_POLEXPIRE
:
2905 return xfrm_exp_policy_notify(xp
, dir
, c
);
2907 printk(KERN_NOTICE
"xfrm_user: Unknown Policy event %d\n",
2915 static inline size_t xfrm_report_msgsize(void)
2917 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2920 static int build_report(struct sk_buff
*skb
, u8 proto
,
2921 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2923 struct xfrm_user_report
*ur
;
2924 struct nlmsghdr
*nlh
;
2926 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2930 ur
= nlmsg_data(nlh
);
2932 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2935 int err
= nla_put(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2937 nlmsg_cancel(skb
, nlh
);
2941 return nlmsg_end(skb
, nlh
);
2944 static int xfrm_send_report(struct net
*net
, u8 proto
,
2945 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2947 struct sk_buff
*skb
;
2949 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2953 if (build_report(skb
, proto
, sel
, addr
) < 0)
2956 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2959 static inline size_t xfrm_mapping_msgsize(void)
2961 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
2964 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
2965 xfrm_address_t
*new_saddr
, __be16 new_sport
)
2967 struct xfrm_user_mapping
*um
;
2968 struct nlmsghdr
*nlh
;
2970 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
2974 um
= nlmsg_data(nlh
);
2976 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
2977 um
->id
.spi
= x
->id
.spi
;
2978 um
->id
.family
= x
->props
.family
;
2979 um
->id
.proto
= x
->id
.proto
;
2980 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
2981 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
2982 um
->new_sport
= new_sport
;
2983 um
->old_sport
= x
->encap
->encap_sport
;
2984 um
->reqid
= x
->props
.reqid
;
2986 return nlmsg_end(skb
, nlh
);
2989 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
2992 struct net
*net
= xs_net(x
);
2993 struct sk_buff
*skb
;
2995 if (x
->id
.proto
!= IPPROTO_ESP
)
3001 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
3005 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
3008 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MAPPING
, GFP_ATOMIC
);
3011 static bool xfrm_is_alive(const struct km_event
*c
)
3013 return (bool)xfrm_acquire_is_on(c
->net
);
3016 static struct xfrm_mgr netlink_mgr
= {
3018 .notify
= xfrm_send_state_notify
,
3019 .acquire
= xfrm_send_acquire
,
3020 .compile_policy
= xfrm_compile_policy
,
3021 .notify_policy
= xfrm_send_policy_notify
,
3022 .report
= xfrm_send_report
,
3023 .migrate
= xfrm_send_migrate
,
3024 .new_mapping
= xfrm_send_mapping
,
3025 .is_alive
= xfrm_is_alive
,
3028 static int __net_init
xfrm_user_net_init(struct net
*net
)
3031 struct netlink_kernel_cfg cfg
= {
3032 .groups
= XFRMNLGRP_MAX
,
3033 .input
= xfrm_netlink_rcv
,
3036 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, &cfg
);
3039 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
3040 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
3044 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
3047 list_for_each_entry(net
, net_exit_list
, exit_list
)
3048 RCU_INIT_POINTER(net
->xfrm
.nlsk
, NULL
);
3050 list_for_each_entry(net
, net_exit_list
, exit_list
)
3051 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
3054 static struct pernet_operations xfrm_user_net_ops
= {
3055 .init
= xfrm_user_net_init
,
3056 .exit_batch
= xfrm_user_net_exit
,
3059 static int __init
xfrm_user_init(void)
3063 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
3065 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
3068 rv
= xfrm_register_km(&netlink_mgr
);
3070 unregister_pernet_subsys(&xfrm_user_net_ops
);
3074 static void __exit
xfrm_user_exit(void)
3076 xfrm_unregister_km(&netlink_mgr
);
3077 unregister_pernet_subsys(&xfrm_user_net_ops
);
3080 module_init(xfrm_user_init
);
3081 module_exit(xfrm_user_exit
);
3082 MODULE_LICENSE("GPL");
3083 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);