x25_forward.c 3.9 KB

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