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>
10 /* Simple IPv6 network layer implementation */
13 #include <env_internal.h>
19 /* NULL IPv6 address */
20 struct in6_addr const net_null_addr_ip6 = ZERO_IPV6_ADDR;
21 /* Our gateway's IPv6 address */
22 struct in6_addr net_gateway6 = ZERO_IPV6_ADDR;
23 /* Our IPv6 addr (0 = unknown) */
24 struct in6_addr net_ip6 = ZERO_IPV6_ADDR;
25 /* Our link local IPv6 addr (0 = unknown) */
26 struct in6_addr net_link_local_ip6 = ZERO_IPV6_ADDR;
27 /* set server IPv6 addr (0 = unknown) */
28 struct in6_addr net_server_ip6 = ZERO_IPV6_ADDR;
29 /* The prefix length of our network */
30 u32 net_prefix_length;
34 static int on_ip6addr(const char *name, const char *value, enum env_op op,
40 if (flags & H_PROGRAMMATIC)
43 if (op == env_op_delete) {
44 net_prefix_length = 0;
45 net_copy_ip6(&net_ip6, &net_null_addr_ip6);
49 mask = strchr(value, '/');
53 net_prefix_length = simple_strtoul(value + len, NULL, 10);
55 return string_to_ip6(value, len, &net_ip6);
58 U_BOOT_ENV_CALLBACK(ip6addr, on_ip6addr);
60 static int on_gatewayip6(const char *name, const char *value, enum env_op op,
63 if (flags & H_PROGRAMMATIC)
66 return string_to_ip6(value, strlen(value), &net_gateway6);
69 U_BOOT_ENV_CALLBACK(gatewayip6, on_gatewayip6);
71 static int on_serverip6(const char *name, const char *value, enum env_op op,
74 if (flags & H_PROGRAMMATIC)
77 return string_to_ip6(value, strlen(value), &net_server_ip6);
80 U_BOOT_ENV_CALLBACK(serverip6, on_serverip6);
82 int ip6_is_unspecified_addr(struct in6_addr *addr)
84 return !(addr->s6_addr32[0] | addr->s6_addr32[1] |
85 addr->s6_addr32[2] | addr->s6_addr32[3]);
88 int ip6_is_our_addr(struct in6_addr *addr)
90 return !memcmp(addr, &net_link_local_ip6, sizeof(struct in6_addr)) ||
91 !memcmp(addr, &net_ip6, sizeof(struct in6_addr));
94 void ip6_make_eui(unsigned char eui[8], unsigned char const enetaddr[6])
96 memcpy(eui, enetaddr, 3);
97 memcpy(&eui[5], &enetaddr[3], 3);
100 eui[0] ^= 2; /* "u" bit set to indicate global scope */
103 void ip6_make_lladdr(struct in6_addr *lladr, unsigned char const enetaddr[6])
105 unsigned char eui[8];
107 memset(lladr, 0, sizeof(struct in6_addr));
108 lladr->s6_addr16[0] = htons(IPV6_LINK_LOCAL_PREFIX);
109 ip6_make_eui(eui, enetaddr);
110 memcpy(&lladr->s6_addr[8], eui, 8);
113 void ip6_make_snma(struct in6_addr *mcast_addr, struct in6_addr *ip6_addr)
115 memset(mcast_addr, 0, sizeof(struct in6_addr));
116 mcast_addr->s6_addr[0] = 0xff;
117 mcast_addr->s6_addr[1] = IPV6_ADDRSCOPE_LINK;
118 mcast_addr->s6_addr[11] = 0x01;
119 mcast_addr->s6_addr[12] = 0xff;
120 mcast_addr->s6_addr[13] = ip6_addr->s6_addr[13];
121 mcast_addr->s6_addr[14] = ip6_addr->s6_addr[14];
122 mcast_addr->s6_addr[15] = ip6_addr->s6_addr[15];
126 ip6_make_mult_ethdstaddr(unsigned char enetaddr[6], struct in6_addr *mcast_addr)
130 memcpy(&enetaddr[2], &mcast_addr->s6_addr[12], 4);
134 ip6_addr_in_subnet(struct in6_addr *our_addr, struct in6_addr *neigh_addr,
138 __be32 *neigh_dwords;
140 addr_dwords = our_addr->s6_addr32;
141 neigh_dwords = neigh_addr->s6_addr32;
144 if (*addr_dwords++ != *neigh_dwords++)
150 /* Check any remaining bits */
152 if ((*addr_dwords >> (32 - plen)) !=
153 (*neigh_dwords >> (32 - plen))) {
161 static inline unsigned int csum_fold(unsigned int sum)
163 sum = (sum & 0xffff) + (sum >> 16);
164 sum = (sum & 0xffff) + (sum >> 16);
166 /* Opaque moment. If reverse it to zero it will not be checked on
167 * receiver's side. It leads to bad negibour advertisement.
175 static inline unsigned short from32to16(unsigned int x)
177 /* add up 16-bit and 16-bit for 16+c bit */
178 x = (x & 0xffff) + (x >> 16);
180 x = (x & 0xffff) + (x >> 16);
184 static u32 csum_do_csum(const u8 *buff, int len)
187 unsigned int result = 0;
191 odd = 1 & (unsigned long)buff;
193 #ifdef __LITTLE_ENDIAN
194 result += (*buff << 8);
202 if (2 & (unsigned long)buff) {
203 result += *(unsigned short *)buff;
208 const unsigned char *end = buff + ((u32)len & ~3);
209 unsigned int carry = 0;
212 unsigned int w = *(unsigned int *)buff;
217 carry = (w > result);
218 } while (buff < end);
220 result = (result & 0xffff) + (result >> 16);
223 result += *(unsigned short *)buff;
228 #ifdef __LITTLE_ENDIAN
231 result += (*buff << 8);
233 result = from32to16(result);
235 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
240 unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum)
242 unsigned int result = csum_do_csum(buff, len);
244 /* add in old sum, and carry.. */
246 /* 16+c bits -> 16 bits */
247 result = (result & 0xffff) + (result >> 16);
252 csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr, u16 len,
253 unsigned short proto, unsigned int csum)
260 sum += saddr->s6_addr32[0];
261 carry = (sum < saddr->s6_addr32[0]);
264 sum += saddr->s6_addr32[1];
265 carry = (sum < saddr->s6_addr32[1]);
268 sum += saddr->s6_addr32[2];
269 carry = (sum < saddr->s6_addr32[2]);
272 sum += saddr->s6_addr32[3];
273 carry = (sum < saddr->s6_addr32[3]);
276 sum += daddr->s6_addr32[0];
277 carry = (sum < daddr->s6_addr32[0]);
280 sum += daddr->s6_addr32[1];
281 carry = (sum < daddr->s6_addr32[1]);
284 sum += daddr->s6_addr32[2];
285 carry = (sum < daddr->s6_addr32[2]);
288 sum += daddr->s6_addr32[3];
289 carry = (sum < daddr->s6_addr32[3]);
292 ulen = htonl((u32)len);
294 carry = (sum < ulen);
297 uproto = htonl(proto);
299 carry = (sum < uproto);
302 return csum_fold(sum);
305 int ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,
306 int nextheader, int hoplimit, int payload_len)
308 struct ip6_hdr *ip6 = (struct ip6_hdr *)xip;
312 ip6->flow_lbl[0] = 0;
313 ip6->flow_lbl[1] = 0;
314 ip6->flow_lbl[2] = 0;
315 ip6->payload_len = htons(payload_len);
316 ip6->nexthdr = nextheader;
317 ip6->hop_limit = hoplimit;
318 net_copy_ip6(&ip6->saddr, src);
319 net_copy_ip6(&ip6->daddr, dest);
321 return sizeof(struct ip6_hdr);
324 int net_send_udp_packet6(uchar *ether, struct in6_addr *dest, int dport,
331 udp = (struct udp_hdr *)((uchar *)net_tx_packet + net_eth_hdr_size() +
334 udp->udp_dst = htons(dport);
335 udp->udp_src = htons(sport);
336 udp->udp_len = htons(len + UDP_HDR_SIZE);
340 csum_p = csum_partial((u8 *)udp, len + UDP_HDR_SIZE, 0);
341 udp->udp_xsum = csum_ipv6_magic(&net_ip6, dest, len + UDP_HDR_SIZE,
342 IPPROTO_UDP, csum_p);
344 /* if MAC address was not discovered yet, save the packet and do
345 * neighbour discovery
347 if (!memcmp(ether, net_null_ethaddr, 6)) {
348 net_copy_ip6(&net_nd_sol_packet_ip6, dest);
349 net_nd_packet_mac = ether;
351 pkt = net_nd_tx_packet;
352 pkt += net_set_ether(pkt, net_nd_packet_mac, PROT_IP6);
353 pkt += ip6_add_hdr(pkt, &net_ip6, dest, IPPROTO_UDP, 64,
355 memcpy(pkt, (uchar *)udp, len + UDP_HDR_SIZE);
357 /* size of the waiting packet */
358 net_nd_tx_packet_size = (pkt - net_nd_tx_packet) +
361 /* and do the neighbor solicitation */
363 net_nd_timer_start = get_timer(0);
365 return 1; /* waiting */
368 pkt = (uchar *)net_tx_packet;
369 pkt += net_set_ether(pkt, ether, PROT_IP6);
370 pkt += ip6_add_hdr(pkt, &net_ip6, dest, IPPROTO_UDP, 64,
372 (void)eth_send(net_tx_packet, pkt - net_tx_packet + UDP_HDR_SIZE + len);
374 return 0; /* transmitted */
377 int net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len)
379 struct in_addr zero_ip = {.s_addr = 0 };
380 struct icmp6hdr *icmp;
386 if (len < IP6_HDR_SIZE)
389 if (ip6->version != 6)
392 switch (ip6->nexthdr) {
394 icmp = (struct icmp6hdr *)(((uchar *)ip6) + IP6_HDR_SIZE);
395 csum = icmp->icmp6_cksum;
396 hlen = ntohs(ip6->payload_len);
397 icmp->icmp6_cksum = 0;
399 csum_p = csum_partial((u8 *)icmp, hlen, 0);
400 icmp->icmp6_cksum = csum_ipv6_magic(&ip6->saddr, &ip6->daddr,
401 hlen, PROT_ICMPV6, csum_p);
403 if (icmp->icmp6_cksum != csum)
406 switch (icmp->icmp6_type) {
407 case IPV6_ICMP_ECHO_REQUEST:
408 case IPV6_ICMP_ECHO_REPLY:
409 ping6_receive(et, ip6, len);
411 case IPV6_NDISC_NEIGHBOUR_SOLICITATION:
412 case IPV6_NDISC_NEIGHBOUR_ADVERTISEMENT:
413 ndisc_receive(et, ip6, len);
420 udp = (struct udp_hdr *)(((uchar *)ip6) + IP6_HDR_SIZE);
421 csum = udp->udp_xsum;
422 hlen = ntohs(ip6->payload_len);
425 csum_p = csum_partial((u8 *)udp, hlen, 0);
426 udp->udp_xsum = csum_ipv6_magic(&ip6->saddr, &ip6->daddr,
427 hlen, IPPROTO_UDP, csum_p);
429 if (csum != udp->udp_xsum)
432 /* IP header OK. Pass the packet to the current handler. */
433 net_get_udp_handler()((uchar *)ip6 + IP6_HDR_SIZE +
438 ntohs(udp->udp_len) - 8);