Deleted Added
sdiff udiff text old ( 24659 ) new ( 25345 )
full compact
1/*
2 * Copyright (c) 1995, Mike Mitchell
3 * Copyright (c) 1984, 1985, 1986, 1987, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ipx_usrreq.c
35 *
36 * $Id: ipx_usrreq.c,v 1.11 1997/02/22 09:41:57 peter Exp $
37 */
38
39#include <sys/param.h>
40#include <sys/queue.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/protosw.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/errno.h>
49#include <sys/stat.h>
50#include <sys/sysctl.h>
51
52#include <net/if.h>
53#include <net/route.h>
54
55#include <netinet/in.h>
56
57#include <netipx/ipx.h>
58#include <netipx/ipx_pcb.h>
59#include <netipx/ipx_if.h>
60#include <netipx/ipx_var.h>
61#include <netipx/ipx_error.h>
62#include <netipx/ipx_ip.h>
63
64/*
65 * IPX protocol implementation.
66 */
67
68int noipxRoute;
69
70int ipxsendspace = IPXSNDQ;
71SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
72 &ipxsendspace, 0, "");
73int ipxrecvspace = IPXRCVQ;
74SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
75 &ipxrecvspace, 0, "");
76
77static int ipx_usr_abort(struct socket *so);
78static int ipx_attach(struct socket *so, int proto);
79static int ipx_bind(struct socket *so, struct mbuf *nam);
80static int ipx_connect(struct socket *so, struct mbuf *nam);
81static int ipx_detach(struct socket *so);
82static int ipx_disconnect(struct socket *so);
83static int ipx_send(struct socket *so, int flags, struct mbuf *m,
84 struct mbuf *addr, struct mbuf *control);
85static int ipx_shutdown(struct socket *so);
86static int ripx_attach(struct socket *so, int proto);
87
88struct pr_usrreqs ipx_usrreqs = {
89 ipx_usr_abort, pru_accept_notsupp, ipx_attach, ipx_bind,
90 ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
91 ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
92 pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
93 ipx_sockaddr
94};
95
96struct pr_usrreqs ripx_usrreqs = {
97 ipx_usr_abort, pru_accept_notsupp, ripx_attach, ipx_bind,
98 ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
99 ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
100 pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
101 ipx_sockaddr
102};
103
104/*
105 * This may also be called for raw listeners.
106 */
107void
108ipx_input(m, ipxp)
109 struct mbuf *m;
110 register struct ipxpcb *ipxp;
111{
112 register struct ipx *ipx = mtod(m, struct ipx *);
113 struct ifnet *ifp = m->m_pkthdr.rcvif;
114 struct sockaddr_ipx ipx_ipx;
115
116 if (ipxp==0)
117 panic("No ipxpcb");
118 /*
119 * Construct sockaddr format source address.
120 * Stuff source address and datagram in user buffer.
121 */
122 ipx_ipx.sipx_len = sizeof(ipx_ipx);
123 ipx_ipx.sipx_family = AF_IPX;
124 ipx_ipx.sipx_addr = ipx->ipx_sna;
125 ipx_ipx.sipx_zero[0] = '\0';
126 ipx_ipx.sipx_zero[1] = '\0';
127 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp) {
128 register struct ifaddr *ifa;
129
130 for (ifa = ifp->if_addrhead.tqh_first; ifa;
131 ifa = ifa->ifa_link.tqe_next) {
132 if (ifa->ifa_addr->sa_family == AF_IPX) {
133 ipx_ipx.sipx_addr.x_net =
134 IA_SIPX(ifa)->sipx_addr.x_net;
135 break;
136 }
137 }
138 }
139 ipxp->ipxp_rpt = ipx->ipx_pt;
140 if ( ! (ipxp->ipxp_flags & IPXP_RAWIN) ) {
141 m->m_len -= sizeof (struct ipx);
142 m->m_pkthdr.len -= sizeof (struct ipx);
143 m->m_data += sizeof (struct ipx);
144 }
145 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
146 m, (struct mbuf *)0) == 0)
147 goto bad;
148 sorwakeup(ipxp->ipxp_socket);
149 return;
150bad:
151 m_freem(m);
152}
153
154void
155ipx_abort(ipxp)
156 struct ipxpcb *ipxp;
157{
158 struct socket *so = ipxp->ipxp_socket;
159
160 ipx_pcbdisconnect(ipxp);
161 soisdisconnected(so);
162}
163/*
164 * Drop connection, reporting
165 * the specified error.
166 */
167/* struct ipxpcb * DELETE THIS */
168void
169ipx_drop(ipxp, errno)
170 register struct ipxpcb *ipxp;
171 int errno;
172{
173 struct socket *so = ipxp->ipxp_socket;
174
175 /*
176 * someday, in the xerox world
177 * we will generate error protocol packets
178 * announcing that the socket has gone away.
179 */
180 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
181 tp->t_state = TCPS_CLOSED;
182 (void) tcp_output(tp);
183 }*/
184 so->so_error = errno;
185 ipx_pcbdisconnect(ipxp);
186 soisdisconnected(so);
187}
188
189int
190ipx_output(ipxp, m0)
191 struct ipxpcb *ipxp;
192 struct mbuf *m0;
193{
194 register struct mbuf *m;
195 register struct ipx *ipx;
196 register struct socket *so;
197 register int len = 0;
198 register struct route *ro;
199 struct mbuf *mprev = NULL;
200
201 /*
202 * Calculate data length.
203 */
204 for (m = m0; m; m = m->m_next) {
205 mprev = m;
206 len += m->m_len;
207 }
208 /*
209 * Make sure packet is actually of even length.
210 */
211
212 if (len & 1) {
213 m = mprev;
214 if ((m->m_flags & M_EXT) == 0 &&
215 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
216 m->m_len++;
217 } else {
218 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
219
220 if (m1 == 0) {
221 m_freem(m0);
222 return (ENOBUFS);
223 }
224 m1->m_len = 1;
225 * mtod(m1, char *) = 0;
226 m->m_next = m1;
227 }
228 m0->m_pkthdr.len++;
229 }
230
231 /*
232 * Fill in mbuf with extended IPX header
233 * and addresses and length put into network format.
234 */
235 m = m0;
236 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
237 ipx = mtod(m, struct ipx *);
238 } else {
239 M_PREPEND(m, sizeof (struct ipx), M_DONTWAIT);
240 if (m == 0)
241 return (ENOBUFS);
242 ipx = mtod(m, struct ipx *);
243 ipx->ipx_tc = 0;
244 ipx->ipx_pt = ipxp->ipxp_dpt;
245 ipx->ipx_sna = ipxp->ipxp_laddr;
246 ipx->ipx_dna = ipxp->ipxp_faddr;
247 len += sizeof (struct ipx);
248 }
249
250 ipx->ipx_len = htons((u_short)len);
251
252 if (ipxcksum) {
253 ipx->ipx_sum = 0;
254 len = ((len - 1) | 1) + 1;
255 ipx->ipx_sum = ipx_cksum(m, len);
256 } else
257 ipx->ipx_sum = 0xffff;
258
259 /*
260 * Output datagram.
261 */
262 so = ipxp->ipxp_socket;
263 if (so->so_options & SO_DONTROUTE)
264 return (ipx_outputfl(m, (struct route *)0,
265 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
266 /*
267 * Use cached route for previous datagram if
268 * possible. If the previous net was the same
269 * and the interface was a broadcast medium, or
270 * if the previous destination was identical,
271 * then we are ok.
272 *
273 * NB: We don't handle broadcasts because that
274 * would require 3 subroutine calls.
275 */
276 ro = &ipxp->ipxp_route;
277#ifdef ancient_history
278 /*
279 * I think that this will all be handled in ipx_pcbconnect!
280 */
281 if (ro->ro_rt) {
282 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
283 /*
284 * This assumes we have no GH type routes
285 */
286 if (ro->ro_rt->rt_flags & RTF_HOST) {
287 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
288 goto re_route;
289
290 }
291 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
292 register struct ipx_addr *dst =
293 &satoipx_addr(ro->ro_dst);
294 dst->x_host = ipx->ipx_dna.x_host;
295 }
296 /*
297 * Otherwise, we go through the same gateway
298 * and dst is already set up.
299 */
300 } else {
301 re_route:
302 RTFREE(ro->ro_rt);
303 ro->ro_rt = (struct rtentry *)0;
304 }
305 }
306 ipxp->ipxp_lastdst = ipx->ipx_dna;
307#endif /* ancient_history */
308 if (noipxRoute)
309 ro = 0;
310 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
311}
312
313/* ARGSUSED */
314int
315ipx_ctloutput(req, so, level, name, value)
316 int req, level;
317 struct socket *so;
318 int name;
319 struct mbuf **value;
320{
321 register struct mbuf *m;
322 struct ipxpcb *ipxp = sotoipxpcb(so);
323 int mask, error = 0;
324 /*extern long ipx_pexseq;*/ /*XXX*//*JRE*/
325
326 if (ipxp == NULL)
327 return (EINVAL);
328
329 switch (req) {
330
331 case PRCO_GETOPT:
332 if (value==NULL)
333 return (EINVAL);
334 m = m_get(M_DONTWAIT, MT_DATA);
335 if (m==NULL)
336 return (ENOBUFS);
337 switch (name) {
338
339 case SO_ALL_PACKETS:
340 mask = IPXP_ALL_PACKETS;
341 goto get_flags;
342
343 case SO_HEADERS_ON_INPUT:
344 mask = IPXP_RAWIN;
345 goto get_flags;
346
347 case SO_HEADERS_ON_OUTPUT:
348 mask = IPXP_RAWOUT;
349 get_flags:
350 m->m_len = sizeof(short);
351 *mtod(m, short *) = ipxp->ipxp_flags & mask;
352 break;
353
354 case SO_DEFAULT_HEADERS:
355 m->m_len = sizeof(struct ipx);
356 {
357 register struct ipx *ipx = mtod(m, struct ipx *);
358 ipx->ipx_len = 0;
359 ipx->ipx_sum = 0;
360 ipx->ipx_tc = 0;
361 ipx->ipx_pt = ipxp->ipxp_dpt;
362 ipx->ipx_dna = ipxp->ipxp_faddr;
363 ipx->ipx_sna = ipxp->ipxp_laddr;
364 }
365 break;
366
367 case SO_SEQNO:
368 m->m_len = sizeof(long);
369 *mtod(m, long *) = ipx_pexseq++;
370 break;
371
372 default:
373 error = EINVAL;
374 }
375 *value = m;
376 break;
377
378 case PRCO_SETOPT:
379 switch (name) {
380 int *ok;
381
382 case SO_ALL_PACKETS:
383 mask = IPXP_ALL_PACKETS;
384 goto set_head;
385
386 case SO_HEADERS_ON_INPUT:
387 mask = IPXP_RAWIN;
388 goto set_head;
389
390 case SO_HEADERS_ON_OUTPUT:
391 mask = IPXP_RAWOUT;
392 set_head:
393 if (value && *value) {
394 ok = mtod(*value, int *);
395 if (*ok)
396 ipxp->ipxp_flags |= mask;
397 else
398 ipxp->ipxp_flags &= ~mask;
399 } else error = EINVAL;
400 break;
401
402 case SO_DEFAULT_HEADERS:
403 {
404 register struct ipx *ipx
405 = mtod(*value, struct ipx *);
406 ipxp->ipxp_dpt = ipx->ipx_pt;
407 }
408 break;
409#ifdef IPXIP
410 case SO_IPXIP_ROUTE:
411 error = ipxip_route(so, *value);
412 break;
413#endif /* IPXIP */
414#ifdef IPXTUNNEL
415 case SO_IPXTUNNEL_ROUTE
416 error = ipxtun_route(so, *value);
417 break;
418#endif
419 default:
420 error = EINVAL;
421 }
422 if (value && *value)
423 m_freem(*value);
424 break;
425 }
426 return (error);
427}
428
429static int
430ipx_usr_abort(so)
431 struct socket *so;
432{
433 int s;
434 struct ipxpcb *ipxp = sotoipxpcb(so);
435
436 s = splnet();
437 ipx_pcbdetach(ipxp);
438 splx(s);
439 sofree(so);
440 soisdisconnected(so);
441 return (0);
442}
443
444static int
445ipx_attach(so, proto)
446 struct socket *so;
447 int proto;
448{
449 int error;
450 int s;
451 struct ipxpcb *ipxp = sotoipxpcb(so);
452
453 if (ipxp != NULL)
454 return (EINVAL);
455 s = splnet();
456 error = ipx_pcballoc(so, &ipxpcb);
457 splx(s);
458 if (error == 0)
459 error = soreserve(so, ipxsendspace, ipxrecvspace);
460 return (error);
461}
462
463static int
464ipx_bind(so, nam)
465 struct socket *so;
466 struct mbuf *nam;
467{
468 struct ipxpcb *ipxp = sotoipxpcb(so);
469
470 return (ipx_pcbbind(ipxp, nam));
471}
472
473static int
474ipx_connect(so, nam)
475 struct socket *so;
476 struct mbuf *nam;
477{
478 int error;
479 int s;
480 struct ipxpcb *ipxp = sotoipxpcb(so);
481
482 if (!ipx_nullhost(ipxp->ipxp_faddr))
483 return (EISCONN);
484 s = splnet();
485 error = ipx_pcbconnect(ipxp, nam);
486 splx(s);
487 if (error == 0)
488 soisconnected(so);
489 return (error);
490}
491
492static int
493ipx_detach(so)
494 struct socket *so;
495{
496 int s;
497 struct ipxpcb *ipxp = sotoipxpcb(so);
498
499 if (ipxp == NULL)
500 return (ENOTCONN);
501 s = splnet();
502 ipx_pcbdetach(ipxp);
503 splx(s);
504 return (0);
505}
506
507static int
508ipx_disconnect(so)
509 struct socket *so;
510{
511 int s;
512 struct ipxpcb *ipxp = sotoipxpcb(so);
513
514 if (ipx_nullhost(ipxp->ipxp_faddr))
515 return (ENOTCONN);
516 s = splnet();
517 ipx_pcbdisconnect(ipxp);
518 splx(s);
519 soisdisconnected(so);
520 return (0);
521}
522
523int
524ipx_peeraddr(so, nam)
525 struct socket *so;
526 struct mbuf *nam;
527{
528 struct ipxpcb *ipxp = sotoipxpcb(so);
529
530 ipx_setpeeraddr(ipxp, nam);
531 return (0);
532}
533
534static int
535ipx_send(so, flags, m, nam, control)
536 struct socket *so;
537 int flags;
538 struct mbuf *m;
539 struct mbuf *nam;
540 struct mbuf *control;
541{
542 int error;
543 struct ipxpcb *ipxp = sotoipxpcb(so);
544 struct ipx_addr laddr;
545 int s = 0;
546
547 if (nam) {
548 laddr = ipxp->ipxp_laddr;
549 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
550 error = EISCONN;
551 goto send_release;
552 }
553 /*
554 * Must block input while temporarily connected.
555 */
556 s = splnet();
557 error = ipx_pcbconnect(ipxp, nam);
558 if (error) {
559 splx(s);
560 goto send_release;
561 }
562 } else {
563 if (ipx_nullhost(ipxp->ipxp_faddr)) {
564 error = ENOTCONN;
565 goto send_release;
566 }
567 }
568 error = ipx_output(ipxp, m);
569 m = NULL;
570 if (nam) {
571 ipx_pcbdisconnect(ipxp);
572 splx(s);
573 ipxp->ipxp_laddr.x_host = laddr.x_host;
574 ipxp->ipxp_laddr.x_port = laddr.x_port;
575 }
576
577send_release:
578 if (m != NULL)
579 m_freem(m);
580 return (error);
581}
582
583static int
584ipx_shutdown(so)
585 struct socket *so;
586{
587 socantsendmore(so);
588 return (0);
589}
590
591int
592ipx_sockaddr(so, nam)
593 struct socket *so;
594 struct mbuf *nam;
595{
596 struct ipxpcb *ipxp = sotoipxpcb(so);
597
598 ipx_setsockaddr(ipxp, nam);
599 return (0);
600}
601
602static int
603ripx_attach(so, proto)
604 struct socket *so;
605 int proto;
606{
607 int error = 0;
608 int s;
609 struct ipxpcb *ipxp = sotoipxpcb(so);
610
611 if (!(so->so_state & SS_PRIV) || (ipxp != NULL))
612 return (EINVAL);
613 s = splnet();
614 error = ipx_pcballoc(so, &ipxrawpcb);
615 splx(s);
616 if (error)
617 return (error);
618 error = soreserve(so, ipxsendspace, ipxrecvspace);
619 if (error)
620 return (error);
621 ipxp = sotoipxpcb(so);
622 ipxp->ipxp_faddr.x_host = ipx_broadhost;
623 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;
624 return (error);
625}