x25_forward.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * This module:
  3. * This module is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version
  6. * 2 of the License, or (at your option) any later version.
  7. *
  8. * History
  9. * 03-01-2007 Added forwarding for x.25 Andrew Hendry
  10. */
  11. #include <linux/if_arp.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <net/x25.h>
  15. LIST_HEAD(x25_forward_list);
  16. DEFINE_RWLOCK(x25_forward_list_lock);
  17. int x25_forward_call(struct x25_address *dest_addr, struct x25_neigh *from,
  18. struct sk_buff *skb, int lci)
  19. {
  20. struct x25_route *rt;
  21. struct x25_neigh *neigh_new = NULL;
  22. struct list_head *entry;
  23. struct x25_forward *x25_frwd, *new_frwd;
  24. struct sk_buff *skbn;
  25. short same_lci = 0;
  26. int rc = 0;
  27. if ((rt = x25_get_route(dest_addr)) == NULL)
  28. goto out_no_route;
  29. if ((neigh_new = x25_get_neigh(rt->dev)) == NULL) {
  30. /* This shouldn't happen, if it occurs somehow
  31. * do something sensible
  32. */
  33. goto out_put_route;
  34. }
  35. /* Avoid a loop. This is the normal exit path for a
  36. * system with only one x.25 iface and default route
  37. */
  38. if (rt->dev == from->dev) {
  39. goto out_put_nb;
  40. }
  41. /* Remote end sending a call request on an already
  42. * established LCI? It shouldn't happen, just in case..
  43. */
  44. read_lock_bh(&x25_forward_list_lock);
  45. list_for_each(entry, &x25_forward_list) {
  46. x25_frwd = list_entry(entry, struct x25_forward, node);
  47. if (x25_frwd->lci == lci) {
  48. printk(KERN_WARNING "X.25: call request for lci which is already registered!, transmitting but not registering new pair\n");
  49. same_lci = 1;
  50. }
  51. }
  52. read_unlock_bh(&x25_forward_list_lock);
  53. /* Save the forwarding details for future traffic */
  54. if (!same_lci){
  55. if ((new_frwd = kmalloc(sizeof(struct x25_forward),
  56. GFP_ATOMIC)) == NULL){
  57. rc = -ENOMEM;
  58. goto out_put_nb;
  59. }
  60. new_frwd->lci = lci;
  61. new_frwd->dev1 = rt->dev;
  62. new_frwd->dev2 = from->dev;
  63. write_lock_bh(&x25_forward_list_lock);
  64. list_add(&new_frwd->node, &x25_forward_list);
  65. write_unlock_bh(&x25_forward_list_lock);
  66. }
  67. /* Forward the call request */
  68. if ( (skbn = skb_clone(skb, GFP_ATOMIC)) == NULL){
  69. goto out_put_nb;
  70. }
  71. x25_transmit_link(skbn, neigh_new);
  72. rc = 1;
  73. out_put_nb:
  74. x25_neigh_put(neigh_new);
  75. out_put_route:
  76. x25_route_put(rt);
  77. out_no_route:
  78. return rc;
  79. }
  80. int x25_forward_data(int lci, struct x25_neigh *from, struct sk_buff *skb) {
  81. struct x25_forward *frwd;
  82. struct list_head *entry;
  83. struct net_device *peer = NULL;
  84. struct x25_neigh *nb;
  85. struct sk_buff *skbn;
  86. int rc = 0;
  87. read_lock_bh(&x25_forward_list_lock);
  88. list_for_each(entry, &x25_forward_list) {
  89. frwd = list_entry(entry, struct x25_forward, node);
  90. if (frwd->lci == lci) {
  91. /* The call is established, either side can send */
  92. if (from->dev == frwd->dev1) {
  93. peer = frwd->dev2;
  94. } else {
  95. peer = frwd->dev1;
  96. }
  97. break;
  98. }
  99. }
  100. read_unlock_bh(&x25_forward_list_lock);
  101. if ( (nb = x25_get_neigh(peer)) == NULL)
  102. goto out;
  103. if ( (skbn = pskb_copy(skb, GFP_ATOMIC)) == NULL){
  104. goto output;
  105. }
  106. x25_transmit_link(skbn, nb);
  107. rc = 1;
  108. output:
  109. x25_neigh_put(nb);
  110. out:
  111. return rc;
  112. }
  113. void x25_clear_forward_by_lci(unsigned int lci)
  114. {
  115. struct x25_forward *fwd;
  116. struct list_head *entry, *tmp;
  117. write_lock_bh(&x25_forward_list_lock);
  118. list_for_each_safe(entry, tmp, &x25_forward_list) {
  119. fwd = list_entry(entry, struct x25_forward, node);
  120. if (fwd->lci == lci) {
  121. list_del(&fwd->node);
  122. kfree(fwd);
  123. }
  124. }
  125. write_unlock_bh(&x25_forward_list_lock);
  126. }
  127. void x25_clear_forward_by_dev(struct net_device *dev)
  128. {
  129. struct x25_forward *fwd;
  130. struct list_head *entry, *tmp;
  131. write_lock_bh(&x25_forward_list_lock);
  132. list_for_each_safe(entry, tmp, &x25_forward_list) {
  133. fwd = list_entry(entry, struct x25_forward, node);
  134. if ((fwd->dev1 == dev) || (fwd->dev2 == dev)){
  135. list_del(&fwd->node);
  136. kfree(fwd);
  137. }
  138. }
  139. write_unlock_bh(&x25_forward_list_lock);
  140. }