x25_forward.c 3.5 KB

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