]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2017 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1989, 1993 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | */ | |
61 | ||
1c79356b A |
62 | #include <sys/param.h> |
63 | #include <sys/systm.h> | |
64 | #include <sys/kernel.h> | |
65 | #include <sys/malloc.h> | |
66 | #include <sys/mbuf.h> | |
67 | #include <sys/socket.h> | |
68 | #include <sys/sockio.h> | |
69 | #include <sys/sysctl.h> | |
70 | ||
2d21ac55 A |
71 | #include <pexpert/pexpert.h> |
72 | ||
0a7de745 | 73 | #define etherbroadcastaddr fugly |
1c79356b | 74 | #include <net/if.h> |
1c79356b A |
75 | #include <net/route.h> |
76 | #include <net/if_llc.h> | |
77 | #include <net/if_dl.h> | |
78 | #include <net/if_types.h> | |
91447636 | 79 | #include <net/if_ether.h> |
39236c6e | 80 | #include <net/if_gif.h> |
1c79356b | 81 | #include <netinet/if_ether.h> |
0a7de745 | 82 | #include <netinet/in.h> /* For M_LOOP */ |
2d21ac55 A |
83 | #include <net/kpi_interface.h> |
84 | #include <net/kpi_protocol.h> | |
b0d623f7 | 85 | #undef etherbroadcastaddr |
1c79356b A |
86 | |
87 | /* | |
0a7de745 A |
88 | #if INET |
89 | #include <netinet/in.h> | |
90 | #include <netinet/in_var.h> | |
91 | * | |
92 | #include <netinet/in_systm.h> | |
93 | #include <netinet/ip.h> | |
94 | #endif | |
95 | */ | |
2d21ac55 | 96 | #include <net/ether_if_module.h> |
1c79356b | 97 | #include <sys/socketvar.h> |
4a249263 | 98 | #include <net/if_vlan_var.h> |
cb323159 | 99 | #include <net/if_6lowpan_var.h> |
316670eb A |
100 | #if BOND |
101 | #include <net/if_bond_internal.h> | |
102 | #endif /* BOND */ | |
6d2010ae A |
103 | #if IF_BRIDGE |
104 | #include <net/if_bridgevar.h> | |
105 | #endif /* IF_BRIDGE */ | |
5ba3f43e A |
106 | #if IF_FAKE |
107 | #include <net/if_fake_var.h> | |
108 | #endif /* IF_FAKE */ | |
cb323159 A |
109 | #if IF_HEADLESS |
110 | extern void if_headless_init(void); | |
111 | #endif /* IF_HEADLESS */ | |
1c79356b A |
112 | |
113 | #include <net/dlil.h> | |
114 | ||
91447636 | 115 | SYSCTL_DECL(_net_link); |
0a7de745 | 116 | SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW | CTLFLAG_LOCKED, 0, |
39236c6e | 117 | "Ethernet"); |
1c79356b | 118 | |
1c79356b | 119 | struct en_desc { |
0a7de745 A |
120 | u_int16_t type; /* Type of protocol stored in data */ |
121 | u_int32_t protocol_family; /* Protocol family */ | |
122 | u_int32_t data[2]; /* Protocol data */ | |
1c79356b | 123 | }; |
2d21ac55 | 124 | |
91447636 | 125 | /* descriptors are allocated in blocks of ETHER_DESC_BLK_SIZE */ |
5ba3f43e A |
126 | #if CONFIG_EMBEDDED |
127 | #define ETHER_DESC_BLK_SIZE (2) /* IP, ARP */ | |
128 | #else | |
2d21ac55 | 129 | #define ETHER_DESC_BLK_SIZE (10) |
5ba3f43e | 130 | #endif |
1c79356b A |
131 | |
132 | /* | |
6d2010ae | 133 | * Header for the demux list, hangs off of IFP at if_family_cookie |
1c79356b | 134 | */ |
1c79356b | 135 | struct ether_desc_blk_str { |
b0d623f7 | 136 | u_int32_t n_max_used; |
0a7de745 A |
137 | u_int32_t n_count; |
138 | u_int32_t n_used; | |
91447636 | 139 | struct en_desc block_ptr[1]; |
1c79356b | 140 | }; |
39236c6e | 141 | |
91447636 | 142 | /* Size of the above struct before the array of struct en_desc */ |
0a7de745 | 143 | #define ETHER_DESC_HEADER_SIZE \ |
39236c6e | 144 | ((size_t) offsetof(struct ether_desc_blk_str, block_ptr)) |
1c79356b | 145 | |
39236c6e | 146 | __private_extern__ u_char etherbroadcastaddr[ETHER_ADDR_LEN] = |
0a7de745 | 147 | { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
55e303ae | 148 | |
1c79356b | 149 | /* |
91447636 | 150 | * Release all descriptor entries owned by this protocol (there may be several). |
7b1edb79 | 151 | * Setting the type to 0 releases the entry. Eventually we should compact-out |
1c79356b A |
152 | * the unused entries. |
153 | */ | |
91447636 | 154 | int |
39236c6e | 155 | ether_del_proto(ifnet_t ifp, protocol_family_t protocol_family) |
1c79356b | 156 | { |
39236c6e A |
157 | struct ether_desc_blk_str *desc_blk = |
158 | (struct ether_desc_blk_str *)ifp->if_family_cookie; | |
159 | u_int32_t current = 0; | |
91447636 | 160 | int found = 0; |
39236c6e | 161 | |
0a7de745 A |
162 | if (desc_blk == NULL) { |
163 | return 0; | |
164 | } | |
39236c6e | 165 | |
91447636 | 166 | for (current = desc_blk->n_max_used; current > 0; current--) { |
39236c6e A |
167 | if (desc_blk->block_ptr[current - 1].protocol_family == |
168 | protocol_family) { | |
91447636 A |
169 | found = 1; |
170 | desc_blk->block_ptr[current - 1].type = 0; | |
171 | desc_blk->n_used--; | |
172 | } | |
173 | } | |
39236c6e | 174 | |
91447636 | 175 | if (desc_blk->n_used == 0) { |
6d2010ae A |
176 | FREE(ifp->if_family_cookie, M_IFADDR); |
177 | ifp->if_family_cookie = 0; | |
39236c6e | 178 | } else { |
91447636 | 179 | /* Decrement n_max_used */ |
39236c6e A |
180 | for (; desc_blk->n_max_used > 0 && |
181 | desc_blk->block_ptr[desc_blk->n_max_used - 1].type == 0; | |
0a7de745 | 182 | desc_blk->n_max_used--) { |
91447636 | 183 | ; |
0a7de745 | 184 | } |
91447636 | 185 | } |
1c79356b | 186 | |
0a7de745 | 187 | return 0; |
39236c6e | 188 | } |
1c79356b | 189 | |
91447636 | 190 | static int |
39236c6e A |
191 | ether_add_proto_internal(struct ifnet *ifp, protocol_family_t protocol, |
192 | const struct ifnet_demux_desc *demux) | |
91447636 A |
193 | { |
194 | struct en_desc *ed; | |
39236c6e A |
195 | struct ether_desc_blk_str *desc_blk = |
196 | (struct ether_desc_blk_str *)ifp->if_family_cookie; | |
91447636 | 197 | u_int32_t i; |
39236c6e | 198 | |
91447636 | 199 | switch (demux->type) { |
39236c6e A |
200 | /* These types are supported */ |
201 | /* Top three are preferred */ | |
202 | case DLIL_DESC_ETYPE2: | |
0a7de745 A |
203 | if (demux->datalen != 2) { |
204 | return EINVAL; | |
205 | } | |
39236c6e A |
206 | break; |
207 | ||
208 | case DLIL_DESC_SAP: | |
0a7de745 A |
209 | if (demux->datalen != 3) { |
210 | return EINVAL; | |
211 | } | |
39236c6e A |
212 | break; |
213 | ||
214 | case DLIL_DESC_SNAP: | |
0a7de745 A |
215 | if (demux->datalen != 5) { |
216 | return EINVAL; | |
217 | } | |
39236c6e A |
218 | break; |
219 | ||
220 | default: | |
0a7de745 | 221 | return ENOTSUP; |
39236c6e A |
222 | } |
223 | ||
224 | /* Verify a matching descriptor does not exist */ | |
225 | if (desc_blk != NULL) { | |
226 | switch (demux->type) { | |
91447636 | 227 | case DLIL_DESC_ETYPE2: |
39236c6e A |
228 | for (i = 0; i < desc_blk->n_max_used; i++) { |
229 | if (desc_blk->block_ptr[i].type == | |
230 | DLIL_DESC_ETYPE2 && | |
231 | desc_blk->block_ptr[i].data[0] == | |
232 | *(u_int16_t*)demux->data) { | |
0a7de745 | 233 | return EADDRINUSE; |
39236c6e | 234 | } |
91447636 A |
235 | } |
236 | break; | |
91447636 | 237 | case DLIL_DESC_SAP: |
91447636 | 238 | case DLIL_DESC_SNAP: |
39236c6e A |
239 | for (i = 0; i < desc_blk->n_max_used; i++) { |
240 | if (desc_blk->block_ptr[i].type == | |
241 | demux->type && | |
242 | bcmp(desc_blk->block_ptr[i].data, | |
243 | demux->data, demux->datalen) == 0) { | |
0a7de745 | 244 | return EADDRINUSE; |
39236c6e | 245 | } |
91447636 A |
246 | } |
247 | break; | |
91447636 A |
248 | } |
249 | } | |
39236c6e A |
250 | |
251 | /* Check for case where all of the descriptor blocks are in use */ | |
91447636 A |
252 | if (desc_blk == NULL || desc_blk->n_used == desc_blk->n_count) { |
253 | struct ether_desc_blk_str *tmp; | |
39236c6e A |
254 | u_int32_t new_count = ETHER_DESC_BLK_SIZE; |
255 | u_int32_t new_size; | |
256 | u_int32_t old_size = 0; | |
257 | ||
91447636 | 258 | i = 0; |
39236c6e | 259 | |
91447636 A |
260 | if (desc_blk) { |
261 | new_count += desc_blk->n_count; | |
0a7de745 | 262 | old_size = desc_blk->n_count * sizeof(struct en_desc) + |
39236c6e | 263 | ETHER_DESC_HEADER_SIZE; |
91447636 A |
264 | i = desc_blk->n_used; |
265 | } | |
39236c6e | 266 | |
0a7de745 | 267 | new_size = new_count * sizeof(struct en_desc) + |
39236c6e A |
268 | ETHER_DESC_HEADER_SIZE; |
269 | ||
91447636 | 270 | tmp = _MALLOC(new_size, M_IFADDR, M_WAITOK); |
0a7de745 | 271 | if (tmp == NULL) { |
91447636 A |
272 | /* |
273 | * Remove any previous descriptors set in the call. | |
274 | */ | |
0a7de745 | 275 | return ENOMEM; |
91447636 | 276 | } |
2d21ac55 A |
277 | |
278 | bzero(((char *)tmp) + old_size, new_size - old_size); | |
91447636 A |
279 | if (desc_blk) { |
280 | bcopy(desc_blk, tmp, old_size); | |
281 | FREE(desc_blk, M_IFADDR); | |
282 | } | |
283 | desc_blk = tmp; | |
6d2010ae | 284 | ifp->if_family_cookie = (uintptr_t)desc_blk; |
91447636 | 285 | desc_blk->n_count = new_count; |
39236c6e | 286 | } else { |
91447636 A |
287 | /* Find a free entry */ |
288 | for (i = 0; i < desc_blk->n_count; i++) { | |
289 | if (desc_blk->block_ptr[i].type == 0) { | |
290 | break; | |
291 | } | |
292 | } | |
293 | } | |
39236c6e | 294 | |
91447636 A |
295 | /* Bump n_max_used if appropriate */ |
296 | if (i + 1 > desc_blk->n_max_used) { | |
297 | desc_blk->n_max_used = i + 1; | |
298 | } | |
39236c6e | 299 | |
91447636 A |
300 | ed = &desc_blk->block_ptr[i]; |
301 | ed->protocol_family = protocol; | |
302 | ed->data[0] = 0; | |
303 | ed->data[1] = 0; | |
39236c6e | 304 | |
91447636 | 305 | switch (demux->type) { |
39236c6e A |
306 | case DLIL_DESC_ETYPE2: |
307 | /* 2 byte ethernet raw protocol type is at native_type */ | |
308 | /* prtocol must be in network byte order */ | |
309 | ed->type = DLIL_DESC_ETYPE2; | |
310 | ed->data[0] = *(u_int16_t*)demux->data; | |
311 | break; | |
312 | ||
313 | case DLIL_DESC_SAP: | |
314 | ed->type = DLIL_DESC_SAP; | |
315 | bcopy(demux->data, &ed->data[0], 3); | |
316 | break; | |
317 | ||
318 | case DLIL_DESC_SNAP: { | |
0a7de745 | 319 | u_int8_t* pDest = ((u_int8_t*)&ed->data[0]) + 3; |
39236c6e A |
320 | ed->type = DLIL_DESC_SNAP; |
321 | bcopy(demux->data, pDest, 5); | |
322 | break; | |
91447636 | 323 | } |
39236c6e A |
324 | } |
325 | ||
91447636 | 326 | desc_blk->n_used++; |
39236c6e | 327 | |
0a7de745 | 328 | return 0; |
91447636 | 329 | } |
1c79356b | 330 | |
91447636 | 331 | int |
0a7de745 | 332 | ether_add_proto(ifnet_t ifp, protocol_family_t protocol, |
39236c6e | 333 | const struct ifnet_demux_desc *demux_list, u_int32_t demux_count) |
91447636 | 334 | { |
39236c6e A |
335 | int error = 0; |
336 | u_int32_t i; | |
337 | ||
91447636 A |
338 | for (i = 0; i < demux_count; i++) { |
339 | error = ether_add_proto_internal(ifp, protocol, &demux_list[i]); | |
340 | if (error) { | |
341 | ether_del_proto(ifp, protocol); | |
342 | break; | |
343 | } | |
344 | } | |
39236c6e | 345 | |
0a7de745 | 346 | return error; |
91447636 | 347 | } |
4a249263 | 348 | |
91447636 | 349 | int |
39236c6e A |
350 | ether_demux(ifnet_t ifp, mbuf_t m, char *frame_header, |
351 | protocol_family_t *protocol_family) | |
1c79356b | 352 | { |
316670eb | 353 | struct ether_header *eh = (struct ether_header *)(void *)frame_header; |
0a7de745 | 354 | u_short ether_type = eh->ether_type; |
39236c6e A |
355 | u_int16_t type; |
356 | u_int8_t *data; | |
357 | u_int32_t i = 0; | |
358 | struct ether_desc_blk_str *desc_blk = | |
359 | (struct ether_desc_blk_str *)ifp->if_family_cookie; | |
360 | u_int32_t maxd = desc_blk ? desc_blk->n_max_used : 0; | |
0a7de745 | 361 | struct en_desc *ed = desc_blk ? desc_blk->block_ptr : NULL; |
39236c6e A |
362 | u_int32_t extProto1 = 0; |
363 | u_int32_t extProto2 = 0; | |
91447636 A |
364 | |
365 | if (eh->ether_dhost[0] & 1) { | |
366 | /* Check for broadcast */ | |
0a7de745 | 367 | if (_ether_cmp(etherbroadcastaddr, eh->ether_dhost) == 0) { |
91447636 | 368 | m->m_flags |= M_BCAST; |
0a7de745 | 369 | } else { |
91447636 | 370 | m->m_flags |= M_MCAST; |
0a7de745 | 371 | } |
91447636 | 372 | } |
1c79356b | 373 | |
6d2010ae | 374 | if (m->m_flags & M_HASFCS) { |
0a7de745 A |
375 | /* |
376 | * If the M_HASFCS is set by the driver we want to make sure | |
377 | * that we strip off the trailing FCS data before handing it | |
378 | * up the stack. | |
379 | */ | |
380 | m_adj(m, -ETHER_CRC_LEN); | |
381 | m->m_flags &= ~M_HASFCS; | |
382 | } | |
6d2010ae | 383 | |
91447636 A |
384 | if ((eh->ether_dhost[0] & 1) == 0) { |
385 | /* | |
39236c6e A |
386 | * When the driver is put into promiscuous mode we may receive |
387 | * unicast frames that are not intended for our interfaces. | |
388 | * They are marked here as being promiscuous so the caller may | |
389 | * dispose of them after passing the packets to any interface | |
390 | * filters. | |
2d21ac55 | 391 | */ |
39236c6e | 392 | if (_ether_cmp(eh->ether_dhost, IF_LLADDR(ifp))) { |
91447636 A |
393 | m->m_flags |= M_PROMISC; |
394 | } | |
395 | } | |
39236c6e | 396 | |
cb323159 A |
397 | /* check for IEEE 802.15.4 */ |
398 | if (ether_type == htons(ETHERTYPE_IEEE802154)) { | |
399 | *protocol_family = PF_802154; | |
400 | return 0; | |
401 | } | |
402 | ||
593a1d5f A |
403 | /* check for VLAN */ |
404 | if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) != 0) { | |
405 | if (EVL_VLANOFTAG(m->m_pkthdr.vlan_tag) != 0) { | |
406 | *protocol_family = PF_VLAN; | |
0a7de745 | 407 | return 0; |
593a1d5f A |
408 | } |
409 | /* the packet is just priority-tagged, clear the bit */ | |
410 | m->m_pkthdr.csum_flags &= ~CSUM_VLAN_TAG_VALID; | |
39236c6e | 411 | } else if (ether_type == htons(ETHERTYPE_VLAN)) { |
0a7de745 | 412 | struct ether_vlan_header * evl; |
593a1d5f | 413 | |
316670eb | 414 | evl = (struct ether_vlan_header *)(void *)frame_header; |
39236c6e A |
415 | if (m->m_len < ETHER_VLAN_ENCAP_LEN || |
416 | ntohs(evl->evl_proto) == ETHERTYPE_VLAN || | |
417 | EVL_VLANOFTAG(ntohs(evl->evl_tag)) != 0) { | |
593a1d5f | 418 | *protocol_family = PF_VLAN; |
0a7de745 | 419 | return 0; |
593a1d5f A |
420 | } |
421 | /* the packet is just priority-tagged */ | |
422 | ||
423 | /* make the encapsulated ethertype the actual ethertype */ | |
424 | ether_type = evl->evl_encap_proto = evl->evl_proto; | |
425 | ||
426 | /* remove the encapsulation header */ | |
427 | m->m_len -= ETHER_VLAN_ENCAP_LEN; | |
428 | m->m_data += ETHER_VLAN_ENCAP_LEN; | |
429 | m->m_pkthdr.len -= ETHER_VLAN_ENCAP_LEN; | |
430 | m->m_pkthdr.csum_flags = 0; /* can't trust hardware checksum */ | |
0a7de745 | 431 | } else if (ether_type == htons(ETHERTYPE_ARP)) { |
39236c6e | 432 | m->m_pkthdr.pkt_flags |= PKTF_INET_RESOLVE; /* ARP packet */ |
0a7de745 | 433 | } |
91447636 | 434 | data = mtod(m, u_int8_t*); |
39236c6e | 435 | |
91447636 | 436 | /* |
0a7de745 A |
437 | * Determine the packet's protocol type and stuff the protocol into |
438 | * longs for quick compares. | |
439 | */ | |
91447636 | 440 | if (ntohs(ether_type) <= 1500) { |
0a7de745 | 441 | bcopy(data, &extProto1, sizeof(u_int32_t)); |
39236c6e A |
442 | |
443 | /* SAP or SNAP */ | |
91447636 | 444 | if ((extProto1 & htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) { |
39236c6e | 445 | /* SNAP */ |
91447636 | 446 | type = DLIL_DESC_SNAP; |
0a7de745 A |
447 | bcopy(data + sizeof(u_int32_t), &extProto2, |
448 | sizeof(u_int32_t)); | |
91447636 A |
449 | extProto1 &= htonl(0x000000FF); |
450 | } else { | |
451 | type = DLIL_DESC_SAP; | |
452 | extProto1 &= htonl(0xFFFFFF00); | |
453 | } | |
454 | } else { | |
455 | type = DLIL_DESC_ETYPE2; | |
456 | } | |
39236c6e A |
457 | |
458 | /* | |
0a7de745 A |
459 | * Search through the connected protocols for a match. |
460 | */ | |
91447636 | 461 | switch (type) { |
39236c6e A |
462 | case DLIL_DESC_ETYPE2: |
463 | for (i = 0; i < maxd; i++) { | |
464 | if ((ed[i].type == type) && | |
465 | (ed[i].data[0] == ether_type)) { | |
466 | *protocol_family = ed[i].protocol_family; | |
0a7de745 | 467 | return 0; |
91447636 | 468 | } |
39236c6e A |
469 | } |
470 | break; | |
471 | ||
472 | case DLIL_DESC_SAP: | |
473 | for (i = 0; i < maxd; i++) { | |
474 | if ((ed[i].type == type) && | |
475 | (ed[i].data[0] == extProto1)) { | |
476 | *protocol_family = ed[i].protocol_family; | |
0a7de745 | 477 | return 0; |
91447636 | 478 | } |
39236c6e | 479 | } |
91447636 | 480 | break; |
39236c6e A |
481 | |
482 | case DLIL_DESC_SNAP: | |
483 | for (i = 0; i < maxd; i++) { | |
484 | if ((ed[i].type == type) && | |
485 | (ed[i].data[0] == extProto1) && | |
0a7de745 | 486 | (ed[i].data[1] == extProto2)) { |
39236c6e | 487 | *protocol_family = ed[i].protocol_family; |
0a7de745 | 488 | return 0; |
39236c6e A |
489 | } |
490 | } | |
0a7de745 | 491 | break; |
91447636 | 492 | } |
39236c6e | 493 | |
0a7de745 | 494 | return ENOENT; |
39236c6e A |
495 | } |
496 | ||
497 | /* | |
498 | * On embedded, ether_frameout is practicaly ether_frameout_extended. | |
499 | * On non-embedded, ether_frameout has long been exposed as a public KPI, | |
500 | * and therefore its signature must remain the same (without the pre- and | |
501 | * postpend length parameters.) | |
502 | */ | |
503 | #if KPI_INTERFACE_EMBEDDED | |
504 | int | |
505 | ether_frameout(struct ifnet *ifp, struct mbuf **m, | |
506 | const struct sockaddr *ndest, const char *edst, | |
507 | const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len) | |
508 | #else /* !KPI_INTERFACE_EMBEDDED */ | |
509 | int | |
510 | ether_frameout(struct ifnet *ifp, struct mbuf **m, | |
511 | const struct sockaddr *ndest, const char *edst, | |
512 | const char *ether_type) | |
513 | #endif /* KPI_INTERFACE_EMBEDDED */ | |
514 | { | |
515 | #if KPI_INTERFACE_EMBEDDED | |
0a7de745 A |
516 | return ether_frameout_extended(ifp, m, ndest, edst, ether_type, |
517 | prepend_len, postpend_len); | |
39236c6e | 518 | #else /* !KPI_INTERFACE_EMBEDDED */ |
0a7de745 A |
519 | return ether_frameout_extended(ifp, m, ndest, edst, ether_type, |
520 | NULL, NULL); | |
39236c6e A |
521 | #endif /* !KPI_INTERFACE_EMBEDDED */ |
522 | } | |
1c79356b A |
523 | |
524 | /* | |
525 | * Ethernet output routine. | |
526 | * Encapsulate a packet of type family for the local net. | |
527 | * Use trailer local net encapsulation if enough data in first | |
528 | * packet leaves a multiple of 512 bytes of data in remainder. | |
1c79356b A |
529 | */ |
530 | int | |
39236c6e A |
531 | ether_frameout_extended(struct ifnet *ifp, struct mbuf **m, |
532 | const struct sockaddr *ndest, const char *edst, | |
533 | const char *ether_type, u_int32_t *prepend_len, u_int32_t *postpend_len) | |
1c79356b | 534 | { |
91447636 | 535 | struct ether_header *eh; |
0a7de745 | 536 | int hlen; /* link layer header length */ |
1c79356b A |
537 | |
538 | hlen = ETHER_HDR_LEN; | |
539 | ||
540 | /* | |
541 | * If a simplex interface, and the packet is being sent to our | |
542 | * Ethernet address or a broadcast address, loopback a copy. | |
543 | * XXX To make a simplex device behave exactly like a duplex | |
544 | * device, we should copy in the case of sending to our own | |
545 | * ethernet address (thus letting the original actually appear | |
546 | * on the wire). However, we don't do that here for security | |
547 | * reasons and compatibility with the original behavior. | |
548 | */ | |
549 | if ((ifp->if_flags & IFF_SIMPLEX) && | |
39236c6e A |
550 | ((*m)->m_flags & M_LOOP) && lo_ifp != NULL) { |
551 | if ((*m)->m_flags & M_BCAST) { | |
552 | struct mbuf *n = m_copy(*m, 0, (int)M_COPYALL); | |
553 | if (n != NULL) { | |
554 | dlil_output(lo_ifp, ndest->sa_family, | |
555 | n, NULL, ndest, 0, NULL); | |
556 | } | |
557 | } else if (_ether_cmp(edst, IF_LLADDR(ifp)) == 0) { | |
558 | dlil_output(lo_ifp, ndest->sa_family, *m, | |
559 | NULL, ndest, 0, NULL); | |
0a7de745 | 560 | return EJUSTRETURN; |
39236c6e | 561 | } |
1c79356b | 562 | } |
39236c6e | 563 | |
1c79356b A |
564 | /* |
565 | * Add local net header. If no space in first mbuf, | |
566 | * allocate another. | |
567 | */ | |
0a7de745 A |
568 | M_PREPEND(*m, sizeof(struct ether_header), M_DONTWAIT, 0); |
569 | if (*m == NULL) { | |
570 | return EJUSTRETURN; | |
571 | } | |
39236c6e | 572 | |
0a7de745 A |
573 | if (prepend_len != NULL) { |
574 | *prepend_len = sizeof(struct ether_header); | |
575 | } | |
576 | if (postpend_len != NULL) { | |
39236c6e | 577 | *postpend_len = 0; |
0a7de745 | 578 | } |
1c79356b | 579 | |
1c79356b | 580 | eh = mtod(*m, struct ether_header *); |
39236c6e A |
581 | (void) memcpy(&eh->ether_type, ether_type, sizeof(eh->ether_type)); |
582 | (void) memcpy(eh->ether_dhost, edst, ETHER_ADDR_LEN); | |
583 | ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, ETHER_ADDR_LEN); | |
1c79356b | 584 | |
0a7de745 | 585 | return 0; |
1c79356b A |
586 | } |
587 | ||
91447636 | 588 | errno_t |
39236c6e | 589 | ether_check_multi(ifnet_t ifp, const struct sockaddr *proto_addr) |
91447636 | 590 | { |
39236c6e | 591 | #pragma unused(ifp) |
0a7de745 | 592 | errno_t result = EAFNOSUPPORT; |
91447636 | 593 | const u_char *e_addr; |
39236c6e | 594 | |
91447636 A |
595 | /* |
596 | * AF_SPEC and AF_LINK don't require translation. We do | |
597 | * want to verify that they specify a valid multicast. | |
598 | */ | |
0a7de745 | 599 | switch (proto_addr->sa_family) { |
39236c6e A |
600 | case AF_UNSPEC: |
601 | e_addr = (const u_char*)&proto_addr->sa_data[0]; | |
0a7de745 | 602 | if ((e_addr[0] & 0x01) != 0x01) { |
39236c6e | 603 | result = EADDRNOTAVAIL; |
0a7de745 | 604 | } else { |
39236c6e | 605 | result = 0; |
0a7de745 | 606 | } |
39236c6e A |
607 | break; |
608 | ||
609 | case AF_LINK: | |
610 | e_addr = CONST_LLADDR((const struct sockaddr_dl*) | |
611 | (uintptr_t)(size_t)proto_addr); | |
0a7de745 | 612 | if ((e_addr[0] & 0x01) != 0x01) { |
39236c6e | 613 | result = EADDRNOTAVAIL; |
0a7de745 | 614 | } else { |
39236c6e | 615 | result = 0; |
0a7de745 | 616 | } |
39236c6e | 617 | break; |
91447636 | 618 | } |
39236c6e | 619 | |
0a7de745 | 620 | return result; |
91447636 A |
621 | } |
622 | ||
1c79356b | 623 | int |
39236c6e | 624 | ether_ioctl(ifnet_t ifp, u_int32_t command, void *data) |
1c79356b | 625 | { |
39236c6e | 626 | #pragma unused(ifp, command, data) |
0a7de745 | 627 | return EOPNOTSUPP; |
1c79356b A |
628 | } |
629 | ||
39236c6e A |
630 | __private_extern__ int |
631 | ether_family_init(void) | |
1c79356b | 632 | { |
0a7de745 | 633 | errno_t error = 0; |
39236c6e | 634 | |
91447636 | 635 | /* Register protocol registration functions */ |
2d21ac55 | 636 | if ((error = proto_register_plumber(PF_INET, APPLE_IF_FAM_ETHERNET, |
39236c6e A |
637 | ether_attach_inet, ether_detach_inet)) != 0) { |
638 | printf("proto_register_plumber failed for PF_INET error=%d\n", | |
639 | error); | |
91447636 A |
640 | goto done; |
641 | } | |
2d21ac55 A |
642 | #if INET6 |
643 | if ((error = proto_register_plumber(PF_INET6, APPLE_IF_FAM_ETHERNET, | |
39236c6e A |
644 | ether_attach_inet6, ether_detach_inet6)) != 0) { |
645 | printf("proto_register_plumber failed for PF_INET6 error=%d\n", | |
646 | error); | |
2d21ac55 A |
647 | goto done; |
648 | } | |
649 | #endif /* INET6 */ | |
2d21ac55 | 650 | #if VLAN |
91447636 | 651 | vlan_family_init(); |
2d21ac55 A |
652 | #endif /* VLAN */ |
653 | #if BOND | |
91447636 | 654 | bond_family_init(); |
2d21ac55 | 655 | #endif /* BOND */ |
6d2010ae A |
656 | #if IF_BRIDGE |
657 | bridgeattach(0); | |
658 | #endif /* IF_BRIDGE */ | |
5ba3f43e A |
659 | #if IF_FAKE |
660 | if_fake_init(); | |
661 | #endif /* IF_FAKE */ | |
cb323159 A |
662 | #if IF_HEADLESS |
663 | if_headless_init(); | |
664 | #endif /* IF_HEADLESS */ | |
665 | #if SIXLOWPAN | |
666 | sixlowpan_family_init(); | |
667 | #endif /* VLAN */ | |
39236c6e | 668 | done: |
55e303ae | 669 | |
0a7de745 | 670 | return error; |
1c79356b | 671 | } |