1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2013 Allied Telesis Labs NZ
4 * Chris Packham, <judge.packham@gmail.com>
6 * Copyright (C) 2022 YADRO
7 * Viacheslav Mitrofanov <v.v.mitrofanov@yadro.com>
14 #include <linux/ctype.h>
16 /* struct in6_addr - 128 bits long IPv6 address */
24 #define s6_addr in6_u.u6_addr8
25 #define s6_addr16 in6_u.u6_addr16
26 #define s6_addr32 in6_u.u6_addr32
29 #define IN6ADDRSZ sizeof(struct in6_addr)
30 #define INETHADDRSZ sizeof(net_ethaddr)
32 #define PROT_IP6 0x86DD /* IPv6 protocol */
33 #define PROT_ICMPV6 58 /* ICMPv6 protocol*/
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
42 #define USE_IP6_CMD_PARAM "-ipv6"
45 * struct ipv6hdr - Internet Protocol V6 (IPv6) header.
47 * IPv6 packet header as defined in RFC 2460.
50 #if defined(__LITTLE_ENDIAN_BITFIELD)
53 #elif defined(__BIG_ENDIAN_BITFIELD)
57 #error "Please fix <asm/byteorder.h>"
63 struct in6_addr saddr;
64 struct in6_addr daddr;
66 #define IP6_HDR_SIZE (sizeof(struct ip6_hdr))
68 /* struct udp_hdr - User Datagram Protocol header */
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 */
77 * Handy for static initialisations of struct in6_addr, atlhough the
78 * c99 '= { 0 }' idiom might work depending on you compiler.
80 #define ZERO_IPV6_ADDR { { { 0x00, 0x00, 0x00, 0x00, \
81 0x00, 0x00, 0x00, 0x00, \
82 0x00, 0x00, 0x00, 0x00, \
83 0x00, 0x00, 0x00, 0x00 } } }
85 #define IPV6_LINK_LOCAL_PREFIX 0xfe80
87 /* hop limit for neighbour discovery packets */
88 #define IPV6_NDISC_HOPLIMIT 255
89 #define NDISC_TIMEOUT 5000UL
90 #define NDISC_TIMEOUT_COUNT 3
92 /* struct icmp6hdr - Internet Control Message Protocol header for IPV6 */
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
111 /* struct icmpv6_echo - echo request/reply message format */
117 /* struct icmpv6_nd_advt - Neighbor Advertisement format */
118 struct icmpv6_nd_advt {
119 #if defined(__LITTLE_ENDIAN_BITFIELD)
125 #elif defined(__BIG_ENDIAN_BITFIELD)
131 #error "Please fix <asm/byteorder.h>"
135 /* struct icmpv6_nd_ra - Router Advertisement format */
136 struct icmpv6_nd_ra {
138 #if defined(__LITTLE_ENDIAN_BITFIELD)
143 #elif defined(__BIG_ENDIAN_BITFIELD)
148 #error "Please fix <asm/byteorder.h>"
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
169 extern struct in6_addr const net_null_addr_ip6; /* NULL IPv6 address */
170 extern struct in6_addr net_gateway6; /* Our gateways IPv6 address */
171 extern struct in6_addr net_ip6; /* Our IPv6 addr (0 = unknown) */
172 extern struct in6_addr net_link_local_ip6; /* Our link local IPv6 addr */
173 extern u32 net_prefix_length; /* Our prefixlength (0 = unknown) */
174 extern struct in6_addr net_server_ip6; /* Server IPv6 addr (0 = unknown) */
175 extern struct in6_addr net_ping_ip6; /* the ipv6 address to ping */
178 #if IS_ENABLED(CONFIG_IPV6)
180 * string_to_ip6() - Convert IPv6 string addr to inner IPV6 addr format
182 * Examples of valid strings:
184 * 2001:0db8:0000:0000:0000:0000:1234:0001
188 * Examples of invalid strings
189 * 2001:db8::0::0 (:: can only appear once)
190 * 2001:db8:192.168.1.1::1 (v4 part can only appear at the end)
191 * 192.168.1.1 (we don't implicity map v4)
193 * @s: IPv6 string addr format
194 * @len: IPv6 string addr length
195 * @addr: converted IPv6 addr
196 * Return: 0 if conversion successful, -EINVAL if fail
198 int string_to_ip6(const char *s, size_t len, struct in6_addr *addr);
201 * ip6_is_unspecified_addr() - Check if IPv6 addr is not set i.e. is zero
204 * Return: 0 if addr is not set, -1 if is set
206 int ip6_is_unspecified_addr(struct in6_addr *addr);
209 * ip6_is_our_addr() - Check if IPv6 addr belongs to our host addr
211 * We have 2 addresses that we should respond to. A link local address and a
212 * global address. This returns true if the specified address matches either
215 * @addr: addr to check
216 * Return: 0 if addr is our, -1 otherwise
218 int ip6_is_our_addr(struct in6_addr *addr);
221 * ip6_addr_in_subnet() - Check if two IPv6 addresses are in the same subnet
223 * @our_addr: first IPv6 addr
224 * @neigh_addr: second IPv6 addr
225 * @prefix_length: network mask length
226 * Return: 0 if two addresses in the same subnet, -1 otherwise
228 int ip6_addr_in_subnet(struct in6_addr *our_addr, struct in6_addr *neigh_addr,
232 * ip6_make_lladd() - rMake up IPv6 Link Local address
234 * @lladdr: formed IPv6 Link Local address
235 * @enetaddr: MAC addr of a device
237 void ip6_make_lladdr(struct in6_addr *lladr, unsigned char const enetaddr[6]);
240 * ip6_make_snma() - aMake up Solicited Node Multicast Address from IPv6 addr
242 * @mcast_addr: formed SNMA addr
243 * @ip6_addr: base IPv6 addr
245 void ip6_make_snma(struct in6_addr *mcast_addr, struct in6_addr *ip6_addr);
248 * ip6_make_mult_ethdstaddr() - Make up IPv6 multicast addr
250 * @enetaddr: MAC addr of a device
251 * @mcast_addr: formed IPv6 multicast addr
253 void ip6_make_mult_ethdstaddr(unsigned char enetaddr[6],
254 struct in6_addr *mcast_addr);
257 * csum_partial() - Compute an internet checksum
259 * @buff: buffer to be checksummed
260 * @len: length of buffer
261 * @sum: initial sum to be added in
262 * Return: internet checksum of the buffer
264 unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
267 * csum_ipv6_magic() - Compute checksum of IPv6 "psuedo-header" per RFC2460 section 8.1
269 * @saddr: source IPv6 addr
270 * @daddr: destination IPv6 add
271 * @len: data length to be checksummed
272 * @proto: IPv6 above protocol code
273 * @csum: upper layer checksum
274 * Return: computed checksum
276 unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
277 struct in6_addr *daddr, u16 len,
278 unsigned short proto, unsigned int csum);
281 * ip6_add_hdr() - Make up IPv6 header
283 * @xip: pointer to IPv6 header to be formed
284 * @src: source IPv6 addr
285 * @dest: destination IPv6 addr
286 * @nextheader: next header type
287 * @hoplimit: hop limit
288 * @payload_len: payload length
289 * Return: IPv6 header length
291 int ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,
292 int nextheader, int hoplimit, int payload_len);
295 * net_send_udp_packet6() - Make up UDP packet and send it
297 * @ether: destination MAC addr
298 * @dest: destination IPv6 addr
299 * @dport: destination port
300 * @sport: source port
301 * @len: UDP packet length
302 * Return: 0 if send successfully, -1 otherwise
304 int net_send_udp_packet6(uchar *ether, struct in6_addr *dest, int dport,
308 * net_ip6_handler() - Handle IPv6 packet
310 * @et: pointer to the beginning of the packet
311 * @ip6: pointer to the beginning of IPv6 protocol
312 * @len: incoming packet len
313 * Return: 0 if handle packet successfully, -EINVAL in case of invalid protocol
315 int net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len);
318 * net_copy_ip6() - Copy IPv6 addr
320 * @to: destination IPv6 addr
321 * @from: source IPv6 addr
323 static inline void net_copy_ip6(void *to, const void *from)
325 memcpy((void *)to, from, sizeof(struct in6_addr));
329 string_to_ip6(const char *s, size_t len, struct in6_addr *addr)
334 static inline int ip6_is_unspecified_addr(struct in6_addr *addr)
339 static inline int ip6_is_our_addr(struct in6_addr *addr)
345 ip6_addr_in_subnet(struct in6_addr *our_addr, struct in6_addr *neigh_addr,
352 ip6_make_lladdr(struct in6_addr *lladdr, unsigned char const enetaddr[6])
357 ip6_make_snma(struct in6_addr *mcast_addr, struct in6_addr *ip6_addr)
362 ip6_make_mult_ethdstaddr(unsigned char enetaddr[6],
363 struct in6_addr *mcast_addr)
367 static inline unsigned int
368 csum_partial(const unsigned char *buff, int len, unsigned int sum)
373 static inline unsigned short
374 csum_ipv6_magic(struct in6_addr *saddr,
375 struct in6_addr *daddr, u16 len,
376 unsigned short proto, unsigned int csum)
381 static inline unsigned int
382 ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,
383 int nextheader, int hoplimit, int payload_len)
389 net_send_udp_packet6(uchar *ether, struct in6_addr *dest,
390 int dport, int sport, int len)
396 net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6,
402 static inline void net_copy_ip6(void *to, const void *from)
407 #if IS_ENABLED(CONFIG_CMD_PING6)
408 /* Send ping requset */
409 void ping6_start(void);
412 * ping6_receive() - Handle reception of ICMPv6 echo request/reply
414 * @et: pointer to incoming patcket
415 * @ip6: pointer to IPv6 protocol
416 * @len: packet length
417 * Return: 0 if success, -EINVAL in case of failure during reception
419 int ping6_receive(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len);
421 static inline void ping6_start(void)
426 int ping6_receive(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len)
430 #endif /* CONFIG_CMD_PING6 */
432 #endif /* __NET6_H__ */