x25_forward.c 3.9 KB

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