lapbether.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. struct net_device_stats stats; /* some statistics */
  54. };
  55. static struct list_head lapbeth_devices = LIST_HEAD_INIT(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->nd_net != &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. lapbeth->stats.rx_packets++;
  94. len = skb->data[0] + skb->data[1] * 256;
  95. lapbeth->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 = 0x00;
  120. skb->protocol = x25_type_trans(skb, dev);
  121. skb->dev->last_rx = jiffies;
  122. return netif_rx(skb);
  123. }
  124. /*
  125. * Send a LAPB frame via an ethernet interface
  126. */
  127. static int lapbeth_xmit(struct sk_buff *skb, struct net_device *dev)
  128. {
  129. int err = -ENODEV;
  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. }
  137. switch (skb->data[0]) {
  138. case 0x00:
  139. err = 0;
  140. break;
  141. case 0x01:
  142. if ((err = lapb_connect_request(dev)) != LAPB_OK)
  143. printk(KERN_ERR "lapbeth: lapb_connect_request "
  144. "error: %d\n", err);
  145. goto drop_ok;
  146. case 0x02:
  147. if ((err = lapb_disconnect_request(dev)) != LAPB_OK)
  148. printk(KERN_ERR "lapbeth: lapb_disconnect_request "
  149. "err: %d\n", err);
  150. /* Fall thru */
  151. default:
  152. goto drop_ok;
  153. }
  154. skb_pull(skb, 1);
  155. if ((err = lapb_data_request(dev, skb)) != LAPB_OK) {
  156. printk(KERN_ERR "lapbeth: lapb_data_request error - %d\n", err);
  157. err = -ENOMEM;
  158. goto drop;
  159. }
  160. err = 0;
  161. out:
  162. return err;
  163. drop_ok:
  164. err = 0;
  165. drop:
  166. kfree_skb(skb);
  167. goto out;
  168. }
  169. static void lapbeth_data_transmit(struct net_device *ndev, struct sk_buff *skb)
  170. {
  171. struct lapbethdev *lapbeth = netdev_priv(ndev);
  172. unsigned char *ptr;
  173. struct net_device *dev;
  174. int size = skb->len;
  175. skb->protocol = htons(ETH_P_X25);
  176. ptr = skb_push(skb, 2);
  177. *ptr++ = size % 256;
  178. *ptr++ = size / 256;
  179. lapbeth->stats.tx_packets++;
  180. lapbeth->stats.tx_bytes += size;
  181. skb->dev = dev = lapbeth->ethdev;
  182. dev->hard_header(skb, dev, ETH_P_DEC, bcast_addr, NULL, 0);
  183. dev_queue_xmit(skb);
  184. }
  185. static void lapbeth_connected(struct net_device *dev, int reason)
  186. {
  187. unsigned char *ptr;
  188. struct sk_buff *skb = dev_alloc_skb(1);
  189. if (!skb) {
  190. printk(KERN_ERR "lapbeth: out of memory\n");
  191. return;
  192. }
  193. ptr = skb_put(skb, 1);
  194. *ptr = 0x01;
  195. skb->protocol = x25_type_trans(skb, dev);
  196. skb->dev->last_rx = jiffies;
  197. netif_rx(skb);
  198. }
  199. static void lapbeth_disconnected(struct net_device *dev, int reason)
  200. {
  201. unsigned char *ptr;
  202. struct sk_buff *skb = dev_alloc_skb(1);
  203. if (!skb) {
  204. printk(KERN_ERR "lapbeth: out of memory\n");
  205. return;
  206. }
  207. ptr = skb_put(skb, 1);
  208. *ptr = 0x02;
  209. skb->protocol = x25_type_trans(skb, dev);
  210. skb->dev->last_rx = jiffies;
  211. netif_rx(skb);
  212. }
  213. /*
  214. * Statistics
  215. */
  216. static struct net_device_stats *lapbeth_get_stats(struct net_device *dev)
  217. {
  218. struct lapbethdev *lapbeth = netdev_priv(dev);
  219. return &lapbeth->stats;
  220. }
  221. /*
  222. * Set AX.25 callsign
  223. */
  224. static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
  225. {
  226. struct sockaddr *sa = addr;
  227. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  228. return 0;
  229. }
  230. static struct lapb_register_struct lapbeth_callbacks = {
  231. .connect_confirmation = lapbeth_connected,
  232. .connect_indication = lapbeth_connected,
  233. .disconnect_confirmation = lapbeth_disconnected,
  234. .disconnect_indication = lapbeth_disconnected,
  235. .data_indication = lapbeth_data_indication,
  236. .data_transmit = lapbeth_data_transmit,
  237. };
  238. /*
  239. * open/close a device
  240. */
  241. static int lapbeth_open(struct net_device *dev)
  242. {
  243. int err;
  244. if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
  245. printk(KERN_ERR "lapbeth: lapb_register error - %d\n", err);
  246. return -ENODEV;
  247. }
  248. netif_start_queue(dev);
  249. return 0;
  250. }
  251. static int lapbeth_close(struct net_device *dev)
  252. {
  253. int err;
  254. netif_stop_queue(dev);
  255. if ((err = lapb_unregister(dev)) != LAPB_OK)
  256. printk(KERN_ERR "lapbeth: lapb_unregister error - %d\n", err);
  257. return 0;
  258. }
  259. /* ------------------------------------------------------------------------ */
  260. static void lapbeth_setup(struct net_device *dev)
  261. {
  262. dev->hard_start_xmit = lapbeth_xmit;
  263. dev->open = lapbeth_open;
  264. dev->stop = lapbeth_close;
  265. dev->destructor = free_netdev;
  266. dev->set_mac_address = lapbeth_set_mac_address;
  267. dev->get_stats = lapbeth_get_stats;
  268. dev->type = ARPHRD_X25;
  269. dev->hard_header_len = 3;
  270. dev->mtu = 1000;
  271. dev->addr_len = 0;
  272. SET_MODULE_OWNER(dev);
  273. }
  274. /*
  275. * Setup a new device.
  276. */
  277. static int lapbeth_new_device(struct net_device *dev)
  278. {
  279. struct net_device *ndev;
  280. struct lapbethdev *lapbeth;
  281. int rc = -ENOMEM;
  282. ASSERT_RTNL();
  283. ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d",
  284. lapbeth_setup);
  285. if (!ndev)
  286. goto out;
  287. lapbeth = netdev_priv(ndev);
  288. lapbeth->axdev = ndev;
  289. dev_hold(dev);
  290. lapbeth->ethdev = dev;
  291. rc = dev_alloc_name(ndev, ndev->name);
  292. if (rc < 0)
  293. goto fail;
  294. rc = -EIO;
  295. if (register_netdevice(ndev))
  296. goto fail;
  297. list_add_rcu(&lapbeth->node, &lapbeth_devices);
  298. rc = 0;
  299. out:
  300. return rc;
  301. fail:
  302. dev_put(dev);
  303. free_netdev(ndev);
  304. kfree(lapbeth);
  305. goto out;
  306. }
  307. /*
  308. * Free a lapb network device.
  309. */
  310. static void lapbeth_free_device(struct lapbethdev *lapbeth)
  311. {
  312. dev_put(lapbeth->ethdev);
  313. list_del_rcu(&lapbeth->node);
  314. unregister_netdevice(lapbeth->axdev);
  315. }
  316. /*
  317. * Handle device status changes.
  318. *
  319. * Called from notifier with RTNL held.
  320. */
  321. static int lapbeth_device_event(struct notifier_block *this,
  322. unsigned long event, void *ptr)
  323. {
  324. struct lapbethdev *lapbeth;
  325. struct net_device *dev = ptr;
  326. if (!dev_is_ethdev(dev))
  327. return NOTIFY_DONE;
  328. switch (event) {
  329. case NETDEV_UP:
  330. /* New ethernet device -> new LAPB interface */
  331. if (lapbeth_get_x25_dev(dev) == NULL)
  332. lapbeth_new_device(dev);
  333. break;
  334. case NETDEV_DOWN:
  335. /* ethernet device closed -> close LAPB interface */
  336. lapbeth = lapbeth_get_x25_dev(dev);
  337. if (lapbeth)
  338. dev_close(lapbeth->axdev);
  339. break;
  340. case NETDEV_UNREGISTER:
  341. /* ethernet device disappears -> remove LAPB interface */
  342. lapbeth = lapbeth_get_x25_dev(dev);
  343. if (lapbeth)
  344. lapbeth_free_device(lapbeth);
  345. break;
  346. }
  347. return NOTIFY_DONE;
  348. }
  349. /* ------------------------------------------------------------------------ */
  350. static struct packet_type lapbeth_packet_type = {
  351. .type = __constant_htons(ETH_P_DEC),
  352. .func = lapbeth_rcv,
  353. };
  354. static struct notifier_block lapbeth_dev_notifier = {
  355. .notifier_call = lapbeth_device_event,
  356. };
  357. static char banner[] __initdata = KERN_INFO "LAPB Ethernet driver version 0.02\n";
  358. static int __init lapbeth_init_driver(void)
  359. {
  360. dev_add_pack(&lapbeth_packet_type);
  361. register_netdevice_notifier(&lapbeth_dev_notifier);
  362. printk(banner);
  363. return 0;
  364. }
  365. module_init(lapbeth_init_driver);
  366. static void __exit lapbeth_cleanup_driver(void)
  367. {
  368. struct lapbethdev *lapbeth;
  369. struct list_head *entry, *tmp;
  370. dev_remove_pack(&lapbeth_packet_type);
  371. unregister_netdevice_notifier(&lapbeth_dev_notifier);
  372. rtnl_lock();
  373. list_for_each_safe(entry, tmp, &lapbeth_devices) {
  374. lapbeth = list_entry(entry, struct lapbethdev, node);
  375. unregister_netdevice(lapbeth->axdev);
  376. }
  377. rtnl_unlock();
  378. }
  379. module_exit(lapbeth_cleanup_driver);
  380. MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
  381. MODULE_DESCRIPTION("The unofficial LAPB over Ethernet driver");
  382. MODULE_LICENSE("GPL");