<p>laforge has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/titan.TestPorts.AF_PACKET/+/21664">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">guard against race between socket(AF_PACKET) and bind()<br><br>An AF_PACKET socket will immediately receive packets of _all_ interfaces<br>until it is bound to one specific interface.  This introduces a race<br>condition between the socket() and the bind() syscall.<br><br>Let's use the ifindex passed for each packet in recvmsg() to drop<br>any packets received for other interfaces.<br><br>Change-Id: Icd0b23eb1d6f75ca3a05e5dd1a569fa389903fdf<br>---<br>M src/AF_PACKET_PT.cc<br>1 file changed, 10 insertions(+), 1 deletion(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/titan.TestPorts.AF_PACKET refs/changes/64/21664/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/AF_PACKET_PT.cc b/src/AF_PACKET_PT.cc</span><br><span>index 3f46216..a1ccfe8 100644</span><br><span>--- a/src/AF_PACKET_PT.cc</span><br><span>+++ b/src/AF_PACKET_PT.cc</span><br><span>@@ -118,14 +118,23 @@</span><br><span>            return;</span><br><span> </span><br><span>  if (is_readable) {</span><br><span style="color: hsl(120, 100%, 40%);">+            struct sockaddr_ll sll;</span><br><span style="color: hsl(120, 100%, 40%);">+               socklen_t sll_len = sizeof(sll);</span><br><span>             int rc;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-             rc = read(fd, mRxBuf, sizeof(mRxBuf));</span><br><span style="color: hsl(120, 100%, 40%);">+                rc = recvfrom(fd, mRxBuf, sizeof(mRxBuf), 0, (struct sockaddr *)&sll, &sll_len);</span><br><span>             if (rc < 0)</span><br><span>                       TTCN_error("Error reading from socket: %s", strerror(errno));</span><br><span>              if (rc == 0)</span><br><span>                         TTCN_error("Dead socket: %s", strerror(errno));</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+         /* ignore any packets that we might have received for a different inerface, between</span><br><span style="color: hsl(120, 100%, 40%);">+            * the socket() and the bind() call */</span><br><span style="color: hsl(120, 100%, 40%);">+                if (sll.sll_ifindex != mIfindex)</span><br><span style="color: hsl(120, 100%, 40%);">+                      return;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+             /* TODO: report the other meta-data fields like sll_pkttype via the port */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>                incoming_message(AF__PACKET__Unitdata(OCTETSTRING(rc, mRxBuf)));</span><br><span>     }</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/titan.TestPorts.AF_PACKET/+/21664">change 21664</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/titan.TestPorts.AF_PACKET/+/21664"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: titan.TestPorts.AF_PACKET </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Icd0b23eb1d6f75ca3a05e5dd1a569fa389903fdf </div>
<div style="display:none"> Gerrit-Change-Number: 21664 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>