blob: 7ad36506c256a55d54fc96dc6e3c323e82c4c230 [file] [log] [blame]
Patrick McHardydc1f6bf2007-05-03 03:28:49 -07001/* 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. Biederman881d9662007-09-17 11:56:21 -070011#include <net/net_namespace.h>
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070012#include "internal.h"
13
David Howellsec9c9482007-05-03 03:29:41 -070014/*
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 */
18int afs_get_MAC_address(u8 *mac, size_t maclen)
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070019{
20 struct net_device *dev;
21 int ret = -ENODEV;
22
Stoyan Gaydarov11ff5f62009-04-09 17:10:28 +010023 BUG_ON(maclen != ETH_ALEN);
David Howellsec9c9482007-05-03 03:29:41 -070024
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070025 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -070026 dev = __dev_getfirstbyhwtype(&init_net, ARPHRD_ETHER);
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070027 if (dev) {
David Howellsec9c9482007-05-03 03:29:41 -070028 memcpy(mac, dev->dev_addr, maclen);
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070029 ret = 0;
30 }
31 rtnl_unlock();
32 return ret;
33}
34
David Howellsec9c9482007-05-03 03:29:41 -070035/*
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 McHardydc1f6bf2007-05-03 03:28:49 -070040int 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 Howellsec9c9482007-05-03 03:29:41 -070047 ASSERT(maxbufs > 0);
48
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070049 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -070050 for_each_netdev(&init_net, dev) {
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070051 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 McHardydc1f6bf2007-05-03 03:28:49 -070057 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 Howellsec9c9482007-05-03 03:29:41 -070061 if (n >= maxbufs)
62 goto out;
63 } endfor_ifa(idev);
Patrick McHardydc1f6bf2007-05-03 03:28:49 -070064 }
65out:
66 rtnl_unlock();
67 return n;
68}