Re: Gettin own IP address thorugh ioctl in kernel space.

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

 






On Wed, Jul 19, 2006 at 03:03:24PM +0530, Chinmaya Mishra wrote:
Hi,

Can you provide an example how to invoke ioctl on
device in kernel module.

For example. I want to find out the IP address of
my eth0 and I want to make SIOCSIFADDR on it from
kernel module.

Recall that in kernel space YOU are the kernel :), so you won't be
syscall()ing, albeit some cases where it's easy to do a function call
to a syscall's _implementation_.
Usual path is to dig, grep into kernel srcs to find HOW it has been
implemented and copy&adapt code from there, net/ipv4/ is a good
place to start.

Below is a code snippet that gets iface's ipaddr using
inet_select_addr() which selects src addr from routing
table (the "src" you'll see with eg.: ip route get 1.1.1.1)

  #include <linux/module.h>
  #include <linux/inetdevice.h>
  #include <linux/rtnetlink.h>
  u32 get_default_ipaddr_by_devname(const char *devname)
  {
       u32 addr;
       struct net_device *dev;
       if (!devname) return 0;
       /* find netdev by name, increment refcnt */
       dev=dev_get_by_name(devname);
       if (!dev) return 0;
       /* get ip addr from rtable (global scope) */
       addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
       /* decrement netdev refcnt */
       dev_put(dev);
       return addr;
  }


How can we get address of bonding devices? For example, if I executed above code on my system, this is what I get:

bond0 device address is: 0x80000203 (128.0.2.3)
bond0:0 => no such device
eth0 device address is: 0x80000203 (128.0.2.3)

This is, ifconfig output:

bond0     Link encap:Ethernet  HWaddr 02:00:00:00:01:00
         inet addr:128.0.2.3  Bcast:128.0.7.255  Mask:255.255.248.0
         inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
         UP BROADCAST RUNNING MASTER MULTICAST  MTU:2500  Metric:1

bond0:0   Link encap:Ethernet  HWaddr 02:00:00:00:01:00
         inet addr:10.10.1.241  Bcast:10.10.1.255  Mask:255.255.255.0
         UP BROADCAST RUNNING MASTER MULTICAST  MTU:2500  Metric:1

eth0      Link encap:Ethernet  HWaddr 02:00:00:00:01:00
         inet addr:128.0.2.3  Bcast:128.0.7.255  Mask:255.255.248.0
         inet6 addr: fe80::ff:fe00:100/64 Scope:Link
         UP BROADCAST RUNNING SLAVE MULTICAST  MTU:2500  Metric:1

-Mohan

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at 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