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