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
7 #define pr_fmt(fmt) "X25: " fmt
8
9 #include <linux/if_arp.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <net/x25.h>
13
14 LIST_HEAD(x25_forward_list);
15 DEFINE_RWLOCK(x25_forward_list_lock);
16
x25_forward_call(struct x25_address * dest_addr,struct x25_neigh * from,struct sk_buff * skb,int lci)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;
22 struct x25_forward *x25_frwd, *new_frwd;
23 struct sk_buff *skbn;
24 short same_lci = 0;
25 int rc = 0;
26
27 if ((rt = x25_get_route(dest_addr)) == NULL)
28 goto out_no_route;
29
30 if ((neigh_new = x25_get_neigh(rt->dev)) == NULL) {
31 /* This shouldn't happen, if it occurs somehow
32 * do something sensible
33 */
34 goto out_put_route;
35 }
36
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
45 * established LCI? It shouldn't happen, just in case..
46 */
47 read_lock_bh(&x25_forward_list_lock);
48 list_for_each_entry(x25_frwd, &x25_forward_list, node) {
49 if (x25_frwd->lci == lci) {
50 pr_warn("call request for lci which is already registered!, transmitting but not registering new pair\n");
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_obj(struct x25_forward, GFP_ATOMIC)) == NULL){
59 rc = -ENOMEM;
60 goto out_put_nb;
61 }
62 new_frwd->lci = lci;
63 new_frwd->dev1 = rt->dev;
64 new_frwd->dev2 = from->dev;
65 write_lock_bh(&x25_forward_list_lock);
66 list_add(&new_frwd->node, &x25_forward_list);
67 write_unlock_bh(&x25_forward_list_lock);
68 }
69
70 /* Forward the call request */
71 if ( (skbn = skb_clone(skb, GFP_ATOMIC)) == NULL){
72 goto out_put_nb;
73 }
74 x25_transmit_link(skbn, neigh_new);
75 rc = 1;
76
77
78 out_put_nb:
79 x25_neigh_put(neigh_new);
80
81 out_put_route:
82 x25_route_put(rt);
83
84 out_no_route:
85 return rc;
86 }
87
88
x25_forward_data(int lci,struct x25_neigh * from,struct sk_buff * skb)89 int x25_forward_data(int lci, struct x25_neigh *from, struct sk_buff *skb) {
90
91 struct x25_forward *frwd;
92 struct net_device *peer = NULL;
93 struct x25_neigh *nb;
94 struct sk_buff *skbn;
95 int rc = 0;
96
97 read_lock_bh(&x25_forward_list_lock);
98 list_for_each_entry(frwd, &x25_forward_list, node) {
99 if (frwd->lci == lci) {
100 /* The call is established, either side can send */
101 if (from->dev == frwd->dev1) {
102 peer = frwd->dev2;
103 } else {
104 peer = frwd->dev1;
105 }
106 break;
107 }
108 }
109 read_unlock_bh(&x25_forward_list_lock);
110
111 if ( (nb = x25_get_neigh(peer)) == NULL)
112 goto out;
113
114 if ( (skbn = pskb_copy(skb, GFP_ATOMIC)) == NULL){
115 goto output;
116
117 }
118 x25_transmit_link(skbn, nb);
119
120 rc = 1;
121 output:
122 x25_neigh_put(nb);
123 out:
124 return rc;
125 }
126
x25_clear_forward_by_lci(unsigned int lci)127 void x25_clear_forward_by_lci(unsigned int lci)
128 {
129 struct x25_forward *fwd, *tmp;
130
131 write_lock_bh(&x25_forward_list_lock);
132
133 list_for_each_entry_safe(fwd, tmp, &x25_forward_list, node) {
134 if (fwd->lci == lci) {
135 list_del(&fwd->node);
136 kfree(fwd);
137 }
138 }
139 write_unlock_bh(&x25_forward_list_lock);
140 }
141
142
x25_clear_forward_by_dev(struct net_device * dev)143 void x25_clear_forward_by_dev(struct net_device *dev)
144 {
145 struct x25_forward *fwd, *tmp;
146
147 write_lock_bh(&x25_forward_list_lock);
148
149 list_for_each_entry_safe(fwd, tmp, &x25_forward_list, node) {
150 if ((fwd->dev1 == dev) || (fwd->dev2 == dev)){
151 list_del(&fwd->node);
152 kfree(fwd);
153 }
154 }
155 write_unlock_bh(&x25_forward_list_lock);
156 }
157