blob: 31df0d9fa5361160b06dd2cbfa3131d8022aa5d0 [file] [log] [blame]
Daniel Borkmann604326b2018-10-13 02:45:58 +02001/* 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];
John Fastabendd3b18ad32018-10-13 02:46:01 +020032 /* 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];
Daniel Borkmann604326b2018-10-13 02:45:58 +020037};
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);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200109int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
110 u32 off, u32 len);
Daniel Borkmann604326b2018-10-13 02:45:58 +0200111void 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);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200119void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes);
Daniel Borkmann604326b2018-10-13 02:45:58 +0200120
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{
John Fastabendd3b18ad32018-10-13 02:46:01 +0200169 BUILD_BUG_ON(ARRAY_SIZE(msg->sg.data) - 1 != MAX_MSG_FRAGS);
Daniel Borkmann604326b2018-10-13 02:45:58 +0200170 memset(msg, 0, sizeof(*msg));
John Fastabendd3b18ad32018-10-13 02:46:01 +0200171 sg_init_marker(msg->sg.data, MAX_MSG_FRAGS);
Daniel Borkmann604326b2018-10-13 02:45:58 +0200172}
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;
John Fastabend3f4c3122018-10-16 10:36:01 -0700179 dst->sg.size += size;
Daniel Borkmann604326b2018-10-13 02:45:58 +0200180 src->sg.data[which].length -= size;
181 src->sg.data[which].offset += size;
182}
183
John Fastabendd3b18ad32018-10-13 02:46:01 +0200184static 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
Daniel Borkmann604326b2018-10-13 02:45:58 +0200190static inline u32 sk_msg_elem_used(const struct sk_msg *msg)
191{
192 return msg->sg.end >= msg->sg.start ?
193 msg->sg.end - msg->sg.start :
194 msg->sg.end + (MAX_MSG_FRAGS - msg->sg.start);
195}
196
197static inline bool sk_msg_full(const struct sk_msg *msg)
198{
199 return (msg->sg.end == msg->sg.start) && msg->sg.size;
200}
201
202static inline struct scatterlist *sk_msg_elem(struct sk_msg *msg, int which)
203{
204 return &msg->sg.data[which];
205}
206
207static inline struct page *sk_msg_page(struct sk_msg *msg, int which)
208{
209 return sg_page(sk_msg_elem(msg, which));
210}
211
212static inline bool sk_msg_to_ingress(const struct sk_msg *msg)
213{
214 return msg->flags & BPF_F_INGRESS;
215}
216
217static inline void sk_msg_compute_data_pointers(struct sk_msg *msg)
218{
219 struct scatterlist *sge = sk_msg_elem(msg, msg->sg.start);
220
221 if (msg->sg.copy[msg->sg.start]) {
222 msg->data = NULL;
223 msg->data_end = NULL;
224 } else {
225 msg->data = sg_virt(sge);
226 msg->data_end = msg->data + sge->length;
227 }
228}
229
230static inline void sk_msg_page_add(struct sk_msg *msg, struct page *page,
231 u32 len, u32 offset)
232{
233 struct scatterlist *sge;
234
235 get_page(page);
236 sge = sk_msg_elem(msg, msg->sg.end);
237 sg_set_page(sge, page, len, offset);
238 sg_unmark_end(sge);
239
240 msg->sg.copy[msg->sg.end] = true;
241 msg->sg.size += len;
242 sk_msg_iter_next(msg, end);
243}
244
John Fastabendd3b18ad32018-10-13 02:46:01 +0200245static inline void sk_msg_sg_copy(struct sk_msg *msg, u32 i, bool copy_state)
246{
247 do {
248 msg->sg.copy[i] = copy_state;
249 sk_msg_iter_var_next(i);
250 if (i == msg->sg.end)
251 break;
252 } while (1);
253}
254
255static inline void sk_msg_sg_copy_set(struct sk_msg *msg, u32 start)
256{
257 sk_msg_sg_copy(msg, start, true);
258}
259
260static inline void sk_msg_sg_copy_clear(struct sk_msg *msg, u32 start)
261{
262 sk_msg_sg_copy(msg, start, false);
263}
264
Daniel Borkmann604326b2018-10-13 02:45:58 +0200265static inline struct sk_psock *sk_psock(const struct sock *sk)
266{
267 return rcu_dereference_sk_user_data(sk);
268}
269
270static inline bool sk_has_psock(struct sock *sk)
271{
272 return sk_psock(sk) != NULL && sk->sk_prot->recvmsg == tcp_bpf_recvmsg;
273}
274
275static inline void sk_psock_queue_msg(struct sk_psock *psock,
276 struct sk_msg *msg)
277{
278 list_add_tail(&msg->list, &psock->ingress_msg);
279}
280
John Fastabendd3b18ad32018-10-13 02:46:01 +0200281static inline bool sk_psock_queue_empty(const struct sk_psock *psock)
282{
283 return psock ? list_empty(&psock->ingress_msg) : true;
284}
285
Daniel Borkmann604326b2018-10-13 02:45:58 +0200286static inline void sk_psock_report_error(struct sk_psock *psock, int err)
287{
288 struct sock *sk = psock->sk;
289
290 sk->sk_err = err;
291 sk->sk_error_report(sk);
292}
293
294struct sk_psock *sk_psock_init(struct sock *sk, int node);
295
296int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock);
297void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock);
298void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock);
299
300int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
301 struct sk_msg *msg);
302
303static inline struct sk_psock_link *sk_psock_init_link(void)
304{
305 return kzalloc(sizeof(struct sk_psock_link),
306 GFP_ATOMIC | __GFP_NOWARN);
307}
308
309static inline void sk_psock_free_link(struct sk_psock_link *link)
310{
311 kfree(link);
312}
313
314struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock);
315#if defined(CONFIG_BPF_STREAM_PARSER)
316void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link);
317#else
318static inline void sk_psock_unlink(struct sock *sk,
319 struct sk_psock_link *link)
320{
321}
322#endif
323
324void __sk_psock_purge_ingress_msg(struct sk_psock *psock);
325
326static inline void sk_psock_cork_free(struct sk_psock *psock)
327{
328 if (psock->cork) {
329 sk_msg_free(psock->sk, psock->cork);
330 kfree(psock->cork);
331 psock->cork = NULL;
332 }
333}
334
335static inline void sk_psock_update_proto(struct sock *sk,
336 struct sk_psock *psock,
337 struct proto *ops)
338{
339 psock->saved_unhash = sk->sk_prot->unhash;
340 psock->saved_close = sk->sk_prot->close;
341 psock->saved_write_space = sk->sk_write_space;
342
343 psock->sk_proto = sk->sk_prot;
344 sk->sk_prot = ops;
345}
346
347static inline void sk_psock_restore_proto(struct sock *sk,
348 struct sk_psock *psock)
349{
350 if (psock->sk_proto) {
351 sk->sk_prot = psock->sk_proto;
352 psock->sk_proto = NULL;
353 }
354}
355
356static inline void sk_psock_set_state(struct sk_psock *psock,
357 enum sk_psock_state_bits bit)
358{
359 set_bit(bit, &psock->state);
360}
361
362static inline void sk_psock_clear_state(struct sk_psock *psock,
363 enum sk_psock_state_bits bit)
364{
365 clear_bit(bit, &psock->state);
366}
367
368static inline bool sk_psock_test_state(const struct sk_psock *psock,
369 enum sk_psock_state_bits bit)
370{
371 return test_bit(bit, &psock->state);
372}
373
374static inline struct sk_psock *sk_psock_get(struct sock *sk)
375{
376 struct sk_psock *psock;
377
378 rcu_read_lock();
379 psock = sk_psock(sk);
380 if (psock && !refcount_inc_not_zero(&psock->refcnt))
381 psock = NULL;
382 rcu_read_unlock();
383 return psock;
384}
385
386void sk_psock_stop(struct sock *sk, struct sk_psock *psock);
387void sk_psock_destroy(struct rcu_head *rcu);
388void sk_psock_drop(struct sock *sk, struct sk_psock *psock);
389
390static inline void sk_psock_put(struct sock *sk, struct sk_psock *psock)
391{
392 if (refcount_dec_and_test(&psock->refcnt))
393 sk_psock_drop(sk, psock);
394}
395
396static inline void psock_set_prog(struct bpf_prog **pprog,
397 struct bpf_prog *prog)
398{
399 prog = xchg(pprog, prog);
400 if (prog)
401 bpf_prog_put(prog);
402}
403
404static inline void psock_progs_drop(struct sk_psock_progs *progs)
405{
406 psock_set_prog(&progs->msg_parser, NULL);
407 psock_set_prog(&progs->skb_parser, NULL);
408 psock_set_prog(&progs->skb_verdict, NULL);
409}
410
411#endif /* _LINUX_SKMSG_H */