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> |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 9 | #include <net/route.h> |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 10 | #include <netinet/icmp6.h> |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 11 | #include <netinet/in.h> |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 12 | #include <netinet/ip.h> |
| 13 | #include <netinet/ip6.h> |
| 14 | #include <netinet/udp.h> |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 15 | #include <stdint.h> |
Hugo Benichi | dcc3239 | 2020-02-27 09:14:40 +0900 | [diff] [blame] | 16 | #include <sys/socket.h> |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 17 | #include <sys/types.h> |
Hugo Benichi | dcc3239 | 2020-02-27 09:14:40 +0900 | [diff] [blame] | 18 | #include <sys/un.h> |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 19 | |
| 20 | #include <string> |
| 21 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 22 | #include <brillo/brillo_export.h> |
| 23 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 24 | #include "patchpanel/mac_address_generator.h" |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 25 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 26 | #ifndef PATCHPANEL_NET_UTIL_H_ |
| 27 | #define PATCHPANEL_NET_UTIL_H_ |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 28 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 29 | namespace patchpanel { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 30 | |
| 31 | // Reverses the byte order of the argument. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 32 | BRILLO_EXPORT constexpr uint32_t Byteswap32(uint32_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 33 | return (x >> 24) | (x << 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000); |
| 34 | } |
| 35 | |
| 36 | // Reverses the byte order of the argument. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 37 | BRILLO_EXPORT constexpr uint16_t Byteswap16(uint16_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 38 | return (x >> 8) | (x << 8); |
| 39 | } |
| 40 | |
| 41 | // Constexpr version of ntohl(). |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 42 | BRILLO_EXPORT constexpr uint32_t Ntohl(uint32_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 43 | return Byteswap32(x); |
| 44 | } |
| 45 | |
| 46 | // Constexpr version of htonl(). |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 47 | BRILLO_EXPORT constexpr uint32_t Htonl(uint32_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 48 | return Byteswap32(x); |
| 49 | } |
| 50 | |
| 51 | // Constexpr version of ntohs(). |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 52 | BRILLO_EXPORT constexpr uint16_t Ntohs(uint16_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 53 | return Byteswap16(x); |
| 54 | } |
| 55 | |
| 56 | // Constexpr version of htons(). |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 57 | BRILLO_EXPORT constexpr uint16_t Htons(uint16_t x) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 58 | return Byteswap16(x); |
| 59 | } |
| 60 | |
| 61 | // Returns the network-byte order int32 representation of the IPv4 address given |
| 62 | // byte per byte, most significant bytes first. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 63 | BRILLO_EXPORT constexpr uint32_t Ipv4Addr(uint8_t b0, |
| 64 | uint8_t b1, |
| 65 | uint8_t b2, |
| 66 | uint8_t b3) { |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 67 | return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0; |
| 68 | } |
| 69 | |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 70 | // Returns the netmask in network byte order given a prefixl length. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 71 | BRILLO_EXPORT uint32_t Ipv4Netmask(uint32_t prefix_len); |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 72 | |
| 73 | // Returns the broadcast address in network byte order for the subnet provided. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 74 | BRILLO_EXPORT uint32_t Ipv4BroadcastAddr(uint32_t base, uint32_t prefix_len); |
Garrick Evans | 6f4fa3a | 2020-02-10 16:15:09 +0900 | [diff] [blame] | 75 | |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 76 | // Returns the literal representation of the IPv4 address given in network byte |
| 77 | // order. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 78 | BRILLO_EXPORT std::string IPv4AddressToString(uint32_t addr); |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 79 | |
Hugo Benichi | af02c26 | 2021-01-26 10:24:10 +0900 | [diff] [blame] | 80 | // Returns the literal representation of the IPv6 address given. |
| 81 | BRILLO_EXPORT std::string IPv6AddressToString(const struct in6_addr& addr); |
| 82 | |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 83 | // Returns the CIDR representation of an IPv4 address given in network byte |
| 84 | // order. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 85 | BRILLO_EXPORT std::string IPv4AddressToCidrString(uint32_t addr, |
| 86 | uint32_t prefix_length); |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 87 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 88 | // Returns a string representation of MAC address given. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 89 | BRILLO_EXPORT std::string MacAddressToString(const MacAddress& addr); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 90 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 91 | BRILLO_EXPORT bool FindFirstIPv6Address(const std::string& ifname, |
| 92 | struct in6_addr* address); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 93 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 94 | BRILLO_EXPORT bool GenerateRandomIPv6Prefix(struct in6_addr* prefix, int len); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 95 | |
Taoyu Li | a0727dc | 2020-09-24 19:54:59 +0900 | [diff] [blame] | 96 | BRILLO_EXPORT bool GenerateEUI64Address(in6_addr* address, |
| 97 | const in6_addr& prefix, |
| 98 | const MacAddress& mac); |
| 99 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 100 | BRILLO_EXPORT void SetSockaddrIn(struct sockaddr* sockaddr, uint32_t addr); |
| 101 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 102 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 103 | const struct in_addr& addr); |
| 104 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 105 | const struct in6_addr& addr); |
| 106 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 107 | const struct sockaddr& addr); |
| 108 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 109 | const struct sockaddr_storage& addr); |
| 110 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 111 | const struct sockaddr_in& addr); |
| 112 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 113 | const struct sockaddr_in6& addr); |
| 114 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 115 | const struct sockaddr_un& addr); |
| 116 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 117 | const struct sockaddr_vm& addr); |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 118 | |
Hugo Benichi | e8758b5 | 2020-04-03 14:49:01 +0900 | [diff] [blame] | 119 | BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream, |
| 120 | const struct rtentry& route); |
| 121 | |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 122 | // Fold 32-bit into 16 bits. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 123 | BRILLO_EXPORT uint16_t FoldChecksum(uint32_t sum); |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 124 | |
| 125 | // RFC 1071: We are doing calculation directly in network order. |
| 126 | // Note this algorithm works regardless of the endianness of the host. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 127 | BRILLO_EXPORT uint32_t NetChecksum(const void* data, ssize_t len); |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 128 | |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 129 | BRILLO_EXPORT uint16_t Ipv4Checksum(const iphdr* ip); |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 130 | |
| 131 | // UDPv4 checksum along with IPv4 pseudo-header is defined in RFC 793, |
| 132 | // Section 3.1. |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 133 | BRILLO_EXPORT uint16_t Udpv4Checksum(const iphdr* ip, const udphdr* udp); |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 134 | |
| 135 | // ICMPv6 checksum is defined in RFC 8200 Section 8.1 |
Andreea Costinas | 2908049 | 2020-03-23 09:22:09 +0100 | [diff] [blame] | 136 | BRILLO_EXPORT uint16_t Icmpv6Checksum(const ip6_hdr* ip6, |
| 137 | const icmp6_hdr* icmp6); |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 138 | |
Hugo Benichi | 69c989d | 2021-03-01 00:23:39 +0900 | [diff] [blame^] | 139 | // Returns true if multicast forwarding should be enabled for this interface. |
| 140 | // TODO(hugobenichi): Move to NetUtil, pending crosreview.com/2738499. |
| 141 | BRILLO_EXPORT bool IsMulticastInterface(const std::string& ifname); |
| 142 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 143 | } // namespace patchpanel |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 144 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 145 | #endif // PATCHPANEL_NET_UTIL_H_ |