VIDEOS 》 Linux Kernel Network Programming

int ip_local_deliver(struct sk_buff *skb) :: http://lxr.free-electrons.com/source/net/ipv4/ip_input.c#L245
static inline bool ip_is_fragment(const struct iphdr *iph) :: http://lxr.free-electrons.com/source/include/net/ip.h#L286
int ip_defrag(struct net *net, struct sk_buff *skb, u32 user) :: http://lxr.free-electrons.com/source/net/ipv4/ip_fragment.c#L655
static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) :: http://lxr.free-electrons.com/source/net/ipv4/ip_output.c#L287
static int ip_fragment( .. ) :: http://lxr.free-electrons.com/ident?i=ip_fragment and http://lxr.free-electrons.com/source/net/ipv4/ip_output.c#L535
int ip_do_fragment( .. ) :: http://lxr.free-electrons.com/ident?i=ip_do_fragment and http://lxr.free-electrons.com/source/include/net/ip.h#L120

Download this episode my entire kernel module sample code, make file, clean script HERE. And here is the source code for a quick reference.

New Update: 01-Jan-2019:

If you are using recent kernels such as 4.17.x or 4.18.x and so on, the above code may fail to compile. Since they replaced the nf_register_hook() and nf_unregister_hook() APIs with nf_register_net_hook() and nf_unregister_net_hook(). So in that case replace these lines with the new corresponding constructs as shown:

//nf_register_hook(&nfho_pre_routing); //compatible for old kernels
nf_register_net_hook(&init_net, &nfho_pre_routing); //compatible for new kernels

and ...

//nf_unregister_hook(&nfho_pre_routing); //compatible for old kernels
nf_unregister_net_hook(&init_net, &nfho_pre_routing); //compatible for new kernels

* which is why it is worth to note that the Linux Kernel is always evolving and constantly changing. As a kernel developer you have to be aware of these. And this is what time to time I always emphasize in many videos. Following just books or some examples wont help. You should learn kernel programming from kernel source itself. Kindly watch my video on this topic if you are more curious: 0x17d Linux Kernel Programming | Tracking changes in Kernel Source code

Here is the full code for quick reference:

/* pkt_drop.c
 * Author: Kiran Kankipati
 * Updated: 09-feb-2017
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/module.h> 
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>  
#include <linux/udp.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/inet.h>
#include <linux/inetdevice.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/if_arp.h>
#include <linux/icmp.h>
#include <linux/netlink.h>
#include <linux/mroute.h>
#include <net/checksum.h>
#include <net/inet_ecn.h>
#include <net/xfrm.h>
#include <net/route.h>
#include <net/sock.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/arp.h>
#include <net/udp.h>
#include <net/icmp.h>
#include <net/inetpeer.h>
#include <net/protocol.h>
#include <net/flow.h>
#include <asm/types.h>

static struct nf_hook_ops nfho_pre_routing;

unsigned int pre_routing_hook_func(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
	//kfree_skb(skb);  //- dont do this, this will crash your system.
	return NF_DROP; //this will drop the packet
	//return NF_ACCEPT; //this will accept the packet

static int hello_init(void)
	//Packet RX
	nfho_pre_routing.hook = pre_routing_hook_func;
	nfho_pre_routing.hooknum = NF_INET_PRE_ROUTING;
	nfho_pre_routing.pf = PF_INET;
	nfho_pre_routing.priority = NF_IP_PRI_FIRST;

	return 0;

static void hello_exit(void) { nf_unregister_hook(&nfho_pre_routing); }


Suggested Topics:

Video Episodes :: Linux Kernel programming

Join The Linux Channel :: Facebook Group ↗

Visit The Linux Channel :: on Youtube ↗

Join a course:

💎 Linux, Kernel, Networking and Device Drivers: PDF Brochure
💎 PhD or equivalent (or Post Doctoral) looking for assistance: Details
💎 ... or unlimited life-time mentorship: Details

💗 Help shape the future: Sponsor/Donate

Recommended Topics:
Featured Video:
Watch on Youtube - [1671/0] 157 Wireshark - Investigate TCP-Connections with Wireshark Flow Graph feature ↗

Trending Video:
Watch on Youtube - [89/0] 205 VLOG - Choose the right harddisk for your NAS ↗

Smart NIC Cards ↗
Monday' 01-Jun-2020
NIC Cards (Network Interface Cards) traditionally contain controller chip which takes care of the core aspects, such as packet reception, buffering (till they are read by OS device drivers), etc. But these days increasingly modern NIC cards can perform several CPU Network Offload functions such as packet/frame checksum, etc. Such hardware offload features of modern NIC cards is crucial to process packets and support packet data transfer rates of around 1Gbps, 10Gbps, even 40Gbps and so on. But beyond that such as 100Gbps and so on, at times even a high-end Xeon/ Intel/ AMD processors will struggle if need to support transfer rates of around 100Gbps or beyond (situations like Link Bonding). These are the situations we need what we call as Smart NIC Cards. A Smart NIC card implements most of the network traffic processing on the NIC itself that would necessarily be performed by the CPU (i.e Operating System) in the case of a traditional NIC card.

Recommended Video:
Watch on Youtube - [89/0] 205 VLOG - Choose the right harddisk for your NAS ↗