PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

sys/lib/libsa/netif.c

http://www.minix3.org/
C | 322 lines | 245 code | 40 blank | 37 comment | 39 complexity | 8dd09de750820fa981a9714563a992c3 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /* $NetBSD: netif.c,v 1.24 2009/01/17 14:00:36 tsutsui Exp $ */
  2. /*
  3. * Copyright (c) 1993 Adam Glass
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by Adam Glass.
  17. * 4. The name of the Author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #include <sys/param.h>
  33. #include <sys/cdefs.h>
  34. #include <sys/mount.h>
  35. #ifdef _STANDALONE
  36. #include <lib/libkern/libkern.h>
  37. #else
  38. #include <string.h>
  39. #endif
  40. #include <netinet/in.h>
  41. #include <netinet/in_systm.h>
  42. #include "stand.h"
  43. #include "netif.h"
  44. struct iodesc sockets[SOPEN_MAX];
  45. #ifdef NETIF_DEBUG
  46. int netif_debug = 0;
  47. #endif
  48. /*
  49. * netif_init:
  50. *
  51. * initialize the generic network interface layer
  52. */
  53. void
  54. netif_init(void)
  55. {
  56. struct netif_driver *drv;
  57. int d, i;
  58. #ifdef NETIF_DEBUG
  59. if (netif_debug)
  60. printf("netif_init: called\n");
  61. #endif
  62. for (d = 0; d < n_netif_drivers; d++) {
  63. drv = netif_drivers[d];
  64. for (i = 0; i < drv->netif_nifs; i++)
  65. drv->netif_ifs[i].dif_used = 0;
  66. }
  67. }
  68. int netif_match(struct netif *, void *);
  69. int
  70. netif_match(struct netif *nif, void *machdep_hint)
  71. {
  72. struct netif_driver *drv = nif->nif_driver;
  73. #if 0
  74. if (netif_debug)
  75. printf("%s%d: netif_match (%d)\n", drv->netif_bname,
  76. nif->nif_unit, nif->nif_sel);
  77. #endif
  78. return drv->netif_match(nif, machdep_hint);
  79. }
  80. struct netif *
  81. netif_select(void *machdep_hint)
  82. {
  83. int d, u, unit_done, s;
  84. struct netif_driver *drv;
  85. struct netif cur_if;
  86. static struct netif best_if;
  87. int best_val;
  88. int val;
  89. best_val = 0;
  90. best_if.nif_driver = NULL;
  91. #ifdef NETIF_DEBUG
  92. if (netif_debug)
  93. printf("netif_select: %d interfaces\n", n_netif_drivers);
  94. #endif
  95. for (d = 0; d < n_netif_drivers; d++) {
  96. cur_if.nif_driver = netif_drivers[d];
  97. drv = cur_if.nif_driver;
  98. for (u = 0; u < drv->netif_nifs; u++) {
  99. cur_if.nif_unit = u;
  100. unit_done = 0;
  101. #ifdef NETIF_DEBUG
  102. if (netif_debug)
  103. printf("\t%s%d:", drv->netif_bname,
  104. cur_if.nif_unit);
  105. #endif
  106. for (s = 0; s < drv->netif_ifs[u].dif_nsel; s++) {
  107. cur_if.nif_sel = s;
  108. if (drv->netif_ifs[u].dif_used & (1 << s)) {
  109. #ifdef NETIF_DEBUG
  110. if (netif_debug)
  111. printf(" [%d used]", s);
  112. #endif
  113. continue;
  114. }
  115. val = netif_match(&cur_if, machdep_hint);
  116. #ifdef NETIF_DEBUG
  117. if (netif_debug)
  118. printf(" [%d -> %d]", s, val);
  119. #endif
  120. if (val > best_val) {
  121. best_val = val;
  122. best_if = cur_if;
  123. }
  124. }
  125. #ifdef NETIF_DEBUG
  126. if (netif_debug)
  127. printf("\n");
  128. #endif
  129. }
  130. }
  131. if (best_if.nif_driver == NULL)
  132. return NULL;
  133. best_if.nif_driver->
  134. netif_ifs[best_if.nif_unit].dif_used |= (1 << best_if.nif_sel);
  135. #ifdef NETIF_DEBUG
  136. if (netif_debug)
  137. printf("netif_select: %s%d(%d) wins\n",
  138. best_if.nif_driver->netif_bname,
  139. best_if.nif_unit, best_if.nif_sel);
  140. #endif
  141. return &best_if;
  142. }
  143. int
  144. netif_probe(struct netif *nif, void *machdep_hint)
  145. {
  146. struct netif_driver *drv = nif->nif_driver;
  147. #ifdef NETIF_DEBUG
  148. if (netif_debug)
  149. printf("%s%d: netif_probe\n", drv->netif_bname, nif->nif_unit);
  150. #endif
  151. return drv->netif_probe(nif, machdep_hint);
  152. }
  153. void
  154. netif_attach(struct netif *nif, struct iodesc *desc, void *machdep_hint)
  155. {
  156. struct netif_driver *drv = nif->nif_driver;
  157. #ifdef NETIF_DEBUG
  158. if (netif_debug)
  159. printf("%s%d: netif_attach\n", drv->netif_bname, nif->nif_unit);
  160. #endif
  161. desc->io_netif = nif;
  162. #ifdef PARANOID
  163. if (drv->netif_init == NULL)
  164. panic("%s%d: no netif_init support", drv->netif_bname,
  165. nif->nif_unit);
  166. #endif
  167. drv->netif_init(desc, machdep_hint);
  168. (void)memset(drv->netif_ifs[nif->nif_unit].dif_stats, 0,
  169. sizeof(struct netif_stats));
  170. }
  171. void
  172. netif_detach(struct netif *nif)
  173. {
  174. struct netif_driver *drv = nif->nif_driver;
  175. #ifdef NETIF_DEBUG
  176. if (netif_debug)
  177. printf("%s%d: netif_detach\n", drv->netif_bname, nif->nif_unit);
  178. #endif
  179. #ifdef PARANOID
  180. if (drv->netif_end == NULL)
  181. panic("%s%d: no netif_end support", drv->netif_bname,
  182. nif->nif_unit);
  183. #endif
  184. drv->netif_end(nif);
  185. }
  186. ssize_t
  187. netif_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timo)
  188. {
  189. struct netif *nif = desc->io_netif;
  190. struct netif_driver *drv = nif->nif_driver;
  191. ssize_t rv;
  192. #ifdef NETIF_DEBUG
  193. if (netif_debug)
  194. printf("%s%d: netif_get\n", drv->netif_bname, nif->nif_unit);
  195. #endif
  196. #ifdef PARANOID
  197. if (drv->netif_get == NULL)
  198. panic("%s%d: no netif_get support", drv->netif_bname,
  199. nif->nif_unit);
  200. #endif
  201. rv = drv->netif_get(desc, pkt, len, timo);
  202. #ifdef NETIF_DEBUG
  203. if (netif_debug)
  204. printf("%s%d: netif_get returning %d\n", drv->netif_bname,
  205. nif->nif_unit, (int)rv);
  206. #endif
  207. return rv;
  208. }
  209. ssize_t
  210. netif_put(struct iodesc *desc, void *pkt, size_t len)
  211. {
  212. struct netif *nif = desc->io_netif;
  213. struct netif_driver *drv = nif->nif_driver;
  214. ssize_t rv;
  215. #ifdef NETIF_DEBUG
  216. if (netif_debug)
  217. printf("%s%d: netif_put\n", drv->netif_bname, nif->nif_unit);
  218. #endif
  219. #ifdef PARANOID
  220. if (drv->netif_put == NULL)
  221. panic("%s%d: no netif_put support", drv->netif_bname,
  222. nif->nif_unit);
  223. #endif
  224. rv = drv->netif_put(desc, pkt, len);
  225. #ifdef NETIF_DEBUG
  226. if (netif_debug)
  227. printf("%s%d: netif_put returning %d\n", drv->netif_bname,
  228. nif->nif_unit, (int)rv);
  229. #endif
  230. return rv;
  231. }
  232. struct iodesc *
  233. socktodesc(int sock)
  234. {
  235. #if !defined(LIBSA_NO_FD_CHECKING)
  236. if (sock >= SOPEN_MAX) {
  237. errno = EBADF;
  238. return NULL;
  239. }
  240. #endif
  241. return &sockets[sock];
  242. }
  243. int
  244. netif_open(void *machdep_hint)
  245. {
  246. int fd;
  247. struct iodesc *s;
  248. struct netif *nif;
  249. /* find a free socket */
  250. for (fd = 0, s = sockets; fd < SOPEN_MAX; fd++, s++)
  251. if (s->io_netif == (struct netif *)0)
  252. goto fnd;
  253. errno = EMFILE;
  254. return -1;
  255. fnd:
  256. (void)memset(s, 0, sizeof(*s));
  257. netif_init();
  258. nif = netif_select(machdep_hint);
  259. if (!nif)
  260. panic("netboot: no interfaces left untried");
  261. if (netif_probe(nif, machdep_hint)) {
  262. printf("netboot: couldn't probe %s%d\n",
  263. nif->nif_driver->netif_bname, nif->nif_unit);
  264. errno = EINVAL;
  265. return -1;
  266. }
  267. netif_attach(nif, s, machdep_hint);
  268. return fd;
  269. }
  270. int
  271. netif_close(int sock)
  272. {
  273. #if !defined(LIBSA_NO_FD_CHECKING)
  274. if (sock >= SOPEN_MAX) {
  275. errno = EBADF;
  276. return -1;
  277. }
  278. #endif
  279. netif_detach(sockets[sock].io_netif);
  280. sockets[sock].io_netif = (struct netif *)0;
  281. return 0;
  282. }