+ VERIFY(ifp != NULL);
+
+ switch (cmd) {
+ case SIOCAIFAGENTID: { /* struct if_agentidreq */
+ uuid_t *first_empty_slot = NULL;
+ // TODO: Use priv_check_cred() instead of root check
+ if ((error = proc_suser(p)) != 0) {
+ break;
+ }
+ for (index = 0; index < IF_MAXAGENTS; index++) {
+ uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
+ if (uuid_compare(*netagent_uuid, ifar->ifar_uuid) == 0) {
+ /* Already present, ignore */
+ break;
+ }
+ if (first_empty_slot == NULL &&
+ uuid_is_null(*netagent_uuid)) {
+ first_empty_slot = netagent_uuid;
+ }
+ }
+ if (first_empty_slot == NULL) {
+ error = ENOMEM; /* No empty slot for a netagent UUID, bail */
+ break;
+ }
+ uuid_copy(*first_empty_slot, ifar->ifar_uuid);
+ netagent_post_updated_interfaces(ifar->ifar_uuid);
+ break;
+ }
+ case SIOCDIFAGENTID: { /* struct if_agentidreq */
+ bool removed_agent_id = FALSE;
+ // TODO: Use priv_check_cred() instead of root check
+ if ((error = proc_suser(p)) != 0) {
+ break;
+ }
+ for (index = 0; index < IF_MAXAGENTS; index++) {
+ uuid_t *netagent_uuid = &(ifp->if_agentids[index]);
+ if (uuid_compare(*netagent_uuid, ifar->ifar_uuid) == 0) {
+ uuid_clear(*netagent_uuid);
+ removed_agent_id = TRUE;
+ break;
+ }
+ }
+ if (removed_agent_id) {
+ netagent_post_updated_interfaces(ifar->ifar_uuid);
+ }
+ break;
+ }
+ case SIOCGIFAGENTIDS32: { /* struct if_agentidsreq32 */
+ bcopy(data, &u.s32, sizeof(u.s32));
+ error = ifioctl_getnetagents(ifp, &u.s32.ifar_count, u.s32.ifar_uuids);
+ if (error == 0) {
+ bcopy(&u.s32, data, sizeof(u.s32));
+ }
+ break;
+ }
+ case SIOCGIFAGENTIDS64: { /* struct if_agentidsreq64 */
+ bcopy(data, &u.s64, sizeof(u.s64));
+ error = ifioctl_getnetagents(ifp, &u.s64.ifar_count, u.s64.ifar_uuids);
+ if (error == 0) {
+ bcopy(&u.s64, data, sizeof(u.s64));
+ }
+ break;
+ }
+ default:
+ VERIFY(0);
+ /* NOTREACHED */
+ }
+
+ return (error);
+}
+
+void
+ifnet_clear_netagent(uuid_t netagent_uuid)
+{
+ struct ifnet *ifp = NULL;
+ int index = 0;
+ bool removed_agent_id = FALSE;
+
+ ifnet_head_lock_shared();
+
+ TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
+ for (index = 0; index < IF_MAXAGENTS; index++) {
+ uuid_t *ifp_netagent_uuid = &(ifp->if_agentids[index]);
+ if (uuid_compare(*ifp_netagent_uuid, netagent_uuid) == 0) {
+ uuid_clear(*ifp_netagent_uuid);
+ removed_agent_id = TRUE;
+ }
+ }
+ }
+
+ ifnet_head_done();
+}
+
+static __attribute__((noinline)) int
+ifioctl_netsignature(struct ifnet *ifp, u_long cmd, caddr_t data)
+{
+ struct if_nsreq *ifnsr = (struct if_nsreq *)(void *)data;
+ u_int16_t flags;
+ int error = 0;
+
+ VERIFY(ifp != NULL);
+
+ switch (cmd) {
+ case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
+ if (ifnsr->ifnsr_len > sizeof (ifnsr->ifnsr_data)) {
+ error = EINVAL;
+ break;
+ }
+ bcopy(&ifnsr->ifnsr_flags, &flags, sizeof (flags));
+ error = ifnet_set_netsignature(ifp, ifnsr->ifnsr_family,
+ ifnsr->ifnsr_len, flags, ifnsr->ifnsr_data);
+ break;
+
+ case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
+ ifnsr->ifnsr_len = sizeof (ifnsr->ifnsr_data);
+ error = ifnet_get_netsignature(ifp, ifnsr->ifnsr_family,
+ &ifnsr->ifnsr_len, &flags, ifnsr->ifnsr_data);
+ if (error == 0)
+ bcopy(&flags, &ifnsr->ifnsr_flags, sizeof (flags));
+ else
+ ifnsr->ifnsr_len = 0;
+ break;
+
+ default:
+ VERIFY(0);
+ /* NOTREACHED */
+ }
+
+ return (error);
+}
+
+/*
+ * Interface ioctls.
+ *
+ * Most of the routines called to handle the ioctls would end up being
+ * tail-call optimized, which unfortunately causes this routine to
+ * consume too much stack space; this is the reason for the "noinline"
+ * attribute used on those routines.
+ */
+int
+ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
+{
+ char ifname[IFNAMSIZ + 1];
+ struct ifnet *ifp = NULL;
+ struct ifstat *ifs = NULL;
+ int error = 0;
+
+ bzero(ifname, sizeof (ifname));
+
+ /*
+ * ioctls which don't require ifp, or ifreq ioctls
+ */
+ switch (cmd) {
+ case OSIOCGIFCONF32: /* struct ifconf32 */
+ case SIOCGIFCONF32: /* struct ifconf32 */
+ case SIOCGIFCONF64: /* struct ifconf64 */
+ case OSIOCGIFCONF64: /* struct ifconf64 */
+ error = ifioctl_ifconf(cmd, data);
+ goto done;
+
+ case SIOCIFGCLONERS32: /* struct if_clonereq32 */
+ case SIOCIFGCLONERS64: /* struct if_clonereq64 */
+ error = ifioctl_ifclone(cmd, data);
+ goto done;
+
+ case SIOCGIFAGENTDATA32: /* struct netagent_req32 */
+ case SIOCGIFAGENTDATA64: /* struct netagent_req64 */
+ error = netagent_ioctl(cmd, data);
+ goto done;
+
+ case SIOCSIFDSTADDR: /* struct ifreq */
+ case SIOCSIFADDR: /* struct ifreq */
+ case SIOCSIFBRDADDR: /* struct ifreq */
+ case SIOCSIFNETMASK: /* struct ifreq */
+ case OSIOCGIFADDR: /* struct ifreq */
+ case OSIOCGIFDSTADDR: /* struct ifreq */
+ case OSIOCGIFBRDADDR: /* struct ifreq */
+ case OSIOCGIFNETMASK: /* struct ifreq */
+ case SIOCSIFKPI: /* struct ifreq */
+ if (so->so_proto == NULL) {
+ error = EOPNOTSUPP;
+ goto done;
+ }
+ /* FALLTHRU */
+ case SIOCIFCREATE: /* struct ifreq */
+ case SIOCIFCREATE2: /* struct ifreq */
+ case SIOCIFDESTROY: /* struct ifreq */
+ case SIOCGIFFLAGS: /* struct ifreq */
+ case SIOCGIFEFLAGS: /* struct ifreq */
+ case SIOCGIFCAP: /* struct ifreq */
+#if CONFIG_MACF_NET
+ case SIOCGIFMAC: /* struct ifreq */
+ case SIOCSIFMAC: /* struct ifreq */
+#endif /* CONFIG_MACF_NET */
+ case SIOCGIFMETRIC: /* struct ifreq */
+ case SIOCGIFMTU: /* struct ifreq */
+ case SIOCGIFPHYS: /* struct ifreq */
+ case SIOCSIFFLAGS: /* struct ifreq */
+ case SIOCSIFCAP: /* struct ifreq */
+ case SIOCSIFMETRIC: /* struct ifreq */
+ case SIOCSIFPHYS: /* struct ifreq */
+ case SIOCSIFMTU: /* struct ifreq */
+ case SIOCADDMULTI: /* struct ifreq */
+ case SIOCDELMULTI: /* struct ifreq */
+ case SIOCDIFPHYADDR: /* struct ifreq */
+ case SIOCSIFMEDIA: /* struct ifreq */
+ case SIOCSIFGENERIC: /* struct ifreq */
+ case SIOCSIFLLADDR: /* struct ifreq */
+ case SIOCSIFALTMTU: /* struct ifreq */
+ case SIOCSIFVLAN: /* struct ifreq */
+ case SIOCSIFBOND: /* struct ifreq */
+ case SIOCGIFLLADDR: /* struct ifreq */
+ case SIOCGIFTYPE: /* struct ifreq */
+ case SIOCGIFFUNCTIONALTYPE: /* struct ifreq */
+ case SIOCGIFPSRCADDR: /* struct ifreq */
+ case SIOCGIFPDSTADDR: /* struct ifreq */
+ case SIOCGIFGENERIC: /* struct ifreq */
+ case SIOCGIFDEVMTU: /* struct ifreq */
+ case SIOCGIFVLAN: /* struct ifreq */
+ case SIOCGIFBOND: /* struct ifreq */
+ case SIOCGIFWAKEFLAGS: /* struct ifreq */
+ case SIOCGIFGETRTREFCNT: /* struct ifreq */
+ case SIOCSIFOPPORTUNISTIC: /* struct ifreq */
+ case SIOCGIFOPPORTUNISTIC: /* struct ifreq */
+ case SIOCGIFLINKQUALITYMETRIC: /* struct ifreq */
+ case SIOCSIFLOG: /* struct ifreq */
+ case SIOCGIFLOG: /* struct ifreq */
+ case SIOCGIFDELEGATE: /* struct ifreq */
+ case SIOCGIFEXPENSIVE: /* struct ifreq */
+ case SIOCSIFEXPENSIVE: /* struct ifreq */
+ case SIOCSIF2KCL: /* struct ifreq */
+ case SIOCGIF2KCL: /* struct ifreq */
+ case SIOCSIFINTERFACESTATE: /* struct ifreq */
+ case SIOCGIFINTERFACESTATE: /* struct ifreq */
+ case SIOCSIFPROBECONNECTIVITY: /* struct ifreq */
+ case SIOCGIFPROBECONNECTIVITY: /* struct ifreq */
+ case SIOCGSTARTDELAY: { /* struct ifreq */
+ struct ifreq ifr;
+ bcopy(data, &ifr, sizeof (ifr));
+ ifr.ifr_name[IFNAMSIZ - 1] = '\0';
+ bcopy(&ifr.ifr_name, ifname, IFNAMSIZ);
+ error = ifioctl_ifreq(so, cmd, &ifr, p);
+ bcopy(&ifr, data, sizeof (ifr));
+ goto done;
+ }
+ }
+
+ /*
+ * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
+ * here to ensure that the ifnet, if found, has been fully attached.
+ */
+ dlil_if_lock();
+ switch (cmd) {
+ case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
+ bcopy(((struct in_aliasreq *)(void *)data)->ifra_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+#if INET6
+ case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
+ bcopy(((struct in6_aliasreq_32 *)(void *)data)->ifra_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+ case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
+ bcopy(((struct in6_aliasreq_64 *)(void *)data)->ifra_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+#endif /* INET6 */
+
+ case SIOCGIFSTATUS: /* struct ifstat */
+ ifs = _MALLOC(sizeof (*ifs), M_DEVBUF, M_WAITOK);
+ if (ifs == NULL) {
+ error = ENOMEM;
+ dlil_if_unlock();
+ goto done;
+ }
+ bcopy(data, ifs, sizeof (*ifs));
+ ifs->ifs_name[IFNAMSIZ - 1] = '\0';
+ bcopy(ifs->ifs_name, ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+ case SIOCGIFMEDIA32: /* struct ifmediareq32 */
+ bcopy(((struct ifmediareq32 *)(void *)data)->ifm_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+ case SIOCGIFMEDIA64: /* struct ifmediareq64 */
+ bcopy(((struct ifmediareq64 *)(void *)data)->ifm_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+ case SIOCSIFDESC: /* struct if_descreq */
+ case SIOCGIFDESC: /* struct if_descreq */
+ bcopy(((struct if_descreq *)(void *)data)->ifdr_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+ case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
+ case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
+ bcopy(((struct if_linkparamsreq *)(void *)data)->iflpr_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+ case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
+ bcopy(((struct if_qstatsreq *)(void *)data)->ifqr_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+ case SIOCSIFTHROTTLE: /* struct if_throttlereq */
+ case SIOCGIFTHROTTLE: /* struct if_throttlereq */
+ bcopy(((struct if_throttlereq *)(void *)data)->ifthr_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+ case SIOCAIFAGENTID: /* struct if_agentidreq */
+ case SIOCDIFAGENTID: /* struct if_agentidreq */
+ case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
+ case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
+ bcopy(((struct if_agentidreq *)(void *)data)->ifar_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+ case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
+ case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
+ bcopy(((struct if_nsreq *)(void *)data)->ifnsr_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+
+ default:
+ /*
+ * This is a bad assumption, but the code seems to
+ * have been doing this in the past; caveat emptor.
+ */
+ bcopy(((struct ifreq *)(void *)data)->ifr_name,
+ ifname, IFNAMSIZ);
+ ifp = ifunit(ifname);
+ break;
+ }
+ dlil_if_unlock();
+
+ if (ifp == NULL) {
+ error = ENXIO;
+ goto done;
+ }
+
+ switch (cmd) {
+ case SIOCSIFPHYADDR: /* struct {if,in_}aliasreq */
+#if INET6
+ case SIOCSIFPHYADDR_IN6_32: /* struct in6_aliasreq_32 */
+ case SIOCSIFPHYADDR_IN6_64: /* struct in6_aliasreq_64 */
+#endif /* INET6 */
+ error = proc_suser(p);
+ if (error != 0)
+ break;
+
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
+ if (error != 0)
+ break;
+
+ ifnet_touch_lastchange(ifp);
+ break;
+
+ case SIOCGIFSTATUS: /* struct ifstat */
+ VERIFY(ifs != NULL);
+ ifs->ascii[0] = '\0';
+
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifs);
+
+ bcopy(ifs, data, sizeof (*ifs));
+ break;
+
+ case SIOCGIFMEDIA32: /* struct ifmediareq32 */
+ case SIOCGIFMEDIA64: /* struct ifmediareq64 */
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
+ break;
+
+ case SIOCSIFDESC: /* struct if_descreq */
+ case SIOCGIFDESC: /* struct if_descreq */
+ error = ifioctl_ifdesc(ifp, cmd, data, p);
+ break;
+
+ case SIOCSIFLINKPARAMS: /* struct if_linkparamsreq */
+ case SIOCGIFLINKPARAMS: /* struct if_linkparamsreq */
+ error = ifioctl_linkparams(ifp, cmd, data, p);
+ break;
+
+ case SIOCGIFQUEUESTATS: /* struct if_qstatsreq */
+ error = ifioctl_qstats(ifp, cmd, data);
+ break;
+
+ case SIOCSIFTHROTTLE: /* struct if_throttlereq */
+ case SIOCGIFTHROTTLE: /* struct if_throttlereq */
+ error = ifioctl_throttle(ifp, cmd, data, p);
+ break;
+
+ case SIOCAIFAGENTID: /* struct if_agentidreq */
+ case SIOCDIFAGENTID: /* struct if_agentidreq */
+ case SIOCGIFAGENTIDS32: /* struct if_agentidsreq32 */
+ case SIOCGIFAGENTIDS64: /* struct if_agentidsreq64 */
+ error = ifioctl_netagent(ifp, cmd, data, p);
+ break;
+
+ case SIOCSIFNETSIGNATURE: /* struct if_nsreq */
+ case SIOCGIFNETSIGNATURE: /* struct if_nsreq */
+ error = ifioctl_netsignature(ifp, cmd, data);
+ break;
+
+ default:
+ if (so->so_proto == NULL) {
+ error = EOPNOTSUPP;
+ break;
+ }
+
+ socket_lock(so, 1);
+ error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
+ data, ifp, p));
+ socket_unlock(so, 1);
+
+ if (error == EOPNOTSUPP || error == ENOTSUP) {
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, data);
+ }
+ break;
+ }
+
+done:
+ if (ifs != NULL)
+ _FREE(ifs, M_DEVBUF);
+
+ if (if_verbose) {
+ if (ifname[0] == '\0')
+ (void) snprintf(ifname, sizeof (ifname), "%s",
+ "NULL");
+ else if (ifp != NULL)
+ (void) snprintf(ifname, sizeof (ifname), "%s",
+ if_name(ifp));
+
+ if (error != 0) {
+ printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
+ "%c %lu) error %d\n", __func__,
+ proc_name_address(p), proc_pid(p),
+ ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
+ (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
+ (char)IOCGROUP(cmd), cmd & 0xff, error);
+ } else if (if_verbose > 1) {
+ printf("%s[%s,%d]: ifp %s cmd 0x%08lx (%c%c [%lu] "
+ "%c %lu) OK\n", __func__,
+ proc_name_address(p), proc_pid(p),
+ ifname, cmd, (cmd & IOC_IN) ? 'I' : ' ',
+ (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd),
+ (char)IOCGROUP(cmd), cmd & 0xff);
+ }
+ }
+
+ return (error);
+}
+
+static __attribute__((noinline)) int
+ifioctl_ifreq(struct socket *so, u_long cmd, struct ifreq *ifr, struct proc *p)
+{
+ struct ifnet *ifp;
+ u_long ocmd = cmd;
+ int error = 0;
+ struct kev_msg ev_msg;
+ struct net_event_data ev_data;
+
+ bzero(&ev_data, sizeof (struct net_event_data));
+ bzero(&ev_msg, sizeof (struct kev_msg));
+
+ switch (cmd) {
+ case SIOCIFCREATE:
+ case SIOCIFCREATE2:
+ error = proc_suser(p);
+ if (error)
+ return (error);
+ return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
+ cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
+ case SIOCIFDESTROY:
+ error = proc_suser(p);
+ if (error)
+ return (error);
+ return (if_clone_destroy(ifr->ifr_name));
+ }
+
+ /*
+ * ioctls which require ifp. Note that we acquire dlil_ifnet_lock
+ * here to ensure that the ifnet, if found, has been fully attached.
+ */
+ dlil_if_lock();
+ ifp = ifunit(ifr->ifr_name);
+ dlil_if_unlock();
+
+ if (ifp == NULL)
+ return (ENXIO);
+
+ switch (cmd) {
+ case SIOCGIFFLAGS:
+ ifnet_lock_shared(ifp);
+ ifr->ifr_flags = ifp->if_flags;
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCGIFEFLAGS:
+ ifnet_lock_shared(ifp);
+ ifr->ifr_eflags = ifp->if_eflags;
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCGIFCAP:
+ ifnet_lock_shared(ifp);
+ ifr->ifr_reqcap = ifp->if_capabilities;
+ ifr->ifr_curcap = ifp->if_capenable;
+ ifnet_lock_done(ifp);
+ break;
+
+#if CONFIG_MACF_NET
+ case SIOCGIFMAC:
+ error = mac_ifnet_label_get(kauth_cred_get(), ifr, ifp);
+ break;
+
+ case SIOCSIFMAC:
+ error = mac_ifnet_label_set(kauth_cred_get(), ifr, ifp);
+ break;
+#endif /* CONFIG_MACF_NET */
+
+ case SIOCGIFMETRIC:
+ ifnet_lock_shared(ifp);
+ ifr->ifr_metric = ifp->if_metric;
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCGIFMTU:
+ ifnet_lock_shared(ifp);
+ ifr->ifr_mtu = ifp->if_mtu;
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCGIFPHYS:
+ ifnet_lock_shared(ifp);
+ ifr->ifr_phys = ifp->if_physical;
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCSIFFLAGS:
+ error = proc_suser(p);
+ if (error != 0)
+ break;
+
+ (void) ifnet_set_flags(ifp, ifr->ifr_flags,
+ (u_int16_t)~IFF_CANTCHANGE);
+
+ /*
+ * Note that we intentionally ignore any error from below
+ * for the SIOCSIFFLAGS case.
+ */
+ (void) ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
+
+ /*
+ * Send the event even upon error from the driver because
+ * we changed the flags.
+ */
+ ev_msg.vendor_code = KEV_VENDOR_APPLE;
+ ev_msg.kev_class = KEV_NETWORK_CLASS;
+ ev_msg.kev_subclass = KEV_DL_SUBCLASS;
+
+ ev_msg.event_code = KEV_DL_SIFFLAGS;
+ strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
+ ev_data.if_family = ifp->if_family;
+ ev_data.if_unit = (u_int32_t) ifp->if_unit;
+ ev_msg.dv[0].data_length = sizeof(struct net_event_data);
+ ev_msg.dv[0].data_ptr = &ev_data;
+ ev_msg.dv[1].data_length = 0;
+ kev_post_msg(&ev_msg);
+
+ ifnet_touch_lastchange(ifp);
+ break;
+
+ case SIOCSIFCAP:
+ error = proc_suser(p);
+ if (error != 0)
+ break;
+
+ if ((ifr->ifr_reqcap & ~ifp->if_capabilities)) {
+ error = EINVAL;
+ break;
+ }
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
+
+ ifnet_touch_lastchange(ifp);
+ break;
+
+ case SIOCSIFMETRIC:
+ error = proc_suser(p);
+ if (error != 0)
+ break;
+
+ ifp->if_metric = ifr->ifr_metric;
+
+ ev_msg.vendor_code = KEV_VENDOR_APPLE;
+ ev_msg.kev_class = KEV_NETWORK_CLASS;
+ ev_msg.kev_subclass = KEV_DL_SUBCLASS;
+
+ ev_msg.event_code = KEV_DL_SIFMETRICS;
+ strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
+ ev_data.if_family = ifp->if_family;
+ ev_data.if_unit = (u_int32_t) ifp->if_unit;
+ ev_msg.dv[0].data_length = sizeof(struct net_event_data);
+ ev_msg.dv[0].data_ptr = &ev_data;
+
+ ev_msg.dv[1].data_length = 0;
+ kev_post_msg(&ev_msg);
+
+ ifnet_touch_lastchange(ifp);
+ break;
+
+ case SIOCSIFPHYS:
+ error = proc_suser(p);
+ if (error != 0)
+ break;
+
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
+ if (error != 0)
+ break;
+
+ ev_msg.vendor_code = KEV_VENDOR_APPLE;
+ ev_msg.kev_class = KEV_NETWORK_CLASS;
+ ev_msg.kev_subclass = KEV_DL_SUBCLASS;
+
+ ev_msg.event_code = KEV_DL_SIFPHYS;
+ strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
+ ev_data.if_family = ifp->if_family;
+ ev_data.if_unit = (u_int32_t) ifp->if_unit;
+ ev_msg.dv[0].data_length = sizeof(struct net_event_data);
+ ev_msg.dv[0].data_ptr = &ev_data;
+ ev_msg.dv[1].data_length = 0;
+ kev_post_msg(&ev_msg);
+
+ ifnet_touch_lastchange(ifp);
+ break;
+
+ case SIOCSIFMTU: {
+ u_int32_t oldmtu = ifp->if_mtu;
+ struct ifclassq *ifq = &ifp->if_snd;
+
+ error = proc_suser(p);
+ if (error != 0)
+ break;
+
+ if (ifp->if_ioctl == NULL) {
+ error = EOPNOTSUPP;
+ break;
+ }
+ if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
+ error = EINVAL;
+ break;
+ }
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
+ if (error != 0)
+ break;
+
+ ev_msg.vendor_code = KEV_VENDOR_APPLE;
+ ev_msg.kev_class = KEV_NETWORK_CLASS;
+ ev_msg.kev_subclass = KEV_DL_SUBCLASS;
+
+ ev_msg.event_code = KEV_DL_SIFMTU;
+ strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
+ ev_data.if_family = ifp->if_family;
+ ev_data.if_unit = (u_int32_t) ifp->if_unit;
+ ev_msg.dv[0].data_length = sizeof(struct net_event_data);
+ ev_msg.dv[0].data_ptr = &ev_data;
+ ev_msg.dv[1].data_length = 0;
+ kev_post_msg(&ev_msg);
+
+ ifnet_touch_lastchange(ifp);
+ rt_ifmsg(ifp);
+
+ /*
+ * If the link MTU changed, do network layer specific procedure
+ * and update all route entries associated with the interface,
+ * so that their MTU metric gets updated.
+ */
+ if (ifp->if_mtu != oldmtu) {
+ if_rtmtu_update(ifp);
+#if INET6
+ nd6_setmtu(ifp);
+#endif /* INET6 */
+ /* Inform all transmit queues about the new MTU */
+ IFCQ_LOCK(ifq);
+ ifnet_update_sndq(ifq, CLASSQ_EV_LINK_MTU);
+ IFCQ_UNLOCK(ifq);
+ }
+ break;
+ }
+
+ case SIOCADDMULTI:
+ case SIOCDELMULTI:
+ error = proc_suser(p);
+ if (error != 0)
+ break;
+
+ /* Don't allow group membership on non-multicast interfaces. */
+ if ((ifp->if_flags & IFF_MULTICAST) == 0) {
+ error = EOPNOTSUPP;
+ break;
+ }
+
+ /* Don't let users screw up protocols' entries. */
+ if (ifr->ifr_addr.sa_family != AF_UNSPEC &&
+ ifr->ifr_addr.sa_family != AF_LINK) {
+ error = EINVAL;
+ break;
+ }
+
+ /*
+ * User is permitted to anonymously join a particular link
+ * multicast group via SIOCADDMULTI. Subsequent join requested
+ * for the same record which has an outstanding refcnt from a
+ * past if_addmulti_anon() will not result in EADDRINUSE error
+ * (unlike other BSDs.) Anonymously leaving a group is also
+ * allowed only as long as there is an outstanding refcnt held
+ * by a previous anonymous request, or else ENOENT (even if the
+ * link-layer multicast membership exists for a network-layer
+ * membership.)
+ */
+ if (cmd == SIOCADDMULTI) {
+ error = if_addmulti_anon(ifp, &ifr->ifr_addr, NULL);
+ ev_msg.event_code = KEV_DL_ADDMULTI;
+ } else {
+ error = if_delmulti_anon(ifp, &ifr->ifr_addr);
+ ev_msg.event_code = KEV_DL_DELMULTI;
+ }
+ if (error != 0)
+ break;
+
+ ev_msg.vendor_code = KEV_VENDOR_APPLE;
+ ev_msg.kev_class = KEV_NETWORK_CLASS;
+ ev_msg.kev_subclass = KEV_DL_SUBCLASS;
+ strlcpy(&ev_data.if_name[0], ifp->if_name, IFNAMSIZ);
+
+ ev_data.if_family = ifp->if_family;
+ ev_data.if_unit = (u_int32_t) ifp->if_unit;
+ ev_msg.dv[0].data_length = sizeof(struct net_event_data);
+ ev_msg.dv[0].data_ptr = &ev_data;
+ ev_msg.dv[1].data_length = 0;
+ kev_post_msg(&ev_msg);
+
+ ifnet_touch_lastchange(ifp);
+ break;
+
+ case SIOCDIFPHYADDR:
+ case SIOCSIFMEDIA:
+ case SIOCSIFGENERIC:
+ case SIOCSIFLLADDR:
+ case SIOCSIFALTMTU:
+ case SIOCSIFVLAN:
+ case SIOCSIFBOND:
+ error = proc_suser(p);
+ if (error != 0)
+ break;
+
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
+ if (error != 0)
+ break;
+
+ ifnet_touch_lastchange(ifp);
+ break;
+
+ case SIOCGIFLLADDR: {
+ struct sockaddr_dl *sdl = SDL(ifp->if_lladdr->ifa_addr);
+
+ if (sdl->sdl_alen == 0) {
+ error = EADDRNOTAVAIL;
+ break;
+ }
+ /* If larger than 14-bytes we'll need another mechanism */
+ if (sdl->sdl_alen > sizeof (ifr->ifr_addr.sa_data)) {
+ error = EMSGSIZE;
+ break;
+ }
+ /* Follow the same convention used by SIOCSIFLLADDR */
+ bzero(&ifr->ifr_addr, sizeof (ifr->ifr_addr));
+ ifr->ifr_addr.sa_family = AF_LINK;
+ ifr->ifr_addr.sa_len = sdl->sdl_alen;
+ error = ifnet_guarded_lladdr_copy_bytes(ifp,
+ &ifr->ifr_addr.sa_data, sdl->sdl_alen);
+ break;
+ }
+
+ case SIOCGIFTYPE:
+ ifr->ifr_type.ift_type = ifp->if_type;
+ ifr->ifr_type.ift_family = ifp->if_family;
+ ifr->ifr_type.ift_subfamily = ifp->if_subfamily;
+ break;
+
+ case SIOCGIFFUNCTIONALTYPE:
+ ifr->ifr_functional_type = if_functional_type(ifp);
+ break;
+
+ case SIOCGIFPSRCADDR:
+ case SIOCGIFPDSTADDR:
+ case SIOCGIFGENERIC:
+ case SIOCGIFDEVMTU:
+ case SIOCGIFVLAN:
+ case SIOCGIFBOND:
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd, (caddr_t)ifr);
+ break;
+
+ case SIOCGIFWAKEFLAGS:
+ ifnet_lock_shared(ifp);
+ ifr->ifr_wake_flags = ifnet_get_wake_flags(ifp);
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCGIFGETRTREFCNT:
+ ifnet_lock_shared(ifp);
+ ifr->ifr_route_refcnt = ifp->if_route_refcnt;
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCSIFOPPORTUNISTIC:
+ case SIOCGIFOPPORTUNISTIC:
+ error = ifnet_getset_opportunistic(ifp, cmd, ifr, p);
+ break;
+
+ case SIOCGIFLINKQUALITYMETRIC:
+ ifnet_lock_shared(ifp);
+ if ((ifp->if_interface_state.valid_bitmask &
+ IF_INTERFACE_STATE_LQM_STATE_VALID))
+ ifr->ifr_link_quality_metric =
+ ifp->if_interface_state.lqm_state;
+ else if ((ifp->if_refflags & IFRF_ATTACHED)) {
+ ifr->ifr_link_quality_metric =
+ IFNET_LQM_THRESH_UNKNOWN;
+ } else {
+ ifr->ifr_link_quality_metric =
+ IFNET_LQM_THRESH_OFF;
+ }
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCSIFLOG:
+ case SIOCGIFLOG:
+ error = ifnet_getset_log(ifp, cmd, ifr, p);
+ break;
+
+ case SIOCGIFDELEGATE:
+ ifnet_lock_shared(ifp);
+ ifr->ifr_delegated = ((ifp->if_delegated.ifp != NULL) ?
+ ifp->if_delegated.ifp->if_index : 0);
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCGIFEXPENSIVE:
+ ifnet_lock_shared(ifp);
+ if (ifp->if_eflags & IFEF_EXPENSIVE)
+ ifr->ifr_expensive = 1;
+ else
+ ifr->ifr_expensive = 0;
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCSIFEXPENSIVE:
+ {
+ struct ifnet *difp;
+
+ if ((error = priv_check_cred(kauth_cred_get(),
+ PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
+ return (error);
+ ifnet_lock_exclusive(ifp);
+ if (ifr->ifr_expensive)
+ ifp->if_eflags |= IFEF_EXPENSIVE;
+ else
+ ifp->if_eflags &= ~IFEF_EXPENSIVE;
+ ifnet_lock_done(ifp);
+ /*
+ * Update the expensive bit in the delegated interface
+ * structure.
+ */
+ ifnet_head_lock_shared();
+ TAILQ_FOREACH(difp, &ifnet_head, if_link) {
+ ifnet_lock_exclusive(difp);
+ if (difp->if_delegated.ifp == ifp) {
+ difp->if_delegated.expensive =
+ ifp->if_eflags & IFEF_EXPENSIVE ? 1 : 0;
+
+ }
+ ifnet_lock_done(difp);
+ }
+ ifnet_head_done();
+ break;
+ }
+
+ case SIOCGIF2KCL:
+ ifnet_lock_shared(ifp);
+ if (ifp->if_eflags & IFEF_2KCL)
+ ifr->ifr_2kcl = 1;
+ else
+ ifr->ifr_2kcl = 0;
+ ifnet_lock_done(ifp);
+ break;
+
+ case SIOCSIF2KCL:
+ if ((error = priv_check_cred(kauth_cred_get(),
+ PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
+ return (error);
+ ifnet_lock_exclusive(ifp);
+ if (ifr->ifr_2kcl)
+ ifp->if_eflags |= IFEF_2KCL;
+ else
+ ifp->if_eflags &= ~IFEF_2KCL;
+ ifnet_lock_done(ifp);
+ break;
+ case SIOCGSTARTDELAY:
+ ifnet_lock_shared(ifp);
+ if (ifp->if_eflags & IFEF_ENQUEUE_MULTI) {
+ ifr->ifr_start_delay_qlen =
+ ifp->if_start_delay_qlen;
+ ifr->ifr_start_delay_timeout =
+ ifp->if_start_delay_timeout;
+ } else {
+ ifr->ifr_start_delay_qlen = 0;
+ ifr->ifr_start_delay_timeout = 0;
+ }
+ ifnet_lock_done(ifp);
+ break;
+ case SIOCSIFDSTADDR:
+ case SIOCSIFADDR:
+ case SIOCSIFBRDADDR:
+ case SIOCSIFNETMASK:
+ case OSIOCGIFADDR:
+ case OSIOCGIFDSTADDR:
+ case OSIOCGIFBRDADDR:
+ case OSIOCGIFNETMASK:
+ case SIOCSIFKPI:
+ VERIFY(so->so_proto != NULL);
+
+ if (cmd == SIOCSIFDSTADDR || cmd == SIOCSIFADDR ||
+ cmd == SIOCSIFBRDADDR || cmd == SIOCSIFNETMASK) {
+#if BYTE_ORDER != BIG_ENDIAN
+ if (ifr->ifr_addr.sa_family == 0 &&
+ ifr->ifr_addr.sa_len < 16) {
+ ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
+ ifr->ifr_addr.sa_len = 16;
+ }
+#else
+ if (ifr->ifr_addr.sa_len == 0)
+ ifr->ifr_addr.sa_len = 16;
+#endif
+ } else if (cmd == OSIOCGIFADDR) {
+ cmd = SIOCGIFADDR; /* struct ifreq */
+ } else if (cmd == OSIOCGIFDSTADDR) {
+ cmd = SIOCGIFDSTADDR; /* struct ifreq */
+ } else if (cmd == OSIOCGIFBRDADDR) {
+ cmd = SIOCGIFBRDADDR; /* struct ifreq */
+ } else if (cmd == OSIOCGIFNETMASK) {
+ cmd = SIOCGIFNETMASK; /* struct ifreq */
+ }
+
+ socket_lock(so, 1);
+ error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
+ (caddr_t)ifr, ifp, p));
+ socket_unlock(so, 1);
+
+ switch (ocmd) {
+ case OSIOCGIFADDR:
+ case OSIOCGIFDSTADDR:
+ case OSIOCGIFBRDADDR:
+ case OSIOCGIFNETMASK:
+ bcopy(&ifr->ifr_addr.sa_family, &ifr->ifr_addr,
+ sizeof (u_short));
+ }
+
+ if (cmd == SIOCSIFKPI) {
+ int temperr = proc_suser(p);
+ if (temperr != 0)
+ error = temperr;
+ }
+
+ if (error == EOPNOTSUPP || error == ENOTSUP) {
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), cmd,
+ (caddr_t)ifr);
+ }
+ break;
+
+ case SIOCGIFINTERFACESTATE:
+ if_get_state(ifp, &ifr->ifr_interface_state);
+
+ break;
+ case SIOCSIFINTERFACESTATE:
+ if ((error = priv_check_cred(kauth_cred_get(),
+ PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
+ return (error);
+
+ error = if_state_update(ifp, &ifr->ifr_interface_state);
+
+ break;
+ case SIOCSIFPROBECONNECTIVITY:
+ if ((error = priv_check_cred(kauth_cred_get(),
+ PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
+ return (error);
+ error = if_probe_connectivity(ifp,
+ ifr->ifr_probe_connectivity);
+ break;
+ case SIOCGIFPROBECONNECTIVITY:
+ if ((error = priv_check_cred(kauth_cred_get(),
+ PRIV_NET_INTERFACE_CONTROL, 0)) != 0)
+ return (error);
+ if (ifp->if_eflags & IFEF_PROBE_CONNECTIVITY)
+ ifr->ifr_probe_connectivity = 1;
+ else
+ ifr->ifr_probe_connectivity = 0;
+ break;
+ default:
+ VERIFY(0);
+ /* NOTREACHED */
+ }
+
+ return (error);
+}
+
+int
+ifioctllocked(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
+{
+ int error;
+
+ socket_unlock(so, 0);
+ error = ifioctl(so, cmd, data, p);
+ socket_lock(so, 0);
+ return(error);
+}
+
+/*
+ * Set/clear promiscuous mode on interface ifp based on the truth value
+ * of pswitch. The calls are reference counted so that only the first
+ * "on" request actually has an effect, as does the final "off" request.
+ * Results are undefined if the "off" and "on" requests are not matched.
+ */
+errno_t