netdevices.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* AFS network device helpers
  3. *
  4. * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
  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>
  11. #include <net/net_namespace.h>
  12. #include "internal.h"
  13. /*
  14. * get a list of this system's interface IPv4 addresses, netmasks and MTUs
  15. * - maxbufs must be at least 1
  16. * - returns the number of interface records in the buffer
  17. */
  18. int afs_get_ipv4_interfaces(struct afs_net *net, struct afs_interface *bufs,
  19. size_t maxbufs, bool wantloopback)
  20. {
  21. struct net_device *dev;
  22. struct in_device *idev;
  23. int n = 0;
  24. ASSERT(maxbufs > 0);
  25. rtnl_lock();
  26. for_each_netdev(net->net, dev) {
  27. if (dev->type == ARPHRD_LOOPBACK && !wantloopback)
  28. continue;
  29. idev = __in_dev_get_rtnl(dev);
  30. if (!idev)
  31. continue;
  32. for_primary_ifa(idev) {
  33. bufs[n].address.s_addr = ifa->ifa_address;
  34. bufs[n].netmask.s_addr = ifa->ifa_mask;
  35. bufs[n].mtu = dev->mtu;
  36. n++;
  37. if (n >= maxbufs)
  38. goto out;
  39. } endfor_ifa(idev);
  40. }
  41. out:
  42. rtnl_unlock();
  43. return n;
  44. }