2023-01-15 10:16:12 +03:00
// SPDX-License-Identifier: GPL-2.0
# include "vmlinux.h"
2023-01-15 10:16:11 +03:00
# include "net_shared.h"
2020-01-20 16:06:49 +03:00
# include <bpf/bpf_helpers.h>
2016-12-01 19:48:08 +03:00
samples/bpf: fix broken cgroup socket testing
Currently, executing test_cgrp2_sock2 fails due to wrong section
header. This 'cgroup/sock1' style section is previously used at
'samples/bpf_load' (deprecated) BPF loader. Because this style isn't
supported in libbpf, this commit fixes this problem by correcting the
section header.
$ sudo ./test_cgrp2_sock2.sh
libbpf: prog 'bpf_prog1': missing BPF prog type, check ELF section name 'cgroup/sock1'
libbpf: prog 'bpf_prog1': failed to load: -22
libbpf: failed to load object './sock_flags_kern.o'
ERROR: loading BPF object file failed
In addition, this BPF program filters ping packets by comparing whether
the socket type uses SOCK_RAW. However, after the ICMP socket[1] was
developed, ping sends ICMP packets using SOCK_DGRAM. Therefore, in this
commit, the packet filtering is changed to use SOCK_DGRAM instead of
SOCK_RAW.
$ strace --trace socket ping -6 -c1 -w1 ::1
socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6) = 3
[1]: https://lwn.net/Articles/422330/
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Link: https://lore.kernel.org/r/20230115071613.125791-5-danieltimlee@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-15 10:16:07 +03:00
SEC ( " cgroup/sock " )
2016-12-01 19:48:08 +03:00
int bpf_prog1 ( struct bpf_sock * sk )
{
char fmt [ ] = " socket: family %d type %d protocol %d \n " ;
2017-09-01 01:05:50 +03:00
char fmt2 [ ] = " socket: uid %u gid %u \n " ;
__u64 gid_uid = bpf_get_current_uid_gid ( ) ;
__u32 uid = gid_uid & 0xffffffff ;
__u32 gid = gid_uid > > 32 ;
2016-12-01 19:48:08 +03:00
bpf_trace_printk ( fmt , sizeof ( fmt ) , sk - > family , sk - > type , sk - > protocol ) ;
2017-09-01 01:05:50 +03:00
bpf_trace_printk ( fmt2 , sizeof ( fmt2 ) , uid , gid ) ;
2016-12-01 19:48:08 +03:00
2023-01-15 10:16:11 +03:00
/* block AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6 sockets
2016-12-01 19:48:08 +03:00
* ie . , make ping6 fail
*/
2023-01-15 10:16:11 +03:00
if ( sk - > family = = AF_INET6 & &
samples/bpf: fix broken cgroup socket testing
Currently, executing test_cgrp2_sock2 fails due to wrong section
header. This 'cgroup/sock1' style section is previously used at
'samples/bpf_load' (deprecated) BPF loader. Because this style isn't
supported in libbpf, this commit fixes this problem by correcting the
section header.
$ sudo ./test_cgrp2_sock2.sh
libbpf: prog 'bpf_prog1': missing BPF prog type, check ELF section name 'cgroup/sock1'
libbpf: prog 'bpf_prog1': failed to load: -22
libbpf: failed to load object './sock_flags_kern.o'
ERROR: loading BPF object file failed
In addition, this BPF program filters ping packets by comparing whether
the socket type uses SOCK_RAW. However, after the ICMP socket[1] was
developed, ping sends ICMP packets using SOCK_DGRAM. Therefore, in this
commit, the packet filtering is changed to use SOCK_DGRAM instead of
SOCK_RAW.
$ strace --trace socket ping -6 -c1 -w1 ::1
socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6) = 3
[1]: https://lwn.net/Articles/422330/
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Link: https://lore.kernel.org/r/20230115071613.125791-5-danieltimlee@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-15 10:16:07 +03:00
sk - > type = = SOCK_DGRAM & &
2016-12-01 19:48:08 +03:00
sk - > protocol = = IPPROTO_ICMPV6 )
return 0 ;
return 1 ;
}
samples/bpf: fix broken cgroup socket testing
Currently, executing test_cgrp2_sock2 fails due to wrong section
header. This 'cgroup/sock1' style section is previously used at
'samples/bpf_load' (deprecated) BPF loader. Because this style isn't
supported in libbpf, this commit fixes this problem by correcting the
section header.
$ sudo ./test_cgrp2_sock2.sh
libbpf: prog 'bpf_prog1': missing BPF prog type, check ELF section name 'cgroup/sock1'
libbpf: prog 'bpf_prog1': failed to load: -22
libbpf: failed to load object './sock_flags_kern.o'
ERROR: loading BPF object file failed
In addition, this BPF program filters ping packets by comparing whether
the socket type uses SOCK_RAW. However, after the ICMP socket[1] was
developed, ping sends ICMP packets using SOCK_DGRAM. Therefore, in this
commit, the packet filtering is changed to use SOCK_DGRAM instead of
SOCK_RAW.
$ strace --trace socket ping -6 -c1 -w1 ::1
socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6) = 3
[1]: https://lwn.net/Articles/422330/
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Link: https://lore.kernel.org/r/20230115071613.125791-5-danieltimlee@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-15 10:16:07 +03:00
SEC ( " cgroup/sock " )
2016-12-01 19:48:08 +03:00
int bpf_prog2 ( struct bpf_sock * sk )
{
char fmt [ ] = " socket: family %d type %d protocol %d \n " ;
bpf_trace_printk ( fmt , sizeof ( fmt ) , sk - > family , sk - > type , sk - > protocol ) ;
2023-01-15 10:16:11 +03:00
/* block AF_INET, SOCK_DGRAM, IPPROTO_ICMP sockets
2016-12-01 19:48:08 +03:00
* ie . , make ping fail
*/
2023-01-15 10:16:11 +03:00
if ( sk - > family = = AF_INET & &
samples/bpf: fix broken cgroup socket testing
Currently, executing test_cgrp2_sock2 fails due to wrong section
header. This 'cgroup/sock1' style section is previously used at
'samples/bpf_load' (deprecated) BPF loader. Because this style isn't
supported in libbpf, this commit fixes this problem by correcting the
section header.
$ sudo ./test_cgrp2_sock2.sh
libbpf: prog 'bpf_prog1': missing BPF prog type, check ELF section name 'cgroup/sock1'
libbpf: prog 'bpf_prog1': failed to load: -22
libbpf: failed to load object './sock_flags_kern.o'
ERROR: loading BPF object file failed
In addition, this BPF program filters ping packets by comparing whether
the socket type uses SOCK_RAW. However, after the ICMP socket[1] was
developed, ping sends ICMP packets using SOCK_DGRAM. Therefore, in this
commit, the packet filtering is changed to use SOCK_DGRAM instead of
SOCK_RAW.
$ strace --trace socket ping -6 -c1 -w1 ::1
socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6) = 3
[1]: https://lwn.net/Articles/422330/
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Link: https://lore.kernel.org/r/20230115071613.125791-5-danieltimlee@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-01-15 10:16:07 +03:00
sk - > type = = SOCK_DGRAM & &
2016-12-01 19:48:08 +03:00
sk - > protocol = = IPPROTO_ICMP )
return 0 ;
return 1 ;
}
char _license [ ] SEC ( " license " ) = " GPL " ;