@@ -197,229 +197,3 @@ int BPF_KPROBE(kprobe__tcp_close, struct sock *sk, long timeout)
197197{
198198 return tcp_close__enter (sk );
199199}
200-
201- #ifdef notyet
202- /*
203- * XXX naive, only handles ROUTING and DEST, untested, ipv6 needs more work to
204- * be enabled.
205- */
206- int skb_peel_nexthdr (struct __sk_buff * skb , u8 wanted )
207- {
208- struct ipv6hdr ip6 ;
209- int off ;
210- u16 next ;
211-
212- off = 0 ;
213- if (bpf_skb_load_bytes (skb , off , & ip6 , sizeof (ip6 )))
214- return (-1 );
215- off += sizeof (ip6 );
216- next = ip6 .nexthdr ;
217-
218- for (;;) {
219- if (next == wanted )
220- return (off );
221- switch (next ) {
222- case NEXTHDR_ROUTING : /* FALLTHROUGH */
223- case NEXTHDR_DEST :
224- if (bpf_skb_load_bytes (skb , off , & next , sizeof (next )))
225- return (-1 );
226- off += ((next >> 8 ) + 1 ) * 8 ;
227- next = next & 0xff ;
228- continue ;
229- default :
230- return (-1 );
231- }
232- }
233-
234- return (-1 ); /* NOTREACHED */
235- }
236- #endif
237-
238- int skb_in_or_egress (struct __sk_buff * skb , int ingress )
239- {
240- struct udphdr udp ;
241- struct bpf_sock * sk ;
242- u32 * tgid , cap_len , zero = 0 ;
243- u64 * sk_addr ;
244- struct ebpf_dns_event * event ;
245- struct ebpf_varlen_field * field ;
246-
247- if (skb -> family != AF_INET && skb -> family != AF_INET6 )
248- goto ignore ;
249- if ((sk = skb -> sk ) == NULL )
250- goto ignore ;
251- if ((sk = bpf_sk_fullsock (sk )) == NULL )
252- goto ignore ;
253- if (sk -> protocol != IPPROTO_UDP )
254- goto ignore ;
255-
256- if (sk -> family == AF_INET ) {
257- struct iphdr ip ;
258-
259- if (bpf_skb_load_bytes (skb , 0 , & ip , sizeof (ip )))
260- goto ignore ;
261- if (ip .protocol != IPPROTO_UDP )
262- goto ignore ;
263- if (bpf_skb_load_bytes (skb , ip .ihl << 2 , & udp , sizeof (udp )))
264- goto ignore ;
265- } else {
266- goto ignore ;
267- }
268- #ifdef notyet /* ipv6 needs further work */
269- else if (sk -> family == AF_INET6 )
270- {
271- int t_off ;
272-
273- t_off = skb_peel_nexthdr (skb , NEXTHDR_UDP );
274- if (t_off == -1 )
275- goto ignore ;
276-
277- if (bpf_skb_load_bytes (skb , t_off , & udp , sizeof (udp )))
278- goto ignore ;
279- }
280- #endif
281-
282- if (bpf_ntohs (udp .dest ) != 53 && bpf_ntohs (udp .source ) != 53 )
283- goto ignore ;
284-
285- /*
286- * Needed for kernels prior to f79efcb0075a20633cbf9b47759f2c0d538f78d8
287- * bpf: Permits pointers on stack for helper calls
288- */
289- sk_addr = bpf_map_lookup_elem (& scratch64 , & zero );
290- if (sk_addr == NULL )
291- goto ignore ;
292- * sk_addr = (u64 )sk ;
293- tgid = bpf_map_lookup_elem (& sk_to_tgid , sk_addr );
294- if (tgid == NULL )
295- goto ignore ;
296-
297- cap_len = skb -> len ;
298- /*
299- * verifier will complain, even with a skb->len
300- * check at the beginning.
301- */
302- if (cap_len > MAX_DNS_PACKET )
303- cap_len = MAX_DNS_PACKET ;
304-
305- /*
306- * Yes this code is weird, but it convinces old verifiers (5.10), don't
307- * blame me, be sure to test 5.10 if you change it. The minimal packet
308- * should be iphlen + udphlen + 12(dns header size). Old verifiers
309- * (5.10) are very sensitive here and a non constant right expression
310- * (since iphlen is not constant due to options) fails. Do what we can
311- * and filter the remaining bad packets in userland, same applies to
312- * ipv6. Also be careful with `if cap_len > 0`, as clang will compile it
313- * to a JNZ, which doesn't adjust umin, causing the
314- * bpf_skb_load_bytes() down below to think cap_len can be zero.
315- */
316- if (cap_len >= (sizeof (struct iphdr ) + sizeof (udp ) + 12 )) {
317- event = get_event_buffer ();
318- if (event == NULL )
319- goto ignore ;
320-
321- event -> hdr .type = EBPF_EVENT_NETWORK_DNS_PKT ;
322- event -> hdr .ts = bpf_ktime_get_ns ();
323- event -> hdr .ts_boot = bpf_ktime_get_boot_ns_helper ();
324- event -> tgid = * tgid ;
325- event -> cap_len = cap_len ;
326- event -> orig_len = skb -> len ;
327- event -> direction = ingress ? EBPF_NETWORK_DIR_INGRESS : EBPF_NETWORK_DIR_EGRESS ;
328-
329- ebpf_vl_fields__init (& event -> vl_fields );
330- field = ebpf_vl_field__add (& event -> vl_fields , EBPF_VL_FIELD_DNS_BODY );
331- if (bpf_skb_load_bytes (skb , 0 , field -> data , cap_len ))
332- goto ignore ;
333- ebpf_vl_field__set_size (& event -> vl_fields , field , cap_len );
334-
335- ebpf_ringbuf_write (& ringbuf , event , EVENT_SIZE (event ), 0 );
336- }
337-
338- ignore :
339- return (1 );
340- }
341-
342- SEC ("cgroup_skb/egress" )
343- int skb_egress (struct __sk_buff * skb )
344- {
345- return skb_in_or_egress (skb , 0 );
346- }
347-
348- SEC ("cgroup_skb/ingress" )
349- int skb_ingress (struct __sk_buff * skb )
350- {
351- return skb_in_or_egress (skb , 1 );
352- }
353-
354- int sk_maybe_save_tgid (struct bpf_sock * sk )
355- {
356- u32 tgid , zero = 0 ;
357- u64 * sk_addr ;
358-
359- if (sk -> protocol != IPPROTO_UDP )
360- return (1 );
361-
362- tgid = bpf_get_current_pid_tgid () >> 32 ;
363-
364- /*
365- * Needed for kernels prior to f79efcb0075a20633cbf9b47759f2c0d538f78d8
366- * bpf: Permits pointers on stack for helper calls
367- */
368- sk_addr = bpf_map_lookup_elem (& scratch64 , & zero );
369- if (sk_addr == NULL )
370- return (1 );
371- * sk_addr = (u64 )sk ;
372- bpf_map_update_elem (& sk_to_tgid , sk_addr , & tgid , BPF_ANY );
373-
374- return (1 );
375- }
376-
377- /*
378- * We save tgid again in send/recv/connect as the file descriptor might have
379- * been passed to another process.
380- */
381- SEC ("cgroup/sendmsg4" )
382- int sendmsg4 (struct bpf_sock_addr * sa )
383- {
384- return sk_maybe_save_tgid (sa -> sk );
385- }
386-
387- SEC ("cgroup/recvmsg4" )
388- int recvmsg4 (struct bpf_sock_addr * sa )
389- {
390- return sk_maybe_save_tgid (sa -> sk );
391- }
392-
393- SEC ("cgroup/connect4" )
394- int connect4 (struct bpf_sock_addr * sa )
395- {
396- return sk_maybe_save_tgid (sa -> sk );
397- }
398-
399- SEC ("cgroup/sock_create" )
400- int sock_create (struct bpf_sock * sk )
401- {
402- return sk_maybe_save_tgid (sk );
403- }
404-
405- SEC ("cgroup/sock_release" )
406- int sock_release (struct bpf_sock * sk )
407- {
408- u32 zero = 0 ;
409- u64 * sk_addr ;
410-
411- if (sk -> protocol != IPPROTO_UDP )
412- return (1 );
413-
414- /*
415- * Needed for kernels prior to f79efcb0075a20633cbf9b47759f2c0d538f78d8
416- * bpf: Permits pointers on stack for helper calls
417- */
418- sk_addr = bpf_map_lookup_elem (& scratch64 , & zero );
419- if (sk_addr == NULL )
420- return (1 );
421- * sk_addr = (u64 )sk ;
422- bpf_map_delete_elem (& sk_to_tgid , sk_addr );
423-
424- return (1 );
425- }
0 commit comments