blob: eb2d1b3324286104e362a1e6b82430bf243b2d61 [file] [log] [blame]
Hugo Benichi2ac4d072019-05-28 14:51:23 +09001// 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 Evans260ff302019-07-25 11:22:50 +09005#include <arpa/inet.h>
6#include <ifaddrs.h>
7#include <linux/in6.h>
Hugo Benichidcc32392020-02-27 09:14:40 +09008#include <linux/vm_sockets.h>
Hugo Benichie8758b52020-04-03 14:49:01 +09009#include <net/route.h>
Jason Jeremy Iman16f91722020-01-14 09:53:28 +090010#include <netinet/icmp6.h>
Garrick Evans260ff302019-07-25 11:22:50 +090011#include <netinet/in.h>
Jason Jeremy Iman16f91722020-01-14 09:53:28 +090012#include <netinet/ip.h>
13#include <netinet/ip6.h>
14#include <netinet/udp.h>
Hugo Benichi2ac4d072019-05-28 14:51:23 +090015#include <stdint.h>
Hugo Benichidcc32392020-02-27 09:14:40 +090016#include <sys/socket.h>
Garrick Evans260ff302019-07-25 11:22:50 +090017#include <sys/types.h>
Hugo Benichidcc32392020-02-27 09:14:40 +090018#include <sys/un.h>
Hugo Benichi2ac4d072019-05-28 14:51:23 +090019
20#include <string>
21
Andreea Costinas29080492020-03-23 09:22:09 +010022#include <brillo/brillo_export.h>
23
Garrick Evans3388a032020-03-24 11:25:55 +090024#include "patchpanel/mac_address_generator.h"
Garrick Evans54861622019-07-19 09:05:09 +090025
Garrick Evans3388a032020-03-24 11:25:55 +090026#ifndef PATCHPANEL_NET_UTIL_H_
27#define PATCHPANEL_NET_UTIL_H_
Hugo Benichi2ac4d072019-05-28 14:51:23 +090028
Garrick Evans3388a032020-03-24 11:25:55 +090029namespace patchpanel {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090030
31// Reverses the byte order of the argument.
Andreea Costinas29080492020-03-23 09:22:09 +010032BRILLO_EXPORT constexpr uint32_t Byteswap32(uint32_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090033 return (x >> 24) | (x << 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000);
34}
35
36// Reverses the byte order of the argument.
Andreea Costinas29080492020-03-23 09:22:09 +010037BRILLO_EXPORT constexpr uint16_t Byteswap16(uint16_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090038 return (x >> 8) | (x << 8);
39}
40
41// Constexpr version of ntohl().
Andreea Costinas29080492020-03-23 09:22:09 +010042BRILLO_EXPORT constexpr uint32_t Ntohl(uint32_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090043 return Byteswap32(x);
44}
45
46// Constexpr version of htonl().
Andreea Costinas29080492020-03-23 09:22:09 +010047BRILLO_EXPORT constexpr uint32_t Htonl(uint32_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090048 return Byteswap32(x);
49}
50
51// Constexpr version of ntohs().
Andreea Costinas29080492020-03-23 09:22:09 +010052BRILLO_EXPORT constexpr uint16_t Ntohs(uint16_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090053 return Byteswap16(x);
54}
55
56// Constexpr version of htons().
Andreea Costinas29080492020-03-23 09:22:09 +010057BRILLO_EXPORT constexpr uint16_t Htons(uint16_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090058 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 Costinas29080492020-03-23 09:22:09 +010063BRILLO_EXPORT constexpr uint32_t Ipv4Addr(uint8_t b0,
64 uint8_t b1,
65 uint8_t b2,
66 uint8_t b3) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090067 return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
68}
69
Garrick Evans6f4fa3a2020-02-10 16:15:09 +090070// Returns the netmask in network byte order given a prefixl length.
Andreea Costinas29080492020-03-23 09:22:09 +010071BRILLO_EXPORT uint32_t Ipv4Netmask(uint32_t prefix_len);
Garrick Evans6f4fa3a2020-02-10 16:15:09 +090072
73// Returns the broadcast address in network byte order for the subnet provided.
Andreea Costinas29080492020-03-23 09:22:09 +010074BRILLO_EXPORT uint32_t Ipv4BroadcastAddr(uint32_t base, uint32_t prefix_len);
Garrick Evans6f4fa3a2020-02-10 16:15:09 +090075
Hugo Benichi2ac4d072019-05-28 14:51:23 +090076// Returns the literal representation of the IPv4 address given in network byte
77// order.
Andreea Costinas29080492020-03-23 09:22:09 +010078BRILLO_EXPORT std::string IPv4AddressToString(uint32_t addr);
Hugo Benichi2ac4d072019-05-28 14:51:23 +090079
Hugo Benichiaf02c262021-01-26 10:24:10 +090080// Returns the literal representation of the IPv6 address given.
81BRILLO_EXPORT std::string IPv6AddressToString(const struct in6_addr& addr);
82
Hugo Benichi2ac4d072019-05-28 14:51:23 +090083// Returns the CIDR representation of an IPv4 address given in network byte
84// order.
Andreea Costinas29080492020-03-23 09:22:09 +010085BRILLO_EXPORT std::string IPv4AddressToCidrString(uint32_t addr,
86 uint32_t prefix_length);
Hugo Benichi2ac4d072019-05-28 14:51:23 +090087
Garrick Evans54861622019-07-19 09:05:09 +090088// Returns a string representation of MAC address given.
Andreea Costinas29080492020-03-23 09:22:09 +010089BRILLO_EXPORT std::string MacAddressToString(const MacAddress& addr);
Garrick Evans54861622019-07-19 09:05:09 +090090
Andreea Costinas29080492020-03-23 09:22:09 +010091BRILLO_EXPORT bool FindFirstIPv6Address(const std::string& ifname,
92 struct in6_addr* address);
Garrick Evans260ff302019-07-25 11:22:50 +090093
Andreea Costinas29080492020-03-23 09:22:09 +010094BRILLO_EXPORT bool GenerateRandomIPv6Prefix(struct in6_addr* prefix, int len);
Garrick Evans260ff302019-07-25 11:22:50 +090095
Taoyu Lia0727dc2020-09-24 19:54:59 +090096BRILLO_EXPORT bool GenerateEUI64Address(in6_addr* address,
97 const in6_addr& prefix,
98 const MacAddress& mac);
99
Hugo Benichie8758b52020-04-03 14:49:01 +0900100BRILLO_EXPORT void SetSockaddrIn(struct sockaddr* sockaddr, uint32_t addr);
101
Andreea Costinas29080492020-03-23 09:22:09 +0100102BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
103 const struct in_addr& addr);
104BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
105 const struct in6_addr& addr);
106BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
107 const struct sockaddr& addr);
108BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
109 const struct sockaddr_storage& addr);
110BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
111 const struct sockaddr_in& addr);
112BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
113 const struct sockaddr_in6& addr);
114BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
115 const struct sockaddr_un& addr);
116BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
117 const struct sockaddr_vm& addr);
Garrick Evans260ff302019-07-25 11:22:50 +0900118
Hugo Benichie8758b52020-04-03 14:49:01 +0900119BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
120 const struct rtentry& route);
121
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900122// Fold 32-bit into 16 bits.
Andreea Costinas29080492020-03-23 09:22:09 +0100123BRILLO_EXPORT uint16_t FoldChecksum(uint32_t sum);
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900124
125// RFC 1071: We are doing calculation directly in network order.
126// Note this algorithm works regardless of the endianness of the host.
Andreea Costinas29080492020-03-23 09:22:09 +0100127BRILLO_EXPORT uint32_t NetChecksum(const void* data, ssize_t len);
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900128
Andreea Costinas29080492020-03-23 09:22:09 +0100129BRILLO_EXPORT uint16_t Ipv4Checksum(const iphdr* ip);
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900130
131// UDPv4 checksum along with IPv4 pseudo-header is defined in RFC 793,
132// Section 3.1.
Andreea Costinas29080492020-03-23 09:22:09 +0100133BRILLO_EXPORT uint16_t Udpv4Checksum(const iphdr* ip, const udphdr* udp);
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900134
135// ICMPv6 checksum is defined in RFC 8200 Section 8.1
Andreea Costinas29080492020-03-23 09:22:09 +0100136BRILLO_EXPORT uint16_t Icmpv6Checksum(const ip6_hdr* ip6,
137 const icmp6_hdr* icmp6);
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900138
Hugo Benichi69c989d2021-03-01 00:23:39 +0900139// Returns true if multicast forwarding should be enabled for this interface.
140// TODO(hugobenichi): Move to NetUtil, pending crosreview.com/2738499.
141BRILLO_EXPORT bool IsMulticastInterface(const std::string& ifname);
142
Garrick Evans3388a032020-03-24 11:25:55 +0900143} // namespace patchpanel
Hugo Benichi2ac4d072019-05-28 14:51:23 +0900144
Garrick Evans3388a032020-03-24 11:25:55 +0900145#endif // PATCHPANEL_NET_UTIL_H_