Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0-only */
2 : /*
3 : * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4 : */
5 :
6 : #pragma once
7 :
8 : #include <stddef.h>
9 :
10 : #include "core/front.h"
11 : #include "core/helper.h"
12 :
13 : struct bf_ns;
14 :
15 : #define _free_bf_request_ __attribute__((cleanup(bf_request_free)))
16 :
17 : /**
18 : * @enum bf_request_cmd
19 : *
20 : * Defines a request type, so bpfilter can understand the client-specific
21 : * data contained in the request, and call the proper handler.
22 : *
23 : * @var bf_request_cmd::BF_REQ_CUSTOM
24 : * Custom request: only the front this request is targeted to is able to
25 : * understand what is the actual command. Allows for fronts to implement
26 : * new commands.
27 : */
28 : enum bf_request_cmd
29 : {
30 : /* Flush the ruleset: remove all the filtering rules defined for a
31 : * front-end. */
32 : BF_REQ_RULESET_FLUSH,
33 : BF_REQ_RULESET_GET,
34 :
35 : /** Set the current ruleset. Existing chains are flushed for the current
36 : * front-end and replaced with the chains defined in the request. */
37 : BF_REQ_RULESET_SET,
38 :
39 : BF_REQ_CHAIN_SET,
40 : BF_REQ_CHAIN_GET,
41 : BF_REQ_CHAIN_LOGS_FD,
42 : BF_REQ_CHAIN_LOAD,
43 : BF_REQ_CHAIN_ATTACH,
44 : BF_REQ_CHAIN_UPDATE,
45 : BF_REQ_CHAIN_FLUSH,
46 :
47 : BF_REQ_COUNTERS_SET,
48 : BF_REQ_COUNTERS_GET,
49 : BF_REQ_CUSTOM,
50 : _BF_REQ_CMD_MAX,
51 : };
52 :
53 : /**
54 : * @struct bf_request
55 : *
56 : * Generic request format sent by the client to the daemon.
57 : *
58 : * @var bf_request::front
59 : * Front this request is targeted to.
60 : * @var bf_request::cmd
61 : * Command.
62 : * @var bf_request::ipt_cmd
63 : * Custom command for the IPT front.
64 : * @var bf_request::data_len
65 : * Length of the client-specific data.
66 : * @var bf_request::data
67 : * Client-specific data.
68 : */
69 : struct bf_request
70 : {
71 : enum bf_front front;
72 : enum bf_request_cmd cmd;
73 :
74 : /** Namespaces the request is coming from. This field will be automatically
75 : * populated by the daemon when receiving the request. */
76 : struct bf_ns *ns;
77 :
78 : /** File descriptor of the receiver socket. This field is automatically
79 : * populated by the daemon when receiving the request. The request doesn't
80 : * own the file descriptor. */
81 : int fd;
82 :
83 : union
84 : {
85 : struct
86 : {
87 : int ipt_cmd;
88 : };
89 : };
90 :
91 : size_t data_len;
92 : char data[];
93 : };
94 :
95 : /**
96 : * Allocate and initialise a new request.
97 : *
98 : * @param request Pointer to the request to allocate. Must be non-NULL.
99 : * @param data Client-specific data.
100 : * @param data_len Length of the client-specific data.
101 : * @return 0 on success or negative errno code on failure.
102 : */
103 : int bf_request_new(struct bf_request **request, const void *data,
104 : size_t data_len);
105 :
106 : /**
107 : * Free a request.
108 : *
109 : * If @p request points to a NULL pointer, this function does nothing. Once the
110 : * function returns, @p request points to a NULL pointer.
111 : *
112 : * @param request Request to free. Can't be NULL.
113 : */
114 : void bf_request_free(struct bf_request **request);
115 :
116 : /**
117 : * Copy a request.
118 : *
119 : * @param dest The destination request. It will be allocated during the call.
120 : * Can't be NULL.
121 : * @param src The source request, to copy. Can't be NULL.
122 : * @return 0 on success, negative error code on failure.
123 : */
124 : int bf_request_copy(struct bf_request **dest, const struct bf_request *src);
125 :
126 : /**
127 : * Get the total size of the request: request structure and data.
128 : *
129 : * @param request Request to get the size of. Can't be NULL.
130 : * @return Total size of the request.
131 : */
132 0 : static inline size_t bf_request_size(const struct bf_request *request)
133 : {
134 0 : bf_assert(request);
135 :
136 0 : return sizeof(struct bf_request) + request->data_len;
137 : }
138 :
139 : /**
140 : * @brief Convert a `bf_request_cmd` value to a string.
141 : *
142 : * @param cmd The request command to convert. Must be a valid command.
143 : * @return String representation of `cmd`.
144 : */
145 : const char *bf_request_cmd_to_str(enum bf_request_cmd cmd);
|