1 /* $NetBSD: ddp_usrreq.c,v 1.39 2009/04/16 21:37:17 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.39 2009/04/16 21:37:17 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/queue.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/protosw.h> 44 #include <sys/kauth.h> 45 #include <sys/sysctl.h> 46 #include <net/if.h> 47 #include <net/route.h> 48 #include <net/if_ether.h> 49 #include <net/net_stats.h> 50 #include <netinet/in.h> 51 52 #include <netatalk/at.h> 53 #include <netatalk/at_var.h> 54 #include <netatalk/ddp_var.h> 55 #include <netatalk/ddp_private.h> 56 #include <netatalk/aarp.h> 57 #include <netatalk/at_extern.h> 58 59 static void at_pcbdisconnect(struct ddpcb *); 60 static void at_sockaddr(struct ddpcb *, struct mbuf *); 61 static int at_pcbsetaddr(struct ddpcb *, struct mbuf *, struct lwp *); 62 static int at_pcbconnect(struct ddpcb *, struct mbuf *, struct lwp *); 63 static void at_pcbdetach(struct socket *, struct ddpcb *); 64 static int at_pcballoc(struct socket *); 65 66 struct ifqueue atintrq1, atintrq2; 67 struct ddpcb *ddp_ports[ATPORT_LAST]; 68 struct ddpcb *ddpcb = NULL; 69 percpu_t *ddpstat_percpu; 70 struct at_ifaddrhead at_ifaddr; /* Here as inited in this file */ 71 u_long ddp_sendspace = DDP_MAXSZ; /* Max ddp size + 1 (ddp_type) */ 72 u_long ddp_recvspace = 25 * (587 + sizeof(struct sockaddr_at)); 73 74 #ifdef MBUFTRACE 75 struct mowner atalk_rx_mowner = MOWNER_INIT("atalk", "rx"); 76 struct mowner atalk_tx_mowner = MOWNER_INIT("atalk", "tx"); 77 #endif 78 79 /* ARGSUSED */ 80 int 81 ddp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr, struct mbuf *rights, struct lwp *l) 82 { 83 struct ddpcb *ddp; 84 int error = 0; 85 86 ddp = sotoddpcb(so); 87 88 if (req == PRU_CONTROL) { 89 return (at_control((long) m, (void *) addr, 90 (struct ifnet *) rights, l)); 91 } 92 if (req == PRU_PURGEIF) { 93 mutex_enter(softnet_lock); 94 at_purgeif((struct ifnet *) rights); 95 mutex_exit(softnet_lock); 96 return (0); 97 } 98 if (rights && rights->m_len) { 99 error = EINVAL; 100 goto release; 101 } 102 if (ddp == NULL && req != PRU_ATTACH) { 103 error = EINVAL; 104 goto release; 105 } 106 switch (req) { 107 case PRU_ATTACH: 108 if (ddp != NULL) { 109 error = EINVAL; 110 break; 111 } 112 sosetlock(so); 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, l); 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, l); 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, l); 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(struct ddpcb *ddp, struct mbuf *addr) 227 { 228 struct sockaddr_at *sat; 229 230 addr->m_len = sizeof(struct sockaddr_at); 231 sat = mtod(addr, struct sockaddr_at *); 232 *sat = ddp->ddp_lsat; 233 } 234 235 static int 236 at_pcbsetaddr(struct ddpcb *ddp, struct mbuf *addr, struct lwp *l) 237 { 238 struct sockaddr_at lsat, *sat; 239 struct at_ifaddr *aa; 240 struct ddpcb *ddpp; 241 242 if (ddp->ddp_lsat.sat_port != ATADDR_ANYPORT) { /* shouldn't be bound */ 243 return (EINVAL); 244 } 245 if (addr != 0) { /* validate passed address */ 246 sat = mtod(addr, struct sockaddr_at *); 247 if (addr->m_len != sizeof(*sat)) 248 return (EINVAL); 249 250 if (sat->sat_family != AF_APPLETALK) 251 return (EAFNOSUPPORT); 252 253 if (sat->sat_addr.s_node != ATADDR_ANYNODE || 254 sat->sat_addr.s_net != ATADDR_ANYNET) { 255 TAILQ_FOREACH(aa, &at_ifaddr, aa_list) { 256 if ((sat->sat_addr.s_net == 257 AA_SAT(aa)->sat_addr.s_net) && 258 (sat->sat_addr.s_node == 259 AA_SAT(aa)->sat_addr.s_node)) 260 break; 261 } 262 if (!aa) 263 return (EADDRNOTAVAIL); 264 } 265 if (sat->sat_port != ATADDR_ANYPORT) { 266 int error; 267 268 if (sat->sat_port < ATPORT_FIRST || 269 sat->sat_port >= ATPORT_LAST) 270 return (EINVAL); 271 272 if (sat->sat_port < ATPORT_RESERVED && l && 273 (error = kauth_authorize_network(l->l_cred, 274 KAUTH_NETWORK_BIND, KAUTH_REQ_NETWORK_BIND_PRIVPORT, 275 ddpcb->ddp_socket, sat, NULL)) != 0) 276 return (error); 277 } 278 } else { 279 memset((void *) & lsat, 0, sizeof(struct sockaddr_at)); 280 lsat.sat_len = sizeof(struct sockaddr_at); 281 lsat.sat_addr.s_node = ATADDR_ANYNODE; 282 lsat.sat_addr.s_net = ATADDR_ANYNET; 283 lsat.sat_family = AF_APPLETALK; 284 sat = &lsat; 285 } 286 287 if (sat->sat_addr.s_node == ATADDR_ANYNODE && 288 sat->sat_addr.s_net == ATADDR_ANYNET) { 289 if (TAILQ_EMPTY(&at_ifaddr)) 290 return EADDRNOTAVAIL; 291 sat->sat_addr = AA_SAT(TAILQ_FIRST(&at_ifaddr))->sat_addr; 292 } 293 ddp->ddp_lsat = *sat; 294 295 /* 296 * Choose port. 297 */ 298 if (sat->sat_port == ATADDR_ANYPORT) { 299 for (sat->sat_port = ATPORT_RESERVED; 300 sat->sat_port < ATPORT_LAST; sat->sat_port++) { 301 if (ddp_ports[sat->sat_port - 1] == 0) 302 break; 303 } 304 if (sat->sat_port == ATPORT_LAST) { 305 return (EADDRNOTAVAIL); 306 } 307 ddp->ddp_lsat.sat_port = sat->sat_port; 308 ddp_ports[sat->sat_port - 1] = ddp; 309 } else { 310 for (ddpp = ddp_ports[sat->sat_port - 1]; ddpp; 311 ddpp = ddpp->ddp_pnext) { 312 if (ddpp->ddp_lsat.sat_addr.s_net == 313 sat->sat_addr.s_net && 314 ddpp->ddp_lsat.sat_addr.s_node == 315 sat->sat_addr.s_node) 316 break; 317 } 318 if (ddpp != NULL) 319 return (EADDRINUSE); 320 321 ddp->ddp_pnext = ddp_ports[sat->sat_port - 1]; 322 ddp_ports[sat->sat_port - 1] = ddp; 323 if (ddp->ddp_pnext) 324 ddp->ddp_pnext->ddp_pprev = ddp; 325 } 326 327 return 0; 328 } 329 330 static int 331 at_pcbconnect(struct ddpcb *ddp, struct mbuf *addr, struct lwp *l) 332 { 333 struct rtentry *rt; 334 const struct sockaddr_at *cdst; 335 struct sockaddr_at *sat = mtod(addr, struct sockaddr_at *); 336 struct route *ro; 337 struct at_ifaddr *aa; 338 struct ifnet *ifp; 339 u_short hintnet = 0, net; 340 341 if (addr->m_len != sizeof(*sat)) 342 return EINVAL; 343 if (sat->sat_family != AF_APPLETALK) { 344 return EAFNOSUPPORT; 345 } 346 /* 347 * Under phase 2, network 0 means "the network". We take "the 348 * network" to mean the network the control block is bound to. 349 * If the control block is not bound, there is an error. 350 */ 351 if (sat->sat_addr.s_net == ATADDR_ANYNET 352 && sat->sat_addr.s_node != ATADDR_ANYNODE) { 353 if (ddp->ddp_lsat.sat_port == ATADDR_ANYPORT) { 354 return EADDRNOTAVAIL; 355 } 356 hintnet = ddp->ddp_lsat.sat_addr.s_net; 357 } 358 ro = &ddp->ddp_route; 359 /* 360 * If we've got an old route for this pcb, check that it is valid. 361 * If we've changed our address, we may have an old "good looking" 362 * route here. Attempt to detect it. 363 */ 364 if ((rt = rtcache_validate(ro)) != NULL || 365 (rt = rtcache_update(ro, 1)) != NULL) { 366 if (hintnet) { 367 net = hintnet; 368 } else { 369 net = sat->sat_addr.s_net; 370 } 371 if ((ifp = rt->rt_ifp) != NULL) { 372 TAILQ_FOREACH(aa, &at_ifaddr, aa_list) { 373 if (aa->aa_ifp == ifp && 374 ntohs(net) >= ntohs(aa->aa_firstnet) && 375 ntohs(net) <= ntohs(aa->aa_lastnet)) { 376 break; 377 } 378 } 379 } else 380 aa = NULL; 381 cdst = satocsat(rtcache_getdst(ro)); 382 if (aa == NULL || (cdst->sat_addr.s_net != 383 (hintnet ? hintnet : sat->sat_addr.s_net) || 384 cdst->sat_addr.s_node != sat->sat_addr.s_node)) { 385 rtcache_free(ro); 386 rt = NULL; 387 } 388 } 389 /* 390 * If we've got no route for this interface, try to find one. 391 */ 392 if (rt == NULL) { 393 union { 394 struct sockaddr dst; 395 struct sockaddr_at dsta; 396 } u; 397 398 sockaddr_at_init(&u.dsta, &sat->sat_addr, 0); 399 if (hintnet) 400 u.dsta.sat_addr.s_net = hintnet; 401 rt = rtcache_lookup(ro, &u.dst); 402 } 403 /* 404 * Make sure any route that we have has a valid interface. 405 */ 406 if (rt != NULL && (ifp = rt->rt_ifp) != NULL) { 407 TAILQ_FOREACH(aa, &at_ifaddr, aa_list) { 408 if (aa->aa_ifp == ifp) 409 break; 410 } 411 } else 412 aa = NULL; 413 if (aa == NULL) 414 return ENETUNREACH; 415 ddp->ddp_fsat = *sat; 416 if (ddp->ddp_lsat.sat_port == ATADDR_ANYPORT) 417 return at_pcbsetaddr(ddp, NULL, l); 418 return 0; 419 } 420 421 static void 422 at_pcbdisconnect(struct ddpcb *ddp) 423 { 424 ddp->ddp_fsat.sat_addr.s_net = ATADDR_ANYNET; 425 ddp->ddp_fsat.sat_addr.s_node = ATADDR_ANYNODE; 426 ddp->ddp_fsat.sat_port = ATADDR_ANYPORT; 427 } 428 429 static int 430 at_pcballoc(struct socket *so) 431 { 432 struct ddpcb *ddp; 433 434 ddp = malloc(sizeof(*ddp), M_PCB, M_WAITOK|M_ZERO); 435 if (!ddp) 436 panic("at_pcballoc"); 437 ddp->ddp_lsat.sat_port = ATADDR_ANYPORT; 438 439 ddp->ddp_next = ddpcb; 440 ddp->ddp_prev = NULL; 441 ddp->ddp_pprev = NULL; 442 ddp->ddp_pnext = NULL; 443 if (ddpcb) { 444 ddpcb->ddp_prev = ddp; 445 } 446 ddpcb = ddp; 447 448 ddp->ddp_socket = so; 449 so->so_pcb = (void *) ddp; 450 #ifdef MBUFTRACE 451 so->so_rcv.sb_mowner = &atalk_rx_mowner; 452 so->so_snd.sb_mowner = &atalk_tx_mowner; 453 #endif 454 return 0; 455 } 456 457 static void 458 at_pcbdetach(struct socket *so, struct ddpcb *ddp) 459 { 460 soisdisconnected(so); 461 so->so_pcb = 0; 462 /* sofree drops the lock */ 463 sofree(so); 464 mutex_enter(softnet_lock); 465 466 /* remove ddp from ddp_ports list */ 467 if (ddp->ddp_lsat.sat_port != ATADDR_ANYPORT && 468 ddp_ports[ddp->ddp_lsat.sat_port - 1] != NULL) { 469 if (ddp->ddp_pprev != NULL) { 470 ddp->ddp_pprev->ddp_pnext = ddp->ddp_pnext; 471 } else { 472 ddp_ports[ddp->ddp_lsat.sat_port - 1] = ddp->ddp_pnext; 473 } 474 if (ddp->ddp_pnext != NULL) { 475 ddp->ddp_pnext->ddp_pprev = ddp->ddp_pprev; 476 } 477 } 478 rtcache_free(&ddp->ddp_route); 479 if (ddp->ddp_prev) { 480 ddp->ddp_prev->ddp_next = ddp->ddp_next; 481 } else { 482 ddpcb = ddp->ddp_next; 483 } 484 if (ddp->ddp_next) { 485 ddp->ddp_next->ddp_prev = ddp->ddp_prev; 486 } 487 free(ddp, M_PCB); 488 } 489 490 /* 491 * For the moment, this just find the pcb with the correct local address. 492 * In the future, this will actually do some real searching, so we can use 493 * the sender's address to do de-multiplexing on a single port to many 494 * sockets (pcbs). 495 */ 496 struct ddpcb * 497 ddp_search( 498 struct sockaddr_at *from, 499 struct sockaddr_at *to, 500 struct at_ifaddr *aa) 501 { 502 struct ddpcb *ddp; 503 504 /* 505 * Check for bad ports. 506 */ 507 if (to->sat_port < ATPORT_FIRST || to->sat_port >= ATPORT_LAST) 508 return NULL; 509 510 /* 511 * Make sure the local address matches the sent address. What about 512 * the interface? 513 */ 514 for (ddp = ddp_ports[to->sat_port - 1]; ddp; ddp = ddp->ddp_pnext) { 515 /* XXX should we handle 0.YY? */ 516 517 /* XXXX.YY to socket on destination interface */ 518 if (to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net && 519 to->sat_addr.s_node == ddp->ddp_lsat.sat_addr.s_node) { 520 break; 521 } 522 /* 0.255 to socket on receiving interface */ 523 if (to->sat_addr.s_node == ATADDR_BCAST && 524 (to->sat_addr.s_net == 0 || 525 to->sat_addr.s_net == ddp->ddp_lsat.sat_addr.s_net) && 526 ddp->ddp_lsat.sat_addr.s_net == AA_SAT(aa)->sat_addr.s_net) { 527 break; 528 } 529 /* XXXX.0 to socket on destination interface */ 530 if (to->sat_addr.s_net == aa->aa_firstnet && 531 to->sat_addr.s_node == 0 && 532 ntohs(ddp->ddp_lsat.sat_addr.s_net) >= 533 ntohs(aa->aa_firstnet) && 534 ntohs(ddp->ddp_lsat.sat_addr.s_net) <= 535 ntohs(aa->aa_lastnet)) { 536 break; 537 } 538 } 539 return (ddp); 540 } 541 542 /* 543 * Initialize all the ddp & appletalk stuff 544 */ 545 void 546 ddp_init(void) 547 { 548 549 ddpstat_percpu = percpu_alloc(sizeof(uint64_t) * DDP_NSTATS); 550 551 TAILQ_INIT(&at_ifaddr); 552 atintrq1.ifq_maxlen = IFQ_MAXLEN; 553 atintrq2.ifq_maxlen = IFQ_MAXLEN; 554 555 MOWNER_ATTACH(&atalk_tx_mowner); 556 MOWNER_ATTACH(&atalk_rx_mowner); 557 } 558 559 #if 0 560 static void 561 ddp_clean(void) 562 { 563 struct ddpcb *ddp; 564 565 for (ddp = ddpcb; ddp; ddp = ddp->ddp_next) 566 at_pcbdetach(ddp->ddp_socket, ddp); 567 } 568 #endif 569 570 static int 571 sysctl_net_atalk_ddp_stats(SYSCTLFN_ARGS) 572 { 573 574 return (NETSTAT_SYSCTL(ddpstat_percpu, DDP_NSTATS)); 575 } 576 577 /* 578 * Sysctl for DDP variables. 579 */ 580 SYSCTL_SETUP(sysctl_net_atalk_ddp_setup, "sysctl net.atalk.ddp subtree setup") 581 { 582 583 sysctl_createv(clog, 0, NULL, NULL, 584 CTLFLAG_PERMANENT, 585 CTLTYPE_NODE, "net", NULL, 586 NULL, 0, NULL, 0, 587 CTL_NET, CTL_EOL); 588 sysctl_createv(clog, 0, NULL, NULL, 589 CTLFLAG_PERMANENT, 590 CTLTYPE_NODE, "atalk", NULL, 591 NULL, 0, NULL, 0, 592 CTL_NET, PF_APPLETALK, CTL_EOL); 593 sysctl_createv(clog, 0, NULL, NULL, 594 CTLFLAG_PERMANENT, 595 CTLTYPE_NODE, "ddp", 596 SYSCTL_DESCR("DDP related settings"), 597 NULL, 0, NULL, 0, 598 CTL_NET, PF_APPLETALK, ATPROTO_DDP, CTL_EOL); 599 600 sysctl_createv(clog, 0, NULL, NULL, 601 CTLFLAG_PERMANENT, 602 CTLTYPE_STRUCT, "stats", 603 SYSCTL_DESCR("DDP statistics"), 604 sysctl_net_atalk_ddp_stats, 0, NULL, 0, 605 CTL_NET, PF_APPLETALK, ATPROTO_DDP, CTL_CREATE, 606 CTL_EOL); 607 } 608