Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
net_ip.h
Go to the documentation of this file.
1
6
7/*
8 * Copyright (c) 2016 Intel Corporation
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13#ifndef ZEPHYR_INCLUDE_NET_NET_IP_H_
14#define ZEPHYR_INCLUDE_NET_NET_IP_H_
15
24
25#include <string.h>
26#include <zephyr/types.h>
27#include <stdbool.h>
28#include <zephyr/sys/util.h>
30#include <zephyr/toolchain.h>
31
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
39/* Specifying VLAN tag here in order to avoid circular dependencies */
40#define NET_VLAN_TAG_UNSPEC 0x0fff
42
43/* Protocol families. */
44#define NET_PF_UNSPEC 0
45#define NET_PF_INET 1
46#define NET_PF_INET6 2
47#define NET_PF_PACKET 3
48#define NET_PF_CAN 4
49#define NET_PF_NET_MGMT 5
50#define NET_PF_LOCAL 6
51#define NET_PF_UNIX NET_PF_LOCAL
52
53/* Address families. */
54#define NET_AF_UNSPEC NET_PF_UNSPEC
55#define NET_AF_INET NET_PF_INET
56#define NET_AF_INET6 NET_PF_INET6
57#define NET_AF_PACKET NET_PF_PACKET
58#define NET_AF_CAN NET_PF_CAN
59#define NET_AF_NET_MGMT NET_PF_NET_MGMT
60#define NET_AF_LOCAL NET_PF_LOCAL
61#define NET_AF_UNIX NET_PF_UNIX
62
76
86
93
100#define net_ntohs(x) sys_be16_to_cpu(x)
101
108#define net_ntohl(x) sys_be32_to_cpu(x)
109
116#define net_ntohll(x) sys_be64_to_cpu(x)
117
124#define net_htons(x) sys_cpu_to_be16(x)
125
132#define net_htonl(x) sys_cpu_to_be32(x)
133
140#define net_htonll(x) sys_cpu_to_be64(x)
141
144 union {
148 };
149};
150
152#define NET_IPV6_ADDR_SIZE 16
153
163
165#define NET_IPV4_ADDR_SIZE 4
166
168typedef unsigned short int net_sa_family_t;
169
172
173/*
174 * Note that the sin_port and sin6_port are in network byte order
175 * in various net_sockaddr* structs.
176 */
177
185
192
203
205
207struct net_sockaddr_in6_ptr {
208 net_sa_family_t sin6_family;
209 uint16_t sin6_port;
210 struct net_in6_addr *sin6_addr;
211 uint8_t sin6_scope_id;
212};
213
215struct net_sockaddr_in_ptr {
216 net_sa_family_t sin_family;
217 uint16_t sin_port;
218 struct net_in_addr *sin_addr;
219};
220
222struct net_sockaddr_ll_ptr {
223 net_sa_family_t sll_family;
224 uint16_t sll_protocol;
225 int sll_ifindex;
226 uint16_t sll_hatype;
227 uint8_t sll_pkttype;
228 uint8_t sll_halen;
229 uint8_t *sll_addr;
230};
231
233struct net_sockaddr_un_ptr {
234 net_sa_family_t sun_family;
235 char *sun_path;
236};
237
238struct net_sockaddr_can_ptr {
239 net_sa_family_t can_family;
240 int can_ifindex;
241};
242
244
245#if !defined(HAVE_IOVEC)
247struct net_iovec {
248 void *iov_base;
249 size_t iov_len;
250};
251#endif
252
263
271
273
274/* Alignment for headers and data. These are arch specific but define
275 * them here atm if not found already.
276 */
277#if !defined(NET_ALIGN_H)
278#define NET_ALIGN_H(x) ROUND_UP(x, __alignof__(struct net_cmsghdr))
279#endif
280#if !defined(NET_ALIGN_D)
281#define NET_ALIGN_D(x) ROUND_UP(x, __alignof__(z_max_align_t))
282#endif
283
285
286#if !defined(NET_CMSG_FIRSTHDR)
292#define NET_CMSG_FIRSTHDR(msghdr) \
293 ((msghdr)->msg_controllen >= sizeof(struct net_cmsghdr) ? \
294 (struct net_cmsghdr *)((msghdr)->msg_control) : NULL)
295#endif
296
297#if !defined(NET_CMSG_NXTHDR)
302#define NET_CMSG_NXTHDR(msghdr, cmsg) \
303 (((cmsg) == NULL) ? NET_CMSG_FIRSTHDR(msghdr) : \
304 (((uint8_t *)(cmsg) + NET_ALIGN_H((cmsg)->cmsg_len) + \
305 NET_ALIGN_D(sizeof(struct net_cmsghdr)) > \
306 (uint8_t *)((msghdr)->msg_control) + (msghdr)->msg_controllen) ? \
307 NULL : \
308 (struct net_cmsghdr *)((uint8_t *)(cmsg) + \
309 NET_ALIGN_H((cmsg)->cmsg_len))))
310#endif
311
312#if !defined(NET_CMSG_DATA)
320#define NET_CMSG_DATA(cmsg) ((uint8_t *)(cmsg) + NET_ALIGN_D(sizeof(struct net_cmsghdr)))
321#endif
322
323#if !defined(NET_CMSG_SPACE)
328#define NET_CMSG_SPACE(length) (NET_ALIGN_D(sizeof(struct net_cmsghdr)) + NET_ALIGN_H(length))
329#endif
330
331#if !defined(NET_CMSG_LEN)
337#define NET_CMSG_LEN(length) (NET_ALIGN_D(sizeof(struct net_cmsghdr)) + length)
338#endif
339
341
342/* Packet types. */
343#define NET_PACKET_HOST 0 /* To us */
344#define NET_PACKET_BROADCAST 1 /* To all */
345#define NET_PACKET_MULTICAST 2 /* To group */
346#define NET_PACKET_OTHERHOST 3 /* To someone else */
347#define NET_PACKET_OUTGOING 4 /* Originated by us */
348#define NET_PACKET_LOOPBACK 5
349#define NET_PACKET_FASTROUTE 6
350
351/* ARP protocol HARDWARE identifiers. */
352#define NET_ARPHRD_ETHER 1
353#define NET_ARPHRD_PPP 512
354
355/* Note: These macros are defined in a specific order.
356 * The largest sockaddr size is the last one.
357 */
358#if defined(CONFIG_NET_IPV4)
359#undef NET_SOCKADDR_MAX_SIZE
360#undef NET_SOCKADDR_PTR_MAX_SIZE
361#define NET_SOCKADDR_MAX_SIZE (sizeof(struct net_sockaddr_in))
362#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct net_sockaddr_in_ptr))
363#endif
364
365#if defined(CONFIG_NET_SOCKETS_PACKET)
366#undef NET_SOCKADDR_MAX_SIZE
367#undef NET_SOCKADDR_PTR_MAX_SIZE
368#define NET_SOCKADDR_MAX_SIZE (sizeof(struct net_sockaddr_ll))
369#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct net_sockaddr_ll_ptr))
370#endif
371
372#if defined(CONFIG_NET_IPV6)
373#undef NET_SOCKADDR_MAX_SIZE
374#define NET_SOCKADDR_MAX_SIZE (sizeof(struct net_sockaddr_in6))
375#if !defined(CONFIG_NET_SOCKETS_PACKET)
376#undef NET_SOCKADDR_PTR_MAX_SIZE
377#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct net_sockaddr_in6_ptr))
378#endif
379#endif
380
381#if defined(CONFIG_NET_NATIVE_OFFLOADED_SOCKETS)
382#define UNIX_PATH_MAX 108
383#undef NET_SOCKADDR_MAX_SIZE
384/* Define NET_SOCKADDR_MAX_SIZE to be struct of sa_family_t + char[UNIX_PATH_MAX] */
385#define NET_SOCKADDR_MAX_SIZE (UNIX_PATH_MAX+sizeof(net_sa_family_t))
386#if !defined(CONFIG_NET_SOCKETS_PACKET)
387#undef NET_SOCKADDR_PTR_MAX_SIZE
388#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct net_sockaddr_un_ptr))
389#endif
390#endif
391
392#if !defined(CONFIG_NET_IPV4)
393#if !defined(CONFIG_NET_IPV6)
394#if !defined(CONFIG_NET_SOCKETS_PACKET)
395#if !defined(CONFIG_NET_NATIVE_OFFLOADED_SOCKETS)
396#define NET_SOCKADDR_MAX_SIZE (sizeof(struct net_sockaddr_in6))
397#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct net_sockaddr_in6_ptr))
398#endif
399#endif
400#endif
401#endif
402
404
405#define SOCKADDR_ALIGN (4)
406
411 char data[NET_SOCKADDR_MAX_SIZE - sizeof(net_sa_family_t)];
413} __aligned(SOCKADDR_ALIGN);
414
416
417struct net_sockaddr_ptr {
418 net_sa_family_t family;
419 char data[NET_SOCKADDR_PTR_MAX_SIZE - sizeof(net_sa_family_t)];
420} __aligned(SOCKADDR_ALIGN);
421
422/* Same as sockaddr in our case */
423struct net_sockaddr_storage {
424 net_sa_family_t ss_family;
425 char data[NET_SOCKADDR_MAX_SIZE - sizeof(net_sa_family_t)];
426} __aligned(SOCKADDR_ALIGN);
427
428/* Socket address struct for UNIX domain sockets */
429struct net_sockaddr_un {
430 net_sa_family_t sun_family; /* NET_AF_UNIX */
431 char sun_path[NET_SOCKADDR_MAX_SIZE - sizeof(net_sa_family_t)];
432};
433
434struct net_addr {
435 net_sa_family_t family;
436 union {
437#if defined(CONFIG_NET_NAMESPACE_COMPAT_MODE)
438 struct net_in6_addr net_in6_addr;
439 struct net_in_addr net_in_addr;
440#else
441 struct net_in6_addr in6_addr;
442 struct net_in_addr in_addr;
443#endif
444 };
445};
446
448extern const struct net_in6_addr net_in6addr_any;
449
451extern const struct net_in6_addr net_in6addr_loopback;
452
454
456#define NET_IN6ADDR_ANY_INIT { { { 0, 0, 0, 0, 0, 0, 0, 0, \
457 0, 0, 0, 0, 0, 0, 0, 0 } } }
458
460#define NET_IN6ADDR_LOOPBACK_INIT { { { 0, 0, 0, 0, 0, 0, 0, 0, \
461 0, 0, 0, 0, 0, 0, 0, 1 } } }
462
464#define NET_INADDR_ANY 0
465
467#define NET_INADDR_BROADCAST 0xffffffff
468
470#define NET_INADDR_ANY_INIT { { { NET_INADDR_ANY } } }
471
473#define NET_INADDR_LOOPBACK_INIT { { { 127, 0, 0, 1 } } }
474
476#define NET_INET_ADDRSTRLEN 16
477
481#define NET_INET6_ADDRSTRLEN 46
482
484
485/* These are for internal usage of the stack */
486#define NET_IPV6_ADDR_LEN sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxx.xxx.xxx.xxx")
487#define NET_IPV4_ADDR_LEN sizeof("xxx.xxx.xxx.xxx")
488
490
496#if defined(CONFIG_NET_NATIVE_IPV6)
497 NET_IPV6_MTU = CONFIG_NET_IPV6_MTU,
498#else
500#endif
501
505#if defined(CONFIG_NET_NATIVE_IPV4)
506 NET_IPV4_MTU = CONFIG_NET_IPV4_MTU,
507#else
509#endif
510};
511
523
524#define NET_MAX_PRIORITIES 8
525
534
542
556
558
559struct net_ipv6_hdr {
560 uint8_t vtc;
561 uint8_t tcflow;
562 uint16_t flow;
563 uint16_t len;
564 uint8_t nexthdr;
565 uint8_t hop_limit;
568} __packed;
569
570struct net_ipv6_frag_hdr {
571 uint8_t nexthdr;
572 uint8_t reserved;
573 uint16_t offset;
574 uint32_t id;
575} __packed;
576
577struct net_ipv4_hdr {
578 uint8_t vhl;
579 uint8_t tos;
580 uint16_t len;
581 uint8_t id[2];
582 uint8_t offset[2];
583 uint8_t ttl;
584 uint8_t proto;
585 uint16_t chksum;
588} __packed;
589
590struct net_icmp_hdr {
591 uint8_t type;
592 uint8_t code;
593 uint16_t chksum;
594} __packed;
595
596struct net_udp_hdr {
597 uint16_t src_port;
598 uint16_t dst_port;
599 uint16_t len;
600 uint16_t chksum;
601} __packed;
602
603struct net_tcp_hdr {
604 uint16_t src_port;
605 uint16_t dst_port;
606 uint8_t seq[4];
607 uint8_t ack[4];
608 uint8_t offset;
610 uint8_t wnd[2];
611 uint16_t chksum;
612 uint8_t urg[2];
613 uint8_t optdata[0];
614} __packed;
615
616static inline const char *net_addr_type2str(enum net_addr_type type)
617{
618 switch (type) {
620 return "AUTO";
621 case NET_ADDR_DHCP:
622 return "DHCP";
623 case NET_ADDR_MANUAL:
624 return "MANUAL";
626 return "OVERRIDE";
627 case NET_ADDR_ANY:
628 default:
629 break;
630 }
631
632 return "<unknown>";
633}
634
635/* IPv6 extension headers types */
636#define NET_IPV6_NEXTHDR_HBHO 0
637#define NET_IPV6_NEXTHDR_DESTO 60
638#define NET_IPV6_NEXTHDR_ROUTING 43
639#define NET_IPV6_NEXTHDR_FRAG 44
640#define NET_IPV6_NEXTHDR_NONE 59
641
646union net_ip_header {
647 struct net_ipv4_hdr *ipv4;
648 struct net_ipv6_hdr *ipv6;
649};
650
651union net_proto_header {
652 struct net_udp_hdr *udp;
653 struct net_tcp_hdr *tcp;
654};
655
656#define NET_UDPH_LEN 8 /* Size of UDP header */
657#define NET_TCPH_LEN 20 /* Size of TCP header */
658#define NET_ICMPH_LEN 4 /* Size of ICMP header */
659
660#define NET_IPV6H_LEN 40 /* Size of IPv6 header */
661#define NET_ICMPV6H_LEN NET_ICMPH_LEN /* Size of ICMPv6 header */
662#define NET_IPV6UDPH_LEN (NET_UDPH_LEN + NET_IPV6H_LEN) /* IPv6 + UDP */
663#define NET_IPV6TCPH_LEN (NET_TCPH_LEN + NET_IPV6H_LEN) /* IPv6 + TCP */
664#define NET_IPV6ICMPH_LEN (NET_IPV6H_LEN + NET_ICMPH_LEN) /* ICMPv6 + IPv6 */
665#define NET_IPV6_FRAGH_LEN 8
666
667#define NET_IPV4H_LEN 20 /* Size of IPv4 header */
668#define NET_ICMPV4H_LEN NET_ICMPH_LEN /* Size of ICMPv4 header */
669#define NET_IPV4UDPH_LEN (NET_UDPH_LEN + NET_IPV4H_LEN) /* IPv4 + UDP */
670#define NET_IPV4TCPH_LEN (NET_TCPH_LEN + NET_IPV4H_LEN) /* IPv4 + TCP */
671#define NET_IPV4ICMPH_LEN (NET_IPV4H_LEN + NET_ICMPH_LEN) /* ICMPv4 + IPv4 */
672
673#define NET_IPV6H_LENGTH_OFFSET 0x04 /* Offset of the Length field in the IPv6 header */
674
675#define NET_IPV6_FRAGH_OFFSET_MASK 0xfff8 /* Mask for the 13-bit Fragment Offset field */
676#define NET_IPV4_FRAGH_OFFSET_MASK 0x1fff /* Mask for the 13-bit Fragment Offset field */
677#define NET_IPV4_MORE_FRAG_MASK 0x2000 /* Mask for the 1-bit More Fragments field */
678#define NET_IPV4_DO_NOT_FRAG_MASK 0x4000 /* Mask for the 1-bit Do Not Fragment field */
679
681#if defined(CONFIG_NET_INTERFACE_NAME)
682#define NET_IFNAMSIZ CONFIG_NET_INTERFACE_NAME_LEN
683#else
684#if defined(Z_DEVICE_MAX_NAME_LEN)
685#define NET_IFNAMSIZ Z_DEVICE_MAX_NAME_LEN
686#else
687#define NET_IFNAMSIZ 1
688#endif /* Z_DEVICE_MAX_NAME_LEN */
689#endif /* CONFIG_NET_INTERFACE_NAME */
690
692
697
699struct net_ifreq {
700 char ifr_name[NET_IFNAMSIZ];
701};
702
703
715
724
732
743
752 unsigned int ipi6_ifindex;
753};
754
756static inline bool net_ipv6_is_addr_loopback_raw(const uint8_t *addr)
757{
758 return UNALIGNED_GET((uint32_t *)addr) == 0 &&
759 UNALIGNED_GET((uint32_t *)addr + 1) == 0 &&
760 UNALIGNED_GET((uint32_t *)addr + 2) == 0 &&
761 net_ntohl(UNALIGNED_GET((uint32_t *)addr + 3)) == 1;
762}
764
772static inline bool net_ipv6_is_addr_loopback(const struct net_in6_addr *addr)
773{
774 return net_ipv6_is_addr_loopback_raw(addr->s6_addr);
775}
776
778static inline bool net_ipv6_is_addr_mcast_raw(const uint8_t *addr)
779{
780 return addr[0] == 0xff;
781}
783
791static inline bool net_ipv6_is_addr_mcast(const struct net_in6_addr *addr)
792{
793 return net_ipv6_is_addr_mcast_raw(addr->s6_addr);
794}
795
796struct net_if;
797struct net_if_config;
798
800extern struct net_if_addr *net_if_ipv6_addr_lookup_raw(const uint8_t *addr,
801 struct net_if **ret);
802
803static inline bool net_ipv6_is_my_addr_raw(const uint8_t *addr)
804{
805 return net_if_ipv6_addr_lookup_raw(addr, NULL) != NULL;
806}
808
809extern struct net_if_addr *net_if_ipv6_addr_lookup(const struct net_in6_addr *addr,
810 struct net_if **iface);
811
819static inline bool net_ipv6_is_my_addr(struct net_in6_addr *addr)
820{
821 return net_if_ipv6_addr_lookup(addr, NULL) != NULL;
822}
823
825 const struct net_in6_addr *addr, struct net_if **iface);
826
835static inline bool net_ipv6_is_my_maddr(struct net_in6_addr *maddr)
836{
837 return net_if_ipv6_maddr_lookup(maddr, NULL) != NULL;
838}
839
849static inline bool net_ipv6_is_prefix(const uint8_t *addr1,
850 const uint8_t *addr2,
851 uint8_t length)
852{
853 uint8_t bits = 128 - length;
854 uint8_t bytes = length / 8U;
855 uint8_t remain = bits % 8;
856 uint8_t mask;
857
858 if (length > 128) {
859 return false;
860 }
861
862 if (memcmp(addr1, addr2, bytes)) {
863 return false;
864 }
865
866 if (!remain) {
867 /* No remaining bits, the prefixes are the same as first
868 * bytes are the same.
869 */
870 return true;
871 }
872
873 /* Create a mask that has remaining most significant bits set */
874 mask = (uint8_t)((0xff << (8 - remain)) ^ 0xff) << remain;
875
876 return (addr1[bytes] & mask) == (addr2[bytes] & mask);
877}
878
879
887static inline void net_ipv6_addr_prefix_mask(const uint8_t *inaddr,
888 uint8_t *outaddr,
889 uint8_t prefix_len)
890{
891 uint8_t bits = 128 - prefix_len;
892 uint8_t bytes = prefix_len / 8U;
893 uint8_t remain = bits % 8;
894 uint8_t mask;
895
896 memset(outaddr, 0, 16U);
897 memcpy(outaddr, inaddr, bytes);
898
899 if (!remain) {
900 /* No remaining bits, the prefixes are the same as first
901 * bytes are the same.
902 */
903 return;
904 }
905
906 /* Create a mask that has remaining most significant bits set */
907 mask = (uint8_t)((0xff << (8 - remain)) ^ 0xff) << remain;
908 outaddr[bytes] = inaddr[bytes] & mask;
909}
910
912static inline bool net_ipv4_is_addr_loopback_raw(const uint8_t *addr)
913{
914 return addr[0] == 127U;
915}
917
925static inline bool net_ipv4_is_addr_loopback(const struct net_in_addr *addr)
926{
927 return net_ipv4_is_addr_loopback_raw(addr->s4_addr);
928}
929
931static inline bool net_ipv4_is_addr_unspecified_raw(const uint8_t *addr)
932{
933 return UNALIGNED_GET((uint32_t *)addr) == 0;
934}
936
944static inline bool net_ipv4_is_addr_unspecified(const struct net_in_addr *addr)
945{
946 return net_ipv4_is_addr_unspecified_raw(addr->s4_addr);
947}
948
950static inline bool net_ipv4_is_addr_mcast_raw(const uint8_t *addr)
951{
952 return (net_ntohl(UNALIGNED_GET((uint32_t *)addr)) & 0xF0000000) == 0xE0000000;
953}
955
963static inline bool net_ipv4_is_addr_mcast(const struct net_in_addr *addr)
964{
965 return net_ipv4_is_addr_mcast_raw(addr->s4_addr);
966}
967
969static inline bool net_ipv4_is_ll_addr_raw(const uint8_t *addr)
970{
971 return (net_ntohl(UNALIGNED_GET((uint32_t *)addr)) & 0xFFFF0000) == 0xA9FE0000;
972}
974
982static inline bool net_ipv4_is_ll_addr(const struct net_in_addr *addr)
983{
984 return net_ipv4_is_ll_addr_raw(addr->s4_addr);
985}
986
996static inline bool net_ipv4_is_private_addr(const struct net_in_addr *addr)
997{
998 uint32_t masked_24, masked_16, masked_12, masked_10, masked_8;
999
1000 masked_24 = net_ntohl(UNALIGNED_GET(&addr->s_addr)) & 0xFFFFFF00;
1001 masked_16 = masked_24 & 0xFFFF0000;
1002 masked_12 = masked_24 & 0xFFF00000;
1003 masked_10 = masked_24 & 0xFFC00000;
1004 masked_8 = masked_24 & 0xFF000000;
1005
1006 return masked_8 == 0x0A000000 || /* 10.0.0.0/8 */
1007 masked_10 == 0x64400000 || /* 100.64.0.0/10 */
1008 masked_12 == 0xAC100000 || /* 172.16.0.0/12 */
1009 masked_16 == 0xC0A80000 || /* 192.168.0.0/16 */
1010 masked_24 == 0xC0000200 || /* 192.0.2.0/24 */
1011 masked_24 == 0xC0336400 || /* 192.51.100.0/24 */
1012 masked_24 == 0xCB007100; /* 203.0.113.0/24 */
1013}
1014
1023#define net_ipaddr_copy(dest, src) \
1024 UNALIGNED_PUT(UNALIGNED_GET(src), dest)
1025
1032static inline void net_ipv4_addr_copy_raw(uint8_t *dest,
1033 const uint8_t *src)
1034{
1035 net_ipaddr_copy((struct net_in_addr *)dest, (const struct net_in_addr *)src);
1036}
1037
1044static inline void net_ipv6_addr_copy_raw(uint8_t *dest,
1045 const uint8_t *src)
1046{
1047 memcpy(dest, src, sizeof(struct net_in6_addr));
1048}
1049
1058static inline bool net_ipv4_addr_cmp_raw(const uint8_t *addr1,
1059 const uint8_t *addr2)
1060{
1061 return UNALIGNED_GET((uint32_t *)addr1) == UNALIGNED_GET((uint32_t *)addr2);
1062}
1063
1072static inline bool net_ipv4_addr_cmp(const struct net_in_addr *addr1,
1073 const struct net_in_addr *addr2)
1074{
1075 return net_ipv4_addr_cmp_raw(addr1->s4_addr, addr2->s4_addr);
1076}
1077
1086static inline bool net_ipv6_addr_cmp(const struct net_in6_addr *addr1,
1087 const struct net_in6_addr *addr2)
1088{
1089 return !memcmp(addr1, addr2, sizeof(struct net_in6_addr));
1090}
1091
1100static inline bool net_ipv6_addr_cmp_raw(const uint8_t *addr1,
1101 const uint8_t *addr2)
1102{
1103 return net_ipv6_addr_cmp((const struct net_in6_addr *)addr1,
1104 (const struct net_in6_addr *)addr2);
1105}
1106
1108static inline bool net_ipv6_is_ll_addr_raw(const uint8_t *addr)
1109{
1110 return UNALIGNED_GET((uint16_t *)addr) == net_htons(0xFE80);
1111}
1113
1121static inline bool net_ipv6_is_ll_addr(const struct net_in6_addr *addr)
1122{
1123 return net_ipv6_is_ll_addr_raw(addr->s6_addr);
1124}
1125
1133static inline bool net_ipv6_is_sl_addr(const struct net_in6_addr *addr)
1134{
1135 return UNALIGNED_GET(&addr->s6_addr16[0]) == net_htons(0xFEC0);
1136}
1137
1138
1146static inline bool net_ipv6_is_ula_addr(const struct net_in6_addr *addr)
1147{
1148 return addr->s6_addr[0] == 0xFD;
1149}
1150
1158static inline bool net_ipv6_is_global_addr(const struct net_in6_addr *addr)
1159{
1160 return (addr->s6_addr[0] & 0xE0) == 0x20;
1161}
1162
1172static inline bool net_ipv6_is_private_addr(const struct net_in6_addr *addr)
1173{
1174 uint32_t masked_32, masked_7;
1175
1176 masked_32 = net_ntohl(UNALIGNED_GET(&addr->s6_addr32[0]));
1177 masked_7 = masked_32 & 0xfc000000;
1178
1179 return masked_32 == 0x20010db8 || /* 2001:db8::/32 */
1180 masked_7 == 0xfc000000; /* fc00::/7 */
1181}
1182
1189
1196
1203
1204struct net_if;
1205extern bool net_if_ipv4_addr_mask_cmp(struct net_if *iface,
1206 const struct net_in_addr *addr);
1207
1217static inline bool net_ipv4_addr_mask_cmp(struct net_if *iface,
1218 const struct net_in_addr *addr)
1219{
1220 return net_if_ipv4_addr_mask_cmp(iface, addr);
1221}
1222
1224extern bool net_if_ipv4_is_addr_bcast_raw(struct net_if *iface,
1225 const uint8_t *addr);
1226
1227#if defined(CONFIG_NET_NATIVE_IPV4)
1228static inline bool net_ipv4_is_addr_bcast_raw(struct net_if *iface,
1229 const uint8_t *addr)
1230{
1231 if (net_ipv4_addr_cmp_raw(addr, net_ipv4_broadcast_address()->s4_addr)) {
1232 return true;
1233 }
1234
1235 return net_if_ipv4_is_addr_bcast_raw(iface, addr);
1236}
1237#else
1238static inline bool net_ipv4_is_addr_bcast_raw(struct net_if *iface,
1239 const uint8_t *addr)
1240{
1241 ARG_UNUSED(iface);
1242 ARG_UNUSED(addr);
1243
1244 return false;
1245}
1246#endif
1248
1249extern bool net_if_ipv4_is_addr_bcast(struct net_if *iface,
1250 const struct net_in_addr *addr);
1251
1260#if defined(CONFIG_NET_NATIVE_IPV4)
1261static inline bool net_ipv4_is_addr_bcast(struct net_if *iface,
1262 const struct net_in_addr *addr)
1263{
1265 return true;
1266 }
1267
1268 return net_if_ipv4_is_addr_bcast(iface, addr);
1269}
1270#else
1271static inline bool net_ipv4_is_addr_bcast(struct net_if *iface,
1272 const struct net_in_addr *addr)
1273{
1274 ARG_UNUSED(iface);
1275 ARG_UNUSED(addr);
1276
1277 return false;
1278}
1279#endif
1280
1282extern struct net_if_addr *net_if_ipv4_addr_lookup_raw(const uint8_t *addr,
1283 struct net_if **ret);
1284
1285static inline bool net_ipv4_is_my_addr_raw(const uint8_t *addr)
1286{
1287 bool ret;
1288
1289 ret = net_if_ipv4_addr_lookup_raw(addr, NULL) != NULL;
1290 if (!ret) {
1291 ret = net_ipv4_is_addr_bcast_raw(NULL, addr);
1292 }
1293
1294 return ret;
1295}
1297
1298extern struct net_if_addr *net_if_ipv4_addr_lookup(const struct net_in_addr *addr,
1299 struct net_if **iface);
1300
1310static inline bool net_ipv4_is_my_addr(const struct net_in_addr *addr)
1311{
1312 bool ret;
1313
1314 ret = net_if_ipv4_addr_lookup(addr, NULL) != NULL;
1315 if (!ret) {
1316 ret = net_ipv4_is_addr_bcast(NULL, addr);
1317 }
1318
1319 return ret;
1320}
1321
1323static inline bool net_ipv6_is_addr_unspecified_raw(const uint8_t *addr)
1324{
1325 return UNALIGNED_GET((uint32_t *)addr) == 0 &&
1326 UNALIGNED_GET((uint32_t *)addr + 1) == 0 &&
1327 UNALIGNED_GET((uint32_t *)addr + 2) == 0 &&
1328 UNALIGNED_GET((uint32_t *)addr + 3) == 0;
1329}
1331
1339static inline bool net_ipv6_is_addr_unspecified(const struct net_in6_addr *addr)
1340{
1341 return net_ipv6_is_addr_unspecified_raw(addr->s6_addr);
1342}
1343
1345static inline bool net_ipv6_is_addr_solicited_node_raw(const uint8_t *addr)
1346{
1347 return UNALIGNED_GET((uint32_t *)addr) == net_htonl(0xff020000) &&
1348 UNALIGNED_GET((uint32_t *)addr + 1) == 0x00000000 &&
1349 UNALIGNED_GET((uint32_t *)addr + 2) == net_htonl(0x00000001) &&
1350 ((UNALIGNED_GET((uint32_t *)addr + 3) & net_htonl(0xff000000)) ==
1351 net_htonl(0xff000000));
1352}
1354
1363static inline bool net_ipv6_is_addr_solicited_node(const struct net_in6_addr *addr)
1364{
1365 return net_ipv6_is_addr_solicited_node_raw(addr->s6_addr);
1366}
1367
1369static inline bool net_ipv6_is_addr_mcast_scope_raw(const uint8_t *addr,
1370 int scope)
1371{
1372 return (addr[0] == 0xff) && ((addr[1] & 0xF) == scope);
1373}
1375
1377static inline int net_ipv6_get_addr_mcast_scope_raw(const uint8_t *addr)
1378{
1379 if (addr[0] == 0xff) {
1380 return (addr[1] & 0xF);
1381 }
1382
1383 return -1;
1384}
1386
1397static inline bool net_ipv6_is_addr_mcast_scope(const struct net_in6_addr *addr,
1398 int scope)
1399{
1400 return net_ipv6_is_addr_mcast_scope_raw(addr->s6_addr, scope);
1401}
1402
1412static inline bool net_ipv6_is_same_mcast_scope(const struct net_in6_addr *addr_1,
1413 const struct net_in6_addr *addr_2)
1414{
1415 return (addr_1->s6_addr[0] == 0xff) && (addr_2->s6_addr[0] == 0xff) &&
1416 (addr_1->s6_addr[1] == addr_2->s6_addr[1]);
1417}
1418
1426static inline int net_ipv6_get_addr_mcast_scope(const struct net_in6_addr *addr)
1427{
1428 return net_ipv6_get_addr_mcast_scope_raw(addr->s6_addr);
1429}
1430
1432static inline bool net_ipv6_is_addr_mcast_iface_raw(const uint8_t *addr)
1433{
1434 return net_ipv6_is_addr_mcast_scope_raw(addr, 0x01);
1435}
1436
1437static inline bool net_ipv6_is_addr_mcast_link_raw(const uint8_t *addr)
1438{
1439 return net_ipv6_is_addr_mcast_scope_raw(addr, 0x02);
1440}
1441
1442static inline bool net_ipv6_is_addr_mcast_mesh_raw(const uint8_t *addr)
1443{
1444 return net_ipv6_is_addr_mcast_scope_raw(addr, 0x03);
1445}
1446
1447static inline bool net_ipv6_is_addr_mcast_site_raw(const uint8_t *addr)
1448{
1449 return net_ipv6_is_addr_mcast_scope_raw(addr, 0x05);
1450}
1451
1452static inline bool net_ipv6_is_addr_mcast_org_raw(const uint8_t *addr)
1453{
1454 return net_ipv6_is_addr_mcast_scope_raw(addr, 0x08);
1455}
1457
1465static inline bool net_ipv6_is_addr_mcast_global(const struct net_in6_addr *addr)
1466{
1467 return net_ipv6_is_addr_mcast_scope(addr, 0x0e);
1468}
1469
1479static inline bool net_ipv6_is_addr_mcast_iface(const struct net_in6_addr *addr)
1480{
1481 return net_ipv6_is_addr_mcast_scope(addr, 0x01);
1482}
1483
1493static inline bool net_ipv6_is_addr_mcast_link(const struct net_in6_addr *addr)
1494{
1495 return net_ipv6_is_addr_mcast_scope(addr, 0x02);
1496}
1497
1507static inline bool net_ipv6_is_addr_mcast_mesh(const struct net_in6_addr *addr)
1508{
1509 return net_ipv6_is_addr_mcast_scope(addr, 0x03);
1510}
1511
1521static inline bool net_ipv6_is_addr_mcast_site(const struct net_in6_addr *addr)
1522{
1523 return net_ipv6_is_addr_mcast_scope(addr, 0x05);
1524}
1525
1535static inline bool net_ipv6_is_addr_mcast_org(const struct net_in6_addr *addr)
1536{
1537 return net_ipv6_is_addr_mcast_scope(addr, 0x08);
1538}
1539
1541static inline bool net_ipv6_is_addr_mcast_group_raw(const uint8_t *addr,
1542 const uint8_t *group)
1543{
1544 return UNALIGNED_GET((uint16_t *)addr + 1) == UNALIGNED_GET((uint16_t *)group + 1) &&
1545 UNALIGNED_GET((uint32_t *)addr + 1) == UNALIGNED_GET((uint32_t *)group + 1) &&
1546 UNALIGNED_GET((uint32_t *)addr + 2) == UNALIGNED_GET((uint32_t *)group + 2) &&
1547 UNALIGNED_GET((uint32_t *)addr + 3) == UNALIGNED_GET((uint32_t *)group + 3);
1548}
1550
1561static inline bool net_ipv6_is_addr_mcast_group(const struct net_in6_addr *addr,
1562 const struct net_in6_addr *group)
1563{
1564 return net_ipv6_is_addr_mcast_group_raw(addr->s6_addr, group->s6_addr);
1565}
1566
1568static inline bool net_ipv6_is_addr_mcast_all_nodes_group_raw(const uint8_t *addr)
1569{
1570 static const uint8_t all_nodes_mcast_group[NET_IPV6_ADDR_SIZE] = {
1571 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1572 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1573 };
1574
1575 return net_ipv6_is_addr_mcast_group_raw(addr, all_nodes_mcast_group);
1576}
1578
1587static inline bool
1589{
1590 return net_ipv6_is_addr_mcast_all_nodes_group_raw(addr->s6_addr);
1591}
1592
1602static inline bool
1608
1610static inline bool net_ipv6_is_addr_mcast_link_all_nodes_raw(const uint8_t *addr)
1611{
1612 return net_ipv6_is_addr_mcast_link_raw(addr) &&
1613 net_ipv6_is_addr_mcast_all_nodes_group_raw(addr);
1614}
1616
1626static inline bool
1632
1640static inline
1642 struct net_in6_addr *dst)
1643{
1644 dst->s6_addr[0] = 0xFF;
1645 dst->s6_addr[1] = 0x02;
1646 UNALIGNED_PUT(0, &dst->s6_addr16[1]);
1647 UNALIGNED_PUT(0, &dst->s6_addr16[2]);
1648 UNALIGNED_PUT(0, &dst->s6_addr16[3]);
1649 UNALIGNED_PUT(0, &dst->s6_addr16[4]);
1650 dst->s6_addr[10] = 0U;
1651 dst->s6_addr[11] = 0x01;
1652 dst->s6_addr[12] = 0xFF;
1653 dst->s6_addr[13] = src->s6_addr[13];
1654 UNALIGNED_PUT(UNALIGNED_GET(&src->s6_addr16[7]), &dst->s6_addr16[7]);
1655}
1656
1669static inline void net_ipv6_addr_create(struct net_in6_addr *addr,
1670 uint16_t addr0, uint16_t addr1,
1671 uint16_t addr2, uint16_t addr3,
1672 uint16_t addr4, uint16_t addr5,
1673 uint16_t addr6, uint16_t addr7)
1674{
1675 UNALIGNED_PUT(net_htons(addr0), &addr->s6_addr16[0]);
1676 UNALIGNED_PUT(net_htons(addr1), &addr->s6_addr16[1]);
1677 UNALIGNED_PUT(net_htons(addr2), &addr->s6_addr16[2]);
1678 UNALIGNED_PUT(net_htons(addr3), &addr->s6_addr16[3]);
1679 UNALIGNED_PUT(net_htons(addr4), &addr->s6_addr16[4]);
1680 UNALIGNED_PUT(net_htons(addr5), &addr->s6_addr16[5]);
1681 UNALIGNED_PUT(net_htons(addr6), &addr->s6_addr16[6]);
1682 UNALIGNED_PUT(net_htons(addr7), &addr->s6_addr16[7]);
1683}
1684
1691{
1692 net_ipv6_addr_create(addr, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001);
1693}
1694
1701{
1702 net_ipv6_addr_create(addr, 0xff02, 0, 0, 0, 0, 0, 0, 0x0002);
1703}
1704
1711static inline void net_ipv6_addr_create_v4_mapped(const struct net_in_addr *addr4,
1712 struct net_in6_addr *addr6)
1713{
1714 net_ipv6_addr_create(addr6, 0, 0, 0, 0, 0, 0xffff,
1715 net_ntohs(addr4->s4_addr16[0]),
1716 net_ntohs(addr4->s4_addr16[1]));
1717}
1718
1727static inline bool net_ipv6_addr_is_v4_mapped(const struct net_in6_addr *addr)
1728{
1729 if (UNALIGNED_GET(&addr->s6_addr32[0]) == 0 &&
1730 UNALIGNED_GET(&addr->s6_addr32[1]) == 0 &&
1731 UNALIGNED_GET(&addr->s6_addr16[5]) == 0xffff) {
1732 return true;
1733 }
1734
1735 return false;
1736}
1737
1757 const struct net_in6_addr *prefix,
1758 uint8_t *network_id, size_t network_id_len,
1759 uint8_t dad_counter,
1760 struct net_in6_addr *addr,
1761 struct net_linkaddr *lladdr);
1762
1769static inline void net_ipv6_addr_create_iid(struct net_in6_addr *addr,
1770 struct net_linkaddr *lladdr)
1771{
1772 (void)net_ipv6_addr_generate_iid(NULL, NULL, NULL, 0, 0, addr, lladdr);
1773}
1774
1776static inline bool net_ipv6_addr_based_on_ll_raw(const uint8_t *addr,
1777 const struct net_linkaddr *lladdr)
1778{
1779 if (addr == NULL || lladdr == NULL) {
1780 return false;
1781 }
1782
1783 switch (lladdr->len) {
1784 case 2:
1785 if (!memcmp(&addr[14], lladdr->addr, lladdr->len) &&
1786 addr[8] == 0U &&
1787 addr[9] == 0U &&
1788 addr[10] == 0U &&
1789 addr[11] == 0xff &&
1790 addr[12] == 0xfe) {
1791 return true;
1792 }
1793
1794 break;
1795 case 6:
1796 if (lladdr->type == NET_LINK_ETHERNET) {
1797 if (!memcmp(&addr[9], &lladdr->addr[1], 2) &&
1798 !memcmp(&addr[13], &lladdr->addr[3], 3) &&
1799 addr[11] == 0xff &&
1800 addr[12] == 0xfe &&
1801 (addr[8] ^ 0x02) == lladdr->addr[0]) {
1802 return true;
1803 }
1804 }
1805
1806 break;
1807 case 8:
1808 if (sizeof(lladdr->addr) < 8) {
1809 return false;
1810 }
1811
1812 if (!memcmp(&addr[9], &lladdr->addr[1],
1813 lladdr->len - 1) &&
1814 (addr[8] ^ 0x02) == lladdr->addr[0]) {
1815 return true;
1816 }
1817
1818 break;
1819 default:
1820 return false;
1821 }
1822
1823 return false;
1824}
1826
1832static inline bool net_ipv6_addr_based_on_ll(const struct net_in6_addr *addr,
1833 const struct net_linkaddr *lladdr)
1834{
1835 if (addr == NULL || lladdr == NULL) {
1836 return false;
1837 }
1838
1839 return net_ipv6_addr_based_on_ll_raw(addr->s6_addr, lladdr);
1840}
1841
1850static inline struct net_sockaddr *net_sad(const struct net_sockaddr_storage *addr)
1851{
1852 return (struct net_sockaddr *)addr;
1853}
1854
1863static inline struct net_sockaddr_in6 *net_sin6(const struct net_sockaddr *addr)
1864{
1865 return (struct net_sockaddr_in6 *)addr;
1866}
1867
1876static inline struct net_sockaddr_in *net_sin(const struct net_sockaddr *addr)
1877{
1878 return (struct net_sockaddr_in *)addr;
1879}
1880
1889static inline
1890struct net_sockaddr_in6_ptr *net_sin6_ptr(const struct net_sockaddr_ptr *addr)
1891{
1892 return (struct net_sockaddr_in6_ptr *)addr;
1893}
1894
1903static inline
1904struct net_sockaddr_in_ptr *net_sin_ptr(const struct net_sockaddr_ptr *addr)
1905{
1906 return (struct net_sockaddr_in_ptr *)addr;
1907}
1908
1917static inline
1918struct net_sockaddr_ll_ptr *net_sll_ptr(const struct net_sockaddr_ptr *addr)
1919{
1920 return (struct net_sockaddr_ll_ptr *)addr;
1921}
1922
1931static inline
1932struct net_sockaddr_can_ptr *net_can_ptr(const struct net_sockaddr_ptr *addr)
1933{
1934 return (struct net_sockaddr_can_ptr *)addr;
1935}
1936
1950__syscall int net_addr_pton(net_sa_family_t family, const char *src, void *dst);
1951
1963__syscall char *net_addr_ntop(net_sa_family_t family, const void *src,
1964 char *dst, size_t size);
1965
1977 struct net_sockaddr *mask);
1978
1990 uint8_t *mask_len);
1991
2013bool net_ipaddr_parse(const char *str, size_t str_len,
2014 struct net_sockaddr *addr);
2015
2043const char *net_ipaddr_parse_mask(const char *str, size_t str_len,
2044 struct net_sockaddr *addr, uint8_t *mask_len);
2045
2055int net_port_set_default(struct net_sockaddr *addr, uint16_t default_port);
2056
2068static inline int32_t net_tcp_seq_cmp(uint32_t seq1, uint32_t seq2)
2069{
2070 return (int32_t)(seq1 - seq2);
2071}
2072
2083static inline bool net_tcp_seq_greater(uint32_t seq1, uint32_t seq2)
2084{
2085 return net_tcp_seq_cmp(seq1, seq2) > 0;
2086}
2087
2099int net_bytes_from_str(uint8_t *buf, int buf_len, const char *src);
2100
2110
2120
2129static inline enum net_priority net_vlan2priority(uint8_t priority)
2130{
2131 /* Map according to IEEE 802.1Q */
2132 static const uint8_t vlan2priority[] = {
2141 };
2142
2143 if (priority >= ARRAY_SIZE(vlan2priority)) {
2144 /* Use Best Effort as the default priority */
2145 return NET_PRIORITY_BE;
2146 }
2147
2148 return (enum net_priority)vlan2priority[priority];
2149}
2150
2158static inline uint8_t net_priority2vlan(enum net_priority priority)
2159{
2160 /* The conversion works both ways */
2161 return (uint8_t)net_vlan2priority(priority);
2162}
2163
2173
2184#if defined(CONFIG_NET_IPV6_PE)
2185int net_ipv6_pe_add_filter(struct net_in6_addr *addr, bool is_denylist);
2186#else
2187static inline int net_ipv6_pe_add_filter(struct net_in6_addr *addr,
2188 bool is_denylist)
2189{
2190 ARG_UNUSED(addr);
2191 ARG_UNUSED(is_denylist);
2192
2193 return -ENOTSUP;
2194}
2195#endif /* CONFIG_NET_IPV6_PE */
2196
2204#if defined(CONFIG_NET_IPV6_PE)
2205int net_ipv6_pe_del_filter(struct net_in6_addr *addr);
2206#else
2207static inline int net_ipv6_pe_del_filter(struct net_in6_addr *addr)
2208{
2209 ARG_UNUSED(addr);
2210
2211 return -ENOTSUP;
2212}
2213#endif /* CONFIG_NET_IPV6_PE */
2214
2215#ifdef __cplusplus
2216}
2217#endif
2218
2219#include <zephyr/syscalls/net_ip.h>
2220
2221#if defined(CONFIG_NET_NAMESPACE_COMPAT_MODE)
2222#define ZEPHYR_INCLUDE_NET_COMPAT_MODE_SYMBOLS
2223#include <zephyr/net/net_compat.h>
2224#undef ZEPHYR_INCLUDE_NET_COMPAT_MODE_SYMBOLS
2225#endif /* CONFIG_NET_NAMESPACE_COMPAT_MODE */
2226
2230
2231#endif /* ZEPHYR_INCLUDE_NET_NET_IP_H_ */
static struct net_sockaddr_can_ptr * net_can_ptr(const struct net_sockaddr_ptr *addr)
Get net_sockaddr_can_ptr from net_sockaddr_ptr.
Definition net_ip.h:1932
static bool net_ipv6_is_ula_addr(const struct net_in6_addr *addr)
Check if the given IPv6 address is a unique local address.
Definition net_ip.h:1146
#define NET_IPV4_ADDR_SIZE
Binary size of the IPv4 address.
Definition net_ip.h:165
static enum net_priority net_vlan2priority(uint8_t priority)
Convert network packet VLAN priority to network packet priority so we can place the packet into corre...
Definition net_ip.h:2129
static int32_t net_tcp_seq_cmp(uint32_t seq1, uint32_t seq2)
Compare TCP sequence numbers.
Definition net_ip.h:2068
static bool net_ipv6_is_my_maddr(struct net_in6_addr *maddr)
Check if IPv6 multicast address is found in one of the network interfaces.
Definition net_ip.h:835
const struct net_in_addr * net_ipv4_broadcast_address(void)
Return pointer to broadcast (all bits ones) IPv4 address.
#define NET_IPV6_ADDR_SIZE
Binary size of the IPv6 address.
Definition net_ip.h:152
static bool net_ipv4_addr_cmp(const struct net_in_addr *addr1, const struct net_in_addr *addr2)
Compare two IPv4 addresses.
Definition net_ip.h:1072
static bool net_ipv6_addr_is_v4_mapped(const struct net_in6_addr *addr)
Is the IPv6 address an IPv4 mapped one.
Definition net_ip.h:1727
static bool net_ipv6_is_addr_unspecified(const struct net_in6_addr *addr)
Check if the IPv6 address is unspecified (all bits zero)
Definition net_ip.h:1339
static bool net_ipv6_is_addr_mcast_mesh(const struct net_in6_addr *addr)
Check if the IPv6 address is a mesh-local scope multicast address (FFx3::).
Definition net_ip.h:1507
static void net_ipv6_addr_create_v4_mapped(const struct net_in_addr *addr4, struct net_in6_addr *addr6)
Create IPv4 mapped IPv6 address.
Definition net_ip.h:1711
static bool net_ipv4_is_addr_mcast(const struct net_in_addr *addr)
Check if the IPv4 address is a multicast address.
Definition net_ip.h:963
net_addr_state
What is the current state of the network address.
Definition net_ip.h:536
static bool net_ipv4_addr_cmp_raw(const uint8_t *addr1, const uint8_t *addr2)
Compare two raw IPv4 address buffers.
Definition net_ip.h:1058
uint32_t net_socklen_t
Length of a socket address.
Definition net_ip.h:171
static bool net_ipv6_is_private_addr(const struct net_in6_addr *addr)
Check if the given IPv6 address is from a private/local address range.
Definition net_ip.h:1172
static bool net_ipv4_is_addr_unspecified(const struct net_in_addr *addr)
Check if the IPv4 address is unspecified (all bits zero)
Definition net_ip.h:944
static bool net_ipv6_is_addr_mcast(const struct net_in6_addr *addr)
Check if the IPv6 address is a multicast address.
Definition net_ip.h:791
struct net_if_addr * net_if_ipv6_addr_lookup(const struct net_in6_addr *addr, struct net_if **iface)
static bool net_ipv6_is_addr_mcast_iface(const struct net_in6_addr *addr)
Check if the IPv6 address is a interface scope multicast address (FFx1::).
Definition net_ip.h:1479
bool net_if_ipv4_is_addr_bcast(struct net_if *iface, const struct net_in_addr *addr)
int net_port_set_default(struct net_sockaddr *addr, uint16_t default_port)
Set the default port in the sockaddr structure.
#define net_ntohs(x)
Convert 16-bit value from network to host byte order.
Definition net_ip.h:100
static void net_ipv6_addr_copy_raw(uint8_t *dest, const uint8_t *src)
Copy an IPv6 address raw buffer.
Definition net_ip.h:1044
static bool net_ipv4_addr_mask_cmp(struct net_if *iface, const struct net_in_addr *addr)
Check if the given address belongs to same subnet that has been configured for the interface.
Definition net_ip.h:1217
const char * net_family2str(net_sa_family_t family)
Return network address family value as a string.
static void net_ipv6_addr_prefix_mask(const uint8_t *inaddr, uint8_t *outaddr, uint8_t prefix_len)
Get the IPv6 network address via the unicast address and the prefix mask.
Definition net_ip.h:887
struct net_if_addr * net_if_ipv4_addr_lookup(const struct net_in_addr *addr, struct net_if **iface)
int net_ipv6_addr_generate_iid(struct net_if *iface, const struct net_in6_addr *prefix, uint8_t *network_id, size_t network_id_len, uint8_t dad_counter, struct net_in6_addr *addr, struct net_linkaddr *lladdr)
Generate IPv6 address using a prefix and interface identifier.
static struct net_sockaddr * net_sad(const struct net_sockaddr_storage *addr)
Get net_sockaddr from net_sockaddr_storage.
Definition net_ip.h:1850
#define net_htonl(x)
Convert 32-bit value from host to network byte order.
Definition net_ip.h:132
const char * net_ipaddr_parse_mask(const char *str, size_t str_len, struct net_sockaddr *addr, uint8_t *mask_len)
Parse a string that contains either IPv4 or IPv6 address and optional mask len, and store the informa...
static struct net_sockaddr_in6 * net_sin6(const struct net_sockaddr *addr)
Get net_sockaddr_in6 from net_sockaddr.
Definition net_ip.h:1863
static struct net_sockaddr_in_ptr * net_sin_ptr(const struct net_sockaddr_ptr *addr)
Get net_sockaddr_in_ptr from sockaddr_ptr.
Definition net_ip.h:1904
static bool net_ipv4_is_my_addr(const struct net_in_addr *addr)
Check if the IPv4 address is assigned to any network interface in the system.
Definition net_ip.h:1310
static bool net_ipv4_is_addr_loopback(const struct net_in_addr *addr)
Check if the IPv4 address is a loopback address (127.0.0.0/8).
Definition net_ip.h:925
int net_addr_pton(net_sa_family_t family, const char *src, void *dst)
Convert a string to IP address.
int net_mask_len_to_netmask(net_sa_family_t family, uint8_t mask_len, struct net_sockaddr *mask)
Create netmask from mask length.
static bool net_ipv4_is_ll_addr(const struct net_in_addr *addr)
Check if the given IPv4 address is a link local address.
Definition net_ip.h:982
static bool net_ipv6_addr_cmp(const struct net_in6_addr *addr1, const struct net_in6_addr *addr2)
Compare two IPv6 addresses.
Definition net_ip.h:1086
static void net_ipv6_addr_create_ll_allnodes_mcast(struct net_in6_addr *addr)
Create link local allnodes multicast IPv6 address.
Definition net_ip.h:1690
net_ip_protocol_secure
Protocol numbers for TLS protocols.
Definition net_ip.h:78
#define net_ipaddr_copy(dest, src)
Copy an IPv4 or IPv6 address.
Definition net_ip.h:1023
static bool net_ipv6_addr_based_on_ll(const struct net_in6_addr *addr, const struct net_linkaddr *lladdr)
Check if given address is based on link layer address.
Definition net_ip.h:1832
net_ip_mtu
IP Maximum Transfer Unit.
Definition net_ip.h:492
int net_rx_priority2tc(enum net_priority prio)
Convert Rx network packet priority to traffic class so we can place the packet into correct Rx queue.
static int net_ipv6_get_addr_mcast_scope(const struct net_in6_addr *addr)
Returns the scope of the given IPv6 address.
Definition net_ip.h:1426
int net_netmask_to_mask_len(net_sa_family_t family, struct net_sockaddr *mask, uint8_t *mask_len)
Create mask length from netmask.
static void net_ipv6_addr_create_iid(struct net_in6_addr *addr, struct net_linkaddr *lladdr)
Create IPv6 address interface identifier.
Definition net_ip.h:1769
static struct net_sockaddr_in * net_sin(const struct net_sockaddr *addr)
Get net_sockaddr_in from net_sockaddr.
Definition net_ip.h:1876
unsigned short int net_sa_family_t
Socket address family type.
Definition net_ip.h:168
int net_bytes_from_str(uint8_t *buf, int buf_len, const char *src)
Convert a string of hex values to array of bytes.
static uint8_t net_priority2vlan(enum net_priority priority)
Convert network packet priority to network packet VLAN priority.
Definition net_ip.h:2158
static bool net_ipv6_is_addr_mcast_site(const struct net_in6_addr *addr)
Check if the IPv6 address is a site scope multicast address (FFx5::).
Definition net_ip.h:1521
bool net_ipaddr_parse(const char *str, size_t str_len, struct net_sockaddr *addr)
Parse a string that contains either IPv4 or IPv6 address and optional port, and store the information...
bool net_if_ipv4_addr_mask_cmp(struct net_if *iface, const struct net_in_addr *addr)
static bool net_ipv6_is_addr_mcast_scope(const struct net_in6_addr *addr, int scope)
Check if the IPv6 address is a given scope multicast address (FFyx::).
Definition net_ip.h:1397
#define SOCKADDR_ALIGN
Definition net_ip.h:405
static bool net_ipv6_is_my_addr(struct net_in6_addr *addr)
Check if IPv6 address is found in one of the network interfaces.
Definition net_ip.h:819
static bool net_ipv6_is_prefix(const uint8_t *addr1, const uint8_t *addr2, uint8_t length)
Check if two IPv6 addresses are same when compared after prefix mask.
Definition net_ip.h:849
static bool net_ipv6_is_addr_loopback(const struct net_in6_addr *addr)
Check if the IPv6 address is a loopback address (::1).
Definition net_ip.h:772
static bool net_ipv6_is_addr_mcast_org(const struct net_in6_addr *addr)
Check if the IPv6 address is an organization scope multicast address (FFx8::).
Definition net_ip.h:1535
static int net_ipv6_pe_add_filter(struct net_in6_addr *addr, bool is_denylist)
Add IPv6 prefix as a privacy extension filter.
Definition net_ip.h:2187
static bool net_ipv6_is_addr_mcast_group(const struct net_in6_addr *addr, const struct net_in6_addr *group)
Check if the IPv6 address belongs to certain multicast group.
Definition net_ip.h:1561
static bool net_tcp_seq_greater(uint32_t seq1, uint32_t seq2)
Check that one TCP sequence number is greater.
Definition net_ip.h:2083
net_sock_type
Socket type.
Definition net_ip.h:88
static bool net_ipv6_is_addr_solicited_node(const struct net_in6_addr *addr)
Check if the IPv6 address is solicited node multicast address FF02:0:0:0:0:1:FFXX:XXXX defined in RFC...
Definition net_ip.h:1363
static void net_ipv6_addr_create(struct net_in6_addr *addr, uint16_t addr0, uint16_t addr1, uint16_t addr2, uint16_t addr3, uint16_t addr4, uint16_t addr5, uint16_t addr6, uint16_t addr7)
Construct an IPv6 address from eight 16-bit words.
Definition net_ip.h:1669
static bool net_ipv6_is_global_addr(const struct net_in6_addr *addr)
Check if the given IPv6 address is a global address.
Definition net_ip.h:1158
static struct net_sockaddr_ll_ptr * net_sll_ptr(const struct net_sockaddr_ptr *addr)
Get net_sockaddr_ll_ptr from sockaddr_ptr.
Definition net_ip.h:1918
static bool net_ipv6_is_addr_mcast_link_all_nodes(const struct net_in6_addr *addr)
Check if the IPv6 address is a link local scope all nodes multicast address (FF02::1).
Definition net_ip.h:1627
const struct net_in_addr * net_ipv4_unspecified_address(void)
Return pointer to any (all bits zeros) IPv4 address.
static struct net_sockaddr_in6_ptr * net_sin6_ptr(const struct net_sockaddr_ptr *addr)
Get net_sockaddr_in6_ptr from net_sockaddr_ptr.
Definition net_ip.h:1890
static bool net_ipv6_is_addr_mcast_all_nodes_group(const struct net_in6_addr *addr)
Check if the IPv6 address belongs to the all nodes multicast group.
Definition net_ip.h:1588
static bool net_ipv6_is_same_mcast_scope(const struct net_in6_addr *addr_1, const struct net_in6_addr *addr_2)
Check if the IPv6 addresses have the same multicast scope (FFyx::).
Definition net_ip.h:1412
static int net_ipv6_pe_del_filter(struct net_in6_addr *addr)
Delete IPv6 prefix from privacy extension filter list.
Definition net_ip.h:2207
static bool net_ipv6_is_ll_addr(const struct net_in6_addr *addr)
Check if the given IPv6 address is a link local address.
Definition net_ip.h:1121
struct net_if_mcast_addr * net_if_ipv6_maddr_lookup(const struct net_in6_addr *addr, struct net_if **iface)
static bool net_ipv4_is_private_addr(const struct net_in_addr *addr)
Check if the given IPv4 address is from a private address range.
Definition net_ip.h:996
static void net_ipv6_addr_create_solicited_node(const struct net_in6_addr *src, struct net_in6_addr *dst)
Create solicited node IPv6 multicast address FF02:0:0:0:0:1:FFXX:XXXX defined in RFC 3513.
Definition net_ip.h:1641
static bool net_ipv6_is_addr_mcast_link(const struct net_in6_addr *addr)
Check if the IPv6 address is a link local scope multicast address (FFx2::).
Definition net_ip.h:1493
#define net_ntohl(x)
Convert 32-bit value from network to host byte order.
Definition net_ip.h:108
#define net_htons(x)
Convert 16-bit value from host to network byte order.
Definition net_ip.h:124
static void net_ipv6_addr_create_ll_allrouters_mcast(struct net_in6_addr *addr)
Create link local allrouters multicast IPv6 address.
Definition net_ip.h:1700
int net_tx_priority2tc(enum net_priority prio)
Convert Tx network packet priority to traffic class so we can place the packet into correct Tx queue.
net_priority
Network packet priority settings described in IEEE 802.1Q Annex I.1.
Definition net_ip.h:513
static bool net_ipv6_is_addr_mcast_iface_all_nodes(const struct net_in6_addr *addr)
Check if the IPv6 address is a interface scope all nodes multicast address (FF01::1).
Definition net_ip.h:1603
static bool net_ipv6_is_sl_addr(const struct net_in6_addr *addr)
Check if the given IPv6 address is a site local address.
Definition net_ip.h:1133
net_ip_protocol
Protocol numbers from IANA/BSD.
Definition net_ip.h:64
const struct net_in6_addr * net_ipv6_unspecified_address(void)
Return pointer to any (all bits zeros) IPv6 address.
static void net_ipv4_addr_copy_raw(uint8_t *dest, const uint8_t *src)
Copy an IPv4 address raw buffer.
Definition net_ip.h:1032
static bool net_ipv6_is_addr_mcast_global(const struct net_in6_addr *addr)
Check if the IPv6 address is a global multicast address (FFxE::/16).
Definition net_ip.h:1465
char * net_addr_ntop(net_sa_family_t family, const void *src, char *dst, size_t size)
Convert IP address to string form.
static bool net_ipv6_addr_cmp_raw(const uint8_t *addr1, const uint8_t *addr2)
Compare two raw IPv6 address buffers.
Definition net_ip.h:1100
net_addr_type
How the network address is assigned to network interface.
Definition net_ip.h:544
static bool net_ipv4_is_addr_bcast(struct net_if *iface, const struct net_in_addr *addr)
Check if the given IPv4 address is a broadcast address.
Definition net_ip.h:1271
@ NET_ADDR_ANY_STATE
Default (invalid) address type.
Definition net_ip.h:537
@ NET_ADDR_TENTATIVE
Tentative address.
Definition net_ip.h:538
@ NET_ADDR_DEPRECATED
Deprecated address.
Definition net_ip.h:540
@ NET_ADDR_PREFERRED
Preferred address.
Definition net_ip.h:539
@ NET_IPPROTO_TLS_1_2
TLS 1.2 protocol.
Definition net_ip.h:81
@ NET_IPPROTO_TLS_1_1
TLS 1.1 protocol.
Definition net_ip.h:80
@ NET_IPPROTO_TLS_1_0
TLS 1.0 protocol.
Definition net_ip.h:79
@ NET_IPPROTO_DTLS_1_2
DTLS 1.2 protocol.
Definition net_ip.h:84
@ NET_IPPROTO_TLS_1_3
TLS 1.3 protocol.
Definition net_ip.h:82
@ NET_IPPROTO_DTLS_1_0
DTLS 1.0 protocol.
Definition net_ip.h:83
@ NET_IPV4_MTU
IPv4 MTU length.
Definition net_ip.h:508
@ NET_IPV6_MTU
IPv6 MTU length.
Definition net_ip.h:499
@ NET_SOCK_STREAM
Stream socket type.
Definition net_ip.h:89
@ NET_SOCK_RAW
RAW socket type.
Definition net_ip.h:91
@ NET_SOCK_DGRAM
Datagram socket type.
Definition net_ip.h:90
@ NET_PRIORITY_NC
Network control (highest)
Definition net_ip.h:521
@ NET_PRIORITY_IC
Internetwork control.
Definition net_ip.h:520
@ NET_PRIORITY_CA
Critical applications.
Definition net_ip.h:517
@ NET_PRIORITY_VO
Voice, < 10 ms latency and jitter.
Definition net_ip.h:519
@ NET_PRIORITY_VI
Video, < 100 ms latency and jitter.
Definition net_ip.h:518
@ NET_PRIORITY_BE
Best effort (default)
Definition net_ip.h:515
@ NET_PRIORITY_EE
Excellent effort.
Definition net_ip.h:516
@ NET_PRIORITY_BK
Background (lowest)
Definition net_ip.h:514
@ NET_IPPROTO_TCP
TCP protocol.
Definition net_ip.h:70
@ NET_IPPROTO_ICMP
ICMP protocol.
Definition net_ip.h:66
@ NET_IPPROTO_ICMPV6
ICMPv6 protocol.
Definition net_ip.h:73
@ NET_IPPROTO_UDP
UDP protocol.
Definition net_ip.h:71
@ NET_IPPROTO_IP
IP protocol (pseudo-val for setsockopt()
Definition net_ip.h:65
@ NET_IPPROTO_RAW
RAW IP packets.
Definition net_ip.h:74
@ NET_IPPROTO_IGMP
IGMP protocol.
Definition net_ip.h:67
@ NET_IPPROTO_IPV6
IPv6 protocol.
Definition net_ip.h:72
@ NET_IPPROTO_IPIP
IPIP tunnels.
Definition net_ip.h:69
@ NET_IPPROTO_ETH_P_ALL
Every packet.
Definition net_ip.h:68
@ NET_ADDR_ANY
Default value.
Definition net_ip.h:546
@ NET_ADDR_OVERRIDABLE
Manually set address which is overridable by DHCP.
Definition net_ip.h:554
@ NET_ADDR_DHCP
Address is from DHCP.
Definition net_ip.h:550
@ NET_ADDR_MANUAL
Manually set address.
Definition net_ip.h:552
@ NET_ADDR_AUTOCONF
Auto configured address.
Definition net_ip.h:548
@ NET_LINK_ETHERNET
Ethernet link address.
Definition net_linkaddr.h:55
#define ARRAY_SIZE(array)
Number of elements in the given array.
Definition util.h:118
#define ENOTSUP
Unsupported value.
Definition errno.h:114
#define NULL
Definition iar_missing_defs.h:20
#define in_addr
Definition in.h:20
#define in6_addr
Definition in.h:21
Network namespace compatibility mode header.
Public API for network link address.
flags
Definition parser.h:97
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
void * memset(void *buf, int c, size_t n)
int memcmp(const void *m1, const void *m2, size_t n)
void * memcpy(void *ZRESTRICT d, const void *ZRESTRICT s, size_t n)
Group structure.
Definition grp.h:18
Control message ancillary data.
Definition net_ip.h:265
net_socklen_t cmsg_len
Number of bytes, including header.
Definition net_ip.h:266
int cmsg_level
Originating protocol.
Definition net_ip.h:267
int cmsg_type
Protocol-specific type.
Definition net_ip.h:268
z_max_align_t cmsg_data[]
Flexible array member to force alignment of net_cmsghdr.
Definition net_ip.h:269
Network Interface unicast IP addresses.
Definition net_if.h:56
IP and other configuration related data for network interface.
Definition net_if.h:586
Network Interface multicast IP addresses.
Definition net_if.h:161
Network Interface structure.
Definition net_if.h:726
Interface description structure.
Definition net_ip.h:699
char ifr_name[NET_IFNAMSIZ]
Network interface name.
Definition net_ip.h:700
IPv6 address struct.
Definition net_ip.h:143
uint16_t s6_addr16[8]
In big endian.
Definition net_ip.h:146
uint32_t s6_addr32[4]
In big endian.
Definition net_ip.h:147
uint8_t s6_addr[16]
IPv6 address buffer.
Definition net_ip.h:145
Incoming IPv6 packet information.
Definition net_ip.h:750
unsigned int ipi6_ifindex
Receive interface index.
Definition net_ip.h:752
struct net_in6_addr ipi6_addr
Destination IPv6 address.
Definition net_ip.h:751
IPv4 address struct.
Definition net_ip.h:155
uint16_t s4_addr16[2]
In big endian.
Definition net_ip.h:158
uint32_t s4_addr32[1]
In big endian.
Definition net_ip.h:159
uint32_t s_addr
In big endian, for POSIX compatibility.
Definition net_ip.h:160
uint8_t s4_addr[4]
IPv4 address buffer.
Definition net_ip.h:157
Incoming IPv4 packet information.
Definition net_ip.h:710
unsigned int ipi_ifindex
Network interface index.
Definition net_ip.h:711
struct net_in_addr ipi_addr
Header Destination address.
Definition net_ip.h:713
struct net_in_addr ipi_spec_dst
Local address.
Definition net_ip.h:712
IO vector array element.
Definition net_ip.h:247
void * iov_base
Pointer to data.
Definition net_ip.h:248
size_t iov_len
Length of the data.
Definition net_ip.h:249
Struct used when setting a IPv4 multicast network interface.
Definition net_ip.h:728
struct net_in_addr imr_multiaddr
IP multicast group address.
Definition net_ip.h:729
struct net_in_addr imr_interface
IP address of local interface.
Definition net_ip.h:730
Struct used when joining or leaving a IPv4 multicast group.
Definition net_ip.h:719
int imr_ifindex
Network interface index.
Definition net_ip.h:722
struct net_in_addr imr_address
IP address of local interface.
Definition net_ip.h:721
struct net_in_addr imr_multiaddr
IP multicast group address.
Definition net_ip.h:720
Struct used when joining or leaving a IPv6 multicast group.
Definition net_ip.h:736
struct net_in6_addr ipv6mr_multiaddr
IPv6 multicast address of group.
Definition net_ip.h:738
int ipv6mr_ifindex
Network interface index of the local IPv6 address.
Definition net_ip.h:741
Hardware link address structure.
Definition net_linkaddr.h:72
uint8_t addr[6]
The array of bytes representing the address.
Definition net_linkaddr.h:80
uint8_t type
What kind of address is this for.
Definition net_linkaddr.h:74
uint8_t len
The real length of the ll address.
Definition net_linkaddr.h:77
Message struct.
Definition net_ip.h:254
int msg_flags
Flags on received message.
Definition net_ip.h:261
struct net_iovec * msg_iov
Scatter/gather array.
Definition net_ip.h:257
net_socklen_t msg_namelen
Size of socket address.
Definition net_ip.h:256
size_t msg_controllen
Ancillary data buffer len.
Definition net_ip.h:260
void * msg_control
Ancillary data.
Definition net_ip.h:259
void * msg_name
Optional socket address, big endian.
Definition net_ip.h:255
size_t msg_iovlen
Number of elements in msg_iov.
Definition net_ip.h:258
Socket address struct for IPv6.
Definition net_ip.h:179
struct net_in6_addr sin6_addr
IPv6 address.
Definition net_ip.h:182
net_sa_family_t sin6_family
NET_AF_INET6.
Definition net_ip.h:180
uint8_t sin6_scope_id
Interfaces for a scope.
Definition net_ip.h:183
uint16_t sin6_port
Port number.
Definition net_ip.h:181
Socket address struct for IPv4.
Definition net_ip.h:187
uint16_t sin_port
Port number.
Definition net_ip.h:189
struct net_in_addr sin_addr
IPv4 address.
Definition net_ip.h:190
net_sa_family_t sin_family
NET_AF_INET.
Definition net_ip.h:188
Socket address struct for packet socket.
Definition net_ip.h:194
uint8_t sll_pkttype
Packet type.
Definition net_ip.h:199
int sll_ifindex
Interface number.
Definition net_ip.h:197
net_sa_family_t sll_family
Always NET_AF_PACKET.
Definition net_ip.h:195
uint16_t sll_hatype
ARP hardware type.
Definition net_ip.h:198
uint8_t sll_halen
Length of address.
Definition net_ip.h:200
uint16_t sll_protocol
Physical-layer protocol.
Definition net_ip.h:196
uint8_t sll_addr[8]
Physical-layer address, big endian.
Definition net_ip.h:201
Generic sockaddr struct.
Definition net_ip.h:408
net_sa_family_t sa_family
Address family.
Definition net_ip.h:409
IPv6/IPv4 network connection tuple.
Definition net_ip.h:527
struct net_addr * remote_addr
IPv6/IPv4 remote address.
Definition net_ip.h:528
uint16_t local_port
UDP/TCP local port.
Definition net_ip.h:531
enum net_ip_protocol ip_proto
IP protocol.
Definition net_ip.h:532
uint16_t remote_port
UDP/TCP remote port.
Definition net_ip.h:530
struct net_addr * local_addr
IPv6/IPv4 local address.
Definition net_ip.h:529
Byte order helpers.
Misc utilities.
Macros to abstract toolchain specific capabilities.