casaroli commented on code in PR #20627:
URL: https://github.com/apache/nuttx/pull/20627#discussion_r4665500032
##########
net/tcp/tcp_input.c:
##########
@@ -190,6 +193,203 @@ static bool tcp_trim_head(FAR struct net_driver_s *dev,
return false;
}
+#ifdef CONFIG_NET_TCPURGDATA
+/****************************************************************************
+ * Name: tcp_remove_urgbyte
+ *
+ * Description:
+ * Remove one byte at the specified offset from the IOB chain.
+ *
+ * Input Parameters:
+ * head - Pointer to head of the IOB chain
+ * offset - Offset of the byte to remove
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void tcp_remove_urgbyte(FAR struct iob_s **head, unsigned int offset)
+{
+ FAR struct iob_s *iob;
+ FAR struct iob_s *prev = NULL;
+ FAR struct iob_s *cur;
+
+ if (head == NULL || *head == NULL)
+ {
+ return;
+ }
+
+ iob = *head;
+ cur = iob;
+
+ while (cur != NULL && offset >= cur->io_len)
+ {
+ offset -= cur->io_len;
+ prev = cur;
+ cur = cur->io_flink;
+ }
+
+ if (cur != NULL)
+ {
+ unsigned int nbytes = cur->io_len - offset - 1;
+
+ if (nbytes > 0)
+ {
+ memmove(&cur->io_data[cur->io_offset + offset],
+ &cur->io_data[cur->io_offset + offset + 1],
+ nbytes);
+ }
+
+ cur->io_len--;
+ iob->io_pktlen--;
+
+ if (cur->io_len == 0)
+ {
+ if (prev != NULL)
+ {
+ prev->io_flink = cur->io_flink;
+ cur->io_flink = NULL;
+ iob_free(cur);
+ }
+ else if (cur->io_flink != NULL)
+ {
+ FAR struct iob_s *next = cur->io_flink;
+
+ next->io_pktlen = iob->io_pktlen;
+ cur->io_flink = NULL;
+ iob_free(cur);
+ *head = next;
+ }
+ else
+ {
+ cur->io_flink = NULL;
+ iob_free(cur);
+ *head = NULL;
+ }
+ }
+ }
+}
+
+/****************************************************************************
+ * Name: tcp_urg_input
+ *
+ * Description:
+ * Handle urgent (out-of-band) data for an incoming TCP segment.
+ *
+ * Input Parameters:
+ * dev - The structure of the network driver
+ * conn - The TCP connection of interest
+ * tcp - The TCP header of the incoming segment
+ * flags - Current event flags
+ *
+ * Returned Value:
+ * Updated event flags (including TCP_URGDATA if urgent data received)
+ *
+ ****************************************************************************/
+
+static uint32_t tcp_urg_input(FAR struct net_driver_s *dev,
+ FAR struct tcp_conn_s *conn,
+ FAR struct tcp_hdr_s *tcp,
+ uint32_t flags)
+{
+ uint32_t urg_seq = tcp_getsequence(tcp->seqno);
+ uint32_t urg_rcvseq = tcp_getsequence(conn->rcvseq);
+ uint32_t urg_offset = 0;
+ bool has_urg = false;
+
+ /* Check the URG flag. If this is set, or if we are awaiting
+ * urgent data announced earlier that falls into this segment,
+ * process it.
+ */
+
+ if ((tcp->flags & TCP_URG) != 0)
+ {
+ uint16_t urgp = (tcp->urgp[0] << 8) | tcp->urgp[1];
+
+ /* BSD/Linux default semantics (tcp_stdurg=0):
+ * The urgent pointer points ONE BYTE PAST the urgent byte.
+ * The single urgent byte is at payload[urgp - 1].
+ */
+
+ if (urgp >= 1)
+ {
+ uint32_t urgseq = urg_seq + urgp - 1;
+
+ /* Only process if the urgent byte has not already been
+ * acknowledged (i.e., not an old duplicate segment).
+ */
+
+ if (TCP_SEQ_GTE(urgseq, urg_rcvseq))
+ {
+ conn->urg_seq = urgseq;
+
+ if (urgp <= dev->d_len)
+ {
+ urg_offset = urgp - 1;
+ has_urg = true;
+ }
+ else
+ {
+ conn->urg_state = TCP_URG_AWAITING;
+ flags |= TCP_URGDATA;
+ }
+ }
+ }
+ }
+ else if (conn->urg_state == TCP_URG_AWAITING &&
+ TCP_SEQ_GTE(conn->urg_seq, urg_seq) &&
+ TCP_SEQ_LT(conn->urg_seq, urg_seq + dev->d_len))
+ {
+ urg_offset = TCP_SEQ_SUB(conn->urg_seq, urg_seq);
+ has_urg = true;
+ }
+
+ if (has_urg)
+ {
+ int app_offset;
+
+ /* Payload is stored in an IOB chain starting at
+ * dev->d_iob. Calculate the offset of d_appdata
+ * within dev->d_iob.
+ */
+
+ app_offset = (dev->d_appdata - dev->d_iob->io_data) -
+ dev->d_iob->io_offset;
+
+ /* Copy the urgent byte safely from the IOB chain */
+
+ iob_copyout(&conn->urg_data, dev->d_iob, 1,
+ app_offset + urg_offset);
+ conn->urg_state = TCP_URG_VALID;
+
+ /* Signal urgent data availability to poll/select */
+
+ flags |= TCP_URGDATA;
+
+ /* The urgent byte is out-of-band and must not be
+ * received in the normal data stream unless SO_OOBINLINE
+ * is enabled.
+ */
+
+ if (!_SO_GETOPT(conn->sconn.s_options, SO_OOBINLINE))
+ {
+ tcp_remove_urgbyte(&dev->d_iob, app_offset + urg_offset);
+ dev->d_len--;
+
+ /* The urgent byte is consumed; advance rcvseq immediately
+ * so that out-of-order processing inside tcp_callback sees
+ * the updated sequence number.
+ */
+
+ net_incr32(conn->rcvseq, 1);
Review Comment:
`tcp_urg_input()` adds 1 to `rcvseq` for the urgent byte before the data of
the segment is stored. When fewer than `CONFIG_IOB_THROTTLE` IOBs are free,
`tcp_datahandler()` stores nothing (`tcp_callback.c:443`), `tcp_data_event()`
adds 0 and still sends an ACK. That ACK covers the first byte of the segment.
If the urgent byte is not the first byte, the peer never sends that normal byte
again: it is lost. `tcp_recvhandler()` has the same problem when it stores
fewer bytes than the offset of the urgent byte. The PR body says the packet is
dropped "without advancing `rcvseq`".
Fix: add the 1 for the urgent byte where the stored length is added to
`rcvseq` (`tcp_data_event()`, `tcp_recvhandler()`), and only when the stored
bytes reach the offset of the urgent byte.
##########
net/tcp/tcp_callback.c:
##########
@@ -109,6 +110,42 @@ tcp_data_event(FAR struct net_driver_s *dev, FAR struct
tcp_conn_s *conn,
****************************************************************************/
#ifdef CONFIG_NET_TCP_OUT_OF_ORDER
+#ifdef CONFIG_NET_TCPURGDATA
+/****************************************************************************
+ * Name: tcp_ofoseg_urg
+ *
+ * Description:
+ * Check and extract urgent data from an out-of-order segment being
+ * committed to the receive stream.
+ *
+ ****************************************************************************/
+
+static uint32_t tcp_ofoseg_urg(FAR struct net_driver_s *dev,
+ FAR struct tcp_conn_s *conn,
+ FAR struct tcp_ofoseg_s *seg,
+ uint32_t flags)
+{
+ if (conn->urg_state == TCP_URG_AWAITING &&
+ TCP_SEQ_GTE(conn->urg_seq, seg->left) &&
+ TCP_SEQ_LT(conn->urg_seq, seg->right))
+ {
+ uint32_t urg_offset = TCP_SEQ_SUB(conn->urg_seq, seg->left);
+
+ iob_copyout(&conn->urg_data, seg->data, 1, urg_offset);
+ conn->urg_state = TCP_URG_VALID;
+ flags |= TCP_URGDATA;
+ devif_conn_event(dev, TCP_URGDATA, conn->sconn.list);
Review Comment:
`struct socket_conn_s` has no member `list` since 2e202d7309f (the `s_`
prefix). So `sim:dynconns`, which has `CONFIG_NET_TCPURGDATA=y`, does not
build: `tcp_callback.c:137:54: error: 'struct socket_conn_s' has no member
named 'list'` (CI Linux (sim-02)). The branch also conflicts with master in
`tcp_input.c`, `tcp_netpoll.c` and `tcp_send_buffered.c`.
Fix: use `conn->sconn.s_list`, and rebase on master.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]