1 // SPDX-License-Identifier: GPL-2.0
2 /* 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>
12 #include <net/net_namespace.h>
13 #include "internal.h"
14 
15 /*
16  * 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  */
afs_get_ipv4_interfaces(struct afs_net * net,struct afs_interface * bufs,size_t maxbufs,bool wantloopback)20 int afs_get_ipv4_interfaces(struct afs_net *net, struct afs_interface *bufs,
21 			    size_t maxbufs, bool wantloopback)
22 {
23 	struct net_device *dev;
24 	struct in_device *idev;
25 	int n = 0;
26 
27 	ASSERT(maxbufs > 0);
28 
29 	rtnl_lock();
30 	for_each_netdev(net->net, dev) {
31 		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) {
37 			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++;
41 			if (n >= maxbufs)
42 				goto out;
43 		} endfor_ifa(idev);
44 	}
45 out:
46 	rtnl_unlock();
47 	return n;
48 }
49