+/*
+ * Given a media word, return one suitable for an application
+ * using the original encoding.
+ */
+static int
+compat_media(int media)
+{
+ if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
+ media &= ~IFM_TMASK;
+ media |= IFM_OTHER;
+ }
+ return media;
+}
+
+static int
+compat_ifmu_ulist(struct ifnet *ifp, u_long cmd, void *data)
+{
+ struct ifmediareq *ifmr = (struct ifmediareq *)data;
+ user_addr_t user_addr;
+ int i;
+ int *media_list = NULL;
+ int error = 0;
+ bool list_modified = false;
+
+ user_addr = (cmd == SIOCGIFMEDIA64) ?
+ ((struct ifmediareq64 *)ifmr)->ifmu_ulist :
+ CAST_USER_ADDR_T(((struct ifmediareq32 *)ifmr)->ifmu_ulist);
+ if (user_addr == USER_ADDR_NULL || ifmr->ifm_count == 0) {
+ return 0;
+ }
+ MALLOC(media_list, int *, ifmr->ifm_count * sizeof(int),
+ M_TEMP, M_WAITOK | M_ZERO);
+ if (media_list == NULL) {
+ os_log_error(OS_LOG_DEFAULT,
+ "%s: %s MALLOC() failed",
+ __func__, ifp->if_xname);
+ error = ENOMEM;
+ goto done;
+ }
+ error = copyin(user_addr, media_list, ifmr->ifm_count * sizeof(int));
+ if (error != 0) {
+ os_log_error(OS_LOG_DEFAULT,
+ "%s: %s copyin() error %d",
+ __func__, ifp->if_xname, error);
+ goto done;
+ }
+ for (i = 0; i < ifmr->ifm_count; i++) {
+ int old_media, new_media;
+
+ old_media = media_list[i];
+
+ new_media = compat_media(old_media);
+ if (new_media == old_media) {
+ continue;
+ }
+ if (if_verbose != 0) {
+ os_log_info(OS_LOG_DEFAULT,
+ "%s: %s converted extended media %08x to compat media %08x",
+ __func__, ifp->if_xname, old_media, new_media);
+ }
+ media_list[i] = new_media;
+ list_modified = true;
+ }
+ if (list_modified) {
+ error = copyout(media_list, user_addr, ifmr->ifm_count * sizeof(int));
+ if (error != 0) {
+ os_log_error(OS_LOG_DEFAULT,
+ "%s: %s copyout() error %d",
+ __func__, ifp->if_xname, error);
+ goto done;
+ }
+ }
+done:
+ if (media_list != NULL) {
+ FREE(media_list, M_TEMP);
+ }
+ return error;
+}
+
+static int
+compat_ifmediareq(struct ifnet *ifp, u_long cmd, void *data)
+{
+ struct ifmediareq *ifmr = (struct ifmediareq *)data;
+ int error;
+
+ ifmr->ifm_active = compat_media(ifmr->ifm_active);
+ ifmr->ifm_current = compat_media(ifmr->ifm_current);
+
+ error = compat_ifmu_ulist(ifp, cmd, data);
+
+ return error;
+}
+
+static int
+ifioctl_get_media(struct ifnet *ifp, struct socket *so, u_long cmd, caddr_t data)
+{
+ int error = 0;
+
+ /*
+ * An ifnet must not implement SIOCGIFXMEDIA as it gets the extended
+ * media subtypes macros from <net/if_media.h>
+ */
+ switch (cmd) {
+ case SIOCGIFMEDIA32:
+ case SIOCGIFXMEDIA32:
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), SIOCGIFMEDIA32, data);
+ break;
+ case SIOCGIFMEDIA64:
+ case SIOCGIFXMEDIA64:
+ error = ifnet_ioctl(ifp, SOCK_DOM(so), SIOCGIFMEDIA64, data);
+ break;
+ }
+ if (if_verbose != 0 && error != 0) {
+ os_log(OS_LOG_DEFAULT, "%s: first ifnet_ioctl(%s, %08lx) error %d",
+ __func__, ifp->if_xname, cmd, error);
+ }
+ if (error == 0 && (cmd == SIOCGIFMEDIA32 || cmd == SIOCGIFMEDIA64)) {
+ error = compat_ifmediareq(ifp, cmd, data);
+ }
+ return error;
+}