1 /* $NetBSD: ddp_usrreq.c,v 1.14 2005/12/11 12:24:54 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1990,1991 Regents of The University of Michigan. 5 * All Rights Reserved. 6 * 7 * Permission to use, copy, modify, and distribute this software and 8 * its documentation for any purpose and without fee is hereby granted, 9 * provided that the above copyright notice appears in all copies and 10 * that both that copyright notice and this permission notice appear 11 * in supporting documentation, and that the name of The University 12 * of Michigan not be used in advertising or publicity pertaining to 13 * distribution of the software without specific, written prior 14 * permission. This software is supplied as is without expressed or 15 * implied warranties of any kind. 16 * 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 20 * Research Systems Unix Group 21 * The University of Michigan 22 * c/o Wesley Craig 23 * 535 W. William Street 24 * Ann Arbor, Michigan 25 * +1-313-764-2278 26 * netatalk@umich.edu 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: ddp_usrreq.c,v 1.14 2005/12/11 12:24:54 christos Exp $"); 31 32 #include "opt_mbuftrace.h" 33 34 #include <sys/param.h> 35 #include <sys/errno.h> 36 #include <sys/systm.h> 37 #include <sys/proc.h> 38 #include <sys/mbuf.h> 39 #include <sys/ioctl.h> 40 #include <sys/socket.h> 41 #include <sys/socketvar.h> 42 #include <sys/protosw.h> 43 #include <net/if.h> 44 #include <net/route.h> 45 #include <net/if_ether.h> 46 #include <netinet/in.h> 47 48 #include <netatalk/at.h> 49 #include <netatalk/at_var.h> 50 #include <netatalk/ddp_var.h> 51 #include <netatalk/aarp.h> 52 #include <netatalk/at_extern.h> 53 54 static void at_pcbdisconnect __P((struct ddpcb *)); 55 static void at_sockaddr __P((struct ddpcb *, struct mbuf *)); 56 static int at_pcbsetaddr __P((struct ddpcb *, struct mbuf *, struct proc *)); 57 static int at_pcbconnect __P((struct ddpcb *, struct mbuf *, struct proc *)); 58 static void at_pcbdetach __P((struct socket *, struct ddpcb *)); 59 static int at_pcballoc __P((struct socket *)); 60 61 struct ifqueue atintrq1, atintrq2; 62 struct ddpcb *ddp_ports[ATPORT_LAST]; 63 struct ddpcb *ddpcb = NULL; 64 struct ddpstat ddpstat; 65 struct at_ifaddrhead at_ifaddr; /* Here as inited in this file */ 66 u_long ddp_sendspace = DDP_MAXSZ; /* Max ddp size + 1 (ddp_type) */ 67 u_long ddp_recvspace = 25 * (587 + sizeof(struct sockaddr_at)); 68 69 #ifdef MBUFTRACE 70 struct mowner atalk_rx_mowner = { "atalk", "rx" }; 71 struct mowner atalk_tx_mowner = { "atalk", "tx" }; 72 #endif 73 74 /* ARGSUSED */ 75 int 76 ddp_usrreq(so, req, m, addr, rights, l) 77 struct socket *so; 78 int req; 79 struct mbuf *m; 80 struct mbuf *addr; 81 struct mbuf *rights; 82 struct lwp *l; 83 { 84 struct proc *p; 85 struct ddpcb *ddp; 86 int error = 0; 87 88 p = l ? l->l_proc : NULL; 89 ddp = sotoddpcb(so); 90 91 if (req == PRU_CONTROL) { 92 return (at_control((long) m, (caddr_t) addr, 93 (struct ifnet *) rights, (struct proc *) p)); 94 } 95 if (req == PRU_PURGEIF) { 96 at_purgeif((struct ifnet *) rights); 97 return (0); 98 } 99 if (rights && rights->m_len) { 100 error = EINVAL; 101 goto release; 102 } 103 if (ddp == NULL && req != PRU_ATTACH) { 104 error = EINVAL; 105 goto release; 106 } 107 switch (req) { 108 case PRU_ATTACH: 109 if (ddp != NULL) { 110 error = EINVAL; 111 break; 112 } 113 if ((error = at_pcballoc(so)) != 0) { 114 break; 115 } 116 error = soreserve(so, ddp_sendspace, ddp_recvspace); 117 break; 118 119 case PRU_DETACH: 120 at_pcbdetach(so, ddp); 121 break; 122 123 case PRU_BIND: 124 error = at_pcbsetaddr(ddp, addr, p); 125 break; 126 127 case PRU_SOCKADDR: 128 at_sockaddr(ddp, addr); 129 break; 130 131 case PRU_CONNECT: 132 if (ddp->ddp_fsat.sat_port != ATADDR_ANYPORT) { 133 error = EISCONN; 134 break; 135 } 136 error = at_pcbconnect(ddp, addr, p); 137 if (error == 0) 138 soisconnected(so); 139 break; 140 141 case PRU_DISCONNECT: 142 if (ddp->ddp_fsat.sat_addr.s_node == ATADDR_ANYNODE) { 143 error = ENOTCONN; 144 break; 145 } 146 at_pcbdisconnect(ddp); 147 soisdisconnected(so); 148 break; 149 150 case PRU_SHUTDOWN: 151 socantsendmore(so); 152 break; 153 154 case PRU_SEND:{ 155 int s = 0; 156 157 if (addr) { 158 if (ddp->ddp_fsat.sat_port != ATADDR_ANYPORT) { 159 error = EISCONN; 160 break; 161 } 162 s = splnet(); 163 error = at_pcbconnect(ddp, addr, p); 164 if (error) { 165 splx(s); 166 break; 167 } 168 } else { 169 if (ddp->ddp_fsat.sat_port == ATADDR_ANYPORT) { 170 error = ENOTCONN; 171 break; 172 } 173 } 174 175 error = ddp_output(m, ddp); 176 m = NULL; 177 if (addr) { 178 at_pcbdisconnect(ddp); 179 splx(s); 180 } 181 } 182 break; 183 184 case PRU_ABORT: 185 soisdisconnected(so); 186 at_pcbdetach(so, ddp); 187 break; 188 189 case PRU_LISTEN: 190 case PRU_CONNECT2: 191 case PRU_ACCEPT: 192 case PRU_SENDOOB: 193 case PRU_FASTTIMO: 194 case PRU_SLOWTIMO: 195 case PRU_PROTORCV: 196 case PRU_PROTOSEND: 197 error = EOPNOTSUPP; 198 break; 199 200 case PRU_RCVD: 201 case PRU_RCVOOB: 202 /* 203 * Don't mfree. Good architecture... 204 */ 205 return (EOPNOTSUPP); 206 207 case PRU_SENSE: 208 /* 209 * 1. Don't return block size. 210 * 2. Don't mfree. 211 */ 212 return (0); 213 214 default: 215 error = EOPNOTSUPP; 216 } 217 218 release: 219 if (m != NULL) { 220 m_freem(m); 221 } 222 return (error); 223 } 224 225 static void 226 at_sockaddr(ddp, addr) 227 struct ddpcb *ddp; 228 struct mbuf *addr; 229 { 230 struct sockaddr_at *sat; 231 232 addr->m_len = sizeof(struct sockaddr_at); 233 sat = mtod(addr, struct sockaddr_at *); 234 *sat = ddp->ddp_lsat; 235 } 236 237 static int 238 at_pcbsetaddr(ddp, addr, p) 239 struct ddpcb *ddp; 240 struct mbuf *addr; 241 struct proc *p; 242 { 243 struct sockaddr_at lsat, *sat; 244 struct at_ifaddr *aa; 245 struct ddpcb *ddpp; 246 247 if (ddp->ddp_lsat.sat_port != ATADDR_ANYPORT) { /* shouldn't be bound */ 248 return (EINVAL); 249 } 250 if (addr != 0) { /* validate passed address */ 251 sat = mtod(addr, struct sockaddr_at *); 252 if (addr->m_len != sizeof(*sat)) 253 return (EINVAL); 254 255 if (sat->sat_family != AF_APPLETALK) 256 return (EAFNOSUPPORT); 257 258 if (sat->sat_addr.s_node != ATADDR_ANYNODE || 259 sat->sat_addr.s_net != ATADDR_ANYNET) { 260 for (aa = at_ifaddr.tqh_first; aa; 261 aa = aa->aa_list.tqe_next) { 262 if ((sat->sat_addr.s_net == 263 AA_SAT(aa)->sat_addr.s_net) && 264 (sat->sat_addr.s_node == 265 AA_SAT(aa)->sat_addr.s_node)) 266 break; 267 } 268 if (!aa) 269 return (EADDRNOTAVAIL); 270 } 271 if (sat->sat_port != ATADDR_ANYPORT) { 272 if (sat->sat_port < ATPORT_FIRST || 273 sat->sat_port >= ATPORT_LAST) 274 return (EINVAL); 275 276 if (sat->sat_port < ATPORT_RESERVED && 277 suser(p->p_ucred, &p->p_acflag)) 278 return (EACCES); 279 } 280 } else { 281 bzero((caddr_t) & lsat, sizeof(struct sockaddr_at)); 282 lsat.sat_len = sizeof(struct sockaddr_at); 283 lsat.sat_addr.s_node = ATADDR_ANYNODE; 284 lsat.sat_addr.s_net = ATADDR_ANYNET; 285 lsat.sat_family = AF_APPLETALK; 286 sat = &lsat; 287 } 288 289 if (sat->sat_addr.s_node == ATADDR_ANYNODE && 290 sat->sat_addr.s_net == ATADDR_ANYNET) { 291 if (at_ifaddr.tqh_first == NULL) 292 return (EADDRNOTAVAIL); 293 sat->sat_addr = AA_SAT(at_ifaddr.tqh_first)->sat_addr; 294 } 295 ddp->ddp_lsat = *sat; 296 297 /* 298 * Choose port. 299 */ 300 if (sat->sat_port == ATADDR_ANYPORT) { 301 for (sat->sat_port = ATPORT_RESERVED; 302 sat->sat_port < ATPORT_LAST; sat->sat_port++) { 303 if (ddp_ports[sat->sat_port - 1] == 0) 304 break; 305 } 306 if (sat->sat_port == ATPORT_LAST) { 307 return (EADDRNOTAVAIL); 308 } 309 ddp->ddp_lsat.sat_port = sat->sat_port; 310 ddp_ports[sat->sat_port - 1] = ddp; 311 } else { 312 for (ddpp = ddp_ports[sat->sat_port - 1]; ddpp; 313 ddpp = ddpp->ddp_pnext) { 314 if (ddpp->ddp_lsat.sat_addr.s_net == 315 sat->sat_addr.s_net && 316 ddpp->ddp_lsat.sat_addr.s_node == 317 sat->sat_addr.s_node) 318 break; 319 } 320 if (ddpp != NULL) 321 return (EADDRINUSE); 322 323 ddp->ddp_pnext = ddp_ports[sat->sat_port - 1]; 324 ddp_ports[sat->sat_port - 1] = ddp; 325 if (ddp->ddp_pnext) 326 ddp->ddp_pnext->ddp_pprev = ddp; 327 } 328 329 return 0; 330 } 331 332 static int 333 at_pcbconnect(ddp, addr, p) 334 struct ddpcb *ddp; 335 struct mbuf *addr; 336 struct proc *p; 337 { 338 struct sockaddr_at *sat = mtod(addr, struct sockaddr_at *); 339 struct route *ro; 340 struct at_ifaddr *aa = 0; 341 struct ifnet *ifp; 342 u_short hintnet = 0, net; 343 344 if (addr->m_len != sizeof(*sat)) 345 return (EINVAL); 346 if (sat->sat_family != AF_APPLETALK) { 347 return (EAFNOSUPPORT); 348 } 349 /* 350 * Under phase 2, network 0 means "the network". We take "the 351 * network" to mean the network the control block is bound to. 352 * If the control block is not bound, there is an error. 353 */ 354 if (sat->sat_addr.s_net == ATADDR_ANYNET 355 && sat->sat_addr.s_node != ATADDR_ANYNODE) { 356 if (ddp->ddp_lsat.sat_port == ATADDR_ANYPORT) { 357 return (EADDRNOTAVAIL); 358 } 359 hintnet = ddp->ddp_lsat.sat_addr.s_net; 360 } 361 ro = &ddp->ddp_route; 362 /* 363 * If we've got an old route for this pcb, check that it is valid. 364 * If we've changed our address, we may have an old "good looking" 365 * route here. Attempt to detect it. 366 */ 367 if (ro->ro_rt) { 368 if (hintnet) { 369 net = hintnet; 370 } else { 371 net = sat->sat_addr.s_net; 372 } 373 aa = 0; 374 if ((ifp = ro->ro_rt->rt_ifp) != NULL) { 375 for (aa = at_ifaddr.tqh_first; aa; 376 aa = aa->aa_list.tqe_next) { 377 if (aa->aa_ifp == ifp && 378 ntohs(net) >= ntohs(aa->aa_firstnet) && 379 ntohs(net) <= ntohs(aa->aa_lastnet)) { 380 break; 381 } 382 } 383 } 384 if (aa == NULL || (satosat(&ro->ro_dst)->sat_addr.s_net != 385 (hintnet ? hintnet : sat->sat_addr.s_net) || 386 satosat(&ro->ro_dst)->sat_addr.s_node != 387 sat->sat_addr.s_node)) { 388 RTFREE(ro->ro_rt); 389 ro->ro_rt = (struct rtentry *) 0; 390 } 391 } 392 /* 393 * If we've got no route for this interface, try to find one. 394 */ 395 if (ro->ro_rt == (struct rtentry *) 0 || 396 ro->ro_rt->rt_ifp == (struct ifnet *) 0) { 397 bzero(&ro->ro_dst, sizeof(struct sockaddr_at)); 398 ro->ro_dst.sa_len = sizeof(struct sockaddr_at); 399 ro->ro_dst.sa_family = AF_APPLETALK; 400 if (hintnet) { 401 satosat(&ro->ro_dst)->sat_addr.s_net = hintnet; 402 } else { 403 satosat(&ro->ro_dst)->sat_addr.s_net = 404 sat->sat_addr.s_net; 405 } 406 satosat(&ro->ro_dst)->sat_addr.s_node = sat->sat_addr.s_node; 407 rtalloc(ro); 408 } 409 /* 410 * Make sure any route that we have has a valid interface. 411 */ 412 aa = 0; 413 if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp)) { 414 for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) { 415 if (aa->aa_ifp == ifp) { 416 break; 417 } 418 } 419 } 420 if (aa == 0) { 421 return (ENETUNREACH); 422 } 423 ddp->ddp_fsat = *sat; 424 if (ddp->ddp_lsat.sat_port == ATADDR_ANYPORT) { 425 return (at_pcbsetaddr(ddp, (struct mbuf *) 0, p)); 426 } 427 return (0); 428 } 429 430 static void 431 at_pcbdisconnect(ddp) 432 struct ddpcb *ddp; 433 { 434 ddp->ddp_fsat.sat_addr.s_net = ATADDR_ANYNET; 435 ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE; 436 ddp->ddp_fsat.sat_port = ATADDR_ANYPORT; 437 } 438 439 static int 440 at_pcballoc(so) 441 struct socket *so; 442 { 443 struct ddpcb *ddp; 444 445 MALLOC(ddp, struct ddpcb *, sizeof(*ddp), M_PCB, M_WAITOK|M_ZERO); 446 if (!ddp) 447 panic("at_pcballoc"); 448 ddp->ddp_lsat.sat_port = ATADDR_ANYPORT; 449 450 ddp->ddp_next = ddpcb; 451 ddp->ddp_prev = NULL; 452 ddp->ddp_pprev = NULL; 453 ddp->ddp_pnext = NULL; 454 if (ddpcb) { 455 ddpcb->ddp_prev = ddp; 456 } 457 ddpcb = ddp; 458 459 ddp->ddp_socket = so; 460 so->so_pcb = (caddr_t) ddp; 461 #ifdef MBUFTRACE 462 so->so_rcv.sb_mowner = &atalk_rx_mowner; 463 so->so_snd.sb_mowner = &atalk_tx_mowner; 464 #endif 465 return (0); 466 } 467 468 static void 469 at_pcbdetach(so, ddp) 470 struct socket *so; 471 struct ddpcb *ddp; 472 { 473 soisdisconnected(so); 474 so->so_pcb = 0; 475 sofree(so); 476 477 /* remove ddp from ddp_ports list */ 478 if (ddp->ddp_lsat.sat_port != ATADDR_ANYPORT && 479 ddp_ports[ddp->ddp_lsat.sat_port - 1] != NULL) { 480 if (ddp->ddp_pprev != NULL) { 481 ddp->ddp_pprev->ddp_pnext = ddp->ddp_pnext; 482 } else { 483 ddp_ports[ddp->ddp_lsat.sat_port - 1] = ddp->ddp_pnext; 484 } 485 if (ddp->ddp_pnext != NULL) { 486 ddp->ddp_pnext->ddp_pprev = ddp->ddp_pprev; 487 } 488 } 489 if (ddp->ddp_route.ro_rt) { 490 rtfree(ddp->ddp_route.ro_rt); 491 } 492 if (ddp->ddp_prev) { 493 ddp->ddp_prev->ddp_next = ddp->ddp_next; 494 } else { 495 ddpcb = ddp->ddp_next; 496 } 497 if (ddp->ddp_next) { 498 ddp->ddp_next->ddp_prev = ddp->ddp_prev; 499 } 500 free(ddp, M_PCB); 501 } 502 503 /* 504 * For the moment, this just find the pcb with the correct local address. 505 * In the future, this will actually do some real searching, so we can use 506 * the sender's address to do de-multiplexing on a single port to many 507 * sockets (pcbs). 508 */ 509 struct ddpcb * 510 ddp_search(from, to, aa) 511 struct sockaddr_at *from; 512 struct sockaddr_at *to; 513 struct at_ifaddr *aa; 514 { 515 struct ddpcb *ddp; 516 517 /* 518 * Check for bad ports. 519 */ 520 if (to->sat_port < ATPORT_FIRST || to->sat_port >= ATPORT_LAST) { 521 return (NULL); 522 } 523 /* 524 * Make sure the local address matches the sent address. What about 525 * the interface? 526 */ 527 for (ddp = ddp_ports[to->sat_port - 1]; ddp; ddp = ddp->ddp_pnext) { 528 /* XXX should we handle 0.YY? */ 529 530 /* XXXX.YY to socket on destination interface */ 531 if (to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net && 532 to->sat_addr.s_node == ddp->ddp_lsat.sat_addr.s_node) { 533 break; 534 } 535 /* 0.255 to socket on receiving interface */ 536 if (to->sat_addr.s_node == ATADDR_BCAST && 537 (to->sat_addr.s_net == 0 || 538 to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net) && 539 ddp->ddp_lsat.sat_addr.s_net == AA_SAT(aa)->sat_addr.s_net) { 540 break; 541 } 542 /* XXXX.0 to socket on destination interface */ 543 if (to->sat_addr.s_net == aa->aa_firstnet && 544 to->sat_addr.s_node == 0 && 545 ntohs(ddp->ddp_lsat.sat_addr.s_net) >= 546 ntohs(aa->aa_firstnet) && 547 ntohs(ddp->ddp_lsat.sat_addr.s_net) <= 548 ntohs(aa->aa_lastnet)) { 549 break; 550 } 551 } 552 return (ddp); 553 } 554 555 /* 556 * Initialize all the ddp & appletalk stuff 557 */ 558 void 559 ddp_init() 560 { 561 TAILQ_INIT(&at_ifaddr); 562 atintrq1.ifq_maxlen = IFQ_MAXLEN; 563 atintrq2.ifq_maxlen = IFQ_MAXLEN; 564 565 MOWNER_ATTACH(&atalk_tx_mowner); 566 MOWNER_ATTACH(&atalk_rx_mowner); 567 } 568 569 #if 0 570 static void 571 ddp_clean() 572 { 573 struct ddpcb *ddp; 574 575 for (ddp = ddpcb; ddp; ddp = ddp->ddp_next) 576 at_pcbdetach(ddp->ddp_socket, ddp); 577 } 578 #endif 579