bpqether.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. #include <linux/errno.h>
  57. #include <linux/types.h>
  58. #include <linux/socket.h>
  59. #include <linux/in.h>
  60. #include <linux/kernel.h>
  61. #include <linux/string.h>
  62. #include <linux/net.h>
  63. #include <linux/slab.h>
  64. #include <net/ax25.h>
  65. #include <linux/inet.h>
  66. #include <linux/netdevice.h>
  67. #include <linux/etherdevice.h>
  68. #include <linux/if_arp.h>
  69. #include <linux/skbuff.h>
  70. #include <net/sock.h>
  71. #include <asm/uaccess.h>
  72. #include <linux/mm.h>
  73. #include <linux/interrupt.h>
  74. #include <linux/notifier.h>
  75. #include <linux/proc_fs.h>
  76. #include <linux/seq_file.h>
  77. #include <linux/stat.h>
  78. #include <linux/module.h>
  79. #include <linux/init.h>
  80. #include <linux/rtnetlink.h>
  81. #include <net/ip.h>
  82. #include <net/arp.h>
  83. #include <net/net_namespace.h>
  84. #include <linux/bpqether.h>
  85. static const char banner[] __initconst = KERN_INFO \
  86. "AX.25: bpqether driver version 004\n";
  87. static int bpq_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
  88. static int bpq_device_event(struct notifier_block *, unsigned long, void *);
  89. static struct packet_type bpq_packet_type __read_mostly = {
  90. .type = cpu_to_be16(ETH_P_BPQ),
  91. .func = bpq_rcv,
  92. };
  93. static struct notifier_block bpq_dev_notifier = {
  94. .notifier_call = bpq_device_event,
  95. };
  96. struct bpqdev {
  97. struct list_head bpq_list; /* list of bpq devices chain */
  98. struct net_device *ethdev; /* link to ethernet device */
  99. struct net_device *axdev; /* bpq device (bpq#) */
  100. char dest_addr[6]; /* ether destination address */
  101. char acpt_addr[6]; /* accept ether frames from this address only */
  102. };
  103. static LIST_HEAD(bpq_devices);
  104. /*
  105. * bpqether network devices are paired with ethernet devices below them, so
  106. * form a special "super class" of normal ethernet devices; split their locks
  107. * off into a separate class since they always nest.
  108. */
  109. static struct lock_class_key bpq_netdev_xmit_lock_key;
  110. static struct lock_class_key bpq_netdev_addr_lock_key;
  111. static void bpq_set_lockdep_class_one(struct net_device *dev,
  112. struct netdev_queue *txq,
  113. void *_unused)
  114. {
  115. lockdep_set_class(&txq->_xmit_lock, &bpq_netdev_xmit_lock_key);
  116. }
  117. static void bpq_set_lockdep_class(struct net_device *dev)
  118. {
  119. lockdep_set_class(&dev->addr_list_lock, &bpq_netdev_addr_lock_key);
  120. netdev_for_each_tx_queue(dev, bpq_set_lockdep_class_one, NULL);
  121. }
  122. /* ------------------------------------------------------------------------ */
  123. /*
  124. * Get the ethernet device for a BPQ device
  125. */
  126. static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
  127. {
  128. struct bpqdev *bpq = netdev_priv(dev);
  129. return bpq ? bpq->ethdev : NULL;
  130. }
  131. /*
  132. * Get the BPQ device for the ethernet device
  133. */
  134. static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
  135. {
  136. struct bpqdev *bpq;
  137. list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list) {
  138. if (bpq->ethdev == dev)
  139. return bpq->axdev;
  140. }
  141. return NULL;
  142. }
  143. static inline int dev_is_ethdev(struct net_device *dev)
  144. {
  145. return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
  146. }
  147. /* ------------------------------------------------------------------------ */
  148. /*
  149. * Receive an AX.25 frame via an ethernet interface.
  150. */
  151. static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
  152. {
  153. int len;
  154. char * ptr;
  155. struct ethhdr *eth;
  156. struct bpqdev *bpq;
  157. if (!net_eq(dev_net(dev), &init_net))
  158. goto drop;
  159. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  160. return NET_RX_DROP;
  161. if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
  162. goto drop;
  163. rcu_read_lock();
  164. dev = bpq_get_ax25_dev(dev);
  165. if (dev == NULL || !netif_running(dev))
  166. goto drop_unlock;
  167. /*
  168. * if we want to accept frames from just one ethernet device
  169. * we check the source address of the sender.
  170. */
  171. bpq = netdev_priv(dev);
  172. eth = eth_hdr(skb);
  173. if (!(bpq->acpt_addr[0] & 0x01) &&
  174. !ether_addr_equal(eth->h_source, bpq->acpt_addr))
  175. goto drop_unlock;
  176. if (skb_cow(skb, sizeof(struct ethhdr)))
  177. goto drop_unlock;
  178. len = skb->data[0] + skb->data[1] * 256 - 5;
  179. skb_pull(skb, 2); /* Remove the length bytes */
  180. skb_trim(skb, len); /* Set the length of the data */
  181. dev->stats.rx_packets++;
  182. dev->stats.rx_bytes += len;
  183. ptr = skb_push(skb, 1);
  184. *ptr = 0;
  185. skb->protocol = ax25_type_trans(skb, dev);
  186. netif_rx(skb);
  187. unlock:
  188. rcu_read_unlock();
  189. return 0;
  190. drop_unlock:
  191. kfree_skb(skb);
  192. goto unlock;
  193. drop:
  194. kfree_skb(skb);
  195. return 0;
  196. }
  197. /*
  198. * Send an AX.25 frame via an ethernet interface
  199. */
  200. static netdev_tx_t bpq_xmit(struct sk_buff *skb, struct net_device *dev)
  201. {
  202. unsigned char *ptr;
  203. struct bpqdev *bpq;
  204. struct net_device *orig_dev;
  205. int size;
  206. if (skb->protocol == htons(ETH_P_IP))
  207. return ax25_ip_xmit(skb);
  208. /*
  209. * Just to be *really* sure not to send anything if the interface
  210. * is down, the ethernet device may have gone.
  211. */
  212. if (!netif_running(dev)) {
  213. kfree_skb(skb);
  214. return NETDEV_TX_OK;
  215. }
  216. skb_pull(skb, 1); /* Drop KISS byte */
  217. size = skb->len;
  218. /*
  219. * We're about to mess with the skb which may still shared with the
  220. * generic networking code so unshare and ensure it's got enough
  221. * space for the BPQ headers.
  222. */
  223. if (skb_cow(skb, AX25_BPQ_HEADER_LEN)) {
  224. if (net_ratelimit())
  225. pr_err("bpqether: out of memory\n");
  226. kfree_skb(skb);
  227. return NETDEV_TX_OK;
  228. }
  229. ptr = skb_push(skb, 2); /* Make space for length */
  230. *ptr++ = (size + 5) % 256;
  231. *ptr++ = (size + 5) / 256;
  232. bpq = netdev_priv(dev);
  233. orig_dev = dev;
  234. if ((dev = bpq_get_ether_dev(dev)) == NULL) {
  235. orig_dev->stats.tx_dropped++;
  236. kfree_skb(skb);
  237. return NETDEV_TX_OK;
  238. }
  239. skb->protocol = ax25_type_trans(skb, dev);
  240. skb_reset_network_header(skb);
  241. dev_hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
  242. dev->stats.tx_packets++;
  243. dev->stats.tx_bytes+=skb->len;
  244. dev_queue_xmit(skb);
  245. netif_wake_queue(dev);
  246. return NETDEV_TX_OK;
  247. }
  248. /*
  249. * Set AX.25 callsign
  250. */
  251. static int bpq_set_mac_address(struct net_device *dev, void *addr)
  252. {
  253. struct sockaddr *sa = (struct sockaddr *)addr;
  254. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  255. return 0;
  256. }
  257. /* Ioctl commands
  258. *
  259. * SIOCSBPQETHOPT reserved for enhancements
  260. * SIOCSBPQETHADDR set the destination and accepted
  261. * source ethernet address (broadcast
  262. * or multicast: accept all)
  263. */
  264. static int bpq_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  265. {
  266. struct bpq_ethaddr __user *ethaddr = ifr->ifr_data;
  267. struct bpqdev *bpq = netdev_priv(dev);
  268. struct bpq_req req;
  269. if (!capable(CAP_NET_ADMIN))
  270. return -EPERM;
  271. switch (cmd) {
  272. case SIOCSBPQETHOPT:
  273. if (copy_from_user(&req, ifr->ifr_data, sizeof(struct bpq_req)))
  274. return -EFAULT;
  275. switch (req.cmd) {
  276. case SIOCGBPQETHPARAM:
  277. case SIOCSBPQETHPARAM:
  278. default:
  279. return -EINVAL;
  280. }
  281. break;
  282. case SIOCSBPQETHADDR:
  283. if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
  284. return -EFAULT;
  285. if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
  286. return -EFAULT;
  287. break;
  288. default:
  289. return -EINVAL;
  290. }
  291. return 0;
  292. }
  293. /*
  294. * open/close a device
  295. */
  296. static int bpq_open(struct net_device *dev)
  297. {
  298. netif_start_queue(dev);
  299. return 0;
  300. }
  301. static int bpq_close(struct net_device *dev)
  302. {
  303. netif_stop_queue(dev);
  304. return 0;
  305. }
  306. /* ------------------------------------------------------------------------ */
  307. /*
  308. * Proc filesystem
  309. */
  310. static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
  311. __acquires(RCU)
  312. {
  313. int i = 1;
  314. struct bpqdev *bpqdev;
  315. rcu_read_lock();
  316. if (*pos == 0)
  317. return SEQ_START_TOKEN;
  318. list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
  319. if (i == *pos)
  320. return bpqdev;
  321. }
  322. return NULL;
  323. }
  324. static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  325. {
  326. struct list_head *p;
  327. struct bpqdev *bpqdev = v;
  328. ++*pos;
  329. if (v == SEQ_START_TOKEN)
  330. p = rcu_dereference(list_next_rcu(&bpq_devices));
  331. else
  332. p = rcu_dereference(list_next_rcu(&bpqdev->bpq_list));
  333. return (p == &bpq_devices) ? NULL
  334. : list_entry(p, struct bpqdev, bpq_list);
  335. }
  336. static void bpq_seq_stop(struct seq_file *seq, void *v)
  337. __releases(RCU)
  338. {
  339. rcu_read_unlock();
  340. }
  341. static int bpq_seq_show(struct seq_file *seq, void *v)
  342. {
  343. if (v == SEQ_START_TOKEN)
  344. seq_puts(seq,
  345. "dev ether destination accept from\n");
  346. else {
  347. const struct bpqdev *bpqdev = v;
  348. seq_printf(seq, "%-5s %-10s %pM ",
  349. bpqdev->axdev->name, bpqdev->ethdev->name,
  350. bpqdev->dest_addr);
  351. if (is_multicast_ether_addr(bpqdev->acpt_addr))
  352. seq_printf(seq, "*\n");
  353. else
  354. seq_printf(seq, "%pM\n", bpqdev->acpt_addr);
  355. }
  356. return 0;
  357. }
  358. static const struct seq_operations bpq_seqops = {
  359. .start = bpq_seq_start,
  360. .next = bpq_seq_next,
  361. .stop = bpq_seq_stop,
  362. .show = bpq_seq_show,
  363. };
  364. static int bpq_info_open(struct inode *inode, struct file *file)
  365. {
  366. return seq_open(file, &bpq_seqops);
  367. }
  368. static const struct file_operations bpq_info_fops = {
  369. .owner = THIS_MODULE,
  370. .open = bpq_info_open,
  371. .read = seq_read,
  372. .llseek = seq_lseek,
  373. .release = seq_release,
  374. };
  375. /* ------------------------------------------------------------------------ */
  376. static const struct net_device_ops bpq_netdev_ops = {
  377. .ndo_open = bpq_open,
  378. .ndo_stop = bpq_close,
  379. .ndo_start_xmit = bpq_xmit,
  380. .ndo_set_mac_address = bpq_set_mac_address,
  381. .ndo_do_ioctl = bpq_ioctl,
  382. };
  383. static void bpq_setup(struct net_device *dev)
  384. {
  385. dev->netdev_ops = &bpq_netdev_ops;
  386. dev->destructor = free_netdev;
  387. memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
  388. memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN);
  389. dev->flags = 0;
  390. dev->features = NETIF_F_LLTX; /* Allow recursion */
  391. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  392. dev->header_ops = &ax25_header_ops;
  393. #endif
  394. dev->type = ARPHRD_AX25;
  395. dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
  396. dev->mtu = AX25_DEF_PACLEN;
  397. dev->addr_len = AX25_ADDR_LEN;
  398. }
  399. /*
  400. * Setup a new device.
  401. */
  402. static int bpq_new_device(struct net_device *edev)
  403. {
  404. int err;
  405. struct net_device *ndev;
  406. struct bpqdev *bpq;
  407. ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN,
  408. bpq_setup);
  409. if (!ndev)
  410. return -ENOMEM;
  411. bpq = netdev_priv(ndev);
  412. dev_hold(edev);
  413. bpq->ethdev = edev;
  414. bpq->axdev = ndev;
  415. eth_broadcast_addr(bpq->dest_addr);
  416. eth_broadcast_addr(bpq->acpt_addr);
  417. err = register_netdevice(ndev);
  418. if (err)
  419. goto error;
  420. bpq_set_lockdep_class(ndev);
  421. /* List protected by RTNL */
  422. list_add_rcu(&bpq->bpq_list, &bpq_devices);
  423. return 0;
  424. error:
  425. dev_put(edev);
  426. free_netdev(ndev);
  427. return err;
  428. }
  429. static void bpq_free_device(struct net_device *ndev)
  430. {
  431. struct bpqdev *bpq = netdev_priv(ndev);
  432. dev_put(bpq->ethdev);
  433. list_del_rcu(&bpq->bpq_list);
  434. unregister_netdevice(ndev);
  435. }
  436. /*
  437. * Handle device status changes.
  438. */
  439. static int bpq_device_event(struct notifier_block *this,
  440. unsigned long event, void *ptr)
  441. {
  442. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  443. if (!net_eq(dev_net(dev), &init_net))
  444. return NOTIFY_DONE;
  445. if (!dev_is_ethdev(dev))
  446. return NOTIFY_DONE;
  447. switch (event) {
  448. case NETDEV_UP: /* new ethernet device -> new BPQ interface */
  449. if (bpq_get_ax25_dev(dev) == NULL)
  450. bpq_new_device(dev);
  451. break;
  452. case NETDEV_DOWN: /* ethernet device closed -> close BPQ interface */
  453. if ((dev = bpq_get_ax25_dev(dev)) != NULL)
  454. dev_close(dev);
  455. break;
  456. case NETDEV_UNREGISTER: /* ethernet device removed -> free BPQ interface */
  457. if ((dev = bpq_get_ax25_dev(dev)) != NULL)
  458. bpq_free_device(dev);
  459. break;
  460. default:
  461. break;
  462. }
  463. return NOTIFY_DONE;
  464. }
  465. /* ------------------------------------------------------------------------ */
  466. /*
  467. * Initialize driver. To be called from af_ax25 if not compiled as a
  468. * module
  469. */
  470. static int __init bpq_init_driver(void)
  471. {
  472. #ifdef CONFIG_PROC_FS
  473. if (!proc_create("bpqether", S_IRUGO, init_net.proc_net,
  474. &bpq_info_fops)) {
  475. printk(KERN_ERR
  476. "bpq: cannot create /proc/net/bpqether entry.\n");
  477. return -ENOENT;
  478. }
  479. #endif /* CONFIG_PROC_FS */
  480. dev_add_pack(&bpq_packet_type);
  481. register_netdevice_notifier(&bpq_dev_notifier);
  482. printk(banner);
  483. return 0;
  484. }
  485. static void __exit bpq_cleanup_driver(void)
  486. {
  487. struct bpqdev *bpq;
  488. dev_remove_pack(&bpq_packet_type);
  489. unregister_netdevice_notifier(&bpq_dev_notifier);
  490. remove_proc_entry("bpqether", init_net.proc_net);
  491. rtnl_lock();
  492. while (!list_empty(&bpq_devices)) {
  493. bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
  494. bpq_free_device(bpq->axdev);
  495. }
  496. rtnl_unlock();
  497. }
  498. MODULE_AUTHOR("Joerg Reuter DL1BKE <jreuter@yaina.de>");
  499. MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
  500. MODULE_LICENSE("GPL");
  501. module_init(bpq_init_driver);
  502. module_exit(bpq_cleanup_driver);