xref: /linux-6.15/include/linux/hid_bpf.h (revision 3ac83fcd)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 
3 #ifndef __HID_BPF_H
4 #define __HID_BPF_H
5 
6 #include <linux/bpf.h>
7 #include <linux/mutex.h>
8 #include <linux/srcu.h>
9 #include <uapi/linux/hid.h>
10 
11 struct hid_device;
12 
13 /*
14  * The following is the user facing HID BPF API.
15  *
16  * Extra care should be taken when editing this part, as
17  * it might break existing out of the tree bpf programs.
18  */
19 
20 /**
21  * struct hid_bpf_ctx - User accessible data for all HID programs
22  *
23  * ``data`` is not directly accessible from the context. We need to issue
24  * a call to hid_bpf_get_data() in order to get a pointer to that field.
25  *
26  * @hid: the &struct hid_device representing the device itself
27  * @allocated_size: Allocated size of data.
28  *
29  *                  This is how much memory is available and can be requested
30  *                  by the HID program.
31  *                  Note that for ``HID_BPF_RDESC_FIXUP``, that memory is set to
32  *                  ``4096`` (4 KB)
33  * @size: Valid data in the data field.
34  *
35  *        Programs can get the available valid size in data by fetching this field.
36  *        Programs can also change this value by returning a positive number in the
37  *        program.
38  *        To discard the event, return a negative error code.
39  *
40  *        ``size`` must always be less or equal than ``allocated_size`` (it is enforced
41  *        once all BPF programs have been run).
42  * @retval: Return value of the previous program.
43  *
44  * ``hid`` and ``allocated_size`` are read-only, ``size`` and ``retval`` are read-write.
45  */
46 struct hid_bpf_ctx {
47 	struct hid_device *hid;
48 	__u32 allocated_size;
49 	union {
50 		__s32 retval;
51 		__s32 size;
52 	};
53 };
54 
55 /*
56  * Below is HID internal
57  */
58 
59 #define HID_BPF_MAX_PROGS_PER_DEV 64
60 #define HID_BPF_FLAG_MASK (((HID_BPF_FLAG_MAX - 1) << 1) - 1)
61 
62 
63 struct hid_report_enum;
64 
65 struct hid_ops {
66 	struct hid_report *(*hid_get_report)(struct hid_report_enum *report_enum, const u8 *data);
67 	int (*hid_hw_raw_request)(struct hid_device *hdev,
68 				  unsigned char reportnum, __u8 *buf,
69 				  size_t len, enum hid_report_type rtype,
70 				  enum hid_class_request reqtype,
71 				  __u64 source, bool from_bpf);
72 	int (*hid_hw_output_report)(struct hid_device *hdev, __u8 *buf, size_t len,
73 				    __u64 source, bool from_bpf);
74 	int (*hid_input_report)(struct hid_device *hid, enum hid_report_type type,
75 				u8 *data, u32 size, int interrupt, u64 source);
76 	struct module *owner;
77 	const struct bus_type *bus_type;
78 };
79 
80 extern struct hid_ops *hid_ops;
81 
82 /**
83  * struct hid_bpf_ops - A BPF struct_ops of callbacks allowing to attach HID-BPF
84  *			programs to a HID device
85  * @hid_id: the HID uniq ID to attach to. This is writeable before ``load()``, and
86  *	    cannot be changed after
87  * @flags: flags used while attaching the struct_ops to the device. Currently only
88  *	   available value is %0 or ``BPF_F_BEFORE``.
89  *	   Writeable only before ``load()``
90  */
91 struct hid_bpf_ops {
92 	/* hid_id needs to stay first so we can easily change it
93 	 * from userspace.
94 	 */
95 	int			hid_id;
96 	u32			flags;
97 
98 	/* private: do not show up in the docs */
99 	struct list_head	list;
100 
101 	/* public: rest should show up in the docs */
102 
103 	/**
104 	 * @hid_device_event: called whenever an event is coming in from the device
105 	 *
106 	 * It has the following arguments:
107 	 *
108 	 * ``ctx``: The HID-BPF context as &struct hid_bpf_ctx
109 	 *
110 	 * Return: %0 on success and keep processing; a positive
111 	 * value to change the incoming size buffer; a negative
112 	 * error code to interrupt the processing of this event
113 	 *
114 	 * Context: Interrupt context.
115 	 */
116 	int (*hid_device_event)(struct hid_bpf_ctx *ctx, enum hid_report_type report_type,
117 				__u64 source);
118 
119 	/**
120 	 * @hid_rdesc_fixup: called when the probe function parses the report descriptor
121 	 * of the HID device
122 	 *
123 	 * It has the following arguments:
124 	 *
125 	 * ``ctx``: The HID-BPF context as &struct hid_bpf_ctx
126 	 *
127 	 * Return: %0 on success and keep processing; a positive
128 	 * value to change the incoming size buffer; a negative
129 	 * error code to interrupt the processing of this device
130 	 */
131 	int (*hid_rdesc_fixup)(struct hid_bpf_ctx *ctx);
132 
133 	/**
134 	 * @hid_hw_request: called whenever a hid_hw_raw_request() call is emitted
135 	 * on the HID device
136 	 *
137 	 * It has the following arguments:
138 	 *
139 	 * ``ctx``: The HID-BPF context as &struct hid_bpf_ctx
140 	 * ``reportnum``: the report number, as in hid_hw_raw_request()
141 	 * ``rtype``: the report type (``HID_INPUT_REPORT``, ``HID_FEATURE_REPORT``,
142 	 *            ``HID_OUTPUT_REPORT``)
143 	 * ``reqtype``: the request
144 	 * ``source``: a u64 referring to a uniq but identifiable source. If %0, the
145 	 *             kernel itself emitted that call. For hidraw, ``source`` is set
146 	 *             to the associated ``struct file *``.
147 	 *
148 	 * Return: %0 to keep processing the request by hid-core; any other value
149 	 * stops hid-core from processing that event. A positive value should be
150 	 * returned with the number of bytes returned in the incoming buffer; a
151 	 * negative error code interrupts the processing of this call.
152 	 */
153 	int (*hid_hw_request)(struct hid_bpf_ctx *ctx, unsigned char reportnum,
154 			       enum hid_report_type rtype, enum hid_class_request reqtype,
155 			       __u64 source);
156 
157 	/**
158 	 * @hid_hw_output_report: called whenever a hid_hw_output_report() call is emitted
159 	 * on the HID device
160 	 *
161 	 * It has the following arguments:
162 	 *
163 	 * ``ctx``: The HID-BPF context as &struct hid_bpf_ctx
164 	 * ``source``: a u64 referring to a uniq but identifiable source. If %0, the
165 	 *             kernel itself emitted that call. For hidraw, ``source`` is set
166 	 *             to the associated ``struct file *``.
167 	 *
168 	 * Return: %0 to keep processing the request by hid-core; any other value
169 	 * stops hid-core from processing that event. A positive value should be
170 	 * returned with the number of bytes written to the device; a negative error
171 	 * code interrupts the processing of this call.
172 	 */
173 	int (*hid_hw_output_report)(struct hid_bpf_ctx *ctx, __u64 source);
174 
175 
176 	/* private: do not show up in the docs */
177 	struct hid_device *hdev;
178 };
179 
180 /* stored in each device */
181 struct hid_bpf {
182 	u8 *device_data;		/* allocated when a bpf program of type
183 					 * SEC(f.../hid_bpf_device_event) has been attached
184 					 * to this HID device
185 					 */
186 	u32 allocated_data;
187 	bool destroyed;			/* prevents the assignment of any progs */
188 
189 	struct hid_bpf_ops *rdesc_ops;
190 	struct list_head prog_list;
191 	struct mutex prog_list_lock;	/* protects prog_list update */
192 	struct srcu_struct srcu;	/* protects prog_list read-only access */
193 };
194 
195 #ifdef CONFIG_HID_BPF
196 u8 *dispatch_hid_bpf_device_event(struct hid_device *hid, enum hid_report_type type, u8 *data,
197 				  u32 *size, int interrupt, u64 source);
198 int dispatch_hid_bpf_raw_requests(struct hid_device *hdev,
199 				  unsigned char reportnum, __u8 *buf,
200 				  u32 size, enum hid_report_type rtype,
201 				  enum hid_class_request reqtype,
202 				  __u64 source, bool from_bpf);
203 int dispatch_hid_bpf_output_report(struct hid_device *hdev, __u8 *buf, u32 size,
204 				   __u64 source, bool from_bpf);
205 int hid_bpf_connect_device(struct hid_device *hdev);
206 void hid_bpf_disconnect_device(struct hid_device *hdev);
207 void hid_bpf_destroy_device(struct hid_device *hid);
208 int hid_bpf_device_init(struct hid_device *hid);
209 u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int *size);
210 #else /* CONFIG_HID_BPF */
211 static inline u8 *dispatch_hid_bpf_device_event(struct hid_device *hid, enum hid_report_type type,
212 						u8 *data, u32 *size, int interrupt,
213 						u64 source) { return data; }
214 static inline int dispatch_hid_bpf_raw_requests(struct hid_device *hdev,
215 						unsigned char reportnum, u8 *buf,
216 						u32 size, enum hid_report_type rtype,
217 						enum hid_class_request reqtype,
218 						u64 source, bool from_bpf) { return 0; }
219 static inline int dispatch_hid_bpf_output_report(struct hid_device *hdev, __u8 *buf, u32 size,
220 						 __u64 source, bool from_bpf) { return 0; }
221 static inline int hid_bpf_connect_device(struct hid_device *hdev) { return 0; }
222 static inline void hid_bpf_disconnect_device(struct hid_device *hdev) {}
223 static inline void hid_bpf_destroy_device(struct hid_device *hid) {}
224 static inline int hid_bpf_device_init(struct hid_device *hid) { return 0; }
225 #define call_hid_bpf_rdesc_fixup(_hdev, _rdesc, _size)	\
226 		((u8 *)kmemdup(_rdesc, *(_size), GFP_KERNEL))
227 
228 #endif /* CONFIG_HID_BPF */
229 
230 #endif /* __HID_BPF_H */
231