Skip to contentSkip to conversation

Function to determine if IP exists on a net-device?

DISCUSSION Ben Greear 1 participant 3 messages Nov 19, 2003 – Nov 20, 2003 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Ben Greear

author
#0 lore raw

Is there a method already written that will tell me if an IP exists on a netdevice?

If not, I am quite sure the answer lies in the in_ifaddr list, but which field holds the IP address for the device?

Thanks,
Ben

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#2 lore raw
Quoted 11 lines · David S. Miller
Quoted 5 lines · On Wed, 19 Nov 2003 01:25:06 -0800 Ben Greear <[email protected]> wrote:

Is there a method already written that will tell me if an IP exists on a netdevice?

If not, I am quite sure the answer lies in the in_ifaddr list, but which field holds the IP address for the device?

Quoted 7 lines

Call getifaddrs(), walk the list finding the each and every list entry with the 'ifa_name' name matching the device you want. If there are multiple addresses attached to an interface there will be multiple in_ifaddr list entries with the 'ifa_name' of your interface.

In each of those entry, check the sockaddr pointed to by the 'ifa_addr' field.

Is there not a more direct access if I already have the netdevice in question? ie, can I get at the list by looking at dev->ip_ptr struct?

Thanks,
Ben

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#4 lore raw
Quoted 21 lines · David S. Miller
Quoted 2 lines · On Wed, 19 Nov 2003 18:05:15 -0800 Ben Greear <[email protected]> wrote:

Is there not a more direct access if I already have the netdevice in question? ie, can I get at the list by looking at dev->ip_ptr struct?

Yes, using dev->ip_ptr as a "struct in_device *in_dev" do something like this:

	struct in_ifaddr *ifa;

	read_lock(&in_dev->lock);
	for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
		if (inet_ifa_match(my_addr, ifa)) {
			/* match */
		}
	}
	read_unlock(&in_dev->lock);
Quoted 2 lines

should work...

The inet_ifa_match thing uses a mask, and was matching everything on the subnet, or something...

This seems to work though:

static int is_ip_on_dev(struct net_device* dev, __u32 ip) {
	int rv = 0;
	struct in_device* in_dev = in_dev_get(dev);
	if (in_dev) {
		struct in_ifaddr *ifa;

		read_lock(&in_dev->lock);
		for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
			if (ifa->ifa_address == ip) {
				/* match */
				rv = 1;
				break;
			}
		}
		read_unlock(&in_dev->lock);
		in_dev_put(in_dev);
	}
	return rv;
}
Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com