x25_forward.c 3.8 KB

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