| 1 | /* |
| 2 | * Copyright (c) 1980, 1986, 1993 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | * |
| 33 | * @(#)if.c 8.3 (Berkeley) 1/4/94 |
| 34 | * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $ |
| 35 | * $DragonFly: src/sys/net/if.c,v 1.82 2008/11/11 13:48:01 sephe Exp $ |
| 36 | */ |
| 37 | |
| 38 | #include "opt_compat.h" |
| 39 | #include "opt_inet6.h" |
| 40 | #include "opt_inet.h" |
| 41 | #include "opt_polling.h" |
| 42 | |
| 43 | #include <sys/param.h> |
| 44 | #include <sys/malloc.h> |
| 45 | #include <sys/mbuf.h> |
| 46 | #include <sys/systm.h> |
| 47 | #include <sys/proc.h> |
| 48 | #include <sys/protosw.h> |
| 49 | #include <sys/socket.h> |
| 50 | #include <sys/socketvar.h> |
| 51 | #include <sys/socketops.h> |
| 52 | #include <sys/protosw.h> |
| 53 | #include <sys/kernel.h> |
| 54 | #include <sys/ktr.h> |
| 55 | #include <sys/sockio.h> |
| 56 | #include <sys/syslog.h> |
| 57 | #include <sys/sysctl.h> |
| 58 | #include <sys/domain.h> |
| 59 | #include <sys/thread.h> |
| 60 | #include <sys/thread2.h> |
| 61 | #include <sys/serialize.h> |
| 62 | #include <sys/msgport2.h> |
| 63 | #include <sys/bus.h> |
| 64 | |
| 65 | #include <net/if.h> |
| 66 | #include <net/if_arp.h> |
| 67 | #include <net/if_dl.h> |
| 68 | #include <net/if_types.h> |
| 69 | #include <net/if_var.h> |
| 70 | #include <net/ifq_var.h> |
| 71 | #include <net/radix.h> |
| 72 | #include <net/route.h> |
| 73 | #include <net/if_clone.h> |
| 74 | #include <net/netisr.h> |
| 75 | #include <net/netmsg2.h> |
| 76 | |
| 77 | #include <machine/atomic.h> |
| 78 | #include <machine/stdarg.h> |
| 79 | #include <machine/smp.h> |
| 80 | |
| 81 | #if defined(INET) || defined(INET6) |
| 82 | /*XXX*/ |
| 83 | #include <netinet/in.h> |
| 84 | #include <netinet/in_var.h> |
| 85 | #include <netinet/if_ether.h> |
| 86 | #ifdef INET6 |
| 87 | #include <netinet6/in6_var.h> |
| 88 | #include <netinet6/in6_ifattach.h> |
| 89 | #endif |
| 90 | #endif |
| 91 | |
| 92 | #if defined(COMPAT_43) |
| 93 | #include <emulation/43bsd/43bsd_socket.h> |
| 94 | #endif /* COMPAT_43 */ |
| 95 | |
| 96 | struct netmsg_ifaddr { |
| 97 | struct netmsg netmsg; |
| 98 | struct ifaddr *ifa; |
| 99 | struct ifnet *ifp; |
| 100 | int tail; |
| 101 | }; |
| 102 | |
| 103 | /* |
| 104 | * System initialization |
| 105 | */ |
| 106 | static void if_attachdomain(void *); |
| 107 | static void if_attachdomain1(struct ifnet *); |
| 108 | static int ifconf(u_long, caddr_t, struct ucred *); |
| 109 | static void ifinit(void *); |
| 110 | static void ifnetinit(void *); |
| 111 | static void if_slowtimo(void *); |
| 112 | static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); |
| 113 | static int if_rtdel(struct radix_node *, void *); |
| 114 | |
| 115 | #ifdef INET6 |
| 116 | /* |
| 117 | * XXX: declare here to avoid to include many inet6 related files.. |
| 118 | * should be more generalized? |
| 119 | */ |
| 120 | extern void nd6_setmtu(struct ifnet *); |
| 121 | #endif |
| 122 | |
| 123 | SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); |
| 124 | SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); |
| 125 | |
| 126 | SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL) |
| 127 | /* Must be after netisr_init */ |
| 128 | SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL) |
| 129 | |
| 130 | MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); |
| 131 | MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); |
| 132 | |
| 133 | int ifqmaxlen = IFQ_MAXLEN; |
| 134 | struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet); |
| 135 | |
| 136 | /* In ifq_dispatch(), try to do direct ifnet.if_start first */ |
| 137 | static int ifq_dispatch_schedonly = 0; |
| 138 | SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schedonly, CTLFLAG_RW, |
| 139 | &ifq_dispatch_schedonly, 0, ""); |
| 140 | |
| 141 | /* In ifq_dispatch(), schedule ifnet.if_start without checking ifnet.if_snd */ |
| 142 | static int ifq_dispatch_schednochk = 0; |
| 143 | SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schednochk, CTLFLAG_RW, |
| 144 | &ifq_dispatch_schednochk, 0, ""); |
| 145 | |
| 146 | /* In if_devstart(), try to do direct ifnet.if_start first */ |
| 147 | static int if_devstart_schedonly = 0; |
| 148 | SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schedonly, CTLFLAG_RW, |
| 149 | &if_devstart_schedonly, 0, ""); |
| 150 | |
| 151 | /* In if_devstart(), schedule ifnet.if_start without checking ifnet.if_snd */ |
| 152 | static int if_devstart_schednochk = 0; |
| 153 | SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schednochk, CTLFLAG_RW, |
| 154 | &if_devstart_schednochk, 0, ""); |
| 155 | |
| 156 | #ifdef SMP |
| 157 | /* Schedule ifnet.if_start on the current CPU */ |
| 158 | static int if_start_oncpu_sched = 0; |
| 159 | SYSCTL_INT(_net_link_generic, OID_AUTO, if_start_oncpu_sched, CTLFLAG_RW, |
| 160 | &if_start_oncpu_sched, 0, ""); |
| 161 | #endif |
| 162 | |
| 163 | struct callout if_slowtimo_timer; |
| 164 | |
| 165 | int if_index = 0; |
| 166 | struct ifnet **ifindex2ifnet = NULL; |
| 167 | static struct thread ifnet_threads[MAXCPU]; |
| 168 | static int ifnet_mpsafe_thread = NETMSG_SERVICE_MPSAFE; |
| 169 | |
| 170 | #define IFQ_KTR_STRING "ifq=%p" |
| 171 | #define IFQ_KTR_ARG_SIZE (sizeof(void *)) |
| 172 | #ifndef KTR_IFQ |
| 173 | #define KTR_IFQ KTR_ALL |
| 174 | #endif |
| 175 | KTR_INFO_MASTER(ifq); |
| 176 | KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE); |
| 177 | KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE); |
| 178 | #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg) |
| 179 | |
| 180 | #define IF_START_KTR_STRING "ifp=%p" |
| 181 | #define IF_START_KTR_ARG_SIZE (sizeof(void *)) |
| 182 | #ifndef KTR_IF_START |
| 183 | #define KTR_IF_START KTR_ALL |
| 184 | #endif |
| 185 | KTR_INFO_MASTER(if_start); |
| 186 | KTR_INFO(KTR_IF_START, if_start, run, 0, |
| 187 | IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE); |
| 188 | KTR_INFO(KTR_IF_START, if_start, sched, 1, |
| 189 | IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE); |
| 190 | KTR_INFO(KTR_IF_START, if_start, avoid, 2, |
| 191 | IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE); |
| 192 | KTR_INFO(KTR_IF_START, if_start, contend_sched, 3, |
| 193 | IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE); |
| 194 | KTR_INFO(KTR_IF_START, if_start, chase_sched, 4, |
| 195 | IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE); |
| 196 | #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg) |
| 197 | |
| 198 | /* |
| 199 | * Network interface utility routines. |
| 200 | * |
| 201 | * Routines with ifa_ifwith* names take sockaddr *'s as |
| 202 | * parameters. |
| 203 | */ |
| 204 | /* ARGSUSED*/ |
| 205 | void |
| 206 | ifinit(void *dummy) |
| 207 | { |
| 208 | struct ifnet *ifp; |
| 209 | |
| 210 | callout_init(&if_slowtimo_timer); |
| 211 | |
| 212 | crit_enter(); |
| 213 | TAILQ_FOREACH(ifp, &ifnet, if_link) { |
| 214 | if (ifp->if_snd.ifq_maxlen == 0) { |
| 215 | if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n"); |
| 216 | ifp->if_snd.ifq_maxlen = ifqmaxlen; |
| 217 | } |
| 218 | } |
| 219 | crit_exit(); |
| 220 | |
| 221 | if_slowtimo(0); |
| 222 | } |
| 223 | |
| 224 | static int |
| 225 | if_start_cpuid(struct ifnet *ifp) |
| 226 | { |
| 227 | return ifp->if_cpuid; |
| 228 | } |
| 229 | |
| 230 | #ifdef DEVICE_POLLING |
| 231 | static int |
| 232 | if_start_cpuid_poll(struct ifnet *ifp) |
| 233 | { |
| 234 | int poll_cpuid = ifp->if_poll_cpuid; |
| 235 | |
| 236 | if (poll_cpuid >= 0) |
| 237 | return poll_cpuid; |
| 238 | else |
| 239 | return ifp->if_cpuid; |
| 240 | } |
| 241 | #endif |
| 242 | |
| 243 | static void |
| 244 | if_start_ipifunc(void *arg) |
| 245 | { |
| 246 | struct ifnet *ifp = arg; |
| 247 | struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].nm_lmsg; |
| 248 | |
| 249 | crit_enter(); |
| 250 | if (lmsg->ms_flags & MSGF_DONE) |
| 251 | lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg); |
| 252 | crit_exit(); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Schedule ifnet.if_start on ifnet's CPU |
| 257 | */ |
| 258 | static void |
| 259 | if_start_schedule(struct ifnet *ifp) |
| 260 | { |
| 261 | #ifdef SMP |
| 262 | int cpu; |
| 263 | |
| 264 | if (if_start_oncpu_sched) |
| 265 | cpu = mycpuid; |
| 266 | else |
| 267 | cpu = ifp->if_start_cpuid(ifp); |
| 268 | |
| 269 | if (cpu != mycpuid) |
| 270 | lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp); |
| 271 | else |
| 272 | #endif |
| 273 | if_start_ipifunc(ifp); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * NOTE: |
| 278 | * This function will release ifnet.if_start interlock, |
| 279 | * if ifnet.if_start does not need to be scheduled |
| 280 | */ |
| 281 | static __inline int |
| 282 | if_start_need_schedule(struct ifaltq *ifq, int running) |
| 283 | { |
| 284 | if (!running || ifq_is_empty(ifq) |
| 285 | #ifdef ALTQ |
| 286 | || ifq->altq_tbr != NULL |
| 287 | #endif |
| 288 | ) { |
| 289 | ALTQ_LOCK(ifq); |
| 290 | /* |
| 291 | * ifnet.if_start interlock is released, if: |
| 292 | * 1) Hardware can not take any packets, due to |
| 293 | * o interface is marked down |
| 294 | * o hardware queue is full (IFF_OACTIVE) |
| 295 | * Under the second situation, hardware interrupt |
| 296 | * or polling(4) will call/schedule ifnet.if_start |
| 297 | * when hardware queue is ready |
| 298 | * 2) There is not packet in the ifnet.if_snd. |
| 299 | * Further ifq_dispatch or ifq_handoff will call/ |
| 300 | * schedule ifnet.if_start |
| 301 | * 3) TBR is used and it does not allow further |
| 302 | * dequeueing. |
| 303 | * TBR callout will call ifnet.if_start |
| 304 | */ |
| 305 | if (!running || !ifq_data_ready(ifq)) { |
| 306 | ifq->altq_started = 0; |
| 307 | ALTQ_UNLOCK(ifq); |
| 308 | return 0; |
| 309 | } |
| 310 | ALTQ_UNLOCK(ifq); |
| 311 | } |
| 312 | return 1; |
| 313 | } |
| 314 | |
| 315 | static void |
| 316 | if_start_dispatch(struct netmsg *nmsg) |
| 317 | { |
| 318 | struct lwkt_msg *lmsg = &nmsg->nm_lmsg; |
| 319 | struct ifnet *ifp = lmsg->u.ms_resultp; |
| 320 | struct ifaltq *ifq = &ifp->if_snd; |
| 321 | int running = 0; |
| 322 | |
| 323 | crit_enter(); |
| 324 | lwkt_replymsg(lmsg, 0); /* reply ASAP */ |
| 325 | crit_exit(); |
| 326 | |
| 327 | #ifdef SMP |
| 328 | if (!if_start_oncpu_sched && mycpuid != ifp->if_start_cpuid(ifp)) { |
| 329 | /* |
| 330 | * If the ifnet is still up, we need to |
| 331 | * chase its CPU change. |
| 332 | */ |
| 333 | if (ifp->if_flags & IFF_UP) { |
| 334 | logifstart(chase_sched, ifp); |
| 335 | if_start_schedule(ifp); |
| 336 | return; |
| 337 | } else { |
| 338 | goto check; |
| 339 | } |
| 340 | } |
| 341 | #endif |
| 342 | |
| 343 | if (ifp->if_flags & IFF_UP) { |
| 344 | lwkt_serialize_enter(ifp->if_serializer); /* XXX try? */ |
| 345 | if ((ifp->if_flags & IFF_OACTIVE) == 0) { |
| 346 | logifstart(run, ifp); |
| 347 | ifp->if_start(ifp); |
| 348 | if ((ifp->if_flags & |
| 349 | (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING) |
| 350 | running = 1; |
| 351 | } |
| 352 | lwkt_serialize_exit(ifp->if_serializer); |
| 353 | } |
| 354 | #ifdef SMP |
| 355 | check: |
| 356 | #endif |
| 357 | if (if_start_need_schedule(ifq, running)) { |
| 358 | crit_enter(); |
| 359 | if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */ |
| 360 | logifstart(sched, ifp); |
| 361 | lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg); |
| 362 | } |
| 363 | crit_exit(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /* Device driver ifnet.if_start helper function */ |
| 368 | void |
| 369 | if_devstart(struct ifnet *ifp) |
| 370 | { |
| 371 | struct ifaltq *ifq = &ifp->if_snd; |
| 372 | int running = 0; |
| 373 | |
| 374 | ASSERT_SERIALIZED(ifp->if_serializer); |
| 375 | |
| 376 | ALTQ_LOCK(ifq); |
| 377 | if (ifq->altq_started || !ifq_data_ready(ifq)) { |
| 378 | logifstart(avoid, ifp); |
| 379 | ALTQ_UNLOCK(ifq); |
| 380 | return; |
| 381 | } |
| 382 | ifq->altq_started = 1; |
| 383 | ALTQ_UNLOCK(ifq); |
| 384 | |
| 385 | if (if_devstart_schedonly) { |
| 386 | /* |
| 387 | * Always schedule ifnet.if_start on ifnet's CPU, |
| 388 | * short circuit the rest of this function. |
| 389 | */ |
| 390 | logifstart(sched, ifp); |
| 391 | if_start_schedule(ifp); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | logifstart(run, ifp); |
| 396 | ifp->if_start(ifp); |
| 397 | |
| 398 | if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING) |
| 399 | running = 1; |
| 400 | |
| 401 | if (if_devstart_schednochk || if_start_need_schedule(ifq, running)) { |
| 402 | /* |
| 403 | * More data need to be transmitted, ifnet.if_start is |
| 404 | * scheduled on ifnet's CPU, and we keep going. |
| 405 | * NOTE: ifnet.if_start interlock is not released. |
| 406 | */ |
| 407 | logifstart(sched, ifp); |
| 408 | if_start_schedule(ifp); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Attach an interface to the list of "active" interfaces. |
| 414 | * |
| 415 | * The serializer is optional. If non-NULL access to the interface |
| 416 | * may be MPSAFE. |
| 417 | */ |
| 418 | void |
| 419 | if_attach(struct ifnet *ifp, lwkt_serialize_t serializer) |
| 420 | { |
| 421 | unsigned socksize, ifasize; |
| 422 | int namelen, masklen; |
| 423 | struct sockaddr_dl *sdl; |
| 424 | struct ifaddr *ifa; |
| 425 | struct ifaltq *ifq; |
| 426 | int i; |
| 427 | |
| 428 | static int if_indexlim = 8; |
| 429 | |
| 430 | /* |
| 431 | * The serializer can be passed in from the device, allowing the |
| 432 | * same serializer to be used for both the interrupt interlock and |
| 433 | * the device queue. If not specified, the netif structure will |
| 434 | * use an embedded serializer. |
| 435 | */ |
| 436 | if (serializer == NULL) { |
| 437 | serializer = &ifp->if_default_serializer; |
| 438 | lwkt_serialize_init(serializer); |
| 439 | } |
| 440 | ifp->if_serializer = serializer; |
| 441 | |
| 442 | ifp->if_start_cpuid = if_start_cpuid; |
| 443 | ifp->if_cpuid = 0; |
| 444 | |
| 445 | #ifdef DEVICE_POLLING |
| 446 | /* Device is not in polling mode by default */ |
| 447 | ifp->if_poll_cpuid = -1; |
| 448 | if (ifp->if_poll != NULL) |
| 449 | ifp->if_start_cpuid = if_start_cpuid_poll; |
| 450 | #endif |
| 451 | |
| 452 | ifp->if_start_nmsg = kmalloc(ncpus * sizeof(struct netmsg), |
| 453 | M_IFADDR /* XXX */, M_WAITOK); |
| 454 | for (i = 0; i < ncpus; ++i) { |
| 455 | netmsg_init(&ifp->if_start_nmsg[i], &netisr_adone_rport, 0, |
| 456 | if_start_dispatch); |
| 457 | ifp->if_start_nmsg[i].nm_lmsg.u.ms_resultp = ifp; |
| 458 | } |
| 459 | |
| 460 | TAILQ_INSERT_TAIL(&ifnet, ifp, if_link); |
| 461 | ifp->if_index = ++if_index; |
| 462 | |
| 463 | /* |
| 464 | * XXX - |
| 465 | * The old code would work if the interface passed a pre-existing |
| 466 | * chain of ifaddrs to this code. We don't trust our callers to |
| 467 | * properly initialize the tailq, however, so we no longer allow |
| 468 | * this unlikely case. |
| 469 | */ |
| 470 | ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead), |
| 471 | M_IFADDR, M_WAITOK | M_ZERO); |
| 472 | for (i = 0; i < ncpus; ++i) |
| 473 | TAILQ_INIT(&ifp->if_addrheads[i]); |
| 474 | |
| 475 | TAILQ_INIT(&ifp->if_prefixhead); |
| 476 | LIST_INIT(&ifp->if_multiaddrs); |
| 477 | getmicrotime(&ifp->if_lastchange); |
| 478 | if (ifindex2ifnet == NULL || if_index >= if_indexlim) { |
| 479 | unsigned int n; |
| 480 | struct ifnet **q; |
| 481 | |
| 482 | if_indexlim <<= 1; |
| 483 | |
| 484 | /* grow ifindex2ifnet */ |
| 485 | n = if_indexlim * sizeof(*q); |
| 486 | q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO); |
| 487 | if (ifindex2ifnet) { |
| 488 | bcopy(ifindex2ifnet, q, n/2); |
| 489 | kfree(ifindex2ifnet, M_IFADDR); |
| 490 | } |
| 491 | ifindex2ifnet = q; |
| 492 | } |
| 493 | |
| 494 | ifindex2ifnet[if_index] = ifp; |
| 495 | |
| 496 | /* |
| 497 | * create a Link Level name for this device |
| 498 | */ |
| 499 | namelen = strlen(ifp->if_xname); |
| 500 | #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) |
| 501 | masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; |
| 502 | socksize = masklen + ifp->if_addrlen; |
| 503 | #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) |
| 504 | if (socksize < sizeof(*sdl)) |
| 505 | socksize = sizeof(*sdl); |
| 506 | socksize = ROUNDUP(socksize); |
| 507 | ifasize = sizeof(struct ifaddr) + 2 * socksize; |
| 508 | ifa = ifa_create(ifasize, M_WAITOK); |
| 509 | sdl = (struct sockaddr_dl *)(ifa + 1); |
| 510 | sdl->sdl_len = socksize; |
| 511 | sdl->sdl_family = AF_LINK; |
| 512 | bcopy(ifp->if_xname, sdl->sdl_data, namelen); |
| 513 | sdl->sdl_nlen = namelen; |
| 514 | sdl->sdl_index = ifp->if_index; |
| 515 | sdl->sdl_type = ifp->if_type; |
| 516 | ifp->if_lladdr = ifa; |
| 517 | ifa->ifa_ifp = ifp; |
| 518 | ifa->ifa_rtrequest = link_rtrequest; |
| 519 | ifa->ifa_addr = (struct sockaddr *)sdl; |
| 520 | sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); |
| 521 | ifa->ifa_netmask = (struct sockaddr *)sdl; |
| 522 | sdl->sdl_len = masklen; |
| 523 | while (namelen != 0) |
| 524 | sdl->sdl_data[--namelen] = 0xff; |
| 525 | ifa_iflink(ifa, ifp, 0 /* Insert head */); |
| 526 | |
| 527 | EVENTHANDLER_INVOKE(ifnet_attach_event, ifp); |
| 528 | devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); |
| 529 | |
| 530 | ifq = &ifp->if_snd; |
| 531 | ifq->altq_type = 0; |
| 532 | ifq->altq_disc = NULL; |
| 533 | ifq->altq_flags &= ALTQF_CANTCHANGE; |
| 534 | ifq->altq_tbr = NULL; |
| 535 | ifq->altq_ifp = ifp; |
| 536 | ifq->altq_started = 0; |
| 537 | ifq->altq_prepended = NULL; |
| 538 | ALTQ_LOCK_INIT(ifq); |
| 539 | ifq_set_classic(ifq); |
| 540 | |
| 541 | if (!SLIST_EMPTY(&domains)) |
| 542 | if_attachdomain1(ifp); |
| 543 | |
| 544 | /* Announce the interface. */ |
| 545 | rt_ifannouncemsg(ifp, IFAN_ARRIVAL); |
| 546 | } |
| 547 | |
| 548 | static void |
| 549 | if_attachdomain(void *dummy) |
| 550 | { |
| 551 | struct ifnet *ifp; |
| 552 | |
| 553 | crit_enter(); |
| 554 | TAILQ_FOREACH(ifp, &ifnet, if_list) |
| 555 | if_attachdomain1(ifp); |
| 556 | crit_exit(); |
| 557 | } |
| 558 | SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, |
| 559 | if_attachdomain, NULL); |
| 560 | |
| 561 | static void |
| 562 | if_attachdomain1(struct ifnet *ifp) |
| 563 | { |
| 564 | struct domain *dp; |
| 565 | |
| 566 | crit_enter(); |
| 567 | |
| 568 | /* address family dependent data region */ |
| 569 | bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); |
| 570 | SLIST_FOREACH(dp, &domains, dom_next) |
| 571 | if (dp->dom_ifattach) |
| 572 | ifp->if_afdata[dp->dom_family] = |
| 573 | (*dp->dom_ifattach)(ifp); |
| 574 | crit_exit(); |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * Purge all addresses whose type is _not_ AF_LINK |
| 579 | */ |
| 580 | void |
| 581 | if_purgeaddrs_nolink(struct ifnet *ifp) |
| 582 | { |
| 583 | struct ifaddr_container *ifac, *next; |
| 584 | |
| 585 | TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid], |
| 586 | ifa_link, next) { |
| 587 | struct ifaddr *ifa = ifac->ifa; |
| 588 | |
| 589 | /* Leave link ifaddr as it is */ |
| 590 | if (ifa->ifa_addr->sa_family == AF_LINK) |
| 591 | continue; |
| 592 | #ifdef INET |
| 593 | /* XXX: Ugly!! ad hoc just for INET */ |
| 594 | if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { |
| 595 | struct ifaliasreq ifr; |
| 596 | #ifdef IFADDR_DEBUG_VERBOSE |
| 597 | int i; |
| 598 | |
| 599 | kprintf("purge in4 addr %p: ", ifa); |
| 600 | for (i = 0; i < ncpus; ++i) |
| 601 | kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt); |
| 602 | kprintf("\n"); |
| 603 | #endif |
| 604 | |
| 605 | bzero(&ifr, sizeof ifr); |
| 606 | ifr.ifra_addr = *ifa->ifa_addr; |
| 607 | if (ifa->ifa_dstaddr) |
| 608 | ifr.ifra_broadaddr = *ifa->ifa_dstaddr; |
| 609 | if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, |
| 610 | NULL) == 0) |
| 611 | continue; |
| 612 | } |
| 613 | #endif /* INET */ |
| 614 | #ifdef INET6 |
| 615 | if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { |
| 616 | #ifdef IFADDR_DEBUG_VERBOSE |
| 617 | int i; |
| 618 | |
| 619 | kprintf("purge in6 addr %p: ", ifa); |
| 620 | for (i = 0; i < ncpus; ++i) |
| 621 | kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt); |
| 622 | kprintf("\n"); |
| 623 | #endif |
| 624 | |
| 625 | in6_purgeaddr(ifa); |
| 626 | /* ifp_addrhead is already updated */ |
| 627 | continue; |
| 628 | } |
| 629 | #endif /* INET6 */ |
| 630 | ifa_ifunlink(ifa, ifp); |
| 631 | ifa_destroy(ifa); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * Detach an interface, removing it from the |
| 637 | * list of "active" interfaces. |
| 638 | */ |
| 639 | void |
| 640 | if_detach(struct ifnet *ifp) |
| 641 | { |
| 642 | struct radix_node_head *rnh; |
| 643 | int i; |
| 644 | int cpu, origcpu; |
| 645 | struct domain *dp; |
| 646 | |
| 647 | EVENTHANDLER_INVOKE(ifnet_detach_event, ifp); |
| 648 | |
| 649 | /* |
| 650 | * Remove routes and flush queues. |
| 651 | */ |
| 652 | crit_enter(); |
| 653 | #ifdef DEVICE_POLLING |
| 654 | if (ifp->if_flags & IFF_POLLING) |
| 655 | ether_poll_deregister(ifp); |
| 656 | #endif |
| 657 | if_down(ifp); |
| 658 | |
| 659 | if (ifq_is_enabled(&ifp->if_snd)) |
| 660 | altq_disable(&ifp->if_snd); |
| 661 | if (ifq_is_attached(&ifp->if_snd)) |
| 662 | altq_detach(&ifp->if_snd); |
| 663 | |
| 664 | /* |
| 665 | * Clean up all addresses. |
| 666 | */ |
| 667 | ifp->if_lladdr = NULL; |
| 668 | |
| 669 | if_purgeaddrs_nolink(ifp); |
| 670 | if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) { |
| 671 | struct ifaddr *ifa; |
| 672 | |
| 673 | ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa; |
| 674 | KASSERT(ifa->ifa_addr->sa_family == AF_LINK, |
| 675 | ("non-link ifaddr is left on if_addrheads")); |
| 676 | |
| 677 | ifa_ifunlink(ifa, ifp); |
| 678 | ifa_destroy(ifa); |
| 679 | KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]), |
| 680 | ("there are still ifaddrs left on if_addrheads")); |
| 681 | } |
| 682 | |
| 683 | #ifdef INET |
| 684 | /* |
| 685 | * Remove all IPv4 kernel structures related to ifp. |
| 686 | */ |
| 687 | in_ifdetach(ifp); |
| 688 | #endif |
| 689 | |
| 690 | #ifdef INET6 |
| 691 | /* |
| 692 | * Remove all IPv6 kernel structs related to ifp. This should be done |
| 693 | * before removing routing entries below, since IPv6 interface direct |
| 694 | * routes are expected to be removed by the IPv6-specific kernel API. |
| 695 | * Otherwise, the kernel will detect some inconsistency and bark it. |
| 696 | */ |
| 697 | in6_ifdetach(ifp); |
| 698 | #endif |
| 699 | |
| 700 | /* |
| 701 | * Delete all remaining routes using this interface |
| 702 | * Unfortuneatly the only way to do this is to slog through |
| 703 | * the entire routing table looking for routes which point |
| 704 | * to this interface...oh well... |
| 705 | */ |
| 706 | origcpu = mycpuid; |
| 707 | for (cpu = 0; cpu < ncpus2; cpu++) { |
| 708 | lwkt_migratecpu(cpu); |
| 709 | for (i = 1; i <= AF_MAX; i++) { |
| 710 | if ((rnh = rt_tables[cpu][i]) == NULL) |
| 711 | continue; |
| 712 | rnh->rnh_walktree(rnh, if_rtdel, ifp); |
| 713 | } |
| 714 | } |
| 715 | lwkt_migratecpu(origcpu); |
| 716 | |
| 717 | /* Announce that the interface is gone. */ |
| 718 | rt_ifannouncemsg(ifp, IFAN_DEPARTURE); |
| 719 | devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); |
| 720 | |
| 721 | SLIST_FOREACH(dp, &domains, dom_next) |
| 722 | if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) |
| 723 | (*dp->dom_ifdetach)(ifp, |
| 724 | ifp->if_afdata[dp->dom_family]); |
| 725 | |
| 726 | /* |
| 727 | * Remove interface from ifindex2ifp[] and maybe decrement if_index. |
| 728 | */ |
| 729 | ifindex2ifnet[ifp->if_index] = NULL; |
| 730 | while (if_index > 0 && ifindex2ifnet[if_index] == NULL) |
| 731 | if_index--; |
| 732 | |
| 733 | TAILQ_REMOVE(&ifnet, ifp, if_link); |
| 734 | kfree(ifp->if_addrheads, M_IFADDR); |
| 735 | kfree(ifp->if_start_nmsg, M_IFADDR); |
| 736 | crit_exit(); |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * Delete Routes for a Network Interface |
| 741 | * |
| 742 | * Called for each routing entry via the rnh->rnh_walktree() call above |
| 743 | * to delete all route entries referencing a detaching network interface. |
| 744 | * |
| 745 | * Arguments: |
| 746 | * rn pointer to node in the routing table |
| 747 | * arg argument passed to rnh->rnh_walktree() - detaching interface |
| 748 | * |
| 749 | * Returns: |
| 750 | * 0 successful |
| 751 | * errno failed - reason indicated |
| 752 | * |
| 753 | */ |
| 754 | static int |
| 755 | if_rtdel(struct radix_node *rn, void *arg) |
| 756 | { |
| 757 | struct rtentry *rt = (struct rtentry *)rn; |
| 758 | struct ifnet *ifp = arg; |
| 759 | int err; |
| 760 | |
| 761 | if (rt->rt_ifp == ifp) { |
| 762 | |
| 763 | /* |
| 764 | * Protect (sorta) against walktree recursion problems |
| 765 | * with cloned routes |
| 766 | */ |
| 767 | if (!(rt->rt_flags & RTF_UP)) |
| 768 | return (0); |
| 769 | |
| 770 | err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, |
| 771 | rt_mask(rt), rt->rt_flags, |
| 772 | (struct rtentry **) NULL); |
| 773 | if (err) { |
| 774 | log(LOG_WARNING, "if_rtdel: error %d\n", err); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | return (0); |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Locate an interface based on a complete address. |
| 783 | */ |
| 784 | struct ifaddr * |
| 785 | ifa_ifwithaddr(struct sockaddr *addr) |
| 786 | { |
| 787 | struct ifnet *ifp; |
| 788 | |
| 789 | TAILQ_FOREACH(ifp, &ifnet, if_link) { |
| 790 | struct ifaddr_container *ifac; |
| 791 | |
| 792 | TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { |
| 793 | struct ifaddr *ifa = ifac->ifa; |
| 794 | |
| 795 | if (ifa->ifa_addr->sa_family != addr->sa_family) |
| 796 | continue; |
| 797 | if (sa_equal(addr, ifa->ifa_addr)) |
| 798 | return (ifa); |
| 799 | if ((ifp->if_flags & IFF_BROADCAST) && |
| 800 | ifa->ifa_broadaddr && |
| 801 | /* IPv6 doesn't have broadcast */ |
| 802 | ifa->ifa_broadaddr->sa_len != 0 && |
| 803 | sa_equal(ifa->ifa_broadaddr, addr)) |
| 804 | return (ifa); |
| 805 | } |
| 806 | } |
| 807 | return (NULL); |
| 808 | } |
| 809 | /* |
| 810 | * Locate the point to point interface with a given destination address. |
| 811 | */ |
| 812 | struct ifaddr * |
| 813 | ifa_ifwithdstaddr(struct sockaddr *addr) |
| 814 | { |
| 815 | struct ifnet *ifp; |
| 816 | |
| 817 | TAILQ_FOREACH(ifp, &ifnet, if_link) { |
| 818 | struct ifaddr_container *ifac; |
| 819 | |
| 820 | if (!(ifp->if_flags & IFF_POINTOPOINT)) |
| 821 | continue; |
| 822 | |
| 823 | TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { |
| 824 | struct ifaddr *ifa = ifac->ifa; |
| 825 | |
| 826 | if (ifa->ifa_addr->sa_family != addr->sa_family) |
| 827 | continue; |
| 828 | if (ifa->ifa_dstaddr && |
| 829 | sa_equal(addr, ifa->ifa_dstaddr)) |
| 830 | return (ifa); |
| 831 | } |
| 832 | } |
| 833 | return (NULL); |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Find an interface on a specific network. If many, choice |
| 838 | * is most specific found. |
| 839 | */ |
| 840 | struct ifaddr * |
| 841 | ifa_ifwithnet(struct sockaddr *addr) |
| 842 | { |
| 843 | struct ifnet *ifp; |
| 844 | struct ifaddr *ifa_maybe = NULL; |
| 845 | u_int af = addr->sa_family; |
| 846 | char *addr_data = addr->sa_data, *cplim; |
| 847 | |
| 848 | /* |
| 849 | * AF_LINK addresses can be looked up directly by their index number, |
| 850 | * so do that if we can. |
| 851 | */ |
| 852 | if (af == AF_LINK) { |
| 853 | struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; |
| 854 | |
| 855 | if (sdl->sdl_index && sdl->sdl_index <= if_index) |
| 856 | return (ifindex2ifnet[sdl->sdl_index]->if_lladdr); |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Scan though each interface, looking for ones that have |
| 861 | * addresses in this address family. |
| 862 | */ |
| 863 | TAILQ_FOREACH(ifp, &ifnet, if_link) { |
| 864 | struct ifaddr_container *ifac; |
| 865 | |
| 866 | TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { |
| 867 | struct ifaddr *ifa = ifac->ifa; |
| 868 | char *cp, *cp2, *cp3; |
| 869 | |
| 870 | if (ifa->ifa_addr->sa_family != af) |
| 871 | next: continue; |
| 872 | if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { |
| 873 | /* |
| 874 | * This is a bit broken as it doesn't |
| 875 | * take into account that the remote end may |
| 876 | * be a single node in the network we are |
| 877 | * looking for. |
| 878 | * The trouble is that we don't know the |
| 879 | * netmask for the remote end. |
| 880 | */ |
| 881 | if (ifa->ifa_dstaddr != NULL && |
| 882 | sa_equal(addr, ifa->ifa_dstaddr)) |
| 883 | return (ifa); |
| 884 | } else { |
| 885 | /* |
| 886 | * if we have a special address handler, |
| 887 | * then use it instead of the generic one. |
| 888 | */ |
| 889 | if (ifa->ifa_claim_addr) { |
| 890 | if ((*ifa->ifa_claim_addr)(ifa, addr)) { |
| 891 | return (ifa); |
| 892 | } else { |
| 893 | continue; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * Scan all the bits in the ifa's address. |
| 899 | * If a bit dissagrees with what we are |
| 900 | * looking for, mask it with the netmask |
| 901 | * to see if it really matters. |
| 902 | * (A byte at a time) |
| 903 | */ |
| 904 | if (ifa->ifa_netmask == 0) |
| 905 | continue; |
| 906 | cp = addr_data; |
| 907 | cp2 = ifa->ifa_addr->sa_data; |
| 908 | cp3 = ifa->ifa_netmask->sa_data; |
| 909 | cplim = ifa->ifa_netmask->sa_len + |
| 910 | (char *)ifa->ifa_netmask; |
| 911 | while (cp3 < cplim) |
| 912 | if ((*cp++ ^ *cp2++) & *cp3++) |
| 913 | goto next; /* next address! */ |
| 914 | /* |
| 915 | * If the netmask of what we just found |
| 916 | * is more specific than what we had before |
| 917 | * (if we had one) then remember the new one |
| 918 | * before continuing to search |
| 919 | * for an even better one. |
| 920 | */ |
| 921 | if (ifa_maybe == 0 || |
| 922 | rn_refines((char *)ifa->ifa_netmask, |
| 923 | (char *)ifa_maybe->ifa_netmask)) |
| 924 | ifa_maybe = ifa; |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | return (ifa_maybe); |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Find an interface address specific to an interface best matching |
| 933 | * a given address. |
| 934 | */ |
| 935 | struct ifaddr * |
| 936 | ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) |
| 937 | { |
| 938 | struct ifaddr_container *ifac; |
| 939 | char *cp, *cp2, *cp3; |
| 940 | char *cplim; |
| 941 | struct ifaddr *ifa_maybe = 0; |
| 942 | u_int af = addr->sa_family; |
| 943 | |
| 944 | if (af >= AF_MAX) |
| 945 | return (0); |
| 946 | TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { |
| 947 | struct ifaddr *ifa = ifac->ifa; |
| 948 | |
| 949 | if (ifa->ifa_addr->sa_family != af) |
| 950 | continue; |
| 951 | if (ifa_maybe == 0) |
| 952 | ifa_maybe = ifa; |
| 953 | if (ifa->ifa_netmask == NULL) { |
| 954 | if (sa_equal(addr, ifa->ifa_addr) || |
| 955 | (ifa->ifa_dstaddr != NULL && |
| 956 | sa_equal(addr, ifa->ifa_dstaddr))) |
| 957 | return (ifa); |
| 958 | continue; |
| 959 | } |
| 960 | if (ifp->if_flags & IFF_POINTOPOINT) { |
| 961 | if (sa_equal(addr, ifa->ifa_dstaddr)) |
| 962 | return (ifa); |
| 963 | } else { |
| 964 | cp = addr->sa_data; |
| 965 | cp2 = ifa->ifa_addr->sa_data; |
| 966 | cp3 = ifa->ifa_netmask->sa_data; |
| 967 | cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; |
| 968 | for (; cp3 < cplim; cp3++) |
| 969 | if ((*cp++ ^ *cp2++) & *cp3) |
| 970 | break; |
| 971 | if (cp3 == cplim) |
| 972 | return (ifa); |
| 973 | } |
| 974 | } |
| 975 | return (ifa_maybe); |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Default action when installing a route with a Link Level gateway. |
| 980 | * Lookup an appropriate real ifa to point to. |
| 981 | * This should be moved to /sys/net/link.c eventually. |
| 982 | */ |
| 983 | static void |
| 984 | link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) |
| 985 | { |
| 986 | struct ifaddr *ifa; |
| 987 | struct sockaddr *dst; |
| 988 | struct ifnet *ifp; |
| 989 | |
| 990 | if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL || |
| 991 | (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL) |
| 992 | return; |
| 993 | ifa = ifaof_ifpforaddr(dst, ifp); |
| 994 | if (ifa != NULL) { |
| 995 | IFAFREE(rt->rt_ifa); |
| 996 | IFAREF(ifa); |
| 997 | rt->rt_ifa = ifa; |
| 998 | if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) |
| 999 | ifa->ifa_rtrequest(cmd, rt, info); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /* |
| 1004 | * Mark an interface down and notify protocols of |
| 1005 | * the transition. |
| 1006 | * NOTE: must be called at splnet or eqivalent. |
| 1007 | */ |
| 1008 | void |
| 1009 | if_unroute(struct ifnet *ifp, int flag, int fam) |
| 1010 | { |
| 1011 | struct ifaddr_container *ifac; |
| 1012 | |
| 1013 | ifp->if_flags &= ~flag; |
| 1014 | getmicrotime(&ifp->if_lastchange); |
| 1015 | TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { |
| 1016 | struct ifaddr *ifa = ifac->ifa; |
| 1017 | |
| 1018 | if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) |
| 1019 | kpfctlinput(PRC_IFDOWN, ifa->ifa_addr); |
| 1020 | } |
| 1021 | ifq_purge(&ifp->if_snd); |
| 1022 | rt_ifmsg(ifp); |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * Mark an interface up and notify protocols of |
| 1027 | * the transition. |
| 1028 | * NOTE: must be called at splnet or eqivalent. |
| 1029 | */ |
| 1030 | void |
| 1031 | if_route(struct ifnet *ifp, int flag, int fam) |
| 1032 | { |
| 1033 | struct ifaddr_container *ifac; |
| 1034 | |
| 1035 | ifq_purge(&ifp->if_snd); |
| 1036 | ifp->if_flags |= flag; |
| 1037 | getmicrotime(&ifp->if_lastchange); |
| 1038 | TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { |
| 1039 | struct ifaddr *ifa = ifac->ifa; |
| 1040 | |
| 1041 | if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) |
| 1042 | kpfctlinput(PRC_IFUP, ifa->ifa_addr); |
| 1043 | } |
| 1044 | rt_ifmsg(ifp); |
| 1045 | #ifdef INET6 |
| 1046 | in6_if_up(ifp); |
| 1047 | #endif |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Mark an interface down and notify protocols of the transition. An |
| 1052 | * interface going down is also considered to be a synchronizing event. |
| 1053 | * We must ensure that all packet processing related to the interface |
| 1054 | * has completed before we return so e.g. the caller can free the ifnet |
| 1055 | * structure that the mbufs may be referencing. |
| 1056 | * |
| 1057 | * NOTE: must be called at splnet or eqivalent. |
| 1058 | */ |
| 1059 | void |
| 1060 | if_down(struct ifnet *ifp) |
| 1061 | { |
| 1062 | if_unroute(ifp, IFF_UP, AF_UNSPEC); |
| 1063 | netmsg_service_sync(); |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * Mark an interface up and notify protocols of |
| 1068 | * the transition. |
| 1069 | * NOTE: must be called at splnet or eqivalent. |
| 1070 | */ |
| 1071 | void |
| 1072 | if_up(struct ifnet *ifp) |
| 1073 | { |
| 1074 | if_route(ifp, IFF_UP, AF_UNSPEC); |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Process a link state change. |
| 1079 | * NOTE: must be called at splsoftnet or equivalent. |
| 1080 | */ |
| 1081 | void |
| 1082 | if_link_state_change(struct ifnet *ifp) |
| 1083 | { |
| 1084 | int link_state = ifp->if_link_state; |
| 1085 | |
| 1086 | rt_ifmsg(ifp); |
| 1087 | devctl_notify("IFNET", ifp->if_xname, |
| 1088 | (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * Handle interface watchdog timer routines. Called |
| 1093 | * from softclock, we decrement timers (if set) and |
| 1094 | * call the appropriate interface routine on expiration. |
| 1095 | */ |
| 1096 | static void |
| 1097 | if_slowtimo(void *arg) |
| 1098 | { |
| 1099 | struct ifnet *ifp; |
| 1100 | |
| 1101 | crit_enter(); |
| 1102 | |
| 1103 | TAILQ_FOREACH(ifp, &ifnet, if_link) { |
| 1104 | if (ifp->if_timer == 0 || --ifp->if_timer) |
| 1105 | continue; |
| 1106 | if (ifp->if_watchdog) { |
| 1107 | if (lwkt_serialize_try(ifp->if_serializer)) { |
| 1108 | (*ifp->if_watchdog)(ifp); |
| 1109 | lwkt_serialize_exit(ifp->if_serializer); |
| 1110 | } else { |
| 1111 | /* try again next timeout */ |
| 1112 | ++ifp->if_timer; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | crit_exit(); |
| 1118 | |
| 1119 | callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL); |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * Map interface name to |
| 1124 | * interface structure pointer. |
| 1125 | */ |
| 1126 | struct ifnet * |
| 1127 | ifunit(const char *name) |
| 1128 | { |
| 1129 | struct ifnet *ifp; |
| 1130 | |
| 1131 | /* |
| 1132 | * Search all the interfaces for this name/number |
| 1133 | */ |
| 1134 | |
| 1135 | TAILQ_FOREACH(ifp, &ifnet, if_link) { |
| 1136 | if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0) |
| 1137 | break; |
| 1138 | } |
| 1139 | return (ifp); |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | /* |
| 1144 | * Map interface name in a sockaddr_dl to |
| 1145 | * interface structure pointer. |
| 1146 | */ |
| 1147 | struct ifnet * |
| 1148 | if_withname(struct sockaddr *sa) |
| 1149 | { |
| 1150 | char ifname[IFNAMSIZ+1]; |
| 1151 | struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; |
| 1152 | |
| 1153 | if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) || |
| 1154 | (sdl->sdl_nlen > IFNAMSIZ) ) |
| 1155 | return NULL; |
| 1156 | |
| 1157 | /* |
| 1158 | * ifunit wants a null-terminated name. It may not be null-terminated |
| 1159 | * in the sockaddr. We don't want to change the caller's sockaddr, |
| 1160 | * and there might not be room to put the trailing null anyway, so we |
| 1161 | * make a local copy that we know we can null terminate safely. |
| 1162 | */ |
| 1163 | |
| 1164 | bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen); |
| 1165 | ifname[sdl->sdl_nlen] = '\0'; |
| 1166 | return ifunit(ifname); |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | /* |
| 1171 | * Interface ioctls. |
| 1172 | */ |
| 1173 | int |
| 1174 | ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred) |
| 1175 | { |
| 1176 | struct ifnet *ifp; |
| 1177 | struct ifreq *ifr; |
| 1178 | struct ifstat *ifs; |
| 1179 | int error; |
| 1180 | short oif_flags; |
| 1181 | int new_flags; |
| 1182 | size_t namelen, onamelen; |
| 1183 | char new_name[IFNAMSIZ]; |
| 1184 | struct ifaddr *ifa; |
| 1185 | struct sockaddr_dl *sdl; |
| 1186 | |
| 1187 | switch (cmd) { |
| 1188 | |
| 1189 | case SIOCGIFCONF: |
| 1190 | case OSIOCGIFCONF: |
| 1191 | return (ifconf(cmd, data, cred)); |
| 1192 | } |
| 1193 | ifr = (struct ifreq *)data; |
| 1194 | |
| 1195 | switch (cmd) { |
| 1196 | case SIOCIFCREATE: |
| 1197 | case SIOCIFDESTROY: |
| 1198 | if ((error = suser_cred(cred, 0)) != 0) |
| 1199 | return (error); |
| 1200 | return ((cmd == SIOCIFCREATE) ? |
| 1201 | if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) : |
| 1202 | if_clone_destroy(ifr->ifr_name)); |
| 1203 | |
| 1204 | case SIOCIFGCLONERS: |
| 1205 | return (if_clone_list((struct if_clonereq *)data)); |
| 1206 | } |
| 1207 | |
| 1208 | ifp = ifunit(ifr->ifr_name); |
| 1209 | if (ifp == 0) |
| 1210 | return (ENXIO); |
| 1211 | switch (cmd) { |
| 1212 | |
| 1213 | case SIOCGIFFLAGS: |
| 1214 | ifr->ifr_flags = ifp->if_flags; |
| 1215 | ifr->ifr_flagshigh = ifp->if_flags >> 16; |
| 1216 | break; |
| 1217 | |
| 1218 | case SIOCGIFCAP: |
| 1219 | ifr->ifr_reqcap = ifp->if_capabilities; |
| 1220 | ifr->ifr_curcap = ifp->if_capenable; |
| 1221 | break; |
| 1222 | |
| 1223 | case SIOCGIFMETRIC: |
| 1224 | ifr->ifr_metric = ifp->if_metric; |
| 1225 | break; |
| 1226 | |
| 1227 | case SIOCGIFMTU: |
| 1228 | ifr->ifr_mtu = ifp->if_mtu; |
| 1229 | break; |
| 1230 | |
| 1231 | case SIOCGIFPHYS: |
| 1232 | ifr->ifr_phys = ifp->if_physical; |
| 1233 | break; |
| 1234 | |
| 1235 | case SIOCGIFPOLLCPU: |
| 1236 | #ifdef DEVICE_POLLING |
| 1237 | ifr->ifr_pollcpu = ifp->if_poll_cpuid; |
| 1238 | #else |
| 1239 | ifr->ifr_pollcpu = -1; |
| 1240 | #endif |
| 1241 | break; |
| 1242 | |
| 1243 | case SIOCSIFPOLLCPU: |
| 1244 | #ifdef DEVICE_POLLING |
| 1245 | if ((ifp->if_flags & IFF_POLLING) == 0) |
| 1246 | ether_pollcpu_register(ifp, ifr->ifr_pollcpu); |
| 1247 | #endif |
| 1248 | break; |
| 1249 | |
| 1250 | case SIOCSIFFLAGS: |
| 1251 | error = suser_cred(cred, 0); |
| 1252 | if (error) |
| 1253 | return (error); |
| 1254 | new_flags = (ifr->ifr_flags & 0xffff) | |
| 1255 | (ifr->ifr_flagshigh << 16); |
| 1256 | if (ifp->if_flags & IFF_SMART) { |
| 1257 | /* Smart drivers twiddle their own routes */ |
| 1258 | } else if (ifp->if_flags & IFF_UP && |
| 1259 | (new_flags & IFF_UP) == 0) { |
| 1260 | crit_enter(); |
| 1261 | if_down(ifp); |
| 1262 | crit_exit(); |
| 1263 | } else if (new_flags & IFF_UP && |
| 1264 | (ifp->if_flags & IFF_UP) == 0) { |
| 1265 | crit_enter(); |
| 1266 | if_up(ifp); |
| 1267 | crit_exit(); |
| 1268 | } |
| 1269 | |
| 1270 | #ifdef DEVICE_POLLING |
| 1271 | if ((new_flags ^ ifp->if_flags) & IFF_POLLING) { |
| 1272 | if (new_flags & IFF_POLLING) { |
| 1273 | ether_poll_register(ifp); |
| 1274 | } else { |
| 1275 | ether_poll_deregister(ifp); |
| 1276 | } |
| 1277 | } |
| 1278 | #endif |
| 1279 | |
| 1280 | ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | |
| 1281 | (new_flags &~ IFF_CANTCHANGE); |
| 1282 | if (new_flags & IFF_PPROMISC) { |
| 1283 | /* Permanently promiscuous mode requested */ |
| 1284 | ifp->if_flags |= IFF_PROMISC; |
| 1285 | } else if (ifp->if_pcount == 0) { |
| 1286 | ifp->if_flags &= ~IFF_PROMISC; |
| 1287 | } |
| 1288 | if (ifp->if_ioctl) { |
| 1289 | lwkt_serialize_enter(ifp->if_serializer); |
| 1290 | ifp->if_ioctl(ifp, cmd, data, cred); |
| 1291 | lwkt_serialize_exit(ifp->if_serializer); |
| 1292 | } |
| 1293 | getmicrotime(&ifp->if_lastchange); |
| 1294 | break; |
| 1295 | |
| 1296 | case SIOCSIFCAP: |
| 1297 | error = suser_cred(cred, 0); |
| 1298 | if (error) |
| 1299 | return (error); |
| 1300 | if (ifr->ifr_reqcap & ~ifp->if_capabilities) |
| 1301 | return (EINVAL); |
| 1302 | lwkt_serialize_enter(ifp->if_serializer); |
| 1303 | ifp->if_ioctl(ifp, cmd, data, cred); |
| 1304 | lwkt_serialize_exit(ifp->if_serializer); |
| 1305 | break; |
| 1306 | |
| 1307 | case SIOCSIFNAME: |
| 1308 | error = suser_cred(cred, 0); |
| 1309 | if (error != 0) |
| 1310 | return (error); |
| 1311 | error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); |
| 1312 | if (error != 0) |
| 1313 | return (error); |
| 1314 | if (new_name[0] == '\0') |
| 1315 | return (EINVAL); |
| 1316 | if (ifunit(new_name) != NULL) |
| 1317 | return (EEXIST); |
| 1318 | |
| 1319 | EVENTHANDLER_INVOKE(ifnet_detach_event, ifp); |
| 1320 | |
| 1321 | /* Announce the departure of the interface. */ |
| 1322 | rt_ifannouncemsg(ifp, IFAN_DEPARTURE); |
| 1323 | |
| 1324 | strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); |
| 1325 | ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa; |
| 1326 | /* XXX IFA_LOCK(ifa); */ |
| 1327 | sdl = (struct sockaddr_dl *)ifa->ifa_addr; |
| 1328 | namelen = strlen(new_name); |
| 1329 | onamelen = sdl->sdl_nlen; |
| 1330 | /* |
| 1331 | * Move the address if needed. This is safe because we |
| 1332 | * allocate space for a name of length IFNAMSIZ when we |
| 1333 | * create this in if_attach(). |
| 1334 | */ |
| 1335 | if (namelen != onamelen) { |
| 1336 | bcopy(sdl->sdl_data + onamelen, |
| 1337 | sdl->sdl_data + namelen, sdl->sdl_alen); |
| 1338 | } |
| 1339 | bcopy(new_name, sdl->sdl_data, namelen); |
| 1340 | sdl->sdl_nlen = namelen; |
| 1341 | sdl = (struct sockaddr_dl *)ifa->ifa_netmask; |
| 1342 | bzero(sdl->sdl_data, onamelen); |
| 1343 | while (namelen != 0) |
| 1344 | sdl->sdl_data[--namelen] = 0xff; |
| 1345 | /* XXX IFA_UNLOCK(ifa) */ |
| 1346 | |
| 1347 | EVENTHANDLER_INVOKE(ifnet_attach_event, ifp); |
| 1348 | |
| 1349 | /* Announce the return of the interface. */ |
| 1350 | rt_ifannouncemsg(ifp, IFAN_ARRIVAL); |
| 1351 | break; |
| 1352 | |
| 1353 | case SIOCSIFMETRIC: |
| 1354 | error = suser_cred(cred, 0); |
| 1355 | if (error) |
| 1356 | return (error); |
| 1357 | ifp->if_metric = ifr->ifr_metric; |
| 1358 | getmicrotime(&ifp->if_lastchange); |
| 1359 | break; |
| 1360 | |
| 1361 | case SIOCSIFPHYS: |
| 1362 | error = suser_cred(cred, 0); |
| 1363 | if (error) |
| 1364 | return error; |
| 1365 | if (!ifp->if_ioctl) |
| 1366 | return EOPNOTSUPP; |
| 1367 | lwkt_serialize_enter(ifp->if_serializer); |
| 1368 | error = ifp->if_ioctl(ifp, cmd, data, cred); |
| 1369 | lwkt_serialize_exit(ifp->if_serializer); |
| 1370 | if (error == 0) |
| 1371 | getmicrotime(&ifp->if_lastchange); |
| 1372 | return (error); |
| 1373 | |
| 1374 | case SIOCSIFMTU: |
| 1375 | { |
| 1376 | u_long oldmtu = ifp->if_mtu; |
| 1377 | |
| 1378 | error = suser_cred(cred, 0); |
| 1379 | if (error) |
| 1380 | return (error); |
| 1381 | if (ifp->if_ioctl == NULL) |
| 1382 | return (EOPNOTSUPP); |
| 1383 | if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) |
| 1384 | return (EINVAL); |
| 1385 | lwkt_serialize_enter(ifp->if_serializer); |
| 1386 | error = ifp->if_ioctl(ifp, cmd, data, cred); |
| 1387 | lwkt_serialize_exit(ifp->if_serializer); |
| 1388 | if (error == 0) { |
| 1389 | getmicrotime(&ifp->if_lastchange); |
| 1390 | rt_ifmsg(ifp); |
| 1391 | } |
| 1392 | /* |
| 1393 | * If the link MTU changed, do network layer specific procedure. |
| 1394 | */ |
| 1395 | if (ifp->if_mtu != oldmtu) { |
| 1396 | #ifdef INET6 |
| 1397 | nd6_setmtu(ifp); |
| 1398 | #endif |
| 1399 | } |
| 1400 | return (error); |
| 1401 | } |
| 1402 | |
| 1403 | case SIOCADDMULTI: |
| 1404 | case SIOCDELMULTI: |
| 1405 | error = suser_cred(cred, 0); |
| 1406 | if (error) |
| 1407 | return (error); |
| 1408 | |
| 1409 | /* Don't allow group membership on non-multicast interfaces. */ |
| 1410 | if ((ifp->if_flags & IFF_MULTICAST) == 0) |
| 1411 | return EOPNOTSUPP; |
| 1412 | |
| 1413 | /* Don't let users screw up protocols' entries. */ |
| 1414 | if (ifr->ifr_addr.sa_family != AF_LINK) |
| 1415 | return EINVAL; |
| 1416 | |
| 1417 | if (cmd == SIOCADDMULTI) { |
| 1418 | struct ifmultiaddr *ifma; |
| 1419 | error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); |
| 1420 | } else { |
| 1421 | error = if_delmulti(ifp, &ifr->ifr_addr); |
| 1422 | } |
| 1423 | if (error == 0) |
| 1424 | getmicrotime(&ifp->if_lastchange); |
| 1425 | return error; |
| 1426 | |
| 1427 | case SIOCSIFPHYADDR: |
| 1428 | case SIOCDIFPHYADDR: |
| 1429 | #ifdef INET6 |
| 1430 | case SIOCSIFPHYADDR_IN6: |
| 1431 | #endif |
| 1432 | case SIOCSLIFPHYADDR: |
| 1433 | case SIOCSIFMEDIA: |
| 1434 | case SIOCSIFGENERIC: |
| 1435 | error = suser_cred(cred, 0); |
| 1436 | if (error) |
| 1437 | return (error); |
| 1438 | if (ifp->if_ioctl == 0) |
| 1439 | return (EOPNOTSUPP); |
| 1440 | lwkt_serialize_enter(ifp->if_serializer); |
| 1441 | error = ifp->if_ioctl(ifp, cmd, data, cred); |
| 1442 | lwkt_serialize_exit(ifp->if_serializer); |
| 1443 | if (error == 0) |
| 1444 | getmicrotime(&ifp->if_lastchange); |
| 1445 | return error; |
| 1446 | |
| 1447 | case SIOCGIFSTATUS: |
| 1448 | ifs = (struct ifstat *)data; |
| 1449 | ifs->ascii[0] = '\0'; |
| 1450 | |
| 1451 | case SIOCGIFPSRCADDR: |
| 1452 | case SIOCGIFPDSTADDR: |
| 1453 | case SIOCGLIFPHYADDR: |
| 1454 | case SIOCGIFMEDIA: |
| 1455 | case SIOCGIFGENERIC: |
| 1456 | if (ifp->if_ioctl == NULL) |
| 1457 | return (EOPNOTSUPP); |
| 1458 | lwkt_serialize_enter(ifp->if_serializer); |
| 1459 | error = ifp->if_ioctl(ifp, cmd, data, cred); |
| 1460 | lwkt_serialize_exit(ifp->if_serializer); |
| 1461 | return (error); |
| 1462 | |
| 1463 | case SIOCSIFLLADDR: |
| 1464 | error = suser_cred(cred, 0); |
| 1465 | if (error) |
| 1466 | return (error); |
| 1467 | return if_setlladdr(ifp, |
| 1468 | ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); |
| 1469 | |
| 1470 | default: |
| 1471 | oif_flags = ifp->if_flags; |
| 1472 | if (so->so_proto == 0) |
| 1473 | return (EOPNOTSUPP); |
| 1474 | #ifndef COMPAT_43 |
| 1475 | error = so_pru_control(so, cmd, data, ifp); |
| 1476 | #else |
| 1477 | { |
| 1478 | int ocmd = cmd; |
| 1479 | |
| 1480 | switch (cmd) { |
| 1481 | |
| 1482 | case SIOCSIFDSTADDR: |
| 1483 | case SIOCSIFADDR: |
| 1484 | case SIOCSIFBRDADDR: |
| 1485 | case SIOCSIFNETMASK: |
| 1486 | #if BYTE_ORDER != BIG_ENDIAN |
| 1487 | if (ifr->ifr_addr.sa_family == 0 && |
| 1488 | ifr->ifr_addr.sa_len < 16) { |
| 1489 | ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; |
| 1490 | ifr->ifr_addr.sa_len = 16; |
| 1491 | } |
| 1492 | #else |
| 1493 | if (ifr->ifr_addr.sa_len == 0) |
| 1494 | ifr->ifr_addr.sa_len = 16; |
| 1495 | #endif |
| 1496 | break; |
| 1497 | |
| 1498 | case OSIOCGIFADDR: |
| 1499 | cmd = SIOCGIFADDR; |
| 1500 | break; |
| 1501 | |
| 1502 | case OSIOCGIFDSTADDR: |
| 1503 | cmd = SIOCGIFDSTADDR; |
| 1504 | break; |
| 1505 | |
| 1506 | case OSIOCGIFBRDADDR: |
| 1507 | cmd = SIOCGIFBRDADDR; |
| 1508 | break; |
| 1509 | |
| 1510 | case OSIOCGIFNETMASK: |
| 1511 | cmd = SIOCGIFNETMASK; |
| 1512 | } |
| 1513 | error = so_pru_control(so, cmd, data, ifp); |
| 1514 | switch (ocmd) { |
| 1515 | |
| 1516 | case OSIOCGIFADDR: |
| 1517 | case OSIOCGIFDSTADDR: |
| 1518 | case OSIOCGIFBRDADDR: |
| 1519 | case OSIOCGIFNETMASK: |
| 1520 | *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; |
| 1521 | |
| 1522 | } |
| 1523 | } |
| 1524 | #endif /* COMPAT_43 */ |
| 1525 | |
| 1526 | if ((oif_flags ^ ifp->if_flags) & IFF_UP) { |
| 1527 | #ifdef INET6 |
| 1528 | DELAY(100);/* XXX: temporary workaround for fxp issue*/ |
| 1529 | if (ifp->if_flags & IFF_UP) { |
| 1530 | crit_enter(); |
| 1531 | in6_if_up(ifp); |
| 1532 | crit_exit(); |
| 1533 | } |
| 1534 | #endif |
| 1535 | } |
| 1536 | return (error); |
| 1537 | |
| 1538 | } |
| 1539 | return (0); |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * Set/clear promiscuous mode on interface ifp based on the truth value |
| 1544 | * of pswitch. The calls are reference counted so that only the first |
| 1545 | * "on" request actually has an effect, as does the final "off" request. |
| 1546 | * Results are undefined if the "off" and "on" requests are not matched. |
| 1547 | */ |
| 1548 | int |
| 1549 | ifpromisc(struct ifnet *ifp, int pswitch) |
| 1550 | { |
| 1551 | struct ifreq ifr; |
| 1552 | int error; |
| 1553 | int oldflags; |
| 1554 | |
| 1555 | oldflags = ifp->if_flags; |
| 1556 | if (ifp->if_flags & IFF_PPROMISC) { |
| 1557 | /* Do nothing if device is in permanently promiscuous mode */ |
| 1558 | ifp->if_pcount += pswitch ? 1 : -1; |
| 1559 | return (0); |
| 1560 | } |
| 1561 | if (pswitch) { |
| 1562 | /* |
| 1563 | * If the device is not configured up, we cannot put it in |
| 1564 | * promiscuous mode. |
| 1565 | */ |
| 1566 | if ((ifp->if_flags & IFF_UP) == 0) |
| 1567 | return (ENETDOWN); |
| 1568 | if (ifp->if_pcount++ != 0) |
| 1569 | return (0); |
| 1570 | ifp->if_flags |= IFF_PROMISC; |
| 1571 | log(LOG_INFO, "%s: promiscuous mode enabled\n", |
| 1572 | ifp->if_xname); |
| 1573 | } else { |
| 1574 | if (--ifp->if_pcount > 0) |
| 1575 | return (0); |
| 1576 | ifp->if_flags &= ~IFF_PROMISC; |
| 1577 | log(LOG_INFO, "%s: promiscuous mode disabled\n", |
| 1578 | ifp->if_xname); |
| 1579 | } |
| 1580 | ifr.ifr_flags = ifp->if_flags; |
| 1581 | ifr.ifr_flagshigh = ifp->if_flags >> 16; |
| 1582 | lwkt_serialize_enter(ifp->if_serializer); |
| 1583 | error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, |
| 1584 | (struct ucred *)NULL); |
| 1585 | lwkt_serialize_exit(ifp->if_serializer); |
| 1586 | if (error == 0) |
| 1587 | rt_ifmsg(ifp); |
| 1588 | else |
| 1589 | ifp->if_flags = oldflags; |
| 1590 | return error; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * Return interface configuration |
| 1595 | * of system. List may be used |
| 1596 | * in later ioctl's (above) to get |
| 1597 | * other information. |
| 1598 | */ |
| 1599 | static int |
| 1600 | ifconf(u_long cmd, caddr_t data, struct ucred *cred) |
| 1601 | { |
| 1602 | struct ifconf *ifc = (struct ifconf *)data; |
| 1603 | struct ifnet *ifp; |
| 1604 | struct sockaddr *sa; |
| 1605 | struct ifreq ifr, *ifrp; |
| 1606 | int space = ifc->ifc_len, error = 0; |
| 1607 | |
| 1608 | ifrp = ifc->ifc_req; |
| 1609 | TAILQ_FOREACH(ifp, &ifnet, if_link) { |
| 1610 | struct ifaddr_container *ifac; |
| 1611 | int addrs; |
| 1612 | |
| 1613 | if (space <= sizeof ifr) |
| 1614 | break; |
| 1615 | |
| 1616 | /* |
| 1617 | * Zero the stack declared structure first to prevent |
| 1618 | * memory disclosure. |
| 1619 | */ |
| 1620 | bzero(&ifr, sizeof(ifr)); |
| 1621 | if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) |
| 1622 | >= sizeof(ifr.ifr_name)) { |
| 1623 | error = ENAMETOOLONG; |
| 1624 | break; |
| 1625 | } |
| 1626 | |
| 1627 | addrs = 0; |
| 1628 | TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { |
| 1629 | struct ifaddr *ifa = ifac->ifa; |
| 1630 | |
| 1631 | if (space <= sizeof ifr) |
| 1632 | break; |
| 1633 | sa = ifa->ifa_addr; |
| 1634 | if (cred->cr_prison && |
| 1635 | prison_if(cred, sa)) |
| 1636 | continue; |
| 1637 | addrs++; |
| 1638 | #ifdef COMPAT_43 |
| 1639 | if (cmd == OSIOCGIFCONF) { |
| 1640 | struct osockaddr *osa = |
| 1641 | (struct osockaddr *)&ifr.ifr_addr; |
| 1642 | ifr.ifr_addr = *sa; |
| 1643 | osa->sa_family = sa->sa_family; |
| 1644 | error = copyout(&ifr, ifrp, sizeof ifr); |
| 1645 | ifrp++; |
| 1646 | } else |
| 1647 | #endif |
| 1648 | if (sa->sa_len <= sizeof(*sa)) { |
| 1649 | ifr.ifr_addr = *sa; |
| 1650 | error = copyout(&ifr, ifrp, sizeof ifr); |
| 1651 | ifrp++; |
| 1652 | } else { |
| 1653 | if (space < (sizeof ifr) + sa->sa_len - |
| 1654 | sizeof(*sa)) |
| 1655 | break; |
| 1656 | space -= sa->sa_len - sizeof(*sa); |
| 1657 | error = copyout(&ifr, ifrp, |
| 1658 | sizeof ifr.ifr_name); |
| 1659 | if (error == 0) |
| 1660 | error = copyout(sa, &ifrp->ifr_addr, |
| 1661 | sa->sa_len); |
| 1662 | ifrp = (struct ifreq *) |
| 1663 | (sa->sa_len + (caddr_t)&ifrp->ifr_addr); |
| 1664 | } |
| 1665 | if (error) |
| 1666 | break; |
| 1667 | space -= sizeof ifr; |
| 1668 | } |
| 1669 | if (error) |
| 1670 | break; |
| 1671 | if (!addrs) { |
| 1672 | bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr); |
| 1673 | error = copyout(&ifr, ifrp, sizeof ifr); |
| 1674 | if (error) |
| 1675 | break; |
| 1676 | space -= sizeof ifr; |
| 1677 | ifrp++; |
| 1678 | } |
| 1679 | } |
| 1680 | ifc->ifc_len -= space; |
| 1681 | return (error); |
| 1682 | } |
| 1683 | |
| 1684 | /* |
| 1685 | * Just like if_promisc(), but for all-multicast-reception mode. |
| 1686 | */ |
| 1687 | int |
| 1688 | if_allmulti(struct ifnet *ifp, int onswitch) |
| 1689 | { |
| 1690 | int error = 0; |
| 1691 | struct ifreq ifr; |
| 1692 | |
| 1693 | crit_enter(); |
| 1694 | |
| 1695 | if (onswitch) { |
| 1696 | if (ifp->if_amcount++ == 0) { |
| 1697 | ifp->if_flags |= IFF_ALLMULTI; |
| 1698 | ifr.ifr_flags = ifp->if_flags; |
| 1699 | ifr.ifr_flagshigh = ifp->if_flags >> 16; |
| 1700 | lwkt_serialize_enter(ifp->if_serializer); |
| 1701 | error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, |
| 1702 | (struct ucred *)NULL); |
| 1703 | lwkt_serialize_exit(ifp->if_serializer); |
| 1704 | } |
| 1705 | } else { |
| 1706 | if (ifp->if_amcount > 1) { |
| 1707 | ifp->if_amcount--; |
| 1708 | } else { |
| 1709 | ifp->if_amcount = 0; |
| 1710 | ifp->if_flags &= ~IFF_ALLMULTI; |
| 1711 | ifr.ifr_flags = ifp->if_flags; |
| 1712 | ifr.ifr_flagshigh = ifp->if_flags >> 16; |
| 1713 | lwkt_serialize_enter(ifp->if_serializer); |
| 1714 | error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, |
| 1715 | (struct ucred *)NULL); |
| 1716 | lwkt_serialize_exit(ifp->if_serializer); |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | crit_exit(); |
| 1721 | |
| 1722 | if (error == 0) |
| 1723 | rt_ifmsg(ifp); |
| 1724 | return error; |
| 1725 | } |
| 1726 | |
| 1727 | /* |
| 1728 | * Add a multicast listenership to the interface in question. |
| 1729 | * The link layer provides a routine which converts |
| 1730 | */ |
| 1731 | int |
| 1732 | if_addmulti( |
| 1733 | struct ifnet *ifp, /* interface to manipulate */ |
| 1734 | struct sockaddr *sa, /* address to add */ |
| 1735 | struct ifmultiaddr **retifma) |
| 1736 | { |
| 1737 | struct sockaddr *llsa, *dupsa; |
| 1738 | int error; |
| 1739 | struct ifmultiaddr *ifma; |
| 1740 | |
| 1741 | /* |
| 1742 | * If the matching multicast address already exists |
| 1743 | * then don't add a new one, just add a reference |
| 1744 | */ |
| 1745 | LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { |
| 1746 | if (sa_equal(sa, ifma->ifma_addr)) { |
| 1747 | ifma->ifma_refcount++; |
| 1748 | if (retifma) |
| 1749 | *retifma = ifma; |
| 1750 | return 0; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | * Give the link layer a chance to accept/reject it, and also |
| 1756 | * find out which AF_LINK address this maps to, if it isn't one |
| 1757 | * already. |
| 1758 | */ |
| 1759 | if (ifp->if_resolvemulti) { |
| 1760 | lwkt_serialize_enter(ifp->if_serializer); |
| 1761 | error = ifp->if_resolvemulti(ifp, &llsa, sa); |
| 1762 | lwkt_serialize_exit(ifp->if_serializer); |
| 1763 | if (error) |
| 1764 | return error; |
| 1765 | } else { |
| 1766 | llsa = 0; |
| 1767 | } |
| 1768 | |
| 1769 | MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); |
| 1770 | MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); |
| 1771 | bcopy(sa, dupsa, sa->sa_len); |
| 1772 | |
| 1773 | ifma->ifma_addr = dupsa; |
| 1774 | ifma->ifma_lladdr = llsa; |
| 1775 | ifma->ifma_ifp = ifp; |
| 1776 | ifma->ifma_refcount = 1; |
| 1777 | ifma->ifma_protospec = 0; |
| 1778 | rt_newmaddrmsg(RTM_NEWMADDR, ifma); |
| 1779 | |
| 1780 | /* |
| 1781 | * Some network interfaces can scan the address list at |
| 1782 | * interrupt time; lock them out. |
| 1783 | */ |
| 1784 | crit_enter(); |
| 1785 | LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); |
| 1786 | crit_exit(); |
| 1787 | *retifma = ifma; |
| 1788 | |
| 1789 | if (llsa != 0) { |
| 1790 | LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { |
| 1791 | if (sa_equal(ifma->ifma_addr, llsa)) |
| 1792 | break; |
| 1793 | } |
| 1794 | if (ifma) { |
| 1795 | ifma->ifma_refcount++; |
| 1796 | } else { |
| 1797 | MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, |
| 1798 | M_IFMADDR, M_WAITOK); |
| 1799 | MALLOC(dupsa, struct sockaddr *, llsa->sa_len, |
| 1800 | M_IFMADDR, M_WAITOK); |
| 1801 | bcopy(llsa, dupsa, llsa->sa_len); |
| 1802 | ifma->ifma_addr = dupsa; |
| 1803 | ifma->ifma_ifp = ifp; |
| 1804 | ifma->ifma_refcount = 1; |
| 1805 | crit_enter(); |
| 1806 | LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); |
| 1807 | crit_exit(); |
| 1808 | } |
| 1809 | } |
| 1810 | /* |
| 1811 | * We are certain we have added something, so call down to the |
| 1812 | * interface to let them know about it. |
| 1813 | */ |
| 1814 | crit_enter(); |
| 1815 | lwkt_serialize_enter(ifp->if_serializer); |
| 1816 | ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL); |
| 1817 | lwkt_serialize_exit(ifp->if_serializer); |
| 1818 | crit_exit(); |
| 1819 | |
| 1820 | return 0; |
| 1821 | } |
| 1822 | |
| 1823 | /* |
| 1824 | * Remove a reference to a multicast address on this interface. Yell |
| 1825 | * if the request does not match an existing membership. |
| 1826 | */ |
| 1827 | int |
| 1828 | if_delmulti(struct ifnet *ifp, struct sockaddr *sa) |
| 1829 | { |
| 1830 | struct ifmultiaddr *ifma; |
| 1831 | |
| 1832 | LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) |
| 1833 | if (sa_equal(sa, ifma->ifma_addr)) |
| 1834 | break; |
| 1835 | if (ifma == 0) |
| 1836 | return ENOENT; |
| 1837 | |
| 1838 | if (ifma->ifma_refcount > 1) { |
| 1839 | ifma->ifma_refcount--; |
| 1840 | return 0; |
| 1841 | } |
| 1842 | |
| 1843 | rt_newmaddrmsg(RTM_DELMADDR, ifma); |
| 1844 | sa = ifma->ifma_lladdr; |
| 1845 | crit_enter(); |
| 1846 | LIST_REMOVE(ifma, ifma_link); |
| 1847 | /* |
| 1848 | * Make sure the interface driver is notified |
| 1849 | * in the case of a link layer mcast group being left. |
| 1850 | */ |
| 1851 | if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) { |
| 1852 | lwkt_serialize_enter(ifp->if_serializer); |
| 1853 | ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL); |
| 1854 | lwkt_serialize_exit(ifp->if_serializer); |
| 1855 | } |
| 1856 | crit_exit(); |
| 1857 | kfree(ifma->ifma_addr, M_IFMADDR); |
| 1858 | kfree(ifma, M_IFMADDR); |
| 1859 | if (sa == 0) |
| 1860 | return 0; |
| 1861 | |
| 1862 | /* |
| 1863 | * Now look for the link-layer address which corresponds to |
| 1864 | * this network address. It had been squirreled away in |
| 1865 | * ifma->ifma_lladdr for this purpose (so we don't have |
| 1866 | * to call ifp->if_resolvemulti() again), and we saved that |
| 1867 | * value in sa above. If some nasty deleted the |
| 1868 | * link-layer address out from underneath us, we can deal because |
| 1869 | * the address we stored was is not the same as the one which was |
| 1870 | * in the record for the link-layer address. (So we don't complain |
| 1871 | * in that case.) |
| 1872 | */ |
| 1873 | LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) |
| 1874 | if (sa_equal(sa, ifma->ifma_addr)) |
| 1875 | break; |
| 1876 | if (ifma == 0) |
| 1877 | return 0; |
| 1878 | |
| 1879 | if (ifma->ifma_refcount > 1) { |
| 1880 | ifma->ifma_refcount--; |
| 1881 | return 0; |
| 1882 | } |
| 1883 | |
| 1884 | crit_enter(); |
| 1885 | lwkt_serialize_enter(ifp->if_serializer); |
| 1886 | LIST_REMOVE(ifma, ifma_link); |
| 1887 | ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL); |
| 1888 | lwkt_serialize_exit(ifp->if_serializer); |
| 1889 | crit_exit(); |
| 1890 | kfree(ifma->ifma_addr, M_IFMADDR); |
| 1891 | kfree(sa, M_IFMADDR); |
| 1892 | kfree(ifma, M_IFMADDR); |
| 1893 | |
| 1894 | return 0; |
| 1895 | } |
| 1896 | |
| 1897 | /* |
| 1898 | * Set the link layer address on an interface. |
| 1899 | * |
| 1900 | * At this time we only support certain types of interfaces, |
| 1901 | * and we don't allow the length of the address to change. |
| 1902 | */ |
| 1903 | int |
| 1904 | if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) |
| 1905 | { |
| 1906 | struct sockaddr_dl *sdl; |
| 1907 | struct ifreq ifr; |
| 1908 | |
| 1909 | sdl = IF_LLSOCKADDR(ifp); |
| 1910 | if (sdl == NULL) |
| 1911 | return (EINVAL); |
| 1912 | if (len != sdl->sdl_alen) /* don't allow length to change */ |
| 1913 | return (EINVAL); |
| 1914 | switch (ifp->if_type) { |
| 1915 | case IFT_ETHER: /* these types use struct arpcom */ |
| 1916 | case IFT_XETHER: |
| 1917 | case IFT_L2VLAN: |
| 1918 | bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len); |
| 1919 | bcopy(lladdr, LLADDR(sdl), len); |
| 1920 | break; |
| 1921 | default: |
| 1922 | return (ENODEV); |
| 1923 | } |
| 1924 | /* |
| 1925 | * If the interface is already up, we need |
| 1926 | * to re-init it in order to reprogram its |
| 1927 | * address filter. |
| 1928 | */ |
| 1929 | lwkt_serialize_enter(ifp->if_serializer); |
| 1930 | if ((ifp->if_flags & IFF_UP) != 0) { |
| 1931 | struct ifaddr_container *ifac; |
| 1932 | |
| 1933 | ifp->if_flags &= ~IFF_UP; |
| 1934 | ifr.ifr_flags = ifp->if_flags; |
| 1935 | ifr.ifr_flagshigh = ifp->if_flags >> 16; |
| 1936 | ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, |
| 1937 | (struct ucred *)NULL); |
| 1938 | ifp->if_flags |= IFF_UP; |
| 1939 | ifr.ifr_flags = ifp->if_flags; |
| 1940 | ifr.ifr_flagshigh = ifp->if_flags >> 16; |
| 1941 | ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, |
| 1942 | (struct ucred *)NULL); |
| 1943 | #ifdef INET |
| 1944 | /* |
| 1945 | * Also send gratuitous ARPs to notify other nodes about |
| 1946 | * the address change. |
| 1947 | */ |
| 1948 | TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) { |
| 1949 | struct ifaddr *ifa = ifac->ifa; |
| 1950 | |
| 1951 | if (ifa->ifa_addr != NULL && |
| 1952 | ifa->ifa_addr->sa_family == AF_INET) |
| 1953 | arp_ifinit(ifp, ifa); |
| 1954 | } |
| 1955 | #endif |
| 1956 | } |
| 1957 | lwkt_serialize_exit(ifp->if_serializer); |
| 1958 | return (0); |
| 1959 | } |
| 1960 | |
| 1961 | struct ifmultiaddr * |
| 1962 | ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp) |
| 1963 | { |
| 1964 | struct ifmultiaddr *ifma; |
| 1965 | |
| 1966 | LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) |
| 1967 | if (sa_equal(ifma->ifma_addr, sa)) |
| 1968 | break; |
| 1969 | |
| 1970 | return ifma; |
| 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * This function locates the first real ethernet MAC from a network |
| 1975 | * card and loads it into node, returning 0 on success or ENOENT if |
| 1976 | * no suitable interfaces were found. It is used by the uuid code to |
| 1977 | * generate a unique 6-byte number. |
| 1978 | */ |
| 1979 | int |
| 1980 | if_getanyethermac(uint16_t *node, int minlen) |
| 1981 | { |
| 1982 | struct ifnet *ifp; |
| 1983 | struct sockaddr_dl *sdl; |
| 1984 | |
| 1985 | TAILQ_FOREACH(ifp, &ifnet, if_link) { |
| 1986 | if (ifp->if_type != IFT_ETHER) |
| 1987 | continue; |
| 1988 | sdl = IF_LLSOCKADDR(ifp); |
| 1989 | if (sdl->sdl_alen < minlen) |
| 1990 | continue; |
| 1991 | bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node, |
| 1992 | minlen); |
| 1993 | return(0); |
| 1994 | } |
| 1995 | return (ENOENT); |
| 1996 | } |
| 1997 | |
| 1998 | /* |
| 1999 | * The name argument must be a pointer to storage which will last as |
| 2000 | * long as the interface does. For physical devices, the result of |
| 2001 | * device_get_name(dev) is a good choice and for pseudo-devices a |
| 2002 | * static string works well. |
| 2003 | */ |
| 2004 | void |
| 2005 | if_initname(struct ifnet *ifp, const char *name, int unit) |
| 2006 | { |
| 2007 | ifp->if_dname = name; |
| 2008 | ifp->if_dunit = unit; |
| 2009 | if (unit != IF_DUNIT_NONE) |
| 2010 | ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); |
| 2011 | else |
| 2012 | strlcpy(ifp->if_xname, name, IFNAMSIZ); |
| 2013 | } |
| 2014 | |
| 2015 | int |
| 2016 | if_printf(struct ifnet *ifp, const char *fmt, ...) |
| 2017 | { |
| 2018 | __va_list ap; |
| 2019 | int retval; |
| 2020 | |
| 2021 | retval = kprintf("%s: ", ifp->if_xname); |
| 2022 | __va_start(ap, fmt); |
| 2023 | retval += kvprintf(fmt, ap); |
| 2024 | __va_end(ap); |
| 2025 | return (retval); |
| 2026 | } |
| 2027 | |
| 2028 | void |
| 2029 | ifq_set_classic(struct ifaltq *ifq) |
| 2030 | { |
| 2031 | ifq->altq_enqueue = ifq_classic_enqueue; |
| 2032 | ifq->altq_dequeue = ifq_classic_dequeue; |
| 2033 | ifq->altq_request = ifq_classic_request; |
| 2034 | } |
| 2035 | |
| 2036 | int |
| 2037 | ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m, |
| 2038 | struct altq_pktattr *pa __unused) |
| 2039 | { |
| 2040 | logifq(enqueue, ifq); |
| 2041 | if (IF_QFULL(ifq)) { |
| 2042 | m_freem(m); |
| 2043 | return(ENOBUFS); |
| 2044 | } else { |
| 2045 | IF_ENQUEUE(ifq, m); |
| 2046 | return(0); |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | struct mbuf * |
| 2051 | ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op) |
| 2052 | { |
| 2053 | struct mbuf *m; |
| 2054 | |
| 2055 | switch (op) { |
| 2056 | case ALTDQ_POLL: |
| 2057 | IF_POLL(ifq, m); |
| 2058 | break; |
| 2059 | case ALTDQ_REMOVE: |
| 2060 | logifq(dequeue, ifq); |
| 2061 | IF_DEQUEUE(ifq, m); |
| 2062 | break; |
| 2063 | default: |
| 2064 | panic("unsupported ALTQ dequeue op: %d", op); |
| 2065 | } |
| 2066 | KKASSERT(mpolled == NULL || mpolled == m); |
| 2067 | return(m); |
| 2068 | } |
| 2069 | |
| 2070 | int |
| 2071 | ifq_classic_request(struct ifaltq *ifq, int req, void *arg) |
| 2072 | { |
| 2073 | switch (req) { |
| 2074 | case ALTRQ_PURGE: |
| 2075 | IF_DRAIN(ifq); |
| 2076 | break; |
| 2077 | default: |
| 2078 | panic("unsupported ALTQ request: %d", req); |
| 2079 | } |
| 2080 | return(0); |
| 2081 | } |
| 2082 | |
| 2083 | int |
| 2084 | ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa) |
| 2085 | { |
| 2086 | struct ifaltq *ifq = &ifp->if_snd; |
| 2087 | int running = 0, error, start = 0; |
| 2088 | |
| 2089 | ASSERT_NOT_SERIALIZED(ifp->if_serializer); |
| 2090 | |
| 2091 | ALTQ_LOCK(ifq); |
| 2092 | error = ifq_enqueue_locked(ifq, m, pa); |
| 2093 | if (error) { |
| 2094 | ALTQ_UNLOCK(ifq); |
| 2095 | return error; |
| 2096 | } |
| 2097 | if (!ifq->altq_started) { |
| 2098 | /* |
| 2099 | * Hold the interlock of ifnet.if_start |
| 2100 | */ |
| 2101 | ifq->altq_started = 1; |
| 2102 | start = 1; |
| 2103 | } |
| 2104 | ALTQ_UNLOCK(ifq); |
| 2105 | |
| 2106 | ifp->if_obytes += m->m_pkthdr.len; |
| 2107 | if (m->m_flags & M_MCAST) |
| 2108 | ifp->if_omcasts++; |
| 2109 | |
| 2110 | if (!start) { |
| 2111 | logifstart(avoid, ifp); |
| 2112 | return 0; |
| 2113 | } |
| 2114 | |
| 2115 | if (ifq_dispatch_schedonly) { |
| 2116 | /* |
| 2117 | * Always schedule ifnet.if_start on ifnet's CPU, |
| 2118 | * short circuit the rest of this function. |
| 2119 | */ |
| 2120 | logifstart(sched, ifp); |
| 2121 | if_start_schedule(ifp); |
| 2122 | return 0; |
| 2123 | } |
| 2124 | |
| 2125 | /* |
| 2126 | * Try to do direct ifnet.if_start first, if there is |
| 2127 | * contention on ifnet's serializer, ifnet.if_start will |
| 2128 | * be scheduled on ifnet's CPU. |
| 2129 | */ |
| 2130 | if (!lwkt_serialize_try(ifp->if_serializer)) { |
| 2131 | /* |
| 2132 | * ifnet serializer contention happened, |
| 2133 | * ifnet.if_start is scheduled on ifnet's |
| 2134 | * CPU, and we keep going. |
| 2135 | */ |
| 2136 | logifstart(contend_sched, ifp); |
| 2137 | if_start_schedule(ifp); |
| 2138 | return 0; |
| 2139 | } |
| 2140 | |
| 2141 | if ((ifp->if_flags & IFF_OACTIVE) == 0) { |
| 2142 | logifstart(run, ifp); |
| 2143 | ifp->if_start(ifp); |
| 2144 | if ((ifp->if_flags & |
| 2145 | (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING) |
| 2146 | running = 1; |
| 2147 | } |
| 2148 | |
| 2149 | lwkt_serialize_exit(ifp->if_serializer); |
| 2150 | |
| 2151 | if (ifq_dispatch_schednochk || if_start_need_schedule(ifq, running)) { |
| 2152 | /* |
| 2153 | * More data need to be transmitted, ifnet.if_start is |
| 2154 | * scheduled on ifnet's CPU, and we keep going. |
| 2155 | * NOTE: ifnet.if_start interlock is not released. |
| 2156 | */ |
| 2157 | logifstart(sched, ifp); |
| 2158 | if_start_schedule(ifp); |
| 2159 | } |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | void * |
| 2164 | ifa_create(int size, int flags) |
| 2165 | { |
| 2166 | struct ifaddr *ifa; |
| 2167 | int i; |
| 2168 | |
| 2169 | KASSERT(size >= sizeof(*ifa), ("ifaddr size too small\n")); |
| 2170 | |
| 2171 | ifa = kmalloc(size, M_IFADDR, flags | M_ZERO); |
| 2172 | if (ifa == NULL) |
| 2173 | return NULL; |
| 2174 | |
| 2175 | ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container), |
| 2176 | M_IFADDR, M_WAITOK | M_ZERO); |
| 2177 | ifa->ifa_ncnt = ncpus; |
| 2178 | for (i = 0; i < ncpus; ++i) { |
| 2179 | struct ifaddr_container *ifac = &ifa->ifa_containers[i]; |
| 2180 | |
| 2181 | ifac->ifa_magic = IFA_CONTAINER_MAGIC; |
| 2182 | ifac->ifa = ifa; |
| 2183 | ifac->ifa_refcnt = 1; |
| 2184 | } |
| 2185 | #ifdef IFADDR_DEBUG |
| 2186 | kprintf("alloc ifa %p %d\n", ifa, size); |
| 2187 | #endif |
| 2188 | return ifa; |
| 2189 | } |
| 2190 | |
| 2191 | void |
| 2192 | ifac_free(struct ifaddr_container *ifac, int cpu_id) |
| 2193 | { |
| 2194 | struct ifaddr *ifa = ifac->ifa; |
| 2195 | |
| 2196 | KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC); |
| 2197 | KKASSERT(ifac->ifa_refcnt == 0); |
| 2198 | KASSERT(ifac->ifa_listmask == 0, |
| 2199 | ("ifa is still on %#x lists\n", ifac->ifa_listmask)); |
| 2200 | |
| 2201 | ifac->ifa_magic = IFA_CONTAINER_DEAD; |
| 2202 | |
| 2203 | #ifdef IFADDR_DEBUG_VERBOSE |
| 2204 | kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id); |
| 2205 | #endif |
| 2206 | |
| 2207 | KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus, |
| 2208 | ("invalid # of ifac, %d\n", ifa->ifa_ncnt)); |
| 2209 | if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) { |
| 2210 | #ifdef IFADDR_DEBUG |
| 2211 | kprintf("free ifa %p\n", ifa); |
| 2212 | #endif |
| 2213 | kfree(ifa->ifa_containers, M_IFADDR); |
| 2214 | kfree(ifa, M_IFADDR); |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | static void |
| 2219 | ifa_iflink_dispatch(struct netmsg *nmsg) |
| 2220 | { |
| 2221 | struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg; |
| 2222 | struct ifaddr *ifa = msg->ifa; |
| 2223 | struct ifnet *ifp = msg->ifp; |
| 2224 | int cpu = mycpuid; |
| 2225 | struct ifaddr_container *ifac; |
| 2226 | |
| 2227 | crit_enter(); |
| 2228 | |
| 2229 | ifac = &ifa->ifa_containers[cpu]; |
| 2230 | ASSERT_IFAC_VALID(ifac); |
| 2231 | KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0, |
| 2232 | ("ifaddr is on if_addrheads\n")); |
| 2233 | |
| 2234 | ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD; |
| 2235 | if (msg->tail) |
| 2236 | TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link); |
| 2237 | else |
| 2238 | TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link); |
| 2239 | |
| 2240 | crit_exit(); |
| 2241 | |
| 2242 | ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1); |
| 2243 | } |
| 2244 | |
| 2245 | void |
| 2246 | ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail) |
| 2247 | { |
| 2248 | struct netmsg_ifaddr msg; |
| 2249 | |
| 2250 | netmsg_init(&msg.netmsg, &curthread->td_msgport, 0, |
| 2251 | ifa_iflink_dispatch); |
| 2252 | msg.ifa = ifa; |
| 2253 | msg.ifp = ifp; |
| 2254 | msg.tail = tail; |
| 2255 | |
| 2256 | ifa_domsg(&msg.netmsg.nm_lmsg, 0); |
| 2257 | } |
| 2258 | |
| 2259 | static void |
| 2260 | ifa_ifunlink_dispatch(struct netmsg *nmsg) |
| 2261 | { |
| 2262 | struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg; |
| 2263 | struct ifaddr *ifa = msg->ifa; |
| 2264 | struct ifnet *ifp = msg->ifp; |
| 2265 | int cpu = mycpuid; |
| 2266 | struct ifaddr_container *ifac; |
| 2267 | |
| 2268 | crit_enter(); |
| 2269 | |
| 2270 | ifac = &ifa->ifa_containers[cpu]; |
| 2271 | ASSERT_IFAC_VALID(ifac); |
| 2272 | KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD, |
| 2273 | ("ifaddr is not on if_addrhead\n")); |
| 2274 | |
| 2275 | TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link); |
| 2276 | ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD; |
| 2277 | |
| 2278 | crit_exit(); |
| 2279 | |
| 2280 | ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1); |
| 2281 | } |
| 2282 | |
| 2283 | void |
| 2284 | ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp) |
| 2285 | { |
| 2286 | struct netmsg_ifaddr msg; |
| 2287 | |
| 2288 | netmsg_init(&msg.netmsg, &curthread->td_msgport, 0, |
| 2289 | ifa_ifunlink_dispatch); |
| 2290 | msg.ifa = ifa; |
| 2291 | msg.ifp = ifp; |
| 2292 | |
| 2293 | ifa_domsg(&msg.netmsg.nm_lmsg, 0); |
| 2294 | } |
| 2295 | |
| 2296 | static void |
| 2297 | ifa_destroy_dispatch(struct netmsg *nmsg) |
| 2298 | { |
| 2299 | struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg; |
| 2300 | |
| 2301 | IFAFREE(msg->ifa); |
| 2302 | ifa_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1); |
| 2303 | } |
| 2304 | |
| 2305 | void |
| 2306 | ifa_destroy(struct ifaddr *ifa) |
| 2307 | { |
| 2308 | struct netmsg_ifaddr msg; |
| 2309 | |
| 2310 | netmsg_init(&msg.netmsg, &curthread->td_msgport, 0, |
| 2311 | ifa_destroy_dispatch); |
| 2312 | msg.ifa = ifa; |
| 2313 | |
| 2314 | ifa_domsg(&msg.netmsg.nm_lmsg, 0); |
| 2315 | } |
| 2316 | |
| 2317 | struct lwkt_port * |
| 2318 | ifnet_portfn(int cpu) |
| 2319 | { |
| 2320 | return &ifnet_threads[cpu].td_msgport; |
| 2321 | } |
| 2322 | |
| 2323 | void |
| 2324 | ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu) |
| 2325 | { |
| 2326 | KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus); |
| 2327 | |
| 2328 | if (next_cpu < ncpus) |
| 2329 | lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg); |
| 2330 | else |
| 2331 | lwkt_replymsg(lmsg, 0); |
| 2332 | } |
| 2333 | |
| 2334 | void |
| 2335 | ifnet_domsg(struct lwkt_msg *lmsg, int cpu) |
| 2336 | { |
| 2337 | KKASSERT(cpu < ncpus); |
| 2338 | lwkt_domsg(ifnet_portfn(cpu), lmsg, 0); |
| 2339 | } |
| 2340 | |
| 2341 | void |
| 2342 | ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu) |
| 2343 | { |
| 2344 | KKASSERT(cpu < ncpus); |
| 2345 | lwkt_sendmsg(ifnet_portfn(cpu), lmsg); |
| 2346 | } |
| 2347 | |
| 2348 | static void |
| 2349 | ifnetinit(void *dummy __unused) |
| 2350 | { |
| 2351 | int i; |
| 2352 | |
| 2353 | for (i = 0; i < ncpus; ++i) { |
| 2354 | struct thread *thr = &ifnet_threads[i]; |
| 2355 | |
| 2356 | lwkt_create(netmsg_service_loop, &ifnet_mpsafe_thread, NULL, |
| 2357 | thr, TDF_NETWORK | TDF_MPSAFE, i, "ifnet %d", i); |
| 2358 | netmsg_service_port_init(&thr->td_msgport); |
| 2359 | } |
| 2360 | } |