1 /* 2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved. 3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved. 4 * 5 * This code is derived from software contributed to The DragonFly Project 6 * by Jeffrey M. Hsu. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of The DragonFly Project nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific, prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * $DragonFly: src/sys/kern/uipc_msg.c,v 1.26 2008/10/27 02:56:30 sephe Exp $ 34 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/msgport.h> 40 #include <sys/protosw.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/socketops.h> 44 #include <sys/thread.h> 45 #include <sys/thread2.h> 46 #include <sys/msgport2.h> 47 #include <vm/pmap.h> 48 #include <net/netmsg2.h> 49 50 #include <net/netisr.h> 51 #include <net/netmsg.h> 52 53 /* 54 * Abort a socket and free it. Called from soabort() only. 55 * 56 * The SS_ABORTING flag must already be set. 57 */ 58 void 59 so_pru_abort(struct socket *so) 60 { 61 struct netmsg_pru_abort msg; 62 lwkt_port_t port; 63 64 KKASSERT(so->so_state & SS_ABORTING); 65 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_ABORT); 66 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 67 0, netmsg_pru_abort); 68 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_abort; 69 msg.nm_so = so; 70 (void)lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 71 } 72 73 /* 74 * Abort a socket and free it, asynchronously. Called from 75 * soaborta() only. 76 * 77 * The SS_ABORTING flag must already be set. 78 */ 79 void 80 so_pru_aborta(struct socket *so) 81 { 82 struct netmsg_pru_abort *msg; 83 lwkt_port_t port; 84 85 KKASSERT(so->so_state & SS_ABORTING); 86 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK | M_ZERO); 87 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_ABORT); 88 netmsg_init(&msg->nm_netmsg, &netisr_afree_rport, 89 0, netmsg_pru_abort); 90 msg->nm_prufn = so->so_proto->pr_usrreqs->pru_abort; 91 msg->nm_so = so; 92 lwkt_sendmsg(port, &msg->nm_netmsg.nm_lmsg); 93 } 94 95 /* 96 * Abort a socket and free it. Called from soabort_oncpu() only. 97 * Caller must make sure that the current CPU is inpcb's owner CPU. 98 * 99 * The SS_ABORTING flag must already be set. 100 */ 101 void 102 so_pru_abort_oncpu(struct socket *so) 103 { 104 so->so_proto->pr_usrreqs->pru_abort(so); 105 } 106 107 int 108 so_pru_accept(struct socket *so, struct sockaddr **nam) 109 { 110 /* Block (memory allocation) in process context. XXX JH */ 111 return ((*so->so_proto->pr_usrreqs->pru_accept)(so, nam)); 112 113 #ifdef notdef 114 int error; 115 struct netmsg_pru_accept msg; 116 lwkt_port_t port; 117 118 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_ACCEPT); 119 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 120 netmsg_pru_accept); 121 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_accept; 122 msg.nm_so = so; 123 msg.nm_nam = nam; 124 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 125 return (error); 126 #endif 127 } 128 129 int 130 so_pru_attach(struct socket *so, int proto, struct pru_attach_info *ai) 131 { 132 int error; 133 struct netmsg_pru_attach msg; 134 lwkt_port_t port; 135 136 port = so->so_proto->pr_mport(NULL, NULL, NULL, PRU_ATTACH); 137 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 138 netmsg_pru_attach); 139 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_attach; 140 msg.nm_so = so; 141 msg.nm_proto = proto; 142 msg.nm_ai = ai; 143 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 144 return (error); 145 } 146 147 int 148 so_pru_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 149 { 150 int error; 151 struct netmsg_pru_bind msg; 152 lwkt_port_t port; 153 154 /* Send mesg to thread for new address. */ 155 port = so->so_proto->pr_mport(NULL, nam, NULL, PRU_BIND); 156 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 157 netmsg_pru_bind); 158 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_bind; 159 msg.nm_so = so; 160 msg.nm_nam = nam; 161 msg.nm_td = td; /* used only for prison_ip() XXX JH */ 162 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 163 return (error); 164 } 165 166 int 167 so_pru_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 168 { 169 int error; 170 struct netmsg_pru_connect msg; 171 lwkt_port_t port; 172 173 port = so->so_proto->pr_mport(so, nam, NULL, PRU_CONNECT); 174 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 175 netmsg_pru_connect); 176 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_connect; 177 msg.nm_so = so; 178 msg.nm_nam = nam; 179 msg.nm_td = td; 180 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 181 return (error); 182 } 183 184 int 185 so_pru_connect2(struct socket *so1, struct socket *so2) 186 { 187 int error; 188 struct netmsg_pru_connect2 msg; 189 lwkt_port_t port; 190 191 port = so1->so_proto->pr_mport(so1, NULL, NULL, PRU_CONNECT2); 192 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 193 netmsg_pru_connect2); 194 msg.nm_prufn = so1->so_proto->pr_usrreqs->pru_connect2; 195 msg.nm_so1 = so1; 196 msg.nm_so2 = so2; 197 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 198 return (error); 199 } 200 201 int 202 so_pru_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp) 203 { 204 return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, ifp, 205 curthread)); 206 #ifdef gag /* does copyin and copyout deep inside stack XXX JH */ 207 int error; 208 struct netmsg_pru_control msg; 209 lwkt_port_t port; 210 211 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_CONTROL); 212 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 213 netmsg_pru_control); 214 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_control; 215 msg.nm_so = so; 216 msg.nm_cmd = cmd; 217 msg.nm_data = data; 218 msg.nm_ifp = ifp; 219 msg.nm_td = td; 220 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 221 return (error); 222 #endif 223 } 224 225 int 226 so_pru_detach(struct socket *so) 227 { 228 int error; 229 struct netmsg_pru_detach msg; 230 lwkt_port_t port; 231 232 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_DETACH); 233 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 234 netmsg_pru_detach); 235 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_detach; 236 msg.nm_so = so; 237 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 238 return (error); 239 } 240 241 int 242 so_pru_disconnect(struct socket *so) 243 { 244 int error; 245 struct netmsg_pru_disconnect msg; 246 lwkt_port_t port; 247 248 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_DISCONNECT); 249 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 250 netmsg_pru_disconnect); 251 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_disconnect; 252 msg.nm_so = so; 253 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 254 return (error); 255 } 256 257 int 258 so_pru_listen(struct socket *so, struct thread *td) 259 { 260 int error; 261 struct netmsg_pru_listen msg; 262 lwkt_port_t port; 263 264 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_LISTEN); 265 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 266 netmsg_pru_listen); 267 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_listen; 268 msg.nm_so = so; 269 msg.nm_td = td; /* used only for prison_ip() XXX JH */ 270 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 271 return (error); 272 } 273 274 int 275 so_pru_peeraddr(struct socket *so, struct sockaddr **nam) 276 { 277 int error; 278 struct netmsg_pru_peeraddr msg; 279 lwkt_port_t port; 280 281 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_PEERADDR); 282 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 283 netmsg_pru_peeraddr); 284 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_peeraddr; 285 msg.nm_so = so; 286 msg.nm_nam = nam; 287 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 288 return (error); 289 } 290 291 int 292 so_pru_rcvd(struct socket *so, int flags) 293 { 294 int error; 295 struct netmsg_pru_rcvd msg; 296 lwkt_port_t port; 297 298 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_RCVD); 299 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 300 netmsg_pru_rcvd); 301 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_rcvd; 302 msg.nm_so = so; 303 msg.nm_flags = flags; 304 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 305 return (error); 306 } 307 308 int 309 so_pru_rcvoob(struct socket *so, struct mbuf *m, int flags) 310 { 311 int error; 312 struct netmsg_pru_rcvoob msg; 313 lwkt_port_t port; 314 315 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_RCVOOB); 316 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 317 netmsg_pru_rcvoob); 318 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_rcvoob; 319 msg.nm_so = so; 320 msg.nm_m = m; 321 msg.nm_flags = flags; 322 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 323 return (error); 324 } 325 326 int 327 so_pru_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 328 struct mbuf *control, struct thread *td) 329 { 330 int error; 331 struct netmsg_pru_send msg; 332 lwkt_port_t port; 333 334 port = so->so_proto->pr_mport(so, addr, &m, PRU_SEND); 335 if (port == NULL) { 336 KKASSERT(m == NULL); 337 return EINVAL; 338 } 339 340 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 341 netmsg_pru_send); 342 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_send; 343 msg.nm_so = so; 344 msg.nm_flags = flags; 345 msg.nm_m = m; 346 msg.nm_addr = addr; 347 msg.nm_control = control; 348 msg.nm_td = td; 349 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 350 return (error); 351 } 352 353 int 354 so_pru_sense(struct socket *so, struct stat *sb) 355 { 356 int error; 357 struct netmsg_pru_sense msg; 358 lwkt_port_t port; 359 360 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_SENSE); 361 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 362 netmsg_pru_sense); 363 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_sense; 364 msg.nm_so = so; 365 msg.nm_stat = sb; 366 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 367 return (error); 368 } 369 370 int 371 so_pru_shutdown(struct socket *so) 372 { 373 int error; 374 struct netmsg_pru_shutdown msg; 375 lwkt_port_t port; 376 377 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_SHUTDOWN); 378 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 379 netmsg_pru_shutdown); 380 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_shutdown; 381 msg.nm_so = so; 382 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 383 return (error); 384 } 385 386 int 387 so_pru_sockaddr(struct socket *so, struct sockaddr **nam) 388 { 389 int error; 390 struct netmsg_pru_sockaddr msg; 391 lwkt_port_t port; 392 393 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_SOCKADDR); 394 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 395 netmsg_pru_sockaddr); 396 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_sockaddr; 397 msg.nm_so = so; 398 msg.nm_nam = nam; 399 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 400 return (error); 401 } 402 403 int 404 so_pru_sopoll(struct socket *so, int events, struct ucred *cred) 405 { 406 int error; 407 struct netmsg_pru_sopoll msg; 408 lwkt_port_t port; 409 410 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_SOPOLL); 411 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 412 netmsg_pru_sopoll); 413 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_sopoll; 414 msg.nm_so = so; 415 msg.nm_events = events; 416 msg.nm_cred = cred; 417 msg.nm_td = curthread; 418 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 419 return (error); 420 } 421 422 int 423 so_pru_ctloutput(struct socket *so, struct sockopt *sopt) 424 { 425 struct netmsg_pru_ctloutput msg; 426 lwkt_port_t port; 427 int error; 428 429 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val)); 430 port = so->so_proto->pr_mport(so, NULL, NULL, PRU_CTLOUTPUT); 431 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 432 netmsg_pru_ctloutput); 433 /* TBD: move pr_ctloutput to pr_usrreqs */ 434 msg.nm_prufn = so->so_proto->pr_ctloutput; 435 msg.nm_so = so; 436 msg.nm_sopt = sopt; 437 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 438 return (error); 439 } 440 441 /* 442 * Protocol control input, typically via icmp. 443 * 444 * If the protocol pr_ctlport is not NULL we call it to figure out the 445 * protocol port. If NULL is returned we can just return, otherwise 446 * we issue a netmsg to call pr_ctlinput in the proper thread. 447 * 448 * This must be done synchronously as arg and/or extra may point to 449 * temporary data. 450 */ 451 void 452 so_pru_ctlinput(struct protosw *pr, int cmd, struct sockaddr *arg, void *extra) 453 { 454 struct netmsg_pru_ctlinput msg; 455 lwkt_port_t port; 456 457 if (pr->pr_ctlport == NULL) 458 return; 459 KKASSERT(pr->pr_ctlinput != NULL); 460 port = pr->pr_ctlport(cmd, arg, extra); 461 if (port == NULL) 462 return; 463 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 464 netmsg_pru_ctlinput); 465 msg.nm_prufn = pr->pr_ctlinput; 466 msg.nm_cmd = cmd; 467 msg.nm_arg = arg; 468 msg.nm_extra = extra; 469 lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 470 } 471 472 /* 473 * If we convert all the protosw pr_ functions for all the protocols 474 * to take a message directly, this layer can go away. For the moment 475 * our dispatcher ignores the return value, but since we are handling 476 * the replymsg ourselves we return EASYNC by convention. 477 */ 478 479 /* 480 * Abort and destroy a socket. 481 */ 482 void 483 netmsg_pru_abort(netmsg_t msg) 484 { 485 struct netmsg_pru_abort *nm = (void *)msg; 486 struct socket *so = nm->nm_so; 487 int error; 488 489 KKASSERT(so->so_state & SS_ABORTING); 490 so->so_state &= ~SS_ABORTING; 491 error = nm->nm_prufn(so); 492 if (error) 493 sofree(so); 494 lwkt_replymsg(&msg->nm_lmsg, error); 495 } 496 497 #ifdef notused 498 void 499 netmsg_pru_accept(netmsg_t msg) 500 { 501 struct netmsg_pru_accept *nm = (void *)msg; 502 503 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so, nm->nm_nam)); 504 } 505 #endif 506 507 void 508 netmsg_pru_attach(netmsg_t msg) 509 { 510 struct netmsg_pru_attach *nm = (void *)msg; 511 512 lwkt_replymsg(&msg->nm_lmsg, 513 nm->nm_prufn(nm->nm_so, nm->nm_proto, nm->nm_ai)); 514 } 515 516 void 517 netmsg_pru_bind(netmsg_t msg) 518 { 519 struct netmsg_pru_bind *nm = (void *)msg; 520 521 lwkt_replymsg(&msg->nm_lmsg, 522 nm->nm_prufn(nm->nm_so, nm->nm_nam, nm->nm_td)); 523 } 524 525 void 526 netmsg_pru_connect(netmsg_t msg) 527 { 528 struct netmsg_pru_connect *nm = (void *)msg; 529 530 lwkt_replymsg(&msg->nm_lmsg, 531 nm->nm_prufn(nm->nm_so, nm->nm_nam, nm->nm_td)); 532 } 533 534 void 535 netmsg_pru_connect2(netmsg_t msg) 536 { 537 struct netmsg_pru_connect2 *nm = (void *)msg; 538 539 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so1, nm->nm_so2)); 540 } 541 542 void 543 netmsg_pru_control(netmsg_t msg) 544 { 545 struct netmsg_pru_control *nm = (void *)msg; 546 int error; 547 548 error = nm->nm_prufn(nm->nm_so, nm->nm_cmd, nm->nm_data, 549 nm->nm_ifp, nm->nm_td); 550 lwkt_replymsg(&msg->nm_lmsg, error); 551 } 552 553 void 554 netmsg_pru_detach(netmsg_t msg) 555 { 556 struct netmsg_pru_detach *nm = (void *)msg; 557 558 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so)); 559 } 560 561 void 562 netmsg_pru_disconnect(netmsg_t msg) 563 { 564 struct netmsg_pru_disconnect *nm = (void *)msg; 565 566 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so)); 567 } 568 569 void 570 netmsg_pru_listen(netmsg_t msg) 571 { 572 struct netmsg_pru_listen *nm = (void *)msg; 573 574 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so, nm->nm_td)); 575 } 576 577 void 578 netmsg_pru_peeraddr(netmsg_t msg) 579 { 580 struct netmsg_pru_peeraddr *nm = (void *)msg; 581 582 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so, nm->nm_nam)); 583 } 584 585 void 586 netmsg_pru_rcvd(netmsg_t msg) 587 { 588 struct netmsg_pru_rcvd *nm = (void *)msg; 589 590 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so, nm->nm_flags)); 591 } 592 593 void 594 netmsg_pru_rcvoob(netmsg_t msg) 595 { 596 struct netmsg_pru_rcvoob *nm = (void *)msg; 597 598 lwkt_replymsg(&msg->nm_lmsg, 599 nm->nm_prufn(nm->nm_so, nm->nm_m, nm->nm_flags)); 600 } 601 602 void 603 netmsg_pru_send(netmsg_t msg) 604 { 605 struct netmsg_pru_send *nm = (void *)msg; 606 int error; 607 608 error = nm->nm_prufn(nm->nm_so, nm->nm_flags, nm->nm_m, 609 nm->nm_addr, nm->nm_control, nm->nm_td); 610 lwkt_replymsg(&msg->nm_lmsg, error); 611 } 612 613 void 614 netmsg_pru_sense(netmsg_t msg) 615 { 616 struct netmsg_pru_sense *nm = (void *)msg; 617 618 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so, nm->nm_stat)); 619 } 620 621 void 622 netmsg_pru_shutdown(netmsg_t msg) 623 { 624 struct netmsg_pru_shutdown *nm = (void *)msg; 625 626 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so)); 627 } 628 629 void 630 netmsg_pru_sockaddr(netmsg_t msg) 631 { 632 struct netmsg_pru_sockaddr *nm = (void *)msg; 633 634 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so, nm->nm_nam)); 635 } 636 637 void 638 netmsg_pru_sopoll(netmsg_t msg) 639 { 640 struct netmsg_pru_sopoll *nm = (void *)msg; 641 int error; 642 643 error = nm->nm_prufn(nm->nm_so, nm->nm_events, nm->nm_cred, nm->nm_td); 644 lwkt_replymsg(&msg->nm_lmsg, error); 645 } 646 647 void 648 netmsg_pru_ctloutput(netmsg_t msg) 649 { 650 struct netmsg_pru_ctloutput *nm = (void *)msg; 651 652 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prufn(nm->nm_so, nm->nm_sopt)); 653 } 654 655 void 656 netmsg_pru_ctlinput(netmsg_t msg) 657 { 658 struct netmsg_pru_ctlinput *nm = (void *)msg; 659 660 nm->nm_prufn(nm->nm_cmd, nm->nm_arg, nm->nm_extra); 661 lwkt_replymsg(&nm->nm_netmsg.nm_lmsg, 0); 662 } 663 664 void 665 netmsg_pr_timeout(netmsg_t msg) 666 { 667 struct netmsg_pr_timeout *nm = (void *)msg; 668 669 lwkt_replymsg(&msg->nm_lmsg, nm->nm_prfn()); 670 } 671 672 /* 673 * Handle a predicate event request. This function is only called once 674 * when the predicate message queueing request is received. 675 */ 676 void 677 netmsg_so_notify(netmsg_t netmsg) 678 { 679 struct netmsg_so_notify *msg = (void *)netmsg; 680 struct signalsockbuf *ssb; 681 682 ssb = (msg->nm_etype & NM_REVENT) ? 683 &msg->nm_so->so_rcv : 684 &msg->nm_so->so_snd; 685 686 /* 687 * Reply immediately if the event has occured, otherwise queue the 688 * request. 689 */ 690 if (msg->nm_predicate(&msg->nm_netmsg)) { 691 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, 692 msg->nm_netmsg.nm_lmsg.ms_error); 693 } else { 694 TAILQ_INSERT_TAIL(&ssb->ssb_sel.si_mlist, msg, nm_list); 695 ssb->ssb_flags |= SSB_MEVENT; 696 } 697 } 698 699 /* 700 * Called by doio when trying to abort a netmsg_so_notify message. 701 * Unlike the other functions this one is dispatched directly by 702 * the LWKT subsystem, so it takes a lwkt_msg_t as an argument. 703 * 704 * The original message, lmsg, is under the control of the caller and 705 * will not be destroyed until we return so we can safely reference it 706 * in our synchronous abort request. 707 * 708 * This part of the abort request occurs on the originating cpu which 709 * means we may race the message flags and the original message may 710 * not even have been processed by the target cpu yet. 711 */ 712 void 713 netmsg_so_notify_doabort(lwkt_msg_t lmsg) 714 { 715 struct netmsg_so_notify_abort msg; 716 717 if ((lmsg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) { 718 netmsg_init(&msg.nm_netmsg, &curthread->td_msgport, 0, 719 netmsg_so_notify_abort); 720 msg.nm_notifymsg = (void *)lmsg; 721 lwkt_domsg(lmsg->ms_target_port, &msg.nm_netmsg.nm_lmsg, 0); 722 } 723 } 724 725 /* 726 * Predicate requests can be aborted. This function is only called once 727 * and will interlock against processing/reply races (since such races 728 * occur on the same thread that controls the port where the abort is 729 * requeued). 730 * 731 * This part of the abort request occurs on the target cpu. The message 732 * flags must be tested again in case the test that we did on the 733 * originating cpu raced. Since messages are handled in sequence, the 734 * original message will have already been handled by the loop and either 735 * replied to or queued. 736 * 737 * We really only need to interlock with MSGF_REPLY (a bit that is set on 738 * our cpu when we reply). Note that MSGF_DONE is not set until the 739 * reply reaches the originating cpu. Test both bits anyway. 740 */ 741 void 742 netmsg_so_notify_abort(netmsg_t netmsg) 743 { 744 struct netmsg_so_notify_abort *abrtmsg = (void *)netmsg; 745 struct netmsg_so_notify *msg = abrtmsg->nm_notifymsg; 746 struct signalsockbuf *ssb; 747 748 /* 749 * The original notify message is not destroyed until after the 750 * abort request is returned, so we can check its state. 751 */ 752 if ((msg->nm_netmsg.nm_lmsg.ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) { 753 ssb = (msg->nm_etype & NM_REVENT) ? 754 &msg->nm_so->so_rcv : 755 &msg->nm_so->so_snd; 756 TAILQ_REMOVE(&ssb->ssb_sel.si_mlist, msg, nm_list); 757 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, EINTR); 758 } 759 760 /* 761 * Reply to the abort message 762 */ 763 lwkt_replymsg(&abrtmsg->nm_netmsg.nm_lmsg, 0); 764 } 765 766