Skip to content

Commit 106cdda

Browse files
author
Sachin
committed
Re-visiting this repo
- Cleanup - Compile module against running kernel. - Few module compilation throws error. Will fix them when I get time. - Network stack drop packet example: `pkt_drop`
1 parent 108e40d commit 106cdda

File tree

16 files changed

+144
-29
lines changed

16 files changed

+144
-29
lines changed

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# gitignore file for https://github.com/psachin/kernel_modules
2+
3+
*.ko
4+
*.order
5+
*.symvers
6+
*.o
7+
*.cmd
8+
9+
*.mod.c
10+
*.o.d
11+
12+
.tmp_versions/
13+

ReadMe.org

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1-
* Basics linux kernel modules
2-
Simple Linux kernel modules
1+
* Linux kernel modules
2+
Basic Linux kernel modules examples
3+
4+
*** Example usage:
5+
6+
#+BEGIN_SRC sh
7+
# Visit module directory
8+
cd hello
9+
10+
# Compile module
11+
make
12+
13+
# Display module information
14+
modinfo hello.ko
15+
16+
# Insert module
17+
sudo insmod hello.ko
18+
19+
# Remove module
20+
sudo rmmod hello.ko
21+
#+END_SRC

firmware/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
ifneq ($(KERNELRELEASE),)
33
obj-m := m_firmware.o
44
else
5-
KDIR=/usr/src/linux-headers-$(shell uname -r)
5+
KDIR=/lib/modules/$(shell uname -r)/build
66

77
all: modules
88

hello/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Makefile for hello.c
22
obj-m := hello.o
33

4-
KDIR=/usr/src/linux-headers-$(shell uname -r)
4+
KDIR=/lib/modules/$(shell uname -r)/build
55

66
all:
77
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

hello2/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Makefile for hello2.c
22
obj-m := hello2.o
33

4-
KDIR=/usr/src/linux-headers-$(shell uname -r)
4+
KDIR=/lib/modules/$(shell uname -r)/build
55

66
all:
77
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
88

99
clean:
10-
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean # from lkmpg
11-
rm -rvf *~
10+
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean # from lkmpg
11+
rm -rvf *~

hello3/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Makefile for hello3.c
22
obj-m := hello3.o
33

4-
KDIR=/usr/src/linux-headers-$(shell uname -r)
4+
KDIR=/lib/modules/$(shell uname -r)/build
55

66
all:
77
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
88

99
clean:
1010
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean # from lkmpg
11-
rm -rvf *~
11+
rm -rvf *~

module_param2/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
ifneq ($(KERNELRELEASE),)
44
obj-m := module-param.o
55
else
6-
KDIR=/usr/src/linux-headers-$(shell uname -r)
6+
KDIR=/lib/modules/$(shell uname -r)/build
77

88
all: modules
99

multiple_file/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Makefile for multiple_f.c
22
obj-m := multiple_f.o
33

4-
KDIR=/usr/src/linux-headers-$(shell uname -r)
4+
KDIR=/lib/modules/$(shell uname -r)/build
55

66
all:
77
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

pid/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Makefile for hello.c
22
obj-m := hello_pid.o
33

4-
KDIR=/usr/src/linux-headers-$(shell uname -r)
4+
KDIR=/lib/modules/$(shell uname -r)/build
55

66
all:
77
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

pkt_drop/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Makefile for hello.c
2+
obj-m += pkt_drop.o
3+
4+
KDIR=/lib/modules/$(shell uname -r)/build
5+
6+
all:
7+
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
8+
9+
clean:
10+
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean # from lkmpg
11+
rm -rvf *~

pkt_drop/pkt_drop.c

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* pkt_drop.c
2+
* Author: Kiran Kankipati
3+
* Updated: 09-feb-2017
4+
* URL: http://the-linux-channel.the-toffee-project.org/index.php?page=38-videos-linux-kernel-programming
5+
*/
6+
#include <linux/kernel.h>
7+
#include <linux/types.h>
8+
#include <linux/module.h>
9+
#include <linux/errno.h>
10+
#include <linux/slab.h>
11+
#include <linux/netfilter.h>
12+
#include <linux/netfilter_ipv4.h>
13+
#include <linux/skbuff.h>
14+
#include <linux/udp.h>
15+
#include <linux/ip.h>
16+
#include <linux/in.h>
17+
#include <linux/string.h>
18+
#include <linux/init.h>
19+
#include <linux/net.h>
20+
#include <linux/netdevice.h>
21+
#include <linux/socket.h>
22+
#include <linux/sockios.h>
23+
#include <linux/inet.h>
24+
#include <linux/inetdevice.h>
25+
#include <linux/netdevice.h>
26+
#include <linux/etherdevice.h>
27+
#include <linux/if_arp.h>
28+
#include <linux/icmp.h>
29+
#include <linux/netlink.h>
30+
#include <linux/mroute.h>
31+
#include <net/checksum.h>
32+
#include <net/inet_ecn.h>
33+
#include <net/xfrm.h>
34+
#include <net/route.h>
35+
#include <net/sock.h>
36+
#include <net/ip.h>
37+
#include <net/tcp.h>
38+
#include <net/arp.h>
39+
#include <net/udp.h>
40+
#include <net/icmp.h>
41+
#include <net/inetpeer.h>
42+
#include <net/protocol.h>
43+
#include <net/flow.h>
44+
#include <asm/types.h>
45+
46+
static struct nf_hook_ops nfho_pre_routing;
47+
48+
unsigned int pre_routing_hook_func(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
49+
{
50+
//kfree_skb(skb); //- dont do this, this will crash your system.
51+
return NF_DROP; //this will drop the packet
52+
53+
//return NF_ACCEPT; //this will accept the packet
54+
}
55+
56+
57+
static int hello_init(void)
58+
{
59+
//Packet RX
60+
nfho_pre_routing.hook = pre_routing_hook_func;
61+
nfho_pre_routing.hooknum = NF_INET_PRE_ROUTING;
62+
nfho_pre_routing.pf = PF_INET;
63+
nfho_pre_routing.priority = NF_IP_PRI_FIRST;
64+
nf_register_hook(&nfho_pre_routing);
65+
66+
return 0;
67+
}
68+
69+
static void hello_exit(void) { nf_unregister_hook(&nfho_pre_routing); }
70+
71+
module_init(hello_init);
72+
module_exit(hello_exit);

start_cleanup/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
obj-m := final.o
33
final-objs := startup.o cleanup.o
44

5-
KDIR=/usr/src/linux-headers-$(shell uname -r)
5+
KDIR=/lib/modules/$(shell uname -r)/build
66

77
all:
88
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

stick_driver/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Makefile for stick_driver.c
22
obj-m := stick_driver.o
33

4-
KDIR=/usr/src/linux-headers-$(shell uname -r)
4+
KDIR=/lib/modules/$(shell uname -r)/build
55

66
all:
77
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

test_char_device/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Makefile for charater devices
22
obj-m += testCharDevice.o null-driver.o video-ram.o
33

4-
KDIR=/usr/src/linux-headers-$(shell uname -r)
4+
KDIR=/lib/modules/$(shell uname -r)/build
55

66
all:
77
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

usb/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Makefile for pen-driver.c
22
obj-m := pen-driver.o
33

4-
KDIR=/usr/src/linux-headers-$(shell uname -r)
4+
KDIR=/lib/modules/$(shell uname -r)/build
55

66
all:
77
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

usb/pen-driver.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
pen_driver.c
33
Original author: Anil Kumar Pugalia <email_at_sarika-pugs_dot_com>
44
@@ -11,26 +11,26 @@
1111
#include <linux/kernel.h>
1212
#include <linux/usb.h>
1313

14-
static struct usb_device *device;
15-
14+
// static struct usb_device *device;
15+
1616
static int pen_probe(struct usb_interface *interface, const struct usb_device_id *id)
1717
{
1818
struct usb_host_interface *iface_desc;
1919
struct usb_endpoint_descriptor *endpoint;
2020
int i;
2121

2222
iface_desc = interface->cur_altsetting;
23-
printk(KERN_INFO "[PD]: device i/f %d, (%04X:%04X) plugged\n",
23+
printk(KERN_INFO "[PD]: device i/f %d, (%04X:%04X) plugged\n",
2424
iface_desc->desc.bInterfaceNumber, id->idVendor, id->idProduct);
25-
printk(KERN_INFO "ID->bNumEndpoints: %02X\n",
25+
printk(KERN_INFO "ID->bNumEndpoints: %02X\n",
2626
iface_desc->desc.bNumEndpoints);
2727
printk(KERN_INFO "ID->bInterfaceClass: %02X\n",
2828
iface_desc->desc.bInterfaceClass);
2929

3030
for (i = 0; i < iface_desc->desc.bNumEndpoints; i++)
3131
{
3232
endpoint = &iface_desc->endpoint[i].desc;
33-
33+
3434
printk(KERN_INFO "ED[%d]->bEndpointAddress: 0x%02X\n",
3535
i, endpoint->bEndpointAddress);
3636
printk(KERN_INFO "ED[%d]->bmAttributes: 0x%02X\n",
@@ -41,12 +41,12 @@ static int pen_probe(struct usb_interface *interface, const struct usb_device_id
4141
// device = interface_to_usbdev(interface);
4242
return 0;
4343
}
44-
44+
4545
static void pen_disconnect(struct usb_interface *interface)
4646
{
4747
printk(KERN_INFO "[PD]: device disconnected\n");
4848
}
49-
49+
5050
static struct usb_device_id pen_table[] =
5151
{
5252
{ USB_DEVICE(0x14cd, 0x125a) }, /* Super Top */
@@ -55,30 +55,30 @@ static struct usb_device_id pen_table[] =
5555
{} /* Terminating entry */
5656
};
5757
MODULE_DEVICE_TABLE (usb, pen_table);
58-
58+
5959
static struct usb_driver pen_driver =
6060
{
6161
.name = "pen-driver",
6262
.id_table = pen_table,
6363
.probe = pen_probe,
6464
.disconnect = pen_disconnect,
6565
};
66-
66+
6767
static int __init pen_init(void)
6868
{
6969
printk(KERN_INFO "[PD]: module loaded\n");
7070
return usb_register(&pen_driver);
7171
}
72-
72+
7373
static void __exit pen_exit(void)
7474
{
7575
printk(KERN_INFO "[PD]: module unloaded\n");
7676
usb_deregister(&pen_driver);
7777
}
78-
78+
7979
module_init(pen_init);
8080
module_exit(pen_exit);
81-
81+
8282
MODULE_LICENSE("GPL");
8383
MODULE_AUTHOR("Tas Devil");
8484
MODULE_DESCRIPTION("USB Pen Registration Driver");

0 commit comments

Comments
 (0)