|
|
1.1 root 1: /*
2: * Mach Operating System
3: * Copyright (c) 1993-1989 Carnegie Mellon University
4: * All Rights Reserved.
5: *
6: * Permission to use, copy, modify and distribute this software and its
7: * documentation is hereby granted, provided that both the copyright
8: * notice and this permission notice appear in all copies of the
9: * software, derivative works or modified versions, and any portions
10: * thereof, and that both notices appear in supporting documentation.
11: *
12: * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13: * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14: * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15: *
16: * Carnegie Mellon requests users of this software to return to
17: *
18: * Software Distribution Coordinator or [email protected]
19: * School of Computer Science
20: * Carnegie Mellon University
21: * Pittsburgh PA 15213-3890
22: *
23: * any improvements or extensions that they make and grant Carnegie Mellon
24: * the rights to redistribute these changes.
25: */
26: /*
27: * Author: David B. Golub, Carnegie Mellon University
28: * Date: 3/98
29: *
30: * Network IO.
31: *
32: * Packet filter code taken from vaxif/enet.c written
33: * CMU and Stanford.
34: */
35:
36: /*
37: * Note: don't depend on anything in this file.
38: * It may change a lot real soon. -cmaeda 11 June 1993
39: */
40:
41: #include <sys/types.h>
1.1.1.3 ! root 42: #include <string.h>
! 43:
1.1 root 44: #include <device/net_status.h>
45: #include <machine/machspl.h> /* spl definitions */
46: #include <device/net_io.h>
47: #include <device/if_hdr.h>
48: #include <device/io_req.h>
49: #include <device/ds_routines.h>
50:
51: #include <mach/boolean.h>
52: #include <mach/vm_param.h>
53:
54: #include <ipc/ipc_port.h>
55: #include <ipc/ipc_kmsg.h>
56: #include <ipc/ipc_mqueue.h>
57:
58: #include <kern/counters.h>
1.1.1.3 ! root 59: #include <kern/debug.h>
1.1 root 60: #include <kern/lock.h>
1.1.1.3 ! root 61: #include <kern/printf.h>
1.1 root 62: #include <kern/queue.h>
63: #include <kern/sched_prim.h>
1.1.1.3 ! root 64: #include <kern/slab.h>
1.1 root 65: #include <kern/thread.h>
66:
67: #include <machine/machspl.h>
68:
69: #if MACH_TTD
70: #include <ttd/ttd_stub.h>
71: #endif /* MACH_TTD */
72:
73: #if MACH_TTD
74: int kttd_async_counter= 0;
75: #endif /* MACH_TTD */
76:
77:
78: /*
79: * Packet Buffer Management
80: *
81: * This module manages a private pool of kmsg buffers.
82: */
83:
84: /*
85: * List of net kmsgs queued to be sent to users.
86: * Messages can be high priority or low priority.
87: * The network thread processes high priority messages first.
88: */
89: decl_simple_lock_data(,net_queue_lock)
90: boolean_t net_thread_awake = FALSE;
91: struct ipc_kmsg_queue net_queue_high;
92: int net_queue_high_size = 0;
93: int net_queue_high_max = 0; /* for debugging */
94: struct ipc_kmsg_queue net_queue_low;
95: int net_queue_low_size = 0;
96: int net_queue_low_max = 0; /* for debugging */
97:
98: /*
99: * List of net kmsgs that can be touched at interrupt level.
100: * If it is empty, we will also steal low priority messages.
101: */
102: decl_simple_lock_data(,net_queue_free_lock)
103: struct ipc_kmsg_queue net_queue_free;
104: int net_queue_free_size = 0; /* on free list */
105: int net_queue_free_max = 0; /* for debugging */
106:
107: /*
108: * This value is critical to network performance.
109: * At least this many buffers should be sitting in net_queue_free.
110: * If this is set too small, we will drop network packets.
111: * Even a low drop rate (<1%) can cause severe network throughput problems.
112: * We add one to net_queue_free_min for every filter.
113: */
114: int net_queue_free_min = 3;
115:
116: int net_queue_free_hits = 0; /* for debugging */
117: int net_queue_free_steals = 0; /* for debugging */
118: int net_queue_free_misses = 0; /* for debugging */
119:
120: int net_kmsg_send_high_hits = 0; /* for debugging */
121: int net_kmsg_send_low_hits = 0; /* for debugging */
122: int net_kmsg_send_high_misses = 0; /* for debugging */
123: int net_kmsg_send_low_misses = 0; /* for debugging */
124:
125: int net_thread_awaken = 0; /* for debugging */
126: int net_ast_taken = 0; /* for debugging */
127:
128: decl_simple_lock_data(,net_kmsg_total_lock)
129: int net_kmsg_total = 0; /* total allocated */
130: int net_kmsg_max; /* initialized below */
131:
132: vm_size_t net_kmsg_size; /* initialized below */
133:
134: /*
135: * We want more buffers when there aren't enough in the free queue
136: * and the low priority queue. However, we don't want to allocate
137: * more than net_kmsg_max.
138: */
139:
140: #define net_kmsg_want_more() \
141: (((net_queue_free_size + net_queue_low_size) < net_queue_free_min) && \
142: (net_kmsg_total < net_kmsg_max))
143:
144: ipc_kmsg_t
145: net_kmsg_get(void)
146: {
147: register ipc_kmsg_t kmsg;
148: spl_t s;
149:
150: /*
151: * First check the list of free buffers.
152: */
153: s = splimp();
154: simple_lock(&net_queue_free_lock);
155: kmsg = ipc_kmsg_queue_first(&net_queue_free);
156: if (kmsg != IKM_NULL) {
157: ipc_kmsg_rmqueue_first_macro(&net_queue_free, kmsg);
158: net_queue_free_size--;
159: net_queue_free_hits++;
160: }
161: simple_unlock(&net_queue_free_lock);
162:
163: if (kmsg == IKM_NULL) {
164: /*
165: * Try to steal from the low priority queue.
166: */
167: simple_lock(&net_queue_lock);
168: kmsg = ipc_kmsg_queue_first(&net_queue_low);
169: if (kmsg != IKM_NULL) {
170: ipc_kmsg_rmqueue_first_macro(&net_queue_low, kmsg);
171: net_queue_low_size--;
172: net_queue_free_steals++;
173: }
174: simple_unlock(&net_queue_lock);
175: }
176:
177: if (kmsg == IKM_NULL)
178: net_queue_free_misses++;
179: (void) splx(s);
180:
181: if (net_kmsg_want_more() || (kmsg == IKM_NULL)) {
182: boolean_t awake;
183:
184: s = splimp();
185: simple_lock(&net_queue_lock);
186: awake = net_thread_awake;
187: net_thread_awake = TRUE;
188: simple_unlock(&net_queue_lock);
189: (void) splx(s);
190:
191: if (!awake)
192: thread_wakeup((event_t) &net_thread_awake);
193: }
194:
195: return kmsg;
196: }
197:
198: void
199: net_kmsg_put(register ipc_kmsg_t kmsg)
200: {
201: spl_t s;
202:
203: s = splimp();
204: simple_lock(&net_queue_free_lock);
205: ipc_kmsg_enqueue_macro(&net_queue_free, kmsg);
206: if (++net_queue_free_size > net_queue_free_max)
207: net_queue_free_max = net_queue_free_size;
208: simple_unlock(&net_queue_free_lock);
209: (void) splx(s);
210: }
211:
212: void
213: net_kmsg_collect(void)
214: {
215: register ipc_kmsg_t kmsg;
216: spl_t s;
217:
218: s = splimp();
219: simple_lock(&net_queue_free_lock);
220: while (net_queue_free_size > net_queue_free_min) {
221: kmsg = ipc_kmsg_dequeue(&net_queue_free);
222: net_queue_free_size--;
223: simple_unlock(&net_queue_free_lock);
224: (void) splx(s);
225:
226: net_kmsg_free(kmsg);
227: simple_lock(&net_kmsg_total_lock);
228: net_kmsg_total--;
229: simple_unlock(&net_kmsg_total_lock);
230:
231: s = splimp();
232: simple_lock(&net_queue_free_lock);
233: }
234: simple_unlock(&net_queue_free_lock);
235: (void) splx(s);
236: }
237:
238: void
239: net_kmsg_more(void)
240: {
241: register ipc_kmsg_t kmsg;
242:
243: /*
244: * Replenish net kmsg pool if low. We don't have the locks
245: * necessary to look at these variables, but that's OK because
246: * misread values aren't critical. The danger in this code is
247: * that while we allocate buffers, interrupts are happening
248: * which take buffers out of the free list. If we are not
249: * careful, we will sit in the loop and allocate a zillion
250: * buffers while a burst of packets arrives. So we count
251: * buffers in the low priority queue as available, because
252: * net_kmsg_get will make use of them, and we cap the total
253: * number of buffers we are willing to allocate.
254: */
255:
256: while (net_kmsg_want_more()) {
257: simple_lock(&net_kmsg_total_lock);
258: net_kmsg_total++;
259: simple_unlock(&net_kmsg_total_lock);
260: kmsg = net_kmsg_alloc();
261: net_kmsg_put(kmsg);
262: }
263: }
264:
265: /*
266: * Packet Filter Data Structures
267: *
268: * Each network interface has a set of packet filters
269: * that are run on incoming packets.
270: *
271: * Each packet filter may represent a single network
272: * session or multiple network sessions. For example,
273: * all application level TCP sessions would be represented
274: * by a single packet filter data structure.
275: *
276: * If a packet filter has a single session, we use a
277: * struct net_rcv_port to represent it. If the packet
278: * filter represents multiple sessions, we use a
279: * struct net_hash_header to represent it.
280: */
281:
282: /*
283: * Each interface has a write port and a set of read ports.
284: * Each read port has one or more filters to determine what packets
285: * should go to that port.
286: */
287:
288: /*
289: * Receive port for net, with packet filter.
290: * This data structure by itself represents a packet
291: * filter for a single session.
292: */
293: struct net_rcv_port {
1.1.1.3 ! root 294: queue_chain_t input; /* list of input open_descriptors */
! 295: queue_chain_t output; /* list of output open_descriptors */
1.1 root 296: ipc_port_t rcv_port; /* port to send packet to */
297: int rcv_qlimit; /* port's qlimit */
298: int rcv_count; /* number of packets received */
299: int priority; /* priority for filter */
300: filter_t *filter_end; /* pointer to end of filter */
301: filter_t filter[NET_MAX_FILTER];
302: /* filter operations */
303: };
304: typedef struct net_rcv_port *net_rcv_port_t;
305:
1.1.1.3 ! root 306: struct kmem_cache net_rcv_cache; /* cache of net_rcv_port structs */
1.1 root 307:
308:
309: #define NET_HASH_SIZE 256
310: #define N_NET_HASH 4
311: #define N_NET_HASH_KEYS 4
312:
313: unsigned int bpf_hash (int, unsigned int *);
314:
315: /*
316: * A single hash entry.
317: */
318: struct net_hash_entry {
319: queue_chain_t chain; /* list of entries with same hval */
320: #define he_next chain.next
321: #define he_prev chain.prev
322: ipc_port_t rcv_port; /* destination port */
323: int rcv_qlimit; /* qlimit for the port */
324: unsigned int keys[N_NET_HASH_KEYS];
325: };
326: typedef struct net_hash_entry *net_hash_entry_t;
327:
1.1.1.3 ! root 328: struct kmem_cache net_hash_entry_cache;
1.1 root 329:
330: /*
331: * This structure represents a packet filter with multiple sessions.
332: *
333: * For example, all application level TCP sessions might be
334: * represented by one of these structures. It looks like a
335: * net_rcv_port struct so that both types can live on the
336: * same packet filter queues.
337: */
338: struct net_hash_header {
339: struct net_rcv_port rcv;
340: int n_keys; /* zero if not used */
341: int ref_count; /* reference count */
342: net_hash_entry_t table[NET_HASH_SIZE];
343: } filter_hash_header[N_NET_HASH];
344:
345: typedef struct net_hash_header *net_hash_header_t;
346:
347: decl_simple_lock_data(,net_hash_header_lock)
348:
349: #define HASH_ITERATE(head, elt) (elt) = (net_hash_entry_t) (head); do {
350: #define HASH_ITERATE_END(head, elt) \
351: (elt) = (net_hash_entry_t) queue_next((queue_entry_t) (elt)); \
352: } while ((elt) != (head));
353:
354:
1.1.1.3 ! root 355: #define FILTER_ITERATE(if_port_list, fp, nextfp, chain) \
! 356: for ((fp) = (net_rcv_port_t) queue_first(if_port_list); \
! 357: !queue_end(if_port_list, (queue_entry_t)(fp)); \
! 358: (fp) = (nextfp)) { \
! 359: (nextfp) = (net_rcv_port_t) queue_next(chain);
1.1 root 360: #define FILTER_ITERATE_END }
361:
362: /* entry_p must be net_rcv_port_t or net_hash_entry_t */
1.1.1.3 ! root 363: #define ENQUEUE_DEAD(dead, entry_p, chain) { \
1.1 root 364: queue_next(&(entry_p)->chain) = (queue_entry_t) (dead); \
365: (dead) = (queue_entry_t)(entry_p); \
366: }
367:
368: extern boolean_t net_do_filter(); /* CSPF */
369: extern int bpf_do_filter(); /* BPF */
370:
1.1.1.3 ! root 371: int hash_ent_remove (
! 372: struct ifnet *ifp,
! 373: net_hash_header_t hp,
! 374: int used,
! 375: net_hash_entry_t *head,
! 376: net_hash_entry_t entp,
! 377: queue_entry_t *dead_p);
! 378:
! 379: void net_free_dead_infp (queue_entry_t dead_infp);
! 380: void net_free_dead_entp (queue_entry_t dead_entp);
! 381:
! 382: int bpf_validate(
! 383: bpf_insn_t f,
! 384: int bytes,
! 385: bpf_insn_t *match);
! 386:
! 387: int bpf_eq (
! 388: bpf_insn_t f1,
! 389: bpf_insn_t f2,
! 390: register int bytes);
! 391:
! 392: int net_add_q_info (ipc_port_t rcv_port);
! 393:
! 394: int bpf_match (
! 395: net_hash_header_t hash,
! 396: int n_keys,
! 397: unsigned int *keys,
! 398: net_hash_entry_t **hash_headpp,
! 399: net_hash_entry_t *entpp);
! 400:
1.1 root 401:
402: /*
403: * ethernet_priority:
404: *
405: * This function properly belongs in the ethernet interfaces;
406: * it should not be called by this module. (We get packet
407: * priorities as an argument to net_filter.) It is here
408: * to avoid massive code duplication.
409: *
410: * Returns TRUE for high-priority packets.
411: */
412:
413: boolean_t ethernet_priority(kmsg)
414: ipc_kmsg_t kmsg;
415: {
416: register unsigned char *addr =
417: (unsigned char *) net_kmsg(kmsg)->header;
418:
419: /*
420: * A simplistic check for broadcast packets.
421: */
422:
423: if ((addr[0] == 0xff) && (addr[1] == 0xff) &&
424: (addr[2] == 0xff) && (addr[3] == 0xff) &&
425: (addr[4] == 0xff) && (addr[5] == 0xff))
426: return FALSE;
427: else
428: return TRUE;
429: }
430:
431: mach_msg_type_t header_type = {
432: MACH_MSG_TYPE_BYTE,
433: 8,
434: NET_HDW_HDR_MAX,
435: TRUE,
436: FALSE,
437: FALSE,
438: 0
439: };
440:
441: mach_msg_type_t packet_type = {
442: MACH_MSG_TYPE_BYTE, /* name */
443: 8, /* size */
444: 0, /* number */
445: TRUE, /* inline */
446: FALSE, /* longform */
447: FALSE /* deallocate */
448: };
449:
450: /*
451: * net_deliver:
452: *
453: * Called and returns holding net_queue_lock, at splimp.
454: * Dequeues a message and delivers it at spl0.
455: * Returns FALSE if no messages.
456: */
457: boolean_t net_deliver(nonblocking)
458: boolean_t nonblocking;
459: {
460: register ipc_kmsg_t kmsg;
461: boolean_t high_priority;
462: struct ipc_kmsg_queue send_list;
463:
464: /*
465: * Pick up a pending network message and deliver it.
466: * Deliver high priority messages before low priority.
467: */
468:
469: if ((kmsg = ipc_kmsg_dequeue(&net_queue_high)) != IKM_NULL) {
470: net_queue_high_size--;
471: high_priority = TRUE;
472: } else if ((kmsg = ipc_kmsg_dequeue(&net_queue_low)) != IKM_NULL) {
473: net_queue_low_size--;
474: high_priority = FALSE;
475: } else
476: return FALSE;
477: simple_unlock(&net_queue_lock);
478: (void) spl0();
479:
480: /*
481: * Run the packet through the filters,
482: * getting back a queue of packets to send.
483: */
484: net_filter(kmsg, &send_list);
485:
486: if (!nonblocking) {
487: /*
488: * There is a danger of running out of available buffers
489: * because they all get moved into the high priority queue
490: * or a port queue. In particular, we might need to
491: * allocate more buffers as we pull (previously available)
492: * buffers out of the low priority queue. But we can only
493: * allocate if we are allowed to block.
494: */
495: net_kmsg_more();
496: }
497:
498: while ((kmsg = ipc_kmsg_dequeue(&send_list)) != IKM_NULL) {
499: int count;
500:
501: /*
502: * Fill in the rest of the kmsg.
503: */
504: count = net_kmsg(kmsg)->net_rcv_msg_packet_count;
505:
506: ikm_init_special(kmsg, IKM_SIZE_NETWORK);
507:
508: kmsg->ikm_header.msgh_bits =
509: MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0);
510: /* remember message sizes must be rounded up */
511: kmsg->ikm_header.msgh_size =
1.1.1.3 ! root 512: (((mach_msg_size_t) (sizeof(struct net_rcv_msg)
! 513: - NET_RCV_MAX + count)) + 3) &~ 3;
1.1 root 514: kmsg->ikm_header.msgh_local_port = MACH_PORT_NULL;
515: kmsg->ikm_header.msgh_kind = MACH_MSGH_KIND_NORMAL;
516: kmsg->ikm_header.msgh_id = NET_RCV_MSG_ID;
517:
518: net_kmsg(kmsg)->header_type = header_type;
519: net_kmsg(kmsg)->packet_type = packet_type;
520: net_kmsg(kmsg)->net_rcv_msg_packet_count = count;
521:
522: /*
523: * Send the packet to the destination port. Drop it
524: * if the destination port is over its backlog.
525: */
526:
527: if (ipc_mqueue_send(kmsg, MACH_SEND_TIMEOUT, 0) ==
528: MACH_MSG_SUCCESS) {
529: if (high_priority)
530: net_kmsg_send_high_hits++;
531: else
532: net_kmsg_send_low_hits++;
533: /* the receiver is responsible for the message now */
534: } else {
535: if (high_priority)
536: net_kmsg_send_high_misses++;
537: else
538: net_kmsg_send_low_misses++;
539: ipc_kmsg_destroy(kmsg);
540: }
541: }
542:
543: (void) splimp();
544: simple_lock(&net_queue_lock);
545: return TRUE;
546: }
547:
548: /*
549: * We want to deliver packets using ASTs, so we can avoid the
550: * thread_wakeup/thread_block needed to get to the network
551: * thread. However, we can't allocate memory in the AST handler,
552: * because memory allocation might block. Hence we have the
553: * network thread to allocate memory. The network thread also
554: * delivers packets, so it can be allocating and delivering for a
555: * burst. net_thread_awake is protected by net_queue_lock
556: * (instead of net_queue_free_lock) so that net_packet and
557: * net_ast can safely determine if the network thread is running.
558: * This prevents a race that might leave a packet sitting without
559: * being delivered. It is possible for net_kmsg_get to think
560: * the network thread is awake, and so avoid a wakeup, and then
561: * have the network thread sleep without allocating. The next
562: * net_kmsg_get will do a wakeup.
563: */
564:
565: void net_ast()
566: {
567: spl_t s;
568:
569: net_ast_taken++;
570:
571: /*
572: * If the network thread is awake, then we would
573: * rather deliver messages from it, because
574: * it can also allocate memory.
575: */
576:
577: s = splimp();
578: simple_lock(&net_queue_lock);
579: while (!net_thread_awake && net_deliver(TRUE))
580: continue;
581:
582: /*
583: * Prevent an unnecessary AST. Either the network
584: * thread will deliver the messages, or there are
585: * no messages left to deliver.
586: */
587:
588: simple_unlock(&net_queue_lock);
589: (void) splsched();
590: ast_off(cpu_number(), AST_NETWORK);
591: (void) splx(s);
592: }
593:
594: void net_thread_continue()
595: {
596: for (;;) {
597: spl_t s;
598:
599: net_thread_awaken++;
600:
601: /*
602: * First get more buffers.
603: */
604: net_kmsg_more();
605:
606: s = splimp();
607: simple_lock(&net_queue_lock);
608: while (net_deliver(FALSE))
609: continue;
610:
611: net_thread_awake = FALSE;
612: assert_wait(&net_thread_awake, FALSE);
613: simple_unlock(&net_queue_lock);
614: (void) splx(s);
615: counter(c_net_thread_block++);
616: thread_block(net_thread_continue);
617: }
618: }
619:
620: void net_thread()
621: {
622: spl_t s;
623:
624: /*
625: * We should be very high priority.
626: */
627:
628: thread_set_own_priority(0);
629:
630: /*
631: * We sleep initially, so that we don't allocate any buffers
632: * unless the network is really in use and they are needed.
633: */
634:
635: s = splimp();
636: simple_lock(&net_queue_lock);
637: net_thread_awake = FALSE;
638: assert_wait(&net_thread_awake, FALSE);
639: simple_unlock(&net_queue_lock);
640: (void) splx(s);
641: counter(c_net_thread_block++);
642: thread_block(net_thread_continue);
643: net_thread_continue();
644: /*NOTREACHED*/
645: }
646:
647: void
648: reorder_queue(first, last)
649: register queue_t first, last;
650: {
651: register queue_entry_t prev, next;
652:
653: prev = first->prev;
654: next = last->next;
655:
656: prev->next = last;
657: next->prev = first;
658:
659: last->prev = prev;
660: last->next = first;
661:
662: first->next = next;
663: first->prev = last;
664: }
665:
666: /*
667: * Incoming packet. Header has already been moved to proper place.
668: * We are already at splimp.
669: */
670: void
671: net_packet(ifp, kmsg, count, priority)
672: register struct ifnet *ifp;
673: register ipc_kmsg_t kmsg;
674: unsigned int count;
675: boolean_t priority;
676: {
677: boolean_t awake;
678:
679: #if MACH_TTD
680: /*
681: * Do a quick check to see if it is a kernel TTD packet.
682: *
683: * Only check if KernelTTD is enabled, ie. the current
684: * device driver supports TTD, and the bootp succeded.
685: */
686: if (kttd_enabled && kttd_handle_async(kmsg)) {
687: /*
688: * Packet was a valid ttd packet and
689: * doesn't need to be passed up to filter.
690: * The ttd code put the used kmsg buffer
691: * back onto the free list.
692: */
693: if (kttd_debug)
694: printf("**%x**", kttd_async_counter++);
695: return;
696: }
697: #endif /* MACH_TTD */
698:
699: kmsg->ikm_header.msgh_remote_port = (mach_port_t) ifp;
700: net_kmsg(kmsg)->net_rcv_msg_packet_count = count;
701:
702: simple_lock(&net_queue_lock);
703: if (priority) {
704: ipc_kmsg_enqueue(&net_queue_high, kmsg);
705: if (++net_queue_high_size > net_queue_high_max)
706: net_queue_high_max = net_queue_high_size;
707: } else {
708: ipc_kmsg_enqueue(&net_queue_low, kmsg);
709: if (++net_queue_low_size > net_queue_low_max)
710: net_queue_low_max = net_queue_low_size;
711: }
712: /*
713: * If the network thread is awake, then we don't
714: * need to take an AST, because the thread will
715: * deliver the packet.
716: */
717: awake = net_thread_awake;
718: simple_unlock(&net_queue_lock);
719:
720: if (!awake) {
721: spl_t s = splsched();
722: ast_on(cpu_number(), AST_NETWORK);
723: (void) splx(s);
724: }
725: }
726:
727: int net_filter_queue_reorder = 0; /* non-zero to enable reordering */
728:
729: /*
730: * Run a packet through the filters, returning a list of messages.
731: * We are *not* called at interrupt level.
732: */
733: void
734: net_filter(kmsg, send_list)
735: register ipc_kmsg_t kmsg;
736: ipc_kmsg_queue_t send_list;
737: {
738: register struct ifnet *ifp;
739: register net_rcv_port_t infp, nextfp;
740: register ipc_kmsg_t new_kmsg;
741:
742: net_hash_entry_t entp, *hash_headp;
743: ipc_port_t dest;
744: queue_entry_t dead_infp = (queue_entry_t) 0;
745: queue_entry_t dead_entp = (queue_entry_t) 0;
746: unsigned int ret_count;
747:
1.1.1.3 ! root 748: queue_head_t *if_port_list;
! 749:
1.1 root 750: int count = net_kmsg(kmsg)->net_rcv_msg_packet_count;
751: ifp = (struct ifnet *) kmsg->ikm_header.msgh_remote_port;
752: ipc_kmsg_queue_init(send_list);
753:
1.1.1.3 ! root 754: if (net_kmsg(kmsg)->sent)
! 755: if_port_list = &ifp->if_snd_port_list;
! 756: else
! 757: if_port_list = &ifp->if_rcv_port_list;
! 758:
1.1 root 759: /*
760: * Unfortunately we can't allocate or deallocate memory
1.1.1.3 ! root 761: * while holding these locks. And we can't drop the locks
! 762: * while examining the filter lists.
! 763: * Both locks are hold in case a filter is removed from both
! 764: * queues.
1.1 root 765: */
766: simple_lock(&ifp->if_rcv_port_list_lock);
1.1.1.3 ! root 767: simple_lock(&ifp->if_snd_port_list_lock);
! 768: FILTER_ITERATE(if_port_list, infp, nextfp,
! 769: net_kmsg(kmsg)->sent ? &infp->output : &infp->input)
! 770: {
1.1 root 771: entp = (net_hash_entry_t) 0;
1.1.1.3 ! root 772: if ((infp->filter[0] & NETF_TYPE_MASK) == NETF_BPF) {
! 773: ret_count = bpf_do_filter(infp, net_kmsg(kmsg)->packet
! 774: + sizeof(struct packet_header),
! 775: count - sizeof(struct packet_header),
! 776: net_kmsg(kmsg)->header,
! 777: ifp->if_header_size, &hash_headp,
! 778: &entp);
1.1 root 779: if (entp == (net_hash_entry_t) 0)
780: dest = infp->rcv_port;
781: else
782: dest = entp->rcv_port;
1.1.1.3 ! root 783: if (ret_count)
! 784: ret_count += sizeof(struct packet_header);
1.1 root 785: } else {
786: ret_count = net_do_filter(infp, net_kmsg(kmsg)->packet, count,
787: net_kmsg(kmsg)->header);
788: if (ret_count)
789: ret_count = count;
790: dest = infp->rcv_port;
791: }
792:
793: if (ret_count) {
794:
795: /*
796: * Make a send right for the destination.
797: */
798:
799: dest = ipc_port_copy_send(dest);
800: if (!IP_VALID(dest)) {
801: /*
802: * This filter is dead. We remove it from the
803: * filter list and set it aside for deallocation.
804: */
805:
806: if (entp == (net_hash_entry_t) 0) {
1.1.1.3 ! root 807: if (infp->filter[0] & NETF_IN)
! 808: queue_remove(&ifp->if_rcv_port_list, infp,
! 809: net_rcv_port_t, input);
! 810: if (infp->filter[0] & NETF_OUT)
! 811: queue_remove(&ifp->if_snd_port_list, infp,
! 812: net_rcv_port_t, output);
! 813:
! 814: /* Use input only for queues of dead filters. */
! 815: ENQUEUE_DEAD(dead_infp, infp, input);
1.1 root 816: continue;
817: } else {
818: hash_ent_remove (ifp,
819: (net_hash_header_t)infp,
820: FALSE, /* no longer used */
821: hash_headp,
822: entp,
823: &dead_entp);
824: continue;
825: }
826: }
827:
828: /*
829: * Deliver copy of packet to this channel.
830: */
831: if (ipc_kmsg_queue_empty(send_list)) {
832: /*
833: * Only receiver, so far
834: */
835: new_kmsg = kmsg;
836: } else {
837: /*
838: * Other receivers - must allocate message and copy.
839: */
840: new_kmsg = net_kmsg_get();
841: if (new_kmsg == IKM_NULL) {
842: ipc_port_release_send(dest);
843: break;
844: }
845:
1.1.1.3 ! root 846: memcpy(
1.1 root 847: net_kmsg(new_kmsg)->packet,
1.1.1.3 ! root 848: net_kmsg(kmsg)->packet,
1.1 root 849: ret_count);
1.1.1.3 ! root 850: memcpy(
1.1 root 851: net_kmsg(new_kmsg)->header,
1.1.1.3 ! root 852: net_kmsg(kmsg)->header,
1.1 root 853: NET_HDW_HDR_MAX);
854: }
855: net_kmsg(new_kmsg)->net_rcv_msg_packet_count = ret_count;
856: new_kmsg->ikm_header.msgh_remote_port = (mach_port_t) dest;
857: ipc_kmsg_enqueue(send_list, new_kmsg);
858:
859: {
860: register net_rcv_port_t prevfp;
861: int rcount = ++infp->rcv_count;
862:
863: /*
864: * See if ordering of filters is wrong
865: */
866: if (infp->priority >= NET_HI_PRI) {
1.1.1.3 ! root 867: #define REORDER_PRIO(chain) \
! 868: prevfp = (net_rcv_port_t) queue_prev(&infp->chain); \
! 869: /* \
! 870: * If infp is not the first element on the queue, \
! 871: * and the previous element is at equal priority \
! 872: * but has a lower count, then promote infp to \
! 873: * be in front of prevfp. \
! 874: */ \
! 875: if ((queue_t)prevfp != if_port_list && \
! 876: infp->priority == prevfp->priority) { \
! 877: /* \
! 878: * Threshold difference to prevent thrashing \
! 879: */ \
! 880: if (net_filter_queue_reorder \
! 881: && (100 + prevfp->rcv_count < rcount)) \
! 882: reorder_queue(&prevfp->chain, &infp->chain);\
1.1 root 883: }
1.1.1.3 ! root 884:
! 885: REORDER_PRIO(input);
! 886: REORDER_PRIO(output);
! 887:
1.1 root 888: /*
889: * High-priority filter -> no more deliveries
890: */
891: break;
892: }
893: }
894: }
895: }
896: FILTER_ITERATE_END
1.1.1.3 ! root 897: simple_unlock(&ifp->if_snd_port_list_lock);
1.1 root 898: simple_unlock(&ifp->if_rcv_port_list_lock);
899:
900: /*
901: * Deallocate dead filters.
902: */
903: if (dead_infp != 0)
904: net_free_dead_infp(dead_infp);
905: if (dead_entp != 0)
906: net_free_dead_entp(dead_entp);
907:
908: if (ipc_kmsg_queue_empty(send_list)) {
909: /* Not sent - recycle */
910: net_kmsg_put(kmsg);
911: }
912: }
913:
914: boolean_t
915: net_do_filter(infp, data, data_count, header)
916: net_rcv_port_t infp;
917: char * data;
918: unsigned int data_count;
919: char * header;
920: {
921: int stack[NET_FILTER_STACK_DEPTH+1];
922: register int *sp;
923: register filter_t *fp, *fpe;
924: register unsigned int op, arg;
925:
926: /*
927: * The filter accesses the header and data
928: * as unsigned short words.
929: */
930: data_count /= sizeof(unsigned short);
931:
932: #define data_word ((unsigned short *)data)
933: #define header_word ((unsigned short *)header)
934:
935: sp = &stack[NET_FILTER_STACK_DEPTH];
1.1.1.3 ! root 936: fp = &infp->filter[1]; /* filter[0] used for flags */
1.1 root 937: fpe = infp->filter_end;
938:
939: *sp = TRUE;
940:
941: while (fp < fpe) {
942: arg = *fp++;
943: op = NETF_OP(arg);
944: arg = NETF_ARG(arg);
945:
946: switch (arg) {
947: case NETF_NOPUSH:
948: arg = *sp++;
949: break;
950: case NETF_PUSHZERO:
951: arg = 0;
952: break;
953: case NETF_PUSHLIT:
954: arg = *fp++;
955: break;
956: case NETF_PUSHIND:
957: arg = *sp++;
958: if (arg >= data_count)
959: return FALSE;
960: arg = data_word[arg];
961: break;
962: case NETF_PUSHHDRIND:
963: arg = *sp++;
964: if (arg >= NET_HDW_HDR_MAX/sizeof(unsigned short))
965: return FALSE;
966: arg = header_word[arg];
967: break;
968: default:
969: if (arg >= NETF_PUSHSTK) {
970: arg = sp[arg - NETF_PUSHSTK];
971: }
972: else if (arg >= NETF_PUSHHDR) {
973: arg = header_word[arg - NETF_PUSHHDR];
974: }
975: else {
976: arg -= NETF_PUSHWORD;
977: if (arg >= data_count)
978: return FALSE;
979: arg = data_word[arg];
980: }
981: break;
982:
983: }
984: switch (op) {
985: case NETF_OP(NETF_NOP):
986: *--sp = arg;
987: break;
988: case NETF_OP(NETF_AND):
989: *sp &= arg;
990: break;
991: case NETF_OP(NETF_OR):
992: *sp |= arg;
993: break;
994: case NETF_OP(NETF_XOR):
995: *sp ^= arg;
996: break;
997: case NETF_OP(NETF_EQ):
998: *sp = (*sp == arg);
999: break;
1000: case NETF_OP(NETF_NEQ):
1001: *sp = (*sp != arg);
1002: break;
1003: case NETF_OP(NETF_LT):
1004: *sp = (*sp < arg);
1005: break;
1006: case NETF_OP(NETF_LE):
1007: *sp = (*sp <= arg);
1008: break;
1009: case NETF_OP(NETF_GT):
1010: *sp = (*sp > arg);
1011: break;
1012: case NETF_OP(NETF_GE):
1013: *sp = (*sp >= arg);
1014: break;
1015: case NETF_OP(NETF_COR):
1016: if (*sp++ == arg)
1017: return (TRUE);
1018: break;
1019: case NETF_OP(NETF_CAND):
1020: if (*sp++ != arg)
1021: return (FALSE);
1022: break;
1023: case NETF_OP(NETF_CNOR):
1024: if (*sp++ == arg)
1025: return (FALSE);
1026: break;
1027: case NETF_OP(NETF_CNAND):
1028: if (*sp++ != arg)
1029: return (TRUE);
1030: break;
1031: case NETF_OP(NETF_LSH):
1032: *sp <<= arg;
1033: break;
1034: case NETF_OP(NETF_RSH):
1035: *sp >>= arg;
1036: break;
1037: case NETF_OP(NETF_ADD):
1038: *sp += arg;
1039: break;
1040: case NETF_OP(NETF_SUB):
1041: *sp -= arg;
1042: break;
1043: }
1044: }
1045: return ((*sp) ? TRUE : FALSE);
1046:
1047: #undef data_word
1048: #undef header_word
1049: }
1050:
1051: /*
1052: * Check filter for invalid operations or stack over/under-flow.
1053: */
1054: boolean_t
1055: parse_net_filter(filter, count)
1056: register filter_t *filter;
1057: unsigned int count;
1058: {
1059: register int sp;
1060: register filter_t *fpe = &filter[count];
1061: register filter_t op, arg;
1062:
1.1.1.3 ! root 1063: /*
! 1064: * count is at least 1, and filter[0] is used for flags.
! 1065: */
! 1066: filter++;
1.1 root 1067: sp = NET_FILTER_STACK_DEPTH;
1068:
1069: for (; filter < fpe; filter++) {
1070: op = NETF_OP(*filter);
1071: arg = NETF_ARG(*filter);
1072:
1073: switch (arg) {
1074: case NETF_NOPUSH:
1075: break;
1076: case NETF_PUSHZERO:
1077: sp--;
1078: break;
1079: case NETF_PUSHLIT:
1080: filter++;
1081: if (filter >= fpe)
1082: return (FALSE); /* literal value not in filter */
1083: sp--;
1084: break;
1085: case NETF_PUSHIND:
1086: case NETF_PUSHHDRIND:
1087: break;
1088: default:
1089: if (arg >= NETF_PUSHSTK) {
1090: if (arg - NETF_PUSHSTK + sp > NET_FILTER_STACK_DEPTH)
1091: return FALSE;
1092: }
1093: else if (arg >= NETF_PUSHHDR) {
1094: if (arg - NETF_PUSHHDR >=
1095: NET_HDW_HDR_MAX/sizeof(unsigned short))
1096: return FALSE;
1097: }
1098: /* else... cannot check for packet bounds
1099: without packet */
1100: sp--;
1101: break;
1102: }
1103: if (sp < 2) {
1104: return (FALSE); /* stack overflow */
1105: }
1106: if (op == NETF_OP(NETF_NOP))
1107: continue;
1108:
1109: /*
1110: * all non-NOP operators are binary.
1111: */
1112: if (sp > NET_MAX_FILTER-2)
1113: return (FALSE);
1114:
1115: sp++;
1116: switch (op) {
1117: case NETF_OP(NETF_AND):
1118: case NETF_OP(NETF_OR):
1119: case NETF_OP(NETF_XOR):
1120: case NETF_OP(NETF_EQ):
1121: case NETF_OP(NETF_NEQ):
1122: case NETF_OP(NETF_LT):
1123: case NETF_OP(NETF_LE):
1124: case NETF_OP(NETF_GT):
1125: case NETF_OP(NETF_GE):
1126: case NETF_OP(NETF_COR):
1127: case NETF_OP(NETF_CAND):
1128: case NETF_OP(NETF_CNOR):
1129: case NETF_OP(NETF_CNAND):
1130: case NETF_OP(NETF_LSH):
1131: case NETF_OP(NETF_RSH):
1132: case NETF_OP(NETF_ADD):
1133: case NETF_OP(NETF_SUB):
1134: break;
1135: default:
1136: return (FALSE);
1137: }
1138: }
1139: return (TRUE);
1140: }
1141:
1142: /*
1143: * Set a filter for a network interface.
1144: *
1145: * We are given a naked send right for the rcv_port.
1146: * If we are successful, we must consume that right.
1147: */
1148: io_return_t
1149: net_set_filter(ifp, rcv_port, priority, filter, filter_count)
1150: struct ifnet *ifp;
1151: ipc_port_t rcv_port;
1152: int priority;
1153: filter_t *filter;
1154: unsigned int filter_count;
1155: {
1156: int filter_bytes;
1157: bpf_insn_t match;
1158: register net_rcv_port_t infp, my_infp;
1159: net_rcv_port_t nextfp;
1160: net_hash_header_t hhp;
1161: register net_hash_entry_t entp, hash_entp;
1162: net_hash_entry_t *head, nextentp;
1163: queue_entry_t dead_infp, dead_entp;
1164: int i;
1165: int ret, is_new_infp;
1166: io_return_t rval;
1.1.1.3 ! root 1167: boolean_t in, out;
1.1 root 1168:
1169: /*
1170: * Check the filter syntax.
1171: */
1172:
1173: filter_bytes = CSPF_BYTES(filter_count);
1174: match = (bpf_insn_t) 0;
1175:
1.1.1.3 ! root 1176: if (filter_count == 0) {
! 1177: return (D_INVALID_OPERATION);
! 1178: } else if (!((filter[0] & NETF_IN) || (filter[0] & NETF_OUT))) {
! 1179: return (D_INVALID_OPERATION); /* NETF_IN or NETF_OUT required */
! 1180: } else if ((filter[0] & NETF_TYPE_MASK) == NETF_BPF) {
1.1 root 1181: ret = bpf_validate((bpf_insn_t)filter, filter_bytes, &match);
1182: if (!ret)
1183: return (D_INVALID_OPERATION);
1.1.1.3 ! root 1184: } else if ((filter[0] & NETF_TYPE_MASK) == 0) {
1.1 root 1185: if (!parse_net_filter(filter, filter_count))
1186: return (D_INVALID_OPERATION);
1.1.1.3 ! root 1187: } else {
! 1188: return (D_INVALID_OPERATION);
1.1 root 1189: }
1190:
1191: rval = D_SUCCESS; /* default return value */
1192: dead_infp = dead_entp = 0;
1193:
1194: if (match == (bpf_insn_t) 0) {
1195: /*
1196: * If there is no match instruction, we allocate
1197: * a normal packet filter structure.
1198: */
1.1.1.3 ! root 1199: my_infp = (net_rcv_port_t) kmem_cache_alloc(&net_rcv_cache);
1.1 root 1200: my_infp->rcv_port = rcv_port;
1201: is_new_infp = TRUE;
1202: } else {
1203: /*
1.1.1.3 ! root 1204: * If there is a match instruction, we assume there will be
! 1205: * multiple sessions with a common substructure and allocate
1.1 root 1206: * a hash table to deal with them.
1207: */
1208: my_infp = 0;
1.1.1.3 ! root 1209: hash_entp = (net_hash_entry_t) kmem_cache_alloc(&net_hash_entry_cache);
1.1 root 1210: is_new_infp = FALSE;
1211: }
1212:
1213: /*
1214: * Look for an existing filter on the same reply port.
1215: * Look for filters with dead ports (for GC).
1216: * Look for a filter with the same code except KEY insns.
1217: */
1.1.1.3 ! root 1218: void check_filter_list(queue_head_t *if_port_list)
1.1 root 1219: {
1.1.1.3 ! root 1220: FILTER_ITERATE(if_port_list, infp, nextfp,
! 1221: (if_port_list == &ifp->if_rcv_port_list)
! 1222: ? &infp->input : &infp->output)
! 1223: {
1.1 root 1224: if (infp->rcv_port == MACH_PORT_NULL) {
1.1.1.3 ! root 1225: if (match != 0
! 1226: && infp->priority == priority
! 1227: && my_infp == 0
! 1228: && (infp->filter_end - infp->filter) == filter_count
! 1229: && bpf_eq((bpf_insn_t)infp->filter,
! 1230: (bpf_insn_t)filter, filter_bytes))
! 1231: my_infp = infp;
! 1232:
! 1233: for (i = 0; i < NET_HASH_SIZE; i++) {
! 1234: head = &((net_hash_header_t) infp)->table[i];
! 1235: if (*head == 0)
! 1236: continue;
! 1237:
! 1238: /*
! 1239: * Check each hash entry to make sure the
! 1240: * destination port is still valid. Remove
! 1241: * any invalid entries.
! 1242: */
! 1243: entp = *head;
! 1244: do {
! 1245: nextentp = (net_hash_entry_t) entp->he_next;
1.1 root 1246:
1.1.1.3 ! root 1247: /* checked without
! 1248: ip_lock(entp->rcv_port) */
! 1249: if (entp->rcv_port == rcv_port
! 1250: || !IP_VALID(entp->rcv_port)
! 1251: || !ip_active(entp->rcv_port)) {
! 1252: ret = hash_ent_remove (ifp,
! 1253: (net_hash_header_t)infp,
! 1254: (my_infp == infp),
! 1255: head,
! 1256: entp,
! 1257: &dead_entp);
! 1258: if (ret)
! 1259: goto hash_loop_end;
! 1260: }
1.1 root 1261:
1.1.1.3 ! root 1262: entp = nextentp;
! 1263: /* While test checks head since hash_ent_remove
! 1264: might modify it.
! 1265: */
! 1266: } while (*head != 0 && entp != *head);
! 1267: }
! 1268:
1.1 root 1269: hash_loop_end:
1270: ;
1271: } else if (infp->rcv_port == rcv_port
1272: || !IP_VALID(infp->rcv_port)
1273: || !ip_active(infp->rcv_port)) {
1.1.1.3 ! root 1274:
! 1275: /* Remove the old filter from lists */
! 1276: if (infp->filter[0] & NETF_IN)
! 1277: queue_remove(&ifp->if_rcv_port_list, infp,
! 1278: net_rcv_port_t, input);
! 1279: if (infp->filter[0] & NETF_OUT)
! 1280: queue_remove(&ifp->if_snd_port_list, infp,
! 1281: net_rcv_port_t, output);
! 1282:
! 1283: ENQUEUE_DEAD(dead_infp, infp, input);
1.1 root 1284: }
1.1.1.3 ! root 1285: }
! 1286: FILTER_ITERATE_END
1.1 root 1287: }
1.1.1.3 ! root 1288:
! 1289: in = (filter[0] & NETF_IN) != 0;
! 1290: out = (filter[0] & NETF_OUT) != 0;
! 1291:
! 1292: simple_lock(&ifp->if_rcv_port_list_lock);
! 1293: simple_lock(&ifp->if_snd_port_list_lock);
! 1294:
! 1295: if (in)
! 1296: check_filter_list(&ifp->if_rcv_port_list);
! 1297: if (out)
! 1298: check_filter_list(&ifp->if_snd_port_list);
1.1 root 1299:
1300: if (my_infp == 0) {
1301: /* Allocate a dummy infp */
1302: simple_lock(&net_hash_header_lock);
1303: for (i = 0; i < N_NET_HASH; i++) {
1304: if (filter_hash_header[i].n_keys == 0)
1305: break;
1306: }
1307: if (i == N_NET_HASH) {
1308: simple_unlock(&net_hash_header_lock);
1.1.1.3 ! root 1309: simple_unlock(&ifp->if_snd_port_list_lock);
1.1 root 1310: simple_unlock(&ifp->if_rcv_port_list_lock);
1311:
1312: ipc_port_release_send(rcv_port);
1313: if (match != 0)
1.1.1.3 ! root 1314: kmem_cache_free(&net_hash_entry_cache,
! 1315: (vm_offset_t)hash_entp);
1.1 root 1316:
1317: rval = D_NO_MEMORY;
1318: goto clean_and_return;
1319: }
1320:
1321: hhp = &filter_hash_header[i];
1322: hhp->n_keys = match->jt;
1323: simple_unlock(&net_hash_header_lock);
1324:
1325: hhp->ref_count = 0;
1326: for (i = 0; i < NET_HASH_SIZE; i++)
1327: hhp->table[i] = 0;
1328:
1329: my_infp = (net_rcv_port_t)hhp;
1330: my_infp->rcv_port = MACH_PORT_NULL; /* indication of dummy */
1331: is_new_infp = TRUE;
1332: }
1333:
1334: if (is_new_infp) {
1335: my_infp->priority = priority;
1336: my_infp->rcv_count = 0;
1337:
1338: /* Copy filter program. */
1.1.1.3 ! root 1339: memcpy (my_infp->filter, filter, filter_bytes);
1.1 root 1340: my_infp->filter_end =
1341: (filter_t *)((char *)my_infp->filter + filter_bytes);
1342:
1343: if (match == 0) {
1344: my_infp->rcv_qlimit = net_add_q_info(rcv_port);
1345: } else {
1346: my_infp->rcv_qlimit = 0;
1347: }
1348:
1349: /* Insert my_infp according to priority */
1.1.1.3 ! root 1350: if (in) {
! 1351: queue_iterate(&ifp->if_rcv_port_list, infp, net_rcv_port_t, input)
! 1352: if (priority > infp->priority)
! 1353: break;
! 1354:
! 1355: queue_enter(&ifp->if_rcv_port_list, my_infp, net_rcv_port_t, input);
! 1356: }
! 1357:
! 1358: if (out) {
! 1359: queue_iterate(&ifp->if_snd_port_list, infp, net_rcv_port_t, output)
! 1360: if (priority > infp->priority)
! 1361: break;
! 1362:
! 1363: queue_enter(&ifp->if_snd_port_list, my_infp, net_rcv_port_t, output);
! 1364: }
1.1 root 1365: }
1366:
1367: if (match != 0)
1368: { /* Insert to hash list */
1369: net_hash_entry_t *p;
1370:
1371: hash_entp->rcv_port = rcv_port;
1372: for (i = 0; i < match->jt; i++) /* match->jt is n_keys */
1373: hash_entp->keys[i] = match[i+1].k;
1374: p = &((net_hash_header_t)my_infp)->
1375: table[bpf_hash(match->jt, hash_entp->keys)];
1376:
1377: /* Not checking for the same key values */
1378: if (*p == 0) {
1.1.1.3 ! root 1379: queue_init (&hash_entp->chain);
1.1 root 1380: *p = hash_entp;
1381: } else {
1.1.1.3 ! root 1382: enqueue_tail(&(*p)->chain, &hash_entp->chain);
1.1 root 1383: }
1384:
1385: ((net_hash_header_t)my_infp)->ref_count++;
1386: hash_entp->rcv_qlimit = net_add_q_info(rcv_port);
1387: }
1388:
1.1.1.3 ! root 1389: simple_unlock(&ifp->if_snd_port_list_lock);
1.1 root 1390: simple_unlock(&ifp->if_rcv_port_list_lock);
1391:
1392: clean_and_return:
1393: /* No locks are held at this point. */
1394:
1395: if (dead_infp != 0)
1396: net_free_dead_infp(dead_infp);
1397: if (dead_entp != 0)
1398: net_free_dead_entp(dead_entp);
1399:
1400: return (rval);
1401: }
1402:
1403: /*
1404: * Other network operations
1405: */
1406: io_return_t
1407: net_getstat(ifp, flavor, status, count)
1408: struct ifnet *ifp;
1409: dev_flavor_t flavor;
1410: dev_status_t status; /* pointer to OUT array */
1411: natural_t *count; /* OUT */
1412: {
1413: switch (flavor) {
1414: case NET_STATUS:
1415: {
1416: register struct net_status *ns = (struct net_status *)status;
1417:
1418: if (*count < NET_STATUS_COUNT)
1419: return (D_INVALID_OPERATION);
1420:
1421: ns->min_packet_size = ifp->if_header_size;
1422: ns->max_packet_size = ifp->if_header_size + ifp->if_mtu;
1423: ns->header_format = ifp->if_header_format;
1424: ns->header_size = ifp->if_header_size;
1425: ns->address_size = ifp->if_address_size;
1426: ns->flags = ifp->if_flags;
1427: ns->mapped_size = 0;
1428:
1429: *count = NET_STATUS_COUNT;
1430: break;
1431: }
1432: case NET_ADDRESS:
1433: {
1434: register int addr_byte_count;
1435: register int addr_int_count;
1436: register int i;
1437:
1438: addr_byte_count = ifp->if_address_size;
1439: addr_int_count = (addr_byte_count + (sizeof(int)-1))
1440: / sizeof(int);
1441:
1442: if (*count < addr_int_count)
1443: {
1444: /* XXX debug hack. */
1445: printf ("net_getstat: count: %d, addr_int_count: %d\n",
1446: *count, addr_int_count);
1447: return (D_INVALID_OPERATION);
1448: }
1449:
1.1.1.3 ! root 1450: memcpy(status, ifp->if_address, addr_byte_count);
1.1 root 1451: if (addr_byte_count < addr_int_count * sizeof(int))
1.1.1.3 ! root 1452: memset((char *)status + addr_byte_count, 0,
! 1453: (addr_int_count * sizeof(int)
1.1 root 1454: - addr_byte_count));
1455:
1456: for (i = 0; i < addr_int_count; i++) {
1457: register int word;
1458:
1459: word = status[i];
1460: status[i] = htonl(word);
1461: }
1462: *count = addr_int_count;
1463: break;
1464: }
1465: default:
1466: return (D_INVALID_OPERATION);
1467: }
1468: return (D_SUCCESS);
1469: }
1470:
1471: io_return_t
1472: net_write(ifp, start, ior)
1473: register struct ifnet *ifp;
1474: int (*start)();
1475: io_req_t ior;
1476: {
1477: spl_t s;
1478: kern_return_t rc;
1479: boolean_t wait;
1480:
1481: /*
1482: * Reject the write if the interface is down.
1483: */
1484: if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
1485: return (D_DEVICE_DOWN);
1486:
1487: /*
1488: * Reject the write if the packet is too large or too small.
1489: */
1490: if (ior->io_count < ifp->if_header_size ||
1491: ior->io_count > ifp->if_header_size + ifp->if_mtu)
1492: return (D_INVALID_SIZE);
1493:
1494: /*
1495: * Wire down the memory.
1496: */
1497:
1498: rc = device_write_get(ior, &wait);
1499: if (rc != KERN_SUCCESS)
1500: return (rc);
1501:
1502: /*
1503: * Network interfaces can't cope with VM continuations.
1504: * If wait is set, just panic.
1505: */
1506: if (wait) {
1507: panic("net_write: VM continuation");
1508: }
1509:
1510: /*
1511: * Queue the packet on the output queue, and
1512: * start the device.
1513: */
1514: s = splimp();
1515: IF_ENQUEUE(&ifp->if_snd, ior);
1516: (*start)(ifp->if_unit);
1517: splx(s);
1518:
1519: return (D_IO_QUEUED);
1520: }
1521:
1522: /*
1523: * Initialize the whole package.
1524: */
1525: void
1526: net_io_init()
1527: {
1528: register vm_size_t size;
1529:
1530: size = sizeof(struct net_rcv_port);
1.1.1.3 ! root 1531: kmem_cache_init(&net_rcv_cache, "net_rcv_port", size, 0,
! 1532: NULL, NULL, NULL, 0);
1.1 root 1533:
1534: size = sizeof(struct net_hash_entry);
1.1.1.3 ! root 1535: kmem_cache_init(&net_hash_entry_cache, "net_hash_entry", size, 0,
! 1536: NULL, NULL, NULL, 0);
1.1 root 1537:
1538: size = ikm_plus_overhead(sizeof(struct net_rcv_msg));
1539: net_kmsg_size = round_page(size);
1540:
1541: /*
1542: * net_kmsg_max caps the number of buffers
1543: * we are willing to allocate. By default,
1544: * we allow for net_queue_free_min plus
1545: * the queue limit for each filter.
1546: * (Added as the filters are added.)
1547: */
1548:
1549: simple_lock_init(&net_kmsg_total_lock);
1550: if (net_kmsg_max == 0)
1551: net_kmsg_max = net_queue_free_min;
1552:
1553: simple_lock_init(&net_queue_free_lock);
1554: ipc_kmsg_queue_init(&net_queue_free);
1555:
1556: simple_lock_init(&net_queue_lock);
1557: ipc_kmsg_queue_init(&net_queue_high);
1558: ipc_kmsg_queue_init(&net_queue_low);
1559:
1560: simple_lock_init(&net_hash_header_lock);
1561: }
1562:
1563:
1564: /* ======== BPF: Berkeley Packet Filter ======== */
1565:
1566: /*-
1567: * Copyright (c) 1990-1991 The Regents of the University of California.
1568: * All rights reserved.
1569: *
1570: * This code is derived from the Stanford/CMU enet packet filter,
1571: * (net/enet.c) distributed as part of 4.3BSD, and code contributed
1572: * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
1573: * Berkeley Laboratory.
1574: *
1575: * Redistribution and use in source and binary forms, with or without
1576: * modification, are permitted provided that the following conditions
1577: * are met:
1578: * 1. Redistributions of source code must retain the above copyright
1579: * notice, this list of conditions and the following disclaimer.
1580: * 2. Redistributions in binary form must reproduce the above copyright
1581: * notice, this list of conditions and the following disclaimer in the
1582: * documentation and/or other materials provided with the distribution.
1583: * 4. Neither the name of the University nor the names of its contributors
1584: * may be used to endorse or promote products derived from this software
1585: * without specific prior written permission.
1586: *
1587: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1588: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1589: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1590: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1591: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1592: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1593: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1594: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1595: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1596: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1597: * SUCH DAMAGE.
1598: *
1599: * @(#)bpf.c 7.5 (Berkeley) 7/15/91
1600: */
1601:
1602: #if defined(sparc) || defined(mips) || defined(ibm032) || defined(alpha)
1603: #define BPF_ALIGN
1604: #endif
1605:
1606: #ifndef BPF_ALIGN
1607: #define EXTRACT_SHORT(p) ((u_short)ntohs(*(u_short *)p))
1608: #define EXTRACT_LONG(p) (ntohl(*(u_long *)p))
1609: #else
1610: #define EXTRACT_SHORT(p)\
1611: ((u_short)\
1612: ((u_short)*((u_char *)p+0)<<8|\
1613: (u_short)*((u_char *)p+1)<<0))
1614: #define EXTRACT_LONG(p)\
1615: ((u_long)*((u_char *)p+0)<<24|\
1616: (u_long)*((u_char *)p+1)<<16|\
1617: (u_long)*((u_char *)p+2)<<8|\
1618: (u_long)*((u_char *)p+3)<<0)
1619: #endif
1620:
1621: /*
1622: * Execute the filter program starting at pc on the packet p
1623: * wirelen is the length of the original packet
1624: * buflen is the amount of data present
1625: */
1626:
1627: int
1.1.1.3 ! root 1628: bpf_do_filter(infp, p, wirelen, header, hlen, hash_headpp, entpp)
1.1 root 1629: net_rcv_port_t infp;
1630: char * p; /* packet data */
1631: unsigned int wirelen; /* data_count (in bytes) */
1632: char * header;
1.1.1.3 ! root 1633: unsigned int hlen; /* header len (in bytes) */
1.1 root 1634: net_hash_entry_t **hash_headpp, *entpp; /* out */
1635: {
1636: register bpf_insn_t pc, pc_end;
1637: register unsigned int buflen;
1638:
1.1.1.3 ! root 1639: register unsigned int A, X;
1.1 root 1640: register int k;
1.1.1.3 ! root 1641: unsigned int mem[BPF_MEMWORDS];
! 1642:
! 1643: /* Generic pointer to either HEADER or P according to the specified offset. */
! 1644: char *data = NULL;
1.1 root 1645:
1646: pc = ((bpf_insn_t) infp->filter) + 1;
1.1.1.3 ! root 1647: /* filter[0].code is (NETF_BPF | flags) */
1.1 root 1648: pc_end = (bpf_insn_t)infp->filter_end;
1649: buflen = NET_RCV_MAX;
1650: *entpp = 0; /* default */
1651:
1652: A = 0;
1653: X = 0;
1.1.1.3 ! root 1654:
1.1 root 1655: for (; pc < pc_end; ++pc) {
1656: switch (pc->code) {
1657:
1658: default:
1659: #ifdef KERNEL
1660: return 0;
1661: #else
1662: abort();
1663: #endif
1664: case BPF_RET|BPF_K:
1665: if (infp->rcv_port == MACH_PORT_NULL &&
1666: *entpp == 0) {
1667: return 0;
1668: }
1669: return ((u_int)pc->k <= wirelen) ?
1670: pc->k : wirelen;
1671:
1672: case BPF_RET|BPF_A:
1673: if (infp->rcv_port == MACH_PORT_NULL &&
1674: *entpp == 0) {
1675: return 0;
1676: }
1677: return ((u_int)A <= wirelen) ?
1678: A : wirelen;
1679:
1680: case BPF_RET|BPF_MATCH_IMM:
1681: if (bpf_match ((net_hash_header_t)infp, pc->jt, mem,
1682: hash_headpp, entpp)) {
1683: return ((u_int)pc->k <= wirelen) ?
1684: pc->k : wirelen;
1685: }
1686: return 0;
1687:
1688: case BPF_LD|BPF_W|BPF_ABS:
1689: k = pc->k;
1690:
1.1.1.3 ! root 1691: load_word:
! 1692: if ((u_int)k + sizeof(int) <= hlen)
! 1693: data = header;
! 1694: else if ((u_int)k + sizeof(int) <= buflen) {
! 1695: k -= hlen;
! 1696: data = p;
! 1697: } else
! 1698: return 0;
! 1699:
1.1 root 1700: #ifdef BPF_ALIGN
1.1.1.3 ! root 1701: if (((int)(data + k) & 3) != 0)
! 1702: A = EXTRACT_LONG(&data[k]);
! 1703: else
1.1 root 1704: #endif
1.1.1.3 ! root 1705: A = ntohl(*(int *)(data + k));
! 1706: continue;
1.1 root 1707:
1708: case BPF_LD|BPF_H|BPF_ABS:
1709: k = pc->k;
1710:
1.1.1.3 ! root 1711: load_half:
! 1712: if ((u_int)k + sizeof(short) <= hlen)
! 1713: data = header;
! 1714: else if ((u_int)k + sizeof(short) <= buflen) {
! 1715: k -= hlen;
! 1716: data = p;
! 1717: } else
! 1718: return 0;
! 1719:
! 1720: A = EXTRACT_SHORT(&data[k]);
! 1721: continue;
1.1 root 1722:
1723: case BPF_LD|BPF_B|BPF_ABS:
1.1.1.3 ! root 1724: k = pc->k;
! 1725:
! 1726: load_byte:
! 1727: if ((u_int)k < hlen)
! 1728: data = header;
! 1729: else if ((u_int)k < buflen) {
! 1730: data = p;
! 1731: k -= hlen;
! 1732: } else
! 1733: return 0;
! 1734:
! 1735: A = data[k];
! 1736: continue;
1.1 root 1737:
1738: case BPF_LD|BPF_W|BPF_LEN:
1739: A = wirelen;
1740: continue;
1741:
1742: case BPF_LDX|BPF_W|BPF_LEN:
1743: X = wirelen;
1744: continue;
1745:
1746: case BPF_LD|BPF_W|BPF_IND:
1747: k = X + pc->k;
1.1.1.3 ! root 1748: goto load_word;
! 1749:
1.1 root 1750: case BPF_LD|BPF_H|BPF_IND:
1751: k = X + pc->k;
1.1.1.3 ! root 1752: goto load_half;
1.1 root 1753:
1754: case BPF_LD|BPF_B|BPF_IND:
1755: k = X + pc->k;
1.1.1.3 ! root 1756: goto load_byte;
1.1 root 1757:
1758: case BPF_LDX|BPF_MSH|BPF_B:
1759: k = pc->k;
1.1.1.3 ! root 1760: if (k < hlen)
! 1761: data = header;
! 1762: else if (k < buflen) {
! 1763: data = p;
! 1764: k -= hlen;
! 1765: } else
! 1766: return 0;
! 1767:
! 1768: X = (data[k] & 0xf) << 2;
1.1 root 1769: continue;
1770:
1771: case BPF_LD|BPF_IMM:
1772: A = pc->k;
1773: continue;
1774:
1775: case BPF_LDX|BPF_IMM:
1776: X = pc->k;
1777: continue;
1778:
1779: case BPF_LD|BPF_MEM:
1780: A = mem[pc->k];
1781: continue;
1782:
1783: case BPF_LDX|BPF_MEM:
1784: X = mem[pc->k];
1785: continue;
1786:
1787: case BPF_ST:
1788: mem[pc->k] = A;
1789: continue;
1790:
1791: case BPF_STX:
1792: mem[pc->k] = X;
1793: continue;
1794:
1795: case BPF_JMP|BPF_JA:
1796: pc += pc->k;
1797: continue;
1798:
1799: case BPF_JMP|BPF_JGT|BPF_K:
1800: pc += (A > pc->k) ? pc->jt : pc->jf;
1801: continue;
1802:
1803: case BPF_JMP|BPF_JGE|BPF_K:
1804: pc += (A >= pc->k) ? pc->jt : pc->jf;
1805: continue;
1806:
1807: case BPF_JMP|BPF_JEQ|BPF_K:
1808: pc += (A == pc->k) ? pc->jt : pc->jf;
1809: continue;
1810:
1811: case BPF_JMP|BPF_JSET|BPF_K:
1812: pc += (A & pc->k) ? pc->jt : pc->jf;
1813: continue;
1814:
1815: case BPF_JMP|BPF_JGT|BPF_X:
1816: pc += (A > X) ? pc->jt : pc->jf;
1817: continue;
1818:
1819: case BPF_JMP|BPF_JGE|BPF_X:
1820: pc += (A >= X) ? pc->jt : pc->jf;
1821: continue;
1822:
1823: case BPF_JMP|BPF_JEQ|BPF_X:
1824: pc += (A == X) ? pc->jt : pc->jf;
1825: continue;
1826:
1827: case BPF_JMP|BPF_JSET|BPF_X:
1828: pc += (A & X) ? pc->jt : pc->jf;
1829: continue;
1830:
1831: case BPF_ALU|BPF_ADD|BPF_X:
1832: A += X;
1833: continue;
1834:
1835: case BPF_ALU|BPF_SUB|BPF_X:
1836: A -= X;
1837: continue;
1838:
1839: case BPF_ALU|BPF_MUL|BPF_X:
1840: A *= X;
1841: continue;
1842:
1843: case BPF_ALU|BPF_DIV|BPF_X:
1844: if (X == 0)
1845: return 0;
1846: A /= X;
1847: continue;
1848:
1849: case BPF_ALU|BPF_AND|BPF_X:
1850: A &= X;
1851: continue;
1852:
1853: case BPF_ALU|BPF_OR|BPF_X:
1854: A |= X;
1855: continue;
1856:
1857: case BPF_ALU|BPF_LSH|BPF_X:
1858: A <<= X;
1859: continue;
1860:
1861: case BPF_ALU|BPF_RSH|BPF_X:
1862: A >>= X;
1863: continue;
1864:
1865: case BPF_ALU|BPF_ADD|BPF_K:
1866: A += pc->k;
1867: continue;
1868:
1869: case BPF_ALU|BPF_SUB|BPF_K:
1870: A -= pc->k;
1871: continue;
1872:
1873: case BPF_ALU|BPF_MUL|BPF_K:
1874: A *= pc->k;
1875: continue;
1876:
1877: case BPF_ALU|BPF_DIV|BPF_K:
1878: A /= pc->k;
1879: continue;
1880:
1881: case BPF_ALU|BPF_AND|BPF_K:
1882: A &= pc->k;
1883: continue;
1884:
1885: case BPF_ALU|BPF_OR|BPF_K:
1886: A |= pc->k;
1887: continue;
1888:
1889: case BPF_ALU|BPF_LSH|BPF_K:
1890: A <<= pc->k;
1891: continue;
1892:
1893: case BPF_ALU|BPF_RSH|BPF_K:
1894: A >>= pc->k;
1895: continue;
1896:
1897: case BPF_ALU|BPF_NEG:
1898: A = -A;
1899: continue;
1900:
1901: case BPF_MISC|BPF_TAX:
1902: X = A;
1903: continue;
1904:
1905: case BPF_MISC|BPF_TXA:
1906: A = X;
1907: continue;
1908: }
1909: }
1910:
1911: return 0;
1912: }
1913:
1914: /*
1915: * Return 1 if the 'f' is a valid filter program without a MATCH
1916: * instruction. Return 2 if it is a valid filter program with a MATCH
1917: * instruction. Otherwise, return 0.
1918: * The constraints are that each jump be forward and to a valid
1919: * code. The code must terminate with either an accept or reject.
1920: * 'valid' is an array for use by the routine (it must be at least
1921: * 'len' bytes long).
1922: *
1923: * The kernel needs to be able to verify an application's filter code.
1924: * Otherwise, a bogus program could easily crash the system.
1925: */
1926: int
1927: bpf_validate(f, bytes, match)
1928: bpf_insn_t f;
1929: int bytes;
1930: bpf_insn_t *match;
1931: {
1932: register int i, j, len;
1933: register bpf_insn_t p;
1934:
1935: len = BPF_BYTES2LEN(bytes);
1.1.1.3 ! root 1936:
! 1937: /*
! 1938: * f[0].code is already checked to be (NETF_BPF | flags).
! 1939: * So skip f[0].
! 1940: */
1.1 root 1941:
1942: for (i = 1; i < len; ++i) {
1943: /*
1944: * Check that that jumps are forward, and within
1945: * the code block.
1946: */
1947: p = &f[i];
1948: if (BPF_CLASS(p->code) == BPF_JMP) {
1949: register int from = i + 1;
1950:
1951: if (BPF_OP(p->code) == BPF_JA) {
1952: if (from + p->k >= len)
1953: return 0;
1954: }
1955: else if (from + p->jt >= len || from + p->jf >= len)
1956: return 0;
1957: }
1958: /*
1959: * Check that memory operations use valid addresses.
1960: */
1961: if ((BPF_CLASS(p->code) == BPF_ST ||
1962: (BPF_CLASS(p->code) == BPF_LD &&
1963: (p->code & 0xe0) == BPF_MEM)) &&
1964: (p->k >= BPF_MEMWORDS || p->k < 0))
1965: return 0;
1966: /*
1967: * Check for constant division by 0.
1968: */
1969: if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
1970: return 0;
1971: /*
1972: * Check for match instruction.
1973: * Only one match instruction per filter is allowed.
1974: */
1975: if (p->code == (BPF_RET|BPF_MATCH_IMM)) {
1976: if (*match != 0 ||
1977: p->jt == 0 ||
1978: p->jt > N_NET_HASH_KEYS)
1979: return 0;
1980: i += p->jt; /* skip keys */
1981: if (i + 1 > len)
1982: return 0;
1983:
1984: for (j = 1; j <= p->jt; j++) {
1985: if (p[j].code != (BPF_MISC|BPF_KEY))
1986: return 0;
1987: }
1988:
1989: *match = p;
1990: }
1991: }
1992: if (BPF_CLASS(f[len - 1].code) == BPF_RET)
1993: return ((*match == 0) ? 1 : 2);
1994: else
1995: return 0;
1996: }
1997:
1998: int
1999: bpf_eq (f1, f2, bytes)
2000: register bpf_insn_t f1, f2;
2001: register int bytes;
2002: {
2003: register int count;
2004:
2005: count = BPF_BYTES2LEN(bytes);
2006: for (; count--; f1++, f2++) {
2007: if (!BPF_INSN_EQ(f1, f2)) {
2008: if ( f1->code == (BPF_MISC|BPF_KEY) &&
2009: f2->code == (BPF_MISC|BPF_KEY) )
2010: continue;
2011: return FALSE;
2012: }
2013: };
2014: return TRUE;
2015: }
2016:
2017: unsigned int
2018: bpf_hash (n, keys)
2019: register int n;
2020: register unsigned int *keys;
2021: {
2022: register unsigned int hval = 0;
2023:
2024: while (n--) {
2025: hval += *keys++;
2026: }
2027: return (hval % NET_HASH_SIZE);
2028: }
2029:
2030:
2031: int
2032: bpf_match (hash, n_keys, keys, hash_headpp, entpp)
2033: net_hash_header_t hash;
2034: register int n_keys;
2035: register unsigned int *keys;
2036: net_hash_entry_t **hash_headpp, *entpp;
2037: {
2038: register net_hash_entry_t head, entp;
2039: register int i;
2040:
2041: if (n_keys != hash->n_keys)
2042: return FALSE;
2043:
2044: *hash_headpp = &hash->table[bpf_hash(n_keys, keys)];
2045: head = **hash_headpp;
2046:
2047: if (head == 0)
2048: return FALSE;
2049:
2050: HASH_ITERATE (head, entp)
2051: {
2052: for (i = 0; i < n_keys; i++) {
2053: if (keys[i] != entp->keys[i])
2054: break;
2055: }
2056: if (i == n_keys) {
2057: *entpp = entp;
2058: return TRUE;
2059: }
2060: }
2061: HASH_ITERATE_END (head, entp)
2062: return FALSE;
2063: }
2064:
2065:
2066: /*
2067: * Removes a hash entry (ENTP) from its queue (HEAD).
2068: * If the reference count of filter (HP) becomes zero and not USED,
1.1.1.3 ! root 2069: * HP is removed from the corresponding port lists and is freed.
1.1 root 2070: */
2071:
2072: int
2073: hash_ent_remove (ifp, hp, used, head, entp, dead_p)
2074: struct ifnet *ifp;
2075: net_hash_header_t hp;
2076: int used;
2077: net_hash_entry_t *head, entp;
2078: queue_entry_t *dead_p;
2079: {
2080: hp->ref_count--;
2081:
2082: if (*head == entp) {
2083: if (queue_empty((queue_t) entp)) {
2084: *head = 0;
1.1.1.3 ! root 2085: ENQUEUE_DEAD(*dead_p, entp, chain);
1.1 root 2086: if (hp->ref_count == 0 && !used) {
1.1.1.3 ! root 2087: if (((net_rcv_port_t)hp)->filter[0] & NETF_IN)
! 2088: queue_remove(&ifp->if_rcv_port_list,
! 2089: (net_rcv_port_t)hp,
! 2090: net_rcv_port_t, input);
! 2091: if (((net_rcv_port_t)hp)->filter[0] & NETF_OUT)
! 2092: queue_remove(&ifp->if_snd_port_list,
! 2093: (net_rcv_port_t)hp,
! 2094: net_rcv_port_t, output);
1.1 root 2095: hp->n_keys = 0;
2096: return TRUE;
2097: }
2098: return FALSE;
2099: } else {
2100: *head = (net_hash_entry_t)queue_next((queue_t) entp);
2101: }
2102: }
2103:
2104: remqueue((queue_t)*head, (queue_entry_t)entp);
1.1.1.3 ! root 2105: ENQUEUE_DEAD(*dead_p, entp, chain);
1.1 root 2106: return FALSE;
2107: }
2108:
2109: int
2110: net_add_q_info (rcv_port)
2111: ipc_port_t rcv_port;
2112: {
2113: mach_port_msgcount_t qlimit = 0;
2114:
2115: /*
2116: * We use a new port, so increase net_queue_free_min
2117: * and net_kmsg_max to allow for more queued messages.
2118: */
2119:
2120: if (IP_VALID(rcv_port)) {
2121: ip_lock(rcv_port);
2122: if (ip_active(rcv_port))
2123: qlimit = rcv_port->ip_qlimit;
2124: ip_unlock(rcv_port);
2125: }
2126:
2127: simple_lock(&net_kmsg_total_lock);
2128: net_queue_free_min++;
2129: net_kmsg_max += qlimit + 1;
2130: simple_unlock(&net_kmsg_total_lock);
2131:
2132: return (int)qlimit;
2133: }
2134:
1.1.1.3 ! root 2135: void
1.1 root 2136: net_del_q_info (qlimit)
2137: int qlimit;
2138: {
2139: simple_lock(&net_kmsg_total_lock);
2140: net_queue_free_min--;
2141: net_kmsg_max -= qlimit + 1;
2142: simple_unlock(&net_kmsg_total_lock);
2143: }
2144:
2145:
2146: /*
2147: * net_free_dead_infp (dead_infp)
2148: * queue_entry_t dead_infp; list of dead net_rcv_port_t.
2149: *
2150: * Deallocates dead net_rcv_port_t.
2151: * No locks should be held when called.
2152: */
1.1.1.3 ! root 2153: void
1.1 root 2154: net_free_dead_infp (dead_infp)
2155: queue_entry_t dead_infp;
2156: {
2157: register net_rcv_port_t infp, nextfp;
2158:
2159: for (infp = (net_rcv_port_t) dead_infp; infp != 0; infp = nextfp)
2160: {
1.1.1.3 ! root 2161: nextfp = (net_rcv_port_t) queue_next(&infp->input);
1.1 root 2162: ipc_port_release_send(infp->rcv_port);
2163: net_del_q_info(infp->rcv_qlimit);
1.1.1.3 ! root 2164: kmem_cache_free(&net_rcv_cache, (vm_offset_t) infp);
1.1 root 2165: }
2166: }
2167:
2168: /*
2169: * net_free_dead_entp (dead_entp)
2170: * queue_entry_t dead_entp; list of dead net_hash_entry_t.
2171: *
2172: * Deallocates dead net_hash_entry_t.
2173: * No locks should be held when called.
2174: */
1.1.1.3 ! root 2175: void
1.1 root 2176: net_free_dead_entp (dead_entp)
2177: queue_entry_t dead_entp;
2178: {
2179: register net_hash_entry_t entp, nextentp;
2180:
2181: for (entp = (net_hash_entry_t)dead_entp; entp != 0; entp = nextentp)
2182: {
2183: nextentp = (net_hash_entry_t) queue_next(&entp->chain);
2184:
2185: ipc_port_release_send(entp->rcv_port);
2186: net_del_q_info(entp->rcv_qlimit);
1.1.1.3 ! root 2187: kmem_cache_free(&net_hash_entry_cache, (vm_offset_t) entp);
1.1 root 2188: }
2189: }
2190:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.