Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions subsys/net/ip/net_pkt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,8 @@ u16_t net_pkt_append(struct net_pkt *pkt, u16_t len, const u8_t *data,
s32_t timeout)
{
struct net_buf *frag;
struct net_context *ctx;
size_t max_len;

if (!pkt || !data) {
return 0;
Expand All @@ -1177,6 +1179,40 @@ u16_t net_pkt_append(struct net_pkt *pkt, u16_t len, const u8_t *data,
net_pkt_frag_add(pkt, frag);
}

/* Make sure we don't send more data in one packet than
* MTU allows.
* This check really belongs to net_pkt_append_bytes(),
* but instead done here for efficiency, we assume
* (at least for now) that that callers of
* net_pkt_append_bytes() are smart enough to not
* overflow MTU.
*/
max_len = 0x10000;

ctx = net_pkt_context(pkt);
if (ctx) {
#if defined(CONFIG_NET_TCP)
if (ctx->tcp) {
max_len = ctx->tcp->send_mss;
} else
#endif
{
struct net_if *iface = net_context_get_iface(ctx);
max_len = net_if_get_mtu(iface);
/* Optimize for number of jumps in the code ("if"
* instead of "if/else").
*/
max_len -= NET_IPV4TCPH_LEN;
if (net_context_get_family(ctx) != AF_INET) {
max_len -= NET_IPV6TCPH_LEN - NET_IPV4TCPH_LEN;
}
}
}

if (len > max_len) {
len = max_len;
}

return net_pkt_append_bytes(pkt, data, len, timeout);
}

Expand Down
13 changes: 0 additions & 13 deletions subsys/net/lib/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ ssize_t zsock_sendto(int sock, const void *buf, size_t len, int flags,
struct net_pkt *send_pkt;
s32_t timeout = K_FOREVER;
struct net_context *ctx = INT_TO_POINTER(sock);
size_t max_len = net_if_get_mtu(net_context_get_iface(ctx));

ARG_UNUSED(flags);

Expand All @@ -249,18 +248,6 @@ ssize_t zsock_sendto(int sock, const void *buf, size_t len, int flags,
return -1;
}

/* Make sure we don't send more data in one packet than
* MTU allows. Optimize for number of branches in the code.
*/
max_len -= NET_IPV4TCPH_LEN;
if (net_context_get_family(ctx) != AF_INET) {
max_len -= NET_IPV6TCPH_LEN - NET_IPV4TCPH_LEN;
}

if (len > max_len) {
len = max_len;
}

len = net_pkt_append(send_pkt, len, buf, timeout);
if (!len) {
net_pkt_unref(send_pkt);
Expand Down