1 /*
2  * Copyright (c) 2016 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Public API for network link address
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_NET_NET_LINKADDR_H_
13 #define ZEPHYR_INCLUDE_NET_NET_LINKADDR_H_
14 
15 #include <zephyr/types.h>
16 #include <stdbool.h>
17 #include <errno.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /**
24  * @brief Network link address library
25  * @defgroup net_linkaddr Network Link Address Library
26  * @ingroup networking
27  * @{
28  */
29 
30 /** Maximum length of the link address */
31 #ifdef CONFIG_NET_L2_IEEE802154
32 #define NET_LINK_ADDR_MAX_LENGTH 8
33 #else
34 #ifdef CONFIG_NET_L2_PPP
35 #define NET_LINK_ADDR_MAX_LENGTH 8
36 #else
37 #define NET_LINK_ADDR_MAX_LENGTH 6
38 #endif
39 #endif
40 
41 /**
42  * Type of the link address. This indicates the network technology that this
43  * address is used in. Note that in order to save space we store the value
44  * into a uint8_t variable, so please do not introduce any values > 255 in
45  * this enum.
46  */
47 enum net_link_type {
48 	/** Unknown link address type. */
49 	NET_LINK_UNKNOWN = 0,
50 	/** IEEE 802.15.4 link address. */
51 	NET_LINK_IEEE802154,
52 	/** Bluetooth IPSP link address. */
53 	NET_LINK_BLUETOOTH,
54 	/** Ethernet link address. */
55 	NET_LINK_ETHERNET,
56 	/** Dummy link address. Used in testing apps and loopback support. */
57 	NET_LINK_DUMMY,
58 	/** CANBUS link address. */
59 	NET_LINK_CANBUS_RAW,
60 } __packed;
61 
62 /**
63  *  @brief Hardware link address structure
64  *
65  *  Used to hold the link address information
66  */
67 struct net_linkaddr {
68 	/** The array of byte representing the address */
69 	uint8_t *addr; /* in big endian */
70 
71 	/** Length of that address array */
72 	uint8_t len;
73 
74 	/** What kind of address is this for */
75 	uint8_t type;
76 };
77 
78 /**
79  *  @brief Hardware link address structure
80  *
81  *  Used to hold the link address information. This variant is needed
82  *  when we have to store the link layer address.
83  *
84  *  Note that you cannot cast this to net_linkaddr as uint8_t * is
85  *  handled differently than uint8_t addr[] and the fields are purposely
86  *  in different order.
87  */
88 struct net_linkaddr_storage {
89 	/** What kind of address is this for */
90 	uint8_t type;
91 
92 	/** The real length of the ll address. */
93 	uint8_t len;
94 
95 	/** The array of bytes representing the address */
96 	uint8_t addr[NET_LINK_ADDR_MAX_LENGTH]; /* in big endian */
97 };
98 
99 /**
100  * @brief Compare two link layer addresses.
101  *
102  * @param lladdr1 Pointer to a link layer address
103  * @param lladdr2 Pointer to a link layer address
104  *
105  * @return True if the addresses are the same, false otherwise.
106  */
net_linkaddr_cmp(struct net_linkaddr * lladdr1,struct net_linkaddr * lladdr2)107 static inline bool net_linkaddr_cmp(struct net_linkaddr *lladdr1,
108 				    struct net_linkaddr *lladdr2)
109 {
110 	if (!lladdr1 || !lladdr2) {
111 		return false;
112 	}
113 
114 	if (lladdr1->len != lladdr2->len) {
115 		return false;
116 	}
117 
118 	return !memcmp(lladdr1->addr, lladdr2->addr, lladdr1->len);
119 }
120 
121 /**
122  *
123  * @brief Set the member data of a link layer address storage structure.
124  *
125  * @param lladdr_store The link address storage structure to change.
126  * @param new_addr Array of bytes containing the link address.
127  * @param new_len Length of the link address array.
128  * This value should always be <= NET_LINK_ADDR_MAX_LENGTH.
129  */
net_linkaddr_set(struct net_linkaddr_storage * lladdr_store,uint8_t * new_addr,uint8_t new_len)130 static inline int net_linkaddr_set(struct net_linkaddr_storage *lladdr_store,
131 				   uint8_t *new_addr, uint8_t new_len)
132 {
133 	if (!lladdr_store || !new_addr) {
134 		return -EINVAL;
135 	}
136 
137 	if (new_len > NET_LINK_ADDR_MAX_LENGTH) {
138 		return -EMSGSIZE;
139 	}
140 
141 	lladdr_store->len = new_len;
142 	memcpy(lladdr_store->addr, new_addr, new_len);
143 
144 	return 0;
145 }
146 
147 /**
148  * @}
149  */
150 
151 #ifdef __cplusplus
152 }
153 #endif
154 
155 #endif /* ZEPHYR_INCLUDE_NET_NET_LINKADDR_H_ */
156