bpqether.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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/etherdevice.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 const char banner[] __initdata = KERN_INFO \
  87. "AX.25: bpqether driver version 004\n";
  88. static char bcast_addr[6]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  89. static char bpq_eth_addr[6];
  90. static int bpq_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
  91. static int bpq_device_event(struct notifier_block *, unsigned long, void *);
  92. static struct packet_type bpq_packet_type __read_mostly = {
  93. .type = cpu_to_be16(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. char dest_addr[6]; /* ether destination address */
  104. char acpt_addr[6]; /* accept ether frames from this address only */
  105. };
  106. static LIST_HEAD(bpq_devices);
  107. /*
  108. * bpqether network devices are paired with ethernet devices below them, so
  109. * form a special "super class" of normal ethernet devices; split their locks
  110. * off into a separate class since they always nest.
  111. */
  112. static struct lock_class_key bpq_netdev_xmit_lock_key;
  113. static struct lock_class_key bpq_netdev_addr_lock_key;
  114. static void bpq_set_lockdep_class_one(struct net_device *dev,
  115. struct netdev_queue *txq,
  116. void *_unused)
  117. {
  118. lockdep_set_class(&txq->_xmit_lock, &bpq_netdev_xmit_lock_key);
  119. }
  120. static void bpq_set_lockdep_class(struct net_device *dev)
  121. {
  122. lockdep_set_class(&dev->addr_list_lock, &bpq_netdev_addr_lock_key);
  123. netdev_for_each_tx_queue(dev, bpq_set_lockdep_class_one, NULL);
  124. }
  125. /* ------------------------------------------------------------------------ */
  126. /*
  127. * Get the ethernet device for a BPQ device
  128. */
  129. static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
  130. {
  131. struct bpqdev *bpq = netdev_priv(dev);
  132. return bpq ? bpq->ethdev : NULL;
  133. }
  134. /*
  135. * Get the BPQ device for the ethernet device
  136. */
  137. static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
  138. {
  139. struct bpqdev *bpq;
  140. list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list) {
  141. if (bpq->ethdev == dev)
  142. return bpq->axdev;
  143. }
  144. return NULL;
  145. }
  146. static inline int dev_is_ethdev(struct net_device *dev)
  147. {
  148. return (
  149. dev->type == ARPHRD_ETHER
  150. && strncmp(dev->name, "dummy", 5)
  151. );
  152. }
  153. /* ------------------------------------------------------------------------ */
  154. /*
  155. * Receive an AX.25 frame via an ethernet interface.
  156. */
  157. static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
  158. {
  159. int len;
  160. char * ptr;
  161. struct ethhdr *eth;
  162. struct bpqdev *bpq;
  163. if (dev_net(dev) != &init_net)
  164. goto drop;
  165. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  166. return NET_RX_DROP;
  167. if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
  168. goto drop;
  169. rcu_read_lock();
  170. dev = bpq_get_ax25_dev(dev);
  171. if (dev == NULL || !netif_running(dev))
  172. goto drop_unlock;
  173. /*
  174. * if we want to accept frames from just one ethernet device
  175. * we check the source address of the sender.
  176. */
  177. bpq = netdev_priv(dev);
  178. eth = eth_hdr(skb);
  179. if (!(bpq->acpt_addr[0] & 0x01) &&
  180. memcmp(eth->h_source, bpq->acpt_addr, ETH_ALEN))
  181. goto drop_unlock;
  182. if (skb_cow(skb, sizeof(struct ethhdr)))
  183. goto drop_unlock;
  184. len = skb->data[0] + skb->data[1] * 256 - 5;
  185. skb_pull(skb, 2); /* Remove the length bytes */
  186. skb_trim(skb, len); /* Set the length of the data */
  187. dev->stats.rx_packets++;
  188. dev->stats.rx_bytes += len;
  189. ptr = skb_push(skb, 1);
  190. *ptr = 0;
  191. skb->protocol = ax25_type_trans(skb, dev);
  192. netif_rx(skb);
  193. unlock:
  194. rcu_read_unlock();
  195. return 0;
  196. drop_unlock:
  197. kfree_skb(skb);
  198. goto unlock;
  199. drop:
  200. kfree_skb(skb);
  201. return 0;
  202. }
  203. /*
  204. * Send an AX.25 frame via an ethernet interface
  205. */
  206. static int bpq_xmit(struct sk_buff *skb, struct net_device *dev)
  207. {
  208. struct sk_buff *newskb;
  209. unsigned char *ptr;
  210. struct bpqdev *bpq;
  211. int size;
  212. /*
  213. * Just to be *really* sure not to send anything if the interface
  214. * is down, the ethernet device may have gone.
  215. */
  216. if (!netif_running(dev)) {
  217. kfree_skb(skb);
  218. return -ENODEV;
  219. }
  220. skb_pull(skb, 1);
  221. size = skb->len;
  222. /*
  223. * The AX.25 code leaves enough room for the ethernet header, but
  224. * sendto() does not.
  225. */
  226. if (skb_headroom(skb) < AX25_BPQ_HEADER_LEN) { /* Ough! */
  227. if ((newskb = skb_realloc_headroom(skb, AX25_BPQ_HEADER_LEN)) == NULL) {
  228. printk(KERN_WARNING "bpqether: out of memory\n");
  229. kfree_skb(skb);
  230. return -ENOMEM;
  231. }
  232. if (skb->sk != NULL)
  233. skb_set_owner_w(newskb, skb->sk);
  234. kfree_skb(skb);
  235. skb = newskb;
  236. }
  237. ptr = skb_push(skb, 2);
  238. *ptr++ = (size + 5) % 256;
  239. *ptr++ = (size + 5) / 256;
  240. bpq = netdev_priv(dev);
  241. if ((dev = bpq_get_ether_dev(dev)) == NULL) {
  242. dev->stats.tx_dropped++;
  243. kfree_skb(skb);
  244. return -ENODEV;
  245. }
  246. skb->protocol = ax25_type_trans(skb, dev);
  247. skb_reset_network_header(skb);
  248. dev_hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
  249. dev->stats.tx_packets++;
  250. dev->stats.tx_bytes+=skb->len;
  251. dev_queue_xmit(skb);
  252. netif_wake_queue(dev);
  253. return 0;
  254. }
  255. /*
  256. * Set AX.25 callsign
  257. */
  258. static int bpq_set_mac_address(struct net_device *dev, void *addr)
  259. {
  260. struct sockaddr *sa = (struct sockaddr *)addr;
  261. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  262. return 0;
  263. }
  264. /* Ioctl commands
  265. *
  266. * SIOCSBPQETHOPT reserved for enhancements
  267. * SIOCSBPQETHADDR set the destination and accepted
  268. * source ethernet address (broadcast
  269. * or multicast: accept all)
  270. */
  271. static int bpq_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  272. {
  273. struct bpq_ethaddr __user *ethaddr = ifr->ifr_data;
  274. struct bpqdev *bpq = netdev_priv(dev);
  275. struct bpq_req req;
  276. if (!capable(CAP_NET_ADMIN))
  277. return -EPERM;
  278. switch (cmd) {
  279. case SIOCSBPQETHOPT:
  280. if (copy_from_user(&req, ifr->ifr_data, sizeof(struct bpq_req)))
  281. return -EFAULT;
  282. switch (req.cmd) {
  283. case SIOCGBPQETHPARAM:
  284. case SIOCSBPQETHPARAM:
  285. default:
  286. return -EINVAL;
  287. }
  288. break;
  289. case SIOCSBPQETHADDR:
  290. if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
  291. return -EFAULT;
  292. if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
  293. return -EFAULT;
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. return 0;
  299. }
  300. /*
  301. * open/close a device
  302. */
  303. static int bpq_open(struct net_device *dev)
  304. {
  305. netif_start_queue(dev);
  306. return 0;
  307. }
  308. static int bpq_close(struct net_device *dev)
  309. {
  310. netif_stop_queue(dev);
  311. return 0;
  312. }
  313. /* ------------------------------------------------------------------------ */
  314. /*
  315. * Proc filesystem
  316. */
  317. static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
  318. __acquires(RCU)
  319. {
  320. int i = 1;
  321. struct bpqdev *bpqdev;
  322. rcu_read_lock();
  323. if (*pos == 0)
  324. return SEQ_START_TOKEN;
  325. list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
  326. if (i == *pos)
  327. return bpqdev;
  328. }
  329. return NULL;
  330. }
  331. static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  332. {
  333. struct list_head *p;
  334. ++*pos;
  335. if (v == SEQ_START_TOKEN)
  336. p = rcu_dereference(bpq_devices.next);
  337. else
  338. p = rcu_dereference(((struct bpqdev *)v)->bpq_list.next);
  339. return (p == &bpq_devices) ? NULL
  340. : list_entry(p, struct bpqdev, bpq_list);
  341. }
  342. static void bpq_seq_stop(struct seq_file *seq, void *v)
  343. __releases(RCU)
  344. {
  345. rcu_read_unlock();
  346. }
  347. static int bpq_seq_show(struct seq_file *seq, void *v)
  348. {
  349. if (v == SEQ_START_TOKEN)
  350. seq_puts(seq,
  351. "dev ether destination accept from\n");
  352. else {
  353. const struct bpqdev *bpqdev = v;
  354. seq_printf(seq, "%-5s %-10s %pM ",
  355. bpqdev->axdev->name, bpqdev->ethdev->name,
  356. bpqdev->dest_addr);
  357. if (is_multicast_ether_addr(bpqdev->acpt_addr))
  358. seq_printf(seq, "*\n");
  359. else
  360. seq_printf(seq, "%pM\n", bpqdev->acpt_addr);
  361. }
  362. return 0;
  363. }
  364. static const struct seq_operations bpq_seqops = {
  365. .start = bpq_seq_start,
  366. .next = bpq_seq_next,
  367. .stop = bpq_seq_stop,
  368. .show = bpq_seq_show,
  369. };
  370. static int bpq_info_open(struct inode *inode, struct file *file)
  371. {
  372. return seq_open(file, &bpq_seqops);
  373. }
  374. static const struct file_operations bpq_info_fops = {
  375. .owner = THIS_MODULE,
  376. .open = bpq_info_open,
  377. .read = seq_read,
  378. .llseek = seq_lseek,
  379. .release = seq_release,
  380. };
  381. /* ------------------------------------------------------------------------ */
  382. static const struct net_device_ops bpq_netdev_ops = {
  383. .ndo_open = bpq_open,
  384. .ndo_stop = bpq_close,
  385. .ndo_start_xmit = bpq_xmit,
  386. .ndo_set_mac_address = bpq_set_mac_address,
  387. .ndo_do_ioctl = bpq_ioctl,
  388. };
  389. static void bpq_setup(struct net_device *dev)
  390. {
  391. dev->netdev_ops = &bpq_netdev_ops;
  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->header_ops = &ax25_header_ops;
  398. #endif
  399. dev->type = ARPHRD_AX25;
  400. dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
  401. dev->mtu = AX25_DEF_PACLEN;
  402. dev->addr_len = AX25_ADDR_LEN;
  403. }
  404. /*
  405. * Setup a new device.
  406. */
  407. static int bpq_new_device(struct net_device *edev)
  408. {
  409. int err;
  410. struct net_device *ndev;
  411. struct bpqdev *bpq;
  412. ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d",
  413. bpq_setup);
  414. if (!ndev)
  415. return -ENOMEM;
  416. bpq = netdev_priv(ndev);
  417. dev_hold(edev);
  418. bpq->ethdev = edev;
  419. bpq->axdev = ndev;
  420. memcpy(bpq->dest_addr, bcast_addr, sizeof(bpq_eth_addr));
  421. memcpy(bpq->acpt_addr, bcast_addr, sizeof(bpq_eth_addr));
  422. err = dev_alloc_name(ndev, ndev->name);
  423. if (err < 0)
  424. goto error;
  425. err = register_netdevice(ndev);
  426. if (err)
  427. goto error;
  428. bpq_set_lockdep_class(ndev);
  429. /* List protected by RTNL */
  430. list_add_rcu(&bpq->bpq_list, &bpq_devices);
  431. return 0;
  432. error:
  433. dev_put(edev);
  434. free_netdev(ndev);
  435. return err;
  436. }
  437. static void bpq_free_device(struct net_device *ndev)
  438. {
  439. struct bpqdev *bpq = netdev_priv(ndev);
  440. dev_put(bpq->ethdev);
  441. list_del_rcu(&bpq->bpq_list);
  442. unregister_netdevice(ndev);
  443. }
  444. /*
  445. * Handle device status changes.
  446. */
  447. static int bpq_device_event(struct notifier_block *this,unsigned long event, void *ptr)
  448. {
  449. struct net_device *dev = (struct net_device *)ptr;
  450. if (dev_net(dev) != &init_net)
  451. return NOTIFY_DONE;
  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(&init_net, "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(&init_net, "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);