12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556 |
- #ifndef ZEPHYR_INCLUDE_NET_NET_IP_H_
- #define ZEPHYR_INCLUDE_NET_NET_IP_H_
- #include <string.h>
- #include <zephyr/types.h>
- #include <stdbool.h>
- #include <sys/util.h>
- #include <sys/byteorder.h>
- #include <toolchain.h>
- #include <net/net_linkaddr.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- #define NET_VLAN_TAG_UNSPEC 0x0fff
- #define PF_UNSPEC 0
- #define PF_INET 1
- #define PF_INET6 2
- #define PF_PACKET 3
- #define PF_CAN 4
- #define PF_NET_MGMT 5
- #define PF_LOCAL 6
- #define PF_UNIX PF_LOCAL
- #define AF_UNSPEC PF_UNSPEC
- #define AF_INET PF_INET
- #define AF_INET6 PF_INET6
- #define AF_PACKET PF_PACKET
- #define AF_CAN PF_CAN
- #define AF_NET_MGMT PF_NET_MGMT
- #define AF_LOCAL PF_LOCAL
- #define AF_UNIX PF_UNIX
- enum net_ip_protocol {
- IPPROTO_IP = 0,
- IPPROTO_ICMP = 1,
- IPPROTO_IGMP = 2,
- IPPROTO_IPIP = 4,
- IPPROTO_TCP = 6,
- IPPROTO_UDP = 17,
- IPPROTO_IPV6 = 41,
- IPPROTO_ICMPV6 = 58,
- IPPROTO_RAW = 255,
- };
- enum net_ip_protocol_secure {
- IPPROTO_TLS_1_0 = 256,
- IPPROTO_TLS_1_1 = 257,
- IPPROTO_TLS_1_2 = 258,
- IPPROTO_DTLS_1_0 = 272,
- IPPROTO_DTLS_1_2 = 273,
- };
- enum net_sock_type {
- SOCK_STREAM = 1,
- SOCK_DGRAM,
- SOCK_RAW
- };
- #define ntohs(x) sys_be16_to_cpu(x)
- #define ntohl(x) sys_be32_to_cpu(x)
- #define ntohll(x) sys_be64_to_cpu(x)
- #define htons(x) sys_cpu_to_be16(x)
- #define htonl(x) sys_cpu_to_be32(x)
- #define htonll(x) sys_cpu_to_be64(x)
- struct in6_addr {
- union {
- uint8_t s6_addr[16];
- uint16_t s6_addr16[8];
- uint32_t s6_addr32[4];
- };
- };
- struct in_addr {
- union {
- uint8_t s4_addr[4];
- uint16_t s4_addr16[2];
- uint32_t s4_addr32[1];
- uint32_t s_addr;
- };
- };
- typedef unsigned short int sa_family_t;
- typedef size_t socklen_t;
- struct sockaddr_in6 {
- sa_family_t sin6_family;
- uint16_t sin6_port;
- struct in6_addr sin6_addr;
- uint8_t sin6_scope_id;
- };
- struct sockaddr_in6_ptr {
- sa_family_t sin6_family;
- uint16_t sin6_port;
- struct in6_addr *sin6_addr;
- uint8_t sin6_scope_id;
- };
- struct sockaddr_in {
- sa_family_t sin_family;
- uint16_t sin_port;
- struct in_addr sin_addr;
- };
- struct sockaddr_in_ptr {
- sa_family_t sin_family;
- uint16_t sin_port;
- struct in_addr *sin_addr;
- };
- struct sockaddr_ll {
- sa_family_t sll_family;
- uint16_t sll_protocol;
- int sll_ifindex;
- uint16_t sll_hatype;
- uint8_t sll_pkttype;
- uint8_t sll_halen;
- uint8_t sll_addr[8];
- };
- struct sockaddr_ll_ptr {
- sa_family_t sll_family;
- uint16_t sll_protocol;
- int sll_ifindex;
- uint16_t sll_hatype;
- uint8_t sll_pkttype;
- uint8_t sll_halen;
- uint8_t *sll_addr;
- };
- struct sockaddr_can_ptr {
- sa_family_t can_family;
- int can_ifindex;
- };
- #if !defined(HAVE_IOVEC)
- struct iovec {
- void *iov_base;
- size_t iov_len;
- };
- #endif
- struct msghdr {
- void *msg_name;
- socklen_t msg_namelen;
- struct iovec *msg_iov;
- size_t msg_iovlen;
- void *msg_control;
- size_t msg_controllen;
- int msg_flags;
- };
- struct cmsghdr {
- socklen_t cmsg_len;
- int cmsg_level;
- int cmsg_type;
-
- z_max_align_t cmsg_data[];
- };
- #if !defined(ALIGN_H)
- #define ALIGN_H(x) ROUND_UP(x, __alignof__(struct cmsghdr))
- #endif
- #if !defined(ALIGN_D)
- #define ALIGN_D(x) ROUND_UP(x, __alignof__(z_max_align_t))
- #endif
- #if !defined(CMSG_FIRSTHDR)
- #define CMSG_FIRSTHDR(msghdr) \
- ((msghdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
- (struct cmsghdr *)((msghdr)->msg_control) : NULL)
- #endif
- #if !defined(CMSG_NXTHDR)
- #define CMSG_NXTHDR(msghdr, cmsg) \
- (((cmsg) == NULL) ? CMSG_FIRSTHDR(msghdr) : \
- (((uint8_t *)(cmsg) + ALIGN_H((cmsg)->cmsg_len) + \
- ALIGN_D(sizeof(struct cmsghdr)) > \
- (uint8_t *)((msghdr)->msg_control) + (msghdr)->msg_controllen) ? \
- NULL : \
- (struct cmsghdr *)((uint8_t *)(cmsg) + \
- ALIGN_H((cmsg)->cmsg_len))))
- #endif
- #if !defined(CMSG_DATA)
- #define CMSG_DATA(cmsg) ((uint8_t *)(cmsg) + ALIGN_D(sizeof(struct cmsghdr)))
- #endif
- #if !defined(CMSG_SPACE)
- #define CMSG_SPACE(length) (ALIGN_D(sizeof(struct cmsghdr)) + ALIGN_H(length))
- #endif
- #if !defined(CMSG_LEN)
- #define CMSG_LEN(length) (ALIGN_D(sizeof(struct cmsghdr)) + length)
- #endif
- #define PACKET_HOST 0
- #define PACKET_BROADCAST 1
- #define PACKET_MULTICAST 2
- #define PACKET_OTHERHOST 3
- #define PACKET_OUTGOING 4
- #define PACKET_LOOPBACK 5
- #define PACKET_FASTROUTE 6
- #if defined(CONFIG_NET_IPV4)
- #undef NET_SOCKADDR_MAX_SIZE
- #undef NET_SOCKADDR_PTR_MAX_SIZE
- #define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_in))
- #define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_in_ptr))
- #endif
- #if defined(CONFIG_NET_SOCKETS_PACKET)
- #undef NET_SOCKADDR_MAX_SIZE
- #undef NET_SOCKADDR_PTR_MAX_SIZE
- #define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_ll))
- #define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_ll_ptr))
- #endif
- #if defined(CONFIG_NET_IPV6)
- #undef NET_SOCKADDR_MAX_SIZE
- #define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_in6))
- #if !defined(CONFIG_NET_SOCKETS_PACKET)
- #undef NET_SOCKADDR_PTR_MAX_SIZE
- #define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_in6_ptr))
- #endif
- #endif
- #if !defined(CONFIG_NET_IPV4)
- #if !defined(CONFIG_NET_IPV6)
- #if !defined(CONFIG_NET_SOCKETS_PACKET)
- #define NET_SOCKADDR_MAX_SIZE (sizeof(struct sockaddr_in6))
- #define NET_SOCKADDR_PTR_MAX_SIZE (sizeof(struct sockaddr_in6_ptr))
- #endif
- #endif
- #endif
- struct sockaddr {
- sa_family_t sa_family;
- char data[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
- };
- struct sockaddr_ptr {
- sa_family_t family;
- char data[NET_SOCKADDR_PTR_MAX_SIZE - sizeof(sa_family_t)];
- };
- struct sockaddr_storage {
- sa_family_t ss_family;
- char data[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
- };
- struct sockaddr_un {
- sa_family_t sun_family;
- char sun_path[NET_SOCKADDR_MAX_SIZE - sizeof(sa_family_t)];
- };
- struct net_addr {
- sa_family_t family;
- union {
- struct in6_addr in6_addr;
- struct in_addr in_addr;
- };
- };
- #define IN6ADDR_ANY_INIT { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, \
- 0, 0, 0, 0, 0, 0, 0 } } }
- #define IN6ADDR_LOOPBACK_INIT { { { 0, 0, 0, 0, 0, 0, 0, \
- 0, 0, 0, 0, 0, 0, 0, 0, 1 } } }
- extern const struct in6_addr in6addr_any;
- extern const struct in6_addr in6addr_loopback;
- #define INET_ADDRSTRLEN 16
- #define INET6_ADDRSTRLEN 46
- #define NET_IPV6_ADDR_LEN sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")
- #define NET_IPV4_ADDR_LEN sizeof("xxx.xxx.xxx.xxx")
- #define INADDR_ANY 0
- #define INADDR_ANY_INIT { { { INADDR_ANY } } }
- enum net_ip_mtu {
-
- NET_IPV6_MTU = 1280,
-
- NET_IPV4_MTU = 576,
- };
- enum net_priority {
- NET_PRIORITY_BK = 1,
- NET_PRIORITY_BE = 0,
- NET_PRIORITY_EE = 2,
- NET_PRIORITY_CA = 3,
- NET_PRIORITY_VI = 4,
- NET_PRIORITY_VO = 5,
- NET_PRIORITY_IC = 6,
- NET_PRIORITY_NC = 7
- } __packed;
- #define NET_MAX_PRIORITIES 8
- struct net_tuple {
- struct net_addr *remote_addr;
- struct net_addr *local_addr;
- uint16_t remote_port;
- uint16_t local_port;
- enum net_ip_protocol ip_proto;
- };
- enum net_addr_state {
- NET_ADDR_ANY_STATE = -1,
- NET_ADDR_TENTATIVE = 0,
- NET_ADDR_PREFERRED,
- NET_ADDR_DEPRECATED,
- } __packed;
- enum net_addr_type {
-
- NET_ADDR_ANY = 0,
-
- NET_ADDR_AUTOCONF,
-
- NET_ADDR_DHCP,
-
- NET_ADDR_MANUAL,
-
- NET_ADDR_OVERRIDABLE,
- } __packed;
- struct net_ipv6_hdr {
- uint8_t vtc;
- uint8_t tcflow;
- uint16_t flow;
- uint16_t len;
- uint8_t nexthdr;
- uint8_t hop_limit;
- struct in6_addr src;
- struct in6_addr dst;
- } __packed;
- struct net_ipv6_frag_hdr {
- uint8_t nexthdr;
- uint8_t reserved;
- uint16_t offset;
- uint32_t id;
- } __packed;
- struct net_ipv4_hdr {
- uint8_t vhl;
- uint8_t tos;
- uint16_t len;
- uint8_t id[2];
- uint8_t offset[2];
- uint8_t ttl;
- uint8_t proto;
- uint16_t chksum;
- struct in_addr src;
- struct in_addr dst;
- } __packed;
- struct net_icmp_hdr {
- uint8_t type;
- uint8_t code;
- uint16_t chksum;
- } __packed;
- struct net_udp_hdr {
- uint16_t src_port;
- uint16_t dst_port;
- uint16_t len;
- uint16_t chksum;
- } __packed;
- struct net_tcp_hdr {
- uint16_t src_port;
- uint16_t dst_port;
- uint8_t seq[4];
- uint8_t ack[4];
- uint8_t offset;
- uint8_t flags;
- uint8_t wnd[2];
- uint16_t chksum;
- uint8_t urg[2];
- uint8_t optdata[0];
- } __packed;
- static inline const char *net_addr_type2str(enum net_addr_type type)
- {
- switch (type) {
- case NET_ADDR_AUTOCONF:
- return "AUTO";
- case NET_ADDR_DHCP:
- return "DHCP";
- case NET_ADDR_MANUAL:
- return "MANUAL";
- case NET_ADDR_OVERRIDABLE:
- return "OVERRIDE";
- case NET_ADDR_ANY:
- default:
- break;
- }
- return "<unknown>";
- }
- #define NET_IPV6_NEXTHDR_HBHO 0
- #define NET_IPV6_NEXTHDR_DESTO 60
- #define NET_IPV6_NEXTHDR_ROUTING 43
- #define NET_IPV6_NEXTHDR_FRAG 44
- #define NET_IPV6_NEXTHDR_NONE 59
- union net_ip_header {
- struct net_ipv4_hdr *ipv4;
- struct net_ipv6_hdr *ipv6;
- };
- union net_proto_header {
- struct net_udp_hdr *udp;
- struct net_tcp_hdr *tcp;
- };
- #define NET_UDPH_LEN 8
- #define NET_TCPH_LEN 20
- #define NET_ICMPH_LEN 4
- #define NET_IPV6H_LEN 40
- #define NET_ICMPV6H_LEN NET_ICMPH_LEN
- #define NET_IPV6UDPH_LEN (NET_UDPH_LEN + NET_IPV6H_LEN)
- #define NET_IPV6TCPH_LEN (NET_TCPH_LEN + NET_IPV6H_LEN)
- #define NET_IPV6ICMPH_LEN (NET_IPV6H_LEN + NET_ICMPH_LEN)
- #define NET_IPV6_FRAGH_LEN 8
- #define NET_IPV4H_LEN 20
- #define NET_ICMPV4H_LEN NET_ICMPH_LEN
- #define NET_IPV4UDPH_LEN (NET_UDPH_LEN + NET_IPV4H_LEN)
- #define NET_IPV4TCPH_LEN (NET_TCPH_LEN + NET_IPV4H_LEN)
- #define NET_IPV4ICMPH_LEN (NET_IPV4H_LEN + NET_ICMPH_LEN)
- #define NET_IPV6H_LENGTH_OFFSET 0x04
- #define NET_IPV6_FRAGH_OFFSET_MASK 0xfff8
- static inline bool net_ipv6_is_addr_loopback(struct in6_addr *addr)
- {
- return UNALIGNED_GET(&addr->s6_addr32[0]) == 0 &&
- UNALIGNED_GET(&addr->s6_addr32[1]) == 0 &&
- UNALIGNED_GET(&addr->s6_addr32[2]) == 0 &&
- ntohl(UNALIGNED_GET(&addr->s6_addr32[3])) == 1;
- }
- static inline bool net_ipv6_is_addr_mcast(const struct in6_addr *addr)
- {
- return addr->s6_addr[0] == 0xFF;
- }
- struct net_if;
- struct net_if_config;
- extern struct net_if_addr *net_if_ipv6_addr_lookup(const struct in6_addr *addr,
- struct net_if **iface);
- static inline bool net_ipv6_is_my_addr(struct in6_addr *addr)
- {
- return net_if_ipv6_addr_lookup(addr, NULL) != NULL;
- }
- extern struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(
- const struct in6_addr *addr, struct net_if **iface);
- static inline bool net_ipv6_is_my_maddr(struct in6_addr *maddr)
- {
- return net_if_ipv6_maddr_lookup(maddr, NULL) != NULL;
- }
- static inline bool net_ipv6_is_prefix(const uint8_t *addr1,
- const uint8_t *addr2,
- uint8_t length)
- {
- uint8_t bits = 128 - length;
- uint8_t bytes = length / 8U;
- uint8_t remain = bits % 8;
- uint8_t mask;
- if (length > 128) {
- return false;
- }
- if (memcmp(addr1, addr2, bytes)) {
- return false;
- }
- if (!remain) {
-
- return true;
- }
-
- mask = ((0xff << (8 - remain)) ^ 0xff) << remain;
- return (addr1[bytes] & mask) == (addr2[bytes] & mask);
- }
- static inline bool net_ipv4_is_addr_loopback(struct in_addr *addr)
- {
- return addr->s4_addr[0] == 127U;
- }
- static inline bool net_ipv4_is_addr_unspecified(const struct in_addr *addr)
- {
- return UNALIGNED_GET(&addr->s_addr) == 0;
- }
- static inline bool net_ipv4_is_addr_mcast(const struct in_addr *addr)
- {
- return (ntohl(UNALIGNED_GET(&addr->s_addr)) & 0xF0000000) == 0xE0000000;
- }
- static inline bool net_ipv4_is_ll_addr(const struct in_addr *addr)
- {
- return (ntohl(UNALIGNED_GET(&addr->s_addr)) & 0xA9FE0000) == 0xA9FE0000;
- }
- #define net_ipaddr_copy(dest, src) \
- UNALIGNED_PUT(UNALIGNED_GET(src), dest)
- static inline bool net_ipv4_addr_cmp(const struct in_addr *addr1,
- const struct in_addr *addr2)
- {
- return UNALIGNED_GET(&addr1->s_addr) == UNALIGNED_GET(&addr2->s_addr);
- }
- static inline bool net_ipv6_addr_cmp(const struct in6_addr *addr1,
- const struct in6_addr *addr2)
- {
- return !memcmp(addr1, addr2, sizeof(struct in6_addr));
- }
- static inline bool net_ipv6_is_ll_addr(const struct in6_addr *addr)
- {
- return UNALIGNED_GET(&addr->s6_addr16[0]) == htons(0xFE80);
- }
- static inline bool net_ipv6_is_ula_addr(const struct in6_addr *addr)
- {
- return addr->s6_addr[0] == 0xFD;
- }
- const struct in6_addr *net_ipv6_unspecified_address(void);
- const struct in_addr *net_ipv4_unspecified_address(void);
- const struct in_addr *net_ipv4_broadcast_address(void);
- struct net_if;
- extern bool net_if_ipv4_addr_mask_cmp(struct net_if *iface,
- const struct in_addr *addr);
- static inline bool net_ipv4_addr_mask_cmp(struct net_if *iface,
- const struct in_addr *addr)
- {
- return net_if_ipv4_addr_mask_cmp(iface, addr);
- }
- extern bool net_if_ipv4_is_addr_bcast(struct net_if *iface,
- const struct in_addr *addr);
- #if defined(CONFIG_NET_NATIVE_IPV4)
- static inline bool net_ipv4_is_addr_bcast(struct net_if *iface,
- const struct in_addr *addr)
- {
- if (net_ipv4_addr_cmp(addr, net_ipv4_broadcast_address())) {
- return true;
- }
- return net_if_ipv4_is_addr_bcast(iface, addr);
- }
- #else
- static inline bool net_ipv4_is_addr_bcast(struct net_if *iface,
- const struct in_addr *addr)
- {
- ARG_UNUSED(iface);
- ARG_UNUSED(addr);
- return false;
- }
- #endif
- extern struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr,
- struct net_if **iface);
- static inline bool net_ipv4_is_my_addr(const struct in_addr *addr)
- {
- bool ret;
- ret = net_if_ipv4_addr_lookup(addr, NULL) != NULL;
- if (!ret) {
- ret = net_ipv4_is_addr_bcast(NULL, addr);
- }
- return ret;
- }
- static inline bool net_ipv6_is_addr_unspecified(const struct in6_addr *addr)
- {
- return UNALIGNED_GET(&addr->s6_addr32[0]) == 0 &&
- UNALIGNED_GET(&addr->s6_addr32[1]) == 0 &&
- UNALIGNED_GET(&addr->s6_addr32[2]) == 0 &&
- UNALIGNED_GET(&addr->s6_addr32[3]) == 0;
- }
- static inline bool net_ipv6_is_addr_solicited_node(const struct in6_addr *addr)
- {
- return UNALIGNED_GET(&addr->s6_addr32[0]) == htonl(0xff020000) &&
- UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00000000 &&
- UNALIGNED_GET(&addr->s6_addr32[2]) == htonl(0x00000001) &&
- ((UNALIGNED_GET(&addr->s6_addr32[3]) & htonl(0xff000000)) ==
- htonl(0xff000000));
- }
- static inline bool net_ipv6_is_addr_mcast_scope(const struct in6_addr *addr,
- int scope)
- {
- return (addr->s6_addr[0] == 0xff) && (addr->s6_addr[1] == scope);
- }
- static inline bool net_ipv6_is_same_mcast_scope(const struct in6_addr *addr_1,
- const struct in6_addr *addr_2)
- {
- return (addr_1->s6_addr[0] == 0xff) && (addr_2->s6_addr[0] == 0xff) &&
- (addr_1->s6_addr[1] == addr_2->s6_addr[1]);
- }
- static inline bool net_ipv6_is_addr_mcast_global(const struct in6_addr *addr)
- {
- return net_ipv6_is_addr_mcast_scope(addr, 0x0e);
- }
- static inline bool net_ipv6_is_addr_mcast_iface(const struct in6_addr *addr)
- {
- return net_ipv6_is_addr_mcast_scope(addr, 0x01);
- }
- static inline bool net_ipv6_is_addr_mcast_link(const struct in6_addr *addr)
- {
- return net_ipv6_is_addr_mcast_scope(addr, 0x02);
- }
- static inline bool net_ipv6_is_addr_mcast_mesh(const struct in6_addr *addr)
- {
- return net_ipv6_is_addr_mcast_scope(addr, 0x03);
- }
- static inline bool net_ipv6_is_addr_mcast_site(const struct in6_addr *addr)
- {
- return net_ipv6_is_addr_mcast_scope(addr, 0x05);
- }
- static inline bool net_ipv6_is_addr_mcast_org(const struct in6_addr *addr)
- {
- return net_ipv6_is_addr_mcast_scope(addr, 0x08);
- }
- static inline bool net_ipv6_is_addr_mcast_group(const struct in6_addr *addr,
- const struct in6_addr *group)
- {
- return UNALIGNED_GET(&addr->s6_addr16[1]) == group->s6_addr16[1] &&
- UNALIGNED_GET(&addr->s6_addr16[2]) == group->s6_addr16[2] &&
- UNALIGNED_GET(&addr->s6_addr16[3]) == group->s6_addr16[3] &&
- UNALIGNED_GET(&addr->s6_addr32[1]) == group->s6_addr32[1] &&
- UNALIGNED_GET(&addr->s6_addr32[2]) == group->s6_addr32[1] &&
- UNALIGNED_GET(&addr->s6_addr32[3]) == group->s6_addr32[3];
- }
- static inline bool
- net_ipv6_is_addr_mcast_all_nodes_group(const struct in6_addr *addr)
- {
- static const struct in6_addr all_nodes_mcast_group = {
- { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } }
- };
- return net_ipv6_is_addr_mcast_group(addr, &all_nodes_mcast_group);
- }
- static inline bool
- net_ipv6_is_addr_mcast_iface_all_nodes(const struct in6_addr *addr)
- {
- return net_ipv6_is_addr_mcast_iface(addr) &&
- net_ipv6_is_addr_mcast_all_nodes_group(addr);
- }
- static inline bool
- net_ipv6_is_addr_mcast_link_all_nodes(const struct in6_addr *addr)
- {
- return net_ipv6_is_addr_mcast_link(addr) &&
- net_ipv6_is_addr_mcast_all_nodes_group(addr);
- }
- static inline
- void net_ipv6_addr_create_solicited_node(const struct in6_addr *src,
- struct in6_addr *dst)
- {
- dst->s6_addr[0] = 0xFF;
- dst->s6_addr[1] = 0x02;
- UNALIGNED_PUT(0, &dst->s6_addr16[1]);
- UNALIGNED_PUT(0, &dst->s6_addr16[2]);
- UNALIGNED_PUT(0, &dst->s6_addr16[3]);
- UNALIGNED_PUT(0, &dst->s6_addr16[4]);
- dst->s6_addr[10] = 0U;
- dst->s6_addr[11] = 0x01;
- dst->s6_addr[12] = 0xFF;
- dst->s6_addr[13] = src->s6_addr[13];
- UNALIGNED_PUT(UNALIGNED_GET(&src->s6_addr16[7]), &dst->s6_addr16[7]);
- }
- static inline 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)
- {
- UNALIGNED_PUT(htons(addr0), &addr->s6_addr16[0]);
- UNALIGNED_PUT(htons(addr1), &addr->s6_addr16[1]);
- UNALIGNED_PUT(htons(addr2), &addr->s6_addr16[2]);
- UNALIGNED_PUT(htons(addr3), &addr->s6_addr16[3]);
- UNALIGNED_PUT(htons(addr4), &addr->s6_addr16[4]);
- UNALIGNED_PUT(htons(addr5), &addr->s6_addr16[5]);
- UNALIGNED_PUT(htons(addr6), &addr->s6_addr16[6]);
- UNALIGNED_PUT(htons(addr7), &addr->s6_addr16[7]);
- }
- static inline void net_ipv6_addr_create_ll_allnodes_mcast(struct in6_addr *addr)
- {
- net_ipv6_addr_create(addr, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001);
- }
- static inline void net_ipv6_addr_create_ll_allrouters_mcast(struct in6_addr *addr)
- {
- net_ipv6_addr_create(addr, 0xff02, 0, 0, 0, 0, 0, 0, 0x0002);
- }
- static inline void net_ipv6_addr_create_iid(struct in6_addr *addr,
- struct net_linkaddr *lladdr)
- {
- UNALIGNED_PUT(htonl(0xfe800000), &addr->s6_addr32[0]);
- UNALIGNED_PUT(0, &addr->s6_addr32[1]);
- switch (lladdr->len) {
- case 2:
-
- if (lladdr->type == NET_LINK_IEEE802154 ||
- lladdr->type == NET_LINK_CANBUS) {
- UNALIGNED_PUT(0, &addr->s6_addr32[2]);
- addr->s6_addr[11] = 0xff;
- addr->s6_addr[12] = 0xfe;
- addr->s6_addr[13] = 0U;
- addr->s6_addr[14] = lladdr->addr[0];
- addr->s6_addr[15] = lladdr->addr[1];
- }
- break;
- case 6:
-
- memcpy(&addr->s6_addr[8], lladdr->addr, 3);
- addr->s6_addr[11] = 0xff;
- addr->s6_addr[12] = 0xfe;
- memcpy(&addr->s6_addr[13], lladdr->addr + 3, 3);
- #if defined(CONFIG_NET_L2_BT_ZEP1656)
-
- if (lladdr->type == NET_LINK_BLUETOOTH) {
- addr->s6_addr[8] ^= 0x02;
- }
- #endif
- if (lladdr->type == NET_LINK_ETHERNET) {
- addr->s6_addr[8] ^= 0x02;
- }
- break;
- case 8:
- memcpy(&addr->s6_addr[8], lladdr->addr, lladdr->len);
- addr->s6_addr[8] ^= 0x02;
- break;
- }
- }
- static inline bool net_ipv6_addr_based_on_ll(const struct in6_addr *addr,
- const struct net_linkaddr *lladdr)
- {
- if (!addr || !lladdr) {
- return false;
- }
- switch (lladdr->len) {
- case 2:
- if (!memcmp(&addr->s6_addr[14], lladdr->addr, lladdr->len) &&
- addr->s6_addr[8] == 0U &&
- addr->s6_addr[9] == 0U &&
- addr->s6_addr[10] == 0U &&
- addr->s6_addr[11] == 0xff &&
- addr->s6_addr[12] == 0xfe) {
- return true;
- }
- break;
- case 6:
- if (lladdr->type == NET_LINK_ETHERNET) {
- if (!memcmp(&addr->s6_addr[9], &lladdr->addr[1], 2) &&
- !memcmp(&addr->s6_addr[13], &lladdr->addr[3], 3) &&
- addr->s6_addr[11] == 0xff &&
- addr->s6_addr[12] == 0xfe &&
- (addr->s6_addr[8] ^ 0x02) == lladdr->addr[0]) {
- return true;
- }
- } else if (lladdr->type == NET_LINK_BLUETOOTH) {
- if (!memcmp(&addr->s6_addr[9], &lladdr->addr[1], 2) &&
- !memcmp(&addr->s6_addr[13], &lladdr->addr[3], 3) &&
- addr->s6_addr[11] == 0xff &&
- addr->s6_addr[12] == 0xfe
- #if defined(CONFIG_NET_L2_BT_ZEP1656)
-
- && (addr->s6_addr[8] ^ 0x02) == lladdr->addr[0]
- #endif
- ) {
- return true;
- }
- }
- break;
- case 8:
- if (!memcmp(&addr->s6_addr[9], &lladdr->addr[1],
- lladdr->len - 1) &&
- (addr->s6_addr[8] ^ 0x02) == lladdr->addr[0]) {
- return true;
- }
- break;
- }
- return false;
- }
- static inline struct sockaddr_in6 *net_sin6(const struct sockaddr *addr)
- {
- return (struct sockaddr_in6 *)addr;
- }
- static inline struct sockaddr_in *net_sin(const struct sockaddr *addr)
- {
- return (struct sockaddr_in *)addr;
- }
- static inline
- struct sockaddr_in6_ptr *net_sin6_ptr(const struct sockaddr_ptr *addr)
- {
- return (struct sockaddr_in6_ptr *)addr;
- }
- static inline
- struct sockaddr_in_ptr *net_sin_ptr(const struct sockaddr_ptr *addr)
- {
- return (struct sockaddr_in_ptr *)addr;
- }
- static inline
- struct sockaddr_ll_ptr *net_sll_ptr(const struct sockaddr_ptr *addr)
- {
- return (struct sockaddr_ll_ptr *)addr;
- }
- static inline
- struct sockaddr_can_ptr *net_can_ptr(const struct sockaddr_ptr *addr)
- {
- return (struct sockaddr_can_ptr *)addr;
- }
- __syscall int net_addr_pton(sa_family_t family, const char *src, void *dst);
- __syscall char *net_addr_ntop(sa_family_t family, const void *src,
- char *dst, size_t size);
- bool net_ipaddr_parse(const char *str, size_t str_len,
- struct sockaddr *addr);
- static inline int32_t net_tcp_seq_cmp(uint32_t seq1, uint32_t seq2)
- {
- return (int32_t)(seq1 - seq2);
- }
- static inline bool net_tcp_seq_greater(uint32_t seq1, uint32_t seq2)
- {
- return net_tcp_seq_cmp(seq1, seq2) > 0;
- }
- int net_bytes_from_str(uint8_t *buf, int buf_len, const char *src);
- int net_tx_priority2tc(enum net_priority prio);
- int net_rx_priority2tc(enum net_priority prio);
- static inline enum net_priority net_vlan2priority(uint8_t priority)
- {
-
- static const uint8_t vlan2priority[] = {
- NET_PRIORITY_BE,
- NET_PRIORITY_BK,
- NET_PRIORITY_EE,
- NET_PRIORITY_CA,
- NET_PRIORITY_VI,
- NET_PRIORITY_VO,
- NET_PRIORITY_IC,
- NET_PRIORITY_NC
- };
- if (priority >= ARRAY_SIZE(vlan2priority)) {
-
- return NET_PRIORITY_BE;
- }
- return (enum net_priority)vlan2priority[priority];
- }
- static inline uint8_t net_priority2vlan(enum net_priority priority)
- {
-
- return (uint8_t)net_vlan2priority(priority);
- }
- const char *net_family2str(sa_family_t family);
- #ifdef __cplusplus
- }
- #endif
- #include <syscalls/net_ip.h>
- #endif
|