Re: Notifier chains

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jul 23, 2013 at 7:21 AM, PV Juliet <pvjuliet@xxxxxxxxx> wrote:
Hi, 
 Thanks for the reply . Can you tell me how i can subscribe to notifier chains when  IP address changes in a system ? What are all the steps to make it work??

Thanks and Regards
Juliet

Hey Juliet,

Well, let me start by just reminding you of a fact:
An NIC is represented in the system by the net_device structure so that's the first clue.
What we want to do is to let the kernel notify us when a change happens over the net_device; then we'll check which net_device it is
and was the event.

Declare a variable of type notifier_block:
/* Create a notifier variable and let its notifier_call function pointer point to our handler */
struct notifier_block my_notifier = 
{
    .notifier_call = my_handler_function,
};

The handler function HAS to have the following signature:
int my_handler_function(struct notifier_block *self, unsigned long notification, void *data);

Next you want to register the notifier so that the kernel will call it when an event happens: (most likely, you should have this in the module_init code)
register_netdevice_notifier(&my_notifier);
When you're done with it (probably in the module_exit), do not forget to remove it:
unregister_netdevice_notifier(&my_notifier);

Now what's left is how you'll know which event happened, because your handler is gonna get called when so many events happen:
here's a sample code for the my_notifier that checks if the IP changed and if it did, it logs this 

int my_handler_function(struct notifier_block *self, unsigned long notification, void *data)
{
    struct net_device *dev;
    /* To see what kind of events you can check, refer to the netdevice.h file */
    if(notification != NETDEV_CHANGEADDR)
        return something;
     dev = data;
     printk("Device %s has changed its IP address\n", dev->name);
}

Let us know if you have any more questions,

Regards
Adel

P.S It's generally considered a bad practice to top-post the reply.
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux