| 1 | |
| 2 | |
| 3 | * This tests the blackhole_dev that is created during the
|
| 4 | * net subsystem initialization. The test this module performs is
|
| 5 | * by injecting an skb into the stack with skb->dev as the
|
| 6 | * blackhole_dev and expects kernel to behave in a sane manner
|
| 7 | * (in other words, *not crash*)!
|
| 8 | *
|
| 9 | * Copyright (c) 2018, Mahesh Bandewar <[email protected]>
|
| 10 |
|
| 11 |
|
| 12 | #include <kunit/test.h>
|
| 13 | #include <linux/module.h>
|
| 14 | #include <linux/skbuff.h>
|
| 15 | #include <linux/netdevice.h>
|
| 16 | #include <linux/udp.h>
|
| 17 | #include <linux/ipv6.h>
|
| 18 |
|
| 19 | #include <net/dst.h>
|
| 20 |
|
| 21 | #define SKB_SIZE 256
|
| 22 | #define HEAD_SIZE (14+40+8)
|
| 23 | #define TAIL_SIZE 32
|
| 24 |
|
| 25 | #define UDP_PORT 1234
|
| 26 |
|
| 27 | static void test_blackholedev(struct kunit *test)
|
| 28 | {
|
| 29 | struct ipv6hdr *ip6h;
|
| 30 | struct sk_buff *skb;
|
| 31 | struct udphdr *uh;
|
| 32 | int data_len;
|
| 33 |
|
| 34 | skb = alloc_skb(SKB_SIZE, GFP_KERNEL);
|
| 35 | KUNIT_ASSERT_NOT_NULL(test, skb);
|
| 36 |
|
| 37 |
|
| 38 | skb_reserve(skb, HEAD_SIZE);
|
| 39 |
|
| 40 |
|
| 41 | data_len = SKB_SIZE - (HEAD_SIZE + TAIL_SIZE);
|
| 42 | memset(__skb_put(skb, data_len), 0xf, data_len);
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 | uh = (struct udphdr *)skb_push(skb, sizeof(struct udphdr));
|
| 47 | skb_set_transport_header(skb, 0);
|
| 48 | uh->source = uh->dest = htons(UDP_PORT);
|
| 49 | udp_set_len_short(uh, data_len);
|
| 50 | uh->check = 0;
|
| 51 |
|
| 52 | ip6h = (struct ipv6hdr *)skb_push(skb, sizeof(struct ipv6hdr));
|
| 53 | skb_set_network_header(skb, 0);
|
| 54 | ip6h->hop_limit = 32;
|
| 55 | ip6h->payload_len = htons(data_len + sizeof(struct udphdr));
|
| 56 | ip6h->nexthdr = IPPROTO_UDP;
|
| 57 | ip6h->saddr = in6addr_loopback;
|
| 58 | ip6h->daddr = in6addr_loopback;
|
| 59 |
|
| 60 | skb_push(skb, sizeof(struct ethhdr));
|
| 61 | skb_set_mac_header(skb, 0);
|
| 62 |
|
| 63 | skb->protocol = htons(ETH_P_IPV6);
|
| 64 | skb->pkt_type = PACKET_HOST;
|
| 65 | skb->dev = blackhole_netdev;
|
| 66 |
|
| 67 |
|
| 68 | KUNIT_EXPECT_EQ(test, dev_queue_xmit(skb), NET_XMIT_SUCCESS);
|
| 69 | }
|
| 70 |
|
| 71 | static struct kunit_case blackholedev_cases[] = {
|
| 72 | KUNIT_CASE(test_blackholedev),
|
| 73 | {},
|
| 74 | };
|
| 75 |
|
| 76 | static struct kunit_suite blackholedev_suite = {
|
| 77 | .name = "blackholedev",
|
| 78 | .test_cases = blackholedev_cases,
|
| 79 | };
|
| 80 |
|
| 81 | kunit_test_suite(blackholedev_suite);
|
| 82 |
|
| 83 | MODULE_AUTHOR("Mahesh Bandewar <[email protected]>");
|
| 84 | MODULE_DESCRIPTION("module test of the blackhole_dev");
|
| 85 | MODULE_LICENSE("GPL");
|