Thomas Gleixner | ee5d8f4 | 2019-05-20 19:08:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 2 | /* |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 3 | * History |
| 4 | * 03-01-2007 Added forwarding for x.25 Andrew Hendry |
| 5 | */ |
wangweidong | b73e9e3 | 2013-12-06 19:24:33 +0800 | [diff] [blame] | 6 | |
| 7 | #define pr_fmt(fmt) "X25: " fmt |
| 8 | |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 9 | #include <linux/if_arp.h> |
| 10 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 12 | #include <net/x25.h> |
| 13 | |
Denis Cheng | 0e3cf7e | 2007-12-07 00:50:43 -0800 | [diff] [blame] | 14 | LIST_HEAD(x25_forward_list); |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 15 | DEFINE_RWLOCK(x25_forward_list_lock); |
| 16 | |
| 17 | int x25_forward_call(struct x25_address *dest_addr, struct x25_neigh *from, |
| 18 | struct sk_buff *skb, int lci) |
| 19 | { |
| 20 | struct x25_route *rt; |
| 21 | struct x25_neigh *neigh_new = NULL; |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 22 | struct x25_forward *x25_frwd, *new_frwd; |
| 23 | struct sk_buff *skbn; |
| 24 | short same_lci = 0; |
| 25 | int rc = 0; |
| 26 | |
Adrian Bunk | e4ce837 | 2007-03-19 17:04:56 -0700 | [diff] [blame] | 27 | if ((rt = x25_get_route(dest_addr)) == NULL) |
| 28 | goto out_no_route; |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 29 | |
Adrian Bunk | e4ce837 | 2007-03-19 17:04:56 -0700 | [diff] [blame] | 30 | if ((neigh_new = x25_get_neigh(rt->dev)) == NULL) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 31 | /* This shouldn't happen, if it occurs somehow |
Adrian Bunk | e4ce837 | 2007-03-19 17:04:56 -0700 | [diff] [blame] | 32 | * do something sensible |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 33 | */ |
Adrian Bunk | e4ce837 | 2007-03-19 17:04:56 -0700 | [diff] [blame] | 34 | goto out_put_route; |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Adrian Bunk | e4ce837 | 2007-03-19 17:04:56 -0700 | [diff] [blame] | 37 | /* Avoid a loop. This is the normal exit path for a |
| 38 | * system with only one x.25 iface and default route |
| 39 | */ |
| 40 | if (rt->dev == from->dev) { |
| 41 | goto out_put_nb; |
| 42 | } |
| 43 | |
| 44 | /* Remote end sending a call request on an already |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 45 | * established LCI? It shouldn't happen, just in case.. |
Adrian Bunk | e4ce837 | 2007-03-19 17:04:56 -0700 | [diff] [blame] | 46 | */ |
| 47 | read_lock_bh(&x25_forward_list_lock); |
Wang Hai | 96bffe7 | 2021-06-08 13:30:07 +0000 | [diff] [blame] | 48 | list_for_each_entry(x25_frwd, &x25_forward_list, node) { |
Adrian Bunk | e4ce837 | 2007-03-19 17:04:56 -0700 | [diff] [blame] | 49 | if (x25_frwd->lci == lci) { |
wangweidong | b73e9e3 | 2013-12-06 19:24:33 +0800 | [diff] [blame] | 50 | pr_warn("call request for lci which is already registered!, transmitting but not registering new pair\n"); |
Adrian Bunk | e4ce837 | 2007-03-19 17:04:56 -0700 | [diff] [blame] | 51 | same_lci = 1; |
| 52 | } |
| 53 | } |
| 54 | read_unlock_bh(&x25_forward_list_lock); |
| 55 | |
| 56 | /* Save the forwarding details for future traffic */ |
| 57 | if (!same_lci){ |
| 58 | if ((new_frwd = kmalloc(sizeof(struct x25_forward), |
| 59 | GFP_ATOMIC)) == NULL){ |
| 60 | rc = -ENOMEM; |
| 61 | goto out_put_nb; |
| 62 | } |
| 63 | new_frwd->lci = lci; |
| 64 | new_frwd->dev1 = rt->dev; |
| 65 | new_frwd->dev2 = from->dev; |
| 66 | write_lock_bh(&x25_forward_list_lock); |
| 67 | list_add(&new_frwd->node, &x25_forward_list); |
| 68 | write_unlock_bh(&x25_forward_list_lock); |
| 69 | } |
| 70 | |
| 71 | /* Forward the call request */ |
| 72 | if ( (skbn = skb_clone(skb, GFP_ATOMIC)) == NULL){ |
| 73 | goto out_put_nb; |
| 74 | } |
| 75 | x25_transmit_link(skbn, neigh_new); |
| 76 | rc = 1; |
| 77 | |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 78 | |
| 79 | out_put_nb: |
| 80 | x25_neigh_put(neigh_new); |
| 81 | |
| 82 | out_put_route: |
| 83 | x25_route_put(rt); |
Adrian Bunk | e4ce837 | 2007-03-19 17:04:56 -0700 | [diff] [blame] | 84 | |
| 85 | out_no_route: |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 86 | return rc; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | int x25_forward_data(int lci, struct x25_neigh *from, struct sk_buff *skb) { |
| 91 | |
| 92 | struct x25_forward *frwd; |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 93 | struct net_device *peer = NULL; |
| 94 | struct x25_neigh *nb; |
| 95 | struct sk_buff *skbn; |
| 96 | int rc = 0; |
| 97 | |
| 98 | read_lock_bh(&x25_forward_list_lock); |
Wang Hai | 96bffe7 | 2021-06-08 13:30:07 +0000 | [diff] [blame] | 99 | list_for_each_entry(frwd, &x25_forward_list, node) { |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 100 | if (frwd->lci == lci) { |
| 101 | /* The call is established, either side can send */ |
| 102 | if (from->dev == frwd->dev1) { |
| 103 | peer = frwd->dev2; |
| 104 | } else { |
| 105 | peer = frwd->dev1; |
| 106 | } |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | read_unlock_bh(&x25_forward_list_lock); |
| 111 | |
| 112 | if ( (nb = x25_get_neigh(peer)) == NULL) |
| 113 | goto out; |
| 114 | |
| 115 | if ( (skbn = pskb_copy(skb, GFP_ATOMIC)) == NULL){ |
Julia Lawall | 76975f8 | 2008-01-01 19:30:30 -0800 | [diff] [blame] | 116 | goto output; |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 117 | |
| 118 | } |
| 119 | x25_transmit_link(skbn, nb); |
| 120 | |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 121 | rc = 1; |
Julia Lawall | 76975f8 | 2008-01-01 19:30:30 -0800 | [diff] [blame] | 122 | output: |
| 123 | x25_neigh_put(nb); |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 124 | out: |
| 125 | return rc; |
| 126 | } |
| 127 | |
| 128 | void x25_clear_forward_by_lci(unsigned int lci) |
| 129 | { |
chenqiwu | 1e5946f | 2020-02-14 17:18:26 +0800 | [diff] [blame] | 130 | struct x25_forward *fwd, *tmp; |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 131 | |
| 132 | write_lock_bh(&x25_forward_list_lock); |
| 133 | |
chenqiwu | 1e5946f | 2020-02-14 17:18:26 +0800 | [diff] [blame] | 134 | list_for_each_entry_safe(fwd, tmp, &x25_forward_list, node) { |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 135 | if (fwd->lci == lci) { |
| 136 | list_del(&fwd->node); |
| 137 | kfree(fwd); |
| 138 | } |
| 139 | } |
| 140 | write_unlock_bh(&x25_forward_list_lock); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void x25_clear_forward_by_dev(struct net_device *dev) |
| 145 | { |
chenqiwu | 1e5946f | 2020-02-14 17:18:26 +0800 | [diff] [blame] | 146 | struct x25_forward *fwd, *tmp; |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 147 | |
| 148 | write_lock_bh(&x25_forward_list_lock); |
| 149 | |
chenqiwu | 1e5946f | 2020-02-14 17:18:26 +0800 | [diff] [blame] | 150 | list_for_each_entry_safe(fwd, tmp, &x25_forward_list, node) { |
Andrew Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 151 | if ((fwd->dev1 == dev) || (fwd->dev2 == dev)){ |
| 152 | list_del(&fwd->node); |
| 153 | kfree(fwd); |
| 154 | } |
| 155 | } |
| 156 | write_unlock_bh(&x25_forward_list_lock); |
| 157 | } |