psnap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. rcu_read_lock();
  53. proto = find_snap_client(skb->h.raw);
  54. if (proto) {
  55. /* Pass the frame on. */
  56. skb->h.raw += 5;
  57. skb_pull_rcsum(skb, 5);
  58. rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev);
  59. } else {
  60. skb->sk = NULL;
  61. kfree_skb(skb);
  62. rc = 1;
  63. }
  64. rcu_read_unlock();
  65. return rc;
  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, 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 char snap_err_msg[] __initdata =
  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 0;
  90. }
  91. module_init(snap_init);
  92. static void __exit snap_exit(void)
  93. {
  94. llc_sap_put(snap_sap);
  95. }
  96. module_exit(snap_exit);
  97. /*
  98. * Register SNAP clients. We don't yet use this for IP.
  99. */
  100. struct datalink_proto *register_snap_client(unsigned char *desc,
  101. int (*rcvfunc)(struct sk_buff *,
  102. struct net_device *,
  103. struct packet_type *,
  104. struct net_device *))
  105. {
  106. struct datalink_proto *proto = NULL;
  107. spin_lock_bh(&snap_lock);
  108. if (find_snap_client(desc))
  109. goto out;
  110. proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
  111. if (proto) {
  112. memcpy(proto->type, desc,5);
  113. proto->rcvfunc = rcvfunc;
  114. proto->header_length = 5 + 3; /* snap + 802.2 */
  115. proto->request = snap_request;
  116. list_add_rcu(&proto->node, &snap_list);
  117. }
  118. out:
  119. spin_unlock_bh(&snap_lock);
  120. synchronize_net();
  121. return proto;
  122. }
  123. /*
  124. * Unregister SNAP clients. Protocols no longer want to play with us ...
  125. */
  126. void unregister_snap_client(struct datalink_proto *proto)
  127. {
  128. spin_lock_bh(&snap_lock);
  129. list_del_rcu(&proto->node);
  130. spin_unlock_bh(&snap_lock);
  131. synchronize_net();
  132. kfree(proto);
  133. }
  134. MODULE_LICENSE("GPL");