lapbether.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/in.h>
  26. #include <linux/kernel.h>
  27. #include <linux/string.h>
  28. #include <linux/net.h>
  29. #include <linux/inet.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/if_arp.h>
  32. #include <linux/skbuff.h>
  33. #include <net/sock.h>
  34. #include <asm/system.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/mm.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/notifier.h>
  39. #include <linux/stat.h>
  40. #include <linux/netfilter.h>
  41. #include <linux/module.h>
  42. #include <linux/lapb.h>
  43. #include <linux/init.h>
  44. #include <net/x25device.h>
  45. static char 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. };
  54. static LIST_HEAD(lapbeth_devices);
  55. /* ------------------------------------------------------------------------ */
  56. /*
  57. * Get the LAPB device for the ethernet device
  58. */
  59. static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
  60. {
  61. struct lapbethdev *lapbeth;
  62. list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node) {
  63. if (lapbeth->ethdev == dev)
  64. return lapbeth;
  65. }
  66. return NULL;
  67. }
  68. static __inline__ int dev_is_ethdev(struct net_device *dev)
  69. {
  70. return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
  71. }
  72. /* ------------------------------------------------------------------------ */
  73. /*
  74. * Receive a LAPB frame via an ethernet interface.
  75. */
  76. static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
  77. {
  78. int len, err;
  79. struct lapbethdev *lapbeth;
  80. if (dev_net(dev) != &init_net)
  81. goto drop;
  82. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  83. return NET_RX_DROP;
  84. if (!pskb_may_pull(skb, 2))
  85. goto drop;
  86. rcu_read_lock();
  87. lapbeth = lapbeth_get_x25_dev(dev);
  88. if (!lapbeth)
  89. goto drop_unlock;
  90. if (!netif_running(lapbeth->axdev))
  91. goto drop_unlock;
  92. len = skb->data[0] + skb->data[1] * 256;
  93. dev->stats.rx_packets++;
  94. dev->stats.rx_bytes += len;
  95. skb_pull(skb, 2); /* Remove the length bytes */
  96. skb_trim(skb, len); /* Set the length of the data */
  97. if ((err = lapb_data_received(lapbeth->axdev, skb)) != LAPB_OK) {
  98. printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
  99. goto drop_unlock;
  100. }
  101. out:
  102. rcu_read_unlock();
  103. return 0;
  104. drop_unlock:
  105. kfree_skb(skb);
  106. goto out;
  107. drop:
  108. kfree_skb(skb);
  109. return 0;
  110. }
  111. static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
  112. {
  113. unsigned char *ptr;
  114. skb_push(skb, 1);
  115. if (skb_cow(skb, 1))
  116. return NET_RX_DROP;
  117. ptr = skb->data;
  118. *ptr = 0x00;
  119. skb->protocol = x25_type_trans(skb, dev);
  120. return netif_rx(skb);
  121. }
  122. /*
  123. * Send a LAPB frame via an ethernet interface
  124. */
  125. static int lapbeth_xmit(struct sk_buff *skb, struct net_device *dev)
  126. {
  127. int err = -ENODEV;
  128. /*
  129. * Just to be *really* sure not to send anything if the interface
  130. * is down, the ethernet device may have gone.
  131. */
  132. if (!netif_running(dev)) {
  133. goto drop;
  134. }
  135. switch (skb->data[0]) {
  136. case 0x00:
  137. err = 0;
  138. break;
  139. case 0x01:
  140. if ((err = lapb_connect_request(dev)) != LAPB_OK)
  141. printk(KERN_ERR "lapbeth: lapb_connect_request "
  142. "error: %d\n", err);
  143. goto drop_ok;
  144. case 0x02:
  145. if ((err = lapb_disconnect_request(dev)) != LAPB_OK)
  146. printk(KERN_ERR "lapbeth: lapb_disconnect_request "
  147. "err: %d\n", err);
  148. /* Fall thru */
  149. default:
  150. goto drop_ok;
  151. }
  152. skb_pull(skb, 1);
  153. if ((err = lapb_data_request(dev, skb)) != LAPB_OK) {
  154. printk(KERN_ERR "lapbeth: lapb_data_request error - %d\n", err);
  155. err = -ENOMEM;
  156. goto drop;
  157. }
  158. err = 0;
  159. out:
  160. return err;
  161. drop_ok:
  162. err = 0;
  163. drop:
  164. kfree_skb(skb);
  165. goto out;
  166. }
  167. static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
  168. {
  169. struct lapbethdev *lapbeth = netdev_priv(ndev);
  170. unsigned char *ptr;
  171. struct net_device *dev;
  172. int size = skb->len;
  173. skb->protocol = htons(ETH_P_X25);
  174. ptr = skb_push(skb, 2);
  175. *ptr++ = size % 256;
  176. *ptr++ = size / 256;
  177. ndev->stats.tx_packets++;
  178. ndev->stats.tx_bytes += size;
  179. skb->dev = dev = lapbeth->ethdev;
  180. dev_hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
  181. dev_queue_xmit(skb);
  182. }
  183. static void lapbeth_connected(struct net_device *dev, int reason)
  184. {
  185. unsigned char *ptr;
  186. struct sk_buff *skb = dev_alloc_skb(1);
  187. if (!skb) {
  188. printk(KERN_ERR "lapbeth: out of memory\n");
  189. return;
  190. }
  191. ptr = skb_put(skb, 1);
  192. *ptr = 0x01;
  193. skb->protocol = x25_type_trans(skb, dev);
  194. netif_rx(skb);
  195. }
  196. static void lapbeth_disconnected(struct net_device *dev, int reason)
  197. {
  198. unsigned char *ptr;
  199. struct sk_buff *skb = dev_alloc_skb(1);
  200. if (!skb) {
  201. printk(KERN_ERR "lapbeth: out of memory\n");
  202. return;
  203. }
  204. ptr = skb_put(skb, 1);
  205. *ptr = 0x02;
  206. skb->protocol = x25_type_trans(skb, dev);
  207. netif_rx(skb);
  208. }
  209. /*
  210. * Set AX.25 callsign
  211. */
  212. static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
  213. {
  214. struct sockaddr *sa = addr;
  215. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  216. return 0;
  217. }
  218. static struct lapb_register_struct lapbeth_callbacks = {
  219. .connect_confirmation = lapbeth_connected,
  220. .connect_indication = lapbeth_connected,
  221. .disconnect_confirmation = lapbeth_disconnected,
  222. .disconnect_indication = lapbeth_disconnected,
  223. .data_indication = lapbeth_data_indication,
  224. .data_transmit = lapbeth_data_transmit,
  225. };
  226. /*
  227. * open/close a device
  228. */
  229. static int lapbeth_open(struct net_device *dev)
  230. {
  231. int err;
  232. if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
  233. printk(KERN_ERR "lapbeth: lapb_register error - %d\n", err);
  234. return -ENODEV;
  235. }
  236. netif_start_queue(dev);
  237. return 0;
  238. }
  239. static int lapbeth_close(struct net_device *dev)
  240. {
  241. int err;
  242. netif_stop_queue(dev);
  243. if ((err = lapb_unregister(dev)) != LAPB_OK)
  244. printk(KERN_ERR "lapbeth: lapb_unregister error - %d\n", err);
  245. return 0;
  246. }
  247. /* ------------------------------------------------------------------------ */
  248. static const struct net_device_ops lapbeth_netdev_ops = {
  249. .ndo_open = lapbeth_open,
  250. .ndo_stop = lapbeth_close,
  251. .ndo_start_xmit = lapbeth_xmit,
  252. .ndo_set_mac_address = lapbeth_set_mac_address,
  253. };
  254. static void lapbeth_setup(struct net_device *dev)
  255. {
  256. dev->netdev_ops = &lapbeth_netdev_ops;
  257. dev->destructor = free_netdev;
  258. dev->type = ARPHRD_X25;
  259. dev->hard_header_len = 3;
  260. dev->mtu = 1000;
  261. dev->addr_len = 0;
  262. }
  263. /*
  264. * Setup a new device.
  265. */
  266. static int lapbeth_new_device(struct net_device *dev)
  267. {
  268. struct net_device *ndev;
  269. struct lapbethdev *lapbeth;
  270. int rc = -ENOMEM;
  271. ASSERT_RTNL();
  272. ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d",
  273. lapbeth_setup);
  274. if (!ndev)
  275. goto out;
  276. lapbeth = netdev_priv(ndev);
  277. lapbeth->axdev = ndev;
  278. dev_hold(dev);
  279. lapbeth->ethdev = dev;
  280. rc = dev_alloc_name(ndev, ndev->name);
  281. if (rc < 0)
  282. goto fail;
  283. rc = -EIO;
  284. if (register_netdevice(ndev))
  285. goto fail;
  286. list_add_rcu(&lapbeth->node, &lapbeth_devices);
  287. rc = 0;
  288. out:
  289. return rc;
  290. fail:
  291. dev_put(dev);
  292. free_netdev(ndev);
  293. kfree(lapbeth);
  294. goto out;
  295. }
  296. /*
  297. * Free a lapb network device.
  298. */
  299. static void lapbeth_free_device(struct lapbethdev *lapbeth)
  300. {
  301. dev_put(lapbeth->ethdev);
  302. list_del_rcu(&lapbeth->node);
  303. unregister_netdevice(lapbeth->axdev);
  304. }
  305. /*
  306. * Handle device status changes.
  307. *
  308. * Called from notifier with RTNL held.
  309. */
  310. static int lapbeth_device_event(struct notifier_block *this,
  311. unsigned long event, void *ptr)
  312. {
  313. struct lapbethdev *lapbeth;
  314. struct net_device *dev = ptr;
  315. if (dev_net(dev) != &init_net)
  316. return NOTIFY_DONE;
  317. if (!dev_is_ethdev(dev))
  318. return NOTIFY_DONE;
  319. switch (event) {
  320. case NETDEV_UP:
  321. /* New ethernet device -> new LAPB interface */
  322. if (lapbeth_get_x25_dev(dev) == NULL)
  323. lapbeth_new_device(dev);
  324. break;
  325. case NETDEV_DOWN:
  326. /* ethernet device closed -> close LAPB interface */
  327. lapbeth = lapbeth_get_x25_dev(dev);
  328. if (lapbeth)
  329. dev_close(lapbeth->axdev);
  330. break;
  331. case NETDEV_UNREGISTER:
  332. /* ethernet device disappears -> remove LAPB interface */
  333. lapbeth = lapbeth_get_x25_dev(dev);
  334. if (lapbeth)
  335. lapbeth_free_device(lapbeth);
  336. break;
  337. }
  338. return NOTIFY_DONE;
  339. }
  340. /* ------------------------------------------------------------------------ */
  341. static struct packet_type lapbeth_packet_type __read_mostly = {
  342. .type = cpu_to_be16(ETH_P_DEC),
  343. .func = lapbeth_rcv,
  344. };
  345. static struct notifier_block lapbeth_dev_notifier = {
  346. .notifier_call = lapbeth_device_event,
  347. };
  348. static const char banner[] __initconst =
  349. KERN_INFO "LAPB Ethernet driver version 0.02\n";
  350. static int __init lapbeth_init_driver(void)
  351. {
  352. dev_add_pack(&lapbeth_packet_type);
  353. register_netdevice_notifier(&lapbeth_dev_notifier);
  354. printk(banner);
  355. return 0;
  356. }
  357. module_init(lapbeth_init_driver);
  358. static void __exit lapbeth_cleanup_driver(void)
  359. {
  360. struct lapbethdev *lapbeth;
  361. struct list_head *entry, *tmp;
  362. dev_remove_pack(&lapbeth_packet_type);
  363. unregister_netdevice_notifier(&lapbeth_dev_notifier);
  364. rtnl_lock();
  365. list_for_each_safe(entry, tmp, &lapbeth_devices) {
  366. lapbeth = list_entry(entry, struct lapbethdev, node);
  367. dev_put(lapbeth->ethdev);
  368. unregister_netdevice(lapbeth->axdev);
  369. }
  370. rtnl_unlock();
  371. }
  372. module_exit(lapbeth_cleanup_driver);
  373. MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
  374. MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
  375. MODULE_LICENSE("GPL");