20556b3e921536809fc22ab4c0eb30e414f527e8
[dpdk.git] / drivers / net / pcap / pcap_osdep_freebsd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  * All rights reserved.
5  */
6
7 #include <net/if.h>
8 #include <net/if_dl.h>
9 #include <sys/sysctl.h>
10
11 #include <rte_malloc.h>
12 #include <rte_memcpy.h>
13
14 #include "pcap_osdep.h"
15
16 int
17 osdep_iface_index_get(const char *name)
18 {
19         return if_nametoindex(name);
20 }
21
22 int
23 osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)
24 {
25         struct if_msghdr *ifm;
26         struct sockaddr_dl *sdl;
27         int mib[6];
28         size_t len = 0;
29         char *buf;
30
31         mib[0] = CTL_NET;
32         mib[1] = AF_ROUTE;
33         mib[2] = 0;
34         mib[3] = AF_LINK;
35         mib[4] = NET_RT_IFLIST;
36         mib[5] = if_nametoindex(if_name);
37
38         if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
39                 return -1;
40
41         if (len == 0)
42                 return -1;
43
44         buf = rte_malloc(NULL, len, 0);
45         if (!buf)
46                 return -1;
47
48         if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
49                 rte_free(buf);
50                 return -1;
51         }
52         ifm = (struct if_msghdr *)buf;
53         sdl = (struct sockaddr_dl *)(ifm + 1);
54
55         rte_memcpy(mac->addr_bytes, LLADDR(sdl), RTE_ETHER_ADDR_LEN);
56
57         rte_free(buf);
58         return 0;
59 }