13f7ae09e4cb6061cc836f7e8fa031f34efb3d70
[linux-flexiantxendom0-3.2.10.git] / drivers / net / wan / lapbether.c
1 /*
2  *      "LAPB via ethernet" driver release 001
3  *
4  *      This code REQUIRES 2.1.15 or higher/ NET3.038
5  *
6  *      This module:
7  *              This module is free software; you can redistribute it and/or
8  *              modify it under the terms of the GNU General Public License
9  *              as published by the Free Software Foundation; either version
10  *              2 of the License, or (at your option) any later version.
11  *
12  *      This is a "pseudo" network driver to allow LAPB over Ethernet.
13  *
14  *      This driver can use any ethernet destination address, and can be 
15  *      limited to accept frames from one dedicated ethernet card only.
16  *
17  *      History
18  *      LAPBETH 001     Jonathan Naylor         Cloned from bpqether.c
19  *      2000-10-29      Henner Eisen    lapb_data_indication() return status.
20  *      2000-11-14      Henner Eisen    dev_hold/put, NETDEV_GOING_DOWN support
21  */
22
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/socket.h>
26 #include <linux/in.h>
27 #include <linux/kernel.h>
28 #include <linux/string.h>
29 #include <linux/net.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/if_arp.h>
33 #include <linux/skbuff.h>
34 #include <net/sock.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include <linux/mm.h>
38 #include <linux/interrupt.h>
39 #include <linux/notifier.h>
40 #include <linux/stat.h>
41 #include <linux/netfilter.h>
42 #include <linux/module.h>
43 #include <linux/lapb.h>
44 #include <linux/init.h>
45
46 static char bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
47
48 /* If this number is made larger, check that the temporary string buffer
49  * in lapbeth_new_device is large enough to store the probe device name.*/
50 #define MAXLAPBDEV 100
51
52 struct lapbethdev {
53         struct list_head        node;
54         struct net_device       *ethdev;        /* link to ethernet device */
55         struct net_device       *axdev;         /* lapbeth device (lapb#) */
56         struct net_device_stats stats;          /* some statistics */
57 };
58
59 static struct list_head lapbeth_devices = LIST_HEAD_INIT(lapbeth_devices);
60
61 /* ------------------------------------------------------------------------ */
62
63 /*
64  *      Get the LAPB device for the ethernet device
65  */
66 static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
67 {
68         struct lapbethdev *lapbeth;
69
70         list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node) {
71                 if (lapbeth->ethdev == dev) 
72                         return lapbeth;
73         }
74         return NULL;
75 }
76
77 static __inline__ int dev_is_ethdev(struct net_device *dev)
78 {
79         return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
80 }
81
82 /* ------------------------------------------------------------------------ */
83
84 /*
85  *      Receive a LAPB frame via an ethernet interface.
86  */
87 static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype)
88 {
89         int len, err;
90         struct lapbethdev *lapbeth;
91
92         if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
93                 return NET_RX_DROP;
94
95         if (!pskb_may_pull(skb, 2))
96                 goto drop;
97
98         rcu_read_lock();
99         lapbeth = lapbeth_get_x25_dev(dev);
100         if (!lapbeth)
101                 goto drop_unlock;
102         if (!netif_running(lapbeth->axdev))
103                 goto drop_unlock;
104
105         lapbeth->stats.rx_packets++;
106
107         len = skb->data[0] + skb->data[1] * 256;
108         lapbeth->stats.rx_bytes += len;
109
110         skb_pull(skb, 2);       /* Remove the length bytes */
111         skb_trim(skb, len);     /* Set the length of the data */
112
113         if ((err = lapb_data_received(lapbeth, skb)) != LAPB_OK) {
114                 printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
115                 goto drop_unlock;
116         }
117 out:
118         rcu_read_unlock();
119         return 0;
120 drop_unlock:
121         kfree_skb(skb);
122         goto out;
123 drop:
124         kfree_skb(skb);
125         return 0;
126 }
127
128 static int lapbeth_data_indication(void *token, struct sk_buff *skb)
129 {
130         struct lapbethdev *lapbeth = (struct lapbethdev *)token;
131         unsigned char *ptr;
132
133         skb_push(skb, 1);
134
135         if (skb_cow(skb, 1))
136                 return NET_RX_DROP;
137
138         ptr  = skb->data;
139         *ptr = 0x00;
140
141         skb->dev      = lapbeth->axdev;
142         skb->protocol = htons(ETH_P_X25);
143         skb->mac.raw  = skb->data;
144         skb->pkt_type = PACKET_HOST;
145
146         skb->dev->last_rx = jiffies;
147         return netif_rx(skb);
148 }
149
150 /*
151  *      Send a LAPB frame via an ethernet interface
152  */
153 static int lapbeth_xmit(struct sk_buff *skb, struct net_device *dev)
154 {
155         struct lapbethdev *lapbeth = (struct lapbethdev *)dev->priv;
156         int err = -ENODEV;
157
158         /*
159          * Just to be *really* sure not to send anything if the interface
160          * is down, the ethernet device may have gone.
161          */
162         if (!netif_running(dev)) {
163                 goto drop;
164         }
165
166         switch (skb->data[0]) {
167         case 0x00:
168                 err = 0;
169                 break;
170         case 0x01:
171                 if ((err = lapb_connect_request(lapbeth)) != LAPB_OK)
172                         printk(KERN_ERR "lapbeth: lapb_connect_request "
173                                "error: %d\n", err);
174                 goto drop_ok;
175         case 0x02:
176                 if ((err = lapb_disconnect_request(lapbeth)) != LAPB_OK)
177                         printk(KERN_ERR "lapbeth: lapb_disconnect_request "
178                                "err: %d\n", err);
179                 /* Fall thru */
180         default:
181                 goto drop_ok;
182         }
183
184         skb_pull(skb, 1);
185
186         if ((err = lapb_data_request(lapbeth, skb)) != LAPB_OK) {
187                 printk(KERN_ERR "lapbeth: lapb_data_request error - %d\n", err);
188                 err = -ENOMEM;
189                 goto drop;
190         }
191         err = 0;
192 out:
193         return err;
194 drop_ok:
195         err = 0;
196 drop:
197         kfree_skb(skb);
198         goto out;
199 }
200
201 static void lapbeth_data_transmit(void *token, struct sk_buff *skb)
202 {
203         struct lapbethdev *lapbeth = (struct lapbethdev *)token;
204         unsigned char *ptr;
205         struct net_device *dev;
206         int size = skb->len;
207
208         skb->protocol = htons(ETH_P_X25);
209
210         ptr = skb_push(skb, 2);
211
212         *ptr++ = size % 256;
213         *ptr++ = size / 256;
214
215         lapbeth->stats.tx_packets++;
216         lapbeth->stats.tx_bytes += size;
217
218         skb->dev = dev = lapbeth->ethdev;
219
220         dev->hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
221
222         dev_queue_xmit(skb);
223 }
224
225 static void lapbeth_connected(void *token, int reason)
226 {
227         struct lapbethdev *lapbeth = (struct lapbethdev *)token;
228         unsigned char *ptr;
229         struct sk_buff *skb = dev_alloc_skb(1);
230
231         if (!skb) {
232                 printk(KERN_ERR "lapbeth: out of memory\n");
233                 return;
234         }
235
236         ptr  = skb_put(skb, 1);
237         *ptr = 0x01;
238
239         skb->dev      = lapbeth->axdev;
240         skb->protocol = htons(ETH_P_X25);
241         skb->mac.raw  = skb->data;
242         skb->pkt_type = PACKET_HOST;
243
244         skb->dev->last_rx = jiffies;
245         netif_rx(skb);
246 }
247
248 static void lapbeth_disconnected(void *token, int reason)
249 {
250         struct lapbethdev *lapbeth = (struct lapbethdev *)token;
251         unsigned char *ptr;
252         struct sk_buff *skb = dev_alloc_skb(1);
253
254         if (!skb) {
255                 printk(KERN_ERR "lapbeth: out of memory\n");
256                 return;
257         }
258
259         ptr  = skb_put(skb, 1);
260         *ptr = 0x02;
261
262         skb->dev      = lapbeth->axdev;
263         skb->protocol = htons(ETH_P_X25);
264         skb->mac.raw  = skb->data;
265         skb->pkt_type = PACKET_HOST;
266
267         skb->dev->last_rx = jiffies;
268         netif_rx(skb);
269 }
270
271 /*
272  *      Statistics
273  */
274 static struct net_device_stats *lapbeth_get_stats(struct net_device *dev)
275 {
276         struct lapbethdev *lapbeth = (struct lapbethdev *)dev->priv;
277         return &lapbeth->stats;
278 }
279
280 /*
281  *      Set AX.25 callsign
282  */
283 static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
284 {
285         struct sockaddr *sa = (struct sockaddr *)addr;
286         memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
287         return 0;
288 }
289
290
291 static struct lapb_register_struct lapbeth_callbacks = {
292         .connect_confirmation    = lapbeth_connected,
293         .connect_indication      = lapbeth_connected,
294         .disconnect_confirmation = lapbeth_disconnected,
295         .disconnect_indication   = lapbeth_disconnected,
296         .data_indication         = lapbeth_data_indication,
297         .data_transmit           = lapbeth_data_transmit,
298
299 };
300
301 /*
302  * open/close a device
303  */
304 static int lapbeth_open(struct net_device *dev)
305 {
306         struct lapbethdev *lapbeth;
307         int err;
308
309         lapbeth = (struct lapbethdev *)dev->priv;
310         if ((err = lapb_register(lapbeth, &lapbeth_callbacks)) != LAPB_OK) {
311                 printk(KERN_ERR "lapbeth: lapb_register error - %d\n", err);
312                 return -ENODEV;
313         }
314
315         netif_start_queue(dev);
316         return 0;
317 }
318
319 static int lapbeth_close(struct net_device *dev)
320 {
321         struct lapbethdev *lapbeth = (struct lapbethdev *)dev->priv;
322         int err;
323
324         netif_stop_queue(dev);
325
326         if ((err = lapb_unregister(lapbeth)) != LAPB_OK)
327                 printk(KERN_ERR "lapbeth: lapb_unregister error - %d\n", err);
328
329         return 0;
330 }
331
332 /* ------------------------------------------------------------------------ */
333
334 static void lapbeth_setup(struct net_device *dev)
335 {
336         dev->hard_start_xmit = lapbeth_xmit;
337         dev->open            = lapbeth_open;
338         dev->stop            = lapbeth_close;
339         dev->destructor      = free_netdev;
340         dev->set_mac_address = lapbeth_set_mac_address;
341         dev->get_stats       = lapbeth_get_stats;
342         dev->type            = ARPHRD_X25;
343         dev->hard_header_len = 3;
344         dev->mtu             = 1000;
345         dev->addr_len        = 0;
346         SET_MODULE_OWNER(dev);
347 }
348
349 /*
350  *      Setup a new device.
351  */
352 static int lapbeth_new_device(struct net_device *dev)
353 {
354         struct net_device *ndev;
355         struct lapbethdev *lapbeth;
356         int rc = -ENOMEM;
357
358         ASSERT_RTNL();
359
360         ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", 
361                            lapbeth_setup);
362         if (!ndev)
363                 goto out;
364
365         lapbeth = ndev->priv;
366         lapbeth->axdev = ndev;
367
368         dev_hold(dev);
369         lapbeth->ethdev = dev;
370
371         rc = dev_alloc_name(ndev, ndev->name);
372         if (rc < 0) 
373                 goto fail;
374
375         rc = -EIO;
376         if (register_netdevice(ndev))
377                 goto fail;
378
379         list_add_rcu(&lapbeth->node, &lapbeth_devices);
380         rc = 0;
381 out:
382         return rc;
383 fail:
384         dev_put(dev);
385         kfree(lapbeth);
386         goto out;
387 }
388
389 /*
390  *      Free a lapb network device.
391  */
392 static void lapbeth_free_device(struct lapbethdev *lapbeth)
393 {
394         dev_put(lapbeth->ethdev);
395         list_del_rcu(&lapbeth->node);
396         unregister_netdevice(lapbeth->axdev);
397 }
398
399 /*
400  *      Handle device status changes.
401  */
402 static int lapbeth_device_event(struct notifier_block *this,
403                                 unsigned long event, void *ptr)
404 {
405         struct lapbethdev *lapbeth;
406         struct net_device *dev = (struct net_device *)ptr;
407
408         if (!dev_is_ethdev(dev))
409                 return NOTIFY_DONE;
410
411         rcu_read_lock();
412         switch (event) {
413         case NETDEV_UP:
414                 /* New ethernet device -> new LAPB interface     */
415                 if (lapbeth_get_x25_dev(dev) == NULL)
416                         lapbeth_new_device(dev);
417                 break;
418         case NETDEV_DOWN:       
419                 /* ethernet device closed -> close LAPB interface */
420                 lapbeth = lapbeth_get_x25_dev(dev);
421                 if (lapbeth) 
422                         dev_close(lapbeth->axdev);
423                 break;
424         case NETDEV_UNREGISTER:
425                 /* ethernet device disappears -> remove LAPB interface */
426                 lapbeth = lapbeth_get_x25_dev(dev);
427                 if (lapbeth)
428                         lapbeth_free_device(lapbeth);
429                 break;
430         }
431         rcu_read_unlock();
432
433         return NOTIFY_DONE;
434 }
435
436 /* ------------------------------------------------------------------------ */
437
438 static struct packet_type lapbeth_packet_type = {
439         .type = __constant_htons(ETH_P_DEC),
440         .func = lapbeth_rcv,
441 };
442
443 static struct notifier_block lapbeth_dev_notifier = {
444         .notifier_call = lapbeth_device_event,
445 };
446
447 static char banner[] __initdata = KERN_INFO "LAPB Ethernet driver version 0.02\n";
448
449 static int __init lapbeth_init_driver(void)
450 {
451         dev_add_pack(&lapbeth_packet_type);
452
453         register_netdevice_notifier(&lapbeth_dev_notifier);
454
455         printk(banner);
456
457         return 0;
458 }
459 module_init(lapbeth_init_driver);
460
461 static void __exit lapbeth_cleanup_driver(void)
462 {
463         struct lapbethdev *lapbeth;
464         struct list_head *entry, *tmp;
465
466         dev_remove_pack(&lapbeth_packet_type);
467         unregister_netdevice_notifier(&lapbeth_dev_notifier);
468
469         rtnl_lock();
470         list_for_each_safe(entry, tmp, &lapbeth_devices) {
471                 lapbeth = list_entry(entry, struct lapbethdev, node);
472
473                 unregister_netdevice(lapbeth->axdev);
474         }
475         rtnl_unlock();
476 }
477 module_exit(lapbeth_cleanup_driver);
478
479 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
480 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
481 MODULE_LICENSE("GPL");