ASoC: tfa9879: add DT bindings to MAINTAINERS
[sfrench/cifs-2.6.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                 sock_release(sock);
76                 rc = PTR_ERR(sock_filp);
77                 goto out;
78         }
79
80         rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg);
81
82         fput(sock_filp);
83 out:
84         return rc;
85 }
86
87 int
88 lnet_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask)
89 {
90         struct ifreq ifr;
91         int nob;
92         int rc;
93         __be32 val;
94
95         nob = strnlen(name, IFNAMSIZ);
96         if (nob == IFNAMSIZ) {
97                 CERROR("Interface name %s too long\n", name);
98                 return -EINVAL;
99         }
100
101         BUILD_BUG_ON(sizeof(ifr.ifr_name) < IFNAMSIZ);
102
103         if (strlen(name) > sizeof(ifr.ifr_name) - 1)
104                 return -E2BIG;
105         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
106
107         rc = lnet_sock_ioctl(SIOCGIFFLAGS, (unsigned long)&ifr);
108         if (rc) {
109                 CERROR("Can't get flags for interface %s\n", name);
110                 return rc;
111         }
112
113         if (!(ifr.ifr_flags & IFF_UP)) {
114                 CDEBUG(D_NET, "Interface %s down\n", name);
115                 *up = 0;
116                 *ip = *mask = 0;
117                 return 0;
118         }
119         *up = 1;
120
121         if (strlen(name) > sizeof(ifr.ifr_name) - 1)
122                 return -E2BIG;
123         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
124
125         ifr.ifr_addr.sa_family = AF_INET;
126         rc = lnet_sock_ioctl(SIOCGIFADDR, (unsigned long)&ifr);
127         if (rc) {
128                 CERROR("Can't get IP address for interface %s\n", name);
129                 return rc;
130         }
131
132         val = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
133         *ip = ntohl(val);
134
135         if (strlen(name) > sizeof(ifr.ifr_name) - 1)
136                 return -E2BIG;
137         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
138
139         ifr.ifr_addr.sa_family = AF_INET;
140         rc = lnet_sock_ioctl(SIOCGIFNETMASK, (unsigned long)&ifr);
141         if (rc) {
142                 CERROR("Can't get netmask for interface %s\n", name);
143                 return rc;
144         }
145
146         val = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr.s_addr;
147         *mask = ntohl(val);
148
149         return 0;
150 }
151 EXPORT_SYMBOL(lnet_ipif_query);
152
153 int
154 lnet_ipif_enumerate(char ***namesp)
155 {
156         /* Allocate and fill in 'names', returning # interfaces/error */
157         char **names;
158         int toobig;
159         int nalloc;
160         int nfound;
161         struct ifreq *ifr;
162         struct ifconf ifc;
163         int rc;
164         int nob;
165         int i;
166
167         nalloc = 16;    /* first guess at max interfaces */
168         toobig = 0;
169         for (;;) {
170                 if (nalloc * sizeof(*ifr) > PAGE_SIZE) {
171                         toobig = 1;
172                         nalloc = PAGE_SIZE / sizeof(*ifr);
173                         CWARN("Too many interfaces: only enumerating first %d\n",
174                               nalloc);
175                 }
176
177                 LIBCFS_ALLOC(ifr, nalloc * sizeof(*ifr));
178                 if (!ifr) {
179                         CERROR("ENOMEM enumerating up to %d interfaces\n",
180                                nalloc);
181                         rc = -ENOMEM;
182                         goto out0;
183                 }
184
185                 ifc.ifc_buf = (char *)ifr;
186                 ifc.ifc_len = nalloc * sizeof(*ifr);
187
188                 rc = lnet_sock_ioctl(SIOCGIFCONF, (unsigned long)&ifc);
189                 if (rc < 0) {
190                         CERROR("Error %d enumerating interfaces\n", rc);
191                         goto out1;
192                 }
193
194                 LASSERT(!rc);
195
196                 nfound = ifc.ifc_len / sizeof(*ifr);
197                 LASSERT(nfound <= nalloc);
198
199                 if (nfound < nalloc || toobig)
200                         break;
201
202                 LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
203                 nalloc *= 2;
204         }
205
206         if (!nfound)
207                 goto out1;
208
209         LIBCFS_ALLOC(names, nfound * sizeof(*names));
210         if (!names) {
211                 rc = -ENOMEM;
212                 goto out1;
213         }
214
215         for (i = 0; i < nfound; i++) {
216                 nob = strnlen(ifr[i].ifr_name, IFNAMSIZ);
217                 if (nob == IFNAMSIZ) {
218                         /* no space for terminating NULL */
219                         CERROR("interface name %.*s too long (%d max)\n",
220                                nob, ifr[i].ifr_name, IFNAMSIZ);
221                         rc = -ENAMETOOLONG;
222                         goto out2;
223                 }
224
225                 LIBCFS_ALLOC(names[i], IFNAMSIZ);
226                 if (!names[i]) {
227                         rc = -ENOMEM;
228                         goto out2;
229                 }
230
231                 memcpy(names[i], ifr[i].ifr_name, nob);
232                 names[i][nob] = 0;
233         }
234
235         *namesp = names;
236         rc = nfound;
237
238 out2:
239         if (rc < 0)
240                 lnet_ipif_free_enumeration(names, nfound);
241 out1:
242         LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
243 out0:
244         return rc;
245 }
246 EXPORT_SYMBOL(lnet_ipif_enumerate);
247
248 void
249 lnet_ipif_free_enumeration(char **names, int n)
250 {
251         int i;
252
253         LASSERT(n > 0);
254
255         for (i = 0; i < n && names[i]; i++)
256                 LIBCFS_FREE(names[i], IFNAMSIZ);
257
258         LIBCFS_FREE(names, n * sizeof(*names));
259 }
260 EXPORT_SYMBOL(lnet_ipif_free_enumeration);
261
262 int
263 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
264 {
265         int rc;
266         long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
267         unsigned long then;
268         struct timeval tv;
269         struct kvec  iov = { .iov_base = buffer, .iov_len  = nob };
270         struct msghdr msg = {NULL,};
271
272         LASSERT(nob > 0);
273         /*
274          * Caller may pass a zero timeout if she thinks the socket buffer is
275          * empty enough to take the whole message immediately
276          */
277         iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iov, 1, nob);
278         for (;;) {
279                 msg.msg_flags = !timeout ? MSG_DONTWAIT : 0;
280                 if (timeout) {
281                         /* Set send timeout to remaining time */
282                         jiffies_to_timeval(jiffies_left, &tv);
283                         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
284                                                (char *)&tv, sizeof(tv));
285                         if (rc) {
286                                 CERROR("Can't set socket send timeout %ld.%06d: %d\n",
287                                        (long)tv.tv_sec, (int)tv.tv_usec, rc);
288                                 return rc;
289                         }
290                 }
291
292                 then = jiffies;
293                 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
294                 jiffies_left -= jiffies - then;
295
296                 if (rc < 0)
297                         return rc;
298
299                 if (!rc) {
300                         CERROR("Unexpected zero rc\n");
301                         return -ECONNABORTED;
302                 }
303
304                 if (!msg_data_left(&msg))
305                         break;
306
307                 if (jiffies_left <= 0)
308                         return -EAGAIN;
309         }
310         return 0;
311 }
312 EXPORT_SYMBOL(lnet_sock_write);
313
314 int
315 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
316 {
317         int rc;
318         long jiffies_left = timeout * msecs_to_jiffies(MSEC_PER_SEC);
319         unsigned long then;
320         struct timeval tv;
321
322         LASSERT(nob > 0);
323         LASSERT(jiffies_left > 0);
324
325         for (;;) {
326                 struct kvec  iov = {
327                         .iov_base = buffer,
328                         .iov_len  = nob
329                 };
330                 struct msghdr msg = {
331                         .msg_flags = 0
332                 };
333
334                 /* Set receive timeout to remaining time */
335                 jiffies_to_timeval(jiffies_left, &tv);
336                 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
337                                        (char *)&tv, sizeof(tv));
338                 if (rc) {
339                         CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
340                                (long)tv.tv_sec, (int)tv.tv_usec, rc);
341                         return rc;
342                 }
343
344                 then = jiffies;
345                 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
346                 jiffies_left -= jiffies - then;
347
348                 if (rc < 0)
349                         return rc;
350
351                 if (!rc)
352                         return -ECONNRESET;
353
354                 buffer = ((char *)buffer) + rc;
355                 nob -= rc;
356
357                 if (!nob)
358                         return 0;
359
360                 if (jiffies_left <= 0)
361                         return -ETIMEDOUT;
362         }
363 }
364 EXPORT_SYMBOL(lnet_sock_read);
365
366 static int
367 lnet_sock_create(struct socket **sockp, int *fatal, __u32 local_ip,
368                  int local_port)
369 {
370         struct sockaddr_in locaddr;
371         struct socket *sock;
372         int rc;
373         int option;
374
375         /* All errors are fatal except bind failure if the port is in use */
376         *fatal = 1;
377
378         rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
379         *sockp = sock;
380         if (rc) {
381                 CERROR("Can't create socket: %d\n", rc);
382                 return rc;
383         }
384
385         option = 1;
386         rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
387                                (char *)&option, sizeof(option));
388         if (rc) {
389                 CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
390                 goto failed;
391         }
392
393         if (local_ip || local_port) {
394                 memset(&locaddr, 0, sizeof(locaddr));
395                 locaddr.sin_family = AF_INET;
396                 locaddr.sin_port = htons(local_port);
397                 if (!local_ip)
398                         locaddr.sin_addr.s_addr = htonl(INADDR_ANY);
399                 else
400                         locaddr.sin_addr.s_addr = htonl(local_ip);
401
402                 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
403                                  sizeof(locaddr));
404                 if (rc == -EADDRINUSE) {
405                         CDEBUG(D_NET, "Port %d already in use\n", local_port);
406                         *fatal = 0;
407                         goto failed;
408                 }
409                 if (rc) {
410                         CERROR("Error trying to bind to port %d: %d\n",
411                                local_port, rc);
412                         goto failed;
413                 }
414         }
415         return 0;
416
417 failed:
418         sock_release(sock);
419         return rc;
420 }
421
422 int
423 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
424 {
425         int option;
426         int rc;
427
428         if (txbufsize) {
429                 option = txbufsize;
430                 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
431                                        (char *)&option, sizeof(option));
432                 if (rc) {
433                         CERROR("Can't set send buffer %d: %d\n",
434                                option, rc);
435                         return rc;
436                 }
437         }
438
439         if (rxbufsize) {
440                 option = rxbufsize;
441                 rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
442                                        (char *)&option, sizeof(option));
443                 if (rc) {
444                         CERROR("Can't set receive buffer %d: %d\n",
445                                option, rc);
446                         return rc;
447                 }
448         }
449         return 0;
450 }
451 EXPORT_SYMBOL(lnet_sock_setbuf);
452
453 int
454 lnet_sock_getaddr(struct socket *sock, bool remote, __u32 *ip, int *port)
455 {
456         struct sockaddr_in sin;
457         int len = sizeof(sin);
458         int rc;
459
460         if (remote)
461                 rc = kernel_getpeername(sock, (struct sockaddr *)&sin, &len);
462         else
463                 rc = kernel_getsockname(sock, (struct sockaddr *)&sin, &len);
464         if (rc) {
465                 CERROR("Error %d getting sock %s IP/port\n",
466                        rc, remote ? "peer" : "local");
467                 return rc;
468         }
469
470         if (ip)
471                 *ip = ntohl(sin.sin_addr.s_addr);
472
473         if (port)
474                 *port = ntohs(sin.sin_port);
475
476         return 0;
477 }
478 EXPORT_SYMBOL(lnet_sock_getaddr);
479
480 int
481 lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
482 {
483         if (txbufsize)
484                 *txbufsize = sock->sk->sk_sndbuf;
485
486         if (rxbufsize)
487                 *rxbufsize = sock->sk->sk_rcvbuf;
488
489         return 0;
490 }
491 EXPORT_SYMBOL(lnet_sock_getbuf);
492
493 int
494 lnet_sock_listen(struct socket **sockp, __u32 local_ip, int local_port,
495                  int backlog)
496 {
497         int fatal;
498         int rc;
499
500         rc = lnet_sock_create(sockp, &fatal, local_ip, local_port);
501         if (rc) {
502                 if (!fatal)
503                         CERROR("Can't create socket: port %d already in use\n",
504                                local_port);
505                 return rc;
506         }
507
508         rc = kernel_listen(*sockp, backlog);
509         if (!rc)
510                 return 0;
511
512         CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
513         sock_release(*sockp);
514         return rc;
515 }
516
517 int
518 lnet_sock_accept(struct socket **newsockp, struct socket *sock)
519 {
520         wait_queue_entry_t wait;
521         struct socket *newsock;
522         int rc;
523
524         /*
525          * XXX this should add a ref to sock->ops->owner, if
526          * TCP could be a module
527          */
528         rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
529         if (rc) {
530                 CERROR("Can't allocate socket\n");
531                 return rc;
532         }
533
534         newsock->ops = sock->ops;
535
536         rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
537         if (rc == -EAGAIN) {
538                 /* Nothing ready, so wait for activity */
539                 init_waitqueue_entry(&wait, current);
540                 add_wait_queue(sk_sleep(sock->sk), &wait);
541                 set_current_state(TASK_INTERRUPTIBLE);
542                 schedule();
543                 remove_wait_queue(sk_sleep(sock->sk), &wait);
544                 rc = sock->ops->accept(sock, newsock, O_NONBLOCK, false);
545         }
546
547         if (rc)
548                 goto failed;
549
550         *newsockp = newsock;
551         return 0;
552
553 failed:
554         sock_release(newsock);
555         return rc;
556 }
557
558 int
559 lnet_sock_connect(struct socket **sockp, int *fatal, __u32 local_ip,
560                   int local_port, __u32 peer_ip, int peer_port)
561 {
562         struct sockaddr_in srvaddr;
563         int rc;
564
565         rc = lnet_sock_create(sockp, fatal, local_ip, local_port);
566         if (rc)
567                 return rc;
568
569         memset(&srvaddr, 0, sizeof(srvaddr));
570         srvaddr.sin_family = AF_INET;
571         srvaddr.sin_port = htons(peer_port);
572         srvaddr.sin_addr.s_addr = htonl(peer_ip);
573
574         rc = kernel_connect(*sockp, (struct sockaddr *)&srvaddr,
575                             sizeof(srvaddr), 0);
576         if (!rc)
577                 return 0;
578
579         /*
580          * EADDRNOTAVAIL probably means we're already connected to the same
581          * peer/port on the same local port on a differently typed
582          * connection.  Let our caller retry with a different local
583          * port...
584          */
585         *fatal = !(rc == -EADDRNOTAVAIL);
586
587         CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
588                      "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
589                      &local_ip, local_port, &peer_ip, peer_port);
590
591         sock_release(*sockp);
592         return rc;
593 }