diff --git a/subsys/net/ip/connection.c b/subsys/net/ip/connection.c index c6d3ffdec6af6e..3fc5ce4d269b64 100644 --- a/subsys/net/ip/connection.c +++ b/subsys/net/ip/connection.c @@ -776,9 +776,9 @@ static inline void send_icmp_error(struct net_pkt *pkt) enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_pkt *pkt) { - struct net_udp_hdr hdr, *udp_hdr; int i, best_match = -1; s16_t best_rank = -1; + u16_t src_port, dst_port; u16_t chksum; #if defined(CONFIG_NET_CONN_CACHE) @@ -796,22 +796,42 @@ enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_pkt *pkt) * Because both TCP and UDP header have these in the same * location, we can check them both using the UDP struct. */ - udp_hdr = net_udp_get_hdr(pkt, &hdr); - if (!udp_hdr) { - return NET_DROP; - } + if (IS_ENABLED(CONFIG_NET_UDP) && proto == IPPROTO_UDP) { + struct net_udp_hdr hdr, *udp_hdr; - if (proto == IPPROTO_TCP) { - chksum = net_tcp_get_chksum(pkt, pkt->frags); - } else { + ARG_UNUSED(hdr); + + udp_hdr = net_udp_get_hdr(pkt, &hdr); + if (!udp_hdr) { + return NET_DROP; + } + + src_port = udp_hdr->src_port; + dst_port = udp_hdr->dst_port; chksum = udp_hdr->chksum; + } else if (IS_ENABLED(CONFIG_NET_TCP) && proto == IPPROTO_TCP) { + struct net_tcp_hdr hdr, *tcp_hdr; + + ARG_UNUSED(hdr); + + tcp_hdr = net_tcp_get_hdr(pkt, &hdr); + if (!tcp_hdr) { + return NET_DROP; + } + + src_port = tcp_hdr->src_port; + dst_port = tcp_hdr->dst_port; + chksum = tcp_hdr->chksum; + } else { + NET_DBG("No UDP or TCP configured, dropping packet."); + return NET_DROP; } if (IS_ENABLED(CONFIG_NET_DEBUG_CONN)) { NET_DBG("Check %s listener for pkt %p src port %u dst port %u " "family %d chksum 0x%04x", net_proto2str(proto), pkt, - ntohs(udp_hdr->src_port), - ntohs(udp_hdr->dst_port), + ntohs(src_port), + ntohs(dst_port), net_pkt_family(pkt), ntohs(chksum)); } @@ -826,14 +846,14 @@ enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_pkt *pkt) if (net_sin(&conns[i].remote_addr)->sin_port) { if (net_sin(&conns[i].remote_addr)->sin_port != - udp_hdr->src_port) { + src_port) { continue; } } if (net_sin(&conns[i].local_addr)->sin_port) { if (net_sin(&conns[i].local_addr)->sin_port != - udp_hdr->dst_port) { + dst_port) { continue; } } diff --git a/subsys/net/ip/tcp.h b/subsys/net/ip/tcp.h index b3bf3e8f68fd2e..2ee2d7bf31f857 100644 --- a/subsys/net/ip/tcp.h +++ b/subsys/net/ip/tcp.h @@ -412,6 +412,7 @@ u16_t net_tcp_get_chksum(struct net_pkt *pkt, struct net_buf *frag); #define net_tcp_get_chksum(pkt, frag) (0) #define net_tcp_set_chksum(pkt, frag) NULL #define net_tcp_set_hdr(pkt, frag) NULL +#define net_tcp_get_hdr(pkt, frag) NULL #endif #if defined(CONFIG_NET_TCP) diff --git a/subsys/net/lib/app/net_app.c b/subsys/net/lib/app/net_app.c index 48eefe2fe1de78..92c5aaf2af80b6 100644 --- a/subsys/net/lib/app/net_app.c +++ b/subsys/net/lib/app/net_app.c @@ -116,10 +116,12 @@ void _net_app_received(struct net_context *net_ctx, ctx->cb.close(ctx, status, ctx->user_data); } +#if defined(CONFIG_NET_TCP) if (ctx->proto == IPPROTO_TCP) { net_context_put(ctx->server.net_ctx); ctx->server.net_ctx = NULL; } +#endif return; } @@ -420,7 +422,11 @@ struct net_context *_net_app_select_net_ctx(struct net_app_ctx *ctx, #if defined(CONFIG_NET_APP_SERVER) if (ctx->app_type == NET_APP_SERVER) { if (ctx->proto == IPPROTO_TCP) { +#if defined(CONFIG_NET_TCP) return ctx->server.net_ctx; +#else + return NULL; +#endif } else if (ctx->proto == IPPROTO_UDP) { if (!dst) { return ctx->default_ctx->ctx; @@ -673,7 +679,7 @@ int net_app_close(struct net_app_ctx *ctx) ctx->cb.close(ctx, 0, ctx->user_data); } -#if defined(CONFIG_NET_APP_SERVER) +#if defined(CONFIG_NET_APP_SERVER) && defined(CONFIG_NET_TCP) if (ctx->app_type == NET_APP_SERVER) { ctx->server.net_ctx = NULL; }