Change in titan.TestPorts.AF_PACKET[master]: guard against race between socket(AF_PACKET) and bind()

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Thu Dec 10 20:29:31 UTC 2020


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/titan.TestPorts.AF_PACKET/+/21664 )


Change subject: guard against race between socket(AF_PACKET) and bind()
......................................................................

guard against race between socket(AF_PACKET) and bind()

An AF_PACKET socket will immediately receive packets of _all_ interfaces
until it is bound to one specific interface.  This introduces a race
condition between the socket() and the bind() syscall.

Let's use the ifindex passed for each packet in recvmsg() to drop
any packets received for other interfaces.

Change-Id: Icd0b23eb1d6f75ca3a05e5dd1a569fa389903fdf
---
M src/AF_PACKET_PT.cc
1 file changed, 10 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/titan.TestPorts.AF_PACKET refs/changes/64/21664/1

diff --git a/src/AF_PACKET_PT.cc b/src/AF_PACKET_PT.cc
index 3f46216..a1ccfe8 100644
--- a/src/AF_PACKET_PT.cc
+++ b/src/AF_PACKET_PT.cc
@@ -118,14 +118,23 @@
 		return;
 
 	if (is_readable) {
+		struct sockaddr_ll sll;
+		socklen_t sll_len = sizeof(sll);
 		int rc;
 
-		rc = read(fd, mRxBuf, sizeof(mRxBuf));
+		rc = recvfrom(fd, mRxBuf, sizeof(mRxBuf), 0, (struct sockaddr *)&sll, &sll_len);
 		if (rc < 0)
 			TTCN_error("Error reading from socket: %s", strerror(errno));
 		if (rc == 0)
 			TTCN_error("Dead socket: %s", strerror(errno));
 
+		/* ignore any packets that we might have received for a different inerface, between
+		 * the socket() and the bind() call */
+		if (sll.sll_ifindex != mIfindex)
+			return;
+
+		/* TODO: report the other meta-data fields like sll_pkttype via the port */
+
 		incoming_message(AF__PACKET__Unitdata(OCTETSTRING(rc, mRxBuf)));
 	}
 }

-- 
To view, visit https://gerrit.osmocom.org/c/titan.TestPorts.AF_PACKET/+/21664
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: titan.TestPorts.AF_PACKET
Gerrit-Branch: master
Gerrit-Change-Id: Icd0b23eb1d6f75ca3a05e5dd1a569fa389903fdf
Gerrit-Change-Number: 21664
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20201210/d3884566/attachment.htm>


More information about the gerrit-log mailing list