]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/bridge.h
xnu-344.49.tar.gz
[apple/xnu.git] / bsd / net / bridge.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1998 Luigi Rizzo
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 *
49 */
50 #ifndef _NET_BRIDGE_H_
51 #define _NET_BRIDGE_H_
52 #include <sys/appleapiopts.h>
53
54 #warning This is not used by Darwin, do not include
55
56 extern int do_bridge;
57 /*
58 * the hash table for bridge
59 */
60 typedef struct hash_table {
61 struct ifnet *name ;
62 unsigned char etheraddr[6] ;
63 unsigned short used ;
64 } bdg_hash_table ;
65
66 extern bdg_hash_table *bdg_table ;
67
68 /*
69 * We need additional info for the bridge. The bdg_ifp2sc[] array
70 * provides a pointer to this struct using the if_index.
71 * bdg_softc has a backpointer to the struct ifnet, the bridge
72 * flags, and a cluster (bridging occurs only between port of the
73 * same cluster).
74 */
75 struct bdg_softc {
76 struct ifnet *ifp ;
77 /* also ((struct arpcom *)ifp)->ac_enaddr is the eth. addr */
78 int flags ;
79 #define IFF_BDG_PROMISC 0x0001 /* set promisc mode on this if. */
80 #define IFF_MUTE 0x0002 /* mute this if for bridging. */
81 #define IFF_USED 0x0004 /* use this if for bridging. */
82 short cluster_id ; /* in network format */
83 u_long magic;
84 } ;
85
86 extern struct bdg_softc *ifp2sc;
87
88 #define BDG_USED(ifp) (ifp2sc[ifp->if_index].flags & IFF_USED)
89 #define BDG_MUTED(ifp) (ifp2sc[ifp->if_index].flags & IFF_MUTE)
90 #define BDG_MUTE(ifp) ifp2sc[ifp->if_index].flags |= IFF_MUTE
91 #define BDG_UNMUTE(ifp) ifp2sc[ifp->if_index].flags &= ~IFF_MUTE
92 #define BDG_CLUSTER(ifp) (ifp2sc[ifp->if_index].cluster_id)
93 #define BDG_EH(ifp) ((struct arpcom *)ifp)->ac_enaddr
94
95 #define BDG_SAMECLUSTER(ifp,src) \
96 (src == NULL || BDG_CLUSTER(ifp) == BDG_CLUSTER(src) )
97
98
99 #define BDG_MAX_PORTS 128
100 typedef struct _bdg_addr {
101 unsigned char etheraddr[6] ;
102 short cluster_id ;
103 } bdg_addr ;
104 extern bdg_addr bdg_addresses[BDG_MAX_PORTS];
105 extern int bdg_ports ;
106
107 /*
108 * out of the 6 bytes, the last ones are more "variable". Since
109 * we are on a little endian machine, we have to do some gimmick...
110 */
111 #define HASH_SIZE 8192 /* must be a power of 2 */
112 #define HASH_FN(addr) ( \
113 ntohs( ((short *)addr)[1] ^ ((short *)addr)[2] ) & (HASH_SIZE -1))
114
115 #define IFF_MUTE IFF_LINK2 /* will need a separate flag... */
116
117 struct ifnet *bridge_in(struct ifnet *ifp, struct ether_header *eh);
118 /* bdg_forward frees the mbuf if necessary, returning null */
119 struct mbuf *bdg_forward(struct mbuf *m0, struct ether_header *eh, struct ifnet *dst);
120
121 #ifdef __i386__
122 #define BDG_MATCH(a,b) ( \
123 ((unsigned short *)(a))[2] == ((unsigned short *)(b))[2] && \
124 *((unsigned int *)(a)) == *((unsigned int *)(b)) )
125 #define IS_ETHER_BROADCAST(a) ( \
126 *((unsigned int *)(a)) == 0xffffffff && \
127 ((unsigned short *)(a))[2] == 0xffff )
128 #else
129 #warning... must complete these for the alpha etc.
130 #define BDG_MATCH(a,b) (!bcmp(a, b, ETHER_ADDR_LEN) )
131 #endif
132 /*
133 * The following constants are not legal ifnet pointers, and are used
134 * as return values from the classifier, bridge_dst_lookup()
135 * The same values are used as index in the statistics arrays,
136 * with BDG_FORWARD replacing specifically forwarded packets.
137 */
138 #define BDG_BCAST ( (struct ifnet *)1 )
139 #define BDG_MCAST ( (struct ifnet *)2 )
140 #define BDG_LOCAL ( (struct ifnet *)3 )
141 #define BDG_DROP ( (struct ifnet *)4 )
142 #define BDG_UNKNOWN ( (struct ifnet *)5 )
143 #define BDG_IN ( (struct ifnet *)7 )
144 #define BDG_OUT ( (struct ifnet *)8 )
145 #define BDG_FORWARD ( (struct ifnet *)9 )
146
147 #define PF_BDG 3 /* XXX superhack */
148 /*
149 * statistics, passed up with sysctl interface and ns -p bdg
150 */
151
152 #define STAT_MAX (int)BDG_FORWARD
153 struct bdg_port_stat {
154 char name[16];
155 u_long collisions;
156 u_long p_in[STAT_MAX+1];
157 } ;
158
159 struct bdg_stats {
160 struct bdg_port_stat s[16];
161 } ;
162
163
164 #define BDG_STAT(ifp, type) bdg_stats.s[ifp->if_index].p_in[(int)type]++
165
166 #ifdef KERNEL
167 /*
168 * Find the right pkt destination:
169 * BDG_BCAST is a broadcast
170 * BDG_MCAST is a multicast
171 * BDG_LOCAL is for a local address
172 * BDG_DROP must be dropped
173 * other ifp of the dest. interface (incl.self)
174 *
175 * We assume this is only called for interfaces for which bridging
176 * is enabled, i.e. BDG_USED(ifp) is true.
177 */
178 static __inline
179 struct ifnet *
180 bridge_dst_lookup(struct ether_header *eh)
181 {
182 struct ifnet *dst ;
183 int index ;
184 bdg_addr *p ;
185
186 if (IS_ETHER_BROADCAST(eh->ether_dhost))
187 return BDG_BCAST ;
188 if (eh->ether_dhost[0] & 1)
189 return BDG_MCAST ;
190 /*
191 * Lookup local addresses in case one matches.
192 */
193 for (index = bdg_ports, p = bdg_addresses ; index ; index--, p++ )
194 if (BDG_MATCH(p->etheraddr, eh->ether_dhost) )
195 return BDG_LOCAL ;
196 /*
197 * Look for a possible destination in table
198 */
199 index= HASH_FN( eh->ether_dhost );
200 dst = bdg_table[index].name;
201 if ( dst && BDG_MATCH( bdg_table[index].etheraddr, eh->ether_dhost) )
202 return dst ;
203 else
204 return BDG_UNKNOWN ;
205 }
206
207 #endif /* KERNEL */
208
209 #endif /* _NET_BRIDGE_H_ */