Android-cuttlefish cvd tool
nft_messages.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ALLOCD_NET_NFT_MESSAGES_H_
18#define ALLOCD_NET_NFT_MESSAGES_H_
19
20#include <vector>
21
22#include "absl/strings/string_view.h"
23
26
27// Builds nf_tables netlink messages for ip family tables and parses replies.
28namespace cuttlefish::nft {
29
30// Ruleset changes:
31// The kernel only accepts these inside a Batch.
32// It applies a batch all or nothing.
33
34// Corresponds to create table ip <name> with flags owner.
35// Fails if <name> already exists.
36// The kernel deletes the table when the socket that created it closes.
37NetlinkRequest NewTable(absl::string_view name);
38
39// Corresponds to delete table ip <name>.
40NetlinkRequest DeleteTable(absl::string_view name);
41
42// Corresponds to add chain ip <table> <chain>
43// with type nat hook postrouting priority srcnat.
44NetlinkRequest NewNatChain(absl::string_view table, absl::string_view chain);
45
46// Corresponds to add rule ip <table> <chain>
47// ip saddr <source_cidr> masquerade.
48Result<NetlinkRequest> NewMasqueradeRule(absl::string_view table,
49 absl::string_view chain,
50 absl::string_view source_cidr);
51
52// Wraps the changes in batch begin and end messages.
53std::vector<char> Batch(const std::vector<NetlinkRequest>& changes);
54
55// Fails if the kernel rejected the batch or didn't reply.
56Result<void> CheckReplies(const std::vector<char>& reply_bytes);
57
58// Lookups:
59// The kernel rejects these inside a batch, so they return bytes ready to send.
60
61// Corresponds to list table ip <name>.
62std::vector<char> GetTable(absl::string_view name);
63
64// Returns whether GetTable's reply says the table exists.
65Result<bool> TableExistsFromReply(const std::vector<char>& reply_bytes);
66
67} // namespace cuttlefish::nft
68
69#endif // ALLOCD_NET_NFT_MESSAGES_H_
Definition: nft_messages.cc:44
std::vector< char > Batch(const std::vector< NetlinkRequest > &changes)
Definition: nft_messages.cc:263
std::vector< char > GetTable(absl::string_view name)
Definition: nft_messages.cc:287
NetlinkRequest NewTable(absl::string_view name)
Definition: nft_messages.cc:220
Result< void > CheckReplies(const std::vector< char > &reply_bytes)
Definition: nft_messages.cc:273
Result< bool > TableExistsFromReply(const std::vector< char > &reply_bytes)
Definition: nft_messages.cc:295
Result< NetlinkRequest > NewMasqueradeRule(absl::string_view table, absl::string_view chain, absl::string_view source_cidr)
Definition: nft_messages.cc:246
NetlinkRequest NewNatChain(absl::string_view table, absl::string_view chain)
Definition: nft_messages.cc:234
NetlinkRequest DeleteTable(absl::string_view name)
Definition: nft_messages.cc:228