]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/bridge.c
c9e06ea2c85b6b136c641ab340184a3984937b67
[apple/xnu.git] / bsd / net / bridge.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) 1998 Luigi Rizzo
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 *
35 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 *
47 * $FreeBSD: src/sys/net/bridge.c,v 1.16.2.14 2001/02/09 23:13:41 luigi Exp $
48 */
49
50 /*
51 * This code implements bridging in FreeBSD. It only acts on ethernet
52 * type of interfaces (others are still usable for routing).
53 * A bridging table holds the source MAC address/dest. interface for each
54 * known node. The table is indexed using an hash of the source address.
55 *
56 * Input packets are tapped near the beginning of ether_input(), and
57 * analysed by calling bridge_in(). Depending on the result, the packet
58 * can be forwarded to one or more output interfaces using bdg_forward(),
59 * and/or sent to the upper layer (e.g. in case of multicast).
60 *
61 * Output packets are intercepted near the end of ether_output(),
62 * the correct destination is selected calling bridge_dst_lookup(),
63 * and then forwarding is done using bdg_forward().
64 * Bridging is controlled by the sysctl variable net.link.ether.bridge
65 *
66 * The arp code is also modified to let a machine answer to requests
67 * irrespective of the port the request came from.
68 *
69 * In case of loops in the bridging topology, the bridge detects this
70 * event and temporarily mutes output bridging on one of the ports.
71 * Periodically, interfaces are unmuted by bdg_timeout().
72 * Muting is only implemented as a safety measure, and also as
73 * a mechanism to support a user-space implementation of the spanning
74 * tree algorithm. In the final release, unmuting will only occur
75 * because of explicit action of the user-level daemon.
76 *
77 * To build a bridging kernel, use the following option
78 * option BRIDGE
79 * and then at runtime set the sysctl variable to enable bridging.
80 *
81 * Only one interface is supposed to have addresses set (but
82 * there are no problems in practice if you set addresses for more
83 * than one interface).
84 * Bridging will act before routing, but nothing prevents a machine
85 * from doing both (modulo bugs in the implementation...).
86 *
87 * THINGS TO REMEMBER
88 * - bridging is incompatible with multicast routing on the same
89 * machine. There is not an easy fix to this.
90 * - loop detection is still not very robust.
91 * - the interface of bdg_forward() could be improved.
92 */
93
94 #include <sys/param.h>
95 #include <sys/mbuf.h>
96 #include <sys/malloc.h>
97 #include <sys/systm.h>
98 #include <sys/socket.h> /* for net/if.h */
99 #include <sys/kernel.h>
100 #include <sys/sysctl.h>
101
102 #include <net/if.h>
103 #include <net/if_types.h>
104
105 #include <netinet/in.h> /* for struct arpcom */
106 #include <netinet/in_systm.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip.h>
109 #include <netinet/if_ether.h> /* for struct arpcom */
110
111 #include "opt_ipfw.h"
112 #include "opt_ipdn.h"
113
114 #if defined(IPFIREWALL)
115 #include <net/route.h>
116 #include <netinet/ip_fw.h>
117 #if defined(DUMMYNET)
118 #include <netinet/ip_dummynet.h>
119 #endif
120 #endif
121
122 #include <net/bridge.h>
123
124 /*
125 * For debugging, you can use the following macros.
126 * remember, rdtsc() only works on Pentium-class machines
127
128 quad_t ticks;
129 DDB(ticks = rdtsc();)
130 ... interesting code ...
131 DDB(bdg_fw_ticks += (u_long)(rdtsc() - ticks) ; bdg_fw_count++ ;)
132
133 *
134 */
135
136 #define DDB(x) x
137 #define DEB(x)
138
139 static void bdginit(void *);
140 static void bdgtakeifaces(void);
141 static void flush_table(void);
142 static void bdg_promisc_on(void);
143 static void parse_bdg_cfg(void);
144
145 static int bdg_ipfw = 0 ;
146 int do_bridge = 0;
147 bdg_hash_table *bdg_table = NULL ;
148
149 /*
150 * System initialization
151 */
152
153 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, bdginit, NULL)
154
155 static struct bdg_stats bdg_stats ;
156 struct bdg_softc *ifp2sc = NULL ;
157 /* XXX make it static of size BDG_MAX_PORTS */
158
159 #define IFP_CHK(ifp, x) \
160 if (ifp2sc[ifp->if_index].magic != 0xDEADBEEF) { x ; }
161
162 /*
163 * turn off promisc mode, optionally clear the IFF_USED flag.
164 * The flag is turned on by parse_bdg_config
165 */
166 static void
167 bdg_promisc_off(int clear_used)
168 {
169 struct ifnet *ifp ;
170 ifnet_head_lock_shared();
171 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
172 if ( (ifp2sc[ifp->if_index].flags & IFF_BDG_PROMISC) ) {
173 int s, ret ;
174 s = splimp();
175 ret = ifnet_set_promiscuous(ifp, 0);
176 splx(s);
177 ifp2sc[ifp->if_index].flags &= ~(IFF_BDG_PROMISC|IFF_MUTE) ;
178 DEB(printf(">> now %s%d promisc OFF if_flags 0x%x bdg_flags 0x%x\n",
179 ifp->if_name, ifp->if_unit,
180 ifp->if_flags, ifp2sc[ifp->if_index].flags);)
181 }
182 if (clear_used) {
183 ifp2sc[ifp->if_index].flags &= ~(IFF_USED) ;
184 bdg_stats.s[ifp->if_index].name[0] = '\0';
185 }
186 }
187 ifnet_head_done();
188 }
189
190 /*
191 * set promisc mode on the interfaces we use.
192 */
193 static void
194 bdg_promisc_on()
195 {
196 struct ifnet *ifp ;
197 int s ;
198
199 ifnet_head_lock_shared();
200 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
201 if ( !BDG_USED(ifp) )
202 continue ;
203 if ( 0 == ( ifp->if_flags & IFF_UP) ) {
204 s = splimp();
205 if_up(ifp);
206 splx(s);
207 }
208 if ( !(ifp2sc[ifp->if_index].flags & IFF_BDG_PROMISC) ) {
209 int ret ;
210 s = splimp();
211 ret = ifnet_set_promiscuous(ifp, 1);
212 splx(s);
213 ifp2sc[ifp->if_index].flags |= IFF_BDG_PROMISC ;
214 printf(">> now %s%d promisc ON if_flags 0x%x bdg_flags 0x%x\n",
215 ifp->if_name, ifp->if_unit,
216 ifp->if_flags, ifp2sc[ifp->if_index].flags);
217 }
218 if (BDG_MUTED(ifp)) {
219 printf(">> unmuting %s%d\n", ifp->if_name, ifp->if_unit);
220 BDG_UNMUTE(ifp) ;
221 }
222 }
223 ifnet_head_done();
224 }
225
226 static int
227 sysctl_bdg(SYSCTL_HANDLER_ARGS)
228 {
229 int error, oldval = do_bridge ;
230
231 error = sysctl_handle_int(oidp,
232 oidp->oid_arg1, oidp->oid_arg2, req);
233 DEB( printf("called sysctl for bridge name %s arg2 %d val %d->%d\n",
234 oidp->oid_name, oidp->oid_arg2,
235 oldval, do_bridge); )
236
237 if (bdg_table == NULL)
238 do_bridge = 0 ;
239 if (oldval != do_bridge) {
240 bdg_promisc_off( 1 ); /* reset previously used interfaces */
241 flush_table();
242 if (do_bridge) {
243 parse_bdg_cfg();
244 bdg_promisc_on();
245 }
246 }
247 return error ;
248 }
249
250 static char bridge_cfg[256] = { "" } ;
251
252 /*
253 * parse the config string, set IFF_USED, name and cluster_id
254 * for all interfaces found.
255 */
256 static void
257 parse_bdg_cfg()
258 {
259 char *p, *beg ;
260 int i, l, cluster;
261 struct bdg_softc *b;
262
263 for (p= bridge_cfg; *p ; p++) {
264 /* interface names begin with [a-z] and continue up to ':' */
265 if (*p < 'a' || *p > 'z')
266 continue ;
267 for ( beg = p ; *p && *p != ':' ; p++ )
268 ;
269 if (*p == 0) /* end of string, ':' not found */
270 return ;
271 l = p - beg ; /* length of name string */
272 p++ ;
273 DEB(printf("-- match beg(%d) <%s> p <%s>\n", l, beg, p);)
274 for (cluster = 0 ; *p && *p >= '0' && *p <= '9' ; p++)
275 cluster = cluster*10 + (*p -'0');
276 /*
277 * now search in bridge strings
278 */
279 for (i=0, b = ifp2sc ; i < if_index ; i++, b++) {
280 char buf[32];
281 struct ifnet *ifp = b->ifp ;
282
283 if (ifp == NULL)
284 continue;
285 sprintf(buf, "%s%d", ifp->if_name, ifp->if_unit);
286 if (!strncmp(beg, buf, l)) { /* XXX not correct for >10 if! */
287 b->cluster_id = htons(cluster) ;
288 b->flags |= IFF_USED ;
289 sprintf(bdg_stats.s[ifp->if_index].name,
290 "%s%d:%d", ifp->if_name, ifp->if_unit, cluster);
291
292 DEB(printf("--++ found %s\n",
293 bdg_stats.s[ifp->if_index].name);)
294 break ;
295 }
296 }
297 if (*p == '\0')
298 break ;
299 }
300 }
301
302 static int
303 sysctl_bdg_cfg(SYSCTL_HANDLER_ARGS)
304 {
305 int error = 0 ;
306 char oldval[256] ;
307
308 strcpy(oldval, bridge_cfg) ;
309
310 error = sysctl_handle_string(oidp,
311 bridge_cfg, oidp->oid_arg2, req);
312 DEB(
313 printf("called sysctl for bridge name %s arg2 %d err %d val %s->%s\n",
314 oidp->oid_name, oidp->oid_arg2,
315 error,
316 oldval, bridge_cfg);
317 )
318 if (strcmp(oldval, bridge_cfg)) {
319 bdg_promisc_off( 1 ); /* reset previously-used interfaces */
320 flush_table();
321 parse_bdg_cfg(); /* and set new ones... */
322 if (do_bridge)
323 bdg_promisc_on(); /* re-enable interfaces */
324 }
325 return error ;
326 }
327
328 static int
329 sysctl_refresh(SYSCTL_HANDLER_ARGS)
330 {
331 if (req->newptr)
332 bdgtakeifaces();
333
334 return 0;
335 }
336
337
338 SYSCTL_DECL(_net_link_ether);
339 SYSCTL_PROC(_net_link_ether, OID_AUTO, bridge_cfg, CTLTYPE_STRING|CTLFLAG_RW,
340 &bridge_cfg, sizeof(bridge_cfg), &sysctl_bdg_cfg, "A",
341 "Bridge configuration");
342
343 SYSCTL_PROC(_net_link_ether, OID_AUTO, bridge, CTLTYPE_INT|CTLFLAG_RW,
344 &do_bridge, 0, &sysctl_bdg, "I", "Bridging");
345
346 SYSCTL_INT(_net_link_ether, OID_AUTO, bridge_ipfw, CTLFLAG_RW,
347 &bdg_ipfw,0,"Pass bridged pkts through firewall");
348
349 #define SY(parent, var, comment) \
350 static int var ; \
351 SYSCTL_INT(parent, OID_AUTO, var, CTLFLAG_RW, &(var), 0, comment);
352
353 int bdg_ipfw_drops;
354 SYSCTL_INT(_net_link_ether, OID_AUTO, bridge_ipfw_drop,
355 CTLFLAG_RW, &bdg_ipfw_drops,0,"");
356
357 int bdg_ipfw_colls;
358 SYSCTL_INT(_net_link_ether, OID_AUTO, bridge_ipfw_collisions,
359 CTLFLAG_RW, &bdg_ipfw_colls,0,"");
360
361 SYSCTL_PROC(_net_link_ether, OID_AUTO, bridge_refresh, CTLTYPE_INT|CTLFLAG_WR,
362 NULL, 0, &sysctl_refresh, "I", "iface refresh");
363
364 #if 1 /* diagnostic vars */
365
366 SY(_net_link_ether, verbose, "Be verbose");
367 SY(_net_link_ether, bdg_split_pkts, "Packets split in bdg_forward");
368
369 SY(_net_link_ether, bdg_thru, "Packets through bridge");
370
371 SY(_net_link_ether, bdg_copied, "Packets copied in bdg_forward");
372
373 SY(_net_link_ether, bdg_copy, "Force copy in bdg_forward");
374 SY(_net_link_ether, bdg_predict, "Correctly predicted header location");
375
376 SY(_net_link_ether, bdg_fw_avg, "Cycle counter avg");
377 SY(_net_link_ether, bdg_fw_ticks, "Cycle counter item");
378 SY(_net_link_ether, bdg_fw_count, "Cycle counter count");
379 #endif
380
381 SYSCTL_STRUCT(_net_link_ether, PF_BDG, bdgstats,
382 CTLFLAG_RD, &bdg_stats , bdg_stats, "bridge statistics");
383
384 static int bdg_loops ;
385
386 /*
387 * completely flush the bridge table.
388 */
389 static void
390 flush_table()
391 {
392 int s,i;
393
394 if (bdg_table == NULL)
395 return ;
396 s = splimp();
397 for (i=0; i< HASH_SIZE; i++)
398 bdg_table[i].name= NULL; /* clear table */
399 splx(s);
400 }
401
402 /*
403 * called periodically to flush entries etc.
404 */
405 static void
406 bdg_timeout(void *dummy)
407 {
408 static int slowtimer = 0 ;
409
410 if (do_bridge) {
411 static int age_index = 0 ; /* index of table position to age */
412 int l = age_index + HASH_SIZE/4 ;
413 /*
414 * age entries in the forwarding table.
415 */
416 if (l > HASH_SIZE)
417 l = HASH_SIZE ;
418 for (; age_index < l ; age_index++)
419 if (bdg_table[age_index].used)
420 bdg_table[age_index].used = 0 ;
421 else if (bdg_table[age_index].name) {
422 /* printf("xx flushing stale entry %d\n", age_index); */
423 bdg_table[age_index].name = NULL ;
424 }
425 if (age_index >= HASH_SIZE)
426 age_index = 0 ;
427
428 if (--slowtimer <= 0 ) {
429 slowtimer = 5 ;
430
431 bdg_promisc_on() ; /* we just need unmute, really */
432 bdg_loops = 0 ;
433 }
434 }
435 timeout(bdg_timeout, (void *)0, 2*hz );
436 }
437
438 /*
439 * local MAC addresses are held in a small array. This makes comparisons
440 * much faster.
441 */
442 bdg_addr bdg_addresses[BDG_MAX_PORTS];
443 int bdg_ports ;
444
445 /*
446 * initialization of bridge code. This needs to be done after all
447 * interfaces have been configured.
448 */
449 static void
450 bdginit(void *dummy)
451 {
452
453 if (bdg_table == NULL)
454 bdg_table = (struct hash_table *)
455 _MALLOC(HASH_SIZE * sizeof(struct hash_table),
456 M_IFADDR, M_WAITOK);
457 flush_table();
458
459 ifp2sc = _MALLOC(BDG_MAX_PORTS * sizeof(struct bdg_softc),
460 M_IFADDR, M_WAITOK );
461 bzero(ifp2sc, BDG_MAX_PORTS * sizeof(struct bdg_softc) );
462
463 bzero(&bdg_stats, sizeof(bdg_stats) );
464 bdgtakeifaces();
465 bdg_timeout(0);
466 do_bridge=0;
467 }
468
469 void
470 bdgtakeifaces(void)
471 {
472 int i ;
473 struct ifnet *ifp;
474 bdg_addr *p = bdg_addresses ;
475 struct bdg_softc *bp;
476
477 bdg_ports = 0 ;
478 *bridge_cfg = '\0';
479
480 printf("BRIDGE 010131, have %d interfaces\n", if_index);
481 ifnet_head_lock_shared();
482 for (i = 0 , ifp = ifnet.tqh_first ; i < if_index ;
483 i++, ifp = TAILQ_NEXT(ifp, if_link) )
484 if (ifp->if_type == IFT_ETHER) { /* ethernet ? */
485 ifnet_lladdr_copy_bytes(ifp, p->etheraddr, ETHER_ADDR_LEN);
486 bp = &ifp2sc[ifp->if_index] ;
487 sprintf(bridge_cfg + strlen(bridge_cfg),
488 "%s%d:1,", ifp->if_name, ifp->if_unit);
489 printf("-- index %d %s type %d phy %d addrl %d addr %6D\n",
490 ifp->if_index,
491 bdg_stats.s[ifp->if_index].name,
492 (int)ifp->if_type, (int) ifp->if_physical,
493 (int)ifp->if_addrlen,
494 p->etheraddr, "." );
495 p++ ;
496 bp->ifp = ifp ;
497 bp->flags = IFF_USED ;
498 bp->cluster_id = htons(1) ;
499 bp->magic = 0xDEADBEEF ;
500
501 sprintf(bdg_stats.s[ifp->if_index].name,
502 "%s%d:%d", ifp->if_name, ifp->if_unit,
503 ntohs(bp->cluster_id));
504 bdg_ports ++ ;
505 }
506 ifnet_head_done();
507 }
508
509 /*
510 * bridge_in() is invoked to perform bridging decision on input packets.
511 *
512 * On Input:
513 * eh Ethernet header of the incoming packet.
514 *
515 * On Return: destination of packet, one of
516 * BDG_BCAST broadcast
517 * BDG_MCAST multicast
518 * BDG_LOCAL is only for a local address (do not forward)
519 * BDG_DROP drop the packet
520 * ifp ifp of the destination interface.
521 *
522 * Forwarding is not done directly to give a chance to some drivers
523 * to fetch more of the packet, or simply drop it completely.
524 */
525
526 struct ifnet *
527 bridge_in(struct ifnet *ifp, struct ether_header *eh)
528 {
529 int index;
530 struct ifnet *dst , *old ;
531 int dropit = BDG_MUTED(ifp) ;
532
533 /*
534 * hash the source address
535 */
536 index= HASH_FN(eh->ether_shost);
537 bdg_table[index].used = 1 ;
538 old = bdg_table[index].name ;
539 if ( old ) { /* the entry is valid. */
540 IFP_CHK(old, printf("bridge_in-- reading table\n") );
541
542 if (!BDG_MATCH( eh->ether_shost, bdg_table[index].etheraddr) ) {
543 bdg_ipfw_colls++ ;
544 bdg_table[index].name = NULL ;
545 } else if (old != ifp) {
546 /*
547 * found a loop. Either a machine has moved, or there
548 * is a misconfiguration/reconfiguration of the network.
549 * First, do not forward this packet!
550 * Record the relocation anyways; then, if loops persist,
551 * suspect a reconfiguration and disable forwarding
552 * from the old interface.
553 */
554 bdg_table[index].name = ifp ; /* relocate address */
555 printf("-- loop (%d) %6D to %s%d from %s%d (%s)\n",
556 bdg_loops, eh->ether_shost, ".",
557 ifp->if_name, ifp->if_unit,
558 old->if_name, old->if_unit,
559 BDG_MUTED(old) ? "muted":"active");
560 dropit = 1 ;
561 if ( !BDG_MUTED(old) ) {
562 if (++bdg_loops > 10)
563 BDG_MUTE(old) ;
564 }
565 }
566 }
567
568 /*
569 * now write the source address into the table
570 */
571 if (bdg_table[index].name == NULL) {
572 DEB(printf("new addr %6D at %d for %s%d\n",
573 eh->ether_shost, ".", index, ifp->if_name, ifp->if_unit);)
574 bcopy(eh->ether_shost, bdg_table[index].etheraddr, 6);
575 bdg_table[index].name = ifp ;
576 }
577 dst = bridge_dst_lookup(eh);
578 /* Return values:
579 * BDG_BCAST, BDG_MCAST, BDG_LOCAL, BDG_UNKNOWN, BDG_DROP, ifp.
580 * For muted interfaces, the first 3 are changed in BDG_LOCAL,
581 * and others to BDG_DROP. Also, for incoming packets, ifp is changed
582 * to BDG_DROP in case ifp == src . These mods are not necessary
583 * for outgoing packets from ether_output().
584 */
585 BDG_STAT(ifp, BDG_IN);
586 switch ((int)dst) {
587 case (int)BDG_BCAST:
588 case (int)BDG_MCAST:
589 case (int)BDG_LOCAL:
590 case (int)BDG_UNKNOWN:
591 case (int)BDG_DROP:
592 BDG_STAT(ifp, dst);
593 break ;
594 default :
595 if (dst == ifp || dropit )
596 BDG_STAT(ifp, BDG_DROP);
597 else
598 BDG_STAT(ifp, BDG_FORWARD);
599 break ;
600 }
601
602 if ( dropit ) {
603 if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_LOCAL)
604 return BDG_LOCAL ;
605 else
606 return BDG_DROP ;
607 } else {
608 return (dst == ifp ? BDG_DROP : dst ) ;
609 }
610 }
611
612 /*
613 * Forward to dst, excluding src port and muted interfaces.
614 * If src == NULL, the pkt comes from ether_output, and dst is the real
615 * interface the packet is originally sent to. In this case we must forward
616 * it to the whole cluster. We never call bdg_forward ether_output on
617 * interfaces which are not part of a cluster.
618 *
619 * The packet is freed if possible (i.e. surely not of interest for
620 * the upper layer), otherwise a copy is left for use by the caller
621 * (pointer in m0).
622 *
623 * It would be more efficient to make bdg_forward() always consume
624 * the packet, leaving to the caller the task to check if it needs a copy
625 * and get one in case. As it is now, bdg_forward() can sometimes make
626 * a copy whereas it is not necessary.
627 *
628 * XXX be careful about eh, it can be a pointer into *m
629 */
630 struct mbuf *
631 bdg_forward(struct mbuf *m0, struct ether_header *const eh, struct ifnet *dst)
632 {
633 struct ifnet *src = m0->m_pkthdr.rcvif; /* could be NULL in output */
634 struct ifnet *ifp, *last = NULL ;
635 int s ;
636 int shared = bdg_copy ; /* someone else is using the mbuf */
637 int once = 0; /* loop only once */
638 struct ifnet *real_dst = dst ; /* real dst from ether_output */
639 #ifdef IPFIREWALL
640 struct ip_fw_chain *rule = NULL ; /* did we match a firewall rule ? */
641 #endif
642
643 /*
644 * XXX eh is usually a pointer within the mbuf (some ethernet drivers
645 * do that), so we better copy it before doing anything with the mbuf,
646 * or we might corrupt the header.
647 */
648 struct ether_header save_eh = *eh ;
649
650 #if defined(IPFIREWALL) && defined(DUMMYNET)
651 if (m0->m_type == MT_DUMMYNET) {
652 /* extract info from dummynet header */
653 rule = (struct ip_fw_chain *)(m0->m_data) ;
654 m0 = m0->m_next ;
655 src = m0->m_pkthdr.rcvif;
656 shared = 0 ; /* For sure this is our own mbuf. */
657 } else
658 #endif
659 bdg_thru++; /* only count once */
660
661 if (src == NULL) /* packet from ether_output */
662 dst = bridge_dst_lookup(eh);
663 if (dst == BDG_DROP) { /* this should not happen */
664 printf("xx bdg_forward for BDG_DROP\n");
665 m_freem(m0);
666 return NULL;
667 }
668 if (dst == BDG_LOCAL) { /* this should not happen as well */
669 printf("xx ouch, bdg_forward for local pkt\n");
670 return m0;
671 }
672 if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_UNKNOWN) {
673 ifp = ifnet_head.tqh_first ; /* scan all ports */
674 once = 0 ;
675 if (dst != BDG_UNKNOWN) /* need a copy for the local stack */
676 shared = 1 ;
677 } else {
678 ifp = dst ;
679 once = 1 ;
680 }
681 if ( (u_int)(ifp) <= (u_int)BDG_FORWARD )
682 panic("bdg_forward: bad dst");
683
684 #ifdef IPFIREWALL
685 /*
686 * Do filtering in a very similar way to what is done in ip_output.
687 * Only if firewall is loaded, enabled, and the packet is not
688 * from ether_output() (src==NULL, or we would filter it twice).
689 * Additional restrictions may apply e.g. non-IP, short packets,
690 * and pkts already gone through a pipe.
691 */
692 if (ip_fw_chk_ptr && bdg_ipfw != 0 && src != NULL) {
693 struct ip *ip ;
694 int i;
695
696 if (rule != NULL) /* dummynet packet, already partially processed */
697 goto forward; /* HACK! I should obey the fw_one_pass */
698 if (ntohs(save_eh.ether_type) != ETHERTYPE_IP)
699 goto forward ; /* not an IP packet, ipfw is not appropriate */
700 if (m0->m_pkthdr.len < sizeof(struct ip) )
701 goto forward ; /* header too short for an IP pkt, cannot filter */
702 /*
703 * i need some amt of data to be contiguous, and in case others need
704 * the packet (shared==1) also better be in the first mbuf.
705 */
706 i = min(m0->m_pkthdr.len, max_protohdr) ;
707 if ( shared || m0->m_len < i) {
708 m0 = m_pullup(m0, i) ;
709 if (m0 == NULL) {
710 printf("-- bdg: pullup failed.\n") ;
711 return NULL ;
712 }
713 }
714
715 /*
716 * before calling the firewall, swap fields the same as IP does.
717 * here we assume the pkt is an IP one and the header is contiguous
718 */
719 ip = mtod(m0, struct ip *);
720 NTOHS(ip->ip_len);
721 NTOHS(ip->ip_off);
722
723 /*
724 * The third parameter to the firewall code is the dst. interface.
725 * Since we apply checks only on input pkts we use NULL.
726 * The firewall knows this is a bridged packet as the cookie ptr
727 * is NULL.
728 */
729 i = (*ip_fw_chk_ptr)(&ip, 0, NULL, NULL /* cookie */, &m0, &rule, NULL);
730 if ( (i & IP_FW_PORT_DENY_FLAG) || m0 == NULL) /* drop */
731 return m0 ;
732 /*
733 * If we get here, the firewall has passed the pkt, but the mbuf
734 * pointer might have changed. Restore ip and the fields NTOHS()'d.
735 */
736 ip = mtod(m0, struct ip *);
737 HTONS(ip->ip_len);
738 HTONS(ip->ip_off);
739
740 if (i == 0) /* a PASS rule. */
741 goto forward ;
742 #ifdef DUMMYNET
743 if (i & IP_FW_PORT_DYNT_FLAG) {
744 /*
745 * Pass the pkt to dummynet, which consumes it.
746 * If shared, make a copy and keep the original.
747 * Need to prepend the ethernet header, optimize the common
748 * case of eh pointing already into the original mbuf.
749 */
750 struct mbuf *m ;
751 if (shared) {
752 m = m_copypacket(m0, M_DONTWAIT);
753 if (m == NULL) {
754 printf("bdg_fwd: copy(1) failed\n");
755 return m0;
756 }
757 } else {
758 m = m0 ; /* pass the original to dummynet */
759 m0 = NULL ; /* and nothing back to the caller */
760 }
761 if ( (void *)(eh + 1) == (void *)m->m_data) {
762 m->m_data -= ETHER_HDR_LEN ;
763 m->m_len += ETHER_HDR_LEN ;
764 m->m_pkthdr.len += ETHER_HDR_LEN ;
765 bdg_predict++;
766 } else {
767 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
768 if (!m && verbose) printf("M_PREPEND failed\n");
769 if (m == NULL) /* nope... */
770 return m0 ;
771 bcopy(&save_eh, mtod(m, struct ether_header *), ETHER_HDR_LEN);
772 }
773 dummynet_io((i & 0xffff),DN_TO_BDG_FWD,m,real_dst,NULL,0,rule,0);
774 return m0 ;
775 }
776 #endif
777 /*
778 * XXX add divert/forward actions...
779 */
780 /* if none of the above matches, we have to drop the pkt */
781 bdg_ipfw_drops++ ;
782 printf("bdg_forward: No rules match, so dropping packet!\n");
783 return m0 ;
784 }
785 forward:
786 #endif /* IPFIREWALL */
787 /*
788 * Again, bring up the headers in case of shared bufs to avoid
789 * corruptions in the future.
790 */
791 if ( shared ) {
792 int i = min(m0->m_pkthdr.len, max_protohdr) ;
793
794 m0 = m_pullup(m0, i) ;
795 if (m0 == NULL) {
796 printf("-- bdg: pullup2 failed.\n") ;
797 return NULL ;
798 }
799 }
800 /* now real_dst is used to determine the cluster where to forward */
801 if (src != NULL) /* pkt comes from ether_input */
802 real_dst = src ;
803 for (;;) {
804 if (last) { /* need to forward packet leftover from previous loop */
805 struct mbuf *m ;
806 if (shared == 0 && once ) { /* no need to copy */
807 m = m0 ;
808 m0 = NULL ; /* original is gone */
809 } else {
810 m = m_copypacket(m0, M_DONTWAIT);
811 if (m == NULL) {
812 printf("bdg_forward: sorry, m_copypacket failed!\n");
813 return m0 ; /* the original is still there... */
814 }
815 }
816 /*
817 * Add header (optimized for the common case of eh pointing
818 * already into the mbuf) and execute last part of ether_output:
819 * queue pkt and start output if interface not yet active.
820 */
821 if ( (void *)(eh + 1) == (void *)m->m_data) {
822 m->m_data -= ETHER_HDR_LEN ;
823 m->m_len += ETHER_HDR_LEN ;
824 m->m_pkthdr.len += ETHER_HDR_LEN ;
825 bdg_predict++;
826 } else {
827 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
828 if (!m && verbose) printf("M_PREPEND failed\n");
829 if (m == NULL)
830 return m0;
831 bcopy(&save_eh, mtod(m, struct ether_header *), ETHER_HDR_LEN);
832 }
833 s = splimp();
834 if (IF_QFULL(&last->if_snd)) {
835 IF_DROP(&last->if_snd);
836 #if 0
837 BDG_MUTE(last); /* should I also mute ? */
838 #endif
839 splx(s);
840 m_freem(m); /* consume the pkt anyways */
841 } else {
842 last->if_obytes += m->m_pkthdr.len ;
843 if (m->m_flags & M_MCAST)
844 last->if_omcasts++;
845 if (m->m_pkthdr.len != m->m_len) /* this pkt is on >1 bufs */
846 bdg_split_pkts++;
847
848 IF_ENQUEUE(&last->if_snd, m);
849 if ((last->if_flags & IFF_OACTIVE) == 0)
850 (*last->if_start)(last);
851 splx(s);
852 }
853 BDG_STAT(last, BDG_OUT);
854 last = NULL ;
855 if (once)
856 break ;
857 }
858 if (ifp == NULL)
859 break ;
860 /*
861 * If the interface is used for bridging, not muted, not full,
862 * up and running, is not the source interface, and belongs to
863 * the same cluster as the 'real_dst', then send here.
864 */
865 if ( BDG_USED(ifp) && !BDG_MUTED(ifp) && !IF_QFULL(&ifp->if_snd) &&
866 (ifp->if_flags & (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING) &&
867 ifp != src && BDG_SAMECLUSTER(ifp, real_dst) )
868 last = ifp ;
869 ifp = TAILQ_NEXT(ifp, if_link) ;
870 if (ifp == NULL)
871 once = 1 ;
872 }
873 DEB(bdg_fw_ticks += (u_long)(rdtsc() - ticks) ; bdg_fw_count++ ;
874 if (bdg_fw_count != 0) bdg_fw_avg = bdg_fw_ticks/bdg_fw_count; )
875 return m0 ;
876 }