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