Skip to content

Commit ac3cb9d

Browse files
jukkarkartben
authored andcommitted
net: Change the net_linkaddr struct to not use pointers
Previously the net_linkaddr struct had pointers to the link address. This is error prone and difficult to handle if cloning the packet as those pointers can point to wrong place. Mitigate this issue by allocating the space for link address in net_linkaddr struct. This will increase the size of the net_pkt by 4 octets for IEEE 802.15.4 where the link address length is 8, but there no increase in size if link address is 6 bytes like in Ethernet/Wi-Fi. Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
1 parent fedd815 commit ac3cb9d

File tree

55 files changed

+399
-382
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+399
-382
lines changed

drivers/ethernet/dsa_ksz8xxx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,8 @@ struct net_pkt *dsa_ksz8xxx_xmit_pkt(struct net_if *iface, struct net_pkt *pkt)
938938
struct net_buf *buf;
939939
size_t len, pad = 0;
940940

941-
lladst.len = sizeof(hdr->dst.addr);
942-
lladst.addr = &hdr->dst.addr[0];
941+
(void)net_linkaddr_create(&lladst, hdr->dst.addr, sizeof(hdr->dst.addr),
942+
NET_LINK_ETHERNET);
943943

944944
len = net_pkt_get_len(pkt);
945945
/*

drivers/ethernet/eth_native_tap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ static int eth_send(const struct device *dev, struct net_pkt *pkt)
202202

203203
static struct net_linkaddr *eth_get_mac(struct eth_context *ctx)
204204
{
205-
ctx->ll_addr.addr = ctx->mac_addr;
206-
ctx->ll_addr.len = sizeof(ctx->mac_addr);
205+
(void)net_linkaddr_set(&ctx->ll_addr, ctx->mac_addr,
206+
sizeof(ctx->mac_addr));
207207

208208
return &ctx->ll_addr;
209209
}

drivers/net/ppp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,7 @@ static int ppp_driver_init(const struct device *dev)
976976

977977
static inline struct net_linkaddr *ppp_get_mac(struct ppp_driver_context *ppp)
978978
{
979-
ppp->ll_addr.addr = ppp->mac_addr;
980-
ppp->ll_addr.len = sizeof(ppp->mac_addr);
979+
(void)net_linkaddr_set(&ppp->ll_addr, ppp->mac_addr, sizeof(ppp->mac_addr));
981980

982981
return &ppp->ll_addr;
983982
}

drivers/net/slip.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ int slip_init(const struct device *dev)
366366

367367
static inline struct net_linkaddr *slip_get_mac(struct slip_context *slip)
368368
{
369-
slip->ll_addr.addr = slip->mac_addr;
370-
slip->ll_addr.len = sizeof(slip->mac_addr);
369+
(void)net_linkaddr_set(&slip->ll_addr, slip->mac_addr,
370+
sizeof(slip->mac_addr));
371371

372372
return &slip->ll_addr;
373373
}

include/zephyr/net/ieee802154.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ struct ieee802154_context {
296296
uint8_t ext_addr[IEEE802154_MAX_ADDR_LENGTH];
297297

298298
/** Link layer address (in big endian) */
299-
struct net_linkaddr_storage linkaddr;
299+
struct net_linkaddr linkaddr;
300300

301301
#ifdef CONFIG_NET_L2_IEEE802154_SECURITY
302302
/** Security context */

include/zephyr/net/net_if.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,13 +1246,20 @@ static inline int net_if_set_link_addr_unlocked(struct net_if *iface,
12461246
uint8_t *addr, uint8_t len,
12471247
enum net_link_type type)
12481248
{
1249+
int ret;
1250+
12491251
if (net_if_flag_is_set(iface, NET_IF_RUNNING)) {
12501252
return -EPERM;
12511253
}
12521254

1253-
net_if_get_link_addr(iface)->addr = addr;
1254-
net_if_get_link_addr(iface)->len = len;
1255-
net_if_get_link_addr(iface)->type = type;
1255+
if (len > sizeof(net_if_get_link_addr(iface)->addr)) {
1256+
return -EINVAL;
1257+
}
1258+
1259+
ret = net_linkaddr_create(net_if_get_link_addr(iface), addr, len, type);
1260+
if (ret < 0) {
1261+
return ret;
1262+
}
12561263

12571264
net_hostname_set_postfix(addr, len);
12581265

include/zephyr/net/net_linkaddr.h

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,6 @@ enum net_link_type {
6161
NET_LINK_CANBUS_RAW,
6262
} __packed;
6363

64-
/**
65-
* @brief Hardware link address structure
66-
*
67-
* Used to hold the link address information
68-
*/
69-
struct net_linkaddr {
70-
/** The array of byte representing the address */
71-
uint8_t *addr; /* in big endian */
72-
73-
/** Length of that address array */
74-
uint8_t len;
75-
76-
/** What kind of address is this for */
77-
uint8_t type;
78-
};
79-
8064
/**
8165
* @brief Hardware link address structure
8266
*
@@ -87,7 +71,7 @@ struct net_linkaddr {
8771
* handled differently than uint8_t addr[] and the fields are purposely
8872
* in different order.
8973
*/
90-
struct net_linkaddr_storage {
74+
struct net_linkaddr {
9175
/** What kind of address is this for */
9276
uint8_t type;
9377

@@ -124,28 +108,100 @@ static inline bool net_linkaddr_cmp(struct net_linkaddr *lladdr1,
124108
*
125109
* @brief Set the member data of a link layer address storage structure.
126110
*
127-
* @param lladdr_store The link address storage structure to change.
111+
* @param lladdr The link address storage structure to change.
128112
* @param new_addr Array of bytes containing the link address.
129113
* @param new_len Length of the link address array.
130114
* This value should always be <= NET_LINK_ADDR_MAX_LENGTH.
115+
* @return 0 if ok, <0 if error
131116
*/
132-
static inline int net_linkaddr_set(struct net_linkaddr_storage *lladdr_store,
133-
uint8_t *new_addr, uint8_t new_len)
117+
static inline int net_linkaddr_set(struct net_linkaddr *lladdr,
118+
const uint8_t *new_addr,
119+
uint8_t new_len)
134120
{
135-
if (!lladdr_store || !new_addr) {
121+
if (lladdr == NULL || new_addr == NULL) {
136122
return -EINVAL;
137123
}
138124

139125
if (new_len > NET_LINK_ADDR_MAX_LENGTH) {
140126
return -EMSGSIZE;
141127
}
142128

143-
lladdr_store->len = new_len;
144-
memcpy(lladdr_store->addr, new_addr, new_len);
129+
lladdr->len = new_len;
130+
memcpy(lladdr->addr, new_addr, new_len);
145131

146132
return 0;
147133
}
148134

135+
/**
136+
* @brief Copy link address from one variable to another.
137+
*
138+
* @param dst The link address structure destination.
139+
* @param src The link address structure to source.
140+
* @return 0 if ok, <0 if error
141+
*/
142+
static inline int net_linkaddr_copy(struct net_linkaddr *dst,
143+
const struct net_linkaddr *src)
144+
{
145+
if (dst == NULL || src == NULL) {
146+
return -EINVAL;
147+
}
148+
149+
if (src->len > NET_LINK_ADDR_MAX_LENGTH) {
150+
return -EMSGSIZE;
151+
}
152+
153+
dst->type = src->type;
154+
dst->len = src->len;
155+
memcpy(dst->addr, src->addr, src->len);
156+
157+
return 0;
158+
}
159+
160+
/**
161+
* @brief Create a link address structure.
162+
*
163+
* @param lladdr The link address structure to change.
164+
* @param addr Array of bytes containing the link address. If set to NULL,
165+
* the address will be cleared.
166+
* @param len Length of the link address array.
167+
* @param type Type of the link address.
168+
* @return 0 if ok, <0 if error
169+
*/
170+
static inline int net_linkaddr_create(struct net_linkaddr *lladdr,
171+
const uint8_t *addr, uint8_t len,
172+
enum net_link_type type)
173+
{
174+
if (lladdr == NULL) {
175+
return -EINVAL;
176+
}
177+
178+
if (len > NET_LINK_ADDR_MAX_LENGTH) {
179+
return -EMSGSIZE;
180+
}
181+
182+
if (addr == NULL) {
183+
memset(lladdr->addr, 0, NET_LINK_ADDR_MAX_LENGTH);
184+
} else {
185+
memcpy(lladdr->addr, addr, len);
186+
}
187+
188+
lladdr->type = type;
189+
lladdr->len = len;
190+
191+
return 0;
192+
}
193+
194+
/**
195+
* @brief Clear link address.
196+
*
197+
* @param lladdr The link address structure.
198+
* @return 0 if ok, <0 if error
199+
*/
200+
static inline int net_linkaddr_clear(struct net_linkaddr *lladdr)
201+
{
202+
return net_linkaddr_create(lladdr, NULL, 0, NET_LINK_UNKNOWN);
203+
}
204+
149205
/**
150206
* @}
151207
*/

include/zephyr/net/net_pkt.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,16 +1284,23 @@ static inline struct net_linkaddr *net_pkt_lladdr_dst(struct net_pkt *pkt)
12841284

12851285
static inline void net_pkt_lladdr_swap(struct net_pkt *pkt)
12861286
{
1287-
uint8_t *addr = net_pkt_lladdr_src(pkt)->addr;
1287+
struct net_linkaddr tmp;
12881288

1289-
net_pkt_lladdr_src(pkt)->addr = net_pkt_lladdr_dst(pkt)->addr;
1290-
net_pkt_lladdr_dst(pkt)->addr = addr;
1289+
memcpy(tmp.addr,
1290+
net_pkt_lladdr_src(pkt)->addr,
1291+
net_pkt_lladdr_src(pkt)->len);
1292+
memcpy(net_pkt_lladdr_src(pkt)->addr,
1293+
net_pkt_lladdr_dst(pkt)->addr,
1294+
net_pkt_lladdr_dst(pkt)->len);
1295+
memcpy(net_pkt_lladdr_dst(pkt)->addr,
1296+
tmp.addr,
1297+
net_pkt_lladdr_src(pkt)->len);
12911298
}
12921299

12931300
static inline void net_pkt_lladdr_clear(struct net_pkt *pkt)
12941301
{
1295-
net_pkt_lladdr_src(pkt)->addr = NULL;
1296-
net_pkt_lladdr_src(pkt)->len = 0U;
1302+
(void)net_linkaddr_clear(net_pkt_lladdr_src(pkt));
1303+
(void)net_linkaddr_clear(net_pkt_lladdr_dst(pkt));
12971304
}
12981305

12991306
static inline uint16_t net_pkt_ll_proto_type(struct net_pkt *pkt)

include/zephyr/net/virtual.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ struct virtual_interface_context {
160160
bool is_init;
161161

162162
/** Link address for this network interface */
163-
struct net_linkaddr_storage lladdr;
163+
struct net_linkaddr lladdr;
164164

165165
/** User friendly name of this L2 layer. */
166166
char name[VIRTUAL_MAX_NAME_LEN];

samples/net/dsa/src/dsa_lldp.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ static const uint8_t eth_filter_l2_addr_base[][6] = {
3636
enum net_verdict dsa_ll_addr_switch_cb(struct net_if *iface, struct net_pkt *pkt)
3737
{
3838
struct net_eth_hdr *hdr = NET_ETH_HDR(pkt);
39-
struct net_linkaddr lladst;
39+
struct net_linkaddr lladst = { 0 };
4040

4141
net_pkt_cursor_init(pkt);
42-
lladst.len = sizeof(hdr->dst.addr);
43-
lladst.addr = &hdr->dst.addr[0];
42+
43+
(void)net_linkaddr_set(&lladst, (const uint8_t *)hdr->dst.addr,
44+
sizeof(hdr->dst.addr));
4445

4546
/*
4647
* Pass packet to lan1..3 when matching one from

0 commit comments

Comments
 (0)