lapbether.c 12 KB

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