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