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