rose_loopback.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  8. */
  9. #include <linux/types.h>
  10. #include <linux/slab.h>
  11. #include <linux/socket.h>
  12. #include <linux/timer.h>
  13. #include <net/ax25.h>
  14. #include <linux/skbuff.h>
  15. #include <net/rose.h>
  16. #include <linux/init.h>
  17. static struct sk_buff_head loopback_queue;
  18. #define ROSE_LOOPBACK_LIMIT 1000
  19. static struct timer_list loopback_timer;
  20. static void rose_set_loopback_timer(void);
  21. static void rose_loopback_timer(struct timer_list *unused);
  22. void rose_loopback_init(void)
  23. {
  24. skb_queue_head_init(&loopback_queue);
  25. timer_setup(&loopback_timer, rose_loopback_timer, 0);
  26. }
  27. static int rose_loopback_running(void)
  28. {
  29. return timer_pending(&loopback_timer);
  30. }
  31. int rose_loopback_queue(struct sk_buff *skb, struct rose_neigh *neigh)
  32. {
  33. struct sk_buff *skbn = NULL;
  34. if (skb_queue_len(&loopback_queue) < ROSE_LOOPBACK_LIMIT)
  35. skbn = skb_clone(skb, GFP_ATOMIC);
  36. if (skbn) {
  37. consume_skb(skb);
  38. skb_queue_tail(&loopback_queue, skbn);
  39. if (!rose_loopback_running())
  40. rose_set_loopback_timer();
  41. } else {
  42. kfree_skb(skb);
  43. }
  44. return 1;
  45. }
  46. static void rose_set_loopback_timer(void)
  47. {
  48. mod_timer(&loopback_timer, jiffies + 10);
  49. }
  50. static void rose_loopback_timer(struct timer_list *unused)
  51. {
  52. struct sk_buff *skb;
  53. struct net_device *dev;
  54. rose_address *dest;
  55. struct sock *sk;
  56. unsigned short frametype;
  57. unsigned int lci_i, lci_o;
  58. int count;
  59. for (count = 0; count < ROSE_LOOPBACK_LIMIT; count++) {
  60. skb = skb_dequeue(&loopback_queue);
  61. if (!skb)
  62. return;
  63. if (skb->len < ROSE_MIN_LEN) {
  64. kfree_skb(skb);
  65. continue;
  66. }
  67. lci_i = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
  68. frametype = skb->data[2];
  69. if (frametype == ROSE_CALL_REQUEST &&
  70. (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
  71. skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
  72. ROSE_CALL_REQ_ADDR_LEN_VAL)) {
  73. kfree_skb(skb);
  74. continue;
  75. }
  76. dest = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
  77. lci_o = ROSE_DEFAULT_MAXVC + 1 - lci_i;
  78. skb_reset_transport_header(skb);
  79. sk = rose_find_socket(lci_o, rose_loopback_neigh);
  80. if (sk) {
  81. if (rose_process_rx_frame(sk, skb) == 0)
  82. kfree_skb(skb);
  83. continue;
  84. }
  85. if (frametype == ROSE_CALL_REQUEST) {
  86. if (!rose_loopback_neigh->dev) {
  87. kfree_skb(skb);
  88. continue;
  89. }
  90. dev = rose_dev_get(dest);
  91. if (!dev) {
  92. kfree_skb(skb);
  93. continue;
  94. }
  95. if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0) {
  96. dev_put(dev);
  97. kfree_skb(skb);
  98. }
  99. } else {
  100. kfree_skb(skb);
  101. }
  102. }
  103. if (!skb_queue_empty(&loopback_queue))
  104. mod_timer(&loopback_timer, jiffies + 1);
  105. }
  106. void __exit rose_loopback_clear(void)
  107. {
  108. struct sk_buff *skb;
  109. del_timer(&loopback_timer);
  110. while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
  111. skb->sk = NULL;
  112. kfree_skb(skb);
  113. }
  114. }