]>
git.proxmox.com Git - mirror_frr.git/blob - bfdd/bsd.c
4 * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF")
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
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.
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
27 #include <net/if_types.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
38 int ptm_bfd_fetch_ifindex(const char *ifname
)
40 return if_nametoindex(ifname
);
43 void ptm_bfd_fetch_local_mac(const char *ifname
, uint8_t *mac
)
45 struct ifaddrs
*ifap
, *ifa
;
47 struct sockaddr_dl
*sdl
;
50 /* Always clean the target, zeroed macs mean failure. */
51 memset(mac
, 0, ETHERNET_ADDRESS_LENGTH
);
53 if (getifaddrs(&ifap
) != 0)
56 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
57 /* Find interface with that name. */
58 if (strcmp(ifa
->ifa_name
, ifname
) != 0)
60 /* Skip non link addresses. We want the MAC address. */
61 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
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
)
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
);
75 maclen
= (sdl
->sdl_alen
> ETHERNET_ADDRESS_LENGTH
)
76 ? ETHERNET_ADDRESS_LENGTH
78 memcpy(mac
, LLADDR(sdl
), maclen
);
86 /* Was _fetch_portname_from_ifindex() */
87 void fetch_portname_from_ifindex(int ifindex
, char *ifname
, size_t ifnamelen
)
89 char ifname_tmp
[IF_NAMESIZE
];
91 /* Set ifname to empty to signalize failures. */
92 memset(ifname
, 0, ifnamelen
);
94 if (if_indextoname(ifindex
, ifname_tmp
) == NULL
)
97 if (strlcpy(ifname
, ifname_tmp
, ifnamelen
) > ifnamelen
)
98 log_warning("%s:%d interface name truncated", __func__
,
102 int bp_bind_dev(int sd
, const char *dev
)
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.