1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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
29 #include <sys/types.h>
30 #include <net/ethernet.h>
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <md5.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "bhyverun.h"
41 #include "config.h"
42 #include "debug.h"
43 #include "net_utils.h"
44
45 int
net_parsemac(const char * mac_str,uint8_t * mac_addr)46 net_parsemac(const char *mac_str, uint8_t *mac_addr)
47 {
48 struct ether_addr *ea;
49 char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
50
51 if (mac_str == NULL)
52 return (EINVAL);
53
54 ea = ether_aton(mac_str);
55
56 if (ea == NULL || ETHER_IS_MULTICAST(ea->octet) ||
57 memcmp(ea->octet, zero_addr, ETHER_ADDR_LEN) == 0) {
58 EPRINTLN("Invalid MAC %s", mac_str);
59 return (EINVAL);
60 } else
61 memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN);
62
63 return (0);
64 }
65
66 int
net_parsemtu(const char * mtu_str,unsigned long * mtu)67 net_parsemtu(const char *mtu_str, unsigned long *mtu)
68 {
69 char *end;
70 unsigned long val;
71
72 assert(mtu_str != NULL);
73
74 if (*mtu_str == '-')
75 goto err;
76
77 val = strtoul(mtu_str, &end, 0);
78
79 if (*end != '\0')
80 goto err;
81
82 if (val == ULONG_MAX)
83 return (ERANGE);
84
85 if (val == 0 && errno == EINVAL)
86 return (EINVAL);
87
88 *mtu = val;
89
90 return (0);
91
92 err:
93 errno = EINVAL;
94 return (EINVAL);
95 }
96
97 void
net_genmac(struct pci_devinst * pi,uint8_t * macaddr)98 net_genmac(struct pci_devinst *pi, uint8_t *macaddr)
99 {
100 /*
101 * The default MAC address is the standard NetApp OUI of 00-a0-98,
102 * followed by an MD5 of the PCI slot/func number and dev name
103 */
104 MD5_CTX mdctx;
105 unsigned char digest[16];
106 char nstr[80];
107
108 snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot,
109 pi->pi_func, get_config_value("name"));
110
111 MD5Init(&mdctx);
112 MD5Update(&mdctx, nstr, (unsigned int)strlen(nstr));
113 MD5Final(digest, &mdctx);
114
115 macaddr[0] = 0x00;
116 macaddr[1] = 0xa0;
117 macaddr[2] = 0x98;
118 macaddr[3] = digest[0];
119 macaddr[4] = digest[1];
120 macaddr[5] = digest[2];
121 }
122