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