From patchwork Fri Dec 2 09:17:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Vyacheslav V. Mitrofanov" X-Patchwork-Id: 1711308 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=yadro.com header.i=@yadro.com header.a=rsa-sha256 header.s=mta-01 header.b=hycR/4U9; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NNnSn30cHz23nC for ; Fri, 2 Dec 2022 20:19:21 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6650885423; Fri, 2 Dec 2022 10:19:03 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=yadro.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=yadro.com header.i=@yadro.com header.b="hycR/4U9"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BE7928503A; Fri, 2 Dec 2022 10:18:54 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mta-01.yadro.com (mta-02.yadro.com [89.207.88.252]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 914BF85221 for ; Fri, 2 Dec 2022 10:18:47 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=yadro.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=v.v.mitrofanov@yadro.com Received: from localhost (unknown [127.0.0.1]) by mta-01.yadro.com (Postfix) with ESMTP id 3D3E440889; Fri, 2 Dec 2022 09:18:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=yadro.com; h= content-type:content-type:content-transfer-encoding:mime-version :references:in-reply-to:x-mailer:message-id:date:date:subject :subject:from:from:received:received:received:received; s= mta-01; t=1669972725; x=1671787126; bh=xZhqW8TgRsuUzFV5WMxDlBOj8 DKHFKcvVbcI4McWeSM=; b=hycR/4U9V1d+ycuIFL03SFL5QSg1sQOHXfnDYj+sV K3991xnw78JXYt65VF2jOIdSdWpmFvnso0To1zAe0cAK81T/iFbb6RKa96CbAgfp fQhbOqt66WHNVsbjnNUHMnrJ8Z53z2cHwfN+5+9dZWGB/8H8vi9/G4wCTpnQYThT vs= Received: from mta-01.yadro.com ([127.0.0.1]) by localhost (mta-01.yadro.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 59hbyTqjyIZb; Fri, 2 Dec 2022 12:18:45 +0300 (MSK) Received: from T-EXCH-01.corp.yadro.com (T-EXCH-01.corp.yadro.com [172.17.10.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mta-01.yadro.com (Postfix) with ESMTPS id 559BA4014D; Fri, 2 Dec 2022 12:18:45 +0300 (MSK) Received: from T-Exch-05.corp.yadro.com (172.17.10.109) by T-EXCH-01.corp.yadro.com (172.17.10.101) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.669.32; Fri, 2 Dec 2022 12:18:45 +0300 Received: from v.yadro.com (10.178.114.10) by T-Exch-05.corp.yadro.com (172.17.10.109) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.1118.9; Fri, 2 Dec 2022 12:18:44 +0300 From: Viacheslav Mitrofanov To: CC: , , , , , , Subject: [PATCH v5 01/19] net: ipv6: Add IPv6 basic primitives Date: Fri, 2 Dec 2022 12:17:58 +0300 Message-ID: <20221202091818.2382748-2-v.v.mitrofanov@yadro.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221202091818.2382748-1-v.v.mitrofanov@yadro.com> References: <20221202091818.2382748-1-v.v.mitrofanov@yadro.com> MIME-Version: 1.0 X-Originating-IP: [10.178.114.10] X-ClientProxiedBy: T-EXCH-01.corp.yadro.com (172.17.10.101) To T-Exch-05.corp.yadro.com (172.17.10.109) X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean This patch is a collection of basic primitives that are prerequisite for further IPv6 implementation. There are structures definition such as IPv6 header, UDP header (for TFTP), ICMPv6 header. There are auxiliary defines such as protocol codes, padding, struct size and etc. Also here are functions prototypes and its empty implementation that will be used as API for further patches. Here are variables declaration such as IPv6 address of our host, gateway, ipv6 server. Series-changes: 3 - Added functions and structures descriptions - Removed enums ND_OPT_*. It will be moved into further patches - Substituted -1 for error codes Series-changes: 4 - Changed functions and structures description style Signed-off-by: Viacheslav Mitrofanov Reviewed-by: Ramon Fried Reviewed-by: Simon Glass --- include/net6.h | 369 +++++++++++++++++++++++++++++++++++++++++++++++++ net/net6.c | 31 +++++ 2 files changed, 400 insertions(+) create mode 100644 include/net6.h create mode 100644 net/net6.c diff --git a/include/net6.h b/include/net6.h new file mode 100644 index 0000000000..9200eae04e --- /dev/null +++ b/include/net6.h @@ -0,0 +1,369 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (C) 2013 Allied Telesis Labs NZ + * Chris Packham, + * + * Copyright (C) 2022 YADRO + * Viacheslav Mitrofanov + */ + +#ifndef __NET6_H__ +#define __NET6_H__ + +#include +#include + +/* struct in6_addr - 128 bits long IPv6 address */ +struct in6_addr { + union { + u8 u6_addr8[16]; + __be16 u6_addr16[8]; + __be32 u6_addr32[4]; + } in6_u; + +#define s6_addr in6_u.u6_addr8 +#define s6_addr16 in6_u.u6_addr16 +#define s6_addr32 in6_u.u6_addr32 +}; + +#define IN6ADDRSZ sizeof(struct in6_addr) +#define INETHADDRSZ sizeof(net_ethaddr) + +#define PROT_IP6 0x86DD /* IPv6 protocol */ +#define PROT_ICMPV6 58 /* ICMPv6 protocol*/ + +#define IPV6_ADDRSCOPE_INTF 0x01 +#define IPV6_ADDRSCOPE_LINK 0x02 +#define IPV6_ADDRSCOPE_AMDIN 0x04 +#define IPV6_ADDRSCOPE_SITE 0x05 +#define IPV6_ADDRSCOPE_ORG 0x08 +#define IPV6_ADDRSCOPE_GLOBAL 0x0E + +#define USE_IP6_CMD_PARAM "-ipv6" + +/** + * struct ipv6hdr - Internet Protocol V6 (IPv6) header. + * + * IPv6 packet header as defined in RFC 2460. + */ +struct ip6_hdr { +#if defined(__LITTLE_ENDIAN_BITFIELD) + u8 priority:4, + version:4; +#elif defined(__BIG_ENDIAN_BITFIELD) + u8 version:4, + priority:4; +#else +#error "Please fix " +#endif + u8 flow_lbl[3]; + __be16 payload_len; + u8 nexthdr; + u8 hop_limit; + struct in6_addr saddr; + struct in6_addr daddr; +}; +#define IP6_HDR_SIZE (sizeof(struct ip6_hdr)) + +/* struct udp_hdr - User Datagram Protocol header */ +struct udp_hdr { + u16 udp_src; /* UDP source port */ + u16 udp_dst; /* UDP destination port */ + u16 udp_len; /* Length of UDP packet */ + u16 udp_xsum; /* Checksum */ +} __packed; + +/* + * Handy for static initialisations of struct in6_addr, atlhough the + * c99 '= { 0 }' idiom might work depending on you compiler. + */ +#define ZERO_IPV6_ADDR { { { 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00 } } } + +#define IPV6_LINK_LOCAL_PREFIX 0xfe80 + +/* hop limit for neighbour discovery packets */ +#define IPV6_NDISC_HOPLIMIT 255 +#define NDISC_TIMEOUT 5000UL +#define NDISC_TIMEOUT_COUNT 3 + +/* struct icmp6hdr - Internet Control Message Protocol header for IPV6 */ +struct icmp6hdr { + u8 icmp6_type; +#define IPV6_ICMP_ECHO_REQUEST 128 +#define IPV6_ICMP_ECHO_REPLY 129 +#define IPV6_NDISC_ROUTER_SOLICITATION 133 +#define IPV6_NDISC_ROUTER_ADVERTISEMENT 134 +#define IPV6_NDISC_NEIGHBOUR_SOLICITATION 135 +#define IPV6_NDISC_NEIGHBOUR_ADVERTISEMENT 136 +#define IPV6_NDISC_REDIRECT 137 + u8 icmp6_code; + __be16 icmp6_cksum; + + /* ICMPv6 data */ + union { + __be32 un_data32[1]; + __be16 un_data16[2]; + u8 un_data8[4]; + + /* struct icmpv6_echo - echo request/reply message format */ + struct icmpv6_echo { + __be16 identifier; + __be16 sequence; + } u_echo; + + /* struct icmpv6_nd_advt - Neighbor Advertisement format */ + struct icmpv6_nd_advt { +#if defined(__LITTLE_ENDIAN_BITFIELD) + __be32 reserved:5, + override:1, + solicited:1, + router:1, + reserved2:24; +#elif defined(__BIG_ENDIAN_BITFIELD) + __be32 router:1, + solicited:1, + override:1, + reserved:29; +#else +#error "Please fix " +#endif + } u_nd_advt; + + /* struct icmpv6_nd_ra - Router Advertisement format */ + struct icmpv6_nd_ra { + u8 hop_limit; +#if defined(__LITTLE_ENDIAN_BITFIELD) + u8 reserved:6, + other:1, + managed:1; + +#elif defined(__BIG_ENDIAN_BITFIELD) + u8 managed:1, + other:1, + reserved:6; +#else +#error "Please fix " +#endif + __be16 rt_lifetime; + } u_nd_ra; + } icmp6_dataun; +#define icmp6_identifier icmp6_dataun.u_echo.identifier +#define icmp6_sequence icmp6_dataun.u_echo.sequence +#define icmp6_pointer icmp6_dataun.un_data32[0] +#define icmp6_mtu icmp6_dataun.un_data32[0] +#define icmp6_unused icmp6_dataun.un_data32[0] +#define icmp6_maxdelay icmp6_dataun.un_data16[0] +#define icmp6_router icmp6_dataun.u_nd_advt.router +#define icmp6_solicited icmp6_dataun.u_nd_advt.solicited +#define icmp6_override icmp6_dataun.u_nd_advt.override +#define icmp6_ndiscreserved icmp6_dataun.u_nd_advt.reserved +#define icmp6_hop_limit icmp6_dataun.u_nd_ra.hop_limit +#define icmp6_addrconf_managed icmp6_dataun.u_nd_ra.managed +#define icmp6_addrconf_other icmp6_dataun.u_nd_ra.other +#define icmp6_rt_lifetime icmp6_dataun.u_nd_ra.rt_lifetime +}; + +extern struct in6_addr const net_null_addr_ip6; /* NULL IPv6 address */ +extern struct in6_addr net_gateway6; /* Our gateways IPv6 address */ +extern struct in6_addr net_ip6; /* Our IPv6 addr (0 = unknown) */ +extern struct in6_addr net_link_local_ip6; /* Our link local IPv6 addr */ +extern u32 net_prefix_length; /* Our prefixlength (0 = unknown) */ +extern struct in6_addr net_server_ip6; /* Server IPv6 addr (0 = unknown) */ +extern bool use_ip6; + +/** + * string_to_ip6() - Convert IPv6 string addr to inner IPV6 addr format + * + * Examples of valid strings: + * 2001:db8::0:1234:1 + * 2001:0db8:0000:0000:0000:0000:1234:0001 + * ::1 + * ::ffff:192.168.1.1 + * + * Examples of invalid strings + * 2001:db8::0::0 (:: can only appear once) + * 2001:db8:192.168.1.1::1 (v4 part can only appear at the end) + * 192.168.1.1 (we don't implicity map v4) + * + * @s: IPv6 string addr format + * @len: IPv6 string addr length + * @addr: converted IPv6 addr + * Return: 0 if conversion successful, -EINVAL if fail + */ +static inline int +string_to_ip6(const char *s, size_t len, struct in6_addr *addr) +{ + return -EINVAL; +} + +/** + * ip6_is_unspecified_addr() - Check if IPv6 addr is not set i.e. is zero + * + * @addr: IPv6 addr + * Return: 0 if addr is not set, -1 if is set + */ +static inline int ip6_is_unspecified_addr(struct in6_addr *addr) +{ + return -1; +} + +/** + * ip6_is_our_addr() - Check if IPv6 addr belongs to our host addr + * + * We have 2 addresses that we should respond to. A link local address and a + * global address. This returns true if the specified address matches either + * of these. + * + * @addr: addr to check + * Return: 0 if addr is our, -1 otherwise + */ +static inline int ip6_is_our_addr(struct in6_addr *addr) +{ + return -1; +} + +/** + * ip6_addr_in_subnet() - Check if two IPv6 addresses are in the same subnet + * + * @our_addr: first IPv6 addr + * @neigh_addr: second IPv6 addr + * @prefix_length: network mask length + * Return: 0 if two addresses in the same subnet, -1 otherwise + */ +static inline int +ip6_addr_in_subnet(struct in6_addr *our_addr, struct in6_addr *neigh_addr, + u32 prefix_length) +{ + return -1; +} + +/** + * ip6_make_lladd() - rMake up IPv6 Link Local address + * + * @lladdr: formed IPv6 Link Local address + * @enetaddr: MAC addr of a device + */ +static inline void +ip6_make_lladdr(struct in6_addr *lladdr, unsigned char const enetaddr[6]) +{ +} + +/** + * ip6_make_snma() - aMake up Solicited Node Multicast Address from IPv6 addr + * + * @mcast_addr: formed SNMA addr + * @ip6_addr: base IPv6 addr + */ +static inline void +ip6_make_snma(struct in6_addr *mcast_addr, struct in6_addr *ip6_addr) +{ +} + +/** + * ip6_make_mult_ethdstaddr() - Make up IPv6 multicast addr + * + * @enetaddr: MAC addr of a device + * @mcast_addr: formed IPv6 multicast addr + */ +static inline void +ip6_make_mult_ethdstaddr(unsigned char enetaddr[6], + struct in6_addr *mcast_addr) +{ +} + +/** + * csum_partial() - Compute an internet checksum + * + * @buff: buffer to be checksummed + * @len: length of buffer + * @sum: initial sum to be added in + * Return: internet checksum of the buffer + */ +static inline unsigned int +csum_partial(const unsigned char *buff, int len, unsigned int sum) +{ + return 0; +} + +/** + * csum_ipv6_magic() - Compute checksum of IPv6 "psuedo-header" per RFC2460 section 8.1 + * + * @saddr: source IPv6 addr + * @daddr: destination IPv6 add + * @len: data length to be checksumed + * @proto: IPv6 above protocol code + * @csum: upper layer checksum + * Return: computed checksum + */ +static inline unsigned short +csum_ipv6_magic(struct in6_addr *saddr, + struct in6_addr *daddr, u16 len, + unsigned short proto, unsigned int csum) +{ + return 0; +} + +/** + * ip6_add_hdr() - Make up IPv6 header + * + * @xip: pointer to IPv6 header to be formed + * @src: source IPv6 addr + * @dest: destination IPv6 addr + * @nextheader: next header type + * @hoplimit: hop limit + * @payload_len: payload length + * Return: IPv6 header length + */ +static inline unsigned int +ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest, + int nextheader, int hoplimit, int payload_len) +{ + return 0; +} + +/** + * net_send_udp_packet6() - Make up UDP packet and send it + * + * @ether: destination MAC addr + * @dest: destination IPv6 addr + * @dport: destination port + * @sport: source port + * @len: UDP packet length + * Return: 0 if send successfully, -1 otherwise + */ +static inline int +net_send_udp_packet6(uchar *ether, struct in6_addr *dest, + int dport, int sport, int len) +{ + return -1; +} + +/** + * net_ip6_handler() - Handle IPv6 packet + * + * @et: pointer to the begining of the packet + * @ip6: pointer to the begining of IPv6 protocol + * @len: incoming packet len + * Return: 0 if handle packet successfully, -EINVAL in case of invalid protocol + */ +static inline int +net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6, + int len) +{ + return -EINVAL; +} + +/** + * net_copy_ip6() - Copy IPv6 addr + * + * @to: destination IPv6 addr + * @from: source IPv6 addr + */ +static inline void net_copy_ip6(void *to, const void *from) +{ +} + +#endif /* __NET6_H__ */ diff --git a/net/net6.c b/net/net6.c new file mode 100644 index 0000000000..7cd442e6e2 --- /dev/null +++ b/net/net6.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2013 Allied Telesis Labs NZ + * Chris Packham, + * + * Copyright (C) 2022 YADRO + * Viacheslav Mitrofanov + */ + +/* Simple IPv6 network layer implementation */ + +#include +#include +#include +#include +#include + +/* NULL IPv6 address */ +struct in6_addr const net_null_addr_ip6 = ZERO_IPV6_ADDR; +/* Our gateway's IPv6 address */ +struct in6_addr net_gateway6 = ZERO_IPV6_ADDR; +/* Our IPv6 addr (0 = unknown) */ +struct in6_addr net_ip6 = ZERO_IPV6_ADDR; +/* Our link local IPv6 addr (0 = unknown) */ +struct in6_addr net_link_local_ip6 = ZERO_IPV6_ADDR; +/* set server IPv6 addr (0 = unknown) */ +struct in6_addr net_server_ip6 = ZERO_IPV6_ADDR; +/* The prefix length of our network */ +u32 net_prefix_length; + +bool use_ip6;