blob: 2ceeaba063991cec9a30756f15eb0f6a39b02582 [file] [log] [blame]
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +03001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (C) 2013 Allied Telesis Labs NZ
4 * Chris Packham, <judge.packham@gmail.com>
5 *
6 * Copyright (C) 2022 YADRO
7 * Viacheslav Mitrofanov <v.v.mitrofanov@yadro.com>
8 */
9
10#ifndef __NET6_H__
11#define __NET6_H__
12
13#include <net.h>
14#include <linux/ctype.h>
Tom Rinia4a8ea92023-12-14 13:16:57 -050015#include <linux/errno.h>
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +030016
17/* struct in6_addr - 128 bits long IPv6 address */
18struct in6_addr {
19 union {
20 u8 u6_addr8[16];
21 __be16 u6_addr16[8];
22 __be32 u6_addr32[4];
23 } in6_u;
24
25#define s6_addr in6_u.u6_addr8
26#define s6_addr16 in6_u.u6_addr16
27#define s6_addr32 in6_u.u6_addr32
Sergei Antonovdc4d9c22023-01-18 20:52:18 +030028} __packed;
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +030029
30#define IN6ADDRSZ sizeof(struct in6_addr)
31#define INETHADDRSZ sizeof(net_ethaddr)
32
33#define PROT_IP6 0x86DD /* IPv6 protocol */
34#define PROT_ICMPV6 58 /* ICMPv6 protocol*/
35
36#define IPV6_ADDRSCOPE_INTF 0x01
37#define IPV6_ADDRSCOPE_LINK 0x02
38#define IPV6_ADDRSCOPE_AMDIN 0x04
39#define IPV6_ADDRSCOPE_SITE 0x05
40#define IPV6_ADDRSCOPE_ORG 0x08
41#define IPV6_ADDRSCOPE_GLOBAL 0x0E
42
43#define USE_IP6_CMD_PARAM "-ipv6"
44
45/**
46 * struct ipv6hdr - Internet Protocol V6 (IPv6) header.
47 *
48 * IPv6 packet header as defined in RFC 2460.
49 */
50struct ip6_hdr {
51#if defined(__LITTLE_ENDIAN_BITFIELD)
52 u8 priority:4,
53 version:4;
54#elif defined(__BIG_ENDIAN_BITFIELD)
55 u8 version:4,
56 priority:4;
57#else
58#error "Please fix <asm/byteorder.h>"
59#endif
60 u8 flow_lbl[3];
61 __be16 payload_len;
62 u8 nexthdr;
63 u8 hop_limit;
64 struct in6_addr saddr;
65 struct in6_addr daddr;
Sergei Antonovdc4d9c22023-01-18 20:52:18 +030066} __packed;
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +030067#define IP6_HDR_SIZE (sizeof(struct ip6_hdr))
68
69/* struct udp_hdr - User Datagram Protocol header */
70struct udp_hdr {
71 u16 udp_src; /* UDP source port */
72 u16 udp_dst; /* UDP destination port */
73 u16 udp_len; /* Length of UDP packet */
74 u16 udp_xsum; /* Checksum */
75} __packed;
76
77/*
78 * Handy for static initialisations of struct in6_addr, atlhough the
79 * c99 '= { 0 }' idiom might work depending on you compiler.
80 */
81#define ZERO_IPV6_ADDR { { { 0x00, 0x00, 0x00, 0x00, \
82 0x00, 0x00, 0x00, 0x00, \
83 0x00, 0x00, 0x00, 0x00, \
84 0x00, 0x00, 0x00, 0x00 } } }
Ehsan Mohandesia0d6d272023-04-21 17:08:21 -070085/*
86 * All-routers multicast address is the link-local scope address to reach all
87 * routers.
88 */
89#define ALL_ROUTERS_MULT_ADDR { { { 0xFF, 0x02, 0x00, 0x00, \
90 0x00, 0x00, 0x00, 0x00, \
91 0x00, 0x00, 0x00, 0x00, \
92 0x00, 0x00, 0x00, 0x02 } } }
Sean Edmond380ea552025-03-24 13:48:45 -070093/*
94 * With IPv6, the broadcast MAC address is not used. Instead, it should use
95 * the multicast address (see RFC RFC2464 section 7)
96 */
97#define IPV6_ALL_NODE_ETH_ADDR(_ip6_addr) {0x33, \
98 0x33, \
99 _ip6_addr.in6_u.u6_addr8[12], \
100 _ip6_addr.in6_u.u6_addr8[13], \
101 _ip6_addr.in6_u.u6_addr8[14], \
102 _ip6_addr.in6_u.u6_addr8[15]}
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300103
104#define IPV6_LINK_LOCAL_PREFIX 0xfe80
Ehsan Mohandesia0d6d272023-04-21 17:08:21 -0700105#define IPV6_LINK_LOCAL_MASK 0xffb0 /* The first 10-bit of address mask. */
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300106
107/* hop limit for neighbour discovery packets */
108#define IPV6_NDISC_HOPLIMIT 255
109#define NDISC_TIMEOUT 5000UL
110#define NDISC_TIMEOUT_COUNT 3
111
112/* struct icmp6hdr - Internet Control Message Protocol header for IPV6 */
113struct icmp6hdr {
114 u8 icmp6_type;
115#define IPV6_ICMP_ECHO_REQUEST 128
116#define IPV6_ICMP_ECHO_REPLY 129
117#define IPV6_NDISC_ROUTER_SOLICITATION 133
118#define IPV6_NDISC_ROUTER_ADVERTISEMENT 134
119#define IPV6_NDISC_NEIGHBOUR_SOLICITATION 135
120#define IPV6_NDISC_NEIGHBOUR_ADVERTISEMENT 136
121#define IPV6_NDISC_REDIRECT 137
122 u8 icmp6_code;
123 __be16 icmp6_cksum;
124
125 /* ICMPv6 data */
126 union {
127 __be32 un_data32[1];
128 __be16 un_data16[2];
129 u8 un_data8[4];
130
131 /* struct icmpv6_echo - echo request/reply message format */
132 struct icmpv6_echo {
133 __be16 identifier;
134 __be16 sequence;
135 } u_echo;
136
137 /* struct icmpv6_nd_advt - Neighbor Advertisement format */
138 struct icmpv6_nd_advt {
139#if defined(__LITTLE_ENDIAN_BITFIELD)
140 __be32 reserved:5,
141 override:1,
142 solicited:1,
143 router:1,
144 reserved2:24;
145#elif defined(__BIG_ENDIAN_BITFIELD)
146 __be32 router:1,
147 solicited:1,
148 override:1,
149 reserved:29;
150#else
151#error "Please fix <asm/byteorder.h>"
152#endif
153 } u_nd_advt;
154
155 /* struct icmpv6_nd_ra - Router Advertisement format */
156 struct icmpv6_nd_ra {
157 u8 hop_limit;
158#if defined(__LITTLE_ENDIAN_BITFIELD)
159 u8 reserved:6,
160 other:1,
161 managed:1;
162
163#elif defined(__BIG_ENDIAN_BITFIELD)
164 u8 managed:1,
165 other:1,
166 reserved:6;
167#else
168#error "Please fix <asm/byteorder.h>"
169#endif
170 __be16 rt_lifetime;
171 } u_nd_ra;
172 } icmp6_dataun;
173#define icmp6_identifier icmp6_dataun.u_echo.identifier
174#define icmp6_sequence icmp6_dataun.u_echo.sequence
175#define icmp6_pointer icmp6_dataun.un_data32[0]
176#define icmp6_mtu icmp6_dataun.un_data32[0]
177#define icmp6_unused icmp6_dataun.un_data32[0]
178#define icmp6_maxdelay icmp6_dataun.un_data16[0]
179#define icmp6_router icmp6_dataun.u_nd_advt.router
180#define icmp6_solicited icmp6_dataun.u_nd_advt.solicited
181#define icmp6_override icmp6_dataun.u_nd_advt.override
182#define icmp6_ndiscreserved icmp6_dataun.u_nd_advt.reserved
183#define icmp6_hop_limit icmp6_dataun.u_nd_ra.hop_limit
184#define icmp6_addrconf_managed icmp6_dataun.u_nd_ra.managed
185#define icmp6_addrconf_other icmp6_dataun.u_nd_ra.other
186#define icmp6_rt_lifetime icmp6_dataun.u_nd_ra.rt_lifetime
Sergei Antonovdc4d9c22023-01-18 20:52:18 +0300187} __packed;
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300188
Ehsan Mohandesia0d6d272023-04-21 17:08:21 -0700189/*
190 * struct icmp6_ra_prefix_info - Prefix Information option of the ICMPv6 message
191 * The Prefix Information option provides hosts with on-link prefixes and
192 * prefixes for Address Autoconfiguration. Refer to RFC 4861 for more info.
193 */
194struct icmp6_ra_prefix_info {
195 u8 type; /* Type is 3 for Prefix Information. */
196 u8 len; /* Len is 4 for Prefix Information. */
197 /* The number of leading bits in the Prefix that are valid. */
198 u8 prefix_len;
199 u8 reserved1:6, /* MUST be ignored by the receiver. */
200 aac:1, /* autonomous address-configuration flag */
201 /* Indicates that this prefix can be used for on-link determination. */
202 on_link:1;
203 /*
204 * The length of time in seconds that the prefix is valid for the
205 * purpose of on-link determination.
206 */
207 __be32 valid_lifetime;
208 /* The length of time addresses remain preferred. */
209 __be32 preferred_lifetime;
210 __be32 reserved2; /* MUST be ignored by the receiver. */
211 /*
212 * Prefix is an IP address or a prefix of an IP address. The Prefix
213 * Length field contains the number of valid leading bits in the prefix.
214 * The bits in the prefix after the prefix length are reserved and MUST
215 * be initialized to zero by the sender and ignored by the receiver.
216 */
217 struct in6_addr prefix;
Ehsan Mohandesib38292c2023-05-18 11:24:39 -0700218} __packed;
Ehsan Mohandesia0d6d272023-04-21 17:08:21 -0700219
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300220extern struct in6_addr const net_null_addr_ip6; /* NULL IPv6 address */
221extern struct in6_addr net_gateway6; /* Our gateways IPv6 address */
222extern struct in6_addr net_ip6; /* Our IPv6 addr (0 = unknown) */
223extern struct in6_addr net_link_local_ip6; /* Our link local IPv6 addr */
224extern u32 net_prefix_length; /* Our prefixlength (0 = unknown) */
225extern struct in6_addr net_server_ip6; /* Server IPv6 addr (0 = unknown) */
Viacheslav Mitrofanove03c8aa2022-12-02 12:18:08 +0300226extern struct in6_addr net_ping_ip6; /* the ipv6 address to ping */
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300227extern bool use_ip6;
228
Viacheslav Mitrofanovfb7968c2022-12-02 12:18:02 +0300229#if IS_ENABLED(CONFIG_IPV6)
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300230/**
231 * string_to_ip6() - Convert IPv6 string addr to inner IPV6 addr format
232 *
233 * Examples of valid strings:
234 * 2001:db8::0:1234:1
235 * 2001:0db8:0000:0000:0000:0000:1234:0001
236 * ::1
237 * ::ffff:192.168.1.1
238 *
239 * Examples of invalid strings
240 * 2001:db8::0::0 (:: can only appear once)
241 * 2001:db8:192.168.1.1::1 (v4 part can only appear at the end)
242 * 192.168.1.1 (we don't implicity map v4)
243 *
244 * @s: IPv6 string addr format
245 * @len: IPv6 string addr length
246 * @addr: converted IPv6 addr
247 * Return: 0 if conversion successful, -EINVAL if fail
248 */
Viacheslav Mitrofanovfb7968c2022-12-02 12:18:02 +0300249int string_to_ip6(const char *s, size_t len, struct in6_addr *addr);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300250
251/**
252 * ip6_is_unspecified_addr() - Check if IPv6 addr is not set i.e. is zero
253 *
254 * @addr: IPv6 addr
255 * Return: 0 if addr is not set, -1 if is set
256 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300257int ip6_is_unspecified_addr(struct in6_addr *addr);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300258
259/**
260 * ip6_is_our_addr() - Check if IPv6 addr belongs to our host addr
261 *
262 * We have 2 addresses that we should respond to. A link local address and a
263 * global address. This returns true if the specified address matches either
264 * of these.
265 *
266 * @addr: addr to check
267 * Return: 0 if addr is our, -1 otherwise
268 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300269int ip6_is_our_addr(struct in6_addr *addr);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300270
271/**
272 * ip6_addr_in_subnet() - Check if two IPv6 addresses are in the same subnet
273 *
274 * @our_addr: first IPv6 addr
275 * @neigh_addr: second IPv6 addr
276 * @prefix_length: network mask length
277 * Return: 0 if two addresses in the same subnet, -1 otherwise
278 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300279int ip6_addr_in_subnet(struct in6_addr *our_addr, struct in6_addr *neigh_addr,
280 u32 prefix_length);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300281
282/**
283 * ip6_make_lladd() - rMake up IPv6 Link Local address
284 *
285 * @lladdr: formed IPv6 Link Local address
286 * @enetaddr: MAC addr of a device
287 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300288void ip6_make_lladdr(struct in6_addr *lladr, unsigned char const enetaddr[6]);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300289
290/**
291 * ip6_make_snma() - aMake up Solicited Node Multicast Address from IPv6 addr
292 *
293 * @mcast_addr: formed SNMA addr
294 * @ip6_addr: base IPv6 addr
295 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300296void ip6_make_snma(struct in6_addr *mcast_addr, struct in6_addr *ip6_addr);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300297
298/**
299 * ip6_make_mult_ethdstaddr() - Make up IPv6 multicast addr
300 *
301 * @enetaddr: MAC addr of a device
302 * @mcast_addr: formed IPv6 multicast addr
303 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300304void ip6_make_mult_ethdstaddr(unsigned char enetaddr[6],
305 struct in6_addr *mcast_addr);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300306
307/**
308 * csum_partial() - Compute an internet checksum
309 *
310 * @buff: buffer to be checksummed
311 * @len: length of buffer
312 * @sum: initial sum to be added in
313 * Return: internet checksum of the buffer
314 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300315unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300316
317/**
318 * csum_ipv6_magic() - Compute checksum of IPv6 "psuedo-header" per RFC2460 section 8.1
319 *
320 * @saddr: source IPv6 addr
321 * @daddr: destination IPv6 add
322 * @len: data length to be checksummed
323 * @proto: IPv6 above protocol code
324 * @csum: upper layer checksum
325 * Return: computed checksum
326 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300327unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
328 struct in6_addr *daddr, u16 len,
329 unsigned short proto, unsigned int csum);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300330
331/**
332 * ip6_add_hdr() - Make up IPv6 header
333 *
334 * @xip: pointer to IPv6 header to be formed
335 * @src: source IPv6 addr
336 * @dest: destination IPv6 addr
337 * @nextheader: next header type
338 * @hoplimit: hop limit
339 * @payload_len: payload length
340 * Return: IPv6 header length
341 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300342int ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,
343 int nextheader, int hoplimit, int payload_len);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300344
345/**
346 * net_send_udp_packet6() - Make up UDP packet and send it
347 *
348 * @ether: destination MAC addr
349 * @dest: destination IPv6 addr
350 * @dport: destination port
351 * @sport: source port
352 * @len: UDP packet length
353 * Return: 0 if send successfully, -1 otherwise
354 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300355int net_send_udp_packet6(uchar *ether, struct in6_addr *dest, int dport,
356 int sport, int len);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300357
358/**
359 * net_ip6_handler() - Handle IPv6 packet
360 *
361 * @et: pointer to the beginning of the packet
362 * @ip6: pointer to the beginning of IPv6 protocol
363 * @len: incoming packet len
364 * Return: 0 if handle packet successfully, -EINVAL in case of invalid protocol
365 */
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300366int net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len);
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300367
368/**
369 * net_copy_ip6() - Copy IPv6 addr
370 *
371 * @to: destination IPv6 addr
372 * @from: source IPv6 addr
373 */
374static inline void net_copy_ip6(void *to, const void *from)
375{
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300376 memcpy((void *)to, from, sizeof(struct in6_addr));
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300377}
Viacheslav Mitrofanov99819ad2022-12-02 12:18:05 +0300378#else
379static inline int
380string_to_ip6(const char *s, size_t len, struct in6_addr *addr)
381{
382 return -EINVAL;
383}
384
385static inline int ip6_is_unspecified_addr(struct in6_addr *addr)
386{
387 return -1;
388}
389
390static inline int ip6_is_our_addr(struct in6_addr *addr)
391{
392 return -1;
393}
394
395static inline int
396ip6_addr_in_subnet(struct in6_addr *our_addr, struct in6_addr *neigh_addr,
397 u32 prefix_length)
398{
399 return -1;
400}
401
402static inline void
403ip6_make_lladdr(struct in6_addr *lladdr, unsigned char const enetaddr[6])
404{
405}
406
407static inline void
408ip6_make_snma(struct in6_addr *mcast_addr, struct in6_addr *ip6_addr)
409{
410}
411
412static inline void
413ip6_make_mult_ethdstaddr(unsigned char enetaddr[6],
414 struct in6_addr *mcast_addr)
415{
416}
417
418static inline unsigned int
419csum_partial(const unsigned char *buff, int len, unsigned int sum)
420{
421 return 0;
422}
423
424static inline unsigned short
425csum_ipv6_magic(struct in6_addr *saddr,
426 struct in6_addr *daddr, u16 len,
427 unsigned short proto, unsigned int csum)
428{
429 return 0;
430}
431
432static inline unsigned int
433ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,
434 int nextheader, int hoplimit, int payload_len)
435{
436 return 0;
437}
438
439static inline int
440net_send_udp_packet6(uchar *ether, struct in6_addr *dest,
441 int dport, int sport, int len)
442{
443 return -1;
444}
445
446static inline int
447net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6,
448 int len)
449{
450 return -EINVAL;
451}
452
453static inline void net_copy_ip6(void *to, const void *from)
454{
455}
456#endif
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300457
Viacheslav Mitrofanove03c8aa2022-12-02 12:18:08 +0300458#if IS_ENABLED(CONFIG_CMD_PING6)
459/* Send ping requset */
460void ping6_start(void);
461
462/**
463 * ping6_receive() - Handle reception of ICMPv6 echo request/reply
464 *
465 * @et: pointer to incoming patcket
466 * @ip6: pointer to IPv6 protocol
467 * @len: packet length
468 * Return: 0 if success, -EINVAL in case of failure during reception
469 */
470int ping6_receive(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len);
471#else
472static inline void ping6_start(void)
473{
474}
475
476static inline
477int ping6_receive(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len)
478{
479 return -EINVAL;
480}
481#endif /* CONFIG_CMD_PING6 */
482
Viacheslav Mitrofanov7674b122022-12-02 12:17:58 +0300483#endif /* __NET6_H__ */