xref: /illumos-gate/usr/src/uts/common/io/bpf/bpf_mac.c (revision f195a053)
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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/stream.h>
29 #include <net/bpf.h>
30 #include <net/bpfdesc.h>
31 
32 /*
33  * This file provides the link to the functions required from the mac
34  * module. It is currently in bpf, rather than mac (like ipnet_bpf)
35  * because of the mac/dls split. The bpf driver needs to know when
36  * interfaces appear and disappear and the best place for that is in
37  * dls. Unfortunately all of the other functions used here are found
38  * in the mac module, making it seem ill suited to being at home in
39  * dls. Similarly it has even less purpose being in mac as it is
40  * today.
41  */
42 static int	mac_bpf_open(const char *, uintptr_t *, zoneid_t);
43 static void	mac_bpf_close(uintptr_t);
44 static const char *mac_bpf_name(uintptr_t);
45 static int	mac_bpf_type(uintptr_t);
46 static void	mac_bpf_sdu_get(uintptr_t, uint_t *);
47 static int	mac_bpf_tx(uintptr_t, mblk_t *);
48 static uintptr_t mac_bpf_promisc_add(uintptr_t, int, void *, uintptr_t *, int);
49 static void	mac_bpf_promisc_remove(uintptr_t);
50 static int	mac_bpf_client_open(uintptr_t, uintptr_t *);
51 static void	mac_bpf_client_close(uintptr_t);
52 static const char *mac_bpf_client_name(uintptr_t);
53 static int	mac_bpf_getdlt(uintptr_t, uint_t *);
54 static int	mac_bpf_getlinkid(const char *, datalink_id_t *, zoneid_t);
55 static int	mac_bpf_getzone(uintptr_t, zoneid_t *);
56 
57 bpf_provider_t bpf_mac = {
58 	BPR_MAC,
59 	mac_bpf_open,
60 	mac_bpf_close,
61 	mac_bpf_name,
62 	mac_bpf_type,
63 	mac_bpf_sdu_get,
64 	mac_bpf_tx,
65 	mac_bpf_promisc_add,
66 	mac_bpf_promisc_remove,
67 	mac_bpf_getlinkid,
68 	mac_bpf_client_close,
69 	mac_bpf_client_name,
70 	mac_bpf_client_open,
71 	mac_bpf_getzone,
72 	mac_bpf_getdlt
73 };
74 
75 /*ARGSUSED*/
76 static int
mac_bpf_open(const char * name,uintptr_t * mhandlep,zoneid_t zoneid)77 mac_bpf_open(const char *name, uintptr_t *mhandlep, zoneid_t zoneid)
78 {
79 	return (mac_open_by_linkname(name, (mac_handle_t *)mhandlep));
80 }
81 
82 static void
mac_bpf_close(uintptr_t mhandle)83 mac_bpf_close(uintptr_t mhandle)
84 {
85 	mac_close((mac_handle_t)mhandle);
86 }
87 
88 static const char *
mac_bpf_name(uintptr_t mhandle)89 mac_bpf_name(uintptr_t mhandle)
90 {
91 	return (mac_name((mac_handle_t)mhandle));
92 }
93 
94 static int
mac_bpf_type(uintptr_t mhandle)95 mac_bpf_type(uintptr_t mhandle)
96 {
97 	return (mac_nativetype((mac_handle_t)mhandle));
98 }
99 
100 static void
mac_bpf_sdu_get(uintptr_t mhandle,uint_t * mtup)101 mac_bpf_sdu_get(uintptr_t mhandle, uint_t *mtup)
102 {
103 	mac_sdu_get((mac_handle_t)mhandle, NULL, mtup);
104 }
105 
106 static int
mac_bpf_tx(uintptr_t chandle,mblk_t * pkt)107 mac_bpf_tx(uintptr_t chandle, mblk_t *pkt)
108 {
109 	/*
110 	 * If the mac layer cannot deliver a packet as requested by BPF then
111 	 * simply have the mac layer drop it. BPF isn't interested in doing
112 	 * any amount of retry - that's left to the application.
113 	 */
114 	return (mac_tx((mac_client_handle_t)chandle, pkt, 0,
115 	    MAC_DROP_ON_NO_DESC, NULL));
116 }
117 
118 static uintptr_t
mac_bpf_promisc_add(uintptr_t chandle,int how,void * arg,uintptr_t * promisc,int flags)119 mac_bpf_promisc_add(uintptr_t chandle, int how, void *arg, uintptr_t *promisc,
120     int flags)
121 {
122 	return (mac_promisc_add((mac_client_handle_t)chandle, how, bpf_mtap,
123 	    arg, (mac_promisc_handle_t *)promisc, flags));
124 }
125 
126 static void
mac_bpf_promisc_remove(uintptr_t phandle)127 mac_bpf_promisc_remove(uintptr_t phandle)
128 {
129 	mac_promisc_remove((mac_promisc_handle_t)phandle);
130 }
131 
132 static int
mac_bpf_client_open(uintptr_t mhandle,uintptr_t * chandlep)133 mac_bpf_client_open(uintptr_t mhandle, uintptr_t *chandlep)
134 {
135 	return (mac_client_open((mac_handle_t)mhandle,
136 	    (mac_client_handle_t *)chandlep,  NULL,
137 	    MAC_OPEN_FLAGS_USE_DATALINK_NAME));
138 }
139 
140 static void
mac_bpf_client_close(uintptr_t chandle)141 mac_bpf_client_close(uintptr_t chandle)
142 {
143 	mac_client_close((mac_client_handle_t)chandle, 0);
144 }
145 
146 static const char *
mac_bpf_client_name(uintptr_t chandle)147 mac_bpf_client_name(uintptr_t chandle)
148 {
149 	return (mac_client_name((mac_client_handle_t)chandle));
150 }
151 
152 /*ARGSUSED*/
153 static int
mac_bpf_getlinkid(const char * name,datalink_id_t * idp,zoneid_t zoneid)154 mac_bpf_getlinkid(const char *name, datalink_id_t *idp, zoneid_t zoneid)
155 {
156 	int error;
157 
158 	/*
159 	 * If at first we don't succeed, try again, just in case it is in
160 	 * hiding. The first call requires the datalink management daemon
161 	 * (the authorative source of information about name to id mapping)
162 	 * to be present and answering upcalls, the seond does not.
163 	 */
164 	error = dls_mgmt_get_linkid(name, idp);
165 	if (error != 0)
166 		error = dls_devnet_macname2linkid(name, idp);
167 
168 	return (error);
169 }
170 
171 static int
mac_bpf_getzone(uintptr_t handle,zoneid_t * zip)172 mac_bpf_getzone(uintptr_t handle, zoneid_t *zip)
173 {
174 	mac_perim_handle_t mph;
175 	int error;
176 
177 	mac_perim_enter_by_mh((mac_handle_t)handle, &mph);
178 	error = dls_link_getzid(mac_name((mac_handle_t)handle), zip);
179 	mac_perim_exit(mph);
180 	return (error);
181 }
182 
183 static int
mac_bpf_getdlt(uintptr_t handle,uint_t * dltp)184 mac_bpf_getdlt(uintptr_t handle, uint_t *dltp)
185 {
186 	*dltp = mac_nativetype((mac_handle_t)handle);
187 
188 	return (0);
189 }
190