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/
31 #define DEBUG_SUBSYSTEM S_LNET
35 #include <linux/net.h>
36 #include <net/addrconf.h>
38 #include <linux/file.h>
39 #include <linux/pagemap.h>
40 /* For sys_open & sys_close */
41 #include <linux/syscalls.h>
43 #include <linux/inetdevice.h>
45 #include <libcfs/linux/linux-time.h>
46 #include <libcfs/linux/linux-net.h>
47 #include <libcfs/libcfs.h>
48 #include <lnet/lib-lnet.h>
51 lnet_sock_write(struct socket *sock, void *buffer, int nob, int timeout)
54 long jiffies_left = cfs_time_seconds(timeout);
58 /* Caller may pass a zero timeout if she thinks the socket buffer is
59 * empty enough to take the whole message immediately */
67 .msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0
71 struct sock *sk = sock->sk;
73 /* Set send timeout to remaining time */
75 sk->sk_sndtimeo = jiffies_left;
80 rc = kernel_sendmsg(sock, &msg, &iov, 1, nob);
81 jiffies_left -= jiffies - then;
90 CERROR("Unexpected zero rc\n");
94 if (jiffies_left <= 0)
97 buffer = ((char *)buffer) + rc;
102 EXPORT_SYMBOL(lnet_sock_write);
105 lnet_sock_read(struct socket *sock, void *buffer, int nob, int timeout)
108 long jiffies_left = cfs_time_seconds(timeout);
112 LASSERT(jiffies_left > 0);
119 struct msghdr msg = {
122 struct sock *sk = sock->sk;
124 /* Set receive timeout to remaining time */
126 sk->sk_rcvtimeo = jiffies_left;
130 rc = kernel_recvmsg(sock, &msg, &iov, 1, nob, 0);
131 jiffies_left -= jiffies - then;
139 buffer = ((char *)buffer) + rc;
145 if (jiffies_left <= 0)
149 EXPORT_SYMBOL(lnet_sock_read);
151 int choose_ipv4_src(__u32 *ret, int interface, __u32 dst_ipaddr, struct net *ns)
153 struct net_device *dev;
154 struct in_device *in_dev;
156 DECLARE_CONST_IN_IFADDR(ifa);
159 dev = dev_get_by_index_rcu(ns, interface);
161 if (!dev || !(dev->flags & IFF_UP))
163 in_dev = __in_dev_get_rcu(dev);
167 in_dev_for_each_ifa_rcu(ifa, in_dev) {
169 ((dst_ipaddr ^ ntohl(ifa->ifa_local))
170 & ntohl(ifa->ifa_mask)) == 0) {
171 /* This address at least as good as what we
174 *ret = ntohl(ifa->ifa_local);
183 EXPORT_SYMBOL(choose_ipv4_src);
185 static struct socket *
186 lnet_sock_create(int interface, struct sockaddr *remaddr,
187 int local_port, struct net *ns)
195 family = remaddr->sa_family;
197 #ifdef HAVE_SOCK_CREATE_KERN_USE_NET
198 rc = sock_create_kern(ns, family, SOCK_STREAM, 0, &sock);
200 rc = sock_create_kern(family, SOCK_STREAM, 0, &sock);
202 if (rc == -EAFNOSUPPORT && family == AF_INET6 && !remaddr) {
208 CERROR("Can't create socket: %d\n", rc);
212 sock->sk->sk_reuseport = 1;
214 if (interface >= 0 || local_port != 0) {
215 struct sockaddr_storage locaddr = {};
219 struct sockaddr_in *sin = (void *)&locaddr;
221 sin->sin_family = AF_INET;
222 sin->sin_addr.s_addr = INADDR_ANY;
223 if (interface >= 0 && remaddr) {
224 struct sockaddr_in *rem = (void *)remaddr;
227 rc = choose_ipv4_src(&ip,
229 ntohl(rem->sin_addr.s_addr),
233 sin->sin_addr.s_addr = htonl(ip);
235 sin->sin_port = htons(local_port);
238 #if IS_ENABLED(CONFIG_IPV6)
240 struct sockaddr_in6 *sin6 = (void *)&locaddr;
242 sin6->sin6_family = AF_INET6;
243 sin6->sin6_addr = in6addr_any;
244 if (interface >= 0 && remaddr) {
245 struct sockaddr_in6 *rem = (void *)remaddr;
247 ipv6_dev_get_saddr(ns,
253 sin6->sin6_port = htons(local_port);
256 #endif /* IS_ENABLED(CONFIG_IPV6) */
258 rc = kernel_bind(sock, (struct sockaddr *)&locaddr,
260 if (rc == -EADDRINUSE) {
261 CDEBUG(D_NET, "Port %d already in use\n", local_port);
265 CERROR("Error trying to bind to port %d: %d\n",
278 lnet_sock_setbuf(struct socket *sock, int txbufsize, int rxbufsize)
280 struct sock *sk = sock->sk;
282 if (txbufsize != 0) {
283 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
284 sk->sk_sndbuf = txbufsize;
285 sk->sk_write_space(sk);
288 if (rxbufsize != 0) {
289 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
290 sk->sk_sndbuf = rxbufsize;
293 EXPORT_SYMBOL(lnet_sock_setbuf);
296 lnet_sock_getaddr(struct socket *sock, bool remote,
297 struct sockaddr_storage *peer)
300 #ifndef HAVE_KERN_SOCK_GETNAME_2ARGS
301 int len = sizeof(*peer);
305 rc = lnet_kernel_getpeername(sock,
306 (struct sockaddr *)peer, &len);
308 rc = lnet_kernel_getsockname(sock,
309 (struct sockaddr *)peer, &len);
311 CERROR("Error %d getting sock %s IP/port\n",
312 rc, remote ? "peer" : "local");
315 if (peer->ss_family == AF_INET6) {
316 struct sockaddr_in6 *in6 = (void *)peer;
317 struct sockaddr_in *in = (void *)peer;
318 short port = in6->sin6_port;
320 if (ipv6_addr_v4mapped(&in6->sin6_addr)) {
321 /* Pretend it is a v4 socket */
322 memset(in, 0, sizeof(*in));
323 in->sin_family = AF_INET;
325 memcpy(&in->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
330 EXPORT_SYMBOL(lnet_sock_getaddr);
332 void lnet_sock_getbuf(struct socket *sock, int *txbufsize, int *rxbufsize)
334 if (txbufsize != NULL)
335 *txbufsize = sock->sk->sk_sndbuf;
337 if (rxbufsize != NULL)
338 *rxbufsize = sock->sk->sk_rcvbuf;
340 EXPORT_SYMBOL(lnet_sock_getbuf);
343 lnet_sock_listen(int local_port, int backlog, struct net *ns)
349 sock = lnet_sock_create(-1, NULL, local_port, ns);
352 if (rc == -EADDRINUSE)
353 CERROR("Can't create socket: port %d already in use\n",
358 /* Make sure we get both IPv4 and IPv6 connections.
359 * This is the default, but it can be overridden so
362 #ifdef HAVE_KERNEL_SETSOCKOPT
363 kernel_setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
364 (char *) &val, sizeof(val));
365 #elif defined(_LINUX_SOCKPTR_H)
366 /* sockptr_t was introduced around v5.8-rc4-1952-ga7b75c5a8c41
367 * and allows a kernel address to be passed to ->setsockopt
369 if (ipv6_only_sock(sock->sk)) {
370 sockptr_t optval = KERNEL_SOCKPTR(&val);
371 sock->ops->setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
372 optval, sizeof(val));
375 /* From v5.7-rc6-2614-g5a892ff2facb when kernel_setsockopt()
376 * was removed until sockptr_t (above) there is no clean
377 * way to pass kernel address to setsockopt. We could use
378 * get_fs()/set_fs(), but in this particular situation there
380 * It depends on the fact that at least for these few kernels
381 * a NULL address to ipv6_setsockopt() is treated like the address
384 if (ipv6_only_sock(sock->sk) && !val) {
386 sock->ops->setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
387 optval, sizeof(val));
391 rc = kernel_listen(sock, backlog);
395 CERROR("Can't set listen backlog %d: %d\n", backlog, rc);
401 lnet_sock_connect(int interface, int local_port,
402 struct sockaddr *peeraddr,
408 sock = lnet_sock_create(interface, peeraddr, local_port, ns);
412 rc = kernel_connect(sock, peeraddr, sizeof(struct sockaddr_in6), 0);
416 /* EADDRNOTAVAIL probably means we're already connected to the same
417 * peer/port on the same local port on a differently typed
418 * connection. Let our caller retry with a different local
421 CDEBUG_LIMIT(rc == -EADDRNOTAVAIL ? D_NET : D_NETERROR,
422 "Error %d connecting %d -> %pISp\n", rc,
423 local_port, peeraddr);