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
];
127 if ((p
->flags
& XFRM_STATE_ESN
) && !rt
)
133 if (p
->id
.proto
!= IPPROTO_ESP
)
136 if (p
->replay_window
!= 0)
142 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
143 struct nlattr
**attrs
)
153 #if IS_ENABLED(CONFIG_IPV6)
165 switch (p
->id
.proto
) {
167 if ((!attrs
[XFRMA_ALG_AUTH
] &&
168 !attrs
[XFRMA_ALG_AUTH_TRUNC
]) ||
169 attrs
[XFRMA_ALG_AEAD
] ||
170 attrs
[XFRMA_ALG_CRYPT
] ||
171 attrs
[XFRMA_ALG_COMP
] ||
177 if (attrs
[XFRMA_ALG_COMP
])
179 if (!attrs
[XFRMA_ALG_AUTH
] &&
180 !attrs
[XFRMA_ALG_AUTH_TRUNC
] &&
181 !attrs
[XFRMA_ALG_CRYPT
] &&
182 !attrs
[XFRMA_ALG_AEAD
])
184 if ((attrs
[XFRMA_ALG_AUTH
] ||
185 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
186 attrs
[XFRMA_ALG_CRYPT
]) &&
187 attrs
[XFRMA_ALG_AEAD
])
189 if (attrs
[XFRMA_TFCPAD
] &&
190 p
->mode
!= XFRM_MODE_TUNNEL
)
195 if (!attrs
[XFRMA_ALG_COMP
] ||
196 attrs
[XFRMA_ALG_AEAD
] ||
197 attrs
[XFRMA_ALG_AUTH
] ||
198 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
199 attrs
[XFRMA_ALG_CRYPT
] ||
204 #if IS_ENABLED(CONFIG_IPV6)
205 case IPPROTO_DSTOPTS
:
206 case IPPROTO_ROUTING
:
207 if (attrs
[XFRMA_ALG_COMP
] ||
208 attrs
[XFRMA_ALG_AUTH
] ||
209 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
210 attrs
[XFRMA_ALG_AEAD
] ||
211 attrs
[XFRMA_ALG_CRYPT
] ||
212 attrs
[XFRMA_ENCAP
] ||
213 attrs
[XFRMA_SEC_CTX
] ||
214 attrs
[XFRMA_TFCPAD
] ||
215 !attrs
[XFRMA_COADDR
])
224 if ((err
= verify_aead(attrs
)))
226 if ((err
= verify_auth_trunc(attrs
)))
228 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
230 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
232 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
234 if ((err
= verify_sec_ctx_len(attrs
)))
236 if ((err
= verify_replay(p
, attrs
)))
241 case XFRM_MODE_TRANSPORT
:
242 case XFRM_MODE_TUNNEL
:
243 case XFRM_MODE_ROUTEOPTIMIZATION
:
257 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
258 struct xfrm_algo_desc
*(*get_byname
)(const char *, int),
261 struct xfrm_algo
*p
, *ualg
;
262 struct xfrm_algo_desc
*algo
;
267 ualg
= nla_data(rta
);
269 algo
= get_byname(ualg
->alg_name
, 1);
272 *props
= algo
->desc
.sadb_alg_id
;
274 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
278 strcpy(p
->alg_name
, algo
->name
);
283 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
286 struct xfrm_algo
*ualg
;
287 struct xfrm_algo_auth
*p
;
288 struct xfrm_algo_desc
*algo
;
293 ualg
= nla_data(rta
);
295 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
298 *props
= algo
->desc
.sadb_alg_id
;
300 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
304 strcpy(p
->alg_name
, algo
->name
);
305 p
->alg_key_len
= ualg
->alg_key_len
;
306 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
307 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
313 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
316 struct xfrm_algo_auth
*p
, *ualg
;
317 struct xfrm_algo_desc
*algo
;
322 ualg
= nla_data(rta
);
324 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
327 if ((ualg
->alg_trunc_len
/ 8) > MAX_AH_AUTH_LEN
||
328 ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
330 *props
= algo
->desc
.sadb_alg_id
;
332 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
336 strcpy(p
->alg_name
, algo
->name
);
337 if (!p
->alg_trunc_len
)
338 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
344 static int attach_aead(struct xfrm_algo_aead
**algpp
, u8
*props
,
347 struct xfrm_algo_aead
*p
, *ualg
;
348 struct xfrm_algo_desc
*algo
;
353 ualg
= nla_data(rta
);
355 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
358 *props
= algo
->desc
.sadb_alg_id
;
360 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
364 strcpy(p
->alg_name
, algo
->name
);
369 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn
*replay_esn
,
372 struct xfrm_replay_state_esn
*up
;
374 if (!replay_esn
|| !rp
)
379 if (xfrm_replay_state_esn_len(replay_esn
) !=
380 xfrm_replay_state_esn_len(up
))
386 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn
**replay_esn
,
387 struct xfrm_replay_state_esn
**preplay_esn
,
390 struct xfrm_replay_state_esn
*p
, *pp
, *up
;
397 p
= kmemdup(up
, xfrm_replay_state_esn_len(up
), GFP_KERNEL
);
401 pp
= kmemdup(up
, xfrm_replay_state_esn_len(up
), GFP_KERNEL
);
413 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
418 len
+= sizeof(struct xfrm_user_sec_ctx
);
419 len
+= xfrm_ctx
->ctx_len
;
424 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
426 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
427 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
428 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
429 x
->props
.mode
= p
->mode
;
430 x
->props
.replay_window
= p
->replay_window
;
431 x
->props
.reqid
= p
->reqid
;
432 x
->props
.family
= p
->family
;
433 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
434 x
->props
.flags
= p
->flags
;
436 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
437 x
->sel
.family
= p
->family
;
441 * someday when pfkey also has support, we could have the code
442 * somehow made shareable and move it to xfrm_state.c - JHS
445 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
)
447 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
448 struct nlattr
*re
= attrs
[XFRMA_REPLAY_ESN_VAL
];
449 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
450 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
451 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
454 struct xfrm_replay_state_esn
*replay_esn
;
455 replay_esn
= nla_data(re
);
456 memcpy(x
->replay_esn
, replay_esn
,
457 xfrm_replay_state_esn_len(replay_esn
));
458 memcpy(x
->preplay_esn
, replay_esn
,
459 xfrm_replay_state_esn_len(replay_esn
));
463 struct xfrm_replay_state
*replay
;
464 replay
= nla_data(rp
);
465 memcpy(&x
->replay
, replay
, sizeof(*replay
));
466 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
470 struct xfrm_lifetime_cur
*ltime
;
471 ltime
= nla_data(lt
);
472 x
->curlft
.bytes
= ltime
->bytes
;
473 x
->curlft
.packets
= ltime
->packets
;
474 x
->curlft
.add_time
= ltime
->add_time
;
475 x
->curlft
.use_time
= ltime
->use_time
;
479 x
->replay_maxage
= nla_get_u32(et
);
482 x
->replay_maxdiff
= nla_get_u32(rt
);
485 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
486 struct xfrm_usersa_info
*p
,
487 struct nlattr
**attrs
,
490 struct xfrm_state
*x
= xfrm_state_alloc(net
);
496 copy_from_user_state(x
, p
);
498 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
499 attrs
[XFRMA_ALG_AEAD
])))
501 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
502 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
504 if (!x
->props
.aalgo
) {
505 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
506 attrs
[XFRMA_ALG_AUTH
])))
509 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
510 xfrm_ealg_get_byname
,
511 attrs
[XFRMA_ALG_CRYPT
])))
513 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
514 xfrm_calg_get_byname
,
515 attrs
[XFRMA_ALG_COMP
])))
518 if (attrs
[XFRMA_ENCAP
]) {
519 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
520 sizeof(*x
->encap
), GFP_KERNEL
);
521 if (x
->encap
== NULL
)
525 if (attrs
[XFRMA_TFCPAD
])
526 x
->tfcpad
= nla_get_u32(attrs
[XFRMA_TFCPAD
]);
528 if (attrs
[XFRMA_COADDR
]) {
529 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
530 sizeof(*x
->coaddr
), GFP_KERNEL
);
531 if (x
->coaddr
== NULL
)
535 xfrm_mark_get(attrs
, &x
->mark
);
537 err
= __xfrm_init_state(x
, false);
541 if (attrs
[XFRMA_SEC_CTX
] &&
542 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
545 if ((err
= xfrm_alloc_replay_state_esn(&x
->replay_esn
, &x
->preplay_esn
,
546 attrs
[XFRMA_REPLAY_ESN_VAL
])))
550 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
551 /* sysctl_xfrm_aevent_etime is in 100ms units */
552 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
554 if ((err
= xfrm_init_replay(x
)))
557 /* override default values from above */
558 xfrm_update_ae_params(x
, attrs
);
563 x
->km
.state
= XFRM_STATE_DEAD
;
570 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
571 struct nlattr
**attrs
)
573 struct net
*net
= sock_net(skb
->sk
);
574 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
575 struct xfrm_state
*x
;
578 uid_t loginuid
= audit_get_loginuid(current
);
579 u32 sessionid
= audit_get_sessionid(current
);
582 err
= verify_newsa_info(p
, attrs
);
586 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
591 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
592 err
= xfrm_state_add(x
);
594 err
= xfrm_state_update(x
);
596 security_task_getsecid(current
, &sid
);
597 xfrm_audit_state_add(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
600 x
->km
.state
= XFRM_STATE_DEAD
;
605 c
.seq
= nlh
->nlmsg_seq
;
606 c
.pid
= nlh
->nlmsg_pid
;
607 c
.event
= nlh
->nlmsg_type
;
609 km_state_notify(x
, &c
);
615 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
616 struct xfrm_usersa_id
*p
,
617 struct nlattr
**attrs
,
620 struct xfrm_state
*x
= NULL
;
623 u32 mark
= xfrm_mark_get(attrs
, &m
);
625 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
627 x
= xfrm_state_lookup(net
, mark
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
629 xfrm_address_t
*saddr
= NULL
;
631 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
638 x
= xfrm_state_lookup_byaddr(net
, mark
,
640 p
->proto
, p
->family
);
649 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
650 struct nlattr
**attrs
)
652 struct net
*net
= sock_net(skb
->sk
);
653 struct xfrm_state
*x
;
656 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
657 uid_t loginuid
= audit_get_loginuid(current
);
658 u32 sessionid
= audit_get_sessionid(current
);
661 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
665 if ((err
= security_xfrm_state_delete(x
)) != 0)
668 if (xfrm_state_kern(x
)) {
673 err
= xfrm_state_delete(x
);
678 c
.seq
= nlh
->nlmsg_seq
;
679 c
.pid
= nlh
->nlmsg_pid
;
680 c
.event
= nlh
->nlmsg_type
;
681 km_state_notify(x
, &c
);
684 security_task_getsecid(current
, &sid
);
685 xfrm_audit_state_delete(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
690 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
692 memset(p
, 0, sizeof(*p
));
693 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
694 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
695 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
696 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
697 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
698 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
699 p
->mode
= x
->props
.mode
;
700 p
->replay_window
= x
->props
.replay_window
;
701 p
->reqid
= x
->props
.reqid
;
702 p
->family
= x
->props
.family
;
703 p
->flags
= x
->props
.flags
;
707 struct xfrm_dump_info
{
708 struct sk_buff
*in_skb
;
709 struct sk_buff
*out_skb
;
714 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
716 struct xfrm_user_sec_ctx
*uctx
;
718 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
720 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
724 uctx
= nla_data(attr
);
725 uctx
->exttype
= XFRMA_SEC_CTX
;
726 uctx
->len
= ctx_size
;
727 uctx
->ctx_doi
= s
->ctx_doi
;
728 uctx
->ctx_alg
= s
->ctx_alg
;
729 uctx
->ctx_len
= s
->ctx_len
;
730 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
735 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
737 struct xfrm_algo
*algo
;
740 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
741 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
745 algo
= nla_data(nla
);
746 strncpy(algo
->alg_name
, auth
->alg_name
, sizeof(algo
->alg_name
));
747 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
748 algo
->alg_key_len
= auth
->alg_key_len
;
753 /* Don't change this without updating xfrm_sa_len! */
754 static int copy_to_user_state_extra(struct xfrm_state
*x
,
755 struct xfrm_usersa_info
*p
,
760 copy_to_user_state(x
, p
);
763 ret
= nla_put(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
768 ret
= nla_put_u64(skb
, XFRMA_LASTUSED
, x
->lastused
);
773 ret
= nla_put(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
778 ret
= copy_to_user_auth(x
->aalg
, skb
);
780 ret
= nla_put(skb
, XFRMA_ALG_AUTH_TRUNC
,
781 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
786 ret
= nla_put(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
791 ret
= nla_put(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
796 ret
= nla_put(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
801 ret
= nla_put_u32(skb
, XFRMA_TFCPAD
, x
->tfcpad
);
805 ret
= xfrm_mark_put(skb
, &x
->mark
);
809 ret
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
810 xfrm_replay_state_esn_len(x
->replay_esn
),
816 ret
= copy_sec_ctx(x
->security
, skb
);
821 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
823 struct xfrm_dump_info
*sp
= ptr
;
824 struct sk_buff
*in_skb
= sp
->in_skb
;
825 struct sk_buff
*skb
= sp
->out_skb
;
826 struct xfrm_usersa_info
*p
;
827 struct nlmsghdr
*nlh
;
830 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
831 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
837 err
= copy_to_user_state_extra(x
, p
, skb
);
839 nlmsg_cancel(skb
, nlh
);
846 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
848 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
849 xfrm_state_walk_done(walk
);
853 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
855 struct net
*net
= sock_net(skb
->sk
);
856 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
857 struct xfrm_dump_info info
;
859 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
860 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
862 info
.in_skb
= cb
->skb
;
864 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
865 info
.nlmsg_flags
= NLM_F_MULTI
;
869 xfrm_state_walk_init(walk
, 0);
872 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
877 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
878 struct xfrm_state
*x
, u32 seq
)
880 struct xfrm_dump_info info
;
884 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
886 return ERR_PTR(-ENOMEM
);
888 info
.in_skb
= in_skb
;
890 info
.nlmsg_seq
= seq
;
891 info
.nlmsg_flags
= 0;
893 err
= dump_one_state(x
, 0, &info
);
902 static inline size_t xfrm_spdinfo_msgsize(void)
904 return NLMSG_ALIGN(4)
905 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
906 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
909 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
910 u32 pid
, u32 seq
, u32 flags
)
912 struct xfrmk_spdinfo si
;
913 struct xfrmu_spdinfo spc
;
914 struct xfrmu_spdhinfo sph
;
915 struct nlmsghdr
*nlh
;
919 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
920 if (nlh
== NULL
) /* shouldn't really happen ... */
925 xfrm_spd_getinfo(net
, &si
);
926 spc
.incnt
= si
.incnt
;
927 spc
.outcnt
= si
.outcnt
;
928 spc
.fwdcnt
= si
.fwdcnt
;
929 spc
.inscnt
= si
.inscnt
;
930 spc
.outscnt
= si
.outscnt
;
931 spc
.fwdscnt
= si
.fwdscnt
;
932 sph
.spdhcnt
= si
.spdhcnt
;
933 sph
.spdhmcnt
= si
.spdhmcnt
;
935 err
= nla_put(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
937 err
= nla_put(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
939 nlmsg_cancel(skb
, nlh
);
943 return nlmsg_end(skb
, nlh
);
946 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
947 struct nlattr
**attrs
)
949 struct net
*net
= sock_net(skb
->sk
);
950 struct sk_buff
*r_skb
;
951 u32
*flags
= nlmsg_data(nlh
);
952 u32 spid
= NETLINK_CB(skb
).pid
;
953 u32 seq
= nlh
->nlmsg_seq
;
955 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
959 if (build_spdinfo(r_skb
, net
, spid
, seq
, *flags
) < 0)
962 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, spid
);
965 static inline size_t xfrm_sadinfo_msgsize(void)
967 return NLMSG_ALIGN(4)
968 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
969 + nla_total_size(4); /* XFRMA_SAD_CNT */
972 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
973 u32 pid
, u32 seq
, u32 flags
)
975 struct xfrmk_sadinfo si
;
976 struct xfrmu_sadhinfo sh
;
977 struct nlmsghdr
*nlh
;
981 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
982 if (nlh
== NULL
) /* shouldn't really happen ... */
987 xfrm_sad_getinfo(net
, &si
);
989 sh
.sadhmcnt
= si
.sadhmcnt
;
990 sh
.sadhcnt
= si
.sadhcnt
;
992 err
= nla_put_u32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
994 err
= nla_put(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
996 nlmsg_cancel(skb
, nlh
);
1000 return nlmsg_end(skb
, nlh
);
1003 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1004 struct nlattr
**attrs
)
1006 struct net
*net
= sock_net(skb
->sk
);
1007 struct sk_buff
*r_skb
;
1008 u32
*flags
= nlmsg_data(nlh
);
1009 u32 spid
= NETLINK_CB(skb
).pid
;
1010 u32 seq
= nlh
->nlmsg_seq
;
1012 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
1016 if (build_sadinfo(r_skb
, net
, spid
, seq
, *flags
) < 0)
1019 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, spid
);
1022 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1023 struct nlattr
**attrs
)
1025 struct net
*net
= sock_net(skb
->sk
);
1026 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
1027 struct xfrm_state
*x
;
1028 struct sk_buff
*resp_skb
;
1031 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
1035 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1036 if (IS_ERR(resp_skb
)) {
1037 err
= PTR_ERR(resp_skb
);
1039 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).pid
);
1046 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
1048 switch (p
->info
.id
.proto
) {
1054 /* IPCOMP spi is 16-bits. */
1055 if (p
->max
>= 0x10000)
1063 if (p
->min
> p
->max
)
1069 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1070 struct nlattr
**attrs
)
1072 struct net
*net
= sock_net(skb
->sk
);
1073 struct xfrm_state
*x
;
1074 struct xfrm_userspi_info
*p
;
1075 struct sk_buff
*resp_skb
;
1076 xfrm_address_t
*daddr
;
1082 p
= nlmsg_data(nlh
);
1083 err
= verify_userspi_info(p
);
1087 family
= p
->info
.family
;
1088 daddr
= &p
->info
.id
.daddr
;
1092 mark
= xfrm_mark_get(attrs
, &m
);
1094 x
= xfrm_find_acq_byseq(net
, mark
, p
->info
.seq
);
1095 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
1102 x
= xfrm_find_acq(net
, &m
, p
->info
.mode
, p
->info
.reqid
,
1103 p
->info
.id
.proto
, daddr
,
1110 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
1114 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1115 if (IS_ERR(resp_skb
)) {
1116 err
= PTR_ERR(resp_skb
);
1120 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).pid
);
1128 static int verify_policy_dir(u8 dir
)
1131 case XFRM_POLICY_IN
:
1132 case XFRM_POLICY_OUT
:
1133 case XFRM_POLICY_FWD
:
1143 static int verify_policy_type(u8 type
)
1146 case XFRM_POLICY_TYPE_MAIN
:
1147 #ifdef CONFIG_XFRM_SUB_POLICY
1148 case XFRM_POLICY_TYPE_SUB
:
1159 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1162 case XFRM_SHARE_ANY
:
1163 case XFRM_SHARE_SESSION
:
1164 case XFRM_SHARE_USER
:
1165 case XFRM_SHARE_UNIQUE
:
1172 switch (p
->action
) {
1173 case XFRM_POLICY_ALLOW
:
1174 case XFRM_POLICY_BLOCK
:
1181 switch (p
->sel
.family
) {
1186 #if IS_ENABLED(CONFIG_IPV6)
1189 return -EAFNOSUPPORT
;
1196 return verify_policy_dir(p
->dir
);
1199 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1201 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1202 struct xfrm_user_sec_ctx
*uctx
;
1207 uctx
= nla_data(rt
);
1208 return security_xfrm_policy_alloc(&pol
->security
, uctx
);
1211 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1217 for (i
= 0; i
< nr
; i
++, ut
++) {
1218 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1220 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1221 memcpy(&t
->saddr
, &ut
->saddr
,
1222 sizeof(xfrm_address_t
));
1223 t
->reqid
= ut
->reqid
;
1225 t
->share
= ut
->share
;
1226 t
->optional
= ut
->optional
;
1227 t
->aalgos
= ut
->aalgos
;
1228 t
->ealgos
= ut
->ealgos
;
1229 t
->calgos
= ut
->calgos
;
1230 /* If all masks are ~0, then we allow all algorithms. */
1231 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1232 t
->encap_family
= ut
->family
;
1236 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1240 if (nr
> XFRM_MAX_DEPTH
)
1243 for (i
= 0; i
< nr
; i
++) {
1244 /* We never validated the ut->family value, so many
1245 * applications simply leave it at zero. The check was
1246 * never made and ut->family was ignored because all
1247 * templates could be assumed to have the same family as
1248 * the policy itself. Now that we will have ipv4-in-ipv6
1249 * and ipv6-in-ipv4 tunnels, this is no longer true.
1252 ut
[i
].family
= family
;
1254 switch (ut
[i
].family
) {
1257 #if IS_ENABLED(CONFIG_IPV6)
1269 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1271 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1276 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1277 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1280 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1284 copy_templates(pol
, utmpl
, nr
);
1289 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1291 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1292 struct xfrm_userpolicy_type
*upt
;
1293 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1301 err
= verify_policy_type(type
);
1309 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1311 xp
->priority
= p
->priority
;
1312 xp
->index
= p
->index
;
1313 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1314 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1315 xp
->action
= p
->action
;
1316 xp
->flags
= p
->flags
;
1317 xp
->family
= p
->sel
.family
;
1318 /* XXX xp->share = p->share; */
1321 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1323 memset(p
, 0, sizeof(*p
));
1324 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1325 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1326 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1327 p
->priority
= xp
->priority
;
1328 p
->index
= xp
->index
;
1329 p
->sel
.family
= xp
->family
;
1331 p
->action
= xp
->action
;
1332 p
->flags
= xp
->flags
;
1333 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1336 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1338 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1346 copy_from_user_policy(xp
, p
);
1348 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1352 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1353 err
= copy_from_user_sec_ctx(xp
, attrs
);
1357 xfrm_mark_get(attrs
, &xp
->mark
);
1363 xfrm_policy_destroy(xp
);
1367 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1368 struct nlattr
**attrs
)
1370 struct net
*net
= sock_net(skb
->sk
);
1371 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1372 struct xfrm_policy
*xp
;
1376 uid_t loginuid
= audit_get_loginuid(current
);
1377 u32 sessionid
= audit_get_sessionid(current
);
1380 err
= verify_newpolicy_info(p
);
1383 err
= verify_sec_ctx_len(attrs
);
1387 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1391 /* shouldn't excl be based on nlh flags??
1392 * Aha! this is anti-netlink really i.e more pfkey derived
1393 * in netlink excl is a flag and you wouldnt need
1394 * a type XFRM_MSG_UPDPOLICY - JHS */
1395 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1396 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1397 security_task_getsecid(current
, &sid
);
1398 xfrm_audit_policy_add(xp
, err
? 0 : 1, loginuid
, sessionid
, sid
);
1401 security_xfrm_policy_free(xp
->security
);
1406 c
.event
= nlh
->nlmsg_type
;
1407 c
.seq
= nlh
->nlmsg_seq
;
1408 c
.pid
= nlh
->nlmsg_pid
;
1409 km_policy_notify(xp
, p
->dir
, &c
);
1416 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1418 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1421 if (xp
->xfrm_nr
== 0)
1424 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1425 struct xfrm_user_tmpl
*up
= &vec
[i
];
1426 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1428 memset(up
, 0, sizeof(*up
));
1429 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1430 up
->family
= kp
->encap_family
;
1431 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1432 up
->reqid
= kp
->reqid
;
1433 up
->mode
= kp
->mode
;
1434 up
->share
= kp
->share
;
1435 up
->optional
= kp
->optional
;
1436 up
->aalgos
= kp
->aalgos
;
1437 up
->ealgos
= kp
->ealgos
;
1438 up
->calgos
= kp
->calgos
;
1441 return nla_put(skb
, XFRMA_TMPL
,
1442 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1445 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1448 return copy_sec_ctx(x
->security
, skb
);
1453 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1456 return copy_sec_ctx(xp
->security
, skb
);
1459 static inline size_t userpolicy_type_attrsize(void)
1461 #ifdef CONFIG_XFRM_SUB_POLICY
1462 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1468 #ifdef CONFIG_XFRM_SUB_POLICY
1469 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1471 struct xfrm_userpolicy_type upt
= {
1475 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1479 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1485 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1487 struct xfrm_dump_info
*sp
= ptr
;
1488 struct xfrm_userpolicy_info
*p
;
1489 struct sk_buff
*in_skb
= sp
->in_skb
;
1490 struct sk_buff
*skb
= sp
->out_skb
;
1491 struct nlmsghdr
*nlh
;
1494 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1495 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1499 p
= nlmsg_data(nlh
);
1500 copy_to_user_policy(xp
, p
, dir
);
1501 err
= copy_to_user_tmpl(xp
, skb
);
1503 err
= copy_to_user_sec_ctx(xp
, skb
);
1505 err
= copy_to_user_policy_type(xp
->type
, skb
);
1507 err
= xfrm_mark_put(skb
, &xp
->mark
);
1509 nlmsg_cancel(skb
, nlh
);
1512 nlmsg_end(skb
, nlh
);
1516 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1518 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1520 xfrm_policy_walk_done(walk
);
1524 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1526 struct net
*net
= sock_net(skb
->sk
);
1527 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1528 struct xfrm_dump_info info
;
1530 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1531 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1533 info
.in_skb
= cb
->skb
;
1535 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1536 info
.nlmsg_flags
= NLM_F_MULTI
;
1540 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1543 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1548 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1549 struct xfrm_policy
*xp
,
1552 struct xfrm_dump_info info
;
1553 struct sk_buff
*skb
;
1556 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1558 return ERR_PTR(-ENOMEM
);
1560 info
.in_skb
= in_skb
;
1562 info
.nlmsg_seq
= seq
;
1563 info
.nlmsg_flags
= 0;
1565 err
= dump_one_policy(xp
, dir
, 0, &info
);
1568 return ERR_PTR(err
);
1574 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1575 struct nlattr
**attrs
)
1577 struct net
*net
= sock_net(skb
->sk
);
1578 struct xfrm_policy
*xp
;
1579 struct xfrm_userpolicy_id
*p
;
1580 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1585 u32 mark
= xfrm_mark_get(attrs
, &m
);
1587 p
= nlmsg_data(nlh
);
1588 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1590 err
= copy_from_user_policy_type(&type
, attrs
);
1594 err
= verify_policy_dir(p
->dir
);
1599 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, delete, &err
);
1601 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1602 struct xfrm_sec_ctx
*ctx
;
1604 err
= verify_sec_ctx_len(attrs
);
1610 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1612 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1616 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
, &p
->sel
,
1618 security_xfrm_policy_free(ctx
);
1624 struct sk_buff
*resp_skb
;
1626 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1627 if (IS_ERR(resp_skb
)) {
1628 err
= PTR_ERR(resp_skb
);
1630 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1631 NETLINK_CB(skb
).pid
);
1634 uid_t loginuid
= audit_get_loginuid(current
);
1635 u32 sessionid
= audit_get_sessionid(current
);
1638 security_task_getsecid(current
, &sid
);
1639 xfrm_audit_policy_delete(xp
, err
? 0 : 1, loginuid
, sessionid
,
1645 c
.data
.byid
= p
->index
;
1646 c
.event
= nlh
->nlmsg_type
;
1647 c
.seq
= nlh
->nlmsg_seq
;
1648 c
.pid
= nlh
->nlmsg_pid
;
1649 km_policy_notify(xp
, p
->dir
, &c
);
1657 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1658 struct nlattr
**attrs
)
1660 struct net
*net
= sock_net(skb
->sk
);
1662 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1663 struct xfrm_audit audit_info
;
1666 audit_info
.loginuid
= audit_get_loginuid(current
);
1667 audit_info
.sessionid
= audit_get_sessionid(current
);
1668 security_task_getsecid(current
, &audit_info
.secid
);
1669 err
= xfrm_state_flush(net
, p
->proto
, &audit_info
);
1671 if (err
== -ESRCH
) /* empty table */
1675 c
.data
.proto
= p
->proto
;
1676 c
.event
= nlh
->nlmsg_type
;
1677 c
.seq
= nlh
->nlmsg_seq
;
1678 c
.pid
= nlh
->nlmsg_pid
;
1680 km_state_notify(NULL
, &c
);
1685 static inline size_t xfrm_aevent_msgsize(struct xfrm_state
*x
)
1687 size_t replay_size
= x
->replay_esn
?
1688 xfrm_replay_state_esn_len(x
->replay_esn
) :
1689 sizeof(struct xfrm_replay_state
);
1691 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1692 + nla_total_size(replay_size
)
1693 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1694 + nla_total_size(sizeof(struct xfrm_mark
))
1695 + nla_total_size(4) /* XFRM_AE_RTHR */
1696 + nla_total_size(4); /* XFRM_AE_ETHR */
1699 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
1701 struct xfrm_aevent_id
*id
;
1702 struct nlmsghdr
*nlh
;
1705 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1709 id
= nlmsg_data(nlh
);
1710 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1711 id
->sa_id
.spi
= x
->id
.spi
;
1712 id
->sa_id
.family
= x
->props
.family
;
1713 id
->sa_id
.proto
= x
->id
.proto
;
1714 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1715 id
->reqid
= x
->props
.reqid
;
1716 id
->flags
= c
->data
.aevent
;
1718 if (x
->replay_esn
) {
1719 err
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
1720 xfrm_replay_state_esn_len(x
->replay_esn
),
1723 err
= nla_put(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
),
1728 err
= nla_put(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1732 if (id
->flags
& XFRM_AE_RTHR
) {
1733 err
= nla_put_u32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1737 if (id
->flags
& XFRM_AE_ETHR
) {
1738 err
= nla_put_u32(skb
, XFRMA_ETIMER_THRESH
,
1739 x
->replay_maxage
* 10 / HZ
);
1743 err
= xfrm_mark_put(skb
, &x
->mark
);
1747 return nlmsg_end(skb
, nlh
);
1750 nlmsg_cancel(skb
, nlh
);
1754 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1755 struct nlattr
**attrs
)
1757 struct net
*net
= sock_net(skb
->sk
);
1758 struct xfrm_state
*x
;
1759 struct sk_buff
*r_skb
;
1764 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1765 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1767 mark
= xfrm_mark_get(attrs
, &m
);
1769 x
= xfrm_state_lookup(net
, mark
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1773 r_skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
1774 if (r_skb
== NULL
) {
1780 * XXX: is this lock really needed - none of the other
1781 * gets lock (the concern is things getting updated
1782 * while we are still reading) - jhs
1784 spin_lock_bh(&x
->lock
);
1785 c
.data
.aevent
= p
->flags
;
1786 c
.seq
= nlh
->nlmsg_seq
;
1787 c
.pid
= nlh
->nlmsg_pid
;
1789 if (build_aevent(r_skb
, x
, &c
) < 0)
1791 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).pid
);
1792 spin_unlock_bh(&x
->lock
);
1797 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1798 struct nlattr
**attrs
)
1800 struct net
*net
= sock_net(skb
->sk
);
1801 struct xfrm_state
*x
;
1806 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1807 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1808 struct nlattr
*re
= attrs
[XFRMA_REPLAY_ESN_VAL
];
1809 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1811 if (!lt
&& !rp
&& !re
)
1814 /* pedantic mode - thou shalt sayeth replaceth */
1815 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1818 mark
= xfrm_mark_get(attrs
, &m
);
1820 x
= xfrm_state_lookup(net
, mark
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1824 if (x
->km
.state
!= XFRM_STATE_VALID
)
1827 err
= xfrm_replay_verify_len(x
->replay_esn
, rp
);
1831 spin_lock_bh(&x
->lock
);
1832 xfrm_update_ae_params(x
, attrs
);
1833 spin_unlock_bh(&x
->lock
);
1835 c
.event
= nlh
->nlmsg_type
;
1836 c
.seq
= nlh
->nlmsg_seq
;
1837 c
.pid
= nlh
->nlmsg_pid
;
1838 c
.data
.aevent
= XFRM_AE_CU
;
1839 km_state_notify(x
, &c
);
1846 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1847 struct nlattr
**attrs
)
1849 struct net
*net
= sock_net(skb
->sk
);
1851 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1853 struct xfrm_audit audit_info
;
1855 err
= copy_from_user_policy_type(&type
, attrs
);
1859 audit_info
.loginuid
= audit_get_loginuid(current
);
1860 audit_info
.sessionid
= audit_get_sessionid(current
);
1861 security_task_getsecid(current
, &audit_info
.secid
);
1862 err
= xfrm_policy_flush(net
, type
, &audit_info
);
1864 if (err
== -ESRCH
) /* empty table */
1870 c
.event
= nlh
->nlmsg_type
;
1871 c
.seq
= nlh
->nlmsg_seq
;
1872 c
.pid
= nlh
->nlmsg_pid
;
1874 km_policy_notify(NULL
, 0, &c
);
1878 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1879 struct nlattr
**attrs
)
1881 struct net
*net
= sock_net(skb
->sk
);
1882 struct xfrm_policy
*xp
;
1883 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1884 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1885 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1888 u32 mark
= xfrm_mark_get(attrs
, &m
);
1890 err
= copy_from_user_policy_type(&type
, attrs
);
1894 err
= verify_policy_dir(p
->dir
);
1899 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, 0, &err
);
1901 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1902 struct xfrm_sec_ctx
*ctx
;
1904 err
= verify_sec_ctx_len(attrs
);
1910 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1912 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1916 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
,
1917 &p
->sel
, ctx
, 0, &err
);
1918 security_xfrm_policy_free(ctx
);
1923 if (unlikely(xp
->walk
.dead
))
1928 uid_t loginuid
= audit_get_loginuid(current
);
1929 u32 sessionid
= audit_get_sessionid(current
);
1932 security_task_getsecid(current
, &sid
);
1933 xfrm_policy_delete(xp
, p
->dir
);
1934 xfrm_audit_policy_delete(xp
, 1, loginuid
, sessionid
, sid
);
1937 // reset the timers here?
1938 WARN(1, "Dont know what to do with soft policy expire\n");
1940 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1947 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1948 struct nlattr
**attrs
)
1950 struct net
*net
= sock_net(skb
->sk
);
1951 struct xfrm_state
*x
;
1953 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1954 struct xfrm_usersa_info
*p
= &ue
->state
;
1956 u32 mark
= xfrm_mark_get(attrs
, &m
);
1958 x
= xfrm_state_lookup(net
, mark
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1964 spin_lock_bh(&x
->lock
);
1966 if (x
->km
.state
!= XFRM_STATE_VALID
)
1968 km_state_expired(x
, ue
->hard
, current
->pid
);
1971 uid_t loginuid
= audit_get_loginuid(current
);
1972 u32 sessionid
= audit_get_sessionid(current
);
1975 security_task_getsecid(current
, &sid
);
1976 __xfrm_state_delete(x
);
1977 xfrm_audit_state_delete(x
, 1, loginuid
, sessionid
, sid
);
1981 spin_unlock_bh(&x
->lock
);
1986 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1987 struct nlattr
**attrs
)
1989 struct net
*net
= sock_net(skb
->sk
);
1990 struct xfrm_policy
*xp
;
1991 struct xfrm_user_tmpl
*ut
;
1993 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1994 struct xfrm_mark mark
;
1996 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1997 struct xfrm_state
*x
= xfrm_state_alloc(net
);
2003 xfrm_mark_get(attrs
, &mark
);
2005 err
= verify_newpolicy_info(&ua
->policy
);
2010 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
2014 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
2015 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
2016 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
2017 xp
->mark
.m
= x
->mark
.m
= mark
.m
;
2018 xp
->mark
.v
= x
->mark
.v
= mark
.v
;
2020 /* extract the templates and for each call km_key */
2021 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
2022 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
2023 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
2024 x
->props
.mode
= t
->mode
;
2025 x
->props
.reqid
= t
->reqid
;
2026 x
->props
.family
= ut
->family
;
2027 t
->aalgos
= ua
->aalgos
;
2028 t
->ealgos
= ua
->ealgos
;
2029 t
->calgos
= ua
->calgos
;
2030 err
= km_query(x
, t
, xp
);
2040 WARN(1, "BAD policy passed\n");
2047 #ifdef CONFIG_XFRM_MIGRATE
2048 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
2049 struct xfrm_kmaddress
*k
,
2050 struct nlattr
**attrs
, int *num
)
2052 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
2053 struct xfrm_user_migrate
*um
;
2057 struct xfrm_user_kmaddress
*uk
;
2059 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
2060 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
2061 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
2062 k
->family
= uk
->family
;
2063 k
->reserved
= uk
->reserved
;
2067 num_migrate
= nla_len(rt
) / sizeof(*um
);
2069 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
2072 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
2073 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
2074 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
2075 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
2076 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
2078 ma
->proto
= um
->proto
;
2079 ma
->mode
= um
->mode
;
2080 ma
->reqid
= um
->reqid
;
2082 ma
->old_family
= um
->old_family
;
2083 ma
->new_family
= um
->new_family
;
2090 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2091 struct nlattr
**attrs
)
2093 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
2094 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2095 struct xfrm_kmaddress km
, *kmp
;
2100 if (attrs
[XFRMA_MIGRATE
] == NULL
)
2103 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
2105 err
= copy_from_user_policy_type(&type
, attrs
);
2109 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
2116 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
);
2121 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2122 struct nlattr
**attrs
)
2124 return -ENOPROTOOPT
;
2128 #ifdef CONFIG_XFRM_MIGRATE
2129 static int copy_to_user_migrate(const struct xfrm_migrate
*m
, struct sk_buff
*skb
)
2131 struct xfrm_user_migrate um
;
2133 memset(&um
, 0, sizeof(um
));
2134 um
.proto
= m
->proto
;
2136 um
.reqid
= m
->reqid
;
2137 um
.old_family
= m
->old_family
;
2138 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
2139 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
2140 um
.new_family
= m
->new_family
;
2141 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
2142 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
2144 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
2147 static int copy_to_user_kmaddress(const struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
2149 struct xfrm_user_kmaddress uk
;
2151 memset(&uk
, 0, sizeof(uk
));
2152 uk
.family
= k
->family
;
2153 uk
.reserved
= k
->reserved
;
2154 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
2155 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
2157 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
2160 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
)
2162 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
2163 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
2164 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
2165 + userpolicy_type_attrsize();
2168 static int build_migrate(struct sk_buff
*skb
, const struct xfrm_migrate
*m
,
2169 int num_migrate
, const struct xfrm_kmaddress
*k
,
2170 const struct xfrm_selector
*sel
, u8 dir
, u8 type
)
2172 const struct xfrm_migrate
*mp
;
2173 struct xfrm_userpolicy_id
*pol_id
;
2174 struct nlmsghdr
*nlh
;
2177 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
2181 pol_id
= nlmsg_data(nlh
);
2182 /* copy data from selector, dir, and type to the pol_id */
2183 memset(pol_id
, 0, sizeof(*pol_id
));
2184 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
2188 err
= copy_to_user_kmaddress(k
, skb
);
2192 err
= copy_to_user_policy_type(type
, skb
);
2195 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2196 err
= copy_to_user_migrate(mp
, skb
);
2201 return nlmsg_end(skb
, nlh
);
2204 nlmsg_cancel(skb
, nlh
);
2208 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2209 const struct xfrm_migrate
*m
, int num_migrate
,
2210 const struct xfrm_kmaddress
*k
)
2212 struct net
*net
= &init_net
;
2213 struct sk_buff
*skb
;
2215 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
), GFP_ATOMIC
);
2220 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, dir
, type
) < 0)
2223 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
2226 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2227 const struct xfrm_migrate
*m
, int num_migrate
,
2228 const struct xfrm_kmaddress
*k
)
2230 return -ENOPROTOOPT
;
2234 #define XMSGSIZE(type) sizeof(struct type)
2236 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2237 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2238 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2239 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2240 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2241 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2242 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2243 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2244 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2245 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2246 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2247 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2248 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2249 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2250 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2251 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2252 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2253 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2254 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2255 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2256 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2261 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2262 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2263 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2264 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2265 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2266 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2267 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2268 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2269 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2270 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2271 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2272 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2273 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2274 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2275 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2276 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2277 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2278 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2279 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2280 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2281 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2282 [XFRMA_MARK
] = { .len
= sizeof(struct xfrm_mark
) },
2283 [XFRMA_TFCPAD
] = { .type
= NLA_U32
},
2284 [XFRMA_REPLAY_ESN_VAL
] = { .len
= sizeof(struct xfrm_replay_state_esn
) },
2287 static struct xfrm_link
{
2288 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2289 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2290 int (*done
)(struct netlink_callback
*);
2291 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2292 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2293 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2294 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2295 .dump
= xfrm_dump_sa
,
2296 .done
= xfrm_dump_sa_done
},
2297 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2298 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2299 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2300 .dump
= xfrm_dump_policy
,
2301 .done
= xfrm_dump_policy_done
},
2302 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2303 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2304 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2305 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2306 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2307 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2308 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2309 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2310 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2311 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2312 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2313 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2314 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2317 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2319 struct net
*net
= sock_net(skb
->sk
);
2320 struct nlattr
*attrs
[XFRMA_MAX
+1];
2321 struct xfrm_link
*link
;
2324 type
= nlh
->nlmsg_type
;
2325 if (type
> XFRM_MSG_MAX
)
2328 type
-= XFRM_MSG_BASE
;
2329 link
= &xfrm_dispatch
[type
];
2331 /* All operations require privileges, even GET */
2332 if (!capable(CAP_NET_ADMIN
))
2335 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2336 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2337 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2338 if (link
->dump
== NULL
)
2342 struct netlink_dump_control c
= {
2346 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, &c
);
2350 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
2355 if (link
->doit
== NULL
)
2358 return link
->doit(skb
, nlh
, attrs
);
2361 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2363 mutex_lock(&xfrm_cfg_mutex
);
2364 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2365 mutex_unlock(&xfrm_cfg_mutex
);
2368 static inline size_t xfrm_expire_msgsize(void)
2370 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
))
2371 + nla_total_size(sizeof(struct xfrm_mark
));
2374 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
2376 struct xfrm_user_expire
*ue
;
2377 struct nlmsghdr
*nlh
;
2380 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2384 ue
= nlmsg_data(nlh
);
2385 copy_to_user_state(x
, &ue
->state
);
2386 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2388 err
= xfrm_mark_put(skb
, &x
->mark
);
2392 return nlmsg_end(skb
, nlh
);
2395 static int xfrm_exp_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2397 struct net
*net
= xs_net(x
);
2398 struct sk_buff
*skb
;
2400 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2404 if (build_expire(skb
, x
, c
) < 0) {
2409 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2412 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2414 struct net
*net
= xs_net(x
);
2415 struct sk_buff
*skb
;
2417 skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
2421 if (build_aevent(skb
, x
, c
) < 0)
2424 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
2427 static int xfrm_notify_sa_flush(const struct km_event
*c
)
2429 struct net
*net
= c
->net
;
2430 struct xfrm_usersa_flush
*p
;
2431 struct nlmsghdr
*nlh
;
2432 struct sk_buff
*skb
;
2433 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2435 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2439 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2445 p
= nlmsg_data(nlh
);
2446 p
->proto
= c
->data
.proto
;
2448 nlmsg_end(skb
, nlh
);
2450 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2453 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2457 l
+= nla_total_size(aead_len(x
->aead
));
2459 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2460 (x
->aalg
->alg_key_len
+ 7) / 8);
2461 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2464 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2466 l
+= nla_total_size(sizeof(*x
->calg
));
2468 l
+= nla_total_size(sizeof(*x
->encap
));
2470 l
+= nla_total_size(sizeof(x
->tfcpad
));
2472 l
+= nla_total_size(xfrm_replay_state_esn_len(x
->replay_esn
));
2474 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2475 x
->security
->ctx_len
);
2477 l
+= nla_total_size(sizeof(*x
->coaddr
));
2479 /* Must count x->lastused as it may become non-zero behind our back. */
2480 l
+= nla_total_size(sizeof(u64
));
2485 static int xfrm_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
2487 struct net
*net
= xs_net(x
);
2488 struct xfrm_usersa_info
*p
;
2489 struct xfrm_usersa_id
*id
;
2490 struct nlmsghdr
*nlh
;
2491 struct sk_buff
*skb
;
2492 int len
= xfrm_sa_len(x
);
2495 headlen
= sizeof(*p
);
2496 if (c
->event
== XFRM_MSG_DELSA
) {
2497 len
+= nla_total_size(headlen
);
2498 headlen
= sizeof(*id
);
2499 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2501 len
+= NLMSG_ALIGN(headlen
);
2503 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2507 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2512 p
= nlmsg_data(nlh
);
2513 if (c
->event
== XFRM_MSG_DELSA
) {
2514 struct nlattr
*attr
;
2516 id
= nlmsg_data(nlh
);
2517 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2518 id
->spi
= x
->id
.spi
;
2519 id
->family
= x
->props
.family
;
2520 id
->proto
= x
->id
.proto
;
2522 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2529 err
= copy_to_user_state_extra(x
, p
, skb
);
2533 nlmsg_end(skb
, nlh
);
2535 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2542 static int xfrm_send_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2546 case XFRM_MSG_EXPIRE
:
2547 return xfrm_exp_state_notify(x
, c
);
2548 case XFRM_MSG_NEWAE
:
2549 return xfrm_aevent_state_notify(x
, c
);
2550 case XFRM_MSG_DELSA
:
2551 case XFRM_MSG_UPDSA
:
2552 case XFRM_MSG_NEWSA
:
2553 return xfrm_notify_sa(x
, c
);
2554 case XFRM_MSG_FLUSHSA
:
2555 return xfrm_notify_sa_flush(c
);
2557 printk(KERN_NOTICE
"xfrm_user: Unknown SA event %d\n",
2566 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2567 struct xfrm_policy
*xp
)
2569 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2570 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2571 + nla_total_size(sizeof(struct xfrm_mark
))
2572 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2573 + userpolicy_type_attrsize();
2576 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2577 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2580 __u32 seq
= xfrm_get_acqseq();
2581 struct xfrm_user_acquire
*ua
;
2582 struct nlmsghdr
*nlh
;
2585 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2589 ua
= nlmsg_data(nlh
);
2590 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2591 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2592 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2593 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2594 ua
->aalgos
= xt
->aalgos
;
2595 ua
->ealgos
= xt
->ealgos
;
2596 ua
->calgos
= xt
->calgos
;
2597 ua
->seq
= x
->km
.seq
= seq
;
2599 err
= copy_to_user_tmpl(xp
, skb
);
2601 err
= copy_to_user_state_sec_ctx(x
, skb
);
2603 err
= copy_to_user_policy_type(xp
->type
, skb
);
2605 err
= xfrm_mark_put(skb
, &xp
->mark
);
2607 nlmsg_cancel(skb
, nlh
);
2611 return nlmsg_end(skb
, nlh
);
2614 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2615 struct xfrm_policy
*xp
, int dir
)
2617 struct net
*net
= xs_net(x
);
2618 struct sk_buff
*skb
;
2620 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2624 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2627 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2630 /* User gives us xfrm_user_policy_info followed by an array of 0
2631 * or more templates.
2633 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2634 u8
*data
, int len
, int *dir
)
2636 struct net
*net
= sock_net(sk
);
2637 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2638 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2639 struct xfrm_policy
*xp
;
2642 switch (sk
->sk_family
) {
2644 if (opt
!= IP_XFRM_POLICY
) {
2649 #if IS_ENABLED(CONFIG_IPV6)
2651 if (opt
!= IPV6_XFRM_POLICY
) {
2664 if (len
< sizeof(*p
) ||
2665 verify_newpolicy_info(p
))
2668 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2669 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2672 if (p
->dir
> XFRM_POLICY_OUT
)
2675 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
2681 copy_from_user_policy(xp
, p
);
2682 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2683 copy_templates(xp
, ut
, nr
);
2690 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2692 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2693 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2694 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2695 + nla_total_size(sizeof(struct xfrm_mark
))
2696 + userpolicy_type_attrsize();
2699 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2700 int dir
, const struct km_event
*c
)
2702 struct xfrm_user_polexpire
*upe
;
2703 int hard
= c
->data
.hard
;
2704 struct nlmsghdr
*nlh
;
2707 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2711 upe
= nlmsg_data(nlh
);
2712 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2713 err
= copy_to_user_tmpl(xp
, skb
);
2715 err
= copy_to_user_sec_ctx(xp
, skb
);
2717 err
= copy_to_user_policy_type(xp
->type
, skb
);
2719 err
= xfrm_mark_put(skb
, &xp
->mark
);
2721 nlmsg_cancel(skb
, nlh
);
2726 return nlmsg_end(skb
, nlh
);
2729 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2731 struct net
*net
= xp_net(xp
);
2732 struct sk_buff
*skb
;
2734 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2738 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2741 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2744 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2746 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2747 struct net
*net
= xp_net(xp
);
2748 struct xfrm_userpolicy_info
*p
;
2749 struct xfrm_userpolicy_id
*id
;
2750 struct nlmsghdr
*nlh
;
2751 struct sk_buff
*skb
;
2754 headlen
= sizeof(*p
);
2755 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2756 len
+= nla_total_size(headlen
);
2757 headlen
= sizeof(*id
);
2759 len
+= userpolicy_type_attrsize();
2760 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2761 len
+= NLMSG_ALIGN(headlen
);
2763 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2767 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2772 p
= nlmsg_data(nlh
);
2773 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2774 struct nlattr
*attr
;
2776 id
= nlmsg_data(nlh
);
2777 memset(id
, 0, sizeof(*id
));
2780 id
->index
= xp
->index
;
2782 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2784 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2792 copy_to_user_policy(xp
, p
, dir
);
2793 err
= copy_to_user_tmpl(xp
, skb
);
2795 err
= copy_to_user_policy_type(xp
->type
, skb
);
2797 err
= xfrm_mark_put(skb
, &xp
->mark
);
2801 nlmsg_end(skb
, nlh
);
2803 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2810 static int xfrm_notify_policy_flush(const struct km_event
*c
)
2812 struct net
*net
= c
->net
;
2813 struct nlmsghdr
*nlh
;
2814 struct sk_buff
*skb
;
2817 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2821 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2825 err
= copy_to_user_policy_type(c
->data
.type
, skb
);
2829 nlmsg_end(skb
, nlh
);
2831 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2838 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2842 case XFRM_MSG_NEWPOLICY
:
2843 case XFRM_MSG_UPDPOLICY
:
2844 case XFRM_MSG_DELPOLICY
:
2845 return xfrm_notify_policy(xp
, dir
, c
);
2846 case XFRM_MSG_FLUSHPOLICY
:
2847 return xfrm_notify_policy_flush(c
);
2848 case XFRM_MSG_POLEXPIRE
:
2849 return xfrm_exp_policy_notify(xp
, dir
, c
);
2851 printk(KERN_NOTICE
"xfrm_user: Unknown Policy event %d\n",
2859 static inline size_t xfrm_report_msgsize(void)
2861 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2864 static int build_report(struct sk_buff
*skb
, u8 proto
,
2865 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2867 struct xfrm_user_report
*ur
;
2868 struct nlmsghdr
*nlh
;
2870 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2874 ur
= nlmsg_data(nlh
);
2876 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2879 int err
= nla_put(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2881 nlmsg_cancel(skb
, nlh
);
2885 return nlmsg_end(skb
, nlh
);
2888 static int xfrm_send_report(struct net
*net
, u8 proto
,
2889 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2891 struct sk_buff
*skb
;
2893 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2897 if (build_report(skb
, proto
, sel
, addr
) < 0)
2900 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2903 static inline size_t xfrm_mapping_msgsize(void)
2905 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
2908 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
2909 xfrm_address_t
*new_saddr
, __be16 new_sport
)
2911 struct xfrm_user_mapping
*um
;
2912 struct nlmsghdr
*nlh
;
2914 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
2918 um
= nlmsg_data(nlh
);
2920 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
2921 um
->id
.spi
= x
->id
.spi
;
2922 um
->id
.family
= x
->props
.family
;
2923 um
->id
.proto
= x
->id
.proto
;
2924 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
2925 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
2926 um
->new_sport
= new_sport
;
2927 um
->old_sport
= x
->encap
->encap_sport
;
2928 um
->reqid
= x
->props
.reqid
;
2930 return nlmsg_end(skb
, nlh
);
2933 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
2936 struct net
*net
= xs_net(x
);
2937 struct sk_buff
*skb
;
2939 if (x
->id
.proto
!= IPPROTO_ESP
)
2945 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
2949 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
2952 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MAPPING
, GFP_ATOMIC
);
2955 static struct xfrm_mgr netlink_mgr
= {
2957 .notify
= xfrm_send_state_notify
,
2958 .acquire
= xfrm_send_acquire
,
2959 .compile_policy
= xfrm_compile_policy
,
2960 .notify_policy
= xfrm_send_policy_notify
,
2961 .report
= xfrm_send_report
,
2962 .migrate
= xfrm_send_migrate
,
2963 .new_mapping
= xfrm_send_mapping
,
2966 static int __net_init
xfrm_user_net_init(struct net
*net
)
2969 struct netlink_kernel_cfg cfg
= {
2970 .groups
= XFRMNLGRP_MAX
,
2971 .input
= xfrm_netlink_rcv
,
2974 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, THIS_MODULE
, &cfg
);
2977 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
2978 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
2982 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
2985 list_for_each_entry(net
, net_exit_list
, exit_list
)
2986 RCU_INIT_POINTER(net
->xfrm
.nlsk
, NULL
);
2988 list_for_each_entry(net
, net_exit_list
, exit_list
)
2989 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
2992 static struct pernet_operations xfrm_user_net_ops
= {
2993 .init
= xfrm_user_net_init
,
2994 .exit_batch
= xfrm_user_net_exit
,
2997 static int __init
xfrm_user_init(void)
3001 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
3003 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
3006 rv
= xfrm_register_km(&netlink_mgr
);
3008 unregister_pernet_subsys(&xfrm_user_net_ops
);
3012 static void __exit
xfrm_user_exit(void)
3014 xfrm_unregister_km(&netlink_mgr
);
3015 unregister_pernet_subsys(&xfrm_user_net_ops
);
3018 module_init(xfrm_user_init
);
3019 module_exit(xfrm_user_exit
);
3020 MODULE_LICENSE("GPL");
3021 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);