summaryrefslogtreecommitdiffstats
path: root/net/socket.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/socket.c')
-rw-r--r--net/socket.c277
1 files changed, 259 insertions, 18 deletions
diff --git a/net/socket.c b/net/socket.c
index 3c176a12fe48..8255f5bda0aa 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -384,6 +384,18 @@ static struct file_system_type sock_fs_type = {
384 * but we take care of internal coherence yet. 384 * but we take care of internal coherence yet.
385 */ 385 */
386 386
387/**
388 * sock_alloc_file - Bind a &socket to a &file
389 * @sock: socket
390 * @flags: file status flags
391 * @dname: protocol name
392 *
393 * Returns the &file bound with @sock, implicitly storing it
394 * in sock->file. If dname is %NULL, sets to "".
395 * On failure the return is a ERR pointer (see linux/err.h).
396 * This function uses GFP_KERNEL internally.
397 */
398
387struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname) 399struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
388{ 400{
389 struct file *file; 401 struct file *file;
@@ -424,6 +436,14 @@ static int sock_map_fd(struct socket *sock, int flags)
424 return PTR_ERR(newfile); 436 return PTR_ERR(newfile);
425} 437}
426 438
439/**
440 * sock_from_file - Return the &socket bounded to @file.
441 * @file: file
442 * @err: pointer to an error code return
443 *
444 * On failure returns %NULL and assigns -ENOTSOCK to @err.
445 */
446
427struct socket *sock_from_file(struct file *file, int *err) 447struct socket *sock_from_file(struct file *file, int *err)
428{ 448{
429 if (file->f_op == &socket_file_ops) 449 if (file->f_op == &socket_file_ops)
@@ -532,11 +552,11 @@ static const struct inode_operations sockfs_inode_ops = {
532}; 552};
533 553
534/** 554/**
535 * sock_alloc - allocate a socket 555 * sock_alloc - allocate a socket
536 * 556 *
537 * Allocate a new inode and socket object. The two are bound together 557 * Allocate a new inode and socket object. The two are bound together
538 * and initialised. The socket is then returned. If we are out of inodes 558 * and initialised. The socket is then returned. If we are out of inodes
539 * NULL is returned. 559 * NULL is returned. This functions uses GFP_KERNEL internally.
540 */ 560 */
541 561
542struct socket *sock_alloc(void) 562struct socket *sock_alloc(void)
@@ -561,7 +581,7 @@ struct socket *sock_alloc(void)
561EXPORT_SYMBOL(sock_alloc); 581EXPORT_SYMBOL(sock_alloc);
562 582
563/** 583/**
564 * sock_release - close a socket 584 * sock_release - close a socket
565 * @sock: socket to close 585 * @sock: socket to close
566 * 586 *
567 * The socket is released from the protocol stack if it has a release 587 * The socket is released from the protocol stack if it has a release
@@ -617,6 +637,15 @@ void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
617} 637}
618EXPORT_SYMBOL(__sock_tx_timestamp); 638EXPORT_SYMBOL(__sock_tx_timestamp);
619 639
640/**
641 * sock_sendmsg - send a message through @sock
642 * @sock: socket
643 * @msg: message to send
644 *
645 * Sends @msg through @sock, passing through LSM.
646 * Returns the number of bytes sent, or an error code.
647 */
648
620static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg) 649static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
621{ 650{
622 int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg)); 651 int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg));
@@ -633,6 +662,18 @@ int sock_sendmsg(struct socket *sock, struct msghdr *msg)
633} 662}
634EXPORT_SYMBOL(sock_sendmsg); 663EXPORT_SYMBOL(sock_sendmsg);
635 664
665/**
666 * kernel_sendmsg - send a message through @sock (kernel-space)
667 * @sock: socket
668 * @msg: message header
669 * @vec: kernel vec
670 * @num: vec array length
671 * @size: total message data size
672 *
673 * Builds the message data with @vec and sends it through @sock.
674 * Returns the number of bytes sent, or an error code.
675 */
676
636int kernel_sendmsg(struct socket *sock, struct msghdr *msg, 677int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
637 struct kvec *vec, size_t num, size_t size) 678 struct kvec *vec, size_t num, size_t size)
638{ 679{
@@ -641,6 +682,19 @@ int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
641} 682}
642EXPORT_SYMBOL(kernel_sendmsg); 683EXPORT_SYMBOL(kernel_sendmsg);
643 684
685/**
686 * kernel_sendmsg_locked - send a message through @sock (kernel-space)
687 * @sk: sock
688 * @msg: message header
689 * @vec: output s/g array
690 * @num: output s/g array length
691 * @size: total message data size
692 *
693 * Builds the message data with @vec and sends it through @sock.
694 * Returns the number of bytes sent, or an error code.
695 * Caller must hold @sk.
696 */
697
644int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg, 698int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
645 struct kvec *vec, size_t num, size_t size) 699 struct kvec *vec, size_t num, size_t size)
646{ 700{
@@ -811,6 +865,16 @@ void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
811} 865}
812EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops); 866EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
813 867
868/**
869 * sock_recvmsg - receive a message from @sock
870 * @sock: socket
871 * @msg: message to receive
872 * @flags: message flags
873 *
874 * Receives @msg from @sock, passing through LSM. Returns the total number
875 * of bytes received, or an error.
876 */
877
814static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg, 878static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
815 int flags) 879 int flags)
816{ 880{
@@ -826,20 +890,21 @@ int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
826EXPORT_SYMBOL(sock_recvmsg); 890EXPORT_SYMBOL(sock_recvmsg);
827 891
828/** 892/**
829 * kernel_recvmsg - Receive a message from a socket (kernel space) 893 * kernel_recvmsg - Receive a message from a socket (kernel space)
830 * @sock: The socket to receive the message from 894 * @sock: The socket to receive the message from
831 * @msg: Received message 895 * @msg: Received message
832 * @vec: Input s/g array for message data 896 * @vec: Input s/g array for message data
833 * @num: Size of input s/g array 897 * @num: Size of input s/g array
834 * @size: Number of bytes to read 898 * @size: Number of bytes to read
835 * @flags: Message flags (MSG_DONTWAIT, etc...) 899 * @flags: Message flags (MSG_DONTWAIT, etc...)
836 * 900 *
837 * On return the msg structure contains the scatter/gather array passed in the 901 * On return the msg structure contains the scatter/gather array passed in the
838 * vec argument. The array is modified so that it consists of the unfilled 902 * vec argument. The array is modified so that it consists of the unfilled
839 * portion of the original array. 903 * portion of the original array.
840 * 904 *
841 * The returned value is the total number of bytes received, or an error. 905 * The returned value is the total number of bytes received, or an error.
842 */ 906 */
907
843int kernel_recvmsg(struct socket *sock, struct msghdr *msg, 908int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
844 struct kvec *vec, size_t num, size_t size, int flags) 909 struct kvec *vec, size_t num, size_t size, int flags)
845{ 910{
@@ -1005,6 +1070,13 @@ static long sock_do_ioctl(struct net *net, struct socket *sock,
1005 * what to do with it - that's up to the protocol still. 1070 * what to do with it - that's up to the protocol still.
1006 */ 1071 */
1007 1072
1073/**
1074 * get_net_ns - increment the refcount of the network namespace
1075 * @ns: common namespace (net)
1076 *
1077 * Returns the net's common namespace.
1078 */
1079
1008struct ns_common *get_net_ns(struct ns_common *ns) 1080struct ns_common *get_net_ns(struct ns_common *ns)
1009{ 1081{
1010 return &get_net(container_of(ns, struct net, ns))->ns; 1082 return &get_net(container_of(ns, struct net, ns))->ns;
@@ -1099,6 +1171,19 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1099 return err; 1171 return err;
1100} 1172}
1101 1173
1174/**
1175 * sock_create_lite - creates a socket
1176 * @family: protocol family (AF_INET, ...)
1177 * @type: communication type (SOCK_STREAM, ...)
1178 * @protocol: protocol (0, ...)
1179 * @res: new socket
1180 *
1181 * Creates a new socket and assigns it to @res, passing through LSM.
1182 * The new socket initialization is not complete, see kernel_accept().
1183 * Returns 0 or an error. On failure @res is set to %NULL.
1184 * This function internally uses GFP_KERNEL.
1185 */
1186
1102int sock_create_lite(int family, int type, int protocol, struct socket **res) 1187int sock_create_lite(int family, int type, int protocol, struct socket **res)
1103{ 1188{
1104 int err; 1189 int err;
@@ -1224,6 +1309,21 @@ call_kill:
1224} 1309}
1225EXPORT_SYMBOL(sock_wake_async); 1310EXPORT_SYMBOL(sock_wake_async);
1226 1311
1312/**
1313 * __sock_create - creates a socket
1314 * @net: net namespace
1315 * @family: protocol family (AF_INET, ...)
1316 * @type: communication type (SOCK_STREAM, ...)
1317 * @protocol: protocol (0, ...)
1318 * @res: new socket
1319 * @kern: boolean for kernel space sockets
1320 *
1321 * Creates a new socket and assigns it to @res, passing through LSM.
1322 * Returns 0 or an error. On failure @res is set to %NULL. @kern must
1323 * be set to true if the socket resides in kernel space.
1324 * This function internally uses GFP_KERNEL.
1325 */
1326
1227int __sock_create(struct net *net, int family, int type, int protocol, 1327int __sock_create(struct net *net, int family, int type, int protocol,
1228 struct socket **res, int kern) 1328 struct socket **res, int kern)
1229{ 1329{
@@ -1333,12 +1433,35 @@ out_release:
1333} 1433}
1334EXPORT_SYMBOL(__sock_create); 1434EXPORT_SYMBOL(__sock_create);
1335 1435
1436/**
1437 * sock_create - creates a socket
1438 * @family: protocol family (AF_INET, ...)
1439 * @type: communication type (SOCK_STREAM, ...)
1440 * @protocol: protocol (0, ...)
1441 * @res: new socket
1442 *
1443 * A wrapper around __sock_create().
1444 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1445 */
1446
1336int sock_create(int family, int type, int protocol, struct socket **res) 1447int sock_create(int family, int type, int protocol, struct socket **res)
1337{ 1448{
1338 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0); 1449 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1339} 1450}
1340EXPORT_SYMBOL(sock_create); 1451EXPORT_SYMBOL(sock_create);
1341 1452
1453/**
1454 * sock_create_kern - creates a socket (kernel space)
1455 * @net: net namespace
1456 * @family: protocol family (AF_INET, ...)
1457 * @type: communication type (SOCK_STREAM, ...)
1458 * @protocol: protocol (0, ...)
1459 * @res: new socket
1460 *
1461 * A wrapper around __sock_create().
1462 * Returns 0 or an error. This function internally uses GFP_KERNEL.
1463 */
1464
1342int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res) 1465int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
1343{ 1466{
1344 return __sock_create(net, family, type, protocol, res, 1); 1467 return __sock_create(net, family, type, protocol, res, 1);
@@ -3322,18 +3445,46 @@ static long compat_sock_ioctl(struct file *file, unsigned int cmd,
3322} 3445}
3323#endif 3446#endif
3324 3447
3448/**
3449 * kernel_bind - bind an address to a socket (kernel space)
3450 * @sock: socket
3451 * @addr: address
3452 * @addrlen: length of address
3453 *
3454 * Returns 0 or an error.
3455 */
3456
3325int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen) 3457int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
3326{ 3458{
3327 return sock->ops->bind(sock, addr, addrlen); 3459 return sock->ops->bind(sock, addr, addrlen);
3328} 3460}
3329EXPORT_SYMBOL(kernel_bind); 3461EXPORT_SYMBOL(kernel_bind);
3330 3462
3463/**
3464 * kernel_listen - move socket to listening state (kernel space)
3465 * @sock: socket
3466 * @backlog: pending connections queue size
3467 *
3468 * Returns 0 or an error.
3469 */
3470
3331int kernel_listen(struct socket *sock, int backlog) 3471int kernel_listen(struct socket *sock, int backlog)
3332{ 3472{
3333 return sock->ops->listen(sock, backlog); 3473 return sock->ops->listen(sock, backlog);
3334} 3474}
3335EXPORT_SYMBOL(kernel_listen); 3475EXPORT_SYMBOL(kernel_listen);
3336 3476
3477/**
3478 * kernel_accept - accept a connection (kernel space)
3479 * @sock: listening socket
3480 * @newsock: new connected socket
3481 * @flags: flags
3482 *
3483 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
3484 * If it fails, @newsock is guaranteed to be %NULL.
3485 * Returns 0 or an error.
3486 */
3487
3337int kernel_accept(struct socket *sock, struct socket **newsock, int flags) 3488int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
3338{ 3489{
3339 struct sock *sk = sock->sk; 3490 struct sock *sk = sock->sk;
@@ -3359,6 +3510,19 @@ done:
3359} 3510}
3360EXPORT_SYMBOL(kernel_accept); 3511EXPORT_SYMBOL(kernel_accept);
3361 3512
3513/**
3514 * kernel_connect - connect a socket (kernel space)
3515 * @sock: socket
3516 * @addr: address
3517 * @addrlen: address length
3518 * @flags: flags (O_NONBLOCK, ...)
3519 *
3520 * For datagram sockets, @addr is the addres to which datagrams are sent
3521 * by default, and the only address from which datagrams are received.
3522 * For stream sockets, attempts to connect to @addr.
3523 * Returns 0 or an error code.
3524 */
3525
3362int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen, 3526int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3363 int flags) 3527 int flags)
3364{ 3528{
@@ -3366,18 +3530,48 @@ int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
3366} 3530}
3367EXPORT_SYMBOL(kernel_connect); 3531EXPORT_SYMBOL(kernel_connect);
3368 3532
3533/**
3534 * kernel_getsockname - get the address which the socket is bound (kernel space)
3535 * @sock: socket
3536 * @addr: address holder
3537 *
3538 * Fills the @addr pointer with the address which the socket is bound.
3539 * Returns 0 or an error code.
3540 */
3541
3369int kernel_getsockname(struct socket *sock, struct sockaddr *addr) 3542int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
3370{ 3543{
3371 return sock->ops->getname(sock, addr, 0); 3544 return sock->ops->getname(sock, addr, 0);
3372} 3545}
3373EXPORT_SYMBOL(kernel_getsockname); 3546EXPORT_SYMBOL(kernel_getsockname);
3374 3547
3548/**
3549 * kernel_peername - get the address which the socket is connected (kernel space)
3550 * @sock: socket
3551 * @addr: address holder
3552 *
3553 * Fills the @addr pointer with the address which the socket is connected.
3554 * Returns 0 or an error code.
3555 */
3556
3375int kernel_getpeername(struct socket *sock, struct sockaddr *addr) 3557int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
3376{ 3558{
3377 return sock->ops->getname(sock, addr, 1); 3559 return sock->ops->getname(sock, addr, 1);
3378} 3560}
3379EXPORT_SYMBOL(kernel_getpeername); 3561EXPORT_SYMBOL(kernel_getpeername);
3380 3562
3563/**
3564 * kernel_getsockopt - get a socket option (kernel space)
3565 * @sock: socket
3566 * @level: API level (SOL_SOCKET, ...)
3567 * @optname: option tag
3568 * @optval: option value
3569 * @optlen: option length
3570 *
3571 * Assigns the option length to @optlen.
3572 * Returns 0 or an error.
3573 */
3574
3381int kernel_getsockopt(struct socket *sock, int level, int optname, 3575int kernel_getsockopt(struct socket *sock, int level, int optname,
3382 char *optval, int *optlen) 3576 char *optval, int *optlen)
3383{ 3577{
@@ -3400,6 +3594,17 @@ int kernel_getsockopt(struct socket *sock, int level, int optname,
3400} 3594}
3401EXPORT_SYMBOL(kernel_getsockopt); 3595EXPORT_SYMBOL(kernel_getsockopt);
3402 3596
3597/**
3598 * kernel_setsockopt - set a socket option (kernel space)
3599 * @sock: socket
3600 * @level: API level (SOL_SOCKET, ...)
3601 * @optname: option tag
3602 * @optval: option value
3603 * @optlen: option length
3604 *
3605 * Returns 0 or an error.
3606 */
3607
3403int kernel_setsockopt(struct socket *sock, int level, int optname, 3608int kernel_setsockopt(struct socket *sock, int level, int optname,
3404 char *optval, unsigned int optlen) 3609 char *optval, unsigned int optlen)
3405{ 3610{
@@ -3420,6 +3625,17 @@ int kernel_setsockopt(struct socket *sock, int level, int optname,
3420} 3625}
3421EXPORT_SYMBOL(kernel_setsockopt); 3626EXPORT_SYMBOL(kernel_setsockopt);
3422 3627
3628/**
3629 * kernel_sendpage - send a &page through a socket (kernel space)
3630 * @sock: socket
3631 * @page: page
3632 * @offset: page offset
3633 * @size: total size in bytes
3634 * @flags: flags (MSG_DONTWAIT, ...)
3635 *
3636 * Returns the total amount sent in bytes or an error.
3637 */
3638
3423int kernel_sendpage(struct socket *sock, struct page *page, int offset, 3639int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3424 size_t size, int flags) 3640 size_t size, int flags)
3425{ 3641{
@@ -3430,6 +3646,18 @@ int kernel_sendpage(struct socket *sock, struct page *page, int offset,
3430} 3646}
3431EXPORT_SYMBOL(kernel_sendpage); 3647EXPORT_SYMBOL(kernel_sendpage);
3432 3648
3649/**
3650 * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
3651 * @sk: sock
3652 * @page: page
3653 * @offset: page offset
3654 * @size: total size in bytes
3655 * @flags: flags (MSG_DONTWAIT, ...)
3656 *
3657 * Returns the total amount sent in bytes or an error.
3658 * Caller must hold @sk.
3659 */
3660
3433int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset, 3661int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3434 size_t size, int flags) 3662 size_t size, int flags)
3435{ 3663{
@@ -3443,17 +3671,30 @@ int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
3443} 3671}
3444EXPORT_SYMBOL(kernel_sendpage_locked); 3672EXPORT_SYMBOL(kernel_sendpage_locked);
3445 3673
3674/**
3675 * kernel_shutdown - shut down part of a full-duplex connection (kernel space)
3676 * @sock: socket
3677 * @how: connection part
3678 *
3679 * Returns 0 or an error.
3680 */
3681
3446int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how) 3682int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
3447{ 3683{
3448 return sock->ops->shutdown(sock, how); 3684 return sock->ops->shutdown(sock, how);
3449} 3685}
3450EXPORT_SYMBOL(kernel_sock_shutdown); 3686EXPORT_SYMBOL(kernel_sock_shutdown);
3451 3687
3452/* This routine returns the IP overhead imposed by a socket i.e. 3688/**
3453 * the length of the underlying IP header, depending on whether 3689 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
3454 * this is an IPv4 or IPv6 socket and the length from IP options turned 3690 * @sk: socket
3455 * on at the socket. Assumes that the caller has a lock on the socket. 3691 *
3692 * This routine returns the IP overhead imposed by a socket i.e.
3693 * the length of the underlying IP header, depending on whether
3694 * this is an IPv4 or IPv6 socket and the length from IP options turned
3695 * on at the socket. Assumes that the caller has a lock on the socket.
3456 */ 3696 */
3697
3457u32 kernel_sock_ip_overhead(struct sock *sk) 3698u32 kernel_sock_ip_overhead(struct sock *sk)
3458{ 3699{
3459 struct inet_sock *inet; 3700 struct inet_sock *inet;