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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
36 * $FreeBSD: src/sys/netipx/ipx_usrreq.c,v 1.26.2.1 2001/02/22 09:44:18 bp Exp $
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/socketvar2.h>
51 #include <sys/sysctl.h>
53 #include <sys/thread2.h>
54 #include <sys/msgport2.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
68 * IPX protocol implementation.
71 static int ipxsendspace = IPXSNDQ;
72 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
73 &ipxsendspace, 0, "");
74 static int ipxrecvspace = IPXRCVQ;
75 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
76 &ipxrecvspace, 0, "");
78 static void ipx_usr_abort(netmsg_t msg);
79 static void ipx_attach(netmsg_t msg);
80 static void ipx_bind(netmsg_t msg);
81 static void ipx_connect(netmsg_t msg);
82 /*static void ipx_control(netmsg_t msg);*/
83 static void ipx_detach(netmsg_t msg);
84 static void ipx_disconnect(netmsg_t msg);
85 /*static void ipx_peeraddr(netmsg_t msg);*/
86 static void ipx_send(netmsg_t msg);
87 static void ipx_shutdown(netmsg_t msg);
88 /*static void ipx_sockaddr(netmsg_t msg);*/
89 static void ripx_attach(netmsg_t msg);
91 struct pr_usrreqs ipx_usrreqs = {
92 .pru_abort = ipx_usr_abort,
93 .pru_accept = pr_generic_notsupp,
94 .pru_attach = ipx_attach,
96 .pru_connect = ipx_connect,
97 .pru_connect2 = pr_generic_notsupp,
98 .pru_control = ipx_control,
99 .pru_detach = ipx_detach,
100 .pru_disconnect = ipx_disconnect,
101 .pru_listen = pr_generic_notsupp,
102 .pru_peeraddr = ipx_peeraddr,
103 .pru_rcvd = pr_generic_notsupp,
104 .pru_rcvoob = pr_generic_notsupp,
105 .pru_send = ipx_send,
106 .pru_sense = pru_sense_null,
107 .pru_shutdown = ipx_shutdown,
108 .pru_sockaddr = ipx_sockaddr,
109 .pru_sosend = sosend,
110 .pru_soreceive = soreceive
113 struct pr_usrreqs ripx_usrreqs = {
114 .pru_abort = ipx_usr_abort,
115 .pru_accept = pr_generic_notsupp,
116 .pru_attach = ripx_attach,
117 .pru_bind = ipx_bind,
118 .pru_connect = ipx_connect,
119 .pru_connect2 = pr_generic_notsupp,
120 .pru_control = ipx_control,
121 .pru_detach = ipx_detach,
122 .pru_disconnect = ipx_disconnect,
123 .pru_listen = pr_generic_notsupp,
124 .pru_peeraddr = ipx_peeraddr,
125 .pru_rcvd = pr_generic_notsupp,
126 .pru_rcvoob = pr_generic_notsupp,
127 .pru_send = ipx_send,
128 .pru_sense = pru_sense_null,
129 .pru_shutdown = ipx_shutdown,
130 .pru_sockaddr = ipx_sockaddr,
131 .pru_sosend = sosend,
132 .pru_soreceive = soreceive
136 * This may also be called for raw listeners.
139 ipx_input(struct mbuf *m, struct ipxpcb *ipxp)
141 struct ipx *ipx = mtod(m, struct ipx *);
142 struct ifnet *ifp = m->m_pkthdr.rcvif;
143 struct sockaddr_ipx ipx_ipx;
148 * Construct sockaddr format source address.
149 * Stuff source address and datagram in user buffer.
151 ipx_ipx.sipx_len = sizeof(ipx_ipx);
152 ipx_ipx.sipx_family = AF_IPX;
153 ipx_ipx.sipx_addr = ipx->ipx_sna;
154 ipx_ipx.sipx_zero[0] = '\0';
155 ipx_ipx.sipx_zero[1] = '\0';
156 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
157 struct ifaddr_container *ifac;
159 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
160 struct ifaddr *ifa = ifac->ifa;
162 if (ifa->ifa_addr->sa_family == AF_IPX) {
163 ipx_ipx.sipx_addr.x_net =
164 IA_SIPX(ifa)->sipx_addr.x_net;
169 ipxp->ipxp_rpt = ipx->ipx_pt;
170 if (!(ipxp->ipxp_flags & IPXP_RAWIN) ) {
171 m->m_len -= sizeof(struct ipx);
172 m->m_pkthdr.len -= sizeof(struct ipx);
173 m->m_data += sizeof(struct ipx);
175 if (ssb_appendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
178 sorwakeup(ipxp->ipxp_socket);
185 ipx_abort(struct ipxpcb *ipxp)
187 struct socket *so = ipxp->ipxp_socket;
190 ipx_pcbdisconnect(ipxp);
191 soisdisconnected(so);
196 * Drop connection, reporting
197 * the specified error.
200 ipx_drop(struct ipxpcb *ipxp, int errno)
202 struct socket *so = ipxp->ipxp_socket;
205 * someday, in the IPX world
206 * we will generate error protocol packets
207 * announcing that the socket has gone away.
209 * XXX Probably never. IPX does not have error packets.
211 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
212 tp->t_state = TCPS_CLOSED;
215 so->so_error = errno;
217 ipx_pcbdisconnect(ipxp);
218 soisdisconnected(so);
223 ipx_output(struct ipxpcb *ipxp, struct mbuf *m0)
230 struct mbuf *mprev = NULL;
233 * Calculate data length.
235 for (m = m0; m != NULL; m = m->m_next) {
240 * Make sure packet is actually of even length.
245 if ((m->m_flags & M_EXT) == 0 &&
246 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
247 mtod(m, char*)[m->m_len++] = 0;
249 struct mbuf *m1 = m_get(MB_DONTWAIT, MT_DATA);
256 * mtod(m1, char *) = 0;
263 * Fill in mbuf with extended IPX header
264 * and addresses and length put into network format.
267 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
268 ipx = mtod(m, struct ipx *);
270 M_PREPEND(m, sizeof(struct ipx), MB_DONTWAIT);
273 ipx = mtod(m, struct ipx *);
275 ipx->ipx_pt = ipxp->ipxp_dpt;
276 ipx->ipx_sna = ipxp->ipxp_laddr;
277 ipx->ipx_dna = ipxp->ipxp_faddr;
278 len += sizeof(struct ipx);
281 ipx->ipx_len = htons((u_short)len);
283 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
284 ipx->ipx_sum = ipx_cksum(m, len);
286 ipx->ipx_sum = 0xffff;
291 so = ipxp->ipxp_socket;
292 if (so->so_options & SO_DONTROUTE)
293 return (ipx_outputfl(m, NULL,
294 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
296 * Use cached route for previous datagram if
297 * possible. If the previous net was the same
298 * and the interface was a broadcast medium, or
299 * if the previous destination was identical,
302 * NB: We don't handle broadcasts because that
303 * would require 3 subroutine calls.
305 ro = &ipxp->ipxp_route;
306 #ifdef ancient_history
308 * I think that this will all be handled in ipx_pcbconnect!
310 if (ro->ro_rt != NULL) {
311 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
313 * This assumes we have no GH type routes
315 if (ro->ro_rt->rt_flags & RTF_HOST) {
316 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
320 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
321 struct ipx_addr *dst =
322 &satoipx_addr(ro->ro_dst);
323 dst->x_host = ipx->ipx_dna.x_host;
326 * Otherwise, we go through the same gateway
327 * and dst is already set up.
335 ipxp->ipxp_lastdst = ipx->ipx_dna;
336 #endif /* ancient_history */
337 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
341 ipx_ctloutput(netmsg_t msg)
343 struct socket *so = msg->base.nm_so;
344 struct sockopt *sopt = msg->ctloutput.nm_sopt;
345 struct ipxpcb *ipxp = sotoipxpcb(so);
346 int mask, error, optval;
356 switch (sopt->sopt_dir) {
358 switch (sopt->sopt_name) {
360 mask = IPXP_ALL_PACKETS;
363 case SO_HEADERS_ON_INPUT:
367 case SO_IPX_CHECKSUM:
368 mask = IPXP_CHECKSUM;
371 case SO_HEADERS_ON_OUTPUT:
374 soptval = ipxp->ipxp_flags & mask;
375 error = sooptcopyout(sopt, &soptval, sizeof soptval);
378 case SO_DEFAULT_HEADERS:
382 ioptval.ipx_pt = ipxp->ipxp_dpt;
383 ioptval.ipx_dna = ipxp->ipxp_faddr;
384 ioptval.ipx_sna = ipxp->ipxp_laddr;
385 error = sooptcopyout(sopt, &soptval, sizeof soptval);
389 error = sooptcopyout(sopt, &ipx_pexseq,
400 switch (sopt->sopt_name) {
402 mask = IPXP_ALL_PACKETS;
405 case SO_HEADERS_ON_INPUT:
409 case SO_IPX_CHECKSUM:
410 mask = IPXP_CHECKSUM;
413 case SO_HEADERS_ON_OUTPUT:
416 error = sooptcopyin(sopt, &optval, sizeof optval,
421 ipxp->ipxp_flags |= mask;
423 ipxp->ipxp_flags &= ~mask;
426 case SO_DEFAULT_HEADERS:
427 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
431 ipxp->ipxp_dpt = ioptval.ipx_pt;
435 error = ipxip_route(so, sopt);
440 case SO_IPXTUNNEL_ROUTE:
441 error = ipxtun_route(so, sopt);
451 lwkt_replymsg(&msg->lmsg, error);
455 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
456 * will sofree() it when we return.
459 ipx_usr_abort(netmsg_t msg)
461 struct socket *so = msg->base.nm_so;
462 struct ipxpcb *ipxp = sotoipxpcb(so);
465 soisdisconnected(so);
467 lwkt_replymsg(&msg->lmsg, 0);
471 ipx_attach(netmsg_t msg)
473 struct socket *so = msg->base.nm_so;
474 struct pru_attach_info *ai = msg->attach.nm_ai;
475 struct ipxpcb *ipxp = sotoipxpcb(so);
481 error = ipx_pcballoc(so, &ipxpcb_list);
483 error = soreserve(so, ipxsendspace, ipxrecvspace,
487 lwkt_replymsg(&msg->lmsg, error);
491 ipx_bind(netmsg_t msg)
493 struct socket *so = msg->base.nm_so;
494 struct ipxpcb *ipxp = sotoipxpcb(so);
497 error = ipx_pcbbind(ipxp, msg->bind.nm_nam, msg->bind.nm_td);
498 lwkt_replymsg(&msg->lmsg, error);
502 ipx_connect(netmsg_t msg)
504 struct socket *so = msg->base.nm_so;
505 struct ipxpcb *ipxp = sotoipxpcb(so);
508 if (ipx_nullhost(ipxp->ipxp_faddr)) {
509 error = ipx_pcbconnect(ipxp,
517 lwkt_replymsg(&msg->lmsg, error);
521 ipx_control(netmsg_t msg)
525 error = ipx_control_oncpu(msg->base.nm_so,
527 msg->control.nm_data,
530 lwkt_replymsg(&msg->lmsg, error);
534 ipx_detach(netmsg_t msg)
536 struct socket *so = msg->base.nm_so;
537 struct ipxpcb *ipxp = sotoipxpcb(so);
546 lwkt_replymsg(&msg->lmsg, error);
550 ipx_disconnect(netmsg_t msg)
552 struct socket *so = msg->base.nm_so;
553 struct ipxpcb *ipxp = sotoipxpcb(so);
556 if (ipx_nullhost(ipxp->ipxp_faddr)) {
560 ipx_pcbdisconnect(ipxp);
561 soisdisconnected(so);
565 lwkt_replymsg(&msg->lmsg, error);
569 ipx_peeraddr(netmsg_t msg)
571 struct socket *so = msg->base.nm_so;
572 struct ipxpcb *ipxp = sotoipxpcb(so);
574 ipx_setpeeraddr(ipxp, msg->peeraddr.nm_nam);
575 lwkt_replymsg(&msg->lmsg, 0);
579 ipx_send(netmsg_t msg)
581 struct socket *so = msg->base.nm_so;
582 struct mbuf *m = msg->send.nm_m;
583 struct sockaddr *nam = msg->send.nm_addr;
584 struct mbuf *control = msg->send.nm_control;
585 struct thread *td = msg->send.nm_td;
586 struct ipxpcb *ipxp = sotoipxpcb(so);
587 struct ipx_addr laddr;
596 laddr = ipxp->ipxp_laddr;
597 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
602 * Must block input while temporarily connected.
604 error = ipx_pcbconnect(ipxp, nam, td);
609 if (ipx_nullhost(ipxp->ipxp_faddr)) {
614 error = ipx_output(ipxp, m);
617 ipx_pcbdisconnect(ipxp);
618 ipxp->ipxp_laddr = laddr;
623 lwkt_replymsg(&msg->lmsg, error);
627 ipx_shutdown(netmsg_t msg)
629 struct socket *so = msg->base.nm_so;
633 lwkt_replymsg(&msg->lmsg, 0);
637 ipx_sockaddr(netmsg_t msg)
639 struct socket *so = msg->base.nm_so;
640 struct ipxpcb *ipxp = sotoipxpcb(so);
642 /* XXX what if alloc fails? */
643 ipx_setsockaddr(ipxp, msg->sockaddr.nm_nam);
644 lwkt_replymsg(&msg->lmsg, 0);
648 ripx_attach(netmsg_t msg)
650 struct socket *so = msg->base.nm_so;
651 struct pru_attach_info *ai = msg->attach.nm_ai;
655 error = priv_check_cred(ai->p_ucred, PRIV_ROOT, NULL_CRED_OKAY);
658 error = ipx_pcballoc(so, &ipxrawpcb_list);
661 error = soreserve(so, ipxsendspace, ipxrecvspace, ai->sb_rlimit);
664 ipxp = sotoipxpcb(so);
665 ipxp->ipxp_faddr.x_host = ipx_broadhost;
666 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;
668 lwkt_replymsg(&msg->lmsg, error);