1 // SPDX-License-Identifier: GPL-2.0
3 * This module 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*)!
9 * Copyright (c) 2018, Mahesh Bandewar <maheshb@google.com>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/printk.h>
15 #include <linux/skbuff.h>
16 #include <linux/netdevice.h>
17 #include <linux/udp.h>
18 #include <linux/ipv6.h>
23 #define HEAD_SIZE (14+40+8) /* Ether + IPv6 + UDP */
24 #define TAIL_SIZE 32 /* random tail-room */
28 static int __init
test_blackholedev_init(void)
36 skb
= alloc_skb(SKB_SIZE
, GFP_KERNEL
);
40 /* Reserve head-room for the headers */
41 skb_reserve(skb
, HEAD_SIZE
);
43 /* Add data to the skb */
44 data_len
= SKB_SIZE
- (HEAD_SIZE
+ TAIL_SIZE
);
45 memset(__skb_put(skb
, data_len
), 0xf, data_len
);
47 /* Add protocol data */
49 uh
= (struct udphdr
*)skb_push(skb
, sizeof(struct udphdr
));
50 skb_set_transport_header(skb
, 0);
51 uh
->source
= uh
->dest
= htons(UDP_PORT
);
52 uh
->len
= htons(data_len
);
55 ip6h
= (struct ipv6hdr
*)skb_push(skb
, sizeof(struct ipv6hdr
));
56 skb_set_network_header(skb
, 0);
58 ip6h
->payload_len
= data_len
+ sizeof(struct udphdr
);
59 ip6h
->nexthdr
= IPPROTO_UDP
;
60 ip6h
->saddr
= in6addr_loopback
;
61 ip6h
->daddr
= in6addr_loopback
;
63 skb_push(skb
, sizeof(struct ethhdr
));
64 skb_set_mac_header(skb
, 0);
66 skb
->protocol
= htons(ETH_P_IPV6
);
67 skb
->pkt_type
= PACKET_HOST
;
68 skb
->dev
= blackhole_netdev
;
70 /* Now attempt to send the packet */
71 ret
= dev_queue_xmit(skb
);
74 case NET_XMIT_SUCCESS
:
75 pr_warn("dev_queue_xmit() returned NET_XMIT_SUCCESS\n");
78 pr_warn("dev_queue_xmit() returned NET_XMIT_DROP\n");
81 pr_warn("dev_queue_xmit() returned NET_XMIT_CN\n");
84 pr_err("dev_queue_xmit() returned UNKNOWN(%d)\n", ret
);
90 static void __exit
test_blackholedev_exit(void)
92 pr_warn("test_blackholedev module terminating.\n");
95 module_init(test_blackholedev_init
);
96 module_exit(test_blackholedev_exit
);
98 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
99 MODULE_DESCRIPTION("module test of the blackhole_dev");
100 MODULE_LICENSE("GPL");