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