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 : #define _cleanup_bf_request_ __attribute__((cleanup(bf_request_free)))
14 :
15 : /**
16 : * @enum bf_request_cmd
17 : *
18 : * Defines a request type, so bpfilter can understand the client-specific
19 : * data contained in the request, and call the proper handler.
20 : *
21 : * @var bf_request_cmd::BF_REQ_CUSTOM
22 : * Custom request: only the front this request is targeted to is able to
23 : * understand what is the actual command. Allows for fronts to implement
24 : * new commands.
25 : */
26 : enum bf_request_cmd
27 : {
28 : /* Flush the ruleset: remove all the filtering rules defined for a
29 : * front-end. */
30 : BF_REQ_RULESET_FLUSH,
31 : BF_REQ_RULES_SET,
32 : BF_REQ_RULES_GET,
33 : BF_REQ_COUNTERS_SET,
34 : BF_REQ_COUNTERS_GET,
35 : BF_REQ_CUSTOM,
36 : _BF_REQ_CMD_MAX,
37 : };
38 :
39 : /**
40 : * @struct bf_request
41 : *
42 : * Generic request format sent by the client to the daemon.
43 : *
44 : * @var bf_request::front
45 : * Front this request is targeted to.
46 : * @var bf_request::cmd
47 : * Command.
48 : * @var bf_request::ipt_cmd
49 : * Custom command for the IPT front.
50 : * @var bf_request::data_len
51 : * Length of the client-specific data.
52 : * @var bf_request::data
53 : * Client-specific data.
54 : */
55 : struct bf_request
56 : {
57 : enum bf_front front;
58 : enum bf_request_cmd cmd;
59 :
60 : union
61 : {
62 : struct
63 : {
64 : int ipt_cmd;
65 : };
66 : };
67 :
68 : size_t data_len;
69 : char data[];
70 : };
71 :
72 : /**
73 : * Allocate and initialise a new request.
74 : *
75 : * @param request Pointer to the request to allocate. Must be non-NULL.
76 : * @param data Client-specific data.
77 : * @param data_len Length of the client-specific data.
78 : * @return 0 on success or negative errno code on failure.
79 : */
80 : int bf_request_new(struct bf_request **request, const void *data,
81 : size_t data_len);
82 :
83 : /**
84 : * Free a request.
85 : *
86 : * If @p request points to a NULL pointer, this function does nothing. Once the
87 : * function returns, @p request points to a NULL pointer.
88 : *
89 : * @param request Request to free. Can't be NULL.
90 : */
91 : void bf_request_free(struct bf_request **request);
92 :
93 : /**
94 : * Copy a request.
95 : *
96 : * @param dest The destination request. It will be allocated during the call.
97 : * Can't be NULL.
98 : * @param src The source request, to copy. Can't be NULL.
99 : * @return 0 on success, negative error code on failure.
100 : */
101 : int bf_request_copy(struct bf_request **dest, const struct bf_request *src);
102 :
103 : /**
104 : * Get the total size of the request: request structure and data.
105 : *
106 : * @param request Request to get the size of. Can't be NULL.
107 : * @return Total size of the request.
108 : */
109 0 : static inline size_t bf_request_size(const struct bf_request *request)
110 : {
111 0 : bf_assert(request);
112 :
113 0 : return sizeof(struct bf_request) + request->data_len;
114 : }
|