1 /*
2  * Copyright (c) 2016 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Network Events code public header
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_NET_NET_EVENT_H_
13 #define ZEPHYR_INCLUDE_NET_NET_EVENT_H_
14 
15 #include <zephyr/net/net_ip.h>
16 #include <zephyr/net/hostname.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 /**
23  * @addtogroup net_mgmt
24  * @{
25  */
26 
27 /** @cond INTERNAL_HIDDEN */
28 
29 /* Network Interface events */
30 #define _NET_IF_LAYER		NET_MGMT_LAYER_L2
31 #define _NET_IF_CORE_CODE	0x001
32 #define _NET_EVENT_IF_BASE	(NET_MGMT_EVENT_BIT |			\
33 				 NET_MGMT_IFACE_BIT |			\
34 				 NET_MGMT_LAYER(_NET_IF_LAYER) |	\
35 				 NET_MGMT_LAYER_CODE(_NET_IF_CORE_CODE))
36 
37 enum net_event_if_cmd {
38 	NET_EVENT_IF_CMD_DOWN = 1,
39 	NET_EVENT_IF_CMD_UP,
40 	NET_EVENT_IF_CMD_ADMIN_DOWN,
41 	NET_EVENT_IF_CMD_ADMIN_UP,
42 };
43 
44 /* IPv6 Events */
45 #define _NET_IPV6_LAYER		NET_MGMT_LAYER_L3
46 #define _NET_IPV6_CORE_CODE	0x060
47 #define _NET_EVENT_IPV6_BASE	(NET_MGMT_EVENT_BIT |			\
48 				 NET_MGMT_IFACE_BIT |			\
49 				 NET_MGMT_LAYER(_NET_IPV6_LAYER) |	\
50 				 NET_MGMT_LAYER_CODE(_NET_IPV6_CORE_CODE))
51 
52 enum net_event_ipv6_cmd {
53 	NET_EVENT_IPV6_CMD_ADDR_ADD	= 1,
54 	NET_EVENT_IPV6_CMD_ADDR_DEL,
55 	NET_EVENT_IPV6_CMD_MADDR_ADD,
56 	NET_EVENT_IPV6_CMD_MADDR_DEL,
57 	NET_EVENT_IPV6_CMD_PREFIX_ADD,
58 	NET_EVENT_IPV6_CMD_PREFIX_DEL,
59 	NET_EVENT_IPV6_CMD_MCAST_JOIN,
60 	NET_EVENT_IPV6_CMD_MCAST_LEAVE,
61 	NET_EVENT_IPV6_CMD_ROUTER_ADD,
62 	NET_EVENT_IPV6_CMD_ROUTER_DEL,
63 	NET_EVENT_IPV6_CMD_ROUTE_ADD,
64 	NET_EVENT_IPV6_CMD_ROUTE_DEL,
65 	NET_EVENT_IPV6_CMD_DAD_SUCCEED,
66 	NET_EVENT_IPV6_CMD_DAD_FAILED,
67 	NET_EVENT_IPV6_CMD_NBR_ADD,
68 	NET_EVENT_IPV6_CMD_NBR_DEL,
69 	NET_EVENT_IPV6_CMD_DHCP_START,
70 	NET_EVENT_IPV6_CMD_DHCP_BOUND,
71 	NET_EVENT_IPV6_CMD_DHCP_STOP,
72 	NET_EVENT_IPV6_CMD_ADDR_DEPRECATED,
73 	NET_EVENT_IPV6_CMD_PE_ENABLED,
74 	NET_EVENT_IPV6_CMD_PE_DISABLED,
75 	NET_EVENT_IPV6_CMD_PE_FILTER_ADD,
76 	NET_EVENT_IPV6_CMD_PE_FILTER_DEL,
77 };
78 
79 /* IPv4 Events*/
80 #define _NET_IPV4_LAYER		NET_MGMT_LAYER_L3
81 #define _NET_IPV4_CORE_CODE	0x004
82 #define _NET_EVENT_IPV4_BASE	(NET_MGMT_EVENT_BIT |			\
83 				 NET_MGMT_IFACE_BIT |			\
84 				 NET_MGMT_LAYER(_NET_IPV4_LAYER) |	\
85 				 NET_MGMT_LAYER_CODE(_NET_IPV4_CORE_CODE))
86 
87 enum net_event_ipv4_cmd {
88 	NET_EVENT_IPV4_CMD_ADDR_ADD	= 1,
89 	NET_EVENT_IPV4_CMD_ADDR_DEL,
90 	NET_EVENT_IPV4_CMD_MADDR_ADD,
91 	NET_EVENT_IPV4_CMD_MADDR_DEL,
92 	NET_EVENT_IPV4_CMD_ROUTER_ADD,
93 	NET_EVENT_IPV4_CMD_ROUTER_DEL,
94 	NET_EVENT_IPV4_CMD_DHCP_START,
95 	NET_EVENT_IPV4_CMD_DHCP_BOUND,
96 	NET_EVENT_IPV4_CMD_DHCP_STOP,
97 	NET_EVENT_IPV4_CMD_MCAST_JOIN,
98 	NET_EVENT_IPV4_CMD_MCAST_LEAVE,
99 	NET_EVENT_IPV4_CMD_ACD_SUCCEED,
100 	NET_EVENT_IPV4_CMD_ACD_FAILED,
101 	NET_EVENT_IPV4_CMD_ACD_CONFLICT,
102 };
103 
104 /* L4 network events */
105 #define _NET_L4_LAYER		NET_MGMT_LAYER_L4
106 #define _NET_L4_CORE_CODE	0x114
107 #define _NET_EVENT_L4_BASE	(NET_MGMT_EVENT_BIT |			\
108 				 NET_MGMT_IFACE_BIT |			\
109 				 NET_MGMT_LAYER(_NET_L4_LAYER) |	\
110 				 NET_MGMT_LAYER_CODE(_NET_L4_CORE_CODE))
111 
112 enum net_event_l4_cmd {
113 	NET_EVENT_L4_CMD_CONNECTED = 1,
114 	NET_EVENT_L4_CMD_DISCONNECTED,
115 	NET_EVENT_L4_CMD_IPV4_CONNECTED,
116 	NET_EVENT_L4_CMD_IPV4_DISCONNECTED,
117 	NET_EVENT_L4_CMD_IPV6_CONNECTED,
118 	NET_EVENT_L4_CMD_IPV6_DISCONNECTED,
119 	NET_EVENT_L4_CMD_DNS_SERVER_ADD,
120 	NET_EVENT_L4_CMD_DNS_SERVER_DEL,
121 	NET_EVENT_L4_CMD_HOSTNAME_CHANGED,
122 	NET_EVENT_L4_CMD_CAPTURE_STARTED,
123 	NET_EVENT_L4_CMD_CAPTURE_STOPPED,
124 };
125 
126 /** @endcond */
127 
128 /** Event emitted when the network interface goes down. */
129 #define NET_EVENT_IF_DOWN					\
130 	(_NET_EVENT_IF_BASE | NET_EVENT_IF_CMD_DOWN)
131 
132 /** Event emitted when the network interface goes up. */
133 #define NET_EVENT_IF_UP						\
134 	(_NET_EVENT_IF_BASE | NET_EVENT_IF_CMD_UP)
135 
136 /** Event emitted when the network interface is taken down manually. */
137 #define NET_EVENT_IF_ADMIN_DOWN					\
138 	(_NET_EVENT_IF_BASE | NET_EVENT_IF_CMD_ADMIN_DOWN)
139 
140 /** Event emitted when the network interface goes up manually. */
141 #define NET_EVENT_IF_ADMIN_UP					\
142 	(_NET_EVENT_IF_BASE | NET_EVENT_IF_CMD_ADMIN_UP)
143 
144 /** Event emitted when an IPv6 address is added to the system. */
145 #define NET_EVENT_IPV6_ADDR_ADD					\
146 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_ADDR_ADD)
147 
148 /** Event emitted when an IPv6 address is removed from the system. */
149 #define NET_EVENT_IPV6_ADDR_DEL					\
150 	(_NET_EVENT_IPV6_BASE |	NET_EVENT_IPV6_CMD_ADDR_DEL)
151 
152 /** Event emitted when an IPv6 multicast address is added to the system. */
153 #define NET_EVENT_IPV6_MADDR_ADD				\
154 	(_NET_EVENT_IPV6_BASE |	NET_EVENT_IPV6_CMD_MADDR_ADD)
155 
156 /** Event emitted when an IPv6 multicast address is removed from the system. */
157 #define NET_EVENT_IPV6_MADDR_DEL				\
158 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_MADDR_DEL)
159 
160 /** Event emitted when an IPv6 prefix is added to the system. */
161 #define NET_EVENT_IPV6_PREFIX_ADD				\
162 	(_NET_EVENT_IPV6_BASE |	NET_EVENT_IPV6_CMD_PREFIX_ADD)
163 
164 /** Event emitted when an IPv6 prefix is removed from the system. */
165 #define NET_EVENT_IPV6_PREFIX_DEL				\
166 	(_NET_EVENT_IPV6_BASE |	NET_EVENT_IPV6_CMD_PREFIX_DEL)
167 
168 /** Event emitted when an IPv6 multicast group is joined. */
169 #define NET_EVENT_IPV6_MCAST_JOIN				\
170 	(_NET_EVENT_IPV6_BASE |	NET_EVENT_IPV6_CMD_MCAST_JOIN)
171 
172 /** Event emitted when an IPv6 multicast group is left. */
173 #define NET_EVENT_IPV6_MCAST_LEAVE				\
174 	(_NET_EVENT_IPV6_BASE |	NET_EVENT_IPV6_CMD_MCAST_LEAVE)
175 
176 /** Event emitted when an IPv6 router is added to the system. */
177 #define NET_EVENT_IPV6_ROUTER_ADD				\
178 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_ROUTER_ADD)
179 
180 /** Event emitted when an IPv6 router is removed from the system. */
181 #define NET_EVENT_IPV6_ROUTER_DEL				\
182 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_ROUTER_DEL)
183 
184 /** Event emitted when an IPv6 route is added to the system. */
185 #define NET_EVENT_IPV6_ROUTE_ADD				\
186 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_ROUTE_ADD)
187 
188 /** Event emitted when an IPv6 route is removed from the system. */
189 #define NET_EVENT_IPV6_ROUTE_DEL				\
190 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_ROUTE_DEL)
191 
192 /** Event emitted when an IPv6 duplicate address detection succeeds. */
193 #define NET_EVENT_IPV6_DAD_SUCCEED				\
194 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_DAD_SUCCEED)
195 
196 /** Event emitted when an IPv6 duplicate address detection fails. */
197 #define NET_EVENT_IPV6_DAD_FAILED				\
198 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_DAD_FAILED)
199 
200 /** Event emitted when an IPv6 neighbor is added to the system. */
201 #define NET_EVENT_IPV6_NBR_ADD					\
202 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_NBR_ADD)
203 
204 /** Event emitted when an IPv6 neighbor is removed from the system. */
205 #define NET_EVENT_IPV6_NBR_DEL					\
206 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_NBR_DEL)
207 
208 /** Event emitted when an IPv6 DHCP client starts. */
209 #define NET_EVENT_IPV6_DHCP_START				\
210 	(_NET_EVENT_IPV6_BASE |	NET_EVENT_IPV6_CMD_DHCP_START)
211 
212 /** Event emitted when an IPv6 DHCP client address is bound. */
213 #define NET_EVENT_IPV6_DHCP_BOUND				\
214 	(_NET_EVENT_IPV6_BASE |	NET_EVENT_IPV6_CMD_DHCP_BOUND)
215 
216 /** Event emitted when an IPv6 DHCP client is stopped. */
217 #define NET_EVENT_IPV6_DHCP_STOP				\
218 	(_NET_EVENT_IPV6_BASE |	NET_EVENT_IPV6_CMD_DHCP_STOP)
219 
220 /** IPv6 address is deprecated. */
221 #define NET_EVENT_IPV6_ADDR_DEPRECATED				\
222 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_ADDR_DEPRECATED)
223 
224 /** IPv6 Privacy extension is enabled. */
225 #define NET_EVENT_IPV6_PE_ENABLED				\
226 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_PE_ENABLED)
227 
228 /** IPv6 Privacy extension is disabled. */
229 #define NET_EVENT_IPV6_PE_DISABLED				\
230 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_PE_DISABLED)
231 
232 /** IPv6 Privacy extension filter is added. */
233 #define NET_EVENT_IPV6_PE_FILTER_ADD				\
234 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_PE_FILTER_ADD)
235 
236 /** IPv6 Privacy extension filter is removed. */
237 #define NET_EVENT_IPV6_PE_FILTER_DEL				\
238 	(_NET_EVENT_IPV6_BASE | NET_EVENT_IPV6_CMD_PE_FILTER_DEL)
239 
240 /** Event emitted when an IPv4 address is added to the system. */
241 #define NET_EVENT_IPV4_ADDR_ADD					\
242 	(_NET_EVENT_IPV4_BASE | NET_EVENT_IPV4_CMD_ADDR_ADD)
243 
244 /** Event emitted when an IPv4 address is removed from the system. */
245 #define NET_EVENT_IPV4_ADDR_DEL					\
246 	(_NET_EVENT_IPV4_BASE |	NET_EVENT_IPV4_CMD_ADDR_DEL)
247 
248 /** Event emitted when an IPv4 multicast address is added to the system. */
249 #define NET_EVENT_IPV4_MADDR_ADD				\
250 	(_NET_EVENT_IPV4_BASE |	NET_EVENT_IPV4_CMD_MADDR_ADD)
251 
252 /** Event emitted when an IPv4 multicast address is removed from the system. */
253 #define NET_EVENT_IPV4_MADDR_DEL				\
254 	(_NET_EVENT_IPV4_BASE | NET_EVENT_IPV4_CMD_MADDR_DEL)
255 
256 /** Event emitted when an IPv4 router is added to the system. */
257 #define NET_EVENT_IPV4_ROUTER_ADD				\
258 	(_NET_EVENT_IPV4_BASE |	NET_EVENT_IPV4_CMD_ROUTER_ADD)
259 
260 /** Event emitted when an IPv4 router is removed from the system. */
261 #define NET_EVENT_IPV4_ROUTER_DEL				\
262 	(_NET_EVENT_IPV4_BASE |	NET_EVENT_IPV4_CMD_ROUTER_DEL)
263 
264 /** Event emitted when an IPv4 DHCP client is started. */
265 #define NET_EVENT_IPV4_DHCP_START				\
266 	(_NET_EVENT_IPV4_BASE |	NET_EVENT_IPV4_CMD_DHCP_START)
267 
268 /** Event emitted when an IPv4 DHCP client address is bound. */
269 #define NET_EVENT_IPV4_DHCP_BOUND				\
270 	(_NET_EVENT_IPV4_BASE |	NET_EVENT_IPV4_CMD_DHCP_BOUND)
271 
272 /** Event emitted when an IPv4 DHCP client is stopped. */
273 #define NET_EVENT_IPV4_DHCP_STOP				\
274 	(_NET_EVENT_IPV4_BASE |	NET_EVENT_IPV4_CMD_DHCP_STOP)
275 
276 /** Event emitted when an IPv4 multicast group is joined. */
277 #define NET_EVENT_IPV4_MCAST_JOIN				\
278 	(_NET_EVENT_IPV4_BASE |	NET_EVENT_IPV4_CMD_MCAST_JOIN)
279 
280 /** Event emitted when an IPv4 multicast group is left. */
281 #define NET_EVENT_IPV4_MCAST_LEAVE				\
282 	(_NET_EVENT_IPV4_BASE |	NET_EVENT_IPV4_CMD_MCAST_LEAVE)
283 
284 /** Event emitted when an IPv4 address conflict detection succeeds. */
285 #define NET_EVENT_IPV4_ACD_SUCCEED				\
286 	(_NET_EVENT_IPV4_BASE | NET_EVENT_IPV4_CMD_ACD_SUCCEED)
287 
288 /** Event emitted when an IPv4 address conflict detection fails. */
289 #define NET_EVENT_IPV4_ACD_FAILED				\
290 	(_NET_EVENT_IPV4_BASE | NET_EVENT_IPV4_CMD_ACD_FAILED)
291 
292 /** Event emitted when an IPv4 address conflict was detected after the address
293  *  was confirmed as safe to use. It's up to the application to determine on
294  *  how to act in such case.
295  */
296 #define NET_EVENT_IPV4_ACD_CONFLICT				\
297 	(_NET_EVENT_IPV4_BASE | NET_EVENT_IPV4_CMD_ACD_CONFLICT)
298 
299 /** Event emitted when the system is considered to be connected.
300  * The connected in this context means that the network interface is up,
301  * and the interface has either IPv4 or IPv6 address assigned to it.
302  */
303 #define NET_EVENT_L4_CONNECTED					\
304 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_CONNECTED)
305 
306 /** Event emitted when the system is no longer connected.
307  * Typically this means that network connectivity is lost either by
308  * the network interface is going down, or the interface has no longer
309  * an IP address etc.
310  */
311 #define NET_EVENT_L4_DISCONNECTED			\
312 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_DISCONNECTED)
313 
314 
315 /** Event raised when IPv4 network connectivity is available. */
316 #define NET_EVENT_L4_IPV4_CONNECTED			\
317 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_IPV4_CONNECTED)
318 
319 /** Event emitted when IPv4 network connectivity is lost. */
320 #define NET_EVENT_L4_IPV4_DISCONNECTED			\
321 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_IPV4_DISCONNECTED)
322 
323 /** Event emitted when IPv6 network connectivity is available. */
324 #define NET_EVENT_L4_IPV6_CONNECTED			\
325 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_IPV6_CONNECTED)
326 
327 /** Event emitted when IPv6 network connectivity is lost. */
328 #define NET_EVENT_L4_IPV6_DISCONNECTED			\
329 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_IPV6_DISCONNECTED)
330 
331 /** Event emitted when a DNS server is added to the system. */
332 #define NET_EVENT_DNS_SERVER_ADD			\
333 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_DNS_SERVER_ADD)
334 
335 /** Event emitted when a DNS server is removed from the system. */
336 #define NET_EVENT_DNS_SERVER_DEL			\
337 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_DNS_SERVER_DEL)
338 
339 /** Event emitted when the system hostname is changed. */
340 #define NET_EVENT_HOSTNAME_CHANGED			\
341 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_HOSTNAME_CHANGED)
342 
343 /** Network packet capture is started. */
344 #define NET_EVENT_CAPTURE_STARTED			\
345 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_CAPTURE_STARTED)
346 
347 /** Network packet capture is stopped. */
348 #define NET_EVENT_CAPTURE_STOPPED			\
349 	(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_CAPTURE_STOPPED)
350 
351 /**
352  * @brief Network Management event information structure
353  * Used to pass information on network events like
354  *   NET_EVENT_IPV6_ADDR_ADD,
355  *   NET_EVENT_IPV6_ADDR_DEL,
356  *   NET_EVENT_IPV6_MADDR_ADD and
357  *   NET_EVENT_IPV6_MADDR_DEL
358  * when CONFIG_NET_MGMT_EVENT_INFO enabled and event generator pass the
359  * information.
360  */
361 struct net_event_ipv6_addr {
362 	/** IPv6 address related to this event */
363 	struct in6_addr addr;
364 };
365 
366 /**
367  * @brief Network Management event information structure
368  * Used to pass information on network events like
369  *   NET_EVENT_IPV6_NBR_ADD and
370  *   NET_EVENT_IPV6_NBR_DEL
371  * when CONFIG_NET_MGMT_EVENT_INFO enabled and event generator pass the
372  * information.
373  * @note: idx will be '-1' in case of NET_EVENT_IPV6_NBR_DEL event.
374  */
375 struct net_event_ipv6_nbr {
376 	/** Neighbor IPv6 address */
377 	struct in6_addr addr;
378 	/** Neighbor index in cache */
379 	int idx;
380 };
381 
382 /**
383  * @brief Network Management event information structure
384  * Used to pass information on network events like
385  *   NET_EVENT_IPV6_ROUTE_ADD and
386  *   NET_EVENT_IPV6_ROUTE_DEL
387  * when CONFIG_NET_MGMT_EVENT_INFO enabled and event generator pass the
388  * information.
389  */
390 struct net_event_ipv6_route {
391 	/** IPv6 address of the next hop */
392 	struct in6_addr nexthop;
393 	/** IPv6 address or prefix of the route */
394 	struct in6_addr addr;
395 	/** IPv6 prefix length */
396 	uint8_t prefix_len;
397 };
398 
399 /**
400  * @brief Network Management event information structure
401  * Used to pass information on network events like
402  *   NET_EVENT_IPV6_PREFIX_ADD and
403  *   NET_EVENT_IPV6_PREFIX_DEL
404  * when CONFIG_NET_MGMT_EVENT_INFO is enabled and event generator pass the
405  * information.
406  */
407 struct net_event_ipv6_prefix {
408 	/** IPv6 prefix */
409 	struct in6_addr addr;
410 	/** IPv6 prefix length */
411 	uint8_t len;
412 	/** IPv6 prefix lifetime in seconds */
413 	uint32_t lifetime;
414 };
415 
416 /**
417  * @brief Network Management event information structure
418  * Used to pass information on NET_EVENT_HOSTNAME_CHANGED event when
419  * CONFIG_NET_MGMT_EVENT_INFO is enabled and event generator pass the
420  * information.
421  */
422 struct net_event_l4_hostname {
423 	/** New hostname */
424 	char hostname[NET_HOSTNAME_SIZE];
425 };
426 
427 /**
428  * @brief Network Management event information structure
429  * Used to pass information on network events like
430  *   NET_EVENT_IPV6_PE_FILTER_ADD and
431  *   NET_EVENT_IPV6_PE_FILTER_DEL
432  * when CONFIG_NET_MGMT_EVENT_INFO is enabled and event generator pass the
433  * information.
434  *
435  * This is only available if CONFIG_NET_IPV6_PE_FILTER_PREFIX_COUNT is >0.
436  */
437 struct net_event_ipv6_pe_filter {
438 	/** IPv6 address of privacy extension filter */
439 	struct in6_addr prefix;
440 	/** IPv6 filter deny or allow list */
441 	bool is_deny_list;
442 };
443 
444 #ifdef __cplusplus
445 }
446 #endif
447 
448 /**
449  * @}
450  */
451 
452 #endif /* ZEPHYR_INCLUDE_NET_NET_EVENT_H_ */
453