blob: b634d7a5640eed4fe79857520ed55849c7ae73eb [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/eth_media.c: Ethernet bearer support for TIPC
3 *
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
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.
20 *
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.
32 */
33
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>
39
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
44
45
46/**
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
51 */
52
53struct eth_bearer {
54 struct tipc_bearer *bearer;
55 struct net_device *dev;
56 struct packet_type tipc_packet_type;
57};
58
59static struct eth_bearer eth_bearers[MAX_ETH_BEARERS];
60static int eth_started = 0;
61static struct notifier_block notifier;
62
63/**
64 * send_msg - send a TIPC message out over an Ethernet interface
65 */
66
67static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
68 struct tipc_media_addr *dest)
69{
70 struct sk_buff *clone;
71 struct net_device *dev;
72
73 clone = skb_clone(buf, GFP_ATOMIC);
74 if (clone) {
75 clone->nh.raw = clone->data;
76 dev = ((struct eth_bearer *)(tb_ptr->usr_handle))->dev;
77 clone->dev = 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);
82 }
83 return TIPC_OK;
84}
85
86/**
87 * recv_msg - handle incoming TIPC message from an Ethernet interface
88 *
89 * Routine truncates any Ethernet padding/CRC appended to the message,
90 * and ensures message size matches actual length
91 */
92
93static int recv_msg(struct sk_buff *buf, struct net_device *dev,
94 struct packet_type *pt, struct net_device *orig_dev)
95{
96 struct eth_bearer *eb_ptr = (struct eth_bearer *)pt->af_packet_priv;
97 u32 size;
98
99 if (likely(eb_ptr->bearer)) {
100 size = msg_size((struct tipc_msg *)buf->data);
101 skb_trim(buf, size);
102 if (likely(buf->len == size)) {
103 buf->next = NULL;
104 tipc_recv_msg(buf, eb_ptr->bearer);
105 } else {
106 kfree_skb(buf);
107 }
108 } else {
109 kfree_skb(buf);
110 }
111 return TIPC_OK;
112}
113
114/**
115 * enable_bearer - attach TIPC bearer to an Ethernet interface
116 */
117
118static int enable_bearer(struct tipc_bearer *tb_ptr)
119{
120 struct net_device *dev = dev_base;
121 struct eth_bearer *eb_ptr = &eth_bearers[0];
122 struct eth_bearer *stop = &eth_bearers[MAX_ETH_BEARERS];
123 char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1;
124
125 /* Find device with specified name */
126
127 while (dev && dev->name &&
128 (memcmp(dev->name, driver_name, strlen(dev->name)))) {
129 dev = dev->next;
130 }
131 if (!dev)
132 return -ENODEV;
133
134 /* Find Ethernet bearer for device (or create one) */
135
136 for (;(eb_ptr != stop) && eb_ptr->dev && (eb_ptr->dev != dev); eb_ptr++);
137 if (eb_ptr == stop)
138 return -EDQUOT;
139 if (!eb_ptr->dev) {
140 eb_ptr->dev = dev;
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));
146 dev_hold(dev);
147 dev_add_pack(&eb_ptr->tipc_packet_type);
148 }
149
150 /* Associate TIPC bearer with Ethernet bearer */
151
152 eb_ptr->bearer = tb_ptr;
153 tb_ptr->usr_handle = (void *)eb_ptr;
154 tb_ptr->mtu = dev->mtu;
155 tb_ptr->blocked = 0;
156 tb_ptr->addr.type = htonl(TIPC_MEDIA_TYPE_ETH);
157 memcpy(&tb_ptr->addr.dev_addr, &dev->dev_addr, ETH_ALEN);
158 return 0;
159}
160
161/**
162 * disable_bearer - detach TIPC bearer from an Ethernet interface
163 *
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.
167 */
168
169static void disable_bearer(struct tipc_bearer *tb_ptr)
170{
171 ((struct eth_bearer *)tb_ptr->usr_handle)->bearer = 0;
172}
173
174/**
175 * recv_notification - handle device updates from OS
176 *
177 * Change the state of the Ethernet bearer (if any) associated with the
178 * specified device.
179 */
180
181static int recv_notification(struct notifier_block *nb, unsigned long evt,
182 void *dv)
183{
184 struct net_device *dev = (struct net_device *)dv;
185 struct eth_bearer *eb_ptr = &eth_bearers[0];
186 struct eth_bearer *stop = &eth_bearers[MAX_ETH_BEARERS];
187
188 while ((eb_ptr->dev != dev)) {
189 if (++eb_ptr == stop)
190 return NOTIFY_DONE; /* couldn't find device */
191 }
192 if (!eb_ptr->bearer)
193 return NOTIFY_DONE; /* bearer had been disabled */
194
195 eb_ptr->bearer->mtu = dev->mtu;
196
197 switch (evt) {
198 case NETDEV_CHANGE:
199 if (netif_carrier_ok(dev))
200 tipc_continue(eb_ptr->bearer);
201 else
202 tipc_block_bearer(eb_ptr->bearer->name);
203 break;
204 case NETDEV_UP:
205 tipc_continue(eb_ptr->bearer);
206 break;
207 case NETDEV_DOWN:
208 tipc_block_bearer(eb_ptr->bearer->name);
209 break;
210 case NETDEV_CHANGEMTU:
211 case NETDEV_CHANGEADDR:
212 tipc_block_bearer(eb_ptr->bearer->name);
213 tipc_continue(eb_ptr->bearer);
214 break;
215 case NETDEV_UNREGISTER:
216 case NETDEV_CHANGENAME:
217 tipc_disable_bearer(eb_ptr->bearer->name);
218 break;
219 }
220 return NOTIFY_OK;
221}
222
223/**
224 * eth_addr2str - convert Ethernet address to string
225 */
226
227static char *eth_addr2str(struct tipc_media_addr *a, char *str_buf, int str_size)
228{
229 unchar *addr = (unchar *)&a->dev_addr;
230
231 if (str_size < 18)
232 *str_buf = '\0';
233 else
234 sprintf(str_buf, "%02x:%02x:%02x:%02x:%02x:%02x",
235 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
236 return str_buf;
237}
238
239/**
240 * eth_media_start - activate Ethernet bearer support
241 *
242 * Register Ethernet media type with TIPC bearer code. Also register
243 * with OS for notifications about device state changes.
244 */
245
246int eth_media_start(void)
247{
248 struct tipc_media_addr bcast_addr;
249 int res;
250
251 if (eth_started)
252 return -EINVAL;
253
254 memset(&bcast_addr, 0xff, sizeof(bcast_addr));
255 memset(eth_bearers, 0, sizeof(eth_bearers));
256
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);
261 if (res)
262 return res;
263
264 notifier.notifier_call = &recv_notification;
265 notifier.priority = 0;
266 res = register_netdevice_notifier(&notifier);
267 if (!res)
268 eth_started = 1;
269 return res;
270}
271
272/**
273 * eth_media_stop - deactivate Ethernet bearer support
274 */
275
276void eth_media_stop(void)
277{
278 int i;
279
280 if (!eth_started)
281 return;
282
283 unregister_netdevice_notifier(&notifier);
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;
288 }
289 if (eth_bearers[i].dev) {
290 dev_remove_pack(&eth_bearers[i].tipc_packet_type);
291 dev_put(eth_bearers[i].dev);
292 }
293 }
294 memset(&eth_bearers, 0, sizeof(eth_bearers));
295 eth_started = 0;
296}