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
, '/');
52 net_prefix_length
= simple_strtoul(mask
+ 1, NULL
, 10);
58 return string_to_ip6(value
, len
, &net_ip6
);
61 U_BOOT_ENV_CALLBACK(ip6addr
, on_ip6addr
);
63 static int on_gatewayip6(const char *name
, const char *value
, enum env_op op
,
66 if (flags
& H_PROGRAMMATIC
)
69 return string_to_ip6(value
, strlen(value
), &net_gateway6
);
72 U_BOOT_ENV_CALLBACK(gatewayip6
, on_gatewayip6
);
74 static int on_serverip6(const char *name
, const char *value
, enum env_op op
,
77 if (flags
& H_PROGRAMMATIC
)
80 return string_to_ip6(value
, strlen(value
), &net_server_ip6
);
83 U_BOOT_ENV_CALLBACK(serverip6
, on_serverip6
);
85 int ip6_is_unspecified_addr(struct in6_addr
*addr
)
87 return !(addr
->s6_addr32
[0] | addr
->s6_addr32
[1] |
88 addr
->s6_addr32
[2] | addr
->s6_addr32
[3]);
91 int ip6_is_our_addr(struct in6_addr
*addr
)
93 return !memcmp(addr
, &net_link_local_ip6
, sizeof(struct in6_addr
)) ||
94 !memcmp(addr
, &net_ip6
, sizeof(struct in6_addr
));
97 void ip6_make_eui(unsigned char eui
[8], unsigned char const enetaddr
[6])
99 memcpy(eui
, enetaddr
, 3);
100 memcpy(&eui
[5], &enetaddr
[3], 3);
103 eui
[0] ^= 2; /* "u" bit set to indicate global scope */
106 void ip6_make_lladdr(struct in6_addr
*lladr
, unsigned char const enetaddr
[6])
108 unsigned char eui
[8];
110 memset(lladr
, 0, sizeof(struct in6_addr
));
111 lladr
->s6_addr16
[0] = htons(IPV6_LINK_LOCAL_PREFIX
);
112 ip6_make_eui(eui
, enetaddr
);
113 memcpy(&lladr
->s6_addr
[8], eui
, 8);
116 void ip6_make_snma(struct in6_addr
*mcast_addr
, struct in6_addr
*ip6_addr
)
118 memset(mcast_addr
, 0, sizeof(struct in6_addr
));
119 mcast_addr
->s6_addr
[0] = 0xff;
120 mcast_addr
->s6_addr
[1] = IPV6_ADDRSCOPE_LINK
;
121 mcast_addr
->s6_addr
[11] = 0x01;
122 mcast_addr
->s6_addr
[12] = 0xff;
123 mcast_addr
->s6_addr
[13] = ip6_addr
->s6_addr
[13];
124 mcast_addr
->s6_addr
[14] = ip6_addr
->s6_addr
[14];
125 mcast_addr
->s6_addr
[15] = ip6_addr
->s6_addr
[15];
129 ip6_make_mult_ethdstaddr(unsigned char enetaddr
[6], struct in6_addr
*mcast_addr
)
133 memcpy(&enetaddr
[2], &mcast_addr
->s6_addr
[12], 4);
137 ip6_addr_in_subnet(struct in6_addr
*our_addr
, struct in6_addr
*neigh_addr
,
141 __be32
*neigh_dwords
;
143 addr_dwords
= our_addr
->s6_addr32
;
144 neigh_dwords
= neigh_addr
->s6_addr32
;
147 if (*addr_dwords
++ != *neigh_dwords
++)
153 /* Check any remaining bits */
155 if ((*addr_dwords
>> (32 - plen
)) !=
156 (*neigh_dwords
>> (32 - plen
))) {
164 static inline unsigned int csum_fold(unsigned int sum
)
166 sum
= (sum
& 0xffff) + (sum
>> 16);
167 sum
= (sum
& 0xffff) + (sum
>> 16);
169 /* Opaque moment. If reverse it to zero it will not be checked on
170 * receiver's side. It leads to bad negibour advertisement.
178 static inline unsigned short from32to16(unsigned int x
)
180 /* add up 16-bit and 16-bit for 16+c bit */
181 x
= (x
& 0xffff) + (x
>> 16);
183 x
= (x
& 0xffff) + (x
>> 16);
187 static u32
csum_do_csum(const u8
*buff
, int len
)
190 unsigned int result
= 0;
194 odd
= 1 & (unsigned long)buff
;
196 #ifdef __LITTLE_ENDIAN
197 result
+= (*buff
<< 8);
205 if (2 & (unsigned long)buff
) {
206 result
+= *(unsigned short *)buff
;
211 const unsigned char *end
= buff
+ ((u32
)len
& ~3);
212 unsigned int carry
= 0;
215 unsigned int w
= *(unsigned int *)buff
;
220 carry
= (w
> result
);
221 } while (buff
< end
);
223 result
= (result
& 0xffff) + (result
>> 16);
226 result
+= *(unsigned short *)buff
;
231 #ifdef __LITTLE_ENDIAN
234 result
+= (*buff
<< 8);
236 result
= from32to16(result
);
238 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
243 unsigned int csum_partial(const unsigned char *buff
, int len
, unsigned int sum
)
245 unsigned int result
= csum_do_csum(buff
, len
);
247 /* add in old sum, and carry.. */
249 /* 16+c bits -> 16 bits */
250 result
= (result
& 0xffff) + (result
>> 16);
255 csum_ipv6_magic(struct in6_addr
*saddr
, struct in6_addr
*daddr
, u16 len
,
256 unsigned short proto
, unsigned int csum
)
263 sum
+= saddr
->s6_addr32
[0];
264 carry
= (sum
< saddr
->s6_addr32
[0]);
267 sum
+= saddr
->s6_addr32
[1];
268 carry
= (sum
< saddr
->s6_addr32
[1]);
271 sum
+= saddr
->s6_addr32
[2];
272 carry
= (sum
< saddr
->s6_addr32
[2]);
275 sum
+= saddr
->s6_addr32
[3];
276 carry
= (sum
< saddr
->s6_addr32
[3]);
279 sum
+= daddr
->s6_addr32
[0];
280 carry
= (sum
< daddr
->s6_addr32
[0]);
283 sum
+= daddr
->s6_addr32
[1];
284 carry
= (sum
< daddr
->s6_addr32
[1]);
287 sum
+= daddr
->s6_addr32
[2];
288 carry
= (sum
< daddr
->s6_addr32
[2]);
291 sum
+= daddr
->s6_addr32
[3];
292 carry
= (sum
< daddr
->s6_addr32
[3]);
295 ulen
= htonl((u32
)len
);
297 carry
= (sum
< ulen
);
300 uproto
= htonl(proto
);
302 carry
= (sum
< uproto
);
305 return csum_fold(sum
);
308 int ip6_add_hdr(uchar
*xip
, struct in6_addr
*src
, struct in6_addr
*dest
,
309 int nextheader
, int hoplimit
, int payload_len
)
311 struct ip6_hdr
*ip6
= (struct ip6_hdr
*)xip
;
315 ip6
->flow_lbl
[0] = 0;
316 ip6
->flow_lbl
[1] = 0;
317 ip6
->flow_lbl
[2] = 0;
318 ip6
->payload_len
= htons(payload_len
);
319 ip6
->nexthdr
= nextheader
;
320 ip6
->hop_limit
= hoplimit
;
321 net_copy_ip6(&ip6
->saddr
, src
);
322 net_copy_ip6(&ip6
->daddr
, dest
);
324 return sizeof(struct ip6_hdr
);
327 int net_send_udp_packet6(uchar
*ether
, struct in6_addr
*dest
, int dport
,
334 udp
= (struct udp_hdr
*)((uchar
*)net_tx_packet
+ net_eth_hdr_size() +
337 udp
->udp_dst
= htons(dport
);
338 udp
->udp_src
= htons(sport
);
339 udp
->udp_len
= htons(len
+ UDP_HDR_SIZE
);
343 csum_p
= csum_partial((u8
*)udp
, len
+ UDP_HDR_SIZE
, 0);
344 udp
->udp_xsum
= csum_ipv6_magic(&net_ip6
, dest
, len
+ UDP_HDR_SIZE
,
345 IPPROTO_UDP
, csum_p
);
347 /* if MAC address was not discovered yet, save the packet and do
348 * neighbour discovery
350 if (!memcmp(ether
, net_null_ethaddr
, 6)) {
351 net_copy_ip6(&net_nd_sol_packet_ip6
, dest
);
352 net_nd_packet_mac
= ether
;
354 pkt
= net_nd_tx_packet
;
355 pkt
+= net_set_ether(pkt
, net_nd_packet_mac
, PROT_IP6
);
356 pkt
+= ip6_add_hdr(pkt
, &net_ip6
, dest
, IPPROTO_UDP
, 64,
358 memcpy(pkt
, (uchar
*)udp
, len
+ UDP_HDR_SIZE
);
360 /* size of the waiting packet */
361 net_nd_tx_packet_size
= (pkt
- net_nd_tx_packet
) +
364 /* and do the neighbor solicitation */
366 net_nd_timer_start
= get_timer(0);
368 return 1; /* waiting */
371 pkt
= (uchar
*)net_tx_packet
;
372 pkt
+= net_set_ether(pkt
, ether
, PROT_IP6
);
373 pkt
+= ip6_add_hdr(pkt
, &net_ip6
, dest
, IPPROTO_UDP
, 64,
375 (void)eth_send(net_tx_packet
, pkt
- net_tx_packet
+ UDP_HDR_SIZE
+ len
);
377 return 0; /* transmitted */
380 int net_ip6_handler(struct ethernet_hdr
*et
, struct ip6_hdr
*ip6
, int len
)
382 struct in_addr zero_ip
= {.s_addr
= 0 };
383 struct icmp6hdr
*icmp
;
389 if (len
< IP6_HDR_SIZE
)
392 if (ip6
->version
!= 6)
395 switch (ip6
->nexthdr
) {
397 icmp
= (struct icmp6hdr
*)(((uchar
*)ip6
) + IP6_HDR_SIZE
);
398 csum
= icmp
->icmp6_cksum
;
399 hlen
= ntohs(ip6
->payload_len
);
400 icmp
->icmp6_cksum
= 0;
402 csum_p
= csum_partial((u8
*)icmp
, hlen
, 0);
403 icmp
->icmp6_cksum
= csum_ipv6_magic(&ip6
->saddr
, &ip6
->daddr
,
404 hlen
, PROT_ICMPV6
, csum_p
);
406 if (icmp
->icmp6_cksum
!= csum
)
409 switch (icmp
->icmp6_type
) {
410 case IPV6_ICMP_ECHO_REQUEST
:
411 case IPV6_ICMP_ECHO_REPLY
:
412 ping6_receive(et
, ip6
, len
);
414 case IPV6_NDISC_NEIGHBOUR_SOLICITATION
:
415 case IPV6_NDISC_NEIGHBOUR_ADVERTISEMENT
:
416 case IPV6_NDISC_ROUTER_ADVERTISEMENT
:
417 ndisc_receive(et
, ip6
, len
);
424 udp
= (struct udp_hdr
*)(((uchar
*)ip6
) + IP6_HDR_SIZE
);
425 csum
= udp
->udp_xsum
;
426 hlen
= ntohs(ip6
->payload_len
);
429 csum_p
= csum_partial((u8
*)udp
, hlen
, 0);
430 udp
->udp_xsum
= csum_ipv6_magic(&ip6
->saddr
, &ip6
->daddr
,
431 hlen
, IPPROTO_UDP
, csum_p
);
433 if (csum
!= udp
->udp_xsum
)
436 /* IP header OK. Pass the packet to the current handler. */
437 net_get_udp_handler()((uchar
*)ip6
+ IP6_HDR_SIZE
+
442 ntohs(udp
->udp_len
) - 8);