bpqether.c 15 KB

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