xref: /illumos-gate/usr/src/uts/common/io/bpf/bpf_dlt.c (revision 46b592853d0f4f11781b6b0a7533f267c6aee132)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/dlpi.h>
29 #include <net/if.h>
30 #include <net/dlt.h>
31 
32 /*
33  * This table provides a mapping of the DLPI data link types used in
34  * Solaris to the BPF data link types. Providing this translation in
35  * the kernel allows libpcap to be downloaded and used without any
36  * need for change.
37  *
38  * Note that this table is not necessarily sorted.
39  */
40 static uint_t dl_to_dlt[][3] = {
41 	{ DL_CSMACD,	DLT_EN10MB,	14 },	/* IEEE 802.3 CSMA/CD */
42 	{ DL_TPB,	DLT_NULL,	0 },	/* IEEE 802.4 Token Bus */
43 	{ DL_TPR,	DLT_IEEE802,	0 },	/* IEEE 802.5 Token Ring */
44 	{ DL_METRO,	DLT_NULL,	0 },	/* IEEE 802.6 Metro Net */
45 	{ DL_ETHER,	DLT_EN10MB,	14 },	/* Ethernet Bus */
46 	{ DL_HDLC,	DLT_C_HDLC,	0 },	/* Cisco HDLC protocol */
47 	{ DL_CHAR,	DLT_NULL,	0 },	/* Character Synchr. proto */
48 	{ DL_CTCA,	DLT_NULL,	0 },	/* IBM Channel-to-Channel */
49 	{ DL_FDDI,	DLT_FDDI,	24 },	/* Fiber Distributed data */
50 	{ DL_FC,	DLT_NULL,	0 },	/* Fibre Channel interface */
51 	{ DL_ATM,	DLT_SUNATM,	0 },	/* ATM */
52 	{ DL_IPATM,	DLT_ATM_CLIP,	0 },	/* ATM CLIP */
53 	{ DL_X25,	DLT_NULL,	0 },	/* X.25 LAPB interface */
54 	{ DL_ISDN,	DLT_NULL,	0 },	/* ISDN interface */
55 	{ DL_HIPPI,	DLT_HIPPI,	0 },	/* HIPPI interface */
56 	{ DL_100VG,	DLT_EN10MB,	14 },	/* 100 Based VG Ethernet */
57 	{ DL_100VGTPR,	DLT_IEEE802,	0 },	/* 100 Based VG Token Ring */
58 	{ DL_ETH_CSMA,	DLT_EN10MB,	14 },	/* ISO 8802/3 and Ethernet */
59 	{ DL_100BT,	DLT_EN10MB,	14 },	/* 100 Base T */
60 	{ DL_IB,	DLT_IPOIB,	44 },	/* Solaris IPoIB (infini.) */
61 	{ DL_FRAME,	DLT_FRELAY,	0 },	/* Frame Relay LAPF */
62 	{ DL_MPFRAME,	DLT_NULL,	0 },	/* Multi-protocol Frame Relay */
63 	{ DL_ASYNC,	DLT_NULL,	0 },	/* Character Asynch. Protocol */
64 	{ DL_IPX25,	DLT_NULL,	0 },	/* X.25 Classical IP */
65 	{ DL_LOOP,	DLT_NULL,	0 },	/* software loopback */
66 	{ DL_IPV4,	DLT_RAW,	0 },	/* IPv4 Tunnel Link */
67 	{ DL_IPV6,	DLT_RAW,	0 },	/* IPv6 Tunnel Link */
68 	{ SUNW_DL_VNI,	DLT_NULL,	0 },	/* Virtual network interface */
69 	{ DL_WIFI,	DLT_IEEE802_11,	0 },	/* IEEE 802.11 */
70 	{ DL_IPNET,	DLT_IPNET,	24 },	/* Solaris IP Observability */
71 	{ DL_OTHER,	DLT_NULL,	0 },	/* Mediums not listed above */
72 	{ 0,		0 }
73 };
74 
75 /*
76  * Given a data link type number used with DLPI on Solaris, return
77  * the equivalent data link type number for use with BPF.
78  */
79 int
80 bpf_dl_to_dlt(int dl)
81 {
82 	int i;
83 
84 	for (i = 0; i < sizeof (dl_to_dlt) / sizeof (dl_to_dlt[0]); i++)
85 		if (dl_to_dlt[i][0] == dl)
86 			return (dl_to_dlt[i][1]);
87 	return (0);
88 }
89 
90 /*
91  * Given a DLPI data link type for Solaris, return the expected header
92  * size of the link layer.
93  */
94 int
95 bpf_dl_hdrsize(int dl)
96 {
97 	int i;
98 
99 	for (i = 0; i < sizeof (dl_to_dlt) / sizeof (dl_to_dlt[0]); i++)
100 		if (dl_to_dlt[i][0] == dl)
101 			return (dl_to_dlt[i][2]);
102 	return (0);
103 }
104