Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions drivers/net/nsos.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
#define NSOS_MID_PF_UNSPEC 0 /**< Unspecified protocol family. */
#define NSOS_MID_PF_INET 1 /**< IP protocol family version 4. */
#define NSOS_MID_PF_INET6 2 /**< IP protocol family version 6. */
#define NSOS_MID_PF_UNIX 6 /**< Unix protocol. */
#define NSOS_MID_PF_PACKET 3 /**< Packet family. */

/* Address families. */
#define NSOS_MID_AF_UNSPEC NSOS_MID_PF_UNSPEC /**< Unspecified address family. */
#define NSOS_MID_AF_INET NSOS_MID_PF_INET /**< IP protocol family version 4. */
#define NSOS_MID_AF_INET6 NSOS_MID_PF_INET6 /**< IP protocol family version 6. */
#define NSOS_MID_AF_UNIX NSOS_MID_PF_UNIX /**< Unix protocol. */
#define NSOS_MID_AF_PACKET NSOS_MID_PF_PACKET /**< Packet family. */

/** Protocol numbers from IANA/BSD */
enum nsos_mid_net_ip_protocol {
NSOS_MID_IPPROTO_IP = 0, /**< IP protocol (pseudo-val for setsockopt() */
NSOS_MID_IPPROTO_ICMP = 1, /**< ICMP protocol */
NSOS_MID_IPPROTO_IGMP = 2, /**< IGMP protocol */
NSOS_MID_IPPROTO_ETH_P_ALL = 3, /**< Every packet. from linux if_ether.h */
NSOS_MID_IPPROTO_IPIP = 4, /**< IPIP tunnels */
NSOS_MID_IPPROTO_TCP = 6, /**< TCP protocol */
NSOS_MID_IPPROTO_UDP = 17, /**< UDP protocol */
Expand Down Expand Up @@ -63,10 +68,28 @@
uint32_t sin6_scope_id; /* Set of interfaces for a scope */
};

#define UNIX_PATH_MAX 108
struct nsos_mid_sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[UNIX_PATH_MAX]; /* pathname */
};

struct nsos_mid_sockaddr_ll {
sa_family_t sll_family; /**< Always AF_PACKET */
uint16_t sll_protocol; /**< Physical-layer protocol */
int sll_ifindex; /**< Interface number */
uint16_t sll_hatype; /**< ARP hardware type */
uint8_t sll_pkttype; /**< Packet type */
uint8_t sll_halen; /**< Length of address */
uint8_t sll_addr[8]; /**< Physical-layer address, big endian */
};

Check notice on line 85 in drivers/net/nsos.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/net/nsos.h:85 - sa_family_t sun_family; /* AF_UNIX */ - char sun_path[UNIX_PATH_MAX]; /* pathname */ + sa_family_t sun_family; /* AF_UNIX */ + char sun_path[UNIX_PATH_MAX]; /* pathname */ }; struct nsos_mid_sockaddr_ll { - sa_family_t sll_family; /**< Always AF_PACKET */ - uint16_t sll_protocol; /**< Physical-layer protocol */ - int sll_ifindex; /**< Interface number */ - uint16_t sll_hatype; /**< ARP hardware type */ - uint8_t sll_pkttype; /**< Packet type */ - uint8_t sll_halen; /**< Length of address */ - uint8_t sll_addr[8]; /**< Physical-layer address, big endian */ + sa_family_t sll_family; /**< Always AF_PACKET */ + uint16_t sll_protocol; /**< Physical-layer protocol */ + int sll_ifindex; /**< Interface number */ + uint16_t sll_hatype; /**< ARP hardware type */ + uint8_t sll_pkttype; /**< Packet type */ + uint8_t sll_halen; /**< Length of address */ + uint8_t sll_addr[8]; /**< Physical-layer address, big endian */

struct nsos_mid_sockaddr_storage {
union {
struct nsos_mid_sockaddr_in sockaddr_in;
struct nsos_mid_sockaddr_in6 sockaddr_in6;
struct nsos_mid_sockaddr_un sockaddr_un;
struct nsos_mid_sockaddr_ll sockaddr_ll;
};
};

Expand Down
86 changes: 86 additions & 0 deletions drivers/net/nsos_adapt.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/if_ether.h>
#include <netpacket/packet.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#include "nsos.h"
Expand Down Expand Up @@ -68,6 +71,12 @@
case NSOS_MID_AF_INET6:
*family = AF_INET6;
break;
case NSOS_MID_AF_UNIX:
*family = AF_UNIX;
break;
case NSOS_MID_AF_PACKET:
*family = AF_PACKET;
break;
default:
nsi_print_warning("%s: socket family %d not supported\n", __func__, family_mid);
return -NSOS_MID_EAFNOSUPPORT;
Expand All @@ -88,6 +97,12 @@
case AF_INET6:
*family_mid = NSOS_MID_AF_INET6;
break;
case AF_UNIX:
*family_mid = NSOS_MID_AF_UNIX;
break;
case AF_PACKET:
*family_mid = NSOS_MID_AF_PACKET;
break;
default:
nsi_print_warning("%s: socket family %d not supported\n", __func__, family);
return -NSOS_MID_EAFNOSUPPORT;
Expand Down Expand Up @@ -123,6 +138,9 @@
case NSOS_MID_IPPROTO_RAW:
*proto = IPPROTO_RAW;
break;
case NSOS_MID_IPPROTO_ETH_P_ALL:
*proto = htons(ETH_P_ALL);
break;
default:
nsi_print_warning("%s: socket protocol %d not supported\n", __func__, proto_mid);
return -NSOS_MID_EPROTONOSUPPORT;
Expand Down Expand Up @@ -158,6 +176,9 @@
case IPPROTO_RAW:
*proto_mid = NSOS_MID_IPPROTO_RAW;
break;
case ETH_P_ALL:
*proto_mid = htons(NSOS_MID_IPPROTO_ETH_P_ALL);
break;
default:
nsi_print_warning("%s: socket protocol %d not supported\n", __func__, proto);
return -NSOS_MID_EPROTONOSUPPORT;
Expand Down Expand Up @@ -296,6 +317,37 @@

return 0;
}
case NSOS_MID_AF_UNIX: {
const struct nsos_mid_sockaddr_un *addr_un_mid =
(const struct nsos_mid_sockaddr_un *)addr_mid;
struct sockaddr_un *addr_un = (struct sockaddr_un *)*addr;

addr_un->sun_family = AF_UNIX;
memcpy(addr_un->sun_path, addr_un_mid->sun_path,
sizeof(addr_un->sun_path));

Check notice on line 328 in drivers/net/nsos_adapt.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/net/nsos_adapt.c:328 - memcpy(addr_un->sun_path, addr_un_mid->sun_path, - sizeof(addr_un->sun_path)); + memcpy(addr_un->sun_path, addr_un_mid->sun_path, sizeof(addr_un->sun_path));
*addrlen = sizeof(*addr_un);

return 0;
}
case NSOS_MID_AF_PACKET: {
const struct nsos_mid_sockaddr_ll *addr_ll_mid =
(const struct nsos_mid_sockaddr_ll *)addr_mid;
struct sockaddr_ll *addr_ll = (struct sockaddr_ll *)*addr;

addr_ll->sll_family = NSOS_MID_AF_PACKET;
addr_ll->sll_protocol = addr_ll_mid->sll_protocol;
addr_ll->sll_ifindex = addr_ll_mid->sll_ifindex;
addr_ll->sll_hatype = addr_ll_mid->sll_hatype;
addr_ll->sll_pkttype = addr_ll_mid->sll_pkttype;
addr_ll->sll_halen = addr_ll_mid->sll_halen;
memcpy(addr_ll->sll_addr, addr_ll_mid->sll_addr,
sizeof(addr_ll->sll_addr));

Check notice on line 346 in drivers/net/nsos_adapt.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/net/nsos_adapt.c:346 - memcpy(addr_ll->sll_addr, addr_ll_mid->sll_addr, - sizeof(addr_ll->sll_addr)); + memcpy(addr_ll->sll_addr, addr_ll_mid->sll_addr, sizeof(addr_ll->sll_addr));
*addrlen = sizeof(*addr_ll);

return 0;
}
}

return -NSOS_MID_EINVAL;
Expand Down Expand Up @@ -347,6 +399,40 @@

return 0;
}
case AF_UNIX: {
struct nsos_mid_sockaddr_un *addr_un_mid =
(struct nsos_mid_sockaddr_un *)addr_mid;
const struct sockaddr_un *addr_un = (const struct sockaddr_un *)addr;

Check notice on line 405 in drivers/net/nsos_adapt.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/net/nsos_adapt.c:405 - struct nsos_mid_sockaddr_un *addr_un_mid = - (struct nsos_mid_sockaddr_un *)addr_mid; + struct nsos_mid_sockaddr_un *addr_un_mid = (struct nsos_mid_sockaddr_un *)addr_mid;

if (addr_un_mid) {
addr_un_mid->sun_family = NSOS_MID_AF_UNIX;
memcpy(addr_un_mid->sun_path, addr_un->sun_path,
sizeof(addr_un_mid->sun_path));
}

if (addrlen_mid) {
*addrlen_mid = sizeof(*addr_un);
}

return 0;
}
case AF_PACKET: {
struct nsos_mid_sockaddr_ll *addr_ll_mid =
(struct nsos_mid_sockaddr_ll *)addr_mid;
const struct sockaddr_ll *addr_ll = (const struct sockaddr_ll *)addr;

Check notice on line 422 in drivers/net/nsos_adapt.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/net/nsos_adapt.c:422 - struct nsos_mid_sockaddr_ll *addr_ll_mid = - (struct nsos_mid_sockaddr_ll *)addr_mid; + struct nsos_mid_sockaddr_ll *addr_ll_mid = (struct nsos_mid_sockaddr_ll *)addr_mid;

if (addr_ll_mid) {
addr_ll_mid->sll_family = NSOS_MID_AF_PACKET;
addr_ll_mid->sll_protocol = addr_ll->sll_protocol;
addr_ll_mid->sll_ifindex = addr_ll->sll_ifindex;
}

if (addrlen_mid) {
*addrlen_mid = sizeof(*addr_ll);
}

return 0;
}
}

nsi_print_warning("%s: socket family %d not supported\n", __func__, addr->sa_family);
Expand Down
59 changes: 59 additions & 0 deletions drivers/net/nsos_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
case AF_INET6:
*family_mid = NSOS_MID_AF_INET6;
break;
case AF_UNIX:
*family_mid = NSOS_MID_AF_UNIX;
break;
case AF_PACKET:
*family_mid = NSOS_MID_AF_PACKET;
break;
default:
return -NSOS_MID_EAFNOSUPPORT;
}
Expand Down Expand Up @@ -106,6 +112,9 @@
case IPPROTO_RAW:
*proto_mid = NSOS_MID_IPPROTO_RAW;
break;
case htons(IPPROTO_ETH_P_ALL):
*proto_mid = NSOS_MID_IPPROTO_ETH_P_ALL;
break;
default:
return -NSOS_MID_EPROTONOSUPPORT;
}
Expand Down Expand Up @@ -446,6 +455,47 @@

return 0;
}
case AF_UNIX: {
const struct sockaddr_un *addr_un =
(const struct sockaddr_un *)addr;
struct nsos_mid_sockaddr_un *addr_un_mid =
(struct nsos_mid_sockaddr_un *)*addr_mid;

if (addrlen < sizeof(*addr_un)) {
return -NSOS_MID_EINVAL;
}

addr_un_mid->sun_family = NSOS_MID_AF_UNIX;
memcpy(addr_un_mid->sun_path, addr_un->sun_path,
sizeof(addr_un_mid->sun_path));

*addrlen_mid = sizeof(*addr_un_mid);

return 0;
}
case AF_PACKET: {
const struct sockaddr_ll *addr_ll =
(const struct sockaddr_ll *)addr;
struct nsos_mid_sockaddr_ll *addr_ll_mid =
(struct nsos_mid_sockaddr_ll *)*addr_mid;

if (addrlen < sizeof(*addr_ll)) {
return -NSOS_MID_EINVAL;

Check notice on line 483 in drivers/net/nsos_sockets.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/net/nsos_sockets.c:483 - const struct sockaddr_un *addr_un = - (const struct sockaddr_un *)addr; - struct nsos_mid_sockaddr_un *addr_un_mid = - (struct nsos_mid_sockaddr_un *)*addr_mid; + const struct sockaddr_un *addr_un = (const struct sockaddr_un *)addr; + struct nsos_mid_sockaddr_un *addr_un_mid = (struct nsos_mid_sockaddr_un *)*addr_mid; if (addrlen < sizeof(*addr_un)) { return -NSOS_MID_EINVAL; } addr_un_mid->sun_family = NSOS_MID_AF_UNIX; - memcpy(addr_un_mid->sun_path, addr_un->sun_path, - sizeof(addr_un_mid->sun_path)); + memcpy(addr_un_mid->sun_path, addr_un->sun_path, sizeof(addr_un_mid->sun_path)); *addrlen_mid = sizeof(*addr_un_mid); return 0; } case AF_PACKET: { - const struct sockaddr_ll *addr_ll = - (const struct sockaddr_ll *)addr; - struct nsos_mid_sockaddr_ll *addr_ll_mid = - (struct nsos_mid_sockaddr_ll *)*addr_mid; + const struct sockaddr_ll *addr_ll = (const struct sockaddr_ll *)addr; + struct nsos_mid_sockaddr_ll *addr_ll_mid = (struct nsos_mid_sockaddr_ll *)*addr_mid;
}

addr_ll_mid->sll_family = NSOS_MID_AF_UNIX;
addr_ll_mid->sll_protocol = addr_ll->sll_protocol;
addr_ll_mid->sll_ifindex = addr_ll->sll_ifindex;
addr_ll_mid->sll_hatype = addr_ll->sll_hatype;
addr_ll_mid->sll_pkttype = addr_ll->sll_pkttype;
addr_ll_mid->sll_halen = addr_ll->sll_halen;
memcpy(addr_ll_mid->sll_addr, addr_ll->sll_addr,
sizeof(addr_ll->sll_addr));

*addrlen_mid = sizeof(*addr_ll_mid);

Check notice on line 496 in drivers/net/nsos_sockets.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/net/nsos_sockets.c:496 - memcpy(addr_ll_mid->sll_addr, addr_ll->sll_addr, - sizeof(addr_ll->sll_addr)); + memcpy(addr_ll_mid->sll_addr, addr_ll->sll_addr, sizeof(addr_ll->sll_addr));
return 0;
}
}

return -NSOS_MID_EINVAL;
Expand Down Expand Up @@ -925,6 +975,9 @@
case NSOS_MID_IPPROTO_RAW:
*proto = IPPROTO_RAW;
break;
case NSOS_MID_IPPROTO_ETH_P_ALL:
*proto = htons(IPPROTO_ETH_P_ALL);
break;
default:
return -NSOS_MID_EPROTONOSUPPORT;
}
Expand All @@ -944,6 +997,12 @@
case NSOS_MID_AF_INET6:
*family = AF_INET6;
break;
case NSOS_MID_AF_UNIX:
*family = AF_UNIX;
break;
case NSOS_MID_AF_PACKET:
*family = AF_PACKET;
break;
default:
return -NSOS_MID_EAFNOSUPPORT;
}
Expand Down
20 changes: 20 additions & 0 deletions include/zephyr/net/net_ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
IPPROTO_IP = 0, /**< IP protocol (pseudo-val for setsockopt() */
IPPROTO_ICMP = 1, /**< ICMP protocol */
IPPROTO_IGMP = 2, /**< IGMP protocol */
IPPROTO_ETH_P_ALL = 3, /**< Every packet. from linux if_ether.h */
IPPROTO_IPIP = 4, /**< IPIP tunnels */
IPPROTO_TCP = 6, /**< TCP protocol */
IPPROTO_UDP = 17, /**< UDP protocol */
Expand Down Expand Up @@ -231,6 +232,12 @@
uint8_t *sll_addr; /**< Physical-layer address, big endian */
};

/** Socket address struct for unix socket where address is a pointer */
struct sockaddr_un_ptr {
sa_family_t sun_family; /**< Always AF_UNIX */
char *sun_path; /**< pathname */
};

Check notice on line 239 in include/zephyr/net/net_ip.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

include/zephyr/net/net_ip.h:239 - sa_family_t sun_family; /**< Always AF_UNIX */ - char *sun_path; /**< pathname */ + sa_family_t sun_family; /**< Always AF_UNIX */ + char *sun_path; /**< pathname */

struct sockaddr_can_ptr {
sa_family_t can_family;
int can_ifindex;
Expand Down Expand Up @@ -373,14 +380,27 @@
#endif
#endif

#if defined(CONFIG_NET_NATIVE_OFFLOADED_SOCKETS)
#define UNIX_PATH_MAX 108
#undef NET_SOCKADDR_MAX_SIZE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message of the last commit drivers: nsos: fix support of AF_PACKET is a bit misleading as the change does not do any changes for the nsos driver, it just enables it to work.
Could you change the commit subject to net: ip: Add check for packet socket for offloaded sockets
Not sure what exactly to say in commit message. Could you describe there why this change is needed and also mention nsos driver there. Currently your commit message says it fixes things but does not describe why the fix works in this case (which is important to write here).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I missed your comment about merging the last commit. Yes, we could merge it in which case my comment is not really valid any more 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I missed your comment too. It should be done now.

/* Define NET_SOCKADDR_MAX_SIZE to be struct of sa_family_t + char[UNIX_PATH_MAX] */
#define NET_SOCKADDR_MAX_SIZE (UNIX_PATH_MAX+sizeof(sa_family_t))
#if !defined(CONFIG_NET_SOCKETS_PACKET)

Check notice on line 388 in include/zephyr/net/net_ip.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

include/zephyr/net/net_ip.h:388 -#define NET_SOCKADDR_MAX_SIZE (UNIX_PATH_MAX+sizeof(sa_family_t)) +#define NET_SOCKADDR_MAX_SIZE (UNIX_PATH_MAX + sizeof(sa_family_t))
#undef NET_SOCKADDR_PTR_MAX_SIZE
#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_un_ptr))
#endif
#endif

#if !defined(CONFIG_NET_IPV4)
#if !defined(CONFIG_NET_IPV6)
#if !defined(CONFIG_NET_SOCKETS_PACKET)
#if !defined(CONFIG_NET_NATIVE_OFFLOADED_SOCKETS)
#define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_in6))
#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_in6_ptr))
#endif
#endif
#endif
#endif

/** @endcond */

Expand Down
Loading