1

I am writing a kernel module which forms its own packet at ip level and then sends the data . I just want to know how to find the ip address of a local network interface.

3
  • A network interface can (and will) receive packets from many different senders. You get the sender from the received packet. Mar 30, 2012 at 10:20
  • @JoachimPileborg I want the ip address for my interface . Is there no function of getting a ip address for my interface . I dont want to capture rx packet and take from there as I would be generating traffic
    – karan421
    Mar 30, 2012 at 10:24
  • I edited the question and title to make it clearer. Mar 30, 2012 at 10:49

3 Answers 3

1

There are defined ioctls SIOCGIFADDR (to get if address) and SIOCSIFADDR (to set if address).

All device configuration for IPv4 is done in net/ipv4/devinet.c. You can refer to this source file to get more information.

1
  • I want to use ioctl for ip address in kernel space .....how could i do it ..please tell by writing a code
    – karan421
    Mar 30, 2012 at 14:50
1

Wanting to read interface addresses reeks of a design problem. However, if you are looking for determining the preferred source address to use when contacting a remote peer is given in struct rt6_info.rt6i_prefsrc after obtaining it with ip6_route_output. Something along the lines of (with no implied guarantees):

int pick_addr(struct in6_addr *saddr, struct net *net, const struct in6_addr *daddr)
{
    struct rt6_info *rt;
    struct flowi6 fl6;
    int ret;
    memset(&fl6, 0, sizeof(fl6));
    memcpy(&fl6.daddr, daddr, sizeof(*daddr));
    dst = (struct rt6_info *)ip6_route_output(net, NULL /* or sk if you have it */, &fl6);
    ret = rt->dst.error;
    if (ret == 0)
            memcpy(saddr, &rt->rt6i_prefsrc.addr, sizeof(*saddr));

    dst_release(&rt->dst);
    return ret;
}
1
  • It's similar for IPv4 (see net/netfilter/xt_TEE.c for the skeleton). prefaddr is then in struct rtable.rt_src AFAICS.
    – jørgensen
    Mar 30, 2012 at 16:42
0

We have if_getconfig function available in linux-x.y.z/Documentation/networking/ifenslave.c file which is a very good example of how to use ioctls and fetch address from kernel space

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.