Skip to content

Commit 2253a26

Browse files
Noemie Gilletnashif
authored andcommitted
drivers: nsos: support for AF_PACKET
Handle AF_PACKET family. Signed-off-by: Noemie Gillet <[email protected]>
1 parent 10bb61e commit 2253a26

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

drivers/net/nsos.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@
1515
#define NSOS_MID_PF_INET 1 /**< IP protocol family version 4. */
1616
#define NSOS_MID_PF_INET6 2 /**< IP protocol family version 6. */
1717
#define NSOS_MID_PF_UNIX 6 /**< Unix protocol. */
18+
#define NSOS_MID_PF_PACKET 3 /**< Packet family. */
1819

1920
/* Address families. */
2021
#define NSOS_MID_AF_UNSPEC NSOS_MID_PF_UNSPEC /**< Unspecified address family. */
2122
#define NSOS_MID_AF_INET NSOS_MID_PF_INET /**< IP protocol family version 4. */
2223
#define NSOS_MID_AF_INET6 NSOS_MID_PF_INET6 /**< IP protocol family version 6. */
2324
#define NSOS_MID_AF_UNIX NSOS_MID_PF_UNIX /**< Unix protocol. */
25+
#define NSOS_MID_AF_PACKET NSOS_MID_PF_PACKET /**< Packet family. */
2426

2527
/** Protocol numbers from IANA/BSD */
2628
enum nsos_mid_net_ip_protocol {
2729
NSOS_MID_IPPROTO_IP = 0, /**< IP protocol (pseudo-val for setsockopt() */
2830
NSOS_MID_IPPROTO_ICMP = 1, /**< ICMP protocol */
2931
NSOS_MID_IPPROTO_IGMP = 2, /**< IGMP protocol */
32+
NSOS_MID_IPPROTO_ETH_P_ALL = 3, /**< Every packet. from linux if_ether.h */
3033
NSOS_MID_IPPROTO_IPIP = 4, /**< IPIP tunnels */
3134
NSOS_MID_IPPROTO_TCP = 6, /**< TCP protocol */
3235
NSOS_MID_IPPROTO_UDP = 17, /**< UDP protocol */
@@ -71,12 +74,22 @@ struct nsos_mid_sockaddr_un {
7174
char sun_path[UNIX_PATH_MAX]; /* pathname */
7275
};
7376

77+
struct nsos_mid_sockaddr_ll {
78+
sa_family_t sll_family; /**< Always AF_PACKET */
79+
uint16_t sll_protocol; /**< Physical-layer protocol */
80+
int sll_ifindex; /**< Interface number */
81+
uint16_t sll_hatype; /**< ARP hardware type */
82+
uint8_t sll_pkttype; /**< Packet type */
83+
uint8_t sll_halen; /**< Length of address */
84+
uint8_t sll_addr[8]; /**< Physical-layer address, big endian */
85+
};
7486

7587
struct nsos_mid_sockaddr_storage {
7688
union {
7789
struct nsos_mid_sockaddr_in sockaddr_in;
7890
struct nsos_mid_sockaddr_in6 sockaddr_in6;
7991
struct nsos_mid_sockaddr_un sockaddr_un;
92+
struct nsos_mid_sockaddr_ll sockaddr_ll;
8093
};
8194
};
8295

drivers/net/nsos_adapt.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <netdb.h>
1818
#include <netinet/in.h>
1919
#include <netinet/tcp.h>
20+
#include <netinet/if_ether.h>
21+
#include <netpacket/packet.h>
2022
#include <poll.h>
2123
#include <stdlib.h>
2224
#include <string.h>
@@ -72,6 +74,9 @@ static int socket_family_from_nsos_mid(int family_mid, int *family)
7274
case NSOS_MID_AF_UNIX:
7375
*family = AF_UNIX;
7476
break;
77+
case NSOS_MID_AF_PACKET:
78+
*family = AF_PACKET;
79+
break;
7580
default:
7681
nsi_print_warning("%s: socket family %d not supported\n", __func__, family_mid);
7782
return -NSOS_MID_EAFNOSUPPORT;
@@ -95,6 +100,9 @@ static int socket_family_to_nsos_mid(int family, int *family_mid)
95100
case AF_UNIX:
96101
*family_mid = NSOS_MID_AF_UNIX;
97102
break;
103+
case AF_PACKET:
104+
*family_mid = NSOS_MID_AF_PACKET;
105+
break;
98106
default:
99107
nsi_print_warning("%s: socket family %d not supported\n", __func__, family);
100108
return -NSOS_MID_EAFNOSUPPORT;
@@ -130,6 +138,9 @@ static int socket_proto_from_nsos_mid(int proto_mid, int *proto)
130138
case NSOS_MID_IPPROTO_RAW:
131139
*proto = IPPROTO_RAW;
132140
break;
141+
case NSOS_MID_IPPROTO_ETH_P_ALL:
142+
*proto = htons(ETH_P_ALL);
143+
break;
133144
default:
134145
nsi_print_warning("%s: socket protocol %d not supported\n", __func__, proto_mid);
135146
return -NSOS_MID_EPROTONOSUPPORT;
@@ -165,6 +176,9 @@ static int socket_proto_to_nsos_mid(int proto, int *proto_mid)
165176
case IPPROTO_RAW:
166177
*proto_mid = NSOS_MID_IPPROTO_RAW;
167178
break;
179+
case ETH_P_ALL:
180+
*proto_mid = htons(NSOS_MID_IPPROTO_ETH_P_ALL);
181+
break;
168182
default:
169183
nsi_print_warning("%s: socket protocol %d not supported\n", __func__, proto);
170184
return -NSOS_MID_EPROTONOSUPPORT;
@@ -316,6 +330,24 @@ static int sockaddr_from_nsos_mid(struct sockaddr **addr, socklen_t *addrlen,
316330

317331
return 0;
318332
}
333+
case NSOS_MID_AF_PACKET: {
334+
const struct nsos_mid_sockaddr_ll *addr_ll_mid =
335+
(const struct nsos_mid_sockaddr_ll *)addr_mid;
336+
struct sockaddr_ll *addr_ll = (struct sockaddr_ll *)*addr;
337+
338+
addr_ll->sll_family = NSOS_MID_AF_PACKET;
339+
addr_ll->sll_protocol = addr_ll_mid->sll_protocol;
340+
addr_ll->sll_ifindex = addr_ll_mid->sll_ifindex;
341+
addr_ll->sll_hatype = addr_ll_mid->sll_hatype;
342+
addr_ll->sll_pkttype = addr_ll_mid->sll_pkttype;
343+
addr_ll->sll_halen = addr_ll_mid->sll_halen;
344+
memcpy(addr_ll->sll_addr, addr_ll_mid->sll_addr,
345+
sizeof(addr_ll->sll_addr));
346+
347+
*addrlen = sizeof(*addr_ll);
348+
349+
return 0;
350+
}
319351
}
320352

321353
return -NSOS_MID_EINVAL;
@@ -384,6 +416,23 @@ static int sockaddr_to_nsos_mid(const struct sockaddr *addr, socklen_t addrlen,
384416

385417
return 0;
386418
}
419+
case AF_PACKET: {
420+
struct nsos_mid_sockaddr_ll *addr_ll_mid =
421+
(struct nsos_mid_sockaddr_ll *)addr_mid;
422+
const struct sockaddr_ll *addr_ll = (const struct sockaddr_ll *)addr;
423+
424+
if (addr_ll_mid) {
425+
addr_ll_mid->sll_family = NSOS_MID_AF_PACKET;
426+
addr_ll_mid->sll_protocol = addr_ll->sll_protocol;
427+
addr_ll_mid->sll_ifindex = addr_ll->sll_ifindex;
428+
}
429+
430+
if (addrlen_mid) {
431+
*addrlen_mid = sizeof(*addr_ll);
432+
}
433+
434+
return 0;
435+
}
387436
}
388437

389438
nsi_print_warning("%s: socket family %d not supported\n", __func__, addr->sa_family);

drivers/net/nsos_sockets.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ static int socket_family_to_nsos_mid(int family, int *family_mid)
7575
case AF_UNIX:
7676
*family_mid = NSOS_MID_AF_UNIX;
7777
break;
78+
case AF_PACKET:
79+
*family_mid = NSOS_MID_AF_PACKET;
80+
break;
7881
default:
7982
return -NSOS_MID_EAFNOSUPPORT;
8083
}
@@ -109,6 +112,9 @@ static int socket_proto_to_nsos_mid(int proto, int *proto_mid)
109112
case IPPROTO_RAW:
110113
*proto_mid = NSOS_MID_IPPROTO_RAW;
111114
break;
115+
case htons(IPPROTO_ETH_P_ALL):
116+
*proto_mid = NSOS_MID_IPPROTO_ETH_P_ALL;
117+
break;
112118
default:
113119
return -NSOS_MID_EPROTONOSUPPORT;
114120
}
@@ -469,6 +475,29 @@ static int sockaddr_to_nsos_mid(const struct sockaddr *addr, socklen_t addrlen,
469475

470476
return 0;
471477
}
478+
case AF_PACKET: {
479+
const struct sockaddr_ll *addr_ll =
480+
(const struct sockaddr_ll *)addr;
481+
struct nsos_mid_sockaddr_ll *addr_ll_mid =
482+
(struct nsos_mid_sockaddr_ll *)*addr_mid;
483+
484+
if (addrlen < sizeof(*addr_ll)) {
485+
return -NSOS_MID_EINVAL;
486+
}
487+
488+
addr_ll_mid->sll_family = NSOS_MID_AF_UNIX;
489+
addr_ll_mid->sll_protocol = addr_ll->sll_protocol;
490+
addr_ll_mid->sll_ifindex = addr_ll->sll_ifindex;
491+
addr_ll_mid->sll_hatype = addr_ll->sll_hatype;
492+
addr_ll_mid->sll_pkttype = addr_ll->sll_pkttype;
493+
addr_ll_mid->sll_halen = addr_ll->sll_halen;
494+
memcpy(addr_ll_mid->sll_addr, addr_ll->sll_addr,
495+
sizeof(addr_ll->sll_addr));
496+
497+
*addrlen_mid = sizeof(*addr_ll_mid);
498+
499+
return 0;
500+
}
472501
}
473502

474503
return -NSOS_MID_EINVAL;
@@ -948,6 +977,9 @@ static int socket_proto_from_nsos_mid(int proto_mid, int *proto)
948977
case NSOS_MID_IPPROTO_RAW:
949978
*proto = IPPROTO_RAW;
950979
break;
980+
case NSOS_MID_IPPROTO_ETH_P_ALL:
981+
*proto = htons(IPPROTO_ETH_P_ALL);
982+
break;
951983
default:
952984
return -NSOS_MID_EPROTONOSUPPORT;
953985
}
@@ -970,6 +1002,9 @@ static int socket_family_from_nsos_mid(int family_mid, int *family)
9701002
case NSOS_MID_AF_UNIX:
9711003
*family = AF_UNIX;
9721004
break;
1005+
case NSOS_MID_AF_PACKET:
1006+
*family = AF_PACKET;
1007+
break;
9731008
default:
9741009
return -NSOS_MID_EAFNOSUPPORT;
9751010
}

include/zephyr/net/net_ip.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ enum net_ip_protocol {
6565
IPPROTO_IP = 0, /**< IP protocol (pseudo-val for setsockopt() */
6666
IPPROTO_ICMP = 1, /**< ICMP protocol */
6767
IPPROTO_IGMP = 2, /**< IGMP protocol */
68+
IPPROTO_ETH_P_ALL = 3, /**< Every packet. from linux if_ether.h */
6869
IPPROTO_IPIP = 4, /**< IPIP tunnels */
6970
IPPROTO_TCP = 6, /**< TCP protocol */
7071
IPPROTO_UDP = 17, /**< UDP protocol */

0 commit comments

Comments
 (0)