On 01/04/2013 10:00 AM, Roman Bogorodskiy wrote:
---
src/util/virnetdev.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index c7eeb50..6e4f7ad 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -42,6 +42,11 @@
# undef HAVE_STRUCT_IFREQ
#endif
+#if defined(__FreeBSD__)
+# include <sys/sockio.h>
+# include <net/if_dl.h>
+#endif
+
#define VIR_FROM_THIS VIR_FROM_NONE
#if defined(HAVE_STRUCT_IFREQ)
@@ -200,6 +205,51 @@ cleanup:
VIR_FORCE_CLOSE(fd);
return ret;
}
+#elif defined(__FreeBSD__)
+int virNetDevSetMAC(const char *ifname,
+ const virMacAddrPtr macaddr)
+{
+ struct ifreq ifr;
+ struct sockaddr_dl sdl;
+ uint8_t mac[19];
19 was odd to me... I found VIR_MAC_STRING_BUFLEN (which is essentially
18) using cscope. Figured it would be better to go that route - it does
make it easier to find code later on that's adjust the MAC
+ char *macstr;
+ int s;
+ int ret = -1;
+
+ if ((s = virNetDevSetupControl(ifname, &ifr)) < 0)
+ return -1;
+
+ if (VIR_ALLOC_N(macstr, VIR_MAC_STRING_BUFLEN) < 0) {
+ virReportOOMError();
+ ret = - 1;
Redundant with the initialization
+ goto cleanup;
+ }
+ virMacAddrFormat(&macaddr, macstr);
+
+ virStrncpy(ifr.ifr_name, ifname, IFNAMSIZ, sizeof(ifr.ifr_name));
Ignoring the status of the virStrncpy()
+ memset(mac, 0, sizeof(mac));
+ mac[0] = ':';
+ virStrncpy(mac + 1, macstr, strlen(macstr), sizeof(mac));
Ignoring the status of the virStrncpy()
+ sdl.sdl_len = sizeof(sdl);
+ link_addr(mac, &sdl);
+
+ bcopy(sdl.sdl_data, ifr.ifr_addr.sa_data, 6);
+ ifr.ifr_addr.sa_len = 6;
+
+ if (ioctl(s, SIOCSIFLLADDR, &ifr) < 0) {
+ virReportSystemError(errno,
+ _("Cannot set interface MAC on
'%s'"),
+ ifname);
+ goto cleanup;
+ }
+
+ ret = 0;
+cleanup:
+ VIR_FREE(macstr);
+ VIR_FORCE_CLOSE(s);
+
+ return ret;
+}
#else
int virNetDevSetMAC(const char *ifname,
const virMacAddrPtr macaddr ATTRIBUTE_UNUSED)