Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
net_ip.h
Go to the documentation of this file.
1
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
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
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
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
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
172typedef size_t socklen_t;
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
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
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
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
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)
305#define CMSG_NXTHDR(msghdr, cmsg) \
306 (((cmsg) == NULL) ? CMSG_FIRSTHDR(msghdr) : \
307 (((uint8_t *)(cmsg) + ALIGN_H((cmsg)->cmsg_len) + \
308 ALIGN_D(sizeof(struct cmsghdr)) > \
309 (uint8_t *)((msghdr)->msg_control) + (msghdr)->msg_controllen) ? \
310 NULL : \
311 (struct cmsghdr *)((uint8_t *)(cmsg) + \
312 ALIGN_H((cmsg)->cmsg_len))))
313#endif
314
315#if !defined(CMSG_DATA)
323#define CMSG_DATA(cmsg) ((uint8_t *)(cmsg) + ALIGN_D(sizeof(struct cmsghdr)))
324#endif
325
326#if !defined(CMSG_SPACE)
331#define CMSG_SPACE(length) (ALIGN_D(sizeof(struct cmsghdr)) + ALIGN_H(length))
332#endif
333
334#if !defined(CMSG_LEN)
340#define CMSG_LEN(length) (ALIGN_D(sizeof(struct cmsghdr)) + length)
341#endif
342
345/* Packet types. */
346#define PACKET_HOST 0 /* To us */
347#define PACKET_BROADCAST 1 /* To all */
348#define PACKET_MULTICAST 2 /* To group */
349#define PACKET_OTHERHOST 3 /* To someone else */
350#define PACKET_OUTGOING 4 /* Originated by us */
351#define PACKET_LOOPBACK 5
352#define PACKET_FASTROUTE 6
353
354/* ARP protocol HARDWARE identifiers. */
355#define ARPHRD_ETHER 1
356
357/* Note: These macros are defined in a specific order.
358 * The largest sockaddr size is the last one.
359 */
360#if defined(CONFIG_NET_IPV4)
361#undef NET_SOCKADDR_MAX_SIZE
362#undef NET_SOCKADDR_PTR_MAX_SIZE
363#define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_in))
364#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_in_ptr))
365#endif
366
367#if defined(CONFIG_NET_SOCKETS_PACKET)
368#undef NET_SOCKADDR_MAX_SIZE
369#undef NET_SOCKADDR_PTR_MAX_SIZE
370#define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_ll))
371#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_ll_ptr))
372#endif
373
374#if defined(CONFIG_NET_IPV6)
375#undef NET_SOCKADDR_MAX_SIZE
376#define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_in6))
377#if !defined(CONFIG_NET_SOCKETS_PACKET)
378#undef NET_SOCKADDR_PTR_MAX_SIZE
379#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_in6_ptr))
380#endif
381#endif
382
383#if defined(CONFIG_NET_NATIVE_OFFLOADED_SOCKETS)
384#define UNIX_PATH_MAX 108
385#undef NET_SOCKADDR_MAX_SIZE
386/* Define NET_SOCKADDR_MAX_SIZE to be struct of sa_family_t + char[UNIX_PATH_MAX] */
387#define NET_SOCKADDR_MAX_SIZE (UNIX_PATH_MAX+sizeof(sa_family_t))
388#if !defined(CONFIG_NET_SOCKETS_PACKET)
389#undef NET_SOCKADDR_PTR_MAX_SIZE
390#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_un_ptr))
391#endif
392#endif
393
394#if !defined(CONFIG_NET_IPV4)
395#if !defined(CONFIG_NET_IPV6)
396#if !defined(CONFIG_NET_SOCKETS_PACKET)
397#if !defined(CONFIG_NET_NATIVE_OFFLOADED_SOCKETS)
398#define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_in6))
399#define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_in6_ptr))
400#endif
401#endif
402#endif
403#endif
404
408struct sockaddr {
411 char data[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
413};
414
417struct sockaddr_ptr {
418 sa_family_t family;
419 char data[NET_SOCKADDR_PTR_MAX_SIZE - sizeof(sa_family_t)];
420};
421
422/* Same as sockaddr in our case */
423struct sockaddr_storage {
424 sa_family_t ss_family;
425 char data[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
426};
427
428/* Socket address struct for UNIX domain sockets */
429struct sockaddr_un {
430 sa_family_t sun_family; /* AF_UNIX */
431 char sun_path[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
432};
433
434struct net_addr {
435 sa_family_t family;
436 union {
437 struct in6_addr in6_addr;
438 struct in_addr in_addr;
439 };
440};
441
443extern const struct in6_addr in6addr_any;
444
446extern const struct in6_addr in6addr_loopback;
447
451#define IN6ADDR_ANY_INIT { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, \
452 0, 0, 0, 0, 0, 0, 0 } } }
453
455#define IN6ADDR_LOOPBACK_INIT { { { 0, 0, 0, 0, 0, 0, 0, \
456 0, 0, 0, 0, 0, 0, 0, 0, 1 } } }
457
459#define INADDR_ANY 0
460
462#define INADDR_BROADCAST 0xffffffff
463
465#define INADDR_ANY_INIT { { { INADDR_ANY } } }
466
468#define INADDR_LOOPBACK_INIT { { { 127, 0, 0, 1 } } }
469
471#define INET_ADDRSTRLEN 16
475#define INET6_ADDRSTRLEN 46
476
479/* These are for internal usage of the stack */
480#define NET_IPV6_ADDR_LEN sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")
481#define NET_IPV4_ADDR_LEN sizeof("xxx.xxx.xxx.xxx")
482
490#if defined(CONFIG_NET_NATIVE_IPV6)
491 NET_IPV6_MTU = CONFIG_NET_IPV6_MTU,
492#else
494#endif
495
499#if defined(CONFIG_NET_NATIVE_IPV4)
500 NET_IPV4_MTU = CONFIG_NET_IPV4_MTU,
501#else
503#endif
504};
505
517
518#define NET_MAX_PRIORITIES 8
528
536
550
553struct net_ipv6_hdr {
554 uint8_t vtc;
555 uint8_t tcflow;
556 uint16_t flow;
557 uint16_t len;
558 uint8_t nexthdr;
559 uint8_t hop_limit;
562} __packed;
563
564struct net_ipv6_frag_hdr {
565 uint8_t nexthdr;
566 uint8_t reserved;
567 uint16_t offset;
568 uint32_t id;
569} __packed;
570
571struct net_ipv4_hdr {
572 uint8_t vhl;
573 uint8_t tos;
574 uint16_t len;
575 uint8_t id[2];
576 uint8_t offset[2];
577 uint8_t ttl;
578 uint8_t proto;
579 uint16_t chksum;
582} __packed;
583
584struct net_icmp_hdr {
585 uint8_t type;
586 uint8_t code;
587 uint16_t chksum;
588} __packed;
589
590struct net_udp_hdr {
591 uint16_t src_port;
592 uint16_t dst_port;
593 uint16_t len;
594 uint16_t chksum;
595} __packed;
596
597struct net_tcp_hdr {
598 uint16_t src_port;
599 uint16_t dst_port;
600 uint8_t seq[4];
601 uint8_t ack[4];
602 uint8_t offset;
604 uint8_t wnd[2];
605 uint16_t chksum;
606 uint8_t urg[2];
607 uint8_t optdata[0];
608} __packed;
609
610static inline const char *net_addr_type2str(enum net_addr_type type)
611{
612 switch (type) {
614 return "AUTO";
615 case NET_ADDR_DHCP:
616 return "DHCP";
617 case NET_ADDR_MANUAL:
618 return "MANUAL";
620 return "OVERRIDE";
621 case NET_ADDR_ANY:
622 default:
623 break;
624 }
625
626 return "<unknown>";
627}
628
629/* IPv6 extension headers types */
630#define NET_IPV6_NEXTHDR_HBHO 0
631#define NET_IPV6_NEXTHDR_DESTO 60
632#define NET_IPV6_NEXTHDR_ROUTING 43
633#define NET_IPV6_NEXTHDR_FRAG 44
634#define NET_IPV6_NEXTHDR_NONE 59
635
640union net_ip_header {
641 struct net_ipv4_hdr *ipv4;
642 struct net_ipv6_hdr *ipv6;
643};
644
645union net_proto_header {
646 struct net_udp_hdr *udp;
647 struct net_tcp_hdr *tcp;
648};
649
650#define NET_UDPH_LEN 8 /* Size of UDP header */
651#define NET_TCPH_LEN 20 /* Size of TCP header */
652#define NET_ICMPH_LEN 4 /* Size of ICMP header */
653
654#define NET_IPV6H_LEN 40 /* Size of IPv6 header */
655#define NET_ICMPV6H_LEN NET_ICMPH_LEN /* Size of ICMPv6 header */
656#define NET_IPV6UDPH_LEN (NET_UDPH_LEN + NET_IPV6H_LEN) /* IPv6 + UDP */
657#define NET_IPV6TCPH_LEN (NET_TCPH_LEN + NET_IPV6H_LEN) /* IPv6 + TCP */
658#define NET_IPV6ICMPH_LEN (NET_IPV6H_LEN + NET_ICMPH_LEN) /* ICMPv6 + IPv6 */
659#define NET_IPV6_FRAGH_LEN 8
660
661#define NET_IPV4H_LEN 20 /* Size of IPv4 header */
662#define NET_ICMPV4H_LEN NET_ICMPH_LEN /* Size of ICMPv4 header */
663#define NET_IPV4UDPH_LEN (NET_UDPH_LEN + NET_IPV4H_LEN) /* IPv4 + UDP */
664#define NET_IPV4TCPH_LEN (NET_TCPH_LEN + NET_IPV4H_LEN) /* IPv4 + TCP */
665#define NET_IPV4ICMPH_LEN (NET_IPV4H_LEN + NET_ICMPH_LEN) /* ICMPv4 + IPv4 */
666
667#define NET_IPV6H_LENGTH_OFFSET 0x04 /* Offset of the Length field in the IPv6 header */
668
669#define NET_IPV6_FRAGH_OFFSET_MASK 0xfff8 /* Mask for the 13-bit Fragment Offset field */
670#define NET_IPV4_FRAGH_OFFSET_MASK 0x1fff /* Mask for the 13-bit Fragment Offset field */
671#define NET_IPV4_MORE_FRAG_MASK 0x2000 /* Mask for the 1-bit More Fragments field */
672#define NET_IPV4_DO_NOT_FRAG_MASK 0x4000 /* Mask for the 1-bit Do Not Fragment field */
673
683static inline bool net_ipv6_is_addr_loopback(struct in6_addr *addr)
684{
685 return UNALIGNED_GET(&addr->s6_addr32[0]) == 0 &&
686 UNALIGNED_GET(&addr->s6_addr32[1]) == 0 &&
687 UNALIGNED_GET(&addr->s6_addr32[2]) == 0 &&
688 ntohl(UNALIGNED_GET(&addr->s6_addr32[3])) == 1;
689}
690
698static inline bool net_ipv6_is_addr_mcast(const struct in6_addr *addr)
699{
700 return addr->s6_addr[0] == 0xFF;
701}
702
703struct net_if;
704struct net_if_config;
705
706extern struct net_if_addr *net_if_ipv6_addr_lookup(const struct in6_addr *addr,
707 struct net_if **iface);
708
716static inline bool net_ipv6_is_my_addr(struct in6_addr *addr)
717{
718 return net_if_ipv6_addr_lookup(addr, NULL) != NULL;
719}
720
722 const struct in6_addr *addr, struct net_if **iface);
723
732static inline bool net_ipv6_is_my_maddr(struct in6_addr *maddr)
733{
734 return net_if_ipv6_maddr_lookup(maddr, NULL) != NULL;
735}
736
746static inline bool net_ipv6_is_prefix(const uint8_t *addr1,
747 const uint8_t *addr2,
748 uint8_t length)
749{
750 uint8_t bits = 128 - length;
751 uint8_t bytes = length / 8U;
752 uint8_t remain = bits % 8;
753 uint8_t mask;
754
755 if (length > 128) {
756 return false;
757 }
758
759 if (memcmp(addr1, addr2, bytes)) {
760 return false;
761 }
762
763 if (!remain) {
764 /* No remaining bits, the prefixes are the same as first
765 * bytes are the same.
766 */
767 return true;
768 }
769
770 /* Create a mask that has remaining most significant bits set */
771 mask = (uint8_t)((0xff << (8 - remain)) ^ 0xff) << remain;
772
773 return (addr1[bytes] & mask) == (addr2[bytes] & mask);
774}
775
776
784static inline void net_ipv6_addr_prefix_mask(const uint8_t *inaddr,
785 uint8_t *outaddr,
786 uint8_t prefix_len)
787{
788 uint8_t bits = 128 - prefix_len;
789 uint8_t bytes = prefix_len / 8U;
790 uint8_t remain = bits % 8;
791 uint8_t mask;
792
793 memset(outaddr, 0, 16U);
794 memcpy(outaddr, inaddr, bytes);
795
796 if (!remain) {
797 /* No remaining bits, the prefixes are the same as first
798 * bytes are the same.
799 */
800 return;
801 }
802
803 /* Create a mask that has remaining most significant bits set */
804 mask = (uint8_t)((0xff << (8 - remain)) ^ 0xff) << remain;
805 outaddr[bytes] = inaddr[bytes] & mask;
806}
807
815static inline bool net_ipv4_is_addr_loopback(struct in_addr *addr)
816{
817 return addr->s4_addr[0] == 127U;
818}
819
827static inline bool net_ipv4_is_addr_unspecified(const struct in_addr *addr)
828{
829 return UNALIGNED_GET(&addr->s_addr) == 0;
830}
831
839static inline bool net_ipv4_is_addr_mcast(const struct in_addr *addr)
840{
841 return (ntohl(UNALIGNED_GET(&addr->s_addr)) & 0xF0000000) == 0xE0000000;
842}
843
851static inline bool net_ipv4_is_ll_addr(const struct in_addr *addr)
852{
853 return (ntohl(UNALIGNED_GET(&addr->s_addr)) & 0xFFFF0000) == 0xA9FE0000;
854}
855
865static inline bool net_ipv4_is_private_addr(const struct in_addr *addr)
866{
867 uint32_t masked_24, masked_16, masked_12, masked_10, masked_8;
868
869 masked_24 = ntohl(UNALIGNED_GET(&addr->s_addr)) & 0xFFFFFF00;
870 masked_16 = masked_24 & 0xFFFF0000;
871 masked_12 = masked_24 & 0xFFF00000;
872 masked_10 = masked_24 & 0xFFC00000;
873 masked_8 = masked_24 & 0xFF000000;
874
875 return masked_8 == 0x0A000000 || /* 10.0.0.0/8 */
876 masked_10 == 0x64400000 || /* 100.64.0.0/10 */
877 masked_12 == 0xAC100000 || /* 172.16.0.0/12 */
878 masked_16 == 0xC0A80000 || /* 192.168.0.0/16 */
879 masked_24 == 0xC0000200 || /* 192.0.2.0/24 */
880 masked_24 == 0xC0336400 || /* 192.51.100.0/24 */
881 masked_24 == 0xCB007100; /* 203.0.113.0/24 */
882}
883
892#define net_ipaddr_copy(dest, src) \
893 UNALIGNED_PUT(UNALIGNED_GET(src), dest)
894
901static inline void net_ipv4_addr_copy_raw(uint8_t *dest,
902 const uint8_t *src)
903{
904 net_ipaddr_copy((struct in_addr *)dest, (const struct in_addr *)src);
905}
906
913static inline void net_ipv6_addr_copy_raw(uint8_t *dest,
914 const uint8_t *src)
915{
916 memcpy(dest, src, sizeof(struct in6_addr));
917}
918
927static inline bool net_ipv4_addr_cmp(const struct in_addr *addr1,
928 const struct in_addr *addr2)
929{
930 return UNALIGNED_GET(&addr1->s_addr) == UNALIGNED_GET(&addr2->s_addr);
931}
932
941static inline bool net_ipv4_addr_cmp_raw(const uint8_t *addr1,
942 const uint8_t *addr2)
943{
944 return net_ipv4_addr_cmp((const struct in_addr *)addr1,
945 (const struct in_addr *)addr2);
946}
947
956static inline bool net_ipv6_addr_cmp(const struct in6_addr *addr1,
957 const struct in6_addr *addr2)
958{
959 return !memcmp(addr1, addr2, sizeof(struct in6_addr));
960}
961
970static inline bool net_ipv6_addr_cmp_raw(const uint8_t *addr1,
971 const uint8_t *addr2)
972{
973 return net_ipv6_addr_cmp((const struct in6_addr *)addr1,
974 (const struct in6_addr *)addr2);
975}
976
984static inline bool net_ipv6_is_ll_addr(const struct in6_addr *addr)
985{
986 return UNALIGNED_GET(&addr->s6_addr16[0]) == htons(0xFE80);
987}
988
996static inline bool net_ipv6_is_sl_addr(const struct in6_addr *addr)
997{
998 return UNALIGNED_GET(&addr->s6_addr16[0]) == htons(0xFEC0);
999}
1000
1001
1009static inline bool net_ipv6_is_ula_addr(const struct in6_addr *addr)
1010{
1011 return addr->s6_addr[0] == 0xFD;
1012}
1013
1021static inline bool net_ipv6_is_global_addr(const struct in6_addr *addr)
1022{
1023 return (addr->s6_addr[0] & 0xE0) == 0x20;
1024}
1025
1035static inline bool net_ipv6_is_private_addr(const struct in6_addr *addr)
1036{
1037 uint32_t masked_32, masked_7;
1038
1039 masked_32 = ntohl(UNALIGNED_GET(&addr->s6_addr32[0]));
1040 masked_7 = masked_32 & 0xfc000000;
1041
1042 return masked_32 == 0x20010db8 || /* 2001:db8::/32 */
1043 masked_7 == 0xfc000000; /* fc00::/7 */
1044}
1045
1052
1059
1066
1067struct net_if;
1068extern bool net_if_ipv4_addr_mask_cmp(struct net_if *iface,
1069 const struct in_addr *addr);
1070
1080static inline bool net_ipv4_addr_mask_cmp(struct net_if *iface,
1081 const struct in_addr *addr)
1082{
1083 return net_if_ipv4_addr_mask_cmp(iface, addr);
1084}
1085
1086extern bool net_if_ipv4_is_addr_bcast(struct net_if *iface,
1087 const struct in_addr *addr);
1088
1097#if defined(CONFIG_NET_NATIVE_IPV4)
1098static inline bool net_ipv4_is_addr_bcast(struct net_if *iface,
1099 const struct in_addr *addr)
1100{
1102 return true;
1103 }
1104
1105 return net_if_ipv4_is_addr_bcast(iface, addr);
1106}
1107#else
1108static inline bool net_ipv4_is_addr_bcast(struct net_if *iface,
1109 const struct in_addr *addr)
1110{
1111 ARG_UNUSED(iface);
1112 ARG_UNUSED(addr);
1113
1114 return false;
1115}
1116#endif
1117
1118extern struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr,
1119 struct net_if **iface);
1120
1130static inline bool net_ipv4_is_my_addr(const struct in_addr *addr)
1131{
1132 bool ret;
1133
1134 ret = net_if_ipv4_addr_lookup(addr, NULL) != NULL;
1135 if (!ret) {
1136 ret = net_ipv4_is_addr_bcast(NULL, addr);
1137 }
1138
1139 return ret;
1140}
1141
1149static inline bool net_ipv6_is_addr_unspecified(const struct in6_addr *addr)
1150{
1151 return UNALIGNED_GET(&addr->s6_addr32[0]) == 0 &&
1152 UNALIGNED_GET(&addr->s6_addr32[1]) == 0 &&
1153 UNALIGNED_GET(&addr->s6_addr32[2]) == 0 &&
1154 UNALIGNED_GET(&addr->s6_addr32[3]) == 0;
1155}
1156
1165static inline bool net_ipv6_is_addr_solicited_node(const struct in6_addr *addr)
1166{
1167 return UNALIGNED_GET(&addr->s6_addr32[0]) == htonl(0xff020000) &&
1168 UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00000000 &&
1169 UNALIGNED_GET(&addr->s6_addr32[2]) == htonl(0x00000001) &&
1170 ((UNALIGNED_GET(&addr->s6_addr32[3]) & htonl(0xff000000)) ==
1171 htonl(0xff000000));
1172}
1173
1184static inline bool net_ipv6_is_addr_mcast_scope(const struct in6_addr *addr,
1185 int scope)
1186{
1187 return (addr->s6_addr[0] == 0xff) && ((addr->s6_addr[1] & 0xF) == scope);
1188}
1189
1199static inline bool net_ipv6_is_same_mcast_scope(const struct in6_addr *addr_1,
1200 const struct in6_addr *addr_2)
1201{
1202 return (addr_1->s6_addr[0] == 0xff) && (addr_2->s6_addr[0] == 0xff) &&
1203 (addr_1->s6_addr[1] == addr_2->s6_addr[1]);
1204}
1205
1213static inline bool net_ipv6_is_addr_mcast_global(const struct in6_addr *addr)
1214{
1215 return net_ipv6_is_addr_mcast_scope(addr, 0x0e);
1216}
1217
1227static inline bool net_ipv6_is_addr_mcast_iface(const struct in6_addr *addr)
1228{
1229 return net_ipv6_is_addr_mcast_scope(addr, 0x01);
1230}
1231
1241static inline bool net_ipv6_is_addr_mcast_link(const struct in6_addr *addr)
1242{
1243 return net_ipv6_is_addr_mcast_scope(addr, 0x02);
1244}
1245
1255static inline bool net_ipv6_is_addr_mcast_mesh(const struct in6_addr *addr)
1256{
1257 return net_ipv6_is_addr_mcast_scope(addr, 0x03);
1258}
1259
1269static inline bool net_ipv6_is_addr_mcast_site(const struct in6_addr *addr)
1270{
1271 return net_ipv6_is_addr_mcast_scope(addr, 0x05);
1272}
1273
1283static inline bool net_ipv6_is_addr_mcast_org(const struct in6_addr *addr)
1284{
1285 return net_ipv6_is_addr_mcast_scope(addr, 0x08);
1286}
1287
1298static inline bool net_ipv6_is_addr_mcast_group(const struct in6_addr *addr,
1299 const struct in6_addr *group)
1300{
1301 return UNALIGNED_GET(&addr->s6_addr16[1]) == group->s6_addr16[1] &&
1302 UNALIGNED_GET(&addr->s6_addr16[2]) == group->s6_addr16[2] &&
1303 UNALIGNED_GET(&addr->s6_addr16[3]) == group->s6_addr16[3] &&
1304 UNALIGNED_GET(&addr->s6_addr32[1]) == group->s6_addr32[1] &&
1305 UNALIGNED_GET(&addr->s6_addr32[2]) == group->s6_addr32[1] &&
1306 UNALIGNED_GET(&addr->s6_addr32[3]) == group->s6_addr32[3];
1307}
1308
1317static inline bool
1319{
1320 static const struct in6_addr all_nodes_mcast_group = {
1321 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1322 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } }
1323 };
1324
1325 return net_ipv6_is_addr_mcast_group(addr, &all_nodes_mcast_group);
1326}
1327
1337static inline bool
1343
1353static inline bool
1359
1367static inline
1369 struct in6_addr *dst)
1370{
1371 dst->s6_addr[0] = 0xFF;
1372 dst->s6_addr[1] = 0x02;
1373 UNALIGNED_PUT(0, &dst->s6_addr16[1]);
1374 UNALIGNED_PUT(0, &dst->s6_addr16[2]);
1375 UNALIGNED_PUT(0, &dst->s6_addr16[3]);
1376 UNALIGNED_PUT(0, &dst->s6_addr16[4]);
1377 dst->s6_addr[10] = 0U;
1378 dst->s6_addr[11] = 0x01;
1379 dst->s6_addr[12] = 0xFF;
1380 dst->s6_addr[13] = src->s6_addr[13];
1381 UNALIGNED_PUT(UNALIGNED_GET(&src->s6_addr16[7]), &dst->s6_addr16[7]);
1382}
1383
1396static inline void net_ipv6_addr_create(struct in6_addr *addr,
1397 uint16_t addr0, uint16_t addr1,
1398 uint16_t addr2, uint16_t addr3,
1399 uint16_t addr4, uint16_t addr5,
1400 uint16_t addr6, uint16_t addr7)
1401{
1402 UNALIGNED_PUT(htons(addr0), &addr->s6_addr16[0]);
1403 UNALIGNED_PUT(htons(addr1), &addr->s6_addr16[1]);
1404 UNALIGNED_PUT(htons(addr2), &addr->s6_addr16[2]);
1405 UNALIGNED_PUT(htons(addr3), &addr->s6_addr16[3]);
1406 UNALIGNED_PUT(htons(addr4), &addr->s6_addr16[4]);
1407 UNALIGNED_PUT(htons(addr5), &addr->s6_addr16[5]);
1408 UNALIGNED_PUT(htons(addr6), &addr->s6_addr16[6]);
1409 UNALIGNED_PUT(htons(addr7), &addr->s6_addr16[7]);
1410}
1411
1417static inline void net_ipv6_addr_create_ll_allnodes_mcast(struct in6_addr *addr)
1418{
1419 net_ipv6_addr_create(addr, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001);
1420}
1421
1428{
1429 net_ipv6_addr_create(addr, 0xff02, 0, 0, 0, 0, 0, 0, 0x0002);
1430}
1431
1438static inline void net_ipv6_addr_create_v4_mapped(const struct in_addr *addr4,
1439 struct in6_addr *addr6)
1440{
1441 net_ipv6_addr_create(addr6, 0, 0, 0, 0, 0, 0xffff,
1442 ntohs(addr4->s4_addr16[0]),
1443 ntohs(addr4->s4_addr16[1]));
1444}
1445
1454static inline bool net_ipv6_addr_is_v4_mapped(const struct in6_addr *addr)
1455{
1456 if (UNALIGNED_GET(&addr->s6_addr32[0]) == 0 &&
1457 UNALIGNED_GET(&addr->s6_addr32[1]) == 0 &&
1458 UNALIGNED_GET(&addr->s6_addr16[5]) == 0xffff) {
1459 return true;
1460 }
1461
1462 return false;
1463}
1464
1484 const struct in6_addr *prefix,
1485 uint8_t *network_id, size_t network_id_len,
1486 uint8_t dad_counter,
1487 struct in6_addr *addr,
1488 struct net_linkaddr *lladdr);
1489
1496static inline void net_ipv6_addr_create_iid(struct in6_addr *addr,
1497 struct net_linkaddr *lladdr)
1498{
1499 (void)net_ipv6_addr_generate_iid(NULL, NULL, NULL, 0, 0, addr, lladdr);
1500}
1501
1507static inline bool net_ipv6_addr_based_on_ll(const struct in6_addr *addr,
1508 const struct net_linkaddr *lladdr)
1509{
1510 if (!addr || !lladdr) {
1511 return false;
1512 }
1513
1514 switch (lladdr->len) {
1515 case 2:
1516 if (!memcmp(&addr->s6_addr[14], lladdr->addr, lladdr->len) &&
1517 addr->s6_addr[8] == 0U &&
1518 addr->s6_addr[9] == 0U &&
1519 addr->s6_addr[10] == 0U &&
1520 addr->s6_addr[11] == 0xff &&
1521 addr->s6_addr[12] == 0xfe) {
1522 return true;
1523 }
1524
1525 break;
1526 case 6:
1527 if (lladdr->type == NET_LINK_ETHERNET) {
1528 if (!memcmp(&addr->s6_addr[9], &lladdr->addr[1], 2) &&
1529 !memcmp(&addr->s6_addr[13], &lladdr->addr[3], 3) &&
1530 addr->s6_addr[11] == 0xff &&
1531 addr->s6_addr[12] == 0xfe &&
1532 (addr->s6_addr[8] ^ 0x02) == lladdr->addr[0]) {
1533 return true;
1534 }
1535 }
1536
1537 break;
1538 case 8:
1539 if (!memcmp(&addr->s6_addr[9], &lladdr->addr[1],
1540 lladdr->len - 1) &&
1541 (addr->s6_addr[8] ^ 0x02) == lladdr->addr[0]) {
1542 return true;
1543 }
1544
1545 break;
1546 default:
1547 return false;
1548 }
1549
1550 return false;
1551}
1552
1561static inline struct sockaddr_in6 *net_sin6(const struct sockaddr *addr)
1562{
1563 return (struct sockaddr_in6 *)addr;
1564}
1565
1574static inline struct sockaddr_in *net_sin(const struct sockaddr *addr)
1575{
1576 return (struct sockaddr_in *)addr;
1577}
1578
1587static inline
1588struct sockaddr_in6_ptr *net_sin6_ptr(const struct sockaddr_ptr *addr)
1589{
1590 return (struct sockaddr_in6_ptr *)addr;
1591}
1592
1601static inline
1602struct sockaddr_in_ptr *net_sin_ptr(const struct sockaddr_ptr *addr)
1603{
1604 return (struct sockaddr_in_ptr *)addr;
1605}
1606
1615static inline
1616struct sockaddr_ll_ptr *net_sll_ptr(const struct sockaddr_ptr *addr)
1617{
1618 return (struct sockaddr_ll_ptr *)addr;
1619}
1620
1629static inline
1630struct sockaddr_can_ptr *net_can_ptr(const struct sockaddr_ptr *addr)
1631{
1632 return (struct sockaddr_can_ptr *)addr;
1633}
1634
1648__syscall int net_addr_pton(sa_family_t family, const char *src, void *dst);
1649
1661__syscall char *net_addr_ntop(sa_family_t family, const void *src,
1662 char *dst, size_t size);
1663
1685bool net_ipaddr_parse(const char *str, size_t str_len,
1686 struct sockaddr *addr);
1687
1697int net_port_set_default(struct sockaddr *addr, uint16_t default_port);
1698
1710static inline int32_t net_tcp_seq_cmp(uint32_t seq1, uint32_t seq2)
1711{
1712 return (int32_t)(seq1 - seq2);
1713}
1714
1725static inline bool net_tcp_seq_greater(uint32_t seq1, uint32_t seq2)
1726{
1727 return net_tcp_seq_cmp(seq1, seq2) > 0;
1728}
1729
1741int net_bytes_from_str(uint8_t *buf, int buf_len, const char *src);
1742
1752
1762
1771static inline enum net_priority net_vlan2priority(uint8_t priority)
1772{
1773 /* Map according to IEEE 802.1Q */
1774 static const uint8_t vlan2priority[] = {
1783 };
1784
1785 if (priority >= ARRAY_SIZE(vlan2priority)) {
1786 /* Use Best Effort as the default priority */
1787 return NET_PRIORITY_BE;
1788 }
1789
1790 return (enum net_priority)vlan2priority[priority];
1791}
1792
1800static inline uint8_t net_priority2vlan(enum net_priority priority)
1801{
1802 /* The conversion works both ways */
1803 return (uint8_t)net_vlan2priority(priority);
1804}
1805
1814const char *net_family2str(sa_family_t family);
1815
1826#if defined(CONFIG_NET_IPV6_PE)
1827int net_ipv6_pe_add_filter(struct in6_addr *addr, bool is_denylist);
1828#else
1829static inline int net_ipv6_pe_add_filter(struct in6_addr *addr,
1830 bool is_denylist)
1831{
1832 ARG_UNUSED(addr);
1833 ARG_UNUSED(is_denylist);
1834
1835 return -ENOTSUP;
1836}
1837#endif /* CONFIG_NET_IPV6_PE */
1838
1846#if defined(CONFIG_NET_IPV6_PE)
1847int net_ipv6_pe_del_filter(struct in6_addr *addr);
1848#else
1849static inline int net_ipv6_pe_del_filter(struct in6_addr *addr)
1850{
1851 ARG_UNUSED(addr);
1852
1853 return -ENOTSUP;
1854}
1855#endif /* CONFIG_NET_IPV6_PE */
1856
1857#ifdef __cplusplus
1858}
1859#endif
1860
1861#include <zephyr/syscalls/net_ip.h>
1862
1868#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:716
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:1396
static bool net_ipv4_addr_cmp(const struct in_addr *addr1, const struct in_addr *addr2)
Compare two IPv4 addresses.
Definition net_ip.h:927
#define NET_IPV4_ADDR_SIZE
Binary size of the IPv4 address.
Definition net_ip.h:165
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:1283
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:1771
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:1829
static int32_t net_tcp_seq_cmp(uint32_t seq1, uint32_t seq2)
Compare TCP sequence numbers.
Definition net_ip.h:1710
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:698
#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:1427
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:530
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:941
static bool net_ipv6_addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2)
Compare two IPv6 addresses.
Definition net_ip.h:956
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:1338
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:1130
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:1199
static void net_ipv6_addr_copy_raw(uint8_t *dest, const uint8_t *src)
Copy an IPv6 address raw buffer.
Definition net_ip.h:913
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:1255
static struct sockaddr_in_ptr * net_sin_ptr(const struct sockaddr_ptr *addr)
Get sockaddr_in_ptr from sockaddr_ptr.
Definition net_ip.h:1602
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:784
#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:1454
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:1213
static void net_ipv6_addr_create_ll_allnodes_mcast(struct in6_addr *addr)
Create link local allnodes multicast IPv6 address.
Definition net_ip.h:1417
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:1165
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:1368
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:1298
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:1269
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:996
static void net_ipv6_addr_create_iid(struct in6_addr *addr, struct net_linkaddr *lladdr)
Create IPv6 address interface identifier.
Definition net_ip.h:1496
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:865
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:1241
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:1080
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:892
static int net_ipv6_pe_del_filter(struct in6_addr *addr)
Delete IPv6 prefix from privacy extension filter list.
Definition net_ip.h:1849
net_ip_mtu
IP Maximum Transfer Unit.
Definition net_ip.h:486
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 bool net_ipv4_is_addr_loopback(struct in_addr *addr)
Check if the IPv4 address is a loopback address (127.0.0.0/8).
Definition net_ip.h:815
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:1800
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:1438
bool net_if_ipv4_is_addr_bcast(struct net_if *iface, const struct in_addr *addr)
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:746
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...
int net_port_set_default(struct sockaddr *addr, uint16_t default_port)
Set the default port in the sockaddr structure.
static bool net_ipv6_is_addr_loopback(struct in6_addr *addr)
Check if the IPv6 address is a loopback address (::1).
Definition net_ip.h:683
static bool net_tcp_seq_greater(uint32_t seq1, uint32_t seq2)
Check that one TCP sequence number is greater.
Definition net_ip.h:1725
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:1021
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:1354
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:851
static struct sockaddr_can_ptr * net_can_ptr(const struct sockaddr_ptr *addr)
Get sockaddr_can_ptr from sockaddr_ptr.
Definition net_ip.h:1630
#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:1108
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:984
static struct sockaddr_in * net_sin(const struct sockaddr *addr)
Get sockaddr_in from sockaddr.
Definition net_ip.h:1574
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:1318
size_t socklen_t
Length of a socket address.
Definition net_ip.h:172
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:1035
static struct sockaddr_ll_ptr * net_sll_ptr(const struct sockaddr_ptr *addr)
Get sockaddr_ll_ptr from sockaddr_ptr.
Definition net_ip.h:1616
static struct sockaddr_in6 * net_sin6(const struct sockaddr *addr)
Get sockaddr_in6 from sockaddr.
Definition net_ip.h:1561
#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:827
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:1184
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:1009
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:1227
#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:1588
net_priority
Network packet priority settings described in IEEE 802.1Q Annex I.1.
Definition net_ip.h:507
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:839
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:1507
static void net_ipv4_addr_copy_raw(uint8_t *dest, const uint8_t *src)
Copy an IPv4 address raw buffer.
Definition net_ip.h:901
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:732
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:970
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:1149
net_addr_type
How the network address is assigned to network interface.
Definition net_ip.h:538
@ NET_ADDR_ANY_STATE
Default (invalid) address type.
Definition net_ip.h:531
@ NET_ADDR_TENTATIVE
Tentative address
Definition net_ip.h:532
@ NET_ADDR_DEPRECATED
Deprecated address
Definition net_ip.h:534
@ NET_ADDR_PREFERRED
Preferred address
Definition net_ip.h:533
@ 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:502
@ NET_IPV6_MTU
IPv6 MTU length.
Definition net_ip.h:493
@ 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:515
@ NET_PRIORITY_IC
Internetwork control
Definition net_ip.h:514
@ NET_PRIORITY_CA
Critical applications
Definition net_ip.h:511
@ NET_PRIORITY_VO
Voice, < 10 ms latency and jitter
Definition net_ip.h:513
@ NET_PRIORITY_VI
Video, < 100 ms latency and jitter.
Definition net_ip.h:512
@ NET_PRIORITY_BE
Best effort (default)
Definition net_ip.h:509
@ NET_PRIORITY_EE
Excellent effort
Definition net_ip.h:510
@ NET_PRIORITY_BK
Background (lowest)
Definition net_ip.h:508
@ 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:540
@ NET_ADDR_OVERRIDABLE
Manually set address which is overridable by DHCP.
Definition net_ip.h:548
@ NET_ADDR_DHCP
Address is from DHCP.
Definition net_ip.h:544
@ NET_ADDR_MANUAL
Manually set address.
Definition net_ip.h:546
@ NET_ADDR_AUTOCONF
Auto configured address.
Definition net_ip.h:542
@ NET_LINK_ETHERNET
Ethernet link address.
Definition net_linkaddr.h:57
#define ARRAY_SIZE(array)
Number of elements in the given array.
Definition util.h:120
#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:585
Network Interface multicast IP addresses.
Definition net_if.h:160
Network Interface structure.
Definition net_if.h:714
Hardware link address structure.
Definition net_linkaddr.h:74
uint8_t addr[6]
The array of bytes representing the address.
Definition net_linkaddr.h:82
uint8_t type
What kind of address is this for.
Definition net_linkaddr.h:76
uint8_t len
The real length of the ll address.
Definition net_linkaddr.h:79
IPv6/IPv4 network connection tuple.
Definition net_ip.h:521
struct net_addr * remote_addr
IPv6/IPv4 remote address.
Definition net_ip.h:522
uint16_t local_port
UDP/TCP local port
Definition net_ip.h:525
enum net_ip_protocol ip_proto
IP protocol
Definition net_ip.h:526
uint16_t remote_port
UDP/TCP remote port
Definition net_ip.h:524
struct net_addr * local_addr
IPv6/IPv4 local address
Definition net_ip.h:523
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:408
sa_family_t sa_family
Address family.
Definition net_ip.h:409
Byte order helpers.
Misc utilities.
Macros to abstract toolchain specific capabilities.