GNU Linux-libre 4.19.304-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 <linux/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->needs_free_netdev = true;
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         if (dev->type != ARPHRD_ETHER)
348                 return -EINVAL;
349
350         ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
351                             lapbeth_setup);
352         if (!ndev)
353                 goto out;
354
355         /* When transmitting data:
356          * first this driver removes a pseudo header of 1 byte,
357          * then the lapb module prepends an LAPB header of at most 3 bytes,
358          * then this driver prepends a length field of 2 bytes,
359          * then the underlying Ethernet device prepends its own header.
360          */
361         ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len
362                                            + dev->needed_headroom;
363         ndev->needed_tailroom = dev->needed_tailroom;
364
365         lapbeth = netdev_priv(ndev);
366         lapbeth->axdev = ndev;
367
368         dev_hold(dev);
369         lapbeth->ethdev = dev;
370
371         lapbeth->up = false;
372         spin_lock_init(&lapbeth->up_lock);
373
374         rc = -EIO;
375         if (register_netdevice(ndev))
376                 goto fail;
377
378         list_add_rcu(&lapbeth->node, &lapbeth_devices);
379         rc = 0;
380 out:
381         return rc;
382 fail:
383         dev_put(dev);
384         free_netdev(ndev);
385         goto out;
386 }
387
388 /*
389  *      Free a lapb network device.
390  */
391 static void lapbeth_free_device(struct lapbethdev *lapbeth)
392 {
393         dev_put(lapbeth->ethdev);
394         list_del_rcu(&lapbeth->node);
395         unregister_netdevice(lapbeth->axdev);
396 }
397
398 /*
399  *      Handle device status changes.
400  *
401  * Called from notifier with RTNL held.
402  */
403 static int lapbeth_device_event(struct notifier_block *this,
404                                 unsigned long event, void *ptr)
405 {
406         struct lapbethdev *lapbeth;
407         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
408
409         if (dev_net(dev) != &init_net)
410                 return NOTIFY_DONE;
411
412         if (!dev_is_ethdev(dev) && !lapbeth_get_x25_dev(dev))
413                 return NOTIFY_DONE;
414
415         switch (event) {
416         case NETDEV_UP:
417                 /* New ethernet device -> new LAPB interface     */
418                 if (lapbeth_get_x25_dev(dev) == NULL)
419                         lapbeth_new_device(dev);
420                 break;
421         case NETDEV_DOWN:       
422                 /* ethernet device closed -> close LAPB interface */
423                 lapbeth = lapbeth_get_x25_dev(dev);
424                 if (lapbeth) 
425                         dev_close(lapbeth->axdev);
426                 break;
427         case NETDEV_UNREGISTER:
428                 /* ethernet device disappears -> remove LAPB interface */
429                 lapbeth = lapbeth_get_x25_dev(dev);
430                 if (lapbeth)
431                         lapbeth_free_device(lapbeth);
432                 break;
433         }
434
435         return NOTIFY_DONE;
436 }
437
438 /* ------------------------------------------------------------------------ */
439
440 static struct packet_type lapbeth_packet_type __read_mostly = {
441         .type = cpu_to_be16(ETH_P_DEC),
442         .func = lapbeth_rcv,
443 };
444
445 static struct notifier_block lapbeth_dev_notifier = {
446         .notifier_call = lapbeth_device_event,
447 };
448
449 static const char banner[] __initconst =
450         KERN_INFO "LAPB Ethernet driver version 0.02\n";
451
452 static int __init lapbeth_init_driver(void)
453 {
454         dev_add_pack(&lapbeth_packet_type);
455
456         register_netdevice_notifier(&lapbeth_dev_notifier);
457
458         printk(banner);
459
460         return 0;
461 }
462 module_init(lapbeth_init_driver);
463
464 static void __exit lapbeth_cleanup_driver(void)
465 {
466         struct lapbethdev *lapbeth;
467         struct list_head *entry, *tmp;
468
469         dev_remove_pack(&lapbeth_packet_type);
470         unregister_netdevice_notifier(&lapbeth_dev_notifier);
471
472         rtnl_lock();
473         list_for_each_safe(entry, tmp, &lapbeth_devices) {
474                 lapbeth = list_entry(entry, struct lapbethdev, node);
475
476                 dev_put(lapbeth->ethdev);
477                 unregister_netdevice(lapbeth->axdev);
478         }
479         rtnl_unlock();
480 }
481 module_exit(lapbeth_cleanup_driver);
482
483 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
484 MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
485 MODULE_LICENSE("GPL");