lapbether.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * "LAPB via ethernet" driver release 001
  3. *
  4. * This code REQUIRES 2.1.15 or higher/ NET3.038
  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 LAPB over Ethernet.
  13. *
  14. * This driver can use any ethernet destination address, and can be
  15. * limited to accept frames from one dedicated ethernet card only.
  16. *
  17. * History
  18. * LAPBETH 001 Jonathan Naylor Cloned from bpqether.c
  19. * 2000-10-29 Henner Eisen lapb_data_indication() return status.
  20. * 2000-11-14 Henner Eisen dev_hold/put, NETDEV_GOING_DOWN support
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/errno.h>
  24. #include <linux/types.h>
  25. #include <linux/socket.h>
  26. #include <linux/in.h>
  27. #include <linux/slab.h>
  28. #include <linux/kernel.h>
  29. #include <linux/string.h>
  30. #include <linux/net.h>
  31. #include <linux/inet.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/skbuff.h>
  35. #include <net/sock.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/mm.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/notifier.h>
  40. #include <linux/stat.h>
  41. #include <linux/module.h>
  42. #include <linux/lapb.h>
  43. #include <linux/init.h>
  44. #include <net/x25device.h>
  45. static const u8 bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  46. /* If this number is made larger, check that the temporary string buffer
  47. * in lapbeth_new_device is large enough to store the probe device name.*/
  48. #define MAXLAPBDEV 100
  49. struct lapbethdev {
  50. struct list_head node;
  51. struct net_device *ethdev; /* link to ethernet device */
  52. struct net_device *axdev; /* lapbeth device (lapb#) */
  53. bool up;
  54. spinlock_t up_lock; /* Protects "up" */
  55. };
  56. static LIST_HEAD(lapbeth_devices);
  57. /* ------------------------------------------------------------------------ */
  58. /*
  59. * Get the LAPB device for the ethernet device
  60. */
  61. static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
  62. {
  63. struct lapbethdev *lapbeth;
  64. list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node) {
  65. if (lapbeth->ethdev == dev)
  66. return lapbeth;
  67. }
  68. return NULL;
  69. }
  70. static __inline__ int dev_is_ethdev(struct net_device *dev)
  71. {
  72. return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
  73. }
  74. /* ------------------------------------------------------------------------ */
  75. /*
  76. * Receive a LAPB frame via an ethernet interface.
  77. */
  78. static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
  79. {
  80. int len, err;
  81. struct lapbethdev *lapbeth;
  82. if (dev_net(dev) != &init_net)
  83. goto drop;
  84. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  85. return NET_RX_DROP;
  86. if (!pskb_may_pull(skb, 2))
  87. goto drop;
  88. rcu_read_lock();
  89. lapbeth = lapbeth_get_x25_dev(dev);
  90. if (!lapbeth)
  91. goto drop_unlock_rcu;
  92. spin_lock_bh(&lapbeth->up_lock);
  93. if (!lapbeth->up)
  94. goto drop_unlock;
  95. len = skb->data[0] + skb->data[1] * 256;
  96. dev->stats.rx_packets++;
  97. dev->stats.rx_bytes += len;
  98. skb_pull(skb, 2); /* Remove the length bytes */
  99. skb_trim(skb, len); /* Set the length of the data */
  100. if ((err = lapb_data_received(lapbeth->axdev, skb)) != LAPB_OK) {
  101. printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
  102. goto drop_unlock;
  103. }
  104. out:
  105. spin_unlock_bh(&lapbeth->up_lock);
  106. rcu_read_unlock();
  107. return 0;
  108. drop_unlock:
  109. kfree_skb(skb);
  110. goto out;
  111. drop_unlock_rcu:
  112. rcu_read_unlock();
  113. drop:
  114. kfree_skb(skb);
  115. return 0;
  116. }
  117. static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
  118. {
  119. unsigned char *ptr;
  120. skb_push(skb, 1);
  121. if (skb_cow(skb, 1))
  122. return NET_RX_DROP;
  123. ptr = skb->data;
  124. *ptr = X25_IFACE_DATA;
  125. skb->protocol = x25_type_trans(skb, dev);
  126. return netif_rx(skb);
  127. }
  128. /*
  129. * Send a LAPB frame via an ethernet interface
  130. */
  131. static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
  132. struct net_device *dev)
  133. {
  134. struct lapbethdev *lapbeth = netdev_priv(dev);
  135. int err;
  136. spin_lock_bh(&lapbeth->up_lock);
  137. if (!lapbeth->up)
  138. goto drop;
  139. /* There should be a pseudo header of 1 byte added by upper layers.
  140. * Check to make sure it is there before reading it.
  141. */
  142. if (skb->len < 1)
  143. goto drop;
  144. switch (skb->data[0]) {
  145. case X25_IFACE_DATA:
  146. break;
  147. case X25_IFACE_CONNECT:
  148. if ((err = lapb_connect_request(dev)) != LAPB_OK)
  149. pr_err("lapb_connect_request error: %d\n", err);
  150. goto drop;
  151. case X25_IFACE_DISCONNECT:
  152. if ((err = lapb_disconnect_request(dev)) != LAPB_OK)
  153. pr_err("lapb_disconnect_request err: %d\n", err);
  154. /* Fall thru */
  155. default:
  156. goto drop;
  157. }
  158. skb_pull(skb, 1);
  159. if ((err = lapb_data_request(dev, skb)) != LAPB_OK) {
  160. pr_err("lapb_data_request error - %d\n", err);
  161. goto drop;
  162. }
  163. out:
  164. spin_unlock_bh(&lapbeth->up_lock);
  165. return NETDEV_TX_OK;
  166. drop:
  167. kfree_skb(skb);
  168. goto out;
  169. }
  170. static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
  171. {
  172. struct lapbethdev *lapbeth = netdev_priv(ndev);
  173. unsigned char *ptr;
  174. struct net_device *dev;
  175. int size = skb->len;
  176. ptr = skb_push(skb, 2);
  177. *ptr++ = size % 256;
  178. *ptr++ = size / 256;
  179. ndev->stats.tx_packets++;
  180. ndev->stats.tx_bytes += size;
  181. skb->dev = dev = lapbeth->ethdev;
  182. skb->protocol = htons(ETH_P_DEC);
  183. skb_reset_network_header(skb);
  184. dev_hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
  185. dev_queue_xmit(skb);
  186. }
  187. static void lapbeth_connected(struct net_device *dev, int reason)
  188. {
  189. unsigned char *ptr;
  190. struct sk_buff *skb = dev_alloc_skb(1);
  191. if (!skb) {
  192. pr_err("out of memory\n");
  193. return;
  194. }
  195. ptr = skb_put(skb, 1);
  196. *ptr = X25_IFACE_CONNECT;
  197. skb->protocol = x25_type_trans(skb, dev);
  198. netif_rx(skb);
  199. }
  200. static void lapbeth_disconnected(struct net_device *dev, int reason)
  201. {
  202. unsigned char *ptr;
  203. struct sk_buff *skb = dev_alloc_skb(1);
  204. if (!skb) {
  205. pr_err("out of memory\n");
  206. return;
  207. }
  208. ptr = skb_put(skb, 1);
  209. *ptr = X25_IFACE_DISCONNECT;
  210. skb->protocol = x25_type_trans(skb, dev);
  211. netif_rx(skb);
  212. }
  213. /*
  214. * Set AX.25 callsign
  215. */
  216. static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
  217. {
  218. struct sockaddr *sa = addr;
  219. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  220. return 0;
  221. }
  222. static const struct lapb_register_struct lapbeth_callbacks = {
  223. .connect_confirmation = lapbeth_connected,
  224. .connect_indication = lapbeth_connected,
  225. .disconnect_confirmation = lapbeth_disconnected,
  226. .disconnect_indication = lapbeth_disconnected,
  227. .data_indication = lapbeth_data_indication,
  228. .data_transmit = lapbeth_data_transmit,
  229. };
  230. /*
  231. * open/close a device
  232. */
  233. static int lapbeth_open(struct net_device *dev)
  234. {
  235. struct lapbethdev *lapbeth = netdev_priv(dev);
  236. int err;
  237. if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
  238. pr_err("lapb_register error: %d\n", err);
  239. return -ENODEV;
  240. }
  241. spin_lock_bh(&lapbeth->up_lock);
  242. lapbeth->up = true;
  243. spin_unlock_bh(&lapbeth->up_lock);
  244. return 0;
  245. }
  246. static int lapbeth_close(struct net_device *dev)
  247. {
  248. struct lapbethdev *lapbeth = netdev_priv(dev);
  249. int err;
  250. spin_lock_bh(&lapbeth->up_lock);
  251. lapbeth->up = false;
  252. spin_unlock_bh(&lapbeth->up_lock);
  253. if ((err = lapb_unregister(dev)) != LAPB_OK)
  254. pr_err("lapb_unregister error: %d\n", err);
  255. return 0;
  256. }
  257. /* ------------------------------------------------------------------------ */
  258. static const struct net_device_ops lapbeth_netdev_ops = {
  259. .ndo_open = lapbeth_open,
  260. .ndo_stop = lapbeth_close,
  261. .ndo_start_xmit = lapbeth_xmit,
  262. .ndo_set_mac_address = lapbeth_set_mac_address,
  263. };
  264. static void lapbeth_setup(struct net_device *dev)
  265. {
  266. dev->netdev_ops = &lapbeth_netdev_ops;
  267. dev->needs_free_netdev = true;
  268. dev->type = ARPHRD_X25;
  269. dev->hard_header_len = 0;
  270. dev->mtu = 1000;
  271. dev->addr_len = 0;
  272. }
  273. /*
  274. * Setup a new device.
  275. */
  276. static int lapbeth_new_device(struct net_device *dev)
  277. {
  278. struct net_device *ndev;
  279. struct lapbethdev *lapbeth;
  280. int rc = -ENOMEM;
  281. ASSERT_RTNL();
  282. ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d", NET_NAME_UNKNOWN,
  283. lapbeth_setup);
  284. if (!ndev)
  285. goto out;
  286. /* When transmitting data:
  287. * first this driver removes a pseudo header of 1 byte,
  288. * then the lapb module prepends an LAPB header of at most 3 bytes,
  289. * then this driver prepends a length field of 2 bytes,
  290. * then the underlying Ethernet device prepends its own header.
  291. */
  292. ndev->needed_headroom = -1 + 3 + 2 + dev->hard_header_len
  293. + dev->needed_headroom;
  294. ndev->needed_tailroom = dev->needed_tailroom;
  295. lapbeth = netdev_priv(ndev);
  296. lapbeth->axdev = ndev;
  297. dev_hold(dev);
  298. lapbeth->ethdev = dev;
  299. lapbeth->up = false;
  300. spin_lock_init(&lapbeth->up_lock);
  301. rc = -EIO;
  302. if (register_netdevice(ndev))
  303. goto fail;
  304. list_add_rcu(&lapbeth->node, &lapbeth_devices);
  305. rc = 0;
  306. out:
  307. return rc;
  308. fail:
  309. dev_put(dev);
  310. free_netdev(ndev);
  311. goto out;
  312. }
  313. /*
  314. * Free a lapb network device.
  315. */
  316. static void lapbeth_free_device(struct lapbethdev *lapbeth)
  317. {
  318. dev_put(lapbeth->ethdev);
  319. list_del_rcu(&lapbeth->node);
  320. unregister_netdevice(lapbeth->axdev);
  321. }
  322. /*
  323. * Handle device status changes.
  324. *
  325. * Called from notifier with RTNL held.
  326. */
  327. static int lapbeth_device_event(struct notifier_block *this,
  328. unsigned long event, void *ptr)
  329. {
  330. struct lapbethdev *lapbeth;
  331. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  332. if (dev_net(dev) != &init_net)
  333. return NOTIFY_DONE;
  334. if (!dev_is_ethdev(dev))
  335. return NOTIFY_DONE;
  336. switch (event) {
  337. case NETDEV_UP:
  338. /* New ethernet device -> new LAPB interface */
  339. if (lapbeth_get_x25_dev(dev) == NULL)
  340. lapbeth_new_device(dev);
  341. break;
  342. case NETDEV_DOWN:
  343. /* ethernet device closed -> close LAPB interface */
  344. lapbeth = lapbeth_get_x25_dev(dev);
  345. if (lapbeth)
  346. dev_close(lapbeth->axdev);
  347. break;
  348. case NETDEV_UNREGISTER:
  349. /* ethernet device disappears -> remove LAPB interface */
  350. lapbeth = lapbeth_get_x25_dev(dev);
  351. if (lapbeth)
  352. lapbeth_free_device(lapbeth);
  353. break;
  354. }
  355. return NOTIFY_DONE;
  356. }
  357. /* ------------------------------------------------------------------------ */
  358. static struct packet_type lapbeth_packet_type __read_mostly = {
  359. .type = cpu_to_be16(ETH_P_DEC),
  360. .func = lapbeth_rcv,
  361. };
  362. static struct notifier_block lapbeth_dev_notifier = {
  363. .notifier_call = lapbeth_device_event,
  364. };
  365. static const char banner[] __initconst =
  366. KERN_INFO "LAPB Ethernet driver version 0.02\n";
  367. static int __init lapbeth_init_driver(void)
  368. {
  369. dev_add_pack(&lapbeth_packet_type);
  370. register_netdevice_notifier(&lapbeth_dev_notifier);
  371. printk(banner);
  372. return 0;
  373. }
  374. module_init(lapbeth_init_driver);
  375. static void __exit lapbeth_cleanup_driver(void)
  376. {
  377. struct lapbethdev *lapbeth;
  378. struct list_head *entry, *tmp;
  379. dev_remove_pack(&lapbeth_packet_type);
  380. unregister_netdevice_notifier(&lapbeth_dev_notifier);
  381. rtnl_lock();
  382. list_for_each_safe(entry, tmp, &lapbeth_devices) {
  383. lapbeth = list_entry(entry, struct lapbethdev, node);
  384. dev_put(lapbeth->ethdev);
  385. unregister_netdevice(lapbeth->axdev);
  386. }
  387. rtnl_unlock();
  388. }
  389. module_exit(lapbeth_cleanup_driver);
  390. MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
  391. MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
  392. MODULE_LICENSE("GPL");