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