[BRIDGE]: filter packets in learning state
[linux-2.6.git] / net / bridge / br_device.c
1 /*
2  *      Device handling code
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *
8  *      $Id: br_device.c,v 1.6 2001/12/24 00:59:55 davem Exp $
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19
20 #include <asm/uaccess.h>
21 #include "br_private.h"
22
23 static struct net_device_stats *br_dev_get_stats(struct net_device *dev)
24 {
25         struct net_bridge *br = netdev_priv(dev);
26         return &br->statistics;
27 }
28
29 int br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
30 {
31         struct net_bridge *br = netdev_priv(dev);
32         const unsigned char *dest = skb->data;
33         struct net_bridge_fdb_entry *dst;
34
35         br->statistics.tx_packets++;
36         br->statistics.tx_bytes += skb->len;
37
38         skb->mac.raw = skb->data;
39         skb_pull(skb, ETH_HLEN);
40
41         rcu_read_lock();
42         if (dest[0] & 1) 
43                 br_flood_deliver(br, skb, 0);
44         else if ((dst = __br_fdb_get(br, dest)) != NULL)
45                 br_deliver(dst->dst, skb);
46         else
47                 br_flood_deliver(br, skb, 0);
48
49         rcu_read_unlock();
50         return 0;
51 }
52
53 static int br_dev_open(struct net_device *dev)
54 {
55         struct net_bridge *br = netdev_priv(dev);
56
57         br_features_recompute(br);
58         netif_start_queue(dev);
59         br_stp_enable_bridge(br);
60
61         return 0;
62 }
63
64 static void br_dev_set_multicast_list(struct net_device *dev)
65 {
66 }
67
68 static int br_dev_stop(struct net_device *dev)
69 {
70         br_stp_disable_bridge(netdev_priv(dev));
71
72         netif_stop_queue(dev);
73
74         return 0;
75 }
76
77 static int br_change_mtu(struct net_device *dev, int new_mtu)
78 {
79         if (new_mtu < 68 || new_mtu > br_min_mtu(netdev_priv(dev)))
80                 return -EINVAL;
81
82         dev->mtu = new_mtu;
83         return 0;
84 }
85
86 /* Allow setting mac address of pseudo-bridge to be same as
87  * any of the bound interfaces
88  */
89 static int br_set_mac_address(struct net_device *dev, void *p)
90 {
91         struct net_bridge *br = netdev_priv(dev);
92         struct sockaddr *addr = p;
93         struct net_bridge_port *port;
94         int err = -EADDRNOTAVAIL;
95
96         spin_lock_bh(&br->lock);
97         list_for_each_entry(port, &br->port_list, list) {
98                 if (!compare_ether_addr(port->dev->dev_addr, addr->sa_data)) {
99                         br_stp_change_bridge_id(br, addr->sa_data);
100                         err = 0;
101                         break;
102                 }
103         }
104         spin_unlock_bh(&br->lock);
105
106         return err;
107 }
108
109 void br_dev_setup(struct net_device *dev)
110 {
111         memset(dev->dev_addr, 0, ETH_ALEN);
112
113         ether_setup(dev);
114
115         dev->do_ioctl = br_dev_ioctl;
116         dev->get_stats = br_dev_get_stats;
117         dev->hard_start_xmit = br_dev_xmit;
118         dev->open = br_dev_open;
119         dev->set_multicast_list = br_dev_set_multicast_list;
120         dev->change_mtu = br_change_mtu;
121         dev->destructor = free_netdev;
122         SET_MODULE_OWNER(dev);
123         dev->stop = br_dev_stop;
124         dev->tx_queue_len = 0;
125         dev->set_mac_address = br_set_mac_address;
126         dev->priv_flags = IFF_EBRIDGE;
127 }