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