x25_forward.c 3.4 KB

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