Pull up following revision(s) (requested by jdolecek in ticket #583): sys/kern/uipc_socket.c: revision 1.308 sys/kern/uipc_syscalls.c: revision 1.211 sys/sys/socketvar.h: revision 1.168 sys/net/if_gre.c: revision 1.185 fix PIPE_SOCKETPAIR variant of pipe1() to apply correctly the 'flags' passed when called via pipe2(2), fixing repeatable process hang during compilation with 'gcc -pipe' refactor fsocreate() to return the new socket and file pointers, expect the caller to call fd_affix() once initialization is fully complete use the new fsocreate() to replace the duplicate open-coded 'flags' handling in makesocket() used for socketpair(2), and in the PIPE_SOCKETPAIR pipe1() this also fixes lib/libc/sys/t_pipe2 pipe2_cloexec test to succeed on PIPE_SOCKETPAIR kernel fixes PR kern/55690diff -r1.302 -r1.302.4.1 src/sys/kern/uipc_socket.c
(martin)
--- src/sys/kern/uipc_socket.c 2022/04/09 23:52:22 1.302
+++ src/sys/kern/uipc_socket.c 2024/02/04 11:20:15 1.302.4.1
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: uipc_socket.c,v 1.302 2022/04/09 23:52:22 riastradh Exp $ */ | 1 | /* $NetBSD: uipc_socket.c,v 1.302.4.1 2024/02/04 11:20:15 martin Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc. | 4 | * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc. | |
5 | * All rights reserved. | 5 | * All rights reserved. | |
6 | * | 6 | * | |
7 | * This code is derived from software contributed to The NetBSD Foundation | 7 | * This code is derived from software contributed to The NetBSD Foundation | |
8 | * by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran. | 8 | * by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran. | |
9 | * | 9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | 10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | 11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | 12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | 13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | 14 | * notice, this list of conditions and the following disclaimer. | |
@@ -61,27 +61,27 @@ | @@ -61,27 +61,27 @@ | |||
61 | * | 61 | * | |
62 | * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95 | 62 | * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95 | |
63 | */ | 63 | */ | |
64 | 64 | |||
65 | /* | 65 | /* | |
66 | * Socket operation routines. | 66 | * Socket operation routines. | |
67 | * | 67 | * | |
68 | * These routines are called by the routines in sys_socket.c or from a | 68 | * These routines are called by the routines in sys_socket.c or from a | |
69 | * system process, and implement the semantics of socket operations by | 69 | * system process, and implement the semantics of socket operations by | |
70 | * switching out to the protocol specific routines. | 70 | * switching out to the protocol specific routines. | |
71 | */ | 71 | */ | |
72 | 72 | |||
73 | #include <sys/cdefs.h> | 73 | #include <sys/cdefs.h> | |
74 | __KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.302 2022/04/09 23:52:22 riastradh Exp $"); | 74 | __KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.302.4.1 2024/02/04 11:20:15 martin Exp $"); | |
75 | 75 | |||
76 | #ifdef _KERNEL_OPT | 76 | #ifdef _KERNEL_OPT | |
77 | #include "opt_compat_netbsd.h" | 77 | #include "opt_compat_netbsd.h" | |
78 | #include "opt_sock_counters.h" | 78 | #include "opt_sock_counters.h" | |
79 | #include "opt_sosend_loan.h" | 79 | #include "opt_sosend_loan.h" | |
80 | #include "opt_mbuftrace.h" | 80 | #include "opt_mbuftrace.h" | |
81 | #include "opt_somaxkva.h" | 81 | #include "opt_somaxkva.h" | |
82 | #include "opt_multiprocessor.h" /* XXX */ | 82 | #include "opt_multiprocessor.h" /* XXX */ | |
83 | #include "opt_sctp.h" | 83 | #include "opt_sctp.h" | |
84 | #endif | 84 | #endif | |
85 | 85 | |||
86 | #include <sys/param.h> | 86 | #include <sys/param.h> | |
87 | #include <sys/systm.h> | 87 | #include <sys/systm.h> | |
@@ -558,64 +558,71 @@ socreate(int dom, struct socket **aso, i | @@ -558,64 +558,71 @@ socreate(int dom, struct socket **aso, i | |||
558 | so->so_state |= SS_NOFDREF; | 558 | so->so_state |= SS_NOFDREF; | |
559 | sofree(so); | 559 | sofree(so); | |
560 | return error; | 560 | return error; | |
561 | } | 561 | } | |
562 | so->so_cred = kauth_cred_dup(l->l_cred); | 562 | so->so_cred = kauth_cred_dup(l->l_cred); | |
563 | sounlock(so); | 563 | sounlock(so); | |
564 | 564 | |||
565 | *aso = so; | 565 | *aso = so; | |
566 | return 0; | 566 | return 0; | |
567 | } | 567 | } | |
568 | 568 | |||
569 | /* | 569 | /* | |
570 | * fsocreate: create a socket and a file descriptor associated with it. | 570 | * fsocreate: create a socket and a file descriptor associated with it. | |
571 | * Returns the allocated file structure in *fpp, but the descriptor | |||
572 | * is not visible yet for the process. | |||
573 | * Caller is responsible for calling fd_affix() for the returned *fpp once | |||
574 | * it's socket initialization is finished successfully, or fd_abort() if it's | |||
575 | * initialization fails. | |||
576 | * | |||
571 | * | 577 | * | |
572 | * => On success, write file descriptor to fdout and return zero. | 578 | * => On success, write file descriptor to *fdout and *fpp and return zero. | |
573 | * => On failure, return non-zero; *fdout will be undefined. | 579 | * => On failure, return non-zero; *fdout and *fpp will be undefined. | |
574 | */ | 580 | */ | |
575 | int | 581 | int | |
576 | fsocreate(int domain, struct socket **sop, int type, int proto, int *fdout) | 582 | fsocreate(int domain, struct socket **sop, int type, int proto, int *fdout, | |
583 | file_t **fpp, struct socket *lockso) | |||
577 | { | 584 | { | |
578 | lwp_t *l = curlwp; | 585 | lwp_t *l = curlwp; | |
579 | int error, fd, flags; | 586 | int error, fd, flags; | |
580 | struct socket *so; | 587 | struct socket *so; | |
581 | struct file *fp; | 588 | file_t *fp; | |
589 | ||||
590 | flags = type & SOCK_FLAGS_MASK; | |||
591 | type &= ~SOCK_FLAGS_MASK; | |||
592 | error = socreate(domain, &so, type, proto, l, lockso); | |||
593 | if (error) { | |||
594 | return error; | |||
595 | } | |||
582 | 596 | |||
583 | if ((error = fd_allocfile(&fp, &fd)) != 0) { | 597 | if ((error = fd_allocfile(&fp, &fd)) != 0) { | |
598 | soclose(so); | |||
584 | return error; | 599 | return error; | |
585 | } | 600 | } | |
586 | flags = type & SOCK_FLAGS_MASK; | |||
587 | fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0); | 601 | fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0); | |
588 | fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| | 602 | fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| | |
589 | ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); | 603 | ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); | |
590 | fp->f_type = DTYPE_SOCKET; | 604 | fp->f_type = DTYPE_SOCKET; | |
591 | fp->f_ops = &socketops; | 605 | fp->f_ops = &socketops; | |
592 | ||||
593 | type &= ~SOCK_FLAGS_MASK; | |||
594 | error = socreate(domain, &so, type, proto, l, NULL); | |||
595 | if (error) { | |||
596 | fd_abort(curproc, fp, fd); | |||
597 | return error; | |||
598 | } | |||
599 | if (flags & SOCK_NONBLOCK) { | 606 | if (flags & SOCK_NONBLOCK) { | |
600 | so->so_state |= SS_NBIO; | 607 | so->so_state |= SS_NBIO; | |
601 | } | 608 | } | |
602 | fp->f_socket = so; | 609 | fp->f_socket = so; | |
603 | fd_affix(curproc, fp, fd); | |||
604 | 610 | |||
605 | if (sop != NULL) { | 611 | if (sop != NULL) { | |
606 | *sop = so; | 612 | *sop = so; | |
607 | } | 613 | } | |
608 | *fdout = fd; | 614 | *fdout = fd; | |
615 | *fpp = fp; | |||
609 | return error; | 616 | return error; | |
610 | } | 617 | } | |
611 | 618 | |||
612 | int | 619 | int | |
613 | sofamily(const struct socket *so) | 620 | sofamily(const struct socket *so) | |
614 | { | 621 | { | |
615 | const struct protosw *pr; | 622 | const struct protosw *pr; | |
616 | const struct domain *dom; | 623 | const struct domain *dom; | |
617 | 624 | |||
618 | if ((pr = so->so_proto) == NULL) | 625 | if ((pr = so->so_proto) == NULL) | |
619 | return AF_UNSPEC; | 626 | return AF_UNSPEC; | |
620 | if ((dom = pr->pr_domain) == NULL) | 627 | if ((dom = pr->pr_domain) == NULL) | |
621 | return AF_UNSPEC; | 628 | return AF_UNSPEC; |
--- src/sys/kern/uipc_syscalls.c 2022/07/01 22:30:51 1.206
+++ src/sys/kern/uipc_syscalls.c 2024/02/04 11:20:15 1.206.4.1
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: uipc_syscalls.c,v 1.206 2022/07/01 22:30:51 riastradh Exp $ */ | 1 | /* $NetBSD: uipc_syscalls.c,v 1.206.4.1 2024/02/04 11:20:15 martin Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. | 4 | * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. | |
5 | * All rights reserved. | 5 | * All rights reserved. | |
6 | * | 6 | * | |
7 | * This code is derived from software contributed to The NetBSD Foundation | 7 | * This code is derived from software contributed to The NetBSD Foundation | |
8 | * by Andrew Doran. | 8 | * by Andrew Doran. | |
9 | * | 9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | 10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | 11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | 12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | 13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | 14 | * notice, this list of conditions and the following disclaimer. | |
@@ -51,27 +51,27 @@ | @@ -51,27 +51,27 @@ | |||
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | 58 | * SUCH DAMAGE. | |
59 | * | 59 | * | |
60 | * @(#)uipc_syscalls.c 8.6 (Berkeley) 2/14/95 | 60 | * @(#)uipc_syscalls.c 8.6 (Berkeley) 2/14/95 | |
61 | */ | 61 | */ | |
62 | 62 | |||
63 | #include <sys/cdefs.h> | 63 | #include <sys/cdefs.h> | |
64 | __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.206 2022/07/01 22:30:51 riastradh Exp $"); | 64 | __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.206.4.1 2024/02/04 11:20:15 martin Exp $"); | |
65 | 65 | |||
66 | #ifdef _KERNEL_OPT | 66 | #ifdef _KERNEL_OPT | |
67 | #include "opt_pipe.h" | 67 | #include "opt_pipe.h" | |
68 | #include "opt_sctp.h" | 68 | #include "opt_sctp.h" | |
69 | #endif | 69 | #endif | |
70 | 70 | |||
71 | #define MBUFTYPES | 71 | #define MBUFTYPES | |
72 | #include <sys/param.h> | 72 | #include <sys/param.h> | |
73 | #include <sys/systm.h> | 73 | #include <sys/systm.h> | |
74 | #include <sys/filedesc.h> | 74 | #include <sys/filedesc.h> | |
75 | #include <sys/proc.h> | 75 | #include <sys/proc.h> | |
76 | #include <sys/file.h> | 76 | #include <sys/file.h> | |
77 | #include <sys/buf.h> | 77 | #include <sys/buf.h> | |
@@ -101,30 +101,32 @@ extern const struct fileops socketops; | @@ -101,30 +101,32 @@ extern const struct fileops socketops; | |||
101 | 101 | |||
102 | static int sockargs_sb(struct sockaddr_big *, const void *, socklen_t); | 102 | static int sockargs_sb(struct sockaddr_big *, const void *, socklen_t); | |
103 | 103 | |||
104 | int | 104 | int | |
105 | sys___socket30(struct lwp *l, const struct sys___socket30_args *uap, | 105 | sys___socket30(struct lwp *l, const struct sys___socket30_args *uap, | |
106 | register_t *retval) | 106 | register_t *retval) | |
107 | { | 107 | { | |
108 | /* { | 108 | /* { | |
109 | syscallarg(int) domain; | 109 | syscallarg(int) domain; | |
110 | syscallarg(int) type; | 110 | syscallarg(int) type; | |
111 | syscallarg(int) protocol; | 111 | syscallarg(int) protocol; | |
112 | } */ | 112 | } */ | |
113 | int fd, error; | 113 | int fd, error; | |
114 | file_t *fp; | |||
114 | 115 | |||
115 | error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type), | 116 | error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type), | |
116 | SCARG(uap, protocol), &fd); | 117 | SCARG(uap, protocol), &fd, &fp, NULL); | |
117 | if (error == 0) { | 118 | if (error == 0) { | |
119 | fd_affix(l->l_proc, fp, fd); | |||
118 | *retval = fd; | 120 | *retval = fd; | |
119 | } | 121 | } | |
120 | return error; | 122 | return error; | |
121 | } | 123 | } | |
122 | 124 | |||
123 | int | 125 | int | |
124 | sys_bind(struct lwp *l, const struct sys_bind_args *uap, register_t *retval) | 126 | sys_bind(struct lwp *l, const struct sys_bind_args *uap, register_t *retval) | |
125 | { | 127 | { | |
126 | /* { | 128 | /* { | |
127 | syscallarg(int) s; | 129 | syscallarg(int) s; | |
128 | syscallarg(const struct sockaddr *) name; | 130 | syscallarg(const struct sockaddr *) name; | |
129 | syscallarg(unsigned int) namelen; | 131 | syscallarg(unsigned int) namelen; | |
130 | } */ | 132 | } */ | |
@@ -392,83 +394,53 @@ do_sys_connect(struct lwp *l, int fd, st | @@ -392,83 +394,53 @@ do_sys_connect(struct lwp *l, int fd, st | |||
392 | so->so_error = 0; | 394 | so->so_error = 0; | |
393 | } | 395 | } | |
394 | bad: | 396 | bad: | |
395 | if (!interrupted) | 397 | if (!interrupted) | |
396 | so->so_state &= ~SS_ISCONNECTING; | 398 | so->so_state &= ~SS_ISCONNECTING; | |
397 | if (error == ERESTART) | 399 | if (error == ERESTART) | |
398 | error = EINTR; | 400 | error = EINTR; | |
399 | out: | 401 | out: | |
400 | sounlock(so); | 402 | sounlock(so); | |
401 | fd_putfile(fd); | 403 | fd_putfile(fd); | |
402 | return error; | 404 | return error; | |
403 | } | 405 | } | |
404 | 406 | |||
405 | static int | |||
406 | makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type, | |||
407 | int domain, int proto, struct socket *soo) | |||
408 | { | |||
409 | struct socket *so; | |||
410 | int error; | |||
411 | ||||
412 | if ((error = socreate(domain, &so, type, proto, l, soo)) != 0) { | |||
413 | return error; | |||
414 | } | |||
415 | if (flags & SOCK_NONBLOCK) { | |||
416 | so->so_state |= SS_NBIO; | |||
417 | } | |||
418 | ||||
419 | if ((error = fd_allocfile(fp, fd)) != 0) { | |||
420 | soclose(so); | |||
421 | return error; | |||
422 | } | |||
423 | fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0); | |||
424 | (*fp)->f_flag = FREAD|FWRITE| | |||
425 | ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)| | |||
426 | ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0); | |||
427 | (*fp)->f_type = DTYPE_SOCKET; | |||
428 | (*fp)->f_ops = &socketops; | |||
429 | (*fp)->f_socket = so; | |||
430 | return 0; | |||
431 | } | |||
432 | ||||
433 | int | 407 | int | |
434 | sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap, | 408 | sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap, | |
435 | register_t *retval) | 409 | register_t *retval) | |
436 | { | 410 | { | |
437 | /* { | 411 | /* { | |
438 | syscallarg(int) domain; | 412 | syscallarg(int) domain; | |
439 | syscallarg(int) type; | 413 | syscallarg(int) type; | |
440 | syscallarg(int) protocol; | 414 | syscallarg(int) protocol; | |
441 | syscallarg(int *) rsv; | 415 | syscallarg(int *) rsv; | |
442 | } */ | 416 | } */ | |
443 | file_t *fp1, *fp2; | 417 | file_t *fp1, *fp2; | |
444 | struct socket *so1, *so2; | 418 | struct socket *so1, *so2; | |
445 | int fd, error, sv[2]; | 419 | int fd, error, sv[2]; | |
446 | proc_t *p = curproc; | 420 | proc_t *p = curproc; | |
447 | int flags = SCARG(uap, type) & SOCK_FLAGS_MASK; | 421 | int flags = SCARG(uap, type) & SOCK_FLAGS_MASK; | |
448 | int type = SCARG(uap, type) & ~SOCK_FLAGS_MASK; | 422 | int type = SCARG(uap, type) & ~SOCK_FLAGS_MASK; | |
449 | int domain = SCARG(uap, domain); | 423 | int domain = SCARG(uap, domain); | |
450 | int proto = SCARG(uap, protocol); | 424 | int proto = SCARG(uap, protocol); | |
451 | 425 | |||
452 | error = makesocket(l, &fp1, &fd, flags, type, domain, proto, NULL); | 426 | error = fsocreate(domain, &so1, type|flags, proto, &fd, &fp1, NULL); | |
453 | if (error) | 427 | if (error) | |
454 | return error; | 428 | return error; | |
455 | so1 = fp1->f_socket; | |||
456 | sv[0] = fd; | 429 | sv[0] = fd; | |
457 | 430 | |||
458 | error = makesocket(l, &fp2, &fd, flags, type, domain, proto, so1); | 431 | error = fsocreate(domain, &so2, type|flags, proto, &fd, &fp2, so1); | |
459 | if (error) | 432 | if (error) | |
460 | goto out; | 433 | goto out; | |
461 | so2 = fp2->f_socket; | |||
462 | sv[1] = fd; | 434 | sv[1] = fd; | |
463 | 435 | |||
464 | solock(so1); | 436 | solock(so1); | |
465 | error = soconnect2(so1, so2); | 437 | error = soconnect2(so1, so2); | |
466 | if (error == 0 && type == SOCK_DGRAM) { | 438 | if (error == 0 && type == SOCK_DGRAM) { | |
467 | /* | 439 | /* | |
468 | * Datagram socket connection is asymmetric. | 440 | * Datagram socket connection is asymmetric. | |
469 | */ | 441 | */ | |
470 | error = soconnect2(so2, so1); | 442 | error = soconnect2(so2, so1); | |
471 | } | 443 | } | |
472 | sounlock(so1); | 444 | sounlock(so1); | |
473 | 445 | |||
474 | if (error == 0) | 446 | if (error == 0) | |
@@ -1287,84 +1259,90 @@ sys_getsockopt2(struct lwp *l, const str | @@ -1287,84 +1259,90 @@ sys_getsockopt2(struct lwp *l, const str | |||
1287 | syscallarg(void *) val; | 1259 | syscallarg(void *) val; | |
1288 | syscallarg(unsigned int *) avalsize; | 1260 | syscallarg(unsigned int *) avalsize; | |
1289 | } */ | 1261 | } */ | |
1290 | return getsockopt(l, (const struct sys_getsockopt_args *) uap, retval, true); | 1262 | return getsockopt(l, (const struct sys_getsockopt_args *) uap, retval, true); | |
1291 | } | 1263 | } | |
1292 | 1264 | |||
1293 | #ifdef PIPE_SOCKETPAIR | 1265 | #ifdef PIPE_SOCKETPAIR | |
1294 | 1266 | |||
1295 | int | 1267 | int | |
1296 | pipe1(struct lwp *l, int *fildes, int flags) | 1268 | pipe1(struct lwp *l, int *fildes, int flags) | |
1297 | { | 1269 | { | |
1298 | file_t *rf, *wf; | 1270 | file_t *rf, *wf; | |
1299 | struct socket *rso, *wso; | 1271 | struct socket *rso, *wso; | |
1300 | int fd, error; | 1272 | int error, soflags = 0; | |
1301 | proc_t *p; | 1273 | unsigned rfd, wfd; | |
1274 | proc_t *p = l->l_proc; | |||
1302 | 1275 | |||
1303 | if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE)) | 1276 | if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE)) | |
1304 | return EINVAL; | 1277 | return EINVAL; | |
1305 | p = curproc; | 1278 | if (flags & O_CLOEXEC) | |
1306 | if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0) | 1279 | soflags |= SOCK_CLOEXEC; | |
1307 | return error; | 1280 | if (flags & O_NONBLOCK) | |
1308 | if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0) | 1281 | soflags |= SOCK_NONBLOCK; | |
1282 | if (flags & O_NOSIGPIPE) | |||
1283 | soflags |= SOCK_NOSIGPIPE; | |||
1284 | ||||
1285 | error = fsocreate(AF_LOCAL, &rso, SOCK_STREAM|soflags, 0, &rfd, &rf, | |||
1286 | NULL); | |||
1287 | if (error) | |||
1309 | goto free1; | 1288 | goto free1; | |
1289 | error = fsocreate(AF_LOCAL, &wso, SOCK_STREAM|soflags, 0, &wfd, &wf, | |||
1290 | rso); | |||
1291 | if (error) | |||
1292 | goto free2; | |||
1293 | ||||
1294 | /* make sure the descriptors are uni-directional */ | |||
1295 | rf->f_type = rf->f_type & ~(FWRITE); | |||
1296 | wf->f_type = wf->f_type & ~(FREAD); | |||
1297 | ||||
1310 | /* remember this socket pair implements a pipe */ | 1298 | /* remember this socket pair implements a pipe */ | |
1311 | wso->so_state |= SS_ISAPIPE; | |||
1312 | rso->so_state |= SS_ISAPIPE; | 1299 | rso->so_state |= SS_ISAPIPE; | |
1313 | if ((error = fd_allocfile(&rf, &fd)) != 0) | 1300 | wso->so_state |= SS_ISAPIPE; | |
1314 | goto free2; | 1301 | ||
1315 | fildes[0] = fd; | |||
1316 | rf->f_flag = FREAD | flags; | |||
1317 | rf->f_type = DTYPE_SOCKET; | |||
1318 | rf->f_ops = &socketops; | |||
1319 | rf->f_socket = rso; | |||
1320 | if ((error = fd_allocfile(&wf, &fd)) != 0) | |||
1321 | goto free3; | |||
1322 | wf->f_flag = FWRITE | flags; | |||
1323 | wf->f_type = DTYPE_SOCKET; | |||
1324 | wf->f_ops = &socketops; | |||
1325 | wf->f_socket = wso; | |||
1326 | fildes[1] = fd; | |||
1327 | solock(wso); | 1302 | solock(wso); | |
1328 | /* | 1303 | /* | |
1329 | * Pipes must be readable when there is at least 1 | 1304 | * Pipes must be readable when there is at least 1 | |
1330 | * byte of data available in the receive buffer. | 1305 | * byte of data available in the receive buffer. | |
1331 | * | 1306 | * | |
1332 | * Pipes must be writable when there is space for | 1307 | * Pipes must be writable when there is space for | |
1333 | * at least PIPE_BUF bytes in the send buffer. | 1308 | * at least PIPE_BUF bytes in the send buffer. | |
1334 | * If we're increasing the low water mark for the | 1309 | * If we're increasing the low water mark for the | |
1335 | * send buffer, then mimic how soreserve() would | 1310 | * send buffer, then mimic how soreserve() would | |
1336 | * have set the high water mark. | 1311 | * have set the high water mark. | |
1337 | */ | 1312 | */ | |
1338 | rso->so_rcv.sb_lowat = 1; | 1313 | rso->so_rcv.sb_lowat = 1; | |
1339 | if (wso->so_snd.sb_lowat < PIPE_BUF) { | 1314 | if (wso->so_snd.sb_lowat < PIPE_BUF) { | |
1340 | wso->so_snd.sb_hiwat = PIPE_BUF * 2; | 1315 | wso->so_snd.sb_hiwat = PIPE_BUF * 2; | |
1341 | } | 1316 | } | |
1342 | wso->so_snd.sb_lowat = PIPE_BUF; | 1317 | wso->so_snd.sb_lowat = PIPE_BUF; | |
1343 | error = unp_connect2(wso, rso); | 1318 | error = unp_connect2(wso, rso); | |
1344 | sounlock(wso); | 1319 | sounlock(wso); | |
1320 | ||||
1345 | if (error != 0) | 1321 | if (error != 0) | |
1346 | goto free4; | 1322 | goto free3; | |
1347 | fd_affix(p, wf, fildes[1]); | 1323 | ||
1348 | fd_affix(p, rf, fildes[0]); | 1324 | fd_affix(p, wf, wfd); | |
1325 | fd_affix(p, rf, rfd); | |||
1326 | fildes[0] = rfd; | |||
1327 | fildes[1] = wfd; | |||
1349 | return (0); | 1328 | return (0); | |
1350 | free4: | |||
1351 | fd_abort(p, wf, fildes[1]); | |||
1352 | free3: | 1329 | free3: | |
1353 | fd_abort(p, rf, fildes[0]); | |||
1354 | free2: | |||
1355 | (void)soclose(wso); | 1330 | (void)soclose(wso); | |
1356 | free1: | 1331 | fd_abort(p, wf, wfd); | |
1332 | free2: | |||
1357 | (void)soclose(rso); | 1333 | (void)soclose(rso); | |
1334 | fd_abort(p, rf, rfd); | |||
1335 | free1: | |||
1358 | return error; | 1336 | return error; | |
1359 | } | 1337 | } | |
1360 | #endif /* PIPE_SOCKETPAIR */ | 1338 | #endif /* PIPE_SOCKETPAIR */ | |
1361 | 1339 | |||
1362 | /* | 1340 | /* | |
1363 | * Get peer socket name. | 1341 | * Get peer socket name. | |
1364 | */ | 1342 | */ | |
1365 | int | 1343 | int | |
1366 | do_sys_getpeername(int fd, struct sockaddr *nam) | 1344 | do_sys_getpeername(int fd, struct sockaddr *nam) | |
1367 | { | 1345 | { | |
1368 | struct socket *so; | 1346 | struct socket *so; | |
1369 | int error; | 1347 | int error; | |
1370 | 1348 |
--- src/sys/net/if_gre.c 2022/09/03 02:47:59 1.184
+++ src/sys/net/if_gre.c 2024/02/04 11:20:15 1.184.4.1
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: if_gre.c,v 1.184 2022/09/03 02:47:59 thorpej Exp $ */ | 1 | /* $NetBSD: if_gre.c,v 1.184.4.1 2024/02/04 11:20:15 martin Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc. | 4 | * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc. | |
5 | * All rights reserved. | 5 | * All rights reserved. | |
6 | * | 6 | * | |
7 | * This code is derived from software contributed to The NetBSD Foundation | 7 | * This code is derived from software contributed to The NetBSD Foundation | |
8 | * by Heiko W.Rupp <hwr@pilhuhn.de> | 8 | * by Heiko W.Rupp <hwr@pilhuhn.de> | |
9 | * | 9 | * | |
10 | * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de> | 10 | * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de> | |
11 | * | 11 | * | |
12 | * GRE over UDP/IPv4/IPv6 sockets contributed by David Young <dyoung@NetBSD.org> | 12 | * GRE over UDP/IPv4/IPv6 sockets contributed by David Young <dyoung@NetBSD.org> | |
13 | * | 13 | * | |
14 | * Redistribution and use in source and binary forms, with or without | 14 | * Redistribution and use in source and binary forms, with or without | |
@@ -35,27 +35,27 @@ | @@ -35,27 +35,27 @@ | |||
35 | * This material is based upon work partially supported by NSF | 35 | * This material is based upon work partially supported by NSF | |
36 | * under Contract No. NSF CNS-0626584. | 36 | * under Contract No. NSF CNS-0626584. | |
37 | */ | 37 | */ | |
38 | 38 | |||
39 | /* | 39 | /* | |
40 | * Encapsulate L3 protocols into IP | 40 | * Encapsulate L3 protocols into IP | |
41 | * See RFC 1701 and 1702 for more details. | 41 | * See RFC 1701 and 1702 for more details. | |
42 | * If_gre is compatible with Cisco GRE tunnels, so you can | 42 | * If_gre is compatible with Cisco GRE tunnels, so you can | |
43 | * have a NetBSD box as the other end of a tunnel interface of a Cisco | 43 | * have a NetBSD box as the other end of a tunnel interface of a Cisco | |
44 | * router. See gre(4) for more details. | 44 | * router. See gre(4) for more details. | |
45 | */ | 45 | */ | |
46 | 46 | |||
47 | #include <sys/cdefs.h> | 47 | #include <sys/cdefs.h> | |
48 | __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.184 2022/09/03 02:47:59 thorpej Exp $"); | 48 | __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.184.4.1 2024/02/04 11:20:15 martin Exp $"); | |
49 | 49 | |||
50 | #ifdef _KERNEL_OPT | 50 | #ifdef _KERNEL_OPT | |
51 | #include "opt_atalk.h" | 51 | #include "opt_atalk.h" | |
52 | #include "opt_gre.h" | 52 | #include "opt_gre.h" | |
53 | #include "opt_inet.h" | 53 | #include "opt_inet.h" | |
54 | #include "opt_mpls.h" | 54 | #include "opt_mpls.h" | |
55 | #endif | 55 | #endif | |
56 | 56 | |||
57 | #include <sys/param.h> | 57 | #include <sys/param.h> | |
58 | #include <sys/file.h> | 58 | #include <sys/file.h> | |
59 | #include <sys/filedesc.h> | 59 | #include <sys/filedesc.h> | |
60 | #include <sys/malloc.h> | 60 | #include <sys/malloc.h> | |
61 | #include <sys/mallocvar.h> | 61 | #include <sys/mallocvar.h> | |
@@ -425,43 +425,41 @@ gre_upcall_add(struct socket *so, void * | @@ -425,43 +425,41 @@ gre_upcall_add(struct socket *so, void * | |||
425 | 425 | |||
426 | static void | 426 | static void | |
427 | gre_upcall_remove(struct socket *so) | 427 | gre_upcall_remove(struct socket *so) | |
428 | { | 428 | { | |
429 | so->so_rcv.sb_flags &= ~SB_UPCALL; | 429 | so->so_rcv.sb_flags &= ~SB_UPCALL; | |
430 | so->so_upcallarg = NULL; | 430 | so->so_upcallarg = NULL; | |
431 | so->so_upcall = NULL; | 431 | so->so_upcall = NULL; | |
432 | } | 432 | } | |
433 | 433 | |||
434 | static int | 434 | static int | |
435 | gre_socreate(struct gre_softc *sc, const struct gre_soparm *sp, int *fdout) | 435 | gre_socreate(struct gre_softc *sc, const struct gre_soparm *sp, int *fdout) | |
436 | { | 436 | { | |
437 | int fd, rc; | 437 | int fd, rc; | |
438 | file_t *fp; | |||
438 | struct socket *so; | 439 | struct socket *so; | |
439 | struct sockaddr_big sbig; | 440 | struct sockaddr_big sbig; | |
440 | sa_family_t af; | 441 | sa_family_t af; | |
441 | int val; | 442 | int val; | |
442 | 443 | |||
443 | GRE_DPRINTF(sc, "enter\n"); | 444 | GRE_DPRINTF(sc, "enter\n"); | |
444 | 445 | |||
445 | af = sp->sp_src.ss_family; | 446 | af = sp->sp_src.ss_family; | |
446 | rc = fsocreate(af, NULL, sp->sp_type, sp->sp_proto, &fd); | 447 | rc = fsocreate(af, &so, sp->sp_type, sp->sp_proto, &fd, &fp, NULL); | |
447 | if (rc != 0) { | 448 | if (rc != 0) { | |
448 | GRE_DPRINTF(sc, "fsocreate failed\n"); | 449 | GRE_DPRINTF(sc, "fsocreate failed\n"); | |
449 | return rc; | 450 | return rc; | |
450 | } | 451 | } | |
451 | 452 | |||
452 | if ((rc = fd_getsock(fd, &so)) != 0) | |||
453 | return rc; | |||
454 | ||||
455 | memcpy(&sbig, &sp->sp_src, sizeof(sp->sp_src)); | 453 | memcpy(&sbig, &sp->sp_src, sizeof(sp->sp_src)); | |
456 | if ((rc = sobind(so, (struct sockaddr *)&sbig, curlwp)) != 0) { | 454 | if ((rc = sobind(so, (struct sockaddr *)&sbig, curlwp)) != 0) { | |
457 | GRE_DPRINTF(sc, "sobind failed\n"); | 455 | GRE_DPRINTF(sc, "sobind failed\n"); | |
458 | goto out; | 456 | goto out; | |
459 | } | 457 | } | |
460 | 458 | |||
461 | memcpy(&sbig, &sp->sp_dst, sizeof(sp->sp_dst)); | 459 | memcpy(&sbig, &sp->sp_dst, sizeof(sp->sp_dst)); | |
462 | solock(so); | 460 | solock(so); | |
463 | if ((rc = soconnect(so, (struct sockaddr *)&sbig, curlwp)) != 0) { | 461 | if ((rc = soconnect(so, (struct sockaddr *)&sbig, curlwp)) != 0) { | |
464 | GRE_DPRINTF(sc, "soconnect failed\n"); | 462 | GRE_DPRINTF(sc, "soconnect failed\n"); | |
465 | sounlock(so); | 463 | sounlock(so); | |
466 | goto out; | 464 | goto out; | |
467 | } | 465 | } | |
@@ -474,30 +472,31 @@ gre_socreate(struct gre_softc *sc, const | @@ -474,30 +472,31 @@ gre_socreate(struct gre_softc *sc, const | |||
474 | if (rc != 0) { | 472 | if (rc != 0) { | |
475 | GRE_DPRINTF(sc, "so_setsockopt ttl failed\n"); | 473 | GRE_DPRINTF(sc, "so_setsockopt ttl failed\n"); | |
476 | rc = 0; | 474 | rc = 0; | |
477 | } | 475 | } | |
478 | 476 | |||
479 | val = 1; | 477 | val = 1; | |
480 | rc = so_setsockopt(curlwp, so, SOL_SOCKET, SO_NOHEADER, | 478 | rc = so_setsockopt(curlwp, so, SOL_SOCKET, SO_NOHEADER, | |
481 | &val, sizeof(val)); | 479 | &val, sizeof(val)); | |
482 | if (rc != 0) { | 480 | if (rc != 0) { | |
483 | GRE_DPRINTF(sc, "so_setsockopt SO_NOHEADER failed\n"); | 481 | GRE_DPRINTF(sc, "so_setsockopt SO_NOHEADER failed\n"); | |
484 | rc = 0; | 482 | rc = 0; | |
485 | } | 483 | } | |
486 | out: | 484 | out: | |
487 | if (rc != 0) | 485 | if (rc != 0) { | |
488 | fd_close(fd); | 486 | soclose(so); | |
489 | else { | 487 | fd_abort(curproc, fp, fd); | |
490 | fd_putfile(fd); | 488 | } else { | |
489 | fd_affix(curproc, fp, fd); | |||
491 | *fdout = fd; | 490 | *fdout = fd; | |
492 | } | 491 | } | |
493 | 492 | |||
494 | return rc; | 493 | return rc; | |
495 | } | 494 | } | |
496 | 495 | |||
497 | static int | 496 | static int | |
498 | gre_sosend(struct socket *so, struct mbuf *top) | 497 | gre_sosend(struct socket *so, struct mbuf *top) | |
499 | { | 498 | { | |
500 | struct proc *p; | 499 | struct proc *p; | |
501 | long space, resid; | 500 | long space, resid; | |
502 | int error; | 501 | int error; | |
503 | struct lwp * const l = curlwp; | 502 | struct lwp * const l = curlwp; |
--- src/sys/sys/socketvar.h 2022/04/09 23:52:23 1.165
+++ src/sys/sys/socketvar.h 2024/02/04 11:20:15 1.165.4.1
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: socketvar.h,v 1.165 2022/04/09 23:52:23 riastradh Exp $ */ | 1 | /* $NetBSD: socketvar.h,v 1.165.4.1 2024/02/04 11:20:15 martin Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. | 4 | * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. | |
5 | * All rights reserved. | 5 | * All rights reserved. | |
6 | * | 6 | * | |
7 | * This code is derived from software contributed to The NetBSD Foundation | 7 | * This code is derived from software contributed to The NetBSD Foundation | |
8 | * by Andrew Doran. | 8 | * by Andrew Doran. | |
9 | * | 9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | 10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | 11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | 12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | 13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | 14 | * notice, this list of conditions and the following disclaimer. | |
@@ -296,27 +296,28 @@ void soinit1(void); | @@ -296,27 +296,28 @@ void soinit1(void); | |||
296 | void soinit2(void); | 296 | void soinit2(void); | |
297 | int soabort(struct socket *); | 297 | int soabort(struct socket *); | |
298 | int soaccept(struct socket *, struct sockaddr *); | 298 | int soaccept(struct socket *, struct sockaddr *); | |
299 | int sofamily(const struct socket *); | 299 | int sofamily(const struct socket *); | |
300 | int sobind(struct socket *, struct sockaddr *, struct lwp *); | 300 | int sobind(struct socket *, struct sockaddr *, struct lwp *); | |
301 | void socantrcvmore(struct socket *); | 301 | void socantrcvmore(struct socket *); | |
302 | void socantsendmore(struct socket *); | 302 | void socantsendmore(struct socket *); | |
303 | void soroverflow(struct socket *); | 303 | void soroverflow(struct socket *); | |
304 | int soclose(struct socket *); | 304 | int soclose(struct socket *); | |
305 | int soconnect(struct socket *, struct sockaddr *, struct lwp *); | 305 | int soconnect(struct socket *, struct sockaddr *, struct lwp *); | |
306 | int soconnect2(struct socket *, struct socket *); | 306 | int soconnect2(struct socket *, struct socket *); | |
307 | int socreate(int, struct socket **, int, int, struct lwp *, | 307 | int socreate(int, struct socket **, int, int, struct lwp *, | |
308 | struct socket *); | 308 | struct socket *); | |
309 | int fsocreate(int, struct socket **, int, int, int *); | 309 | int fsocreate(int, struct socket **, int, int, int *, file_t **, | |
310 | struct socket *); | |||
310 | int sodisconnect(struct socket *); | 311 | int sodisconnect(struct socket *); | |
311 | void sofree(struct socket *); | 312 | void sofree(struct socket *); | |
312 | int sogetopt(struct socket *, struct sockopt *); | 313 | int sogetopt(struct socket *, struct sockopt *); | |
313 | void sohasoutofband(struct socket *); | 314 | void sohasoutofband(struct socket *); | |
314 | void soisconnected(struct socket *); | 315 | void soisconnected(struct socket *); | |
315 | void soisconnecting(struct socket *); | 316 | void soisconnecting(struct socket *); | |
316 | void soisdisconnected(struct socket *); | 317 | void soisdisconnected(struct socket *); | |
317 | void soisdisconnecting(struct socket *); | 318 | void soisdisconnecting(struct socket *); | |
318 | int solisten(struct socket *, int, struct lwp *); | 319 | int solisten(struct socket *, int, struct lwp *); | |
319 | struct socket * | 320 | struct socket * | |
320 | sonewconn(struct socket *, bool); | 321 | sonewconn(struct socket *, bool); | |
321 | void soqinsque(struct socket *, struct socket *, int); | 322 | void soqinsque(struct socket *, struct socket *, int); | |
322 | bool soqremque(struct socket *, int); | 323 | bool soqremque(struct socket *, int); |