GNU Linux-libre 4.4.283-gnu1
[releases.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/socket.h>
28 #include <linux/in.h>
29 #include <linux/slab.h>
30 #include <linux/kernel.h>
31 #include <linux/string.h>
32 #include <linux/net.h>
33 #include <linux/inet.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/skbuff.h>
37 #include <net/sock.h>
38 #include <asm/uaccess.h>
39 #include <linux/mm.h>
40 #include <linux/interrupt.h>
41 #include <linux/notifier.h>
42 #include <linux/stat.h>
43 #include <linux/module.h>
44 #include <linux/lapb.h>
45 #include <linux/init.h>
46
47 #include <net/x25device.h>
48
49 static const u8 bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
50
51 /* If this number is made larger, check that the temporary string buffer
52  * in lapbeth_new_device is large enough to store the probe device name.*/
53 #define MAXLAPBDEV 100
54
55 struct lapbethdev {
56         struct list_head        node;
57         struct net_device       *ethdev;        /* link to ethernet device */
58         struct net_device       *axdev;         /* lapbeth device (lapb#) */
59         bool                    up;
60         spinlock_t              up_lock;        /* Protects "up" */
61 };
62
63 static LIST_HEAD(lapbeth_devices);
64
65 /* ------------------------------------------------------------------------ */
66
67 /*
68  *      Get the LAPB device for the ethernet device
69  */
70 static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
71 {
72         struct lapbethdev *lapbeth;
73
74         list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node) {
75                 if (lapbeth->ethdev == dev) 
76                         return lapbeth;
77         }
78         return NULL;
79 }
80
81 static __inline__ int dev_is_ethdev(struct net_device *dev)
82 {
83         return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
84 }
85
86 /* ------------------------------------------------------------------------ */
87
88 /*
89  *      Receive a LAPB frame via an ethernet interface.
90  */
91 static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
92 {
93         int len, err;
94         struct lapbethdev *lapbeth;
95
96         if (dev_net(dev) != &init_net)
97                 goto drop;
98
99         if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
100                 return NET_RX_DROP;
101
102         if (!pskb_may_pull(skb, 2))
103                 goto drop;
104
105         rcu_read_lock();
106         lapbeth = lapbeth_get_x25_dev(dev);
107         if (!lapbeth)
108                 goto drop_unlock_rcu;
109         spin_lock_bh(&lapbeth->up_lock);
110         if (!lapbeth->up)
111                 goto drop_unlock;
112
113         len = skb->data[0] + skb->data[1] * 256;
114         dev->stats.rx_packets++;
115         dev->stats.rx_bytes += len;
116
117         skb_pull(skb, 2);       /* Remove the length bytes */
118         skb_trim(skb, len);     /* Set the length of the data */
119
120         if ((err = lapb_data_received(lapbeth->axdev, skb)) != LAPB_OK) {
121                 printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
122                 goto drop_unlock;
123         }
124 out:
125         spin_unlock_bh(&lapbeth->up_lock);
126         rcu_read_unlock();
127         return 0;
128 drop_unlock:
129         kfree_skb(skb);
130         goto out;
131 drop_unlock_rcu:
132         rcu_read_unlock();
133 drop:
134         kfree_skb(skb);
135         return 0;
136 }
137
138 static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
139 {
140         unsigned char *ptr;
141
142         skb_push(skb, 1);
143
144         if (skb_cow(skb, 1))
145                 return NET_RX_DROP;
146
147         ptr  = skb->data;
148         *ptr = X25_IFACE_DATA;
149
150         skb->protocol = x25_type_trans(skb, dev);
151         return netif_rx(skb);
152 }
153
154 /*
155  *      Send a LAPB frame via an ethernet interface
156  */
157 static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
158                                       struct net_device *dev)
159 {
160         struct lapbethdev *lapbeth = netdev_priv(dev);
161         int err;
162
163         spin_lock_bh(&lapbeth->up_lock);
164         if (!lapbeth->up)
165                 goto drop;
166
167         /* There should be a pseudo header of 1 byte added by upper layers.
168          * Check to make sure it is there before reading it.
169          */
170         if (skb->len < 1)
171                 goto drop;
172
173         switch (skb->data[0]) {
174         case X25_IFACE_DATA:
175                 break;
176         case X25_IFACE_CONNECT:
177                 if ((err = lapb_connect_request(dev)) != LAPB_OK)
178                         pr_err("lapb_connect_request error: %d\n", err);
179                 goto drop;
180         case X25_IFACE_DISCONNECT:
181                 if ((err = lapb_disconnect_request(dev)) != LAPB_OK)
182                         pr_err("lapb_disconnect_request err: %d\n", err);
183                 /* Fall thru */
184         default:
185                 goto drop;
186         }
187
188         skb_pull(skb, 1);
189
190         if ((err = lapb_data_request(dev, skb)) != LAPB_OK) {
191                 pr_err("lapb_data_request error - %d\n", err);
192                 goto drop;
193         }
194 out:
195         spin_unlock_bh(&lapbeth->up_lock);
196         return NETDEV_TX_OK;
197 drop:
198         kfree_skb(skb);
199         goto out;
200 }
201
202 static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
203 {
204         struct lapbethdev *lapbeth = netdev_priv(ndev);
205         unsigned char *ptr;
206         struct net_device *dev;
207         int size = skb->len;
208
209         ptr = skb_push(skb, 2);
210
211         *ptr++ = size % 256;
212         *ptr++ = size / 256;
213
214         ndev->stats.tx_packets++;
215         ndev->stats.tx_bytes += size;
216
217         skb->dev = dev = lapbeth->ethdev;
218
219         skb->protocol = htons(ETH_P_DEC);
220
221         skb_reset_network_header(skb);
222
223         dev_hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
224
225         dev_queue_xmit(skb);
226 }
227
228 static void lapbeth_connected(struct net_device *dev, int reason)
229 {
230         unsigned char *ptr;
231         struct sk_buff *skb = dev_alloc_skb(1);
232
233         if (!skb) {
234                 pr_err("out of memory\n");
235                 return;
236         }
237
238         ptr  = skb_put(skb, 1);
239         *ptr = X25_IFACE_CONNECT;
240
241         skb->protocol = x25_type_trans(skb, dev);
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                 pr_err("out of memory\n");
252                 return;
253         }
254
255         ptr  = skb_put(skb, 1);
256         *ptr = X25_IFACE_DISCONNECT;
257
258         skb->protocol = x25_type_trans(skb, dev);
259         netif_rx(skb);
260 }
261
262 /*
263  *      Set AX.25 callsign
264  */
265 static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
266 {
267         struct sockaddr *sa = addr;
268         memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
269         return 0;
270 }
271
272
273 static const struct lapb_register_struct lapbeth_callbacks = {
274         .connect_confirmation    = lapbeth_connected,
275         .connect_indication      = lapbeth_connected,
276         .disconnect_confirmation = lapbeth_disconnected,
277         .disconnect_indication   = lapbeth_disconnected,
278         .data_indication         = lapbeth_data_indication,
279         .data_transmit           = lapbeth_data_transmit,
280 };
281
282 /*
283  * open/close a device
284  */
285 static int lapbeth_open(struct net_device *dev)
286 {
287         struct lapbethdev *lapbeth = netdev_priv(dev);
288         int err;
289
290         if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
291                 pr_err("lapb_register error: %d\n", err);
292                 return -ENODEV;
293         }
294
295         spin_lock_bh(&lapbeth->up_lock);
296         lapbeth->up = true;
297         spin_unlock_bh(&lapbeth->up_lock);
298
299         return 0;
300 }
301
302 static int lapbeth_close(struct net_device *dev)
303 {
304         struct lapbethdev *lapbeth = netdev_priv(dev);
305         int err;
306
307         spin_lock_bh(&lapbeth->up_lock);
308         lapbeth->up = false;
309         spin_unlock_bh(&lapbeth->up_lock);
310
311         if ((err = lapb_unregister(dev)) != LAPB_OK)
312                 pr_err("lapb_unregister error: %d\n", err);
313
314         return 0;
315 }
316
317 /* ------------------------------------------------------------------------ */
318
319 static const struct net_device_ops lapbeth_netdev_ops = {
320         .ndo_open            = lapbeth_open,
321         .ndo_stop            = lapbeth_close,
322         .ndo_start_xmit      = lapbeth_xmit,
323         .ndo_set_mac_address = lapbeth_set_mac_address,
324 };
325
326 static void lapbeth_setup(struct net_device *dev)
327 {
328         dev->netdev_ops      = &lapbeth_netdev_ops;
329         dev->destructor      = free_netdev;
330         dev->type            = ARPHRD_X25;
331         dev->hard_header_len = 0;
332         dev->mtu             = 1000;
333         dev->addr_len        = 0;
334 }
335
336 /*
337  *      Setup a new device.
338  */
339 static int lapbeth_new_device(struct net_device *dev)
340 {
341         struct net_device *ndev;
342         struct lapbethdev *lapbeth;
343         int rc = -ENOMEM;
344
345         ASSERT_RTNL();
346
347         ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
348                             lapbeth_setup);
349         if (!ndev)
350                 goto out;
351
352         /* When transmitting data:
353          * first this driver removes a pseudo header of 1 byte,
354          * then the lapb module prepends an LAPB header of at most 3 bytes,
355          * then this driver prepends a length field of 2 bytes,
356          * then the underlying Ethernet device prepends its own header.
357          */
358         ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len
359                                            + dev->needed_headroom;
360         ndev->needed_tailroom = dev->needed_tailroom;
361
362         lapbeth = netdev_priv(ndev);
363         lapbeth->axdev = ndev;
364
365         dev_hold(dev);
366         lapbeth->ethdev = dev;
367
368         lapbeth->up = false;
369         spin_lock_init(&lapbeth->up_lock);
370
371         rc = -EIO;
372         if (register_netdevice(ndev))
373                 goto fail;
374
375         list_add_rcu(&lapbeth->node, &lapbeth_devices);
376         rc = 0;
377 out:
378         return rc;
379 fail:
380         dev_put(dev);
381         free_netdev(ndev);
382         kfree(lapbeth);
383         goto out;
384 }
385
386 /*
387  *      Free a lapb network device.
388  */
389 static void lapbeth_free_device(struct lapbethdev *lapbeth)
390 {
391         dev_put(lapbeth->ethdev);
392         list_del_rcu(&lapbeth->node);
393         unregister_netdevice(lapbeth->axdev);
394 }
395
396 /*
397  *      Handle device status changes.
398  *
399  * Called from notifier with RTNL held.
400  */
401 static int lapbeth_device_event(struct notifier_block *this,
402                                 unsigned long event, void *ptr)
403 {
404         struct lapbethdev *lapbeth;
405         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
406
407         if (dev_net(dev) != &init_net)
408                 return NOTIFY_DONE;
409
410         if (!dev_is_ethdev(dev))
411                 return NOTIFY_DONE;
412
413         switch (event) {
414         case NETDEV_UP:
415                 /* New ethernet device -> new LAPB interface     */
416                 if (lapbeth_get_x25_dev(dev) == NULL)
417                         lapbeth_new_device(dev);
418                 break;
419         case NETDEV_DOWN:       
420                 /* ethernet device closed -> close LAPB interface */
421                 lapbeth = lapbeth_get_x25_dev(dev);
422                 if (lapbeth) 
423                         dev_close(lapbeth->axdev);
424                 break;
425         case NETDEV_UNREGISTER:
426                 /* ethernet device disappears -> remove LAPB interface */
427                 lapbeth = lapbeth_get_x25_dev(dev);
428                 if (lapbeth)
429                         lapbeth_free_device(lapbeth);
430                 break;
431         }
432
433         return NOTIFY_DONE;
434 }
435
436 /* ------------------------------------------------------------------------ */
437
438 static struct packet_type lapbeth_packet_type __read_mostly = {
439         .type = cpu_to_be16(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 const char banner[] __initconst =
448         KERN_INFO "LAPB Ethernet driver version 0.02\n";
449
450 static int __init lapbeth_init_driver(void)
451 {
452         dev_add_pack(&lapbeth_packet_type);
453
454         register_netdevice_notifier(&lapbeth_dev_notifier);
455
456         printk(banner);
457
458         return 0;
459 }
460 module_init(lapbeth_init_driver);
461
462 static void __exit lapbeth_cleanup_driver(void)
463 {
464         struct lapbethdev *lapbeth;
465         struct list_head *entry, *tmp;
466
467         dev_remove_pack(&lapbeth_packet_type);
468         unregister_netdevice_notifier(&lapbeth_dev_notifier);
469
470         rtnl_lock();
471         list_for_each_safe(entry, tmp, &lapbeth_devices) {
472                 lapbeth = list_entry(entry, struct lapbethdev, node);
473
474                 dev_put(lapbeth->ethdev);
475                 unregister_netdevice(lapbeth->axdev);
476         }
477         rtnl_unlock();
478 }
479 module_exit(lapbeth_cleanup_driver);
480
481 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
482 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
483 MODULE_LICENSE("GPL");