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.
-
A network interface can (and will) receive packets from many different senders. You get the sender from the received packet.– Some programmer dudeMar 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– karan421Mar 30, 2012 at 10:24
-
I edited the question and title to make it clearer.– Some programmer dudeMar 30, 2012 at 10:49
3 Answers
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.
-
I want to use ioctl for ip address in kernel space .....how could i do it ..please tell by writing a code– karan421Mar 30, 2012 at 14:50
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;
}
-
It's similar for IPv4 (see net/netfilter/xt_TEE.c for the skeleton). prefaddr is then in
struct rtable.rt_srcAFAICS. Mar 30, 2012 at 16:42
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