home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / darwin / darwinx86.iso / usr / include / net / bridge.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-30  |  5.5 KB  |  169 lines

  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 REGENTS AND CONTRIBUTORS ``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. #ifndef _NET_BRIDGE_H_
  48. #define _NET_BRIDGE_H_
  49.  
  50. extern int do_bridge;
  51. /*
  52.  * the hash table for bridge
  53.  */
  54. typedef struct hash_table {
  55.     struct ifnet *name ;
  56.     unsigned char etheraddr[6] ;
  57.     unsigned short used ;
  58. } bdg_hash_table ;
  59.  
  60. extern bdg_hash_table *bdg_table ;
  61.  
  62. #define BDG_MAX_PORTS 128
  63. extern unsigned char bdg_addresses[6*BDG_MAX_PORTS];
  64. extern int bdg_ports ;
  65.  
  66. /*
  67.  * out of the 6 bytes, the last ones are more "variable". Since
  68.  * we are on a little endian machine, we have to do some gimmick...
  69.  */
  70. #define HASH_SIZE 8192    /* must be a power of 2 */
  71. #define HASH_FN(addr)   (    \
  72.     ntohs( ((short *)addr)[1] ^ ((short *)addr)[2] ) & (HASH_SIZE -1))
  73.  
  74. #define    IFF_MUTE    IFF_LINK2    /* will need a separate flag... */
  75.  
  76. struct ifnet *bridge_in(struct mbuf *m);
  77. /* bdg_forward frees the mbuf if necessary, returning null */
  78. int bdg_forward (struct mbuf **m, struct ifnet *dst);
  79.  
  80. #ifdef __i386__
  81. #define BDG_MATCH(a,b) ( \
  82.     ((unsigned short *)(a))[2] == ((unsigned short *)(b))[2] && \
  83.     *((unsigned int *)(a)) == *((unsigned int *)(b)) )
  84. #define IS_ETHER_BROADCAST(a) ( \
  85.     *((unsigned int *)(a)) == 0xffffffff && \
  86.     ((unsigned short *)(a))[2] == 0xffff )
  87. #else
  88. #warning... must complete these for the alpha etc.
  89. #define BDG_MATCH(a,b) (!bcmp(a, b, ETHER_ADDR_LEN) )
  90. #endif
  91. /*
  92.  * The following constants are not legal ifnet pointers, and are used
  93.  * as return values from the classifier, bridge_dst_lookup()
  94.  * The same values are used as index in the statistics arrays,
  95.  * with BDG_FORWARD replacing specifically forwarded packets.
  96.  */
  97. #define BDG_BCAST    ( (struct ifnet *)1 )
  98. #define BDG_MCAST    ( (struct ifnet *)2 )
  99. #define BDG_LOCAL    ( (struct ifnet *)3 )
  100. #define BDG_DROP    ( (struct ifnet *)4 )
  101. #define BDG_UNKNOWN    ( (struct ifnet *)5 )
  102. #define BDG_IN        ( (struct ifnet *)7 )
  103. #define BDG_OUT        ( (struct ifnet *)8 )
  104. #define BDG_FORWARD    ( (struct ifnet *)9 )
  105.  
  106. #define PF_BDG 3 /* XXX superhack */
  107. /*
  108.  * statistics, passed up with sysctl interface and ns -p bdg
  109.  */
  110.  
  111. #define STAT_MAX (int)BDG_FORWARD
  112. struct bdg_port_stat {
  113.     char name[16];
  114.     u_long collisions;
  115.     u_long p_in[STAT_MAX+1];
  116. } ;
  117.  
  118. struct bdg_stats {
  119.     struct bdg_port_stat s[16];
  120. } ;
  121.  
  122.  
  123. #define BDG_STAT(ifp, type) bdg_stats.s[ifp->if_index].p_in[(int)type]++ 
  124.  
  125. #if KERNEL
  126. /*
  127.  * Find the right pkt destination:
  128.  *    BDG_BCAST    is a broadcast
  129.  *    BDG_MCAST    is a multicast
  130.  *    BDG_LOCAL    is for a local address
  131.  *    BDG_DROP    must be dropped
  132.  *    other        ifp of the dest. interface (incl.self)
  133.  */
  134. static __inline
  135. struct ifnet *
  136. bridge_dst_lookup(struct mbuf *m)
  137. {
  138.     struct ether_header *eh = mtod(m, struct ether_header *);
  139.     struct ifnet *dst ;
  140.     int index ;
  141.     u_char *eth_addr = bdg_addresses ;
  142.  
  143.     if (IS_ETHER_BROADCAST(eh->ether_dhost))
  144.     return BDG_BCAST ;
  145.     if (eh->ether_dhost[0] & 1)
  146.     return BDG_MCAST ;
  147.     /*
  148.      * Lookup local addresses in case one matches.
  149.      */
  150.     for (index = bdg_ports, eth_addr = bdg_addresses ;
  151.          index ; index--, eth_addr += 6 )
  152.     if (BDG_MATCH(eth_addr, eh->ether_dhost) )
  153.         return BDG_LOCAL ;
  154.     /*
  155.      * Look for a possible destination in table
  156.      */
  157.     index= HASH_FN( eh->ether_dhost );
  158.     dst = bdg_table[index].name;
  159.     if ( dst && BDG_MATCH( bdg_table[index].etheraddr, eh->ether_dhost) )
  160.     return dst ;
  161.     else
  162.     return BDG_UNKNOWN ;
  163. }
  164.  
  165. #endif /* KERNEL */
  166.  
  167. #endif /* ! _NET_BRIDGE_H_ */
  168.  
  169.