Skip to content

Commit 4f3db7a

Browse files
committed
net: ptp: add l2 mcast filtering support
also add multicast membership for L2 transport. Signed-off-by: Fin Maaß <info@finmaass.de>
1 parent 448a1d5 commit 4f3db7a

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

subsys/net/lib/ptp/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ config PTP_UDP_IPV6_PROTOCOL
137137
config PTP_IEEE_802_3_PROTOCOL
138138
bool "IEEE 802.3 (Layer 2 / EtherType 0x88F7)"
139139
select NET_SOCKETS_PACKET
140+
select NET_SOCKETS_PACKET_MCAST_MEMBERSHIP if NET_NATIVE
140141

141142
endchoice
142143

subsys/net/lib/ptp/transport.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ static const struct net_in6_addr pdelay_mcast_addr_ipv6 = {
3232
static const uint8_t mcast_addr_l2[PTP_L2_ADDR_LEN] = {0x01, 0x1B, 0x19, 0x00, 0x00, 0x00};
3333
static const uint8_t pdelay_mcast_addr_l2[PTP_L2_ADDR_LEN] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x0E};
3434

35+
#if defined(CONFIG_PTP_IEEE_802_3_PROTOCOL) && defined(CONFIG_NET_SOCKETS_PACKET_MCAST_MEMBERSHIP)
36+
BUILD_ASSERT((CONFIG_PTP_NUM_PORTS * CONFIG_PTP_DELAY_MECHANISM) <=
37+
CONFIG_NET_SOCKETS_PACKET_MCAST_MEMBERSHIP_COUNT,
38+
"CONFIG_NET_SOCKETS_PACKET_MCAST_MEMBERSHIP_COUNT not large enough for the number of "
39+
"PTP ports and delay mechanisms");
40+
#endif
41+
3542
static bool transport_is_pdelay_msg(const void *buf)
3643
{
3744
const struct ptp_msg *msg = buf;
@@ -136,8 +143,42 @@ static int transport_join_ipv6_group(struct ptp_port *port, int socket,
136143
return 0;
137144
}
138145

146+
static int transport_join_l2_group(struct ptp_port *port, int socket, const uint8_t *addr,
147+
const char *name)
148+
{
149+
struct net_packet_mreq mreq = {0};
150+
151+
mreq.mr_ifindex = net_if_get_by_iface(port->iface);
152+
mreq.mr_type = NET_PACKET_MR_MULTICAST;
153+
mreq.mr_alen = PTP_L2_ADDR_LEN;
154+
memcpy(mreq.mr_address, addr, PTP_L2_ADDR_LEN);
155+
156+
if (zsock_setsockopt(socket, ZSOCK_SOL_PACKET, ZSOCK_PACKET_ADD_MEMBERSHIP, &mreq,
157+
sizeof(mreq)) != 0) {
158+
LOG_ERR("Failed to join L2 %s multicast group", name);
159+
return -1;
160+
}
161+
162+
return 0;
163+
}
164+
139165
static int transport_join_multicast(struct ptp_port *port)
140166
{
167+
if (IS_ENABLED(CONFIG_PTP_IEEE_802_3_PROTOCOL)) {
168+
if (transport_join_l2_group(port, port->socket[PTP_SOCKET_EVENT], mcast_addr_l2,
169+
"default") != 0) {
170+
return -1;
171+
}
172+
173+
if (CONFIG_PTP_DELAY_MECHANISM == PTP_DM_P2P &&
174+
transport_join_l2_group(port, port->socket[PTP_SOCKET_EVENT],
175+
pdelay_mcast_addr_l2, "peer-delay") != 0) {
176+
return -1;
177+
}
178+
179+
return 0;
180+
}
181+
141182
if (IS_ENABLED(CONFIG_PTP_UDP_IPV4_PROTOCOL)) {
142183
if (transport_join_ipv4_group(port, port->socket[PTP_SOCKET_GENERAL],
143184
&mcast_addr_ipv4, "default") != 0) {
@@ -379,7 +420,7 @@ int ptp_transport_open(struct ptp_port *port)
379420
port->socket[PTP_SOCKET_EVENT] = socket;
380421
port->socket[PTP_SOCKET_GENERAL] = -1;
381422

382-
return 0;
423+
goto end;
383424
}
384425

385426
for (int i = 0; i < PTP_SOCKET_CNT; i++) {
@@ -399,6 +440,7 @@ int ptp_transport_open(struct ptp_port *port)
399440
port->socket[i] = socket;
400441
}
401442

443+
end:
402444
if (transport_join_multicast(port)) {
403445
ptp_transport_close(port);
404446
return -1;

0 commit comments

Comments
 (0)