David Ahern | 554ae6e | 2016-12-01 08:48:08 -0800 | [diff] [blame] | 1 | #!/bin/bash |
Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
David Ahern | 554ae6e | 2016-12-01 08:48:08 -0800 | [diff] [blame] | 3 | |
Daniel T. Lee | d89af13 | 2020-11-24 09:03:05 +0000 | [diff] [blame] | 4 | BPFFS=/sys/fs/bpf |
| 5 | LINK_PIN=$BPFFS/test_cgrp2_sock2 |
| 6 | |
David Ahern | 554ae6e | 2016-12-01 08:48:08 -0800 | [diff] [blame] | 7 | function config_device { |
| 8 | ip netns add at_ns0 |
| 9 | ip link add veth0 type veth peer name veth0b |
| 10 | ip link set veth0b up |
| 11 | ip link set veth0 netns at_ns0 |
| 12 | ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0 |
| 13 | ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad |
| 14 | ip netns exec at_ns0 ip link set dev veth0 up |
| 15 | ip addr add 172.16.1.101/24 dev veth0b |
| 16 | ip addr add 2401:db00::2/64 dev veth0b nodad |
| 17 | } |
| 18 | |
| 19 | function config_cgroup { |
| 20 | rm -rf /tmp/cgroupv2 |
| 21 | mkdir -p /tmp/cgroupv2 |
| 22 | mount -t cgroup2 none /tmp/cgroupv2 |
| 23 | mkdir -p /tmp/cgroupv2/foo |
| 24 | echo $$ >> /tmp/cgroupv2/foo/cgroup.procs |
| 25 | } |
| 26 | |
Daniel T. Lee | d89af13 | 2020-11-24 09:03:05 +0000 | [diff] [blame] | 27 | function config_bpffs { |
| 28 | if mount | grep $BPFFS > /dev/null; then |
| 29 | echo "bpffs already mounted" |
| 30 | else |
| 31 | echo "bpffs not mounted. Mounting..." |
| 32 | mount -t bpf none $BPFFS |
| 33 | fi |
| 34 | } |
David Ahern | 554ae6e | 2016-12-01 08:48:08 -0800 | [diff] [blame] | 35 | |
| 36 | function attach_bpf { |
Daniel T. Lee | d89af13 | 2020-11-24 09:03:05 +0000 | [diff] [blame] | 37 | ./test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1 |
David Ahern | 554ae6e | 2016-12-01 08:48:08 -0800 | [diff] [blame] | 38 | [ $? -ne 0 ] && exit 1 |
| 39 | } |
| 40 | |
| 41 | function cleanup { |
Daniel T. Lee | d89af13 | 2020-11-24 09:03:05 +0000 | [diff] [blame] | 42 | rm -rf $LINK_PIN |
David Ahern | 554ae6e | 2016-12-01 08:48:08 -0800 | [diff] [blame] | 43 | ip link del veth0b |
| 44 | ip netns delete at_ns0 |
| 45 | umount /tmp/cgroupv2 |
| 46 | rm -rf /tmp/cgroupv2 |
| 47 | } |
| 48 | |
| 49 | cleanup 2>/dev/null |
| 50 | |
| 51 | set -e |
| 52 | config_device |
| 53 | config_cgroup |
Daniel T. Lee | d89af13 | 2020-11-24 09:03:05 +0000 | [diff] [blame] | 54 | config_bpffs |
David Ahern | 554ae6e | 2016-12-01 08:48:08 -0800 | [diff] [blame] | 55 | set +e |
| 56 | |
| 57 | # |
| 58 | # Test 1 - fail ping6 |
| 59 | # |
| 60 | attach_bpf 0 |
| 61 | ping -c1 -w1 172.16.1.100 |
| 62 | if [ $? -ne 0 ]; then |
| 63 | echo "ping failed when it should succeed" |
| 64 | cleanup |
| 65 | exit 1 |
| 66 | fi |
| 67 | |
| 68 | ping6 -c1 -w1 2401:db00::1 |
| 69 | if [ $? -eq 0 ]; then |
| 70 | echo "ping6 succeeded when it should not" |
| 71 | cleanup |
| 72 | exit 1 |
| 73 | fi |
| 74 | |
Daniel T. Lee | d89af13 | 2020-11-24 09:03:05 +0000 | [diff] [blame] | 75 | rm -rf $LINK_PIN |
| 76 | sleep 1 # Wait for link detach |
| 77 | |
David Ahern | 554ae6e | 2016-12-01 08:48:08 -0800 | [diff] [blame] | 78 | # |
| 79 | # Test 2 - fail ping |
| 80 | # |
| 81 | attach_bpf 1 |
| 82 | ping6 -c1 -w1 2401:db00::1 |
| 83 | if [ $? -ne 0 ]; then |
| 84 | echo "ping6 failed when it should succeed" |
| 85 | cleanup |
| 86 | exit 1 |
| 87 | fi |
| 88 | |
| 89 | ping -c1 -w1 172.16.1.100 |
| 90 | if [ $? -eq 0 ]; then |
| 91 | echo "ping succeeded when it should not" |
| 92 | cleanup |
| 93 | exit 1 |
| 94 | fi |
| 95 | |
| 96 | cleanup |
| 97 | echo |
| 98 | echo "*** PASS ***" |