1 1.5 riastrad /* $NetBSD: if_media_80.c,v 1.5 2022/08/03 01:38:51 riastradh Exp $ */ 2 1.1 msaitoh 3 1.1 msaitoh /*- 4 1.1 msaitoh * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 1.1 msaitoh * All rights reserved. 6 1.1 msaitoh * 7 1.1 msaitoh * This code is derived from software contributed to The NetBSD Foundation 8 1.1 msaitoh * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 1.1 msaitoh * NASA Ames Research Center. 10 1.1 msaitoh * 11 1.1 msaitoh * Redistribution and use in source and binary forms, with or without 12 1.1 msaitoh * modification, are permitted provided that the following conditions 13 1.1 msaitoh * are met: 14 1.1 msaitoh * 1. Redistributions of source code must retain the above copyright 15 1.1 msaitoh * notice, this list of conditions and the following disclaimer. 16 1.1 msaitoh * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 msaitoh * notice, this list of conditions and the following disclaimer in the 18 1.1 msaitoh * documentation and/or other materials provided with the distribution. 19 1.1 msaitoh * 20 1.1 msaitoh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 1.1 msaitoh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 1.1 msaitoh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 1.1 msaitoh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 1.1 msaitoh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 1.1 msaitoh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 1.1 msaitoh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 1.1 msaitoh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 1.1 msaitoh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 1.1 msaitoh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 1.1 msaitoh * POSSIBILITY OF SUCH DAMAGE. 31 1.1 msaitoh */ 32 1.1 msaitoh 33 1.1 msaitoh /* 34 1.1 msaitoh * Copyright (c) 1997 35 1.1 msaitoh * Jonathan Stone and Jason R. Thorpe. All rights reserved. 36 1.1 msaitoh * 37 1.1 msaitoh * This software is derived from information provided by Matt Thomas. 38 1.1 msaitoh * 39 1.1 msaitoh * Redistribution and use in source and binary forms, with or without 40 1.1 msaitoh * modification, are permitted provided that the following conditions 41 1.1 msaitoh * are met: 42 1.1 msaitoh * 1. Redistributions of source code must retain the above copyright 43 1.1 msaitoh * notice, this list of conditions and the following disclaimer. 44 1.1 msaitoh * 2. Redistributions in binary form must reproduce the above copyright 45 1.1 msaitoh * notice, this list of conditions and the following disclaimer in the 46 1.1 msaitoh * documentation and/or other materials provided with the distribution. 47 1.1 msaitoh * 3. All advertising materials mentioning features or use of this software 48 1.1 msaitoh * must display the following acknowledgement: 49 1.1 msaitoh * This product includes software developed by Jonathan Stone 50 1.1 msaitoh * and Jason R. Thorpe for the NetBSD Project. 51 1.1 msaitoh * 4. The names of the authors may not be used to endorse or promote products 52 1.1 msaitoh * derived from this software without specific prior written permission. 53 1.1 msaitoh * 54 1.1 msaitoh * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 55 1.1 msaitoh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 56 1.1 msaitoh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 57 1.1 msaitoh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 58 1.1 msaitoh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 59 1.1 msaitoh * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 60 1.1 msaitoh * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 61 1.1 msaitoh * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 62 1.1 msaitoh * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 1.1 msaitoh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 1.1 msaitoh * SUCH DAMAGE. 65 1.1 msaitoh */ 66 1.1 msaitoh 67 1.1 msaitoh #include <sys/cdefs.h> 68 1.5 riastrad __KERNEL_RCSID(0, "$NetBSD: if_media_80.c,v 1.5 2022/08/03 01:38:51 riastradh Exp $"); 69 1.1 msaitoh 70 1.1 msaitoh #include <sys/param.h> 71 1.1 msaitoh #include <sys/kernel.h> 72 1.1 msaitoh #include <sys/syscallargs.h> 73 1.1 msaitoh #include <sys/errno.h> 74 1.1 msaitoh #include <sys/malloc.h> 75 1.1 msaitoh #include <sys/proc.h> 76 1.1 msaitoh #include <sys/compat_stub.h> 77 1.1 msaitoh 78 1.1 msaitoh #include <net/if.h> 79 1.1 msaitoh #include <net/if_media.h> 80 1.1 msaitoh 81 1.1 msaitoh #include <compat/sys/sockio.h> 82 1.1 msaitoh #include <compat/common/compat_mod.h> 83 1.1 msaitoh 84 1.1 msaitoh static void 85 1.1 msaitoh ifmword_n2o(int *oldwd, int *newwd) 86 1.1 msaitoh { 87 1.1 msaitoh 88 1.1 msaitoh if (IFM_SUBTYPE(*newwd) > IFM_OTHER) 89 1.1 msaitoh *oldwd = (*newwd & ~(_IFM_ETH_XTMASK | IFM_TMASK)) | IFM_OTHER; 90 1.1 msaitoh else 91 1.1 msaitoh *oldwd = *newwd; 92 1.1 msaitoh } 93 1.1 msaitoh 94 1.1 msaitoh /*ARGSUSED*/ 95 1.1 msaitoh static int 96 1.1 msaitoh compat_ifmediareq_pre(struct ifreq *ifr, u_long *cmd, bool *do_post) 97 1.1 msaitoh { 98 1.1 msaitoh struct ifmediareq *ifmr = (struct ifmediareq *)ifr; 99 1.1 msaitoh 100 1.1 msaitoh switch (*cmd) { 101 1.1 msaitoh case SIOCSIFMEDIA_80: 102 1.1 msaitoh *cmd = SIOCSIFMEDIA; /* Convert to new one */ 103 1.1 msaitoh if ((IFM_TYPE(ifr->ifr_media) == IFM_ETHER) && 104 1.1 msaitoh IFM_SUBTYPE(ifr->ifr_media) > IFM_OTHER) { 105 1.1 msaitoh /* Clear unused bits to not to change to wrong media */ 106 1.1 msaitoh ifr->ifr_media &= ~_IFM_ETH_XTMASK; 107 1.1 msaitoh } 108 1.1 msaitoh return 0; 109 1.1 msaitoh case SIOCGIFMEDIA_80: 110 1.1 msaitoh *cmd = SIOCGIFMEDIA; /* Convert to new one */ 111 1.1 msaitoh if (ifmr->ifm_count != 0) { 112 1.1 msaitoh /* 113 1.1 msaitoh * Tell the upper layer to try to convert each ifmedia 114 1.1 msaitoh * entry in the post process. 115 1.1 msaitoh */ 116 1.1 msaitoh *do_post = true; 117 1.1 msaitoh } 118 1.1 msaitoh return 0; 119 1.1 msaitoh default: 120 1.1 msaitoh return 0; 121 1.1 msaitoh } 122 1.1 msaitoh } 123 1.1 msaitoh 124 1.1 msaitoh /*ARGSUSED*/ 125 1.1 msaitoh static int 126 1.1 msaitoh compat_ifmediareq_post(struct ifreq *ifr, u_long cmd) 127 1.1 msaitoh { 128 1.1 msaitoh struct ifmediareq *ifmr = (struct ifmediareq *)ifr; 129 1.1 msaitoh size_t minwords; 130 1.2 christos size_t count; 131 1.2 christos int error, *kptr; 132 1.1 msaitoh 133 1.1 msaitoh switch (cmd) { 134 1.1 msaitoh case SIOCSIFMEDIA: 135 1.1 msaitoh return 0; 136 1.1 msaitoh case SIOCGIFMEDIA: 137 1.1 msaitoh if (ifmr->ifm_count < 0) 138 1.1 msaitoh return EINVAL; 139 1.1 msaitoh 140 1.1 msaitoh /* 141 1.1 msaitoh * ifmr->ifm_count was already ajusted in ifmedia_ioctl(), so 142 1.1 msaitoh * there is no problem to trust ifm_count. 143 1.1 msaitoh */ 144 1.1 msaitoh minwords = ifmr->ifm_count; 145 1.4 riastrad kptr = malloc(minwords * sizeof(*kptr), M_TEMP, 146 1.4 riastrad M_WAITOK|M_ZERO); 147 1.1 msaitoh if (kptr == NULL) 148 1.1 msaitoh return ENOMEM; 149 1.1 msaitoh 150 1.1 msaitoh /* 151 1.1 msaitoh * Convert ifm_current and ifm_active. 152 1.1 msaitoh * It's not required to convert ifm_mask. 153 1.1 msaitoh */ 154 1.1 msaitoh ifmword_n2o(&ifmr->ifm_current, &ifmr->ifm_current); 155 1.1 msaitoh ifmword_n2o(&ifmr->ifm_active, &ifmr->ifm_active); 156 1.1 msaitoh 157 1.1 msaitoh /* Convert ifm_ulist array */ 158 1.1 msaitoh for (count = 0; count < minwords; count++) { 159 1.1 msaitoh int oldmwd; 160 1.1 msaitoh 161 1.1 msaitoh error = ufetch_int(&ifmr->ifm_ulist[count], &oldmwd); 162 1.1 msaitoh if (error != 0) 163 1.1 msaitoh goto out; 164 1.1 msaitoh ifmword_n2o(&kptr[count], &oldmwd); 165 1.1 msaitoh } 166 1.1 msaitoh 167 1.1 msaitoh /* Copy to userland in old format */ 168 1.1 msaitoh error = copyout(kptr, ifmr->ifm_ulist, 169 1.1 msaitoh minwords * sizeof(*kptr)); 170 1.1 msaitoh out: 171 1.1 msaitoh free(kptr, M_TEMP); 172 1.1 msaitoh return error; 173 1.1 msaitoh default: 174 1.1 msaitoh return 0; 175 1.1 msaitoh } 176 1.1 msaitoh } 177 1.1 msaitoh 178 1.1 msaitoh void 179 1.1 msaitoh ifmedia_80_init(void) 180 1.1 msaitoh { 181 1.1 msaitoh 182 1.3 pgoyette MODULE_HOOK_SET(ifmedia_80_pre_hook, compat_ifmediareq_pre); 183 1.3 pgoyette MODULE_HOOK_SET(ifmedia_80_post_hook, compat_ifmediareq_post); 184 1.1 msaitoh } 185 1.1 msaitoh 186 1.1 msaitoh void 187 1.1 msaitoh ifmedia_80_fini(void) 188 1.1 msaitoh { 189 1.1 msaitoh 190 1.1 msaitoh MODULE_HOOK_UNSET(ifmedia_80_post_hook); 191 1.1 msaitoh MODULE_HOOK_UNSET(ifmedia_80_pre_hook); 192 1.1 msaitoh } 193