Pull up following revision(s) (requested by bouyer in ticket #34): sys/netcan/can_pcb.c: revision 1.6 tests/net/can/t_can.c: revision 1.6 Refuse to bind to a non-CAN interface. Also release the lock in the error branch. -- Test bind()ing to a non-existent interface.diff -r1.5 -r1.5.2.1 src/sys/netcan/can_pcb.c
(snj)
--- src/sys/netcan/can_pcb.c 2017/06/01 02:45:14 1.5
+++ src/sys/netcan/can_pcb.c 2017/06/15 05:32:35 1.5.2.1
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: can_pcb.c,v 1.5 2017/06/01 02:45:14 chs Exp $ */ | 1 | /* $NetBSD: can_pcb.c,v 1.5.2.1 2017/06/15 05:32:35 snj Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. | 4 | * Copyright (c) 2003, 2017 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 Robert Swindells and Manuel Bouyer | 8 | * by Robert Swindells and Manuel Bouyer | |
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. | |
@@ -20,27 +20,27 @@ | @@ -20,27 +20,27 @@ | |||
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | 29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | 30 | */ | |
31 | 31 | |||
32 | #include <sys/cdefs.h> | 32 | #include <sys/cdefs.h> | |
33 | __KERNEL_RCSID(0, "$NetBSD: can_pcb.c,v 1.5 2017/06/01 02:45:14 chs Exp $"); | 33 | __KERNEL_RCSID(0, "$NetBSD: can_pcb.c,v 1.5.2.1 2017/06/15 05:32:35 snj Exp $"); | |
34 | 34 | |||
35 | #include <sys/param.h> | 35 | #include <sys/param.h> | |
36 | #include <sys/systm.h> | 36 | #include <sys/systm.h> | |
37 | #include <sys/malloc.h> | 37 | #include <sys/malloc.h> | |
38 | #include <sys/kmem.h> | 38 | #include <sys/kmem.h> | |
39 | #include <sys/mbuf.h> | 39 | #include <sys/mbuf.h> | |
40 | #include <sys/protosw.h> | 40 | #include <sys/protosw.h> | |
41 | #include <sys/socket.h> | 41 | #include <sys/socket.h> | |
42 | #include <sys/socketvar.h> | 42 | #include <sys/socketvar.h> | |
43 | #include <sys/ioctl.h> | 43 | #include <sys/ioctl.h> | |
44 | #include <sys/errno.h> | 44 | #include <sys/errno.h> | |
45 | #include <sys/time.h> | 45 | #include <sys/time.h> | |
46 | #include <sys/pool.h> | 46 | #include <sys/pool.h> | |
@@ -117,28 +117,32 @@ can_pcballoc(struct socket *so, void *v) | @@ -117,28 +117,32 @@ can_pcballoc(struct socket *so, void *v) | |||
117 | return (0); | 117 | return (0); | |
118 | } | 118 | } | |
119 | 119 | |||
120 | int | 120 | int | |
121 | can_pcbbind(void *v, struct sockaddr_can *scan, struct lwp *l) | 121 | can_pcbbind(void *v, struct sockaddr_can *scan, struct lwp *l) | |
122 | { | 122 | { | |
123 | struct canpcb *canp = v; | 123 | struct canpcb *canp = v; | |
124 | 124 | |||
125 | if (scan->can_family != AF_CAN) | 125 | if (scan->can_family != AF_CAN) | |
126 | return (EAFNOSUPPORT); | 126 | return (EAFNOSUPPORT); | |
127 | mutex_enter(&canp->canp_mtx); | 127 | mutex_enter(&canp->canp_mtx); | |
128 | if (scan->can_ifindex != 0) { | 128 | if (scan->can_ifindex != 0) { | |
129 | canp->canp_ifp = if_byindex(scan->can_ifindex); | 129 | canp->canp_ifp = if_byindex(scan->can_ifindex); | |
130 | if (canp->canp_ifp == NULL) | 130 | if (canp->canp_ifp == NULL || | |
131 | canp->canp_ifp->if_dlt != DLT_CAN_SOCKETCAN) { | |||
132 | canp->canp_ifp = NULL; | |||
133 | mutex_exit(&canp->canp_mtx); | |||
131 | return (EADDRNOTAVAIL); | 134 | return (EADDRNOTAVAIL); | |
135 | } | |||
132 | soisconnected(canp->canp_socket); | 136 | soisconnected(canp->canp_socket); | |
133 | } else { | 137 | } else { | |
134 | canp->canp_ifp = NULL; | 138 | canp->canp_ifp = NULL; | |
135 | canp->canp_socket->so_state &= ~SS_ISCONNECTED; /* XXX */ | 139 | canp->canp_socket->so_state &= ~SS_ISCONNECTED; /* XXX */ | |
136 | } | 140 | } | |
137 | can_pcbstate(canp, CANP_BOUND); | 141 | can_pcbstate(canp, CANP_BOUND); | |
138 | mutex_exit(&canp->canp_mtx); | 142 | mutex_exit(&canp->canp_mtx); | |
139 | return 0; | 143 | return 0; | |
140 | } | 144 | } | |
141 | 145 | |||
142 | /* | 146 | /* | |
143 | * Connect from a socket to a specified address. | 147 | * Connect from a socket to a specified address. | |
144 | */ | 148 | */ |
--- src/tests/net/can/t_can.c 2017/05/28 14:53:13 1.5
+++ src/tests/net/can/t_can.c 2017/06/15 05:32:35 1.5.2.1
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: t_can.c,v 1.5 2017/05/28 14:53:13 christos Exp $ */ | 1 | /* $NetBSD: t_can.c,v 1.5.2.1 2017/06/15 05:32:35 snj Exp $ */ | |
2 | 2 | |||
3 | /*- | 3 | /*- | |
4 | * Copyright (c) 2017 The NetBSD Foundation, Inc. | 4 | * Copyright (c) 2017 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 Manuel Bouyer | 8 | * by Manuel Bouyer | |
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. | |
@@ -22,27 +22,27 @@ | @@ -22,27 +22,27 @@ | |||
22 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 22 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
23 | * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY | 23 | * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY | |
24 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 24 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
26 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 26 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
28 | * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 28 | * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN | 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN | |
30 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 30 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | 31 | */ | |
32 | 32 | |||
33 | #include <sys/cdefs.h> | 33 | #include <sys/cdefs.h> | |
34 | #ifndef lint | 34 | #ifndef lint | |
35 | __RCSID("$NetBSD: t_can.c,v 1.5 2017/05/28 14:53:13 christos Exp $"); | 35 | __RCSID("$NetBSD: t_can.c,v 1.5.2.1 2017/06/15 05:32:35 snj Exp $"); | |
36 | #endif /* not lint */ | 36 | #endif /* not lint */ | |
37 | 37 | |||
38 | #include <sys/types.h> | 38 | #include <sys/types.h> | |
39 | #include <sys/resource.h> | 39 | #include <sys/resource.h> | |
40 | #include <sys/wait.h> | 40 | #include <sys/wait.h> | |
41 | #include <sys/sockio.h> | 41 | #include <sys/sockio.h> | |
42 | #include <sys/param.h> | 42 | #include <sys/param.h> | |
43 | 43 | |||
44 | #include <atf-c.h> | 44 | #include <atf-c.h> | |
45 | #include <assert.h> | 45 | #include <assert.h> | |
46 | #include <fcntl.h> | 46 | #include <fcntl.h> | |
47 | #include <stdio.h> | 47 | #include <stdio.h> | |
48 | #include <stdlib.h> | 48 | #include <stdlib.h> | |
@@ -707,28 +707,54 @@ ATF_TC_BODY(cannoloop, tc) | @@ -707,28 +707,54 @@ ATF_TC_BODY(cannoloop, tc) | |||
707 | "recvfrom (2) packet is not what we sent"); | 707 | "recvfrom (2) packet is not what we sent"); | |
708 | ATF_CHECK_MSG(sa.can_family == AF_CAN, | 708 | ATF_CHECK_MSG(sa.can_family == AF_CAN, | |
709 | "recvfrom provided wrong %d family", sa.can_family); | 709 | "recvfrom provided wrong %d family", sa.can_family); | |
710 | ATF_CHECK_MSG(salen == sizeof(sa), | 710 | ATF_CHECK_MSG(salen == sizeof(sa), | |
711 | "recvfrom provided wrong size %u (!= %zu)", | 711 | "recvfrom provided wrong size %u (!= %zu)", | |
712 | salen, sizeof(sa)); | 712 | salen, sizeof(sa)); | |
713 | ATF_CHECK_MSG(sa.can_ifindex == ifindex, | 713 | ATF_CHECK_MSG(sa.can_ifindex == ifindex, | |
714 | "recvfrom provided wrong ifindex %d (!= %d)", | 714 | "recvfrom provided wrong ifindex %d (!= %d)", | |
715 | sa.can_ifindex, ifindex); | 715 | sa.can_ifindex, ifindex); | |
716 | atf_tc_fail_nonfatal("we got message on s2"); | 716 | atf_tc_fail_nonfatal("we got message on s2"); | |
717 | } | 717 | } | |
718 | } | 718 | } | |
719 | 719 | |||
720 | ATF_TC(canbindunknown); | |||
721 | ATF_TC_HEAD(canbindunknown, tc) | |||
722 | { | |||
723 | ||||
724 | atf_tc_set_md_var(tc, "descr", "check that bind to unkown interface fails"); | |||
725 | atf_tc_set_md_var(tc, "timeout", "5"); | |||
726 | } | |||
727 | ||||
728 | ATF_TC_BODY(canbindunknown, tc) | |||
729 | { | |||
730 | struct sockaddr_can sa; | |||
731 | int r, s; | |||
732 | ||||
733 | rump_init(); | |||
734 | ||||
735 | s = can_socket_with_own(); | |||
736 | ||||
737 | sa.can_family = AF_CAN; | |||
738 | sa.can_ifindex = 10; /* should not exist */ | |||
739 | ||||
740 | r = rump_sys_bind(s, (struct sockaddr *)&sa, sizeof(sa)); | |||
741 | ||||
742 | ATF_CHECK_MSG(r < 0, "bind() didn't fail (%d)", r); | |||
743 | } | |||
744 | ||||
720 | ATF_TP_ADD_TCS(tp) | 745 | ATF_TP_ADD_TCS(tp) | |
721 | { | 746 | { | |
722 | 747 | |||
723 | ATF_TP_ADD_TC(tp, canlocreate); | 748 | ATF_TP_ADD_TC(tp, canlocreate); | |
724 | ATF_TP_ADD_TC(tp, cannoown); | 749 | ATF_TP_ADD_TC(tp, cannoown); | |
725 | ATF_TP_ADD_TC(tp, canwritelo); | 750 | ATF_TP_ADD_TC(tp, canwritelo); | |
726 | ATF_TP_ADD_TC(tp, canwriteunbound); | 751 | ATF_TP_ADD_TC(tp, canwriteunbound); | |
727 | ATF_TP_ADD_TC(tp, cansendtolo); | 752 | ATF_TP_ADD_TC(tp, cansendtolo); | |
728 | ATF_TP_ADD_TC(tp, cansendtowrite); | 753 | ATF_TP_ADD_TC(tp, cansendtowrite); | |
729 | ATF_TP_ADD_TC(tp, canreadlocal); | 754 | ATF_TP_ADD_TC(tp, canreadlocal); | |
730 | ATF_TP_ADD_TC(tp, canrecvfrom); | 755 | ATF_TP_ADD_TC(tp, canrecvfrom); | |
731 | ATF_TP_ADD_TC(tp, canbindfilter); | 756 | ATF_TP_ADD_TC(tp, canbindfilter); | |
732 | ATF_TP_ADD_TC(tp, cannoloop); | 757 | ATF_TP_ADD_TC(tp, cannoloop); | |
758 | ATF_TP_ADD_TC(tp, canbindunknown); | |||
733 | return atf_no_error(); | 759 | return atf_no_error(); | |
734 | } | 760 | } |