rose_loopback.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. lci_i = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
  60. frametype = skb->data[2];
  61. dest = (rose_address *)(skb->data + 4);
  62. lci_o = ROSE_DEFAULT_MAXVC + 1 - lci_i;
  63. skb_reset_transport_header(skb);
  64. sk = rose_find_socket(lci_o, rose_loopback_neigh);
  65. if (sk) {
  66. if (rose_process_rx_frame(sk, skb) == 0)
  67. kfree_skb(skb);
  68. continue;
  69. }
  70. if (frametype == ROSE_CALL_REQUEST) {
  71. if ((dev = rose_dev_get(dest)) != NULL) {
  72. if (rose_rx_call_request(skb, dev, rose_loopback_neigh, lci_o) == 0)
  73. kfree_skb(skb);
  74. } else {
  75. kfree_skb(skb);
  76. }
  77. } else {
  78. kfree_skb(skb);
  79. }
  80. }
  81. }
  82. void __exit rose_loopback_clear(void)
  83. {
  84. struct sk_buff *skb;
  85. del_timer(&loopback_timer);
  86. while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
  87. skb->sk = NULL;
  88. kfree_skb(skb);
  89. }
  90. }