rose_loopback.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/socket.h>
  11. #include <linux/timer.h>
  12. #include <net/ax25.h>
  13. #include <linux/skbuff.h>
  14. #include <net/rose.h>
  15. #include <linux/init.h>
  16. static struct sk_buff_head loopback_queue;
  17. static struct timer_list loopback_timer;
  18. static void rose_set_loopback_timer(void);
  19. void rose_loopback_init(void)
  20. {
  21. skb_queue_head_init(&loopback_queue);
  22. init_timer(&loopback_timer);
  23. }
  24. static int rose_loopback_running(void)
  25. {
  26. return timer_pending(&loopback_timer);
  27. }
  28. int rose_loopback_queue(struct sk_buff *skb, struct rose_neigh *neigh)
  29. {
  30. struct sk_buff *skbn;
  31. skbn = skb_clone(skb, GFP_ATOMIC);
  32. kfree_skb(skb);
  33. if (skbn != NULL) {
  34. skb_queue_tail(&loopback_queue, skbn);
  35. if (!rose_loopback_running())
  36. rose_set_loopback_timer();
  37. }
  38. return 1;
  39. }
  40. static void rose_loopback_timer(unsigned long);
  41. static void rose_set_loopback_timer(void)
  42. {
  43. del_timer(&loopback_timer);
  44. loopback_timer.data = 0;
  45. loopback_timer.function = &rose_loopback_timer;
  46. loopback_timer.expires = jiffies + 10;
  47. add_timer(&loopback_timer);
  48. }
  49. static void rose_loopback_timer(unsigned long param)
  50. {
  51. struct sk_buff *skb;
  52. struct net_device *dev;
  53. rose_address *dest;
  54. struct sock *sk;
  55. unsigned short frametype;
  56. unsigned int lci_i, lci_o;
  57. while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
  58. lci_i = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
  59. frametype = skb->data[2];
  60. dest = (rose_address *)(skb->data + 4);
  61. lci_o = 0xFFF - lci_i;
  62. skb_reset_transport_header(skb);
  63. sk = rose_find_socket(lci_o, rose_loopback_neigh);
  64. if (sk) {
  65. if (rose_process_rx_frame(sk, skb) == 0)
  66. kfree_skb(skb);
  67. continue;
  68. }
  69. if (frametype == ROSE_CALL_REQUEST) {
  70. if ((dev = rose_dev_get(dest)) != NULL) {
  71. if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
  72. kfree_skb(skb);
  73. } else {
  74. kfree_skb(skb);
  75. }
  76. } else {
  77. kfree_skb(skb);
  78. }
  79. }
  80. }
  81. void __exit rose_loopback_clear(void)
  82. {
  83. struct sk_buff *skb;
  84. del_timer(&loopback_timer);
  85. while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
  86. skb->sk = NULL;
  87. kfree_skb(skb);
  88. }
  89. }