ipx_usrreq.c revision 31742
1/* 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. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 * 34 * @(#)ipx_usrreq.c 35 * 36 * $Id: ipx_usrreq.c,v 1.17 1997/09/14 03:10:41 peter Exp $ 37 */ 38 39#include "opt_ipx.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/kernel.h> 44#include <sys/mbuf.h> 45#include <sys/proc.h> 46#include <sys/protosw.h> 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/sysctl.h> 50 51#include <net/if.h> 52#include <net/route.h> 53 54#include <netinet/in.h> 55 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> 61 62/* 63 * IPX protocol implementation. 64 */ 65 66int ipxsendspace = IPXSNDQ; 67SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxsendspace, CTLFLAG_RW, 68 &ipxsendspace, 0, ""); 69int ipxrecvspace = IPXRCVQ; 70SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxrecvspace, CTLFLAG_RW, 71 &ipxrecvspace, 0, ""); 72 73static int ipx_usr_abort(struct socket *so); 74static int ipx_attach(struct socket *so, int proto, struct proc *p); 75static int ipx_bind(struct socket *so, struct sockaddr *nam, struct proc *p); 76static int ipx_connect(struct socket *so, struct sockaddr *nam, 77 struct proc *p); 78static int ipx_detach(struct socket *so); 79static int ipx_disconnect(struct socket *so); 80static int ipx_send(struct socket *so, int flags, struct mbuf *m, 81 struct sockaddr *addr, struct mbuf *control, 82 struct proc *p); 83static int ipx_shutdown(struct socket *so); 84static int ripx_attach(struct socket *so, int proto, struct proc *p); 85static int ipx_output(struct ipxpcb *ipxp, struct mbuf *m0); 86 87struct 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 93}; 94 95struct 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 101}; 102 103/* 104 * This may also be called for raw listeners. 105 */ 106void 107ipx_input(m, ipxp) 108 struct mbuf *m; 109 register struct ipxpcb *ipxp; 110{ 111 register struct ipx *ipx = mtod(m, struct ipx *); 112 struct ifnet *ifp = m->m_pkthdr.rcvif; 113 struct sockaddr_ipx ipx_ipx; 114 115 if (ipxp == NULL) 116 panic("No ipxpcb"); 117 /* 118 * Construct sockaddr format source address. 119 * Stuff source address and datagram in user buffer. 120 */ 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; 128 129 for (ifa = ifp->if_addrhead.tqh_first; ifa != NULL; 130 ifa = ifa->ifa_link.tqe_next) { 131 if (ifa->ifa_addr->sa_family == AF_IPX) { 132 ipx_ipx.sipx_addr.x_net = 133 IA_SIPX(ifa)->sipx_addr.x_net; 134 break; 135 } 136 } 137 } 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); 143 } 144 if (sbappendaddr(&ipxp->ipxp_socket->so_rcv, (struct sockaddr *)&ipx_ipx, 145 m, (struct mbuf *)NULL) == 0) 146 goto bad; 147 sorwakeup(ipxp->ipxp_socket); 148 return; 149bad: 150 m_freem(m); 151} 152 153void 154ipx_abort(ipxp) 155 struct ipxpcb *ipxp; 156{ 157 struct socket *so = ipxp->ipxp_socket; 158 159 ipx_pcbdisconnect(ipxp); 160 soisdisconnected(so); 161} 162 163/* 164 * Drop connection, reporting 165 * the specified error. 166 */ 167void 168ipx_drop(ipxp, errno) 169 register struct ipxpcb *ipxp; 170 int errno; 171{ 172 struct socket *so = ipxp->ipxp_socket; 173 174 /* 175 * someday, in the IPX world 176 * we will generate error protocol packets 177 * announcing that the socket has gone away. 178 * 179 * XXX Probably never. IPX does not have error packets. 180 */ 181 /*if (TCPS_HAVERCVDSYN(tp->t_state)) { 182 tp->t_state = TCPS_CLOSED; 183 tcp_output(tp); 184 }*/ 185 so->so_error = errno; 186 ipx_pcbdisconnect(ipxp); 187 soisdisconnected(so); 188} 189 190static int 191ipx_output(ipxp, m0) 192 struct ipxpcb *ipxp; 193 struct mbuf *m0; 194{ 195 register struct mbuf *m; 196 register struct ipx *ipx; 197 register struct socket *so; 198 register int len = 0; 199 register struct route *ro; 200 struct mbuf *mprev = NULL; 201 202 /* 203 * Calculate data length. 204 */ 205 for (m = m0; m != NULL; m = m->m_next) { 206 mprev = m; 207 len += m->m_len; 208 } 209 /* 210 * Make sure packet is actually of even length. 211 */ 212 213 if (len & 1) { 214 m = mprev; 215 if ((m->m_flags & M_EXT) == 0 && 216 (m->m_len + m->m_data < &m->m_dat[MLEN])) { 217 m->m_len++; 218 } else { 219 struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA); 220 221 if (m1 == NULL) { 222 m_freem(m0); 223 return (ENOBUFS); 224 } 225 m1->m_len = 1; 226 * mtod(m1, char *) = 0; 227 m->m_next = m1; 228 } 229 m0->m_pkthdr.len++; 230 } 231 232 /* 233 * Fill in mbuf with extended IPX header 234 * and addresses and length put into network format. 235 */ 236 m = m0; 237 if (ipxp->ipxp_flags & IPXP_RAWOUT) { 238 ipx = mtod(m, struct ipx *); 239 } else { 240 M_PREPEND(m, sizeof(struct ipx), M_DONTWAIT); 241 if (m == NULL) 242 return (ENOBUFS); 243 ipx = mtod(m, struct ipx *); 244 ipx->ipx_tc = 0; 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); 249 } 250 251 ipx->ipx_len = htons((u_short)len); 252 253 if (ipxcksum) { 254 ipx->ipx_sum = 0; 255 len = ((len - 1) | 1) + 1; 256 ipx->ipx_sum = ipx_cksum(m, len); 257 } else 258 ipx->ipx_sum = 0xffff; 259 260 /* 261 * Output datagram. 262 */ 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)); 267 /* 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, 272 * then we are ok. 273 * 274 * NB: We don't handle broadcasts because that 275 * would require 3 subroutine calls. 276 */ 277 ro = &ipxp->ipxp_route; 278#ifdef ancient_history 279 /* 280 * I think that this will all be handled in ipx_pcbconnect! 281 */ 282 if (ro->ro_rt != NULL) { 283 if(ipx_neteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) { 284 /* 285 * This assumes we have no GH type routes 286 */ 287 if (ro->ro_rt->rt_flags & RTF_HOST) { 288 if (!ipx_hosteq(ipxp->ipxp_lastdst, ipx->ipx_dna)) 289 goto re_route; 290 291 } 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; 296 } 297 /* 298 * Otherwise, we go through the same gateway 299 * and dst is already set up. 300 */ 301 } else { 302 re_route: 303 RTFREE(ro->ro_rt); 304 ro->ro_rt = NULL; 305 } 306 } 307 ipxp->ipxp_lastdst = ipx->ipx_dna; 308#endif /* ancient_history */ 309 return (ipx_outputfl(m, ro, so->so_options & SO_BROADCAST)); 310} 311 312int 313ipx_ctloutput(req, so, level, name, value, p) 314 int req, level; 315 struct socket *so; 316 int name; 317 struct mbuf **value; 318 struct proc *p; 319{ 320 register struct mbuf *m; 321 struct ipxpcb *ipxp = sotoipxpcb(so); 322 int mask, error = 0; 323 324 if (ipxp == NULL) 325 return (EINVAL); 326 327 switch (req) { 328 329 case PRCO_GETOPT: 330 if (value == NULL) 331 return (EINVAL); 332 m = m_get(M_DONTWAIT, MT_DATA); 333 if (m == NULL) 334 return (ENOBUFS); 335 switch (name) { 336 337 case SO_ALL_PACKETS: 338 mask = IPXP_ALL_PACKETS; 339 goto get_flags; 340 341 case SO_HEADERS_ON_INPUT: 342 mask = IPXP_RAWIN; 343 goto get_flags; 344 345 case SO_HEADERS_ON_OUTPUT: 346 mask = IPXP_RAWOUT; 347 get_flags: 348 m->m_len = sizeof(short); 349 *mtod(m, short *) = ipxp->ipxp_flags & mask; 350 break; 351 352 case SO_DEFAULT_HEADERS: 353 m->m_len = sizeof(struct ipx); 354 { 355 register struct ipx *ipx = mtod(m, struct ipx *); 356 ipx->ipx_len = 0; 357 ipx->ipx_sum = 0; 358 ipx->ipx_tc = 0; 359 ipx->ipx_pt = ipxp->ipxp_dpt; 360 ipx->ipx_dna = ipxp->ipxp_faddr; 361 ipx->ipx_sna = ipxp->ipxp_laddr; 362 } 363 break; 364 365 case SO_SEQNO: 366 m->m_len = sizeof(long); 367 *mtod(m, long *) = ipx_pexseq++; 368 break; 369 370 default: 371 error = EINVAL; 372 } 373 *value = m; 374 break; 375 376 case PRCO_SETOPT: 377 switch (name) { 378 int *ok; 379 380 case SO_ALL_PACKETS: 381 mask = IPXP_ALL_PACKETS; 382 goto set_head; 383 384 case SO_HEADERS_ON_INPUT: 385 mask = IPXP_RAWIN; 386 goto set_head; 387 388 case SO_HEADERS_ON_OUTPUT: 389 mask = IPXP_RAWOUT; 390 set_head: 391 if (value && *value) { 392 ok = mtod(*value, int *); 393 if (*ok) 394 ipxp->ipxp_flags |= mask; 395 else 396 ipxp->ipxp_flags &= ~mask; 397 } else error = EINVAL; 398 break; 399 400 case SO_DEFAULT_HEADERS: 401 { 402 register struct ipx *ipx 403 = mtod(*value, struct ipx *); 404 ipxp->ipxp_dpt = ipx->ipx_pt; 405 } 406 break; 407#ifdef IPXIP 408 case SO_IPXIP_ROUTE: 409 error = ipxip_route(so, *value, p); 410 break; 411#endif /* IPXIP */ 412#ifdef IPTUNNEL 413#if 0 414 case SO_IPXTUNNEL_ROUTE: 415 error = ipxtun_route(so, *value, p); 416 break; 417#endif 418#endif 419 default: 420 error = EINVAL; 421 } 422 if (value && *value) 423 m_freem(*value); 424 break; 425 } 426 return (error); 427} 428 429static int 430ipx_usr_abort(so) 431 struct socket *so; 432{ 433 int s; 434 struct ipxpcb *ipxp = sotoipxpcb(so); 435 436 s = splnet(); 437 ipx_pcbdetach(ipxp); 438 splx(s); 439 sofree(so); 440 soisdisconnected(so); 441 return (0); 442} 443 444static int 445ipx_attach(so, proto, p) 446 struct socket *so; 447 int proto; 448 struct proc *p; 449{ 450 int error; 451 int s; 452 struct ipxpcb *ipxp = sotoipxpcb(so); 453 454 if (ipxp != NULL) 455 return (EINVAL); 456 s = splnet(); 457 error = ipx_pcballoc(so, &ipxpcb, p); 458 splx(s); 459 if (error == 0) 460 error = soreserve(so, ipxsendspace, ipxrecvspace); 461 return (error); 462} 463 464static int 465ipx_bind(so, nam, p) 466 struct socket *so; 467 struct sockaddr *nam; 468 struct proc *p; 469{ 470 struct ipxpcb *ipxp = sotoipxpcb(so); 471 472 return (ipx_pcbbind(ipxp, nam, p)); 473} 474 475static int 476ipx_connect(so, nam, p) 477 struct socket *so; 478 struct sockaddr *nam; 479 struct proc *p; 480{ 481 int error; 482 int s; 483 struct ipxpcb *ipxp = sotoipxpcb(so); 484 485 if (!ipx_nullhost(ipxp->ipxp_faddr)) 486 return (EISCONN); 487 s = splnet(); 488 error = ipx_pcbconnect(ipxp, nam, p); 489 splx(s); 490 if (error == 0) 491 soisconnected(so); 492 return (error); 493} 494 495static int 496ipx_detach(so) 497 struct socket *so; 498{ 499 int s; 500 struct ipxpcb *ipxp = sotoipxpcb(so); 501 502 if (ipxp == NULL) 503 return (ENOTCONN); 504 s = splnet(); 505 ipx_pcbdetach(ipxp); 506 splx(s); 507 return (0); 508} 509 510static int 511ipx_disconnect(so) 512 struct socket *so; 513{ 514 int s; 515 struct ipxpcb *ipxp = sotoipxpcb(so); 516 517 if (ipx_nullhost(ipxp->ipxp_faddr)) 518 return (ENOTCONN); 519 s = splnet(); 520 ipx_pcbdisconnect(ipxp); 521 splx(s); 522 soisdisconnected(so); 523 return (0); 524} 525 526int 527ipx_peeraddr(so, nam) 528 struct socket *so; 529 struct sockaddr **nam; 530{ 531 struct ipxpcb *ipxp = sotoipxpcb(so); 532 533 ipx_setpeeraddr(ipxp, nam); /* XXX what if alloc fails? */ 534 return (0); 535} 536 537static int 538ipx_send(so, flags, m, nam, control, p) 539 struct socket *so; 540 int flags; 541 struct mbuf *m; 542 struct sockaddr *nam; 543 struct mbuf *control; 544 struct proc *p; 545{ 546 int error; 547 struct ipxpcb *ipxp = sotoipxpcb(so); 548 struct ipx_addr laddr; 549 int s = 0; 550 551 if (nam != NULL) { 552 laddr = ipxp->ipxp_laddr; 553 if (!ipx_nullhost(ipxp->ipxp_faddr)) { 554 error = EISCONN; 555 goto send_release; 556 } 557 /* 558 * Must block input while temporarily connected. 559 */ 560 s = splnet(); 561 error = ipx_pcbconnect(ipxp, nam, p); 562 if (error) { 563 splx(s); 564 goto send_release; 565 } 566 } else { 567 if (ipx_nullhost(ipxp->ipxp_faddr)) { 568 error = ENOTCONN; 569 goto send_release; 570 } 571 } 572 error = ipx_output(ipxp, m); 573 m = NULL; 574 if (nam != NULL) { 575 ipx_pcbdisconnect(ipxp); 576 splx(s); 577 ipxp->ipxp_laddr.x_host = laddr.x_host; 578 ipxp->ipxp_laddr.x_port = laddr.x_port; 579 } 580 581send_release: 582 if (m != NULL) 583 m_freem(m); 584 return (error); 585} 586 587static int 588ipx_shutdown(so) 589 struct socket *so; 590{ 591 socantsendmore(so); 592 return (0); 593} 594 595int 596ipx_sockaddr(so, nam) 597 struct socket *so; 598 struct sockaddr **nam; 599{ 600 struct ipxpcb *ipxp = sotoipxpcb(so); 601 602 ipx_setsockaddr(ipxp, nam); /* XXX what if alloc fails? */ 603 return (0); 604} 605 606static int 607ripx_attach(so, proto, p) 608 struct socket *so; 609 int proto; 610 struct proc *p; 611{ 612 int error = 0; 613 int s; 614 struct ipxpcb *ipxp = sotoipxpcb(so); 615 616 if (p != NULL && (error = suser(p->p_ucred, &p->p_acflag)) != 0) 617 return (error); 618 s = splnet(); 619 error = ipx_pcballoc(so, &ipxrawpcb, p); 620 splx(s); 621 if (error) 622 return (error); 623 error = soreserve(so, ipxsendspace, ipxrecvspace); 624 if (error) 625 return (error); 626 ipxp = sotoipxpcb(so); 627 ipxp->ipxp_faddr.x_host = ipx_broadhost; 628 ipxp->ipxp_flags = IPXP_RAWIN | IPXP_RAWOUT; 629 return (error); 630} 631