Skip to contentSkip to conversation

linux-user: dhclient support

PATCH In review Laurent Vivier 2 participants 12 messages Dec 31, 2012 – Jan 1, 2013

lore
Share & tools
lore
#0 lore raw

cover linux-user: dhclient support

This two patches allow to use dhclient to configure IP addresses in a linux container running the linux-user version of qemu.

[PATCH 1/2] linux-user: Add setsockopt(SO_ATTACH_FILTER) [PATCH 2/2] linux-user: SOCK_PACKET uses network endian to encode

#1 lore raw

PATCH 1/2 linux-user: Add setsockopt(SO_ATTACH_FILTER)

This is needed to be able to run dhclient.

---
 linux-user/syscall.c      |   34 +++++++++++++++++++++++++++++++++-
 linux-user/syscall_defs.h |   12 ++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)
linux-user/syscall.c +33 -1
index e99adab..000b640 100644
@@ -98,6 +98,7 @@ int __clone2(int (*fn)(void *), void *child_stack_base,
 #include <linux/fb.h>
 #include <linux/vt.h>
 #include <linux/dm-ioctl.h>
+#include <linux/filter.h>
 #include "linux_loop.h"
 #include "cpu-uname.h"
 
@@ -1491,6 +1492,38 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
         break;
     case TARGET_SOL_SOCKET:
         switch (optname) {
+        case TARGET_SO_ATTACH_FILTER: {
+                struct target_sock_fprog *tfprog;
+                struct target_sock_filter *tfilter;
+                struct sock_fprog fprog;
+                struct sock_filter *filter;
+                int i;
+
+                if (optlen != sizeof(*tfprog))
+                    return -TARGET_EINVAL;
+                if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0))
+                    return -TARGET_EFAULT;
+                if (!lock_user_struct(VERIFY_READ, tfilter,
+                                      tswapal(tfprog->filter), 0))
+                    return -TARGET_EFAULT;
+
+                fprog.len = tswap16(tfprog->len);
+                filter = alloca(fprog.len * sizeof(*filter));
+                for (i = 0; i < fprog.len; i ++) {
+                    filter[i].code = tswap16(tfilter[i].code);
+                    filter[i].jt = tfilter[i].jt;
+                    filter[i].jf = tfilter[i].jf;
+                    filter[i].k = tswap32(tfilter[i].k);
+                }
+                fprog.filter = filter;
+
+                ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
+                                SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
+
+                unlock_user_struct(tfilter, tfprog->filter, 1);
+                unlock_user_struct(tfprog, optval_addr, 1);
+                return ret;
+        }
             /* Options with 'int' argument.  */
         case TARGET_SO_DEBUG:
 		optname = SO_DEBUG;
@@ -1548,7 +1581,6 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
         case TARGET_SO_SNDTIMEO:
 		optname = SO_SNDTIMEO;
 		break;
-            break;
         default:
             goto unimplemented;
         }
linux-user/syscall_defs.h +12 -0
index d4589e7..501735f 100644
@@ -119,6 +119,18 @@ struct target_sockaddr {
     uint8_t sa_data[14];
 };
 
+struct target_sock_filter {
+    target_ushort code;
+    uint8_t jt;
+    uint8_t jf;
+    target_uint k;
+};
+
+struct target_sock_fprog {
+    target_ushort len;
+    abi_ulong filter;
+};
+
 struct target_in_addr {
     uint32_t s_addr; /* big endian */
 };
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] linux-user: Add setsockopt(SO_ATTACH_FILTER)

Quoted 21 lines · Laurent Vivier

This is needed to be able to run dhclient.

---
 linux-user/syscall.c      |   34 +++++++++++++++++++++++++++++++++-
 linux-user/syscall_defs.h |   12 ++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)
linux-user/syscall.c +1 -0
index e99adab..000b640 100644
@@ -98,6 +98,7 @@ int __clone2(int (*fn)(void *), void *child_stack_base,
 #include <linux/fb.h>
 #include <linux/vt.h>
 #include <linux/dm-ioctl.h>
+#include <linux/filter.h>
 #include "linux_loop.h"
 #include "cpu-uname.h"
 

linux-user/syscall.c static abi_long do_setsockopt(int sockfd, int level, int optname, · line 1495

Quoted 4 lines
linux-user/syscall.c +1 -0
@@ -1491,6 +1492,38 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
         break;
     case TARGET_SOL_SOCKET:
         switch (optname) {
+        case TARGET_SO_ATTACH_FILTER: {

This brace should go on a line of its own (lined up with the 'c' in case) IMHO.

linux-user/syscall.c static abi_long do_setsockopt(int sockfd, int level, int optname, · line 1503

Quoted 8 lines
linux-user/syscall.c +8 -0
+                struct target_sock_fprog *tfprog;
+                struct target_sock_filter *tfilter;
+                struct sock_fprog fprog;
+                struct sock_filter *filter;
+                int i;
+
+                if (optlen != sizeof(*tfprog))
+                    return -TARGET_EINVAL;

QEMU style requires braces on this if. You can use checkpatch.pl to catch this kind of thing.

linux-user/syscall.c static abi_long do_setsockopt(int sockfd, int level, int optname, · line 1508

Quoted 5 lines
linux-user/syscall.c +5 -0
+                if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0))
+                    return -TARGET_EFAULT;
+                if (!lock_user_struct(VERIFY_READ, tfilter,
+                                      tswapal(tfprog->filter), 0))
+                    return -TARGET_EFAULT;

This will fail to unlock tfprog in the failure case.

linux-user/syscall.c static abi_long do_setsockopt(int sockfd, int level, int optname, · line 1511

Quoted 3 lines
linux-user/syscall.c +3 -0
+
+                fprog.len = tswap16(tfprog->len);
+                filter = alloca(fprog.len * sizeof(*filter));

Not sure an unconstrained-size alloca based on data from the guest binary is a fantastic idea (though we no doubt do something similar for some other syscalls).

Quoted 19 lines
linux-user/syscall.c +15 -0
+                for (i = 0; i < fprog.len; i ++) {
+                    filter[i].code = tswap16(tfilter[i].code);
+                    filter[i].jt = tfilter[i].jt;
+                    filter[i].jf = tfilter[i].jf;
+                    filter[i].k = tswap32(tfilter[i].k);
+                }
+                fprog.filter = filter;
+
+                ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
+                                SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
+
+                unlock_user_struct(tfilter, tfprog->filter, 1);
+                unlock_user_struct(tfprog, optval_addr, 1);
+                return ret;
+        }
             /* Options with 'int' argument.  */
         case TARGET_SO_DEBUG:
                optname = SO_DEBUG;

linux-user/syscall.c static abi_long do_setsockopt(int sockfd, int level, int optname, · line 1551

Quoted 4 lines
linux-user/syscall.c +0 -1
@@ -1548,7 +1581,6 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
         case TARGET_SO_SNDTIMEO:
                optname = SO_SNDTIMEO;
                break;
-            break;

Nice catch, but this is an unrelated change that should go in its own patch.

linux-user/syscall.c static abi_long do_setsockopt(int sockfd, int level, int optname, · line 1586

Quoted 3 lines
linux-user/syscall.c +0 -0
         default:
             goto unimplemented;
         }

-- PMM

#3 lore raw

PATCH 2/2 linux-user: SOCK_PACKET uses network endian to encode protocol in socket()

In reply to Laurent Vivier

From: Laurent Vivier <[email protected]>

in PACKET(7) :
protocol is the IEEE 802.3 protocol
number in network order. See the <linux/if_ether.h> include file for a list of allowed protocols. When protocol is set to htons(ETH_P_ALL) then all protocols are received. All incoming packets of that protocol type will be passed to the packet socket before they are passed to the protocols implemented in the kernel.

---
 include/exec/user/abitypes.h |   22 ++++++++++++++++++++++
 linux-user/syscall.c         |    8 +++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)
include/exec/user/abitypes.h +22 -0
index fe7f662..f4f526a 100644
@@ -15,6 +15,15 @@ static inline abi_ulong tswapal(abi_ulong v)
     return tswap32(v);
 }
 
+static inline abi_ulong abi_ntohl(abi_ulong v)
+{
+#if defined(HOST_BIG_ENDIAN)
+    return v;
+#else
+    return bswap_32(v);
+#endif
+}
+
 #else
 typedef target_ulong abi_ulong;
 typedef target_long abi_long;
@@ -32,5 +41,18 @@ static inline abi_ulong tswapal(abi_ulong v)
     return tswapl(v);
 }
 
+static inline abi_ulong abi_ntohl(abi_ulong v)
+{
+#if defined(HOST_BIG_ENDIAN)
+    return v;
+#else
+#if TARGET_LONG_SIZE == 4
+    return bswap_32(v);
+#else
+    return bswap_64(v);
+#endif
+#endif
+}
+
 #endif
 #endif
linux-user/syscall.c +7 -1
index 000b640..29151a6 100644
@@ -1874,7 +1874,7 @@ static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
 }
 
 /* do_socket() Must return target values and target errnos. */
-static abi_long do_socket(int domain, int type, int protocol)
+static abi_long do_socket(int domain, int type, abi_ulong protocol)
 {
 #if defined(TARGET_MIPS)
     switch(type) {
@@ -1900,6 +1900,12 @@ static abi_long do_socket(int domain, int type, int protocol)
 #endif
     if (domain == PF_NETLINK)
         return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
+    if (type == SOCK_PACKET) {
+        /* in this case, socket() needs a network endian short */
+        protocol = tswapal(protocol); /* restore network endian long */
+        protocol = abi_ntohl(protocol); /* a host endian long */
+        protocol = htons(protocol); /* network endian short */
+    }
     return get_errno(socket(domain, type, protocol));
 }
 
Signature · 1 line
1.7.10.4
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: SOCK_PACKET uses network endian to encode protocol in socket()

linux-user/syscall.c static abi_long do_socket(int domain, int type, int protocol) · line 1908

Quoted 10 lines · Laurent Vivier
linux-user/syscall.c +6 -0
@@ -1900,6 +1900,12 @@ static abi_long do_socket(int domain, int type, int protocol)
 #endif
     if (domain == PF_NETLINK)
         return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
+    if (type == SOCK_PACKET) {
+        /* in this case, socket() needs a network endian short */
+        protocol = tswapal(protocol); /* restore network endian long */
+        protocol = abi_ntohl(protocol); /* a host endian long */
+        protocol = htons(protocol); /* network endian short */
+    }

Are you sure this is correct for little endian guests? I've only desk-checked it rather than running a test program, but it looks to me like you end up passing the wrong value to socket().

Also it seems rather involved since we swap things three times and have an entirely new abi_* function. Either I'm completely confused or it should be enough to just have

if (type == SOCK_PACKET) {
      protocol = tswap16(protocol);
}

-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: SOCK_PACKET uses network endian to encode protocol in socket()

Quoted 15 lines · Peter Maydell

linux-user/syscall.c static abi_long do_socket(int domain, int type, int protocol) · line 1908

Quoted 10 lines · Laurent Vivier
linux-user/syscall.c +6 -0
@@ -1900,6 +1900,12 @@ static abi_long do_socket(int domain, int type, int protocol)
 #endif
     if (domain == PF_NETLINK)
         return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
+    if (type == SOCK_PACKET) {
+        /* in this case, socket() needs a network endian short */
+        protocol = tswapal(protocol); /* restore network endian long */
+        protocol = abi_ntohl(protocol); /* a host endian long */
+        protocol = htons(protocol); /* network endian short */
+    }

Are you sure this is correct for little endian guests? I've only desk-checked it rather than running a test program, but it looks to me like you end up passing the wrong value to socket().

I tried to find a solution working in every case.

Quoted 7 lines

Also it seems rather involved since we swap things three times and have an entirely new abi_* function. Either I'm completely confused or it should be enough to just have

if (type == SOCK_PACKET) {
      protocol = tswap16(protocol);
}

works... sometime. In fact, work if target endianess is network endianess.

Correct me if I'm wrong.

target host
little endian / big endian

memory 00 00 00 03
protocol 03000000
tswap16 00000000 -> don't work

tswapal() 00000003
abi_ntohl() 00000003
htons() 00000003 -> work

big endian / little endian:

memory 00 00 00 03
protocol 00000003
tswap16() 00000300 -> work

tswapal() 03000000
abi_ntohl() 00000003
htons() 00000300 -> work

little endian/little endian:

memory: 00 00 00 03 (network endian)
protocol : 03000000
tswap16() : 00000000 -> don't work

tswapal() 03000000
abi_ntohl() 00000003
htons() 00000300 -> work

big endian / big endian

memory 00 00 00 03
protocol 00000003
tswap16() 00000003 -> work

tswapal() 00000003
abi_ntohl() 00000003
htons() 00000003 -> work

Laurent

Signature · 2 lines
"Just play. Have fun. Enjoy the game."
- Michael Jordan
#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: SOCK_PACKET uses network endian to encode protocol in socket()

Quoted 8 lines · Laurent Vivier
Quoted 7 lines · Peter Maydell

Also it seems rather involved since we swap things three times and have an entirely new abi_* function. Either I'm completely confused or it should be enough to just have

if (type == SOCK_PACKET) {
      protocol = tswap16(protocol);
}

Looking more carefully at packet(7) this is actually the wrong guard anyway. You need to check for
(domain == AF_PACKET) || (type == SOCK_PACKET)

since SOCK_PACKET is the obsolete Linux 2.0 way of doing packet sockets.

Quoted 8 lines

works... sometime. In fact, work if target endianess is network endianess.

Correct me if I'm wrong.

target host
little endian / big endian

memory 00 00 00 03

Syscall arguments aren't generally passed in memory, they're in registers (and if they were pased in memory for some architecture then that arch would do a load-and-swap-from-memory in main.c). So the value you see in do_socket() is always "the integer passed as a syscall parameter, as a host-order integer".

So in this case, with a simple guest program:
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>

int main(void) {
return socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
}

you will find that do_socket() in QEMU is passed either 0x3 [if the guest is bigendian and the guest htons() is a no-op] or 0x0300 [if the guest is littleendian]. Since what we want to pass to the host socket() call is 0x3 if the host is bigendian and 0x0300 if the host is little endian, this amounts to needing to do a 16 bit byteswap if the host and guest are different endianness, which is exactly what tswap16() does. I checked with i386-to-i386 that do_socket() gets passed 0x300 and we correctly send it through to the host socket().

-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: SOCK_PACKET uses network endian to encode protocol in socket()

Quoted 6 lines · Peter Maydell
Quoted 5 lines · Laurent Vivier
Quoted 4 lines · Peter Maydell

Also it seems rather involved since we swap things three times and have an entirely new abi_* function. Either I'm completely confused or it should be enough to just have

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
if (type == SOCK_PACKET) {
      protocol = tswap16(protocol);
}

Looking more carefully at packet(7) this is actually the wrong guard anyway. You need to check for
(domain == AF_PACKET) || (type == SOCK_PACKET)

I agree.

Quoted 1 line

since SOCK_PACKET is the obsolete Linux 2.0 way of doing packet sockets.

But dhclient is always using this...

Quoted 7 lines
Quoted 7 lines

works... sometime. In fact, work if target endianess is network endianess.

Correct me if I'm wrong.

target host
little endian / big endian

Quoted 7 lines
Quoted 1 line

memory 00 00 00 03

Syscall arguments aren't generally passed in memory, they're in registers (and if they were pased in memory for some architecture then that arch would do a load-and-swap-from-memory in main.c). So the value you see in do_socket() is always "the integer passed as a syscall parameter, as a host-order integer".

Yes, I missed that.

Quoted 10 lines

So in this case, with a simple guest program:
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>

int main(void) {
   return socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
}
Quoted 9 lines

you will find that do_socket() in QEMU is passed either 0x3 [if the guest is bigendian and the guest htons() is a no-op] or 0x0300 [if the guest is littleendian]. Since what we want to pass to the host socket() call is 0x3 if the host is bigendian and 0x0300 if the host is little endian, this amounts to needing to do a 16 bit byteswap if the host and guest are different endianness, which is exactly what tswap16() does. I checked with i386-to-i386 that do_socket() gets passed 0x300 and we correctly send it through to the host socket().

Yes, I agree. I correct the patch.

Thank you,
Laurent

Signature · 2 lines
"Just play. Have fun. Enjoy the game."
- Michael Jordan
#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: SOCK_PACKET uses network endian to encode protocol in socket()

Quoted 11 lines · Laurent Vivier
Quoted 10 lines · Peter Maydell
Quoted 8 lines · Laurent Vivier

works... sometime. In fact, work if target endianess is network endianess.

Correct me if I'm wrong.

target host
little endian / big endian

memory 00 00 00 03

Quoted 7 lines
Quoted 5 lines

Syscall arguments aren't generally passed in memory, they're in registers (and if they were pased in memory for some architecture then that arch would do a load-and-swap-from-memory in main.c). So the value you see in do_socket() is always "the integer passed as a syscall parameter, as a host-order integer".

Yes, I missed that.

But, in fact, for socketcall(), they are read from memory :

        static abi_long do_socketcall(int num, abi_ulong vptr)
        {
            abi_long ret;
            const int n = sizeof(abi_ulong);
        
            switch(num) {
            case SOCKOP_socket:
                {
                    abi_ulong domain, type, protocol;
        
                    if (get_user_ual(domain, vptr)
                        || get_user_ual(type, vptr + n)
                        || get_user_ual(protocol, vptr + 2 * n))
                        return -TARGET_EFAULT;
        
                    ret = do_socket(domain, type, protocol);
                }
                break;

So, I don't know if "tswap16()" is always correct. It works for m68k-to-x86_64, but I don't understand how it can works for i386-to-i386.

Your opinion ?

Regards,
Laurent

Signature · 2 lines
"Just play. Have fun. Enjoy the game."
- Michael Jordan
#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: SOCK_PACKET uses network endian to encode protocol in socket()

Quoted 18 lines · Laurent Vivier
Quoted 17 lines · Laurent Vivier
Quoted 15 lines · Peter Maydell
Quoted 8 lines · Laurent Vivier

works... sometime. In fact, work if target endianess is network endianess.

Correct me if I'm wrong.

target host
little endian / big endian

memory 00 00 00 03

Syscall arguments aren't generally passed in memory, they're in registers (and if they were pased in memory for some architecture then that arch would do a load-and-swap-from-memory in main.c). So the value you see in do_socket() is always "the integer passed as a syscall parameter, as a host-order integer".

Quoted 3 lines
Quoted 1 line

Yes, I missed that.

But, in fact, for socketcall(), they are read from memory :

Yes, this is because socketcall is weird. The actual kernel implementation also reads them from memory:
http://lxr.linux.no/#linux+v3.7.1/net/socket.c#L2443
as an array of unsigned longs. So as long as qemu also reads them out of memory as an array of target abi_ulongs (which as you can see we do) then we'll retrieve the same value (0x3 or 0x300) to pass to do_socket() as the guest program wrote into its guest view of memory (since it should have written an unsigned long). (What is happening here is that the guest binary writes the protocol value to memory as an unsigned long, so it goes in as 4 bytes in whichever order the guest uses; qemu's get_user_ual() then rereads those 4 bytes, swapping the value back so we get the same integer value the guest program stored. Note that the guest doesn't write the protocol argument as a 2 byte value!)

I would encourage you to write some simple test programs and check them using strace (both of the native program and of qemu running the program).

-- PMM

#10 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: SOCK_PACKET uses network endian to encode protocol in socket()

Quoted 39 lines · Peter Maydell
Quoted 18 lines · Laurent Vivier
Quoted 17 lines · Laurent Vivier
Quoted 15 lines · Peter Maydell
Quoted 8 lines · Laurent Vivier

works... sometime. In fact, work if target endianess is network endianess.

Correct me if I'm wrong.

target host
little endian / big endian

memory 00 00 00 03

Syscall arguments aren't generally passed in memory, they're in registers (and if they were pased in memory for some architecture then that arch would do a load-and-swap-from-memory in main.c). So the value you see in do_socket() is always "the integer passed as a syscall parameter, as a host-order integer".

Quoted 3 lines
Quoted 1 line

Yes, I missed that.

But, in fact, for socketcall(), they are read from memory :

Yes, this is because socketcall is weird. The actual kernel implementation also reads them from memory:
http://lxr.linux.no/#linux+v3.7.1/net/socket.c#L2443
as an array of unsigned longs. So as long as qemu also reads them out of memory as an array of target abi_ulongs (which as you can see we do) then we'll retrieve the same value (0x3 or 0x300) to pass to do_socket() as the guest program wrote into its guest view of memory (since it should have written an unsigned long). (What is happening here is that the guest binary writes the protocol value to memory as an unsigned long, so it goes in as 4 bytes in whichever order the guest uses; qemu's get_user_ual() then rereads those 4 bytes, swapping the value back so we get the same integer value the guest program stored. Note that the guest doesn't write the protocol argument as a 2 byte value!)

Quoted 3 lines

I would encourage you to write some simple test programs and check them using strace (both of the native program and of qemu running the program).

OK, I will... but I think we will fall back to my original patch ;-)

Regards,
Laurent

Signature · 2 lines
"Just play. Have fun. Enjoy the game."
- Michael Jordan
#11 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: SOCK_PACKET uses network endian to encode protocol in socket()

Quoted 6 lines · Laurent Vivier
Quoted 3 lines · Peter Maydell

I would encourage you to write some simple test programs and check them using strace (both of the native program and of qemu running the program).

OK, I will... but I think we will fall back to my original patch ;-)

You'll need to provide test programs which work with that and fail with the simple tswap16() approach first.

-- PMM