VIDEOS 》 Linux Kernel Network Programming

int ip_local_deliver(struct sk_buff *skb) ::
static inline bool ip_is_fragment(const struct iphdr *iph) ::
int ip_defrag(struct net *net, struct sk_buff *skb, u32 user) ::
static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) ::
static int ip_fragment( .. ) :: and
int ip_do_fragment( .. ) :: and

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; = 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

Linux Operating System - User-space Processes ↗
Friday' 10-Jul-2020

Linux ioctl() API interface ↗
Friday' 10-Jul-2020
Watch detailed videos and read topics on Linux Kernel Programming and Linux ioctl() API interface

Linux Kernel Programming - Device Drivers ↗
Thursday' 09-Jul-2020
Watch detailed videos and read topics on Linux Kernel Programming - Device Drivers

Linux Kernel /proc Interface ↗
Thursday' 09-Jul-2020

Linux Kernel - Containers and Namespaces ↗
Friday' 10-Jul-2020

Linux Kernel FileSystems Subsystem ↗
Thursday' 09-Jul-2020

Linux Kernel Compilation ↗
Thursday' 09-Jul-2020

Linux Kernel Architecture ↗
Thursday' 09-Jul-2020

Linux Kernel Programming ↗
Thursday' 09-Jul-2020

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

Tópicos recomendados:
Featured Video:
Assista no Youtube - [97//0] B.E and M.E Final Year Projects - Execute your project ↗

Roadmap - How to become Linux Kernel Developer - Device Drivers Programmer and a Systems Software Expert ↗
Friday' 10-Jul-2020
Many viewers and even sometimes my students ask me how I can become a kernel programmer or just device driver developer and so on. So I shot this video (and an add-on video) where I summarized steps and a roadmap to become a full-fledged Linux Kernel Developer.

Compiling a C Compiler with a C Compilter | Compile gcc with gcc ↗
Friday' 10-Jul-2020
The fundamental aspect of a programming language compiler is to translate code written from language to other. But most commonly compilers will compile code written in high-level human friendly language such as C, C++, Java, etc. to native CPU architecture specific (machine understandable) binary code which is nothing but sequence of CPU instructions. Hence if we see that way we should able to compile gcc Compiler source code with a gcc Compiler binary.

AT&T Archives: The UNIX Operating System ↗
Friday' 10-Jul-2020

CEO, CTO Talk ↗
Friday' 10-Jul-2020

Arduino UNO - RO Water Purifier Controller ↗
Friday' 10-Jul-2020
Here is a Youtube VLOG of my DIY RO Water Purifier Controller done via Arduino UNO. I want the Arduino UNO to control the RO pump, so that it pumps for a specific duration and stops automatically. This is done via Opto-isolated 4 Channel 5V 10A Relay Board meant for Arduino UNO, Raspberry Pi or similar SoC boards which offers GPIO pins. To this relay I have connected the RO water purifier booster pump which works at 24V DC connected via 220V AC to 24V DC power supply adaptar. I have also connected a small active 5V buzzer to notify the progress and completion as it fills the tank/canister.

Linux Kernel Network Programming - Transport Layer L4 TCP/UDP Registration - Protocol APIs ↗
Friday' 10-Jul-2020

Telnet installation and remote access ↗
Friday' 10-Jul-2020

Programming Language Performance and Overheads ↗
Friday' 10-Jul-2020
A detailed Youtube video series of various programming language performance and overheads - a big picture

Nmap Network Scanning ↗
Friday' 10-Jul-2020

CYRIL INGÉNIERIE - CoreFreq Linux CPU monitoring software ↗
Friday' 10-Jul-2020

Trending Video:
Assista no Youtube - [943//0] 193 Linux Kernel /proc Interface - create a directory in /proc file-system ↗

Linux Kernel Programming ↗
Friday' 10-Jul-2020

Recommended Video:
Assista no Youtube - [174//0] 0x9 FreeBSD Networking Sub-system | Layer2 Bridging Stack | Design, APIs and Data-structures | Ep4 ↗