Skip to contentSkip to conversation

linux-user: manage SOCK_PACKET socket type

PATCH Superseded Laurent Vivier 2 participants 12 messages Oct 28, 2015

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover linux-user: manage SOCK_PACKET socket type

This is obsolete, but if we want to use dhcp with some distros (like debian ppc 8.2 jessie), we need it.

At the bind level, we are not able to know the socket type so we try to guess it by analyzing the name. We manage only the case "ethX", "ethX" in spk_device is similar to set htons(0x6574) in sll_protocol in the normal case, and as this protocol does not exist, it's ok.

SOCK_PACKET uses network endian to encode protocol in socket()

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.

v2: Split the patch in 4 parts to manage protocol endianness (socket()) and
interface name (bind()) in different patches.
Use TargetFdTrans array to manage the SOCK_PACKET type special case in bind().
The two others patches are here to introduce a new function in TargetFdTrans to translate sockaddr data structure (rename previous functions to be clear).

Laurent Vivier (4):
linux-user: SOCK_PACKET uses network endian to encode protocol in
socket()
linux-user: rename TargetFdFunc to TargetFdDataFunc, and structure
fields accordingly
linux-user: add a function hook to translate sockaddr
linux-user: manage bind with a socket of SOCK_PACKET type.

 linux-user/syscall.c | 95 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 81 insertions(+), 14 deletions(-)
Signature · 1 line
2.4.3
#1 lore raw

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

in PACKET(7) :

packet_socket = socket(AF_PACKET, int socket_type, int protocol);
[...]
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.
[...]
Compatibility

In Linux 2.0, the only way to get a packet socket was by calling socket(AF_INET, SOCK_PACKET, protocol).

We need to tswap16() the protocol because on big-endian, the ABI is waiting for, for instance for ETH_P_ALL, 0x0003 (big endian == network order), whereas on little-endian it is waiting for 0x0300.

---
 linux-user/syscall.c | 6 ++++++
 1 file changed, 6 insertions(+)
linux-user/syscall.c +6 -0
index 4b4159d..7c724ab 100644
@@ -2089,6 +2089,12 @@ static abi_long do_socket(int domain, int type, int protocol)
 
     if (domain == PF_NETLINK)
         return -TARGET_EAFNOSUPPORT;
+
+    if (domain == AF_PACKET ||
+        (domain == AF_INET && type == SOCK_PACKET)) {
+        protocol = tswap16(protocol);
+    }
+
     ret = get_errno(socket(domain, type, protocol));
     if (ret >= 0) {
         ret = sock_flags_fixup(ret, target_type);
Signature · 1 line
2.4.3
Reviewed-by on v2 1/4
#2 lore raw

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

Quoted 16 lines · Laurent Vivier

in PACKET(7) :

packet_socket = socket(AF_PACKET, int socket_type, int protocol);
[...]
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.
[...]
Compatibility

In Linux 2.0, the only way to get a packet socket was by calling socket(AF_INET, SOCK_PACKET, protocol).

Quoted 6 lines

We need to tswap16() the protocol because on big-endian, the ABI is waiting for, for instance for ETH_P_ALL, 0x0003 (big endian == network order), whereas on little-endian it is waiting for 0x0300.

---

thanks
-- PMM

#3 lore raw

PATCH v2 2/4 linux-user: rename TargetFdFunc to TargetFdDataFunc, and structure fields accordingly

In reply to Laurent Vivier
---
 linux-user/syscall.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
linux-user/syscall.c +9 -9
index 7c724ab..41b85b4 100644
@@ -295,20 +295,20 @@ static bitmask_transtbl fcntl_flags_tbl[] = {
   { 0, 0, 0, 0 }
 };
 
-typedef abi_long (*TargetFdFunc)(void *, size_t);
+typedef abi_long (*TargetFdDataFunc)(void *, size_t);
 typedef struct TargetFdTrans {
-    TargetFdFunc host_to_target;
-    TargetFdFunc target_to_host;
+    TargetFdDataFunc host_to_target_data;
+    TargetFdDataFunc target_to_host_data;
 } TargetFdTrans;
 
 static TargetFdTrans **target_fd_trans;
 
 static unsigned int target_fd_max;
 
-static TargetFdFunc fd_trans_host_to_target(int fd)
+static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
 {
     if (fd < target_fd_max && target_fd_trans[fd]) {
-        return target_fd_trans[fd]->host_to_target;
+        return target_fd_trans[fd]->host_to_target_data;
     }
     return NULL;
 }
@@ -5438,7 +5438,7 @@ host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
     tinfo->ssi_addr = tswap64(info->ssi_addr);
 }
 
-static abi_long host_to_target_signalfd(void *buf, size_t len)
+static abi_long host_to_target_data_signalfd(void *buf, size_t len)
 {
     int i;
 
@@ -5450,7 +5450,7 @@ static abi_long host_to_target_signalfd(void *buf, size_t len)
 }
 
 static TargetFdTrans target_signalfd_trans = {
-    .host_to_target = host_to_target_signalfd,
+    .host_to_target_data = host_to_target_data_signalfd,
 };
 
 static abi_long do_signalfd4(int fd, abi_long mask, int flags)
@@ -5867,8 +5867,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 goto efault;
             ret = get_errno(read(arg1, p, arg3));
             if (ret >= 0 &&
-                fd_trans_host_to_target(arg1)) {
-                ret = fd_trans_host_to_target(arg1)(p, ret);
+                fd_trans_host_to_target_data(arg1)) {
+                ret = fd_trans_host_to_target_data(arg1)(p, ret);
             }
             unlock_user(p, arg2, ret);
         }
Signature · 1 line
2.4.3
#5 lore raw

PATCH v2 3/4 linux-user: add a function hook to translate sockaddr

In reply to Laurent Vivier
---
 linux-user/syscall.c | 41 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 36 insertions(+), 5 deletions(-)
linux-user/syscall.c +36 -5
index 41b85b4..31b5c2c 100644
@@ -296,9 +296,11 @@ static bitmask_transtbl fcntl_flags_tbl[] = {
 };
 
 typedef abi_long (*TargetFdDataFunc)(void *, size_t);
+typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
 typedef struct TargetFdTrans {
     TargetFdDataFunc host_to_target_data;
     TargetFdDataFunc target_to_host_data;
+    TargetFdAddrFunc target_to_host_addr;
 } TargetFdTrans;
 
 static TargetFdTrans **target_fd_trans;
@@ -313,6 +315,14 @@ static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
     return NULL;
 }
 
+static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
+{
+    if (fd < target_fd_max && target_fd_trans[fd]) {
+        return target_fd_trans[fd]->target_to_host_addr;
+    }
+    return NULL;
+}
+
 static void fd_trans_register(int fd, TargetFdTrans *trans)
 {
     unsigned int oldmax;
@@ -2115,7 +2125,12 @@ static abi_long do_bind(int sockfd, abi_ulong target_addr,
 
     addr = alloca(addrlen+1);
 
-    ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    if (fd_trans_target_to_host_addr(sockfd)) {
+        ret = fd_trans_target_to_host_addr(sockfd)(addr, target_addr,
+                                                   addrlen);
+    } else {
+        ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    }
     if (ret)
         return ret;
 
@@ -2135,7 +2150,12 @@ static abi_long do_connect(int sockfd, abi_ulong target_addr,
 
     addr = alloca(addrlen+1);
 
-    ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    if (fd_trans_target_to_host_addr(sockfd)) {
+        ret = fd_trans_target_to_host_addr(sockfd)(addr, target_addr,
+                                                   addrlen);
+    } else {
+        ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    }
     if (ret)
         return ret;
 
@@ -2155,8 +2175,14 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
     if (msgp->msg_name) {
         msg.msg_namelen = tswap32(msgp->msg_namelen);
         msg.msg_name = alloca(msg.msg_namelen+1);
-        ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
-                                msg.msg_namelen);
+        if (fd_trans_target_to_host_addr(fd)) {
+            ret = fd_trans_target_to_host_addr(fd)(msg.msg_name,
+                                                   tswapal(msgp->msg_name),
+                                                   msg.msg_namelen);
+        } else {
+            ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
+                                          msg.msg_namelen);
+        }
         if (ret) {
             goto out2;
         }
@@ -2418,7 +2444,12 @@ static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
         return -TARGET_EFAULT;
     if (target_addr) {
         addr = alloca(addrlen+1);
-        ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+        if (fd_trans_target_to_host_addr(fd)) {
+            ret = fd_trans_target_to_host_addr(fd)(addr, target_addr,
+                                                   addrlen);
+        } else {
+            ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+        }
         if (ret) {
             unlock_user(host_msg, msg, 0);
             return ret;
Signature · 1 line
2.4.3
#6 lore raw

Re: [Qemu-devel] [PATCH v2 3/4] linux-user: add a function hook to translate sockaddr

Quoted 22 lines · Laurent Vivier
---
 linux-user/syscall.c | 41 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 36 insertions(+), 5 deletions(-)
linux-user/syscall.c +2 -0
index 41b85b4..31b5c2c 100644
@@ -296,9 +296,11 @@ static bitmask_transtbl fcntl_flags_tbl[] = {
 };
 
 typedef abi_long (*TargetFdDataFunc)(void *, size_t);
+typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
 typedef struct TargetFdTrans {
     TargetFdDataFunc host_to_target_data;
     TargetFdDataFunc target_to_host_data;
+    TargetFdAddrFunc target_to_host_addr;
 } TargetFdTrans;
 
 static TargetFdTrans **target_fd_trans;

linux-user/syscall.c static TargetFdDataFunc fd_trans_host_to_target_data(int fd) · line 323

Quoted 9 lines
linux-user/syscall.c +6 -0
@@ -313,6 +315,14 @@ static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
     return NULL;
 }
 
+static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
+{
+    if (fd < target_fd_max && target_fd_trans[fd]) {
+        return target_fd_trans[fd]->target_to_host_addr;
+    }
+    return NULL;

This should probably do something sensible with a negative input (either assert, or return NULL, I guess).

Quoted 54 lines
linux-user/syscall.c +22 -4
+}
+
 static void fd_trans_register(int fd, TargetFdTrans *trans)
 {
     unsigned int oldmax;
@@ -2115,7 +2125,12 @@ static abi_long do_bind(int sockfd, abi_ulong target_addr,
 
     addr = alloca(addrlen+1);
 
-    ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    if (fd_trans_target_to_host_addr(sockfd)) {
+        ret = fd_trans_target_to_host_addr(sockfd)(addr, target_addr,
+                                                   addrlen);
+    } else {
+        ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    }
     if (ret)
         return ret;
 
@@ -2135,7 +2150,12 @@ static abi_long do_connect(int sockfd, abi_ulong target_addr,
 
     addr = alloca(addrlen+1);
 
-    ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    if (fd_trans_target_to_host_addr(sockfd)) {
+        ret = fd_trans_target_to_host_addr(sockfd)(addr, target_addr,
+                                                   addrlen);
+    } else {
+        ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    }
     if (ret)
         return ret;
 
@@ -2155,8 +2175,14 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
     if (msgp->msg_name) {
         msg.msg_namelen = tswap32(msgp->msg_namelen);
         msg.msg_name = alloca(msg.msg_namelen+1);
-        ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
-                                msg.msg_namelen);
+        if (fd_trans_target_to_host_addr(fd)) {
+            ret = fd_trans_target_to_host_addr(fd)(msg.msg_name,
+                                                   tswapal(msgp->msg_name),
+                                                   msg.msg_namelen);
+        } else {
+            ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
+                                          msg.msg_namelen);
+        }
         if (ret) {
             goto out2;
         }
@@ -2418,7 +2444,12 @@ static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
         return -TARGET_EFAULT;
     if (target_addr) {
         addr = alloca(addrlen+1);

linux-user/syscall.c static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags, · line 2455

Quoted 10 lines
linux-user/syscall.c +6 -1
-        ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+        if (fd_trans_target_to_host_addr(fd)) {
+            ret = fd_trans_target_to_host_addr(fd)(addr, target_addr,
+                                                   addrlen);
+        } else {
+            ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+        }
         if (ret) {
             unlock_user(host_msg, msg, 0);
             return ret;

The repetition here suggests we should pass the fd into target_to_host_sockaddr and do the handling of fd_trans_target_to_host_addr there.

thanks
-- PMM

#7 lore raw

PATCH v2 4/4 linux-user: manage bind with a socket of SOCK_PACKET type.

In reply to Laurent Vivier

This is obsolete, but if we want to use dhcp with an old distro (like debian etch), we need it. Some users (like dhclient) use SOCK_PACKET with AF_PACKET and the kernel allows that.

packet(7)

In Linux 2.0, the only way to get a packet socket was by calling
socket(AF_INET, SOCK_PACKET, protocol). This is still supported but
strongly deprecated. The main difference between the two methods is that SOCK_PACKET uses the old struct sockaddr_pkt to specify an inter‐ face, which doesn't provide physical layer independence.

     struct sockaddr_pkt {
         unsigned short spkt_family;
         unsigned char  spkt_device[14];
         unsigned short spkt_protocol;
     };

spkt_family contains the device type, spkt_protocol is the IEEE 802.3 protocol type as defined in <sys/if_ether.h> and spkt_device is the device name as a null-terminated string, for example, eth0.

---
 linux-user/syscall.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
linux-user/syscall.c +30 -0
index 31b5c2c..f048437 100644
@@ -2086,6 +2086,30 @@ static int sock_flags_fixup(int fd, int target_type)
     return fd;
 }
 
+static abi_long packet_target_to_host_addr(void *host_addr,
+                                           abi_ulong target_addr,
+                                           socklen_t len)
+{
+    struct sockaddr *addr = host_addr;
+    struct target_sockaddr *target_saddr;
+
+    target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
+    if (!target_saddr) {
+        return -TARGET_EFAULT;
+    }
+
+    memcpy(addr, target_saddr, len);
+    addr->sa_family = tswap16(target_saddr->sa_family);
+    /* spkt_protocol is big-endian */
+
+    unlock_user(target_saddr, target_addr, 0);
+    return 0;
+}
+
+static TargetFdTrans target_packet_trans = {
+    .target_to_host_addr = packet_target_to_host_addr,
+};
+
 /* do_socket() Must return target values and target errnos. */
 static abi_long do_socket(int domain, int type, int protocol)
 {
@@ -2108,6 +2132,12 @@ static abi_long do_socket(int domain, int type, int protocol)
     ret = get_errno(socket(domain, type, protocol));
     if (ret >= 0) {
         ret = sock_flags_fixup(ret, target_type);
+        if (type == SOCK_PACKET) {
+            /* Manage an obsolete case :
+             * if socket type is SOCK_PACKET, bind by name
+             */
+            fd_trans_register(ret, &target_packet_trans);
+        }
     }
     return ret;
 }
Signature · 1 line
2.4.3
Reviewed-by on v2 4/4
#8 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] linux-user: manage bind with a socket of SOCK_PACKET type.

Quoted 32 lines · Laurent Vivier

This is obsolete, but if we want to use dhcp with an old distro (like debian etch), we need it. Some users (like dhclient) use SOCK_PACKET with AF_PACKET and the kernel allows that.

packet(7)

In Linux 2.0, the only way to get a packet socket was by calling
socket(AF_INET, SOCK_PACKET, protocol). This is still supported but
strongly deprecated. The main difference between the two methods is that SOCK_PACKET uses the old struct sockaddr_pkt to specify an inter‐ face, which doesn't provide physical layer independence.

     struct sockaddr_pkt {
         unsigned short spkt_family;
         unsigned char  spkt_device[14];
         unsigned short spkt_protocol;
     };

spkt_family contains the device type, spkt_protocol is the IEEE 802.3 protocol type as defined in <sys/if_ether.h> and spkt_device is the device name as a null-terminated string, for example, eth0.

---
 linux-user/syscall.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
linux-user/syscall.c +0 -0
index 31b5c2c..f048437 100644

linux-user/syscall.c static int sock_flags_fixup(int fd, int target_type) · line 2091

Quoted 6 lines
linux-user/syscall.c +3 -0
@@ -2086,6 +2086,30 @@ static int sock_flags_fixup(int fd, int target_type)
     return fd;
 }
 
+static abi_long packet_target_to_host_addr(void *host_addr,
+                                           abi_ulong target_addr,
+                                           socklen_t len)

Should the function name be ..._to_host_sockaddr ?

Otherwise,

thanks
-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] linux-user: manage bind with a socket of SOCK_PACKET type.

Quoted 36 lines · Peter Maydell
Quoted 35 lines · Laurent Vivier

This is obsolete, but if we want to use dhcp with an old distro (like debian etch), we need it. Some users (like dhclient) use SOCK_PACKET with AF_PACKET and the kernel allows that.

packet(7)

In Linux 2.0, the only way to get a packet socket was by calling
socket(AF_INET, SOCK_PACKET, protocol). This is still supported but
strongly deprecated. The main difference between the two methods is that SOCK_PACKET uses the old struct sockaddr_pkt to specify an inter‐ face, which doesn't provide physical layer independence.

     struct sockaddr_pkt {
         unsigned short spkt_family;
         unsigned char  spkt_device[14];
         unsigned short spkt_protocol;
     };

spkt_family contains the device type, spkt_protocol is the IEEE 802.3 protocol type as defined in <sys/if_ether.h> and spkt_device is the device name as a null-terminated string, for example, eth0.

---
 linux-user/syscall.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
linux-user/syscall.c +0 -0
index 31b5c2c..f048437 100644
@@ -2086,6 +2086,30 @@ static int sock_flags_fixup(int fd, int target_type)
     return fd;
 }
 
Quoted 5 lines

linux-user/syscall.c static int sock_flags_fixup(int fd, int target_type) · line 2091

Quoted 3 lines
linux-user/syscall.c +3 -0
+static abi_long packet_target_to_host_addr(void *host_addr,
+                                           abi_ulong target_addr,
+                                           socklen_t len)

Should the function name be ..._to_host_sockaddr ?

It was what I did in my first draft, but as I have called the type TargetFdDataFunc and TargetFdAddrFunc, I have chosen to call the function _addr.

If you think it is better, I can change that.

Quoted 7 lines

Otherwise,

thanks
-- PMM

#10 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] linux-user: manage bind with a socket of SOCK_PACKET type.

Quoted 8 lines · Laurent Vivier
Quoted 5 lines · Peter Maydell
Quoted 3 lines · Laurent Vivier
linux-user/syscall.c +3 -0
+static abi_long packet_target_to_host_addr(void *host_addr,
+                                           abi_ulong target_addr,
+                                           socklen_t len)
Quoted 7 lines
Quoted 1 line

Should the function name be ..._to_host_sockaddr ?

It was what I did in my first draft, but as I have called the type TargetFdDataFunc and TargetFdAddrFunc, I have chosen to call the function _addr.

If you think it is better, I can change that.

Well, it is sockaddr types we're handling, so it seems a bit clearer to use that in the name.

thanks
-- PMM

#11 lore raw
In reply to Laurent Vivier
Quoted 7 lines · Laurent Vivier

This is obsolete, but if we want to use dhcp with some distros (like debian ppc 8.2 jessie), we need it.

At the bind level, we are not able to know the socket type so we try to guess it by analyzing the name. We manage only the case "ethX", "ethX" in spk_device is similar to set htons(0x6574) in sll_protocol in the normal case, and as this protocol does not exist, it's ok.

This cover letter is underselling the patchset, which now handles more than just "ethX" :-)

I had a couple of minor comments but it looks pretty good overall.

thanks
-- PMM