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>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
56 #include <netipx/ipx.h>
57 #include <netipx/ipx_pcb.h>
58 #include <netipx/ipx_if.h>
59 #include <netipx/ipx_var.h>
60 #include <netipx/ipx_ip.h>
63 * IPX protocol implementation.
66 static int ipxsendspace = IPXSNDQ;
67 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
68 &ipxsendspace, 0, "");
69 static int ipxrecvspace = IPXRCVQ;
70 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
71 &ipxrecvspace, 0, "");
73 static int ipx_usr_abort(struct socket *so);
74 static int ipx_attach(struct socket *so, int proto, struct proc *p);
75 static int ipx_bind(struct socket *so, struct sockaddr *nam, struct proc *p);
76 static int ipx_connect(struct socket *so, struct sockaddr *nam,
78 static int ipx_detach(struct socket *so);
79 static int ipx_disconnect(struct socket *so);
80 static int ipx_send(struct socket *so, int flags, struct mbuf *m,
81 struct sockaddr *addr, struct mbuf *control,
83 static int ipx_shutdown(struct socket *so);
84 static int ripx_attach(struct socket *so, int proto, struct proc *p);
85 static int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
87 struct pr_usrreqs ipx_usrreqs = {
88 ipx_usr_abort, pru_accept_notsupp, ipx_attach, ipx_bind,
89 ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
90 ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
91 pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
92 ipx_sockaddr, sosend, soreceive, sopoll
95 struct pr_usrreqs ripx_usrreqs = {
96 ipx_usr_abort, pru_accept_notsupp, ripx_attach, ipx_bind,
97 ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
98 ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
99 pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
100 ipx_sockaddr, sosend, soreceive, sopoll
104 * This may also be called for raw listeners.
109 register struct ipxpcb *ipxp;
111 register struct ipx *ipx = mtod(m, struct ipx *);
112 struct ifnet *ifp = m->m_pkthdr.rcvif;
113 struct sockaddr_ipx ipx_ipx;
118 * Construct sockaddr format source address.
119 * Stuff source address and datagram in user buffer.
121 ipx_ipx.sipx_len = sizeof(ipx_ipx);
122 ipx_ipx.sipx_family = AF_IPX;
123 ipx_ipx.sipx_addr = ipx->ipx_sna;
124 ipx_ipx.sipx_zero[0] = '\0';
125 ipx_ipx.sipx_zero[1] = '\0';
126 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
127 register struct ifaddr *ifa;
129 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
130 ifa = TAILQ_NEXT(ifa, ifa_link)) {
131 if (ifa->ifa_addr->sa_family == AF_IPX) {
132 ipx_ipx.sipx_addr.x_net =
133 IA_SIPX(ifa)->sipx_addr.x_net;
138 ipxp->ipxp_rpt = ipx->ipx_pt;
139 if (!(ipxp->ipxp_flags & IPXP_RAWIN) ) {
140 m->m_len -= sizeof(struct ipx);
141 m->m_pkthdr.len -= sizeof(struct ipx);
142 m->m_data += sizeof(struct ipx);
144 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
145 m, (struct mbuf *)NULL) == 0)
147 sorwakeup(ipxp->ipxp_socket);
157 struct socket *so = ipxp->ipxp_socket;
159 ipx_pcbdisconnect(ipxp);
160 soisdisconnected(so);
164 * Drop connection, reporting
165 * the specified error.
168 ipx_drop(ipxp, errno)
169 register struct ipxpcb *ipxp;
172 struct socket *so = ipxp->ipxp_socket;
175 * someday, in the IPX world
176 * we will generate error protocol packets
177 * announcing that the socket has gone away.
179 * XXX Probably never. IPX does not have error packets.
181 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
182 tp->t_state = TCPS_CLOSED;
185 so->so_error = errno;
186 ipx_pcbdisconnect(ipxp);
187 soisdisconnected(so);
195 register struct ipx *ipx;
196 register struct socket *so;
197 register int len = 0;
198 register struct route *ro;
200 struct mbuf *mprev = NULL;
203 * Calculate data length.
205 for (m = m0; m != NULL; m = m->m_next) {
210 * Make sure packet is actually of even length.
215 if ((m->m_flags & M_EXT) == 0 &&
216 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
217 mtod(m, char*)[m->m_len++] = 0;
219 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
226 * mtod(m1, char *) = 0;
233 * Fill in mbuf with extended IPX header
234 * and addresses and length put into network format.
237 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
238 ipx = mtod(m, struct ipx *);
240 M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
243 ipx = mtod(m, struct ipx *);
245 ipx->ipx_pt = ipxp->ipxp_dpt;
246 ipx->ipx_sna = ipxp->ipxp_laddr;
247 ipx->ipx_dna = ipxp->ipxp_faddr;
248 len += sizeof(struct ipx);
251 ipx->ipx_len = htons((u_short)len);
253 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
254 ipx->ipx_sum = ipx_cksum(m, len);
256 ipx->ipx_sum = 0xffff;
261 so = ipxp->ipxp_socket;
262 if (so->so_options & SO_DONTROUTE)
263 return (ipx_outputfl(m, (struct route *)NULL,
264 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
266 * Use cached route for previous datagram if
267 * possible. If the previous net was the same
268 * and the interface was a broadcast medium, or
269 * if the previous destination was identical,
272 * NB: We don't handle broadcasts because that
273 * would require 3 subroutine calls.
275 ro = &ipxp->ipxp_route;
276 #ifdef ancient_history
278 * I think that this will all be handled in ipx_pcbconnect!
280 if (ro->ro_rt != NULL) {
281 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
283 * This assumes we have no GH type routes
285 if (ro->ro_rt->rt_flags & RTF_HOST) {
286 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
290 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
291 register struct ipx_addr *dst =
292 &satoipx_addr(ro->ro_dst);
293 dst->x_host = ipx->ipx_dna.x_host;
296 * Otherwise, we go through the same gateway
297 * and dst is already set up.
305 ipxp->ipxp_lastdst = ipx->ipx_dna;
306 #endif /* ancient_history */
307 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
311 ipx_ctloutput(so, sopt)
313 struct sockopt *sopt;
315 struct ipxpcb *ipxp = sotoipxpcb(so);
316 int mask, error, optval;
324 switch (sopt->sopt_dir) {
326 switch (sopt->sopt_name) {
328 mask = IPXP_ALL_PACKETS;
331 case SO_HEADERS_ON_INPUT:
335 case SO_IPX_CHECKSUM:
336 mask = IPXP_CHECKSUM;
339 case SO_HEADERS_ON_OUTPUT:
342 soptval = ipxp->ipxp_flags & mask;
343 error = sooptcopyout(sopt, &soptval, sizeof soptval);
346 case SO_DEFAULT_HEADERS:
350 ioptval.ipx_pt = ipxp->ipxp_dpt;
351 ioptval.ipx_dna = ipxp->ipxp_faddr;
352 ioptval.ipx_sna = ipxp->ipxp_laddr;
353 error = sooptcopyout(sopt, &soptval, sizeof soptval);
357 error = sooptcopyout(sopt, &ipx_pexseq,
368 switch (sopt->sopt_name) {
370 mask = IPXP_ALL_PACKETS;
373 case SO_HEADERS_ON_INPUT:
377 case SO_IPX_CHECKSUM:
378 mask = IPXP_CHECKSUM;
380 case SO_HEADERS_ON_OUTPUT:
383 error = sooptcopyin(sopt, &optval, sizeof optval,
388 ipxp->ipxp_flags |= mask;
390 ipxp->ipxp_flags &= ~mask;
393 case SO_DEFAULT_HEADERS:
394 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
398 ipxp->ipxp_dpt = ioptval.ipx_pt;
402 error = ipxip_route(so, sopt);
407 case SO_IPXTUNNEL_ROUTE:
408 error = ipxtun_route(so, sopt);
425 struct ipxpcb *ipxp = sotoipxpcb(so);
431 soisdisconnected(so);
436 ipx_attach(so, proto, p)
443 struct ipxpcb *ipxp = sotoipxpcb(so);
448 error = ipx_pcballoc(so, &ipxpcb, p);
451 error = soreserve(so, ipxsendspace, ipxrecvspace);
458 struct sockaddr *nam;
461 struct ipxpcb *ipxp = sotoipxpcb(so);
463 return (ipx_pcbbind(ipxp, nam, p));
467 ipx_connect(so, nam, p)
469 struct sockaddr *nam;
474 struct ipxpcb *ipxp = sotoipxpcb(so);
476 if (!ipx_nullhost(ipxp->ipxp_faddr))
479 error = ipx_pcbconnect(ipxp, nam, p);
491 struct ipxpcb *ipxp = sotoipxpcb(so);
506 struct ipxpcb *ipxp = sotoipxpcb(so);
508 if (ipx_nullhost(ipxp->ipxp_faddr))
511 ipx_pcbdisconnect(ipxp);
513 soisdisconnected(so);
518 ipx_peeraddr(so, nam)
520 struct sockaddr **nam;
522 struct ipxpcb *ipxp = sotoipxpcb(so);
524 ipx_setpeeraddr(ipxp, nam); /* XXX what if alloc fails? */
529 ipx_send(so, flags, m, nam, control, p)
533 struct sockaddr *nam;
534 struct mbuf *control;
538 struct ipxpcb *ipxp = sotoipxpcb(so);
539 struct ipx_addr laddr;
543 laddr = ipxp->ipxp_laddr;
544 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
549 * Must block input while temporarily connected.
552 error = ipx_pcbconnect(ipxp, nam, p);
558 if (ipx_nullhost(ipxp->ipxp_faddr)) {
563 error = ipx_output(ipxp, m);
566 ipx_pcbdisconnect(ipxp);
568 ipxp->ipxp_laddr = laddr;
586 ipx_sockaddr(so, nam)
588 struct sockaddr **nam;
590 struct ipxpcb *ipxp = sotoipxpcb(so);
592 ipx_setsockaddr(ipxp, nam); /* XXX what if alloc fails? */
597 ripx_attach(so, proto, p)
604 struct ipxpcb *ipxp = sotoipxpcb(so);
606 if (p != NULL && (error = suser(p)) != 0)
609 error = ipx_pcballoc(so, &ipxrawpcb, p);
613 error = soreserve(so, ipxsendspace, ipxrecvspace);
616 ipxp = sotoipxpcb(so);
617 ipxp->ipxp_faddr.x_host = ipx_broadhost;
618 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;