PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/sys/net/if_loop.c

https://bitbucket.org/freebsd/freebsd-head/
C | 487 lines | 351 code | 57 blank | 79 comment | 41 complexity | 4191ea884dc7867fc1cf04fecde8cc9f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /*-
  2. * Copyright (c) 1982, 1986, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * @(#)if_loop.c 8.2 (Berkeley) 1/9/95
  30. * $FreeBSD$
  31. */
  32. /*
  33. * Loopback interface driver for protocol testing and timing.
  34. */
  35. #include "opt_atalk.h"
  36. #include "opt_inet.h"
  37. #include "opt_inet6.h"
  38. #include "opt_ipx.h"
  39. #include <sys/param.h>
  40. #include <sys/systm.h>
  41. #include <sys/kernel.h>
  42. #include <sys/mbuf.h>
  43. #include <sys/module.h>
  44. #include <machine/bus.h>
  45. #include <sys/rman.h>
  46. #include <sys/socket.h>
  47. #include <sys/sockio.h>
  48. #include <sys/sysctl.h>
  49. #include <net/if.h>
  50. #include <net/if_clone.h>
  51. #include <net/if_types.h>
  52. #include <net/netisr.h>
  53. #include <net/route.h>
  54. #include <net/bpf.h>
  55. #include <net/vnet.h>
  56. #ifdef INET
  57. #include <netinet/in.h>
  58. #include <netinet/in_var.h>
  59. #endif
  60. #ifdef IPX
  61. #include <netipx/ipx.h>
  62. #include <netipx/ipx_if.h>
  63. #endif
  64. #ifdef INET6
  65. #ifndef INET
  66. #include <netinet/in.h>
  67. #endif
  68. #include <netinet6/in6_var.h>
  69. #include <netinet/ip6.h>
  70. #endif
  71. #ifdef NETATALK
  72. #include <netatalk/at.h>
  73. #include <netatalk/at_var.h>
  74. #endif
  75. #include <security/mac/mac_framework.h>
  76. #ifdef TINY_LOMTU
  77. #define LOMTU (1024+512)
  78. #elif defined(LARGE_LOMTU)
  79. #define LOMTU 131072
  80. #else
  81. #define LOMTU 16384
  82. #endif
  83. #define LO_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
  84. #define LO_CSUM_FEATURES6 (CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6)
  85. #define LO_CSUM_SET (CSUM_DATA_VALID | CSUM_DATA_VALID_IPV6 | \
  86. CSUM_PSEUDO_HDR | \
  87. CSUM_IP_CHECKED | CSUM_IP_VALID | \
  88. CSUM_SCTP_VALID)
  89. int loioctl(struct ifnet *, u_long, caddr_t);
  90. static void lortrequest(int, struct rtentry *, struct rt_addrinfo *);
  91. int looutput(struct ifnet *ifp, struct mbuf *m,
  92. struct sockaddr *dst, struct route *ro);
  93. static int lo_clone_create(struct if_clone *, int, caddr_t);
  94. static void lo_clone_destroy(struct ifnet *);
  95. VNET_DEFINE(struct ifnet *, loif); /* Used externally */
  96. #ifdef VIMAGE
  97. static VNET_DEFINE(struct if_clone *, lo_cloner);
  98. #define V_lo_cloner VNET(lo_cloner)
  99. #endif
  100. static struct if_clone *lo_cloner;
  101. static const char loname[] = "lo";
  102. static void
  103. lo_clone_destroy(struct ifnet *ifp)
  104. {
  105. #ifndef VIMAGE
  106. /* XXX: destroying lo0 will lead to panics. */
  107. KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
  108. #endif
  109. bpfdetach(ifp);
  110. if_detach(ifp);
  111. if_free(ifp);
  112. }
  113. static int
  114. lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
  115. {
  116. struct ifnet *ifp;
  117. ifp = if_alloc(IFT_LOOP);
  118. if (ifp == NULL)
  119. return (ENOSPC);
  120. if_initname(ifp, loname, unit);
  121. ifp->if_mtu = LOMTU;
  122. ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
  123. ifp->if_ioctl = loioctl;
  124. ifp->if_output = looutput;
  125. ifp->if_snd.ifq_maxlen = ifqmaxlen;
  126. ifp->if_capabilities = ifp->if_capenable =
  127. IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
  128. ifp->if_hwassist = LO_CSUM_FEATURES | LO_CSUM_FEATURES6;
  129. if_attach(ifp);
  130. bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
  131. if (V_loif == NULL)
  132. V_loif = ifp;
  133. return (0);
  134. }
  135. static void
  136. vnet_loif_init(const void *unused __unused)
  137. {
  138. #ifdef VIMAGE
  139. lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
  140. 1);
  141. V_lo_cloner = lo_cloner;
  142. #else
  143. lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
  144. 1);
  145. #endif
  146. }
  147. VNET_SYSINIT(vnet_loif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
  148. vnet_loif_init, NULL);
  149. #ifdef VIMAGE
  150. static void
  151. vnet_loif_uninit(const void *unused __unused)
  152. {
  153. if_clone_detach(V_lo_cloner);
  154. V_loif = NULL;
  155. }
  156. VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
  157. vnet_loif_uninit, NULL);
  158. #endif
  159. static int
  160. loop_modevent(module_t mod, int type, void *data)
  161. {
  162. switch (type) {
  163. case MOD_LOAD:
  164. break;
  165. case MOD_UNLOAD:
  166. printf("loop module unload - not possible for this module type\n");
  167. return (EINVAL);
  168. default:
  169. return (EOPNOTSUPP);
  170. }
  171. return (0);
  172. }
  173. static moduledata_t loop_mod = {
  174. "if_lo",
  175. loop_modevent,
  176. 0
  177. };
  178. DECLARE_MODULE(if_lo, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
  179. int
  180. looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
  181. struct route *ro)
  182. {
  183. u_int32_t af;
  184. struct rtentry *rt = NULL;
  185. #ifdef MAC
  186. int error;
  187. #endif
  188. M_ASSERTPKTHDR(m); /* check if we have the packet header */
  189. if (ro != NULL)
  190. rt = ro->ro_rt;
  191. #ifdef MAC
  192. error = mac_ifnet_check_transmit(ifp, m);
  193. if (error) {
  194. m_freem(m);
  195. return (error);
  196. }
  197. #endif
  198. if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
  199. m_freem(m);
  200. return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
  201. rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
  202. }
  203. ifp->if_opackets++;
  204. ifp->if_obytes += m->m_pkthdr.len;
  205. /* BPF writes need to be handled specially. */
  206. if (dst->sa_family == AF_UNSPEC) {
  207. bcopy(dst->sa_data, &af, sizeof(af));
  208. dst->sa_family = af;
  209. }
  210. #if 1 /* XXX */
  211. switch (dst->sa_family) {
  212. case AF_INET:
  213. if (ifp->if_capenable & IFCAP_RXCSUM) {
  214. m->m_pkthdr.csum_data = 0xffff;
  215. m->m_pkthdr.csum_flags = LO_CSUM_SET;
  216. }
  217. m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES;
  218. break;
  219. case AF_INET6:
  220. #if 0
  221. /*
  222. * XXX-BZ for now always claim the checksum is good despite
  223. * any interface flags. This is a workaround for 9.1-R and
  224. * a proper solution ought to be sought later.
  225. */
  226. if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
  227. m->m_pkthdr.csum_data = 0xffff;
  228. m->m_pkthdr.csum_flags = LO_CSUM_SET;
  229. }
  230. #else
  231. m->m_pkthdr.csum_data = 0xffff;
  232. m->m_pkthdr.csum_flags = LO_CSUM_SET;
  233. #endif
  234. m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES6;
  235. break;
  236. case AF_IPX:
  237. case AF_APPLETALK:
  238. break;
  239. default:
  240. printf("looutput: af=%d unexpected\n", dst->sa_family);
  241. m_freem(m);
  242. return (EAFNOSUPPORT);
  243. }
  244. #endif
  245. return (if_simloop(ifp, m, dst->sa_family, 0));
  246. }
  247. /*
  248. * if_simloop()
  249. *
  250. * This function is to support software emulation of hardware loopback,
  251. * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
  252. * hear their own broadcasts, we create a copy of the packet that we
  253. * would normally receive via a hardware loopback.
  254. *
  255. * This function expects the packet to include the media header of length hlen.
  256. */
  257. int
  258. if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
  259. {
  260. int isr;
  261. M_ASSERTPKTHDR(m);
  262. m_tag_delete_nonpersistent(m);
  263. m->m_pkthdr.rcvif = ifp;
  264. #ifdef MAC
  265. mac_ifnet_create_mbuf(ifp, m);
  266. #endif
  267. /*
  268. * Let BPF see incoming packet in the following manner:
  269. * - Emulated packet loopback for a simplex interface
  270. * (net/if_ethersubr.c)
  271. * -> passes it to ifp's BPF
  272. * - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
  273. * -> not passes it to any BPF
  274. * - Normal packet loopback from myself to myself (net/if_loop.c)
  275. * -> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
  276. */
  277. if (hlen > 0) {
  278. if (bpf_peers_present(ifp->if_bpf)) {
  279. bpf_mtap(ifp->if_bpf, m);
  280. }
  281. } else {
  282. if (bpf_peers_present(V_loif->if_bpf)) {
  283. if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
  284. /* XXX beware sizeof(af) != 4 */
  285. u_int32_t af1 = af;
  286. /*
  287. * We need to prepend the address family.
  288. */
  289. bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
  290. }
  291. }
  292. }
  293. /* Strip away media header */
  294. if (hlen > 0) {
  295. m_adj(m, hlen);
  296. #ifndef __NO_STRICT_ALIGNMENT
  297. /*
  298. * Some archs do not like unaligned data, so
  299. * we move data down in the first mbuf.
  300. */
  301. if (mtod(m, vm_offset_t) & 3) {
  302. KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
  303. bcopy(m->m_data,
  304. (char *)(mtod(m, vm_offset_t)
  305. - (mtod(m, vm_offset_t) & 3)),
  306. m->m_len);
  307. m->m_data -= (mtod(m,vm_offset_t) & 3);
  308. }
  309. #endif
  310. }
  311. /* Deliver to upper layer protocol */
  312. switch (af) {
  313. #ifdef INET
  314. case AF_INET:
  315. isr = NETISR_IP;
  316. break;
  317. #endif
  318. #ifdef INET6
  319. case AF_INET6:
  320. m->m_flags |= M_LOOP;
  321. isr = NETISR_IPV6;
  322. break;
  323. #endif
  324. #ifdef IPX
  325. case AF_IPX:
  326. isr = NETISR_IPX;
  327. break;
  328. #endif
  329. #ifdef NETATALK
  330. case AF_APPLETALK:
  331. isr = NETISR_ATALK2;
  332. break;
  333. #endif
  334. default:
  335. printf("if_simloop: can't handle af=%d\n", af);
  336. m_freem(m);
  337. return (EAFNOSUPPORT);
  338. }
  339. ifp->if_ipackets++;
  340. ifp->if_ibytes += m->m_pkthdr.len;
  341. netisr_queue(isr, m); /* mbuf is free'd on failure. */
  342. return (0);
  343. }
  344. /* ARGSUSED */
  345. static void
  346. lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
  347. {
  348. RT_LOCK_ASSERT(rt);
  349. rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
  350. }
  351. /*
  352. * Process an ioctl request.
  353. */
  354. /* ARGSUSED */
  355. int
  356. loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  357. {
  358. struct ifaddr *ifa;
  359. struct ifreq *ifr = (struct ifreq *)data;
  360. int error = 0, mask;
  361. switch (cmd) {
  362. case SIOCSIFADDR:
  363. ifp->if_flags |= IFF_UP;
  364. ifp->if_drv_flags |= IFF_DRV_RUNNING;
  365. ifa = (struct ifaddr *)data;
  366. ifa->ifa_rtrequest = lortrequest;
  367. /*
  368. * Everything else is done at a higher level.
  369. */
  370. break;
  371. case SIOCADDMULTI:
  372. case SIOCDELMULTI:
  373. if (ifr == 0) {
  374. error = EAFNOSUPPORT; /* XXX */
  375. break;
  376. }
  377. switch (ifr->ifr_addr.sa_family) {
  378. #ifdef INET
  379. case AF_INET:
  380. break;
  381. #endif
  382. #ifdef INET6
  383. case AF_INET6:
  384. break;
  385. #endif
  386. default:
  387. error = EAFNOSUPPORT;
  388. break;
  389. }
  390. break;
  391. case SIOCSIFMTU:
  392. ifp->if_mtu = ifr->ifr_mtu;
  393. break;
  394. case SIOCSIFFLAGS:
  395. break;
  396. case SIOCSIFCAP:
  397. mask = ifp->if_capenable ^ ifr->ifr_reqcap;
  398. if ((mask & IFCAP_RXCSUM) != 0)
  399. ifp->if_capenable ^= IFCAP_RXCSUM;
  400. if ((mask & IFCAP_TXCSUM) != 0)
  401. ifp->if_capenable ^= IFCAP_TXCSUM;
  402. if ((mask & IFCAP_RXCSUM_IPV6) != 0) {
  403. #if 0
  404. ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
  405. #else
  406. error = EOPNOTSUPP;
  407. break;
  408. #endif
  409. }
  410. if ((mask & IFCAP_TXCSUM_IPV6) != 0) {
  411. #if 0
  412. ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
  413. #else
  414. error = EOPNOTSUPP;
  415. break;
  416. #endif
  417. }
  418. ifp->if_hwassist = 0;
  419. if (ifp->if_capenable & IFCAP_TXCSUM)
  420. ifp->if_hwassist = LO_CSUM_FEATURES;
  421. #if 0
  422. if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
  423. ifp->if_hwassist |= LO_CSUM_FEATURES6;
  424. #endif
  425. break;
  426. default:
  427. error = EINVAL;
  428. }
  429. return (error);
  430. }