psnap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SNAP data link layer. Derived from 802.2
  4. *
  5. * Alan Cox <[email protected]>,
  6. * from the 802.2 layer by Greg Page.
  7. * Merged in additions from Greg Page's psnap.c.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/slab.h>
  13. #include <net/datalink.h>
  14. #include <net/llc.h>
  15. #include <net/psnap.h>
  16. #include <linux/mm.h>
  17. #include <linux/in.h>
  18. #include <linux/init.h>
  19. #include <linux/rculist.h>
  20. static LIST_HEAD(snap_list);
  21. static DEFINE_SPINLOCK(snap_lock);
  22. static struct llc_sap *snap_sap;
  23. /*
  24. * Find a snap client by matching the 5 bytes.
  25. */
  26. static struct datalink_proto *find_snap_client(const unsigned char *desc)
  27. {
  28. struct datalink_proto *proto = NULL, *p;
  29. list_for_each_entry_rcu(p, &snap_list, node, lockdep_is_held(&snap_lock)) {
  30. if (!memcmp(p->type, desc, 5)) {
  31. proto = p;
  32. break;
  33. }
  34. }
  35. return proto;
  36. }
  37. /*
  38. * A SNAP packet has arrived
  39. */
  40. static int snap_rcv(struct sk_buff *skb, struct net_device *dev,
  41. struct packet_type *pt, struct net_device *orig_dev)
  42. {
  43. int rc = 1;
  44. struct datalink_proto *proto;
  45. static struct packet_type snap_packet_type = {
  46. .type = cpu_to_be16(ETH_P_SNAP),
  47. };
  48. if (unlikely(!pskb_may_pull(skb, 5)))
  49. goto drop;
  50. rcu_read_lock();
  51. proto = find_snap_client(skb_transport_header(skb));
  52. if (proto) {
  53. /* Pass the frame on. */
  54. skb->transport_header += 5;
  55. skb_pull_rcsum(skb, 5);
  56. rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev);
  57. }
  58. rcu_read_unlock();
  59. if (unlikely(!proto))
  60. goto drop;
  61. out:
  62. return rc;
  63. drop:
  64. kfree_skb(skb);
  65. goto out;
  66. }
  67. /*
  68. * Put a SNAP header on a frame and pass to 802.2
  69. */
  70. static int snap_request(struct datalink_proto *dl,
  71. struct sk_buff *skb, const u8 *dest)
  72. {
  73. memcpy(skb_push(skb, 5), dl->type, 5);
  74. llc_build_and_send_ui_pkt(snap_sap, skb, dest, snap_sap->laddr.lsap);
  75. return 0;
  76. }
  77. /*
  78. * Set up the SNAP layer
  79. */
  80. EXPORT_SYMBOL(register_snap_client);
  81. EXPORT_SYMBOL(unregister_snap_client);
  82. static const char snap_err_msg[] __initconst =
  83. KERN_CRIT "SNAP - unable to register with 802.2\n";
  84. static int __init snap_init(void)
  85. {
  86. snap_sap = llc_sap_open(0xAA, snap_rcv);
  87. if (!snap_sap) {
  88. printk(snap_err_msg);
  89. return -EBUSY;
  90. }
  91. return 0;
  92. }
  93. module_init(snap_init);
  94. static void __exit snap_exit(void)
  95. {
  96. llc_sap_put(snap_sap);
  97. }
  98. module_exit(snap_exit);
  99. /*
  100. * Register SNAP clients. We don't yet use this for IP.
  101. */
  102. struct datalink_proto *register_snap_client(const unsigned char *desc,
  103. int (*rcvfunc)(struct sk_buff *,
  104. struct net_device *,
  105. struct packet_type *,
  106. struct net_device *))
  107. {
  108. struct datalink_proto *proto = NULL;
  109. spin_lock_bh(&snap_lock);
  110. if (find_snap_client(desc))
  111. goto out;
  112. proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
  113. if (proto) {
  114. memcpy(proto->type, desc, 5);
  115. proto->rcvfunc = rcvfunc;
  116. proto->header_length = 5 + 3; /* snap + 802.2 */
  117. proto->request = snap_request;
  118. list_add_rcu(&proto->node, &snap_list);
  119. }
  120. out:
  121. spin_unlock_bh(&snap_lock);
  122. return proto;
  123. }
  124. /*
  125. * Unregister SNAP clients. Protocols no longer want to play with us ...
  126. */
  127. void unregister_snap_client(struct datalink_proto *proto)
  128. {
  129. spin_lock_bh(&snap_lock);
  130. list_del_rcu(&proto->node);
  131. spin_unlock_bh(&snap_lock);
  132. synchronize_net();
  133. kfree(proto);
  134. }
  135. MODULE_LICENSE("GPL");