netdevices.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 MAC address from a random ethernet interface that has a real one
  14. * - the buffer will normally be 6 bytes in size
  15. */
  16. int afs_get_MAC_address(u8 *mac, size_t maclen)
  17. {
  18. struct net_device *dev;
  19. int ret = -ENODEV;
  20. if (maclen != ETH_ALEN)
  21. BUG();
  22. rtnl_lock();
  23. dev = __dev_getfirstbyhwtype(&init_net, ARPHRD_ETHER);
  24. if (dev) {
  25. memcpy(mac, dev->dev_addr, maclen);
  26. ret = 0;
  27. }
  28. rtnl_unlock();
  29. return ret;
  30. }
  31. /*
  32. * get a list of this system's interface IPv4 addresses, netmasks and MTUs
  33. * - maxbufs must be at least 1
  34. * - returns the number of interface records in the buffer
  35. */
  36. int afs_get_ipv4_interfaces(struct afs_interface *bufs, size_t maxbufs,
  37. bool wantloopback)
  38. {
  39. struct net_device *dev;
  40. struct in_device *idev;
  41. int n = 0;
  42. ASSERT(maxbufs > 0);
  43. rtnl_lock();
  44. for_each_netdev(&init_net, dev) {
  45. if (dev->type == ARPHRD_LOOPBACK && !wantloopback)
  46. continue;
  47. idev = __in_dev_get_rtnl(dev);
  48. if (!idev)
  49. continue;
  50. for_primary_ifa(idev) {
  51. bufs[n].address.s_addr = ifa->ifa_address;
  52. bufs[n].netmask.s_addr = ifa->ifa_mask;
  53. bufs[n].mtu = dev->mtu;
  54. n++;
  55. if (n >= maxbufs)
  56. goto out;
  57. } endfor_ifa(idev);
  58. }
  59. out:
  60. rtnl_unlock();
  61. return n;
  62. }