Merge branch 'work.sock_recvmsg' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / staging / lustre / lnet / lnet / lib-socket.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * GPL HEADER START
4  *
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 only,
9  * as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License version 2 for more details (a copy is included
15  * in the LICENSE file that accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License
18  * version 2 along with this program; If not, see
19  * http://www.gnu.org/licenses/gpl-2.0.html
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Use is subject to license terms.
26  *
27  * Copyright (c) 2012, 2015, Intel Corporation.
28  */
29 /*
30  * This file is part of Lustre, http://www.lustre.org/
31  * Lustre is a trademark of Seagate, Inc.
32  */
33 #define DEBUG_SUBSYSTEM S_LNET
34
35 #include <linux/if.h>
36 #include <linux/in.h>
37 #include <linux/net.h>
38 #include <linux/file.h>
39 #include <linux/pagemap.h>
40 /* For sys_open & sys_close */
41 #include <linux/syscalls.h>
42 #include <net/sock.h>
43
44 #include <linux/libcfs/libcfs.h>
45 #include <linux/lnet/lib-lnet.h>
46
47 static int
48 kernel_sock_unlocked_ioctl(struct file *filp, int cmd, unsigned long arg)
49 {
50         mm_segment_t oldfs = get_fs();
51         int err;
52
53         set_fs(KERNEL_DS);
54         err = filp->f_op->unlocked_ioctl(filp, cmd, arg);
55         set_fs(oldfs);
56
57         return err;
58 }
59
60 static int
61 lnet_sock_ioctl(int cmd, unsigned long arg)
62 {
63         struct file *sock_filp;
64         struct socket *sock;
65         int rc;
66
67         rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
68         if (rc) {
69                 CERROR("Can't create socket: %d\n", rc);
70                 return rc;
71         }
72
73         sock_filp = sock_alloc_file(sock, 0, NULL);
74         if (IS_ERR(sock_filp))
75                 return PTR_ERR(sock_filp);
76
77         rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg);
78
79         fput(sock_filp);
80         return rc;
81 }
82
83 int
84 lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask)
85 {
86         struct ifreq ifr;
87         int nob;
88         int rc;
89         __be32 val;
90
91         nob = strnlen(name, IFNAMSIZ);
92         if (nob == IFNAMSIZ) {
93                 CERROR("Interface name %s too long\n", name);
94                 return -EINVAL;
95         }
96
97         BUILD_BUG_ON(sizeof(ifr.ifr_name) < IFNAMSIZ);
98
99         if (strlen(name) > sizeof(ifr.ifr_name) - 1)
100                 return -E2BIG;
101         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
102
103         rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
104         if (rc) {
105                 CERROR("Can't get flags for interface %s\n", name);
106                 return rc;
107         }
108
109         if (!(ifr.ifr_flags & IFF_UP)) {
110                 CDEBUG(D_NET, "Interface %s down\n", name);
111                 *up = 0;
112                 *ip = *mask = 0;
113                 return 0;
114         }
115         *up = 1;
116
117         if (strlen(name) > sizeof(ifr.ifr_name) - 1)
118                 return -E2BIG;
119         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
120
121         ifr.ifr_addr.sa_family = AF_INET;
122         rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
123         if (rc) {
124                 CERROR("Can't get IP address for interface %s\n", name);
125                 return rc;
126         }
127
128         val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
129         *ip = ntohl(val);
130
131         if (strlen(name) > sizeof(ifr.ifr_name) - 1)
132                 return -E2BIG;
133         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
134
135         ifr.ifr_addr.sa_family = AF_INET;
136         rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
137         if (rc) {
138                 CERROR("Can't get netmask for interface %s\n", name);
139                 return rc;
140         }
141
142         val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
143         *mask = ntohl(val);
144
145         return 0;
146 }
147 EXPORT_SYMBOL(lnet_ipif_query);
148
149 int
150 lnet_ipif_enumerate(char ***namesp)
151 {
152         /* Allocate and fill in 'names', returning # interfaces/error */
153         char **names;
154         int toobig;
155         int nalloc;
156         int nfound;
157         struct ifreq *ifr;
158         struct ifconf ifc;
159         int rc;
160         int nob;
161         int i;
162
163         nalloc = 16;    /* first guess at max interfaces */
164         toobig = 0;
165         for (;;) {
166                 if (nalloc * sizeof(*ifr) > PAGE_SIZE) {
167                         toobig = 1;
168                         nalloc = PAGE_SIZE / sizeof(*ifr);
169                         CWARN("Too many interfaces: only enumerating first %d\n",
170                               nalloc);
171                 }
172
173                 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
174                 if (!ifr) {
175                         CERROR("ENOMEM enumerating up to %d interfaces\n",
176                                nalloc);
177                         rc = -ENOMEM;
178                         goto out0;
179                 }
180
181                 ifc.ifc_buf = (char *)ifr;
182                 ifc.ifc_len = nalloc * sizeof(*ifr);
183
184                 rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
185                 if (rc < 0) {
186                         CERROR("Error %d enumerating interfaces\n", rc);
187                         goto out1;
188                 }
189
190                 LASSERT(!rc);
191
192                 nfound = ifc.ifc_len / sizeof(*ifr);
193                 LASSERT(nfound <= nalloc);
194
195                 if (nfound < nalloc || toobig)
196                         break;
197
198                 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
199                 nalloc *= 2;
200         }
201
202         if (!nfound)
203                 goto out1;
204
205         LIBCFS_ALLOC(names, nfound * sizeof(*names));
206         if (!names) {
207                 rc = -ENOMEM;
208                 goto out1;
209         }
210
211         for (i = 0; i < nfound; i++) {
212                 nob = strnlen(ifr[i].ifr_name, IFNAMSIZ);
213                 if (nob == IFNAMSIZ) {
214                         /* no space for terminating NULL */
215                         CERROR("interface name %.*s too long (%d max)\n",
216                                nob, ifr[i].ifr_name, IFNAMSIZ);
217                         rc = -ENAMETOOLONG;
218                         goto out2;
219                 }
220
221                 LIBCFS_ALLOC(names[i], IFNAMSIZ);
222                 if (!names[i]) {
223                         rc = -ENOMEM;
224                         goto out2;
225                 }
226
227                 memcpy(names[i], ifr[i].ifr_name, nob);
228                 names[i][nob] = 0;
229         }
230
231         *namesp = names;
232         rc = nfound;
233
234 out2:
235         if (rc < 0)
236                 lnet_ipif_free_enumeration(names, nfound);
237 out1:
238         LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
239 out0:
240         return rc;
241 }
242 EXPORT_SYMBOL(lnet_ipif_enumerate);
243
244 void
245 lnet_ipif_free_enumeration(char **names, int n)
246 {
247         int i;
248
249         LASSERT(n > 0);
250
251         for (i = 0; i < n && names[i]; i++)
252                 LIBCFS_FREE(names[i], IFNAMSIZ);
253
254         LIBCFS_FREE(names, n * sizeof(*names));
255 }
256 EXPORT_SYMBOL(lnet_ipif_free_enumeration);
257
258 int
259 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
260 {
261         int rc;
262         long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
263         unsigned long then;
264         struct timeval tv;
265         struct kvec  iov = { .iov_base = buffer, .iov_len  = nob };
266         struct msghdr msg = {NULL,};
267
268         LASSERT(nob > 0);
269         /*
270          * Caller may pass a zero timeout if she thinks the socket buffer is
271          * empty enough to take the whole message immediately
272          */
273         iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iov, 1, nob);
274         for (;;) {
275                 msg.msg_flags = !timeout ? MSG_DONTWAIT : 0;
276                 if (timeout) {
277                         /* Set send timeout to remaining time */
278                         jiffies_to_timeval(jiffies_left, &tv);
279                         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
280                                                (char *)&tv, sizeof(tv));
281                         if (rc) {
282                                 CERROR("Can't set socket send timeout %ld.%06d: %d\n",
283                                        (long)tv.tv_sec, (int)tv.tv_usec, rc);
284                                 return rc;
285                         }
286                 }
287
288                 then = jiffies;
289                 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
290                 jiffies_left -= jiffies - then;
291
292                 if (rc < 0)
293                         return rc;
294
295                 if (!rc) {
296                         CERROR("Unexpected zero rc\n");
297                         return -ECONNABORTED;
298                 }
299
300                 if (!msg_data_left(&msg))
301                         break;
302
303                 if (jiffies_left <= 0)
304                         return -EAGAIN;
305         }
306         return 0;
307 }
308 EXPORT_SYMBOL(lnet_sock_write);
309
310 int
311 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
312 {
313         int rc;
314         long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
315         unsigned long then;
316         struct timeval tv;
317         struct kvec  iov = {
318                 .iov_base = buffer,
319                 .iov_len  = nob
320         };
321         struct msghdr msg = {
322                 .msg_flags = 0
323         };
324
325         LASSERT(nob > 0);
326         LASSERT(jiffies_left > 0);
327
328         iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1, nob);
329
330         for (;;) {
331                 /* Set receive timeout to remaining time */
332                 jiffies_to_timeval(jiffies_left, &tv);
333                 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
334                                        (char *)&tv, sizeof(tv));
335                 if (rc) {
336                         CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
337                                (long)tv.tv_sec, (int)tv.tv_usec, rc);
338                         return rc;
339                 }
340
341                 then = jiffies;
342                 rc = sock_recvmsg(sock, &msg, 0);
343                 jiffies_left -= jiffies - then;
344
345                 if (rc < 0)
346                         return rc;
347
348                 if (!rc)
349                         return -ECONNRESET;
350
351                 if (!msg_data_left(&msg))
352                         return 0;
353
354                 if (jiffies_left <= 0)
355                         return -ETIMEDOUT;
356         }
357 }
358 EXPORT_SYMBOL(lnet_sock_read);
359
360 static int
361 lnet_sock_create(struct socket **sockp, int *fatal, __u32 local_ip,
362                  int local_port)
363 {
364         struct sockaddr_in locaddr;
365         struct socket *sock;
366         int rc;
367         int option;
368
369         /* All errors are fatal except bind failure if the port is in use */
370         *fatal = 1;
371
372         rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
373         *sockp = sock;
374         if (rc) {
375                 CERROR("Can't create socket: %d\n", rc);
376                 return rc;
377         }
378
379         option = 1;
380         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
381                                (char *)&option, sizeof(option));
382         if (rc) {
383                 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
384                 goto failed;
385         }
386
387         if (local_ip || local_port) {
388                 memset(&locaddr, 0, sizeof(locaddr));
389                 locaddr.sin_family = AF_INET;
390                 locaddr.sin_port = htons(local_port);
391                 if (!local_ip)
392                         locaddr.sin_addr.s_addr = htonl(INADDR_ANY);
393                 else
394                         locaddr.sin_addr.s_addr = htonl(local_ip);
395
396                 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
397                                  sizeof(locaddr));
398                 if (rc == -EADDRINUSE) {
399                         CDEBUG(D_NET, "Port %d already in use\n", local_port);
400                         *fatal = 0;
401                         goto failed;
402                 }
403                 if (rc) {
404                         CERROR("Error trying to bind to port %d: %d\n",
405                                local_port, rc);
406                         goto failed;
407                 }
408         }
409         return 0;
410
411 failed:
412         sock_release(sock);
413         return rc;
414 }
415
416 int
417 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
418 {
419         int option;
420         int rc;
421
422         if (txbufsize) {
423                 option = txbufsize;
424                 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
425                                        (char *)&option, sizeof(option));
426                 if (rc) {
427                         CERROR("Can't set send buffer %d: %d\n",
428                                option, rc);
429                         return rc;
430                 }
431         }
432
433         if (rxbufsize) {
434                 option = rxbufsize;
435                 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
436                                        (char *)&option, sizeof(option));
437                 if (rc) {
438                         CERROR("Can't set receive buffer %d: %d\n",
439                                option, rc);
440                         return rc;
441                 }
442         }
443         return 0;
444 }
445 EXPORT_SYMBOL(lnet_sock_setbuf);
446
447 int
448 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
449 {
450         struct sockaddr_in sin;
451         int len = sizeof(sin);
452         int rc;
453
454         if (remote)
455                 rc = kernel_getpeername(sock, (struct sockaddr *)&sin, &len);
456         else
457                 rc = kernel_getsockname(sock, (struct sockaddr *)&sin, &len);
458         if (rc) {
459                 CERROR("Error %d getting sock %s IP/port\n",
460                        rc, remote ? "peer" : "local");
461                 return rc;
462         }
463
464         if (ip)
465                 *ip = ntohl(sin.sin_addr.s_addr);
466
467         if (port)
468                 *port = ntohs(sin.sin_port);
469
470         return 0;
471 }
472 EXPORT_SYMBOL(lnet_sock_getaddr);
473
474 int
475 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
476 {
477         if (txbufsize)
478                 *txbufsize = sock->sk->sk_sndbuf;
479
480         if (rxbufsize)
481                 *rxbufsize = sock->sk->sk_rcvbuf;
482
483         return 0;
484 }
485 EXPORT_SYMBOL(lnet_sock_getbuf);
486
487 int
488 lnet_sock_listen(struct socket **sockp, __u32 local_ip, int local_port,
489                  int backlog)
490 {
491         int fatal;
492         int rc;
493
494         rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
495         if (rc) {
496                 if (!fatal)
497                         CERROR("Can't create socket: port %d already in use\n",
498                                local_port);
499                 return rc;
500         }
501
502         rc = kernel_listen(*sockp, backlog);
503         if (!rc)
504                 return 0;
505
506         CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
507         sock_release(*sockp);
508         return rc;
509 }
510
511 int
512 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
513 {
514         wait_queue_entry_t wait;
515         struct socket *newsock;
516         int rc;
517
518         /*
519          * XXX this should add a ref to sock->ops->owner, if
520          * TCP could be a module
521          */
522         rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
523         if (rc) {
524                 CERROR("Can't allocate socket\n");
525                 return rc;
526         }
527
528         newsock->ops = sock->ops;
529
530         rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
531         if (rc == -EAGAIN) {
532                 /* Nothing ready, so wait for activity */
533                 init_waitqueue_entry(&wait, current);
534                 add_wait_queue(sk_sleep(sock->sk), &wait);
535                 set_current_state(TASK_INTERRUPTIBLE);
536                 schedule();
537                 remove_wait_queue(sk_sleep(sock->sk), &wait);
538                 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
539         }
540
541         if (rc)
542                 goto failed;
543
544         *newsockp = newsock;
545         return 0;
546
547 failed:
548         sock_release(newsock);
549         return rc;
550 }
551
552 int
553 lnet_sock_connect(struct socket **sockp, int *fatal, __u32 local_ip,
554                   int local_port, __u32 peer_ip, int peer_port)
555 {
556         struct sockaddr_in srvaddr;
557         int rc;
558
559         rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
560         if (rc)
561                 return rc;
562
563         memset(&srvaddr, 0, sizeof(srvaddr));
564         srvaddr.sin_family = AF_INET;
565         srvaddr.sin_port = htons(peer_port);
566         srvaddr.sin_addr.s_addr = htonl(peer_ip);
567
568         rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
569                             sizeof(srvaddr), 0);
570         if (!rc)
571                 return 0;
572
573         /*
574          * EADDRNOTAVAIL probably means we're already connected to the same
575          * peer/port on the same local port on a differently typed
576          * connection.  Let our caller retry with a different local
577          * port...
578          */
579         *fatal = !(rc == -EADDRNOTAVAIL);
580
581         CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
582                      "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
583                      &local_ip, local_port, &peer_ip, peer_port);
584
585         sock_release(*sockp);
586         return rc;
587 }