psnap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. static LIST_HEAD(snap_list);
  23. static DEFINE_SPINLOCK(snap_lock);
  24. static struct llc_sap *snap_sap;
  25. /*
  26. * Find a snap client by matching the 5 bytes.
  27. */
  28. static struct datalink_proto *find_snap_client(unsigned char *desc)
  29. {
  30. struct list_head *entry;
  31. struct datalink_proto *proto = NULL, *p;
  32. list_for_each_rcu(entry, &snap_list) {
  33. p = list_entry(entry, struct datalink_proto, 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 = __constant_htons(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 char snap_err_msg[] __initdata =
  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 0;
  94. }
  95. module_init(snap_init);
  96. static void __exit snap_exit(void)
  97. {
  98. llc_sap_put(snap_sap);
  99. }
  100. module_exit(snap_exit);
  101. /*
  102. * Register SNAP clients. We don't yet use this for IP.
  103. */
  104. struct datalink_proto *register_snap_client(unsigned char *desc,
  105. int (*rcvfunc)(struct sk_buff *,
  106. struct net_device *,
  107. struct packet_type *,
  108. struct net_device *))
  109. {
  110. struct datalink_proto *proto = NULL;
  111. spin_lock_bh(&snap_lock);
  112. if (find_snap_client(desc))
  113. goto out;
  114. proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
  115. if (proto) {
  116. memcpy(proto->type, desc,5);
  117. proto->rcvfunc = rcvfunc;
  118. proto->header_length = 5 + 3; /* snap + 802.2 */
  119. proto->request = snap_request;
  120. list_add_rcu(&proto->node, &snap_list);
  121. }
  122. out:
  123. spin_unlock_bh(&snap_lock);
  124. synchronize_net();
  125. return proto;
  126. }
  127. /*
  128. * Unregister SNAP clients. Protocols no longer want to play with us ...
  129. */
  130. void unregister_snap_client(struct datalink_proto *proto)
  131. {
  132. spin_lock_bh(&snap_lock);
  133. list_del_rcu(&proto->node);
  134. spin_unlock_bh(&snap_lock);
  135. synchronize_net();
  136. kfree(proto);
  137. }
  138. MODULE_LICENSE("GPL");