ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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->axdev, 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(struct net_device *dev, struct sk_buff *skb)
129 {
130         unsigned char *ptr;
131
132         skb_push(skb, 1);
133
134         if (skb_cow(skb, 1))
135                 return NET_RX_DROP;
136
137         ptr  = skb->data;
138         *ptr = 0x00;
139
140         skb->dev      = dev;
141         skb->protocol = htons(ETH_P_X25);
142         skb->mac.raw  = skb->data;
143         skb->pkt_type = PACKET_HOST;
144
145         skb->dev->last_rx = jiffies;
146         return netif_rx(skb);
147 }
148
149 /*
150  *      Send a LAPB frame via an ethernet interface
151  */
152 static int lapbeth_xmit(struct sk_buff *skb, struct net_device *dev)
153 {
154         int err = -ENODEV;
155
156         /*
157          * Just to be *really* sure not to send anything if the interface
158          * is down, the ethernet device may have gone.
159          */
160         if (!netif_running(dev)) {
161                 goto drop;
162         }
163
164         switch (skb->data[0]) {
165         case 0x00:
166                 err = 0;
167                 break;
168         case 0x01:
169                 if ((err = lapb_connect_request(dev)) != LAPB_OK)
170                         printk(KERN_ERR "lapbeth: lapb_connect_request "
171                                "error: %d\n", err);
172                 goto drop_ok;
173         case 0x02:
174                 if ((err = lapb_disconnect_request(dev)) != LAPB_OK)
175                         printk(KERN_ERR "lapbeth: lapb_disconnect_request "
176                                "err: %d\n", err);
177                 /* Fall thru */
178         default:
179                 goto drop_ok;
180         }
181
182         skb_pull(skb, 1);
183
184         if ((err = lapb_data_request(dev, skb)) != LAPB_OK) {
185                 printk(KERN_ERR "lapbeth: lapb_data_request error - %d\n", err);
186                 err = -ENOMEM;
187                 goto drop;
188         }
189         err = 0;
190 out:
191         return err;
192 drop_ok:
193         err = 0;
194 drop:
195         kfree_skb(skb);
196         goto out;
197 }
198
199 static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
200 {
201         struct lapbethdev *lapbeth = netdev_priv(ndev);
202         unsigned char *ptr;
203         struct net_device *dev;
204         int size = skb->len;
205
206         skb->protocol = htons(ETH_P_X25);
207
208         ptr = skb_push(skb, 2);
209
210         *ptr++ = size % 256;
211         *ptr++ = size / 256;
212
213         lapbeth->stats.tx_packets++;
214         lapbeth->stats.tx_bytes += size;
215
216         skb->dev = dev = lapbeth->ethdev;
217
218         dev->hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
219
220         dev_queue_xmit(skb);
221 }
222
223 static void lapbeth_connected(struct net_device *dev, int reason)
224 {
225         unsigned char *ptr;
226         struct sk_buff *skb = dev_alloc_skb(1);
227
228         if (!skb) {
229                 printk(KERN_ERR "lapbeth: out of memory\n");
230                 return;
231         }
232
233         ptr  = skb_put(skb, 1);
234         *ptr = 0x01;
235
236         skb->dev      = dev;
237         skb->protocol = htons(ETH_P_X25);
238         skb->mac.raw  = skb->data;
239         skb->pkt_type = PACKET_HOST;
240
241         skb->dev->last_rx = jiffies;
242         netif_rx(skb);
243 }
244
245 static void lapbeth_disconnected(struct net_device *dev, int reason)
246 {
247         unsigned char *ptr;
248         struct sk_buff *skb = dev_alloc_skb(1);
249
250         if (!skb) {
251                 printk(KERN_ERR "lapbeth: out of memory\n");
252                 return;
253         }
254
255         ptr  = skb_put(skb, 1);
256         *ptr = 0x02;
257
258         skb->dev      = dev;
259         skb->protocol = htons(ETH_P_X25);
260         skb->mac.raw  = skb->data;
261         skb->pkt_type = PACKET_HOST;
262
263         skb->dev->last_rx = jiffies;
264         netif_rx(skb);
265 }
266
267 /*
268  *      Statistics
269  */
270 static struct net_device_stats *lapbeth_get_stats(struct net_device *dev)
271 {
272         struct lapbethdev *lapbeth = netdev_priv(dev);
273         return &lapbeth->stats;
274 }
275
276 /*
277  *      Set AX.25 callsign
278  */
279 static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
280 {
281         struct sockaddr *sa = addr;
282         memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
283         return 0;
284 }
285
286
287 static struct lapb_register_struct lapbeth_callbacks = {
288         .connect_confirmation    = lapbeth_connected,
289         .connect_indication      = lapbeth_connected,
290         .disconnect_confirmation = lapbeth_disconnected,
291         .disconnect_indication   = lapbeth_disconnected,
292         .data_indication         = lapbeth_data_indication,
293         .data_transmit           = lapbeth_data_transmit,
294
295 };
296
297 /*
298  * open/close a device
299  */
300 static int lapbeth_open(struct net_device *dev)
301 {
302         int err;
303
304         if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
305                 printk(KERN_ERR "lapbeth: lapb_register error - %d\n", err);
306                 return -ENODEV;
307         }
308
309         netif_start_queue(dev);
310         return 0;
311 }
312
313 static int lapbeth_close(struct net_device *dev)
314 {
315         int err;
316
317         netif_stop_queue(dev);
318
319         if ((err = lapb_unregister(dev)) != LAPB_OK)
320                 printk(KERN_ERR "lapbeth: lapb_unregister error - %d\n", err);
321
322         return 0;
323 }
324
325 /* ------------------------------------------------------------------------ */
326
327 static void lapbeth_setup(struct net_device *dev)
328 {
329         dev->hard_start_xmit = lapbeth_xmit;
330         dev->open            = lapbeth_open;
331         dev->stop            = lapbeth_close;
332         dev->destructor      = free_netdev;
333         dev->set_mac_address = lapbeth_set_mac_address;
334         dev->get_stats       = lapbeth_get_stats;
335         dev->type            = ARPHRD_X25;
336         dev->hard_header_len = 3;
337         dev->mtu             = 1000;
338         dev->addr_len        = 0;
339         SET_MODULE_OWNER(dev);
340 }
341
342 /*
343  *      Setup a new device.
344  */
345 static int lapbeth_new_device(struct net_device *dev)
346 {
347         struct net_device *ndev;
348         struct lapbethdev *lapbeth;
349         int rc = -ENOMEM;
350
351         ASSERT_RTNL();
352
353         ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", 
354                            lapbeth_setup);
355         if (!ndev)
356                 goto out;
357
358         lapbeth = netdev_priv(ndev);
359         lapbeth->axdev = ndev;
360
361         dev_hold(dev);
362         lapbeth->ethdev = dev;
363
364         rc = dev_alloc_name(ndev, ndev->name);
365         if (rc < 0) 
366                 goto fail;
367
368         rc = -EIO;
369         if (register_netdevice(ndev))
370                 goto fail;
371
372         list_add_rcu(&lapbeth->node, &lapbeth_devices);
373         rc = 0;
374 out:
375         return rc;
376 fail:
377         dev_put(dev);
378         free_netdev(ndev);
379         kfree(lapbeth);
380         goto out;
381 }
382
383 /*
384  *      Free a lapb network device.
385  */
386 static void lapbeth_free_device(struct lapbethdev *lapbeth)
387 {
388         dev_put(lapbeth->ethdev);
389         list_del_rcu(&lapbeth->node);
390         unregister_netdevice(lapbeth->axdev);
391 }
392
393 /*
394  *      Handle device status changes.
395  *
396  * Called from notifier with RTNL held.
397  */
398 static int lapbeth_device_event(struct notifier_block *this,
399                                 unsigned long event, void *ptr)
400 {
401         struct lapbethdev *lapbeth;
402         struct net_device *dev = ptr;
403
404         if (!dev_is_ethdev(dev))
405                 return NOTIFY_DONE;
406
407         switch (event) {
408         case NETDEV_UP:
409                 /* New ethernet device -> new LAPB interface     */
410                 if (lapbeth_get_x25_dev(dev) == NULL)
411                         lapbeth_new_device(dev);
412                 break;
413         case NETDEV_DOWN:       
414                 /* ethernet device closed -> close LAPB interface */
415                 lapbeth = lapbeth_get_x25_dev(dev);
416                 if (lapbeth) 
417                         dev_close(lapbeth->axdev);
418                 break;
419         case NETDEV_UNREGISTER:
420                 /* ethernet device disappears -> remove LAPB interface */
421                 lapbeth = lapbeth_get_x25_dev(dev);
422                 if (lapbeth)
423                         lapbeth_free_device(lapbeth);
424                 break;
425         }
426
427         return NOTIFY_DONE;
428 }
429
430 /* ------------------------------------------------------------------------ */
431
432 static struct packet_type lapbeth_packet_type = {
433         .type = __constant_htons(ETH_P_DEC),
434         .func = lapbeth_rcv,
435 };
436
437 static struct notifier_block lapbeth_dev_notifier = {
438         .notifier_call = lapbeth_device_event,
439 };
440
441 static char banner[] __initdata = KERN_INFO "LAPB Ethernet driver version 0.02\n";
442
443 static int __init lapbeth_init_driver(void)
444 {
445         dev_add_pack(&lapbeth_packet_type);
446
447         register_netdevice_notifier(&lapbeth_dev_notifier);
448
449         printk(banner);
450
451         return 0;
452 }
453 module_init(lapbeth_init_driver);
454
455 static void __exit lapbeth_cleanup_driver(void)
456 {
457         struct lapbethdev *lapbeth;
458         struct list_head *entry, *tmp;
459
460         dev_remove_pack(&lapbeth_packet_type);
461         unregister_netdevice_notifier(&lapbeth_dev_notifier);
462
463         rtnl_lock();
464         list_for_each_safe(entry, tmp, &lapbeth_devices) {
465                 lapbeth = list_entry(entry, struct lapbethdev, node);
466
467                 unregister_netdevice(lapbeth->axdev);
468         }
469         rtnl_unlock();
470 }
471 module_exit(lapbeth_cleanup_driver);
472
473 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
474 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
475 MODULE_LICENSE("GPL");