Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 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> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 12 | #include <net/net_namespace.h> |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 13 | #include "internal.h" |
| 14 | |
David Howells | ec9c948 | 2007-05-03 03:29:41 -0700 | [diff] [blame] | 15 | /* |
David Howells | ec9c948 | 2007-05-03 03:29:41 -0700 | [diff] [blame] | 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 | */ |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 20 | int 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 Howells | ec9c948 | 2007-05-03 03:29:41 -0700 | [diff] [blame] | 27 | ASSERT(maxbufs > 0); |
| 28 | |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 29 | rtnl_lock(); |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 30 | for_each_netdev(&init_net, dev) { |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 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) { |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 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++; |
David Howells | ec9c948 | 2007-05-03 03:29:41 -0700 | [diff] [blame] | 41 | if (n >= maxbufs) |
| 42 | goto out; |
| 43 | } endfor_ifa(idev); |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 44 | } |
| 45 | out: |
| 46 | rtnl_unlock(); |
| 47 | return n; |
| 48 | } |