bpqether.c 15 KB

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