psnap.c 3.4 KB

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