Skip to content

Commit 3604c39

Browse files
committed
net: udp: Remove NET_UDP_HDR() macro and direct access to net_buf
Remove NET_UDP_HDR() macro as we cannot safely access UDP header via it if the network packet header spans over multiple net_buf fragments. Fixed also the UDP unit tests so that they pass correctly. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
1 parent 8476da9 commit 3604c39

File tree

34 files changed

+1066
-112
lines changed

34 files changed

+1066
-112
lines changed

include/net/net_pkt.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,6 @@ static inline u8_t *net_pkt_ip_data(struct net_pkt *pkt)
321321
return pkt->frags->data;
322322
}
323323

324-
static inline u8_t *net_pkt_udp_data(struct net_pkt *pkt)
325-
{
326-
return &pkt->frags->data[net_pkt_ip_hdr_len(pkt) +
327-
net_pkt_ipv6_ext_len(pkt)];
328-
}
329-
330324
static inline u8_t *net_pkt_tcp_data(struct net_pkt *pkt)
331325
{
332326
return &pkt->frags->data[net_pkt_ip_hdr_len(pkt) +
@@ -408,7 +402,6 @@ static inline void net_pkt_set_ieee802154_rssi(struct net_pkt *pkt,
408402

409403
#define NET_IPV6_HDR(pkt) ((struct net_ipv6_hdr *)net_pkt_ip_data(pkt))
410404
#define NET_IPV4_HDR(pkt) ((struct net_ipv4_hdr *)net_pkt_ip_data(pkt))
411-
#define NET_UDP_HDR(pkt) ((struct net_udp_hdr *)(net_pkt_udp_data(pkt)))
412405
#define NET_TCP_HDR(pkt) ((struct net_tcp_hdr *)(net_pkt_tcp_data(pkt)))
413406

414407
static inline void net_pkt_set_src_ipv6_addr(struct net_pkt *pkt)

include/net/udp.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/** @file
2+
@brief UDP utility functions
3+
*/
4+
5+
/*
6+
* Copyright (c) 2017 Intel Corporation
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*/
10+
11+
#ifndef __UDP_H
12+
#define __UDP_H
13+
14+
#include <zephyr/types.h>
15+
16+
#include <net/net_core.h>
17+
#include <net/net_ip.h>
18+
#include <net/net_pkt.h>
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
#if defined(CONFIG_NET_UDP)
25+
26+
/**
27+
* @brief Get UDP packet header data from net_pkt.
28+
*
29+
* @details The values in the returned header are in network byte order.
30+
* Note that you must access the UDP header values by the returned pointer,
31+
* the hdr parameter is just a placeholder for the header data and it might
32+
* not contain anything if header can fit properly in the first fragment in
33+
* the network packet.
34+
*
35+
* @param pkt Network packet
36+
* @param hdr Where to place the header if it does not fit in first fragment
37+
* of the network packet.
38+
*
39+
* @return Return pointer to header or NULL if something went wrong.
40+
*/
41+
struct net_udp_hdr *net_udp_get_hdr(struct net_pkt *pkt,
42+
struct net_udp_hdr *hdr);
43+
44+
/**
45+
* @brief Set UDP packet header data in net_pkt.
46+
*
47+
* @details The values in the header must be in network byte order.
48+
* This function is normally called after a call to net_udp_get_hdr().
49+
* The hdr parameter value should be the same that is returned by function
50+
* net_udp_get_hdr() call. Note that if the UDP header fits in first net_pkt
51+
* fragment, then this function will not do anything as your hdr parameter
52+
* was pointing directly to net_pkt.
53+
*
54+
* @param pkt Network packet
55+
* @param hdr Header data pointer that was returned by net_udp_get_hdr().
56+
*
57+
* @return Return pointer to header or NULL if something went wrong.
58+
*/
59+
struct net_udp_hdr *net_udp_set_hdr(struct net_pkt *pkt,
60+
struct net_udp_hdr *hdr);
61+
#else
62+
#define net_udp_get_hdr(pkt, frag) NULL
63+
#define net_udp_set_hdr(pkt, frag) NULL
64+
#endif /* CONFIG_NET_UDP */
65+
66+
#ifdef __cplusplus
67+
}
68+
#endif
69+
70+
#endif /* __UDP_H */

samples/bluetooth/ipsp/src/main.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <net/net_if.h>
1616
#include <net/net_core.h>
1717
#include <net/net_context.h>
18+
#include <net/udp.h>
1819

1920
#include <bluetooth/bluetooth.h>
2021
#include <gatt/ipss.h>
@@ -205,10 +206,18 @@ static inline void set_dst_addr(sa_family_t family,
205206
struct net_pkt *pkt,
206207
struct sockaddr *dst_addr)
207208
{
209+
struct net_udp_hdr hdr, *udp_hdr;
210+
211+
udp_hdr = net_udp_get_hdr(pkt, &hdr);
212+
if (!udp_hdr) {
213+
printk("Invalid UDP data\n");
214+
return;
215+
}
216+
208217
net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr,
209218
&NET_IPV6_HDR(pkt)->src);
210219
net_sin6(dst_addr)->sin6_family = AF_INET6;
211-
net_sin6(dst_addr)->sin6_port = NET_UDP_HDR(pkt)->src_port;
220+
net_sin6(dst_addr)->sin6_port = udp_hdr->src_port;
212221
}
213222

214223
static void udp_received(struct net_context *context,

samples/net/coaps_server/src/udp.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <net/net_context.h>
1010
#include <net/net_pkt.h>
1111
#include <net/net_if.h>
12+
#include <net/udp.h>
1213
#include <string.h>
1314
#include <errno.h>
1415
#include <misc/printk.h>
@@ -20,9 +21,17 @@ static const socklen_t addrlen = sizeof(struct sockaddr_in6);
2021

2122
static void set_client_address(struct sockaddr *addr, struct net_pkt *rx_pkt)
2223
{
24+
struct net_udp_hdr hdr, *udp_hdr;
25+
26+
udp_hdr = net_udp_get_hdr(rx_pkt, &hdr);
27+
if (!udp_hdr) {
28+
printk("Invalid UDP data\n");
29+
return;
30+
}
31+
2332
net_ipaddr_copy(&net_sin6(addr)->sin6_addr, &NET_IPV6_HDR(rx_pkt)->src);
2433
net_sin6(addr)->sin6_family = AF_INET6;
25-
net_sin6(addr)->sin6_port = NET_UDP_HDR(rx_pkt)->src_port;
34+
net_sin6(addr)->sin6_port = udp_hdr->src_port;
2635
}
2736

2837
static void udp_received(struct net_context *context,

samples/net/echo_server/src/echo-server.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct net_pkt *build_reply_pkt(const char *name,
7575
reply_pkt = net_app_get_net_pkt(ctx, net_pkt_family(pkt), K_FOREVER);
7676

7777
NET_ASSERT(reply_pkt);
78+
NET_ASSERT(net_pkt_family(reply_pkt) == net_pkt_family(pkt));
7879

7980
recv_len = net_pkt_get_len(pkt);
8081

samples/net/echo_server/src/udp.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <net/net_pkt.h>
2020
#include <net/net_core.h>
2121
#include <net/net_context.h>
22+
#include <net/udp.h>
2223

2324
#include <net/net_app.h>
2425

@@ -51,12 +52,19 @@ static inline void set_dst_addr(sa_family_t family,
5152
struct net_pkt *pkt,
5253
struct sockaddr *dst_addr)
5354
{
55+
struct net_udp_hdr hdr, *udp_hdr;
56+
57+
udp_hdr = net_udp_get_hdr(pkt, &hdr);
58+
if (!udp_hdr) {
59+
return;
60+
}
61+
5462
#if defined(CONFIG_NET_IPV6)
5563
if (family == AF_INET6) {
5664
net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr,
5765
&NET_IPV6_HDR(pkt)->src);
5866
net_sin6(dst_addr)->sin6_family = AF_INET6;
59-
net_sin6(dst_addr)->sin6_port = NET_UDP_HDR(pkt)->src_port;
67+
net_sin6(dst_addr)->sin6_port = udp_hdr->src_port;
6068
}
6169
#endif /* CONFIG_NET_IPV6) */
6270

@@ -65,7 +73,7 @@ static inline void set_dst_addr(sa_family_t family,
6573
net_ipaddr_copy(&net_sin(dst_addr)->sin_addr,
6674
&NET_IPV4_HDR(pkt)->src);
6775
net_sin(dst_addr)->sin_family = AF_INET;
68-
net_sin(dst_addr)->sin_port = NET_UDP_HDR(pkt)->src_port;
76+
net_sin(dst_addr)->sin_port = udp_hdr->src_port;
6977
}
7078
#endif /* CONFIG_NET_IPV6) */
7179
}

samples/net/leds_demo/src/leds-demo.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <net/net_ip.h>
2121
#include <net/net_pkt.h>
2222
#include <net/net_context.h>
23+
#include <net/udp.h>
2324

2425
#include <net_private.h>
2526

@@ -412,10 +413,19 @@ static void udp_receive(struct net_context *context,
412413
{
413414
struct zoap_packet request;
414415
struct sockaddr_in6 from;
416+
struct net_udp_hdr hdr, *udp_hdr;
415417
int r, header_len;
416418

417419
net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_HDR(pkt)->src);
418-
from.sin6_port = NET_UDP_HDR(pkt)->src_port;
420+
421+
udp_hdr = net_udp_get_hdr(pkt, &hdr);
422+
if (!udp_hdr) {
423+
printk("Invalid UDP data received\n");
424+
net_pkt_unref(pkt);
425+
return;
426+
}
427+
428+
from.sin6_port = udp_hdr->src_port;
419429
from.sin6_family = AF_INET6;
420430

421431
/*

samples/net/mbedtls_dtlsserver/src/udp.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <net/net_context.h>
1010
#include <net/net_pkt.h>
1111
#include <net/net_if.h>
12+
#include <net/udp.h>
1213
#include <string.h>
1314
#include <errno.h>
1415
#include <misc/printk.h>
@@ -21,19 +22,35 @@ static const socklen_t addrlen = sizeof(struct sockaddr_in6);
2122

2223
static void set_client_address(struct sockaddr *addr, struct net_pkt *rx_buf)
2324
{
25+
struct net_udp_hdr hdr, *udp_hdr;
26+
27+
udp_hdr = net_udp_get_hdr(rx_buf, &hdr);
28+
if (!udp_hdr) {
29+
printk("Invalid UDP data\n");
30+
return;
31+
}
32+
2433
net_ipaddr_copy(&net_sin6(addr)->sin6_addr, &NET_IPV6_HDR(rx_buf)->src);
2534
net_sin6(addr)->sin6_family = AF_INET6;
26-
net_sin6(addr)->sin6_port = NET_UDP_HDR(rx_buf)->src_port;
35+
net_sin6(addr)->sin6_port = udp_hdr->src_port;
2736
}
2837

2938
#else
3039
static const socklen_t addrlen = sizeof(struct sockaddr_in);
3140

3241
static void set_client_address(struct sockaddr *addr, struct net_pkt *rx_buf)
3342
{
43+
struct net_udp_hdr hdr, *udp_hdr;
44+
45+
udp_hdr = net_udp_get_hdr(rx_buf, &hdr);
46+
if (!udp_hdr) {
47+
printk("Invalid UDP data\n");
48+
return;
49+
}
50+
3451
net_ipaddr_copy(&net_sin(addr)->sin_addr, &NET_IPV4_HDR(rx_buf)->src);
3552
net_sin(addr)->sin_family = AF_INET;
36-
net_sin(addr)->sin_port = NET_UDP_HDR(rx_buf)->src_port;
53+
net_sin(addr)->sin_port = udp_hdr->src_port;
3754
}
3855

3956
#endif

samples/net/mqtt_publisher/prj_96b_nitrogen.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CONFIG_INIT_STACKS=y
22
CONFIG_NETWORKING=y
33

44
CONFIG_NET_TCP=y
5-
CONFIG_NET_UDP=n
5+
CONFIG_NET_UDP=y
66
CONFIG_NET_ARP=y
77
CONFIG_NET_L2_BLUETOOTH=y
88
CONFIG_NET_L2_BLUETOOTH_SEC_LEVEL=1

samples/net/zoap_client/src/zoap-client.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <net/net_pkt.h>
1515
#include <net/net_mgmt.h>
1616
#include <net/net_ip.h>
17+
#include <net/udp.h>
1718
#include <net/zoap.h>
1819

1920
#if defined(CONFIG_NET_L2_BLUETOOTH)
@@ -76,6 +77,7 @@ static void udp_receive(struct net_context *context,
7677
struct zoap_reply *reply;
7778
struct zoap_packet response;
7879
struct sockaddr_in6 from;
80+
struct net_udp_hdr hdr, *udp_hdr;
7981
int header_len, r;
8082

8183
/*
@@ -98,7 +100,14 @@ static void udp_receive(struct net_context *context,
98100
}
99101

100102
net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_HDR(pkt)->src);
101-
from.sin6_port = NET_UDP_HDR(pkt)->src_port;
103+
104+
udp_hdr = net_udp_get_hdr(pkt, &hdr);
105+
if (!udp_hdr) {
106+
printk("Invalid UDP data received\n");
107+
return;
108+
}
109+
110+
from.sin6_port = udp_hdr->src_port;
102111

103112
reply = zoap_response_received(&response,
104113
(const struct sockaddr *) &from,

0 commit comments

Comments
 (0)