HOMECOURSESSTUDENTSDONATIONSVIDEOSEVENTSTUTORIALSLINKSNEWSCONTACT


VIDEOS 》 Linux Kernel /proc Interface

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.

/* proc_example.c
 * Author: Kiran Kankipati
 * Updated: 22-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/proc_fs.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>

struct proc_dir_entry *tlc_proc_a;

static ssize_t tlc_proc_a_read(struct file *fp, char *buf, size_t len, loff_t * off)
{	static int finished=0; if(finished) {finished=0;return 0;} finished=1;
	//int abc=100;
	//strcpy(buf, "abc: %d\n", abc);
	//sprintf(buf, "abc: %d\n", abc);
	memset(buf,0xaa,10);
	//return strlen(buf);
	return 10;
}

static struct file_operations tlc_proc_a_fops = { .owner=THIS_MODULE, .read=tlc_proc_a_read, };

static int hello_init(void)
{
 	tlc_proc_a = proc_create( "tlc_proc_a", 0444, NULL, &tlc_proc_a_fops); 
 	if(tlc_proc_a==NULL) {	printk(KERN_ALERT "Error: Could not initialize %s\n", "tlc_proc_a"); }
	
	return 0;
}

static void hello_exit(void) 
{  remove_proc_entry("tlc_proc_a", NULL);
}

module_init(hello_init);
module_exit(hello_exit);

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.

/* proc_example.c - write /proc file
 * Author: Kiran Kankipati
 * Updated: 23-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/proc_fs.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>

struct proc_dir_entry *tlc_proc_a;

int abc=100;

#define PROCFS_MAX_SIZE 30
char proc_buf[PROCFS_MAX_SIZE];

static ssize_t tlc_proc_a_write(struct file *fp, const char *buf, size_t len, loff_t * off)
{	if(len > PROCFS_MAX_SIZE) { return -EFAULT; }
   if(copy_from_user(proc_buf, buf, len)) { return -EFAULT; }

	abc=simple_strtoul(proc_buf,NULL,10);
	return len;
}


static ssize_t tlc_proc_a_read(struct file *fp, char *buf, size_t len, loff_t * off)
{	static int finished=0; if(finished) {finished=0;return 0;} finished=1;
	
	sprintf(buf, "abc: %d\n", abc);
	return strlen(buf);
}

static struct file_operations tlc_proc_a_fops = { .owner=THIS_MODULE, .read=tlc_proc_a_read, .write=tlc_proc_a_write, };

static int hello_init(void)
{
 	tlc_proc_a = proc_create( "tlc_proc_a", 0666, NULL, &tlc_proc_a_fops); 
 	if(tlc_proc_a==NULL) {	printk(KERN_ALERT "Error: Could not initialize %s\n", "tlc_proc_a"); }
	
	return 0;
}

static void hello_exit(void) 
{  remove_proc_entry("tlc_proc_a", NULL);
}

module_init(hello_init);
module_exit(hello_exit);

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.

/* proc_example.c - /proc directory
 * Author: Kiran Kankipati
 * Updated: 24-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/proc_fs.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>

struct proc_dir_entry *tlc_proc_dir;
struct proc_dir_entry *tlc_proc_a;

int abc=100;

#define PROCFS_MAX_SIZE 30
char proc_buf[PROCFS_MAX_SIZE];

static ssize_t tlc_proc_a_write(struct file *fp, const char *buf, size_t len, loff_t * off)
{	if(len > PROCFS_MAX_SIZE) { return -EFAULT; }
   if(copy_from_user(proc_buf, buf, len)) { return -EFAULT; }

	abc=simple_strtoul(proc_buf,NULL,10);
	return len;
}


static ssize_t tlc_proc_a_read(struct file *fp, char *buf, size_t len, loff_t * off)
{	static int finished=0; if(finished) {finished=0;return 0;} finished=1;
	
	sprintf(buf, "abc: %d\n", abc);
	return strlen(buf);
}

static struct file_operations tlc_proc_a_fops = { .owner=THIS_MODULE, .read=tlc_proc_a_read, .write=tlc_proc_a_write, };

static int hello_init(void)
{
	tlc_proc_dir=proc_mkdir( "tlc_proc_dir", NULL);
 	tlc_proc_a = proc_create( "tlc_proc_a", 0666, tlc_proc_dir, &tlc_proc_a_fops); 
 	if(tlc_proc_a==NULL) {	printk(KERN_ALERT "Error: Could not initialize %s\n", "tlc_proc_a"); }
	
	return 0;
}

static void hello_exit(void) 
{  remove_proc_entry("tlc_proc_a", NULL);
}

module_init(hello_init);
module_exit(hello_exit);

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.

/* proc_example.c - /proc access to drop pkts
 * Author: Kiran Kankipati
 * Updated: 25-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/proc_fs.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>

struct proc_dir_entry *tlc_proc_a;

int drop_pkts=0;

#define PROCFS_MAX_SIZE 30
char proc_buf[PROCFS_MAX_SIZE];

static ssize_t tlc_proc_a_write(struct file *fp, const char *buf, size_t len, loff_t * off)
{	int val=0;
	if(len > PROCFS_MAX_SIZE) { return -EFAULT; }
   if(copy_from_user(proc_buf, buf, len)) { return -EFAULT; }

	val=simple_strtoul(proc_buf,NULL,10); if(val==0 || val==1) { drop_pkts=val; }
	return len;
}


static ssize_t tlc_proc_a_read(struct file *fp, char *buf, size_t len, loff_t * off)
{	static int finished=0; if(finished) {finished=0;return 0;} finished=1;
	
	if(drop_pkts) { sprintf(buf, "drop_pkts: true\n"); } else { sprintf(buf, "drop_pkts: false\n"); }
	return strlen(buf);
}

static struct file_operations tlc_proc_a_fops = { .owner=THIS_MODULE, .read=tlc_proc_a_read, .write=tlc_proc_a_write, };

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)
{
	if(drop_pkts) { return NF_DROP; }
	return NF_ACCEPT;
}

static int hello_init(void)
{
 	tlc_proc_a = proc_create( "tlc_proc_a", 0666, NULL, &tlc_proc_a_fops); 
 	if(tlc_proc_a==NULL) {	printk(KERN_ALERT "Error: Could not initialize %s\n", "tlc_proc_a"); goto hello_init_end; }
 	
 	//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;
	nf_register_hook(&nfho_pre_routing);

hello_init_end:	
	return 0;
}

static void hello_exit(void) 
{  remove_proc_entry("tlc_proc_a", NULL);
	nf_unregister_hook(&nfho_pre_routing);
}

module_init(hello_init);
module_exit(hello_exit);


Download my sample C-code(with compile script) discussed in this video episode HERE.


Suggested Topics:


Video Episodes :: Linux Kernel programming

Linux Kernel Programming ↗
Wednesday' 24-Feb-2021

Linux Kernel Architecture ↗
Wednesday' 24-Feb-2021

Linux Kernel /proc Interface ↗
Wednesday' 24-Feb-2021

Linux Kernel Programming - Device Drivers ↗
Wednesday' 24-Feb-2021
Watch detailed videos and read topics on Linux Kernel Programming - Device Drivers

Linux ioctl() API interface ↗
Wednesday' 24-Feb-2021
Watch detailed videos and read topics on Linux Kernel Programming and Linux ioctl() API interface

Linux Kernel FileSystems Subsystem ↗
Wednesday' 24-Feb-2021

Linux Kernel Compilation ↗
Wednesday' 24-Feb-2021

Linux Operating System - User-space Processes ↗
Wednesday' 24-Feb-2021

Linux Kernel - Containers and Namespaces ↗
Wednesday' 24-Feb-2021

Join The Linux Channel :: Facebook Group ↗

Visit The Linux Channel :: on Youtube ↗


💗 Help shape the future: Sponsor/Donate


Recommended Topics:
Featured Video:
Watch on Youtube - 352 Linux user-space - Shared Memory IPC - Live Demo and Exa ↗

Linux Kernel Network Programming - Transport Layer L4 TCP/UDP Registration - Protocol APIs ↗
Wednesday' 24-Feb-2021

My Home-Lab Data Storage and Hard drives ↗
Wednesday' 24-Feb-2021

Ubuntu installation, setup and upgrade ↗
Wednesday' 24-Feb-2021

Oracle VM VirtualBox ↗
Wednesday' 24-Feb-2021

B.E(B.Tech, B.S) and M.E(M.Tech, M.S) Collage Final Year Projects ↗
Wednesday' 24-Feb-2021

SNMP and MIB Browsers ↗
Wednesday' 24-Feb-2021

Management ↗
Wednesday' 24-Feb-2021

Tour of my new Home Networking Lab within VirtualBox VM ↗
Wednesday' 24-Feb-2021

What is a BSP | Board Support Package | Big Picture | Embedded Systems Software Development ↗
Wednesday' 24-Feb-2021

Interactive map of Linux Kernel ↗
Wednesday' 24-Feb-2021


Trending Video:
Watch on Youtube - x224 Linux Kernel Dummy Network Interface /drivers/net/dummy... ↗

Deciding the future: Cloud/Colocation Data Center or Premise-Built Data Center? ↗
Wednesday' 24-Feb-2021
Before we go ahead expounding on picking between managed hosting and data center co-location, let us initially comprehend what each of these methods for a business endeavor to achieve. According to an article written by Nitin Mishra, According to an article written by Nitin Mishra, Former Airtel & Samsung executive. “which is perfect for your business? The appropriate response lies in the points of interest and relies upon your organisation's particular prerequisites and the workloads that you would need to keep running from both of these alternatives.” PROS/CONS: Premise-Built Data Center vs. Cloud/Colocation Data Center.



Recommended Video:
Watch on Youtube - x224 Linux Kernel Dummy Network Interface /drivers/net/dummy... ↗