How to create a dynamic interface from kernel space.

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

 



Hi all,

Is there any way to create a dynamic interface from kernel space.

Is it possible to change the parameters of an interface from kernel
space? 


All these can be done by using ifconfig.  But I want to do it from
kernel space.  
I want to change the IP address from kernel space and change the IP
address.  I have written a small code to do this but i am not able to
see the effect.

The code I had written is:



#define __KERNEL__
#define MODULE
#include <linux/kernel.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/bitops.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/in.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/if_ether.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/init.h>
#include <linux/notifier.h>
#include <linux/inetdevice.h>
#include <linux/igmp.h>
#ifdef CONFIG_SYSCTL
#include <linux/sysctl.h>
#endif
#include <linux/kmod.h>

#include <net/ip.h>
#include <net/route.h>
#include <net/ip_fib.h>


int init_module()
{
	struct in_device *in_dev;
	struct in_ifaddr **ifap = NULL;
        struct in_ifaddr *ifa = NULL;
        struct net_device *dev;
			
	if((dev = __dev_get_by_name("eth0")) == NULL)
	{
		printk("<1>Cannot get info for eth0\n");
		return -1;
	}

	if((in_dev = __in_dev_get(dev))!= NULL)
	{
		for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
		{
			if ((strcmp("eth0", ifa->ifa_label) == 0))
			                 break; /* found */
	       }
	}
	if(ifa)
	{
		ifa->ifa_local = htonl(0xac100f63);
			
                ifa->ifa_prefixlen = 24;
                ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
                if ((dev->flags&IFF_BROADCAST) && ifa->ifa_prefixlen <
31)
                             ifa->ifa_broadcast =
ifa->ifa_address|~ifa->ifa_mask;
	}
	else printk("<1>Could not find the device\n");
       
	return 0;
}

void cleanup_module()
{
	printk("<1>MOd removed\n");
}


The changes are made but when I am trying to send packet its failing.

Is there any other way to change the interface parameters from kernel
space ?

TIA

Regards
bhaskar




--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[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