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