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

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

 



Yes, I agree. As the kernel itself, you have access to structures that
describes all the network interfaces, just get them =)

You can start by looking at where the output of /proc/net/dev is
generated, and you will find out how to get the info.

Daniel

On 7/19/06, JuanJo Ciarlante <jjo@xxxxxxxxxxxxxx> wrote:
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;
   }

Regards...

--
--Juanjo

#  Juan Jose Ciarlante (JuanJo) jjo ;at; mendoza.gov.ar                     #
#  GnuPG Public Key: gpg --keyserver wwwkeys.eu.pgp.net --recv-key 66727177 #
#   Key fingerprint: 0D2F 3E5D 8B5C 729E 0560  F453 A3F7 E249 6672 7177     #

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




--
What this world needs is a good five-dollar plasma weapon.

--
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