if_loop.c 11 KB

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