]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1998 Luigi Rizzo | |
24 | * | |
25 | * Redistribution and use in source and binary forms, with or without | |
26 | * modification, are permitted provided that the following conditions | |
27 | * are met: | |
28 | * 1. Redistributions of source code must retain the above copyright | |
29 | * notice, this list of conditions and the following disclaimer. | |
30 | * 2. Redistributions in binary form must reproduce the above copyright | |
31 | * notice, this list of conditions and the following disclaimer in the | |
32 | * documentation and/or other materials provided with the distribution. | |
33 | * | |
34 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND | |
35 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
36 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
37 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
38 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
39 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
40 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
41 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
42 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
43 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
44 | * SUCH DAMAGE. | |
45 | * | |
46 | */ | |
47 | ||
48 | /* | |
49 | * This code implements bridging in FreeBSD. It only acts on ethernet | |
50 | * type of interfaces (others are still usable for routing). | |
51 | * A bridging table holds the source MAC address/dest. interface for each | |
52 | * known node. The table is indexed using an hash of the source address. | |
53 | * | |
54 | * Input packets are tapped near the end of the input routine in each | |
55 | * driver (near the call to bpf_mtap, or before the call to ether_input) | |
56 | * and analysed calling bridge_in(). Depending on the result, the packet | |
57 | * can be forwarded to one or more output interfaces using bdg_forward(), | |
58 | * and/or sent to the upper layer (e.g. in case of multicast). | |
59 | * | |
60 | * Output packets are intercepted near the end of ether_output(), | |
61 | * the correct destination is selected calling bdg_dst_lookup(), | |
62 | * and then forwarding is done using bdg_forward(). | |
63 | * Bridging is controlled by the sysctl variable net.link.ether.bridge | |
64 | * | |
65 | * The arp code is also modified to let a machine answer to requests | |
66 | * irrespective of the port the request came from. | |
67 | * | |
68 | * In case of loops in the bridging topology, the bridge detects this | |
69 | * event and temporarily mutes output bridging on one of the ports. | |
70 | * Periodically, interfaces are unmuted by bdg_timeout(). (For the | |
71 | * mute flag i am temporarily using IFF_LINK2 but this has to | |
72 | * change.) 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 requires some (small) modifications to the interface | |
89 | * driver. Currently (980911) the "ed", "de", "tx", "lnc" drivers | |
90 | * have been modified and tested. "fxp", "ep", "fe" have been | |
91 | * modified but not tested. See the "ed" and "de" drivers as | |
92 | * examples on how to operate. | |
93 | * - bridging is incompatible with multicast routing on the same | |
94 | * machine. There is not an easy fix to this. | |
95 | * - loop detection is still not very robust. | |
96 | * - the interface of bdg_forward() could be improved. | |
97 | */ | |
98 | ||
99 | #include <sys/param.h> | |
100 | #include <sys/mbuf.h> | |
101 | #include <sys/malloc.h> | |
102 | #include <sys/systm.h> | |
103 | #include <sys/socket.h> /* for net/if.h */ | |
104 | #include <sys/kernel.h> | |
105 | #include <sys/sysctl.h> | |
106 | ||
107 | #include <net/if.h> | |
108 | #include <net/if_types.h> | |
109 | ||
110 | #include <netinet/in.h> /* for struct arpcom */ | |
111 | #include <netinet/in_systm.h> | |
112 | #include <netinet/in_var.h> | |
113 | #include <netinet/ip.h> | |
114 | #include <netinet/if_ether.h> /* for struct arpcom */ | |
115 | ||
116 | #include "opt_ipfw.h" | |
117 | #include "opt_ipdn.h" | |
118 | ||
119 | #if defined(IPFIREWALL) && defined(DUMMYNET) | |
120 | #include <net/route.h> | |
121 | #include <netinet/ip_fw.h> | |
122 | #include <netinet/ip_dummynet.h> | |
123 | #endif | |
124 | ||
125 | #include <net/bridge.h> | |
126 | ||
127 | /* | |
128 | * For debugging, you can use the following macros. | |
129 | * remember, rdtsc() only works on Pentium-class machines | |
130 | ||
131 | quad_t ticks; | |
132 | DDB(ticks = rdtsc();) | |
133 | ... interesting code ... | |
134 | DDB(bdg_fw_ticks += (u_long)(rdtsc() - ticks) ; bdg_fw_count++ ;) | |
135 | ||
136 | * | |
137 | */ | |
138 | ||
139 | #define DDB(x) x | |
140 | #define DEB(x) | |
141 | ||
142 | /* | |
143 | * System initialization | |
144 | */ | |
145 | ||
146 | static void bdginit(void *); | |
147 | static void flush_table(void); | |
148 | ||
149 | SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, bdginit, NULL) | |
150 | ||
151 | static int bdg_ipfw = 0 ; | |
152 | int do_bridge = 0; | |
153 | bdg_hash_table *bdg_table = NULL ; | |
154 | ||
155 | /* | |
156 | * we need additional info for the bridge. The bdg_ifp2sc[] array | |
157 | * provides a pointer to this struct using the if_index. | |
158 | * bdg_softc has a backpointer to the struct ifnet, the bridge | |
159 | * flags, and a group (bridging occurs only between port of the | |
160 | * same group). | |
161 | */ | |
162 | struct bdg_softc { | |
163 | struct ifnet *ifp ; | |
164 | /* ((struct arpcom *)ifp)->ac_enaddr is the eth. addr */ | |
165 | int flags ; | |
166 | int group ; | |
167 | } ; | |
168 | ||
169 | static struct bdg_softc **ifp2sc = NULL ; | |
170 | ||
171 | #if 0 /* new code using ifp2sc */ | |
172 | #define SAMEGROUP(ifp,src) (src == NULL || \ | |
173 | ifp2sc[ifp->if_index]->group == ifp2sc[src->if_index]->group ) | |
174 | #define MUTED(ifp) (ifp2sc[ifp->if_index]->flags & IFF_MUTE) | |
175 | #define MUTE(ifp) ifp2sc[ifp->if_index]->flags |= IFF_MUTE | |
176 | #define UNMUTE(ifp) ifp2sc[ifp->if_index]->flags &= ~IFF_MUTE | |
177 | #else | |
178 | #define SAMEGROUP(a,b) 1 | |
179 | #define MUTED(ifp) (ifp->if_flags & IFF_MUTE) | |
180 | #define MUTE(ifp) ifp->if_flags |= IFF_MUTE | |
181 | #define UNMUTE(ifp) ifp->if_flags &= ~IFF_MUTE | |
182 | #endif | |
183 | ||
184 | static int | |
185 | sysctl_bdg SYSCTL_HANDLER_ARGS | |
186 | { | |
187 | int error, oldval = do_bridge ; | |
188 | ||
189 | error = sysctl_handle_int(oidp, | |
190 | oidp->oid_arg1, oidp->oid_arg2, req); | |
191 | printf("called sysctl for bridge name %s arg2 %d val %d->%d\n", | |
192 | oidp->oid_name, oidp->oid_arg2, | |
193 | oldval, do_bridge); | |
194 | if (bdg_table == NULL) | |
195 | do_bridge = 0 ; | |
196 | if (oldval != do_bridge) { | |
197 | flush_table(); | |
198 | } | |
199 | return error ; | |
200 | } | |
201 | ||
202 | SYSCTL_DECL(_net_link_ether); | |
203 | SYSCTL_PROC(_net_link_ether, OID_AUTO, bridge, CTLTYPE_INT|CTLFLAG_RW, | |
204 | &do_bridge, 0, &sysctl_bdg, "I", "Bridging"); | |
205 | ||
206 | SYSCTL_INT(_net_link_ether, OID_AUTO, bridge_ipfw, CTLFLAG_RW, &bdg_ipfw,0,""); | |
207 | #if 1 /* diagnostic vars */ | |
208 | int bdg_in_count = 0 , bdg_in_ticks = 0 , bdg_fw_count = 0, bdg_fw_ticks = 0 ; | |
209 | SYSCTL_INT(_net_link_ether, OID_AUTO, bdginc, CTLFLAG_RW, &bdg_in_count,0,""); | |
210 | SYSCTL_INT(_net_link_ether, OID_AUTO, bdgint, CTLFLAG_RW, &bdg_in_ticks,0,""); | |
211 | SYSCTL_INT(_net_link_ether, OID_AUTO, bdgfwc, CTLFLAG_RW, &bdg_fw_count,0,""); | |
212 | SYSCTL_INT(_net_link_ether, OID_AUTO, bdgfwt, CTLFLAG_RW, &bdg_fw_ticks,0,""); | |
213 | #endif | |
214 | static struct bdg_stats bdg_stats ; | |
215 | SYSCTL_STRUCT(_net_link_ether, PF_BDG, bdgstats, | |
216 | CTLFLAG_RD, &bdg_stats , bdg_stats, "bridge statistics"); | |
217 | ||
218 | static int bdg_loops ; | |
219 | ||
220 | /* | |
221 | * completely flush the bridge table. | |
222 | */ | |
223 | static void | |
224 | flush_table() | |
225 | { | |
226 | int s,i; | |
227 | ||
228 | if (bdg_table == NULL) | |
229 | return ; | |
230 | s = splimp(); | |
231 | for (i=0; i< HASH_SIZE; i++) | |
232 | bdg_table[i].name= NULL; /* clear table */ | |
233 | splx(s); | |
234 | } | |
235 | ||
236 | /* wrapper for funnel */ | |
237 | void | |
238 | bdg_timeout_funneled(void * dummy) | |
239 | { | |
240 | boolean_t funnel_state; | |
241 | ||
242 | funnel_state = thread_funnel_set(network_flock, TRUE); | |
243 | bdg_timeout(dummy); | |
244 | funnel_state = thread_funnel_set(network_flock, FALSE); | |
245 | } | |
246 | ||
247 | /* | |
248 | * called periodically to flush entries etc. | |
249 | */ | |
250 | static void | |
251 | bdg_timeout(void *dummy) | |
252 | { | |
253 | struct ifnet *ifp ; | |
254 | int s ; | |
255 | static int slowtimer = 0 ; | |
256 | boolean_t funnel_state; | |
257 | ||
258 | ||
259 | if (do_bridge) { | |
260 | static int age_index = 0 ; /* index of table position to age */ | |
261 | int l = age_index + HASH_SIZE/4 ; | |
262 | /* | |
263 | * age entries in the forwarding table. | |
264 | */ | |
265 | if (l > HASH_SIZE) | |
266 | l = HASH_SIZE ; | |
267 | for (; age_index < l ; age_index++) | |
268 | if (bdg_table[age_index].used) | |
269 | bdg_table[age_index].used = 0 ; | |
270 | else if (bdg_table[age_index].name) { | |
271 | /* printf("xx flushing stale entry %d\n", age_index); */ | |
272 | bdg_table[age_index].name = NULL ; | |
273 | } | |
274 | if (age_index >= HASH_SIZE) | |
275 | age_index = 0 ; | |
276 | ||
277 | if (--slowtimer <= 0 ) { | |
278 | slowtimer = 5 ; | |
279 | ||
280 | for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { | |
281 | if (ifp->if_type != IFT_ETHER) | |
282 | continue ; | |
283 | if ( 0 == ( ifp->if_flags & IFF_UP) ) { | |
284 | s = splimp(); | |
285 | if_up(ifp); | |
286 | splx(s); | |
287 | } | |
288 | if ( 0 == ( ifp->if_flags & IFF_PROMISC) ) { | |
289 | int ret ; | |
290 | s = splimp(); | |
291 | ret = ifpromisc(ifp, 1); | |
292 | splx(s); | |
293 | printf(">> now %s%d flags 0x%x promisc %d\n", | |
294 | ifp->if_name, ifp->if_unit, | |
295 | ifp->if_flags, ret); | |
296 | } | |
297 | if (MUTED(ifp)) { | |
298 | printf(">> unmuting %s%d\n", ifp->if_name, ifp->if_unit); | |
299 | UNMUTE(ifp) ; | |
300 | } | |
301 | } | |
302 | bdg_loops = 0 ; | |
303 | } | |
304 | } | |
305 | timeout(bdg_timeout_funneled, (void *)0, 2*hz ); | |
306 | ||
307 | } | |
308 | ||
309 | /* | |
310 | * local MAC addresses are held in a small array. This makes comparisons | |
311 | * much faster. | |
312 | */ | |
313 | unsigned char bdg_addresses[6*BDG_MAX_PORTS]; | |
314 | int bdg_ports ; | |
315 | ||
316 | /* | |
317 | * initialization of bridge code. | |
318 | */ | |
319 | static void | |
320 | bdginit(dummy) | |
321 | void *dummy; | |
322 | { | |
323 | int i ; | |
324 | struct ifnet *ifp; | |
325 | struct arpcom *ac ; | |
326 | u_char *eth_addr ; | |
327 | /* | |
328 | * initialization of bridge code | |
329 | */ | |
330 | if (bdg_table == NULL) | |
331 | bdg_table = (struct hash_table *) | |
332 | _MALLOC(HASH_SIZE * sizeof(struct hash_table), | |
333 | M_IFADDR, M_WAITOK); | |
0b4e3aa0 A |
334 | if (bdg_table == NULL) |
335 | return (ENOBUFS); | |
1c79356b A |
336 | flush_table(); |
337 | ||
338 | ifp2sc = _MALLOC(if_index * sizeof(struct bdg_softc *), M_IFADDR, M_WAITOK ); | |
0b4e3aa0 A |
339 | if (ifp2sc == NULL) |
340 | return (ENOBUFS); | |
1c79356b A |
341 | bzero(ifp2sc, if_index * sizeof(struct bdg_softc *) ); |
342 | ||
343 | bzero(&bdg_stats, sizeof(bdg_stats) ); | |
344 | bdg_ports = 0 ; | |
345 | eth_addr = bdg_addresses ; | |
346 | ||
347 | printf("BRIDGE 981214, have %d interfaces\n", if_index); | |
348 | for (i = 0 , ifp = ifnet.tqh_first ; i < if_index ; | |
349 | i++, ifp = ifp->if_link.tqe_next) | |
350 | if (ifp->if_type == IFT_ETHER) { /* ethernet ? */ | |
351 | ac = (struct arpcom *)ifp; | |
352 | sprintf(bdg_stats.s[ifp->if_index].name, | |
353 | "%s%d", ifp->if_name, ifp->if_unit); | |
354 | printf("-- index %d %s type %d phy %d addrl %d addr %6D\n", | |
355 | ifp->if_index, | |
356 | bdg_stats.s[ifp->if_index].name, | |
357 | (int)ifp->if_type, (int) ifp->if_physical, | |
358 | (int)ifp->if_addrlen, | |
359 | ac->ac_enaddr, "." ); | |
360 | bcopy(ac->ac_enaddr, eth_addr, 6); | |
361 | eth_addr += 6 ; | |
362 | ||
363 | ifp2sc[bdg_ports] = _MALLOC(sizeof(struct bdg_softc), | |
364 | M_IFADDR, M_WAITOK ); | |
0b4e3aa0 A |
365 | if (ifp2sc[bdg_ports] == NULL) |
366 | return (ENOBUFS); | |
1c79356b A |
367 | ifp2sc[bdg_ports]->ifp = ifp ; |
368 | ifp2sc[bdg_ports]->flags = 0 ; | |
369 | ifp2sc[bdg_ports]->group = 0 ; | |
370 | bdg_ports ++ ; | |
371 | } | |
372 | bdg_timeout(0); | |
373 | do_bridge=0; | |
374 | } | |
375 | ||
376 | /* | |
377 | * bridge_in() is invoked to perform bridging decision on input packets. | |
378 | * On Input: | |
379 | * m packet to be bridged. The mbuf need not to hold the | |
380 | * whole packet, only the first 14 bytes suffice. We | |
381 | * assume them to be contiguous. No alignment assumptions | |
382 | * because they are not a problem on i386 class machines. | |
383 | * | |
384 | * On Return: destination of packet, one of | |
385 | * BDG_BCAST broadcast | |
386 | * BDG_MCAST multicast | |
387 | * BDG_LOCAL is only for a local address (do not forward) | |
388 | * BDG_DROP drop the packet | |
389 | * ifp ifp of the destination interface. | |
390 | * | |
391 | * Forwarding is not done directly to give a chance to some drivers | |
392 | * to fetch more of the packet, or simply drop it completely. | |
393 | */ | |
394 | ||
395 | ||
396 | struct ifnet * | |
397 | bridge_in(struct mbuf *m) | |
398 | { | |
399 | int index; | |
400 | struct ifnet *ifp = m->m_pkthdr.rcvif, *dst , *old ; | |
401 | int dropit = MUTED(ifp) ; | |
402 | struct ether_header *eh; | |
403 | ||
404 | eh = mtod(m, struct ether_header *); | |
405 | ||
406 | /* | |
407 | * hash the source address | |
408 | */ | |
409 | index= HASH_FN(eh->ether_shost); | |
410 | bdg_table[index].used = 1 ; | |
411 | old = bdg_table[index].name ; | |
412 | if ( old ) { /* the entry is valid. */ | |
413 | if (!BDG_MATCH( eh->ether_shost, bdg_table[index].etheraddr) ) { | |
414 | printf("collision at %d\n", index); | |
415 | bdg_table[index].name = NULL ; | |
416 | } else if (old != ifp) { | |
417 | /* | |
418 | * found a loop. Either a machine has moved, or there | |
419 | * is a misconfiguration/reconfiguration of the network. | |
420 | * First, do not forward this packet! | |
421 | * Record the relocation anyways; then, if loops persist, | |
422 | * suspect a reconfiguration and disable forwarding | |
423 | * from the old interface. | |
424 | */ | |
425 | bdg_table[index].name = ifp ; /* relocate address */ | |
426 | printf("-- loop (%d) %6D to %s%d from %s%d (%s)\n", | |
427 | bdg_loops, eh->ether_shost, ".", | |
428 | ifp->if_name, ifp->if_unit, | |
429 | old->if_name, old->if_unit, | |
430 | old->if_flags & IFF_MUTE ? "muted":"ignore"); | |
431 | dropit = 1 ; | |
432 | if ( !MUTED(old) ) { | |
433 | if (++bdg_loops > 10) | |
434 | MUTE(old) ; | |
435 | } | |
436 | } | |
437 | } | |
438 | ||
439 | /* | |
440 | * now write the source address into the table | |
441 | */ | |
442 | if (bdg_table[index].name == NULL) { | |
443 | DEB(printf("new addr %6D at %d for %s%d\n", | |
444 | eh->ether_shost, ".", index, ifp->if_name, ifp->if_unit);) | |
445 | bcopy(eh->ether_shost, bdg_table[index].etheraddr, 6); | |
446 | bdg_table[index].name = ifp ; | |
447 | } | |
448 | dst = bridge_dst_lookup(m); | |
449 | /* Return values: | |
450 | * BDG_BCAST, BDG_MCAST, BDG_LOCAL, BDG_UNKNOWN, BDG_DROP, ifp. | |
451 | * For muted interfaces, the first 3 are changed in BDG_LOCAL, | |
452 | * and others to BDG_DROP. Also, for incoming packets, ifp is changed | |
453 | * to BDG_DROP in case ifp == src . These mods are not necessary | |
454 | * for outgoing packets from ether_output(). | |
455 | */ | |
456 | BDG_STAT(ifp, BDG_IN); | |
457 | switch ((int)dst) { | |
458 | case (int)BDG_BCAST: | |
459 | case (int)BDG_MCAST: | |
460 | case (int)BDG_LOCAL: | |
461 | case (int)BDG_UNKNOWN: | |
462 | case (int)BDG_DROP: | |
463 | BDG_STAT(ifp, dst); | |
464 | break ; | |
465 | default : | |
466 | if (dst == ifp || dropit ) | |
467 | BDG_STAT(ifp, BDG_DROP); | |
468 | else | |
469 | BDG_STAT(ifp, BDG_FORWARD); | |
470 | break ; | |
471 | } | |
472 | ||
473 | if ( dropit ) { | |
474 | if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_LOCAL) | |
475 | return BDG_LOCAL ; | |
476 | else | |
477 | return BDG_DROP ; | |
478 | } else { | |
479 | return (dst == ifp ? BDG_DROP : dst ) ; | |
480 | } | |
481 | } | |
482 | ||
483 | /* | |
484 | * Forward to dst, excluding src port and (if not a single interface) | |
485 | * muted interfaces. The packet is freed if marked as such | |
486 | * and not for a local destination. | |
487 | * A cleaner implementation would be to make bdg_forward() | |
488 | * always consume the packet, leaving to the caller the task | |
489 | * to make a copy if it needs it. As it is now, bdg_forward() | |
490 | * can keep a copy alive in some cases. | |
491 | */ | |
492 | int | |
493 | bdg_forward (struct mbuf **m0, struct ifnet *dst) | |
494 | { | |
495 | struct ifnet *src = (*m0)->m_pkthdr.rcvif; /* could be NULL in output */ | |
496 | struct ifnet *ifp ; | |
497 | struct ip *ip; | |
498 | int error=0, s ; | |
499 | int once = 0; /* execute the loop only once */ | |
500 | int canfree = 1 ; /* can free the buf at the end */ | |
501 | struct mbuf *m ; | |
502 | ||
503 | struct ether_header *eh = mtod(*m0, struct ether_header *); /* XXX */ | |
504 | ||
505 | if (dst == BDG_DROP) { /* this should not happen */ | |
506 | printf("xx bdg_forward for BDG_DROP)\n"); | |
507 | m_freem(*m0) ; | |
508 | *m0 = NULL ; | |
509 | return 0; | |
510 | } | |
511 | if (dst == BDG_LOCAL) { /* this should not happen as well */ | |
512 | printf("xx ouch, bdg_forward for local pkt\n"); | |
513 | return 0; | |
514 | } | |
515 | if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_UNKNOWN) { | |
516 | ifp = ifnet.tqh_first ; | |
517 | once = 0 ; | |
518 | if (dst != BDG_UNKNOWN) | |
519 | canfree = 0 ; | |
520 | } else { | |
521 | ifp = dst ; | |
522 | once = 1 ; /* and also canfree */ | |
523 | } | |
524 | #if IPFIREWALL | |
525 | /* | |
526 | * do filtering in a very similar way to what is done | |
527 | * in ip_output. Only for IP packets, and only pass/fail/dummynet | |
528 | * is supported. The tricky thing is to make sure that enough of | |
529 | * the packet (basically, Eth+IP+TCP/UDP headers) is contiguous | |
530 | * so that calls to m_pullup in ip_fw_chk will not kill the | |
531 | * ethernet header. | |
532 | */ | |
533 | if (ip_fw_chk_ptr) { | |
534 | u_int16_t dummy ; | |
535 | struct ip_fw_chain *rule; | |
536 | int off; | |
537 | ||
538 | m = *m0 ; | |
539 | if (m->m_type == MT_DUMMYNET) { | |
540 | /* | |
541 | * the packet was already tagged, so part of the | |
542 | * processing was already done, and we need to go down. | |
543 | */ | |
544 | rule = (struct ip_fw_chain *)(m->m_data) ; | |
545 | (*m0) = m = m->m_next ; | |
546 | ||
547 | src = m->m_pkthdr.rcvif; /* could be NULL in output */ | |
548 | eh = mtod(m, struct ether_header *); /* XXX */ | |
549 | canfree = 1 ; /* for sure, a copy is not needed later. */ | |
550 | goto forward; /* HACK! */ | |
551 | } else | |
552 | rule = NULL ; | |
553 | if (bdg_ipfw == 0) | |
554 | goto forward ; | |
555 | if (src == NULL) | |
556 | goto forward ; /* do not apply to packets from ether_output */ | |
557 | if (canfree == 0 ) /* need to make a copy */ | |
558 | m = m_copypacket(*m0, M_DONTWAIT); | |
559 | if (m == NULL) { | |
560 | /* fail... */ | |
561 | return 0 ; | |
562 | } | |
563 | ||
564 | dummy = 0 ; | |
565 | /* | |
566 | * before calling the firewall, swap fields the same as IP does. | |
567 | * here we assume the pkt is an IP one and the header is contiguous | |
568 | */ | |
569 | eh = mtod(m, struct ether_header *); | |
570 | ip = (struct ip *)(eh + 1 ) ; | |
571 | NTOHS(ip->ip_len); | |
572 | NTOHS(ip->ip_id); | |
573 | NTOHS(ip->ip_off); | |
574 | ||
575 | /* | |
576 | * The third parameter to the firewall code is the dst. interface. | |
577 | * Since we apply checks only on input pkts we use NULL. | |
578 | */ | |
579 | off = (*ip_fw_chk_ptr)(NULL, 0, NULL, &dummy, &m, &rule, NULL) ; | |
580 | if (m == NULL) { /* pkt discarded by firewall */ | |
581 | if (canfree) | |
582 | *m0 = NULL ; | |
583 | return 0 ; | |
584 | } | |
585 | /* | |
586 | * on return, the mbuf pointer might have changed. Restore | |
587 | * *m0 (if it was the same as m), eh, ip and then | |
588 | * restore original ordering. | |
589 | */ | |
590 | eh = mtod(m, struct ether_header *); | |
591 | ip = (struct ip *)(eh + 1 ) ; | |
592 | if (canfree) /* m was a reference to *m0, so update *m0 */ | |
593 | *m0 = m ; | |
594 | HTONS(ip->ip_len); | |
595 | HTONS(ip->ip_id); | |
596 | HTONS(ip->ip_off); | |
597 | if (off == 0) { | |
598 | if (canfree == 0) | |
599 | m_freem(m); | |
600 | goto forward ; | |
601 | } | |
602 | #if DUMMYNET | |
603 | if (off & 0x10000) { | |
604 | /* | |
605 | * pass the pkt to dummynet. Need to include m, dst, rule. | |
606 | * Dummynet consumes the packet in all cases. | |
607 | */ | |
608 | dummynet_io((off & 0xffff), DN_TO_BDG_FWD, m, dst, NULL, 0, rule); | |
609 | if (canfree) /* dummynet has consumed the original one */ | |
610 | *m0 = NULL ; | |
611 | return 0 ; | |
612 | } | |
613 | #endif | |
614 | /* if none of the above matches, we have to drop the pkt */ | |
615 | if (m) | |
616 | m_freem(m); | |
617 | if (canfree && m != *m0) { | |
618 | m_freem(*m0); | |
619 | *m0 = NULL ; | |
620 | } | |
621 | return 0 ; | |
622 | } | |
623 | forward: | |
624 | #endif /* COMPAT_IPFW */ | |
625 | if (canfree && once) | |
626 | m = *m0 ; | |
627 | else | |
628 | m = NULL ; | |
629 | ||
630 | for ( ; ifp ; ifp = ifp->if_link.tqe_next ) { | |
631 | if (ifp != src && ifp->if_type == IFT_ETHER && | |
632 | (ifp->if_flags & (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING) && | |
633 | SAMEGROUP(ifp, src) && !MUTED(ifp) ) { | |
634 | if (m == NULL) { /* do i need to make a copy ? */ | |
635 | if (canfree && ifp->if_link.tqe_next == NULL) /* last one! */ | |
636 | m = *m0 ; | |
637 | else /* on a P5-90, m_packetcopy takes 540 ticks */ | |
638 | m = m_copypacket(*m0, M_DONTWAIT); | |
639 | if (m == NULL) { | |
640 | printf("bdg_forward: sorry, m_copy failed!\n"); | |
641 | return ENOBUFS ; | |
642 | } | |
643 | } | |
644 | /* | |
645 | * execute last part of ether_output. | |
646 | */ | |
647 | s = splimp(); | |
648 | /* | |
649 | * Queue message on interface, and start output if interface | |
650 | * not yet active. | |
651 | */ | |
652 | if (IF_QFULL(&ifp->if_snd)) { | |
653 | IF_DROP(&ifp->if_snd); | |
654 | MUTE(ifp); /* good measure... */ | |
655 | splx(s); | |
656 | error = ENOBUFS ; | |
657 | } else { | |
658 | ifp->if_obytes += m->m_pkthdr.len ; | |
659 | if (m->m_flags & M_MCAST) | |
660 | ifp->if_omcasts++; | |
661 | IF_ENQUEUE(&ifp->if_snd, m); | |
662 | if ((ifp->if_flags & IFF_OACTIVE) == 0) | |
663 | (*ifp->if_start)(ifp); | |
664 | splx(s); | |
665 | if (m == *m0) | |
666 | *m0 = NULL ; /* the packet is gone... */ | |
667 | m = NULL ; | |
668 | } | |
669 | BDG_STAT(ifp, BDG_OUT); | |
670 | } | |
671 | if (once) | |
672 | break ; | |
673 | } | |
674 | ||
675 | /* cleanup any mbuf leftover. */ | |
676 | if (m) | |
677 | m_freem(m); | |
678 | if (m == *m0) | |
679 | *m0 = NULL ; | |
680 | if (canfree && *m0) { | |
681 | m_freem(*m0); | |
682 | *m0 = NULL ; | |
683 | } | |
684 | return error ; | |
685 | } |