Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Generic network code. Moved from net.c |
| 4 | * |
| 5 | * Copyright 1994 - 2000 Neil Russell. |
| 6 | * Copyright 2000 Roland Borde |
| 7 | * Copyright 2000 Paolo Scaffardi |
| 8 | * Copyright 2000-2002 Wolfgang Denk, wd@denx.de |
| 9 | * Copyright 2009 Dirk Behme, dirk.behme@googlemail.com |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 13 | #include <net.h> |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 14 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 15 | struct in_addr string_to_ip(const char *s) |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 16 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 17 | struct in_addr addr; |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 18 | char *e; |
| 19 | int i; |
| 20 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 21 | addr.s_addr = 0; |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 22 | if (s == NULL) |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 23 | return addr; |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 24 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 25 | for (addr.s_addr = 0, i = 0; i < 4; ++i) { |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame^] | 26 | ulong val = s ? dectoul(s, &e) : 0; |
Chris Packham | d921ed9 | 2017-01-04 13:36:25 +1300 | [diff] [blame] | 27 | if (val > 255) { |
| 28 | addr.s_addr = 0; |
| 29 | return addr; |
| 30 | } |
Chris Packham | f267e40 | 2017-01-04 13:36:26 +1300 | [diff] [blame] | 31 | if (i != 3 && *e != '.') { |
| 32 | addr.s_addr = 0; |
| 33 | return addr; |
| 34 | } |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 35 | addr.s_addr <<= 8; |
| 36 | addr.s_addr |= (val & 0xFF); |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 37 | if (s) { |
| 38 | s = (*e) ? e+1 : e; |
| 39 | } |
| 40 | } |
| 41 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 42 | addr.s_addr = htonl(addr.s_addr); |
| 43 | return addr; |
Dirk Behme | 6a45e38 | 2010-01-03 08:33:58 +0100 | [diff] [blame] | 44 | } |
Joe Hershberger | fb8977c | 2019-09-13 19:21:16 -0500 | [diff] [blame] | 45 | |
| 46 | void string_to_enetaddr(const char *addr, uint8_t *enetaddr) |
| 47 | { |
| 48 | char *end; |
| 49 | int i; |
| 50 | |
| 51 | if (!enetaddr) |
| 52 | return; |
| 53 | |
| 54 | for (i = 0; i < 6; ++i) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 55 | enetaddr[i] = addr ? hextoul(addr, &end) : 0; |
Joe Hershberger | fb8977c | 2019-09-13 19:21:16 -0500 | [diff] [blame] | 56 | if (addr) |
| 57 | addr = (*end) ? end + 1 : end; |
| 58 | } |
| 59 | } |
Simon Glass | d721001 | 2019-12-06 21:41:39 -0700 | [diff] [blame] | 60 | |
| 61 | uint compute_ip_checksum(const void *vptr, uint nbytes) |
| 62 | { |
| 63 | int sum, oddbyte; |
| 64 | const unsigned short *ptr = vptr; |
| 65 | |
| 66 | sum = 0; |
| 67 | while (nbytes > 1) { |
| 68 | sum += *ptr++; |
| 69 | nbytes -= 2; |
| 70 | } |
| 71 | if (nbytes == 1) { |
| 72 | oddbyte = 0; |
| 73 | ((u8 *)&oddbyte)[0] = *(u8 *)ptr; |
| 74 | ((u8 *)&oddbyte)[1] = 0; |
| 75 | sum += oddbyte; |
| 76 | } |
| 77 | sum = (sum >> 16) + (sum & 0xffff); |
| 78 | sum += (sum >> 16); |
| 79 | sum = ~sum & 0xffff; |
| 80 | |
| 81 | return sum; |
| 82 | } |
| 83 | |
| 84 | uint add_ip_checksums(uint offset, uint sum, uint new) |
| 85 | { |
| 86 | ulong checksum; |
| 87 | |
| 88 | sum = ~sum & 0xffff; |
| 89 | new = ~new & 0xffff; |
| 90 | if (offset & 1) { |
| 91 | /* |
| 92 | * byte-swap the sum if it came from an odd offset; since the |
| 93 | * computation is endian-independent this works. |
| 94 | */ |
| 95 | new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00); |
| 96 | } |
| 97 | checksum = sum + new; |
| 98 | if (checksum > 0xffff) |
| 99 | checksum -= 0xffff; |
| 100 | |
| 101 | return (~checksum) & 0xffff; |
| 102 | } |
| 103 | |
| 104 | int ip_checksum_ok(const void *addr, uint nbytes) |
| 105 | { |
| 106 | return !(compute_ip_checksum(addr, nbytes) & 0xfffe); |
| 107 | } |