psnap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. u8 *hdr = skb->data;
  57. skb->h.raw += 5;
  58. skb_pull_rcsum(skb, 5);
  59. rc = proto->rcvfunc(skb, dev, &snap_packet_type, orig_dev);
  60. } else {
  61. skb->sk = NULL;
  62. kfree_skb(skb);
  63. rc = 1;
  64. }
  65. rcu_read_unlock();
  66. return rc;
  67. }
  68. /*
  69. * Put a SNAP header on a frame and pass to 802.2
  70. */
  71. static int snap_request(struct datalink_proto *dl,
  72. struct sk_buff *skb, u8 *dest)
  73. {
  74. memcpy(skb_push(skb, 5), dl->type, 5);
  75. llc_build_and_send_ui_pkt(snap_sap, skb, dest, snap_sap->laddr.lsap);
  76. return 0;
  77. }
  78. /*
  79. * Set up the SNAP layer
  80. */
  81. EXPORT_SYMBOL(register_snap_client);
  82. EXPORT_SYMBOL(unregister_snap_client);
  83. static char snap_err_msg[] __initdata =
  84. KERN_CRIT "SNAP - unable to register with 802.2\n";
  85. static int __init snap_init(void)
  86. {
  87. snap_sap = llc_sap_open(0xAA, snap_rcv);
  88. if (!snap_sap)
  89. printk(snap_err_msg);
  90. return 0;
  91. }
  92. module_init(snap_init);
  93. static void __exit snap_exit(void)
  94. {
  95. llc_sap_put(snap_sap);
  96. }
  97. module_exit(snap_exit);
  98. /*
  99. * Register SNAP clients. We don't yet use this for IP.
  100. */
  101. struct datalink_proto *register_snap_client(unsigned char *desc,
  102. int (*rcvfunc)(struct sk_buff *,
  103. struct net_device *,
  104. struct packet_type *,
  105. struct net_device *))
  106. {
  107. struct datalink_proto *proto = NULL;
  108. spin_lock_bh(&snap_lock);
  109. if (find_snap_client(desc))
  110. goto out;
  111. proto = kmalloc(sizeof(*proto), GFP_ATOMIC);
  112. if (proto) {
  113. memcpy(proto->type, desc,5);
  114. proto->rcvfunc = rcvfunc;
  115. proto->header_length = 5 + 3; /* snap + 802.2 */
  116. proto->request = snap_request;
  117. list_add_rcu(&proto->node, &snap_list);
  118. }
  119. out:
  120. spin_unlock_bh(&snap_lock);
  121. synchronize_net();
  122. return proto;
  123. }
  124. /*
  125. * Unregister SNAP clients. Protocols no longer want to play with us ...
  126. */
  127. void unregister_snap_client(struct datalink_proto *proto)
  128. {
  129. spin_lock_bh(&snap_lock);
  130. list_del_rcu(&proto->node);
  131. spin_unlock_bh(&snap_lock);
  132. synchronize_net();
  133. kfree(proto);
  134. }
  135. MODULE_LICENSE("GPL");