if_media.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* $OpenBSD: if_media.c,v 1.26 2015/03/14 03:38:51 jsg Exp $ */
  2. /* $NetBSD: if_media.c,v 1.10 2000/03/13 23:52:39 soren Exp $ */
  3. /*-
  4. * Copyright (c) 1998 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
  9. * NASA Ames Research Center.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. * Copyright (c) 1997
  34. * Jonathan Stone and Jason R. Thorpe. All rights reserved.
  35. *
  36. * This software is derived from information provided by Matt Thomas.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. * 1. Redistributions of source code must retain the above copyright
  42. * notice, this list of conditions and the following disclaimer.
  43. * 2. Redistributions in binary form must reproduce the above copyright
  44. * notice, this list of conditions and the following disclaimer in the
  45. * documentation and/or other materials provided with the distribution.
  46. * 3. All advertising materials mentioning features or use of this software
  47. * must display the following acknowledgement:
  48. * This product includes software developed by Jonathan Stone
  49. * and Jason R. Thorpe for the NetBSD Project.
  50. * 4. The names of the authors may not be used to endorse or promote products
  51. * derived from this software without specific prior written permission.
  52. *
  53. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  54. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  55. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  56. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  57. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  58. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  59. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  60. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  61. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  62. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  63. * SUCH DAMAGE.
  64. */
  65. /*
  66. * BSD/OS-compatible network interface media selection.
  67. *
  68. * Where it is safe to do so, this code strays slightly from the BSD/OS
  69. * design. Software which uses the API (device drivers, basically)
  70. * shouldn't notice any difference.
  71. *
  72. * Many thanks to Matt Thomas for providing the information necessary
  73. * to implement this interface.
  74. */
  75. #include <sys/param.h>
  76. #include <sys/systm.h>
  77. #include <sys/errno.h>
  78. #include <sys/ioctl.h>
  79. #include <sys/socket.h>
  80. #include <sys/malloc.h>
  81. #include <net/if.h>
  82. #include <net/if_media.h>
  83. #include <net/netisr.h>
  84. /*
  85. * Compile-time options:
  86. * IFMEDIA_DEBUG:
  87. * turn on implementation-level debug printfs.
  88. * Useful for debugging newly-ported drivers.
  89. */
  90. #ifdef IFMEDIA_DEBUG
  91. int ifmedia_debug = 0;
  92. static void ifmedia_printword(int);
  93. #endif
  94. /*
  95. * Initialize if_media struct for a specific interface instance.
  96. */
  97. void
  98. ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
  99. ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
  100. {
  101. TAILQ_INIT(&ifm->ifm_list);
  102. ifm->ifm_cur = NULL;
  103. ifm->ifm_media = 0;
  104. ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
  105. ifm->ifm_change = change_callback;
  106. ifm->ifm_status = status_callback;
  107. }
  108. /*
  109. * Add a media configuration to the list of supported media
  110. * for a specific interface instance.
  111. */
  112. void
  113. ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
  114. {
  115. struct ifmedia_entry *entry;
  116. #ifdef IFMEDIA_DEBUG
  117. if (ifmedia_debug) {
  118. if (ifm == NULL) {
  119. printf("ifmedia_add: null ifm\n");
  120. return;
  121. }
  122. printf("Adding entry for ");
  123. ifmedia_printword(mword);
  124. }
  125. #endif
  126. entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
  127. if (entry == NULL)
  128. panic("ifmedia_add: can't malloc entry");
  129. entry->ifm_media = mword;
  130. entry->ifm_data = data;
  131. entry->ifm_aux = aux;
  132. TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list);
  133. }
  134. /*
  135. * Add an array of media configurations to the list of
  136. * supported media for a specific interface instance.
  137. */
  138. void
  139. ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count)
  140. {
  141. int i;
  142. for (i = 0; i < count; i++)
  143. ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
  144. lp[i].ifm_aux);
  145. }
  146. /*
  147. * Set the default active media.
  148. *
  149. * Called by device-specific code which is assumed to have already
  150. * selected the default media in hardware. We do _not_ call the
  151. * media-change callback.
  152. */
  153. void
  154. ifmedia_set(struct ifmedia *ifm, int target)
  155. {
  156. struct ifmedia_entry *match;
  157. match = ifmedia_match(ifm, target, ifm->ifm_mask);
  158. /*
  159. * If we didn't find the requested media, then we try to fall
  160. * back to target-type (IFM_ETHER, e.g.) | IFM_NONE. If that's
  161. * not on the list, then we add it and set the media to it.
  162. *
  163. * Since ifmedia_set is almost always called with IFM_AUTO or
  164. * with a known-good media, this really should only occur if we:
  165. *
  166. * a) didn't find any PHYs, or
  167. * b) didn't find an autoselect option on the PHY when the
  168. * parent ethernet driver expected to.
  169. *
  170. * In either case, it makes sense to select no media.
  171. */
  172. if (match == NULL) {
  173. printf("ifmedia_set: no match for 0x%x/0x%x\n",
  174. target, ~ifm->ifm_mask);
  175. target = (target & IFM_NMASK) | IFM_NONE;
  176. match = ifmedia_match(ifm, target, ifm->ifm_mask);
  177. if (match == NULL) {
  178. ifmedia_add(ifm, target, 0, NULL);
  179. match = ifmedia_match(ifm, target, ifm->ifm_mask);
  180. if (match == NULL)
  181. panic("ifmedia_set failed");
  182. }
  183. }
  184. ifm->ifm_cur = match;
  185. #ifdef IFMEDIA_DEBUG
  186. if (ifmedia_debug) {
  187. printf("ifmedia_set: target ");
  188. ifmedia_printword(target);
  189. printf("ifmedia_set: setting to ");
  190. ifmedia_printword(ifm->ifm_cur->ifm_media);
  191. }
  192. #endif
  193. }
  194. /*
  195. * Device-independent media ioctl support function.
  196. */
  197. int
  198. ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
  199. u_long cmd)
  200. {
  201. struct ifmedia_entry *match;
  202. struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
  203. int error = 0;
  204. if (ifp == NULL || ifr == NULL || ifm == NULL)
  205. return (EINVAL);
  206. switch (cmd) {
  207. /*
  208. * Set the current media.
  209. */
  210. case SIOCSIFMEDIA:
  211. {
  212. struct ifmedia_entry *oldentry;
  213. u_int oldmedia;
  214. u_int newmedia = ifr->ifr_media;
  215. match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
  216. if (match == NULL) {
  217. #ifdef IFMEDIA_DEBUG
  218. if (ifmedia_debug) {
  219. printf("ifmedia_ioctl: no media found for 0x%x\n",
  220. newmedia);
  221. }
  222. #endif
  223. return (EINVAL);
  224. }
  225. /*
  226. * If no change, we're done.
  227. * XXX Automedia may involve software intervention.
  228. * Keep going in case the connected media changed.
  229. * Similarly, if best match changed (kernel debugger?).
  230. */
  231. if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
  232. (newmedia == ifm->ifm_media) &&
  233. (match == ifm->ifm_cur))
  234. return (0);
  235. /*
  236. * We found a match, now make the driver switch to it.
  237. * Make sure to preserve our old media type in case the
  238. * driver can't switch.
  239. */
  240. #ifdef IFMEDIA_DEBUG
  241. if (ifmedia_debug) {
  242. printf("ifmedia_ioctl: switching %s to ",
  243. ifp->if_xname);
  244. ifmedia_printword(match->ifm_media);
  245. }
  246. #endif
  247. oldentry = ifm->ifm_cur;
  248. oldmedia = ifm->ifm_media;
  249. ifm->ifm_cur = match;
  250. ifm->ifm_media = newmedia;
  251. error = (*ifm->ifm_change)(ifp);
  252. if (error) {
  253. ifm->ifm_cur = oldentry;
  254. ifm->ifm_media = oldmedia;
  255. }
  256. break;
  257. }
  258. /*
  259. * Get list of available media and current media on interface.
  260. */
  261. case SIOCGIFMEDIA:
  262. {
  263. struct ifmedia_entry *ep;
  264. size_t nwords;
  265. if (ifmr->ifm_count < 0)
  266. return (EINVAL);
  267. ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
  268. ifm->ifm_cur->ifm_media : IFM_NONE;
  269. ifmr->ifm_mask = ifm->ifm_mask;
  270. ifmr->ifm_status = 0;
  271. (*ifm->ifm_status)(ifp, ifmr);
  272. /*
  273. * Count them so we know a-priori how much is the max we'll
  274. * need.
  275. */
  276. ep = TAILQ_FIRST(&ifm->ifm_list);
  277. for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
  278. nwords++;
  279. if (ifmr->ifm_count != 0) {
  280. size_t minwords;
  281. int *kptr;
  282. minwords = nwords > (size_t)ifmr->ifm_count ?
  283. (size_t)ifmr->ifm_count : nwords;
  284. kptr = mallocarray(nwords, sizeof(int), M_TEMP,
  285. M_WAITOK | M_ZERO);
  286. /*
  287. * Get the media words from the interface's list.
  288. */
  289. ep = TAILQ_FIRST(&ifm->ifm_list);
  290. for (nwords = 0; ep != NULL && nwords < minwords;
  291. ep = TAILQ_NEXT(ep, ifm_list))
  292. kptr[nwords++] = ep->ifm_media;
  293. if (ep == NULL)
  294. error = copyout(kptr, ifmr->ifm_ulist,
  295. nwords * sizeof(int));
  296. else
  297. error = E2BIG;
  298. free(kptr, M_TEMP, 0);
  299. }
  300. ifmr->ifm_count = nwords;
  301. break;
  302. }
  303. default:
  304. return (ENOTTY);
  305. }
  306. return (error);
  307. }
  308. /*
  309. * Find media entry matching a given ifm word.
  310. */
  311. struct ifmedia_entry *
  312. ifmedia_match(struct ifmedia *ifm, u_int target, u_int mask)
  313. {
  314. struct ifmedia_entry *match, *next;
  315. match = NULL;
  316. mask = ~mask;
  317. TAILQ_FOREACH(next, &ifm->ifm_list, ifm_list) {
  318. if ((next->ifm_media & mask) == (target & mask)) {
  319. if (match) {
  320. #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
  321. printf("ifmedia_match: multiple match for "
  322. "0x%x/0x%x, selected instance %d\n",
  323. target, mask, IFM_INST(match->ifm_media));
  324. #endif
  325. break;
  326. }
  327. match = next;
  328. }
  329. }
  330. return (match);
  331. }
  332. /*
  333. * Delete all media for a given instance.
  334. */
  335. void
  336. ifmedia_delete_instance(struct ifmedia *ifm, u_int inst)
  337. {
  338. struct ifmedia_entry *ife, *nife;
  339. TAILQ_FOREACH_SAFE(ife, &ifm->ifm_list, ifm_list, nife) {
  340. if (inst == IFM_INST_ANY ||
  341. inst == IFM_INST(ife->ifm_media)) {
  342. TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
  343. free(ife, M_IFADDR, 0);
  344. }
  345. }
  346. }
  347. /*
  348. * Compute the interface `baudrate' from the media, for the interface
  349. * metrics (used by routing daemons).
  350. */
  351. struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
  352. IFM_BAUDRATE_DESCRIPTIONS;
  353. u_int64_t
  354. ifmedia_baudrate(int mword)
  355. {
  356. int i;
  357. for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
  358. if ((mword & (IFM_NMASK|IFM_TMASK)) ==
  359. ifmedia_baudrate_descriptions[i].ifmb_word)
  360. return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
  361. }
  362. /* Not known. */
  363. return (0);
  364. }
  365. #ifdef IFMEDIA_DEBUG
  366. struct ifmedia_description ifm_type_descriptions[] =
  367. IFM_TYPE_DESCRIPTIONS;
  368. struct ifmedia_description ifm_subtype_descriptions[] =
  369. IFM_SUBTYPE_DESCRIPTIONS;
  370. struct ifmedia_description ifm_option_descriptions[] =
  371. IFM_OPTION_DESCRIPTIONS;
  372. /*
  373. * print a media word.
  374. */
  375. static void
  376. ifmedia_printword(int ifmw)
  377. {
  378. struct ifmedia_description *desc;
  379. int seen_option = 0;
  380. /* Print the top-level interface type. */
  381. for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
  382. desc++) {
  383. if (IFM_TYPE(ifmw) == desc->ifmt_word)
  384. break;
  385. }
  386. if (desc->ifmt_string == NULL)
  387. printf("<unknown type> ");
  388. else
  389. printf("%s ", desc->ifmt_string);
  390. /* Print the subtype. */
  391. for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
  392. desc++) {
  393. if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
  394. IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
  395. break;
  396. }
  397. if (desc->ifmt_string == NULL)
  398. printf("<unknown subtype>");
  399. else
  400. printf("%s", desc->ifmt_string);
  401. /* Print any options. */
  402. for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
  403. desc++) {
  404. if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
  405. (ifmw & desc->ifmt_word) != 0 &&
  406. (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
  407. if (seen_option == 0)
  408. printf(" <");
  409. printf("%s%s", seen_option ? "," : "",
  410. desc->ifmt_string);
  411. seen_option |= IFM_OPTIONS(desc->ifmt_word);
  412. }
  413. }
  414. printf("%s\n", seen_option ? ">" : "");
  415. }
  416. #endif /* IFMEDIA_DEBUG */