blob: 50bd5bb1c4fb1b4b91c25980f5f1452509c540e6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Patrick McHardydc1f6bf2007-05-03 03:28:49 -07002/* AFS network device helpers
3 *
4 * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
5 */
6
7#include <linux/string.h>
8#include <linux/rtnetlink.h>
9#include <linux/inetdevice.h>
10#include <linux/netdevice.h>
11#include <linux/if_arp.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070012#include <net/net_namespace.h>
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070013#include "internal.h"
14
David Howellsec9c9482007-05-03 03:29:41 -070015/*
David Howellsec9c9482007-05-03 03:29:41 -070016 * get a list of this system's interface IPv4 addresses, netmasks and MTUs
17 * - maxbufs must be at least 1
18 * - returns the number of interface records in the buffer
19 */
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070020int afs_get_ipv4_interfaces(struct afs_interface *bufs, size_t maxbufs,
21 bool wantloopback)
22{
23 struct net_device *dev;
24 struct in_device *idev;
25 int n = 0;
26
David Howellsec9c9482007-05-03 03:29:41 -070027 ASSERT(maxbufs > 0);
28
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070029 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -070030 for_each_netdev(&init_net, dev) {
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070031 if (dev->type == ARPHRD_LOOPBACK && !wantloopback)
32 continue;
33 idev = __in_dev_get_rtnl(dev);
34 if (!idev)
35 continue;
36 for_primary_ifa(idev) {
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070037 bufs[n].address.s_addr = ifa->ifa_address;
38 bufs[n].netmask.s_addr = ifa->ifa_mask;
39 bufs[n].mtu = dev->mtu;
40 n++;
David Howellsec9c9482007-05-03 03:29:41 -070041 if (n >= maxbufs)
42 goto out;
43 } endfor_ifa(idev);
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070044 }
45out:
46 rtnl_unlock();
47 return n;
48}