Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: Fix connectivity issues if only UDP or TCP is enabled #776

Merged
merged 1 commit into from
Jul 14, 2017
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
44 changes: 32 additions & 12 deletions subsys/net/ip/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));
}

Expand All @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions subsys/net/ip/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion subsys/net/lib/app/net_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down