psnap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 list_head *entry;
  32. struct datalink_proto *proto = NULL, *p;
  33. list_for_each_rcu(entry, &snap_list) {
  34. p = list_entry(entry, struct datalink_proto, node);
  35. if (!memcmp(p->type, desc, 5)) {
  36. proto = p;
  37. break;
  38. }
  39. }
  40. return proto;
  41. }
  42. /*
  43. * A SNAP packet has arrived
  44. */
  45. static int snap_rcv(struct sk_buff *skb, struct net_device *dev,
  46. struct packet_type *pt, struct net_device *orig_dev)
  47. {
  48. int rc = 1;
  49. struct datalink_proto *proto;
  50. static struct packet_type snap_packet_type = {
  51. .type = __constant_htons(ETH_P_SNAP),
  52. };
  53. if (unlikely(!pskb_may_pull(skb, 5)))
  54. goto drop;
  55. rcu_read_lock();
  56. proto = find_snap_client(skb_transport_header(skb));
  57. if (proto) {
  58. /* Pass the frame on. */
  59. skb->transport_header += 5;
  60. skb_pull_rcsum(skb, 5);
  61. rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev);
  62. }
  63. rcu_read_unlock();
  64. if (unlikely(!proto))
  65. goto drop;
  66. out:
  67. return rc;
  68. drop:
  69. kfree_skb(skb);
  70. goto out;
  71. }
  72. /*
  73. * Put a SNAP header on a frame and pass to 802.2
  74. */
  75. static int snap_request(struct datalink_proto *dl,
  76. struct sk_buff *skb, u8 *dest)
  77. {
  78. memcpy(skb_push(skb, 5), dl->type, 5);
  79. llc_build_and_send_ui_pkt(snap_sap, skb, dest, snap_sap->laddr.lsap);
  80. return 0;
  81. }
  82. /*
  83. * Set up the SNAP layer
  84. */
  85. EXPORT_SYMBOL(register_snap_client);
  86. EXPORT_SYMBOL(unregister_snap_client);
  87. static char snap_err_msg[] __initdata =
  88. KERN_CRIT "SNAP - unable to register with 802.2\n";
  89. static int __init snap_init(void)
  90. {
  91. snap_sap = llc_sap_open(0xAA, snap_rcv);
  92. if (!snap_sap)
  93. printk(snap_err_msg);
  94. return 0;
  95. }
  96. module_init(snap_init);
  97. static void __exit snap_exit(void)
  98. {
  99. llc_sap_put(snap_sap);
  100. }
  101. module_exit(snap_exit);
  102. /*
  103. * Register SNAP clients. We don't yet use this for IP.
  104. */
  105. struct datalink_proto *register_snap_client(unsigned char *desc,
  106. int (*rcvfunc)(struct sk_buff *,
  107. struct net_device *,
  108. struct packet_type *,
  109. struct net_device *))
  110. {
  111. struct datalink_proto *proto = NULL;
  112. spin_lock_bh(&snap_lock);
  113. if (find_snap_client(desc))
  114. goto out;
  115. proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
  116. if (proto) {
  117. memcpy(proto->type, desc,5);
  118. proto->rcvfunc = rcvfunc;
  119. proto->header_length = 5 + 3; /* snap + 802.2 */
  120. proto->request = snap_request;
  121. list_add_rcu(&proto->node, &snap_list);
  122. }
  123. out:
  124. spin_unlock_bh(&snap_lock);
  125. synchronize_net();
  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");