if_media.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* $NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $ */
  2. /*-
  3. * SPDX-License-Identifier: BSD-4-Clause
  4. *
  5. * Copyright (c) 1997
  6. * Jonathan Stone and Jason R. Thorpe. All rights reserved.
  7. *
  8. * This software is derived from information provided by Matt Thomas.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. All advertising materials mentioning features or use of this software
  19. * must display the following acknowledgement:
  20. * This product includes software developed by Jonathan Stone
  21. * and Jason R. Thorpe for the NetBSD Project.
  22. * 4. The names of the authors may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  26. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  32. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. /*
  38. * BSD/OS-compatible network interface media selection.
  39. *
  40. * Where it is safe to do so, this code strays slightly from the BSD/OS
  41. * design. Software which uses the API (device drivers, basically)
  42. * shouldn't notice any difference.
  43. *
  44. * Many thanks to Matt Thomas for providing the information necessary
  45. * to implement this interface.
  46. */
  47. #include <sys/cdefs.h>
  48. #include "opt_ifmedia.h"
  49. #include <sys/param.h>
  50. #include <sys/systm.h>
  51. #include <sys/socket.h>
  52. #include <sys/sockio.h>
  53. #include <sys/malloc.h>
  54. #include <sys/module.h>
  55. #include <sys/sysctl.h>
  56. #include <net/if.h>
  57. #include <net/if_media.h>
  58. /*
  59. * Compile-time options:
  60. * IFMEDIA_DEBUG:
  61. * turn on implementation-level debug printfs.
  62. * Useful for debugging newly-ported drivers.
  63. */
  64. static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
  65. int flags, int mask);
  66. #ifdef IFMEDIA_DEBUG
  67. #include <net/if_var.h>
  68. #include <net/if_private.h>
  69. int ifmedia_debug = 0;
  70. SYSCTL_INT(_debug, OID_AUTO, ifmedia, CTLFLAG_RW, &ifmedia_debug,
  71. 0, "if_media debugging msgs");
  72. static void ifmedia_printword(int);
  73. #endif
  74. /*
  75. * Initialize if_media struct for a specific interface instance.
  76. */
  77. void
  78. ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
  79. ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
  80. {
  81. LIST_INIT(&ifm->ifm_list);
  82. ifm->ifm_cur = NULL;
  83. ifm->ifm_media = 0;
  84. ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
  85. ifm->ifm_change = change_callback;
  86. ifm->ifm_status = status_callback;
  87. }
  88. void
  89. ifmedia_removeall(struct ifmedia *ifm)
  90. {
  91. struct ifmedia_entry *entry;
  92. while ((entry = LIST_FIRST(&ifm->ifm_list)) != NULL) {
  93. LIST_REMOVE(entry, ifm_list);
  94. free(entry, M_IFADDR);
  95. }
  96. ifm->ifm_cur = NULL;
  97. }
  98. /*
  99. * Add a media configuration to the list of supported media
  100. * for a specific interface instance.
  101. */
  102. void
  103. ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
  104. {
  105. struct ifmedia_entry *entry;
  106. #ifdef IFMEDIA_DEBUG
  107. if (ifmedia_debug) {
  108. if (ifm == NULL) {
  109. printf("ifmedia_add: null ifm\n");
  110. return;
  111. }
  112. printf("Adding entry for (%#010x) ", mword);
  113. ifmedia_printword(mword);
  114. }
  115. #endif
  116. entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
  117. if (entry == NULL)
  118. panic("ifmedia_add: can't malloc entry");
  119. entry->ifm_media = mword;
  120. entry->ifm_data = data;
  121. entry->ifm_aux = aux;
  122. LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
  123. }
  124. /*
  125. * Add an array of media configurations to the list of
  126. * supported media for a specific interface instance.
  127. */
  128. void
  129. ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count)
  130. {
  131. int i;
  132. for (i = 0; i < count; i++)
  133. ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
  134. lp[i].ifm_aux);
  135. }
  136. /*
  137. * Set the default active media.
  138. *
  139. * Called by device-specific code which is assumed to have already
  140. * selected the default media in hardware. We do _not_ call the
  141. * media-change callback.
  142. */
  143. void
  144. ifmedia_set(struct ifmedia *ifm, int target)
  145. {
  146. struct ifmedia_entry *match;
  147. match = ifmedia_match(ifm, target, ifm->ifm_mask);
  148. if (match == NULL) {
  149. printf("ifmedia_set: no match for 0x%x/0x%x\n",
  150. target, ~ifm->ifm_mask);
  151. panic("ifmedia_set");
  152. }
  153. ifm->ifm_cur = match;
  154. #ifdef IFMEDIA_DEBUG
  155. if (ifmedia_debug) {
  156. printf("ifmedia_set: target ");
  157. ifmedia_printword(target);
  158. printf("ifmedia_set: setting to ");
  159. ifmedia_printword(ifm->ifm_cur->ifm_media);
  160. }
  161. #endif
  162. }
  163. /*
  164. * Given a media word, return one suitable for an application
  165. * using the original encoding.
  166. */
  167. static int
  168. compat_media(int media)
  169. {
  170. if (IFM_TYPE(media) == IFM_ETHER && IFM_SUBTYPE(media) > IFM_OTHER) {
  171. media &= ~(IFM_ETH_XTYPE|IFM_TMASK);
  172. media |= IFM_OTHER;
  173. }
  174. return (media);
  175. }
  176. /*
  177. * Device-independent media ioctl support function.
  178. */
  179. int
  180. ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
  181. u_long cmd)
  182. {
  183. struct ifmedia_entry *match;
  184. struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
  185. int error = 0;
  186. if (ifp == NULL || ifr == NULL || ifm == NULL)
  187. return (EINVAL);
  188. switch (cmd) {
  189. /*
  190. * Set the current media.
  191. */
  192. case SIOCSIFMEDIA:
  193. {
  194. struct ifmedia_entry *oldentry;
  195. int oldmedia;
  196. int newmedia = ifr->ifr_media;
  197. match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
  198. if (match == NULL) {
  199. #ifdef IFMEDIA_DEBUG
  200. if (ifmedia_debug) {
  201. printf(
  202. "ifmedia_ioctl: no media found for %#010x mask %#010x\n",
  203. newmedia, ifm->ifm_mask);
  204. }
  205. #endif
  206. return (ENXIO);
  207. }
  208. /*
  209. * If no change, we're done.
  210. * XXX Automedia may invole software intervention.
  211. * Keep going in case the connected media changed.
  212. * Similarly, if best match changed (kernel debugger?).
  213. */
  214. if (IFM_SUBTYPE(newmedia) != IFM_AUTO &&
  215. newmedia == ifm->ifm_media && match == ifm->ifm_cur)
  216. return (0);
  217. /*
  218. * We found a match, now make the driver switch to it.
  219. * Make sure to preserve our old media type in case the
  220. * driver can't switch.
  221. */
  222. #ifdef IFMEDIA_DEBUG
  223. if (ifmedia_debug) {
  224. printf("ifmedia_ioctl: switching %s to ",
  225. ifp->if_xname);
  226. ifmedia_printword(match->ifm_media);
  227. }
  228. #endif
  229. oldentry = ifm->ifm_cur;
  230. oldmedia = ifm->ifm_media;
  231. ifm->ifm_cur = match;
  232. ifm->ifm_media = newmedia;
  233. error = (*ifm->ifm_change)(ifp);
  234. if (error) {
  235. ifm->ifm_cur = oldentry;
  236. ifm->ifm_media = oldmedia;
  237. }
  238. break;
  239. }
  240. /*
  241. * Get list of available media and current media on interface.
  242. */
  243. case SIOCGIFMEDIA:
  244. case SIOCGIFXMEDIA:
  245. {
  246. struct ifmedia_entry *ep;
  247. int i;
  248. if (ifmr->ifm_count < 0)
  249. return (EINVAL);
  250. if (cmd == SIOCGIFMEDIA) {
  251. ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
  252. compat_media(ifm->ifm_cur->ifm_media) : IFM_NONE;
  253. } else {
  254. ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
  255. ifm->ifm_cur->ifm_media : IFM_NONE;
  256. }
  257. ifmr->ifm_mask = ifm->ifm_mask;
  258. ifmr->ifm_status = 0;
  259. (*ifm->ifm_status)(ifp, ifmr);
  260. /*
  261. * If there are more interfaces on the list, count
  262. * them. This allows the caller to set ifmr->ifm_count
  263. * to 0 on the first call to know how much space to
  264. * allocate.
  265. */
  266. i = 0;
  267. LIST_FOREACH(ep, &ifm->ifm_list, ifm_list) {
  268. if (i < ifmr->ifm_count) {
  269. error = copyout(&ep->ifm_media,
  270. ifmr->ifm_ulist + i, sizeof(int));
  271. if (error != 0)
  272. break;
  273. }
  274. i++;
  275. }
  276. if (error == 0 && i > ifmr->ifm_count)
  277. error = ifmr->ifm_count != 0 ? E2BIG : 0;
  278. ifmr->ifm_count = i;
  279. break;
  280. }
  281. default:
  282. return (EINVAL);
  283. }
  284. return (error);
  285. }
  286. /*
  287. * Find media entry matching a given ifm word.
  288. *
  289. */
  290. static struct ifmedia_entry *
  291. ifmedia_match(struct ifmedia *ifm, int target, int mask)
  292. {
  293. struct ifmedia_entry *match, *next;
  294. match = NULL;
  295. mask = ~mask;
  296. LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
  297. if ((next->ifm_media & mask) == (target & mask)) {
  298. #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
  299. if (match) {
  300. printf("ifmedia_match: multiple match for "
  301. "%#010x/%#010x\n", target, mask);
  302. }
  303. #endif
  304. match = next;
  305. }
  306. }
  307. return (match);
  308. }
  309. /*
  310. * Compute the interface `baudrate' from the media, for the interface
  311. * metrics (used by routing daemons).
  312. */
  313. static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
  314. IFM_BAUDRATE_DESCRIPTIONS;
  315. uint64_t
  316. ifmedia_baudrate(int mword)
  317. {
  318. int i;
  319. for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
  320. if (IFM_TYPE_MATCH(mword, ifmedia_baudrate_descriptions[i].
  321. ifmb_word))
  322. return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
  323. }
  324. /* Not known. */
  325. return (0);
  326. }
  327. #ifdef IFMEDIA_DEBUG
  328. static const struct ifmedia_description ifm_type_descriptions[] =
  329. IFM_TYPE_DESCRIPTIONS;
  330. static const struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
  331. IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
  332. static const struct ifmedia_description
  333. ifm_subtype_ethernet_option_descriptions[] =
  334. IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
  335. static const struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
  336. IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
  337. static const struct ifmedia_description
  338. ifm_subtype_ieee80211_option_descriptions[] =
  339. IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
  340. static const struct ifmedia_description
  341. ifm_subtype_ieee80211_mode_descriptions[] =
  342. IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS;
  343. static const struct ifmedia_description ifm_subtype_atm_descriptions[] =
  344. IFM_SUBTYPE_ATM_DESCRIPTIONS;
  345. static const struct ifmedia_description ifm_subtype_atm_option_descriptions[] =
  346. IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS;
  347. static const struct ifmedia_description ifm_subtype_shared_descriptions[] =
  348. IFM_SUBTYPE_SHARED_DESCRIPTIONS;
  349. static const struct ifmedia_description ifm_shared_option_descriptions[] =
  350. IFM_SHARED_OPTION_DESCRIPTIONS;
  351. struct ifmedia_type_to_subtype {
  352. const struct ifmedia_description *subtypes;
  353. const struct ifmedia_description *options;
  354. const struct ifmedia_description *modes;
  355. };
  356. /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
  357. static const struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
  358. {
  359. &ifm_subtype_ethernet_descriptions[0],
  360. &ifm_subtype_ethernet_option_descriptions[0],
  361. NULL,
  362. },
  363. {
  364. &ifm_subtype_ieee80211_descriptions[0],
  365. &ifm_subtype_ieee80211_option_descriptions[0],
  366. &ifm_subtype_ieee80211_mode_descriptions[0]
  367. },
  368. {
  369. &ifm_subtype_atm_descriptions[0],
  370. &ifm_subtype_atm_option_descriptions[0],
  371. NULL,
  372. },
  373. };
  374. /*
  375. * print a media word.
  376. */
  377. static void
  378. ifmedia_printword(int ifmw)
  379. {
  380. const struct ifmedia_description *desc;
  381. const struct ifmedia_type_to_subtype *ttos;
  382. int seen_option = 0;
  383. /* Find the top-level interface type. */
  384. for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
  385. desc->ifmt_string != NULL; desc++, ttos++)
  386. if (IFM_TYPE(ifmw) == desc->ifmt_word)
  387. break;
  388. if (desc->ifmt_string == NULL) {
  389. printf("<unknown type>\n");
  390. return;
  391. }
  392. printf("%s", desc->ifmt_string);
  393. /* Any mode. */
  394. for (desc = ttos->modes; desc && desc->ifmt_string != NULL; desc++)
  395. if (IFM_MODE(ifmw) == desc->ifmt_word) {
  396. if (desc->ifmt_string != NULL)
  397. printf(" mode %s", desc->ifmt_string);
  398. break;
  399. }
  400. /*
  401. * Check for the shared subtype descriptions first, then the
  402. * type-specific ones.
  403. */
  404. for (desc = ifm_subtype_shared_descriptions;
  405. desc->ifmt_string != NULL; desc++)
  406. if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
  407. goto got_subtype;
  408. for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
  409. if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
  410. break;
  411. if (desc->ifmt_string == NULL) {
  412. printf(" <unknown subtype>\n");
  413. return;
  414. }
  415. got_subtype:
  416. printf(" %s", desc->ifmt_string);
  417. /*
  418. * Look for shared options.
  419. */
  420. for (desc = ifm_shared_option_descriptions;
  421. desc->ifmt_string != NULL; desc++) {
  422. if (ifmw & desc->ifmt_word) {
  423. if (seen_option == 0)
  424. printf(" <");
  425. printf("%s%s", seen_option++ ? "," : "",
  426. desc->ifmt_string);
  427. }
  428. }
  429. /*
  430. * Look for subtype-specific options.
  431. */
  432. for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
  433. if (ifmw & desc->ifmt_word) {
  434. if (seen_option == 0)
  435. printf(" <");
  436. printf("%s%s", seen_option++ ? "," : "",
  437. desc->ifmt_string);
  438. }
  439. }
  440. printf("%s\n", seen_option ? ">" : "");
  441. }
  442. #endif /* IFMEDIA_DEBUG */