184659b24SMichael Zeller /*- 284659b24SMichael Zeller * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 3084659b24SMichael Zeller 3184659b24SMichael Zeller #include <sys/types.h> 3284659b24SMichael Zeller #include <net/ethernet.h> 3384659b24SMichael Zeller 34154972afSPatrick Mooney #include <assert.h> 3584659b24SMichael Zeller #include <errno.h> 36154972afSPatrick Mooney #include <limits.h> 3784659b24SMichael Zeller #include <md5.h> 3884659b24SMichael Zeller #include <stdio.h> 39154972afSPatrick Mooney #include <stdlib.h> 4084659b24SMichael Zeller #include <string.h> 4184659b24SMichael Zeller 4284659b24SMichael Zeller #include "bhyverun.h" 43*2b948146SAndy Fiddaman #include "config.h" 44154972afSPatrick Mooney #include "debug.h" 4584659b24SMichael Zeller #include "net_utils.h" 4684659b24SMichael Zeller 4784659b24SMichael Zeller int 48*2b948146SAndy Fiddaman net_parsemac(const char *mac_str, uint8_t *mac_addr) 4984659b24SMichael Zeller { 5084659b24SMichael Zeller struct ether_addr *ea; 5184659b24SMichael Zeller char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 }; 5284659b24SMichael Zeller 53154972afSPatrick Mooney if (mac_str == NULL) 54154972afSPatrick Mooney return (EINVAL); 5584659b24SMichael Zeller 5684659b24SMichael Zeller ea = ether_aton(mac_str); 5784659b24SMichael Zeller 5884659b24SMichael Zeller if (ea == NULL || ETHER_IS_MULTICAST(ea->octet) || 5984659b24SMichael Zeller memcmp(ea->octet, zero_addr, ETHER_ADDR_LEN) == 0) { 60154972afSPatrick Mooney EPRINTLN("Invalid MAC %s", mac_str); 6184659b24SMichael Zeller return (EINVAL); 6284659b24SMichael Zeller } else 6384659b24SMichael Zeller memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN); 6484659b24SMichael Zeller 6584659b24SMichael Zeller return (0); 6684659b24SMichael Zeller } 6784659b24SMichael Zeller 68154972afSPatrick Mooney int 69154972afSPatrick Mooney net_parsemtu(const char *mtu_str, unsigned long *mtu) 70154972afSPatrick Mooney { 71154972afSPatrick Mooney char *end; 72154972afSPatrick Mooney unsigned long val; 73154972afSPatrick Mooney 74154972afSPatrick Mooney assert(mtu_str != NULL); 75154972afSPatrick Mooney 76154972afSPatrick Mooney if (*mtu_str == '-') 77154972afSPatrick Mooney goto err; 78154972afSPatrick Mooney 79154972afSPatrick Mooney val = strtoul(mtu_str, &end, 0); 80154972afSPatrick Mooney 81154972afSPatrick Mooney if (*end != '\0') 82154972afSPatrick Mooney goto err; 83154972afSPatrick Mooney 84154972afSPatrick Mooney if (val == ULONG_MAX) 85154972afSPatrick Mooney return (ERANGE); 86154972afSPatrick Mooney 87154972afSPatrick Mooney if (val == 0 && errno == EINVAL) 88154972afSPatrick Mooney return (EINVAL); 89154972afSPatrick Mooney 90154972afSPatrick Mooney *mtu = val; 91154972afSPatrick Mooney 92154972afSPatrick Mooney return (0); 93154972afSPatrick Mooney 94154972afSPatrick Mooney err: 95154972afSPatrick Mooney errno = EINVAL; 96154972afSPatrick Mooney return (EINVAL); 97154972afSPatrick Mooney } 98154972afSPatrick Mooney 9984659b24SMichael Zeller void 10084659b24SMichael Zeller net_genmac(struct pci_devinst *pi, uint8_t *macaddr) 10184659b24SMichael Zeller { 10284659b24SMichael Zeller /* 10384659b24SMichael Zeller * The default MAC address is the standard NetApp OUI of 00-a0-98, 10484659b24SMichael Zeller * followed by an MD5 of the PCI slot/func number and dev name 10584659b24SMichael Zeller */ 10684659b24SMichael Zeller MD5_CTX mdctx; 10784659b24SMichael Zeller unsigned char digest[16]; 10884659b24SMichael Zeller char nstr[80]; 10984659b24SMichael Zeller 11084659b24SMichael Zeller snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot, 111*2b948146SAndy Fiddaman pi->pi_func, get_config_value("name")); 11284659b24SMichael Zeller 11384659b24SMichael Zeller MD5Init(&mdctx); 11484659b24SMichael Zeller MD5Update(&mdctx, nstr, (unsigned int)strlen(nstr)); 11584659b24SMichael Zeller MD5Final(digest, &mdctx); 11684659b24SMichael Zeller 11784659b24SMichael Zeller macaddr[0] = 0x00; 11884659b24SMichael Zeller macaddr[1] = 0xa0; 11984659b24SMichael Zeller macaddr[2] = 0x98; 12084659b24SMichael Zeller macaddr[3] = digest[0]; 12184659b24SMichael Zeller macaddr[4] = digest[1]; 12284659b24SMichael Zeller macaddr[5] = digest[2]; 12384659b24SMichael Zeller } 124