1 /* $NetBSD: if_virt.c,v 1.54 2016/12/15 09:28:07 ozaki-r Exp $ */ 2 3 /* 4 * Copyright (c) 2008, 2013 Antti Kantee. All Rights Reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.54 2016/12/15 09:28:07 ozaki-r Exp $"); 30 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/kmem.h> 34 #include <sys/cprng.h> 35 #include <sys/module.h> 36 37 #include <net/bpf.h> 38 #include <net/if.h> 39 #include <net/if_dl.h> 40 #include <net/if_ether.h> 41 42 #include <netinet/in.h> 43 #include <netinet/in_var.h> 44 45 #include "if_virt.h" 46 #include "virtif_user.h" 47 48 /* 49 * Virtual interface. Uses hypercalls to shovel packets back 50 * and forth. The exact method for shoveling depends on the 51 * hypercall implementation. 52 */ 53 54 static int virtif_init(struct ifnet *); 55 static int virtif_ioctl(struct ifnet *, u_long, void *); 56 static void virtif_start(struct ifnet *); 57 static void virtif_stop(struct ifnet *, int); 58 59 struct virtif_sc { 60 struct ethercom sc_ec; 61 struct virtif_user *sc_viu; 62 63 int sc_num; 64 char *sc_linkstr; 65 }; 66 67 static int virtif_clone(struct if_clone *, int); 68 static int virtif_unclone(struct ifnet *); 69 70 struct if_clone VIF_CLONER = 71 IF_CLONE_INITIALIZER(VIF_NAME, virtif_clone, virtif_unclone); 72 73 static int 74 virtif_create(struct ifnet *ifp) 75 { 76 uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 }; 77 char enaddrstr[3*ETHER_ADDR_LEN]; 78 struct virtif_sc *sc = ifp->if_softc; 79 int error; 80 81 if (sc->sc_viu) 82 panic("%s: already created", ifp->if_xname); 83 84 enaddr[2] = cprng_fast32() & 0xff; 85 enaddr[5] = sc->sc_num & 0xff; 86 87 if ((error = VIFHYPER_CREATE(sc->sc_linkstr, 88 sc, enaddr, &sc->sc_viu)) != 0) { 89 printf("VIFHYPER_CREATE failed: %d\n", error); 90 return error; 91 } 92 93 ether_ifattach(ifp, enaddr); 94 ether_snprintf(enaddrstr, sizeof(enaddrstr), enaddr); 95 aprint_normal_ifnet(ifp, "Ethernet address %s\n", enaddrstr); 96 97 IFQ_SET_READY(&ifp->if_snd); 98 99 return 0; 100 } 101 102 static int 103 virtif_clone(struct if_clone *ifc, int num) 104 { 105 struct virtif_sc *sc; 106 struct ifnet *ifp; 107 int error = 0; 108 109 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); 110 sc->sc_num = num; 111 ifp = &sc->sc_ec.ec_if; 112 113 if_initname(ifp, VIF_NAME, num); 114 ifp->if_softc = sc; 115 116 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 117 ifp->if_init = virtif_init; 118 ifp->if_ioctl = virtif_ioctl; 119 ifp->if_start = virtif_start; 120 ifp->if_stop = virtif_stop; 121 ifp->if_mtu = ETHERMTU; 122 ifp->if_dlt = DLT_EN10MB; 123 124 if_initialize(ifp); 125 if_register(ifp); 126 127 #ifndef RUMP_VIF_LINKSTR 128 /* 129 * if the underlying interface does not expect linkstr, we can 130 * create everything now. Otherwise, we need to wait for 131 * SIOCSLINKSTR. 132 */ 133 #define LINKSTRNUMLEN 16 134 sc->sc_linkstr = kmem_alloc(LINKSTRNUMLEN, KM_SLEEP); 135 snprintf(sc->sc_linkstr, LINKSTRNUMLEN, "%d", sc->sc_num); 136 #undef LINKSTRNUMLEN 137 error = virtif_create(ifp); 138 if (error) { 139 if_detach(ifp); 140 kmem_free(sc, sizeof(*sc)); 141 ifp->if_softc = NULL; 142 } 143 #endif /* !RUMP_VIF_LINKSTR */ 144 145 return error; 146 } 147 148 static int 149 virtif_unclone(struct ifnet *ifp) 150 { 151 struct virtif_sc *sc = ifp->if_softc; 152 int rv; 153 154 if (ifp->if_flags & IFF_UP) 155 return EBUSY; 156 157 if ((rv = VIFHYPER_DYING(sc->sc_viu)) != 0) 158 return rv; 159 160 virtif_stop(ifp, 1); 161 if_down(ifp); 162 163 VIFHYPER_DESTROY(sc->sc_viu); 164 165 kmem_free(sc, sizeof(*sc)); 166 167 ether_ifdetach(ifp); 168 if_detach(ifp); 169 170 return 0; 171 } 172 173 static int 174 virtif_init(struct ifnet *ifp) 175 { 176 struct virtif_sc *sc = ifp->if_softc; 177 178 if (sc->sc_viu == NULL) 179 return ENXIO; 180 181 ifp->if_flags |= IFF_RUNNING; 182 return 0; 183 } 184 185 static int 186 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data) 187 { 188 struct virtif_sc *sc = ifp->if_softc; 189 int rv; 190 191 switch (cmd) { 192 #ifdef RUMP_VIF_LINKSTR 193 struct ifdrv *ifd; 194 size_t linkstrlen; 195 196 #ifndef RUMP_VIF_LINKSTRMAX 197 #define RUMP_VIF_LINKSTRMAX 4096 198 #endif 199 200 case SIOCGLINKSTR: 201 ifd = data; 202 203 if (!sc->sc_linkstr) { 204 rv = ENOENT; 205 break; 206 } 207 linkstrlen = strlen(sc->sc_linkstr)+1; 208 209 if (ifd->ifd_cmd == IFLINKSTR_QUERYLEN) { 210 ifd->ifd_len = linkstrlen; 211 rv = 0; 212 break; 213 } 214 if (ifd->ifd_cmd != 0) { 215 rv = ENOTTY; 216 break; 217 } 218 219 rv = copyoutstr(sc->sc_linkstr, 220 ifd->ifd_data, MIN(ifd->ifd_len,linkstrlen), NULL); 221 break; 222 case SIOCSLINKSTR: 223 if (ifp->if_flags & IFF_UP) { 224 rv = EBUSY; 225 break; 226 } 227 228 ifd = data; 229 230 if (ifd->ifd_cmd == IFLINKSTR_UNSET) { 231 panic("unset linkstr not implemented"); 232 } else if (ifd->ifd_cmd != 0) { 233 rv = ENOTTY; 234 break; 235 } else if (sc->sc_linkstr) { 236 rv = EBUSY; 237 break; 238 } 239 240 if (ifd->ifd_len > RUMP_VIF_LINKSTRMAX) { 241 rv = E2BIG; 242 break; 243 } else if (ifd->ifd_len < 1) { 244 rv = EINVAL; 245 break; 246 } 247 248 249 sc->sc_linkstr = kmem_alloc(ifd->ifd_len, KM_SLEEP); 250 rv = copyinstr(ifd->ifd_data, sc->sc_linkstr, 251 ifd->ifd_len, NULL); 252 if (rv) { 253 kmem_free(sc->sc_linkstr, ifd->ifd_len); 254 break; 255 } 256 257 rv = virtif_create(ifp); 258 if (rv) { 259 kmem_free(sc->sc_linkstr, ifd->ifd_len); 260 } 261 break; 262 #endif /* RUMP_VIF_LINKSTR */ 263 default: 264 if (!sc->sc_linkstr) 265 rv = ENXIO; 266 else 267 rv = ether_ioctl(ifp, cmd, data); 268 if (rv == ENETRESET) 269 rv = 0; 270 break; 271 } 272 273 return rv; 274 } 275 276 /* 277 * Output packets in-context until outgoing queue is empty. 278 * Leave responsibility of choosing whether or not to drop the 279 * kernel lock to VIPHYPER_SEND(). 280 */ 281 #define LB_SH 32 282 static void 283 virtif_start(struct ifnet *ifp) 284 { 285 struct virtif_sc *sc = ifp->if_softc; 286 struct mbuf *m, *m0; 287 struct iovec io[LB_SH]; 288 int i; 289 290 ifp->if_flags |= IFF_OACTIVE; 291 292 for (;;) { 293 IF_DEQUEUE(&ifp->if_snd, m0); 294 if (!m0) { 295 break; 296 } 297 298 m = m0; 299 for (i = 0; i < LB_SH && m; ) { 300 if (m->m_len) { 301 io[i].iov_base = mtod(m, void *); 302 io[i].iov_len = m->m_len; 303 i++; 304 } 305 m = m->m_next; 306 } 307 if (i == LB_SH && m) 308 panic("lazy bum"); 309 bpf_mtap(ifp, m0); 310 311 VIFHYPER_SEND(sc->sc_viu, io, i); 312 313 m_freem(m0); 314 ifp->if_opackets++; 315 } 316 317 ifp->if_flags &= ~IFF_OACTIVE; 318 } 319 320 static void 321 virtif_stop(struct ifnet *ifp, int disable) 322 { 323 324 /* XXX: VIFHYPER_STOP() */ 325 326 ifp->if_flags &= ~IFF_RUNNING; 327 } 328 329 void 330 VIF_DELIVERPKT(struct virtif_sc *sc, struct iovec *iov, size_t iovlen) 331 { 332 struct ifnet *ifp = &sc->sc_ec.ec_if; 333 struct ether_header *eth; 334 struct mbuf *m; 335 size_t i; 336 int off, olen; 337 bool passup; 338 const int align 339 = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header); 340 341 if ((ifp->if_flags & IFF_RUNNING) == 0) 342 return; 343 344 m = m_gethdr(M_NOWAIT, MT_DATA); 345 if (m == NULL) 346 return; /* drop packet */ 347 m->m_len = m->m_pkthdr.len = 0; 348 349 for (i = 0, off = align; i < iovlen; i++) { 350 olen = m->m_pkthdr.len; 351 m_copyback(m, off, iov[i].iov_len, iov[i].iov_base); 352 off += iov[i].iov_len; 353 if (olen + off != m->m_pkthdr.len) { 354 aprint_verbose_ifnet(ifp, "m_copyback failed\n"); 355 m_freem(m); 356 return; 357 } 358 } 359 m->m_data += align; 360 m->m_pkthdr.len -= align; 361 m->m_len -= align; 362 363 eth = mtod(m, struct ether_header *); 364 if (memcmp(eth->ether_dhost, CLLADDR(ifp->if_sadl), 365 ETHER_ADDR_LEN) == 0) { 366 passup = true; 367 } else if (ETHER_IS_MULTICAST(eth->ether_dhost)) { 368 passup = true; 369 } else if (ifp->if_flags & IFF_PROMISC) { 370 m->m_flags |= M_PROMISC; 371 passup = true; 372 } else { 373 passup = false; 374 } 375 376 if (passup) { 377 int bound; 378 m_set_rcvif(m, ifp); 379 KERNEL_LOCK(1, NULL); 380 /* Prevent LWP migrations between CPUs for psref(9) */ 381 bound = curlwp_bind(); 382 if_input(ifp, m); 383 curlwp_bindx(bound); 384 KERNEL_UNLOCK_LAST(NULL); 385 } else { 386 m_freem(m); 387 } 388 m = NULL; 389 } 390 391 /* 392 * The following ensures that no two modules using if_virt end up with 393 * the same module name. MODULE() and modcmd wrapped in ... bad mojo. 394 */ 395 #define VIF_MOJO(x) MODULE(MODULE_CLASS_DRIVER,x,NULL); 396 #define VIF_MODULE() VIF_MOJO(VIF_BASENAME(if_virt_,VIRTIF_BASE)) 397 #define VIF_MODCMD VIF_BASENAME3(if_virt_,VIRTIF_BASE,_modcmd) 398 VIF_MODULE(); 399 static int 400 VIF_MODCMD(modcmd_t cmd, void *opaque) 401 { 402 int error = 0; 403 404 switch (cmd) { 405 case MODULE_CMD_INIT: 406 if_clone_attach(&VIF_CLONER); 407 break; 408 case MODULE_CMD_FINI: 409 /* 410 * not sure if interfaces are refcounted 411 * and properly protected 412 */ 413 #if 0 414 if_clone_detach(&VIF_CLONER); 415 #else 416 error = ENOTTY; 417 #endif 418 break; 419 default: 420 error = ENOTTY; 421 } 422 return error; 423 } 424