2 * af_alg: User-space algorithm interface
4 * This file provides the user-space API for algorithms.
6 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <linux/atomic.h>
16 #include <crypto/if_alg.h>
17 #include <linux/crypto.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/net.h>
23 #include <linux/rwsem.h>
24 #include <linux/security.h>
26 struct alg_type_list
{
27 const struct af_alg_type
*type
;
28 struct list_head list
;
31 static atomic_long_t alg_memory_allocated
;
33 static struct proto alg_proto
= {
36 .memory_allocated
= &alg_memory_allocated
,
37 .obj_size
= sizeof(struct alg_sock
),
40 static LIST_HEAD(alg_types
);
41 static DECLARE_RWSEM(alg_types_sem
);
43 static const struct af_alg_type
*alg_get_type(const char *name
)
45 const struct af_alg_type
*type
= ERR_PTR(-ENOENT
);
46 struct alg_type_list
*node
;
48 down_read(&alg_types_sem
);
49 list_for_each_entry(node
, &alg_types
, list
) {
50 if (strcmp(node
->type
->name
, name
))
53 if (try_module_get(node
->type
->owner
))
57 up_read(&alg_types_sem
);
62 int af_alg_register_type(const struct af_alg_type
*type
)
64 struct alg_type_list
*node
;
67 down_write(&alg_types_sem
);
68 list_for_each_entry(node
, &alg_types
, list
) {
69 if (!strcmp(node
->type
->name
, type
->name
))
73 node
= kmalloc(sizeof(*node
), GFP_KERNEL
);
78 type
->ops
->owner
= THIS_MODULE
;
80 list_add(&node
->list
, &alg_types
);
84 up_write(&alg_types_sem
);
88 EXPORT_SYMBOL_GPL(af_alg_register_type
);
90 int af_alg_unregister_type(const struct af_alg_type
*type
)
92 struct alg_type_list
*node
;
95 down_write(&alg_types_sem
);
96 list_for_each_entry(node
, &alg_types
, list
) {
97 if (strcmp(node
->type
->name
, type
->name
))
100 list_del(&node
->list
);
105 up_write(&alg_types_sem
);
109 EXPORT_SYMBOL_GPL(af_alg_unregister_type
);
111 static void alg_do_release(const struct af_alg_type
*type
, void *private)
116 type
->release(private);
117 module_put(type
->owner
);
120 int af_alg_release(struct socket
*sock
)
126 EXPORT_SYMBOL_GPL(af_alg_release
);
128 static int alg_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
130 const u32 forbidden
= CRYPTO_ALG_INTERNAL
;
131 struct sock
*sk
= sock
->sk
;
132 struct alg_sock
*ask
= alg_sk(sk
);
133 struct sockaddr_alg
*sa
= (void *)uaddr
;
134 const struct af_alg_type
*type
;
137 if (sock
->state
== SS_CONNECTED
)
140 if (addr_len
!= sizeof(*sa
))
143 sa
->salg_type
[sizeof(sa
->salg_type
) - 1] = 0;
144 sa
->salg_name
[sizeof(sa
->salg_name
) - 1] = 0;
146 type
= alg_get_type(sa
->salg_type
);
147 if (IS_ERR(type
) && PTR_ERR(type
) == -ENOENT
) {
148 request_module("algif-%s", sa
->salg_type
);
149 type
= alg_get_type(sa
->salg_type
);
153 return PTR_ERR(type
);
155 private = type
->bind(sa
->salg_name
,
156 sa
->salg_feat
& ~forbidden
,
157 sa
->salg_mask
& ~forbidden
);
158 if (IS_ERR(private)) {
159 module_put(type
->owner
);
160 return PTR_ERR(private);
165 swap(ask
->type
, type
);
166 swap(ask
->private, private);
170 alg_do_release(type
, private);
175 static int alg_setkey(struct sock
*sk
, char __user
*ukey
,
178 struct alg_sock
*ask
= alg_sk(sk
);
179 const struct af_alg_type
*type
= ask
->type
;
183 key
= sock_kmalloc(sk
, keylen
, GFP_KERNEL
);
188 if (copy_from_user(key
, ukey
, keylen
))
191 err
= type
->setkey(ask
->private, key
, keylen
);
194 sock_kzfree_s(sk
, key
, keylen
);
199 static int alg_setsockopt(struct socket
*sock
, int level
, int optname
,
200 char __user
*optval
, unsigned int optlen
)
202 struct sock
*sk
= sock
->sk
;
203 struct alg_sock
*ask
= alg_sk(sk
);
204 const struct af_alg_type
*type
;
205 int err
= -ENOPROTOOPT
;
210 if (level
!= SOL_ALG
|| !type
)
215 if (sock
->state
== SS_CONNECTED
)
220 err
= alg_setkey(sk
, optval
, optlen
);
222 case ALG_SET_AEAD_AUTHSIZE
:
223 if (sock
->state
== SS_CONNECTED
)
225 if (!type
->setauthsize
)
227 err
= type
->setauthsize(ask
->private, optlen
);
236 int af_alg_accept(struct sock
*sk
, struct socket
*newsock
)
238 struct alg_sock
*ask
= alg_sk(sk
);
239 const struct af_alg_type
*type
;
250 sk2
= sk_alloc(sock_net(sk
), PF_ALG
, GFP_KERNEL
, &alg_proto
, 0);
255 sock_init_data(newsock
, sk2
);
256 sock_graft(sk2
, newsock
);
257 security_sk_clone(sk
, sk2
);
259 err
= type
->accept(ask
->private, sk2
);
265 sk2
->sk_family
= PF_ALG
;
268 alg_sk(sk2
)->parent
= sk
;
269 alg_sk(sk2
)->type
= type
;
271 newsock
->ops
= type
->ops
;
272 newsock
->state
= SS_CONNECTED
;
281 EXPORT_SYMBOL_GPL(af_alg_accept
);
283 static int alg_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
285 return af_alg_accept(sock
->sk
, newsock
);
288 static const struct proto_ops alg_proto_ops
= {
290 .owner
= THIS_MODULE
,
292 .connect
= sock_no_connect
,
293 .socketpair
= sock_no_socketpair
,
294 .getname
= sock_no_getname
,
295 .ioctl
= sock_no_ioctl
,
296 .listen
= sock_no_listen
,
297 .shutdown
= sock_no_shutdown
,
298 .getsockopt
= sock_no_getsockopt
,
299 .mmap
= sock_no_mmap
,
300 .sendpage
= sock_no_sendpage
,
301 .sendmsg
= sock_no_sendmsg
,
302 .recvmsg
= sock_no_recvmsg
,
303 .poll
= sock_no_poll
,
306 .release
= af_alg_release
,
307 .setsockopt
= alg_setsockopt
,
308 .accept
= alg_accept
,
311 static void alg_sock_destruct(struct sock
*sk
)
313 struct alg_sock
*ask
= alg_sk(sk
);
315 alg_do_release(ask
->type
, ask
->private);
318 static int alg_create(struct net
*net
, struct socket
*sock
, int protocol
,
324 if (sock
->type
!= SOCK_SEQPACKET
)
325 return -ESOCKTNOSUPPORT
;
327 return -EPROTONOSUPPORT
;
330 sk
= sk_alloc(net
, PF_ALG
, GFP_KERNEL
, &alg_proto
, kern
);
334 sock
->ops
= &alg_proto_ops
;
335 sock_init_data(sock
, sk
);
337 sk
->sk_family
= PF_ALG
;
338 sk
->sk_destruct
= alg_sock_destruct
;
345 static const struct net_proto_family alg_family
= {
347 .create
= alg_create
,
348 .owner
= THIS_MODULE
,
351 int af_alg_make_sg(struct af_alg_sgl
*sgl
, struct iov_iter
*iter
, int len
)
357 n
= iov_iter_get_pages(iter
, sgl
->pages
, len
, ALG_MAX_PAGES
, &off
);
361 npages
= (off
+ n
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
362 if (WARN_ON(npages
== 0))
364 /* Add one extra for linking */
365 sg_init_table(sgl
->sg
, npages
+ 1);
367 for (i
= 0, len
= n
; i
< npages
; i
++) {
368 int plen
= min_t(int, len
, PAGE_SIZE
- off
);
370 sg_set_page(sgl
->sg
+ i
, sgl
->pages
[i
], plen
, off
);
375 sg_mark_end(sgl
->sg
+ npages
- 1);
376 sgl
->npages
= npages
;
380 EXPORT_SYMBOL_GPL(af_alg_make_sg
);
382 void af_alg_link_sg(struct af_alg_sgl
*sgl_prev
, struct af_alg_sgl
*sgl_new
)
384 sg_unmark_end(sgl_prev
->sg
+ sgl_prev
->npages
- 1);
385 sg_chain(sgl_prev
->sg
, sgl_prev
->npages
+ 1, sgl_new
->sg
);
387 EXPORT_SYMBOL_GPL(af_alg_link_sg
);
389 void af_alg_free_sg(struct af_alg_sgl
*sgl
)
393 for (i
= 0; i
< sgl
->npages
; i
++)
394 put_page(sgl
->pages
[i
]);
396 EXPORT_SYMBOL_GPL(af_alg_free_sg
);
398 int af_alg_cmsg_send(struct msghdr
*msg
, struct af_alg_control
*con
)
400 struct cmsghdr
*cmsg
;
402 for_each_cmsghdr(cmsg
, msg
) {
403 if (!CMSG_OK(msg
, cmsg
))
405 if (cmsg
->cmsg_level
!= SOL_ALG
)
408 switch (cmsg
->cmsg_type
) {
410 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(*con
->iv
)))
412 con
->iv
= (void *)CMSG_DATA(cmsg
);
413 if (cmsg
->cmsg_len
< CMSG_LEN(con
->iv
->ivlen
+
419 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(u32
)))
421 con
->op
= *(u32
*)CMSG_DATA(cmsg
);
424 case ALG_SET_AEAD_ASSOCLEN
:
425 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(u32
)))
427 con
->aead_assoclen
= *(u32
*)CMSG_DATA(cmsg
);
437 EXPORT_SYMBOL_GPL(af_alg_cmsg_send
);
439 int af_alg_wait_for_completion(int err
, struct af_alg_completion
*completion
)
444 wait_for_completion(&completion
->completion
);
445 reinit_completion(&completion
->completion
);
446 err
= completion
->err
;
452 EXPORT_SYMBOL_GPL(af_alg_wait_for_completion
);
454 void af_alg_complete(struct crypto_async_request
*req
, int err
)
456 struct af_alg_completion
*completion
= req
->data
;
458 if (err
== -EINPROGRESS
)
461 completion
->err
= err
;
462 complete(&completion
->completion
);
464 EXPORT_SYMBOL_GPL(af_alg_complete
);
466 static int __init
af_alg_init(void)
468 int err
= proto_register(&alg_proto
, 0);
473 err
= sock_register(&alg_family
);
475 goto out_unregister_proto
;
480 out_unregister_proto
:
481 proto_unregister(&alg_proto
);
485 static void __exit
af_alg_exit(void)
487 sock_unregister(PF_ALG
);
488 proto_unregister(&alg_proto
);
491 module_init(af_alg_init
);
492 module_exit(af_alg_exit
);
493 MODULE_LICENSE("GPL");
494 MODULE_ALIAS_NETPROTO(AF_ALG
);