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 $
37 * $DragonFly: src/sys/netproto/ipx/ipx_usrreq.c,v 1.5 2003/08/07 21:17:37 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.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 int ipx_usr_abort(struct socket *so);
75 static int ipx_attach(struct socket *so, int proto, struct thread *td);
76 static int ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
77 static int ipx_connect(struct socket *so, struct sockaddr *nam,
79 static int ipx_detach(struct socket *so);
80 static int ipx_disconnect(struct socket *so);
81 static int ipx_send(struct socket *so, int flags, struct mbuf *m,
82 struct sockaddr *addr, struct mbuf *control,
84 static int ipx_shutdown(struct socket *so);
85 static int ripx_attach(struct socket *so, int proto, struct thread *td);
86 static int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
88 struct 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, sosend, soreceive, sopoll
96 struct 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, sosend, soreceive, sopoll
105 * This may also be called for raw listeners.
112 struct ipx *ipx = mtod(m, struct ipx *);
113 struct ifnet *ifp = m->m_pkthdr.rcvif;
114 struct sockaddr_ipx ipx_ipx;
119 * Construct sockaddr format source address.
120 * Stuff source address and datagram in user buffer.
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 != NULL) {
130 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
131 ifa = TAILQ_NEXT(ifa, ifa_link)) {
132 if (ifa->ifa_addr->sa_family == AF_IPX) {
133 ipx_ipx.sipx_addr.x_net =
134 IA_SIPX(ifa)->sipx_addr.x_net;
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);
145 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
146 m, (struct mbuf *)NULL) == 0)
148 sorwakeup(ipxp->ipxp_socket);
158 struct socket *so = ipxp->ipxp_socket;
160 ipx_pcbdisconnect(ipxp);
161 soisdisconnected(so);
165 * Drop connection, reporting
166 * the specified error.
169 ipx_drop(ipxp, errno)
173 struct socket *so = ipxp->ipxp_socket;
176 * someday, in the IPX world
177 * we will generate error protocol packets
178 * announcing that the socket has gone away.
180 * XXX Probably never. IPX does not have error packets.
182 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
183 tp->t_state = TCPS_CLOSED;
186 so->so_error = errno;
187 ipx_pcbdisconnect(ipxp);
188 soisdisconnected(so);
201 struct mbuf *mprev = NULL;
204 * Calculate data length.
206 for (m = m0; m != NULL; m = m->m_next) {
211 * Make sure packet is actually of even length.
216 if ((m->m_flags & M_EXT) == 0 &&
217 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
218 mtod(m, char*)[m->m_len++] = 0;
220 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
227 * mtod(m1, char *) = 0;
234 * Fill in mbuf with extended IPX header
235 * and addresses and length put into network format.
238 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
239 ipx = mtod(m, struct ipx *);
241 M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
244 ipx = mtod(m, struct ipx *);
246 ipx->ipx_pt = ipxp->ipxp_dpt;
247 ipx->ipx_sna = ipxp->ipxp_laddr;
248 ipx->ipx_dna = ipxp->ipxp_faddr;
249 len += sizeof(struct ipx);
252 ipx->ipx_len = htons((u_short)len);
254 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
255 ipx->ipx_sum = ipx_cksum(m, len);
257 ipx->ipx_sum = 0xffff;
262 so = ipxp->ipxp_socket;
263 if (so->so_options & SO_DONTROUTE)
264 return (ipx_outputfl(m, (struct route *)NULL,
265 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
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,
273 * NB: We don't handle broadcasts because that
274 * would require 3 subroutine calls.
276 ro = &ipxp->ipxp_route;
277 #ifdef ancient_history
279 * I think that this will all be handled in ipx_pcbconnect!
281 if (ro->ro_rt != NULL) {
282 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
284 * This assumes we have no GH type routes
286 if (ro->ro_rt->rt_flags & RTF_HOST) {
287 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
291 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
292 struct ipx_addr *dst =
293 &satoipx_addr(ro->ro_dst);
294 dst->x_host = ipx->ipx_dna.x_host;
297 * Otherwise, we go through the same gateway
298 * and dst is already set up.
306 ipxp->ipxp_lastdst = ipx->ipx_dna;
307 #endif /* ancient_history */
308 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
312 ipx_ctloutput(so, sopt)
314 struct sockopt *sopt;
316 struct ipxpcb *ipxp = sotoipxpcb(so);
317 int mask, error, optval;
325 switch (sopt->sopt_dir) {
327 switch (sopt->sopt_name) {
329 mask = IPXP_ALL_PACKETS;
332 case SO_HEADERS_ON_INPUT:
336 case SO_IPX_CHECKSUM:
337 mask = IPXP_CHECKSUM;
340 case SO_HEADERS_ON_OUTPUT:
343 soptval = ipxp->ipxp_flags & mask;
344 error = sooptcopyout(sopt, &soptval, sizeof soptval);
347 case SO_DEFAULT_HEADERS:
351 ioptval.ipx_pt = ipxp->ipxp_dpt;
352 ioptval.ipx_dna = ipxp->ipxp_faddr;
353 ioptval.ipx_sna = ipxp->ipxp_laddr;
354 error = sooptcopyout(sopt, &soptval, sizeof soptval);
358 error = sooptcopyout(sopt, &ipx_pexseq,
369 switch (sopt->sopt_name) {
371 mask = IPXP_ALL_PACKETS;
374 case SO_HEADERS_ON_INPUT:
378 case SO_IPX_CHECKSUM:
379 mask = IPXP_CHECKSUM;
381 case SO_HEADERS_ON_OUTPUT:
384 error = sooptcopyin(sopt, &optval, sizeof optval,
389 ipxp->ipxp_flags |= mask;
391 ipxp->ipxp_flags &= ~mask;
394 case SO_DEFAULT_HEADERS:
395 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
399 ipxp->ipxp_dpt = ioptval.ipx_pt;
403 error = ipxip_route(so, sopt);
408 case SO_IPXTUNNEL_ROUTE:
409 error = ipxtun_route(so, sopt);
426 struct ipxpcb *ipxp = sotoipxpcb(so);
432 soisdisconnected(so);
437 ipx_attach(struct socket *so, int proto, struct thread *td)
441 struct ipxpcb *ipxp = sotoipxpcb(so);
446 error = ipx_pcballoc(so, &ipxpcb, td);
449 error = soreserve(so, ipxsendspace, ipxrecvspace);
454 ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
456 struct ipxpcb *ipxp = sotoipxpcb(so);
458 return (ipx_pcbbind(ipxp, nam, td));
462 ipx_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
466 struct ipxpcb *ipxp = sotoipxpcb(so);
468 if (!ipx_nullhost(ipxp->ipxp_faddr))
471 error = ipx_pcbconnect(ipxp, nam, td);
483 struct ipxpcb *ipxp = sotoipxpcb(so);
498 struct ipxpcb *ipxp = sotoipxpcb(so);
500 if (ipx_nullhost(ipxp->ipxp_faddr))
503 ipx_pcbdisconnect(ipxp);
505 soisdisconnected(so);
510 ipx_peeraddr(so, nam)
512 struct sockaddr **nam;
514 struct ipxpcb *ipxp = sotoipxpcb(so);
516 ipx_setpeeraddr(ipxp, nam); /* XXX what if alloc fails? */
521 ipx_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
522 struct mbuf *control, struct thread *td)
525 struct ipxpcb *ipxp = sotoipxpcb(so);
526 struct ipx_addr laddr;
530 laddr = ipxp->ipxp_laddr;
531 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
536 * Must block input while temporarily connected.
539 error = ipx_pcbconnect(ipxp, nam, td);
545 if (ipx_nullhost(ipxp->ipxp_faddr)) {
550 error = ipx_output(ipxp, m);
553 ipx_pcbdisconnect(ipxp);
555 ipxp->ipxp_laddr = laddr;
573 ipx_sockaddr(so, nam)
575 struct sockaddr **nam;
577 struct ipxpcb *ipxp = sotoipxpcb(so);
579 ipx_setsockaddr(ipxp, nam); /* XXX what if alloc fails? */
584 ripx_attach(struct socket *so, int proto, struct thread *td)
588 struct ipxpcb *ipxp = sotoipxpcb(so);
590 if ((error = suser(td)) != 0)
593 error = ipx_pcballoc(so, &ipxrawpcb, td);
597 error = soreserve(so, ipxsendspace, ipxrecvspace);
600 ipxp = sotoipxpcb(so);
601 ipxp->ipxp_faddr.x_host = ipx_broadhost;
602 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;