Skip to contentSkip to conversation

linux-user: manage SOCK_PACKET socket type

PATCH In review Laurent Vivier 3 participants 12 messages Oct 28, 2015 – Dec 23, 2015

lore
Share & tools
lore
#0 lore raw

v3 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.

bind() uses an interface name instead an interface index, and socket() needs network order value to encode the protocol.

v3: update cover letter message,
insert Reviewed-by: in PATCH 1 and PATCH 2
insert fd_trans_target_to_host_addr into target_to_host_sockaddr and pass fd, check fd is >= 0, rename packet_target_to_host_addr to packet_target_to_host_sockaddr

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 Fiver (5):
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: check fd is >= 0 in
fd_trans_host_to_target_data/fd_trans_host_to_target_addr

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

PATCH v3 1/5 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
#2 lore raw

PATCH v3 2/5 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
#3 lore raw

PATCH v3 3/5 linux-user: add a function hook to translate sockaddr

In reply to Laurent Vivier

---
v3: insert fd_trans_target_to_host_addr() into target_to_host_sockaddr() and
pass fd.

 linux-user/syscall.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
linux-user/syscall.c +21 -6
index 41b85b4..a313846 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;
@@ -1162,7 +1172,7 @@ static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
     return 0;
 }
 
-static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
+static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
                                                abi_ulong target_addr,
                                                socklen_t len)
 {
@@ -1170,6 +1180,10 @@ static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
     sa_family_t sa_family;
     struct target_sockaddr *target_saddr;
 
+    if (fd_trans_target_to_host_addr(fd)) {
+        return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
+    }
+
     target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
     if (!target_saddr)
         return -TARGET_EFAULT;
@@ -2115,7 +2129,7 @@ static abi_long do_bind(int sockfd, abi_ulong target_addr,
 
     addr = alloca(addrlen+1);
 
-    ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
     if (ret)
         return ret;
 
@@ -2135,7 +2149,7 @@ static abi_long do_connect(int sockfd, abi_ulong target_addr,
 
     addr = alloca(addrlen+1);
 
-    ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
     if (ret)
         return ret;
 
@@ -2155,8 +2169,9 @@ 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);
+        ret = target_to_host_sockaddr(fd, msg.msg_name,
+                                      tswapal(msgp->msg_name),
+                                      msg.msg_namelen);
         if (ret) {
             goto out2;
         }
@@ -2418,7 +2433,7 @@ 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);
+        ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
         if (ret) {
             unlock_user(host_msg, msg, 0);
             return ret;
Signature · 1 line
2.4.3
#4 lore raw

PATCH v3 4/5 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.

---
v3: rename packet_target_to_host_addr to packet_target_to_host_sockaddr

 linux-user/syscall.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
linux-user/syscall.c +30 -0
index a313846..22b28a4 100644
@@ -2090,6 +2090,30 @@ static int sock_flags_fixup(int fd, int target_type)
     return fd;
 }
 
+static abi_long packet_target_to_host_sockaddr(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_sockaddr,
+};
+
 /* do_socket() Must return target values and target errnos. */
 static abi_long do_socket(int domain, int type, int protocol)
 {
@@ -2112,6 +2136,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
#5 lore raw

PATCH v3 5/5 linux-user: check fd is >= 0 in fd_trans_host_to_target_data/fd_trans_host_to_target_addr

In reply to Laurent Vivier
---
 linux-user/syscall.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/syscall.c +2 -2
index 22b28a4..eb45e72 100644
@@ -309,7 +309,7 @@ static unsigned int target_fd_max;
 
 static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
 {
-    if (fd < target_fd_max && target_fd_trans[fd]) {
+    if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
         return target_fd_trans[fd]->host_to_target_data;
     }
     return NULL;
@@ -317,7 +317,7 @@ static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
 
 static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
 {
-    if (fd < target_fd_max && target_fd_trans[fd]) {
+    if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
         return target_fd_trans[fd]->target_to_host_addr;
     }
     return NULL;
Signature · 1 line
2.4.3
Reviewed-by on the v3 series
#6 lore raw
In reply to Laurent Vivier
Quoted 20 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.

bind() uses an interface name instead an interface index, and socket() needs network order value to encode the protocol.

v3: update cover letter message,
insert Reviewed-by: in PATCH 1 and PATCH 2
insert fd_trans_target_to_host_addr into target_to_host_sockaddr and pass fd, check fd is >= 0, rename packet_target_to_host_addr to packet_target_to_host_sockaddr

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).

Quoted 1 line

Laurent Fiver (5):

This name doesn't match the names on the actual patch mails, but those are right so I guess it doesn't matter.

Quoted 8 lines

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: check fd is >= 0 in
fd_trans_host_to_target_data/fd_trans_host_to_target_addr

Series

thanks
-- PMM

#7 lore raw
Quoted 21 lines · Peter Maydell
Quoted 20 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.

bind() uses an interface name instead an interface index, and socket() needs network order value to encode the protocol.

v3: update cover letter message,
insert Reviewed-by: in PATCH 1 and PATCH 2
insert fd_trans_target_to_host_addr into target_to_host_sockaddr and pass fd, check fd is >= 0, rename packet_target_to_host_addr to packet_target_to_host_sockaddr

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).

Quoted 1 line
Quoted 1 line

Laurent Fiver (5):

Sorry for that. "vi" is a joker.

Quoted 13 lines

This name doesn't match the names on the actual patch mails, but those are right so I guess it doesn't matter.

Quoted 8 lines

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: check fd is >= 0 in
fd_trans_host_to_target_data/fd_trans_host_to_target_addr

Quoted 6 lines

Series

thanks
-- PMM

thanks,
Laurent

#8 lore raw
In reply to Peter Maydell
Quoted 35 lines · Peter Maydell
Quoted 20 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.

bind() uses an interface name instead an interface index, and socket() needs network order value to encode the protocol.

v3: update cover letter message,
insert Reviewed-by: in PATCH 1 and PATCH 2
insert fd_trans_target_to_host_addr into target_to_host_sockaddr and pass fd, check fd is >= 0, rename packet_target_to_host_addr to packet_target_to_host_sockaddr

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).

Quoted 1 line

Laurent Fiver (5):

This name doesn't match the names on the actual patch mails, but those are right so I guess it doesn't matter.

Quoted 8 lines

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: check fd is >= 0 in
fd_trans_host_to_target_data/fd_trans_host_to_target_addr

Quoted 2 lines

Series

Can we have this series applied ?

Thanks,
Laurent

#9 lore raw
Quoted 36 lines · Laurent Vivier
Quoted 35 lines · Peter Maydell
Quoted 20 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.

bind() uses an interface name instead an interface index, and socket() needs network order value to encode the protocol.

v3: update cover letter message,
insert Reviewed-by: in PATCH 1 and PATCH 2
insert fd_trans_target_to_host_addr into target_to_host_sockaddr and pass fd, check fd is >= 0, rename packet_target_to_host_addr to packet_target_to_host_sockaddr

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).

Quoted 1 line

Laurent Fiver (5):

This name doesn't match the names on the actual patch mails, but those are right so I guess it doesn't matter.

Quoted 8 lines

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: check fd is >= 0 in
fd_trans_host_to_target_data/fd_trans_host_to_target_addr

Quoted 4 lines
Quoted 2 lines

Series

Can we have this series applied ?

I'll create next pull req next week, right now I'm a bit busy. If someone else wants to merge the series before me, you have my Acked-by.

Riku

#10 lore raw
Quoted 37 lines · Riku Voipio
Quoted 36 lines · Laurent Vivier
Quoted 35 lines · Peter Maydell
Quoted 20 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.

bind() uses an interface name instead an interface index, and socket() needs network order value to encode the protocol.

v3: update cover letter message,
insert Reviewed-by: in PATCH 1 and PATCH 2
insert fd_trans_target_to_host_addr into target_to_host_sockaddr and pass fd, check fd is >= 0, rename packet_target_to_host_addr to packet_target_to_host_sockaddr

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).

Quoted 1 line

Laurent Fiver (5):

This name doesn't match the names on the actual patch mails, but those are right so I guess it doesn't matter.

Quoted 8 lines

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: check fd is >= 0 in
fd_trans_host_to_target_data/fd_trans_host_to_target_addr

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Series

Can we have this series applied ?

I'll create next pull req next week, right now I'm a bit busy. If someone else wants to merge the series before me, you have my Acked-by.

Thank you, but remember in your branch "linux-user-for-upstream" you have a commit which breaks errno: "linux-user: Fix array bounds in errno conversion".

Return parameters must stay signed.

Thanks,
Laurent

#11 lore raw
In reply to Riku Voipio
Quoted 37 lines · Riku Voipio
Quoted 36 lines · Laurent Vivier
Quoted 35 lines · Peter Maydell
Quoted 20 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.

bind() uses an interface name instead an interface index, and socket() needs network order value to encode the protocol.

v3: update cover letter message,
insert Reviewed-by: in PATCH 1 and PATCH 2
insert fd_trans_target_to_host_addr into target_to_host_sockaddr and pass fd, check fd is >= 0, rename packet_target_to_host_addr to packet_target_to_host_sockaddr

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).

Quoted 1 line

Laurent Fiver (5):

This name doesn't match the names on the actual patch mails, but those are right so I guess it doesn't matter.

Quoted 8 lines

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: check fd is >= 0 in
fd_trans_host_to_target_data/fd_trans_host_to_target_addr

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Series

Can we have this series applied ?

I'll create next pull req next week, right now I'm a bit busy. If someone else wants to merge the series before me, you have my Acked-by.

If you want, I can do the pull request:
- I take your branch linux-user-for-upstream
from https://git.linaro.org/people/riku.voipio/qemu.git
- remove "linux-user: Fix array bounds in errno conversion" because it is broken,
- add "linux-user: manage SOCK_PACKET socket type"

I'd like to add "linux-user, sh4: fix signal retcode address", "linux-user: Enable sigaltstack syscall for sh4" but they have no reviewer except me.

Laurent