xref: /illumos-gate/usr/src/cmd/bhyve/net_utils.c (revision 32640292339b07090f10ce34d455f98711077343)
184659b24SMichael Zeller /*-
2*32640292SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
384659b24SMichael Zeller  *
484659b24SMichael Zeller  * Copyright (c) 2011 NetApp, Inc.
584659b24SMichael Zeller  *
684659b24SMichael Zeller  * Redistribution and use in source and binary forms, with or without
784659b24SMichael Zeller  * modification, are permitted provided that the following conditions
884659b24SMichael Zeller  * are met:
984659b24SMichael Zeller  * 1. Redistributions of source code must retain the above copyright
1084659b24SMichael Zeller  *    notice, this list of conditions and the following disclaimer.
1184659b24SMichael Zeller  * 2. Redistributions in binary form must reproduce the above copyright
1284659b24SMichael Zeller  *    notice, this list of conditions and the following disclaimer in the
1384659b24SMichael Zeller  *    documentation and/or other materials provided with the distribution.
1484659b24SMichael Zeller  *
1584659b24SMichael Zeller  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
1684659b24SMichael Zeller  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1784659b24SMichael Zeller  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1884659b24SMichael Zeller  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
1984659b24SMichael Zeller  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
2084659b24SMichael Zeller  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
2184659b24SMichael Zeller  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
2284659b24SMichael Zeller  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2384659b24SMichael Zeller  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
2484659b24SMichael Zeller  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
2584659b24SMichael Zeller  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2684659b24SMichael Zeller  */
2784659b24SMichael Zeller 
2884659b24SMichael Zeller #include <sys/cdefs.h>
2984659b24SMichael Zeller 
3084659b24SMichael Zeller #include <sys/types.h>
3184659b24SMichael Zeller #include <net/ethernet.h>
3284659b24SMichael Zeller 
33154972afSPatrick Mooney #include <assert.h>
3484659b24SMichael Zeller #include <errno.h>
35154972afSPatrick Mooney #include <limits.h>
3684659b24SMichael Zeller #include <md5.h>
3784659b24SMichael Zeller #include <stdio.h>
38154972afSPatrick Mooney #include <stdlib.h>
3984659b24SMichael Zeller #include <string.h>
4084659b24SMichael Zeller 
4184659b24SMichael Zeller #include "bhyverun.h"
422b948146SAndy Fiddaman #include "config.h"
43154972afSPatrick Mooney #include "debug.h"
4484659b24SMichael Zeller #include "net_utils.h"
4584659b24SMichael Zeller 
4684659b24SMichael Zeller int
net_parsemac(const char * mac_str,uint8_t * mac_addr)472b948146SAndy Fiddaman net_parsemac(const char *mac_str, uint8_t *mac_addr)
4884659b24SMichael Zeller {
4984659b24SMichael Zeller         struct ether_addr *ea;
5084659b24SMichael Zeller         char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
5184659b24SMichael Zeller 
52154972afSPatrick Mooney 	if (mac_str == NULL)
53154972afSPatrick Mooney 		return (EINVAL);
5484659b24SMichael Zeller 
5584659b24SMichael Zeller 	ea = ether_aton(mac_str);
5684659b24SMichael Zeller 
5784659b24SMichael Zeller 	if (ea == NULL || ETHER_IS_MULTICAST(ea->octet) ||
5884659b24SMichael Zeller 	    memcmp(ea->octet, zero_addr, ETHER_ADDR_LEN) == 0) {
59154972afSPatrick Mooney 		EPRINTLN("Invalid MAC %s", mac_str);
6084659b24SMichael Zeller 		return (EINVAL);
6184659b24SMichael Zeller 	} else
6284659b24SMichael Zeller 		memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN);
6384659b24SMichael Zeller 
6484659b24SMichael Zeller         return (0);
6584659b24SMichael Zeller }
6684659b24SMichael Zeller 
67154972afSPatrick Mooney int
net_parsemtu(const char * mtu_str,unsigned long * mtu)68154972afSPatrick Mooney net_parsemtu(const char *mtu_str, unsigned long *mtu)
69154972afSPatrick Mooney {
70154972afSPatrick Mooney 	char *end;
71154972afSPatrick Mooney 	unsigned long val;
72154972afSPatrick Mooney 
73154972afSPatrick Mooney 	assert(mtu_str != NULL);
74154972afSPatrick Mooney 
75154972afSPatrick Mooney 	if (*mtu_str == '-')
76154972afSPatrick Mooney 		goto err;
77154972afSPatrick Mooney 
78154972afSPatrick Mooney 	val = strtoul(mtu_str, &end, 0);
79154972afSPatrick Mooney 
80154972afSPatrick Mooney 	if (*end != '\0')
81154972afSPatrick Mooney 		goto err;
82154972afSPatrick Mooney 
83154972afSPatrick Mooney 	if (val == ULONG_MAX)
84154972afSPatrick Mooney 		return (ERANGE);
85154972afSPatrick Mooney 
86154972afSPatrick Mooney 	if (val == 0 && errno == EINVAL)
87154972afSPatrick Mooney 		return (EINVAL);
88154972afSPatrick Mooney 
89154972afSPatrick Mooney 	*mtu = val;
90154972afSPatrick Mooney 
91154972afSPatrick Mooney 	return (0);
92154972afSPatrick Mooney 
93154972afSPatrick Mooney err:
94154972afSPatrick Mooney 	errno = EINVAL;
95154972afSPatrick Mooney 	return (EINVAL);
96154972afSPatrick Mooney }
97154972afSPatrick Mooney 
9884659b24SMichael Zeller void
net_genmac(struct pci_devinst * pi,uint8_t * macaddr)9984659b24SMichael Zeller net_genmac(struct pci_devinst *pi, uint8_t *macaddr)
10084659b24SMichael Zeller {
10184659b24SMichael Zeller 	/*
10284659b24SMichael Zeller 	 * The default MAC address is the standard NetApp OUI of 00-a0-98,
10384659b24SMichael Zeller 	 * followed by an MD5 of the PCI slot/func number and dev name
10484659b24SMichael Zeller 	 */
10584659b24SMichael Zeller 	MD5_CTX mdctx;
10684659b24SMichael Zeller 	unsigned char digest[16];
10784659b24SMichael Zeller 	char nstr[80];
10884659b24SMichael Zeller 
10984659b24SMichael Zeller 	snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot,
1102b948146SAndy Fiddaman 	    pi->pi_func, get_config_value("name"));
11184659b24SMichael Zeller 
11284659b24SMichael Zeller 	MD5Init(&mdctx);
11384659b24SMichael Zeller 	MD5Update(&mdctx, nstr, (unsigned int)strlen(nstr));
11484659b24SMichael Zeller 	MD5Final(digest, &mdctx);
11584659b24SMichael Zeller 
11684659b24SMichael Zeller 	macaddr[0] = 0x00;
11784659b24SMichael Zeller 	macaddr[1] = 0xa0;
11884659b24SMichael Zeller 	macaddr[2] = 0x98;
11984659b24SMichael Zeller 	macaddr[3] = digest[0];
12084659b24SMichael Zeller 	macaddr[4] = digest[1];
12184659b24SMichael Zeller 	macaddr[5] = digest[2];
12284659b24SMichael Zeller }
123