netdevices.c 1.0 KB

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