• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "../../../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
libcfs_sock_ioctl(int cmd,unsigned long arg)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
libcfs_ipif_query(char * name,int * up,__u32 * ip,__u32 * mask)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
libcfs_ipif_enumerate(char *** namesp)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 
202 	for (i = 0; i < nfound; i++) {
203 
204 		nob = strnlen (ifr[i].ifr_name, IFNAMSIZ);
205 		if (nob == IFNAMSIZ) {
206 			/* no space for terminating NULL */
207 			CERROR("interface name %.*s too long (%d max)\n",
208 			       nob, ifr[i].ifr_name, IFNAMSIZ);
209 			rc = -ENAMETOOLONG;
210 			goto out2;
211 		}
212 
213 		LIBCFS_ALLOC(names[i], IFNAMSIZ);
214 		if (names[i] == NULL) {
215 			rc = -ENOMEM;
216 			goto out2;
217 		}
218 
219 		memcpy(names[i], ifr[i].ifr_name, nob);
220 		names[i][nob] = 0;
221 	}
222 
223 	*namesp = names;
224 	rc = nfound;
225 
226  out2:
227 	if (rc < 0)
228 		libcfs_ipif_free_enumeration(names, nfound);
229  out1:
230 	LIBCFS_FREE(ifr, nalloc * sizeof(*ifr));
231  out0:
232 	return rc;
233 }
234 
235 EXPORT_SYMBOL(libcfs_ipif_enumerate);
236 
237 void
libcfs_ipif_free_enumeration(char ** names,int n)238 libcfs_ipif_free_enumeration (char **names, int n)
239 {
240 	int      i;
241 
242 	LASSERT (n > 0);
243 
244 	for (i = 0; i < n && names[i] != NULL; i++)
245 		LIBCFS_FREE(names[i], IFNAMSIZ);
246 
247 	LIBCFS_FREE(names, n * sizeof(*names));
248 }
249 
250 EXPORT_SYMBOL(libcfs_ipif_free_enumeration);
251 
252 int
libcfs_sock_write(struct socket * sock,void * buffer,int nob,int timeout)253 libcfs_sock_write (struct socket *sock, void *buffer, int nob, int timeout)
254 {
255 	int	    rc;
256 	long	   ticks = timeout * HZ;
257 	unsigned long  then;
258 	struct timeval tv;
259 
260 	LASSERT (nob > 0);
261 	/* Caller may pass a zero timeout if she thinks the socket buffer is
262 	 * empty enough to take the whole message immediately */
263 
264 	for (;;) {
265 		struct kvec  iov = {
266 			.iov_base = buffer,
267 			.iov_len  = nob
268 		};
269 		struct msghdr msg = {
270 			.msg_flags      = (timeout == 0) ? MSG_DONTWAIT : 0
271 		};
272 
273 		if (timeout != 0) {
274 			/* Set send timeout to remaining time */
275 			tv = (struct timeval) {
276 				.tv_sec = ticks / HZ,
277 				.tv_usec = ((ticks % HZ) * 1000000) / HZ
278 			};
279 			rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
280 					     (char *)&tv, sizeof(tv));
281 			if (rc != 0) {
282 				CERROR("Can't set socket send timeout "
283 				       "%ld.%06d: %d\n",
284 				       (long)tv.tv_sec, (int)tv.tv_usec, rc);
285 				return rc;
286 			}
287 		}
288 
289 		then = jiffies;
290 		rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
291 		ticks -= jiffies - then;
292 
293 		if (rc == nob)
294 			return 0;
295 
296 		if (rc < 0)
297 			return rc;
298 
299 		if (rc == 0) {
300 			CERROR ("Unexpected zero rc\n");
301 			return -ECONNABORTED;
302 		}
303 
304 		if (ticks <= 0)
305 			return -EAGAIN;
306 
307 		buffer = ((char *)buffer) + rc;
308 		nob -= rc;
309 	}
310 
311 	return 0;
312 }
313 EXPORT_SYMBOL(libcfs_sock_write);
314 
315 int
libcfs_sock_read(struct socket * sock,void * buffer,int nob,int timeout)316 libcfs_sock_read (struct socket *sock, void *buffer, int nob, int timeout)
317 {
318 	int	    rc;
319 	long	   ticks = timeout * HZ;
320 	unsigned long  then;
321 	struct timeval tv;
322 
323 	LASSERT (nob > 0);
324 	LASSERT (ticks > 0);
325 
326 	for (;;) {
327 		struct kvec  iov = {
328 			.iov_base = buffer,
329 			.iov_len  = nob
330 		};
331 		struct msghdr msg = {
332 			.msg_flags      = 0
333 		};
334 
335 		/* Set receive timeout to remaining time */
336 		tv = (struct timeval) {
337 			.tv_sec = ticks / HZ,
338 			.tv_usec = ((ticks % HZ) * 1000000) / HZ
339 		};
340 		rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
341 				     (char *)&tv, sizeof(tv));
342 		if (rc != 0) {
343 			CERROR("Can't set socket recv timeout %ld.%06d: %d\n",
344 			       (long)tv.tv_sec, (int)tv.tv_usec, rc);
345 			return rc;
346 		}
347 
348 		then = jiffies;
349 		rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
350 		ticks -= jiffies - then;
351 
352 		if (rc < 0)
353 			return rc;
354 
355 		if (rc == 0)
356 			return -ECONNRESET;
357 
358 		buffer = ((char *)buffer) + rc;
359 		nob -= rc;
360 
361 		if (nob == 0)
362 			return 0;
363 
364 		if (ticks <= 0)
365 			return -ETIMEDOUT;
366 	}
367 }
368 
369 EXPORT_SYMBOL(libcfs_sock_read);
370 
371 static int
libcfs_sock_create(struct socket ** sockp,int * fatal,__u32 local_ip,int local_port)372 libcfs_sock_create (struct socket **sockp, int *fatal,
373 		    __u32 local_ip, int local_port)
374 {
375 	struct sockaddr_in  locaddr;
376 	struct socket      *sock;
377 	int		 rc;
378 	int		 option;
379 
380 	/* All errors are fatal except bind failure if the port is in use */
381 	*fatal = 1;
382 
383 	rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
384 	*sockp = sock;
385 	if (rc != 0) {
386 		CERROR ("Can't create socket: %d\n", rc);
387 		return rc;
388 	}
389 
390 	option = 1;
391 	rc = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
392 			     (char *)&option, sizeof (option));
393 	if (rc != 0) {
394 		CERROR("Can't set SO_REUSEADDR for socket: %d\n", rc);
395 		goto failed;
396 	}
397 
398 	if (local_ip != 0 || local_port != 0) {
399 		memset(&locaddr, 0, sizeof(locaddr));
400 		locaddr.sin_family = AF_INET;
401 		locaddr.sin_port = htons(local_port);
402 		locaddr.sin_addr.s_addr = (local_ip == 0) ?
403 					  INADDR_ANY : htonl(local_ip);
404 
405 		rc = sock->ops->bind(sock, (struct sockaddr *)&locaddr,
406 				     sizeof(locaddr));
407 		if (rc == -EADDRINUSE) {
408 			CDEBUG(D_NET, "Port %d already in use\n", local_port);
409 			*fatal = 0;
410 			goto failed;
411 		}
412 		if (rc != 0) {
413 			CERROR("Error trying to bind to port %d: %d\n",
414 			       local_port, rc);
415 			goto failed;
416 		}
417 	}
418 
419 	return 0;
420 
421  failed:
422 	sock_release(sock);
423 	return rc;
424 }
425 
426 int
libcfs_sock_setbuf(struct socket * sock,int txbufsize,int rxbufsize)427 libcfs_sock_setbuf (struct socket *sock, int txbufsize, int rxbufsize)
428 {
429 	int		 option;
430 	int		 rc;
431 
432 	if (txbufsize != 0) {
433 		option = txbufsize;
434 		rc = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
435 				     (char *)&option, sizeof (option));
436 		if (rc != 0) {
437 			CERROR ("Can't set send buffer %d: %d\n",
438 				option, rc);
439 			return rc;
440 		}
441 	}
442 
443 	if (rxbufsize != 0) {
444 		option = rxbufsize;
445 		rc = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
446 				      (char *)&option, sizeof (option));
447 		if (rc != 0) {
448 			CERROR ("Can't set receive buffer %d: %d\n",
449 				option, rc);
450 			return rc;
451 		}
452 	}
453 
454 	return 0;
455 }
456 
457 EXPORT_SYMBOL(libcfs_sock_setbuf);
458 
459 int
libcfs_sock_getaddr(struct socket * sock,int remote,__u32 * ip,int * port)460 libcfs_sock_getaddr (struct socket *sock, int remote, __u32 *ip, int *port)
461 {
462 	struct sockaddr_in sin;
463 	int		len = sizeof (sin);
464 	int		rc;
465 
466 	rc = sock->ops->getname (sock, (struct sockaddr *)&sin, &len,
467 				 remote ? 2 : 0);
468 	if (rc != 0) {
469 		CERROR ("Error %d getting sock %s IP/port\n",
470 			rc, remote ? "peer" : "local");
471 		return rc;
472 	}
473 
474 	if (ip != NULL)
475 		*ip = ntohl (sin.sin_addr.s_addr);
476 
477 	if (port != NULL)
478 		*port = ntohs (sin.sin_port);
479 
480 	return 0;
481 }
482 
483 EXPORT_SYMBOL(libcfs_sock_getaddr);
484 
485 int
libcfs_sock_getbuf(struct socket * sock,int * txbufsize,int * rxbufsize)486 libcfs_sock_getbuf (struct socket *sock, int *txbufsize, int *rxbufsize)
487 {
488 
489 	if (txbufsize != NULL) {
490 		*txbufsize = sock->sk->sk_sndbuf;
491 	}
492 
493 	if (rxbufsize != NULL) {
494 		*rxbufsize = sock->sk->sk_rcvbuf;
495 	}
496 
497 	return 0;
498 }
499 
500 EXPORT_SYMBOL(libcfs_sock_getbuf);
501 
502 int
libcfs_sock_listen(struct socket ** sockp,__u32 local_ip,int local_port,int backlog)503 libcfs_sock_listen (struct socket **sockp,
504 		    __u32 local_ip, int local_port, int backlog)
505 {
506 	int      fatal;
507 	int      rc;
508 
509 	rc = libcfs_sock_create(sockp, &fatal, local_ip, local_port);
510 	if (rc != 0) {
511 		if (!fatal)
512 			CERROR("Can't create socket: port %d already in use\n",
513 			       local_port);
514 		return rc;
515 	}
516 
517 	rc = (*sockp)->ops->listen(*sockp, backlog);
518 	if (rc == 0)
519 		return 0;
520 
521 	CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
522 	sock_release(*sockp);
523 	return rc;
524 }
525 
526 EXPORT_SYMBOL(libcfs_sock_listen);
527 
528 int
libcfs_sock_accept(struct socket ** newsockp,struct socket * sock)529 libcfs_sock_accept (struct socket **newsockp, struct socket *sock)
530 {
531 	wait_queue_t   wait;
532 	struct socket *newsock;
533 	int	    rc;
534 
535 	init_waitqueue_entry(&wait, current);
536 
537 	/* XXX this should add a ref to sock->ops->owner, if
538 	 * TCP could be a module */
539 	rc = sock_create_lite(PF_PACKET, sock->type, IPPROTO_TCP, &newsock);
540 	if (rc) {
541 		CERROR("Can't allocate socket\n");
542 		return rc;
543 	}
544 
545 	newsock->ops = sock->ops;
546 
547 	set_current_state(TASK_INTERRUPTIBLE);
548 	add_wait_queue(sk_sleep(sock->sk), &wait);
549 
550 	rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
551 	if (rc == -EAGAIN) {
552 		/* Nothing ready, so wait for activity */
553 		schedule();
554 		rc = sock->ops->accept(sock, newsock, O_NONBLOCK);
555 	}
556 
557 	remove_wait_queue(sk_sleep(sock->sk), &wait);
558 	set_current_state(TASK_RUNNING);
559 
560 	if (rc != 0)
561 		goto failed;
562 
563 	*newsockp = newsock;
564 	return 0;
565 
566  failed:
567 	sock_release(newsock);
568 	return rc;
569 }
570 
571 EXPORT_SYMBOL(libcfs_sock_accept);
572 
573 void
libcfs_sock_abort_accept(struct socket * sock)574 libcfs_sock_abort_accept (struct socket *sock)
575 {
576 	wake_up_all(sk_sleep(sock->sk));
577 }
578 
579 EXPORT_SYMBOL(libcfs_sock_abort_accept);
580 
581 int
libcfs_sock_connect(struct socket ** sockp,int * fatal,__u32 local_ip,int local_port,__u32 peer_ip,int peer_port)582 libcfs_sock_connect (struct socket **sockp, int *fatal,
583 		     __u32 local_ip, int local_port,
584 		     __u32 peer_ip, int peer_port)
585 {
586 	struct sockaddr_in  srvaddr;
587 	int		 rc;
588 
589 	rc = libcfs_sock_create(sockp, fatal, local_ip, local_port);
590 	if (rc != 0)
591 		return rc;
592 
593 	memset (&srvaddr, 0, sizeof (srvaddr));
594 	srvaddr.sin_family = AF_INET;
595 	srvaddr.sin_port = htons(peer_port);
596 	srvaddr.sin_addr.s_addr = htonl(peer_ip);
597 
598 	rc = (*sockp)->ops->connect(*sockp,
599 				    (struct sockaddr *)&srvaddr, sizeof(srvaddr),
600 				    0);
601 	if (rc == 0)
602 		return 0;
603 
604 	/* EADDRNOTAVAIL probably means we're already connected to the same
605 	 * peer/port on the same local port on a differently typed
606 	 * connection.  Let our caller retry with a different local
607 	 * port... */
608 	*fatal = !(rc == -EADDRNOTAVAIL);
609 
610 	CDEBUG_LIMIT(*fatal ? D_NETERROR : D_NET,
611 	       "Error %d connecting %pI4h/%d -> %pI4h/%d\n", rc,
612 	       &local_ip, local_port, &peer_ip, peer_port);
613 
614 	sock_release(*sockp);
615 	return rc;
616 }
617 
618 EXPORT_SYMBOL(libcfs_sock_connect);
619 
620 void
libcfs_sock_release(struct socket * sock)621 libcfs_sock_release (struct socket *sock)
622 {
623 	sock_release(sock);
624 }
625 
626 EXPORT_SYMBOL(libcfs_sock_release);
627