Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 5 | #include <arpa/inet.h> |
| 6 | #include <ifaddrs.h> |
| 7 | #include <linux/in6.h> |
Hugo Benichi | dcc3239 | 2020-02-27 09:14:40 +0900 | [diff] [blame] | 8 | #include <linux/vm_sockets.h> |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 9 | #include <netinet/icmp6.h> |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 10 | #include <netinet/in.h> |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 11 | #include <netinet/ip.h> |
| 12 | #include <netinet/ip6.h> |
| 13 | #include <netinet/udp.h> |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 14 | #include <stdint.h> |
Hugo Benichi | dcc3239 | 2020-02-27 09:14:40 +0900 | [diff] [blame] | 15 | #include <sys/socket.h> |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 16 | #include <sys/types.h> |
Hugo Benichi | dcc3239 | 2020-02-27 09:14:40 +0900 | [diff] [blame] | 17 | #include <sys/un.h> |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 18 | |
| 19 | #include <string> |
| 20 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 21 | #include <brillo/brillo_export.h> |
| 22 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 23 | #include "arc/network/mac_address_generator.h" |
| 24 | |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 25 | #ifndef ARC_NETWORK_NET_UTIL_H_ |
| 26 | #define ARC_NETWORK_NET_UTIL_H_ |
| 27 | |
| 28 | namespace arc_networkd { |
| 29 | |
| 30 | // Reverses the byte order of the argument. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 31 | BRILLO_EXPORT constexpr uint32_t Byteswap32(uint32_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 32 | return (x >> 24) | (x << 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000); |
| 33 | } |
| 34 | |
| 35 | // Reverses the byte order of the argument. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 36 | BRILLO_EXPORT constexpr uint16_t Byteswap16(uint16_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 37 | return (x >> 8) | (x << 8); |
| 38 | } |
| 39 | |
| 40 | // Constexpr version of ntohl(). |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 41 | BRILLO_EXPORT constexpr uint32_t Ntohl(uint32_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 42 | return Byteswap32(x); |
| 43 | } |
| 44 | |
| 45 | // Constexpr version of htonl(). |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 46 | BRILLO_EXPORT constexpr uint32_t Htonl(uint32_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 47 | return Byteswap32(x); |
| 48 | } |
| 49 | |
| 50 | // Constexpr version of ntohs(). |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 51 | BRILLO_EXPORT constexpr uint16_t Ntohs(uint16_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 52 | return Byteswap16(x); |
| 53 | } |
| 54 | |
| 55 | // Constexpr version of htons(). |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 56 | BRILLO_EXPORT constexpr uint16_t Htons(uint16_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 57 | return Byteswap16(x); |
| 58 | } |
| 59 | |
| 60 | // Returns the network-byte order int32 representation of the IPv4 address given |
| 61 | // byte per byte, most significant bytes first. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 62 | BRILLO_EXPORT constexpr uint32_t Ipv4Addr(uint8_t b0, |
| 63 | uint8_t b1, |
| 64 | uint8_t b2, |
| 65 | uint8_t b3) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 66 | return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0; |
| 67 | } |
| 68 | |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 69 | // Returns the netmask in network byte order given a prefixl length. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 70 | BRILLO_EXPORT uint32_t Ipv4Netmask(uint32_t prefix_len); |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 71 | |
| 72 | // Returns the broadcast address in network byte order for the subnet provided. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 73 | BRILLO_EXPORT uint32_t Ipv4BroadcastAddr(uint32_t base, uint32_t prefix_len); |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 74 | |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 75 | // Returns the literal representation of the IPv4 address given in network byte |
| 76 | // order. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 77 | BRILLO_EXPORT std::string IPv4AddressToString(uint32_t addr); |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 78 | |
| 79 | // Returns the CIDR representation of an IPv4 address given in network byte |
| 80 | // order. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 81 | BRILLO_EXPORT std::string IPv4AddressToCidrString(uint32_t addr, |
| 82 | uint32_t prefix_length); |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 83 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 84 | // Returns a string representation of MAC address given. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 85 | BRILLO_EXPORT std::string MacAddressToString(const MacAddress& addr); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 86 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 87 | BRILLO_EXPORT bool FindFirstIPv6Address(const std::string& ifname, |
| 88 | struct in6_addr* address); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 89 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 90 | BRILLO_EXPORT bool GenerateRandomIPv6Prefix(struct in6_addr* prefix, int len); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 91 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 92 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 93 | const struct in_addr& addr); |
| 94 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 95 | const struct in6_addr& addr); |
| 96 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 97 | const struct sockaddr& addr); |
| 98 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 99 | const struct sockaddr_storage& addr); |
| 100 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 101 | const struct sockaddr_in& addr); |
| 102 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 103 | const struct sockaddr_in6& addr); |
| 104 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 105 | const struct sockaddr_un& addr); |
| 106 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 107 | const struct sockaddr_vm& addr); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 108 | |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 109 | // Fold 32-bit into 16 bits. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 110 | BRILLO_EXPORT uint16_t FoldChecksum(uint32_t sum); |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 111 | |
| 112 | // RFC 1071: We are doing calculation directly in network order. |
| 113 | // Note this algorithm works regardless of the endianness of the host. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 114 | BRILLO_EXPORT uint32_t NetChecksum(const void* data, ssize_t len); |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 115 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 116 | BRILLO_EXPORT uint16_t Ipv4Checksum(const iphdr* ip); |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 117 | |
| 118 | // UDPv4 checksum along with IPv4 pseudo-header is defined in RFC 793, |
| 119 | // Section 3.1. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 120 | BRILLO_EXPORT uint16_t Udpv4Checksum(const iphdr* ip, const udphdr* udp); |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 121 | |
| 122 | // ICMPv6 checksum is defined in RFC 8200 Section 8.1 |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 123 | BRILLO_EXPORT uint16_t Icmpv6Checksum(const ip6_hdr* ip6, |
| 124 | const icmp6_hdr* icmp6); |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 125 | |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 126 | } // namespace arc_networkd |
| 127 | |
| 128 | #endif // ARC_NETWORK_NET_UTIL_H_ |