1 // SPDX-License-Identifier: GPL-2.0
3 * Implements the IPX routing routines.
4 * Code moved from af_ipx.c.
6 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 2003
8 * See net/ipx/ChangeLog.
11 #include <linux/list.h>
12 #include <linux/route.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
19 LIST_HEAD(ipx_routes
);
20 DEFINE_RWLOCK(ipx_routes_lock
);
22 extern struct ipx_interface
*ipx_internal_net
;
24 extern struct ipx_interface
*ipxitf_find_using_net(__be32 net
);
25 extern int ipxitf_demux_socket(struct ipx_interface
*intrfc
,
26 struct sk_buff
*skb
, int copy
);
27 extern int ipxitf_demux_socket(struct ipx_interface
*intrfc
,
28 struct sk_buff
*skb
, int copy
);
30 struct ipx_route
*ipxrtr_lookup(__be32 net
)
34 read_lock_bh(&ipx_routes_lock
);
35 list_for_each_entry(r
, &ipx_routes
, node
)
36 if (r
->ir_net
== net
) {
42 read_unlock_bh(&ipx_routes_lock
);
47 * Caller must hold a reference to intrfc
49 int ipxrtr_add_route(__be32 network
, struct ipx_interface
*intrfc
,
55 /* Get a route structure; either existing or create */
56 rt
= ipxrtr_lookup(network
);
58 rt
= kmalloc(sizeof(*rt
), GFP_ATOMIC
);
63 refcount_set(&rt
->refcnt
, 1);
65 write_lock_bh(&ipx_routes_lock
);
66 list_add(&rt
->node
, &ipx_routes
);
67 write_unlock_bh(&ipx_routes_lock
);
70 if (intrfc
== ipx_internal_net
)
75 rt
->ir_intrfc
= intrfc
;
77 memset(rt
->ir_router_node
, '\0', IPX_NODE_LEN
);
80 memcpy(rt
->ir_router_node
, node
, IPX_NODE_LEN
);
91 void ipxrtr_del_routes(struct ipx_interface
*intrfc
)
93 struct ipx_route
*r
, *tmp
;
95 write_lock_bh(&ipx_routes_lock
);
96 list_for_each_entry_safe(r
, tmp
, &ipx_routes
, node
)
97 if (r
->ir_intrfc
== intrfc
) {
101 write_unlock_bh(&ipx_routes_lock
);
104 static int ipxrtr_create(struct ipx_route_definition
*rd
)
106 struct ipx_interface
*intrfc
;
107 int rc
= -ENETUNREACH
;
109 /* Find the appropriate interface */
110 intrfc
= ipxitf_find_using_net(rd
->ipx_router_network
);
113 rc
= ipxrtr_add_route(rd
->ipx_network
, intrfc
, rd
->ipx_router_node
);
119 static int ipxrtr_delete(__be32 net
)
121 struct ipx_route
*r
, *tmp
;
124 write_lock_bh(&ipx_routes_lock
);
125 list_for_each_entry_safe(r
, tmp
, &ipx_routes
, node
)
126 if (r
->ir_net
== net
) {
127 /* Directly connected; can't lose route */
138 write_unlock_bh(&ipx_routes_lock
);
143 * The skb has to be unshared, we'll end up calling ipxitf_send, that'll
146 int ipxrtr_route_skb(struct sk_buff
*skb
)
148 struct ipxhdr
*ipx
= ipx_hdr(skb
);
149 struct ipx_route
*r
= ipxrtr_lookup(IPX_SKB_CB(skb
)->ipx_dest_net
);
151 if (!r
) { /* no known route */
156 ipxitf_hold(r
->ir_intrfc
);
157 ipxitf_send(r
->ir_intrfc
, skb
, r
->ir_routed
?
158 r
->ir_router_node
: ipx
->ipx_dest
.node
);
159 ipxitf_put(r
->ir_intrfc
);
166 * Route an outgoing frame from a socket.
168 int ipxrtr_route_packet(struct sock
*sk
, struct sockaddr_ipx
*usipx
,
169 struct msghdr
*msg
, size_t len
, int noblock
)
172 struct ipx_sock
*ipxs
= ipx_sk(sk
);
173 struct ipx_interface
*intrfc
;
177 struct ipx_route
*rt
= NULL
;
180 /* Find the appropriate interface on which to send packet */
181 if (!usipx
->sipx_network
&& ipx_primary_net
) {
182 usipx
->sipx_network
= ipx_primary_net
->if_netnum
;
183 intrfc
= ipx_primary_net
;
185 rt
= ipxrtr_lookup(usipx
->sipx_network
);
189 intrfc
= rt
->ir_intrfc
;
193 ipx_offset
= intrfc
->if_ipx_offset
;
194 size
= sizeof(struct ipxhdr
) + len
+ ipx_offset
;
196 skb
= sock_alloc_send_skb(sk
, size
, noblock
, &rc
);
200 skb_reserve(skb
, ipx_offset
);
203 /* Fill in IPX header */
204 skb_reset_network_header(skb
);
205 skb_reset_transport_header(skb
);
206 skb_put(skb
, sizeof(struct ipxhdr
));
208 ipx
->ipx_pktsize
= htons(len
+ sizeof(struct ipxhdr
));
209 IPX_SKB_CB(skb
)->ipx_tctrl
= 0;
210 ipx
->ipx_type
= usipx
->sipx_type
;
212 IPX_SKB_CB(skb
)->last_hop
.index
= -1;
213 #ifdef CONFIG_IPX_INTERN
214 IPX_SKB_CB(skb
)->ipx_source_net
= ipxs
->intrfc
->if_netnum
;
215 memcpy(ipx
->ipx_source
.node
, ipxs
->node
, IPX_NODE_LEN
);
217 rc
= ntohs(ipxs
->port
);
218 if (rc
== 0x453 || rc
== 0x452) {
219 /* RIP/SAP special handling for mars_nwe */
220 IPX_SKB_CB(skb
)->ipx_source_net
= intrfc
->if_netnum
;
221 memcpy(ipx
->ipx_source
.node
, intrfc
->if_node
, IPX_NODE_LEN
);
223 IPX_SKB_CB(skb
)->ipx_source_net
= ipxs
->intrfc
->if_netnum
;
224 memcpy(ipx
->ipx_source
.node
, ipxs
->intrfc
->if_node
,
227 #endif /* CONFIG_IPX_INTERN */
228 ipx
->ipx_source
.sock
= ipxs
->port
;
229 IPX_SKB_CB(skb
)->ipx_dest_net
= usipx
->sipx_network
;
230 memcpy(ipx
->ipx_dest
.node
, usipx
->sipx_node
, IPX_NODE_LEN
);
231 ipx
->ipx_dest
.sock
= usipx
->sipx_port
;
233 rc
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
239 /* Apply checksum. Not allowed on 802.3 links. */
240 if (sk
->sk_no_check_tx
||
241 intrfc
->if_dlink_type
== htons(IPX_FRAME_8023
))
242 ipx
->ipx_checksum
= htons(0xFFFF);
244 ipx
->ipx_checksum
= ipx_cksum(ipx
, len
+ sizeof(struct ipxhdr
));
246 rc
= ipxitf_send(intrfc
, skb
, (rt
&& rt
->ir_routed
) ?
247 rt
->ir_router_node
: ipx
->ipx_dest
.node
);
257 * We use a normal struct rtentry for route handling
259 int ipxrtr_ioctl(unsigned int cmd
, void __user
*arg
)
261 struct rtentry rt
; /* Use these to behave like 'other' stacks */
262 struct sockaddr_ipx
*sg
, *st
;
265 if (copy_from_user(&rt
, arg
, sizeof(rt
)))
268 sg
= (struct sockaddr_ipx
*)&rt
.rt_gateway
;
269 st
= (struct sockaddr_ipx
*)&rt
.rt_dst
;
272 if (!(rt
.rt_flags
& RTF_GATEWAY
) || /* Direct routes are fixed */
273 sg
->sipx_family
!= AF_IPX
||
274 st
->sipx_family
!= AF_IPX
)
279 rc
= ipxrtr_delete(st
->sipx_network
);
282 struct ipx_route_definition f
;
283 f
.ipx_network
= st
->sipx_network
;
284 f
.ipx_router_network
= sg
->sipx_network
;
285 memcpy(f
.ipx_router_node
, sg
->sipx_node
, IPX_NODE_LEN
);
286 rc
= ipxrtr_create(&f
);