• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *	Implements the IPX routing routines.
4  *	Code moved from af_ipx.c.
5  *
6  *	Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 2003
7  *
8  *	See net/ipx/ChangeLog.
9  */
10 
11 #include <linux/list.h>
12 #include <linux/route.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 
16 #include <net/ipx.h>
17 #include <net/sock.h>
18 
19 LIST_HEAD(ipx_routes);
20 DEFINE_RWLOCK(ipx_routes_lock);
21 
22 extern struct ipx_interface *ipx_internal_net;
23 
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);
29 
ipxrtr_lookup(__be32 net)30 struct ipx_route *ipxrtr_lookup(__be32 net)
31 {
32 	struct ipx_route *r;
33 
34 	read_lock_bh(&ipx_routes_lock);
35 	list_for_each_entry(r, &ipx_routes, node)
36 		if (r->ir_net == net) {
37 			ipxrtr_hold(r);
38 			goto unlock;
39 		}
40 	r = NULL;
41 unlock:
42 	read_unlock_bh(&ipx_routes_lock);
43 	return r;
44 }
45 
46 /*
47  * Caller must hold a reference to intrfc
48  */
ipxrtr_add_route(__be32 network,struct ipx_interface * intrfc,unsigned char * node)49 int ipxrtr_add_route(__be32 network, struct ipx_interface *intrfc,
50 		     unsigned char *node)
51 {
52 	struct ipx_route *rt;
53 	int rc;
54 
55 	/* Get a route structure; either existing or create */
56 	rt = ipxrtr_lookup(network);
57 	if (!rt) {
58 		rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
59 		rc = -EAGAIN;
60 		if (!rt)
61 			goto out;
62 
63 		refcount_set(&rt->refcnt, 1);
64 		ipxrtr_hold(rt);
65 		write_lock_bh(&ipx_routes_lock);
66 		list_add(&rt->node, &ipx_routes);
67 		write_unlock_bh(&ipx_routes_lock);
68 	} else {
69 		rc = -EEXIST;
70 		if (intrfc == ipx_internal_net)
71 			goto out_put;
72 	}
73 
74 	rt->ir_net 	= network;
75 	rt->ir_intrfc 	= intrfc;
76 	if (!node) {
77 		memset(rt->ir_router_node, '\0', IPX_NODE_LEN);
78 		rt->ir_routed = 0;
79 	} else {
80 		memcpy(rt->ir_router_node, node, IPX_NODE_LEN);
81 		rt->ir_routed = 1;
82 	}
83 
84 	rc = 0;
85 out_put:
86 	ipxrtr_put(rt);
87 out:
88 	return rc;
89 }
90 
ipxrtr_del_routes(struct ipx_interface * intrfc)91 void ipxrtr_del_routes(struct ipx_interface *intrfc)
92 {
93 	struct ipx_route *r, *tmp;
94 
95 	write_lock_bh(&ipx_routes_lock);
96 	list_for_each_entry_safe(r, tmp, &ipx_routes, node)
97 		if (r->ir_intrfc == intrfc) {
98 			list_del(&r->node);
99 			ipxrtr_put(r);
100 		}
101 	write_unlock_bh(&ipx_routes_lock);
102 }
103 
ipxrtr_create(struct ipx_route_definition * rd)104 static int ipxrtr_create(struct ipx_route_definition *rd)
105 {
106 	struct ipx_interface *intrfc;
107 	int rc = -ENETUNREACH;
108 
109 	/* Find the appropriate interface */
110 	intrfc = ipxitf_find_using_net(rd->ipx_router_network);
111 	if (!intrfc)
112 		goto out;
113 	rc = ipxrtr_add_route(rd->ipx_network, intrfc, rd->ipx_router_node);
114 	ipxitf_put(intrfc);
115 out:
116 	return rc;
117 }
118 
ipxrtr_delete(__be32 net)119 static int ipxrtr_delete(__be32 net)
120 {
121 	struct ipx_route *r, *tmp;
122 	int rc;
123 
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 */
128 			rc = -EPERM;
129 			if (!r->ir_routed)
130 				goto out;
131 			list_del(&r->node);
132 			ipxrtr_put(r);
133 			rc = 0;
134 			goto out;
135 		}
136 	rc = -ENOENT;
137 out:
138 	write_unlock_bh(&ipx_routes_lock);
139 	return rc;
140 }
141 
142 /*
143  * The skb has to be unshared, we'll end up calling ipxitf_send, that'll
144  * modify the packet
145  */
ipxrtr_route_skb(struct sk_buff * skb)146 int ipxrtr_route_skb(struct sk_buff *skb)
147 {
148 	struct ipxhdr *ipx = ipx_hdr(skb);
149 	struct ipx_route *r = ipxrtr_lookup(IPX_SKB_CB(skb)->ipx_dest_net);
150 
151 	if (!r) {	/* no known route */
152 		kfree_skb(skb);
153 		return 0;
154 	}
155 
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);
160 	ipxrtr_put(r);
161 
162 	return 0;
163 }
164 
165 /*
166  * Route an outgoing frame from a socket.
167  */
ipxrtr_route_packet(struct sock * sk,struct sockaddr_ipx * usipx,struct msghdr * msg,size_t len,int noblock)168 int ipxrtr_route_packet(struct sock *sk, struct sockaddr_ipx *usipx,
169 			struct msghdr *msg, size_t len, int noblock)
170 {
171 	struct sk_buff *skb;
172 	struct ipx_sock *ipxs = ipx_sk(sk);
173 	struct ipx_interface *intrfc;
174 	struct ipxhdr *ipx;
175 	size_t size;
176 	int ipx_offset;
177 	struct ipx_route *rt = NULL;
178 	int rc;
179 
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;
184 	} else {
185 		rt = ipxrtr_lookup(usipx->sipx_network);
186 		rc = -ENETUNREACH;
187 		if (!rt)
188 			goto out;
189 		intrfc = rt->ir_intrfc;
190 	}
191 
192 	ipxitf_hold(intrfc);
193 	ipx_offset = intrfc->if_ipx_offset;
194 	size = sizeof(struct ipxhdr) + len + ipx_offset;
195 
196 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
197 	if (!skb)
198 		goto out_put;
199 
200 	skb_reserve(skb, ipx_offset);
201 	skb->sk = sk;
202 
203 	/* Fill in IPX header */
204 	skb_reset_network_header(skb);
205 	skb_reset_transport_header(skb);
206 	skb_put(skb, sizeof(struct ipxhdr));
207 	ipx = ipx_hdr(skb);
208 	ipx->ipx_pktsize = htons(len + sizeof(struct ipxhdr));
209 	IPX_SKB_CB(skb)->ipx_tctrl = 0;
210 	ipx->ipx_type 	 = usipx->sipx_type;
211 
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);
216 #else
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);
222 	} else {
223 		IPX_SKB_CB(skb)->ipx_source_net = ipxs->intrfc->if_netnum;
224 		memcpy(ipx->ipx_source.node, ipxs->intrfc->if_node,
225 			IPX_NODE_LEN);
226 	}
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;
232 
233 	rc = memcpy_from_msg(skb_put(skb, len), msg, len);
234 	if (rc) {
235 		kfree_skb(skb);
236 		goto out_put;
237 	}
238 
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);
243 	else
244 		ipx->ipx_checksum = ipx_cksum(ipx, len + sizeof(struct ipxhdr));
245 
246 	rc = ipxitf_send(intrfc, skb, (rt && rt->ir_routed) ?
247 			 rt->ir_router_node : ipx->ipx_dest.node);
248 out_put:
249 	ipxitf_put(intrfc);
250 	if (rt)
251 		ipxrtr_put(rt);
252 out:
253 	return rc;
254 }
255 
256 /*
257  * We use a normal struct rtentry for route handling
258  */
ipxrtr_ioctl(unsigned int cmd,void __user * arg)259 int ipxrtr_ioctl(unsigned int cmd, void __user *arg)
260 {
261 	struct rtentry rt;	/* Use these to behave like 'other' stacks */
262 	struct sockaddr_ipx *sg, *st;
263 	int rc = -EFAULT;
264 
265 	if (copy_from_user(&rt, arg, sizeof(rt)))
266 		goto out;
267 
268 	sg = (struct sockaddr_ipx *)&rt.rt_gateway;
269 	st = (struct sockaddr_ipx *)&rt.rt_dst;
270 
271 	rc = -EINVAL;
272 	if (!(rt.rt_flags & RTF_GATEWAY) || /* Direct routes are fixed */
273 	    sg->sipx_family != AF_IPX ||
274 	    st->sipx_family != AF_IPX)
275 		goto out;
276 
277 	switch (cmd) {
278 	case SIOCDELRT:
279 		rc = ipxrtr_delete(st->sipx_network);
280 		break;
281 	case SIOCADDRT: {
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);
287 		break;
288 	}
289 	}
290 
291 out:
292 	return rc;
293 }
294