1/*- 2 * Copyright (c) 2015, Adrian Chadd <adrian@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * 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 PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD$"); 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/types.h> 34#include <sys/libkern.h> 35 36#include <net/ethernet.h> 37 38#include <mips/atheros/ar71xx_macaddr.h> 39 40/* 41 * Some boards don't have a separate MAC address for each individual 42 * device on-board, but instead need to derive them from a single MAC 43 * address stored somewhere. 44 */ 45uint8_t ar71xx_board_mac_addr[ETHER_ADDR_LEN]; 46 47/* 48 * Initialise a MAC address 'dst' from a MAC address 'src'. 49 * 50 * 'offset' is added to the low three bytes to allow for sequential 51 * MAC addresses to be derived from a single one. 52 * 53 * 'is_local' is whether this 'dst' should be made a local MAC address. 54 * 55 * Returns 0 if it was successfully initialised, -1 on error. 56 */ 57int 58ar71xx_mac_addr_init(unsigned char *dst, const unsigned char *src, 59 int offset, int is_local) 60{ 61 int t; 62 63 if (dst == NULL || src == NULL) 64 return (-1); 65 66 /* XXX TODO: validate 'src' is a valid MAC address */ 67 68 t = (((uint32_t) src[3]) << 16) 69 + (((uint32_t) src[4]) << 8) 70 + ((uint32_t) src[5]); 71 72 /* Note: this is handles both positive and negative offsets */ 73 t += offset; 74 75 dst[0] = src[0]; 76 dst[1] = src[1]; 77 dst[2] = src[2]; 78 dst[3] = (t >> 16) & 0xff; 79 dst[4] = (t >> 8) & 0xff; 80 dst[5] = t & 0xff; 81 82 if (is_local) 83 dst[0] |= 0x02; 84 85 /* Everything's okay */ 86 return (0); 87} 88 89/* 90 * Initialise a random MAC address for use by if_arge.c and whatever 91 * else requires it. 92 * 93 * Returns 0 on success, -1 on error. 94 */ 95int 96ar71xx_mac_addr_random_init(unsigned char *dst) 97{ 98 uint32_t rnd; 99 100 rnd = arc4random(); 101 102 dst[0] = 'b'; 103 dst[1] = 's'; 104 dst[2] = 'd'; 105 dst[3] = (rnd >> 24) & 0xff; 106 dst[4] = (rnd >> 16) & 0xff; 107 dst[5] = (rnd >> 8) & 0xff; 108 109 return (0); 110} 111