Zephyr Project API 4.3.2
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 PF_UNSPEC 0
45#define PF_INET 1
46#define PF_INET6 2
47#define PF_PACKET 3
48#define PF_CAN 4
49#define PF_NET_MGMT 5
50#define PF_LOCAL 6
51#define PF_UNIX PF_LOCAL
52
53/* Address families. */
54#define AF_UNSPEC PF_UNSPEC
55#define AF_INET PF_INET
56#define AF_INET6 PF_INET6
57#define AF_PACKET PF_PACKET
58#define AF_CAN PF_CAN
59#define AF_NET_MGMT PF_NET_MGMT
60#define AF_LOCAL PF_LOCAL
61#define AF_UNIX PF_UNIX
62
76
86
93
100#define ntohs(x) sys_be16_to_cpu(x)
101
108#define ntohl(x) sys_be32_to_cpu(x)
109
116#define ntohll(x) sys_be64_to_cpu(x)
117
124#define htons(x) sys_cpu_to_be16(x)
125
132#define htonl(x) sys_cpu_to_be32(x)
133
140#define htonll(x) sys_cpu_to_be64(x)
141
143struct in6_addr {
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 sa_family_t;
169
171#ifndef __socklen_t_defined
173#define __socklen_t_defined
174#endif
175
176/*
177 * Note that the sin_port and sin6_port are in network byte order
178 * in various sockaddr* structs.
179 */
180
188
195
206
208
210struct sockaddr_in6_ptr {
211 sa_family_t sin6_family;
212 uint16_t sin6_port;
213 struct in6_addr *sin6_addr;
214 uint8_t sin6_scope_id;
215};
216
218struct sockaddr_in_ptr {
219 sa_family_t sin_family;
220 uint16_t sin_port;
221 struct in_addr *sin_addr;
222};
223
225struct sockaddr_ll_ptr {
226 sa_family_t sll_family;
227 uint16_t sll_protocol;
228 int sll_ifindex;
229 uint16_t sll_hatype;
230 uint8_t sll_pkttype;
231 uint8_t sll_halen;
232 uint8_t *sll_addr;
233};
234
236struct sockaddr_un_ptr {
237 sa_family_t sun_family;
238 char *sun_path;
239};
240
241struct sockaddr_can_ptr {
242 sa_family_t can_family;
243 int can_ifindex;
244};
245
247
248#if !defined(HAVE_IOVEC)
250struct iovec {
251 void *iov_base;
252 size_t iov_len;
253};
254#endif
255
266
268struct cmsghdr {
272 z_max_align_t cmsg_data[];
273};
274
276
277/* Alignment for headers and data. These are arch specific but define
278 * them here atm if not found already.
279 */
280#if !defined(ALIGN_H)
281#define ALIGN_H(x) ROUND_UP(x, __alignof__(struct cmsghdr))
282#endif
283#if !defined(ALIGN_D)
284#define ALIGN_D(x) ROUND_UP(x, __alignof__(z_max_align_t))
285#endif
286
288
289#if !defined(CMSG_FIRSTHDR)
295#define CMSG_FIRSTHDR(msghdr) \
296 ((msghdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
297 (struct cmsghdr *)((msghdr)->msg_control) : NULL)
298#endif
299
300#if !defined(CMSG_NXTHDR)
308#define CMSG_NXTHDR(msghdr, cmsg) \
309 (((cmsg) == NULL) ? CMSG_FIRSTHDR(msghdr) : \
310 (((cmsg)->cmsg_len < sizeof(struct cmsghdr) || \
311 (cmsg)->cmsg_len > (size_t)((uint8_t *)((msghdr)->msg_control) + \
312 (msghdr)->msg_controllen - \
313 (uint8_t *)(cmsg)) || \
314 (uint8_t *)(cmsg) + ALIGN_H((cmsg)->cmsg_len) + \
315 ALIGN_D(sizeof(struct cmsghdr)) > \
316 (uint8_t *)((msghdr)->msg_control) + (msghdr)->msg_controllen) ? \
317 NULL : \
318 (struct cmsghdr *)((uint8_t *)(cmsg) + \
319 ALIGN_H((cmsg)->cmsg_len))))
320#endif
321
322#if !defined(CMSG_DATA)
330#define CMSG_DATA(cmsg) ((uint8_t *)(cmsg) + ALIGN_D(sizeof(struct cmsghdr)))
331#endif
332
333#if !defined(CMSG_SPACE)
338#define CMSG_SPACE(length) (ALIGN_D(sizeof(struct cmsghdr)) + ALIGN_H(length))
339#endif
340
341#if !defined(CMSG_LEN)
347#define CMSG_LEN(length) (ALIGN_D(sizeof(struct cmsghdr)) + length)
348#endif
349
351
352/* Packet types. */
353#define PACKET_HOST 0 /* To us */
354#define PACKET_BROADCAST 1 /* To all */
355#define PACKET_MULTICAST 2 /* To group */
356#define PACKET_OTHERHOST 3 /* To someone else */
357#define PACKET_OUTGOING 4 /* Originated by us */
358#define PACKET_LOOPBACK 5
359#define PACKET_FASTROUTE 6
360
361/* ARP protocol HARDWARE identifiers. */
362#define ARPHRD_ETHER 1
363
364/* Note: These macros are defined in a specific order.
365 * The largest sockaddr size is the last one.
366 */
367#if defined(CONFIG_NET_IPV4)
368#undef NET_SOCKADDR_MAX_SIZE
369#undef NET_SOCKADDR_PTR_MAX_SIZE
370#define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_in))
371#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_in_ptr))
372#endif
373
374#if defined(CONFIG_NET_SOCKETS_PACKET)
375#undef NET_SOCKADDR_MAX_SIZE
376#undef NET_SOCKADDR_PTR_MAX_SIZE
377#define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_ll))
378#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_ll_ptr))
379#endif
380
381#if defined(CONFIG_NET_IPV6)
382#undef NET_SOCKADDR_MAX_SIZE
383#define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_in6))
384#if !defined(CONFIG_NET_SOCKETS_PACKET)
385#undef NET_SOCKADDR_PTR_MAX_SIZE
386#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_in6_ptr))
387#endif
388#endif
389
390#if defined(CONFIG_NET_NATIVE_OFFLOADED_SOCKETS)
391#define UNIX_PATH_MAX 108
392#undef NET_SOCKADDR_MAX_SIZE
393/* Define NET_SOCKADDR_MAX_SIZE to be struct of sa_family_t + char[UNIX_PATH_MAX] */
394#define NET_SOCKADDR_MAX_SIZE (UNIX_PATH_MAX+sizeof(sa_family_t))
395#if !defined(CONFIG_NET_SOCKETS_PACKET)
396#undef NET_SOCKADDR_PTR_MAX_SIZE
397#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_un_ptr))
398#endif
399#endif
400
401#if !defined(CONFIG_NET_IPV4)
402#if !defined(CONFIG_NET_IPV6)
403#if !defined(CONFIG_NET_SOCKETS_PACKET)
404#if !defined(CONFIG_NET_NATIVE_OFFLOADED_SOCKETS)
405#define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_in6))
406#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_in6_ptr))
407#endif
408#endif
409#endif
410#endif
411
413
414#define SOCKADDR_ALIGN (4)
415
417struct sockaddr {
420 char data[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
422} __aligned(SOCKADDR_ALIGN);
423
425
426struct sockaddr_ptr {
427 sa_family_t family;
428 char data[NET_SOCKADDR_PTR_MAX_SIZE - sizeof(sa_family_t)];
429} __aligned(SOCKADDR_ALIGN);
430
431/* Same as sockaddr in our case */
432struct sockaddr_storage {
433 sa_family_t ss_family;
434 char data[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
435} __aligned(SOCKADDR_ALIGN);
436
437/* Socket address struct for UNIX domain sockets */
438struct sockaddr_un {
439 sa_family_t sun_family; /* AF_UNIX */
440 char sun_path[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
441};
442
443struct net_addr {
444 sa_family_t family;
445 union {
446 struct in6_addr in6_addr;
447 struct in_addr in_addr;
448 };
449};
450
452extern const struct in6_addr in6addr_any;
453
455extern const struct in6_addr in6addr_loopback;
456
458
460#define IN6ADDR_ANY_INIT { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, \
461 0, 0, 0, 0, 0, 0, 0 } } }
462
464#define IN6ADDR_LOOPBACK_INIT { { { 0, 0, 0, 0, 0, 0, 0, \
465 0, 0, 0, 0, 0, 0, 0, 0, 1 } } }
466
468#define INADDR_ANY 0
469
471#define INADDR_BROADCAST 0xffffffff
472
474#define INADDR_ANY_INIT { { { INADDR_ANY } } }
475
477#define INADDR_LOOPBACK_INIT { { { 127, 0, 0, 1 } } }
478
480#define INET_ADDRSTRLEN 16
484#define INET6_ADDRSTRLEN 46
485
487
488/* These are for internal usage of the stack */
489#define NET_IPV6_ADDR_LEN sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxx.xxx.xxx.xxx")
490#define NET_IPV4_ADDR_LEN sizeof("xxx.xxx.xxx.xxx")
491
493
499#if defined(CONFIG_NET_NATIVE_IPV6)
500 NET_IPV6_MTU = CONFIG_NET_IPV6_MTU,
501#else
503#endif
504
508#if defined(CONFIG_NET_NATIVE_IPV4)
509 NET_IPV4_MTU = CONFIG_NET_IPV4_MTU,
510#else
512#endif
513};
514
526
527#define NET_MAX_PRIORITIES 8
528
537
545
559
561
562struct net_ipv6_hdr {
563 uint8_t vtc;
564 uint8_t tcflow;
565 uint16_t flow;
566 uint16_t len;
567 uint8_t nexthdr;
568 uint8_t hop_limit;
571} __packed;
572
573struct net_ipv6_frag_hdr {
574 uint8_t nexthdr;
575 uint8_t reserved;
576 uint16_t offset;
577 uint32_t id;
578} __packed;
579
580struct net_ipv4_hdr {
581 uint8_t vhl;
582 uint8_t tos;
583 uint16_t len;
584 uint8_t id[2];
585 uint8_t offset[2];
586 uint8_t ttl;
587 uint8_t proto;
588 uint16_t chksum;
591} __packed;
592
593struct net_icmp_hdr {
594 uint8_t type;
595 uint8_t code;
596 uint16_t chksum;
597} __packed;
598
599struct net_udp_hdr {
600 uint16_t src_port;
601 uint16_t dst_port;
602 uint16_t len;
603 uint16_t chksum;
604} __packed;
605
606struct net_tcp_hdr {
607 uint16_t src_port;
608 uint16_t dst_port;
609 uint8_t seq[4];
610 uint8_t ack[4];
611 uint8_t offset;
613 uint8_t wnd[2];
614 uint16_t chksum;
615 uint8_t urg[2];
616 uint8_t optdata[0];
617} __packed;
618
619static inline const char *net_addr_type2str(enum net_addr_type type)
620{
621 switch (type) {
623 return "AUTO";
624 case NET_ADDR_DHCP:
625 return "DHCP";
626 case NET_ADDR_MANUAL:
627 return "MANUAL";
629 return "OVERRIDE";
630 case NET_ADDR_ANY:
631 default:
632 break;
633 }
634
635 return "<unknown>";
636}
637
638/* IPv6 extension headers types */
639#define NET_IPV6_NEXTHDR_HBHO 0
640#define NET_IPV6_NEXTHDR_DESTO 60
641#define NET_IPV6_NEXTHDR_ROUTING 43
642#define NET_IPV6_NEXTHDR_FRAG 44
643#define NET_IPV6_NEXTHDR_NONE 59
644
649union net_ip_header {
650 struct net_ipv4_hdr *ipv4;
651 struct net_ipv6_hdr *ipv6;
652};
653
654union net_proto_header {
655 struct net_udp_hdr *udp;
656 struct net_tcp_hdr *tcp;
657};
658
659#define NET_UDPH_LEN 8 /* Size of UDP header */
660#define NET_TCPH_LEN 20 /* Size of TCP header */
661#define NET_ICMPH_LEN 4 /* Size of ICMP header */
662
663#define NET_IPV6H_LEN 40 /* Size of IPv6 header */
664#define NET_ICMPV6H_LEN NET_ICMPH_LEN /* Size of ICMPv6 header */
665#define NET_IPV6UDPH_LEN (NET_UDPH_LEN + NET_IPV6H_LEN) /* IPv6 + UDP */
666#define NET_IPV6TCPH_LEN (NET_TCPH_LEN + NET_IPV6H_LEN) /* IPv6 + TCP */
667#define NET_IPV6ICMPH_LEN (NET_IPV6H_LEN + NET_ICMPH_LEN) /* ICMPv6 + IPv6 */
668#define NET_IPV6_FRAGH_LEN 8
669
670#define NET_IPV4H_LEN 20 /* Size of IPv4 header */
671#define NET_ICMPV4H_LEN NET_ICMPH_LEN /* Size of ICMPv4 header */
672#define NET_IPV4UDPH_LEN (NET_UDPH_LEN + NET_IPV4H_LEN) /* IPv4 + UDP */
673#define NET_IPV4TCPH_LEN (NET_TCPH_LEN + NET_IPV4H_LEN) /* IPv4 + TCP */
674#define NET_IPV4ICMPH_LEN (NET_IPV4H_LEN + NET_ICMPH_LEN) /* ICMPv4 + IPv4 */
675
676#define NET_IPV6H_LENGTH_OFFSET 0x04 /* Offset of the Length field in the IPv6 header */
677
678#define NET_IPV6_FRAGH_OFFSET_MASK 0xfff8 /* Mask for the 13-bit Fragment Offset field */
679#define NET_IPV4_FRAGH_OFFSET_MASK 0x1fff /* Mask for the 13-bit Fragment Offset field */
680#define NET_IPV4_MORE_FRAG_MASK 0x2000 /* Mask for the 1-bit More Fragments field */
681#define NET_IPV4_DO_NOT_FRAG_MASK 0x4000 /* Mask for the 1-bit Do Not Fragment field */
682
684
686static inline bool net_ipv6_is_addr_loopback_raw(const uint8_t *addr)
687{
688 return UNALIGNED_GET((uint32_t *)addr) == 0 &&
689 UNALIGNED_GET((uint32_t *)addr + 1) == 0 &&
690 UNALIGNED_GET((uint32_t *)addr + 2) == 0 &&
691 ntohl(UNALIGNED_GET((uint32_t *)addr + 3)) == 1;
692}
694
702static inline bool net_ipv6_is_addr_loopback(const struct in6_addr *addr)
703{
704 return net_ipv6_is_addr_loopback_raw(addr->s6_addr);
705}
706
708static inline bool net_ipv6_is_addr_mcast_raw(const uint8_t *addr)
709{
710 return addr[0] == 0xff;
711}
713
721static inline bool net_ipv6_is_addr_mcast(const struct in6_addr *addr)
722{
723 return net_ipv6_is_addr_mcast_raw(addr->s6_addr);
724}
725
726struct net_if;
727struct net_if_config;
728
730extern struct net_if_addr *net_if_ipv6_addr_lookup_raw(const uint8_t *addr,
731 struct net_if **ret);
732
733static inline bool net_ipv6_is_my_addr_raw(const uint8_t *addr)
734{
735 return net_if_ipv6_addr_lookup_raw(addr, NULL) != NULL;
736}
738
739extern struct net_if_addr *net_if_ipv6_addr_lookup(const struct in6_addr *addr,
740 struct net_if **iface);
741
749static inline bool net_ipv6_is_my_addr(struct in6_addr *addr)
750{
751 return net_if_ipv6_addr_lookup(addr, NULL) != NULL;
752}
753
755 const struct in6_addr *addr, struct net_if **iface);
756
765static inline bool net_ipv6_is_my_maddr(struct in6_addr *maddr)
766{
767 return net_if_ipv6_maddr_lookup(maddr, NULL) != NULL;
768}
769
779static inline bool net_ipv6_is_prefix(const uint8_t *addr1,
780 const uint8_t *addr2,
781 uint8_t length)
782{
783 uint8_t bits = 128 - length;
784 uint8_t bytes = length / 8U;
785 uint8_t remain = bits % 8;
786 uint8_t mask;
787
788 if (length > 128) {
789 return false;
790 }
791
792 if (memcmp(addr1, addr2, bytes)) {
793 return false;
794 }
795
796 if (!remain) {
797 /* No remaining bits, the prefixes are the same as first
798 * bytes are the same.
799 */
800 return true;
801 }
802
803 /* Create a mask that has remaining most significant bits set */
804 mask = (uint8_t)((0xff << (8 - remain)) ^ 0xff) << remain;
805
806 return (addr1[bytes] & mask) == (addr2[bytes] & mask);
807}
808
809
817static inline void net_ipv6_addr_prefix_mask(const uint8_t *inaddr,
818 uint8_t *outaddr,
819 uint8_t prefix_len)
820{
821 uint8_t bits = 128 - prefix_len;
822 uint8_t bytes = prefix_len / 8U;
823 uint8_t remain = bits % 8;
824 uint8_t mask;
825
826 memset(outaddr, 0, 16U);
827 memcpy(outaddr, inaddr, bytes);
828
829 if (!remain) {
830 /* No remaining bits, the prefixes are the same as first
831 * bytes are the same.
832 */
833 return;
834 }
835
836 /* Create a mask that has remaining most significant bits set */
837 mask = (uint8_t)((0xff << (8 - remain)) ^ 0xff) << remain;
838 outaddr[bytes] = inaddr[bytes] & mask;
839}
840
842static inline bool net_ipv4_is_addr_loopback_raw(const uint8_t *addr)
843{
844 return addr[0] == 127U;
845}
847
855static inline bool net_ipv4_is_addr_loopback(const struct in_addr *addr)
856{
857 return net_ipv4_is_addr_loopback_raw(addr->s4_addr);
858}
859
861static inline bool net_ipv4_is_addr_unspecified_raw(const uint8_t *addr)
862{
863 return UNALIGNED_GET((uint32_t *)addr) == 0;
864}
866
874static inline bool net_ipv4_is_addr_unspecified(const struct in_addr *addr)
875{
876 return net_ipv4_is_addr_unspecified_raw(addr->s4_addr);
877}
878
880static inline bool net_ipv4_is_addr_mcast_raw(const uint8_t *addr)
881{
882 return (ntohl(UNALIGNED_GET((uint32_t *)addr)) & 0xF0000000) == 0xE0000000;
883}
885
893static inline bool net_ipv4_is_addr_mcast(const struct in_addr *addr)
894{
895 return net_ipv4_is_addr_mcast_raw(addr->s4_addr);
896}
897
899static inline bool net_ipv4_is_ll_addr_raw(const uint8_t *addr)
900{
901 return (ntohl(UNALIGNED_GET((uint32_t *)addr)) & 0xFFFF0000) == 0xA9FE0000;
902}
904
912static inline bool net_ipv4_is_ll_addr(const struct in_addr *addr)
913{
914 return net_ipv4_is_ll_addr_raw(addr->s4_addr);
915}
916
926static inline bool net_ipv4_is_private_addr(const struct in_addr *addr)
927{
928 uint32_t masked_24, masked_16, masked_12, masked_10, masked_8;
929
930 masked_24 = ntohl(UNALIGNED_GET(&addr->s_addr)) & 0xFFFFFF00;
931 masked_16 = masked_24 & 0xFFFF0000;
932 masked_12 = masked_24 & 0xFFF00000;
933 masked_10 = masked_24 & 0xFFC00000;
934 masked_8 = masked_24 & 0xFF000000;
935
936 return masked_8 == 0x0A000000 || /* 10.0.0.0/8 */
937 masked_10 == 0x64400000 || /* 100.64.0.0/10 */
938 masked_12 == 0xAC100000 || /* 172.16.0.0/12 */
939 masked_16 == 0xC0A80000 || /* 192.168.0.0/16 */
940 masked_24 == 0xC0000200 || /* 192.0.2.0/24 */
941 masked_24 == 0xC0336400 || /* 192.51.100.0/24 */
942 masked_24 == 0xCB007100; /* 203.0.113.0/24 */
943}
944
953#define net_ipaddr_copy(dest, src) \
954 UNALIGNED_PUT(UNALIGNED_GET(src), dest)
955
962static inline void net_ipv4_addr_copy_raw(uint8_t *dest,
963 const uint8_t *src)
964{
965 net_ipaddr_copy((struct in_addr *)dest, (const struct in_addr *)src);
966}
967
974static inline void net_ipv6_addr_copy_raw(uint8_t *dest,
975 const uint8_t *src)
976{
977 memcpy(dest, src, sizeof(struct in6_addr));
978}
979
988static inline bool net_ipv4_addr_cmp_raw(const uint8_t *addr1,
989 const uint8_t *addr2)
990{
991 return UNALIGNED_GET((uint32_t *)addr1) == UNALIGNED_GET((uint32_t *)addr2);
992}
993
1002static inline bool net_ipv4_addr_cmp(const struct in_addr *addr1,
1003 const struct in_addr *addr2)
1004{
1005 return net_ipv4_addr_cmp_raw(addr1->s4_addr, addr2->s4_addr);
1006}
1007
1016static inline bool net_ipv6_addr_cmp(const struct in6_addr *addr1,
1017 const struct in6_addr *addr2)
1018{
1019 return !memcmp(addr1, addr2, sizeof(struct in6_addr));
1020}
1021
1030static inline bool net_ipv6_addr_cmp_raw(const uint8_t *addr1,
1031 const uint8_t *addr2)
1032{
1033 return net_ipv6_addr_cmp((const struct in6_addr *)addr1,
1034 (const struct in6_addr *)addr2);
1035}
1036
1038static inline bool net_ipv6_is_ll_addr_raw(const uint8_t *addr)
1039{
1040 return UNALIGNED_GET((uint16_t *)addr) == htons(0xFE80);
1041}
1043
1051static inline bool net_ipv6_is_ll_addr(const struct in6_addr *addr)
1052{
1053 return net_ipv6_is_ll_addr_raw(addr->s6_addr);
1054}
1055
1063static inline bool net_ipv6_is_sl_addr(const struct in6_addr *addr)
1064{
1065 return UNALIGNED_GET(&addr->s6_addr16[0]) == htons(0xFEC0);
1066}
1067
1068
1076static inline bool net_ipv6_is_ula_addr(const struct in6_addr *addr)
1077{
1078 return addr->s6_addr[0] == 0xFD;
1079}
1080
1088static inline bool net_ipv6_is_global_addr(const struct in6_addr *addr)
1089{
1090 return (addr->s6_addr[0] & 0xE0) == 0x20;
1091}
1092
1102static inline bool net_ipv6_is_private_addr(const struct in6_addr *addr)
1103{
1104 uint32_t masked_32, masked_7;
1105
1106 masked_32 = ntohl(UNALIGNED_GET(&addr->s6_addr32[0]));
1107 masked_7 = masked_32 & 0xfc000000;
1108
1109 return masked_32 == 0x20010db8 || /* 2001:db8::/32 */
1110 masked_7 == 0xfc000000; /* fc00::/7 */
1111}
1112
1119
1126
1133
1134struct net_if;
1135extern bool net_if_ipv4_addr_mask_cmp(struct net_if *iface,
1136 const struct in_addr *addr);
1137
1147static inline bool net_ipv4_addr_mask_cmp(struct net_if *iface,
1148 const struct in_addr *addr)
1149{
1150 return net_if_ipv4_addr_mask_cmp(iface, addr);
1151}
1152
1154extern bool net_if_ipv4_is_addr_bcast_raw(struct net_if *iface,
1155 const uint8_t *addr);
1156
1157#if defined(CONFIG_NET_NATIVE_IPV4)
1158static inline bool net_ipv4_is_addr_bcast_raw(struct net_if *iface,
1159 const uint8_t *addr)
1160{
1161 if (net_ipv4_addr_cmp_raw(addr, net_ipv4_broadcast_address()->s4_addr)) {
1162 return true;
1163 }
1164
1165 return net_if_ipv4_is_addr_bcast_raw(iface, addr);
1166}
1167#else
1168static inline bool net_ipv4_is_addr_bcast_raw(struct net_if *iface,
1169 const uint8_t *addr)
1170{
1171 ARG_UNUSED(iface);
1172 ARG_UNUSED(addr);
1173
1174 return false;
1175}
1176#endif
1178
1179extern bool net_if_ipv4_is_addr_bcast(struct net_if *iface,
1180 const struct in_addr *addr);
1181
1190#if defined(CONFIG_NET_NATIVE_IPV4)
1191static inline bool net_ipv4_is_addr_bcast(struct net_if *iface,
1192 const struct in_addr *addr)
1193{
1195 return true;
1196 }
1197
1198 return net_if_ipv4_is_addr_bcast(iface, addr);
1199}
1200#else
1201static inline bool net_ipv4_is_addr_bcast(struct net_if *iface,
1202 const struct in_addr *addr)
1203{
1204 ARG_UNUSED(iface);
1205 ARG_UNUSED(addr);
1206
1207 return false;
1208}
1209#endif
1210
1212extern struct net_if_addr *net_if_ipv4_addr_lookup_raw(const uint8_t *addr,
1213 struct net_if **ret);
1214
1215static inline bool net_ipv4_is_my_addr_raw(const uint8_t *addr)
1216{
1217 bool ret;
1218
1219 ret = net_if_ipv4_addr_lookup_raw(addr, NULL) != NULL;
1220 if (!ret) {
1221 ret = net_ipv4_is_addr_bcast_raw(NULL, addr);
1222 }
1223
1224 return ret;
1225}
1227
1228extern struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr,
1229 struct net_if **iface);
1230
1240static inline bool net_ipv4_is_my_addr(const struct in_addr *addr)
1241{
1242 bool ret;
1243
1244 ret = net_if_ipv4_addr_lookup(addr, NULL) != NULL;
1245 if (!ret) {
1246 ret = net_ipv4_is_addr_bcast(NULL, addr);
1247 }
1248
1249 return ret;
1250}
1251
1253static inline bool net_ipv6_is_addr_unspecified_raw(const uint8_t *addr)
1254{
1255 return UNALIGNED_GET((uint32_t *)addr) == 0 &&
1256 UNALIGNED_GET((uint32_t *)addr + 1) == 0 &&
1257 UNALIGNED_GET((uint32_t *)addr + 2) == 0 &&
1258 UNALIGNED_GET((uint32_t *)addr + 3) == 0;
1259}
1261
1269static inline bool net_ipv6_is_addr_unspecified(const struct in6_addr *addr)
1270{
1271 return net_ipv6_is_addr_unspecified_raw(addr->s6_addr);
1272}
1273
1275static inline bool net_ipv6_is_addr_solicited_node_raw(const uint8_t *addr)
1276{
1277 return UNALIGNED_GET((uint32_t *)addr) == htonl(0xff020000) &&
1278 UNALIGNED_GET((uint32_t *)addr + 1) == 0x00000000 &&
1279 UNALIGNED_GET((uint32_t *)addr + 2) == htonl(0x00000001) &&
1280 ((UNALIGNED_GET((uint32_t *)addr + 3) & htonl(0xff000000)) ==
1281 htonl(0xff000000));
1282}
1284
1293static inline bool net_ipv6_is_addr_solicited_node(const struct in6_addr *addr)
1294{
1295 return net_ipv6_is_addr_solicited_node_raw(addr->s6_addr);
1296}
1297
1299static inline bool net_ipv6_is_addr_mcast_scope_raw(const uint8_t *addr,
1300 int scope)
1301{
1302 return (addr[0] == 0xff) && ((addr[1] & 0xF) == scope);
1303}
1305
1307static inline int net_ipv6_get_addr_mcast_scope_raw(const uint8_t *addr)
1308{
1309 if (addr[0] == 0xff) {
1310 return (addr[1] & 0xF);
1311 }
1312
1313 return -1;
1314}
1316
1327static inline bool net_ipv6_is_addr_mcast_scope(const struct in6_addr *addr,
1328 int scope)
1329{
1330 return net_ipv6_is_addr_mcast_scope_raw(addr->s6_addr, scope);
1331}
1332
1342static inline bool net_ipv6_is_same_mcast_scope(const struct in6_addr *addr_1,
1343 const struct in6_addr *addr_2)
1344{
1345 return (addr_1->s6_addr[0] == 0xff) && (addr_2->s6_addr[0] == 0xff) &&
1346 (addr_1->s6_addr[1] == addr_2->s6_addr[1]);
1347}
1348
1356static inline int net_ipv6_get_addr_mcast_scope(const struct in6_addr *addr)
1357{
1358 return net_ipv6_get_addr_mcast_scope_raw(addr->s6_addr);
1359}
1360
1362static inline bool net_ipv6_is_addr_mcast_iface_raw(const uint8_t *addr)
1363{
1364 return net_ipv6_is_addr_mcast_scope_raw(addr, 0x01);
1365}
1366
1367static inline bool net_ipv6_is_addr_mcast_link_raw(const uint8_t *addr)
1368{
1369 return net_ipv6_is_addr_mcast_scope_raw(addr, 0x02);
1370}
1371
1372static inline bool net_ipv6_is_addr_mcast_mesh_raw(const uint8_t *addr)
1373{
1374 return net_ipv6_is_addr_mcast_scope_raw(addr, 0x03);
1375}
1376
1377static inline bool net_ipv6_is_addr_mcast_site_raw(const uint8_t *addr)
1378{
1379 return net_ipv6_is_addr_mcast_scope_raw(addr, 0x05);
1380}
1381
1382static inline bool net_ipv6_is_addr_mcast_org_raw(const uint8_t *addr)
1383{
1384 return net_ipv6_is_addr_mcast_scope_raw(addr, 0x08);
1385}
1387
1395static inline bool net_ipv6_is_addr_mcast_global(const struct in6_addr *addr)
1396{
1397 return net_ipv6_is_addr_mcast_scope(addr, 0x0e);
1398}
1399
1409static inline bool net_ipv6_is_addr_mcast_iface(const struct in6_addr *addr)
1410{
1411 return net_ipv6_is_addr_mcast_scope(addr, 0x01);
1412}
1413
1423static inline bool net_ipv6_is_addr_mcast_link(const struct in6_addr *addr)
1424{
1425 return net_ipv6_is_addr_mcast_scope(addr, 0x02);
1426}
1427
1437static inline bool net_ipv6_is_addr_mcast_mesh(const struct in6_addr *addr)
1438{
1439 return net_ipv6_is_addr_mcast_scope(addr, 0x03);
1440}
1441
1451static inline bool net_ipv6_is_addr_mcast_site(const struct in6_addr *addr)
1452{
1453 return net_ipv6_is_addr_mcast_scope(addr, 0x05);
1454}
1455
1465static inline bool net_ipv6_is_addr_mcast_org(const struct in6_addr *addr)
1466{
1467 return net_ipv6_is_addr_mcast_scope(addr, 0x08);
1468}
1469
1471static inline bool net_ipv6_is_addr_mcast_group_raw(const uint8_t *addr,
1472 const uint8_t *group)
1473{
1474 return UNALIGNED_GET((uint16_t *)addr + 1) == UNALIGNED_GET((uint16_t *)group + 1) &&
1475 UNALIGNED_GET((uint32_t *)addr + 1) == UNALIGNED_GET((uint32_t *)group + 1) &&
1476 UNALIGNED_GET((uint32_t *)addr + 2) == UNALIGNED_GET((uint32_t *)group + 2) &&
1477 UNALIGNED_GET((uint32_t *)addr + 3) == UNALIGNED_GET((uint32_t *)group + 3);
1478}
1480
1491static inline bool net_ipv6_is_addr_mcast_group(const struct in6_addr *addr,
1492 const struct in6_addr *group)
1493{
1494 return net_ipv6_is_addr_mcast_group_raw(addr->s6_addr, group->s6_addr);
1495}
1496
1498static inline bool net_ipv6_is_addr_mcast_all_nodes_group_raw(const uint8_t *addr)
1499{
1500 static const uint8_t all_nodes_mcast_group[NET_IPV6_ADDR_SIZE] = {
1501 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1502 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1503 };
1504
1505 return net_ipv6_is_addr_mcast_group_raw(addr, all_nodes_mcast_group);
1506}
1508
1517static inline bool
1519{
1520 return net_ipv6_is_addr_mcast_all_nodes_group_raw(addr->s6_addr);
1521}
1522
1532static inline bool
1538
1540static inline bool net_ipv6_is_addr_mcast_link_all_nodes_raw(const uint8_t *addr)
1541{
1542 return net_ipv6_is_addr_mcast_link_raw(addr) &&
1543 net_ipv6_is_addr_mcast_all_nodes_group_raw(addr);
1544}
1546
1556static inline bool
1562
1570static inline
1572 struct in6_addr *dst)
1573{
1574 dst->s6_addr[0] = 0xFF;
1575 dst->s6_addr[1] = 0x02;
1576 UNALIGNED_PUT(0, &dst->s6_addr16[1]);
1577 UNALIGNED_PUT(0, &dst->s6_addr16[2]);
1578 UNALIGNED_PUT(0, &dst->s6_addr16[3]);
1579 UNALIGNED_PUT(0, &dst->s6_addr16[4]);
1580 dst->s6_addr[10] = 0U;
1581 dst->s6_addr[11] = 0x01;
1582 dst->s6_addr[12] = 0xFF;
1583 dst->s6_addr[13] = src->s6_addr[13];
1584 UNALIGNED_PUT(UNALIGNED_GET(&src->s6_addr16[7]), &dst->s6_addr16[7]);
1585}
1586
1599static inline void net_ipv6_addr_create(struct in6_addr *addr,
1600 uint16_t addr0, uint16_t addr1,
1601 uint16_t addr2, uint16_t addr3,
1602 uint16_t addr4, uint16_t addr5,
1603 uint16_t addr6, uint16_t addr7)
1604{
1605 UNALIGNED_PUT(htons(addr0), &addr->s6_addr16[0]);
1606 UNALIGNED_PUT(htons(addr1), &addr->s6_addr16[1]);
1607 UNALIGNED_PUT(htons(addr2), &addr->s6_addr16[2]);
1608 UNALIGNED_PUT(htons(addr3), &addr->s6_addr16[3]);
1609 UNALIGNED_PUT(htons(addr4), &addr->s6_addr16[4]);
1610 UNALIGNED_PUT(htons(addr5), &addr->s6_addr16[5]);
1611 UNALIGNED_PUT(htons(addr6), &addr->s6_addr16[6]);
1612 UNALIGNED_PUT(htons(addr7), &addr->s6_addr16[7]);
1613}
1614
1620static inline void net_ipv6_addr_create_ll_allnodes_mcast(struct in6_addr *addr)
1621{
1622 net_ipv6_addr_create(addr, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001);
1623}
1624
1631{
1632 net_ipv6_addr_create(addr, 0xff02, 0, 0, 0, 0, 0, 0, 0x0002);
1633}
1634
1641static inline void net_ipv6_addr_create_v4_mapped(const struct in_addr *addr4,
1642 struct in6_addr *addr6)
1643{
1644 net_ipv6_addr_create(addr6, 0, 0, 0, 0, 0, 0xffff,
1645 ntohs(addr4->s4_addr16[0]),
1646 ntohs(addr4->s4_addr16[1]));
1647}
1648
1657static inline bool net_ipv6_addr_is_v4_mapped(const struct in6_addr *addr)
1658{
1659 if (UNALIGNED_GET(&addr->s6_addr32[0]) == 0 &&
1660 UNALIGNED_GET(&addr->s6_addr32[1]) == 0 &&
1661 UNALIGNED_GET(&addr->s6_addr16[5]) == 0xffff) {
1662 return true;
1663 }
1664
1665 return false;
1666}
1667
1687 const struct in6_addr *prefix,
1688 uint8_t *network_id, size_t network_id_len,
1689 uint8_t dad_counter,
1690 struct in6_addr *addr,
1691 struct net_linkaddr *lladdr);
1692
1699static inline void net_ipv6_addr_create_iid(struct in6_addr *addr,
1700 struct net_linkaddr *lladdr)
1701{
1702 (void)net_ipv6_addr_generate_iid(NULL, NULL, NULL, 0, 0, addr, lladdr);
1703}
1704
1706static inline bool net_ipv6_addr_based_on_ll_raw(const uint8_t *addr,
1707 const struct net_linkaddr *lladdr)
1708{
1709 if (addr == NULL || lladdr == NULL) {
1710 return false;
1711 }
1712
1713 switch (lladdr->len) {
1714 case 2:
1715 if (!memcmp(&addr[14], lladdr->addr, lladdr->len) &&
1716 addr[8] == 0U &&
1717 addr[9] == 0U &&
1718 addr[10] == 0U &&
1719 addr[11] == 0xff &&
1720 addr[12] == 0xfe) {
1721 return true;
1722 }
1723
1724 break;
1725 case 6:
1726 if (lladdr->type == NET_LINK_ETHERNET) {
1727 if (!memcmp(&addr[9], &lladdr->addr[1], 2) &&
1728 !memcmp(&addr[13], &lladdr->addr[3], 3) &&
1729 addr[11] == 0xff &&
1730 addr[12] == 0xfe &&
1731 (addr[8] ^ 0x02) == lladdr->addr[0]) {
1732 return true;
1733 }
1734 }
1735
1736 break;
1737 case 8:
1738 if (sizeof(lladdr->addr) < 8) {
1739 return false;
1740 }
1741
1742 if (!memcmp(&addr[9], &lladdr->addr[1],
1743 lladdr->len - 1) &&
1744 (addr[8] ^ 0x02) == lladdr->addr[0]) {
1745 return true;
1746 }
1747
1748 break;
1749 default:
1750 return false;
1751 }
1752
1753 return false;
1754}
1756
1762static inline bool net_ipv6_addr_based_on_ll(const struct in6_addr *addr,
1763 const struct net_linkaddr *lladdr)
1764{
1765 if (addr == NULL || lladdr == NULL) {
1766 return false;
1767 }
1768
1769 return net_ipv6_addr_based_on_ll_raw(addr->s6_addr, lladdr);
1770}
1771
1780static inline struct sockaddr *net_sad(const struct sockaddr_storage *addr)
1781{
1782 return (struct sockaddr *)addr;
1783}
1784
1793static inline struct sockaddr_in6 *net_sin6(const struct sockaddr *addr)
1794{
1795 return (struct sockaddr_in6 *)addr;
1796}
1797
1806static inline struct sockaddr_in *net_sin(const struct sockaddr *addr)
1807{
1808 return (struct sockaddr_in *)addr;
1809}
1810
1819static inline
1820struct sockaddr_in6_ptr *net_sin6_ptr(const struct sockaddr_ptr *addr)
1821{
1822 return (struct sockaddr_in6_ptr *)addr;
1823}
1824
1833static inline
1834struct sockaddr_in_ptr *net_sin_ptr(const struct sockaddr_ptr *addr)
1835{
1836 return (struct sockaddr_in_ptr *)addr;
1837}
1838
1847static inline
1848struct sockaddr_ll_ptr *net_sll_ptr(const struct sockaddr_ptr *addr)
1849{
1850 return (struct sockaddr_ll_ptr *)addr;
1851}
1852
1861static inline
1862struct sockaddr_can_ptr *net_can_ptr(const struct sockaddr_ptr *addr)
1863{
1864 return (struct sockaddr_can_ptr *)addr;
1865}
1866
1880__syscall int net_addr_pton(sa_family_t family, const char *src, void *dst);
1881
1893__syscall char *net_addr_ntop(sa_family_t family, const void *src,
1894 char *dst, size_t size);
1895
1907 struct sockaddr *mask);
1908
1920 uint8_t *mask_len);
1921
1943bool net_ipaddr_parse(const char *str, size_t str_len,
1944 struct sockaddr *addr);
1945
1973const char *net_ipaddr_parse_mask(const char *str, size_t str_len,
1974 struct sockaddr *addr, uint8_t *mask_len);
1975
1985int net_port_set_default(struct sockaddr *addr, uint16_t default_port);
1986
1998static inline int32_t net_tcp_seq_cmp(uint32_t seq1, uint32_t seq2)
1999{
2000 return (int32_t)(seq1 - seq2);
2001}
2002
2013static inline bool net_tcp_seq_greater(uint32_t seq1, uint32_t seq2)
2014{
2015 return net_tcp_seq_cmp(seq1, seq2) > 0;
2016}
2017
2029int net_bytes_from_str(uint8_t *buf, int buf_len, const char *src);
2030
2040
2050
2059static inline enum net_priority net_vlan2priority(uint8_t priority)
2060{
2061 /* Map according to IEEE 802.1Q */
2062 static const uint8_t vlan2priority[] = {
2071 };
2072
2073 if (priority >= ARRAY_SIZE(vlan2priority)) {
2074 /* Use Best Effort as the default priority */
2075 return NET_PRIORITY_BE;
2076 }
2077
2078 return (enum net_priority)vlan2priority[priority];
2079}
2080
2088static inline uint8_t net_priority2vlan(enum net_priority priority)
2089{
2090 /* The conversion works both ways */
2091 return (uint8_t)net_vlan2priority(priority);
2092}
2093
2102const char *net_family2str(sa_family_t family);
2103
2114#if defined(CONFIG_NET_IPV6_PE)
2115int net_ipv6_pe_add_filter(struct in6_addr *addr, bool is_denylist);
2116#else
2117static inline int net_ipv6_pe_add_filter(struct in6_addr *addr,
2118 bool is_denylist)
2119{
2120 ARG_UNUSED(addr);
2121 ARG_UNUSED(is_denylist);
2122
2123 return -ENOTSUP;
2124}
2125#endif /* CONFIG_NET_IPV6_PE */
2126
2134#if defined(CONFIG_NET_IPV6_PE)
2135int net_ipv6_pe_del_filter(struct in6_addr *addr);
2136#else
2137static inline int net_ipv6_pe_del_filter(struct in6_addr *addr)
2138{
2139 ARG_UNUSED(addr);
2140
2141 return -ENOTSUP;
2142}
2143#endif /* CONFIG_NET_IPV6_PE */
2144
2145#ifdef __cplusplus
2146}
2147#endif
2148
2149#include <zephyr/syscalls/net_ip.h>
2150
2154
2155
2156#endif /* ZEPHYR_INCLUDE_NET_NET_IP_H_ */
static bool net_ipv6_is_my_addr(struct in6_addr *addr)
Check if IPv6 address is found in one of the network interfaces.
Definition net_ip.h:749
struct net_if_addr * net_if_ipv4_addr_lookup(const struct in_addr *addr, struct net_if **iface)
static void net_ipv6_addr_create(struct 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:1599
static bool net_ipv4_addr_cmp(const struct in_addr *addr1, const struct in_addr *addr2)
Compare two IPv4 addresses.
Definition net_ip.h:1002
#define NET_IPV4_ADDR_SIZE
Binary size of the IPv4 address.
Definition net_ip.h:165
int net_netmask_to_mask_len(sa_family_t family, struct sockaddr *mask, uint8_t *mask_len)
Create mask length from netmask.
struct net_if_addr * net_if_ipv6_addr_lookup(const struct in6_addr *addr, struct net_if **iface)
static bool net_ipv6_is_addr_mcast_org(const struct in6_addr *addr)
Check if the IPv6 address is an organization scope multicast address (FFx8::).
Definition net_ip.h:1465
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:2059
static int net_ipv6_pe_add_filter(struct in6_addr *addr, bool is_denylist)
Add IPv6 prefix as a privacy extension filter.
Definition net_ip.h:2117
static int32_t net_tcp_seq_cmp(uint32_t seq1, uint32_t seq2)
Compare TCP sequence numbers.
Definition net_ip.h:1998
static bool net_ipv6_is_addr_mcast(const struct in6_addr *addr)
Check if the IPv6 address is a multicast address.
Definition net_ip.h:721
#define NET_IPV6_ADDR_SIZE
Binary size of the IPv6 address.
Definition net_ip.h:152
int net_addr_pton(sa_family_t family, const char *src, void *dst)
Convert a string to IP address.
unsigned short int sa_family_t
Socket address family type.
Definition net_ip.h:168
static void net_ipv6_addr_create_ll_allrouters_mcast(struct in6_addr *addr)
Create link local allrouters multicast IPv6 address.
Definition net_ip.h:1630
char * net_addr_ntop(sa_family_t family, const void *src, char *dst, size_t size)
Convert IP address to string form.
net_addr_state
What is the current state of the network address.
Definition net_ip.h:539
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:988
static bool net_ipv6_addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2)
Compare two IPv6 addresses.
Definition net_ip.h:1016
static bool net_ipv6_is_addr_mcast_iface_all_nodes(const struct in6_addr *addr)
Check if the IPv6 address is a interface scope all nodes multicast address (FF01::1).
Definition net_ip.h:1533
static bool net_ipv6_is_addr_loopback(const struct in6_addr *addr)
Check if the IPv6 address is a loopback address (::1).
Definition net_ip.h:702
static bool net_ipv4_is_my_addr(const struct in_addr *addr)
Check if the IPv4 address is assigned to any network interface in the system.
Definition net_ip.h:1240
uint32_t socklen_t
Length of a socket address.
Definition net_ip.h:172
static bool net_ipv6_is_same_mcast_scope(const struct in6_addr *addr_1, const struct in6_addr *addr_2)
Check if the IPv6 addresses have the same multicast scope (FFyx::).
Definition net_ip.h:1342
static void net_ipv6_addr_copy_raw(uint8_t *dest, const uint8_t *src)
Copy an IPv6 address raw buffer.
Definition net_ip.h:974
static bool net_ipv6_is_addr_mcast_mesh(const struct in6_addr *addr)
Check if the IPv6 address is a mesh-local scope multicast address (FFx3::).
Definition net_ip.h:1437
static struct sockaddr_in_ptr * net_sin_ptr(const struct sockaddr_ptr *addr)
Get sockaddr_in_ptr from sockaddr_ptr.
Definition net_ip.h:1834
const struct in_addr * net_ipv4_broadcast_address(void)
Return pointer to broadcast (all bits ones) IPv4 address.
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:817
#define htons(x)
Convert 16-bit value from host to network byte order.
Definition net_ip.h:124
static bool net_ipv6_addr_is_v4_mapped(const struct in6_addr *addr)
Is the IPv6 address an IPv4 mapped one.
Definition net_ip.h:1657
bool net_if_ipv4_addr_mask_cmp(struct net_if *iface, const struct in_addr *addr)
static bool net_ipv6_is_addr_mcast_global(const struct in6_addr *addr)
Check if the IPv6 address is a global multicast address (FFxE::/16).
Definition net_ip.h:1395
static void net_ipv6_addr_create_ll_allnodes_mcast(struct in6_addr *addr)
Create link local allnodes multicast IPv6 address.
Definition net_ip.h:1620
static bool net_ipv6_is_addr_solicited_node(const struct 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:1293
static void net_ipv6_addr_create_solicited_node(const struct in6_addr *src, struct 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:1571
static bool net_ipv6_is_addr_mcast_group(const struct in6_addr *addr, const struct in6_addr *group)
Check if the IPv6 address belongs to certain multicast group.
Definition net_ip.h:1491
static bool net_ipv6_is_addr_mcast_site(const struct in6_addr *addr)
Check if the IPv6 address is a site scope multicast address (FFx5::).
Definition net_ip.h:1451
static bool net_ipv6_is_sl_addr(const struct in6_addr *addr)
Check if the given IPv6 address is a site local address.
Definition net_ip.h:1063
static void net_ipv6_addr_create_iid(struct in6_addr *addr, struct net_linkaddr *lladdr)
Create IPv6 address interface identifier.
Definition net_ip.h:1699
static bool net_ipv4_is_private_addr(const struct in_addr *addr)
Check if the given IPv4 address is from a private address range.
Definition net_ip.h:926
static bool net_ipv6_is_addr_mcast_link(const struct in6_addr *addr)
Check if the IPv6 address is a link local scope multicast address (FFx2::).
Definition net_ip.h:1423
static bool net_ipv4_addr_mask_cmp(struct net_if *iface, const struct in_addr *addr)
Check if the given address belongs to same subnet that has been configured for the interface.
Definition net_ip.h:1147
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:953
static int net_ipv6_pe_del_filter(struct in6_addr *addr)
Delete IPv6 prefix from privacy extension filter list.
Definition net_ip.h:2137
const char * net_ipaddr_parse_mask(const char *str, size_t str_len, struct sockaddr *addr, uint8_t *mask_len)
Parse a string that contains either IPv4 or IPv6 address and optional mask len, and store the informa...
net_ip_mtu
IP Maximum Transfer Unit.
Definition net_ip.h:495
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.
int net_ipv6_addr_generate_iid(struct net_if *iface, const struct in6_addr *prefix, uint8_t *network_id, size_t network_id_len, uint8_t dad_counter, struct in6_addr *addr, struct net_linkaddr *lladdr)
Generate IPv6 address using a prefix and interface identifier.
static int net_ipv6_get_addr_mcast_scope(const struct in6_addr *addr)
Returns the scope of the given IPv6 address.
Definition net_ip.h:1356
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:2088
static void net_ipv6_addr_create_v4_mapped(const struct in_addr *addr4, struct in6_addr *addr6)
Create IPv4 mapped IPv6 address.
Definition net_ip.h:1641
bool net_if_ipv4_is_addr_bcast(struct net_if *iface, const struct in_addr *addr)
#define SOCKADDR_ALIGN
Definition net_ip.h:414
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:779
bool net_ipaddr_parse(const char *str, size_t str_len, struct sockaddr *addr)
Parse a string that contains either IPv4 or IPv6 address and optional port, and store the information...
static bool net_ipv4_is_addr_loopback(const struct in_addr *addr)
Check if the IPv4 address is a loopback address (127.0.0.0/8).
Definition net_ip.h:855
int net_port_set_default(struct sockaddr *addr, uint16_t default_port)
Set the default port in the sockaddr structure.
static bool net_tcp_seq_greater(uint32_t seq1, uint32_t seq2)
Check that one TCP sequence number is greater.
Definition net_ip.h:2013
net_sock_type
Socket type.
Definition net_ip.h:88
const struct in6_addr * net_ipv6_unspecified_address(void)
Return pointer to any (all bits zeros) IPv6 address.
static bool net_ipv6_is_global_addr(const struct in6_addr *addr)
Check if the given IPv6 address is a global address.
Definition net_ip.h:1088
static bool net_ipv6_is_addr_mcast_link_all_nodes(const struct in6_addr *addr)
Check if the IPv6 address is a link local scope all nodes multicast address (FF02::1).
Definition net_ip.h:1557
const char * net_family2str(sa_family_t family)
Return network address family value as a string.
static bool net_ipv4_is_ll_addr(const struct in_addr *addr)
Check if the given IPv4 address is a link local address.
Definition net_ip.h:912
int net_mask_len_to_netmask(sa_family_t family, uint8_t mask_len, struct sockaddr *mask)
Create netmask from mask length.
static struct sockaddr_can_ptr * net_can_ptr(const struct sockaddr_ptr *addr)
Get sockaddr_can_ptr from sockaddr_ptr.
Definition net_ip.h:1862
#define ntohl(x)
Convert 32-bit value from network to host byte order.
Definition net_ip.h:108
static bool net_ipv4_is_addr_bcast(struct net_if *iface, const struct in_addr *addr)
Check if the given IPv4 address is a broadcast address.
Definition net_ip.h:1201
static bool net_ipv6_is_ll_addr(const struct in6_addr *addr)
Check if the given IPv6 address is a link local address.
Definition net_ip.h:1051
static struct sockaddr_in * net_sin(const struct sockaddr *addr)
Get sockaddr_in from sockaddr.
Definition net_ip.h:1806
const struct in_addr * net_ipv4_unspecified_address(void)
Return pointer to any (all bits zeros) IPv4 address.
static bool net_ipv6_is_addr_mcast_all_nodes_group(const struct in6_addr *addr)
Check if the IPv6 address belongs to the all nodes multicast group.
Definition net_ip.h:1518
static bool net_ipv6_is_private_addr(const struct in6_addr *addr)
Check if the given IPv6 address is from a private/local address range.
Definition net_ip.h:1102
static struct sockaddr_ll_ptr * net_sll_ptr(const struct sockaddr_ptr *addr)
Get sockaddr_ll_ptr from sockaddr_ptr.
Definition net_ip.h:1848
static struct sockaddr_in6 * net_sin6(const struct sockaddr *addr)
Get sockaddr_in6 from sockaddr.
Definition net_ip.h:1793
#define ntohs(x)
Convert 16-bit value from network to host byte order.
Definition net_ip.h:100
struct net_if_mcast_addr * net_if_ipv6_maddr_lookup(const struct in6_addr *addr, struct net_if **iface)
static bool net_ipv4_is_addr_unspecified(const struct in_addr *addr)
Check if the IPv4 address is unspecified (all bits zero)
Definition net_ip.h:874
static bool net_ipv6_is_addr_mcast_scope(const struct in6_addr *addr, int scope)
Check if the IPv6 address is a given scope multicast address (FFyx::).
Definition net_ip.h:1327
static bool net_ipv6_is_ula_addr(const struct in6_addr *addr)
Check if the given IPv6 address is a unique local address.
Definition net_ip.h:1076
static bool net_ipv6_is_addr_mcast_iface(const struct in6_addr *addr)
Check if the IPv6 address is a interface scope multicast address (FFx1::).
Definition net_ip.h:1409
#define htonl(x)
Convert 32-bit value from host to network byte order.
Definition net_ip.h:132
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.
static struct sockaddr_in6_ptr * net_sin6_ptr(const struct sockaddr_ptr *addr)
Get sockaddr_in6_ptr from sockaddr_ptr.
Definition net_ip.h:1820
net_priority
Network packet priority settings described in IEEE 802.1Q Annex I.1.
Definition net_ip.h:516
static bool net_ipv4_is_addr_mcast(const struct in_addr *addr)
Check if the IPv4 address is a multicast address.
Definition net_ip.h:893
static struct sockaddr * net_sad(const struct sockaddr_storage *addr)
Get sockaddr from sockaddr_storage.
Definition net_ip.h:1780
net_ip_protocol
Protocol numbers from IANA/BSD.
Definition net_ip.h:64
static bool net_ipv6_addr_based_on_ll(const struct in6_addr *addr, const struct net_linkaddr *lladdr)
Check if given address is based on link layer address.
Definition net_ip.h:1762
static void net_ipv4_addr_copy_raw(uint8_t *dest, const uint8_t *src)
Copy an IPv4 address raw buffer.
Definition net_ip.h:962
static bool net_ipv6_is_my_maddr(struct in6_addr *maddr)
Check if IPv6 multicast address is found in one of the network interfaces.
Definition net_ip.h:765
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:1030
static bool net_ipv6_is_addr_unspecified(const struct in6_addr *addr)
Check if the IPv6 address is unspecified (all bits zero)
Definition net_ip.h:1269
net_addr_type
How the network address is assigned to network interface.
Definition net_ip.h:547
@ NET_ADDR_ANY_STATE
Default (invalid) address type.
Definition net_ip.h:540
@ NET_ADDR_TENTATIVE
Tentative address.
Definition net_ip.h:541
@ NET_ADDR_DEPRECATED
Deprecated address.
Definition net_ip.h:543
@ NET_ADDR_PREFERRED
Preferred address.
Definition net_ip.h:542
@ IPPROTO_TLS_1_1
TLS 1.1 protocol.
Definition net_ip.h:80
@ IPPROTO_TLS_1_0
TLS 1.0 protocol.
Definition net_ip.h:79
@ IPPROTO_DTLS_1_0
DTLS 1.0 protocol.
Definition net_ip.h:83
@ IPPROTO_TLS_1_3
TLS 1.3 protocol.
Definition net_ip.h:82
@ IPPROTO_TLS_1_2
TLS 1.2 protocol.
Definition net_ip.h:81
@ IPPROTO_DTLS_1_2
DTLS 1.2 protocol.
Definition net_ip.h:84
@ NET_IPV4_MTU
IPv4 MTU length.
Definition net_ip.h:511
@ NET_IPV6_MTU
IPv6 MTU length.
Definition net_ip.h:502
@ SOCK_DGRAM
Datagram socket type.
Definition net_ip.h:90
@ SOCK_RAW
RAW socket type.
Definition net_ip.h:91
@ SOCK_STREAM
Stream socket type.
Definition net_ip.h:89
@ NET_PRIORITY_NC
Network control (highest)
Definition net_ip.h:524
@ NET_PRIORITY_IC
Internetwork control.
Definition net_ip.h:523
@ NET_PRIORITY_CA
Critical applications.
Definition net_ip.h:520
@ NET_PRIORITY_VO
Voice, < 10 ms latency and jitter.
Definition net_ip.h:522
@ NET_PRIORITY_VI
Video, < 100 ms latency and jitter.
Definition net_ip.h:521
@ NET_PRIORITY_BE
Best effort (default)
Definition net_ip.h:518
@ NET_PRIORITY_EE
Excellent effort.
Definition net_ip.h:519
@ NET_PRIORITY_BK
Background (lowest)
Definition net_ip.h:517
@ IPPROTO_IP
IP protocol (pseudo-val for setsockopt()
Definition net_ip.h:65
@ IPPROTO_RAW
RAW IP packets.
Definition net_ip.h:74
@ IPPROTO_IPIP
IPIP tunnels.
Definition net_ip.h:69
@ IPPROTO_TCP
TCP protocol.
Definition net_ip.h:70
@ IPPROTO_IGMP
IGMP protocol.
Definition net_ip.h:67
@ IPPROTO_ICMP
ICMP protocol.
Definition net_ip.h:66
@ IPPROTO_IPV6
IPv6 protocol.
Definition net_ip.h:72
@ IPPROTO_ETH_P_ALL
Every packet.
Definition net_ip.h:68
@ IPPROTO_UDP
UDP protocol.
Definition net_ip.h:71
@ IPPROTO_ICMPV6
ICMPv6 protocol.
Definition net_ip.h:73
@ NET_ADDR_ANY
Default value.
Definition net_ip.h:549
@ NET_ADDR_OVERRIDABLE
Manually set address which is overridable by DHCP.
Definition net_ip.h:557
@ NET_ADDR_DHCP
Address is from DHCP.
Definition net_ip.h:553
@ NET_ADDR_MANUAL
Manually set address.
Definition net_ip.h:555
@ NET_ADDR_AUTOCONF
Auto configured address.
Definition net_ip.h:551
@ NET_LINK_ETHERNET
Ethernet link address.
Definition net_linkaddr.h:53
#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
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)
Control message ancillary data.
Definition net_ip.h:268
int cmsg_type
Protocol-specific type.
Definition net_ip.h:271
socklen_t cmsg_len
Number of bytes, including header.
Definition net_ip.h:269
z_max_align_t cmsg_data[]
Flexible array member to force alignment of cmsghdr.
Definition net_ip.h:272
int cmsg_level
Originating protocol.
Definition net_ip.h:270
Group structure.
Definition grp.h:18
IPv6 address struct.
Definition net_ip.h:143
uint8_t s6_addr[16]
IPv6 address buffer.
Definition net_ip.h:145
uint32_t s6_addr32[4]
In big endian.
Definition net_ip.h:147
uint16_t s6_addr16[8]
In big endian.
Definition net_ip.h:146
IPv4 address struct.
Definition net_ip.h:155
uint8_t s4_addr[4]
IPv4 address buffer.
Definition net_ip.h:157
uint32_t s_addr
In big endian, for POSIX compatibility.
Definition net_ip.h:160
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
IO vector array element.
Definition net_ip.h:250
void * iov_base
Pointer to data.
Definition net_ip.h:251
size_t iov_len
Length of the data.
Definition net_ip.h:252
Message struct.
Definition net_ip.h:257
struct iovec * msg_iov
Scatter/gather array.
Definition net_ip.h:260
socklen_t msg_namelen
Size of socket address.
Definition net_ip.h:259
void * msg_name
Optional socket address, big endian.
Definition net_ip.h:258
int msg_flags
Flags on received message.
Definition net_ip.h:264
size_t msg_controllen
Ancillary data buffer len.
Definition net_ip.h:263
size_t msg_iovlen
Number of elements in msg_iov.
Definition net_ip.h:261
void * msg_control
Ancillary data.
Definition net_ip.h:262
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
Hardware link address structure.
Definition net_linkaddr.h:70
uint8_t addr[6]
The array of bytes representing the address.
Definition net_linkaddr.h:78
uint8_t type
What kind of address is this for.
Definition net_linkaddr.h:72
uint8_t len
The real length of the ll address.
Definition net_linkaddr.h:75
IPv6/IPv4 network connection tuple.
Definition net_ip.h:530
struct net_addr * remote_addr
IPv6/IPv4 remote address.
Definition net_ip.h:531
uint16_t local_port
UDP/TCP local port.
Definition net_ip.h:534
enum net_ip_protocol ip_proto
IP protocol.
Definition net_ip.h:535
uint16_t remote_port
UDP/TCP remote port.
Definition net_ip.h:533
struct net_addr * local_addr
IPv6/IPv4 local address.
Definition net_ip.h:532
Socket address struct for IPv6.
Definition net_ip.h:182
uint8_t sin6_scope_id
Interfaces for a scope.
Definition net_ip.h:186
struct in6_addr sin6_addr
IPv6 address.
Definition net_ip.h:185
uint16_t sin6_port
Port number.
Definition net_ip.h:184
sa_family_t sin6_family
AF_INET6.
Definition net_ip.h:183
Socket address struct for IPv4.
Definition net_ip.h:190
uint16_t sin_port
Port number.
Definition net_ip.h:192
struct in_addr sin_addr
IPv4 address.
Definition net_ip.h:193
sa_family_t sin_family
AF_INET.
Definition net_ip.h:191
Socket address struct for packet socket.
Definition net_ip.h:197
uint8_t sll_pkttype
Packet type.
Definition net_ip.h:202
uint16_t sll_hatype
ARP hardware type.
Definition net_ip.h:201
sa_family_t sll_family
Always AF_PACKET.
Definition net_ip.h:198
uint16_t sll_protocol
Physical-layer protocol.
Definition net_ip.h:199
int sll_ifindex
Interface number.
Definition net_ip.h:200
uint8_t sll_halen
Length of address.
Definition net_ip.h:203
uint8_t sll_addr[8]
Physical-layer address, big endian.
Definition net_ip.h:204
Generic sockaddr struct.
Definition net_ip.h:417
sa_family_t sa_family
Address family.
Definition net_ip.h:418
Byte order helpers.
Misc utilities.
Macros to abstract toolchain specific capabilities.