The Linux Channel

Videos :: Linux Kernel struct iphdr data-structure

Written by: Kiran Kankipati - Published: 05-Jul-2016


ip_send_check() API Example and sample demo - Linux Kernel struct iphdr data-structure - part3

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

/* ip_send_check.c
 * Author: Kiran Kankipati
 * Updated: 15-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_post_routing;

unsigned int post_routing_hook_func(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
	struct iphdr *iph = ip_hdr(skb);

	/*------ test ip checksum --------*/
	//iph->check = 0;
	//ip_send_check(iph);
	/*--------------------------------*/
	
	return NF_ACCEPT; //this will accept the packet
}


static int hello_init(void)
{
	//Packet TX
	nfho_post_routing.hook = post_routing_hook_func;
	nfho_post_routing.hooknum = NF_INET_POST_ROUTING;
	nfho_post_routing.pf = PF_INET;
	nfho_post_routing.priority = NF_IP_PRI_FIRST;
	nf_register_hook(&nfho_post_routing);

	return 0;
}

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

module_init(hello_init);
module_exit(hello_exit);

Linux Kernel struct iphdr data-structure - part2 - Sample Example Code

Refer: Kernel Doc printk-formats.txt - http://elixir.free-electrons.com/linux/latest/source/Documentation/printk-formats.txt

Here is the screenshot of my Sample Example Code discussed in the video above:
Screenshot

Linux Kernel struct iphdr data-structure - part1 - code walk, Example

Refer:
struct iphdr data-structure - http://elixir.free-electrons.com/linux/latest/source/include/uapi/linux/ip.h#L85
sk_buff to iphdr APIs - http://elixir.free-electrons.com/linux/latest/source/include/linux/ip.h
Definitions for the IP module - http://elixir.free-electrons.com/linux/latest/source/include/net/ip.h

And here is the copy paste of struct iphdr data-structure (/include/uapi/linux/ip.h) from the Kernel-source version 4.13 for quick reference:

struct iphdr {
#if defined(__LITTLE_ENDIAN_BITFIELD)
	__u8	ihl:4,
		version:4;
#elif defined (__BIG_ENDIAN_BITFIELD)
	__u8	version:4,
  		ihl:4;
#else
#error	"Please fix "
#endif
	__u8	tos;
	__be16	tot_len;
	__be16	id;
	__be16	frag_off;
	__u8	ttl;
	__u8	protocol;
	__sum16	check;
	__be32	saddr;
	__be32	daddr;
	/*The options start here. */
};



Suggested Topics:

Linux Kernel programming
Linux Kernel FileSystems Subsystem
16-Dec-2017
Linux Kernel TCP Congestion Control Algorithms
20-Nov-2017
Linux Kernel struct socket and struct sock data-structure
31-Oct-2017
Linux Kernel /proc Interface
22-Feb-2017
Linux ioctl() API interface
11-Oct-2017
Linux Kernel Network Programming
05-Nov-2017
Linux Kernel Programming
05-Jul-2016
Linux Kernel sk_buff data-structure - Episode-11 to Episode-18
26-Mar-2017
Linux Kernel sk_buff data-structure - Episode-1 to Episode-10
05-Jul-2016
Linux Kernel net_device data-structure
05-Jul-2016
Linux Kernel dst_entry data-structure
04-May-2017
Linux Kernel struct ethhdr data-structure
02-Nov-2016
Linux Kernel struct iphdr data-structure
05-Jul-2016
Linux Kernel struct tcphdr data-structure
05-Jul-2016
Linux Kernel struct udphdr data-structure
05-Jul-2016
Linux Kernel Networking Sub-system
05-Jul-2016
Linux Kernel Compilation
05-Jul-2016
Linux Kernel Architecture - Generic
13-May-2017

 

Recommended Topics:

Join a class (course):


JOBS :: Software Engineers - Freshers - Off-Campus:


Video series :: Linux Kernel TCP Congestion Control Algorithms:


TUTORIALS :: Linux Networking Stack :: RFS: Receive Flow Steering:


Network Packet Processing Hardware Offload:



The Linux Channel - v6.26 :: Updated: 11-Dec-2017 :: © 2017 :: Author: Kiran Kankipati
Your IP: 54.226.113.250 :: Browser: CCBot/2.0 (http://commoncrawl.org/faq/)