31 */ 32 33#include "opt_compat.h" 34#include "opt_inet6.h" 35#include "opt_inet.h" 36#include "opt_mac.h" 37#include "opt_carp.h" 38 39#include <sys/param.h> 40#include <sys/types.h> 41#include <sys/conf.h> 42#include <sys/malloc.h> 43#include <sys/sbuf.h> 44#include <sys/bus.h> 45#include <sys/mbuf.h> 46#include <sys/systm.h> 47#include <sys/priv.h> 48#include <sys/proc.h> 49#include <sys/socket.h> 50#include <sys/socketvar.h> 51#include <sys/protosw.h> 52#include <sys/kernel.h> 53#include <sys/sockio.h> 54#include <sys/syslog.h> 55#include <sys/sysctl.h> 56#include <sys/taskqueue.h> 57#include <sys/domain.h> 58#include <sys/jail.h> 59#include <sys/vimage.h> 60#include <machine/stdarg.h> 61 62#include <net/if.h> 63#include <net/if_clone.h> 64#include <net/if_dl.h> 65#include <net/if_types.h> 66#include <net/if_var.h> 67#include <net/radix.h> 68#include <net/route.h> 69 70#if defined(INET) || defined(INET6) 71/*XXX*/ 72#include <netinet/in.h> 73#include <netinet/in_var.h> 74#ifdef INET6 75#include <netinet6/in6_var.h> 76#include <netinet6/in6_ifattach.h> 77#endif 78#endif 79#ifdef INET 80#include <netinet/if_ether.h> 81#endif 82#ifdef DEV_CARP 83#include <netinet/ip_carp.h> 84#endif 85 86#include <security/mac/mac_framework.h> 87 88SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 89SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 90 91/* Log link state change events */ 92static int log_link_state_change = 1; 93 94SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 95 &log_link_state_change, 0, 96 "log interface link state change events"); 97 98void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 99void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 100void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 101 102struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 103 104/* 105 * XXX: Style; these should be sorted alphabetically, and unprototyped 106 * static functions should be prototyped. Currently they are sorted by 107 * declaration order. 108 */ 109static void if_attachdomain(void *); 110static void if_attachdomain1(struct ifnet *); 111static int ifconf(u_long, caddr_t); 112static void if_freemulti(struct ifmultiaddr *); 113static void if_grow(void); 114static void if_init(void *); 115static void if_qflush(struct ifaltq *); 116static void if_route(struct ifnet *, int flag, int fam); 117static int if_setflag(struct ifnet *, int, int, int *, int); 118static void if_slowtimo(void *); 119static void if_unroute(struct ifnet *, int flag, int fam); 120static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 121static int if_rtdel(struct radix_node *, void *); 122static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 123static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 124static void if_start_deferred(void *context, int pending); 125static void do_link_state_change(void *, int); 126static int if_getgroup(struct ifgroupreq *, struct ifnet *); 127static int if_getgroupmembers(struct ifgroupreq *); 128#ifdef INET6 129/* 130 * XXX: declare here to avoid to include many inet6 related files.. 131 * should be more generalized? 132 */ 133extern void nd6_setmtu(struct ifnet *); 134#endif 135 136int if_index = 0; 137int ifqmaxlen = IFQ_MAXLEN; 138struct ifnethead ifnet; /* depend on static init XXX */ 139struct ifgrouphead ifg_head; 140struct mtx ifnet_lock; 141static if_com_alloc_t *if_com_alloc[256]; 142static if_com_free_t *if_com_free[256]; 143 144static int if_indexlim = 8; 145static struct knlist ifklist; 146 147/* 148 * Table of ifnet/cdev by index. Locked with ifnet_lock. 149 */ 150static struct ifindex_entry *ifindex_table = NULL; 151 152static void filt_netdetach(struct knote *kn); 153static int filt_netdev(struct knote *kn, long hint); 154 155static struct filterops netdev_filtops = 156 { 1, NULL, filt_netdetach, filt_netdev }; 157 158/* 159 * System initialization 160 */ 161SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL); 162SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_slowtimo, NULL); 163 164MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 165MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 166MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 167 168struct ifnet * 169ifnet_byindex(u_short idx) 170{ 171 struct ifnet *ifp; 172 173 IFNET_RLOCK(); 174 ifp = V_ifindex_table[idx].ife_ifnet; 175 IFNET_RUNLOCK(); 176 return (ifp); 177} 178 179static void 180ifnet_setbyindex(u_short idx, struct ifnet *ifp) 181{ 182 183 IFNET_WLOCK_ASSERT(); 184 185 V_ifindex_table[idx].ife_ifnet = ifp; 186} 187 188struct ifaddr * 189ifaddr_byindex(u_short idx) 190{ 191 struct ifaddr *ifa; 192 193 IFNET_RLOCK(); 194 ifa = ifnet_byindex(idx)->if_addr; 195 IFNET_RUNLOCK(); 196 return (ifa); 197} 198 199struct cdev * 200ifdev_byindex(u_short idx) 201{ 202 struct cdev *cdev; 203 204 IFNET_RLOCK(); 205 cdev = V_ifindex_table[idx].ife_dev; 206 IFNET_RUNLOCK(); 207 return (cdev); 208} 209 210static void 211ifdev_setbyindex(u_short idx, struct cdev *cdev) 212{ 213 214 IFNET_WLOCK(); 215 V_ifindex_table[idx].ife_dev = cdev; 216 IFNET_WUNLOCK(); 217} 218 219static d_open_t netopen; 220static d_close_t netclose; 221static d_ioctl_t netioctl; 222static d_kqfilter_t netkqfilter; 223 224static struct cdevsw net_cdevsw = { 225 .d_version = D_VERSION, 226 .d_flags = D_NEEDGIANT, 227 .d_open = netopen, 228 .d_close = netclose, 229 .d_ioctl = netioctl, 230 .d_name = "net", 231 .d_kqfilter = netkqfilter, 232}; 233 234static int 235netopen(struct cdev *dev, int flag, int mode, struct thread *td) 236{ 237 return (0); 238} 239 240static int 241netclose(struct cdev *dev, int flags, int fmt, struct thread *td) 242{ 243 return (0); 244} 245 246static int 247netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 248{ 249 struct ifnet *ifp; 250 int error, idx; 251 252 /* only support interface specific ioctls */ 253 if (IOCGROUP(cmd) != 'i') 254 return (EOPNOTSUPP); 255 idx = minor(dev); 256 if (idx == 0) { 257 /* 258 * special network device, not interface. 259 */ 260 if (cmd == SIOCGIFCONF) 261 return (ifconf(cmd, data)); /* XXX remove cmd */ 262#ifdef __amd64__ 263 if (cmd == SIOCGIFCONF32) 264 return (ifconf(cmd, data)); /* XXX remove cmd */ 265#endif 266 return (EOPNOTSUPP); 267 } 268 269 ifp = ifnet_byindex(idx); 270 if (ifp == NULL) 271 return (ENXIO); 272 273 error = ifhwioctl(cmd, ifp, data, td); 274 if (error == ENOIOCTL) 275 error = EOPNOTSUPP; 276 return (error); 277} 278 279static int 280netkqfilter(struct cdev *dev, struct knote *kn) 281{ 282 struct knlist *klist; 283 struct ifnet *ifp; 284 int idx; 285 286 switch (kn->kn_filter) { 287 case EVFILT_NETDEV: 288 kn->kn_fop = &netdev_filtops; 289 break; 290 default: 291 return (EINVAL); 292 } 293 294 idx = minor(dev); 295 if (idx == 0) { 296 klist = &V_ifklist; 297 } else { 298 ifp = ifnet_byindex(idx); 299 if (ifp == NULL) 300 return (1); 301 klist = &ifp->if_klist; 302 } 303 304 kn->kn_hook = (caddr_t)klist; 305 306 knlist_add(klist, kn, 0); 307 308 return (0); 309} 310 311static void 312filt_netdetach(struct knote *kn) 313{ 314 struct knlist *klist = (struct knlist *)kn->kn_hook; 315 316 knlist_remove(klist, kn, 0); 317} 318 319static int 320filt_netdev(struct knote *kn, long hint) 321{ 322 struct knlist *klist = (struct knlist *)kn->kn_hook; 323 324 /* 325 * Currently NOTE_EXIT is abused to indicate device detach. 326 */ 327 if (hint == NOTE_EXIT) { 328 kn->kn_data = NOTE_LINKINV; 329 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 330 knlist_remove_inevent(klist, kn); 331 return (1); 332 } 333 if (hint != 0) 334 kn->kn_data = hint; /* current status */ 335 if (kn->kn_sfflags & hint) 336 kn->kn_fflags |= hint; 337 return (kn->kn_fflags != 0); 338} 339 340/* 341 * Network interface utility routines. 342 * 343 * Routines with ifa_ifwith* names take sockaddr *'s as 344 * parameters. 345 */ 346 347/* ARGSUSED*/ 348static void 349if_init(void *dummy __unused) 350{ 351 352 IFNET_LOCK_INIT(); 353 TAILQ_INIT(&V_ifnet); 354 TAILQ_INIT(&V_ifg_head); 355 knlist_init(&V_ifklist, NULL, NULL, NULL, NULL); 356 if_grow(); /* create initial table */ 357 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL, 358 0600, "network")); 359 if_clone_init(); 360} 361 362static void 363if_grow(void) 364{ 365 u_int n; 366 struct ifindex_entry *e; 367 368 V_if_indexlim <<= 1; 369 n = V_if_indexlim * sizeof(*e); 370 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 371 if (V_ifindex_table != NULL) { 372 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 373 free((caddr_t)V_ifindex_table, M_IFNET); 374 } 375 V_ifindex_table = e; 376} 377 378/* 379 * Allocate a struct ifnet and an index for an interface. A layer 2 380 * common structure will also be allocated if an allocation routine is 381 * registered for the passed type. 382 */ 383struct ifnet* 384if_alloc(u_char type) 385{ 386 struct ifnet *ifp; 387 388 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 389 390 /* 391 * Try to find an empty slot below if_index. If we fail, take 392 * the next slot. 393 * 394 * XXX: should be locked! 395 */ 396 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) { 397 if (ifnet_byindex(ifp->if_index) == NULL) 398 break; 399 } 400 /* Catch if_index overflow. */ 401 if (ifp->if_index < 1) { 402 free(ifp, M_IFNET); 403 return (NULL); 404 } 405 if (ifp->if_index > V_if_index) 406 V_if_index = ifp->if_index; 407 if (V_if_index >= V_if_indexlim) 408 if_grow(); 409 410 ifp->if_type = type; 411 412 if (if_com_alloc[type] != NULL) { 413 ifp->if_l2com = if_com_alloc[type](type, ifp); 414 if (ifp->if_l2com == NULL) { 415 free(ifp, M_IFNET); 416 return (NULL); 417 } 418 } 419 IFNET_WLOCK(); 420 ifnet_setbyindex(ifp->if_index, ifp); 421 IFNET_WUNLOCK(); 422 IF_ADDR_LOCK_INIT(ifp); 423 424 return (ifp); 425} 426 427/* 428 * Free the struct ifnet, the associated index, and the layer 2 common 429 * structure if needed. All the work is done in if_free_type(). 430 * 431 * Do not add code to this function! Add it to if_free_type(). 432 */ 433void 434if_free(struct ifnet *ifp) 435{ 436 437 if_free_type(ifp, ifp->if_type); 438} 439 440/* 441 * Do the actual work of freeing a struct ifnet, associated index, and 442 * layer 2 common structure. This version should only be called by 443 * intefaces that switch their type after calling if_alloc(). 444 */ 445void 446if_free_type(struct ifnet *ifp, u_char type) 447{ 448 449 if (ifp != ifnet_byindex(ifp->if_index)) { 450 if_printf(ifp, "%s: value was not if_alloced, skipping\n", 451 __func__); 452 return; 453 } 454 455 IFNET_WLOCK(); 456 ifnet_setbyindex(ifp->if_index, NULL); 457 458 /* XXX: should be locked with if_findindex() */ 459 while (V_if_index > 0 && ifnet_byindex(V_if_index) == NULL) 460 V_if_index--; 461 IFNET_WUNLOCK(); 462 463 if (if_com_free[type] != NULL) 464 if_com_free[type](ifp->if_l2com, type); 465 466 IF_ADDR_LOCK_DESTROY(ifp); 467 free(ifp, M_IFNET); 468}; 469 470/* 471 * Perform generic interface initalization tasks and attach the interface 472 * to the list of "active" interfaces. 473 * 474 * XXX: 475 * - The decision to return void and thus require this function to 476 * succeed is questionable. 477 * - We do more initialization here then is probably a good idea. 478 * Some of this should probably move to if_alloc(). 479 * - We should probably do more sanity checking. For instance we don't 480 * do anything to insure if_xname is unique or non-empty. 481 */ 482void 483if_attach(struct ifnet *ifp) 484{ 485 unsigned socksize, ifasize; 486 int namelen, masklen; 487 struct sockaddr_dl *sdl; 488 struct ifaddr *ifa; 489 490 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 491 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 492 ifp->if_xname); 493 494 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp); 495 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 496 IF_AFDATA_LOCK_INIT(ifp); 497 ifp->if_afdata_initialized = 0; 498 499 TAILQ_INIT(&ifp->if_addrhead); 500 TAILQ_INIT(&ifp->if_prefixhead); 501 TAILQ_INIT(&ifp->if_multiaddrs); 502 TAILQ_INIT(&ifp->if_groups); 503 504 if_addgroup(ifp, IFG_ALL); 505 506 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL); 507 getmicrotime(&ifp->if_lastchange); 508 ifp->if_data.ifi_epoch = time_uptime; 509 ifp->if_data.ifi_datalen = sizeof(struct if_data); 510 511#ifdef MAC 512 mac_ifnet_init(ifp); 513 mac_ifnet_create(ifp); 514#endif 515 516 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw,
| 31 */ 32 33#include "opt_compat.h" 34#include "opt_inet6.h" 35#include "opt_inet.h" 36#include "opt_mac.h" 37#include "opt_carp.h" 38 39#include <sys/param.h> 40#include <sys/types.h> 41#include <sys/conf.h> 42#include <sys/malloc.h> 43#include <sys/sbuf.h> 44#include <sys/bus.h> 45#include <sys/mbuf.h> 46#include <sys/systm.h> 47#include <sys/priv.h> 48#include <sys/proc.h> 49#include <sys/socket.h> 50#include <sys/socketvar.h> 51#include <sys/protosw.h> 52#include <sys/kernel.h> 53#include <sys/sockio.h> 54#include <sys/syslog.h> 55#include <sys/sysctl.h> 56#include <sys/taskqueue.h> 57#include <sys/domain.h> 58#include <sys/jail.h> 59#include <sys/vimage.h> 60#include <machine/stdarg.h> 61 62#include <net/if.h> 63#include <net/if_clone.h> 64#include <net/if_dl.h> 65#include <net/if_types.h> 66#include <net/if_var.h> 67#include <net/radix.h> 68#include <net/route.h> 69 70#if defined(INET) || defined(INET6) 71/*XXX*/ 72#include <netinet/in.h> 73#include <netinet/in_var.h> 74#ifdef INET6 75#include <netinet6/in6_var.h> 76#include <netinet6/in6_ifattach.h> 77#endif 78#endif 79#ifdef INET 80#include <netinet/if_ether.h> 81#endif 82#ifdef DEV_CARP 83#include <netinet/ip_carp.h> 84#endif 85 86#include <security/mac/mac_framework.h> 87 88SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 89SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 90 91/* Log link state change events */ 92static int log_link_state_change = 1; 93 94SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 95 &log_link_state_change, 0, 96 "log interface link state change events"); 97 98void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 99void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 100void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 101 102struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 103 104/* 105 * XXX: Style; these should be sorted alphabetically, and unprototyped 106 * static functions should be prototyped. Currently they are sorted by 107 * declaration order. 108 */ 109static void if_attachdomain(void *); 110static void if_attachdomain1(struct ifnet *); 111static int ifconf(u_long, caddr_t); 112static void if_freemulti(struct ifmultiaddr *); 113static void if_grow(void); 114static void if_init(void *); 115static void if_qflush(struct ifaltq *); 116static void if_route(struct ifnet *, int flag, int fam); 117static int if_setflag(struct ifnet *, int, int, int *, int); 118static void if_slowtimo(void *); 119static void if_unroute(struct ifnet *, int flag, int fam); 120static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 121static int if_rtdel(struct radix_node *, void *); 122static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 123static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 124static void if_start_deferred(void *context, int pending); 125static void do_link_state_change(void *, int); 126static int if_getgroup(struct ifgroupreq *, struct ifnet *); 127static int if_getgroupmembers(struct ifgroupreq *); 128#ifdef INET6 129/* 130 * XXX: declare here to avoid to include many inet6 related files.. 131 * should be more generalized? 132 */ 133extern void nd6_setmtu(struct ifnet *); 134#endif 135 136int if_index = 0; 137int ifqmaxlen = IFQ_MAXLEN; 138struct ifnethead ifnet; /* depend on static init XXX */ 139struct ifgrouphead ifg_head; 140struct mtx ifnet_lock; 141static if_com_alloc_t *if_com_alloc[256]; 142static if_com_free_t *if_com_free[256]; 143 144static int if_indexlim = 8; 145static struct knlist ifklist; 146 147/* 148 * Table of ifnet/cdev by index. Locked with ifnet_lock. 149 */ 150static struct ifindex_entry *ifindex_table = NULL; 151 152static void filt_netdetach(struct knote *kn); 153static int filt_netdev(struct knote *kn, long hint); 154 155static struct filterops netdev_filtops = 156 { 1, NULL, filt_netdetach, filt_netdev }; 157 158/* 159 * System initialization 160 */ 161SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL); 162SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_slowtimo, NULL); 163 164MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 165MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 166MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 167 168struct ifnet * 169ifnet_byindex(u_short idx) 170{ 171 struct ifnet *ifp; 172 173 IFNET_RLOCK(); 174 ifp = V_ifindex_table[idx].ife_ifnet; 175 IFNET_RUNLOCK(); 176 return (ifp); 177} 178 179static void 180ifnet_setbyindex(u_short idx, struct ifnet *ifp) 181{ 182 183 IFNET_WLOCK_ASSERT(); 184 185 V_ifindex_table[idx].ife_ifnet = ifp; 186} 187 188struct ifaddr * 189ifaddr_byindex(u_short idx) 190{ 191 struct ifaddr *ifa; 192 193 IFNET_RLOCK(); 194 ifa = ifnet_byindex(idx)->if_addr; 195 IFNET_RUNLOCK(); 196 return (ifa); 197} 198 199struct cdev * 200ifdev_byindex(u_short idx) 201{ 202 struct cdev *cdev; 203 204 IFNET_RLOCK(); 205 cdev = V_ifindex_table[idx].ife_dev; 206 IFNET_RUNLOCK(); 207 return (cdev); 208} 209 210static void 211ifdev_setbyindex(u_short idx, struct cdev *cdev) 212{ 213 214 IFNET_WLOCK(); 215 V_ifindex_table[idx].ife_dev = cdev; 216 IFNET_WUNLOCK(); 217} 218 219static d_open_t netopen; 220static d_close_t netclose; 221static d_ioctl_t netioctl; 222static d_kqfilter_t netkqfilter; 223 224static struct cdevsw net_cdevsw = { 225 .d_version = D_VERSION, 226 .d_flags = D_NEEDGIANT, 227 .d_open = netopen, 228 .d_close = netclose, 229 .d_ioctl = netioctl, 230 .d_name = "net", 231 .d_kqfilter = netkqfilter, 232}; 233 234static int 235netopen(struct cdev *dev, int flag, int mode, struct thread *td) 236{ 237 return (0); 238} 239 240static int 241netclose(struct cdev *dev, int flags, int fmt, struct thread *td) 242{ 243 return (0); 244} 245 246static int 247netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 248{ 249 struct ifnet *ifp; 250 int error, idx; 251 252 /* only support interface specific ioctls */ 253 if (IOCGROUP(cmd) != 'i') 254 return (EOPNOTSUPP); 255 idx = minor(dev); 256 if (idx == 0) { 257 /* 258 * special network device, not interface. 259 */ 260 if (cmd == SIOCGIFCONF) 261 return (ifconf(cmd, data)); /* XXX remove cmd */ 262#ifdef __amd64__ 263 if (cmd == SIOCGIFCONF32) 264 return (ifconf(cmd, data)); /* XXX remove cmd */ 265#endif 266 return (EOPNOTSUPP); 267 } 268 269 ifp = ifnet_byindex(idx); 270 if (ifp == NULL) 271 return (ENXIO); 272 273 error = ifhwioctl(cmd, ifp, data, td); 274 if (error == ENOIOCTL) 275 error = EOPNOTSUPP; 276 return (error); 277} 278 279static int 280netkqfilter(struct cdev *dev, struct knote *kn) 281{ 282 struct knlist *klist; 283 struct ifnet *ifp; 284 int idx; 285 286 switch (kn->kn_filter) { 287 case EVFILT_NETDEV: 288 kn->kn_fop = &netdev_filtops; 289 break; 290 default: 291 return (EINVAL); 292 } 293 294 idx = minor(dev); 295 if (idx == 0) { 296 klist = &V_ifklist; 297 } else { 298 ifp = ifnet_byindex(idx); 299 if (ifp == NULL) 300 return (1); 301 klist = &ifp->if_klist; 302 } 303 304 kn->kn_hook = (caddr_t)klist; 305 306 knlist_add(klist, kn, 0); 307 308 return (0); 309} 310 311static void 312filt_netdetach(struct knote *kn) 313{ 314 struct knlist *klist = (struct knlist *)kn->kn_hook; 315 316 knlist_remove(klist, kn, 0); 317} 318 319static int 320filt_netdev(struct knote *kn, long hint) 321{ 322 struct knlist *klist = (struct knlist *)kn->kn_hook; 323 324 /* 325 * Currently NOTE_EXIT is abused to indicate device detach. 326 */ 327 if (hint == NOTE_EXIT) { 328 kn->kn_data = NOTE_LINKINV; 329 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 330 knlist_remove_inevent(klist, kn); 331 return (1); 332 } 333 if (hint != 0) 334 kn->kn_data = hint; /* current status */ 335 if (kn->kn_sfflags & hint) 336 kn->kn_fflags |= hint; 337 return (kn->kn_fflags != 0); 338} 339 340/* 341 * Network interface utility routines. 342 * 343 * Routines with ifa_ifwith* names take sockaddr *'s as 344 * parameters. 345 */ 346 347/* ARGSUSED*/ 348static void 349if_init(void *dummy __unused) 350{ 351 352 IFNET_LOCK_INIT(); 353 TAILQ_INIT(&V_ifnet); 354 TAILQ_INIT(&V_ifg_head); 355 knlist_init(&V_ifklist, NULL, NULL, NULL, NULL); 356 if_grow(); /* create initial table */ 357 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL, 358 0600, "network")); 359 if_clone_init(); 360} 361 362static void 363if_grow(void) 364{ 365 u_int n; 366 struct ifindex_entry *e; 367 368 V_if_indexlim <<= 1; 369 n = V_if_indexlim * sizeof(*e); 370 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 371 if (V_ifindex_table != NULL) { 372 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 373 free((caddr_t)V_ifindex_table, M_IFNET); 374 } 375 V_ifindex_table = e; 376} 377 378/* 379 * Allocate a struct ifnet and an index for an interface. A layer 2 380 * common structure will also be allocated if an allocation routine is 381 * registered for the passed type. 382 */ 383struct ifnet* 384if_alloc(u_char type) 385{ 386 struct ifnet *ifp; 387 388 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 389 390 /* 391 * Try to find an empty slot below if_index. If we fail, take 392 * the next slot. 393 * 394 * XXX: should be locked! 395 */ 396 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) { 397 if (ifnet_byindex(ifp->if_index) == NULL) 398 break; 399 } 400 /* Catch if_index overflow. */ 401 if (ifp->if_index < 1) { 402 free(ifp, M_IFNET); 403 return (NULL); 404 } 405 if (ifp->if_index > V_if_index) 406 V_if_index = ifp->if_index; 407 if (V_if_index >= V_if_indexlim) 408 if_grow(); 409 410 ifp->if_type = type; 411 412 if (if_com_alloc[type] != NULL) { 413 ifp->if_l2com = if_com_alloc[type](type, ifp); 414 if (ifp->if_l2com == NULL) { 415 free(ifp, M_IFNET); 416 return (NULL); 417 } 418 } 419 IFNET_WLOCK(); 420 ifnet_setbyindex(ifp->if_index, ifp); 421 IFNET_WUNLOCK(); 422 IF_ADDR_LOCK_INIT(ifp); 423 424 return (ifp); 425} 426 427/* 428 * Free the struct ifnet, the associated index, and the layer 2 common 429 * structure if needed. All the work is done in if_free_type(). 430 * 431 * Do not add code to this function! Add it to if_free_type(). 432 */ 433void 434if_free(struct ifnet *ifp) 435{ 436 437 if_free_type(ifp, ifp->if_type); 438} 439 440/* 441 * Do the actual work of freeing a struct ifnet, associated index, and 442 * layer 2 common structure. This version should only be called by 443 * intefaces that switch their type after calling if_alloc(). 444 */ 445void 446if_free_type(struct ifnet *ifp, u_char type) 447{ 448 449 if (ifp != ifnet_byindex(ifp->if_index)) { 450 if_printf(ifp, "%s: value was not if_alloced, skipping\n", 451 __func__); 452 return; 453 } 454 455 IFNET_WLOCK(); 456 ifnet_setbyindex(ifp->if_index, NULL); 457 458 /* XXX: should be locked with if_findindex() */ 459 while (V_if_index > 0 && ifnet_byindex(V_if_index) == NULL) 460 V_if_index--; 461 IFNET_WUNLOCK(); 462 463 if (if_com_free[type] != NULL) 464 if_com_free[type](ifp->if_l2com, type); 465 466 IF_ADDR_LOCK_DESTROY(ifp); 467 free(ifp, M_IFNET); 468}; 469 470/* 471 * Perform generic interface initalization tasks and attach the interface 472 * to the list of "active" interfaces. 473 * 474 * XXX: 475 * - The decision to return void and thus require this function to 476 * succeed is questionable. 477 * - We do more initialization here then is probably a good idea. 478 * Some of this should probably move to if_alloc(). 479 * - We should probably do more sanity checking. For instance we don't 480 * do anything to insure if_xname is unique or non-empty. 481 */ 482void 483if_attach(struct ifnet *ifp) 484{ 485 unsigned socksize, ifasize; 486 int namelen, masklen; 487 struct sockaddr_dl *sdl; 488 struct ifaddr *ifa; 489 490 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 491 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 492 ifp->if_xname); 493 494 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp); 495 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 496 IF_AFDATA_LOCK_INIT(ifp); 497 ifp->if_afdata_initialized = 0; 498 499 TAILQ_INIT(&ifp->if_addrhead); 500 TAILQ_INIT(&ifp->if_prefixhead); 501 TAILQ_INIT(&ifp->if_multiaddrs); 502 TAILQ_INIT(&ifp->if_groups); 503 504 if_addgroup(ifp, IFG_ALL); 505 506 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL); 507 getmicrotime(&ifp->if_lastchange); 508 ifp->if_data.ifi_epoch = time_uptime; 509 ifp->if_data.ifi_datalen = sizeof(struct if_data); 510 511#ifdef MAC 512 mac_ifnet_init(ifp); 513 mac_ifnet_create(ifp); 514#endif 515 516 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw,
|
518 net_cdevsw.d_name, ifp->if_xname)); 519 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", 520 net_cdevsw.d_name, ifp->if_index); 521 522 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 523 524 /* 525 * create a Link Level name for this device 526 */ 527 namelen = strlen(ifp->if_xname); 528 /* 529 * Always save enough space for any possiable name so we can do 530 * a rename in place later. 531 */ 532 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 533 socksize = masklen + ifp->if_addrlen; 534 if (socksize < sizeof(*sdl)) 535 socksize = sizeof(*sdl); 536 socksize = roundup2(socksize, sizeof(long)); 537 ifasize = sizeof(*ifa) + 2 * socksize; 538 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 539 IFA_LOCK_INIT(ifa); 540 sdl = (struct sockaddr_dl *)(ifa + 1); 541 sdl->sdl_len = socksize; 542 sdl->sdl_family = AF_LINK; 543 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 544 sdl->sdl_nlen = namelen; 545 sdl->sdl_index = ifp->if_index; 546 sdl->sdl_type = ifp->if_type; 547 ifp->if_addr = ifa; 548 ifa->ifa_ifp = ifp; 549 ifa->ifa_rtrequest = link_rtrequest; 550 ifa->ifa_addr = (struct sockaddr *)sdl; 551 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 552 ifa->ifa_netmask = (struct sockaddr *)sdl; 553 sdl->sdl_len = masklen; 554 while (namelen != 0) 555 sdl->sdl_data[--namelen] = 0xff; 556 ifa->ifa_refcnt = 1; 557 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 558 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */ 559 560 /* 561 * XXX: why do we warn about this? We're correcting it and most 562 * drivers just set the value the way we do. 563 */ 564 if (ifp->if_snd.ifq_maxlen == 0) { 565 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n"); 566 ifp->if_snd.ifq_maxlen = ifqmaxlen; 567 } 568 ifp->if_snd.altq_type = 0; 569 ifp->if_snd.altq_disc = NULL; 570 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 571 ifp->if_snd.altq_tbr = NULL; 572 ifp->if_snd.altq_ifp = ifp; 573 574 IFNET_WLOCK(); 575 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 576 IFNET_WUNLOCK(); 577 578 if (domain_init_status >= 2) 579 if_attachdomain1(ifp); 580 581 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 582 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 583 584 /* Announce the interface. */ 585 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 586 587 if (ifp->if_watchdog != NULL) 588 if_printf(ifp, 589 "WARNING: using obsoleted if_watchdog interface\n"); 590 if (ifp->if_flags & IFF_NEEDSGIANT) 591 if_printf(ifp, 592 "WARNING: using obsoleted IFF_NEEDSGIANT flag\n"); 593} 594 595static void 596if_attachdomain(void *dummy) 597{ 598 struct ifnet *ifp; 599 int s; 600 601 s = splnet(); 602 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 603 if_attachdomain1(ifp); 604 splx(s); 605} 606SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 607 if_attachdomain, NULL); 608 609static void 610if_attachdomain1(struct ifnet *ifp) 611{ 612 struct domain *dp; 613 int s; 614 615 s = splnet(); 616 617 /* 618 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 619 * cannot lock ifp->if_afdata initialization, entirely. 620 */ 621 if (IF_AFDATA_TRYLOCK(ifp) == 0) { 622 splx(s); 623 return; 624 } 625 if (ifp->if_afdata_initialized >= domain_init_status) { 626 IF_AFDATA_UNLOCK(ifp); 627 splx(s); 628 printf("if_attachdomain called more than once on %s\n", 629 ifp->if_xname); 630 return; 631 } 632 ifp->if_afdata_initialized = domain_init_status; 633 IF_AFDATA_UNLOCK(ifp); 634 635 /* address family dependent data region */ 636 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 637 for (dp = domains; dp; dp = dp->dom_next) { 638 if (dp->dom_ifattach) 639 ifp->if_afdata[dp->dom_family] = 640 (*dp->dom_ifattach)(ifp); 641 } 642 643 splx(s); 644} 645 646/* 647 * Remove any unicast or broadcast network addresses from an interface. 648 */ 649void 650if_purgeaddrs(struct ifnet *ifp) 651{ 652 struct ifaddr *ifa, *next; 653 654 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 655 if (ifa->ifa_addr->sa_family == AF_LINK) 656 continue; 657#ifdef INET 658 /* XXX: Ugly!! ad hoc just for INET */ 659 if (ifa->ifa_addr->sa_family == AF_INET) { 660 struct ifaliasreq ifr; 661 662 bzero(&ifr, sizeof(ifr)); 663 ifr.ifra_addr = *ifa->ifa_addr; 664 if (ifa->ifa_dstaddr) 665 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 666 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 667 NULL) == 0) 668 continue; 669 } 670#endif /* INET */ 671#ifdef INET6 672 if (ifa->ifa_addr->sa_family == AF_INET6) { 673 in6_purgeaddr(ifa); 674 /* ifp_addrhead is already updated */ 675 continue; 676 } 677#endif /* INET6 */ 678 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 679 IFAFREE(ifa); 680 } 681} 682 683/* 684 * Remove any multicast network addresses from an interface. 685 */ 686void 687if_purgemaddrs(struct ifnet *ifp) 688{ 689 struct ifmultiaddr *ifma; 690 struct ifmultiaddr *next; 691 692 IF_ADDR_LOCK(ifp); 693 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 694 if_delmulti_locked(ifp, ifma, 1); 695 IF_ADDR_UNLOCK(ifp); 696} 697 698/* 699 * Detach an interface, removing it from the 700 * list of "active" interfaces. 701 * 702 * XXXRW: There are some significant questions about event ordering, and 703 * how to prevent things from starting to use the interface during detach. 704 */ 705void 706if_detach(struct ifnet *ifp) 707{ 708 struct ifaddr *ifa; 709 struct radix_node_head *rnh; 710 int s; 711 int i; 712 struct domain *dp; 713 struct ifnet *iter; 714 int found = 0; 715 716 IFNET_WLOCK(); 717 TAILQ_FOREACH(iter, &V_ifnet, if_link) 718 if (iter == ifp) { 719 TAILQ_REMOVE(&V_ifnet, ifp, if_link); 720 found = 1; 721 break; 722 } 723 IFNET_WUNLOCK(); 724 if (!found) 725 return; 726 727 /* 728 * Remove/wait for pending events. 729 */ 730 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 731 732 /* 733 * Remove routes and flush queues. 734 */ 735 s = splnet(); 736 if_down(ifp); 737#ifdef ALTQ 738 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 739 altq_disable(&ifp->if_snd); 740 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 741 altq_detach(&ifp->if_snd); 742#endif 743 744 if_purgeaddrs(ifp); 745 746#ifdef INET 747 in_ifdetach(ifp); 748#endif 749 750#ifdef INET6 751 /* 752 * Remove all IPv6 kernel structs related to ifp. This should be done 753 * before removing routing entries below, since IPv6 interface direct 754 * routes are expected to be removed by the IPv6-specific kernel API. 755 * Otherwise, the kernel will detect some inconsistency and bark it. 756 */ 757 in6_ifdetach(ifp); 758#endif 759 if_purgemaddrs(ifp); 760 761 /* 762 * Remove link ifaddr pointer and maybe decrement if_index. 763 * Clean up all addresses. 764 */ 765 ifp->if_addr = NULL; 766 destroy_dev(ifdev_byindex(ifp->if_index)); 767 ifdev_setbyindex(ifp->if_index, NULL); 768 769 /* We can now free link ifaddr. */ 770 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 771 ifa = TAILQ_FIRST(&ifp->if_addrhead); 772 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 773 IFAFREE(ifa); 774 } 775 776 /* 777 * Delete all remaining routes using this interface 778 * Unfortuneatly the only way to do this is to slog through 779 * the entire routing table looking for routes which point 780 * to this interface...oh well... 781 */ 782 for (i = 1; i <= AF_MAX; i++) { 783 int j; 784 for (j = 0; j < rt_numfibs; j++) { 785 if ((rnh = V_rt_tables[j][i]) == NULL) 786 continue; 787 RADIX_NODE_HEAD_LOCK(rnh); 788 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 789 RADIX_NODE_HEAD_UNLOCK(rnh); 790 } 791 } 792 793 /* Announce that the interface is gone. */ 794 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 795 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 796 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 797 798 IF_AFDATA_LOCK(ifp); 799 for (dp = domains; dp; dp = dp->dom_next) { 800 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 801 (*dp->dom_ifdetach)(ifp, 802 ifp->if_afdata[dp->dom_family]); 803 } 804 IF_AFDATA_UNLOCK(ifp); 805 806#ifdef MAC 807 mac_ifnet_destroy(ifp); 808#endif /* MAC */ 809 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT); 810 knlist_clear(&ifp->if_klist, 0); 811 knlist_destroy(&ifp->if_klist); 812 mtx_destroy(&ifp->if_snd.ifq_mtx); 813 IF_AFDATA_DESTROY(ifp); 814 splx(s); 815} 816 817/* 818 * Add a group to an interface 819 */ 820int 821if_addgroup(struct ifnet *ifp, const char *groupname) 822{ 823 struct ifg_list *ifgl; 824 struct ifg_group *ifg = NULL; 825 struct ifg_member *ifgm; 826 827 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 828 groupname[strlen(groupname) - 1] <= '9') 829 return (EINVAL); 830 831 IFNET_WLOCK(); 832 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 833 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 834 IFNET_WUNLOCK(); 835 return (EEXIST); 836 } 837 838 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 839 M_NOWAIT)) == NULL) { 840 IFNET_WUNLOCK(); 841 return (ENOMEM); 842 } 843 844 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 845 M_TEMP, M_NOWAIT)) == NULL) { 846 free(ifgl, M_TEMP); 847 IFNET_WUNLOCK(); 848 return (ENOMEM); 849 } 850 851 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 852 if (!strcmp(ifg->ifg_group, groupname)) 853 break; 854 855 if (ifg == NULL) { 856 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 857 M_TEMP, M_NOWAIT)) == NULL) { 858 free(ifgl, M_TEMP); 859 free(ifgm, M_TEMP); 860 IFNET_WUNLOCK(); 861 return (ENOMEM); 862 } 863 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 864 ifg->ifg_refcnt = 0; 865 TAILQ_INIT(&ifg->ifg_members); 866 EVENTHANDLER_INVOKE(group_attach_event, ifg); 867 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 868 } 869 870 ifg->ifg_refcnt++; 871 ifgl->ifgl_group = ifg; 872 ifgm->ifgm_ifp = ifp; 873 874 IF_ADDR_LOCK(ifp); 875 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 876 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 877 IF_ADDR_UNLOCK(ifp); 878 879 IFNET_WUNLOCK(); 880 881 EVENTHANDLER_INVOKE(group_change_event, groupname); 882 883 return (0); 884} 885 886/* 887 * Remove a group from an interface 888 */ 889int 890if_delgroup(struct ifnet *ifp, const char *groupname) 891{ 892 struct ifg_list *ifgl; 893 struct ifg_member *ifgm; 894 895 IFNET_WLOCK(); 896 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 897 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 898 break; 899 if (ifgl == NULL) { 900 IFNET_WUNLOCK(); 901 return (ENOENT); 902 } 903 904 IF_ADDR_LOCK(ifp); 905 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 906 IF_ADDR_UNLOCK(ifp); 907 908 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 909 if (ifgm->ifgm_ifp == ifp) 910 break; 911 912 if (ifgm != NULL) { 913 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 914 free(ifgm, M_TEMP); 915 } 916 917 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 918 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 919 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 920 free(ifgl->ifgl_group, M_TEMP); 921 } 922 IFNET_WUNLOCK(); 923 924 free(ifgl, M_TEMP); 925 926 EVENTHANDLER_INVOKE(group_change_event, groupname); 927 928 return (0); 929} 930 931/* 932 * Stores all groups from an interface in memory pointed 933 * to by data 934 */ 935static int 936if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 937{ 938 int len, error; 939 struct ifg_list *ifgl; 940 struct ifg_req ifgrq, *ifgp; 941 struct ifgroupreq *ifgr = data; 942 943 if (ifgr->ifgr_len == 0) { 944 IF_ADDR_LOCK(ifp); 945 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 946 ifgr->ifgr_len += sizeof(struct ifg_req); 947 IF_ADDR_UNLOCK(ifp); 948 return (0); 949 } 950 951 len = ifgr->ifgr_len; 952 ifgp = ifgr->ifgr_groups; 953 /* XXX: wire */ 954 IF_ADDR_LOCK(ifp); 955 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 956 if (len < sizeof(ifgrq)) { 957 IF_ADDR_UNLOCK(ifp); 958 return (EINVAL); 959 } 960 bzero(&ifgrq, sizeof ifgrq); 961 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 962 sizeof(ifgrq.ifgrq_group)); 963 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 964 IF_ADDR_UNLOCK(ifp); 965 return (error); 966 } 967 len -= sizeof(ifgrq); 968 ifgp++; 969 } 970 IF_ADDR_UNLOCK(ifp); 971 972 return (0); 973} 974 975/* 976 * Stores all members of a group in memory pointed to by data 977 */ 978static int 979if_getgroupmembers(struct ifgroupreq *data) 980{ 981 struct ifgroupreq *ifgr = data; 982 struct ifg_group *ifg; 983 struct ifg_member *ifgm; 984 struct ifg_req ifgrq, *ifgp; 985 int len, error; 986 987 IFNET_RLOCK(); 988 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 989 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 990 break; 991 if (ifg == NULL) { 992 IFNET_RUNLOCK(); 993 return (ENOENT); 994 } 995 996 if (ifgr->ifgr_len == 0) { 997 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 998 ifgr->ifgr_len += sizeof(ifgrq); 999 IFNET_RUNLOCK(); 1000 return (0); 1001 } 1002 1003 len = ifgr->ifgr_len; 1004 ifgp = ifgr->ifgr_groups; 1005 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1006 if (len < sizeof(ifgrq)) { 1007 IFNET_RUNLOCK(); 1008 return (EINVAL); 1009 } 1010 bzero(&ifgrq, sizeof ifgrq); 1011 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1012 sizeof(ifgrq.ifgrq_member)); 1013 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1014 IFNET_RUNLOCK(); 1015 return (error); 1016 } 1017 len -= sizeof(ifgrq); 1018 ifgp++; 1019 } 1020 IFNET_RUNLOCK(); 1021 1022 return (0); 1023} 1024 1025/* 1026 * Delete Routes for a Network Interface 1027 * 1028 * Called for each routing entry via the rnh->rnh_walktree() call above 1029 * to delete all route entries referencing a detaching network interface. 1030 * 1031 * Arguments: 1032 * rn pointer to node in the routing table 1033 * arg argument passed to rnh->rnh_walktree() - detaching interface 1034 * 1035 * Returns: 1036 * 0 successful 1037 * errno failed - reason indicated 1038 * 1039 */ 1040static int 1041if_rtdel(struct radix_node *rn, void *arg) 1042{ 1043 struct rtentry *rt = (struct rtentry *)rn; 1044 struct ifnet *ifp = arg; 1045 int err; 1046 1047 if (rt->rt_ifp == ifp) { 1048 1049 /* 1050 * Protect (sorta) against walktree recursion problems 1051 * with cloned routes 1052 */ 1053 if ((rt->rt_flags & RTF_UP) == 0) 1054 return (0); 1055 1056 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1057 rt_mask(rt), rt->rt_flags, 1058 (struct rtentry **) NULL, rt->rt_fibnum); 1059 if (err) { 1060 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1061 } 1062 } 1063 1064 return (0); 1065} 1066 1067/* 1068 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1069 * structs used to represent other address families, it is necessary 1070 * to perform a different comparison. 1071 */ 1072 1073#define sa_equal(a1, a2) \ 1074 (bcmp((a1), (a2), ((a1))->sa_len) == 0) 1075 1076#define sa_dl_equal(a1, a2) \ 1077 ((((struct sockaddr_dl *)(a1))->sdl_len == \ 1078 ((struct sockaddr_dl *)(a2))->sdl_len) && \ 1079 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 1080 LLADDR((struct sockaddr_dl *)(a2)), \ 1081 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1082 1083/* 1084 * Locate an interface based on a complete address. 1085 */ 1086/*ARGSUSED*/ 1087struct ifaddr * 1088ifa_ifwithaddr(struct sockaddr *addr) 1089{ 1090 struct ifnet *ifp; 1091 struct ifaddr *ifa; 1092 1093 IFNET_RLOCK(); 1094 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1095 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1096 if (ifa->ifa_addr->sa_family != addr->sa_family) 1097 continue; 1098 if (sa_equal(addr, ifa->ifa_addr)) 1099 goto done; 1100 /* IP6 doesn't have broadcast */ 1101 if ((ifp->if_flags & IFF_BROADCAST) && 1102 ifa->ifa_broadaddr && 1103 ifa->ifa_broadaddr->sa_len != 0 && 1104 sa_equal(ifa->ifa_broadaddr, addr)) 1105 goto done; 1106 } 1107 ifa = NULL; 1108done: 1109 IFNET_RUNLOCK(); 1110 return (ifa); 1111} 1112 1113/* 1114 * Locate an interface based on the broadcast address. 1115 */ 1116/* ARGSUSED */ 1117struct ifaddr * 1118ifa_ifwithbroadaddr(struct sockaddr *addr) 1119{ 1120 struct ifnet *ifp; 1121 struct ifaddr *ifa; 1122 1123 IFNET_RLOCK(); 1124 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1125 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1126 if (ifa->ifa_addr->sa_family != addr->sa_family) 1127 continue; 1128 if ((ifp->if_flags & IFF_BROADCAST) && 1129 ifa->ifa_broadaddr && 1130 ifa->ifa_broadaddr->sa_len != 0 && 1131 sa_equal(ifa->ifa_broadaddr, addr)) 1132 goto done; 1133 } 1134 ifa = NULL; 1135done: 1136 IFNET_RUNLOCK(); 1137 return (ifa); 1138} 1139 1140/* 1141 * Locate the point to point interface with a given destination address. 1142 */ 1143/*ARGSUSED*/ 1144struct ifaddr * 1145ifa_ifwithdstaddr(struct sockaddr *addr) 1146{ 1147 struct ifnet *ifp; 1148 struct ifaddr *ifa; 1149 1150 IFNET_RLOCK(); 1151 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1152 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1153 continue; 1154 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1155 if (ifa->ifa_addr->sa_family != addr->sa_family) 1156 continue; 1157 if (ifa->ifa_dstaddr != NULL && 1158 sa_equal(addr, ifa->ifa_dstaddr)) 1159 goto done; 1160 } 1161 } 1162 ifa = NULL; 1163done: 1164 IFNET_RUNLOCK(); 1165 return (ifa); 1166} 1167 1168/* 1169 * Find an interface on a specific network. If many, choice 1170 * is most specific found. 1171 */ 1172struct ifaddr * 1173ifa_ifwithnet(struct sockaddr *addr) 1174{ 1175 struct ifnet *ifp; 1176 struct ifaddr *ifa; 1177 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 1178 u_int af = addr->sa_family; 1179 char *addr_data = addr->sa_data, *cplim; 1180 1181 /* 1182 * AF_LINK addresses can be looked up directly by their index number, 1183 * so do that if we can. 1184 */ 1185 if (af == AF_LINK) { 1186 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1187 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1188 return (ifaddr_byindex(sdl->sdl_index)); 1189 } 1190 1191 /* 1192 * Scan though each interface, looking for ones that have 1193 * addresses in this address family. 1194 */ 1195 IFNET_RLOCK(); 1196 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1197 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1198 char *cp, *cp2, *cp3; 1199 1200 if (ifa->ifa_addr->sa_family != af) 1201next: continue; 1202 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 1203 /* 1204 * This is a bit broken as it doesn't 1205 * take into account that the remote end may 1206 * be a single node in the network we are 1207 * looking for. 1208 * The trouble is that we don't know the 1209 * netmask for the remote end. 1210 */ 1211 if (ifa->ifa_dstaddr != NULL && 1212 sa_equal(addr, ifa->ifa_dstaddr)) 1213 goto done; 1214 } else { 1215 /* 1216 * if we have a special address handler, 1217 * then use it instead of the generic one. 1218 */ 1219 if (ifa->ifa_claim_addr) { 1220 if ((*ifa->ifa_claim_addr)(ifa, addr)) 1221 goto done; 1222 continue; 1223 } 1224 1225 /* 1226 * Scan all the bits in the ifa's address. 1227 * If a bit dissagrees with what we are 1228 * looking for, mask it with the netmask 1229 * to see if it really matters. 1230 * (A byte at a time) 1231 */ 1232 if (ifa->ifa_netmask == 0) 1233 continue; 1234 cp = addr_data; 1235 cp2 = ifa->ifa_addr->sa_data; 1236 cp3 = ifa->ifa_netmask->sa_data; 1237 cplim = ifa->ifa_netmask->sa_len 1238 + (char *)ifa->ifa_netmask; 1239 while (cp3 < cplim) 1240 if ((*cp++ ^ *cp2++) & *cp3++) 1241 goto next; /* next address! */ 1242 /* 1243 * If the netmask of what we just found 1244 * is more specific than what we had before 1245 * (if we had one) then remember the new one 1246 * before continuing to search 1247 * for an even better one. 1248 */ 1249 if (ifa_maybe == 0 || 1250 rn_refines((caddr_t)ifa->ifa_netmask, 1251 (caddr_t)ifa_maybe->ifa_netmask)) 1252 ifa_maybe = ifa; 1253 } 1254 } 1255 } 1256 ifa = ifa_maybe; 1257done: 1258 IFNET_RUNLOCK(); 1259 return (ifa); 1260} 1261 1262/* 1263 * Find an interface address specific to an interface best matching 1264 * a given address. 1265 */ 1266struct ifaddr * 1267ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1268{ 1269 struct ifaddr *ifa; 1270 char *cp, *cp2, *cp3; 1271 char *cplim; 1272 struct ifaddr *ifa_maybe = 0; 1273 u_int af = addr->sa_family; 1274 1275 if (af >= AF_MAX) 1276 return (0); 1277 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1278 if (ifa->ifa_addr->sa_family != af) 1279 continue; 1280 if (ifa_maybe == 0) 1281 ifa_maybe = ifa; 1282 if (ifa->ifa_netmask == 0) { 1283 if (sa_equal(addr, ifa->ifa_addr) || 1284 (ifa->ifa_dstaddr && 1285 sa_equal(addr, ifa->ifa_dstaddr))) 1286 goto done; 1287 continue; 1288 } 1289 if (ifp->if_flags & IFF_POINTOPOINT) { 1290 if (sa_equal(addr, ifa->ifa_dstaddr)) 1291 goto done; 1292 } else { 1293 cp = addr->sa_data; 1294 cp2 = ifa->ifa_addr->sa_data; 1295 cp3 = ifa->ifa_netmask->sa_data; 1296 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1297 for (; cp3 < cplim; cp3++) 1298 if ((*cp++ ^ *cp2++) & *cp3) 1299 break; 1300 if (cp3 == cplim) 1301 goto done; 1302 } 1303 } 1304 ifa = ifa_maybe; 1305done: 1306 return (ifa); 1307} 1308 1309#include <net/route.h> 1310 1311/* 1312 * Default action when installing a route with a Link Level gateway. 1313 * Lookup an appropriate real ifa to point to. 1314 * This should be moved to /sys/net/link.c eventually. 1315 */ 1316static void 1317link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1318{ 1319 struct ifaddr *ifa, *oifa; 1320 struct sockaddr *dst; 1321 struct ifnet *ifp; 1322 1323 RT_LOCK_ASSERT(rt); 1324 1325 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1326 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1327 return; 1328 ifa = ifaof_ifpforaddr(dst, ifp); 1329 if (ifa) { 1330 IFAREF(ifa); /* XXX */ 1331 oifa = rt->rt_ifa; 1332 rt->rt_ifa = ifa; 1333 IFAFREE(oifa); 1334 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1335 ifa->ifa_rtrequest(cmd, rt, info); 1336 } 1337} 1338 1339/* 1340 * Mark an interface down and notify protocols of 1341 * the transition. 1342 * NOTE: must be called at splnet or eqivalent. 1343 */ 1344static void 1345if_unroute(struct ifnet *ifp, int flag, int fam) 1346{ 1347 struct ifaddr *ifa; 1348 1349 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1350 1351 ifp->if_flags &= ~flag; 1352 getmicrotime(&ifp->if_lastchange); 1353 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1354 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1355 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1356 if_qflush(&ifp->if_snd); 1357#ifdef DEV_CARP 1358 if (ifp->if_carp) 1359 carp_carpdev_state(ifp->if_carp); 1360#endif 1361 rt_ifmsg(ifp); 1362} 1363 1364/* 1365 * Mark an interface up and notify protocols of 1366 * the transition. 1367 * NOTE: must be called at splnet or eqivalent. 1368 */ 1369static void 1370if_route(struct ifnet *ifp, int flag, int fam) 1371{ 1372 struct ifaddr *ifa; 1373 1374 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1375 1376 ifp->if_flags |= flag; 1377 getmicrotime(&ifp->if_lastchange); 1378 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1379 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1380 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1381#ifdef DEV_CARP 1382 if (ifp->if_carp) 1383 carp_carpdev_state(ifp->if_carp); 1384#endif 1385 rt_ifmsg(ifp); 1386#ifdef INET6 1387 in6_if_up(ifp); 1388#endif 1389} 1390 1391void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 1392void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1393 1394/* 1395 * Handle a change in the interface link state. To avoid LORs 1396 * between driver lock and upper layer locks, as well as possible 1397 * recursions, we post event to taskqueue, and all job 1398 * is done in static do_link_state_change(). 1399 */ 1400void 1401if_link_state_change(struct ifnet *ifp, int link_state) 1402{ 1403 /* Return if state hasn't changed. */ 1404 if (ifp->if_link_state == link_state) 1405 return; 1406 1407 ifp->if_link_state = link_state; 1408 1409 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 1410} 1411 1412static void 1413do_link_state_change(void *arg, int pending) 1414{ 1415 struct ifnet *ifp = (struct ifnet *)arg; 1416 int link_state = ifp->if_link_state; 1417 int link; 1418 1419 /* Notify that the link state has changed. */ 1420 rt_ifmsg(ifp); 1421 if (link_state == LINK_STATE_UP) 1422 link = NOTE_LINKUP; 1423 else if (link_state == LINK_STATE_DOWN) 1424 link = NOTE_LINKDOWN; 1425 else 1426 link = NOTE_LINKINV; 1427 KNOTE_UNLOCKED(&ifp->if_klist, link); 1428 if (ifp->if_vlantrunk != NULL) 1429 (*vlan_link_state_p)(ifp, link); 1430 1431 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 1432 IFP2AC(ifp)->ac_netgraph != NULL) 1433 (*ng_ether_link_state_p)(ifp, link_state); 1434#ifdef DEV_CARP 1435 if (ifp->if_carp) 1436 carp_carpdev_state(ifp->if_carp); 1437#endif 1438 if (ifp->if_bridge) { 1439 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 1440 (*bstp_linkstate_p)(ifp, link_state); 1441 } 1442 if (ifp->if_lagg) { 1443 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!")); 1444 (*lagg_linkstate_p)(ifp, link_state); 1445 } 1446 1447 devctl_notify("IFNET", ifp->if_xname, 1448 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); 1449 if (pending > 1) 1450 if_printf(ifp, "%d link states coalesced\n", pending); 1451 if (log_link_state_change) 1452 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 1453 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1454} 1455 1456/* 1457 * Mark an interface down and notify protocols of 1458 * the transition. 1459 * NOTE: must be called at splnet or eqivalent. 1460 */ 1461void 1462if_down(struct ifnet *ifp) 1463{ 1464 1465 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1466} 1467 1468/* 1469 * Mark an interface up and notify protocols of 1470 * the transition. 1471 * NOTE: must be called at splnet or eqivalent. 1472 */ 1473void 1474if_up(struct ifnet *ifp) 1475{ 1476 1477 if_route(ifp, IFF_UP, AF_UNSPEC); 1478} 1479 1480/* 1481 * Flush an interface queue. 1482 */ 1483static void 1484if_qflush(struct ifaltq *ifq) 1485{ 1486 struct mbuf *m, *n; 1487 1488 IFQ_LOCK(ifq); 1489#ifdef ALTQ 1490 if (ALTQ_IS_ENABLED(ifq)) 1491 ALTQ_PURGE(ifq); 1492#endif 1493 n = ifq->ifq_head; 1494 while ((m = n) != 0) { 1495 n = m->m_act; 1496 m_freem(m); 1497 } 1498 ifq->ifq_head = 0; 1499 ifq->ifq_tail = 0; 1500 ifq->ifq_len = 0; 1501 IFQ_UNLOCK(ifq); 1502} 1503 1504/* 1505 * Handle interface watchdog timer routines. Called 1506 * from softclock, we decrement timers (if set) and 1507 * call the appropriate interface routine on expiration. 1508 * 1509 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1510 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab 1511 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface. 1512 */ 1513static void 1514if_slowtimo(void *arg) 1515{ 1516 struct ifnet *ifp; 1517 int s = splimp(); 1518 1519 IFNET_RLOCK(); 1520 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1521 if (ifp->if_timer == 0 || --ifp->if_timer) 1522 continue; 1523 if (ifp->if_watchdog) 1524 (*ifp->if_watchdog)(ifp); 1525 } 1526 IFNET_RUNLOCK(); 1527 splx(s); 1528 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1529} 1530 1531/* 1532 * Map interface name to 1533 * interface structure pointer. 1534 */ 1535struct ifnet * 1536ifunit(const char *name) 1537{ 1538 struct ifnet *ifp; 1539 1540 IFNET_RLOCK(); 1541 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1542 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1543 break; 1544 } 1545 IFNET_RUNLOCK(); 1546 return (ifp); 1547} 1548 1549/* 1550 * Hardware specific interface ioctls. 1551 */ 1552static int 1553ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1554{ 1555 struct ifreq *ifr; 1556 struct ifstat *ifs; 1557 int error = 0; 1558 int new_flags, temp_flags; 1559 size_t namelen, onamelen; 1560 char new_name[IFNAMSIZ]; 1561 struct ifaddr *ifa; 1562 struct sockaddr_dl *sdl; 1563 1564 ifr = (struct ifreq *)data; 1565 switch (cmd) { 1566 case SIOCGIFINDEX: 1567 ifr->ifr_index = ifp->if_index; 1568 break; 1569 1570 case SIOCGIFFLAGS: 1571 temp_flags = ifp->if_flags | ifp->if_drv_flags; 1572 ifr->ifr_flags = temp_flags & 0xffff; 1573 ifr->ifr_flagshigh = temp_flags >> 16; 1574 break; 1575 1576 case SIOCGIFCAP: 1577 ifr->ifr_reqcap = ifp->if_capabilities; 1578 ifr->ifr_curcap = ifp->if_capenable; 1579 break; 1580 1581#ifdef MAC 1582 case SIOCGIFMAC: 1583 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 1584 break; 1585#endif 1586 1587 case SIOCGIFMETRIC: 1588 ifr->ifr_metric = ifp->if_metric; 1589 break; 1590 1591 case SIOCGIFMTU: 1592 ifr->ifr_mtu = ifp->if_mtu; 1593 break; 1594 1595 case SIOCGIFPHYS: 1596 ifr->ifr_phys = ifp->if_physical; 1597 break; 1598 1599 case SIOCSIFFLAGS: 1600 error = priv_check(td, PRIV_NET_SETIFFLAGS); 1601 if (error) 1602 return (error); 1603 /* 1604 * Currently, no driver owned flags pass the IFF_CANTCHANGE 1605 * check, so we don't need special handling here yet. 1606 */ 1607 new_flags = (ifr->ifr_flags & 0xffff) | 1608 (ifr->ifr_flagshigh << 16); 1609 if (ifp->if_flags & IFF_SMART) { 1610 /* Smart drivers twiddle their own routes */ 1611 } else if (ifp->if_flags & IFF_UP && 1612 (new_flags & IFF_UP) == 0) { 1613 int s = splimp(); 1614 if_down(ifp); 1615 splx(s); 1616 } else if (new_flags & IFF_UP && 1617 (ifp->if_flags & IFF_UP) == 0) { 1618 int s = splimp(); 1619 if_up(ifp); 1620 splx(s); 1621 } 1622 /* See if permanently promiscuous mode bit is about to flip */ 1623 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 1624 if (new_flags & IFF_PPROMISC) 1625 ifp->if_flags |= IFF_PROMISC; 1626 else if (ifp->if_pcount == 0) 1627 ifp->if_flags &= ~IFF_PROMISC; 1628 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 1629 ifp->if_xname, 1630 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 1631 } 1632 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1633 (new_flags &~ IFF_CANTCHANGE); 1634 if (ifp->if_ioctl) { 1635 IFF_LOCKGIANT(ifp); 1636 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1637 IFF_UNLOCKGIANT(ifp); 1638 } 1639 getmicrotime(&ifp->if_lastchange); 1640 break; 1641 1642 case SIOCSIFCAP: 1643 error = priv_check(td, PRIV_NET_SETIFCAP); 1644 if (error) 1645 return (error); 1646 if (ifp->if_ioctl == NULL) 1647 return (EOPNOTSUPP); 1648 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1649 return (EINVAL); 1650 IFF_LOCKGIANT(ifp); 1651 error = (*ifp->if_ioctl)(ifp, cmd, data); 1652 IFF_UNLOCKGIANT(ifp); 1653 if (error == 0) 1654 getmicrotime(&ifp->if_lastchange); 1655 break; 1656 1657#ifdef MAC 1658 case SIOCSIFMAC: 1659 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 1660 break; 1661#endif 1662 1663 case SIOCSIFNAME: 1664 error = priv_check(td, PRIV_NET_SETIFNAME); 1665 if (error) 1666 return (error); 1667 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1668 if (error != 0) 1669 return (error); 1670 if (new_name[0] == '\0') 1671 return (EINVAL); 1672 if (ifunit(new_name) != NULL) 1673 return (EEXIST); 1674 1675 /* Announce the departure of the interface. */ 1676 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1677 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1678 1679 log(LOG_INFO, "%s: changing name to '%s'\n", 1680 ifp->if_xname, new_name); 1681 1682 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1683 ifa = ifp->if_addr; 1684 IFA_LOCK(ifa); 1685 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1686 namelen = strlen(new_name); 1687 onamelen = sdl->sdl_nlen; 1688 /* 1689 * Move the address if needed. This is safe because we 1690 * allocate space for a name of length IFNAMSIZ when we 1691 * create this in if_attach(). 1692 */ 1693 if (namelen != onamelen) { 1694 bcopy(sdl->sdl_data + onamelen, 1695 sdl->sdl_data + namelen, sdl->sdl_alen); 1696 } 1697 bcopy(new_name, sdl->sdl_data, namelen); 1698 sdl->sdl_nlen = namelen; 1699 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1700 bzero(sdl->sdl_data, onamelen); 1701 while (namelen != 0) 1702 sdl->sdl_data[--namelen] = 0xff; 1703 IFA_UNLOCK(ifa); 1704 1705 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 1706 /* Announce the return of the interface. */ 1707 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1708 break; 1709 1710 case SIOCSIFMETRIC: 1711 error = priv_check(td, PRIV_NET_SETIFMETRIC); 1712 if (error) 1713 return (error); 1714 ifp->if_metric = ifr->ifr_metric; 1715 getmicrotime(&ifp->if_lastchange); 1716 break; 1717 1718 case SIOCSIFPHYS: 1719 error = priv_check(td, PRIV_NET_SETIFPHYS); 1720 if (error) 1721 return (error); 1722 if (ifp->if_ioctl == NULL) 1723 return (EOPNOTSUPP); 1724 IFF_LOCKGIANT(ifp); 1725 error = (*ifp->if_ioctl)(ifp, cmd, data); 1726 IFF_UNLOCKGIANT(ifp); 1727 if (error == 0) 1728 getmicrotime(&ifp->if_lastchange); 1729 break; 1730 1731 case SIOCSIFMTU: 1732 { 1733 u_long oldmtu = ifp->if_mtu; 1734 1735 error = priv_check(td, PRIV_NET_SETIFMTU); 1736 if (error) 1737 return (error); 1738 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1739 return (EINVAL); 1740 if (ifp->if_ioctl == NULL) 1741 return (EOPNOTSUPP); 1742 IFF_LOCKGIANT(ifp); 1743 error = (*ifp->if_ioctl)(ifp, cmd, data); 1744 IFF_UNLOCKGIANT(ifp); 1745 if (error == 0) { 1746 getmicrotime(&ifp->if_lastchange); 1747 rt_ifmsg(ifp); 1748 } 1749 /* 1750 * If the link MTU changed, do network layer specific procedure. 1751 */ 1752 if (ifp->if_mtu != oldmtu) { 1753#ifdef INET6 1754 nd6_setmtu(ifp); 1755#endif 1756 } 1757 break; 1758 } 1759 1760 case SIOCADDMULTI: 1761 case SIOCDELMULTI: 1762 if (cmd == SIOCADDMULTI) 1763 error = priv_check(td, PRIV_NET_ADDMULTI); 1764 else 1765 error = priv_check(td, PRIV_NET_DELMULTI); 1766 if (error) 1767 return (error); 1768 1769 /* Don't allow group membership on non-multicast interfaces. */ 1770 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1771 return (EOPNOTSUPP); 1772 1773 /* Don't let users screw up protocols' entries. */ 1774 if (ifr->ifr_addr.sa_family != AF_LINK) 1775 return (EINVAL); 1776 1777 if (cmd == SIOCADDMULTI) { 1778 struct ifmultiaddr *ifma; 1779 1780 /* 1781 * Userland is only permitted to join groups once 1782 * via the if_addmulti() KPI, because it cannot hold 1783 * struct ifmultiaddr * between calls. It may also 1784 * lose a race while we check if the membership 1785 * already exists. 1786 */ 1787 IF_ADDR_LOCK(ifp); 1788 ifma = if_findmulti(ifp, &ifr->ifr_addr); 1789 IF_ADDR_UNLOCK(ifp); 1790 if (ifma != NULL) 1791 error = EADDRINUSE; 1792 else 1793 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1794 } else { 1795 error = if_delmulti(ifp, &ifr->ifr_addr); 1796 } 1797 if (error == 0) 1798 getmicrotime(&ifp->if_lastchange); 1799 break; 1800 1801 case SIOCSIFPHYADDR: 1802 case SIOCDIFPHYADDR: 1803#ifdef INET6 1804 case SIOCSIFPHYADDR_IN6: 1805#endif 1806 case SIOCSLIFPHYADDR: 1807 case SIOCSIFMEDIA: 1808 case SIOCSIFGENERIC: 1809 error = priv_check(td, PRIV_NET_HWIOCTL); 1810 if (error) 1811 return (error); 1812 if (ifp->if_ioctl == NULL) 1813 return (EOPNOTSUPP); 1814 IFF_LOCKGIANT(ifp); 1815 error = (*ifp->if_ioctl)(ifp, cmd, data); 1816 IFF_UNLOCKGIANT(ifp); 1817 if (error == 0) 1818 getmicrotime(&ifp->if_lastchange); 1819 break; 1820 1821 case SIOCGIFSTATUS: 1822 ifs = (struct ifstat *)data; 1823 ifs->ascii[0] = '\0'; 1824 1825 case SIOCGIFPSRCADDR: 1826 case SIOCGIFPDSTADDR: 1827 case SIOCGLIFPHYADDR: 1828 case SIOCGIFMEDIA: 1829 case SIOCGIFGENERIC: 1830 if (ifp->if_ioctl == NULL) 1831 return (EOPNOTSUPP); 1832 IFF_LOCKGIANT(ifp); 1833 error = (*ifp->if_ioctl)(ifp, cmd, data); 1834 IFF_UNLOCKGIANT(ifp); 1835 break; 1836 1837 case SIOCSIFLLADDR: 1838 error = priv_check(td, PRIV_NET_SETLLADDR); 1839 if (error) 1840 return (error); 1841 error = if_setlladdr(ifp, 1842 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1843 break; 1844 1845 case SIOCAIFGROUP: 1846 { 1847 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1848 1849 error = priv_check(td, PRIV_NET_ADDIFGROUP); 1850 if (error) 1851 return (error); 1852 if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 1853 return (error); 1854 break; 1855 } 1856 1857 case SIOCGIFGROUP: 1858 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 1859 return (error); 1860 break; 1861 1862 case SIOCDIFGROUP: 1863 { 1864 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1865 1866 error = priv_check(td, PRIV_NET_DELIFGROUP); 1867 if (error) 1868 return (error); 1869 if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 1870 return (error); 1871 break; 1872 } 1873 1874 default: 1875 error = ENOIOCTL; 1876 break; 1877 } 1878 return (error); 1879} 1880 1881/* 1882 * Interface ioctls. 1883 */ 1884int 1885ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 1886{ 1887 struct ifnet *ifp; 1888 struct ifreq *ifr; 1889 int error; 1890 int oif_flags; 1891 1892 switch (cmd) { 1893 case SIOCGIFCONF: 1894 case OSIOCGIFCONF: 1895#ifdef __amd64__ 1896 case SIOCGIFCONF32: 1897#endif 1898 return (ifconf(cmd, data)); 1899 } 1900 ifr = (struct ifreq *)data; 1901 1902 switch (cmd) { 1903 case SIOCIFCREATE: 1904 case SIOCIFCREATE2: 1905 error = priv_check(td, PRIV_NET_IFCREATE); 1906 if (error) 1907 return (error); 1908 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name), 1909 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL)); 1910 case SIOCIFDESTROY: 1911 error = priv_check(td, PRIV_NET_IFDESTROY); 1912 if (error) 1913 return (error); 1914 return if_clone_destroy(ifr->ifr_name); 1915 1916 case SIOCIFGCLONERS: 1917 return (if_clone_list((struct if_clonereq *)data)); 1918 case SIOCGIFGMEMB: 1919 return (if_getgroupmembers((struct ifgroupreq *)data)); 1920 } 1921 1922 ifp = ifunit(ifr->ifr_name); 1923 if (ifp == 0) 1924 return (ENXIO); 1925 1926 error = ifhwioctl(cmd, ifp, data, td); 1927 if (error != ENOIOCTL) 1928 return (error); 1929 1930 oif_flags = ifp->if_flags; 1931 if (so->so_proto == 0) 1932 return (EOPNOTSUPP); 1933#ifndef COMPAT_43 1934 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1935 data, 1936 ifp, td)); 1937#else 1938 { 1939 int ocmd = cmd; 1940 1941 switch (cmd) { 1942 1943 case SIOCSIFDSTADDR: 1944 case SIOCSIFADDR: 1945 case SIOCSIFBRDADDR: 1946 case SIOCSIFNETMASK: 1947#if BYTE_ORDER != BIG_ENDIAN 1948 if (ifr->ifr_addr.sa_family == 0 && 1949 ifr->ifr_addr.sa_len < 16) { 1950 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1951 ifr->ifr_addr.sa_len = 16; 1952 } 1953#else 1954 if (ifr->ifr_addr.sa_len == 0) 1955 ifr->ifr_addr.sa_len = 16; 1956#endif 1957 break; 1958 1959 case OSIOCGIFADDR: 1960 cmd = SIOCGIFADDR; 1961 break; 1962 1963 case OSIOCGIFDSTADDR: 1964 cmd = SIOCGIFDSTADDR; 1965 break; 1966 1967 case OSIOCGIFBRDADDR: 1968 cmd = SIOCGIFBRDADDR; 1969 break; 1970 1971 case OSIOCGIFNETMASK: 1972 cmd = SIOCGIFNETMASK; 1973 } 1974 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 1975 cmd, 1976 data, 1977 ifp, td)); 1978 switch (ocmd) { 1979 1980 case OSIOCGIFADDR: 1981 case OSIOCGIFDSTADDR: 1982 case OSIOCGIFBRDADDR: 1983 case OSIOCGIFNETMASK: 1984 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1985 1986 } 1987 } 1988#endif /* COMPAT_43 */ 1989 1990 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 1991#ifdef INET6 1992 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 1993 if (ifp->if_flags & IFF_UP) { 1994 int s = splimp(); 1995 in6_if_up(ifp); 1996 splx(s); 1997 } 1998#endif 1999 } 2000 return (error); 2001} 2002 2003/* 2004 * The code common to handling reference counted flags, 2005 * e.g., in ifpromisc() and if_allmulti(). 2006 * The "pflag" argument can specify a permanent mode flag to check, 2007 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2008 * 2009 * Only to be used on stack-owned flags, not driver-owned flags. 2010 */ 2011static int 2012if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 2013{ 2014 struct ifreq ifr; 2015 int error; 2016 int oldflags, oldcount; 2017 2018 /* Sanity checks to catch programming errors */ 2019 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2020 ("%s: setting driver-owned flag %d", __func__, flag)); 2021 2022 if (onswitch) 2023 KASSERT(*refcount >= 0, 2024 ("%s: increment negative refcount %d for flag %d", 2025 __func__, *refcount, flag)); 2026 else 2027 KASSERT(*refcount > 0, 2028 ("%s: decrement non-positive refcount %d for flag %d", 2029 __func__, *refcount, flag)); 2030 2031 /* In case this mode is permanent, just touch refcount */ 2032 if (ifp->if_flags & pflag) { 2033 *refcount += onswitch ? 1 : -1; 2034 return (0); 2035 } 2036 2037 /* Save ifnet parameters for if_ioctl() may fail */ 2038 oldcount = *refcount; 2039 oldflags = ifp->if_flags; 2040 2041 /* 2042 * See if we aren't the only and touching refcount is enough. 2043 * Actually toggle interface flag if we are the first or last. 2044 */ 2045 if (onswitch) { 2046 if ((*refcount)++) 2047 return (0); 2048 ifp->if_flags |= flag; 2049 } else { 2050 if (--(*refcount)) 2051 return (0); 2052 ifp->if_flags &= ~flag; 2053 } 2054 2055 /* Call down the driver since we've changed interface flags */ 2056 if (ifp->if_ioctl == NULL) { 2057 error = EOPNOTSUPP; 2058 goto recover; 2059 } 2060 ifr.ifr_flags = ifp->if_flags & 0xffff; 2061 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2062 IFF_LOCKGIANT(ifp); 2063 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2064 IFF_UNLOCKGIANT(ifp); 2065 if (error) 2066 goto recover; 2067 /* Notify userland that interface flags have changed */ 2068 rt_ifmsg(ifp); 2069 return (0); 2070 2071recover: 2072 /* Recover after driver error */ 2073 *refcount = oldcount; 2074 ifp->if_flags = oldflags; 2075 return (error); 2076} 2077 2078/* 2079 * Set/clear promiscuous mode on interface ifp based on the truth value 2080 * of pswitch. The calls are reference counted so that only the first 2081 * "on" request actually has an effect, as does the final "off" request. 2082 * Results are undefined if the "off" and "on" requests are not matched. 2083 */ 2084int 2085ifpromisc(struct ifnet *ifp, int pswitch) 2086{ 2087 int error; 2088 int oldflags = ifp->if_flags; 2089 2090 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 2091 &ifp->if_pcount, pswitch); 2092 /* If promiscuous mode status has changed, log a message */ 2093 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 2094 log(LOG_INFO, "%s: promiscuous mode %s\n", 2095 ifp->if_xname, 2096 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 2097 return (error); 2098} 2099 2100/* 2101 * Return interface configuration 2102 * of system. List may be used 2103 * in later ioctl's (above) to get 2104 * other information. 2105 */ 2106/*ARGSUSED*/ 2107static int 2108ifconf(u_long cmd, caddr_t data) 2109{ 2110 struct ifconf *ifc = (struct ifconf *)data; 2111#ifdef __amd64__ 2112 struct ifconf32 *ifc32 = (struct ifconf32 *)data; 2113 struct ifconf ifc_swab; 2114#endif 2115 struct ifnet *ifp; 2116 struct ifaddr *ifa; 2117 struct ifreq ifr; 2118 struct sbuf *sb; 2119 int error, full = 0, valid_len, max_len; 2120 2121#ifdef __amd64__ 2122 if (cmd == SIOCGIFCONF32) { 2123 ifc_swab.ifc_len = ifc32->ifc_len; 2124 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf; 2125 ifc = &ifc_swab; 2126 } 2127#endif 2128 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 2129 max_len = MAXPHYS - 1; 2130 2131 /* Prevent hostile input from being able to crash the system */ 2132 if (ifc->ifc_len <= 0) 2133 return (EINVAL); 2134 2135again: 2136 if (ifc->ifc_len <= max_len) { 2137 max_len = ifc->ifc_len; 2138 full = 1; 2139 } 2140 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2141 max_len = 0; 2142 valid_len = 0; 2143 2144 IFNET_RLOCK(); /* could sleep XXX */ 2145 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2146 int addrs; 2147 2148 /* 2149 * Zero the ifr_name buffer to make sure we don't 2150 * disclose the contents of the stack. 2151 */ 2152 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2153 2154 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2155 >= sizeof(ifr.ifr_name)) { 2156 sbuf_delete(sb); 2157 IFNET_RUNLOCK(); 2158 return (ENAMETOOLONG); 2159 } 2160 2161 addrs = 0; 2162 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2163 struct sockaddr *sa = ifa->ifa_addr; 2164 2165 if (jailed(curthread->td_ucred) && 2166 prison_if(curthread->td_ucred, sa)) 2167 continue; 2168 addrs++; 2169#ifdef COMPAT_43 2170 if (cmd == OSIOCGIFCONF) { 2171 struct osockaddr *osa = 2172 (struct osockaddr *)&ifr.ifr_addr; 2173 ifr.ifr_addr = *sa; 2174 osa->sa_family = sa->sa_family; 2175 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2176 max_len += sizeof(ifr); 2177 } else 2178#endif 2179 if (sa->sa_len <= sizeof(*sa)) { 2180 ifr.ifr_addr = *sa; 2181 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2182 max_len += sizeof(ifr); 2183 } else { 2184 sbuf_bcat(sb, &ifr, 2185 offsetof(struct ifreq, ifr_addr)); 2186 max_len += offsetof(struct ifreq, ifr_addr); 2187 sbuf_bcat(sb, sa, sa->sa_len); 2188 max_len += sa->sa_len; 2189 } 2190 2191 if (!sbuf_overflowed(sb)) 2192 valid_len = sbuf_len(sb); 2193 } 2194 if (addrs == 0) { 2195 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2196 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2197 max_len += sizeof(ifr); 2198 2199 if (!sbuf_overflowed(sb)) 2200 valid_len = sbuf_len(sb); 2201 } 2202 } 2203 IFNET_RUNLOCK(); 2204 2205 /* 2206 * If we didn't allocate enough space (uncommon), try again. If 2207 * we have already allocated as much space as we are allowed, 2208 * return what we've got. 2209 */ 2210 if (valid_len != max_len && !full) { 2211 sbuf_delete(sb); 2212 goto again; 2213 } 2214 2215 ifc->ifc_len = valid_len; 2216#ifdef __amd64__ 2217 if (cmd == SIOCGIFCONF32) 2218 ifc32->ifc_len = valid_len; 2219#endif 2220 sbuf_finish(sb); 2221 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 2222 sbuf_delete(sb); 2223 return (error); 2224} 2225 2226/* 2227 * Just like ifpromisc(), but for all-multicast-reception mode. 2228 */ 2229int 2230if_allmulti(struct ifnet *ifp, int onswitch) 2231{ 2232 2233 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 2234} 2235 2236struct ifmultiaddr * 2237if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 2238{ 2239 struct ifmultiaddr *ifma; 2240 2241 IF_ADDR_LOCK_ASSERT(ifp); 2242 2243 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2244 if (sa->sa_family == AF_LINK) { 2245 if (sa_dl_equal(ifma->ifma_addr, sa)) 2246 break; 2247 } else { 2248 if (sa_equal(ifma->ifma_addr, sa)) 2249 break; 2250 } 2251 } 2252 2253 return ifma; 2254} 2255 2256/* 2257 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2258 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2259 * the ifnet multicast address list here, so the caller must do that and 2260 * other setup work (such as notifying the device driver). The reference 2261 * count is initialized to 1. 2262 */ 2263static struct ifmultiaddr * 2264if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2265 int mflags) 2266{ 2267 struct ifmultiaddr *ifma; 2268 struct sockaddr *dupsa; 2269 2270 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags | 2271 M_ZERO); 2272 if (ifma == NULL) 2273 return (NULL); 2274 2275 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags); 2276 if (dupsa == NULL) { 2277 FREE(ifma, M_IFMADDR); 2278 return (NULL); 2279 } 2280 bcopy(sa, dupsa, sa->sa_len); 2281 ifma->ifma_addr = dupsa; 2282 2283 ifma->ifma_ifp = ifp; 2284 ifma->ifma_refcount = 1; 2285 ifma->ifma_protospec = NULL; 2286 2287 if (llsa == NULL) { 2288 ifma->ifma_lladdr = NULL; 2289 return (ifma); 2290 } 2291 2292 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags); 2293 if (dupsa == NULL) { 2294 FREE(ifma->ifma_addr, M_IFMADDR); 2295 FREE(ifma, M_IFMADDR); 2296 return (NULL); 2297 } 2298 bcopy(llsa, dupsa, llsa->sa_len); 2299 ifma->ifma_lladdr = dupsa; 2300 2301 return (ifma); 2302} 2303 2304/* 2305 * if_freemulti: free ifmultiaddr structure and possibly attached related 2306 * addresses. The caller is responsible for implementing reference 2307 * counting, notifying the driver, handling routing messages, and releasing 2308 * any dependent link layer state. 2309 */ 2310static void 2311if_freemulti(struct ifmultiaddr *ifma) 2312{ 2313 2314 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2315 ifma->ifma_refcount)); 2316 KASSERT(ifma->ifma_protospec == NULL, 2317 ("if_freemulti: protospec not NULL")); 2318 2319 if (ifma->ifma_lladdr != NULL) 2320 FREE(ifma->ifma_lladdr, M_IFMADDR); 2321 FREE(ifma->ifma_addr, M_IFMADDR); 2322 FREE(ifma, M_IFMADDR); 2323} 2324 2325/* 2326 * Register an additional multicast address with a network interface. 2327 * 2328 * - If the address is already present, bump the reference count on the 2329 * address and return. 2330 * - If the address is not link-layer, look up a link layer address. 2331 * - Allocate address structures for one or both addresses, and attach to the 2332 * multicast address list on the interface. If automatically adding a link 2333 * layer address, the protocol address will own a reference to the link 2334 * layer address, to be freed when it is freed. 2335 * - Notify the network device driver of an addition to the multicast address 2336 * list. 2337 * 2338 * 'sa' points to caller-owned memory with the desired multicast address. 2339 * 2340 * 'retifma' will be used to return a pointer to the resulting multicast 2341 * address reference, if desired. 2342 */ 2343int 2344if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2345 struct ifmultiaddr **retifma) 2346{ 2347 struct ifmultiaddr *ifma, *ll_ifma; 2348 struct sockaddr *llsa; 2349 int error; 2350 2351 /* 2352 * If the address is already present, return a new reference to it; 2353 * otherwise, allocate storage and set up a new address. 2354 */ 2355 IF_ADDR_LOCK(ifp); 2356 ifma = if_findmulti(ifp, sa); 2357 if (ifma != NULL) { 2358 ifma->ifma_refcount++; 2359 if (retifma != NULL) 2360 *retifma = ifma; 2361 IF_ADDR_UNLOCK(ifp); 2362 return (0); 2363 } 2364 2365 /* 2366 * The address isn't already present; resolve the protocol address 2367 * into a link layer address, and then look that up, bump its 2368 * refcount or allocate an ifma for that also. If 'llsa' was 2369 * returned, we will need to free it later. 2370 */ 2371 llsa = NULL; 2372 ll_ifma = NULL; 2373 if (ifp->if_resolvemulti != NULL) { 2374 error = ifp->if_resolvemulti(ifp, &llsa, sa); 2375 if (error) 2376 goto unlock_out; 2377 } 2378 2379 /* 2380 * Allocate the new address. Don't hook it up yet, as we may also 2381 * need to allocate a link layer multicast address. 2382 */ 2383 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2384 if (ifma == NULL) { 2385 error = ENOMEM; 2386 goto free_llsa_out; 2387 } 2388 2389 /* 2390 * If a link layer address is found, we'll need to see if it's 2391 * already present in the address list, or allocate is as well. 2392 * When this block finishes, the link layer address will be on the 2393 * list. 2394 */ 2395 if (llsa != NULL) { 2396 ll_ifma = if_findmulti(ifp, llsa); 2397 if (ll_ifma == NULL) { 2398 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2399 if (ll_ifma == NULL) { 2400 --ifma->ifma_refcount; 2401 if_freemulti(ifma); 2402 error = ENOMEM; 2403 goto free_llsa_out; 2404 } 2405 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 2406 ifma_link); 2407 } else 2408 ll_ifma->ifma_refcount++; 2409 ifma->ifma_llifma = ll_ifma; 2410 } 2411 2412 /* 2413 * We now have a new multicast address, ifma, and possibly a new or 2414 * referenced link layer address. Add the primary address to the 2415 * ifnet address list. 2416 */ 2417 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2418 2419 if (retifma != NULL) 2420 *retifma = ifma; 2421 2422 /* 2423 * Must generate the message while holding the lock so that 'ifma' 2424 * pointer is still valid. 2425 */ 2426 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2427 IF_ADDR_UNLOCK(ifp); 2428 2429 /* 2430 * We are certain we have added something, so call down to the 2431 * interface to let them know about it. 2432 */ 2433 if (ifp->if_ioctl != NULL) { 2434 IFF_LOCKGIANT(ifp); 2435 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 2436 IFF_UNLOCKGIANT(ifp); 2437 } 2438 2439 if (llsa != NULL) 2440 FREE(llsa, M_IFMADDR); 2441 2442 return (0); 2443 2444free_llsa_out: 2445 if (llsa != NULL) 2446 FREE(llsa, M_IFMADDR); 2447 2448unlock_out: 2449 IF_ADDR_UNLOCK(ifp); 2450 return (error); 2451} 2452 2453/* 2454 * Delete a multicast group membership by network-layer group address. 2455 * 2456 * Returns ENOENT if the entry could not be found. If ifp no longer 2457 * exists, results are undefined. This entry point should only be used 2458 * from subsystems which do appropriate locking to hold ifp for the 2459 * duration of the call. 2460 * Network-layer protocol domains must use if_delmulti_ifma(). 2461 */ 2462int 2463if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 2464{ 2465 struct ifmultiaddr *ifma; 2466 int lastref; 2467#ifdef INVARIANTS 2468 struct ifnet *oifp; 2469 2470 IFNET_RLOCK(); 2471 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2472 if (ifp == oifp) 2473 break; 2474 if (ifp != oifp) 2475 ifp = NULL; 2476 IFNET_RUNLOCK(); 2477 2478 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 2479#endif 2480 if (ifp == NULL) 2481 return (ENOENT); 2482 2483 IF_ADDR_LOCK(ifp); 2484 lastref = 0; 2485 ifma = if_findmulti(ifp, sa); 2486 if (ifma != NULL) 2487 lastref = if_delmulti_locked(ifp, ifma, 0); 2488 IF_ADDR_UNLOCK(ifp); 2489 2490 if (ifma == NULL) 2491 return (ENOENT); 2492 2493 if (lastref && ifp->if_ioctl != NULL) { 2494 IFF_LOCKGIANT(ifp); 2495 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2496 IFF_UNLOCKGIANT(ifp); 2497 } 2498 2499 return (0); 2500} 2501 2502/* 2503 * Delete a multicast group membership by group membership pointer. 2504 * Network-layer protocol domains must use this routine. 2505 * 2506 * It is safe to call this routine if the ifp disappeared. Callers should 2507 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be 2508 * reconfigured. 2509 */ 2510void 2511if_delmulti_ifma(struct ifmultiaddr *ifma) 2512{ 2513 struct ifnet *ifp; 2514 int lastref; 2515 2516 ifp = ifma->ifma_ifp; 2517#ifdef DIAGNOSTIC 2518 if (ifp == NULL) { 2519 printf("%s: ifma_ifp seems to be detached\n", __func__); 2520 } else { 2521 struct ifnet *oifp; 2522 2523 IFNET_RLOCK(); 2524 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2525 if (ifp == oifp) 2526 break; 2527 if (ifp != oifp) { 2528 printf("%s: ifnet %p disappeared\n", __func__, ifp); 2529 ifp = NULL; 2530 } 2531 IFNET_RUNLOCK(); 2532 } 2533#endif 2534 /* 2535 * If and only if the ifnet instance exists: Acquire the address lock. 2536 */ 2537 if (ifp != NULL) 2538 IF_ADDR_LOCK(ifp); 2539 2540 lastref = if_delmulti_locked(ifp, ifma, 0); 2541 2542 if (ifp != NULL) { 2543 /* 2544 * If and only if the ifnet instance exists: 2545 * Release the address lock. 2546 * If the group was left: update the hardware hash filter. 2547 */ 2548 IF_ADDR_UNLOCK(ifp); 2549 if (lastref && ifp->if_ioctl != NULL) { 2550 IFF_LOCKGIANT(ifp); 2551 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2552 IFF_UNLOCKGIANT(ifp); 2553 } 2554 } 2555} 2556 2557/* 2558 * Perform deletion of network-layer and/or link-layer multicast address. 2559 * 2560 * Return 0 if the reference count was decremented. 2561 * Return 1 if the final reference was released, indicating that the 2562 * hardware hash filter should be reprogrammed. 2563 */ 2564static int 2565if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 2566{ 2567 struct ifmultiaddr *ll_ifma; 2568 2569 if (ifp != NULL && ifma->ifma_ifp != NULL) { 2570 KASSERT(ifma->ifma_ifp == ifp, 2571 ("%s: inconsistent ifp %p", __func__, ifp)); 2572 IF_ADDR_LOCK_ASSERT(ifp); 2573 } 2574 2575 ifp = ifma->ifma_ifp; 2576 2577 /* 2578 * If the ifnet is detaching, null out references to ifnet, 2579 * so that upper protocol layers will notice, and not attempt 2580 * to obtain locks for an ifnet which no longer exists. The 2581 * routing socket announcement must happen before the ifnet 2582 * instance is detached from the system. 2583 */ 2584 if (detaching) { 2585#ifdef DIAGNOSTIC 2586 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 2587#endif 2588 /* 2589 * ifp may already be nulled out if we are being reentered 2590 * to delete the ll_ifma. 2591 */ 2592 if (ifp != NULL) { 2593 rt_newmaddrmsg(RTM_DELMADDR, ifma); 2594 ifma->ifma_ifp = NULL; 2595 } 2596 } 2597 2598 if (--ifma->ifma_refcount > 0) 2599 return 0; 2600 2601 /* 2602 * If this ifma is a network-layer ifma, a link-layer ifma may 2603 * have been associated with it. Release it first if so. 2604 */ 2605 ll_ifma = ifma->ifma_llifma; 2606 if (ll_ifma != NULL) { 2607 KASSERT(ifma->ifma_lladdr != NULL, 2608 ("%s: llifma w/o lladdr", __func__)); 2609 if (detaching) 2610 ll_ifma->ifma_ifp = NULL; /* XXX */ 2611 if (--ll_ifma->ifma_refcount == 0) { 2612 if (ifp != NULL) { 2613 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 2614 ifma_link); 2615 } 2616 if_freemulti(ll_ifma); 2617 } 2618 } 2619 2620 if (ifp != NULL) 2621 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2622 2623 if_freemulti(ifma); 2624 2625 /* 2626 * The last reference to this instance of struct ifmultiaddr 2627 * was released; the hardware should be notified of this change. 2628 */ 2629 return 1; 2630} 2631 2632/* 2633 * Set the link layer address on an interface. 2634 * 2635 * At this time we only support certain types of interfaces, 2636 * and we don't allow the length of the address to change. 2637 */ 2638int 2639if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 2640{ 2641 struct sockaddr_dl *sdl; 2642 struct ifaddr *ifa; 2643 struct ifreq ifr; 2644 2645 ifa = ifp->if_addr; 2646 if (ifa == NULL) 2647 return (EINVAL); 2648 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2649 if (sdl == NULL) 2650 return (EINVAL); 2651 if (len != sdl->sdl_alen) /* don't allow length to change */ 2652 return (EINVAL); 2653 switch (ifp->if_type) { 2654 case IFT_ETHER: 2655 case IFT_FDDI: 2656 case IFT_XETHER: 2657 case IFT_ISO88025: 2658 case IFT_L2VLAN: 2659 case IFT_BRIDGE: 2660 case IFT_ARCNET: 2661 case IFT_IEEE8023ADLAG: 2662 bcopy(lladdr, LLADDR(sdl), len); 2663 break; 2664 default: 2665 return (ENODEV); 2666 } 2667 /* 2668 * If the interface is already up, we need 2669 * to re-init it in order to reprogram its 2670 * address filter. 2671 */ 2672 if ((ifp->if_flags & IFF_UP) != 0) { 2673 if (ifp->if_ioctl) { 2674 IFF_LOCKGIANT(ifp); 2675 ifp->if_flags &= ~IFF_UP; 2676 ifr.ifr_flags = ifp->if_flags & 0xffff; 2677 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2678 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2679 ifp->if_flags |= IFF_UP; 2680 ifr.ifr_flags = ifp->if_flags & 0xffff; 2681 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2682 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2683 IFF_UNLOCKGIANT(ifp); 2684 } 2685#ifdef INET 2686 /* 2687 * Also send gratuitous ARPs to notify other nodes about 2688 * the address change. 2689 */ 2690 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2691 if (ifa->ifa_addr->sa_family == AF_INET) 2692 arp_ifinit(ifp, ifa); 2693 } 2694#endif 2695 } 2696 return (0); 2697} 2698 2699/* 2700 * The name argument must be a pointer to storage which will last as 2701 * long as the interface does. For physical devices, the result of 2702 * device_get_name(dev) is a good choice and for pseudo-devices a 2703 * static string works well. 2704 */ 2705void 2706if_initname(struct ifnet *ifp, const char *name, int unit) 2707{ 2708 ifp->if_dname = name; 2709 ifp->if_dunit = unit; 2710 if (unit != IF_DUNIT_NONE) 2711 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 2712 else 2713 strlcpy(ifp->if_xname, name, IFNAMSIZ); 2714} 2715 2716int 2717if_printf(struct ifnet *ifp, const char * fmt, ...) 2718{ 2719 va_list ap; 2720 int retval; 2721 2722 retval = printf("%s: ", ifp->if_xname); 2723 va_start(ap, fmt); 2724 retval += vprintf(fmt, ap); 2725 va_end(ap); 2726 return (retval); 2727} 2728 2729/* 2730 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot 2731 * be called without Giant. However, we often can't acquire the Giant lock 2732 * at those points; instead, we run it via a task queue that holds Giant via 2733 * if_start_deferred. 2734 * 2735 * XXXRW: We need to make sure that the ifnet isn't fully detached until any 2736 * outstanding if_start_deferred() tasks that will run after the free. This 2737 * probably means waiting in if_detach(). 2738 */ 2739void 2740if_start(struct ifnet *ifp) 2741{ 2742 2743 if (ifp->if_flags & IFF_NEEDSGIANT) { 2744 if (mtx_owned(&Giant)) 2745 (*(ifp)->if_start)(ifp); 2746 else 2747 taskqueue_enqueue(taskqueue_swi_giant, 2748 &ifp->if_starttask); 2749 } else 2750 (*(ifp)->if_start)(ifp); 2751} 2752 2753static void 2754if_start_deferred(void *context, int pending) 2755{ 2756 struct ifnet *ifp; 2757 2758 GIANT_REQUIRED; 2759 2760 ifp = context; 2761 (ifp->if_start)(ifp); 2762} 2763 2764int 2765if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 2766{ 2767 int active = 0; 2768 2769 IF_LOCK(ifq); 2770 if (_IF_QFULL(ifq)) { 2771 _IF_DROP(ifq); 2772 IF_UNLOCK(ifq); 2773 m_freem(m); 2774 return (0); 2775 } 2776 if (ifp != NULL) { 2777 ifp->if_obytes += m->m_pkthdr.len + adjust; 2778 if (m->m_flags & (M_BCAST|M_MCAST)) 2779 ifp->if_omcasts++; 2780 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 2781 } 2782 _IF_ENQUEUE(ifq, m); 2783 IF_UNLOCK(ifq); 2784 if (ifp != NULL && !active) 2785 if_start(ifp); 2786 return (1); 2787} 2788 2789void 2790if_register_com_alloc(u_char type, 2791 if_com_alloc_t *a, if_com_free_t *f) 2792{ 2793 2794 KASSERT(if_com_alloc[type] == NULL, 2795 ("if_register_com_alloc: %d already registered", type)); 2796 KASSERT(if_com_free[type] == NULL, 2797 ("if_register_com_alloc: %d free already registered", type)); 2798 2799 if_com_alloc[type] = a; 2800 if_com_free[type] = f; 2801} 2802 2803void 2804if_deregister_com_alloc(u_char type) 2805{ 2806 2807 KASSERT(if_com_alloc[type] != NULL, 2808 ("if_deregister_com_alloc: %d not registered", type)); 2809 KASSERT(if_com_free[type] != NULL, 2810 ("if_deregister_com_alloc: %d free not registered", type)); 2811 if_com_alloc[type] = NULL; 2812 if_com_free[type] = NULL; 2813}
| 518 net_cdevsw.d_name, ifp->if_xname)); 519 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", 520 net_cdevsw.d_name, ifp->if_index); 521 522 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 523 524 /* 525 * create a Link Level name for this device 526 */ 527 namelen = strlen(ifp->if_xname); 528 /* 529 * Always save enough space for any possiable name so we can do 530 * a rename in place later. 531 */ 532 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 533 socksize = masklen + ifp->if_addrlen; 534 if (socksize < sizeof(*sdl)) 535 socksize = sizeof(*sdl); 536 socksize = roundup2(socksize, sizeof(long)); 537 ifasize = sizeof(*ifa) + 2 * socksize; 538 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 539 IFA_LOCK_INIT(ifa); 540 sdl = (struct sockaddr_dl *)(ifa + 1); 541 sdl->sdl_len = socksize; 542 sdl->sdl_family = AF_LINK; 543 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 544 sdl->sdl_nlen = namelen; 545 sdl->sdl_index = ifp->if_index; 546 sdl->sdl_type = ifp->if_type; 547 ifp->if_addr = ifa; 548 ifa->ifa_ifp = ifp; 549 ifa->ifa_rtrequest = link_rtrequest; 550 ifa->ifa_addr = (struct sockaddr *)sdl; 551 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 552 ifa->ifa_netmask = (struct sockaddr *)sdl; 553 sdl->sdl_len = masklen; 554 while (namelen != 0) 555 sdl->sdl_data[--namelen] = 0xff; 556 ifa->ifa_refcnt = 1; 557 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 558 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */ 559 560 /* 561 * XXX: why do we warn about this? We're correcting it and most 562 * drivers just set the value the way we do. 563 */ 564 if (ifp->if_snd.ifq_maxlen == 0) { 565 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n"); 566 ifp->if_snd.ifq_maxlen = ifqmaxlen; 567 } 568 ifp->if_snd.altq_type = 0; 569 ifp->if_snd.altq_disc = NULL; 570 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 571 ifp->if_snd.altq_tbr = NULL; 572 ifp->if_snd.altq_ifp = ifp; 573 574 IFNET_WLOCK(); 575 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 576 IFNET_WUNLOCK(); 577 578 if (domain_init_status >= 2) 579 if_attachdomain1(ifp); 580 581 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 582 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 583 584 /* Announce the interface. */ 585 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 586 587 if (ifp->if_watchdog != NULL) 588 if_printf(ifp, 589 "WARNING: using obsoleted if_watchdog interface\n"); 590 if (ifp->if_flags & IFF_NEEDSGIANT) 591 if_printf(ifp, 592 "WARNING: using obsoleted IFF_NEEDSGIANT flag\n"); 593} 594 595static void 596if_attachdomain(void *dummy) 597{ 598 struct ifnet *ifp; 599 int s; 600 601 s = splnet(); 602 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 603 if_attachdomain1(ifp); 604 splx(s); 605} 606SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 607 if_attachdomain, NULL); 608 609static void 610if_attachdomain1(struct ifnet *ifp) 611{ 612 struct domain *dp; 613 int s; 614 615 s = splnet(); 616 617 /* 618 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 619 * cannot lock ifp->if_afdata initialization, entirely. 620 */ 621 if (IF_AFDATA_TRYLOCK(ifp) == 0) { 622 splx(s); 623 return; 624 } 625 if (ifp->if_afdata_initialized >= domain_init_status) { 626 IF_AFDATA_UNLOCK(ifp); 627 splx(s); 628 printf("if_attachdomain called more than once on %s\n", 629 ifp->if_xname); 630 return; 631 } 632 ifp->if_afdata_initialized = domain_init_status; 633 IF_AFDATA_UNLOCK(ifp); 634 635 /* address family dependent data region */ 636 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 637 for (dp = domains; dp; dp = dp->dom_next) { 638 if (dp->dom_ifattach) 639 ifp->if_afdata[dp->dom_family] = 640 (*dp->dom_ifattach)(ifp); 641 } 642 643 splx(s); 644} 645 646/* 647 * Remove any unicast or broadcast network addresses from an interface. 648 */ 649void 650if_purgeaddrs(struct ifnet *ifp) 651{ 652 struct ifaddr *ifa, *next; 653 654 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 655 if (ifa->ifa_addr->sa_family == AF_LINK) 656 continue; 657#ifdef INET 658 /* XXX: Ugly!! ad hoc just for INET */ 659 if (ifa->ifa_addr->sa_family == AF_INET) { 660 struct ifaliasreq ifr; 661 662 bzero(&ifr, sizeof(ifr)); 663 ifr.ifra_addr = *ifa->ifa_addr; 664 if (ifa->ifa_dstaddr) 665 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 666 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 667 NULL) == 0) 668 continue; 669 } 670#endif /* INET */ 671#ifdef INET6 672 if (ifa->ifa_addr->sa_family == AF_INET6) { 673 in6_purgeaddr(ifa); 674 /* ifp_addrhead is already updated */ 675 continue; 676 } 677#endif /* INET6 */ 678 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 679 IFAFREE(ifa); 680 } 681} 682 683/* 684 * Remove any multicast network addresses from an interface. 685 */ 686void 687if_purgemaddrs(struct ifnet *ifp) 688{ 689 struct ifmultiaddr *ifma; 690 struct ifmultiaddr *next; 691 692 IF_ADDR_LOCK(ifp); 693 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 694 if_delmulti_locked(ifp, ifma, 1); 695 IF_ADDR_UNLOCK(ifp); 696} 697 698/* 699 * Detach an interface, removing it from the 700 * list of "active" interfaces. 701 * 702 * XXXRW: There are some significant questions about event ordering, and 703 * how to prevent things from starting to use the interface during detach. 704 */ 705void 706if_detach(struct ifnet *ifp) 707{ 708 struct ifaddr *ifa; 709 struct radix_node_head *rnh; 710 int s; 711 int i; 712 struct domain *dp; 713 struct ifnet *iter; 714 int found = 0; 715 716 IFNET_WLOCK(); 717 TAILQ_FOREACH(iter, &V_ifnet, if_link) 718 if (iter == ifp) { 719 TAILQ_REMOVE(&V_ifnet, ifp, if_link); 720 found = 1; 721 break; 722 } 723 IFNET_WUNLOCK(); 724 if (!found) 725 return; 726 727 /* 728 * Remove/wait for pending events. 729 */ 730 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 731 732 /* 733 * Remove routes and flush queues. 734 */ 735 s = splnet(); 736 if_down(ifp); 737#ifdef ALTQ 738 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 739 altq_disable(&ifp->if_snd); 740 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 741 altq_detach(&ifp->if_snd); 742#endif 743 744 if_purgeaddrs(ifp); 745 746#ifdef INET 747 in_ifdetach(ifp); 748#endif 749 750#ifdef INET6 751 /* 752 * Remove all IPv6 kernel structs related to ifp. This should be done 753 * before removing routing entries below, since IPv6 interface direct 754 * routes are expected to be removed by the IPv6-specific kernel API. 755 * Otherwise, the kernel will detect some inconsistency and bark it. 756 */ 757 in6_ifdetach(ifp); 758#endif 759 if_purgemaddrs(ifp); 760 761 /* 762 * Remove link ifaddr pointer and maybe decrement if_index. 763 * Clean up all addresses. 764 */ 765 ifp->if_addr = NULL; 766 destroy_dev(ifdev_byindex(ifp->if_index)); 767 ifdev_setbyindex(ifp->if_index, NULL); 768 769 /* We can now free link ifaddr. */ 770 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 771 ifa = TAILQ_FIRST(&ifp->if_addrhead); 772 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 773 IFAFREE(ifa); 774 } 775 776 /* 777 * Delete all remaining routes using this interface 778 * Unfortuneatly the only way to do this is to slog through 779 * the entire routing table looking for routes which point 780 * to this interface...oh well... 781 */ 782 for (i = 1; i <= AF_MAX; i++) { 783 int j; 784 for (j = 0; j < rt_numfibs; j++) { 785 if ((rnh = V_rt_tables[j][i]) == NULL) 786 continue; 787 RADIX_NODE_HEAD_LOCK(rnh); 788 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 789 RADIX_NODE_HEAD_UNLOCK(rnh); 790 } 791 } 792 793 /* Announce that the interface is gone. */ 794 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 795 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 796 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 797 798 IF_AFDATA_LOCK(ifp); 799 for (dp = domains; dp; dp = dp->dom_next) { 800 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 801 (*dp->dom_ifdetach)(ifp, 802 ifp->if_afdata[dp->dom_family]); 803 } 804 IF_AFDATA_UNLOCK(ifp); 805 806#ifdef MAC 807 mac_ifnet_destroy(ifp); 808#endif /* MAC */ 809 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT); 810 knlist_clear(&ifp->if_klist, 0); 811 knlist_destroy(&ifp->if_klist); 812 mtx_destroy(&ifp->if_snd.ifq_mtx); 813 IF_AFDATA_DESTROY(ifp); 814 splx(s); 815} 816 817/* 818 * Add a group to an interface 819 */ 820int 821if_addgroup(struct ifnet *ifp, const char *groupname) 822{ 823 struct ifg_list *ifgl; 824 struct ifg_group *ifg = NULL; 825 struct ifg_member *ifgm; 826 827 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 828 groupname[strlen(groupname) - 1] <= '9') 829 return (EINVAL); 830 831 IFNET_WLOCK(); 832 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 833 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 834 IFNET_WUNLOCK(); 835 return (EEXIST); 836 } 837 838 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 839 M_NOWAIT)) == NULL) { 840 IFNET_WUNLOCK(); 841 return (ENOMEM); 842 } 843 844 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 845 M_TEMP, M_NOWAIT)) == NULL) { 846 free(ifgl, M_TEMP); 847 IFNET_WUNLOCK(); 848 return (ENOMEM); 849 } 850 851 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 852 if (!strcmp(ifg->ifg_group, groupname)) 853 break; 854 855 if (ifg == NULL) { 856 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 857 M_TEMP, M_NOWAIT)) == NULL) { 858 free(ifgl, M_TEMP); 859 free(ifgm, M_TEMP); 860 IFNET_WUNLOCK(); 861 return (ENOMEM); 862 } 863 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 864 ifg->ifg_refcnt = 0; 865 TAILQ_INIT(&ifg->ifg_members); 866 EVENTHANDLER_INVOKE(group_attach_event, ifg); 867 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 868 } 869 870 ifg->ifg_refcnt++; 871 ifgl->ifgl_group = ifg; 872 ifgm->ifgm_ifp = ifp; 873 874 IF_ADDR_LOCK(ifp); 875 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 876 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 877 IF_ADDR_UNLOCK(ifp); 878 879 IFNET_WUNLOCK(); 880 881 EVENTHANDLER_INVOKE(group_change_event, groupname); 882 883 return (0); 884} 885 886/* 887 * Remove a group from an interface 888 */ 889int 890if_delgroup(struct ifnet *ifp, const char *groupname) 891{ 892 struct ifg_list *ifgl; 893 struct ifg_member *ifgm; 894 895 IFNET_WLOCK(); 896 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 897 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 898 break; 899 if (ifgl == NULL) { 900 IFNET_WUNLOCK(); 901 return (ENOENT); 902 } 903 904 IF_ADDR_LOCK(ifp); 905 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 906 IF_ADDR_UNLOCK(ifp); 907 908 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 909 if (ifgm->ifgm_ifp == ifp) 910 break; 911 912 if (ifgm != NULL) { 913 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 914 free(ifgm, M_TEMP); 915 } 916 917 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 918 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 919 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 920 free(ifgl->ifgl_group, M_TEMP); 921 } 922 IFNET_WUNLOCK(); 923 924 free(ifgl, M_TEMP); 925 926 EVENTHANDLER_INVOKE(group_change_event, groupname); 927 928 return (0); 929} 930 931/* 932 * Stores all groups from an interface in memory pointed 933 * to by data 934 */ 935static int 936if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 937{ 938 int len, error; 939 struct ifg_list *ifgl; 940 struct ifg_req ifgrq, *ifgp; 941 struct ifgroupreq *ifgr = data; 942 943 if (ifgr->ifgr_len == 0) { 944 IF_ADDR_LOCK(ifp); 945 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 946 ifgr->ifgr_len += sizeof(struct ifg_req); 947 IF_ADDR_UNLOCK(ifp); 948 return (0); 949 } 950 951 len = ifgr->ifgr_len; 952 ifgp = ifgr->ifgr_groups; 953 /* XXX: wire */ 954 IF_ADDR_LOCK(ifp); 955 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 956 if (len < sizeof(ifgrq)) { 957 IF_ADDR_UNLOCK(ifp); 958 return (EINVAL); 959 } 960 bzero(&ifgrq, sizeof ifgrq); 961 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 962 sizeof(ifgrq.ifgrq_group)); 963 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 964 IF_ADDR_UNLOCK(ifp); 965 return (error); 966 } 967 len -= sizeof(ifgrq); 968 ifgp++; 969 } 970 IF_ADDR_UNLOCK(ifp); 971 972 return (0); 973} 974 975/* 976 * Stores all members of a group in memory pointed to by data 977 */ 978static int 979if_getgroupmembers(struct ifgroupreq *data) 980{ 981 struct ifgroupreq *ifgr = data; 982 struct ifg_group *ifg; 983 struct ifg_member *ifgm; 984 struct ifg_req ifgrq, *ifgp; 985 int len, error; 986 987 IFNET_RLOCK(); 988 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 989 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 990 break; 991 if (ifg == NULL) { 992 IFNET_RUNLOCK(); 993 return (ENOENT); 994 } 995 996 if (ifgr->ifgr_len == 0) { 997 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 998 ifgr->ifgr_len += sizeof(ifgrq); 999 IFNET_RUNLOCK(); 1000 return (0); 1001 } 1002 1003 len = ifgr->ifgr_len; 1004 ifgp = ifgr->ifgr_groups; 1005 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1006 if (len < sizeof(ifgrq)) { 1007 IFNET_RUNLOCK(); 1008 return (EINVAL); 1009 } 1010 bzero(&ifgrq, sizeof ifgrq); 1011 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1012 sizeof(ifgrq.ifgrq_member)); 1013 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1014 IFNET_RUNLOCK(); 1015 return (error); 1016 } 1017 len -= sizeof(ifgrq); 1018 ifgp++; 1019 } 1020 IFNET_RUNLOCK(); 1021 1022 return (0); 1023} 1024 1025/* 1026 * Delete Routes for a Network Interface 1027 * 1028 * Called for each routing entry via the rnh->rnh_walktree() call above 1029 * to delete all route entries referencing a detaching network interface. 1030 * 1031 * Arguments: 1032 * rn pointer to node in the routing table 1033 * arg argument passed to rnh->rnh_walktree() - detaching interface 1034 * 1035 * Returns: 1036 * 0 successful 1037 * errno failed - reason indicated 1038 * 1039 */ 1040static int 1041if_rtdel(struct radix_node *rn, void *arg) 1042{ 1043 struct rtentry *rt = (struct rtentry *)rn; 1044 struct ifnet *ifp = arg; 1045 int err; 1046 1047 if (rt->rt_ifp == ifp) { 1048 1049 /* 1050 * Protect (sorta) against walktree recursion problems 1051 * with cloned routes 1052 */ 1053 if ((rt->rt_flags & RTF_UP) == 0) 1054 return (0); 1055 1056 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1057 rt_mask(rt), rt->rt_flags, 1058 (struct rtentry **) NULL, rt->rt_fibnum); 1059 if (err) { 1060 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1061 } 1062 } 1063 1064 return (0); 1065} 1066 1067/* 1068 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1069 * structs used to represent other address families, it is necessary 1070 * to perform a different comparison. 1071 */ 1072 1073#define sa_equal(a1, a2) \ 1074 (bcmp((a1), (a2), ((a1))->sa_len) == 0) 1075 1076#define sa_dl_equal(a1, a2) \ 1077 ((((struct sockaddr_dl *)(a1))->sdl_len == \ 1078 ((struct sockaddr_dl *)(a2))->sdl_len) && \ 1079 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 1080 LLADDR((struct sockaddr_dl *)(a2)), \ 1081 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1082 1083/* 1084 * Locate an interface based on a complete address. 1085 */ 1086/*ARGSUSED*/ 1087struct ifaddr * 1088ifa_ifwithaddr(struct sockaddr *addr) 1089{ 1090 struct ifnet *ifp; 1091 struct ifaddr *ifa; 1092 1093 IFNET_RLOCK(); 1094 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1095 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1096 if (ifa->ifa_addr->sa_family != addr->sa_family) 1097 continue; 1098 if (sa_equal(addr, ifa->ifa_addr)) 1099 goto done; 1100 /* IP6 doesn't have broadcast */ 1101 if ((ifp->if_flags & IFF_BROADCAST) && 1102 ifa->ifa_broadaddr && 1103 ifa->ifa_broadaddr->sa_len != 0 && 1104 sa_equal(ifa->ifa_broadaddr, addr)) 1105 goto done; 1106 } 1107 ifa = NULL; 1108done: 1109 IFNET_RUNLOCK(); 1110 return (ifa); 1111} 1112 1113/* 1114 * Locate an interface based on the broadcast address. 1115 */ 1116/* ARGSUSED */ 1117struct ifaddr * 1118ifa_ifwithbroadaddr(struct sockaddr *addr) 1119{ 1120 struct ifnet *ifp; 1121 struct ifaddr *ifa; 1122 1123 IFNET_RLOCK(); 1124 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1125 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1126 if (ifa->ifa_addr->sa_family != addr->sa_family) 1127 continue; 1128 if ((ifp->if_flags & IFF_BROADCAST) && 1129 ifa->ifa_broadaddr && 1130 ifa->ifa_broadaddr->sa_len != 0 && 1131 sa_equal(ifa->ifa_broadaddr, addr)) 1132 goto done; 1133 } 1134 ifa = NULL; 1135done: 1136 IFNET_RUNLOCK(); 1137 return (ifa); 1138} 1139 1140/* 1141 * Locate the point to point interface with a given destination address. 1142 */ 1143/*ARGSUSED*/ 1144struct ifaddr * 1145ifa_ifwithdstaddr(struct sockaddr *addr) 1146{ 1147 struct ifnet *ifp; 1148 struct ifaddr *ifa; 1149 1150 IFNET_RLOCK(); 1151 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1152 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1153 continue; 1154 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1155 if (ifa->ifa_addr->sa_family != addr->sa_family) 1156 continue; 1157 if (ifa->ifa_dstaddr != NULL && 1158 sa_equal(addr, ifa->ifa_dstaddr)) 1159 goto done; 1160 } 1161 } 1162 ifa = NULL; 1163done: 1164 IFNET_RUNLOCK(); 1165 return (ifa); 1166} 1167 1168/* 1169 * Find an interface on a specific network. If many, choice 1170 * is most specific found. 1171 */ 1172struct ifaddr * 1173ifa_ifwithnet(struct sockaddr *addr) 1174{ 1175 struct ifnet *ifp; 1176 struct ifaddr *ifa; 1177 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 1178 u_int af = addr->sa_family; 1179 char *addr_data = addr->sa_data, *cplim; 1180 1181 /* 1182 * AF_LINK addresses can be looked up directly by their index number, 1183 * so do that if we can. 1184 */ 1185 if (af == AF_LINK) { 1186 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1187 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1188 return (ifaddr_byindex(sdl->sdl_index)); 1189 } 1190 1191 /* 1192 * Scan though each interface, looking for ones that have 1193 * addresses in this address family. 1194 */ 1195 IFNET_RLOCK(); 1196 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1197 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1198 char *cp, *cp2, *cp3; 1199 1200 if (ifa->ifa_addr->sa_family != af) 1201next: continue; 1202 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 1203 /* 1204 * This is a bit broken as it doesn't 1205 * take into account that the remote end may 1206 * be a single node in the network we are 1207 * looking for. 1208 * The trouble is that we don't know the 1209 * netmask for the remote end. 1210 */ 1211 if (ifa->ifa_dstaddr != NULL && 1212 sa_equal(addr, ifa->ifa_dstaddr)) 1213 goto done; 1214 } else { 1215 /* 1216 * if we have a special address handler, 1217 * then use it instead of the generic one. 1218 */ 1219 if (ifa->ifa_claim_addr) { 1220 if ((*ifa->ifa_claim_addr)(ifa, addr)) 1221 goto done; 1222 continue; 1223 } 1224 1225 /* 1226 * Scan all the bits in the ifa's address. 1227 * If a bit dissagrees with what we are 1228 * looking for, mask it with the netmask 1229 * to see if it really matters. 1230 * (A byte at a time) 1231 */ 1232 if (ifa->ifa_netmask == 0) 1233 continue; 1234 cp = addr_data; 1235 cp2 = ifa->ifa_addr->sa_data; 1236 cp3 = ifa->ifa_netmask->sa_data; 1237 cplim = ifa->ifa_netmask->sa_len 1238 + (char *)ifa->ifa_netmask; 1239 while (cp3 < cplim) 1240 if ((*cp++ ^ *cp2++) & *cp3++) 1241 goto next; /* next address! */ 1242 /* 1243 * If the netmask of what we just found 1244 * is more specific than what we had before 1245 * (if we had one) then remember the new one 1246 * before continuing to search 1247 * for an even better one. 1248 */ 1249 if (ifa_maybe == 0 || 1250 rn_refines((caddr_t)ifa->ifa_netmask, 1251 (caddr_t)ifa_maybe->ifa_netmask)) 1252 ifa_maybe = ifa; 1253 } 1254 } 1255 } 1256 ifa = ifa_maybe; 1257done: 1258 IFNET_RUNLOCK(); 1259 return (ifa); 1260} 1261 1262/* 1263 * Find an interface address specific to an interface best matching 1264 * a given address. 1265 */ 1266struct ifaddr * 1267ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1268{ 1269 struct ifaddr *ifa; 1270 char *cp, *cp2, *cp3; 1271 char *cplim; 1272 struct ifaddr *ifa_maybe = 0; 1273 u_int af = addr->sa_family; 1274 1275 if (af >= AF_MAX) 1276 return (0); 1277 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1278 if (ifa->ifa_addr->sa_family != af) 1279 continue; 1280 if (ifa_maybe == 0) 1281 ifa_maybe = ifa; 1282 if (ifa->ifa_netmask == 0) { 1283 if (sa_equal(addr, ifa->ifa_addr) || 1284 (ifa->ifa_dstaddr && 1285 sa_equal(addr, ifa->ifa_dstaddr))) 1286 goto done; 1287 continue; 1288 } 1289 if (ifp->if_flags & IFF_POINTOPOINT) { 1290 if (sa_equal(addr, ifa->ifa_dstaddr)) 1291 goto done; 1292 } else { 1293 cp = addr->sa_data; 1294 cp2 = ifa->ifa_addr->sa_data; 1295 cp3 = ifa->ifa_netmask->sa_data; 1296 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1297 for (; cp3 < cplim; cp3++) 1298 if ((*cp++ ^ *cp2++) & *cp3) 1299 break; 1300 if (cp3 == cplim) 1301 goto done; 1302 } 1303 } 1304 ifa = ifa_maybe; 1305done: 1306 return (ifa); 1307} 1308 1309#include <net/route.h> 1310 1311/* 1312 * Default action when installing a route with a Link Level gateway. 1313 * Lookup an appropriate real ifa to point to. 1314 * This should be moved to /sys/net/link.c eventually. 1315 */ 1316static void 1317link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1318{ 1319 struct ifaddr *ifa, *oifa; 1320 struct sockaddr *dst; 1321 struct ifnet *ifp; 1322 1323 RT_LOCK_ASSERT(rt); 1324 1325 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1326 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1327 return; 1328 ifa = ifaof_ifpforaddr(dst, ifp); 1329 if (ifa) { 1330 IFAREF(ifa); /* XXX */ 1331 oifa = rt->rt_ifa; 1332 rt->rt_ifa = ifa; 1333 IFAFREE(oifa); 1334 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1335 ifa->ifa_rtrequest(cmd, rt, info); 1336 } 1337} 1338 1339/* 1340 * Mark an interface down and notify protocols of 1341 * the transition. 1342 * NOTE: must be called at splnet or eqivalent. 1343 */ 1344static void 1345if_unroute(struct ifnet *ifp, int flag, int fam) 1346{ 1347 struct ifaddr *ifa; 1348 1349 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1350 1351 ifp->if_flags &= ~flag; 1352 getmicrotime(&ifp->if_lastchange); 1353 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1354 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1355 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1356 if_qflush(&ifp->if_snd); 1357#ifdef DEV_CARP 1358 if (ifp->if_carp) 1359 carp_carpdev_state(ifp->if_carp); 1360#endif 1361 rt_ifmsg(ifp); 1362} 1363 1364/* 1365 * Mark an interface up and notify protocols of 1366 * the transition. 1367 * NOTE: must be called at splnet or eqivalent. 1368 */ 1369static void 1370if_route(struct ifnet *ifp, int flag, int fam) 1371{ 1372 struct ifaddr *ifa; 1373 1374 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1375 1376 ifp->if_flags |= flag; 1377 getmicrotime(&ifp->if_lastchange); 1378 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1379 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1380 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1381#ifdef DEV_CARP 1382 if (ifp->if_carp) 1383 carp_carpdev_state(ifp->if_carp); 1384#endif 1385 rt_ifmsg(ifp); 1386#ifdef INET6 1387 in6_if_up(ifp); 1388#endif 1389} 1390 1391void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 1392void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1393 1394/* 1395 * Handle a change in the interface link state. To avoid LORs 1396 * between driver lock and upper layer locks, as well as possible 1397 * recursions, we post event to taskqueue, and all job 1398 * is done in static do_link_state_change(). 1399 */ 1400void 1401if_link_state_change(struct ifnet *ifp, int link_state) 1402{ 1403 /* Return if state hasn't changed. */ 1404 if (ifp->if_link_state == link_state) 1405 return; 1406 1407 ifp->if_link_state = link_state; 1408 1409 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 1410} 1411 1412static void 1413do_link_state_change(void *arg, int pending) 1414{ 1415 struct ifnet *ifp = (struct ifnet *)arg; 1416 int link_state = ifp->if_link_state; 1417 int link; 1418 1419 /* Notify that the link state has changed. */ 1420 rt_ifmsg(ifp); 1421 if (link_state == LINK_STATE_UP) 1422 link = NOTE_LINKUP; 1423 else if (link_state == LINK_STATE_DOWN) 1424 link = NOTE_LINKDOWN; 1425 else 1426 link = NOTE_LINKINV; 1427 KNOTE_UNLOCKED(&ifp->if_klist, link); 1428 if (ifp->if_vlantrunk != NULL) 1429 (*vlan_link_state_p)(ifp, link); 1430 1431 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 1432 IFP2AC(ifp)->ac_netgraph != NULL) 1433 (*ng_ether_link_state_p)(ifp, link_state); 1434#ifdef DEV_CARP 1435 if (ifp->if_carp) 1436 carp_carpdev_state(ifp->if_carp); 1437#endif 1438 if (ifp->if_bridge) { 1439 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 1440 (*bstp_linkstate_p)(ifp, link_state); 1441 } 1442 if (ifp->if_lagg) { 1443 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!")); 1444 (*lagg_linkstate_p)(ifp, link_state); 1445 } 1446 1447 devctl_notify("IFNET", ifp->if_xname, 1448 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); 1449 if (pending > 1) 1450 if_printf(ifp, "%d link states coalesced\n", pending); 1451 if (log_link_state_change) 1452 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 1453 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1454} 1455 1456/* 1457 * Mark an interface down and notify protocols of 1458 * the transition. 1459 * NOTE: must be called at splnet or eqivalent. 1460 */ 1461void 1462if_down(struct ifnet *ifp) 1463{ 1464 1465 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1466} 1467 1468/* 1469 * Mark an interface up and notify protocols of 1470 * the transition. 1471 * NOTE: must be called at splnet or eqivalent. 1472 */ 1473void 1474if_up(struct ifnet *ifp) 1475{ 1476 1477 if_route(ifp, IFF_UP, AF_UNSPEC); 1478} 1479 1480/* 1481 * Flush an interface queue. 1482 */ 1483static void 1484if_qflush(struct ifaltq *ifq) 1485{ 1486 struct mbuf *m, *n; 1487 1488 IFQ_LOCK(ifq); 1489#ifdef ALTQ 1490 if (ALTQ_IS_ENABLED(ifq)) 1491 ALTQ_PURGE(ifq); 1492#endif 1493 n = ifq->ifq_head; 1494 while ((m = n) != 0) { 1495 n = m->m_act; 1496 m_freem(m); 1497 } 1498 ifq->ifq_head = 0; 1499 ifq->ifq_tail = 0; 1500 ifq->ifq_len = 0; 1501 IFQ_UNLOCK(ifq); 1502} 1503 1504/* 1505 * Handle interface watchdog timer routines. Called 1506 * from softclock, we decrement timers (if set) and 1507 * call the appropriate interface routine on expiration. 1508 * 1509 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1510 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab 1511 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface. 1512 */ 1513static void 1514if_slowtimo(void *arg) 1515{ 1516 struct ifnet *ifp; 1517 int s = splimp(); 1518 1519 IFNET_RLOCK(); 1520 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1521 if (ifp->if_timer == 0 || --ifp->if_timer) 1522 continue; 1523 if (ifp->if_watchdog) 1524 (*ifp->if_watchdog)(ifp); 1525 } 1526 IFNET_RUNLOCK(); 1527 splx(s); 1528 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1529} 1530 1531/* 1532 * Map interface name to 1533 * interface structure pointer. 1534 */ 1535struct ifnet * 1536ifunit(const char *name) 1537{ 1538 struct ifnet *ifp; 1539 1540 IFNET_RLOCK(); 1541 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1542 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1543 break; 1544 } 1545 IFNET_RUNLOCK(); 1546 return (ifp); 1547} 1548 1549/* 1550 * Hardware specific interface ioctls. 1551 */ 1552static int 1553ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1554{ 1555 struct ifreq *ifr; 1556 struct ifstat *ifs; 1557 int error = 0; 1558 int new_flags, temp_flags; 1559 size_t namelen, onamelen; 1560 char new_name[IFNAMSIZ]; 1561 struct ifaddr *ifa; 1562 struct sockaddr_dl *sdl; 1563 1564 ifr = (struct ifreq *)data; 1565 switch (cmd) { 1566 case SIOCGIFINDEX: 1567 ifr->ifr_index = ifp->if_index; 1568 break; 1569 1570 case SIOCGIFFLAGS: 1571 temp_flags = ifp->if_flags | ifp->if_drv_flags; 1572 ifr->ifr_flags = temp_flags & 0xffff; 1573 ifr->ifr_flagshigh = temp_flags >> 16; 1574 break; 1575 1576 case SIOCGIFCAP: 1577 ifr->ifr_reqcap = ifp->if_capabilities; 1578 ifr->ifr_curcap = ifp->if_capenable; 1579 break; 1580 1581#ifdef MAC 1582 case SIOCGIFMAC: 1583 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 1584 break; 1585#endif 1586 1587 case SIOCGIFMETRIC: 1588 ifr->ifr_metric = ifp->if_metric; 1589 break; 1590 1591 case SIOCGIFMTU: 1592 ifr->ifr_mtu = ifp->if_mtu; 1593 break; 1594 1595 case SIOCGIFPHYS: 1596 ifr->ifr_phys = ifp->if_physical; 1597 break; 1598 1599 case SIOCSIFFLAGS: 1600 error = priv_check(td, PRIV_NET_SETIFFLAGS); 1601 if (error) 1602 return (error); 1603 /* 1604 * Currently, no driver owned flags pass the IFF_CANTCHANGE 1605 * check, so we don't need special handling here yet. 1606 */ 1607 new_flags = (ifr->ifr_flags & 0xffff) | 1608 (ifr->ifr_flagshigh << 16); 1609 if (ifp->if_flags & IFF_SMART) { 1610 /* Smart drivers twiddle their own routes */ 1611 } else if (ifp->if_flags & IFF_UP && 1612 (new_flags & IFF_UP) == 0) { 1613 int s = splimp(); 1614 if_down(ifp); 1615 splx(s); 1616 } else if (new_flags & IFF_UP && 1617 (ifp->if_flags & IFF_UP) == 0) { 1618 int s = splimp(); 1619 if_up(ifp); 1620 splx(s); 1621 } 1622 /* See if permanently promiscuous mode bit is about to flip */ 1623 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 1624 if (new_flags & IFF_PPROMISC) 1625 ifp->if_flags |= IFF_PROMISC; 1626 else if (ifp->if_pcount == 0) 1627 ifp->if_flags &= ~IFF_PROMISC; 1628 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 1629 ifp->if_xname, 1630 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 1631 } 1632 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1633 (new_flags &~ IFF_CANTCHANGE); 1634 if (ifp->if_ioctl) { 1635 IFF_LOCKGIANT(ifp); 1636 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1637 IFF_UNLOCKGIANT(ifp); 1638 } 1639 getmicrotime(&ifp->if_lastchange); 1640 break; 1641 1642 case SIOCSIFCAP: 1643 error = priv_check(td, PRIV_NET_SETIFCAP); 1644 if (error) 1645 return (error); 1646 if (ifp->if_ioctl == NULL) 1647 return (EOPNOTSUPP); 1648 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1649 return (EINVAL); 1650 IFF_LOCKGIANT(ifp); 1651 error = (*ifp->if_ioctl)(ifp, cmd, data); 1652 IFF_UNLOCKGIANT(ifp); 1653 if (error == 0) 1654 getmicrotime(&ifp->if_lastchange); 1655 break; 1656 1657#ifdef MAC 1658 case SIOCSIFMAC: 1659 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 1660 break; 1661#endif 1662 1663 case SIOCSIFNAME: 1664 error = priv_check(td, PRIV_NET_SETIFNAME); 1665 if (error) 1666 return (error); 1667 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1668 if (error != 0) 1669 return (error); 1670 if (new_name[0] == '\0') 1671 return (EINVAL); 1672 if (ifunit(new_name) != NULL) 1673 return (EEXIST); 1674 1675 /* Announce the departure of the interface. */ 1676 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1677 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1678 1679 log(LOG_INFO, "%s: changing name to '%s'\n", 1680 ifp->if_xname, new_name); 1681 1682 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1683 ifa = ifp->if_addr; 1684 IFA_LOCK(ifa); 1685 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1686 namelen = strlen(new_name); 1687 onamelen = sdl->sdl_nlen; 1688 /* 1689 * Move the address if needed. This is safe because we 1690 * allocate space for a name of length IFNAMSIZ when we 1691 * create this in if_attach(). 1692 */ 1693 if (namelen != onamelen) { 1694 bcopy(sdl->sdl_data + onamelen, 1695 sdl->sdl_data + namelen, sdl->sdl_alen); 1696 } 1697 bcopy(new_name, sdl->sdl_data, namelen); 1698 sdl->sdl_nlen = namelen; 1699 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1700 bzero(sdl->sdl_data, onamelen); 1701 while (namelen != 0) 1702 sdl->sdl_data[--namelen] = 0xff; 1703 IFA_UNLOCK(ifa); 1704 1705 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 1706 /* Announce the return of the interface. */ 1707 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1708 break; 1709 1710 case SIOCSIFMETRIC: 1711 error = priv_check(td, PRIV_NET_SETIFMETRIC); 1712 if (error) 1713 return (error); 1714 ifp->if_metric = ifr->ifr_metric; 1715 getmicrotime(&ifp->if_lastchange); 1716 break; 1717 1718 case SIOCSIFPHYS: 1719 error = priv_check(td, PRIV_NET_SETIFPHYS); 1720 if (error) 1721 return (error); 1722 if (ifp->if_ioctl == NULL) 1723 return (EOPNOTSUPP); 1724 IFF_LOCKGIANT(ifp); 1725 error = (*ifp->if_ioctl)(ifp, cmd, data); 1726 IFF_UNLOCKGIANT(ifp); 1727 if (error == 0) 1728 getmicrotime(&ifp->if_lastchange); 1729 break; 1730 1731 case SIOCSIFMTU: 1732 { 1733 u_long oldmtu = ifp->if_mtu; 1734 1735 error = priv_check(td, PRIV_NET_SETIFMTU); 1736 if (error) 1737 return (error); 1738 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1739 return (EINVAL); 1740 if (ifp->if_ioctl == NULL) 1741 return (EOPNOTSUPP); 1742 IFF_LOCKGIANT(ifp); 1743 error = (*ifp->if_ioctl)(ifp, cmd, data); 1744 IFF_UNLOCKGIANT(ifp); 1745 if (error == 0) { 1746 getmicrotime(&ifp->if_lastchange); 1747 rt_ifmsg(ifp); 1748 } 1749 /* 1750 * If the link MTU changed, do network layer specific procedure. 1751 */ 1752 if (ifp->if_mtu != oldmtu) { 1753#ifdef INET6 1754 nd6_setmtu(ifp); 1755#endif 1756 } 1757 break; 1758 } 1759 1760 case SIOCADDMULTI: 1761 case SIOCDELMULTI: 1762 if (cmd == SIOCADDMULTI) 1763 error = priv_check(td, PRIV_NET_ADDMULTI); 1764 else 1765 error = priv_check(td, PRIV_NET_DELMULTI); 1766 if (error) 1767 return (error); 1768 1769 /* Don't allow group membership on non-multicast interfaces. */ 1770 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1771 return (EOPNOTSUPP); 1772 1773 /* Don't let users screw up protocols' entries. */ 1774 if (ifr->ifr_addr.sa_family != AF_LINK) 1775 return (EINVAL); 1776 1777 if (cmd == SIOCADDMULTI) { 1778 struct ifmultiaddr *ifma; 1779 1780 /* 1781 * Userland is only permitted to join groups once 1782 * via the if_addmulti() KPI, because it cannot hold 1783 * struct ifmultiaddr * between calls. It may also 1784 * lose a race while we check if the membership 1785 * already exists. 1786 */ 1787 IF_ADDR_LOCK(ifp); 1788 ifma = if_findmulti(ifp, &ifr->ifr_addr); 1789 IF_ADDR_UNLOCK(ifp); 1790 if (ifma != NULL) 1791 error = EADDRINUSE; 1792 else 1793 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1794 } else { 1795 error = if_delmulti(ifp, &ifr->ifr_addr); 1796 } 1797 if (error == 0) 1798 getmicrotime(&ifp->if_lastchange); 1799 break; 1800 1801 case SIOCSIFPHYADDR: 1802 case SIOCDIFPHYADDR: 1803#ifdef INET6 1804 case SIOCSIFPHYADDR_IN6: 1805#endif 1806 case SIOCSLIFPHYADDR: 1807 case SIOCSIFMEDIA: 1808 case SIOCSIFGENERIC: 1809 error = priv_check(td, PRIV_NET_HWIOCTL); 1810 if (error) 1811 return (error); 1812 if (ifp->if_ioctl == NULL) 1813 return (EOPNOTSUPP); 1814 IFF_LOCKGIANT(ifp); 1815 error = (*ifp->if_ioctl)(ifp, cmd, data); 1816 IFF_UNLOCKGIANT(ifp); 1817 if (error == 0) 1818 getmicrotime(&ifp->if_lastchange); 1819 break; 1820 1821 case SIOCGIFSTATUS: 1822 ifs = (struct ifstat *)data; 1823 ifs->ascii[0] = '\0'; 1824 1825 case SIOCGIFPSRCADDR: 1826 case SIOCGIFPDSTADDR: 1827 case SIOCGLIFPHYADDR: 1828 case SIOCGIFMEDIA: 1829 case SIOCGIFGENERIC: 1830 if (ifp->if_ioctl == NULL) 1831 return (EOPNOTSUPP); 1832 IFF_LOCKGIANT(ifp); 1833 error = (*ifp->if_ioctl)(ifp, cmd, data); 1834 IFF_UNLOCKGIANT(ifp); 1835 break; 1836 1837 case SIOCSIFLLADDR: 1838 error = priv_check(td, PRIV_NET_SETLLADDR); 1839 if (error) 1840 return (error); 1841 error = if_setlladdr(ifp, 1842 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1843 break; 1844 1845 case SIOCAIFGROUP: 1846 { 1847 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1848 1849 error = priv_check(td, PRIV_NET_ADDIFGROUP); 1850 if (error) 1851 return (error); 1852 if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 1853 return (error); 1854 break; 1855 } 1856 1857 case SIOCGIFGROUP: 1858 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 1859 return (error); 1860 break; 1861 1862 case SIOCDIFGROUP: 1863 { 1864 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1865 1866 error = priv_check(td, PRIV_NET_DELIFGROUP); 1867 if (error) 1868 return (error); 1869 if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 1870 return (error); 1871 break; 1872 } 1873 1874 default: 1875 error = ENOIOCTL; 1876 break; 1877 } 1878 return (error); 1879} 1880 1881/* 1882 * Interface ioctls. 1883 */ 1884int 1885ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 1886{ 1887 struct ifnet *ifp; 1888 struct ifreq *ifr; 1889 int error; 1890 int oif_flags; 1891 1892 switch (cmd) { 1893 case SIOCGIFCONF: 1894 case OSIOCGIFCONF: 1895#ifdef __amd64__ 1896 case SIOCGIFCONF32: 1897#endif 1898 return (ifconf(cmd, data)); 1899 } 1900 ifr = (struct ifreq *)data; 1901 1902 switch (cmd) { 1903 case SIOCIFCREATE: 1904 case SIOCIFCREATE2: 1905 error = priv_check(td, PRIV_NET_IFCREATE); 1906 if (error) 1907 return (error); 1908 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name), 1909 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL)); 1910 case SIOCIFDESTROY: 1911 error = priv_check(td, PRIV_NET_IFDESTROY); 1912 if (error) 1913 return (error); 1914 return if_clone_destroy(ifr->ifr_name); 1915 1916 case SIOCIFGCLONERS: 1917 return (if_clone_list((struct if_clonereq *)data)); 1918 case SIOCGIFGMEMB: 1919 return (if_getgroupmembers((struct ifgroupreq *)data)); 1920 } 1921 1922 ifp = ifunit(ifr->ifr_name); 1923 if (ifp == 0) 1924 return (ENXIO); 1925 1926 error = ifhwioctl(cmd, ifp, data, td); 1927 if (error != ENOIOCTL) 1928 return (error); 1929 1930 oif_flags = ifp->if_flags; 1931 if (so->so_proto == 0) 1932 return (EOPNOTSUPP); 1933#ifndef COMPAT_43 1934 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1935 data, 1936 ifp, td)); 1937#else 1938 { 1939 int ocmd = cmd; 1940 1941 switch (cmd) { 1942 1943 case SIOCSIFDSTADDR: 1944 case SIOCSIFADDR: 1945 case SIOCSIFBRDADDR: 1946 case SIOCSIFNETMASK: 1947#if BYTE_ORDER != BIG_ENDIAN 1948 if (ifr->ifr_addr.sa_family == 0 && 1949 ifr->ifr_addr.sa_len < 16) { 1950 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1951 ifr->ifr_addr.sa_len = 16; 1952 } 1953#else 1954 if (ifr->ifr_addr.sa_len == 0) 1955 ifr->ifr_addr.sa_len = 16; 1956#endif 1957 break; 1958 1959 case OSIOCGIFADDR: 1960 cmd = SIOCGIFADDR; 1961 break; 1962 1963 case OSIOCGIFDSTADDR: 1964 cmd = SIOCGIFDSTADDR; 1965 break; 1966 1967 case OSIOCGIFBRDADDR: 1968 cmd = SIOCGIFBRDADDR; 1969 break; 1970 1971 case OSIOCGIFNETMASK: 1972 cmd = SIOCGIFNETMASK; 1973 } 1974 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 1975 cmd, 1976 data, 1977 ifp, td)); 1978 switch (ocmd) { 1979 1980 case OSIOCGIFADDR: 1981 case OSIOCGIFDSTADDR: 1982 case OSIOCGIFBRDADDR: 1983 case OSIOCGIFNETMASK: 1984 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1985 1986 } 1987 } 1988#endif /* COMPAT_43 */ 1989 1990 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 1991#ifdef INET6 1992 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 1993 if (ifp->if_flags & IFF_UP) { 1994 int s = splimp(); 1995 in6_if_up(ifp); 1996 splx(s); 1997 } 1998#endif 1999 } 2000 return (error); 2001} 2002 2003/* 2004 * The code common to handling reference counted flags, 2005 * e.g., in ifpromisc() and if_allmulti(). 2006 * The "pflag" argument can specify a permanent mode flag to check, 2007 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2008 * 2009 * Only to be used on stack-owned flags, not driver-owned flags. 2010 */ 2011static int 2012if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 2013{ 2014 struct ifreq ifr; 2015 int error; 2016 int oldflags, oldcount; 2017 2018 /* Sanity checks to catch programming errors */ 2019 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2020 ("%s: setting driver-owned flag %d", __func__, flag)); 2021 2022 if (onswitch) 2023 KASSERT(*refcount >= 0, 2024 ("%s: increment negative refcount %d for flag %d", 2025 __func__, *refcount, flag)); 2026 else 2027 KASSERT(*refcount > 0, 2028 ("%s: decrement non-positive refcount %d for flag %d", 2029 __func__, *refcount, flag)); 2030 2031 /* In case this mode is permanent, just touch refcount */ 2032 if (ifp->if_flags & pflag) { 2033 *refcount += onswitch ? 1 : -1; 2034 return (0); 2035 } 2036 2037 /* Save ifnet parameters for if_ioctl() may fail */ 2038 oldcount = *refcount; 2039 oldflags = ifp->if_flags; 2040 2041 /* 2042 * See if we aren't the only and touching refcount is enough. 2043 * Actually toggle interface flag if we are the first or last. 2044 */ 2045 if (onswitch) { 2046 if ((*refcount)++) 2047 return (0); 2048 ifp->if_flags |= flag; 2049 } else { 2050 if (--(*refcount)) 2051 return (0); 2052 ifp->if_flags &= ~flag; 2053 } 2054 2055 /* Call down the driver since we've changed interface flags */ 2056 if (ifp->if_ioctl == NULL) { 2057 error = EOPNOTSUPP; 2058 goto recover; 2059 } 2060 ifr.ifr_flags = ifp->if_flags & 0xffff; 2061 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2062 IFF_LOCKGIANT(ifp); 2063 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2064 IFF_UNLOCKGIANT(ifp); 2065 if (error) 2066 goto recover; 2067 /* Notify userland that interface flags have changed */ 2068 rt_ifmsg(ifp); 2069 return (0); 2070 2071recover: 2072 /* Recover after driver error */ 2073 *refcount = oldcount; 2074 ifp->if_flags = oldflags; 2075 return (error); 2076} 2077 2078/* 2079 * Set/clear promiscuous mode on interface ifp based on the truth value 2080 * of pswitch. The calls are reference counted so that only the first 2081 * "on" request actually has an effect, as does the final "off" request. 2082 * Results are undefined if the "off" and "on" requests are not matched. 2083 */ 2084int 2085ifpromisc(struct ifnet *ifp, int pswitch) 2086{ 2087 int error; 2088 int oldflags = ifp->if_flags; 2089 2090 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 2091 &ifp->if_pcount, pswitch); 2092 /* If promiscuous mode status has changed, log a message */ 2093 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 2094 log(LOG_INFO, "%s: promiscuous mode %s\n", 2095 ifp->if_xname, 2096 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 2097 return (error); 2098} 2099 2100/* 2101 * Return interface configuration 2102 * of system. List may be used 2103 * in later ioctl's (above) to get 2104 * other information. 2105 */ 2106/*ARGSUSED*/ 2107static int 2108ifconf(u_long cmd, caddr_t data) 2109{ 2110 struct ifconf *ifc = (struct ifconf *)data; 2111#ifdef __amd64__ 2112 struct ifconf32 *ifc32 = (struct ifconf32 *)data; 2113 struct ifconf ifc_swab; 2114#endif 2115 struct ifnet *ifp; 2116 struct ifaddr *ifa; 2117 struct ifreq ifr; 2118 struct sbuf *sb; 2119 int error, full = 0, valid_len, max_len; 2120 2121#ifdef __amd64__ 2122 if (cmd == SIOCGIFCONF32) { 2123 ifc_swab.ifc_len = ifc32->ifc_len; 2124 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf; 2125 ifc = &ifc_swab; 2126 } 2127#endif 2128 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 2129 max_len = MAXPHYS - 1; 2130 2131 /* Prevent hostile input from being able to crash the system */ 2132 if (ifc->ifc_len <= 0) 2133 return (EINVAL); 2134 2135again: 2136 if (ifc->ifc_len <= max_len) { 2137 max_len = ifc->ifc_len; 2138 full = 1; 2139 } 2140 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2141 max_len = 0; 2142 valid_len = 0; 2143 2144 IFNET_RLOCK(); /* could sleep XXX */ 2145 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2146 int addrs; 2147 2148 /* 2149 * Zero the ifr_name buffer to make sure we don't 2150 * disclose the contents of the stack. 2151 */ 2152 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2153 2154 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2155 >= sizeof(ifr.ifr_name)) { 2156 sbuf_delete(sb); 2157 IFNET_RUNLOCK(); 2158 return (ENAMETOOLONG); 2159 } 2160 2161 addrs = 0; 2162 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2163 struct sockaddr *sa = ifa->ifa_addr; 2164 2165 if (jailed(curthread->td_ucred) && 2166 prison_if(curthread->td_ucred, sa)) 2167 continue; 2168 addrs++; 2169#ifdef COMPAT_43 2170 if (cmd == OSIOCGIFCONF) { 2171 struct osockaddr *osa = 2172 (struct osockaddr *)&ifr.ifr_addr; 2173 ifr.ifr_addr = *sa; 2174 osa->sa_family = sa->sa_family; 2175 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2176 max_len += sizeof(ifr); 2177 } else 2178#endif 2179 if (sa->sa_len <= sizeof(*sa)) { 2180 ifr.ifr_addr = *sa; 2181 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2182 max_len += sizeof(ifr); 2183 } else { 2184 sbuf_bcat(sb, &ifr, 2185 offsetof(struct ifreq, ifr_addr)); 2186 max_len += offsetof(struct ifreq, ifr_addr); 2187 sbuf_bcat(sb, sa, sa->sa_len); 2188 max_len += sa->sa_len; 2189 } 2190 2191 if (!sbuf_overflowed(sb)) 2192 valid_len = sbuf_len(sb); 2193 } 2194 if (addrs == 0) { 2195 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2196 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2197 max_len += sizeof(ifr); 2198 2199 if (!sbuf_overflowed(sb)) 2200 valid_len = sbuf_len(sb); 2201 } 2202 } 2203 IFNET_RUNLOCK(); 2204 2205 /* 2206 * If we didn't allocate enough space (uncommon), try again. If 2207 * we have already allocated as much space as we are allowed, 2208 * return what we've got. 2209 */ 2210 if (valid_len != max_len && !full) { 2211 sbuf_delete(sb); 2212 goto again; 2213 } 2214 2215 ifc->ifc_len = valid_len; 2216#ifdef __amd64__ 2217 if (cmd == SIOCGIFCONF32) 2218 ifc32->ifc_len = valid_len; 2219#endif 2220 sbuf_finish(sb); 2221 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 2222 sbuf_delete(sb); 2223 return (error); 2224} 2225 2226/* 2227 * Just like ifpromisc(), but for all-multicast-reception mode. 2228 */ 2229int 2230if_allmulti(struct ifnet *ifp, int onswitch) 2231{ 2232 2233 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 2234} 2235 2236struct ifmultiaddr * 2237if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 2238{ 2239 struct ifmultiaddr *ifma; 2240 2241 IF_ADDR_LOCK_ASSERT(ifp); 2242 2243 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2244 if (sa->sa_family == AF_LINK) { 2245 if (sa_dl_equal(ifma->ifma_addr, sa)) 2246 break; 2247 } else { 2248 if (sa_equal(ifma->ifma_addr, sa)) 2249 break; 2250 } 2251 } 2252 2253 return ifma; 2254} 2255 2256/* 2257 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2258 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2259 * the ifnet multicast address list here, so the caller must do that and 2260 * other setup work (such as notifying the device driver). The reference 2261 * count is initialized to 1. 2262 */ 2263static struct ifmultiaddr * 2264if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2265 int mflags) 2266{ 2267 struct ifmultiaddr *ifma; 2268 struct sockaddr *dupsa; 2269 2270 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags | 2271 M_ZERO); 2272 if (ifma == NULL) 2273 return (NULL); 2274 2275 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags); 2276 if (dupsa == NULL) { 2277 FREE(ifma, M_IFMADDR); 2278 return (NULL); 2279 } 2280 bcopy(sa, dupsa, sa->sa_len); 2281 ifma->ifma_addr = dupsa; 2282 2283 ifma->ifma_ifp = ifp; 2284 ifma->ifma_refcount = 1; 2285 ifma->ifma_protospec = NULL; 2286 2287 if (llsa == NULL) { 2288 ifma->ifma_lladdr = NULL; 2289 return (ifma); 2290 } 2291 2292 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags); 2293 if (dupsa == NULL) { 2294 FREE(ifma->ifma_addr, M_IFMADDR); 2295 FREE(ifma, M_IFMADDR); 2296 return (NULL); 2297 } 2298 bcopy(llsa, dupsa, llsa->sa_len); 2299 ifma->ifma_lladdr = dupsa; 2300 2301 return (ifma); 2302} 2303 2304/* 2305 * if_freemulti: free ifmultiaddr structure and possibly attached related 2306 * addresses. The caller is responsible for implementing reference 2307 * counting, notifying the driver, handling routing messages, and releasing 2308 * any dependent link layer state. 2309 */ 2310static void 2311if_freemulti(struct ifmultiaddr *ifma) 2312{ 2313 2314 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2315 ifma->ifma_refcount)); 2316 KASSERT(ifma->ifma_protospec == NULL, 2317 ("if_freemulti: protospec not NULL")); 2318 2319 if (ifma->ifma_lladdr != NULL) 2320 FREE(ifma->ifma_lladdr, M_IFMADDR); 2321 FREE(ifma->ifma_addr, M_IFMADDR); 2322 FREE(ifma, M_IFMADDR); 2323} 2324 2325/* 2326 * Register an additional multicast address with a network interface. 2327 * 2328 * - If the address is already present, bump the reference count on the 2329 * address and return. 2330 * - If the address is not link-layer, look up a link layer address. 2331 * - Allocate address structures for one or both addresses, and attach to the 2332 * multicast address list on the interface. If automatically adding a link 2333 * layer address, the protocol address will own a reference to the link 2334 * layer address, to be freed when it is freed. 2335 * - Notify the network device driver of an addition to the multicast address 2336 * list. 2337 * 2338 * 'sa' points to caller-owned memory with the desired multicast address. 2339 * 2340 * 'retifma' will be used to return a pointer to the resulting multicast 2341 * address reference, if desired. 2342 */ 2343int 2344if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2345 struct ifmultiaddr **retifma) 2346{ 2347 struct ifmultiaddr *ifma, *ll_ifma; 2348 struct sockaddr *llsa; 2349 int error; 2350 2351 /* 2352 * If the address is already present, return a new reference to it; 2353 * otherwise, allocate storage and set up a new address. 2354 */ 2355 IF_ADDR_LOCK(ifp); 2356 ifma = if_findmulti(ifp, sa); 2357 if (ifma != NULL) { 2358 ifma->ifma_refcount++; 2359 if (retifma != NULL) 2360 *retifma = ifma; 2361 IF_ADDR_UNLOCK(ifp); 2362 return (0); 2363 } 2364 2365 /* 2366 * The address isn't already present; resolve the protocol address 2367 * into a link layer address, and then look that up, bump its 2368 * refcount or allocate an ifma for that also. If 'llsa' was 2369 * returned, we will need to free it later. 2370 */ 2371 llsa = NULL; 2372 ll_ifma = NULL; 2373 if (ifp->if_resolvemulti != NULL) { 2374 error = ifp->if_resolvemulti(ifp, &llsa, sa); 2375 if (error) 2376 goto unlock_out; 2377 } 2378 2379 /* 2380 * Allocate the new address. Don't hook it up yet, as we may also 2381 * need to allocate a link layer multicast address. 2382 */ 2383 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2384 if (ifma == NULL) { 2385 error = ENOMEM; 2386 goto free_llsa_out; 2387 } 2388 2389 /* 2390 * If a link layer address is found, we'll need to see if it's 2391 * already present in the address list, or allocate is as well. 2392 * When this block finishes, the link layer address will be on the 2393 * list. 2394 */ 2395 if (llsa != NULL) { 2396 ll_ifma = if_findmulti(ifp, llsa); 2397 if (ll_ifma == NULL) { 2398 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2399 if (ll_ifma == NULL) { 2400 --ifma->ifma_refcount; 2401 if_freemulti(ifma); 2402 error = ENOMEM; 2403 goto free_llsa_out; 2404 } 2405 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 2406 ifma_link); 2407 } else 2408 ll_ifma->ifma_refcount++; 2409 ifma->ifma_llifma = ll_ifma; 2410 } 2411 2412 /* 2413 * We now have a new multicast address, ifma, and possibly a new or 2414 * referenced link layer address. Add the primary address to the 2415 * ifnet address list. 2416 */ 2417 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2418 2419 if (retifma != NULL) 2420 *retifma = ifma; 2421 2422 /* 2423 * Must generate the message while holding the lock so that 'ifma' 2424 * pointer is still valid. 2425 */ 2426 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2427 IF_ADDR_UNLOCK(ifp); 2428 2429 /* 2430 * We are certain we have added something, so call down to the 2431 * interface to let them know about it. 2432 */ 2433 if (ifp->if_ioctl != NULL) { 2434 IFF_LOCKGIANT(ifp); 2435 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 2436 IFF_UNLOCKGIANT(ifp); 2437 } 2438 2439 if (llsa != NULL) 2440 FREE(llsa, M_IFMADDR); 2441 2442 return (0); 2443 2444free_llsa_out: 2445 if (llsa != NULL) 2446 FREE(llsa, M_IFMADDR); 2447 2448unlock_out: 2449 IF_ADDR_UNLOCK(ifp); 2450 return (error); 2451} 2452 2453/* 2454 * Delete a multicast group membership by network-layer group address. 2455 * 2456 * Returns ENOENT if the entry could not be found. If ifp no longer 2457 * exists, results are undefined. This entry point should only be used 2458 * from subsystems which do appropriate locking to hold ifp for the 2459 * duration of the call. 2460 * Network-layer protocol domains must use if_delmulti_ifma(). 2461 */ 2462int 2463if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 2464{ 2465 struct ifmultiaddr *ifma; 2466 int lastref; 2467#ifdef INVARIANTS 2468 struct ifnet *oifp; 2469 2470 IFNET_RLOCK(); 2471 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2472 if (ifp == oifp) 2473 break; 2474 if (ifp != oifp) 2475 ifp = NULL; 2476 IFNET_RUNLOCK(); 2477 2478 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 2479#endif 2480 if (ifp == NULL) 2481 return (ENOENT); 2482 2483 IF_ADDR_LOCK(ifp); 2484 lastref = 0; 2485 ifma = if_findmulti(ifp, sa); 2486 if (ifma != NULL) 2487 lastref = if_delmulti_locked(ifp, ifma, 0); 2488 IF_ADDR_UNLOCK(ifp); 2489 2490 if (ifma == NULL) 2491 return (ENOENT); 2492 2493 if (lastref && ifp->if_ioctl != NULL) { 2494 IFF_LOCKGIANT(ifp); 2495 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2496 IFF_UNLOCKGIANT(ifp); 2497 } 2498 2499 return (0); 2500} 2501 2502/* 2503 * Delete a multicast group membership by group membership pointer. 2504 * Network-layer protocol domains must use this routine. 2505 * 2506 * It is safe to call this routine if the ifp disappeared. Callers should 2507 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be 2508 * reconfigured. 2509 */ 2510void 2511if_delmulti_ifma(struct ifmultiaddr *ifma) 2512{ 2513 struct ifnet *ifp; 2514 int lastref; 2515 2516 ifp = ifma->ifma_ifp; 2517#ifdef DIAGNOSTIC 2518 if (ifp == NULL) { 2519 printf("%s: ifma_ifp seems to be detached\n", __func__); 2520 } else { 2521 struct ifnet *oifp; 2522 2523 IFNET_RLOCK(); 2524 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2525 if (ifp == oifp) 2526 break; 2527 if (ifp != oifp) { 2528 printf("%s: ifnet %p disappeared\n", __func__, ifp); 2529 ifp = NULL; 2530 } 2531 IFNET_RUNLOCK(); 2532 } 2533#endif 2534 /* 2535 * If and only if the ifnet instance exists: Acquire the address lock. 2536 */ 2537 if (ifp != NULL) 2538 IF_ADDR_LOCK(ifp); 2539 2540 lastref = if_delmulti_locked(ifp, ifma, 0); 2541 2542 if (ifp != NULL) { 2543 /* 2544 * If and only if the ifnet instance exists: 2545 * Release the address lock. 2546 * If the group was left: update the hardware hash filter. 2547 */ 2548 IF_ADDR_UNLOCK(ifp); 2549 if (lastref && ifp->if_ioctl != NULL) { 2550 IFF_LOCKGIANT(ifp); 2551 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2552 IFF_UNLOCKGIANT(ifp); 2553 } 2554 } 2555} 2556 2557/* 2558 * Perform deletion of network-layer and/or link-layer multicast address. 2559 * 2560 * Return 0 if the reference count was decremented. 2561 * Return 1 if the final reference was released, indicating that the 2562 * hardware hash filter should be reprogrammed. 2563 */ 2564static int 2565if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 2566{ 2567 struct ifmultiaddr *ll_ifma; 2568 2569 if (ifp != NULL && ifma->ifma_ifp != NULL) { 2570 KASSERT(ifma->ifma_ifp == ifp, 2571 ("%s: inconsistent ifp %p", __func__, ifp)); 2572 IF_ADDR_LOCK_ASSERT(ifp); 2573 } 2574 2575 ifp = ifma->ifma_ifp; 2576 2577 /* 2578 * If the ifnet is detaching, null out references to ifnet, 2579 * so that upper protocol layers will notice, and not attempt 2580 * to obtain locks for an ifnet which no longer exists. The 2581 * routing socket announcement must happen before the ifnet 2582 * instance is detached from the system. 2583 */ 2584 if (detaching) { 2585#ifdef DIAGNOSTIC 2586 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 2587#endif 2588 /* 2589 * ifp may already be nulled out if we are being reentered 2590 * to delete the ll_ifma. 2591 */ 2592 if (ifp != NULL) { 2593 rt_newmaddrmsg(RTM_DELMADDR, ifma); 2594 ifma->ifma_ifp = NULL; 2595 } 2596 } 2597 2598 if (--ifma->ifma_refcount > 0) 2599 return 0; 2600 2601 /* 2602 * If this ifma is a network-layer ifma, a link-layer ifma may 2603 * have been associated with it. Release it first if so. 2604 */ 2605 ll_ifma = ifma->ifma_llifma; 2606 if (ll_ifma != NULL) { 2607 KASSERT(ifma->ifma_lladdr != NULL, 2608 ("%s: llifma w/o lladdr", __func__)); 2609 if (detaching) 2610 ll_ifma->ifma_ifp = NULL; /* XXX */ 2611 if (--ll_ifma->ifma_refcount == 0) { 2612 if (ifp != NULL) { 2613 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 2614 ifma_link); 2615 } 2616 if_freemulti(ll_ifma); 2617 } 2618 } 2619 2620 if (ifp != NULL) 2621 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2622 2623 if_freemulti(ifma); 2624 2625 /* 2626 * The last reference to this instance of struct ifmultiaddr 2627 * was released; the hardware should be notified of this change. 2628 */ 2629 return 1; 2630} 2631 2632/* 2633 * Set the link layer address on an interface. 2634 * 2635 * At this time we only support certain types of interfaces, 2636 * and we don't allow the length of the address to change. 2637 */ 2638int 2639if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 2640{ 2641 struct sockaddr_dl *sdl; 2642 struct ifaddr *ifa; 2643 struct ifreq ifr; 2644 2645 ifa = ifp->if_addr; 2646 if (ifa == NULL) 2647 return (EINVAL); 2648 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2649 if (sdl == NULL) 2650 return (EINVAL); 2651 if (len != sdl->sdl_alen) /* don't allow length to change */ 2652 return (EINVAL); 2653 switch (ifp->if_type) { 2654 case IFT_ETHER: 2655 case IFT_FDDI: 2656 case IFT_XETHER: 2657 case IFT_ISO88025: 2658 case IFT_L2VLAN: 2659 case IFT_BRIDGE: 2660 case IFT_ARCNET: 2661 case IFT_IEEE8023ADLAG: 2662 bcopy(lladdr, LLADDR(sdl), len); 2663 break; 2664 default: 2665 return (ENODEV); 2666 } 2667 /* 2668 * If the interface is already up, we need 2669 * to re-init it in order to reprogram its 2670 * address filter. 2671 */ 2672 if ((ifp->if_flags & IFF_UP) != 0) { 2673 if (ifp->if_ioctl) { 2674 IFF_LOCKGIANT(ifp); 2675 ifp->if_flags &= ~IFF_UP; 2676 ifr.ifr_flags = ifp->if_flags & 0xffff; 2677 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2678 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2679 ifp->if_flags |= IFF_UP; 2680 ifr.ifr_flags = ifp->if_flags & 0xffff; 2681 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2682 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2683 IFF_UNLOCKGIANT(ifp); 2684 } 2685#ifdef INET 2686 /* 2687 * Also send gratuitous ARPs to notify other nodes about 2688 * the address change. 2689 */ 2690 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2691 if (ifa->ifa_addr->sa_family == AF_INET) 2692 arp_ifinit(ifp, ifa); 2693 } 2694#endif 2695 } 2696 return (0); 2697} 2698 2699/* 2700 * The name argument must be a pointer to storage which will last as 2701 * long as the interface does. For physical devices, the result of 2702 * device_get_name(dev) is a good choice and for pseudo-devices a 2703 * static string works well. 2704 */ 2705void 2706if_initname(struct ifnet *ifp, const char *name, int unit) 2707{ 2708 ifp->if_dname = name; 2709 ifp->if_dunit = unit; 2710 if (unit != IF_DUNIT_NONE) 2711 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 2712 else 2713 strlcpy(ifp->if_xname, name, IFNAMSIZ); 2714} 2715 2716int 2717if_printf(struct ifnet *ifp, const char * fmt, ...) 2718{ 2719 va_list ap; 2720 int retval; 2721 2722 retval = printf("%s: ", ifp->if_xname); 2723 va_start(ap, fmt); 2724 retval += vprintf(fmt, ap); 2725 va_end(ap); 2726 return (retval); 2727} 2728 2729/* 2730 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot 2731 * be called without Giant. However, we often can't acquire the Giant lock 2732 * at those points; instead, we run it via a task queue that holds Giant via 2733 * if_start_deferred. 2734 * 2735 * XXXRW: We need to make sure that the ifnet isn't fully detached until any 2736 * outstanding if_start_deferred() tasks that will run after the free. This 2737 * probably means waiting in if_detach(). 2738 */ 2739void 2740if_start(struct ifnet *ifp) 2741{ 2742 2743 if (ifp->if_flags & IFF_NEEDSGIANT) { 2744 if (mtx_owned(&Giant)) 2745 (*(ifp)->if_start)(ifp); 2746 else 2747 taskqueue_enqueue(taskqueue_swi_giant, 2748 &ifp->if_starttask); 2749 } else 2750 (*(ifp)->if_start)(ifp); 2751} 2752 2753static void 2754if_start_deferred(void *context, int pending) 2755{ 2756 struct ifnet *ifp; 2757 2758 GIANT_REQUIRED; 2759 2760 ifp = context; 2761 (ifp->if_start)(ifp); 2762} 2763 2764int 2765if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 2766{ 2767 int active = 0; 2768 2769 IF_LOCK(ifq); 2770 if (_IF_QFULL(ifq)) { 2771 _IF_DROP(ifq); 2772 IF_UNLOCK(ifq); 2773 m_freem(m); 2774 return (0); 2775 } 2776 if (ifp != NULL) { 2777 ifp->if_obytes += m->m_pkthdr.len + adjust; 2778 if (m->m_flags & (M_BCAST|M_MCAST)) 2779 ifp->if_omcasts++; 2780 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 2781 } 2782 _IF_ENQUEUE(ifq, m); 2783 IF_UNLOCK(ifq); 2784 if (ifp != NULL && !active) 2785 if_start(ifp); 2786 return (1); 2787} 2788 2789void 2790if_register_com_alloc(u_char type, 2791 if_com_alloc_t *a, if_com_free_t *f) 2792{ 2793 2794 KASSERT(if_com_alloc[type] == NULL, 2795 ("if_register_com_alloc: %d already registered", type)); 2796 KASSERT(if_com_free[type] == NULL, 2797 ("if_register_com_alloc: %d free already registered", type)); 2798 2799 if_com_alloc[type] = a; 2800 if_com_free[type] = f; 2801} 2802 2803void 2804if_deregister_com_alloc(u_char type) 2805{ 2806 2807 KASSERT(if_com_alloc[type] != NULL, 2808 ("if_deregister_com_alloc: %d not registered", type)); 2809 KASSERT(if_com_free[type] != NULL, 2810 ("if_deregister_com_alloc: %d free not registered", type)); 2811 if_com_alloc[type] = NULL; 2812 if_com_free[type] = NULL; 2813}
|