[NET]: Initialize the network namespace of network devices.
[deliverable/linux.git] / drivers / net / hamradio / bpqether.c
CommitLineData
1da177e4
LT
1/*
2 * G8BPQ compatible "AX.25 via ethernet" driver release 004
3 *
4 * This code REQUIRES 2.0.0 or higher/ NET3.029
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 AX.25 over Ethernet
13 * using G8BPQ encapsulation. It has been extracted from the protocol
14 * implementation because
15 *
16 * - things got unreadable within the protocol stack
17 * - to cure the protocol stack from "feature-ism"
18 * - a protocol implementation shouldn't need to know on
19 * which hardware it is running
20 * - user-level programs like the AX.25 utilities shouldn't
21 * need to know about the hardware.
22 * - IP over ethernet encapsulated AX.25 was impossible
23 * - rxecho.c did not work
24 * - to have room for extensions
25 * - it just deserves to "live" as an own driver
26 *
27 * This driver can use any ethernet destination address, and can be
28 * limited to accept frames from one dedicated ethernet card only.
29 *
30 * Note that the driver sets up the BPQ devices automagically on
31 * startup or (if started before the "insmod" of an ethernet device)
32 * on "ifconfig up". It hopefully will remove the BPQ on "rmmod"ing
33 * the ethernet device (in fact: as soon as another ethernet or bpq
34 * device gets "ifconfig"ured).
35 *
36 * I have heard that several people are thinking of experiments
37 * with highspeed packet radio using existing ethernet cards.
38 * Well, this driver is prepared for this purpose, just add
39 * your tx key control and a txdelay / tailtime algorithm,
40 * probably some buffering, and /voila/...
41 *
42 * History
43 * BPQ 001 Joerg(DL1BKE) Extracted BPQ code from AX.25
44 * protocol stack and added my own
45 * yet existing patches
46 * BPQ 002 Joerg(DL1BKE) Scan network device list on
47 * startup.
48 * BPQ 003 Joerg(DL1BKE) Ethernet destination address
49 * and accepted source address
50 * can be configured by an ioctl()
51 * call.
52 * Fixed to match Linux networking
53 * changes - 2.1.15.
54 * BPQ 004 Joerg(DL1BKE) Fixed to not lock up on ifconfig.
55 */
56
1da177e4
LT
57#include <linux/errno.h>
58#include <linux/types.h>
59#include <linux/socket.h>
60#include <linux/in.h>
61#include <linux/kernel.h>
62#include <linux/string.h>
63#include <linux/net.h>
64#include <net/ax25.h>
65#include <linux/inet.h>
66#include <linux/netdevice.h>
67#include <linux/if_ether.h>
68#include <linux/if_arp.h>
69#include <linux/skbuff.h>
70#include <net/sock.h>
71#include <asm/system.h>
72#include <asm/uaccess.h>
73#include <linux/mm.h>
74#include <linux/interrupt.h>
75#include <linux/notifier.h>
76#include <linux/proc_fs.h>
77#include <linux/seq_file.h>
78#include <linux/stat.h>
79#include <linux/netfilter.h>
80#include <linux/module.h>
81#include <linux/init.h>
82#include <linux/rtnetlink.h>
83
84#include <net/ip.h>
85#include <net/arp.h>
457c4cbc 86#include <net/net_namespace.h>
1da177e4
LT
87
88#include <linux/bpqether.h>
89
90static char banner[] __initdata = KERN_INFO "AX.25: bpqether driver version 004\n";
91
1da177e4
LT
92static char bcast_addr[6]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
93
94static char bpq_eth_addr[6];
95
f2ccd8fa 96static int bpq_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
1da177e4
LT
97static int bpq_device_event(struct notifier_block *, unsigned long, void *);
98static const char *bpq_print_ethaddr(const unsigned char *);
99
100static struct packet_type bpq_packet_type = {
101 .type = __constant_htons(ETH_P_BPQ),
102 .func = bpq_rcv,
103};
104
105static struct notifier_block bpq_dev_notifier = {
106 .notifier_call =bpq_device_event,
107};
108
109
110struct bpqdev {
111 struct list_head bpq_list; /* list of bpq devices chain */
112 struct net_device *ethdev; /* link to ethernet device */
113 struct net_device *axdev; /* bpq device (bpq#) */
114 struct net_device_stats stats; /* some statistics */
115 char dest_addr[6]; /* ether destination address */
116 char acpt_addr[6]; /* accept ether frames from this address only */
117};
118
119static LIST_HEAD(bpq_devices);
120
1eeb7e42
RB
121/*
122 * bpqether network devices are paired with ethernet devices below them, so
123 * form a special "super class" of normal ethernet devices; split their locks
124 * off into a separate class since they always nest.
125 */
126static struct lock_class_key bpq_netdev_xmit_lock_key;
1da177e4
LT
127
128/* ------------------------------------------------------------------------ */
129
130
131/*
132 * Get the ethernet device for a BPQ device
133 */
134static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
135{
136 struct bpqdev *bpq = netdev_priv(dev);
137
138 return bpq ? bpq->ethdev : NULL;
139}
140
141/*
142 * Get the BPQ device for the ethernet device
143 */
144static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
145{
146 struct bpqdev *bpq;
147
bc0a7438 148 list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list) {
1da177e4
LT
149 if (bpq->ethdev == dev)
150 return bpq->axdev;
151 }
152 return NULL;
153}
154
155static inline int dev_is_ethdev(struct net_device *dev)
156{
157 return (
158 dev->type == ARPHRD_ETHER
159 && strncmp(dev->name, "dummy", 5)
160 );
161}
162
163/* ------------------------------------------------------------------------ */
164
165
166/*
167 * Receive an AX.25 frame via an ethernet interface.
168 */
f2ccd8fa 169static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
1da177e4
LT
170{
171 int len;
172 char * ptr;
173 struct ethhdr *eth;
174 struct bpqdev *bpq;
175
176 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
177 return NET_RX_DROP;
178
179 if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
180 goto drop;
181
182 rcu_read_lock();
183 dev = bpq_get_ax25_dev(dev);
184
185 if (dev == NULL || !netif_running(dev))
186 goto drop_unlock;
187
188 /*
189 * if we want to accept frames from just one ethernet device
190 * we check the source address of the sender.
191 */
192
193 bpq = netdev_priv(dev);
194
195 eth = eth_hdr(skb);
196
197 if (!(bpq->acpt_addr[0] & 0x01) &&
198 memcmp(eth->h_source, bpq->acpt_addr, ETH_ALEN))
199 goto drop_unlock;
200
201 if (skb_cow(skb, sizeof(struct ethhdr)))
202 goto drop_unlock;
203
204 len = skb->data[0] + skb->data[1] * 256 - 5;
205
206 skb_pull(skb, 2); /* Remove the length bytes */
207 skb_trim(skb, len); /* Set the length of the data */
208
209 bpq->stats.rx_packets++;
210 bpq->stats.rx_bytes += len;
211
212 ptr = skb_push(skb, 1);
213 *ptr = 0;
214
56cb5156 215 skb->protocol = ax25_type_trans(skb, dev);
1da177e4
LT
216 netif_rx(skb);
217 dev->last_rx = jiffies;
218unlock:
219
220 rcu_read_unlock();
221
222 return 0;
223drop_unlock:
224 kfree_skb(skb);
225 goto unlock;
226
227drop:
228 kfree_skb(skb);
229 return 0;
230}
231
232/*
233 * Send an AX.25 frame via an ethernet interface
234 */
235static int bpq_xmit(struct sk_buff *skb, struct net_device *dev)
236{
237 struct sk_buff *newskb;
238 unsigned char *ptr;
239 struct bpqdev *bpq;
240 int size;
241
242 /*
243 * Just to be *really* sure not to send anything if the interface
244 * is down, the ethernet device may have gone.
245 */
246 if (!netif_running(dev)) {
247 kfree_skb(skb);
248 return -ENODEV;
249 }
250
251 skb_pull(skb, 1);
252 size = skb->len;
253
254 /*
255 * The AX.25 code leaves enough room for the ethernet header, but
256 * sendto() does not.
257 */
258 if (skb_headroom(skb) < AX25_BPQ_HEADER_LEN) { /* Ough! */
259 if ((newskb = skb_realloc_headroom(skb, AX25_BPQ_HEADER_LEN)) == NULL) {
260 printk(KERN_WARNING "bpqether: out of memory\n");
261 kfree_skb(skb);
262 return -ENOMEM;
263 }
264
265 if (skb->sk != NULL)
266 skb_set_owner_w(newskb, skb->sk);
267
268 kfree_skb(skb);
269 skb = newskb;
270 }
271
1da177e4
LT
272 ptr = skb_push(skb, 2);
273
274 *ptr++ = (size + 5) % 256;
275 *ptr++ = (size + 5) / 256;
276
277 bpq = netdev_priv(dev);
278
279 if ((dev = bpq_get_ether_dev(dev)) == NULL) {
280 bpq->stats.tx_dropped++;
281 kfree_skb(skb);
282 return -ENODEV;
283 }
284
56cb5156 285 skb->protocol = ax25_type_trans(skb, dev);
c1d2bbe1 286 skb_reset_network_header(skb);
1da177e4
LT
287 dev->hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
288 bpq->stats.tx_packets++;
289 bpq->stats.tx_bytes+=skb->len;
290
291 dev_queue_xmit(skb);
292 netif_wake_queue(dev);
293 return 0;
294}
295
296/*
297 * Statistics
298 */
299static struct net_device_stats *bpq_get_stats(struct net_device *dev)
300{
301 struct bpqdev *bpq = netdev_priv(dev);
302
303 return &bpq->stats;
304}
305
306/*
307 * Set AX.25 callsign
308 */
309static int bpq_set_mac_address(struct net_device *dev, void *addr)
310{
311 struct sockaddr *sa = (struct sockaddr *)addr;
312
313 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
314
315 return 0;
316}
317
318/* Ioctl commands
319 *
320 * SIOCSBPQETHOPT reserved for enhancements
321 * SIOCSBPQETHADDR set the destination and accepted
322 * source ethernet address (broadcast
323 * or multicast: accept all)
324 */
325static int bpq_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
326{
327 struct bpq_ethaddr __user *ethaddr = ifr->ifr_data;
328 struct bpqdev *bpq = netdev_priv(dev);
329 struct bpq_req req;
330
331 if (!capable(CAP_NET_ADMIN))
332 return -EPERM;
333
334 switch (cmd) {
335 case SIOCSBPQETHOPT:
336 if (copy_from_user(&req, ifr->ifr_data, sizeof(struct bpq_req)))
337 return -EFAULT;
338 switch (req.cmd) {
339 case SIOCGBPQETHPARAM:
340 case SIOCSBPQETHPARAM:
341 default:
342 return -EINVAL;
343 }
344
345 break;
346
347 case SIOCSBPQETHADDR:
348 if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
349 return -EFAULT;
350 if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
351 return -EFAULT;
352 break;
353
354 default:
355 return -EINVAL;
356 }
357
358 return 0;
359}
360
361/*
362 * open/close a device
363 */
364static int bpq_open(struct net_device *dev)
365{
366 netif_start_queue(dev);
367 return 0;
368}
369
370static int bpq_close(struct net_device *dev)
371{
372 netif_stop_queue(dev);
373 return 0;
374}
375
376
377/* ------------------------------------------------------------------------ */
378
379
380/*
381 * Proc filesystem
382 */
383static const char * bpq_print_ethaddr(const unsigned char *e)
384{
385 static char buf[18];
386
387 sprintf(buf, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
388 e[0], e[1], e[2], e[3], e[4], e[5]);
389
390 return buf;
391}
392
393static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
394{
395 int i = 1;
396 struct bpqdev *bpqdev;
397
398 rcu_read_lock();
399
400 if (*pos == 0)
401 return SEQ_START_TOKEN;
402
bc0a7438 403 list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
1da177e4
LT
404 if (i == *pos)
405 return bpqdev;
406 }
407 return NULL;
408}
409
410static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
411{
412 struct list_head *p;
413
414 ++*pos;
415
416 if (v == SEQ_START_TOKEN)
b34bd06e 417 p = rcu_dereference(bpq_devices.next);
1da177e4 418 else
b34bd06e 419 p = rcu_dereference(((struct bpqdev *)v)->bpq_list.next);
1da177e4
LT
420
421 return (p == &bpq_devices) ? NULL
b34bd06e 422 : list_entry(p, struct bpqdev, bpq_list);
1da177e4
LT
423}
424
425static void bpq_seq_stop(struct seq_file *seq, void *v)
426{
427 rcu_read_unlock();
428}
429
430
431static int bpq_seq_show(struct seq_file *seq, void *v)
432{
433 if (v == SEQ_START_TOKEN)
434 seq_puts(seq,
435 "dev ether destination accept from\n");
436 else {
437 const struct bpqdev *bpqdev = v;
438
439 seq_printf(seq, "%-5s %-10s %s ",
440 bpqdev->axdev->name, bpqdev->ethdev->name,
441 bpq_print_ethaddr(bpqdev->dest_addr));
442
443 seq_printf(seq, "%s\n",
444 (bpqdev->acpt_addr[0] & 0x01) ? "*"
445 : bpq_print_ethaddr(bpqdev->acpt_addr));
446
447 }
448 return 0;
449}
450
451static struct seq_operations bpq_seqops = {
452 .start = bpq_seq_start,
453 .next = bpq_seq_next,
454 .stop = bpq_seq_stop,
455 .show = bpq_seq_show,
456};
457
458static int bpq_info_open(struct inode *inode, struct file *file)
459{
460 return seq_open(file, &bpq_seqops);
461}
462
d54b1fdb 463static const struct file_operations bpq_info_fops = {
1da177e4
LT
464 .owner = THIS_MODULE,
465 .open = bpq_info_open,
466 .read = seq_read,
467 .llseek = seq_lseek,
468 .release = seq_release,
469};
470
471
472/* ------------------------------------------------------------------------ */
473
474
475static void bpq_setup(struct net_device *dev)
476{
477
478 dev->hard_start_xmit = bpq_xmit;
479 dev->open = bpq_open;
480 dev->stop = bpq_close;
481 dev->set_mac_address = bpq_set_mac_address;
482 dev->get_stats = bpq_get_stats;
483 dev->do_ioctl = bpq_ioctl;
484 dev->destructor = free_netdev;
485
15b1c0e8
RB
486 memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
487 memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN);
1da177e4
LT
488
489 dev->flags = 0;
490
491#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
6f74998e 492 dev->hard_header = ax25_hard_header;
1da177e4
LT
493 dev->rebuild_header = ax25_rebuild_header;
494#endif
495
496 dev->type = ARPHRD_AX25;
497 dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
498 dev->mtu = AX25_DEF_PACLEN;
499 dev->addr_len = AX25_ADDR_LEN;
500
501}
502
503/*
504 * Setup a new device.
505 */
506static int bpq_new_device(struct net_device *edev)
507{
508 int err;
509 struct net_device *ndev;
510 struct bpqdev *bpq;
511
512 ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d",
513 bpq_setup);
514 if (!ndev)
515 return -ENOMEM;
516
517
518 bpq = netdev_priv(ndev);
519 dev_hold(edev);
520 bpq->ethdev = edev;
521 bpq->axdev = ndev;
522
523 memcpy(bpq->dest_addr, bcast_addr, sizeof(bpq_eth_addr));
524 memcpy(bpq->acpt_addr, bcast_addr, sizeof(bpq_eth_addr));
525
526 err = dev_alloc_name(ndev, ndev->name);
527 if (err < 0)
528 goto error;
529
530 err = register_netdevice(ndev);
531 if (err)
532 goto error;
1eeb7e42 533 lockdep_set_class(&ndev->_xmit_lock, &bpq_netdev_xmit_lock_key);
1da177e4
LT
534
535 /* List protected by RTNL */
536 list_add_rcu(&bpq->bpq_list, &bpq_devices);
537 return 0;
538
539 error:
540 dev_put(edev);
541 free_netdev(ndev);
542 return err;
543
544}
545
546static void bpq_free_device(struct net_device *ndev)
547{
548 struct bpqdev *bpq = netdev_priv(ndev);
549
550 dev_put(bpq->ethdev);
551 list_del_rcu(&bpq->bpq_list);
552
553 unregister_netdevice(ndev);
554}
555
556/*
557 * Handle device status changes.
558 */
559static int bpq_device_event(struct notifier_block *this,unsigned long event, void *ptr)
560{
561 struct net_device *dev = (struct net_device *)ptr;
562
563 if (!dev_is_ethdev(dev))
564 return NOTIFY_DONE;
565
1da177e4
LT
566 switch (event) {
567 case NETDEV_UP: /* new ethernet device -> new BPQ interface */
568 if (bpq_get_ax25_dev(dev) == NULL)
569 bpq_new_device(dev);
570 break;
571
572 case NETDEV_DOWN: /* ethernet device closed -> close BPQ interface */
573 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
574 dev_close(dev);
575 break;
576
577 case NETDEV_UNREGISTER: /* ethernet device removed -> free BPQ interface */
578 if ((dev = bpq_get_ax25_dev(dev)) != NULL)
579 bpq_free_device(dev);
580 break;
581 default:
582 break;
583 }
1da177e4
LT
584
585 return NOTIFY_DONE;
586}
587
588
589/* ------------------------------------------------------------------------ */
590
591/*
592 * Initialize driver. To be called from af_ax25 if not compiled as a
593 * module
594 */
595static int __init bpq_init_driver(void)
596{
597#ifdef CONFIG_PROC_FS
457c4cbc 598 if (!proc_net_fops_create(&init_net, "bpqether", S_IRUGO, &bpq_info_fops)) {
1da177e4
LT
599 printk(KERN_ERR
600 "bpq: cannot create /proc/net/bpqether entry.\n");
601 return -ENOENT;
602 }
603#endif /* CONFIG_PROC_FS */
604
605 dev_add_pack(&bpq_packet_type);
606
607 register_netdevice_notifier(&bpq_dev_notifier);
608
609 printk(banner);
610
611 return 0;
612}
613
614static void __exit bpq_cleanup_driver(void)
615{
616 struct bpqdev *bpq;
617
618 dev_remove_pack(&bpq_packet_type);
619
620 unregister_netdevice_notifier(&bpq_dev_notifier);
621
457c4cbc 622 proc_net_remove(&init_net, "bpqether");
1da177e4
LT
623
624 rtnl_lock();
625 while (!list_empty(&bpq_devices)) {
626 bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
627 bpq_free_device(bpq->axdev);
628 }
629 rtnl_unlock();
630}
631
632MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
633MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
634MODULE_LICENSE("GPL");
635module_init(bpq_init_driver);
636module_exit(bpq_cleanup_driver);
This page took 0.299255 seconds and 5 git commands to generate.