4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2015, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 #define DEBUG_SUBSYSTEM S_LNET
36 #include <linux/net.h>
37 #include <net/addrconf.h>
39 #include <linux/file.h>
40 #include <linux/pagemap.h>
41 /* For sys_open & sys_close */
42 #include <linux/syscalls.h>
44 #include <linux/inetdevice.h>
46 #include <libcfs/linux/linux-time.h>
47 #include <libcfs/linux/linux-net.h>
48 #include <libcfs/libcfs.h>
49 #include <lnet/lib-lnet.h>
52 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
55 long jiffies_left = cfs_time_seconds(timeout);
59 /* Caller may pass a zero timeout if she thinks the socket buffer is
60 * empty enough to take the whole message immediately */
68 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
72 struct sock *sk = sock->sk;
74 /* Set send timeout to remaining time */
76 sk->sk_sndtimeo = jiffies_left;
81 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
82 jiffies_left -= jiffies - then;
91 CERROR("Unexpected zero rc\n");
95 if (jiffies_left <= 0)
98 buffer = ((char *)buffer) + rc;
103 EXPORT_SYMBOL(lnet_sock_write);
106 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
109 long jiffies_left = cfs_time_seconds(timeout);
113 LASSERT(jiffies_left > 0);
120 struct msghdr msg = {
123 struct sock *sk = sock->sk;
125 /* Set receive timeout to remaining time */
127 sk->sk_rcvtimeo = jiffies_left;
131 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
132 jiffies_left -= jiffies - then;
140 buffer = ((char *)buffer) + rc;
146 if (jiffies_left <= 0)
150 EXPORT_SYMBOL(lnet_sock_read);
152 int choose_ipv4_src(__u32 *ret, int interface, __u32 dst_ipaddr, struct net *ns)
154 struct net_device *dev;
155 struct in_device *in_dev;
157 DECLARE_CONST_IN_IFADDR(ifa);
160 dev = dev_get_by_index_rcu(ns, interface);
162 if (!dev || !(dev->flags & IFF_UP))
164 in_dev = __in_dev_get_rcu(dev);
168 in_dev_for_each_ifa_rcu(ifa, in_dev) {
170 ((dst_ipaddr ^ ntohl(ifa->ifa_local))
171 & ntohl(ifa->ifa_mask)) == 0) {
172 /* This address at least as good as what we
175 *ret = ntohl(ifa->ifa_local);
184 EXPORT_SYMBOL(choose_ipv4_src);
186 static struct socket *
187 lnet_sock_create(int interface, struct sockaddr *remaddr,
188 int local_port, struct net *ns)
196 family = remaddr->sa_family;
198 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
199 rc = sock_create_kern(ns, family, SOCK_STREAM, 0, &sock);
201 rc = sock_create_kern(family, SOCK_STREAM, 0, &sock);
203 if (rc == -EAFNOSUPPORT && family == AF_INET6 && !remaddr) {
209 CERROR("Can't create socket: %d\n", rc);
213 sock->sk->sk_reuseport = 1;
215 if (interface >= 0 || local_port != 0) {
216 struct sockaddr_storage locaddr = {};
220 struct sockaddr_in *sin = (void *)&locaddr;
222 sin->sin_family = AF_INET;
223 sin->sin_addr.s_addr = INADDR_ANY;
224 if (interface >= 0 && remaddr) {
225 struct sockaddr_in *rem = (void *)remaddr;
228 rc = choose_ipv4_src(&ip,
230 ntohl(rem->sin_addr.s_addr),
234 sin->sin_addr.s_addr = htonl(ip);
236 sin->sin_port = htons(local_port);
239 #if IS_ENABLED(CONFIG_IPV6)
241 struct sockaddr_in6 *sin6 = (void *)&locaddr;
243 sin6->sin6_family = AF_INET6;
244 sin6->sin6_addr = in6addr_any;
245 if (interface >= 0 && remaddr) {
246 struct sockaddr_in6 *rem = (void *)remaddr;
248 ipv6_dev_get_saddr(ns,
254 sin6->sin6_port = htons(local_port);
257 #endif /* IS_ENABLED(CONFIG_IPV6) */
259 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
261 if (rc == -EADDRINUSE) {
262 CDEBUG(D_NET, "Port %d already in use\n", local_port);
266 CERROR("Error trying to bind to port %d: %d\n",
279 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
281 struct sock *sk = sock->sk;
283 if (txbufsize != 0) {
284 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
285 sk->sk_sndbuf = txbufsize;
286 sk->sk_write_space(sk);
289 if (rxbufsize != 0) {
290 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
291 sk->sk_sndbuf = rxbufsize;
294 EXPORT_SYMBOL(lnet_sock_setbuf);
297 lnet_sock_getaddr(struct socket *sock, bool remote,
298 struct sockaddr_storage *peer)
301 #ifndef HAVE_KERN_SOCK_GETNAME_2ARGS
302 int len = sizeof(*peer);
306 rc = lnet_kernel_getpeername(sock,
307 (struct sockaddr *)peer, &len);
309 rc = lnet_kernel_getsockname(sock,
310 (struct sockaddr *)peer, &len);
312 CERROR("Error %d getting sock %s IP/port\n",
313 rc, remote ? "peer" : "local");
316 if (peer->ss_family == AF_INET6) {
317 struct sockaddr_in6 *in6 = (void *)peer;
318 struct sockaddr_in *in = (void *)peer;
319 short port = in6->sin6_port;
321 if (ipv6_addr_v4mapped(&in6->sin6_addr)) {
322 /* Pretend it is a v4 socket */
323 memset(in, 0, sizeof(*in));
324 in->sin_family = AF_INET;
326 memcpy(&in->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
331 EXPORT_SYMBOL(lnet_sock_getaddr);
333 void lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
335 if (txbufsize != NULL)
336 *txbufsize = sock->sk->sk_sndbuf;
338 if (rxbufsize != NULL)
339 *rxbufsize = sock->sk->sk_rcvbuf;
341 EXPORT_SYMBOL(lnet_sock_getbuf);
344 lnet_sock_listen(int local_port, int backlog, struct net *ns)
350 sock = lnet_sock_create(-1, NULL, local_port, ns);
353 if (rc == -EADDRINUSE)
354 CERROR("Can't create socket: port %d already in use\n",
359 /* Make sure we get both IPv4 and IPv6 connections.
360 * This is the default, but it can be overridden so
363 kernel_setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
364 (char *) &val, sizeof(val));
366 rc = kernel_listen(sock, backlog);
370 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
376 lnet_sock_connect(int interface, int local_port,
377 struct sockaddr *peeraddr,
383 sock = lnet_sock_create(interface, peeraddr, local_port, ns);
387 rc = kernel_connect(sock, peeraddr, sizeof(struct sockaddr_in6), 0);
391 /* EADDRNOTAVAIL probably means we're already connected to the same
392 * peer/port on the same local port on a differently typed
393 * connection. Let our caller retry with a different local
396 CDEBUG_LIMIT(rc == -EADDRNOTAVAIL ? D_NET : D_NETERROR,
397 "Error %d connecting %d -> %pISp\n", rc,
398 local_port, peeraddr);