]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/bridge.c
This commit was generated by cvs2svn to compensate for changes in r48905,
[FreeBSD/FreeBSD.git] / sys / net / bridge.c
1 /*
2  * Copyright (c) 1998 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  */
26
27 /*
28  * This code implements bridging in FreeBSD. It only acts on ethernet
29  * type of interfaces (others are still usable for routing).
30  * A bridging table holds the source MAC address/dest. interface for each
31  * known node. The table is indexed using an hash of the source address.
32  *
33  * Input packets are tapped near the end of the input routine in each
34  * driver (near the call to bpf_mtap, or before the call to ether_input)
35  * and analysed 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 bdg_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(). (For the
50  * mute flag i am temporarily using IFF_LINK2 but this has to
51  * change.) Muting is only implemented as a safety measure, and also as
52  * a mechanism to support a user-space implementation of the spanning
53  * tree algorithm. In the final release, unmuting will only occur
54  * because of explicit action of the user-level daemon.
55  *
56  * To build a bridging kernel, use the following option
57  *    option BRIDGE
58  * and then at runtime set the sysctl variable to enable bridging.
59  *
60  * Only one interface is supposed to have addresses set (but
61  * there are no problems in practice if you set addresses for more
62  * than one interface).
63  * Bridging will act before routing, but nothing prevents a machine
64  * from doing both (modulo bugs in the implementation...).
65  *
66  * THINGS TO REMEMBER
67  *  - bridging requires some (small) modifications to the interface
68  *    driver. Currently (980911) the "ed", "de", "tx", "lnc" drivers
69  *    have been modified and tested. "fxp", "ep", "fe" have been
70  *    modified but not tested. See the "ed" and "de" drivers as
71  *    examples on how to operate.
72  *  - bridging is incompatible with multicast routing on the same
73  *    machine. There is not an easy fix to this.
74  *  - loop detection is still not very robust.
75  *  - the interface of bdg_forward() could be improved.
76  */
77
78 #include <sys/param.h>
79 #include <sys/mbuf.h>
80 #include <sys/malloc.h>
81 #include <sys/systm.h>
82 #include <sys/socket.h> /* for net/if.h */
83 #include <sys/kernel.h>
84 #include <sys/sysctl.h>
85
86 #include <net/if.h>
87 #include <net/if_types.h>
88
89 #include <netinet/in.h> /* for struct arpcom */
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.h>
93 #include <netinet/if_ether.h> /* for struct arpcom */
94
95 #include "opt_ipfw.h" 
96 #include "opt_ipdn.h" 
97
98 #if defined(IPFIREWALL)
99 #include <net/route.h>
100 #include <netinet/ip_fw.h>
101 #if defined(DUMMYNET)
102 #include <netinet/ip_dummynet.h>
103 #endif
104 #endif
105
106 #include <net/bridge.h>
107
108 /*
109  * For debugging, you can use the following macros.
110  * remember, rdtsc() only works on Pentium-class machines
111
112     quad_t ticks;
113     DDB(ticks = rdtsc();)
114     ... interesting code ...
115     DDB(bdg_fw_ticks += (u_long)(rdtsc() - ticks) ; bdg_fw_count++ ;)
116
117  *
118  */
119
120 #define DDB(x) x
121 #define DEB(x)
122
123 /*
124  * System initialization
125  */
126
127 static void bdginit(void *);
128 static void flush_table(void);
129
130 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_SECOND, bdginit, NULL)
131
132 static int bdg_ipfw = 0 ;
133 int do_bridge = 0;
134 bdg_hash_table *bdg_table = NULL ;
135
136 /*
137  * we need additional info for the bridge. The bdg_ifp2sc[] array
138  * provides a pointer to this struct using the if_index.
139  * bdg_softc has a backpointer to the struct ifnet, the bridge
140  * flags, and a group (bridging occurs only between port of the
141  * same group).
142  */
143 struct bdg_softc {
144     struct ifnet *ifp ;
145     /* ((struct arpcom *)ifp)->ac_enaddr is the eth. addr */
146     int flags ;
147     int group ;
148 } ;
149     
150 static struct bdg_softc **ifp2sc = NULL ;
151
152 #if 0 /* new code using ifp2sc */
153 #define SAMEGROUP(ifp,src) (src == NULL || \
154     ifp2sc[ifp->if_index]->group == ifp2sc[src->if_index]->group )
155 #define MUTED(ifp) (ifp2sc[ifp->if_index]->flags & IFF_MUTE)
156 #define MUTE(ifp) ifp2sc[ifp->if_index]->flags |= IFF_MUTE
157 #define UNMUTE(ifp) ifp2sc[ifp->if_index]->flags &= ~IFF_MUTE
158 #else
159 #define SAMEGROUP(a,b) 1
160 #define MUTED(ifp) (ifp->if_flags & IFF_MUTE)
161 #define MUTE(ifp) ifp->if_flags |= IFF_MUTE
162 #define UNMUTE(ifp) ifp->if_flags &= ~IFF_MUTE
163 #endif
164
165 static int
166 sysctl_bdg SYSCTL_HANDLER_ARGS
167 {
168     int error, oldval = do_bridge ;
169
170     error = sysctl_handle_int(oidp,
171         oidp->oid_arg1, oidp->oid_arg2, req);
172     printf("called sysctl for bridge name %s arg2 %d val %d->%d\n",
173         oidp->oid_name, oidp->oid_arg2,
174         oldval, do_bridge);
175     if (bdg_table == NULL)
176         do_bridge = 0 ;
177     if (oldval != do_bridge) {
178         flush_table();
179     }
180     return error ;
181 }
182
183 SYSCTL_DECL(_net_link_ether);
184 SYSCTL_PROC(_net_link_ether, OID_AUTO, bridge, CTLTYPE_INT|CTLFLAG_RW,
185            &do_bridge, 0, &sysctl_bdg, "I", "Bridging");
186
187 SYSCTL_INT(_net_link_ether, OID_AUTO, bridge_ipfw, CTLFLAG_RW, &bdg_ipfw,0,"");
188 #if 1 /* diagnostic vars */
189 int bdg_in_count = 0 , bdg_in_ticks = 0 , bdg_fw_count = 0, bdg_fw_ticks = 0 ;
190 SYSCTL_INT(_net_link_ether, OID_AUTO, bdginc, CTLFLAG_RW, &bdg_in_count,0,"");
191 SYSCTL_INT(_net_link_ether, OID_AUTO, bdgint, CTLFLAG_RW, &bdg_in_ticks,0,"");
192 SYSCTL_INT(_net_link_ether, OID_AUTO, bdgfwc, CTLFLAG_RW, &bdg_fw_count,0,"");
193 SYSCTL_INT(_net_link_ether, OID_AUTO, bdgfwt, CTLFLAG_RW, &bdg_fw_ticks,0,"");
194 #endif
195 static struct bdg_stats bdg_stats ;
196 SYSCTL_STRUCT(_net_link_ether, PF_BDG, bdgstats,
197         CTLFLAG_RD, &bdg_stats , bdg_stats, "bridge statistics");
198
199 static int bdg_loops ;
200
201 /*
202  * completely flush the bridge table.
203  */
204 static void
205 flush_table()
206 {   
207     int s,i;
208
209     if (bdg_table == NULL)
210         return ;
211     s = splimp();
212     for (i=0; i< HASH_SIZE; i++)
213         bdg_table[i].name= NULL; /* clear table */
214     splx(s);
215 }
216
217 /*
218  * called periodically to flush entries etc.
219  */
220 static void
221 bdg_timeout(void *dummy)
222 {
223     struct ifnet *ifp ;
224     int s ;
225     static int slowtimer = 0 ;
226
227     if (do_bridge) {
228         static int age_index = 0 ; /* index of table position to age */
229         int l = age_index + HASH_SIZE/4 ;
230         /*
231          * age entries in the forwarding table.
232          */
233         if (l > HASH_SIZE)
234             l = HASH_SIZE ;
235         for (; age_index < l ; age_index++)
236             if (bdg_table[age_index].used)
237                 bdg_table[age_index].used = 0 ;
238             else if (bdg_table[age_index].name) {
239                 /* printf("xx flushing stale entry %d\n", age_index); */
240                 bdg_table[age_index].name = NULL ;
241             }
242         if (age_index >= HASH_SIZE)
243             age_index = 0 ;
244
245         if (--slowtimer <= 0 ) {
246             slowtimer = 5 ;
247
248             for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
249                 if (ifp->if_type != IFT_ETHER)
250                     continue ;
251                 if ( 0 == ( ifp->if_flags & IFF_UP) ) {
252                     s = splimp();
253                     if_up(ifp);
254                     splx(s);
255                 }
256                 if ( 0 == ( ifp->if_flags & IFF_PROMISC) ) {
257                     int ret ;
258                     s = splimp();
259                     ret = ifpromisc(ifp, 1);
260                     splx(s);
261                     printf(">> now  %s%d flags 0x%x promisc %d\n",
262                         ifp->if_name, ifp->if_unit,
263                         ifp->if_flags, ret);
264                 }
265                 if (MUTED(ifp)) {
266                     printf(">> unmuting %s%d\n", ifp->if_name, ifp->if_unit);
267                     UNMUTE(ifp) ;
268                 }
269             }
270             bdg_loops = 0 ;
271         }
272     }
273     timeout(bdg_timeout, (void *)0, 2*hz );
274 }
275
276 /*
277  * local MAC addresses are held in a small array. This makes comparisons
278  * much faster.
279  */
280 unsigned char bdg_addresses[6*BDG_MAX_PORTS];
281 int bdg_ports ;
282
283 /*
284  * initialization of bridge code.
285  *
286  * This will have to change to support kldload.
287  */
288 static void
289 bdginit(dummy)
290         void *dummy;
291 {
292     int i, s;
293     struct ifnet *ifp;
294     struct arpcom *ac ;
295     u_char *eth_addr ;
296
297     /*
298      * initialization of bridge code
299      */
300     s = splimp();       /* XXX does this matter? */
301     if (bdg_table == NULL)
302         bdg_table = (struct hash_table *)
303                 malloc(HASH_SIZE * sizeof(struct hash_table),
304                     M_IFADDR, M_WAITOK);
305     flush_table();
306
307     ifp2sc = malloc(if_index * sizeof(struct bdg_softc *), M_IFADDR, M_WAITOK );
308     bzero(ifp2sc, if_index * sizeof(struct bdg_softc *) );
309
310     bzero(&bdg_stats, sizeof(bdg_stats) );
311     bdg_ports = 0 ;
312     eth_addr = bdg_addresses ;
313
314     printf("BRIDGE 981214, have %d interfaces\n", if_index);
315     for (i = 0 , ifp = ifnet.tqh_first ; i < if_index ;
316                 i++, ifp = ifp->if_link.tqe_next)
317         if (ifp->if_type == IFT_ETHER) { /* ethernet ? */
318             ac = (struct arpcom *)ifp;
319         sprintf(bdg_stats.s[ifp->if_index].name,
320             "%s%d", ifp->if_name, ifp->if_unit);
321         printf("-- index %d %s type %d phy %d addrl %d addr %6D\n",
322             ifp->if_index,
323             bdg_stats.s[ifp->if_index].name,
324             (int)ifp->if_type, (int) ifp->if_physical,
325             (int)ifp->if_addrlen,
326             ac->ac_enaddr, "." );
327         bcopy(ac->ac_enaddr, eth_addr, 6);
328         eth_addr += 6 ;
329
330         ifp2sc[bdg_ports] = malloc(sizeof(struct bdg_softc),
331                 M_IFADDR, M_WAITOK );
332         ifp2sc[bdg_ports]->ifp = ifp ;
333         ifp2sc[bdg_ports]->flags = 0 ;
334         ifp2sc[bdg_ports]->group = 0 ;
335         bdg_ports ++ ;
336     }
337     bdg_timeout(0);
338     do_bridge=0;
339     splx(s);
340 }
341
342 /*
343  * bridge_in() is invoked to perform bridging decision on input packets.
344  * On Input:
345  *   m          packet to be bridged. The mbuf need not to hold the
346  *              whole packet, only the first 14 bytes suffice. We
347  *              assume them to be contiguous. No alignment assumptions
348  *              because they are not a problem on i386 class machines.
349  *
350  * On Return: destination of packet, one of
351  *   BDG_BCAST  broadcast
352  *   BDG_MCAST  multicast
353  *   BDG_LOCAL  is only for a local address (do not forward)
354  *   BDG_DROP   drop the packet
355  *   ifp        ifp of the destination interface.
356  *
357  * Forwarding is not done directly to give a chance to some drivers
358  * to fetch more of the packet, or simply drop it completely.
359  */
360
361
362 struct ifnet *
363 bridge_in(struct mbuf *m)
364 {
365     int index;
366     struct ifnet *ifp = m->m_pkthdr.rcvif,  *dst , *old ;
367     int dropit = MUTED(ifp) ;
368     struct ether_header *eh;
369
370     eh = mtod(m, struct ether_header *);
371
372     /*
373      * hash the source address
374      */
375     index= HASH_FN(eh->ether_shost);
376     bdg_table[index].used = 1 ;
377     old = bdg_table[index].name ;
378     if ( old ) { /* the entry is valid. */
379         if (!BDG_MATCH( eh->ether_shost, bdg_table[index].etheraddr) ) {
380             printf("collision at %d\n", index);
381             bdg_table[index].name = NULL ;
382         } else if (old != ifp) {
383             /*
384              * found a loop. Either a machine has moved, or there
385              * is a misconfiguration/reconfiguration of the network.
386              * First, do not forward this packet!
387              * Record the relocation anyways; then, if loops persist,
388              * suspect a reconfiguration and disable forwarding
389              * from the old interface.
390              */
391             bdg_table[index].name = ifp ; /* relocate address */
392             printf("-- loop (%d) %6D to %s%d from %s%d (%s)\n",
393                         bdg_loops, eh->ether_shost, ".",
394                         ifp->if_name, ifp->if_unit,
395                         old->if_name, old->if_unit,
396                         old->if_flags & IFF_MUTE ? "muted":"ignore");
397             dropit = 1 ;
398             if ( !MUTED(old) ) {
399                 if (++bdg_loops > 10)
400                     MUTE(old) ;
401             }
402         }
403     }
404
405     /*
406      * now write the source address into the table
407      */
408     if (bdg_table[index].name == NULL) {
409         DEB(printf("new addr %6D at %d for %s%d\n",
410             eh->ether_shost, ".", index, ifp->if_name, ifp->if_unit);)
411         bcopy(eh->ether_shost, bdg_table[index].etheraddr, 6);
412         bdg_table[index].name = ifp ;
413     }
414     dst = bridge_dst_lookup(m);
415     /* Return values:
416      *   BDG_BCAST, BDG_MCAST, BDG_LOCAL, BDG_UNKNOWN, BDG_DROP, ifp.
417      * For muted interfaces, the first 3 are changed in BDG_LOCAL,
418      * and others to BDG_DROP. Also, for incoming packets, ifp is changed
419      * to BDG_DROP in case ifp == src . These mods are not necessary
420      * for outgoing packets from ether_output().
421      */
422     BDG_STAT(ifp, BDG_IN);
423     switch ((int)dst) {
424     case (int)BDG_BCAST:
425     case (int)BDG_MCAST:
426     case (int)BDG_LOCAL:
427     case (int)BDG_UNKNOWN:
428     case (int)BDG_DROP:
429         BDG_STAT(ifp, dst);
430         break ;
431     default :
432         if (dst == ifp || dropit )
433             BDG_STAT(ifp, BDG_DROP);
434         else
435             BDG_STAT(ifp, BDG_FORWARD);
436         break ;
437     }
438
439     if ( dropit ) {
440         if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_LOCAL)
441             return BDG_LOCAL ;
442         else
443             return BDG_DROP ;
444     } else {
445         return (dst == ifp ? BDG_DROP : dst ) ;
446     }
447 }
448
449 /*
450  * Forward to dst, excluding src port and (if not a single interface)
451  * muted interfaces. The packet is freed if marked as such
452  * and not for a local destination.
453  * A cleaner implementation would be to make bdg_forward()
454  * always consume the packet, leaving to the caller the task
455  * to make a copy if it needs it. As it is now, bdg_forward()
456  * can keep a copy alive in some cases.
457  */
458 int
459 bdg_forward (struct mbuf **m0, struct ifnet *dst)
460 {
461     struct ifnet *src = (*m0)->m_pkthdr.rcvif; /* could be NULL in output */
462     struct ifnet *ifp ;
463     int error=0, s ;
464     int once = 0;       /* execute the loop only once */
465     int canfree = 1 ; /* can free the buf at the end */
466     struct mbuf *m ;
467 #ifdef IPFIREWALL
468     struct ip *ip;
469     struct ether_header *eh = mtod(*m0, struct ether_header *); /* XXX */
470 #endif
471
472     if (dst == BDG_DROP) { /* this should not happen */
473         printf("xx bdg_forward for BDG_DROP)\n");
474         m_freem(*m0) ;
475         *m0 = NULL ;
476         return 0;
477     }
478     if (dst == BDG_LOCAL) { /* this should not happen as well */
479         printf("xx ouch, bdg_forward for local pkt\n");
480         return 0;
481     }
482     if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_UNKNOWN) {
483         ifp = ifnet.tqh_first ;
484         once = 0 ;
485         if (dst != BDG_UNKNOWN)
486             canfree = 0 ;
487     } else {
488         ifp = dst ;
489         once = 1 ; /* and also canfree */
490     }
491 #ifdef IPFIREWALL
492     /*
493      * do filtering in a very similar way to what is done
494      * in ip_output. Only for IP packets, and only pass/fail/dummynet
495      * is supported. The tricky thing is to make sure that enough of
496      * the packet (basically, Eth+IP+TCP/UDP headers) is contiguous
497      * so that calls to m_pullup in ip_fw_chk will not kill the
498      * ethernet header.
499      */
500     if (ip_fw_chk_ptr) {
501         u_int16_t dummy ;
502         struct ip_fw_chain *rule;
503         int off;
504
505         m = *m0 ;
506         if (m->m_type == MT_DUMMYNET) {
507             /*
508              * the packet was already tagged, so part of the
509              * processing was already done, and we need to go down.
510              */
511             rule = (struct ip_fw_chain *)(m->m_data) ;
512             (*m0) = m = m->m_next ;
513
514             src = m->m_pkthdr.rcvif; /* could be NULL in output */
515             eh = mtod(m, struct ether_header *); /* XXX */
516             canfree = 1 ; /* for sure, a copy is not needed later. */
517             goto forward; /* HACK! */
518         } else
519             rule = NULL ;
520         if (bdg_ipfw == 0)
521             goto forward ;
522         if (src == NULL)
523             goto forward ; /* do not apply to packets from ether_output */
524         if (canfree == 0 ) /* need to make a copy */
525             m = m_copypacket(*m0, M_DONTWAIT);
526         if (m == NULL) {
527             /* fail... */
528             return 0 ;
529         }
530         
531         dummy = 0 ;
532         /*
533          * before calling the firewall, swap fields the same as IP does.
534          * here we assume the pkt is an IP one and the header is contiguous
535          */
536         eh = mtod(m, struct ether_header *);
537         ip = (struct ip *)(eh + 1 ) ;
538         NTOHS(ip->ip_len);
539         NTOHS(ip->ip_id);
540         NTOHS(ip->ip_off);
541
542         /*
543          * The third parameter to the firewall code is the dst.  interface.
544          * Since we apply checks only on input pkts we use NULL.
545          */
546         off = (*ip_fw_chk_ptr)(NULL, 0, NULL, &dummy, &m, &rule, NULL) ;
547         if (m == NULL) { /* pkt discarded by firewall */
548             if (canfree)
549                 *m0 = NULL ;
550             return 0 ;
551         }
552         /*
553          * on return, the mbuf pointer might have changed. Restore
554          * *m0 (if it was the same as m), eh, ip and then
555          * restore original ordering.
556          */
557         eh = mtod(m, struct ether_header *);
558         ip = (struct ip *)(eh + 1 ) ;
559         if (canfree) /* m was a reference to *m0, so update *m0 */
560                 *m0 = m ;
561         HTONS(ip->ip_len);
562         HTONS(ip->ip_id);
563         HTONS(ip->ip_off);
564         if (off == 0) {
565             if (canfree == 0)
566                 m_freem(m);
567             goto forward ;
568         }
569 #ifdef DUMMYNET
570         if (off & 0x10000) {  
571             /*
572              * pass the pkt to dummynet. Need to include m, dst, rule.
573              * Dummynet consumes the packet in all cases.
574              */
575             dummynet_io((off & 0xffff), DN_TO_BDG_FWD, m, dst, NULL, 0, rule);
576             if (canfree) /* dummynet has consumed the original one */
577                 *m0 = NULL ;
578             return 0 ;
579         }
580 #endif
581         /* if none of the above matches, we have to drop the pkt */
582         if (m)
583             m_freem(m);
584         if (canfree && m != *m0) {
585             m_freem(*m0);
586             *m0 = NULL ;
587         }
588         return 0 ;
589     }
590 forward:
591 #endif /* IPFIREWALL */
592     if (canfree && once)
593         m = *m0 ;
594     else
595         m = NULL ;
596
597     for ( ; ifp ; ifp = ifp->if_link.tqe_next ) {
598         if (ifp != src && ifp->if_type == IFT_ETHER &&
599             (ifp->if_flags & (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING) &&
600             SAMEGROUP(ifp, src) && !MUTED(ifp) ) {
601             if (m == NULL) { /* do i need to make a copy ? */
602                 if (canfree && ifp->if_link.tqe_next == NULL) /* last one! */
603                     m = *m0 ;
604                 else /* on a P5-90, m_packetcopy takes 540 ticks */
605                     m = m_copypacket(*m0, M_DONTWAIT);
606                 if (m == NULL) {
607                     printf("bdg_forward: sorry, m_copy failed!\n");
608                     return ENOBUFS ;
609                 }
610             }
611             /*
612              * execute last part of ether_output.
613              */
614             s = splimp();
615             /*
616              * Queue message on interface, and start output if interface
617              * not yet active.
618              */
619             if (IF_QFULL(&ifp->if_snd)) {
620                 IF_DROP(&ifp->if_snd);
621                 MUTE(ifp); /* good measure... */
622                 splx(s);
623                 error = ENOBUFS ;
624             } else {
625                 ifp->if_obytes += m->m_pkthdr.len ;
626                 if (m->m_flags & M_MCAST)
627                     ifp->if_omcasts++;
628                 IF_ENQUEUE(&ifp->if_snd, m);
629                 if ((ifp->if_flags & IFF_OACTIVE) == 0)
630                     (*ifp->if_start)(ifp);
631                 splx(s);
632                 if (m == *m0)
633                     *m0 = NULL ; /* the packet is gone... */
634                 m = NULL ;
635             }
636             BDG_STAT(ifp, BDG_OUT);
637         }
638         if (once)
639             break ;
640     }
641
642     /* cleanup any mbuf leftover. */
643     if (m)
644         m_freem(m);
645     if (m == *m0)
646         *m0 = NULL ;
647     if (canfree && *m0) {
648         m_freem(*m0);
649         *m0 = NULL ;
650     }
651     return error ;
652 }