]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/ether_if_module.c
4673b53a3ef438586ea4e6d8bc0cc16d3f5d9efd
[apple/xnu.git] / bsd / net / ether_if_module.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
62
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
68 #include <sys/mbuf.h>
69 #include <sys/socket.h>
70 #include <sys/sockio.h>
71 #include <sys/sysctl.h>
72
73 #include <pexpert/pexpert.h>
74
75 #include <net/if.h>
76 #include <net/route.h>
77 #include <net/if_llc.h>
78 #include <net/if_dl.h>
79 #include <net/if_types.h>
80 #include <net/if_ether.h>
81 #include <netinet/if_ether.h>
82 #include <netinet/in.h> /* For M_LOOP */
83 #include <net/kpi_interface.h>
84 #include <net/kpi_protocol.h>
85
86 /*
87 #if INET
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #endif
94 */
95 #include <net/ether_if_module.h>
96 #include <sys/socketvar.h>
97 #include <net/if_vlan_var.h>
98 #include <net/if_bond_var.h>
99
100 #include <net/dlil.h>
101
102 #if LLC && CCITT
103 extern struct ifqueue pkintrq;
104 #endif
105
106 /* General stuff from if_ethersubr.c - may not need some of it */
107
108 #include <netat/at_pat.h>
109 #if NETAT
110 extern struct ifqueue atalkintrq;
111 #endif
112
113
114 #if BRIDGE
115 #include <net/bridge.h>
116 #endif
117
118 #define memcpy(x,y,z) bcopy(y, x, z)
119
120
121 SYSCTL_DECL(_net_link);
122 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "Ethernet");
123
124 struct en_desc {
125 u_int16_t type; /* Type of protocol stored in data */
126 u_long protocol_family; /* Protocol family */
127 u_long data[2]; /* Protocol data */
128 };
129
130 /* descriptors are allocated in blocks of ETHER_DESC_BLK_SIZE */
131 #if CONFIG_EMBEDDED
132 #define ETHER_DESC_BLK_SIZE (2) /* IP, ARP */
133 #else
134 #define ETHER_DESC_BLK_SIZE (10)
135 #endif
136
137 /*
138 * Header for the demux list, hangs off of IFP at family_cookie
139 */
140
141 struct ether_desc_blk_str {
142 u_long n_max_used;
143 u_long n_count;
144 u_long n_used;
145 struct en_desc block_ptr[1];
146 };
147 /* Size of the above struct before the array of struct en_desc */
148 #define ETHER_DESC_HEADER_SIZE ((size_t)offsetof(struct ether_desc_blk_str, block_ptr))
149 __private_extern__ u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
150 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
151
152 #if defined (__arm__)
153 static __inline__ int
154 _ether_cmp(const void * a, const void * b)
155 {
156 return (memcmp(a, b, ETHER_ADDR_LEN));
157 }
158
159 #else
160 static __inline__ int
161 _ether_cmp(const void * a, const void * b)
162 {
163 const u_int16_t * a_s = (const u_int16_t *)a;
164 const u_int16_t * b_s = (const u_int16_t *)b;
165
166 if (a_s[0] != b_s[0]
167 || a_s[1] != b_s[1]
168 || a_s[2] != b_s[2]) {
169 return (1);
170 }
171 return (0);
172 }
173 #endif
174
175 /*
176 * Release all descriptor entries owned by this protocol (there may be several).
177 * Setting the type to 0 releases the entry. Eventually we should compact-out
178 * the unused entries.
179 */
180 int
181 ether_del_proto(
182 ifnet_t ifp,
183 protocol_family_t protocol_family)
184 {
185 struct ether_desc_blk_str *desc_blk = (struct ether_desc_blk_str *)ifp->family_cookie;
186 u_long current = 0;
187 int found = 0;
188
189 if (desc_blk == NULL)
190 return 0;
191
192 for (current = desc_blk->n_max_used; current > 0; current--) {
193 if (desc_blk->block_ptr[current - 1].protocol_family == protocol_family) {
194 found = 1;
195 desc_blk->block_ptr[current - 1].type = 0;
196 desc_blk->n_used--;
197 }
198 }
199
200 if (desc_blk->n_used == 0) {
201 FREE(ifp->family_cookie, M_IFADDR);
202 ifp->family_cookie = 0;
203 }
204 else {
205 /* Decrement n_max_used */
206 for (; desc_blk->n_max_used > 0 && desc_blk->block_ptr[desc_blk->n_max_used - 1].type == 0; desc_blk->n_max_used--)
207 ;
208 }
209
210 return 0;
211 }
212
213
214 static int
215 ether_add_proto_internal(
216 struct ifnet *ifp,
217 protocol_family_t protocol,
218 const struct ifnet_demux_desc *demux)
219 {
220 struct en_desc *ed;
221 struct ether_desc_blk_str *desc_blk = (struct ether_desc_blk_str *)ifp->family_cookie;
222 u_int32_t i;
223
224 switch (demux->type) {
225 /* These types are supported */
226 /* Top three are preferred */
227 case DLIL_DESC_ETYPE2:
228 if (demux->datalen != 2) {
229 return EINVAL;
230 }
231 break;
232
233 case DLIL_DESC_SAP:
234 if (demux->datalen != 3) {
235 return EINVAL;
236 }
237 break;
238
239 case DLIL_DESC_SNAP:
240 if (demux->datalen != 5) {
241 return EINVAL;
242 }
243 break;
244
245 default:
246 return ENOTSUP;
247 }
248
249 // Verify a matching descriptor does not exist.
250 if (desc_blk != NULL) {
251 switch (demux->type) {
252 case DLIL_DESC_ETYPE2:
253 for (i = 0; i < desc_blk->n_max_used; i++) {
254 if (desc_blk->block_ptr[i].type == DLIL_DESC_ETYPE2 &&
255 desc_blk->block_ptr[i].data[0] ==
256 *(u_int16_t*)demux->data) {
257 return EADDRINUSE;
258 }
259 }
260 break;
261 case DLIL_DESC_SAP:
262 case DLIL_DESC_SNAP:
263 for (i = 0; i < desc_blk->n_max_used; i++) {
264 if (desc_blk->block_ptr[i].type == demux->type &&
265 bcmp(desc_blk->block_ptr[i].data, demux->data,
266 demux->datalen) == 0) {
267 return EADDRINUSE;
268 }
269 }
270 break;
271 }
272 }
273
274 // Check for case where all of the descriptor blocks are in use
275 if (desc_blk == NULL || desc_blk->n_used == desc_blk->n_count) {
276 struct ether_desc_blk_str *tmp;
277 u_long new_count = ETHER_DESC_BLK_SIZE;
278 u_long new_size;
279 u_long old_size = 0;
280
281 i = 0;
282
283 if (desc_blk) {
284 new_count += desc_blk->n_count;
285 old_size = desc_blk->n_count * sizeof(struct en_desc) + ETHER_DESC_HEADER_SIZE;
286 i = desc_blk->n_used;
287 }
288
289 new_size = new_count * sizeof(struct en_desc) + ETHER_DESC_HEADER_SIZE;
290
291 tmp = _MALLOC(new_size, M_IFADDR, M_WAITOK);
292 if (tmp == 0) {
293 /*
294 * Remove any previous descriptors set in the call.
295 */
296 return ENOMEM;
297 }
298
299 bzero(((char *)tmp) + old_size, new_size - old_size);
300 if (desc_blk) {
301 bcopy(desc_blk, tmp, old_size);
302 FREE(desc_blk, M_IFADDR);
303 }
304 desc_blk = tmp;
305 ifp->family_cookie = (u_long)desc_blk;
306 desc_blk->n_count = new_count;
307 }
308 else {
309 /* Find a free entry */
310 for (i = 0; i < desc_blk->n_count; i++) {
311 if (desc_blk->block_ptr[i].type == 0) {
312 break;
313 }
314 }
315 }
316
317 /* Bump n_max_used if appropriate */
318 if (i + 1 > desc_blk->n_max_used) {
319 desc_blk->n_max_used = i + 1;
320 }
321
322 ed = &desc_blk->block_ptr[i];
323 ed->protocol_family = protocol;
324 ed->data[0] = 0;
325 ed->data[1] = 0;
326
327 switch (demux->type) {
328 case DLIL_DESC_ETYPE2:
329 /* 2 byte ethernet raw protocol type is at native_type */
330 /* prtocol must be in network byte order */
331 ed->type = DLIL_DESC_ETYPE2;
332 ed->data[0] = *(u_int16_t*)demux->data;
333 break;
334
335 case DLIL_DESC_SAP:
336 ed->type = DLIL_DESC_SAP;
337 bcopy(demux->data, &ed->data[0], 3);
338 break;
339
340 case DLIL_DESC_SNAP: {
341 u_int8_t* pDest = ((u_int8_t*)&ed->data[0]) + 3;
342 ed->type = DLIL_DESC_SNAP;
343 bcopy(demux->data, pDest, 5);
344 }
345 break;
346 }
347
348 desc_blk->n_used++;
349
350 return 0;
351 }
352
353 int
354 ether_add_proto(
355 ifnet_t ifp,
356 protocol_family_t protocol,
357 const struct ifnet_demux_desc *demux_list,
358 u_int32_t demux_count)
359 {
360 int error = 0;
361 u_int32_t i;
362
363 for (i = 0; i < demux_count; i++) {
364 error = ether_add_proto_internal(ifp, protocol, &demux_list[i]);
365 if (error) {
366 ether_del_proto(ifp, protocol);
367 break;
368 }
369 }
370
371 return error;
372 }
373
374 int
375 ether_demux(
376 ifnet_t ifp,
377 mbuf_t m,
378 char *frame_header,
379 protocol_family_t *protocol_family)
380 {
381 struct ether_header *eh = (struct ether_header *)frame_header;
382 u_short ether_type = eh->ether_type;
383 u_int16_t type;
384 u_int8_t *data;
385 u_long i = 0;
386 struct ether_desc_blk_str *desc_blk = (struct ether_desc_blk_str *)ifp->family_cookie;
387 u_long maxd = desc_blk ? desc_blk->n_max_used : 0;
388 struct en_desc *ed = desc_blk ? desc_blk->block_ptr : NULL;
389 u_int32_t extProto1 = 0;
390 u_int32_t extProto2 = 0;
391
392 if (eh->ether_dhost[0] & 1) {
393 /* Check for broadcast */
394 if (_ether_cmp(etherbroadcastaddr, eh->ether_dhost) == 0)
395 m->m_flags |= M_BCAST;
396 else
397 m->m_flags |= M_MCAST;
398 }
399
400 if (ifp->if_eflags & IFEF_BOND) {
401 /* if we're bonded, bond "protocol" gets all the packets */
402 *protocol_family = PF_BOND;
403 return (0);
404 }
405
406 if ((eh->ether_dhost[0] & 1) == 0) {
407 /*
408 * When the driver is put into promiscuous mode we may receive unicast
409 * frames that are not intended for our interfaces. They are marked here
410 * as being promiscuous so the caller may dispose of them after passing
411 * the packets to any interface filters.
412 */
413 if (_ether_cmp(eh->ether_dhost, ifnet_lladdr(ifp))) {
414 m->m_flags |= M_PROMISC;
415 }
416 }
417
418 /* Quick check for VLAN */
419 if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) != 0 ||
420 ether_type == htons(ETHERTYPE_VLAN)) {
421 *protocol_family = PF_VLAN;
422 return 0;
423 }
424
425 data = mtod(m, u_int8_t*);
426
427 /*
428 * Determine the packet's protocol type and stuff the protocol into
429 * longs for quick compares.
430 */
431
432 if (ntohs(ether_type) <= 1500) {
433 extProto1 = *(u_int32_t*)data;
434
435 // SAP or SNAP
436 if ((extProto1 & htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) {
437 // SNAP
438 type = DLIL_DESC_SNAP;
439 extProto2 = *(u_int32_t*)(data + sizeof(u_int32_t));
440 extProto1 &= htonl(0x000000FF);
441 } else {
442 type = DLIL_DESC_SAP;
443 extProto1 &= htonl(0xFFFFFF00);
444 }
445 } else {
446 type = DLIL_DESC_ETYPE2;
447 }
448
449 /*
450 * Search through the connected protocols for a match.
451 */
452
453 switch (type) {
454 case DLIL_DESC_ETYPE2:
455 for (i = 0; i < maxd; i++) {
456 if ((ed[i].type == type) && (ed[i].data[0] == ether_type)) {
457 *protocol_family = ed[i].protocol_family;
458 return 0;
459 }
460 }
461 break;
462
463 case DLIL_DESC_SAP:
464 for (i = 0; i < maxd; i++) {
465 if ((ed[i].type == type) && (ed[i].data[0] == extProto1)) {
466 *protocol_family = ed[i].protocol_family;
467 return 0;
468 }
469 }
470 break;
471
472 case DLIL_DESC_SNAP:
473 for (i = 0; i < maxd; i++) {
474 if ((ed[i].type == type) && (ed[i].data[0] == extProto1) &&
475 (ed[i].data[1] == extProto2)) {
476 *protocol_family = ed[i].protocol_family;
477 return 0;
478 }
479 }
480 break;
481 }
482
483 return ENOENT;
484 }
485
486 /*
487 * Ethernet output routine.
488 * Encapsulate a packet of type family for the local net.
489 * Use trailer local net encapsulation if enough data in first
490 * packet leaves a multiple of 512 bytes of data in remainder.
491 */
492 int
493 ether_frameout(
494 struct ifnet *ifp,
495 struct mbuf **m,
496 const struct sockaddr *ndest,
497 const char *edst,
498 const char *ether_type)
499 {
500 struct ether_header *eh;
501 int hlen; /* link layer header length */
502
503 hlen = ETHER_HDR_LEN;
504
505 /*
506 * If a simplex interface, and the packet is being sent to our
507 * Ethernet address or a broadcast address, loopback a copy.
508 * XXX To make a simplex device behave exactly like a duplex
509 * device, we should copy in the case of sending to our own
510 * ethernet address (thus letting the original actually appear
511 * on the wire). However, we don't do that here for security
512 * reasons and compatibility with the original behavior.
513 */
514 if ((ifp->if_flags & IFF_SIMPLEX) &&
515 ((*m)->m_flags & M_LOOP)) {
516 if (lo_ifp) {
517 if ((*m)->m_flags & M_BCAST) {
518 struct mbuf *n = m_copy(*m, 0, (int)M_COPYALL);
519 if (n != NULL)
520 dlil_output(lo_ifp, ndest->sa_family, n, NULL, ndest, 0);
521 }
522 else {
523 if (_ether_cmp(edst, ifnet_lladdr(ifp)) == 0) {
524 dlil_output(lo_ifp, ndest->sa_family, *m, NULL, ndest, 0);
525 return EJUSTRETURN;
526 }
527 }
528 }
529 }
530
531 /*
532 * Add local net header. If no space in first mbuf,
533 * allocate another.
534 */
535 M_PREPEND(*m, sizeof (struct ether_header), M_DONTWAIT);
536 if (*m == 0) {
537 return (EJUSTRETURN);
538 }
539
540
541 eh = mtod(*m, struct ether_header *);
542 (void)memcpy(&eh->ether_type, ether_type,
543 sizeof(eh->ether_type));
544 (void)memcpy(eh->ether_dhost, edst, ETHER_ADDR_LEN);
545 ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, ETHER_ADDR_LEN);
546
547 return 0;
548 }
549
550 errno_t
551 ether_check_multi(
552 __unused ifnet_t ifp,
553 const struct sockaddr *proto_addr)
554 {
555 errno_t result = EAFNOSUPPORT;
556 const u_char *e_addr;
557
558 /*
559 * AF_SPEC and AF_LINK don't require translation. We do
560 * want to verify that they specify a valid multicast.
561 */
562 switch(proto_addr->sa_family) {
563 case AF_UNSPEC:
564 e_addr = (const u_char*)&proto_addr->sa_data[0];
565 if ((e_addr[0] & 0x01) != 0x01)
566 result = EADDRNOTAVAIL;
567 else
568 result = 0;
569 break;
570
571 case AF_LINK:
572 e_addr = CONST_LLADDR((const struct sockaddr_dl*)proto_addr);
573 if ((e_addr[0] & 0x01) != 0x01)
574 result = EADDRNOTAVAIL;
575 else
576 result = 0;
577 break;
578 }
579
580 return result;
581 }
582
583 int
584 ether_ioctl(
585 __unused ifnet_t ifp,
586 __unused u_int32_t command,
587 __unused void* data)
588 {
589 return EOPNOTSUPP;
590 }
591
592 __private_extern__ int ether_family_init(void)
593 {
594 errno_t error = 0;
595
596 /* Register protocol registration functions */
597 if ((error = proto_register_plumber(PF_INET, APPLE_IF_FAM_ETHERNET,
598 ether_attach_inet, ether_detach_inet)) != 0) {
599 printf("proto_register_plumber failed for PF_INET error=%d\n", error);
600 goto done;
601 }
602 #if INET6
603 if ((error = proto_register_plumber(PF_INET6, APPLE_IF_FAM_ETHERNET,
604 ether_attach_inet6, ether_detach_inet6)) != 0) {
605 printf("proto_register_plumber failed for PF_INET6 error=%d\n", error);
606 goto done;
607 }
608 #endif /* INET6 */
609 #if NETAT
610 if ((error = proto_register_plumber(PF_APPLETALK, APPLE_IF_FAM_ETHERNET,
611 ether_attach_at, ether_detach_at)) != 0) {
612 printf("proto_register_plumber failed PF_APPLETALK error=%d\n", error);
613 goto done;
614 }
615 #endif /* NETAT */
616 #if VLAN
617 vlan_family_init();
618 #endif /* VLAN */
619 #if BOND
620 bond_family_init();
621 #endif /* BOND */
622
623 done:
624
625 return (error);
626 }