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