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 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/types.h>
32 #include <sys/libkern.h>
33
34 #include <net/ethernet.h>
35
36 #include <mips/atheros/ar71xx_macaddr.h>
37
38 /*
39 * Some boards don't have a separate MAC address for each individual
40 * device on-board, but instead need to derive them from a single MAC
41 * address stored somewhere.
42 */
43 uint8_t ar71xx_board_mac_addr[ETHER_ADDR_LEN];
44
45 /*
46 * Initialise a MAC address 'dst' from a MAC address 'src'.
47 *
48 * 'offset' is added to the low three bytes to allow for sequential
49 * MAC addresses to be derived from a single one.
50 *
51 * 'is_local' is whether this 'dst' should be made a local MAC address.
52 *
53 * Returns 0 if it was successfully initialised, -1 on error.
54 */
55 int
ar71xx_mac_addr_init(unsigned char * dst,const unsigned char * src,int offset,int is_local)56 ar71xx_mac_addr_init(unsigned char *dst, const unsigned char *src,
57 int offset, int is_local)
58 {
59 int t;
60
61 if (dst == NULL || src == NULL)
62 return (-1);
63
64 /* XXX TODO: validate 'src' is a valid MAC address */
65
66 t = (((uint32_t) src[3]) << 16)
67 + (((uint32_t) src[4]) << 8)
68 + ((uint32_t) src[5]);
69
70 /* Note: this is handles both positive and negative offsets */
71 t += offset;
72
73 dst[0] = src[0];
74 dst[1] = src[1];
75 dst[2] = src[2];
76 dst[3] = (t >> 16) & 0xff;
77 dst[4] = (t >> 8) & 0xff;
78 dst[5] = t & 0xff;
79
80 if (is_local)
81 dst[0] |= 0x02;
82
83 /* Everything's okay */
84 return (0);
85 }
86
87 /*
88 * Initialise a random MAC address for use by if_arge.c and whatever
89 * else requires it.
90 *
91 * Returns 0 on success, -1 on error.
92 */
93 int
ar71xx_mac_addr_random_init(struct ifnet * ifp,struct ether_addr * dst)94 ar71xx_mac_addr_random_init(struct ifnet *ifp, struct ether_addr *dst)
95 {
96
97 ether_gen_addr(ifp, dst);
98 return (0);
99 }
100