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
41 #include <sys/param.h>
42 #include <sys/kernel.h>
45 #include <sys/protosw.h>
46 #include <sys/signalvar.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
54 #include <net/route.h>
56 #include <netinet/in.h>
58 #include <netipx/ipx.h>
59 #include <netipx/ipx_if.h>
60 #include <netipx/ipx_ip.h>
61 #include <netipx/ipx_pcb.h>
62 #include <netipx/ipx_var.h>
65 * IPX protocol implementation.
68 static int ipxsendspace = IPXSNDQ;
69 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW,
70 &ipxsendspace, 0, "");
71 static int ipxrecvspace = IPXRCVQ;
72 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW,
73 &ipxrecvspace, 0, "");
75 static int ipx_usr_abort(struct socket *so);
76 static int ipx_attach(struct socket *so, int proto, struct thread *td);
77 static int ipx_bind(struct socket *so, struct sockaddr *nam, struct thread *td);
78 static int ipx_connect(struct socket *so, struct sockaddr *nam,
80 static int ipx_detach(struct socket *so);
81 static int ipx_disconnect(struct socket *so);
82 static int ipx_send(struct socket *so, int flags, struct mbuf *m,
83 struct sockaddr *addr, struct mbuf *control,
85 static int ipx_shutdown(struct socket *so);
86 static int ripx_attach(struct socket *so, int proto, struct thread *td);
87 static int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0);
89 struct pr_usrreqs ipx_usrreqs = {
90 ipx_usr_abort, pru_accept_notsupp, ipx_attach, ipx_bind,
91 ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
92 ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
93 pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
94 ipx_sockaddr, sosend, soreceive, sopoll
97 struct pr_usrreqs ripx_usrreqs = {
98 ipx_usr_abort, pru_accept_notsupp, ripx_attach, ipx_bind,
99 ipx_connect, pru_connect2_notsupp, ipx_control, ipx_detach,
100 ipx_disconnect, pru_listen_notsupp, ipx_peeraddr, pru_rcvd_notsupp,
101 pru_rcvoob_notsupp, ipx_send, pru_sense_null, ipx_shutdown,
102 ipx_sockaddr, sosend, soreceive, sopoll
106 * This may also be called for raw listeners.
111 register struct ipxpcb *ipxp;
113 register struct ipx *ipx = mtod(m, struct ipx *);
114 struct ifnet *ifp = m->m_pkthdr.rcvif;
115 struct sockaddr_ipx ipx_ipx;
120 * Construct sockaddr format source address.
121 * Stuff source address and datagram in user buffer.
123 ipx_ipx.sipx_len = sizeof(ipx_ipx);
124 ipx_ipx.sipx_family = AF_IPX;
125 ipx_ipx.sipx_addr = ipx->ipx_sna;
126 ipx_ipx.sipx_zero[0] = '\0';
127 ipx_ipx.sipx_zero[1] = '\0';
128 if (ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet) && ifp != NULL) {
129 register struct ifaddr *ifa;
131 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa != NULL;
132 ifa = TAILQ_NEXT(ifa, ifa_link)) {
133 if (ifa->ifa_addr->sa_family == AF_IPX) {
134 ipx_ipx.sipx_addr.x_net =
135 IA_SIPX(ifa)->sipx_addr.x_net;
140 ipxp->ipxp_rpt = ipx->ipx_pt;
141 if (!(ipxp->ipxp_flags & IPXP_RAWIN) ) {
142 m->m_len -= sizeof(struct ipx);
143 m->m_pkthdr.len -= sizeof(struct ipx);
144 m->m_data += sizeof(struct ipx);
146 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx,
147 m, (struct mbuf *)NULL) == 0)
149 sorwakeup(ipxp->ipxp_socket);
159 struct socket *so = ipxp->ipxp_socket;
161 ipx_pcbdisconnect(ipxp);
162 soisdisconnected(so);
166 * Drop connection, reporting
167 * the specified error.
170 ipx_drop(ipxp, errno)
171 register struct ipxpcb *ipxp;
174 struct socket *so = ipxp->ipxp_socket;
177 * someday, in the IPX world
178 * we will generate error protocol packets
179 * announcing that the socket has gone away.
181 * XXX Probably never. IPX does not have error packets.
183 /*if (TCPS_HAVERCVDSYN(tp->t_state)) {
184 tp->t_state = TCPS_CLOSED;
187 so->so_error = errno;
188 ipx_pcbdisconnect(ipxp);
189 soisdisconnected(so);
197 register struct ipx *ipx;
198 register struct socket *so;
199 register int len = 0;
200 register struct route *ro;
202 struct mbuf *mprev = NULL;
205 * Calculate data length.
207 for (m = m0; m != NULL; m = m->m_next) {
212 * Make sure packet is actually of even length.
217 if ((m->m_flags & M_EXT) == 0 &&
218 (m->m_len + m->m_data < &m->m_dat[MLEN])) {
219 mtod(m, char*)[m->m_len++] = 0;
221 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
228 * mtod(m1, char *) = 0;
235 * Fill in mbuf with extended IPX header
236 * and addresses and length put into network format.
239 if (ipxp->ipxp_flags & IPXP_RAWOUT) {
240 ipx = mtod(m, struct ipx *);
242 M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT);
245 ipx = mtod(m, struct ipx *);
247 ipx->ipx_pt = ipxp->ipxp_dpt;
248 ipx->ipx_sna = ipxp->ipxp_laddr;
249 ipx->ipx_dna = ipxp->ipxp_faddr;
250 len += sizeof(struct ipx);
253 ipx->ipx_len = htons((u_short)len);
255 if (ipxp->ipxp_flags & IPXP_CHECKSUM) {
256 ipx->ipx_sum = ipx_cksum(m, len);
258 ipx->ipx_sum = 0xffff;
263 so = ipxp->ipxp_socket;
264 if (so->so_options & SO_DONTROUTE)
265 return (ipx_outputfl(m, (struct route *)NULL,
266 (so->so_options & SO_BROADCAST) | IPX_ROUTETOIF));
268 * Use cached route for previous datagram if
269 * possible. If the previous net was the same
270 * and the interface was a broadcast medium, or
271 * if the previous destination was identical,
274 * NB: We don't handle broadcasts because that
275 * would require 3 subroutine calls.
277 ro = &ipxp->ipxp_route;
278 #ifdef ancient_history
280 * I think that this will all be handled in ipx_pcbconnect!
282 if (ro->ro_rt != NULL) {
283 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) {
285 * This assumes we have no GH type routes
287 if (ro->ro_rt->rt_flags & RTF_HOST) {
288 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna))
292 if ((ro->ro_rt->rt_flags & RTF_GATEWAY) == 0) {
293 register struct ipx_addr *dst =
294 &satoipx_addr(ro->ro_dst);
295 dst->x_host = ipx->ipx_dna.x_host;
298 * Otherwise, we go through the same gateway
299 * and dst is already set up.
307 ipxp->ipxp_lastdst = ipx->ipx_dna;
308 #endif /* ancient_history */
309 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST));
313 ipx_ctloutput(so, sopt)
315 struct sockopt *sopt;
317 struct ipxpcb *ipxp = sotoipxpcb(so);
318 int mask, error, optval;
326 switch (sopt->sopt_dir) {
328 switch (sopt->sopt_name) {
330 mask = IPXP_ALL_PACKETS;
333 case SO_HEADERS_ON_INPUT:
337 case SO_IPX_CHECKSUM:
338 mask = IPXP_CHECKSUM;
341 case SO_HEADERS_ON_OUTPUT:
344 soptval = ipxp->ipxp_flags & mask;
345 error = sooptcopyout(sopt, &soptval, sizeof soptval);
348 case SO_DEFAULT_HEADERS:
352 ioptval.ipx_pt = ipxp->ipxp_dpt;
353 ioptval.ipx_dna = ipxp->ipxp_faddr;
354 ioptval.ipx_sna = ipxp->ipxp_laddr;
355 error = sooptcopyout(sopt, &soptval, sizeof soptval);
359 error = sooptcopyout(sopt, &ipx_pexseq,
370 switch (sopt->sopt_name) {
372 mask = IPXP_ALL_PACKETS;
375 case SO_HEADERS_ON_INPUT:
379 case SO_IPX_CHECKSUM:
380 mask = IPXP_CHECKSUM;
382 case SO_HEADERS_ON_OUTPUT:
385 error = sooptcopyin(sopt, &optval, sizeof optval,
390 ipxp->ipxp_flags |= mask;
392 ipxp->ipxp_flags &= ~mask;
395 case SO_DEFAULT_HEADERS:
396 error = sooptcopyin(sopt, &ioptval, sizeof ioptval,
400 ipxp->ipxp_dpt = ioptval.ipx_pt;
404 error = ipxip_route(so, sopt);
409 case SO_IPXTUNNEL_ROUTE:
410 error = ipxtun_route(so, sopt);
427 struct ipxpcb *ipxp = sotoipxpcb(so);
433 soisdisconnected(so);
438 ipx_attach(so, proto, td)
445 struct ipxpcb *ipxp = sotoipxpcb(so);
450 error = ipx_pcballoc(so, &ipxpcb, td);
453 error = soreserve(so, ipxsendspace, ipxrecvspace);
458 ipx_bind(so, nam, td)
460 struct sockaddr *nam;
463 struct ipxpcb *ipxp = sotoipxpcb(so);
465 return (ipx_pcbbind(ipxp, nam, td));
469 ipx_connect(so, nam, td)
471 struct sockaddr *nam;
476 struct ipxpcb *ipxp = sotoipxpcb(so);
478 if (!ipx_nullhost(ipxp->ipxp_faddr))
481 error = ipx_pcbconnect(ipxp, nam, td);
493 struct ipxpcb *ipxp = sotoipxpcb(so);
508 struct ipxpcb *ipxp = sotoipxpcb(so);
510 if (ipx_nullhost(ipxp->ipxp_faddr))
513 ipx_pcbdisconnect(ipxp);
515 soisdisconnected(so);
520 ipx_peeraddr(so, nam)
522 struct sockaddr **nam;
524 struct ipxpcb *ipxp = sotoipxpcb(so);
526 ipx_setpeeraddr(ipxp, nam); /* XXX what if alloc fails? */
531 ipx_send(so, flags, m, nam, control, td)
535 struct sockaddr *nam;
536 struct mbuf *control;
540 struct ipxpcb *ipxp = sotoipxpcb(so);
541 struct ipx_addr laddr;
545 laddr = ipxp->ipxp_laddr;
546 if (!ipx_nullhost(ipxp->ipxp_faddr)) {
551 * Must block input while temporarily connected.
554 error = ipx_pcbconnect(ipxp, nam, td);
560 if (ipx_nullhost(ipxp->ipxp_faddr)) {
565 error = ipx_output(ipxp, m);
568 ipx_pcbdisconnect(ipxp);
570 ipxp->ipxp_laddr = laddr;
588 ipx_sockaddr(so, nam)
590 struct sockaddr **nam;
592 struct ipxpcb *ipxp = sotoipxpcb(so);
594 ipx_setsockaddr(ipxp, nam); /* XXX what if alloc fails? */
599 ripx_attach(so, proto, td)
606 struct ipxpcb *ipxp = sotoipxpcb(so);
608 if (td != NULL && (error = suser(td)) != 0)
611 error = ipx_pcballoc(so, &ipxrawpcb, td);
615 error = soreserve(so, ipxsendspace, ipxrecvspace);
618 ipxp = sotoipxpcb(so);
619 ipxp->ipxp_faddr.x_host = ipx_broadhost;
620 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT;