blob: 40b2bab3e401445492f91e11661617bda1328f37 [file] [log] [blame]
Patrick McHardydc1f6bf2007-05-03 03:28:49 -07001/* AFS network device helpers
2 *
3 * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
4 */
5
6#include <linux/string.h>
7#include <linux/rtnetlink.h>
8#include <linux/inetdevice.h>
9#include <linux/netdevice.h>
10#include <linux/if_arp.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070011#include <net/net_namespace.h>
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070012#include "internal.h"
13
David Howellsec9c9482007-05-03 03:29:41 -070014/*
David Howellsec9c9482007-05-03 03:29:41 -070015 * get a list of this system's interface IPv4 addresses, netmasks and MTUs
16 * - maxbufs must be at least 1
17 * - returns the number of interface records in the buffer
18 */
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070019int afs_get_ipv4_interfaces(struct afs_interface *bufs, size_t maxbufs,
20 bool wantloopback)
21{
22 struct net_device *dev;
23 struct in_device *idev;
24 int n = 0;
25
David Howellsec9c9482007-05-03 03:29:41 -070026 ASSERT(maxbufs > 0);
27
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070028 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -070029 for_each_netdev(&init_net, dev) {
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070030 if (dev->type == ARPHRD_LOOPBACK && !wantloopback)
31 continue;
32 idev = __in_dev_get_rtnl(dev);
33 if (!idev)
34 continue;
35 for_primary_ifa(idev) {
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070036 bufs[n].address.s_addr = ifa->ifa_address;
37 bufs[n].netmask.s_addr = ifa->ifa_mask;
38 bufs[n].mtu = dev->mtu;
39 n++;
David Howellsec9c9482007-05-03 03:29:41 -070040 if (n >= maxbufs)
41 goto out;
42 } endfor_ifa(idev);
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070043 }
44out:
45 rtnl_unlock();
46 return n;
47}