blob: 622885e7df842f17d51754dd196cc9bd14ef3d3c [file] [log] [blame]
Viacheslav Mitrofanov33b50662022-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>
15
16/* struct in6_addr - 128 bits long IPv6 address */
17struct in6_addr {
18 union {
19 u8 u6_addr8[16];
20 __be16 u6_addr16[8];
21 __be32 u6_addr32[4];
22 } in6_u;
23
24#define s6_addr in6_u.u6_addr8
25#define s6_addr16 in6_u.u6_addr16
26#define s6_addr32 in6_u.u6_addr32
27};
28
29#define IN6ADDRSZ sizeof(struct in6_addr)
30#define INETHADDRSZ sizeof(net_ethaddr)
31
32#define PROT_IP6 0x86DD /* IPv6 protocol */
33#define PROT_ICMPV6 58 /* ICMPv6 protocol*/
34
35#define IPV6_ADDRSCOPE_INTF 0x01
36#define IPV6_ADDRSCOPE_LINK 0x02
37#define IPV6_ADDRSCOPE_AMDIN 0x04
38#define IPV6_ADDRSCOPE_SITE 0x05
39#define IPV6_ADDRSCOPE_ORG 0x08
40#define IPV6_ADDRSCOPE_GLOBAL 0x0E
41
42#define USE_IP6_CMD_PARAM "-ipv6"
43
44/**
45 * struct ipv6hdr - Internet Protocol V6 (IPv6) header.
46 *
47 * IPv6 packet header as defined in RFC 2460.
48 */
49struct ip6_hdr {
50#if defined(__LITTLE_ENDIAN_BITFIELD)
51 u8 priority:4,
52 version:4;
53#elif defined(__BIG_ENDIAN_BITFIELD)
54 u8 version:4,
55 priority:4;
56#else
57#error "Please fix <asm/byteorder.h>"
58#endif
59 u8 flow_lbl[3];
60 __be16 payload_len;
61 u8 nexthdr;
62 u8 hop_limit;
63 struct in6_addr saddr;
64 struct in6_addr daddr;
65};
66#define IP6_HDR_SIZE (sizeof(struct ip6_hdr))
67
68/* struct udp_hdr - User Datagram Protocol header */
69struct udp_hdr {
70 u16 udp_src; /* UDP source port */
71 u16 udp_dst; /* UDP destination port */
72 u16 udp_len; /* Length of UDP packet */
73 u16 udp_xsum; /* Checksum */
74} __packed;
75
76/*
77 * Handy for static initialisations of struct in6_addr, atlhough the
78 * c99 '= { 0 }' idiom might work depending on you compiler.
79 */
80#define ZERO_IPV6_ADDR { { { 0x00, 0x00, 0x00, 0x00, \
81 0x00, 0x00, 0x00, 0x00, \
82 0x00, 0x00, 0x00, 0x00, \
83 0x00, 0x00, 0x00, 0x00 } } }
84
85#define IPV6_LINK_LOCAL_PREFIX 0xfe80
86
87/* hop limit for neighbour discovery packets */
88#define IPV6_NDISC_HOPLIMIT 255
89#define NDISC_TIMEOUT 5000UL
90#define NDISC_TIMEOUT_COUNT 3
91
92/* struct icmp6hdr - Internet Control Message Protocol header for IPV6 */
93struct icmp6hdr {
94 u8 icmp6_type;
95#define IPV6_ICMP_ECHO_REQUEST 128
96#define IPV6_ICMP_ECHO_REPLY 129
97#define IPV6_NDISC_ROUTER_SOLICITATION 133
98#define IPV6_NDISC_ROUTER_ADVERTISEMENT 134
99#define IPV6_NDISC_NEIGHBOUR_SOLICITATION 135
100#define IPV6_NDISC_NEIGHBOUR_ADVERTISEMENT 136
101#define IPV6_NDISC_REDIRECT 137
102 u8 icmp6_code;
103 __be16 icmp6_cksum;
104
105 /* ICMPv6 data */
106 union {
107 __be32 un_data32[1];
108 __be16 un_data16[2];
109 u8 un_data8[4];
110
111 /* struct icmpv6_echo - echo request/reply message format */
112 struct icmpv6_echo {
113 __be16 identifier;
114 __be16 sequence;
115 } u_echo;
116
117 /* struct icmpv6_nd_advt - Neighbor Advertisement format */
118 struct icmpv6_nd_advt {
119#if defined(__LITTLE_ENDIAN_BITFIELD)
120 __be32 reserved:5,
121 override:1,
122 solicited:1,
123 router:1,
124 reserved2:24;
125#elif defined(__BIG_ENDIAN_BITFIELD)
126 __be32 router:1,
127 solicited:1,
128 override:1,
129 reserved:29;
130#else
131#error "Please fix <asm/byteorder.h>"
132#endif
133 } u_nd_advt;
134
135 /* struct icmpv6_nd_ra - Router Advertisement format */
136 struct icmpv6_nd_ra {
137 u8 hop_limit;
138#if defined(__LITTLE_ENDIAN_BITFIELD)
139 u8 reserved:6,
140 other:1,
141 managed:1;
142
143#elif defined(__BIG_ENDIAN_BITFIELD)
144 u8 managed:1,
145 other:1,
146 reserved:6;
147#else
148#error "Please fix <asm/byteorder.h>"
149#endif
150 __be16 rt_lifetime;
151 } u_nd_ra;
152 } icmp6_dataun;
153#define icmp6_identifier icmp6_dataun.u_echo.identifier
154#define icmp6_sequence icmp6_dataun.u_echo.sequence
155#define icmp6_pointer icmp6_dataun.un_data32[0]
156#define icmp6_mtu icmp6_dataun.un_data32[0]
157#define icmp6_unused icmp6_dataun.un_data32[0]
158#define icmp6_maxdelay icmp6_dataun.un_data16[0]
159#define icmp6_router icmp6_dataun.u_nd_advt.router
160#define icmp6_solicited icmp6_dataun.u_nd_advt.solicited
161#define icmp6_override icmp6_dataun.u_nd_advt.override
162#define icmp6_ndiscreserved icmp6_dataun.u_nd_advt.reserved
163#define icmp6_hop_limit icmp6_dataun.u_nd_ra.hop_limit
164#define icmp6_addrconf_managed icmp6_dataun.u_nd_ra.managed
165#define icmp6_addrconf_other icmp6_dataun.u_nd_ra.other
166#define icmp6_rt_lifetime icmp6_dataun.u_nd_ra.rt_lifetime
167};
168
169extern struct in6_addr const net_null_addr_ip6; /* NULL IPv6 address */
170extern struct in6_addr net_gateway6; /* Our gateways IPv6 address */
171extern struct in6_addr net_ip6; /* Our IPv6 addr (0 = unknown) */
172extern struct in6_addr net_link_local_ip6; /* Our link local IPv6 addr */
173extern u32 net_prefix_length; /* Our prefixlength (0 = unknown) */
174extern struct in6_addr net_server_ip6; /* Server IPv6 addr (0 = unknown) */
175extern bool use_ip6;
176
Viacheslav Mitrofanov2f7f2f22022-12-02 12:18:02 +0300177#if IS_ENABLED(CONFIG_IPV6)
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300178/**
179 * string_to_ip6() - Convert IPv6 string addr to inner IPV6 addr format
180 *
181 * Examples of valid strings:
182 * 2001:db8::0:1234:1
183 * 2001:0db8:0000:0000:0000:0000:1234:0001
184 * ::1
185 * ::ffff:192.168.1.1
186 *
187 * Examples of invalid strings
188 * 2001:db8::0::0 (:: can only appear once)
189 * 2001:db8:192.168.1.1::1 (v4 part can only appear at the end)
190 * 192.168.1.1 (we don't implicity map v4)
191 *
192 * @s: IPv6 string addr format
193 * @len: IPv6 string addr length
194 * @addr: converted IPv6 addr
195 * Return: 0 if conversion successful, -EINVAL if fail
196 */
Viacheslav Mitrofanov2f7f2f22022-12-02 12:18:02 +0300197int string_to_ip6(const char *s, size_t len, struct in6_addr *addr);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300198
199/**
200 * ip6_is_unspecified_addr() - Check if IPv6 addr is not set i.e. is zero
201 *
202 * @addr: IPv6 addr
203 * Return: 0 if addr is not set, -1 if is set
204 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300205int ip6_is_unspecified_addr(struct in6_addr *addr);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300206
207/**
208 * ip6_is_our_addr() - Check if IPv6 addr belongs to our host addr
209 *
210 * We have 2 addresses that we should respond to. A link local address and a
211 * global address. This returns true if the specified address matches either
212 * of these.
213 *
214 * @addr: addr to check
215 * Return: 0 if addr is our, -1 otherwise
216 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300217int ip6_is_our_addr(struct in6_addr *addr);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300218
219/**
220 * ip6_addr_in_subnet() - Check if two IPv6 addresses are in the same subnet
221 *
222 * @our_addr: first IPv6 addr
223 * @neigh_addr: second IPv6 addr
224 * @prefix_length: network mask length
225 * Return: 0 if two addresses in the same subnet, -1 otherwise
226 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300227int ip6_addr_in_subnet(struct in6_addr *our_addr, struct in6_addr *neigh_addr,
228 u32 prefix_length);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300229
230/**
231 * ip6_make_lladd() - rMake up IPv6 Link Local address
232 *
233 * @lladdr: formed IPv6 Link Local address
234 * @enetaddr: MAC addr of a device
235 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300236void ip6_make_lladdr(struct in6_addr *lladr, unsigned char const enetaddr[6]);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300237
238/**
239 * ip6_make_snma() - aMake up Solicited Node Multicast Address from IPv6 addr
240 *
241 * @mcast_addr: formed SNMA addr
242 * @ip6_addr: base IPv6 addr
243 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300244void ip6_make_snma(struct in6_addr *mcast_addr, struct in6_addr *ip6_addr);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300245
246/**
247 * ip6_make_mult_ethdstaddr() - Make up IPv6 multicast addr
248 *
249 * @enetaddr: MAC addr of a device
250 * @mcast_addr: formed IPv6 multicast addr
251 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300252void ip6_make_mult_ethdstaddr(unsigned char enetaddr[6],
253 struct in6_addr *mcast_addr);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300254
255/**
256 * csum_partial() - Compute an internet checksum
257 *
258 * @buff: buffer to be checksummed
259 * @len: length of buffer
260 * @sum: initial sum to be added in
261 * Return: internet checksum of the buffer
262 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300263unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300264
265/**
266 * csum_ipv6_magic() - Compute checksum of IPv6 "psuedo-header" per RFC2460 section 8.1
267 *
268 * @saddr: source IPv6 addr
269 * @daddr: destination IPv6 add
270 * @len: data length to be checksummed
271 * @proto: IPv6 above protocol code
272 * @csum: upper layer checksum
273 * Return: computed checksum
274 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300275unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
276 struct in6_addr *daddr, u16 len,
277 unsigned short proto, unsigned int csum);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300278
279/**
280 * ip6_add_hdr() - Make up IPv6 header
281 *
282 * @xip: pointer to IPv6 header to be formed
283 * @src: source IPv6 addr
284 * @dest: destination IPv6 addr
285 * @nextheader: next header type
286 * @hoplimit: hop limit
287 * @payload_len: payload length
288 * Return: IPv6 header length
289 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300290int ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,
291 int nextheader, int hoplimit, int payload_len);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300292
293/**
294 * net_send_udp_packet6() - Make up UDP packet and send it
295 *
296 * @ether: destination MAC addr
297 * @dest: destination IPv6 addr
298 * @dport: destination port
299 * @sport: source port
300 * @len: UDP packet length
301 * Return: 0 if send successfully, -1 otherwise
302 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300303int net_send_udp_packet6(uchar *ether, struct in6_addr *dest, int dport,
304 int sport, int len);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300305
306/**
307 * net_ip6_handler() - Handle IPv6 packet
308 *
309 * @et: pointer to the beginning of the packet
310 * @ip6: pointer to the beginning of IPv6 protocol
311 * @len: incoming packet len
312 * Return: 0 if handle packet successfully, -EINVAL in case of invalid protocol
313 */
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300314int net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len);
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300315
316/**
317 * net_copy_ip6() - Copy IPv6 addr
318 *
319 * @to: destination IPv6 addr
320 * @from: source IPv6 addr
321 */
322static inline void net_copy_ip6(void *to, const void *from)
323{
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300324 memcpy((void *)to, from, sizeof(struct in6_addr));
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300325}
Viacheslav Mitrofanov1feb6972022-12-02 12:18:05 +0300326#else
327static inline int
328string_to_ip6(const char *s, size_t len, struct in6_addr *addr)
329{
330 return -EINVAL;
331}
332
333static inline int ip6_is_unspecified_addr(struct in6_addr *addr)
334{
335 return -1;
336}
337
338static inline int ip6_is_our_addr(struct in6_addr *addr)
339{
340 return -1;
341}
342
343static inline int
344ip6_addr_in_subnet(struct in6_addr *our_addr, struct in6_addr *neigh_addr,
345 u32 prefix_length)
346{
347 return -1;
348}
349
350static inline void
351ip6_make_lladdr(struct in6_addr *lladdr, unsigned char const enetaddr[6])
352{
353}
354
355static inline void
356ip6_make_snma(struct in6_addr *mcast_addr, struct in6_addr *ip6_addr)
357{
358}
359
360static inline void
361ip6_make_mult_ethdstaddr(unsigned char enetaddr[6],
362 struct in6_addr *mcast_addr)
363{
364}
365
366static inline unsigned int
367csum_partial(const unsigned char *buff, int len, unsigned int sum)
368{
369 return 0;
370}
371
372static inline unsigned short
373csum_ipv6_magic(struct in6_addr *saddr,
374 struct in6_addr *daddr, u16 len,
375 unsigned short proto, unsigned int csum)
376{
377 return 0;
378}
379
380static inline unsigned int
381ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,
382 int nextheader, int hoplimit, int payload_len)
383{
384 return 0;
385}
386
387static inline int
388net_send_udp_packet6(uchar *ether, struct in6_addr *dest,
389 int dport, int sport, int len)
390{
391 return -1;
392}
393
394static inline int
395net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6,
396 int len)
397{
398 return -EINVAL;
399}
400
401static inline void net_copy_ip6(void *to, const void *from)
402{
403}
404#endif
Viacheslav Mitrofanov33b50662022-12-02 12:17:58 +0300405
406#endif /* __NET6_H__ */