xref: /freebsd/usr.sbin/bhyve/net_utils.c (revision c4e127e24dc9f1322ebe7ade0991de7022010bf1)
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
18  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
19  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
20  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/types.h>
30 #include <net/ethernet.h>
31 
32 #include <errno.h>
33 #include <md5.h>
34 #include <stdio.h>
35 #include <string.h>
36 
37 #include "bhyverun.h"
38 #include "net_utils.h"
39 
40 int
41 net_parsemac(char *mac_str, uint8_t *mac_addr)
42 {
43         struct ether_addr *ea;
44         char *tmpstr;
45         char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
46 
47         tmpstr = strsep(&mac_str,"=");
48 
49         if ((mac_str != NULL) && (!strcmp(tmpstr,"mac"))) {
50                 ea = ether_aton(mac_str);
51 
52                 if (ea == NULL || ETHER_IS_MULTICAST(ea->octet) ||
53                     memcmp(ea->octet, zero_addr, ETHER_ADDR_LEN) == 0) {
54 			fprintf(stderr, "Invalid MAC %s\n", mac_str);
55                         return (EINVAL);
56                 } else
57                         memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN);
58         }
59 
60         return (0);
61 }
62 
63 void
64 net_genmac(struct pci_devinst *pi, uint8_t *macaddr)
65 {
66 	/*
67 	 * The default MAC address is the standard NetApp OUI of 00-a0-98,
68 	 * followed by an MD5 of the PCI slot/func number and dev name
69 	 */
70 	MD5_CTX mdctx;
71 	unsigned char digest[16];
72 	char nstr[80];
73 
74 	snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot,
75 	    pi->pi_func, vmname);
76 
77 	MD5Init(&mdctx);
78 	MD5Update(&mdctx, nstr, (unsigned int)strlen(nstr));
79 	MD5Final(digest, &mdctx);
80 
81 	macaddr[0] = 0x00;
82 	macaddr[1] = 0xa0;
83 	macaddr[2] = 0x98;
84 	macaddr[3] = digest[0];
85 	macaddr[4] = digest[1];
86 	macaddr[5] = digest[2];
87 }
88