1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * 03-01-2007 Added forwarding for x.25 Andrew Hendry
7 #define pr_fmt(fmt) "X25: " fmt
9 #include <linux/if_arp.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
14 LIST_HEAD(x25_forward_list
);
15 DEFINE_RWLOCK(x25_forward_list_lock
);
17 int x25_forward_call(struct x25_address
*dest_addr
, struct x25_neigh
*from
,
18 struct sk_buff
*skb
, int lci
)
21 struct x25_neigh
*neigh_new
= NULL
;
22 struct x25_forward
*x25_frwd
, *new_frwd
;
27 if ((rt
= x25_get_route(dest_addr
)) == NULL
)
30 if ((neigh_new
= x25_get_neigh(rt
->dev
)) == NULL
) {
31 /* This shouldn't happen, if it occurs somehow
32 * do something sensible
37 /* Avoid a loop. This is the normal exit path for a
38 * system with only one x.25 iface and default route
40 if (rt
->dev
== from
->dev
) {
44 /* Remote end sending a call request on an already
45 * established LCI? It shouldn't happen, just in case..
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");
54 read_unlock_bh(&x25_forward_list_lock
);
56 /* Save the forwarding details for future traffic */
58 if ((new_frwd
= kmalloc(sizeof(struct x25_forward
),
59 GFP_ATOMIC
)) == NULL
){
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
);
71 /* Forward the call request */
72 if ( (skbn
= skb_clone(skb
, GFP_ATOMIC
)) == NULL
){
75 x25_transmit_link(skbn
, neigh_new
);
80 x25_neigh_put(neigh_new
);
90 int x25_forward_data(int lci
, struct x25_neigh
*from
, struct sk_buff
*skb
) {
92 struct x25_forward
*frwd
;
93 struct net_device
*peer
= NULL
;
98 read_lock_bh(&x25_forward_list_lock
);
99 list_for_each_entry(frwd
, &x25_forward_list
, node
) {
100 if (frwd
->lci
== lci
) {
101 /* The call is established, either side can send */
102 if (from
->dev
== frwd
->dev1
) {
110 read_unlock_bh(&x25_forward_list_lock
);
112 if ( (nb
= x25_get_neigh(peer
)) == NULL
)
115 if ( (skbn
= pskb_copy(skb
, GFP_ATOMIC
)) == NULL
){
119 x25_transmit_link(skbn
, nb
);
128 void x25_clear_forward_by_lci(unsigned int lci
)
130 struct x25_forward
*fwd
, *tmp
;
132 write_lock_bh(&x25_forward_list_lock
);
134 list_for_each_entry_safe(fwd
, tmp
, &x25_forward_list
, node
) {
135 if (fwd
->lci
== lci
) {
136 list_del(&fwd
->node
);
140 write_unlock_bh(&x25_forward_list_lock
);
144 void x25_clear_forward_by_dev(struct net_device
*dev
)
146 struct x25_forward
*fwd
, *tmp
;
148 write_lock_bh(&x25_forward_list_lock
);
150 list_for_each_entry_safe(fwd
, tmp
, &x25_forward_list
, node
) {
151 if ((fwd
->dev1
== dev
) || (fwd
->dev2
== dev
)){
152 list_del(&fwd
->node
);
156 write_unlock_bh(&x25_forward_list_lock
);