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