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