Issue with SO_BINDTODEVICE

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Issue with SO_BINDTODEVICE

Oliver Hitz
Hi all,

I have a device with two network interfaces, en0 and en1. I would like
lwip to consider them as separate network interfaces.

As a test, I have installed two DHCP servers in two networks and I'm
assigning each interface an IP address in the same IP range. This works
perfectly. Each IP address is only visible on the interface to which it
was assigned. No ARP or other traffic mentioning the IP address is
"leaked" to the wrong interface. So far so good.

Next I have tried to set up a TCP server which is bound to only "en0"
using SO_BINDTOSOCKET, but without success. It seems as if that call
doesn't have any effect at all. I can connect to the socket from en1
though I would expect to get a "Connection refused". On the contrary,
connecting from en0 causes a SYN-ACK packet to be send out on en1.

My code is as follows:

    int sock;
    int error;

    if ((sock = lwip_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        ...
    }

    struct ifreq dev = { .ifr_name = "en0" };
    error = lwip_setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (const void *) &dev, sizeof(dev));
    if (error) {
        ...
    }

    struct sockaddr_in address = {
        .sin_family = AF_INET,
        .sin_port = htons(1234),
        .sin_addr.s_addr = INADDR_ANY
    };

    error = lwip_bind(sock, (struct sockaddr *) &address, sizeof(address));
    if (error) {
        ....
    }

    lwip_listen(sock, 0);

    while (1) {
        struct sockaddr remote;
        socklen_t size;

        int new_sd = lwip_accept(sock, &remote, &size);
        if (new_sd > 0) {
            lwip_send(new_sd, "Hello", sizeof("Hello"), 0);
            lwip_close(new_sd);
        }
    }

Implementing the same sequence of commands on Linux works.

Any ideas what I could be missing?

Best regards

Oliver

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Issue with SO_BINDTODEVICE

Oliver Hitz
I have found the problem: It seems that lwip_listen() creates a new pcb
but doesn't copy the netbuf_idx field from the original pcb. If
lwip_setsockopt() is called after lwip_listen(), it works as expected.

I will submit a patch which fixes this behaviour, as I think we
shouldn't have to listen on all interfaces first before restricting it
to a single interface.

Regards

Oliver

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Issue with SO_BINDTODEVICE

Oliver Hitz
On 27 Oct 2020, Oliver Hitz wrote:
> I have found the problem: It seems that lwip_listen() creates a new pcb
> but doesn't copy the netbuf_idx field from the original pcb. If

Sorry, this should read "netif_idx" of course.

Oliver

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

signature.asc (849 bytes) Download Attachment