2 * net/tipc/eth_media.c: Ethernet bearer support for TIPC
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <net/tipc/tipc.h>
35 #include <net/tipc/tipc_bearer.h>
36 #include <net/tipc/tipc_msg.h>
37 #include <linux/netdevice.h>
38 #include <linux/version.h>
40 #define MAX_ETH_BEARERS 2
41 #define TIPC_PROTOCOL 0x88ca
42 #define ETH_LINK_PRIORITY 10
43 #define ETH_LINK_TOLERANCE TIPC_DEF_LINK_TOL
47 * struct eth_bearer - Ethernet bearer data structure
48 * @bearer: ptr to associated "generic" bearer structure
49 * @dev: ptr to associated Ethernet network device
50 * @tipc_packet_type: used in binding TIPC to Ethernet driver
54 struct tipc_bearer *bearer;
55 struct net_device *dev;
56 struct packet_type tipc_packet_type;
59 static struct eth_bearer eth_bearers[MAX_ETH_BEARERS];
60 static int eth_started = 0;
61 static struct notifier_block notifier;
64 * send_msg - send a TIPC message out over an Ethernet interface
67 static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
68 struct tipc_media_addr *dest)
70 struct sk_buff *clone;
71 struct net_device *dev;
73 clone = skb_clone(buf, GFP_ATOMIC);
75 clone->nh.raw = clone->data;
76 dev = ((struct eth_bearer *)(tb_ptr->usr_handle))->dev;
78 dev->hard_header(clone, dev, TIPC_PROTOCOL,
79 &dest->dev_addr.eth_addr,
80 dev->dev_addr, clone->len);
81 dev_queue_xmit(clone);
87 * recv_msg - handle incoming TIPC message from an Ethernet interface
89 * Routine truncates any Ethernet padding/CRC appended to the message,
90 * and ensures message size matches actual length
93 static int recv_msg(struct sk_buff *buf, struct net_device *dev,
94 struct packet_type *pt, struct net_device *orig_dev)
96 struct eth_bearer *eb_ptr = (struct eth_bearer *)pt->af_packet_priv;
99 if (likely(eb_ptr->bearer)) {
100 size = msg_size((struct tipc_msg *)buf->data);
102 if (likely(buf->len == size)) {
104 tipc_recv_msg(buf, eb_ptr->bearer);
115 * enable_bearer - attach TIPC bearer to an Ethernet interface
118 static int enable_bearer(struct tipc_bearer *tb_ptr)
120 struct net_device *dev = dev_base;
121 struct eth_bearer *eb_ptr = ð_bearers[0];
122 struct eth_bearer *stop = ð_bearers[MAX_ETH_BEARERS];
123 char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1;
125 /* Find device with specified name */
127 while (dev && dev->name &&
128 (memcmp(dev->name, driver_name, strlen(dev->name)))) {
134 /* Find Ethernet bearer for device (or create one) */
136 for (;(eb_ptr != stop) && eb_ptr->dev && (eb_ptr->dev != dev); eb_ptr++);
141 eb_ptr->tipc_packet_type.type = __constant_htons(TIPC_PROTOCOL);
142 eb_ptr->tipc_packet_type.dev = dev;
143 eb_ptr->tipc_packet_type.func = recv_msg;
144 eb_ptr->tipc_packet_type.af_packet_priv = eb_ptr;
145 INIT_LIST_HEAD(&(eb_ptr->tipc_packet_type.list));
147 dev_add_pack(&eb_ptr->tipc_packet_type);
150 /* Associate TIPC bearer with Ethernet bearer */
152 eb_ptr->bearer = tb_ptr;
153 tb_ptr->usr_handle = (void *)eb_ptr;
154 tb_ptr->mtu = dev->mtu;
156 tb_ptr->addr.type = htonl(TIPC_MEDIA_TYPE_ETH);
157 memcpy(&tb_ptr->addr.dev_addr, &dev->dev_addr, ETH_ALEN);
162 * disable_bearer - detach TIPC bearer from an Ethernet interface
164 * We really should do dev_remove_pack() here, but this function can not be
165 * called at tasklet level. => Use eth_bearer->bearer as a flag to throw away
166 * incoming buffers, & postpone dev_remove_pack() to eth_media_stop() on exit.
169 static void disable_bearer(struct tipc_bearer *tb_ptr)
171 ((struct eth_bearer *)tb_ptr->usr_handle)->bearer = 0;
175 * recv_notification - handle device updates from OS
177 * Change the state of the Ethernet bearer (if any) associated with the
181 static int recv_notification(struct notifier_block *nb, unsigned long evt,
184 struct net_device *dev = (struct net_device *)dv;
185 struct eth_bearer *eb_ptr = ð_bearers[0];
186 struct eth_bearer *stop = ð_bearers[MAX_ETH_BEARERS];
188 while ((eb_ptr->dev != dev)) {
189 if (++eb_ptr == stop)
190 return NOTIFY_DONE; /* couldn't find device */
193 return NOTIFY_DONE; /* bearer had been disabled */
195 eb_ptr->bearer->mtu = dev->mtu;
199 if (netif_carrier_ok(dev))
200 tipc_continue(eb_ptr->bearer);
202 tipc_block_bearer(eb_ptr->bearer->name);
205 tipc_continue(eb_ptr->bearer);
208 tipc_block_bearer(eb_ptr->bearer->name);
210 case NETDEV_CHANGEMTU:
211 case NETDEV_CHANGEADDR:
212 tipc_block_bearer(eb_ptr->bearer->name);
213 tipc_continue(eb_ptr->bearer);
215 case NETDEV_UNREGISTER:
216 case NETDEV_CHANGENAME:
217 tipc_disable_bearer(eb_ptr->bearer->name);
224 * eth_addr2str - convert Ethernet address to string
227 static char *eth_addr2str(struct tipc_media_addr *a, char *str_buf, int str_size)
229 unchar *addr = (unchar *)&a->dev_addr;
234 sprintf(str_buf, "%02x:%02x:%02x:%02x:%02x:%02x",
235 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
240 * eth_media_start - activate Ethernet bearer support
242 * Register Ethernet media type with TIPC bearer code. Also register
243 * with OS for notifications about device state changes.
246 int eth_media_start(void)
248 struct tipc_media_addr bcast_addr;
254 memset(&bcast_addr, 0xff, sizeof(bcast_addr));
255 memset(eth_bearers, 0, sizeof(eth_bearers));
257 res = tipc_register_media(TIPC_MEDIA_TYPE_ETH, "eth",
258 enable_bearer, disable_bearer, send_msg,
259 eth_addr2str, &bcast_addr, ETH_LINK_PRIORITY,
260 ETH_LINK_TOLERANCE, TIPC_DEF_LINK_WIN);
264 notifier.notifier_call = &recv_notification;
265 notifier.priority = 0;
266 res = register_netdevice_notifier(¬ifier);
273 * eth_media_stop - deactivate Ethernet bearer support
276 void eth_media_stop(void)
283 unregister_netdevice_notifier(¬ifier);
284 for (i = 0; i < MAX_ETH_BEARERS ; i++) {
285 if (eth_bearers[i].bearer) {
286 eth_bearers[i].bearer->blocked = 1;
287 eth_bearers[i].bearer = 0;
289 if (eth_bearers[i].dev) {
290 dev_remove_pack(ð_bearers[i].tipc_packet_type);
291 dev_put(eth_bearers[i].dev);
294 memset(ð_bearers, 0, sizeof(eth_bearers));