--- old/src/java.base/share/native/libnet/net_util.h 2019-05-14 16:04:53.648130601 +0100 +++ new/src/java.base/share/native/libnet/net_util.h 2019-05-14 16:04:53.400130610 +0100 @@ -155,6 +155,12 @@ SOCKETADDRESS *sa, int *len, jboolean v4MappedAddress); +JNIEXPORT int JNICALL +NET_InetAddressToSockaddr0(JNIEnv *env, jobject iaObj, int port, + SOCKETADDRESS *sa, int *len, + jboolean v4MappedAddress, + jboolean includeScopeId); + JNIEXPORT jobject JNICALL NET_SockaddrToInetAddress(JNIEnv *env, SOCKETADDRESS *sa, int *port); --- old/src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c 2019-05-14 16:04:54.208130582 +0100 +++ new/src/java.base/unix/native/libnet/PlainDatagramSocketImpl.c 2019-05-14 16:04:53.956130591 +0100 @@ -196,8 +196,8 @@ } /* bind */ - if (NET_InetAddressToSockaddr(env, iaObj, localport, &sa, &len, - JNI_TRUE) != 0) { + if (NET_InetAddressToSockaddr0(env, iaObj, localport, &sa, &len, + JNI_TRUE, JNI_TRUE) != 0) { return; } setDefaultScopeID(env, &sa.sa); --- old/src/java.base/unix/native/libnet/net_util_md.c 2019-05-14 16:04:54.788130562 +0100 +++ new/src/java.base/unix/native/libnet/net_util_md.c 2019-05-14 16:04:54.540130570 +0100 @@ -778,6 +778,16 @@ SOCKETADDRESS *sa, int *len, jboolean v4MappedAddress) { + return NET_InetAddressToSockaddr0(env, iaObj, port, sa, len, v4MappedAddress, + JNI_FALSE); +} + +JNIEXPORT int JNICALL +NET_InetAddressToSockaddr0(JNIEnv *env, jobject iaObj, int port, + SOCKETADDRESS *sa, int *len, + jboolean v4MappedAddress, + jboolean includeScopeId) +{ jint family = getInetAddress_family(env, iaObj); JNU_CHECK_EXCEPTION_RETURN(env, -1); memset((char *)sa, 0, sizeof(SOCKETADDRESS)); @@ -818,8 +828,12 @@ #ifdef __linux__ /* - * On Linux if we are connecting to a link-local address - * we need to specify the interface in the scope_id (2.4 kernel only) + * On Linux if we are connecting to a + * + * - link-local address + * - multicast interface-local or link-local address + * + * we need to specify the interface in the scope_id. * * If the scope was cached then we use the cached value. If not cached but * specified in the Inet6Address we use that, but we first check if the @@ -829,7 +843,10 @@ * we try to determine a value from the routing table. In all these * cases the used value is cached for further use. */ - if (IN6_IS_ADDR_LINKLOCAL(&sa->sa6.sin6_addr)) { + if (IN6_IS_ADDR_LINKLOCAL(&sa->sa6.sin6_addr) + || (includeScopeId == JNI_TRUE + && (IN6_IS_ADDR_MC_NODELOCAL(&sa->sa6.sin6_addr) + || IN6_IS_ADDR_MC_LINKLOCAL(&sa->sa6.sin6_addr)))) { unsigned int cached_scope_id = 0, scope_id = 0; if (ia6_cachedscopeidID) { --- old/src/java.base/unix/native/libnio/ch/Net.c 2019-05-14 16:04:55.368130542 +0100 +++ new/src/java.base/unix/native/libnio/ch/Net.c 2019-05-14 16:04:55.112130551 +0100 @@ -281,8 +281,8 @@ int sa_len = 0; int rv = 0; - if (NET_InetAddressToSockaddr(env, iao, port, &sa, &sa_len, - preferIPv6) != 0) { + if (NET_InetAddressToSockaddr0(env, iao, port, &sa, &sa_len, + preferIPv6, JNI_TRUE) != 0) { return; } --- /dev/null 2019-04-23 10:14:30.487678946 +0100 +++ new/test/jdk/java/net/MulticastSocket/PromiscuousIPv6.java 2019-05-14 16:04:55.684130531 +0100 @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + +/* + * @test + * @bug 8215294 + * @requires os.family == "linux" + * @library /test/lib + * @build jdk.test.lib.NetworkConfiguration + * PromiscuousIPv6 + * @run main/othervm PromiscuousIPv6 + * @key randomness + */ + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.Inet6Address; +import java.net.InetSocketAddress; +import java.net.MulticastSocket; +import java.net.NetworkInterface; +import java.net.SocketTimeoutException; +import java.net.StandardSocketOptions; +import java.util.List; +import java.util.Random; +import jdk.test.lib.NetworkConfiguration; +import jtreg.SkippedException; +import static java.lang.System.out; +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.stream.Collectors.toList; + +/* + * This test was created as a clone of the Promiscuous test and adapted for + * IPv6 node-local and link-local multicast addresses on Linux. + */ +public class PromiscuousIPv6 { + + static final Random rand = new Random(); + + static final int TIMEOUT = 5 * 1000; // 5 secs + + static int sendDatagram(NetworkInterface nif, InetAddress group, int port) + throws IOException + { + try (MulticastSocket mc = new MulticastSocket()) { + mc.setOption(StandardSocketOptions.IP_MULTICAST_IF, nif); + + int id = rand.nextInt(); + byte[] msg = Integer.toString(id).getBytes(UTF_8); + DatagramPacket p = new DatagramPacket(msg, msg.length); + p.setAddress(group); + p.setPort(port); + + out.printf("Sending datagram to: %s/%d\n", group, port); + mc.send(p); + return id; + } + } + + static void receiveDatagram(DatagramSocket mc, boolean datagramExpected, int id) + throws IOException + { + byte[] ba = new byte[100]; + DatagramPacket p = new DatagramPacket(ba, ba.length); + try { + mc.receive(p); + int recvId = Integer.parseInt( + new String(p.getData(), 0, p.getLength(), UTF_8)); + if (datagramExpected) { + if (recvId != id) + throw new RuntimeException("Unexpected id, got " + recvId + + ", expected: " + id); + out.printf("Received message as expected, %s\n", p.getAddress()); + } else { + throw new RuntimeException("Unexpected message received, " + + p.getAddress()); + } + } catch (SocketTimeoutException e) { + if (datagramExpected) + throw new RuntimeException("Expected message not received, " + + e.getMessage()); + else + out.printf("Message not received, as expected\n"); + } + } + + static void test(NetworkInterface nif, InetAddress group1, InetAddress group2) + throws IOException + { + // Bind addresses should include the same network interface / scope, so + // as to not reply on the default route when there are multiple interfaces + InetAddress bindAddr1 = Inet6Address.getByAddress(null, group1.getAddress(), nif); + InetAddress bindAddr2 = Inet6Address.getByAddress(null, group2.getAddress(), nif); + + try (MulticastSocket mc1 = new MulticastSocket(new InetSocketAddress(bindAddr1, 0)); + MulticastSocket mc2 = new MulticastSocket(new InetSocketAddress(bindAddr2, mc1.getLocalPort()))) { + + final int port = mc1.getLocalPort(); + out.printf("Using port: %d\n", port); + + mc1.setSoTimeout(TIMEOUT); + mc2.setSoTimeout(TIMEOUT); + + mc1.joinGroup(new InetSocketAddress(group1, 0), nif); + out.printf("mc1 joined the MC group: %s\n", group1); + mc2.joinGroup(new InetSocketAddress(group2, 0), nif); + out.printf("mc2 joined the MC group: %s\n", group2); + + out.printf("Sending datagram to: %s/%d\n", group1, port); + int id = sendDatagram(nif, group1, port); + + // the packet should be received by mc1 only + receiveDatagram(mc1, true, id); + receiveDatagram(mc2, false, 0); + + + out.printf("Sending datagram to: %s/%d\n", group2, port); + id = sendDatagram(nif, group2, port); + + // the packet should be received by mc2 only + receiveDatagram(mc2, true, id); + receiveDatagram(mc1, false, 0); + + mc1.leaveGroup(new InetSocketAddress(group1, 0), nif); + mc2.leaveGroup(new InetSocketAddress(group2, 0), nif); + } + } + + public static void main(String args[]) throws IOException { + String os = System.getProperty("os.name"); + + if (!os.equals("Linux")) { + throw new SkippedException("This test should be run only on Linux"); + } else { + String osVersion = System.getProperty("os.version"); + String prefix = "3.10.0"; + if (osVersion.startsWith(prefix)) { + throw new SkippedException( + String.format("The behavior under test is known NOT to work on '%s' kernels", prefix)); + } + } + + NetworkConfiguration.printSystemConfiguration(System.out); + List nifs = NetworkConfiguration.probe() + .ip6MulticastInterfaces() + .collect(toList()); + + if (nifs.size() == 0) { + throw new SkippedException( + "No IPv6 interfaces that support multicast found"); + } + + InetAddress interfaceLocal1 = InetAddress.getByName("ff11::3.4.5.6"); + InetAddress interfaceLocal2 = InetAddress.getByName("ff11::5.6.7.8"); + + InetAddress linkLocal1 = InetAddress.getByName("ff12::4.5.6.7"); + InetAddress linkLocal2 = InetAddress.getByName("ff12::7.8.9.10"); + + for (NetworkInterface nif : nifs) { + test(nif, interfaceLocal1, interfaceLocal2); + test(nif, linkLocal1, linkLocal2); + } + } +} --- /dev/null 2019-04-23 10:14:30.487678946 +0100 +++ new/test/jdk/java/nio/channels/DatagramChannel/PromiscuousIPv6.java 2019-05-14 16:04:56.268130511 +0100 @@ -0,0 +1,233 @@ +/* + * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + +/* + * @test + * @bug 8215294 + * @requires os.family == "linux" + * @library /test/lib + * @build jdk.test.lib.NetworkConfiguration + * PromiscuousIPv6 + * @run main PromiscuousIPv6 + * @key randomness + */ + +import java.nio.ByteBuffer; +import java.nio.channels.*; +import java.net.*; +import java.util.*; +import java.io.IOException; +import jdk.test.lib.NetworkConfiguration; +import jtreg.SkippedException; +import static java.net.StandardProtocolFamily.*; +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.stream.Collectors.toList; + +/* + * This test was created as a copy of the Promiscuous test and adapted for + * IPv6 node-local and link-local multicast addresses on Linux. + */ +public class PromiscuousIPv6 { + + static final Random rand = new Random(); + + static final ProtocolFamily UNSPEC = () -> "UNSPEC"; + + /** + * Sends a datagram to the given multicast group + */ + static int sendDatagram(NetworkInterface nif, + InetAddress group, + int port) + throws IOException + { + ProtocolFamily family = (group instanceof Inet6Address) ? INET6 : INET; + DatagramChannel dc = DatagramChannel.open(family) + .setOption(StandardSocketOptions.IP_MULTICAST_IF, nif); + int id = rand.nextInt(); + byte[] msg = Integer.toString(id).getBytes(UTF_8); + ByteBuffer buf = ByteBuffer.wrap(msg); + System.out.format("Send message -> group %s (id=0x%x)\n", + group.getHostAddress(), id); + dc.send(buf, new InetSocketAddress(group, port)); + dc.close(); + return id; + } + + /** + * Waits (with timeout) for datagram. The {@code datagramExpected} + * parameter indicates whether a datagram is expected, and if + * {@code true} then {@code id} is the identifier in the payload. + */ + static void receiveDatagram(DatagramChannel dc, + String name, + boolean datagramExpected, + int id) + throws IOException + { + System.out.println("Checking if received by " + name); + + Selector sel = Selector.open(); + dc.configureBlocking(false); + dc.register(sel, SelectionKey.OP_READ); + ByteBuffer buf = ByteBuffer.allocateDirect(100); + + try { + for (;;) { + System.out.println("Waiting to receive message"); + sel.select(5*1000); + SocketAddress sa = dc.receive(buf); + + // no datagram received + if (sa == null) { + if (datagramExpected) { + throw new RuntimeException("Expected message not received"); + } + System.out.println("No message received (correct)"); + return; + } + + // datagram received + + InetAddress sender = ((InetSocketAddress)sa).getAddress(); + buf.flip(); + byte[] bytes = new byte[buf.remaining()]; + buf.get(bytes); + String s = new String(bytes, "UTF-8"); + int receivedId = -1; + try { + receivedId = Integer.parseInt(s); + System.out.format("Received message from %s (id=0x%x)\n", + sender, receivedId); + } catch (NumberFormatException x) { + System.out.format("Received message from %s (msg=%s)\n", sender, s); + } + + if (!datagramExpected) { + if (receivedId == id) + throw new RuntimeException("Message not expected"); + System.out.println("Message ignored (has wrong id)"); + } else { + if (receivedId == id) { + System.out.println("Message expected"); + return; + } + System.out.println("Message ignored (wrong sender)"); + } + + sel.selectedKeys().clear(); + buf.rewind(); + } + } finally { + sel.close(); + } + } + + static void test(ProtocolFamily family, + NetworkInterface nif, + InetAddress group1, + InetAddress group2) + throws IOException + { + + System.out.format("%nTest family=%s%n", family.name()); + + // Bind addresses should include the same network interface / scope, so + // as to not reply on the default route when there are multiple interfaces + InetAddress bindAddr1 = Inet6Address.getByAddress(null, group1.getAddress(), nif); + InetAddress bindAddr2 = Inet6Address.getByAddress(null, group2.getAddress(), nif); + + DatagramChannel dc1 = (family == UNSPEC) ? + DatagramChannel.open() : DatagramChannel.open(family); + DatagramChannel dc2 = (family == UNSPEC) ? + DatagramChannel.open() : DatagramChannel.open(family); + + try { + dc1.setOption(StandardSocketOptions.SO_REUSEADDR, true); + dc2.setOption(StandardSocketOptions.SO_REUSEADDR, true); + + dc1.bind(new InetSocketAddress(bindAddr1, 0)); + int port = dc1.socket().getLocalPort(); + dc2.bind(new InetSocketAddress(bindAddr2, port)); + + System.out.format("dc1 joining [%s]:%d @ %s\n", + group1.getHostAddress(), port, nif.getName()); + System.out.format("dc2 joining [%s]:%d @ %s\n", + group2.getHostAddress(), port, nif.getName()); + + dc1.join(group1, nif); + dc2.join(group2, nif); + + int id = sendDatagram(nif, group1, port); + + receiveDatagram(dc1, "dc1", true, id); + receiveDatagram(dc2, "dc2", false, id); + + id = sendDatagram(nif, group2, port); + + receiveDatagram(dc1, "dc1", false, id); + receiveDatagram(dc2, "dc2", true, id); + + } finally { + dc1.close(); + dc2.close(); + } + } + + public static void main(String[] args) throws IOException { + + String os = System.getProperty("os.name"); + + if (!os.equals("Linux")) { + throw new SkippedException("This test should be run only on Linux"); + } else { + String osVersion = System.getProperty("os.version"); + String prefix = "3.10.0"; + if (osVersion.startsWith(prefix)) { + throw new SkippedException( + String.format("The behavior under test is known NOT to work on '%s' kernels", prefix)); + } + } + + NetworkConfiguration.printSystemConfiguration(System.out); + List nifs = NetworkConfiguration.probe() + .ip6MulticastInterfaces() + .collect(toList()); + + if (nifs.size() == 0) { + throw new SkippedException( + "No IPv6 interfaces that support multicast found"); + } + + InetAddress interfaceLocal1 = InetAddress.getByName("ff11::2.3.4.5"); + InetAddress interfaceLocal2 = InetAddress.getByName("ff11::6.7.8.9"); + + InetAddress linkLocal1 = InetAddress.getByName("ff12::2.3.4.5"); + InetAddress linkLocal2 = InetAddress.getByName("ff12::6.7.8.9"); + + for (NetworkInterface nif : nifs) { + test(INET6, nif, interfaceLocal1, interfaceLocal2); + test(INET6, nif, linkLocal1, linkLocal2); + } + } +}