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