usb_ethernet.c revision 185950
1/* $FreeBSD: head/sys/dev/usb2/ethernet/usb2_ethernet.c 185950 2008-12-11 23:17:48Z thompsa $ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. 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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <dev/usb2/core/usb2_core.h>
28#include <dev/usb2/ethernet/usb2_ethernet.h>
29
30MODULE_VERSION(usb2_ethernet, 1);
31MODULE_DEPEND(usb2_ethernet, usb2_core, 1, 1, 1);
32
33/*------------------------------------------------------------------------*
34 *	usb2_ether_get_mbuf - get a new ethernet aligned mbuf
35 *------------------------------------------------------------------------*/
36struct mbuf *
37usb2_ether_get_mbuf(void)
38{
39	struct mbuf *m;
40
41	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
42	if (m) {
43		m->m_len = m->m_pkthdr.len = MCLBYTES;
44		m_adj(m, ETHER_ALIGN);
45	}
46	return (m);
47}
48
49/*------------------------------------------------------------------------*
50 *	usb2_ether_cc - common ethernet config copy
51 *------------------------------------------------------------------------*/
52void
53usb2_ether_cc(struct ifnet *ifp, usb2_ether_mchash_t *fhash,
54    struct usb2_ether_cc *cc)
55{
56	struct ifmultiaddr *ifma;
57	uint8_t i;
58
59	if (ifp == NULL) {
60		/* Nothing to do */
61		return;
62	}
63	/* Copy interface flags */
64
65	cc->if_flags = ifp->if_flags;
66
67	/* Copy link layer address */
68
69	for (i = 0; i != ETHER_ADDR_LEN; i++) {
70		cc->if_lladdr[i] = IF_LLADDR(ifp)[i];
71	}
72
73	/* Check hash filter disable bits */
74
75	if ((ifp->if_flags & IFF_ALLMULTI) ||
76	    (ifp->if_flags & IFF_PROMISC)) {
77
78		memset(cc->if_hash, 0xFF, sizeof(cc->if_hash));
79
80	} else if (fhash) {
81
82		/* Compute hash bits for multicast filter */
83
84		IF_ADDR_LOCK(ifp);
85		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
86			if (ifma->ifma_addr->sa_family != AF_LINK) {
87				continue;
88			}
89			fhash(cc, LLADDR((struct sockaddr_dl *)
90			    (ifma->ifma_addr)));
91		}
92		IF_ADDR_UNLOCK(ifp);
93
94		/* Compute hash bits for broadcast address */
95
96		if (ifp->if_flags & IFF_BROADCAST) {
97			fhash(cc, ifp->if_broadcastaddr);
98		}
99	}
100}
101