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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/netipx/ipx_usrreq.c,v 1.26.2.1 2001/02/22 09:44:18 bp Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/socketvar2.h>
47 #include <sys/sysctl.h>
49 #include <sys/thread2.h>
50 #include <sys/msgport2.h>
53 #include <net/route.h>
55 #include <netinet/in.h>
64 * IPX protocol implementation.
67 static int ipxsendspace = IPXSNDQ;
68 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
69 &ipxsendspace, 0, "");
70 static int ipxrecvspace = IPXRCVQ;
71 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
72 &ipxrecvspace, 0, "");
74 static void ipx_usr_abort(netmsg_t msg);
75 static void ipx_attach(netmsg_t msg);
76 static void ipx_bind(netmsg_t msg);
77 static void ipx_connect(netmsg_t msg);
78 /*static void ipx_control(netmsg_t msg);*/
79 static void ipx_detach(netmsg_t msg);
80 static void ipx_disconnect(netmsg_t msg);
81 /*static void ipx_peeraddr(netmsg_t msg);*/
82 static void ipx_send(netmsg_t msg);
83 static void ipx_shutdown(netmsg_t msg);
84 /*static void ipx_sockaddr(netmsg_t msg);*/
85 static void ripx_attach(netmsg_t msg);
87 struct pr_usrreqs ipx_usrreqs = {
88 .pru_abort = ipx_usr_abort,
89 .pru_accept = pr_generic_notsupp,
90 .pru_attach = ipx_attach,
92 .pru_connect = ipx_connect,
93 .pru_connect2 = pr_generic_notsupp,
94 .pru_control = ipx_control,
95 .pru_detach = ipx_detach,
96 .pru_disconnect = ipx_disconnect,
97 .pru_listen = pr_generic_notsupp,
98 .pru_peeraddr = ipx_peeraddr,
99 .pru_rcvd = pr_generic_notsupp,
100 .pru_rcvoob = pr_generic_notsupp,
101 .pru_send = ipx_send,
102 .pru_sense = pru_sense_null,
103 .pru_shutdown = ipx_shutdown,
104 .pru_sockaddr = ipx_sockaddr,
105 .pru_sosend = sosend,
106 .pru_soreceive = soreceive
109 struct pr_usrreqs ripx_usrreqs = {
110 .pru_abort = ipx_usr_abort,
111 .pru_accept = pr_generic_notsupp,
112 .pru_attach = ripx_attach,
113 .pru_bind = ipx_bind,
114 .pru_connect = ipx_connect,
115 .pru_connect2 = pr_generic_notsupp,
116 .pru_control = ipx_control,
117 .pru_detach = ipx_detach,
118 .pru_disconnect = ipx_disconnect,
119 .pru_listen = pr_generic_notsupp,
120 .pru_peeraddr = ipx_peeraddr,
121 .pru_rcvd = pr_generic_notsupp,
122 .pru_rcvoob = pr_generic_notsupp,
123 .pru_send = ipx_send,
124 .pru_sense = pru_sense_null,
125 .pru_shutdown = ipx_shutdown,
126 .pru_sockaddr = ipx_sockaddr,
127 .pru_sosend = sosend,
128 .pru_soreceive = soreceive
132 * This may also be called for raw listeners.
135 ipx_input(struct mbuf *m, struct ipxpcb *ipxp)
137 struct ipx *ipx = mtod(m, struct ipx *);
138 struct ifnet *ifp = m->m_pkthdr.rcvif;
139 struct sockaddr_ipx ipx_ipx;
144 * Construct sockaddr format source address.
145 * Stuff source address and datagram in user buffer.
147 ipx_ipx.sipx_len = sizeof(ipx_ipx);
148 ipx_ipx.sipx_family = AF_IPX;
149 ipx_ipx.sipx_addr = ipx->ipx_sna;
150 ipx_ipx.sipx_zero[0] = '\0';
151 ipx_ipx.sipx_zero[1] = '\0';
152 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
153 struct ifaddr_container *ifac;
155 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
156 struct ifaddr *ifa = ifac->ifa;
158 if (ifa->ifa_addr->sa_family == AF_IPX) {
159 ipx_ipx.sipx_addr.x_net =
160 IA_SIPX(ifa)->sipx_addr.x_net;
165 ipxp->ipxp_rpt = ipx->ipx_pt;
166 if (!(ipxp->ipxp_flags & IPXP_RAWIN) ) {
167 m->m_len -= sizeof(struct ipx);
168 m->m_pkthdr.len -= sizeof(struct ipx);
169 m->m_data += sizeof(struct ipx);
171 if (ssb_appendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
174 sorwakeup(ipxp->ipxp_socket);
181 ipx_abort(struct ipxpcb *ipxp)
183 struct socket *so = ipxp->ipxp_socket;
186 ipx_pcbdisconnect(ipxp);
187 soisdisconnected(so);
192 * Drop connection, reporting
193 * the specified error.
196 ipx_drop(struct ipxpcb *ipxp, int error)
198 struct socket *so = ipxp->ipxp_socket;
201 * someday, in the IPX world
202 * we will generate error protocol packets
203 * announcing that the socket has gone away.
205 * XXX Probably never. IPX does not have error packets.
207 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
208 tp->t_state = TCPS_CLOSED;
211 so->so_error = error;
213 ipx_pcbdisconnect(ipxp);
214 soisdisconnected(so);
219 ipx_output(struct ipxpcb *ipxp, struct mbuf *m0)
226 struct mbuf *mprev = NULL;
229 * Calculate data length.
231 for (m = m0; m != NULL; m = m->m_next) {
236 * Make sure packet is actually of even length.
241 if ((m->m_flags & M_EXT) == 0 &&
242 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
243 mtod(m, char*)[m->m_len++] = 0;
245 struct mbuf *m1 = m_get(MB_DONTWAIT, MT_DATA);
252 * mtod(m1, char *) = 0;
259 * Fill in mbuf with extended IPX header
260 * and addresses and length put into network format.
263 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
264 ipx = mtod(m, struct ipx *);
266 M_PREPEND(m, sizeof(struct ipx), MB_DONTWAIT);
269 ipx = mtod(m, struct ipx *);
271 ipx->ipx_pt = ipxp->ipxp_dpt;
272 ipx->ipx_sna = ipxp->ipxp_laddr;
273 ipx->ipx_dna = ipxp->ipxp_faddr;
274 len += sizeof(struct ipx);
277 ipx->ipx_len = htons((u_short)len);
279 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
280 ipx->ipx_sum = ipx_cksum(m, len);
282 ipx->ipx_sum = 0xffff;
287 so = ipxp->ipxp_socket;
288 if (so->so_options & SO_DONTROUTE)
289 return (ipx_outputfl(m, NULL,
290 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
292 * Use cached route for previous datagram if
293 * possible. If the previous net was the same
294 * and the interface was a broadcast medium, or
295 * if the previous destination was identical,
298 * NB: We don't handle broadcasts because that
299 * would require 3 subroutine calls.
301 ro = &ipxp->ipxp_route;
302 #ifdef ancient_history
304 * I think that this will all be handled in ipx_pcbconnect!
306 if (ro->ro_rt != NULL) {
307 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
309 * This assumes we have no GH type routes
311 if (ro->ro_rt->rt_flags & RTF_HOST) {
312 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
316 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
317 struct ipx_addr *dst =
318 &satoipx_addr(ro->ro_dst);
319 dst->x_host = ipx->ipx_dna.x_host;
322 * Otherwise, we go through the same gateway
323 * and dst is already set up.
331 ipxp->ipxp_lastdst = ipx->ipx_dna;
332 #endif /* ancient_history */
333 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
337 ipx_ctloutput(netmsg_t msg)
339 struct socket *so = msg->base.nm_so;
340 struct sockopt *sopt = msg->ctloutput.nm_sopt;
341 struct ipxpcb *ipxp = sotoipxpcb(so);
342 int mask, error, optval;
352 switch (sopt->sopt_dir) {
354 switch (sopt->sopt_name) {
356 mask = IPXP_ALL_PACKETS;
359 case SO_HEADERS_ON_INPUT:
363 case SO_IPX_CHECKSUM:
364 mask = IPXP_CHECKSUM;
367 case SO_HEADERS_ON_OUTPUT:
370 soptval = ipxp->ipxp_flags & mask;
371 error = sooptcopyout(sopt, &soptval, sizeof soptval);
374 case SO_DEFAULT_HEADERS:
378 ioptval.ipx_pt = ipxp->ipxp_dpt;
379 ioptval.ipx_dna = ipxp->ipxp_faddr;
380 ioptval.ipx_sna = ipxp->ipxp_laddr;
381 error = sooptcopyout(sopt, &soptval, sizeof soptval);
385 error = sooptcopyout(sopt, &ipx_pexseq,
396 switch (sopt->sopt_name) {
398 mask = IPXP_ALL_PACKETS;
401 case SO_HEADERS_ON_INPUT:
405 case SO_IPX_CHECKSUM:
406 mask = IPXP_CHECKSUM;
409 case SO_HEADERS_ON_OUTPUT:
412 error = sooptcopyin(sopt, &optval, sizeof optval,
417 ipxp->ipxp_flags |= mask;
419 ipxp->ipxp_flags &= ~mask;
422 case SO_DEFAULT_HEADERS:
423 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
427 ipxp->ipxp_dpt = ioptval.ipx_pt;
431 error = ipxip_route(so, sopt);
436 case SO_IPXTUNNEL_ROUTE:
437 error = ipxtun_route(so, sopt);
447 lwkt_replymsg(&msg->lmsg, error);
451 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
452 * will sofree() it when we return.
455 ipx_usr_abort(netmsg_t msg)
457 struct socket *so = msg->base.nm_so;
458 struct ipxpcb *ipxp = sotoipxpcb(so);
461 soisdisconnected(so);
463 lwkt_replymsg(&msg->lmsg, 0);
467 ipx_attach(netmsg_t msg)
469 struct socket *so = msg->base.nm_so;
470 struct pru_attach_info *ai = msg->attach.nm_ai;
471 struct ipxpcb *ipxp = sotoipxpcb(so);
477 error = ipx_pcballoc(so, &ipxpcb_list);
479 error = soreserve(so, ipxsendspace, ipxrecvspace,
483 lwkt_replymsg(&msg->lmsg, error);
487 ipx_bind(netmsg_t msg)
489 struct socket *so = msg->base.nm_so;
490 struct ipxpcb *ipxp = sotoipxpcb(so);
493 error = ipx_pcbbind(ipxp, msg->bind.nm_nam, msg->bind.nm_td);
494 lwkt_replymsg(&msg->lmsg, error);
498 ipx_connect(netmsg_t msg)
500 struct socket *so = msg->base.nm_so;
501 struct ipxpcb *ipxp = sotoipxpcb(so);
504 if (ipx_nullhost(ipxp->ipxp_faddr)) {
505 error = ipx_pcbconnect(ipxp,
513 lwkt_replymsg(&msg->lmsg, error);
517 ipx_control(netmsg_t msg)
521 error = ipx_control_oncpu(msg->base.nm_so,
523 msg->control.nm_data,
526 lwkt_replymsg(&msg->lmsg, error);
530 ipx_detach(netmsg_t msg)
532 struct socket *so = msg->base.nm_so;
533 struct ipxpcb *ipxp = sotoipxpcb(so);
542 lwkt_replymsg(&msg->lmsg, error);
546 ipx_disconnect(netmsg_t msg)
548 struct socket *so = msg->base.nm_so;
549 struct ipxpcb *ipxp = sotoipxpcb(so);
552 if (ipx_nullhost(ipxp->ipxp_faddr)) {
556 ipx_pcbdisconnect(ipxp);
557 soisdisconnected(so);
561 lwkt_replymsg(&msg->lmsg, error);
565 ipx_peeraddr(netmsg_t msg)
567 struct socket *so = msg->base.nm_so;
568 struct ipxpcb *ipxp = sotoipxpcb(so);
570 ipx_setpeeraddr(ipxp, msg->peeraddr.nm_nam);
571 lwkt_replymsg(&msg->lmsg, 0);
575 ipx_send(netmsg_t msg)
577 struct socket *so = msg->base.nm_so;
578 struct mbuf *m = msg->send.nm_m;
579 struct sockaddr *nam = msg->send.nm_addr;
580 struct mbuf *control = msg->send.nm_control;
581 struct thread *td = msg->send.nm_td;
582 struct ipxpcb *ipxp = sotoipxpcb(so);
583 struct ipx_addr laddr;
592 laddr = ipxp->ipxp_laddr;
593 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
598 * Must block input while temporarily connected.
600 error = ipx_pcbconnect(ipxp, nam, td);
605 if (ipx_nullhost(ipxp->ipxp_faddr)) {
610 error = ipx_output(ipxp, m);
613 ipx_pcbdisconnect(ipxp);
614 ipxp->ipxp_laddr = laddr;
619 lwkt_replymsg(&msg->lmsg, error);
623 ipx_shutdown(netmsg_t msg)
625 struct socket *so = msg->base.nm_so;
629 lwkt_replymsg(&msg->lmsg, 0);
633 ipx_sockaddr(netmsg_t msg)
635 struct socket *so = msg->base.nm_so;
636 struct ipxpcb *ipxp = sotoipxpcb(so);
638 /* XXX what if alloc fails? */
639 ipx_setsockaddr(ipxp, msg->sockaddr.nm_nam);
640 lwkt_replymsg(&msg->lmsg, 0);
644 ripx_attach(netmsg_t msg)
646 struct socket *so = msg->base.nm_so;
647 struct pru_attach_info *ai = msg->attach.nm_ai;
651 error = priv_check_cred(ai->p_ucred, PRIV_ROOT, NULL_CRED_OKAY);
654 error = ipx_pcballoc(so, &ipxrawpcb_list);
657 error = soreserve(so, ipxsendspace, ipxrecvspace, ai->sb_rlimit);
660 ipxp = sotoipxpcb(so);
661 ipxp->ipxp_faddr.x_host = ipx_broadhost;
662 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;
664 lwkt_replymsg(&msg->lmsg, error);