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 interface
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_NET_NET_IF_H_
13 #define ZEPHYR_INCLUDE_NET_NET_IF_H_
14 
15 /**
16  * @brief Network Interface abstraction layer
17  * @defgroup net_if Network Interface abstraction layer
18  * @ingroup networking
19  * @{
20  */
21 
22 #include <zephyr/device.h>
23 #include <zephyr/sys/slist.h>
24 #include <zephyr/sys/iterable_sections.h>
25 #include <zephyr/net/net_core.h>
26 #include <zephyr/net/hostname.h>
27 #include <zephyr/net/net_linkaddr.h>
28 #include <zephyr/net/net_ip.h>
29 #include <zephyr/net/net_l2.h>
30 #include <zephyr/net/net_stats.h>
31 #include <zephyr/net/net_timeout.h>
32 
33 #if defined(CONFIG_NET_DHCPV4) && defined(CONFIG_NET_NATIVE_IPV4)
34 #include <zephyr/net/dhcpv4.h>
35 #endif
36 #if defined(CONFIG_NET_DHCPV6) && defined(CONFIG_NET_NATIVE_IPV6)
37 #include <zephyr/net/dhcpv6.h>
38 #endif
39 #if defined(CONFIG_NET_IPV4_AUTO) && defined(CONFIG_NET_NATIVE_IPV4)
40 #include <zephyr/net/ipv4_autoconf.h>
41 #endif
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /**
48  * @brief Network Interface unicast IP addresses
49  *
50  * Stores the unicast IP addresses assigned to this network interface.
51  */
52 struct net_if_addr {
53 	/** IP address */
54 	struct net_addr address;
55 
56 #if defined(CONFIG_NET_NATIVE_IPV6)
57 	struct net_timeout lifetime;
58 #endif
59 
60 #if defined(CONFIG_NET_IPV6_DAD) && defined(CONFIG_NET_NATIVE_IPV6)
61 	/** Duplicate address detection (DAD) timer */
62 	sys_snode_t dad_node;
63 	uint32_t dad_start;
64 #endif
65 	/** How the IP address was set */
66 	enum net_addr_type addr_type;
67 
68 	/** What is the current state of the address */
69 	enum net_addr_state addr_state;
70 
71 #if defined(CONFIG_NET_IPV6_DAD) && defined(CONFIG_NET_NATIVE_IPV6)
72 	/** How many times we have done DAD */
73 	uint8_t dad_count;
74 #endif
75 
76 	/** Is the IP address valid forever */
77 	uint8_t is_infinite : 1;
78 
79 	/** Is this IP address used or not */
80 	uint8_t is_used : 1;
81 
82 	/** Is this IP address usage limited to the subnet (mesh) or not */
83 	uint8_t is_mesh_local : 1;
84 
85 	uint8_t _unused : 5;
86 };
87 
88 /**
89  * @brief Network Interface multicast IP addresses
90  *
91  * Stores the multicast IP addresses assigned to this network interface.
92  */
93 struct net_if_mcast_addr {
94 	/** IP address */
95 	struct net_addr address;
96 
97 	/** Is this multicast IP address used or not */
98 	uint8_t is_used : 1;
99 
100 	/** Did we join to this group */
101 	uint8_t is_joined : 1;
102 
103 	uint8_t _unused : 6;
104 };
105 
106 /**
107  * @brief Network Interface IPv6 prefixes
108  *
109  * Stores the multicast IP addresses assigned to this network interface.
110  */
111 struct net_if_ipv6_prefix {
112 	/** Prefix lifetime */
113 	struct net_timeout lifetime;
114 
115 	/** IPv6 prefix */
116 	struct in6_addr prefix;
117 
118 	/** Backpointer to network interface where this prefix is used */
119 	struct net_if *iface;
120 
121 	/** Prefix length */
122 	uint8_t len;
123 
124 	/** Is the IP prefix valid forever */
125 	uint8_t is_infinite : 1;
126 
127 	/** Is this prefix used or not */
128 	uint8_t is_used : 1;
129 
130 	uint8_t _unused : 6;
131 };
132 
133 /**
134  * @brief Information about routers in the system.
135  *
136  * Stores the router information.
137  */
138 struct net_if_router {
139 	/** Slist lifetime timer node */
140 	sys_snode_t node;
141 
142 	/** IP address */
143 	struct net_addr address;
144 
145 	/** Network interface the router is connected to */
146 	struct net_if *iface;
147 
148 	/** Router life timer start */
149 	uint32_t life_start;
150 
151 	/** Router lifetime */
152 	uint16_t lifetime;
153 
154 	/** Is this router used or not */
155 	uint8_t is_used : 1;
156 
157 	/** Is default router */
158 	uint8_t is_default : 1;
159 
160 	/** Is the router valid forever */
161 	uint8_t is_infinite : 1;
162 
163 	uint8_t _unused : 5;
164 };
165 
166 /** Network interface flags. */
167 enum net_if_flag {
168 	/** Interface is admin up. */
169 	NET_IF_UP,
170 
171 	/** Interface is pointopoint */
172 	NET_IF_POINTOPOINT,
173 
174 	/** Interface is in promiscuous mode */
175 	NET_IF_PROMISC,
176 
177 	/** Do not start the interface immediately after initialization.
178 	 * This requires that either the device driver or some other entity
179 	 * will need to manually take the interface up when needed.
180 	 * For example for Ethernet this will happen when the driver calls
181 	 * the net_eth_carrier_on() function.
182 	 */
183 	NET_IF_NO_AUTO_START,
184 
185 	/** Power management specific: interface is being suspended */
186 	NET_IF_SUSPENDED,
187 
188 	/** Flag defines if received multicasts of other interface are
189 	 * forwarded on this interface. This activates multicast
190 	 * routing / forwarding for this interface.
191 	 */
192 	NET_IF_FORWARD_MULTICASTS,
193 
194 	/** Interface supports IPv4 */
195 	NET_IF_IPV4,
196 
197 	/** Interface supports IPv6 */
198 	NET_IF_IPV6,
199 
200 	/** Interface up and running (ready to receive and transmit). */
201 	NET_IF_RUNNING,
202 
203 	/** Driver signals L1 is up. */
204 	NET_IF_LOWER_UP,
205 
206 	/** Driver signals dormant. */
207 	NET_IF_DORMANT,
208 
209 	/** IPv6 Neighbor Discovery disabled. */
210 	NET_IF_IPV6_NO_ND,
211 
212 	/** IPv6 Multicast Listener Discovery disabled. */
213 	NET_IF_IPV6_NO_MLD,
214 
215 /** @cond INTERNAL_HIDDEN */
216 	/* Total number of flags - must be at the end of the enum */
217 	NET_IF_NUM_FLAGS
218 /** @endcond */
219 };
220 
221 /** Network interface operational status (RFC 2863). */
222 enum net_if_oper_state {
223 	NET_IF_OPER_UNKNOWN,
224 	NET_IF_OPER_NOTPRESENT,
225 	NET_IF_OPER_DOWN,
226 	NET_IF_OPER_LOWERLAYERDOWN,
227 	NET_IF_OPER_TESTING,
228 	NET_IF_OPER_DORMANT,
229 	NET_IF_OPER_UP,
230 } __packed;
231 
232 #if defined(CONFIG_NET_OFFLOAD)
233 struct net_offload;
234 #endif /* CONFIG_NET_OFFLOAD */
235 
236 /** @cond INTERNAL_HIDDEN */
237 #if defined(CONFIG_NET_NATIVE_IPV6)
238 #define NET_IF_MAX_IPV6_ADDR CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT
239 #define NET_IF_MAX_IPV6_MADDR CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT
240 #define NET_IF_MAX_IPV6_PREFIX CONFIG_NET_IF_IPV6_PREFIX_COUNT
241 #else
242 #define NET_IF_MAX_IPV6_ADDR 0
243 #define NET_IF_MAX_IPV6_MADDR 0
244 #define NET_IF_MAX_IPV6_PREFIX 0
245 #endif
246 /* @endcond */
247 
248 struct net_if_ipv6 {
249 	/** Unicast IP addresses */
250 	struct net_if_addr unicast[NET_IF_MAX_IPV6_ADDR];
251 
252 	/** Multicast IP addresses */
253 	struct net_if_mcast_addr mcast[NET_IF_MAX_IPV6_MADDR];
254 
255 	/** Prefixes */
256 	struct net_if_ipv6_prefix prefix[NET_IF_MAX_IPV6_PREFIX];
257 
258 	/** Default reachable time (RFC 4861, page 52) */
259 	uint32_t base_reachable_time;
260 
261 	/** Reachable time (RFC 4861, page 20) */
262 	uint32_t reachable_time;
263 
264 	/** Retransmit timer (RFC 4861, page 52) */
265 	uint32_t retrans_timer;
266 #if defined(CONFIG_NET_IPV6_ND) && defined(CONFIG_NET_NATIVE_IPV6)
267 	/** Router solicitation timer node */
268 	sys_snode_t rs_node;
269 
270 	/* RS start time */
271 	uint32_t rs_start;
272 
273 	/** RS count */
274 	uint8_t rs_count;
275 #endif
276 
277 	/** IPv6 hop limit */
278 	uint8_t hop_limit;
279 };
280 
281 #if defined(CONFIG_NET_DHCPV6) && defined(CONFIG_NET_NATIVE_IPV6)
282 struct net_if_dhcpv6 {
283 	/** Used for timer list. */
284 	sys_snode_t node;
285 
286 	/** Generated Client ID. */
287 	struct net_dhcpv6_duid_storage clientid;
288 
289 	/** Server ID of the selected server. */
290 	struct net_dhcpv6_duid_storage serverid;
291 
292 	/** DHCPv6 client state. */
293 	enum net_dhcpv6_state state;
294 
295 	/** DHCPv6 client configuration parameters. */
296 	struct net_dhcpv6_params params;
297 
298 	/** Timeout for the next event, absolute time, milliseconds. */
299 	uint64_t timeout;
300 
301 	/** Time of the current exchange start, absolute time, milliseconds */
302 	uint64_t exchange_start;
303 
304 	/** Renewal time, absolute time, milliseconds. */
305 	uint64_t t1;
306 
307 	/** Rebinding time, absolute time, milliseconds. */
308 	uint64_t t2;
309 
310 	/** The time when the last lease expires (terminates rebinding,
311 	 *  DHCPv6 RFC8415, ch. 18.2.5). Absolute time, milliseconds.
312 	 */
313 	uint64_t expire;
314 
315 	/** Generated IAID for IA_NA. */
316 	uint32_t addr_iaid;
317 
318 	/** Generated IAID for IA_PD. */
319 	uint32_t prefix_iaid;
320 
321 	/** Retransmit timeout for the current message, milliseconds. */
322 	uint32_t retransmit_timeout;
323 
324 	/** Current best server preference received. */
325 	int16_t server_preference;
326 
327 	/** Retransmission counter. */
328 	uint8_t retransmissions;
329 
330 	/** Transaction ID for current exchange. */
331 	uint8_t tid[DHCPV6_TID_SIZE];
332 
333 	/** Prefix length. */
334 	uint8_t prefix_len;
335 
336 	/** Assigned IPv6 prefix. */
337 	struct in6_addr prefix;
338 
339 	/** Assigned IPv6 address. */
340 	struct in6_addr addr;
341 };
342 #endif /* defined(CONFIG_NET_DHCPV6) && defined(CONFIG_NET_NATIVE_IPV6) */
343 
344 /** @cond INTERNAL_HIDDEN */
345 #if defined(CONFIG_NET_NATIVE_IPV4)
346 #define NET_IF_MAX_IPV4_ADDR CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT
347 #define NET_IF_MAX_IPV4_MADDR CONFIG_NET_IF_MCAST_IPV4_ADDR_COUNT
348 #else
349 #define NET_IF_MAX_IPV4_ADDR 0
350 #define NET_IF_MAX_IPV4_MADDR 0
351 #endif
352 /** @endcond */
353 
354 struct net_if_ipv4 {
355 	/** Unicast IP addresses */
356 	struct net_if_addr unicast[NET_IF_MAX_IPV4_ADDR];
357 
358 	/** Multicast IP addresses */
359 	struct net_if_mcast_addr mcast[NET_IF_MAX_IPV4_MADDR];
360 
361 	/** Gateway */
362 	struct in_addr gw;
363 
364 	/** Netmask */
365 	struct in_addr netmask;
366 
367 	/** IPv4 time-to-live */
368 	uint8_t ttl;
369 };
370 
371 #if defined(CONFIG_NET_DHCPV4) && defined(CONFIG_NET_NATIVE_IPV4)
372 struct net_if_dhcpv4 {
373 	/** Used for timer lists */
374 	sys_snode_t node;
375 
376 	/** Timer start */
377 	int64_t timer_start;
378 
379 	/** Time for INIT, DISCOVER, REQUESTING, RENEWAL */
380 	uint32_t request_time;
381 
382 	uint32_t xid;
383 
384 	/** IP address Lease time */
385 	uint32_t lease_time;
386 
387 	/** IP address Renewal time */
388 	uint32_t renewal_time;
389 
390 	/** IP address Rebinding time */
391 	uint32_t rebinding_time;
392 
393 	/** Server ID */
394 	struct in_addr server_id;
395 
396 	/** Requested IP addr */
397 	struct in_addr requested_ip;
398 
399 	/**
400 	 *  DHCPv4 client state in the process of network
401 	 *  address allocation.
402 	 */
403 	enum net_dhcpv4_state state;
404 
405 	/** Number of attempts made for REQUEST and RENEWAL messages */
406 	uint8_t attempts;
407 
408 	/** The address of the server the request is sent to */
409 	struct in_addr request_server_addr;
410 
411 	/** The source address of a received DHCP message */
412 	struct in_addr response_src_addr;
413 };
414 #endif /* CONFIG_NET_DHCPV4 */
415 
416 #if defined(CONFIG_NET_IPV4_AUTO) && defined(CONFIG_NET_NATIVE_IPV4)
417 struct net_if_ipv4_autoconf {
418 	/** Used for timer lists */
419 	sys_snode_t node;
420 
421 	/** Backpointer to correct network interface */
422 	struct net_if *iface;
423 
424 	/** Timer start */
425 	int64_t timer_start;
426 
427 	/** Time for INIT, DISCOVER, REQUESTING, RENEWAL */
428 	uint32_t timer_timeout;
429 
430 	/** Current IP addr */
431 	struct in_addr current_ip;
432 
433 	/** Requested IP addr */
434 	struct in_addr requested_ip;
435 
436 	/** IPV4 Autoconf state in the process of network address allocation.
437 	 */
438 	enum net_ipv4_autoconf_state state;
439 
440 	/** Number of sent probe requests */
441 	uint8_t probe_cnt;
442 
443 	/** Number of sent announcements */
444 	uint8_t announce_cnt;
445 
446 	/** Incoming conflict count */
447 	uint8_t conflict_cnt;
448 };
449 #endif /* CONFIG_NET_IPV4_AUTO */
450 
451 /** @cond INTERNAL_HIDDEN */
452 /* We always need to have at least one IP config */
453 #define NET_IF_MAX_CONFIGS 1
454 /** @endcond */
455 
456 /**
457  * @brief Network interface IP address configuration.
458  */
459 struct net_if_ip {
460 #if defined(CONFIG_NET_NATIVE_IPV6)
461 	struct net_if_ipv6 *ipv6;
462 #endif /* CONFIG_NET_IPV6 */
463 
464 #if defined(CONFIG_NET_NATIVE_IPV4)
465 	struct net_if_ipv4 *ipv4;
466 #endif /* CONFIG_NET_IPV4 */
467 };
468 
469 /**
470  * @brief IP and other configuration related data for network interface.
471  */
472 struct net_if_config {
473 #if defined(CONFIG_NET_IP)
474 	/** IP address configuration setting */
475 	struct net_if_ip ip;
476 #endif
477 
478 #if defined(CONFIG_NET_DHCPV4) && defined(CONFIG_NET_NATIVE_IPV4)
479 	struct net_if_dhcpv4 dhcpv4;
480 #endif /* CONFIG_NET_DHCPV4 */
481 
482 #if defined(CONFIG_NET_DHCPV6) && defined(CONFIG_NET_NATIVE_IPV6)
483 	struct net_if_dhcpv6 dhcpv6;
484 #endif /* CONFIG_NET_DHCPV6 */
485 
486 #if defined(CONFIG_NET_IPV4_AUTO) && defined(CONFIG_NET_NATIVE_IPV4)
487 	struct net_if_ipv4_autoconf ipv4auto;
488 #endif /* CONFIG_NET_IPV4_AUTO */
489 
490 #if defined(CONFIG_NET_L2_VIRTUAL)
491 	/**
492 	 * This list keeps track of the virtual network interfaces
493 	 * that are attached to this network interface.
494 	 */
495 	sys_slist_t virtual_interfaces;
496 #endif /* CONFIG_NET_L2_VIRTUAL */
497 
498 #if defined(CONFIG_NET_INTERFACE_NAME)
499 	/**
500 	 * Network interface can have a name and it is possible
501 	 * to search a network interface using this name.
502 	 */
503 	char name[CONFIG_NET_INTERFACE_NAME_LEN + 1];
504 #endif
505 };
506 
507 /**
508  * @brief Network traffic class.
509  *
510  * Traffic classes are used when sending or receiving data that is classified
511  * with different priorities. So some traffic can be marked as high priority
512  * and it will be sent or received first. Each network packet that is
513  * transmitted or received goes through a fifo to a thread that will transmit
514  * it.
515  */
516 struct net_traffic_class {
517 	/** Fifo for handling this Tx or Rx packet */
518 	struct k_fifo fifo;
519 
520 	/** Traffic class handler thread */
521 	struct k_thread handler;
522 
523 	/** Stack for this handler */
524 	k_thread_stack_t *stack;
525 };
526 
527 /**
528  * @typedef net_socket_create_t
529 
530  * @brief A function prototype to create an offloaded socket. The prototype is
531  *        compatible with socket() function.
532  */
533 typedef int (*net_socket_create_t)(int, int, int);
534 
535 /**
536  * @brief Network Interface Device structure
537  *
538  * Used to handle a network interface on top of a device driver instance.
539  * There can be many net_if_dev instance against the same device.
540  *
541  * Such interface is mainly to be used by the link layer, but is also tight
542  * to a network context: it then makes the relation with a network context
543  * and the network device.
544  *
545  * Because of the strong relationship between a device driver and such
546  * network interface, each net_if_dev should be instantiated by one of the
547  * network device init macros found in net_if.h.
548  */
549 struct net_if_dev {
550 	/** The actually device driver instance the net_if is related to */
551 	const struct device *dev;
552 
553 	/** Interface's L2 layer */
554 	const struct net_l2 * const l2;
555 
556 	/** Interface's private L2 data pointer */
557 	void *l2_data;
558 
559 	/* For internal use */
560 	ATOMIC_DEFINE(flags, NET_IF_NUM_FLAGS);
561 
562 	/** The hardware link address */
563 	struct net_linkaddr link_addr;
564 
565 #if defined(CONFIG_NET_OFFLOAD)
566 	/** TCP/IP Offload functions.
567 	 * If non-NULL, then the TCP/IP stack is located
568 	 * in the communication chip that is accessed via this
569 	 * network interface.
570 	 */
571 	struct net_offload *offload;
572 #endif /* CONFIG_NET_OFFLOAD */
573 
574 	/** The hardware MTU */
575 	uint16_t mtu;
576 
577 #if defined(CONFIG_NET_SOCKETS_OFFLOAD)
578 	/** A function pointer to create an offloaded socket.
579 	 *  If non-NULL, the interface is considered offloaded at socket level.
580 	 */
581 	net_socket_create_t socket_offload;
582 #endif /* CONFIG_NET_SOCKETS_OFFLOAD */
583 
584 	/** RFC 2863 operational status */
585 	enum net_if_oper_state oper_state;
586 };
587 
588 /**
589  * @brief Network Interface structure
590  *
591  * Used to handle a network interface on top of a net_if_dev instance.
592  * There can be many net_if instance against the same net_if_dev instance.
593  *
594  */
595 struct net_if {
596 	/** The net_if_dev instance the net_if is related to */
597 	struct net_if_dev *if_dev;
598 
599 #if defined(CONFIG_NET_STATISTICS_PER_INTERFACE)
600 	/** Network statistics related to this network interface */
601 	struct net_stats stats;
602 #endif /* CONFIG_NET_STATISTICS_PER_INTERFACE */
603 
604 	/** Network interface instance configuration */
605 	struct net_if_config config;
606 
607 #if defined(CONFIG_NET_POWER_MANAGEMENT)
608 	/** Keep track of packets pending in traffic queues. This is
609 	 * needed to avoid putting network device driver to sleep if
610 	 * there are packets waiting to be sent.
611 	 */
612 	int tx_pending;
613 #endif
614 
615 	struct k_mutex lock;
616 };
617 
net_if_lock(struct net_if * iface)618 static inline void net_if_lock(struct net_if *iface)
619 {
620 	NET_ASSERT(iface);
621 
622 	(void)k_mutex_lock(&iface->lock, K_FOREVER);
623 }
624 
net_if_unlock(struct net_if * iface)625 static inline void net_if_unlock(struct net_if *iface)
626 {
627 	NET_ASSERT(iface);
628 
629 	k_mutex_unlock(&iface->lock);
630 }
631 
632 /**
633  * @brief Set a value in network interface flags
634  *
635  * @param iface Pointer to network interface
636  * @param value Flag value
637  */
net_if_flag_set(struct net_if * iface,enum net_if_flag value)638 static inline void net_if_flag_set(struct net_if *iface,
639 				   enum net_if_flag value)
640 {
641 	NET_ASSERT(iface);
642 	NET_ASSERT(iface->if_dev);
643 
644 	atomic_set_bit(iface->if_dev->flags, value);
645 }
646 
647 /**
648  * @brief Test and set a value in network interface flags
649  *
650  * @param iface Pointer to network interface
651  * @param value Flag value
652  *
653  * @return true if the bit was set, false if it wasn't.
654  */
net_if_flag_test_and_set(struct net_if * iface,enum net_if_flag value)655 static inline bool net_if_flag_test_and_set(struct net_if *iface,
656 					    enum net_if_flag value)
657 {
658 	NET_ASSERT(iface);
659 	NET_ASSERT(iface->if_dev);
660 
661 	return atomic_test_and_set_bit(iface->if_dev->flags, value);
662 }
663 
664 /**
665  * @brief Clear a value in network interface flags
666  *
667  * @param iface Pointer to network interface
668  * @param value Flag value
669  */
net_if_flag_clear(struct net_if * iface,enum net_if_flag value)670 static inline void net_if_flag_clear(struct net_if *iface,
671 				     enum net_if_flag value)
672 {
673 	NET_ASSERT(iface);
674 	NET_ASSERT(iface->if_dev);
675 
676 	atomic_clear_bit(iface->if_dev->flags, value);
677 }
678 
679 /**
680  * @brief Test and clear a value in network interface flags
681  *
682  * @param iface Pointer to network interface
683  * @param value Flag value
684  *
685  * @return true if the bit was set, false if it wasn't.
686  */
net_if_flag_test_and_clear(struct net_if * iface,enum net_if_flag value)687 static inline bool net_if_flag_test_and_clear(struct net_if *iface,
688 					      enum net_if_flag value)
689 {
690 	NET_ASSERT(iface);
691 	NET_ASSERT(iface->if_dev);
692 
693 	return atomic_test_and_clear_bit(iface->if_dev->flags, value);
694 }
695 
696 /**
697  * @brief Check if a value in network interface flags is set
698  *
699  * @param iface Pointer to network interface
700  * @param value Flag value
701  *
702  * @return True if the value is set, false otherwise
703  */
net_if_flag_is_set(struct net_if * iface,enum net_if_flag value)704 static inline bool net_if_flag_is_set(struct net_if *iface,
705 				      enum net_if_flag value)
706 {
707 	NET_ASSERT(iface);
708 	NET_ASSERT(iface->if_dev);
709 
710 	if (iface == NULL) {
711 		return false;
712 	}
713 
714 	return atomic_test_bit(iface->if_dev->flags, value);
715 }
716 
717 /**
718  * @brief Set an operational state on an interface
719  *
720  * @param iface Pointer to network interface
721  * @param oper_state Operational state to set
722  *
723  * @return The new operational state of an interface
724  */
net_if_oper_state_set(struct net_if * iface,enum net_if_oper_state oper_state)725 static inline enum net_if_oper_state net_if_oper_state_set(
726 	struct net_if *iface, enum net_if_oper_state oper_state)
727 {
728 	NET_ASSERT(iface);
729 	NET_ASSERT(iface->if_dev);
730 
731 	if (oper_state >= NET_IF_OPER_UNKNOWN && oper_state <= NET_IF_OPER_UP) {
732 		iface->if_dev->oper_state = oper_state;
733 	}
734 
735 	return iface->if_dev->oper_state;
736 }
737 
738 /**
739  * @brief Get an operational state of an interface
740  *
741  * @param iface Pointer to network interface
742  *
743  * @return Operational state of an interface
744  */
net_if_oper_state(struct net_if * iface)745 static inline enum net_if_oper_state net_if_oper_state(struct net_if *iface)
746 {
747 	NET_ASSERT(iface);
748 	NET_ASSERT(iface->if_dev);
749 
750 	return iface->if_dev->oper_state;
751 }
752 
753 /**
754  * @brief Send a packet through a net iface
755  *
756  * @param iface Pointer to a network interface structure
757  * @param pkt Pointer to a net packet to send
758  *
759  * return verdict about the packet
760  */
761 enum net_verdict net_if_send_data(struct net_if *iface, struct net_pkt *pkt);
762 
763 /**
764  * @brief Get a pointer to the interface L2
765  *
766  * @param iface a valid pointer to a network interface structure
767  *
768  * @return a pointer to the iface L2
769  */
net_if_l2(struct net_if * iface)770 static inline const struct net_l2 *net_if_l2(struct net_if *iface)
771 {
772 	if (!iface || !iface->if_dev) {
773 		return NULL;
774 	}
775 
776 	return iface->if_dev->l2;
777 }
778 
779 /**
780  * @brief Input a packet through a net iface
781  *
782  * @param iface Pointer to a network interface structure
783  * @param pkt Pointer to a net packet to input
784  *
785  * @return verdict about the packet
786  */
787 enum net_verdict net_if_recv_data(struct net_if *iface, struct net_pkt *pkt);
788 
789 /**
790  * @brief Get a pointer to the interface L2 private data
791  *
792  * @param iface a valid pointer to a network interface structure
793  *
794  * @return a pointer to the iface L2 data
795  */
net_if_l2_data(struct net_if * iface)796 static inline void *net_if_l2_data(struct net_if *iface)
797 {
798 	NET_ASSERT(iface);
799 	NET_ASSERT(iface->if_dev);
800 
801 	return iface->if_dev->l2_data;
802 }
803 
804 /**
805  * @brief Get an network interface's device
806  *
807  * @param iface Pointer to a network interface structure
808  *
809  * @return a pointer to the device driver instance
810  */
net_if_get_device(struct net_if * iface)811 static inline const struct device *net_if_get_device(struct net_if *iface)
812 {
813 	NET_ASSERT(iface);
814 	NET_ASSERT(iface->if_dev);
815 
816 	return iface->if_dev->dev;
817 }
818 
819 /**
820  * @brief Queue a packet to the net interface TX queue
821  *
822  * @param iface Pointer to a network interface structure
823  * @param pkt Pointer to a net packet to queue
824  */
825 void net_if_queue_tx(struct net_if *iface, struct net_pkt *pkt);
826 
827 /**
828  * @brief Return the IP offload status
829  *
830  * @param iface Network interface
831  *
832  * @return True if IP offloading is active, false otherwise.
833  */
net_if_is_ip_offloaded(struct net_if * iface)834 static inline bool net_if_is_ip_offloaded(struct net_if *iface)
835 {
836 #if defined(CONFIG_NET_OFFLOAD)
837 	return (iface != NULL && iface->if_dev != NULL &&
838 		iface->if_dev->offload != NULL);
839 #else
840 	ARG_UNUSED(iface);
841 
842 	return false;
843 #endif
844 }
845 
846 /**
847  * @brief Return offload status of a given network interface.
848  *
849  * @param iface Network interface
850  *
851  * @return True if IP or socket offloading is active, false otherwise.
852  */
853 bool net_if_is_offloaded(struct net_if *iface);
854 
855 /**
856  * @brief Return the IP offload plugin
857  *
858  * @param iface Network interface
859  *
860  * @return NULL if there is no offload plugin defined, valid pointer otherwise
861  */
net_if_offload(struct net_if * iface)862 static inline struct net_offload *net_if_offload(struct net_if *iface)
863 {
864 #if defined(CONFIG_NET_OFFLOAD)
865 	NET_ASSERT(iface);
866 	NET_ASSERT(iface->if_dev);
867 
868 	return iface->if_dev->offload;
869 #else
870 	ARG_UNUSED(iface);
871 
872 	return NULL;
873 #endif
874 }
875 
876 /**
877  * @brief Return the socket offload status
878  *
879  * @param iface Network interface
880  *
881  * @return True if socket offloading is active, false otherwise.
882  */
net_if_is_socket_offloaded(struct net_if * iface)883 static inline bool net_if_is_socket_offloaded(struct net_if *iface)
884 {
885 #if defined(CONFIG_NET_SOCKETS_OFFLOAD)
886 	NET_ASSERT(iface);
887 	NET_ASSERT(iface->if_dev);
888 
889 	return (iface->if_dev->socket_offload != NULL);
890 #else
891 	ARG_UNUSED(iface);
892 
893 	return false;
894 #endif
895 }
896 
897 /**
898  * @brief Set the function to create an offloaded socket
899  *
900  * @param iface Network interface
901  * @param socket_offload A function to create an offloaded socket
902  */
net_if_socket_offload_set(struct net_if * iface,net_socket_create_t socket_offload)903 static inline void net_if_socket_offload_set(
904 		struct net_if *iface, net_socket_create_t socket_offload)
905 {
906 #if defined(CONFIG_NET_SOCKETS_OFFLOAD)
907 	NET_ASSERT(iface);
908 	NET_ASSERT(iface->if_dev);
909 
910 	iface->if_dev->socket_offload = socket_offload;
911 #else
912 	ARG_UNUSED(iface);
913 	ARG_UNUSED(socket_offload);
914 #endif
915 }
916 
917 /**
918  * @brief Return the function to create an offloaded socket
919  *
920  * @param iface Network interface
921  *
922  * @return NULL if the interface is not socket offloaded, valid pointer otherwise
923  */
net_if_socket_offload(struct net_if * iface)924 static inline net_socket_create_t net_if_socket_offload(struct net_if *iface)
925 {
926 #if defined(CONFIG_NET_SOCKETS_OFFLOAD)
927 	NET_ASSERT(iface);
928 	NET_ASSERT(iface->if_dev);
929 
930 	return iface->if_dev->socket_offload;
931 #else
932 	ARG_UNUSED(iface);
933 
934 	return NULL;
935 #endif
936 }
937 
938 /**
939  * @brief Get an network interface's link address
940  *
941  * @param iface Pointer to a network interface structure
942  *
943  * @return a pointer to the network link address
944  */
net_if_get_link_addr(struct net_if * iface)945 static inline struct net_linkaddr *net_if_get_link_addr(struct net_if *iface)
946 {
947 	NET_ASSERT(iface);
948 	NET_ASSERT(iface->if_dev);
949 
950 	return &iface->if_dev->link_addr;
951 }
952 
953 /**
954  * @brief Return network configuration for this network interface
955  *
956  * @param iface Pointer to a network interface structure
957  *
958  * @return Pointer to configuration
959  */
net_if_get_config(struct net_if * iface)960 static inline struct net_if_config *net_if_get_config(struct net_if *iface)
961 {
962 	NET_ASSERT(iface);
963 
964 	return &iface->config;
965 }
966 
967 /**
968  * @brief Start duplicate address detection procedure.
969  *
970  * @param iface Pointer to a network interface structure
971  */
972 #if defined(CONFIG_NET_IPV6_DAD) && defined(CONFIG_NET_NATIVE_IPV6)
973 void net_if_start_dad(struct net_if *iface);
974 #else
net_if_start_dad(struct net_if * iface)975 static inline void net_if_start_dad(struct net_if *iface)
976 {
977 	ARG_UNUSED(iface);
978 }
979 #endif
980 
981 /**
982  * @brief Start neighbor discovery and send router solicitation message.
983  *
984  * @param iface Pointer to a network interface structure
985  */
986 void net_if_start_rs(struct net_if *iface);
987 
988 
989 /**
990  * @brief Stop neighbor discovery.
991  *
992  * @param iface Pointer to a network interface structure
993  */
994 #if defined(CONFIG_NET_IPV6_ND) && defined(CONFIG_NET_NATIVE_IPV6)
995 void net_if_stop_rs(struct net_if *iface);
996 #else
net_if_stop_rs(struct net_if * iface)997 static inline void net_if_stop_rs(struct net_if *iface)
998 {
999 	ARG_UNUSED(iface);
1000 }
1001 #endif /* CONFIG_NET_IPV6_ND */
1002 
1003 /** @cond INTERNAL_HIDDEN */
1004 
net_if_set_link_addr_unlocked(struct net_if * iface,uint8_t * addr,uint8_t len,enum net_link_type type)1005 static inline int net_if_set_link_addr_unlocked(struct net_if *iface,
1006 						uint8_t *addr, uint8_t len,
1007 						enum net_link_type type)
1008 {
1009 	if (net_if_flag_is_set(iface, NET_IF_RUNNING)) {
1010 		return -EPERM;
1011 	}
1012 
1013 	net_if_get_link_addr(iface)->addr = addr;
1014 	net_if_get_link_addr(iface)->len = len;
1015 	net_if_get_link_addr(iface)->type = type;
1016 
1017 	net_hostname_set_postfix(addr, len);
1018 
1019 	return 0;
1020 }
1021 
1022 int net_if_set_link_addr_locked(struct net_if *iface,
1023 				uint8_t *addr, uint8_t len,
1024 				enum net_link_type type);
1025 /** @endcond */
1026 
1027 /**
1028  * @brief Set a network interface's link address
1029  *
1030  * @param iface Pointer to a network interface structure
1031  * @param addr A pointer to a uint8_t buffer representing the address.
1032  *             The buffer must remain valid throughout interface lifetime.
1033  * @param len length of the address buffer
1034  * @param type network bearer type of this link address
1035  *
1036  * @return 0 on success
1037  */
net_if_set_link_addr(struct net_if * iface,uint8_t * addr,uint8_t len,enum net_link_type type)1038 static inline int net_if_set_link_addr(struct net_if *iface,
1039 				       uint8_t *addr, uint8_t len,
1040 				       enum net_link_type type)
1041 {
1042 #if defined(CONFIG_NET_RAW_MODE)
1043 	return net_if_set_link_addr_unlocked(iface, addr, len, type);
1044 #else
1045 	return net_if_set_link_addr_locked(iface, addr, len, type);
1046 #endif
1047 }
1048 
1049 /**
1050  * @brief Get an network interface's MTU
1051  *
1052  * @param iface Pointer to a network interface structure
1053  *
1054  * @return the MTU
1055  */
net_if_get_mtu(struct net_if * iface)1056 static inline uint16_t net_if_get_mtu(struct net_if *iface)
1057 {
1058 	if (iface == NULL) {
1059 		return 0U;
1060 	}
1061 
1062 	NET_ASSERT(iface->if_dev);
1063 
1064 	return iface->if_dev->mtu;
1065 }
1066 
1067 /**
1068  * @brief Set an network interface's MTU
1069  *
1070  * @param iface Pointer to a network interface structure
1071  * @param mtu New MTU, note that we store only 16 bit mtu value.
1072  */
net_if_set_mtu(struct net_if * iface,uint16_t mtu)1073 static inline void net_if_set_mtu(struct net_if *iface,
1074 				  uint16_t mtu)
1075 {
1076 	if (iface == NULL) {
1077 		return;
1078 	}
1079 
1080 	NET_ASSERT(iface->if_dev);
1081 
1082 	iface->if_dev->mtu = mtu;
1083 }
1084 
1085 /**
1086  * @brief Set the infinite status of the network interface address
1087  *
1088  * @param ifaddr IP address for network interface
1089  * @param is_infinite Infinite status
1090  */
net_if_addr_set_lf(struct net_if_addr * ifaddr,bool is_infinite)1091 static inline void net_if_addr_set_lf(struct net_if_addr *ifaddr,
1092 				      bool is_infinite)
1093 {
1094 	NET_ASSERT(ifaddr);
1095 
1096 	ifaddr->is_infinite = is_infinite;
1097 }
1098 
1099 /**
1100  * @brief Get an interface according to link layer address.
1101  *
1102  * @param ll_addr Link layer address.
1103  *
1104  * @return Network interface or NULL if not found.
1105  */
1106 struct net_if *net_if_get_by_link_addr(struct net_linkaddr *ll_addr);
1107 
1108 /**
1109  * @brief Find an interface from it's related device
1110  *
1111  * @param dev A valid struct device pointer to relate with an interface
1112  *
1113  * @return a valid struct net_if pointer on success, NULL otherwise
1114  */
1115 struct net_if *net_if_lookup_by_dev(const struct device *dev);
1116 
1117 /**
1118  * @brief Get network interface IP config
1119  *
1120  * @param iface Interface to use.
1121  *
1122  * @return NULL if not found or pointer to correct config settings.
1123  */
net_if_config_get(struct net_if * iface)1124 static inline struct net_if_config *net_if_config_get(struct net_if *iface)
1125 {
1126 	NET_ASSERT(iface);
1127 
1128 	return &iface->config;
1129 }
1130 
1131 /**
1132  * @brief Remove a router from the system
1133  *
1134  * @param router Pointer to existing router
1135  */
1136 void net_if_router_rm(struct net_if_router *router);
1137 
1138 /**
1139  * @brief Set the default network interface.
1140  *
1141  * @param iface New default interface, or NULL to revert to the one set by Kconfig.
1142  */
1143 void net_if_set_default(struct net_if *iface);
1144 
1145 /**
1146  * @brief Get the default network interface.
1147  *
1148  * @return Default interface or NULL if no interfaces are configured.
1149  */
1150 struct net_if *net_if_get_default(void);
1151 
1152 /**
1153  * @brief Get the first network interface according to its type.
1154  *
1155  * @param l2 Layer 2 type of the network interface.
1156  *
1157  * @return First network interface of a given type or NULL if no such
1158  * interfaces was found.
1159  */
1160 struct net_if *net_if_get_first_by_type(const struct net_l2 *l2);
1161 
1162 /**
1163  * @brief Get the first network interface which is up.
1164  *
1165  * @return First network interface which is up or NULL if all
1166  * interfaces are down.
1167  */
1168 struct net_if *net_if_get_first_up(void);
1169 
1170 #if defined(CONFIG_NET_L2_IEEE802154)
1171 /**
1172  * @brief Get the first IEEE 802.15.4 network interface.
1173  *
1174  * @return First IEEE 802.15.4 network interface or NULL if no such
1175  * interfaces was found.
1176  */
net_if_get_ieee802154(void)1177 static inline struct net_if *net_if_get_ieee802154(void)
1178 {
1179 	return net_if_get_first_by_type(&NET_L2_GET_NAME(IEEE802154));
1180 }
1181 #endif /* CONFIG_NET_L2_IEEE802154 */
1182 
1183 /**
1184  * @brief Allocate network interface IPv6 config.
1185  *
1186  * @details This function will allocate new IPv6 config.
1187  *
1188  * @param iface Interface to use.
1189  * @param ipv6 Pointer to allocated IPv6 struct is returned to caller.
1190  *
1191  * @return 0 if ok, <0 if error
1192  */
1193 int net_if_config_ipv6_get(struct net_if *iface,
1194 			   struct net_if_ipv6 **ipv6);
1195 
1196 /**
1197  * @brief Release network interface IPv6 config.
1198  *
1199  * @param iface Interface to use.
1200  *
1201  * @return 0 if ok, <0 if error
1202  */
1203 int net_if_config_ipv6_put(struct net_if *iface);
1204 
1205 /**
1206  * @brief Check if this IPv6 address belongs to one of the interfaces.
1207  *
1208  * @param addr IPv6 address
1209  * @param iface Pointer to interface is returned
1210  *
1211  * @return Pointer to interface address, NULL if not found.
1212  */
1213 struct net_if_addr *net_if_ipv6_addr_lookup(const struct in6_addr *addr,
1214 					    struct net_if **iface);
1215 
1216 /**
1217  * @brief Check if this IPv6 address belongs to this specific interfaces.
1218  *
1219  * @param iface Network interface
1220  * @param addr IPv6 address
1221  *
1222  * @return Pointer to interface address, NULL if not found.
1223  */
1224 struct net_if_addr *net_if_ipv6_addr_lookup_by_iface(struct net_if *iface,
1225 						     struct in6_addr *addr);
1226 
1227 /**
1228  * @brief Check if this IPv6 address belongs to one of the interface indices.
1229  *
1230  * @param addr IPv6 address
1231  *
1232  * @return >0 if address was found in given network interface index,
1233  * all other values mean address was not found
1234  */
1235 __syscall int net_if_ipv6_addr_lookup_by_index(const struct in6_addr *addr);
1236 
1237 /**
1238  * @brief Add a IPv6 address to an interface
1239  *
1240  * @param iface Network interface
1241  * @param addr IPv6 address
1242  * @param addr_type IPv6 address type
1243  * @param vlifetime Validity time for this address
1244  *
1245  * @return Pointer to interface address, NULL if cannot be added
1246  */
1247 struct net_if_addr *net_if_ipv6_addr_add(struct net_if *iface,
1248 					 struct in6_addr *addr,
1249 					 enum net_addr_type addr_type,
1250 					 uint32_t vlifetime);
1251 
1252 /**
1253  * @brief Add a IPv6 address to an interface by index
1254  *
1255  * @param index Network interface index
1256  * @param addr IPv6 address
1257  * @param addr_type IPv6 address type
1258  * @param vlifetime Validity time for this address
1259  *
1260  * @return True if ok, false if address could not be added
1261  */
1262 __syscall bool net_if_ipv6_addr_add_by_index(int index,
1263 					     struct in6_addr *addr,
1264 					     enum net_addr_type addr_type,
1265 					     uint32_t vlifetime);
1266 
1267 /**
1268  * @brief Update validity lifetime time of an IPv6 address.
1269  *
1270  * @param ifaddr Network IPv6 address
1271  * @param vlifetime Validity time for this address
1272  */
1273 void net_if_ipv6_addr_update_lifetime(struct net_if_addr *ifaddr,
1274 				      uint32_t vlifetime);
1275 
1276 /**
1277  * @brief Remove an IPv6 address from an interface
1278  *
1279  * @param iface Network interface
1280  * @param addr IPv6 address
1281  *
1282  * @return True if successfully removed, false otherwise
1283  */
1284 bool net_if_ipv6_addr_rm(struct net_if *iface, const struct in6_addr *addr);
1285 
1286 /**
1287  * @brief Remove an IPv6 address from an interface by index
1288  *
1289  * @param index Network interface index
1290  * @param addr IPv6 address
1291  *
1292  * @return True if successfully removed, false otherwise
1293  */
1294 __syscall bool net_if_ipv6_addr_rm_by_index(int index,
1295 					    const struct in6_addr *addr);
1296 
1297 /**
1298  * @typedef net_if_ip_addr_cb_t
1299  * @brief Callback used while iterating over network interface IP addresses
1300  *
1301  * @param iface Pointer to the network interface the address belongs to
1302  * @param addr Pointer to current IP address
1303  * @param user_data A valid pointer to user data or NULL
1304  */
1305 typedef void (*net_if_ip_addr_cb_t)(struct net_if *iface,
1306 				    struct net_if_addr *addr,
1307 				    void *user_data);
1308 
1309 /**
1310  * @brief Go through all IPv6 addresses on a network interface and call callback
1311  * for each used address.
1312  *
1313  * @param iface Pointer to the network interface
1314  * @param cb User-supplied callback function to call
1315  * @param user_data User specified data
1316  */
1317 void net_if_ipv6_addr_foreach(struct net_if *iface, net_if_ip_addr_cb_t cb,
1318 			      void *user_data);
1319 
1320 /**
1321  * @brief Add a IPv6 multicast address to an interface
1322  *
1323  * @param iface Network interface
1324  * @param addr IPv6 multicast address
1325  *
1326  * @return Pointer to interface multicast address, NULL if cannot be added
1327  */
1328 struct net_if_mcast_addr *net_if_ipv6_maddr_add(struct net_if *iface,
1329 						const struct in6_addr *addr);
1330 
1331 /**
1332  * @brief Remove an IPv6 multicast address from an interface
1333  *
1334  * @param iface Network interface
1335  * @param addr IPv6 multicast address
1336  *
1337  * @return True if successfully removed, false otherwise
1338  */
1339 bool net_if_ipv6_maddr_rm(struct net_if *iface, const struct in6_addr *addr);
1340 
1341 /**
1342  * @brief Check if this IPv6 multicast address belongs to a specific interface
1343  * or one of the interfaces.
1344  *
1345  * @param addr IPv6 address
1346  * @param iface If *iface is null, then pointer to interface is returned,
1347  * otherwise the *iface value needs to be matched.
1348  *
1349  * @return Pointer to interface multicast address, NULL if not found.
1350  */
1351 struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(const struct in6_addr *addr,
1352 						   struct net_if **iface);
1353 
1354 /**
1355  * @typedef net_if_mcast_callback_t
1356 
1357  * @brief Define callback that is called whenever IPv6 multicast address group
1358  * is joined or left.
1359 
1360  * @param iface A pointer to a struct net_if to which the multicast address is
1361  *        attached.
1362  * @param addr IP multicast address.
1363  * @param is_joined True if the address is joined, false if left.
1364  */
1365 typedef void (*net_if_mcast_callback_t)(struct net_if *iface,
1366 					const struct net_addr *addr,
1367 					bool is_joined);
1368 
1369 /**
1370  * @brief Multicast monitor handler struct.
1371  *
1372  * Stores the multicast callback information. Caller must make sure that
1373  * the variable pointed by this is valid during the lifetime of
1374  * registration. Typically this means that the variable cannot be
1375  * allocated from stack.
1376  */
1377 struct net_if_mcast_monitor {
1378 	/** Node information for the slist. */
1379 	sys_snode_t node;
1380 
1381 	/** Network interface */
1382 	struct net_if *iface;
1383 
1384 	/** Multicast callback */
1385 	net_if_mcast_callback_t cb;
1386 };
1387 
1388 /**
1389  * @brief Register a multicast monitor
1390  *
1391  * @param mon Monitor handle. This is a pointer to a monitor storage structure
1392  * which should be allocated by caller, but does not need to be initialized.
1393  * @param iface Network interface
1394  * @param cb Monitor callback
1395  */
1396 void net_if_mcast_mon_register(struct net_if_mcast_monitor *mon,
1397 			       struct net_if *iface,
1398 			       net_if_mcast_callback_t cb);
1399 
1400 /**
1401  * @brief Unregister a multicast monitor
1402  *
1403  * @param mon Monitor handle
1404  */
1405 void net_if_mcast_mon_unregister(struct net_if_mcast_monitor *mon);
1406 
1407 /**
1408  * @brief Call registered multicast monitors
1409  *
1410  * @param iface Network interface
1411  * @param addr Multicast address
1412  * @param is_joined Is this multicast address joined (true) or not (false)
1413  */
1414 void net_if_mcast_monitor(struct net_if *iface, const struct net_addr *addr,
1415 			  bool is_joined);
1416 
1417 /**
1418  * @brief Mark a given multicast address to be joined.
1419  *
1420  * @param iface Network interface the address belongs to
1421  * @param addr IPv6 multicast address
1422  */
1423 void net_if_ipv6_maddr_join(struct net_if *iface,
1424 			    struct net_if_mcast_addr *addr);
1425 
1426 /**
1427  * @brief Check if given multicast address is joined or not.
1428  *
1429  * @param addr IPv6 multicast address
1430  *
1431  * @return True if address is joined, False otherwise.
1432  */
net_if_ipv6_maddr_is_joined(struct net_if_mcast_addr * addr)1433 static inline bool net_if_ipv6_maddr_is_joined(struct net_if_mcast_addr *addr)
1434 {
1435 	NET_ASSERT(addr);
1436 
1437 	return addr->is_joined;
1438 }
1439 
1440 /**
1441  * @brief Mark a given multicast address to be left.
1442  *
1443  * @param iface Network interface the address belongs to
1444  * @param addr IPv6 multicast address
1445  */
1446 void net_if_ipv6_maddr_leave(struct net_if *iface,
1447 			     struct net_if_mcast_addr *addr);
1448 
1449 /**
1450  * @brief Return prefix that corresponds to this IPv6 address.
1451  *
1452  * @param iface Network interface
1453  * @param addr IPv6 address
1454  *
1455  * @return Pointer to prefix, NULL if not found.
1456  */
1457 struct net_if_ipv6_prefix *net_if_ipv6_prefix_get(struct net_if *iface,
1458 						  struct in6_addr *addr);
1459 
1460 /**
1461  * @brief Check if this IPv6 prefix belongs to this interface
1462  *
1463  * @param iface Network interface
1464  * @param addr IPv6 address
1465  * @param len Prefix length
1466  *
1467  * @return Pointer to prefix, NULL if not found.
1468  */
1469 struct net_if_ipv6_prefix *net_if_ipv6_prefix_lookup(struct net_if *iface,
1470 						     struct in6_addr *addr,
1471 						     uint8_t len);
1472 
1473 /**
1474  * @brief Add a IPv6 prefix to an network interface.
1475  *
1476  * @param iface Network interface
1477  * @param prefix IPv6 address
1478  * @param len Prefix length
1479  * @param lifetime Prefix lifetime in seconds
1480  *
1481  * @return Pointer to prefix, NULL if the prefix was not added.
1482  */
1483 struct net_if_ipv6_prefix *net_if_ipv6_prefix_add(struct net_if *iface,
1484 						  struct in6_addr *prefix,
1485 						  uint8_t len,
1486 						  uint32_t lifetime);
1487 
1488 /**
1489  * @brief Remove an IPv6 prefix from an interface
1490  *
1491  * @param iface Network interface
1492  * @param addr IPv6 prefix address
1493  * @param len Prefix length
1494  *
1495  * @return True if successfully removed, false otherwise
1496  */
1497 bool net_if_ipv6_prefix_rm(struct net_if *iface, struct in6_addr *addr,
1498 			   uint8_t len);
1499 
1500 /**
1501  * @brief Set the infinite status of the prefix
1502  *
1503  * @param prefix IPv6 address
1504  * @param is_infinite Infinite status
1505  */
net_if_ipv6_prefix_set_lf(struct net_if_ipv6_prefix * prefix,bool is_infinite)1506 static inline void net_if_ipv6_prefix_set_lf(struct net_if_ipv6_prefix *prefix,
1507 					     bool is_infinite)
1508 {
1509 	prefix->is_infinite = is_infinite;
1510 }
1511 
1512 /**
1513  * @brief Set the prefix lifetime timer.
1514  *
1515  * @param prefix IPv6 address
1516  * @param lifetime Prefix lifetime in seconds
1517  */
1518 void net_if_ipv6_prefix_set_timer(struct net_if_ipv6_prefix *prefix,
1519 				  uint32_t lifetime);
1520 
1521 /**
1522  * @brief Unset the prefix lifetime timer.
1523  *
1524  * @param prefix IPv6 address
1525  */
1526 void net_if_ipv6_prefix_unset_timer(struct net_if_ipv6_prefix *prefix);
1527 
1528 /**
1529  * @brief Check if this IPv6 address is part of the subnet of our
1530  * network interface.
1531  *
1532  * @param iface Network interface. This is returned to the caller.
1533  * The iface can be NULL in which case we check all the interfaces.
1534  * @param addr IPv6 address
1535  *
1536  * @return True if address is part of our subnet, false otherwise
1537  */
1538 bool net_if_ipv6_addr_onlink(struct net_if **iface, struct in6_addr *addr);
1539 
1540 /**
1541  * @brief Get the IPv6 address of the given router
1542  * @param router a network router
1543  *
1544  * @return pointer to the IPv6 address, or NULL if none
1545  */
1546 #if defined(CONFIG_NET_NATIVE_IPV6)
net_if_router_ipv6(struct net_if_router * router)1547 static inline struct in6_addr *net_if_router_ipv6(struct net_if_router *router)
1548 {
1549 	NET_ASSERT(router);
1550 
1551 	return &router->address.in6_addr;
1552 }
1553 #else
net_if_router_ipv6(struct net_if_router * router)1554 static inline struct in6_addr *net_if_router_ipv6(struct net_if_router *router)
1555 {
1556 	static struct in6_addr addr;
1557 
1558 	ARG_UNUSED(router);
1559 
1560 	return &addr;
1561 }
1562 #endif
1563 
1564 /**
1565  * @brief Check if IPv6 address is one of the routers configured
1566  * in the system.
1567  *
1568  * @param iface Network interface
1569  * @param addr IPv6 address
1570  *
1571  * @return Pointer to router information, NULL if cannot be found
1572  */
1573 struct net_if_router *net_if_ipv6_router_lookup(struct net_if *iface,
1574 						struct in6_addr *addr);
1575 
1576 /**
1577  * @brief Find default router for this IPv6 address.
1578  *
1579  * @param iface Network interface. This can be NULL in which case we
1580  * go through all the network interfaces to find a suitable router.
1581  * @param addr IPv6 address
1582  *
1583  * @return Pointer to router information, NULL if cannot be found
1584  */
1585 struct net_if_router *net_if_ipv6_router_find_default(struct net_if *iface,
1586 						      struct in6_addr *addr);
1587 
1588 /**
1589  * @brief Update validity lifetime time of a router.
1590  *
1591  * @param router Network IPv6 address
1592  * @param lifetime Lifetime of this router.
1593  */
1594 void net_if_ipv6_router_update_lifetime(struct net_if_router *router,
1595 					uint16_t lifetime);
1596 
1597 /**
1598  * @brief Add IPv6 router to the system.
1599  *
1600  * @param iface Network interface
1601  * @param addr IPv6 address
1602  * @param router_lifetime Lifetime of the router
1603  *
1604  * @return Pointer to router information, NULL if could not be added
1605  */
1606 struct net_if_router *net_if_ipv6_router_add(struct net_if *iface,
1607 					     struct in6_addr *addr,
1608 					     uint16_t router_lifetime);
1609 
1610 /**
1611  * @brief Remove IPv6 router from the system.
1612  *
1613  * @param router Router information.
1614  *
1615  * @return True if successfully removed, false otherwise
1616  */
1617 bool net_if_ipv6_router_rm(struct net_if_router *router);
1618 
1619 /**
1620  * @brief Get IPv6 hop limit specified for a given interface. This is the
1621  * default value but can be overridden by the user.
1622  *
1623  * @param iface Network interface
1624  *
1625  * @return Hop limit
1626  */
1627 uint8_t net_if_ipv6_get_hop_limit(struct net_if *iface);
1628 
1629 /**
1630  * @brief Set the default IPv6 hop limit of a given interface.
1631  *
1632  * @param iface Network interface
1633  * @param hop_limit New hop limit
1634  */
1635 void net_ipv6_set_hop_limit(struct net_if *iface, uint8_t hop_limit);
1636 
1637 /**
1638  * @brief Set IPv6 reachable time for a given interface
1639  *
1640  * @param iface Network interface
1641  * @param reachable_time New reachable time
1642  */
net_if_ipv6_set_base_reachable_time(struct net_if * iface,uint32_t reachable_time)1643 static inline void net_if_ipv6_set_base_reachable_time(struct net_if *iface,
1644 						       uint32_t reachable_time)
1645 {
1646 #if defined(CONFIG_NET_NATIVE_IPV6)
1647 	NET_ASSERT(iface);
1648 
1649 	if (!iface->config.ip.ipv6) {
1650 		return;
1651 	}
1652 
1653 	iface->config.ip.ipv6->base_reachable_time = reachable_time;
1654 #endif
1655 }
1656 
1657 /**
1658  * @brief Get IPv6 reachable timeout specified for a given interface
1659  *
1660  * @param iface Network interface
1661  *
1662  * @return Reachable timeout
1663  */
net_if_ipv6_get_reachable_time(struct net_if * iface)1664 static inline uint32_t net_if_ipv6_get_reachable_time(struct net_if *iface)
1665 {
1666 #if defined(CONFIG_NET_NATIVE_IPV6)
1667 	NET_ASSERT(iface);
1668 
1669 	if (!iface->config.ip.ipv6) {
1670 		return 0;
1671 	}
1672 
1673 	return iface->config.ip.ipv6->reachable_time;
1674 #else
1675 	return 0;
1676 #endif
1677 }
1678 
1679 /**
1680  * @brief Calculate next reachable time value for IPv6 reachable time
1681  *
1682  * @param ipv6 IPv6 address configuration
1683  *
1684  * @return Reachable time
1685  */
1686 uint32_t net_if_ipv6_calc_reachable_time(struct net_if_ipv6 *ipv6);
1687 
1688 /**
1689  * @brief Set IPv6 reachable time for a given interface. This requires
1690  * that base reachable time is set for the interface.
1691  *
1692  * @param ipv6 IPv6 address configuration
1693  */
net_if_ipv6_set_reachable_time(struct net_if_ipv6 * ipv6)1694 static inline void net_if_ipv6_set_reachable_time(struct net_if_ipv6 *ipv6)
1695 {
1696 #if defined(CONFIG_NET_NATIVE_IPV6)
1697 	if (ipv6 == NULL) {
1698 		return;
1699 	}
1700 
1701 	ipv6->reachable_time = net_if_ipv6_calc_reachable_time(ipv6);
1702 #endif
1703 }
1704 
1705 /**
1706  * @brief Set IPv6 retransmit timer for a given interface
1707  *
1708  * @param iface Network interface
1709  * @param retrans_timer New retransmit timer
1710  */
net_if_ipv6_set_retrans_timer(struct net_if * iface,uint32_t retrans_timer)1711 static inline void net_if_ipv6_set_retrans_timer(struct net_if *iface,
1712 						 uint32_t retrans_timer)
1713 {
1714 #if defined(CONFIG_NET_NATIVE_IPV6)
1715 	NET_ASSERT(iface);
1716 
1717 	if (!iface->config.ip.ipv6) {
1718 		return;
1719 	}
1720 
1721 	iface->config.ip.ipv6->retrans_timer = retrans_timer;
1722 #endif
1723 }
1724 
1725 /**
1726  * @brief Get IPv6 retransmit timer specified for a given interface
1727  *
1728  * @param iface Network interface
1729  *
1730  * @return Retransmit timer
1731  */
net_if_ipv6_get_retrans_timer(struct net_if * iface)1732 static inline uint32_t net_if_ipv6_get_retrans_timer(struct net_if *iface)
1733 {
1734 #if defined(CONFIG_NET_NATIVE_IPV6)
1735 	NET_ASSERT(iface);
1736 
1737 	if (!iface->config.ip.ipv6) {
1738 		return 0;
1739 	}
1740 
1741 	return iface->config.ip.ipv6->retrans_timer;
1742 #else
1743 	return 0;
1744 #endif
1745 }
1746 
1747 /**
1748  * @brief Get a IPv6 source address that should be used when sending
1749  * network data to destination.
1750  *
1751  * @param iface Interface that was used when packet was received.
1752  * If the interface is not known, then NULL can be given.
1753  * @param dst IPv6 destination address
1754  *
1755  * @return Pointer to IPv6 address to use, NULL if no IPv6 address
1756  * could be found.
1757  */
1758 #if defined(CONFIG_NET_NATIVE_IPV6)
1759 const struct in6_addr *net_if_ipv6_select_src_addr(struct net_if *iface,
1760 						   const struct in6_addr *dst);
1761 #else
net_if_ipv6_select_src_addr(struct net_if * iface,const struct in6_addr * dst)1762 static inline const struct in6_addr *net_if_ipv6_select_src_addr(
1763 	struct net_if *iface, const struct in6_addr *dst)
1764 {
1765 	ARG_UNUSED(iface);
1766 	ARG_UNUSED(dst);
1767 
1768 	return NULL;
1769 }
1770 #endif
1771 
1772 /**
1773  * @brief Get a network interface that should be used when sending
1774  * IPv6 network data to destination.
1775  *
1776  * @param dst IPv6 destination address
1777  *
1778  * @return Pointer to network interface to use, NULL if no suitable interface
1779  * could be found.
1780  */
1781 #if defined(CONFIG_NET_NATIVE_IPV6)
1782 struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst);
1783 #else
net_if_ipv6_select_src_iface(const struct in6_addr * dst)1784 static inline struct net_if *net_if_ipv6_select_src_iface(
1785 	const struct in6_addr *dst)
1786 {
1787 	ARG_UNUSED(dst);
1788 
1789 	return NULL;
1790 }
1791 #endif
1792 
1793 /**
1794  * @brief Get a IPv6 link local address in a given state.
1795  *
1796  * @param iface Interface to use. Must be a valid pointer to an interface.
1797  * @param addr_state IPv6 address state (preferred, tentative, deprecated)
1798  *
1799  * @return Pointer to link local IPv6 address, NULL if no proper IPv6 address
1800  * could be found.
1801  */
1802 struct in6_addr *net_if_ipv6_get_ll(struct net_if *iface,
1803 				    enum net_addr_state addr_state);
1804 
1805 /**
1806  * @brief Return link local IPv6 address from the first interface that has
1807  * a link local address matching give state.
1808  *
1809  * @param state IPv6 address state (ANY, TENTATIVE, PREFERRED, DEPRECATED)
1810  * @param iface Pointer to interface is returned
1811  *
1812  * @return Pointer to IPv6 address, NULL if not found.
1813  */
1814 struct in6_addr *net_if_ipv6_get_ll_addr(enum net_addr_state state,
1815 					 struct net_if **iface);
1816 
1817 /**
1818  * @brief Stop IPv6 Duplicate Address Detection (DAD) procedure if
1819  * we find out that our IPv6 address is already in use.
1820  *
1821  * @param iface Interface where the DAD was running.
1822  * @param addr IPv6 address that failed DAD
1823  */
1824 void net_if_ipv6_dad_failed(struct net_if *iface, const struct in6_addr *addr);
1825 
1826 /**
1827  * @brief Return global IPv6 address from the first interface that has
1828  * a global IPv6 address matching the given state.
1829  *
1830  * @param state IPv6 address state (ANY, TENTATIVE, PREFERRED, DEPRECATED)
1831  * @param iface Caller can give an interface to check. If iface is set to NULL,
1832  * then all the interfaces are checked. Pointer to interface where the IPv6
1833  * address is defined is returned to the caller.
1834  *
1835  * @return Pointer to IPv6 address, NULL if not found.
1836  */
1837 struct in6_addr *net_if_ipv6_get_global_addr(enum net_addr_state state,
1838 					     struct net_if **iface);
1839 
1840 /**
1841  * @brief Allocate network interface IPv4 config.
1842  *
1843  * @details This function will allocate new IPv4 config.
1844  *
1845  * @param iface Interface to use.
1846  * @param ipv4 Pointer to allocated IPv4 struct is returned to caller.
1847  *
1848  * @return 0 if ok, <0 if error
1849  */
1850 int net_if_config_ipv4_get(struct net_if *iface,
1851 			   struct net_if_ipv4 **ipv4);
1852 
1853 /**
1854  * @brief Release network interface IPv4 config.
1855  *
1856  * @param iface Interface to use.
1857  *
1858  * @return 0 if ok, <0 if error
1859  */
1860 int net_if_config_ipv4_put(struct net_if *iface);
1861 
1862 /**
1863  * @brief Get IPv4 time-to-live value specified for a given interface
1864  *
1865  * @param iface Network interface
1866  *
1867  * @return Time-to-live
1868  */
1869 uint8_t net_if_ipv4_get_ttl(struct net_if *iface);
1870 
1871 /**
1872  * @brief Set IPv4 time-to-live value specified to a given interface
1873  *
1874  * @param iface Network interface
1875  * @param ttl Time-to-live value
1876  */
1877 void net_if_ipv4_set_ttl(struct net_if *iface, uint8_t ttl);
1878 
1879 /**
1880  * @brief Check if this IPv4 address belongs to one of the interfaces.
1881  *
1882  * @param addr IPv4 address
1883  * @param iface Interface is returned
1884  *
1885  * @return Pointer to interface address, NULL if not found.
1886  */
1887 struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr,
1888 					    struct net_if **iface);
1889 
1890 /**
1891  * @brief Add a IPv4 address to an interface
1892  *
1893  * @param iface Network interface
1894  * @param addr IPv4 address
1895  * @param addr_type IPv4 address type
1896  * @param vlifetime Validity time for this address
1897  *
1898  * @return Pointer to interface address, NULL if cannot be added
1899  */
1900 struct net_if_addr *net_if_ipv4_addr_add(struct net_if *iface,
1901 					 struct in_addr *addr,
1902 					 enum net_addr_type addr_type,
1903 					 uint32_t vlifetime);
1904 
1905 /**
1906  * @brief Remove a IPv4 address from an interface
1907  *
1908  * @param iface Network interface
1909  * @param addr IPv4 address
1910  *
1911  * @return True if successfully removed, false otherwise
1912  */
1913 bool net_if_ipv4_addr_rm(struct net_if *iface, const struct in_addr *addr);
1914 
1915 /**
1916  * @brief Check if this IPv4 address belongs to one of the interface indices.
1917  *
1918  * @param addr IPv4 address
1919  *
1920  * @return >0 if address was found in given network interface index,
1921  * all other values mean address was not found
1922  */
1923 __syscall int net_if_ipv4_addr_lookup_by_index(const struct in_addr *addr);
1924 
1925 /**
1926  * @brief Add a IPv4 address to an interface by network interface index
1927  *
1928  * @param index Network interface index
1929  * @param addr IPv4 address
1930  * @param addr_type IPv4 address type
1931  * @param vlifetime Validity time for this address
1932  *
1933  * @return True if ok, false if the address could not be added
1934  */
1935 __syscall bool net_if_ipv4_addr_add_by_index(int index,
1936 					     struct in_addr *addr,
1937 					     enum net_addr_type addr_type,
1938 					     uint32_t vlifetime);
1939 
1940 /**
1941  * @brief Remove a IPv4 address from an interface by interface index
1942  *
1943  * @param index Network interface index
1944  * @param addr IPv4 address
1945  *
1946  * @return True if successfully removed, false otherwise
1947  */
1948 __syscall bool net_if_ipv4_addr_rm_by_index(int index,
1949 					    const struct in_addr *addr);
1950 
1951 /**
1952  * @brief Go through all IPv4 addresses on a network interface and call callback
1953  * for each used address.
1954  *
1955  * @param iface Pointer to the network interface
1956  * @param cb User-supplied callback function to call
1957  * @param user_data User specified data
1958  */
1959 void net_if_ipv4_addr_foreach(struct net_if *iface, net_if_ip_addr_cb_t cb,
1960 			      void *user_data);
1961 
1962 /**
1963  * @brief Add a IPv4 multicast address to an interface
1964  *
1965  * @param iface Network interface
1966  * @param addr IPv4 multicast address
1967  *
1968  * @return Pointer to interface multicast address, NULL if cannot be added
1969  */
1970 struct net_if_mcast_addr *net_if_ipv4_maddr_add(struct net_if *iface,
1971 						const struct in_addr *addr);
1972 
1973 /**
1974  * @brief Remove an IPv4 multicast address from an interface
1975  *
1976  * @param iface Network interface
1977  * @param addr IPv4 multicast address
1978  *
1979  * @return True if successfully removed, false otherwise
1980  */
1981 bool net_if_ipv4_maddr_rm(struct net_if *iface, const struct in_addr *addr);
1982 
1983 /**
1984  * @brief Check if this IPv4 multicast address belongs to a specific interface
1985  * or one of the interfaces.
1986  *
1987  * @param addr IPv4 address
1988  * @param iface If *iface is null, then pointer to interface is returned,
1989  * otherwise the *iface value needs to be matched.
1990  *
1991  * @return Pointer to interface multicast address, NULL if not found.
1992  */
1993 struct net_if_mcast_addr *net_if_ipv4_maddr_lookup(const struct in_addr *addr,
1994 						   struct net_if **iface);
1995 
1996 /**
1997  * @brief Mark a given multicast address to be joined.
1998  *
1999  * @param iface Network interface the address belongs to
2000  * @param addr IPv4 multicast address
2001  */
2002 void net_if_ipv4_maddr_join(struct net_if *iface,
2003 			    struct net_if_mcast_addr *addr);
2004 
2005 /**
2006  * @brief Check if given multicast address is joined or not.
2007  *
2008  * @param addr IPv4 multicast address
2009  *
2010  * @return True if address is joined, False otherwise.
2011  */
net_if_ipv4_maddr_is_joined(struct net_if_mcast_addr * addr)2012 static inline bool net_if_ipv4_maddr_is_joined(struct net_if_mcast_addr *addr)
2013 {
2014 	NET_ASSERT(addr);
2015 
2016 	return addr->is_joined;
2017 }
2018 
2019 /**
2020  * @brief Mark a given multicast address to be left.
2021  *
2022  * @param iface Network interface the address belongs to
2023  * @param addr IPv4 multicast address
2024  */
2025 void net_if_ipv4_maddr_leave(struct net_if *iface,
2026 			     struct net_if_mcast_addr *addr);
2027 
2028 /**
2029  * @brief Get the IPv4 address of the given router
2030  * @param router a network router
2031  *
2032  * @return pointer to the IPv4 address, or NULL if none
2033  */
2034 #if defined(CONFIG_NET_NATIVE_IPV4)
net_if_router_ipv4(struct net_if_router * router)2035 static inline struct in_addr *net_if_router_ipv4(struct net_if_router *router)
2036 {
2037 	NET_ASSERT(router);
2038 
2039 	return &router->address.in_addr;
2040 }
2041 #else
net_if_router_ipv4(struct net_if_router * router)2042 static inline struct in_addr *net_if_router_ipv4(struct net_if_router *router)
2043 {
2044 	static struct in_addr addr;
2045 
2046 	ARG_UNUSED(router);
2047 
2048 	return &addr;
2049 }
2050 #endif
2051 
2052 /**
2053  * @brief Check if IPv4 address is one of the routers configured
2054  * in the system.
2055  *
2056  * @param iface Network interface
2057  * @param addr IPv4 address
2058  *
2059  * @return Pointer to router information, NULL if cannot be found
2060  */
2061 struct net_if_router *net_if_ipv4_router_lookup(struct net_if *iface,
2062 						struct in_addr *addr);
2063 
2064 /**
2065  * @brief Find default router for this IPv4 address.
2066  *
2067  * @param iface Network interface. This can be NULL in which case we
2068  * go through all the network interfaces to find a suitable router.
2069  * @param addr IPv4 address
2070  *
2071  * @return Pointer to router information, NULL if cannot be found
2072  */
2073 struct net_if_router *net_if_ipv4_router_find_default(struct net_if *iface,
2074 						      struct in_addr *addr);
2075 /**
2076  * @brief Add IPv4 router to the system.
2077  *
2078  * @param iface Network interface
2079  * @param addr IPv4 address
2080  * @param is_default Is this router the default one
2081  * @param router_lifetime Lifetime of the router
2082  *
2083  * @return Pointer to router information, NULL if could not be added
2084  */
2085 struct net_if_router *net_if_ipv4_router_add(struct net_if *iface,
2086 					     struct in_addr *addr,
2087 					     bool is_default,
2088 					     uint16_t router_lifetime);
2089 
2090 /**
2091  * @brief Remove IPv4 router from the system.
2092  *
2093  * @param router Router information.
2094  *
2095  * @return True if successfully removed, false otherwise
2096  */
2097 bool net_if_ipv4_router_rm(struct net_if_router *router);
2098 
2099 /**
2100  * @brief Check if the given IPv4 address belongs to local subnet.
2101  *
2102  * @param iface Interface to use. Must be a valid pointer to an interface.
2103  * @param addr IPv4 address
2104  *
2105  * @return True if address is part of local subnet, false otherwise.
2106  */
2107 bool net_if_ipv4_addr_mask_cmp(struct net_if *iface,
2108 			       const struct in_addr *addr);
2109 
2110 /**
2111  * @brief Check if the given IPv4 address is a broadcast address.
2112  *
2113  * @param iface Interface to use. Must be a valid pointer to an interface.
2114  * @param addr IPv4 address, this should be in network byte order
2115  *
2116  * @return True if address is a broadcast address, false otherwise.
2117  */
2118 bool net_if_ipv4_is_addr_bcast(struct net_if *iface,
2119 			       const struct in_addr *addr);
2120 
2121 /**
2122  * @brief Get a network interface that should be used when sending
2123  * IPv4 network data to destination.
2124  *
2125  * @param dst IPv4 destination address
2126  *
2127  * @return Pointer to network interface to use, NULL if no suitable interface
2128  * could be found.
2129  */
2130 #if defined(CONFIG_NET_NATIVE_IPV4)
2131 struct net_if *net_if_ipv4_select_src_iface(const struct in_addr *dst);
2132 #else
net_if_ipv4_select_src_iface(const struct in_addr * dst)2133 static inline struct net_if *net_if_ipv4_select_src_iface(
2134 	const struct in_addr *dst)
2135 {
2136 	ARG_UNUSED(dst);
2137 
2138 	return NULL;
2139 }
2140 #endif
2141 
2142 /**
2143  * @brief Get a IPv4 source address that should be used when sending
2144  * network data to destination.
2145  *
2146  * @param iface Interface to use when sending the packet.
2147  * If the interface is not known, then NULL can be given.
2148  * @param dst IPv4 destination address
2149  *
2150  * @return Pointer to IPv4 address to use, NULL if no IPv4 address
2151  * could be found.
2152  */
2153 #if defined(CONFIG_NET_NATIVE_IPV4)
2154 const struct in_addr *net_if_ipv4_select_src_addr(struct net_if *iface,
2155 						  const struct in_addr *dst);
2156 #else
net_if_ipv4_select_src_addr(struct net_if * iface,const struct in_addr * dst)2157 static inline const struct in_addr *net_if_ipv4_select_src_addr(
2158 	struct net_if *iface, const struct in_addr *dst)
2159 {
2160 	ARG_UNUSED(iface);
2161 	ARG_UNUSED(dst);
2162 
2163 	return NULL;
2164 }
2165 #endif
2166 
2167 /**
2168  * @brief Get a IPv4 link local address in a given state.
2169  *
2170  * @param iface Interface to use. Must be a valid pointer to an interface.
2171  * @param addr_state IPv4 address state (preferred, tentative, deprecated)
2172  *
2173  * @return Pointer to link local IPv4 address, NULL if no proper IPv4 address
2174  * could be found.
2175  */
2176 struct in_addr *net_if_ipv4_get_ll(struct net_if *iface,
2177 				   enum net_addr_state addr_state);
2178 
2179 /**
2180  * @brief Get a IPv4 global address in a given state.
2181  *
2182  * @param iface Interface to use. Must be a valid pointer to an interface.
2183  * @param addr_state IPv4 address state (preferred, tentative, deprecated)
2184  *
2185  * @return Pointer to link local IPv4 address, NULL if no proper IPv4 address
2186  * could be found.
2187  */
2188 struct in_addr *net_if_ipv4_get_global_addr(struct net_if *iface,
2189 					    enum net_addr_state addr_state);
2190 
2191 /**
2192  * @brief Set IPv4 netmask for an interface.
2193  *
2194  * @param iface Interface to use.
2195  * @param netmask IPv4 netmask
2196  */
2197 void net_if_ipv4_set_netmask(struct net_if *iface,
2198 			     const struct in_addr *netmask);
2199 
2200 /**
2201  * @brief Set IPv4 netmask for an interface index.
2202  *
2203  * @param index Network interface index
2204  * @param netmask IPv4 netmask
2205  *
2206  * @return True if netmask was added, false otherwise.
2207  */
2208 __syscall bool net_if_ipv4_set_netmask_by_index(int index,
2209 						const struct in_addr *netmask);
2210 
2211 /**
2212  * @brief Set IPv4 gateway for an interface.
2213  *
2214  * @param iface Interface to use.
2215  * @param gw IPv4 address of an gateway
2216  */
2217 void net_if_ipv4_set_gw(struct net_if *iface, const struct in_addr *gw);
2218 
2219 /**
2220  * @brief Set IPv4 gateway for an interface index.
2221  *
2222  * @param index Network interface index
2223  * @param gw IPv4 address of an gateway
2224  *
2225  * @return True if gateway was added, false otherwise.
2226  */
2227 __syscall bool net_if_ipv4_set_gw_by_index(int index, const struct in_addr *gw);
2228 
2229 /**
2230  * @brief Get a network interface that should be used when sending
2231  * IPv6 or IPv4 network data to destination.
2232  *
2233  * @param dst IPv6 or IPv4 destination address
2234  *
2235  * @return Pointer to network interface to use. Note that the function
2236  * will return the default network interface if the best network interface
2237  * is not found.
2238  */
2239 struct net_if *net_if_select_src_iface(const struct sockaddr *dst);
2240 
2241 /**
2242  * @typedef net_if_link_callback_t
2243  * @brief Define callback that is called after a network packet
2244  *        has been sent.
2245  * @param iface A pointer to a struct net_if to which the the net_pkt was sent to.
2246  * @param dst Link layer address of the destination where the network packet was sent.
2247  * @param status Send status, 0 is ok, < 0 error.
2248  */
2249 typedef void (*net_if_link_callback_t)(struct net_if *iface,
2250 				       struct net_linkaddr *dst,
2251 				       int status);
2252 
2253 /**
2254  * @brief Link callback handler struct.
2255  *
2256  * Stores the link callback information. Caller must make sure that
2257  * the variable pointed by this is valid during the lifetime of
2258  * registration. Typically this means that the variable cannot be
2259  * allocated from stack.
2260  */
2261 struct net_if_link_cb {
2262 	/** Node information for the slist. */
2263 	sys_snode_t node;
2264 
2265 	/** Link callback */
2266 	net_if_link_callback_t cb;
2267 };
2268 
2269 /**
2270  * @brief Register a link callback.
2271  *
2272  * @param link Caller specified handler for the callback.
2273  * @param cb Callback to register.
2274  */
2275 void net_if_register_link_cb(struct net_if_link_cb *link,
2276 			     net_if_link_callback_t cb);
2277 
2278 /**
2279  * @brief Unregister a link callback.
2280  *
2281  * @param link Caller specified handler for the callback.
2282  */
2283 void net_if_unregister_link_cb(struct net_if_link_cb *link);
2284 
2285 /**
2286  * @brief Call a link callback function.
2287  *
2288  * @param iface Network interface.
2289  * @param lladdr Destination link layer address
2290  * @param status 0 is ok, < 0 error
2291  */
2292 void net_if_call_link_cb(struct net_if *iface, struct net_linkaddr *lladdr,
2293 			 int status);
2294 
2295 /**
2296  * @brief Check if received network packet checksum calculation can be avoided
2297  * or not. For example many ethernet devices support network packet offloading
2298  * in which case the IP stack does not need to calculate the checksum.
2299  *
2300  * @param iface Network interface
2301  *
2302  * @return True if checksum needs to be calculated, false otherwise.
2303  */
2304 bool net_if_need_calc_rx_checksum(struct net_if *iface);
2305 
2306 /**
2307  * @brief Check if network packet checksum calculation can be avoided or not
2308  * when sending the packet. For example many ethernet devices support network
2309  * packet offloading in which case the IP stack does not need to calculate the
2310  * checksum.
2311  *
2312  * @param iface Network interface
2313  *
2314  * @return True if checksum needs to be calculated, false otherwise.
2315  */
2316 bool net_if_need_calc_tx_checksum(struct net_if *iface);
2317 
2318 /**
2319  * @brief Get interface according to index
2320  *
2321  * @details This is a syscall only to provide access to the object for purposes
2322  *          of assigning permissions.
2323  *
2324  * @param index Interface index
2325  *
2326  * @return Pointer to interface or NULL if not found.
2327  */
2328 __syscall struct net_if *net_if_get_by_index(int index);
2329 
2330 /**
2331  * @brief Get interface index according to pointer
2332  *
2333  * @param iface Pointer to network interface
2334  *
2335  * @return Interface index
2336  */
2337 int net_if_get_by_iface(struct net_if *iface);
2338 
2339 /**
2340  * @typedef net_if_cb_t
2341  * @brief Callback used while iterating over network interfaces
2342  *
2343  * @param iface Pointer to current network interface
2344  * @param user_data A valid pointer to user data or NULL
2345  */
2346 typedef void (*net_if_cb_t)(struct net_if *iface, void *user_data);
2347 
2348 /**
2349  * @brief Go through all the network interfaces and call callback
2350  * for each interface.
2351  *
2352  * @param cb User-supplied callback function to call
2353  * @param user_data User specified data
2354  */
2355 void net_if_foreach(net_if_cb_t cb, void *user_data);
2356 
2357 /**
2358  * @brief Bring interface up
2359  *
2360  * @param iface Pointer to network interface
2361  *
2362  * @return 0 on success
2363  */
2364 int net_if_up(struct net_if *iface);
2365 
2366 /**
2367  * @brief Check if interface is is up and running.
2368  *
2369  * @param iface Pointer to network interface
2370  *
2371  * @return True if interface is up, False if it is down.
2372  */
net_if_is_up(struct net_if * iface)2373 static inline bool net_if_is_up(struct net_if *iface)
2374 {
2375 	NET_ASSERT(iface);
2376 
2377 	return net_if_flag_is_set(iface, NET_IF_UP) &&
2378 	       net_if_flag_is_set(iface, NET_IF_RUNNING);
2379 }
2380 
2381 /**
2382  * @brief Bring interface down
2383  *
2384  * @param iface Pointer to network interface
2385  *
2386  * @return 0 on success
2387  */
2388 int net_if_down(struct net_if *iface);
2389 
2390 /**
2391  * @brief Check if interface was brought up by the administrator.
2392  *
2393  * @param iface Pointer to network interface
2394  *
2395  * @return True if interface is admin up, false otherwise.
2396  */
net_if_is_admin_up(struct net_if * iface)2397 static inline bool net_if_is_admin_up(struct net_if *iface)
2398 {
2399 	NET_ASSERT(iface);
2400 
2401 	return net_if_flag_is_set(iface, NET_IF_UP);
2402 }
2403 
2404 /**
2405  * @brief Underlying network device has detected the carrier (cable connected).
2406  *
2407  * @details The function should be used by the respective network device driver
2408  *          or L2 implementation to update its state on a network interface.
2409  *
2410  * @param iface Pointer to network interface
2411  */
2412 void net_if_carrier_on(struct net_if *iface);
2413 
2414 /**
2415  * @brief Underlying network device has lost the carrier (cable disconnected).
2416  *
2417  * @details The function should be used by the respective network device driver
2418  *          or L2 implementation to update its state on a network interface.
2419  *
2420  * @param iface Pointer to network interface
2421  */
2422 void net_if_carrier_off(struct net_if *iface);
2423 
2424 /**
2425  * @brief Check if carrier is present on network device.
2426  *
2427  * @param iface Pointer to network interface
2428  *
2429  * @return True if carrier is present, false otherwise.
2430  */
net_if_is_carrier_ok(struct net_if * iface)2431 static inline bool net_if_is_carrier_ok(struct net_if *iface)
2432 {
2433 	NET_ASSERT(iface);
2434 
2435 	return net_if_flag_is_set(iface, NET_IF_LOWER_UP);
2436 }
2437 
2438 /**
2439  * @brief Mark interface as dormant. Dormant state indicates that the interface
2440  *        is not ready to pass packets yet, but is waiting for some event
2441  *        (for example Wi-Fi network association).
2442  *
2443  * @details The function should be used by the respective network device driver
2444  *          or L2 implementation to update its state on a network interface.
2445  *
2446  * @param iface Pointer to network interface
2447  */
2448 void net_if_dormant_on(struct net_if *iface);
2449 
2450 /**
2451  * @brief Mark interface as not dormant.
2452  *
2453  * @details The function should be used by the respective network device driver
2454  *          or L2 implementation to update its state on a network interface.
2455  *
2456  * @param iface Pointer to network interface
2457  */
2458 void net_if_dormant_off(struct net_if *iface);
2459 
2460 /**
2461  * @brief Check if the interface is dormant.
2462  *
2463  * @param iface Pointer to network interface
2464  *
2465  * @return True if interface is dormant, false otherwise.
2466  */
net_if_is_dormant(struct net_if * iface)2467 static inline bool net_if_is_dormant(struct net_if *iface)
2468 {
2469 	NET_ASSERT(iface);
2470 
2471 	return net_if_flag_is_set(iface, NET_IF_DORMANT);
2472 }
2473 
2474 #if defined(CONFIG_NET_PKT_TIMESTAMP) && defined(CONFIG_NET_NATIVE)
2475 /**
2476  * @typedef net_if_timestamp_callback_t
2477  * @brief Define callback that is called after a network packet
2478  *        has been timestamped.
2479  * @param "struct net_pkt *pkt" A pointer on a struct net_pkt which has
2480  *        been timestamped after being sent.
2481  */
2482 typedef void (*net_if_timestamp_callback_t)(struct net_pkt *pkt);
2483 
2484 /**
2485  * @brief Timestamp callback handler struct.
2486  *
2487  * Stores the timestamp callback information. Caller must make sure that
2488  * the variable pointed by this is valid during the lifetime of
2489  * registration. Typically this means that the variable cannot be
2490  * allocated from stack.
2491  */
2492 struct net_if_timestamp_cb {
2493 	/** Node information for the slist. */
2494 	sys_snode_t node;
2495 
2496 	/** Packet for which the callback is needed.
2497 	 *  A NULL value means all packets.
2498 	 */
2499 	struct net_pkt *pkt;
2500 
2501 	/** Net interface for which the callback is needed.
2502 	 *  A NULL value means all interfaces.
2503 	 */
2504 	struct net_if *iface;
2505 
2506 	/** Timestamp callback */
2507 	net_if_timestamp_callback_t cb;
2508 };
2509 
2510 /**
2511  * @brief Register a timestamp callback.
2512  *
2513  * @param handle Caller specified handler for the callback.
2514  * @param pkt Net packet for which the callback is registered. NULL for all
2515  * 	      packets.
2516  * @param iface Net interface for which the callback is. NULL for all
2517  *		interfaces.
2518  * @param cb Callback to register.
2519  */
2520 void net_if_register_timestamp_cb(struct net_if_timestamp_cb *handle,
2521 				  struct net_pkt *pkt,
2522 				  struct net_if *iface,
2523 				  net_if_timestamp_callback_t cb);
2524 
2525 /**
2526  * @brief Unregister a timestamp callback.
2527  *
2528  * @param handle Caller specified handler for the callback.
2529  */
2530 void net_if_unregister_timestamp_cb(struct net_if_timestamp_cb *handle);
2531 
2532 /**
2533  * @brief Call a timestamp callback function.
2534  *
2535  * @param pkt Network buffer.
2536  */
2537 void net_if_call_timestamp_cb(struct net_pkt *pkt);
2538 
2539 /*
2540  * @brief Add timestamped TX buffer to be handled
2541  *
2542  * @param pkt Timestamped buffer
2543  */
2544 void net_if_add_tx_timestamp(struct net_pkt *pkt);
2545 #endif /* CONFIG_NET_PKT_TIMESTAMP */
2546 
2547 /**
2548  * @brief Set network interface into promiscuous mode
2549  *
2550  * @details Note that not all network technologies will support this.
2551  *
2552  * @param iface Pointer to network interface
2553  *
2554  * @return 0 on success, <0 if error
2555  */
2556 #if defined(CONFIG_NET_PROMISCUOUS_MODE)
2557 int net_if_set_promisc(struct net_if *iface);
2558 #else
net_if_set_promisc(struct net_if * iface)2559 static inline int net_if_set_promisc(struct net_if *iface)
2560 {
2561 	ARG_UNUSED(iface);
2562 
2563 	return -ENOTSUP;
2564 }
2565 #endif
2566 
2567 /**
2568  * @brief Set network interface into normal mode
2569  *
2570  * @param iface Pointer to network interface
2571  */
2572 #if defined(CONFIG_NET_PROMISCUOUS_MODE)
2573 void net_if_unset_promisc(struct net_if *iface);
2574 #else
net_if_unset_promisc(struct net_if * iface)2575 static inline void net_if_unset_promisc(struct net_if *iface)
2576 {
2577 	ARG_UNUSED(iface);
2578 }
2579 #endif
2580 
2581 /**
2582  * @brief Check if promiscuous mode is set or not.
2583  *
2584  * @param iface Pointer to network interface
2585  *
2586  * @return True if interface is in promisc mode,
2587  *         False if interface is not in in promiscuous mode.
2588  */
2589 #if defined(CONFIG_NET_PROMISCUOUS_MODE)
2590 bool net_if_is_promisc(struct net_if *iface);
2591 #else
net_if_is_promisc(struct net_if * iface)2592 static inline bool net_if_is_promisc(struct net_if *iface)
2593 {
2594 	ARG_UNUSED(iface);
2595 
2596 	return false;
2597 }
2598 #endif
2599 
2600 /**
2601  * @brief Check if there are any pending TX network data for a given network
2602  *        interface.
2603  *
2604  * @param iface Pointer to network interface
2605  *
2606  * @return True if there are pending TX network packets for this network
2607  *         interface, False otherwise.
2608  */
net_if_are_pending_tx_packets(struct net_if * iface)2609 static inline bool net_if_are_pending_tx_packets(struct net_if *iface)
2610 {
2611 #if defined(CONFIG_NET_POWER_MANAGEMENT)
2612 	return !!iface->tx_pending;
2613 #else
2614 	ARG_UNUSED(iface);
2615 
2616 	return false;
2617 #endif
2618 }
2619 
2620 #ifdef CONFIG_NET_POWER_MANAGEMENT
2621 /**
2622  * @brief Suspend a network interface from a power management perspective
2623  *
2624  * @param iface Pointer to network interface
2625  *
2626  * @return 0 on success, or -EALREADY/-EBUSY as possible errors.
2627  */
2628 int net_if_suspend(struct net_if *iface);
2629 
2630 /**
2631  * @brief Resume a network interface from a power management perspective
2632  *
2633  * @param iface Pointer to network interface
2634  *
2635  * @return 0 on success, or -EALREADY as a possible error.
2636  */
2637 int net_if_resume(struct net_if *iface);
2638 
2639 /**
2640  * @brief Check if the network interface is suspended or not.
2641  *
2642  * @param iface Pointer to network interface
2643  *
2644  * @return True if interface is suspended, False otherwise.
2645  */
2646 bool net_if_is_suspended(struct net_if *iface);
2647 #endif /* CONFIG_NET_POWER_MANAGEMENT */
2648 
2649 /**
2650  * @brief Check if the network interface supports Wi-Fi.
2651  *
2652  * @param iface Pointer to network interface
2653  *
2654  * @return True if interface supports Wi-Fi, False otherwise.
2655  */
2656 bool net_if_is_wifi(struct net_if *iface);
2657 
2658 /**
2659  * @brief Get first Wi-Fi network interface.
2660  *
2661  * @return Pointer to network interface, NULL if not found.
2662  */
2663 struct net_if *net_if_get_first_wifi(void);
2664 
2665 /**
2666  * @brief Get network interface name.
2667  *
2668  * @details If interface name support is not enabled, empty string is returned.
2669  *
2670  * @param iface Pointer to network interface
2671  * @param buf User supplied buffer
2672  * @param len Length of the user supplied buffer
2673  *
2674  * @return Length of the interface name copied to buf,
2675  *         -EINVAL if invalid parameters,
2676  *         -ERANGE if name cannot be copied to the user supplied buffer,
2677  *         -ENOTSUP if interface name support is disabled,
2678  */
2679 int net_if_get_name(struct net_if *iface, char *buf, int len);
2680 
2681 /**
2682  * @brief Set network interface name.
2683  *
2684  * @details Normally this function is not needed to call as the system
2685  *          will automatically assign a name to the network interface.
2686  *
2687  * @param iface Pointer to network interface
2688  * @param buf User supplied name
2689  *
2690  * @return 0 name is set correctly
2691  *         -ENOTSUP interface name support is disabled
2692  *         -EINVAL if invalid parameters are given,
2693  *         -ENAMETOOLONG if name is too long
2694  */
2695 int net_if_set_name(struct net_if *iface, const char *buf);
2696 
2697 /**
2698  * @brief Get interface index according to its name
2699  *
2700  * @param name Name of the network interface
2701  *
2702  * @return Interface index
2703  */
2704 int net_if_get_by_name(const char *name);
2705 
2706 /** @cond INTERNAL_HIDDEN */
2707 struct net_if_api {
2708 	void (*init)(struct net_if *iface);
2709 };
2710 
2711 #if defined(CONFIG_NET_IP)
2712 #define NET_IF_IP_INIT .ip = {},
2713 #else
2714 #define NET_IF_IP_INIT
2715 #endif
2716 
2717 #if defined(CONFIG_NET_DHCPV4) && defined(CONFIG_NET_NATIVE_IPV4)
2718 #define NET_IF_DHCPV4_INIT .dhcpv4.state = NET_DHCPV4_DISABLED,
2719 #else
2720 #define NET_IF_DHCPV4_INIT
2721 #endif
2722 
2723 #define NET_IF_CONFIG_INIT				\
2724 	.config = {					\
2725 		NET_IF_IP_INIT				\
2726 		NET_IF_DHCPV4_INIT			\
2727 	}
2728 
2729 #define NET_IF_GET_NAME(dev_id, sfx) __net_if_##dev_id##_##sfx
2730 #define NET_IF_DEV_GET_NAME(dev_id, sfx) __net_if_dev_##dev_id##_##sfx
2731 
2732 #define NET_IF_GET(dev_id, sfx)						\
2733 	((struct net_if *)&NET_IF_GET_NAME(dev_id, sfx))
2734 
2735 #define NET_IF_INIT(dev_id, sfx, _l2, _mtu, _num_configs)		\
2736 	static STRUCT_SECTION_ITERABLE(net_if_dev,			\
2737 				NET_IF_DEV_GET_NAME(dev_id, sfx)) = {	\
2738 		.dev = &(DEVICE_NAME_GET(dev_id)),			\
2739 		.l2 = &(NET_L2_GET_NAME(_l2)),				\
2740 		.l2_data = &(NET_L2_GET_DATA(dev_id, sfx)),		\
2741 		.mtu = _mtu,						\
2742 		.flags = {BIT(NET_IF_LOWER_UP)},			\
2743 	};								\
2744 	static Z_DECL_ALIGN(struct net_if)				\
2745 		       NET_IF_GET_NAME(dev_id, sfx)[_num_configs]	\
2746 		       __used __in_section(_net_if, static,		\
2747 					   dev_id) = {			\
2748 		[0 ... (_num_configs - 1)] = {				\
2749 			.if_dev = &(NET_IF_DEV_GET_NAME(dev_id, sfx)),	\
2750 			NET_IF_CONFIG_INIT				\
2751 		}							\
2752 	}
2753 
2754 #define NET_IF_OFFLOAD_INIT(dev_id, sfx, _mtu)				\
2755 	static STRUCT_SECTION_ITERABLE(net_if_dev,			\
2756 				NET_IF_DEV_GET_NAME(dev_id, sfx)) = {	\
2757 		.dev = &(DEVICE_NAME_GET(dev_id)),			\
2758 		.mtu = _mtu,						\
2759 		.l2 = &(NET_L2_GET_NAME(OFFLOADED_NETDEV)),		\
2760 		.flags = {BIT(NET_IF_LOWER_UP)},			\
2761 	};								\
2762 	static Z_DECL_ALIGN(struct net_if)				\
2763 		NET_IF_GET_NAME(dev_id, sfx)[NET_IF_MAX_CONFIGS]	\
2764 		       __used __in_section(_net_if, static,		\
2765 					   dev_id) = {			\
2766 		[0 ... (NET_IF_MAX_CONFIGS - 1)] = {			\
2767 			.if_dev = &(NET_IF_DEV_GET_NAME(dev_id, sfx)),	\
2768 			NET_IF_CONFIG_INIT				\
2769 		}							\
2770 	}
2771 
2772 /** @endcond */
2773 
2774 /* Network device initialization macros */
2775 
2776 #define Z_NET_DEVICE_INIT(node_id, dev_id, name, init_fn, pm, data,	\
2777 			  config, prio, api, l2, l2_ctx_type, mtu)	\
2778 	Z_DEVICE_STATE_DEFINE(dev_id);					\
2779 	Z_DEVICE_DEFINE(node_id, dev_id, name, init_fn, pm, data,	\
2780 			config, POST_KERNEL, prio, api,			\
2781 			&Z_DEVICE_STATE_NAME(dev_id));			\
2782 	NET_L2_DATA_INIT(dev_id, 0, l2_ctx_type);			\
2783 	NET_IF_INIT(dev_id, 0, l2, mtu, NET_IF_MAX_CONFIGS)
2784 
2785 /**
2786  * @brief Create a network interface and bind it to network device.
2787  *
2788  * @param dev_id Network device id.
2789  * @param name The name this instance of the driver exposes to
2790  * the system.
2791  * @param init_fn Address to the init function of the driver.
2792  * @param pm Reference to struct pm_device associated with the device.
2793  * (optional).
2794  * @param data Pointer to the device's private data.
2795  * @param config The address to the structure containing the
2796  * configuration information for this instance of the driver.
2797  * @param prio The initialization level at which configuration occurs.
2798  * @param api Provides an initial pointer to the API function struct
2799  * used by the driver. Can be NULL.
2800  * @param l2 Network L2 layer for this network interface.
2801  * @param l2_ctx_type Type of L2 context data.
2802  * @param mtu Maximum transfer unit in bytes for this network interface.
2803  */
2804 #define NET_DEVICE_INIT(dev_id, name, init_fn, pm, data, config, prio,	\
2805 			api, l2, l2_ctx_type, mtu)			\
2806 	Z_NET_DEVICE_INIT(DT_INVALID_NODE, dev_id, name, init_fn, pm,	\
2807 			  data, config, prio, api, l2, l2_ctx_type, mtu)
2808 
2809 /**
2810  * @brief Like NET_DEVICE_INIT but taking metadata from a devicetree node.
2811  * Create a network interface and bind it to network device.
2812  *
2813  * @param node_id The devicetree node identifier.
2814  * @param init_fn Address to the init function of the driver.
2815  * @param pm Reference to struct pm_device associated with the device.
2816  * (optional).
2817  * @param data Pointer to the device's private data.
2818  * @param config The address to the structure containing the
2819  * configuration information for this instance of the driver.
2820  * @param prio The initialization level at which configuration occurs.
2821  * @param api Provides an initial pointer to the API function struct
2822  * used by the driver. Can be NULL.
2823  * @param l2 Network L2 layer for this network interface.
2824  * @param l2_ctx_type Type of L2 context data.
2825  * @param mtu Maximum transfer unit in bytes for this network interface.
2826  */
2827 #define NET_DEVICE_DT_DEFINE(node_id, init_fn, pm, data,		\
2828 			     config, prio, api, l2, l2_ctx_type, mtu)	\
2829 	Z_NET_DEVICE_INIT(node_id, Z_DEVICE_DT_DEV_ID(node_id),	\
2830 			  DEVICE_DT_NAME(node_id), init_fn, pm, data,	\
2831 			  config, prio, api, l2, l2_ctx_type, mtu)
2832 
2833 /**
2834  * @brief Like NET_DEVICE_DT_DEFINE for an instance of a DT_DRV_COMPAT compatible
2835  *
2836  * @param inst instance number.  This is replaced by
2837  * <tt>DT_DRV_COMPAT(inst)</tt> in the call to NET_DEVICE_DT_DEFINE.
2838  *
2839  * @param ... other parameters as expected by NET_DEVICE_DT_DEFINE.
2840  */
2841 #define NET_DEVICE_DT_INST_DEFINE(inst, ...) \
2842 	NET_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
2843 
2844 #define Z_NET_DEVICE_INIT_INSTANCE(node_id, dev_id, name, instance,	\
2845 				   init_fn, pm, data, config, prio,	\
2846 				   api, l2, l2_ctx_type, mtu)		\
2847 	Z_DEVICE_STATE_DEFINE(dev_id);					\
2848 	Z_DEVICE_DEFINE(node_id, dev_id, name, init_fn, pm, data,	\
2849 			config,	POST_KERNEL, prio, api,			\
2850 			&Z_DEVICE_STATE_NAME(dev_id));			\
2851 	NET_L2_DATA_INIT(dev_id, instance, l2_ctx_type);		\
2852 	NET_IF_INIT(dev_id, instance, l2, mtu, NET_IF_MAX_CONFIGS)
2853 
2854 /**
2855  * @brief Create multiple network interfaces and bind them to network device.
2856  * If your network device needs more than one instance of a network interface,
2857  * use this macro below and provide a different instance suffix each time
2858  * (0, 1, 2, ... or a, b, c ... whatever works for you)
2859  *
2860  * @param dev_id Network device id.
2861  * @param name The name this instance of the driver exposes to
2862  * the system.
2863  * @param instance Instance identifier.
2864  * @param init_fn Address to the init function of the driver.
2865  * @param pm Reference to struct pm_device associated with the device.
2866  * (optional).
2867  * @param data Pointer to the device's private data.
2868  * @param config The address to the structure containing the
2869  * configuration information for this instance of the driver.
2870  * @param prio The initialization level at which configuration occurs.
2871  * @param api Provides an initial pointer to the API function struct
2872  * used by the driver. Can be NULL.
2873  * @param l2 Network L2 layer for this network interface.
2874  * @param l2_ctx_type Type of L2 context data.
2875  * @param mtu Maximum transfer unit in bytes for this network interface.
2876  */
2877 #define NET_DEVICE_INIT_INSTANCE(dev_id, name, instance, init_fn, pm,	\
2878 				 data, config, prio, api, l2,		\
2879 				 l2_ctx_type, mtu)			\
2880 	Z_NET_DEVICE_INIT_INSTANCE(DT_INVALID_NODE, dev_id, name,	\
2881 				   instance, init_fn, pm, data, config,	\
2882 				   prio, api, l2, l2_ctx_type, mtu)
2883 
2884 /**
2885  * @brief Like NET_DEVICE_OFFLOAD_INIT but taking metadata from a devicetree.
2886  * Create multiple network interfaces and bind them to network device.
2887  * If your network device needs more than one instance of a network interface,
2888  * use this macro below and provide a different instance suffix each time
2889  * (0, 1, 2, ... or a, b, c ... whatever works for you)
2890  *
2891  * @param node_id The devicetree node identifier.
2892  * @param instance Instance identifier.
2893  * @param init_fn Address to the init function of the driver.
2894  * @param pm Reference to struct pm_device associated with the device.
2895  * (optional).
2896  * @param data Pointer to the device's private data.
2897  * @param config The address to the structure containing the
2898  * configuration information for this instance of the driver.
2899  * @param prio The initialization level at which configuration occurs.
2900  * @param api Provides an initial pointer to the API function struct
2901  * used by the driver. Can be NULL.
2902  * @param l2 Network L2 layer for this network interface.
2903  * @param l2_ctx_type Type of L2 context data.
2904  * @param mtu Maximum transfer unit in bytes for this network interface.
2905  */
2906 #define NET_DEVICE_DT_DEFINE_INSTANCE(node_id, instance, init_fn, pm,	\
2907 				      data, config, prio, api, l2,	\
2908 				      l2_ctx_type, mtu)			\
2909 	Z_NET_DEVICE_INIT_INSTANCE(node_id,				\
2910 				   Z_DEVICE_DT_DEV_ID(node_id),		\
2911 				   DEVICE_DT_NAME(node_id), instance,	\
2912 				   init_fn, pm, data, config, prio,	\
2913 				   api,	l2, l2_ctx_type, mtu)
2914 
2915 /**
2916  * @brief Like NET_DEVICE_DT_DEFINE_INSTANCE for an instance of a DT_DRV_COMPAT
2917  * compatible
2918  *
2919  * @param inst instance number.  This is replaced by
2920  * <tt>DT_DRV_COMPAT(inst)</tt> in the call to NET_DEVICE_DT_DEFINE_INSTANCE.
2921  *
2922  * @param ... other parameters as expected by NET_DEVICE_DT_DEFINE_INSTANCE.
2923  */
2924 #define NET_DEVICE_DT_INST_DEFINE_INSTANCE(inst, ...) \
2925 	NET_DEVICE_DT_DEFINE_INSTANCE(DT_DRV_INST(inst), __VA_ARGS__)
2926 
2927 #define Z_NET_DEVICE_OFFLOAD_INIT(node_id, dev_id, name, init_fn, pm,	\
2928 				  data, config, prio, api, mtu)		\
2929 	Z_DEVICE_STATE_DEFINE(dev_id);					\
2930 	Z_DEVICE_DEFINE(node_id, dev_id, name, init_fn, pm, data,	\
2931 			config, POST_KERNEL, prio, api,			\
2932 			&Z_DEVICE_STATE_NAME(dev_id));			\
2933 	NET_IF_OFFLOAD_INIT(dev_id, 0, mtu)
2934 
2935 /**
2936  * @brief Create a offloaded network interface and bind it to network device.
2937  * The offloaded network interface is implemented by a device vendor HAL or
2938  * similar.
2939  *
2940  * @param dev_id Network device id.
2941  * @param name The name this instance of the driver exposes to
2942  * the system.
2943  * @param init_fn Address to the init function of the driver.
2944  * @param pm Reference to struct pm_device associated with the device.
2945  * (optional).
2946  * @param data Pointer to the device's private data.
2947  * @param config The address to the structure containing the
2948  * configuration information for this instance of the driver.
2949  * @param prio The initialization level at which configuration occurs.
2950  * @param api Provides an initial pointer to the API function struct
2951  * used by the driver. Can be NULL.
2952  * @param mtu Maximum transfer unit in bytes for this network interface.
2953  */
2954 #define NET_DEVICE_OFFLOAD_INIT(dev_id, name, init_fn, pm, data,	\
2955 				config,	prio, api, mtu)			\
2956 	Z_NET_DEVICE_OFFLOAD_INIT(DT_INVALID_NODE, dev_id, name,	\
2957 				  init_fn, pm, data, config, prio, api,	\
2958 				  mtu)
2959 
2960 /**
2961  * @brief Like NET_DEVICE_OFFLOAD_INIT but taking metadata from a devicetree
2962  * node. Create a offloaded network interface and bind it to network device.
2963  * The offloaded network interface is implemented by a device vendor HAL or
2964  * similar.
2965  *
2966  * @param node_id The devicetree node identifier.
2967  * @param init_fn Address to the init function of the driver.
2968  * @param pm Reference to struct pm_device associated with the device.
2969  * (optional).
2970  * @param data Pointer to the device's private data.
2971  * @param config The address to the structure containing the
2972  * configuration information for this instance of the driver.
2973  * @param prio The initialization level at which configuration occurs.
2974  * @param api Provides an initial pointer to the API function struct
2975  * used by the driver. Can be NULL.
2976  * @param mtu Maximum transfer unit in bytes for this network interface.
2977  */
2978 #define NET_DEVICE_DT_OFFLOAD_DEFINE(node_id, init_fn, pm, data,	\
2979 				     config, prio, api, mtu)		\
2980 	Z_NET_DEVICE_OFFLOAD_INIT(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
2981 				  DEVICE_DT_NAME(node_id), init_fn, pm, \
2982 				  data, config,	prio, api, mtu)
2983 
2984 /**
2985  * @brief Like NET_DEVICE_DT_OFFLOAD_DEFINE for an instance of a DT_DRV_COMPAT
2986  * compatible
2987  *
2988  * @param inst instance number.  This is replaced by
2989  * <tt>DT_DRV_COMPAT(inst)</tt> in the call to NET_DEVICE_DT_OFFLOAD_DEFINE.
2990  *
2991  * @param ... other parameters as expected by NET_DEVICE_DT_OFFLOAD_DEFINE.
2992  */
2993 #define NET_DEVICE_DT_INST_OFFLOAD_DEFINE(inst, ...) \
2994 	NET_DEVICE_DT_OFFLOAD_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
2995 
2996 #ifdef __cplusplus
2997 }
2998 #endif
2999 
3000 #include <syscalls/net_if.h>
3001 
3002 /**
3003  * @}
3004  */
3005 
3006 #endif /* ZEPHYR_INCLUDE_NET_NET_IF_H_ */
3007