]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_loop.c
xnu-123.5.tar.gz
[apple/xnu.git] / bsd / net / if_loop.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1982, 1986, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
55 */
56
57 /*
58 * Loopback interface driver for protocol testing and timing.
59 */
60 #include "loop.h"
61 #if NLOOP > 0
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/mbuf.h>
67 #include <sys/socket.h>
68 #include <sys/sockio.h>
69
70 #include <net/if.h>
71 #include <net/if_types.h>
72 #include <net/netisr.h>
73 #include <net/route.h>
74 #include <net/bpf.h>
75 #include <sys/malloc.h>
76
77 #if INET
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #endif
81
82 #if IPX
83 #include <netipx/ipx.h>
84 #include <netipx/ipx_if.h>
85 #endif
86
87 #if INET6
88 #ifndef INET
89 #include <netinet/in.h>
90 #endif
91 #include <netinet6/in6_var.h>
92 #include <netinet/ip6.h>
93 #endif
94
95 #if NS
96 #include <netns/ns.h>
97 #include <netns/ns_if.h>
98 #endif
99
100 #if ISO
101 #include <netiso/iso.h>
102 #include <netiso/iso_var.h>
103 #endif
104
105 #include <net/dlil.h>
106
107 #if NETAT
108 extern struct ifqueue atalkintrq;
109 #endif
110
111 #include "bpfilter.h"
112 #if NBPFILTER > 0
113 #include <net/bpfdesc.h>
114 #endif
115
116 #define NLOOP_ATTACHMENTS (NLOOP * 12)
117
118 struct lo_statics_str {
119 int bpf_mode;
120 int (*bpf_callback)(struct ifnet *, struct mbuf *);
121 };
122
123 static struct if_proto *lo_array[NLOOP_ATTACHMENTS];
124 static struct lo_statics_str lo_statics[NLOOP];
125 static lo_count = 0;
126
127
128 #ifdef TINY_LOMTU
129 #define LOMTU (1024+512)
130 #else
131 #define LOMTU 16384
132 #endif
133
134 struct ifnet loif[NLOOP];
135
136 void lo_reg_if_mods();
137
138
139
140
141 int lo_demux(ifp, m, frame_header, proto)
142 struct ifnet *ifp;
143 struct mbuf *m;
144 char *frame_header;
145 struct if_proto **proto;
146 {
147 int i;
148 struct if_proto **proto_ptr;
149
150 proto_ptr = mtod(m, struct if_proto **);
151 *proto = *proto_ptr;
152 m_adj(m, sizeof(u_long));
153 return 0;
154 }
155
156
157 int lo_framer(ifp, m, dest, dest_linkaddr, frame_type)
158 struct ifnet *ifp;
159 struct mbuf **m;
160 struct sockaddr *dest;
161 char *dest_linkaddr;
162 char *frame_type;
163
164 {
165 char *to_ptr;
166
167 M_PREPEND(*m, (4 * sizeof(u_long)), M_WAITOK);
168 to_ptr = mtod(*m, char *);
169 bcopy(dest_linkaddr, to_ptr, (4 * sizeof(u_long)));
170 return 0;
171 }
172
173 static
174 int lo_add_if(struct ifnet *ifp)
175 {
176 ifp->if_demux = lo_demux;
177 ifp->if_framer = lo_framer;
178 ifp->if_event = 0;
179 return 0;
180 }
181
182 static
183 int lo_del_if(struct ifnet *ifp)
184 {
185 return 0;
186 }
187
188
189
190
191 static
192 int lo_add_proto(struct ddesc_head_str *desc_head, struct if_proto *proto, u_long dl_tag)
193 {
194 int i;
195
196 for (i=0; i < lo_count; i++)
197 if (lo_array[i] == 0) {
198 lo_array[lo_count] = proto;
199 return 0;
200 }
201
202 if ((i == lo_count) && (lo_count == NLOOP_ATTACHMENTS))
203 panic("lo_add_proto -- Too many attachments\n");
204
205 lo_array[lo_count++] = proto;
206 return 0;
207 }
208
209
210 static
211 int lo_del_proto(struct if_proto *proto, u_long dl_tag)
212 {
213 int i;
214
215 for (i=0; i < lo_count; i++)
216 if (lo_array[i] == proto) {
217 lo_array[i] = 0;
218 return 0;
219 }
220
221 return ENOENT;
222 }
223
224 static int
225 lo_output(ifp, m)
226 struct ifnet *ifp;
227 register struct mbuf *m;
228 { u_int *prepend_ptr;
229 u_int af;
230 u_long saved_header[3];
231
232 if ((m->m_flags & M_PKTHDR) == 0)
233 panic("lo_output: no HDR");
234
235 /*
236 * Don't overwrite the rcvif field if it is in use.
237 * This is used to match multicast packets, sent looping
238 * back, with the appropriate group record on input.
239 */
240 if (m->m_pkthdr.rcvif == NULL)
241 m->m_pkthdr.rcvif = ifp;
242 prepend_ptr = mtod(m, u_int *);
243 af = *prepend_ptr;
244 m_adj(m, sizeof(u_int));
245
246
247 #if NBPFILTER > 0
248 if (lo_statics[ifp->if_unit].bpf_mode != BPF_TAP_DISABLE) {
249 struct mbuf m0, *n;
250
251 bcopy(mtod(m, caddr_t), &saved_header[0], (3 * sizeof(u_long)));
252 m_adj(m, (3 * sizeof(u_long)));
253
254 n = m;
255 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
256 /*
257 * We need to prepend the address family as
258 * a four byte field. Cons up a dummy header
259 * to pacify bpf. This is safe because bpf
260 * will only read from the mbuf (i.e., it won't
261 * try to free it or keep a pointer a to it).
262 */
263 m0.m_next = m;
264 m0.m_len = 4;
265 m0.m_data = (char *)&af;
266 n = &m0;
267 }
268
269 (*lo_statics[ifp->if_unit].bpf_callback)(ifp, n);
270
271 M_PREPEND(m, (3 * sizeof(u_long)), M_WAITOK);
272 bcopy(&saved_header[0], mtod(m, caddr_t), (3 * sizeof(u_long)));
273
274 }
275 #endif
276
277 ifp->if_ibytes += m->m_pkthdr.len;
278 ifp->if_obytes += m->m_pkthdr.len;
279
280 ifp->if_opackets++;
281 ifp->if_ipackets++;
282
283 /* WARNING
284 * This won't work for loopbacked multicast
285 */
286 m->m_pkthdr.header = mtod(m, char *);
287 m->m_pkthdr.aux = ifp; /* HACKERY */
288 return dlil_input(ifp, m, m);
289 }
290
291
292 /*
293 * This is a common pre-output route used by INET, AT, etc. This could
294 * (should?) be split into separate pre-output routines for each protocol.
295 */
296
297 static int
298 lo_pre_output(ifp, m, dst, route, frame_type, dst_addr, dl_tag)
299 struct ifnet *ifp;
300 register struct mbuf **m;
301 struct sockaddr *dst;
302 void *route;
303 char *frame_type;
304 char *dst_addr;
305 u_long dl_tag;
306
307 {
308 int s, isr;
309 register struct ifqueue *ifq = 0;
310 u_long *prepend_ptr;
311 register struct rtentry *rt = (struct rtentry *) route;
312
313 prepend_ptr = (u_long *) dst_addr;
314 if (((*m)->m_flags & M_PKTHDR) == 0)
315 panic("looutput no HDR");
316
317 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
318 if (rt->rt_flags & RTF_BLACKHOLE) {
319 m_freem(*m);
320 return EJUSTRETURN;
321 }
322 else
323 return ((rt->rt_flags & RTF_HOST) ? EHOSTUNREACH : ENETUNREACH);
324 }
325
326 switch (dst->sa_family) {
327 #if INET
328 case AF_INET:
329 ifq = &ipintrq;
330 isr = NETISR_IP;
331 break;
332 #endif
333 #if INET6
334 case AF_INET6:
335 ifq = &ip6intrq;
336 isr = NETISR_IPV6;
337 break;
338 #endif
339 #if IPX
340 case AF_IPX:
341 ifq = &ipxintrq;
342 isr = NETISR_IPX;
343 break;
344 #endif
345 #if NS
346 case AF_NS:
347 ifq = &nsintrq;
348 isr = NETISR_NS;
349 break;
350 #endif
351 #if ISO
352 case AF_ISO:
353 ifq = &clnlintrq;
354 isr = NETISR_ISO;
355 break;
356 #endif
357 #if NETAT
358 case AF_APPLETALK:
359 ifq = &atalkintrq;
360 isr = NETISR_APPLETALK;
361 break;
362 #endif NETAT
363 default:
364 return (EAFNOSUPPORT);
365 }
366
367 *prepend_ptr++ = dst->sa_family; /* For lo_output(BPF) */
368 *prepend_ptr++ = dlttoproto(dl_tag); /* For lo_demux */
369 *prepend_ptr++ = (u_long) ifq; /* For lo_input */
370 *prepend_ptr = isr; /* For lo_input */
371
372 return 0;
373 }
374
375
376
377
378 /*
379 * lo_input - This should work for all attached protocols that use the
380 * ifq/schednetisr input mechanism.
381 */
382
383
384 int
385 lo_input(m, fh, ifp, dl_tag, sync_ok)
386 register struct mbuf *m;
387 char *fh;
388 struct ifnet *ifp;
389 u_long dl_tag;
390 int sync_ok;
391
392 {
393 u_long *prepend_ptr;
394 int s, isr;
395 register struct ifqueue *ifq = 0;
396
397 prepend_ptr = mtod(m, u_long *);
398 ifq = (struct ifqueue *) *prepend_ptr++;
399 isr = *prepend_ptr;
400 m_adj(m, (2 * sizeof(u_long)));
401
402 s = splimp();
403 if (IF_QFULL(ifq)) {
404 IF_DROP(ifq);
405 m_freem(m);
406 splx(s);
407 return (EJUSTRETURN);
408 }
409
410 IF_ENQUEUE(ifq, m);
411 schednetisr(isr);
412 splx(s);
413 return (0);
414 }
415
416
417
418
419 /* ARGSUSED */
420 static void
421 lortrequest(cmd, rt, sa)
422 int cmd;
423 struct rtentry *rt;
424 struct sockaddr *sa;
425 {
426 if (rt) {
427 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
428 /*
429 * For optimal performance, the send and receive buffers
430 * should be at least twice the MTU plus a little more for
431 * overhead.
432 */
433 rt->rt_rmx.rmx_recvpipe =
434 rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
435 }
436 }
437
438 /*
439 * Process an ioctl request.
440 */
441 /* ARGSUSED */
442 static int
443 loioctl(dl_tag, ifp, cmd, data)
444 u_long dl_tag;
445 register struct ifnet *ifp;
446 u_long cmd;
447 void *data;
448 {
449 register struct ifaddr *ifa;
450 register struct ifreq *ifr = (struct ifreq *)data;
451 register int error = 0;
452
453 switch (cmd) {
454
455 case SIOCSIFADDR:
456 ifp->if_flags |= IFF_UP | IFF_RUNNING;
457 ifa = (struct ifaddr *)data;
458 ifa->ifa_rtrequest = lortrequest;
459 /*
460 * Everything else is done at a higher level.
461 */
462 break;
463
464 case SIOCADDMULTI:
465 case SIOCDELMULTI:
466 if (ifr == 0) {
467 error = EAFNOSUPPORT; /* XXX */
468 break;
469 }
470 switch (ifr->ifr_addr.sa_family) {
471
472 #if INET
473 case AF_INET:
474 break;
475 #endif
476 #if INET6
477 case AF_INET6:
478 break;
479 #endif
480
481 default:
482 error = EAFNOSUPPORT;
483 break;
484 }
485 break;
486
487 case SIOCSIFMTU:
488 ifp->if_mtu = ifr->ifr_mtu;
489 break;
490
491 case SIOCSIFFLAGS:
492 break;
493
494 default:
495 error = EOPNOTSUPP;
496 }
497 return (error);
498 }
499 #endif /* NLOOP > 0 */
500
501
502 int lo_shutdown()
503 {
504 return 0;
505 }
506
507
508 void lo_reg_if_mods()
509 {
510 struct dlil_ifmod_reg_str lo_ifmod;
511
512 lo_ifmod.add_if = lo_add_if;
513 lo_ifmod.del_if = lo_del_if;
514 lo_ifmod.add_proto = lo_add_proto;
515 lo_ifmod.del_proto = lo_del_proto;
516 lo_ifmod.ifmod_ioctl = 0;
517 lo_ifmod.shutdown = lo_shutdown;
518
519 if (dlil_reg_if_modules(APPLE_IF_FAM_LOOPBACK, &lo_ifmod))
520 panic("Couldn't register lo modules\n");
521 }
522
523
524 u_long lo_attach_inet(struct ifnet *ifp)
525 {
526 struct dlil_proto_reg_str reg;
527 struct dlil_demux_desc desc;
528 u_long dl_tag=0;
529 short native=0;
530 int stat;
531 int i;
532
533 for (i=0; i < lo_count; i++) {
534 if ((lo_array[i]) && (lo_array[i]->ifp == ifp)) {
535 if (lo_array[i]->protocol_family == PF_INET)
536 return lo_array[i]->dl_tag;
537 }
538 }
539
540 TAILQ_INIT(&reg.demux_desc_head);
541 desc.type = DLIL_DESC_RAW;
542 desc.variants.bitmask.proto_id_length = 0;
543 desc.variants.bitmask.proto_id = 0;
544 desc.variants.bitmask.proto_id_mask = 0;
545 desc.native_type = (char *) &native;
546 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
547 reg.interface_family = ifp->if_family;
548 reg.unit_number = ifp->if_unit;
549 reg.input = lo_input;
550 reg.pre_output = lo_pre_output;
551 reg.event = 0;
552 reg.offer = 0;
553 reg.ioctl = loioctl;
554 reg.default_proto = 0;
555 reg.protocol_family = PF_INET;
556
557 stat = dlil_attach_protocol(&reg, &dl_tag);
558 if (stat) {
559 panic("lo_attach_inet can't attach interface\n");
560 }
561
562 return dl_tag;
563 }
564
565
566 int lo_set_bpf_tap(struct ifnet *ifp, int mode, int (*bpf_callback)(struct ifnet *, struct mbuf *))
567 {
568
569 /*
570 * NEED MUTEX HERE XXX
571 */
572 if (mode == BPF_TAP_DISABLE) {
573 lo_statics[ifp->if_unit].bpf_mode = mode;
574 lo_statics[ifp->if_unit].bpf_callback = bpf_callback;
575 }
576 else {
577 lo_statics[ifp->if_unit].bpf_callback = bpf_callback;
578 lo_statics[ifp->if_unit].bpf_mode = mode;
579 }
580
581 return 0;
582 }
583
584
585 /* ARGSUSED */
586 void
587 loopattach(dummy)
588 void *dummy;
589 {
590 register struct ifnet *ifp;
591 register int i = 0;
592
593 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
594 lo_reg_if_mods();
595
596 for (ifp = loif; i < NLOOP; ifp++) {
597 lo_statics[i].bpf_callback = 0;
598 lo_statics[i].bpf_mode = BPF_TAP_DISABLE;
599 ifp->if_name = "lo";
600 ifp->if_family = APPLE_IF_FAM_LOOPBACK;
601 ifp->if_unit = i++;
602 ifp->if_mtu = LOMTU;
603 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
604 ifp->if_ioctl = 0;
605 ifp->if_set_bpf_tap = lo_set_bpf_tap;
606 ifp->if_output = lo_output;
607 ifp->if_type = IFT_LOOP;
608 dlil_if_attach(ifp);
609 #if NBPFILTER > 0
610 bpfattach(ifp, DLT_NULL, sizeof(u_int));
611 #endif
612 }
613 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
614 }