]>
Commit | Line | Data |
---|---|---|
e9e2c950 RZ |
1 | /* |
2 | * *BSD specific code | |
3 | * | |
4 | * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF") | |
5 | * | |
6 | * FRR is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * FRR is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with FRR; see the file COPYING. If not, write to the Free | |
18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | * 02111-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #ifdef BFD_BSD | |
25 | ||
26 | #include <net/if.h> | |
27 | #include <net/if_types.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/socket.h> | |
30 | ||
31 | #include <ifaddrs.h> | |
32 | ||
33 | #include "bfd.h" | |
34 | ||
e9e2c950 RZ |
35 | /* |
36 | * Definitions. | |
37 | */ | |
e9e2c950 RZ |
38 | int ptm_bfd_fetch_ifindex(const char *ifname) |
39 | { | |
40 | return if_nametoindex(ifname); | |
41 | } | |
42 | ||
43 | void ptm_bfd_fetch_local_mac(const char *ifname, uint8_t *mac) | |
44 | { | |
45 | struct ifaddrs *ifap, *ifa; | |
46 | struct if_data *ifi; | |
47 | struct sockaddr_dl *sdl; | |
48 | size_t maclen; | |
49 | ||
50 | /* Always clean the target, zeroed macs mean failure. */ | |
51 | memset(mac, 0, ETHERNET_ADDRESS_LENGTH); | |
52 | ||
53 | if (getifaddrs(&ifap) != 0) | |
54 | return; | |
55 | ||
56 | for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { | |
57 | /* Find interface with that name. */ | |
58 | if (strcmp(ifa->ifa_name, ifname) != 0) | |
59 | continue; | |
60 | /* Skip non link addresses. We want the MAC address. */ | |
61 | if (ifa->ifa_addr->sa_family != AF_LINK) | |
62 | continue; | |
63 | ||
64 | sdl = (struct sockaddr_dl *)ifa->ifa_addr; | |
65 | ifi = (struct if_data *)ifa->ifa_data; | |
66 | /* Skip non ethernet related data. */ | |
67 | if (ifi->ifi_type != IFT_ETHER) | |
68 | continue; | |
69 | ||
70 | if (sdl->sdl_alen != ETHERNET_ADDRESS_LENGTH) | |
71 | log_warning("%s:%d mac address length %d (expected %d)", | |
72 | __func__, __LINE__, sdl->sdl_alen, | |
73 | ETHERNET_ADDRESS_LENGTH); | |
74 | ||
75 | maclen = (sdl->sdl_alen > ETHERNET_ADDRESS_LENGTH) | |
76 | ? ETHERNET_ADDRESS_LENGTH | |
77 | : sdl->sdl_alen; | |
78 | memcpy(mac, LLADDR(sdl), maclen); | |
79 | break; | |
80 | } | |
81 | ||
82 | freeifaddrs(ifap); | |
83 | } | |
84 | ||
85 | ||
86 | /* Was _fetch_portname_from_ifindex() */ | |
87 | void fetch_portname_from_ifindex(int ifindex, char *ifname, size_t ifnamelen) | |
88 | { | |
89 | char ifname_tmp[IF_NAMESIZE]; | |
90 | ||
91 | /* Set ifname to empty to signalize failures. */ | |
92 | memset(ifname, 0, ifnamelen); | |
93 | ||
94 | if (if_indextoname(ifindex, ifname_tmp) == NULL) | |
95 | return; | |
96 | ||
97 | if (strlcpy(ifname, ifname_tmp, ifnamelen) > ifnamelen) | |
98 | log_warning("%s:%d interface name truncated", __func__, | |
99 | __LINE__); | |
100 | } | |
101 | ||
e9e2c950 RZ |
102 | int bp_bind_dev(int sd, const char *dev) |
103 | { | |
104 | /* | |
105 | * *BSDs don't support `SO_BINDTODEVICE`, instead you must | |
106 | * manually specify the main address of the interface or use | |
107 | * BPF on the socket descriptor. | |
108 | */ | |
109 | return 0; | |
110 | } | |
111 | ||
e9e2c950 | 112 | #endif /* BFD_BSD */ |