NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: kern/59486: compat_linux should ignore IP_MULTICAST_ALL



hi team, christoph,

first of all, attaching the latest patch and the rest to play with....

On 6/27/25 10:10, Christoph Badura via gnats wrote:
Isn't this in the wrong places? level is IPPROTO_IP not IPPROTO_UDP.

field testing showed that i was wrong in the original patch about the _udp part, spot level=1:

# LD_PRELOAD=/rtr/sockhook.so /java/bin/java -jar /rtr/rtr.jar routerc /rtr/rtr-


  ####                       ##################
 ##                                  ##
 ##  ## ###   #####   #####  ## ###  ## ## ###
####  ### ## ##   ## ##   ##  ### ## ##  ### ##
 ##   ##  ## ####### #######  ##  ## ##  ##  ##
 ##   ##     ##      ##       ##     ##  ##
 ##   ##     ##   ## ##   ##  ##     ##  ##
 ##   ##      #####   #####   ##     ##  ##

freeRouter v25.6.27-cur, done by sprscc13@mrn0b0dy.

place on the web: http://www.freertr.org/
license: http://creativecommons.org/licenses/by-sa/4.0/
the beer-ware license for selected group of people:
sprscc13@mrn0b0dy wrote these files. as long as you retain this notice you
can do whatever you want with this stuff. if we meet some day, and
you think this stuff is worth it, you can buy me a beer in return

info cfgInit.doInit:cfgInit.java:1168 booting
info cfgInit.doInit:cfgInit.java:1417 initializing hardware
hook called level=1 optname=15 res=-1 errno=22!
hook called level=0 optname=49 res=-1 errno=92!
hook called level=1 optname=6 res=0 errno=92!
hook called level=1 optname=2 res=0 errno=92!
hook called level=1 optname=8 res=-1 errno=105!
hook called level=1 optname=7 res=-1 errno=105!
info cfgInit.doInit:cfgInit.java:1423 applying defaults
info cfgInit.doInit:cfgInit.java:1450 applying configuration
info cfgInit.doInit:cfgInit.java:1487 boot completed
welcome
line ready
yourname#

ps: with the patch in the module, it works without the hook faking success too.... :)



  Wouldn't it be better to return ENOPROTOOPT?  Because that's what the java
  code expects?

cannot disagree, its better not to fake a success....


  Also, you're missing the equivalent changes for IPv6.

just added things needed for a plain [::1]:1234, see in the updated test java code....


  Could you update your changes to be more complete?

above the already discussed changes, now i added the getsockopts aligned to the old and new return codes too...


> Did you check that we don't need to translate any structures?
>

so the now passing-through setsockopts deals with raw options as they appear on the wire so i assume no extra work needed here...


> Thank you for working on this.
>

it's my pleasure that you're giving me feedback (and hopefully accepting the patch when things got sorted out:)


have a nice weekend,

csaba
diff -crB a/linux_socket.c b/linux_socket.c
*** a/linux_socket.c	2021-09-23 08:56:27.000000000 +0200
--- b/linux_socket.c	2025-06-27 13:16:32.420057642 +0200
***************
*** 945,950 ****
--- 945,956 ----
  		return IP_TOS;
  	case LINUX_IP_TTL:
  		return IP_TTL;
+ 	case LINUX_IP_RETOPTS:
+ 		return IP_RETOPTS;
+ 	case LINUX_IP_PKTINFO:
+ 		return IP_PKTINFO;
+ 	case LINUX_IP_RECVOPTS:
+ 		return IP_RECVOPTS;
  	case LINUX_IP_HDRINCL:
  		return IP_HDRINCL;
  	case LINUX_IP_MULTICAST_TTL:
***************
*** 958,964 ****
--- 964,973 ----
  	case LINUX_IP_DROP_MEMBERSHIP:
  		return IP_DROP_MEMBERSHIP;
  	case LINUX_IP_RECVERR:
+ 	case LINUX_IP_FREEBIND:
  		return -2;	/* ignored */
+ 	case LINUX_IP_MULTICAST_ALL:
+ 		return -3;	/* noprotoopt */
  	default:
  		return -1;
  	}
***************
*** 975,980 ****
--- 984,993 ----
  	switch (lopt) {
  	case LINUX_IPV6_V6ONLY:
  		return IPV6_V6ONLY;
+ 	case LINUX_IPV6_MULTICAST_HOPS:
+ 		return IPV6_MULTICAST_HOPS;
+ 	case LINUX_IPV6_MULTICAST_ALL:
+ 		return -3;	/* noprotoopt */
  	default:
  		return -1;
  	}
***************
*** 1075,1084 ****
  		return EINVAL;
  	}
  
! 	if (name == -1)
  		return EINVAL;
! 	if (name == -2)
  		return 0;
  	SCARG(&bsa, name) = name;
  
  	return sys_setsockopt(l, &bsa, retval);
--- 1088,1101 ----
  		return EINVAL;
  	}
  
! 	switch (name) {
! 	case -1:
  		return EINVAL;
! 	case -2:
  		return 0;
+ 	case -3:
+ 		return ENOPROTOOPT;
+ 	}
  	SCARG(&bsa, name) = name;
  
  	return sys_setsockopt(l, &bsa, retval);
***************
*** 1127,1134 ****
  		return EINVAL;
  	}
  
! 	if (name == -1)
  		return EINVAL;
  	SCARG(&bga, name) = name;
  
  	return sys_getsockopt(l, &bga, retval);
--- 1144,1157 ----
  		return EINVAL;
  	}
  
! 	switch (name) {
! 	case -1:
! 		return EINVAL;
! 	case -2:
  		return EINVAL;
+ 	case -3:
+ 		return ENOPROTOOPT;
+ 	}
  	SCARG(&bga, name) = name;
  
  	return sys_getsockopt(l, &bga, retval);
diff -crB a/linux_socket.h b/linux_socket.h
*** a/linux_socket.h	2021-09-23 08:56:27.000000000 +0200
--- b/linux_socket.h	2025-06-27 11:47:47.911425969 +0200
***************
*** 104,121 ****
--- 104,129 ----
  #define LINUX_IP_TOS		1
  #define LINUX_IP_TTL		2
  #define LINUX_IP_HDRINCL	3
+ #define LINUX_IP_RECVOPTS	6
+ #define LINUX_IP_RETOPTS	7
+ #define LINUX_IP_PKTINFO	8
  #define LINUX_IP_RECVERR	11
+ #define LINUX_IP_FREEBIND	15
  #define	LINUX_IP_MULTICAST_IF	32
  #define	LINUX_IP_MULTICAST_TTL	33
  #define	LINUX_IP_MULTICAST_LOOP	34
  #define	LINUX_IP_ADD_MEMBERSHIP	35
  #define	LINUX_IP_DROP_MEMBERSHIP 36
+ #define LINUX_IP_MULTICAST_ALL	49
  
  /*
   * Options for [gs]etsockopt(2), IPV6 level.
   */
  
  #define LINUX_IPV6_V6ONLY		26
+ #define LINUX_IPV6_MULTICAST_HOPS	18
+ #define LINUX_IPV6_MULTICAST_ALL	29
+ 
  
  /*
   * Options for [gs]etsockopt(2), TCP level.
https://github.com/openjdk/jdk/blob/master/src/java.base/unix/native/libnio/ch/Net.c#L300

https://github.com/NetBSD/src/blob/trunk/sys/compat/linux/common/linux_socket.c#L1080

gcc -shared -fPIC sockhook.c -o sockhook.so -ldl

javac socktest.java

diff -crB a b > patch

cd ~/src/sys/compat/linux/common/ ; cp ~/Music/a/* ./ ; patch -p1 < ~/Music/patch

./build.sh -U -O ~/obj -j2 -m amd64 -a x86_64 tools

./build.sh -U -O ~/obj -j2 -m amd64 -a x86_64 kernel=GENERIC

./build.sh -U -O ~/obj -j2 -m amd64 -a x86_64 modules
#include <stdio.h>
#include <sys/socket.h>
#include <dlfcn.h>
#include <errno.h>

#define paramsetsockopt (int fd, int level, int optname, const void *optval, socklen_t optlen)

int (*originalsetsockopt) paramsetsockopt;

int setsockopt paramsetsockopt {
    if (!originalsetsockopt) {
        originalsetsockopt = (int (*) paramsetsockopt )dlsym(RTLD_NEXT, "setsockopt");
    }

    int res = originalsetsockopt(fd, level, optname, optval, optlen);
    printf("hook called level=%i optname=%i res=%i errno=%i!\n", level, optname, res, errno);

    return 0;
}
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Inet6Address;

public class socktest {

    public static void main(String args[]) throws Exception {
        new DatagramSocket().connect(InetAddress.getByName("127.0.0.1"), 1234);
        System.out.println("halfway!");
        new DatagramSocket().connect(Inet6Address.getByName("::1"), 1234);
        System.out.println("ok!");
    }
}


Home | Main Index | Thread Index | Old Index