Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 1 | /* AFS network device helpers |
| 2 | * |
| 3 | * Copyright (c) 2007 Patrick McHardy <kaber@trash.net> |
| 4 | */ |
| 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 "internal.h" |
| 12 | |
David Howells | ec9c948 | 2007-05-03 03:29:41 -0700 | [diff] [blame^] | 13 | /* |
| 14 | * get a MAC address from a random ethernet interface that has a real one |
| 15 | * - the buffer will normally be 6 bytes in size |
| 16 | */ |
| 17 | int afs_get_MAC_address(u8 *mac, size_t maclen) |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 18 | { |
| 19 | struct net_device *dev; |
| 20 | int ret = -ENODEV; |
| 21 | |
David Howells | ec9c948 | 2007-05-03 03:29:41 -0700 | [diff] [blame^] | 22 | if (maclen != ETH_ALEN) |
| 23 | BUG(); |
| 24 | |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 25 | rtnl_lock(); |
| 26 | dev = __dev_getfirstbyhwtype(ARPHRD_ETHER); |
| 27 | if (dev) { |
David Howells | ec9c948 | 2007-05-03 03:29:41 -0700 | [diff] [blame^] | 28 | memcpy(mac, dev->dev_addr, maclen); |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 29 | ret = 0; |
| 30 | } |
| 31 | rtnl_unlock(); |
| 32 | return ret; |
| 33 | } |
| 34 | |
David Howells | ec9c948 | 2007-05-03 03:29:41 -0700 | [diff] [blame^] | 35 | /* |
| 36 | * get a list of this system's interface IPv4 addresses, netmasks and MTUs |
| 37 | * - maxbufs must be at least 1 |
| 38 | * - returns the number of interface records in the buffer |
| 39 | */ |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 40 | int afs_get_ipv4_interfaces(struct afs_interface *bufs, size_t maxbufs, |
| 41 | bool wantloopback) |
| 42 | { |
| 43 | struct net_device *dev; |
| 44 | struct in_device *idev; |
| 45 | int n = 0; |
| 46 | |
David Howells | ec9c948 | 2007-05-03 03:29:41 -0700 | [diff] [blame^] | 47 | ASSERT(maxbufs > 0); |
| 48 | |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 49 | rtnl_lock(); |
| 50 | for (dev = dev_base; dev; dev = dev->next) { |
| 51 | if (dev->type == ARPHRD_LOOPBACK && !wantloopback) |
| 52 | continue; |
| 53 | idev = __in_dev_get_rtnl(dev); |
| 54 | if (!idev) |
| 55 | continue; |
| 56 | for_primary_ifa(idev) { |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 57 | bufs[n].address.s_addr = ifa->ifa_address; |
| 58 | bufs[n].netmask.s_addr = ifa->ifa_mask; |
| 59 | bufs[n].mtu = dev->mtu; |
| 60 | n++; |
David Howells | ec9c948 | 2007-05-03 03:29:41 -0700 | [diff] [blame^] | 61 | if (n >= maxbufs) |
| 62 | goto out; |
| 63 | } endfor_ifa(idev); |
Patrick McHardy | dc1f6bf | 2007-05-03 03:28:49 -0700 | [diff] [blame] | 64 | } |
| 65 | out: |
| 66 | rtnl_unlock(); |
| 67 | return n; |
| 68 | } |