bpf: skmsg, fix psock create on existing kcm/tls port
[linux-block.git] / include / linux / skmsg.h
CommitLineData
604326b4
DB
1/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
3
4#ifndef _LINUX_SKMSG_H
5#define _LINUX_SKMSG_H
6
7#include <linux/bpf.h>
8#include <linux/filter.h>
9#include <linux/scatterlist.h>
10#include <linux/skbuff.h>
11
12#include <net/sock.h>
13#include <net/tcp.h>
14#include <net/strparser.h>
15
16#define MAX_MSG_FRAGS MAX_SKB_FRAGS
17
18enum __sk_action {
19 __SK_DROP = 0,
20 __SK_PASS,
21 __SK_REDIRECT,
22 __SK_NONE,
23};
24
25struct sk_msg_sg {
26 u32 start;
27 u32 curr;
28 u32 end;
29 u32 size;
30 u32 copybreak;
31 bool copy[MAX_MSG_FRAGS];
d3b18ad3
JF
32 /* The extra element is used for chaining the front and sections when
33 * the list becomes partitioned (e.g. end < start). The crypto APIs
34 * require the chaining.
35 */
36 struct scatterlist data[MAX_MSG_FRAGS + 1];
604326b4
DB
37};
38
39struct sk_msg {
40 struct sk_msg_sg sg;
41 void *data;
42 void *data_end;
43 u32 apply_bytes;
44 u32 cork_bytes;
45 u32 flags;
46 struct sk_buff *skb;
47 struct sock *sk_redir;
48 struct sock *sk;
49 struct list_head list;
50};
51
52struct sk_psock_progs {
53 struct bpf_prog *msg_parser;
54 struct bpf_prog *skb_parser;
55 struct bpf_prog *skb_verdict;
56};
57
58enum sk_psock_state_bits {
59 SK_PSOCK_TX_ENABLED,
60};
61
62struct sk_psock_link {
63 struct list_head list;
64 struct bpf_map *map;
65 void *link_raw;
66};
67
68struct sk_psock_parser {
69 struct strparser strp;
70 bool enabled;
71 void (*saved_data_ready)(struct sock *sk);
72};
73
74struct sk_psock_work_state {
75 struct sk_buff *skb;
76 u32 len;
77 u32 off;
78};
79
80struct sk_psock {
81 struct sock *sk;
82 struct sock *sk_redir;
83 u32 apply_bytes;
84 u32 cork_bytes;
85 u32 eval;
86 struct sk_msg *cork;
87 struct sk_psock_progs progs;
88 struct sk_psock_parser parser;
89 struct sk_buff_head ingress_skb;
90 struct list_head ingress_msg;
91 unsigned long state;
92 struct list_head link;
93 spinlock_t link_lock;
94 refcount_t refcnt;
95 void (*saved_unhash)(struct sock *sk);
96 void (*saved_close)(struct sock *sk, long timeout);
97 void (*saved_write_space)(struct sock *sk);
98 struct proto *sk_proto;
99 struct sk_psock_work_state work_state;
100 struct work_struct work;
101 union {
102 struct rcu_head rcu;
103 struct work_struct gc;
104 };
105};
106
107int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
108 int elem_first_coalesce);
d829e9c4
DB
109int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
110 u32 off, u32 len);
604326b4
DB
111void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len);
112int sk_msg_free(struct sock *sk, struct sk_msg *msg);
113int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg);
114void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes);
115void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg,
116 u32 bytes);
117
118void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes);
d3b18ad3 119void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes);
604326b4
DB
120
121int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
122 struct sk_msg *msg, u32 bytes);
123int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
124 struct sk_msg *msg, u32 bytes);
125
126static inline void sk_msg_check_to_free(struct sk_msg *msg, u32 i, u32 bytes)
127{
128 WARN_ON(i == msg->sg.end && bytes);
129}
130
131static inline void sk_msg_apply_bytes(struct sk_psock *psock, u32 bytes)
132{
133 if (psock->apply_bytes) {
134 if (psock->apply_bytes < bytes)
135 psock->apply_bytes = 0;
136 else
137 psock->apply_bytes -= bytes;
138 }
139}
140
141#define sk_msg_iter_var_prev(var) \
142 do { \
143 if (var == 0) \
144 var = MAX_MSG_FRAGS - 1; \
145 else \
146 var--; \
147 } while (0)
148
149#define sk_msg_iter_var_next(var) \
150 do { \
151 var++; \
152 if (var == MAX_MSG_FRAGS) \
153 var = 0; \
154 } while (0)
155
156#define sk_msg_iter_prev(msg, which) \
157 sk_msg_iter_var_prev(msg->sg.which)
158
159#define sk_msg_iter_next(msg, which) \
160 sk_msg_iter_var_next(msg->sg.which)
161
162static inline void sk_msg_clear_meta(struct sk_msg *msg)
163{
164 memset(&msg->sg, 0, offsetofend(struct sk_msg_sg, copy));
165}
166
167static inline void sk_msg_init(struct sk_msg *msg)
168{
d3b18ad3 169 BUILD_BUG_ON(ARRAY_SIZE(msg->sg.data) - 1 != MAX_MSG_FRAGS);
604326b4 170 memset(msg, 0, sizeof(*msg));
d3b18ad3 171 sg_init_marker(msg->sg.data, MAX_MSG_FRAGS);
604326b4
DB
172}
173
174static inline void sk_msg_xfer(struct sk_msg *dst, struct sk_msg *src,
175 int which, u32 size)
176{
177 dst->sg.data[which] = src->sg.data[which];
178 dst->sg.data[which].length = size;
3f4c3127 179 dst->sg.size += size;
604326b4
DB
180 src->sg.data[which].length -= size;
181 src->sg.data[which].offset += size;
182}
183
d3b18ad3
JF
184static inline void sk_msg_xfer_full(struct sk_msg *dst, struct sk_msg *src)
185{
186 memcpy(dst, src, sizeof(*src));
187 sk_msg_init(src);
188}
189
8734a162
JF
190static inline bool sk_msg_full(const struct sk_msg *msg)
191{
192 return (msg->sg.end == msg->sg.start) && msg->sg.size;
193}
194
604326b4
DB
195static inline u32 sk_msg_elem_used(const struct sk_msg *msg)
196{
8734a162
JF
197 if (sk_msg_full(msg))
198 return MAX_MSG_FRAGS;
199
604326b4
DB
200 return msg->sg.end >= msg->sg.start ?
201 msg->sg.end - msg->sg.start :
202 msg->sg.end + (MAX_MSG_FRAGS - msg->sg.start);
203}
204
604326b4
DB
205static inline struct scatterlist *sk_msg_elem(struct sk_msg *msg, int which)
206{
207 return &msg->sg.data[which];
208}
209
210static inline struct page *sk_msg_page(struct sk_msg *msg, int which)
211{
212 return sg_page(sk_msg_elem(msg, which));
213}
214
215static inline bool sk_msg_to_ingress(const struct sk_msg *msg)
216{
217 return msg->flags & BPF_F_INGRESS;
218}
219
220static inline void sk_msg_compute_data_pointers(struct sk_msg *msg)
221{
222 struct scatterlist *sge = sk_msg_elem(msg, msg->sg.start);
223
224 if (msg->sg.copy[msg->sg.start]) {
225 msg->data = NULL;
226 msg->data_end = NULL;
227 } else {
228 msg->data = sg_virt(sge);
229 msg->data_end = msg->data + sge->length;
230 }
231}
232
233static inline void sk_msg_page_add(struct sk_msg *msg, struct page *page,
234 u32 len, u32 offset)
235{
236 struct scatterlist *sge;
237
238 get_page(page);
239 sge = sk_msg_elem(msg, msg->sg.end);
240 sg_set_page(sge, page, len, offset);
241 sg_unmark_end(sge);
242
243 msg->sg.copy[msg->sg.end] = true;
244 msg->sg.size += len;
245 sk_msg_iter_next(msg, end);
246}
247
d3b18ad3
JF
248static inline void sk_msg_sg_copy(struct sk_msg *msg, u32 i, bool copy_state)
249{
250 do {
251 msg->sg.copy[i] = copy_state;
252 sk_msg_iter_var_next(i);
253 if (i == msg->sg.end)
254 break;
255 } while (1);
256}
257
258static inline void sk_msg_sg_copy_set(struct sk_msg *msg, u32 start)
259{
260 sk_msg_sg_copy(msg, start, true);
261}
262
263static inline void sk_msg_sg_copy_clear(struct sk_msg *msg, u32 start)
264{
265 sk_msg_sg_copy(msg, start, false);
266}
267
604326b4
DB
268static inline struct sk_psock *sk_psock(const struct sock *sk)
269{
270 return rcu_dereference_sk_user_data(sk);
271}
272
604326b4
DB
273static inline void sk_psock_queue_msg(struct sk_psock *psock,
274 struct sk_msg *msg)
275{
276 list_add_tail(&msg->list, &psock->ingress_msg);
277}
278
d3b18ad3
JF
279static inline bool sk_psock_queue_empty(const struct sk_psock *psock)
280{
281 return psock ? list_empty(&psock->ingress_msg) : true;
282}
283
604326b4
DB
284static inline void sk_psock_report_error(struct sk_psock *psock, int err)
285{
286 struct sock *sk = psock->sk;
287
288 sk->sk_err = err;
289 sk->sk_error_report(sk);
290}
291
292struct sk_psock *sk_psock_init(struct sock *sk, int node);
293
294int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock);
295void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock);
296void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock);
297
298int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
299 struct sk_msg *msg);
300
301static inline struct sk_psock_link *sk_psock_init_link(void)
302{
303 return kzalloc(sizeof(struct sk_psock_link),
304 GFP_ATOMIC | __GFP_NOWARN);
305}
306
307static inline void sk_psock_free_link(struct sk_psock_link *link)
308{
309 kfree(link);
310}
311
312struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock);
313#if defined(CONFIG_BPF_STREAM_PARSER)
314void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link);
315#else
316static inline void sk_psock_unlink(struct sock *sk,
317 struct sk_psock_link *link)
318{
319}
320#endif
321
322void __sk_psock_purge_ingress_msg(struct sk_psock *psock);
323
324static inline void sk_psock_cork_free(struct sk_psock *psock)
325{
326 if (psock->cork) {
327 sk_msg_free(psock->sk, psock->cork);
328 kfree(psock->cork);
329 psock->cork = NULL;
330 }
331}
332
333static inline void sk_psock_update_proto(struct sock *sk,
334 struct sk_psock *psock,
335 struct proto *ops)
336{
337 psock->saved_unhash = sk->sk_prot->unhash;
338 psock->saved_close = sk->sk_prot->close;
339 psock->saved_write_space = sk->sk_write_space;
340
341 psock->sk_proto = sk->sk_prot;
342 sk->sk_prot = ops;
343}
344
345static inline void sk_psock_restore_proto(struct sock *sk,
346 struct sk_psock *psock)
347{
348 if (psock->sk_proto) {
349 sk->sk_prot = psock->sk_proto;
350 psock->sk_proto = NULL;
351 }
352}
353
354static inline void sk_psock_set_state(struct sk_psock *psock,
355 enum sk_psock_state_bits bit)
356{
357 set_bit(bit, &psock->state);
358}
359
360static inline void sk_psock_clear_state(struct sk_psock *psock,
361 enum sk_psock_state_bits bit)
362{
363 clear_bit(bit, &psock->state);
364}
365
366static inline bool sk_psock_test_state(const struct sk_psock *psock,
367 enum sk_psock_state_bits bit)
368{
369 return test_bit(bit, &psock->state);
370}
371
5032d079
JF
372static inline struct sk_psock *sk_psock_get_checked(struct sock *sk)
373{
374 struct sk_psock *psock;
375
376 rcu_read_lock();
377 psock = sk_psock(sk);
378 if (psock) {
379 if (sk->sk_prot->recvmsg != tcp_bpf_recvmsg) {
380 psock = ERR_PTR(-EBUSY);
381 goto out;
382 }
383
384 if (!refcount_inc_not_zero(&psock->refcnt))
385 psock = ERR_PTR(-EBUSY);
386 }
387out:
388 rcu_read_unlock();
389 return psock;
390}
391
604326b4
DB
392static inline struct sk_psock *sk_psock_get(struct sock *sk)
393{
394 struct sk_psock *psock;
395
396 rcu_read_lock();
397 psock = sk_psock(sk);
398 if (psock && !refcount_inc_not_zero(&psock->refcnt))
399 psock = NULL;
400 rcu_read_unlock();
401 return psock;
402}
403
404void sk_psock_stop(struct sock *sk, struct sk_psock *psock);
405void sk_psock_destroy(struct rcu_head *rcu);
406void sk_psock_drop(struct sock *sk, struct sk_psock *psock);
407
408static inline void sk_psock_put(struct sock *sk, struct sk_psock *psock)
409{
410 if (refcount_dec_and_test(&psock->refcnt))
411 sk_psock_drop(sk, psock);
412}
413
414static inline void psock_set_prog(struct bpf_prog **pprog,
415 struct bpf_prog *prog)
416{
417 prog = xchg(pprog, prog);
418 if (prog)
419 bpf_prog_put(prog);
420}
421
422static inline void psock_progs_drop(struct sk_psock_progs *progs)
423{
424 psock_set_prog(&progs->msg_parser, NULL);
425 psock_set_prog(&progs->skb_parser, NULL);
426 psock_set_prog(&progs->skb_verdict, NULL);
427}
428
429#endif /* _LINUX_SKMSG_H */