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 _cleanup_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_LOAD,
42 : BF_REQ_CHAIN_ATTACH,
43 : BF_REQ_CHAIN_UPDATE,
44 : BF_REQ_CHAIN_FLUSH,
45 :
46 : BF_REQ_COUNTERS_SET,
47 : BF_REQ_COUNTERS_GET,
48 : BF_REQ_CUSTOM,
49 : _BF_REQ_CMD_MAX,
50 : };
51 :
52 : /**
53 : * @struct bf_request
54 : *
55 : * Generic request format sent by the client to the daemon.
56 : *
57 : * @var bf_request::front
58 : * Front this request is targeted to.
59 : * @var bf_request::cmd
60 : * Command.
61 : * @var bf_request::ipt_cmd
62 : * Custom command for the IPT front.
63 : * @var bf_request::data_len
64 : * Length of the client-specific data.
65 : * @var bf_request::data
66 : * Client-specific data.
67 : */
68 : struct bf_request
69 : {
70 : enum bf_front front;
71 : enum bf_request_cmd cmd;
72 :
73 : /** Namespaces the request is coming from. This field will be automatically
74 : * populated by the daemon when receiving the request. */
75 : struct bf_ns *ns;
76 :
77 : union
78 : {
79 : struct
80 : {
81 : int ipt_cmd;
82 : };
83 : };
84 :
85 : size_t data_len;
86 : char data[];
87 : };
88 :
89 : /**
90 : * Allocate and initialise a new request.
91 : *
92 : * @param request Pointer to the request to allocate. Must be non-NULL.
93 : * @param data Client-specific data.
94 : * @param data_len Length of the client-specific data.
95 : * @return 0 on success or negative errno code on failure.
96 : */
97 : int bf_request_new(struct bf_request **request, const void *data,
98 : size_t data_len);
99 :
100 : /**
101 : * Free a request.
102 : *
103 : * If @p request points to a NULL pointer, this function does nothing. Once the
104 : * function returns, @p request points to a NULL pointer.
105 : *
106 : * @param request Request to free. Can't be NULL.
107 : */
108 : void bf_request_free(struct bf_request **request);
109 :
110 : /**
111 : * Copy a request.
112 : *
113 : * @param dest The destination request. It will be allocated during the call.
114 : * Can't be NULL.
115 : * @param src The source request, to copy. Can't be NULL.
116 : * @return 0 on success, negative error code on failure.
117 : */
118 : int bf_request_copy(struct bf_request **dest, const struct bf_request *src);
119 :
120 : /**
121 : * Get the total size of the request: request structure and data.
122 : *
123 : * @param request Request to get the size of. Can't be NULL.
124 : * @return Total size of the request.
125 : */
126 0 : static inline size_t bf_request_size(const struct bf_request *request)
127 : {
128 0 : bf_assert(request);
129 :
130 0 : return sizeof(struct bf_request) + request->data_len;
131 : }
132 :
133 : /**
134 : * @brief Convert a `bf_request_cmd` value to a string.
135 : *
136 : * @param cmd The request command to convert. Must be a valid command.
137 : * @return String representation of `cmd`.
138 : */
139 : const char *bf_request_cmd_to_str(enum bf_request_cmd cmd);
|