psnap.c 3.5 KB

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