lapbether.c 10 KB

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