rose_loopback.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. static struct timer_list loopback_timer;
  19. static void rose_set_loopback_timer(void);
  20. void rose_loopback_init(void)
  21. {
  22. skb_queue_head_init(&loopback_queue);
  23. init_timer(&loopback_timer);
  24. }
  25. static int rose_loopback_running(void)
  26. {
  27. return timer_pending(&loopback_timer);
  28. }
  29. int rose_loopback_queue(struct sk_buff *skb, struct rose_neigh *neigh)
  30. {
  31. struct sk_buff *skbn;
  32. skbn = skb_clone(skb, GFP_ATOMIC);
  33. kfree_skb(skb);
  34. if (skbn != NULL) {
  35. skb_queue_tail(&loopback_queue, skbn);
  36. if (!rose_loopback_running())
  37. rose_set_loopback_timer();
  38. }
  39. return 1;
  40. }
  41. static void rose_loopback_timer(unsigned long);
  42. static void rose_set_loopback_timer(void)
  43. {
  44. del_timer(&loopback_timer);
  45. loopback_timer.data = 0;
  46. loopback_timer.function = &rose_loopback_timer;
  47. loopback_timer.expires = jiffies + 10;
  48. add_timer(&loopback_timer);
  49. }
  50. static void rose_loopback_timer(unsigned long param)
  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. while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
  59. if (skb->len < ROSE_MIN_LEN) {
  60. kfree_skb(skb);
  61. continue;
  62. }
  63. lci_i = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
  64. frametype = skb->data[2];
  65. if (frametype == ROSE_CALL_REQUEST &&
  66. (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
  67. skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
  68. ROSE_CALL_REQ_ADDR_LEN_VAL)) {
  69. kfree_skb(skb);
  70. continue;
  71. }
  72. dest = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
  73. lci_o = ROSE_DEFAULT_MAXVC + 1 - lci_i;
  74. skb_reset_transport_header(skb);
  75. sk = rose_find_socket(lci_o, rose_loopback_neigh);
  76. if (sk) {
  77. if (rose_process_rx_frame(sk, skb) == 0)
  78. kfree_skb(skb);
  79. continue;
  80. }
  81. if (frametype == ROSE_CALL_REQUEST) {
  82. if ((dev = rose_dev_get(dest)) != NULL) {
  83. if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
  84. kfree_skb(skb);
  85. } else {
  86. kfree_skb(skb);
  87. }
  88. } else {
  89. kfree_skb(skb);
  90. }
  91. }
  92. }
  93. void __exit rose_loopback_clear(void)
  94. {
  95. struct sk_buff *skb;
  96. del_timer(&loopback_timer);
  97. while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
  98. skb->sk = NULL;
  99. kfree_skb(skb);
  100. }
  101. }