Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * This module tests the blackhole_dev that is created during the
0004  * net subsystem initialization. The test this module performs is
0005  * by injecting an skb into the stack with skb->dev as the
0006  * blackhole_dev and expects kernel to behave in a sane manner
0007  * (in other words, *not crash*)!
0008  *
0009  * Copyright (c) 2018, Mahesh Bandewar <maheshb@google.com>
0010  */
0011 
0012 #include <linux/init.h>
0013 #include <linux/module.h>
0014 #include <linux/printk.h>
0015 #include <linux/skbuff.h>
0016 #include <linux/netdevice.h>
0017 #include <linux/udp.h>
0018 #include <linux/ipv6.h>
0019 
0020 #include <net/dst.h>
0021 
0022 #define SKB_SIZE  256
0023 #define HEAD_SIZE (14+40+8) /* Ether + IPv6 + UDP */
0024 #define TAIL_SIZE 32        /* random tail-room */
0025 
0026 #define UDP_PORT 1234
0027 
0028 static int __init test_blackholedev_init(void)
0029 {
0030     struct ipv6hdr *ip6h;
0031     struct sk_buff *skb;
0032     struct ethhdr *ethh;
0033     struct udphdr *uh;
0034     int data_len;
0035     int ret;
0036 
0037     skb = alloc_skb(SKB_SIZE, GFP_KERNEL);
0038     if (!skb)
0039         return -ENOMEM;
0040 
0041     /* Reserve head-room for the headers */
0042     skb_reserve(skb, HEAD_SIZE);
0043 
0044     /* Add data to the skb */
0045     data_len = SKB_SIZE - (HEAD_SIZE + TAIL_SIZE);
0046     memset(__skb_put(skb, data_len), 0xf, data_len);
0047 
0048     /* Add protocol data */
0049     /* (Transport) UDP */
0050     uh = (struct udphdr *)skb_push(skb, sizeof(struct udphdr));
0051     skb_set_transport_header(skb, 0);
0052     uh->source = uh->dest = htons(UDP_PORT);
0053     uh->len = htons(data_len);
0054     uh->check = 0;
0055     /* (Network) IPv6 */
0056     ip6h = (struct ipv6hdr *)skb_push(skb, sizeof(struct ipv6hdr));
0057     skb_set_network_header(skb, 0);
0058     ip6h->hop_limit = 32;
0059     ip6h->payload_len = data_len + sizeof(struct udphdr);
0060     ip6h->nexthdr = IPPROTO_UDP;
0061     ip6h->saddr = in6addr_loopback;
0062     ip6h->daddr = in6addr_loopback;
0063     /* Ether */
0064     ethh = (struct ethhdr *)skb_push(skb, sizeof(struct ethhdr));
0065     skb_set_mac_header(skb, 0);
0066 
0067     skb->protocol = htons(ETH_P_IPV6);
0068     skb->pkt_type = PACKET_HOST;
0069     skb->dev = blackhole_netdev;
0070 
0071     /* Now attempt to send the packet */
0072     ret = dev_queue_xmit(skb);
0073 
0074     switch (ret) {
0075     case NET_XMIT_SUCCESS:
0076         pr_warn("dev_queue_xmit() returned NET_XMIT_SUCCESS\n");
0077         break;
0078     case NET_XMIT_DROP:
0079         pr_warn("dev_queue_xmit() returned NET_XMIT_DROP\n");
0080         break;
0081     case NET_XMIT_CN:
0082         pr_warn("dev_queue_xmit() returned NET_XMIT_CN\n");
0083         break;
0084     default:
0085         pr_err("dev_queue_xmit() returned UNKNOWN(%d)\n", ret);
0086     }
0087 
0088     return 0;
0089 }
0090 
0091 static void __exit test_blackholedev_exit(void)
0092 {
0093     pr_warn("test_blackholedev module terminating.\n");
0094 }
0095 
0096 module_init(test_blackholedev_init);
0097 module_exit(test_blackholedev_exit);
0098 
0099 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
0100 MODULE_LICENSE("GPL");