Pages

2010-09-09

Linux: Catchnig Net Device Notification in the Linux Kernel

A Net Device notification, is sent when we access a network device. To catch such event we do:
#include <linux/notifier.h>
#include <asm/kdebug.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>


static struct notifier_block my_dev_notifier = 
{
  .notifier_call = my_dev_event_handler,
}; 


int my_dev_event_handler(struct notifier_block *self, unsigned long val, void *data)
{
  printk("my_dev_event: Val=%ld, Interface=%s\n", val,  ((struct net_device *) data)->name);
  return 0;
/* ... */ 
register_netdevice_notifier(&my_dev_notifier); 
In this example, Val will take 1 (when we do from the shell ifconfig eth0 up) or 0 (ifconfig eth0 down).

Name will be the name of the network device (eth0, eth1, etc...).

No comments:

Post a Comment