2 * Implements the IPX routing routines.
3 * Code moved from af_ipx.c.
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 2003
7 * See net/ipx/ChangeLog.
10 #include <linux/list.h>
11 #include <linux/route.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
18 LIST_HEAD(ipx_routes
);
19 DEFINE_RWLOCK(ipx_routes_lock
);
21 extern struct ipx_interface
*ipx_internal_net
;
23 extern struct ipx_interface
*ipxitf_find_using_net(__be32 net
);
24 extern int ipxitf_demux_socket(struct ipx_interface
*intrfc
,
25 struct sk_buff
*skb
, int copy
);
26 extern int ipxitf_demux_socket(struct ipx_interface
*intrfc
,
27 struct sk_buff
*skb
, int copy
);
29 struct ipx_route
*ipxrtr_lookup(__be32 net
)
33 read_lock_bh(&ipx_routes_lock
);
34 list_for_each_entry(r
, &ipx_routes
, node
)
35 if (r
->ir_net
== net
) {
41 read_unlock_bh(&ipx_routes_lock
);
46 * Caller must hold a reference to intrfc
48 int ipxrtr_add_route(__be32 network
, struct ipx_interface
*intrfc
,
54 /* Get a route structure; either existing or create */
55 rt
= ipxrtr_lookup(network
);
57 rt
= kmalloc(sizeof(*rt
), GFP_ATOMIC
);
62 atomic_set(&rt
->refcnt
, 1);
64 write_lock_bh(&ipx_routes_lock
);
65 list_add(&rt
->node
, &ipx_routes
);
66 write_unlock_bh(&ipx_routes_lock
);
69 if (intrfc
== ipx_internal_net
)
74 rt
->ir_intrfc
= intrfc
;
76 memset(rt
->ir_router_node
, '\0', IPX_NODE_LEN
);
79 memcpy(rt
->ir_router_node
, node
, IPX_NODE_LEN
);
90 void ipxrtr_del_routes(struct ipx_interface
*intrfc
)
92 struct ipx_route
*r
, *tmp
;
94 write_lock_bh(&ipx_routes_lock
);
95 list_for_each_entry_safe(r
, tmp
, &ipx_routes
, node
)
96 if (r
->ir_intrfc
== intrfc
) {
100 write_unlock_bh(&ipx_routes_lock
);
103 static int ipxrtr_create(struct ipx_route_definition
*rd
)
105 struct ipx_interface
*intrfc
;
106 int rc
= -ENETUNREACH
;
108 /* Find the appropriate interface */
109 intrfc
= ipxitf_find_using_net(rd
->ipx_router_network
);
112 rc
= ipxrtr_add_route(rd
->ipx_network
, intrfc
, rd
->ipx_router_node
);
118 static int ipxrtr_delete(__be32 net
)
120 struct ipx_route
*r
, *tmp
;
123 write_lock_bh(&ipx_routes_lock
);
124 list_for_each_entry_safe(r
, tmp
, &ipx_routes
, node
)
125 if (r
->ir_net
== net
) {
126 /* Directly connected; can't lose route */
137 write_unlock_bh(&ipx_routes_lock
);
142 * The skb has to be unshared, we'll end up calling ipxitf_send, that'll
145 int ipxrtr_route_skb(struct sk_buff
*skb
)
147 struct ipxhdr
*ipx
= ipx_hdr(skb
);
148 struct ipx_route
*r
= ipxrtr_lookup(IPX_SKB_CB(skb
)->ipx_dest_net
);
150 if (!r
) { /* no known route */
155 ipxitf_hold(r
->ir_intrfc
);
156 ipxitf_send(r
->ir_intrfc
, skb
, r
->ir_routed
?
157 r
->ir_router_node
: ipx
->ipx_dest
.node
);
158 ipxitf_put(r
->ir_intrfc
);
165 * Route an outgoing frame from a socket.
167 int ipxrtr_route_packet(struct sock
*sk
, struct sockaddr_ipx
*usipx
,
168 struct msghdr
*msg
, size_t len
, int noblock
)
171 struct ipx_sock
*ipxs
= ipx_sk(sk
);
172 struct ipx_interface
*intrfc
;
176 struct ipx_route
*rt
= NULL
;
179 /* Find the appropriate interface on which to send packet */
180 if (!usipx
->sipx_network
&& ipx_primary_net
) {
181 usipx
->sipx_network
= ipx_primary_net
->if_netnum
;
182 intrfc
= ipx_primary_net
;
184 rt
= ipxrtr_lookup(usipx
->sipx_network
);
188 intrfc
= rt
->ir_intrfc
;
192 ipx_offset
= intrfc
->if_ipx_offset
;
193 size
= sizeof(struct ipxhdr
) + len
+ ipx_offset
;
195 skb
= sock_alloc_send_skb(sk
, size
, noblock
, &rc
);
199 skb_reserve(skb
, ipx_offset
);
202 /* Fill in IPX header */
203 skb_reset_network_header(skb
);
204 skb_reset_transport_header(skb
);
205 skb_put(skb
, sizeof(struct ipxhdr
));
207 ipx
->ipx_pktsize
= htons(len
+ sizeof(struct ipxhdr
));
208 IPX_SKB_CB(skb
)->ipx_tctrl
= 0;
209 ipx
->ipx_type
= usipx
->sipx_type
;
211 IPX_SKB_CB(skb
)->last_hop
.index
= -1;
212 #ifdef CONFIG_IPX_INTERN
213 IPX_SKB_CB(skb
)->ipx_source_net
= ipxs
->intrfc
->if_netnum
;
214 memcpy(ipx
->ipx_source
.node
, ipxs
->node
, IPX_NODE_LEN
);
216 rc
= ntohs(ipxs
->port
);
217 if (rc
== 0x453 || rc
== 0x452) {
218 /* RIP/SAP special handling for mars_nwe */
219 IPX_SKB_CB(skb
)->ipx_source_net
= intrfc
->if_netnum
;
220 memcpy(ipx
->ipx_source
.node
, intrfc
->if_node
, IPX_NODE_LEN
);
222 IPX_SKB_CB(skb
)->ipx_source_net
= ipxs
->intrfc
->if_netnum
;
223 memcpy(ipx
->ipx_source
.node
, ipxs
->intrfc
->if_node
,
226 #endif /* CONFIG_IPX_INTERN */
227 ipx
->ipx_source
.sock
= ipxs
->port
;
228 IPX_SKB_CB(skb
)->ipx_dest_net
= usipx
->sipx_network
;
229 memcpy(ipx
->ipx_dest
.node
, usipx
->sipx_node
, IPX_NODE_LEN
);
230 ipx
->ipx_dest
.sock
= usipx
->sipx_port
;
232 rc
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
238 /* Apply checksum. Not allowed on 802.3 links. */
239 if (sk
->sk_no_check_tx
||
240 intrfc
->if_dlink_type
== htons(IPX_FRAME_8023
))
241 ipx
->ipx_checksum
= htons(0xFFFF);
243 ipx
->ipx_checksum
= ipx_cksum(ipx
, len
+ sizeof(struct ipxhdr
));
245 rc
= ipxitf_send(intrfc
, skb
, (rt
&& rt
->ir_routed
) ?
246 rt
->ir_router_node
: ipx
->ipx_dest
.node
);
256 * We use a normal struct rtentry for route handling
258 int ipxrtr_ioctl(unsigned int cmd
, void __user
*arg
)
260 struct rtentry rt
; /* Use these to behave like 'other' stacks */
261 struct sockaddr_ipx
*sg
, *st
;
264 if (copy_from_user(&rt
, arg
, sizeof(rt
)))
267 sg
= (struct sockaddr_ipx
*)&rt
.rt_gateway
;
268 st
= (struct sockaddr_ipx
*)&rt
.rt_dst
;
271 if (!(rt
.rt_flags
& RTF_GATEWAY
) || /* Direct routes are fixed */
272 sg
->sipx_family
!= AF_IPX
||
273 st
->sipx_family
!= AF_IPX
)
278 rc
= ipxrtr_delete(st
->sipx_network
);
281 struct ipx_route_definition f
;
282 f
.ipx_network
= st
->sipx_network
;
283 f
.ipx_router_network
= sg
->sipx_network
;
284 memcpy(f
.ipx_router_node
, sg
->sipx_node
, IPX_NODE_LEN
);
285 rc
= ipxrtr_create(&f
);