lapbether.c 12 KB

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