if_stf.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /* $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $ */
  2. /*-
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. *
  5. * Copyright (C) 2000 WIDE Project.
  6. * Copyright (c) 2010 Hiroki Sato <hrs@FreeBSD.org>
  7. * Copyright (c) 2013 Ermal Luci <eri@FreeBSD.org>
  8. * Copyright (c) 2017-2021 Rubicon Communications, LLC (Netgate)
  9. * All rights reserved.
  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. * 3. Neither the name of the project nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. */
  35. /*
  36. * 6to4 interface, based on RFC3056.
  37. *
  38. * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
  39. * There is no address mapping defined from IPv6 multicast address to IPv4
  40. * address. Therefore, we do not have IFF_MULTICAST on the interface.
  41. *
  42. * Due to the lack of address mapping for link-local addresses, we cannot
  43. * throw packets toward link-local addresses (fe80::x). Also, we cannot throw
  44. * packets to link-local multicast addresses (ff02::x).
  45. *
  46. * Here are interesting symptoms due to the lack of link-local address:
  47. *
  48. * Unicast routing exchange:
  49. * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9,
  50. * and link-local addresses as nexthop.
  51. * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address
  52. * assigned to the link, and makes use of them. Also, HELLO packets use
  53. * link-local multicast addresses (ff02::5 and ff02::6).
  54. * - BGP4+: Maybe. You can only use global address as nexthop, and global
  55. * address as TCP endpoint address.
  56. *
  57. * Multicast routing protocols:
  58. * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
  59. * Adjacent PIM routers must be configured manually (is it really spec-wise
  60. * correct thing to do?).
  61. *
  62. * ICMPv6:
  63. * - Redirects cannot be used due to the lack of link-local address.
  64. *
  65. * stf interface does not have, and will not need, a link-local address.
  66. * It seems to have no real benefit and does not help the above symptoms much.
  67. * Even if we assign link-locals to interface, we cannot really
  68. * use link-local unicast/multicast on top of 6to4 cloud (since there's no
  69. * encapsulation defined for link-local address), and the above analysis does
  70. * not change. RFC3056 does not mandate the assignment of link-local address
  71. * either.
  72. *
  73. * 6to4 interface has security issues. Refer to
  74. * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
  75. * for details. The code tries to filter out some of malicious packets.
  76. * Note that there is no way to be 100% secure.
  77. */
  78. #include <sys/param.h>
  79. #include <sys/systm.h>
  80. #include <sys/socket.h>
  81. #include <sys/sockio.h>
  82. #include <sys/mbuf.h>
  83. #include <sys/endian.h>
  84. #include <sys/errno.h>
  85. #include <sys/kernel.h>
  86. #include <sys/lock.h>
  87. #include <sys/module.h>
  88. #include <sys/priv.h>
  89. #include <sys/proc.h>
  90. #include <sys/queue.h>
  91. #include <sys/sdt.h>
  92. #include <sys/sysctl.h>
  93. #include <machine/cpu.h>
  94. #include <sys/malloc.h>
  95. #include <net/if.h>
  96. #include <net/if_var.h>
  97. #include <net/if_private.h>
  98. #include <net/if_clone.h>
  99. #include <net/route.h>
  100. #include <net/route/nhop.h>
  101. #include <net/netisr.h>
  102. #include <net/if_stf.h>
  103. #include <net/if_types.h>
  104. #include <net/vnet.h>
  105. #include <netinet/in.h>
  106. #include <netinet/in_fib.h>
  107. #include <netinet/in_systm.h>
  108. #include <netinet/ip.h>
  109. #include <netinet/ip_var.h>
  110. #include <netinet/in_var.h>
  111. #include <netinet/ip6.h>
  112. #include <netinet6/in6_fib.h>
  113. #include <netinet6/ip6_var.h>
  114. #include <netinet6/in6_var.h>
  115. #include <netinet/ip_ecn.h>
  116. #include <netinet/ip_encap.h>
  117. #include <machine/stdarg.h>
  118. #include <net/bpf.h>
  119. #include <security/mac/mac_framework.h>
  120. SDT_PROVIDER_DEFINE(if_stf);
  121. SDT_PROBE_DEFINE3(if_stf, , encapcheck, in, "struct mbuf *", "int", "int");
  122. SDT_PROBE_DEFINE0(if_stf, , encapcheck, accept);
  123. SDT_PROBE_DEFINE3(if_stf, , getsrcifa6, in, "struct ifnet *",
  124. "struct in6_addr *", "struct in6_addr *");
  125. SDT_PROBE_DEFINE2(if_stf, , getsrcifa6, found, "struct in6_addr *",
  126. "struct in6_addr *");
  127. SDT_PROBE_DEFINE0(if_stf, , getsrcifa6, notfound);
  128. SDT_PROBE_DEFINE4(if_stf, , stf_output, in, "struct ifnet *", "struct mbuf *",
  129. "struct sockaddr *", "struct route *");
  130. SDT_PROBE_DEFINE2(if_stf, , stf_output, error, "int", "int");
  131. SDT_PROBE_DEFINE1(if_stf, , stf_output, out, "int");
  132. SDT_PROBE_DEFINE3(if_stf, , checkaddr6, in, "struct stf_softc *",
  133. "struct in6_addr *", "struct ifnet *");
  134. SDT_PROBE_DEFINE2(if_stf, , checkaddr6, out, "int", "int");
  135. SDT_PROBE_DEFINE3(if_stf, , stf_input, in, "struct mbuf *", "int", "int");
  136. SDT_PROBE_DEFINE2(if_stf, , stf_input, out, "int", "int");
  137. SDT_PROBE_DEFINE3(if_stf, , ioctl, sv4net, "struct in_addr *",
  138. "struct in_addr *", "int");
  139. SDT_PROBE_DEFINE1(if_stf, , ioctl, sdstv4, "struct in_addr *");
  140. SDT_PROBE_DEFINE1(if_stf, , ioctl, ifaddr, "struct ifaddr *");
  141. SDT_PROBE_DEFINE4(if_stf, , getin4addr_in6, out, "struct in6_addr *",
  142. "struct in6_addr *", "struct in6_addr *", "struct sockaddr_in *");
  143. SDT_PROBE_DEFINE2(if_stf, , getin4addr, in, "struct in6_addr *", "struct in6_addr *");
  144. SDT_PROBE_DEFINE1(if_stf, , getin4addr, out, "struct sockaddr_in *");
  145. SYSCTL_DECL(_net_link);
  146. static SYSCTL_NODE(_net_link, IFT_STF, stf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  147. "6to4 Interface");
  148. static int stf_permit_rfc1918 = 0;
  149. SYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RWTUN,
  150. &stf_permit_rfc1918, 0, "Permit the use of private IPv4 addresses");
  151. #define STFUNIT 0
  152. #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002)
  153. /*
  154. * XXX: Return a pointer with 16-bit aligned. Don't cast it to
  155. * struct in_addr *; use bcopy() instead.
  156. */
  157. #define GET_V4(x) (&(x)->s6_addr16[1])
  158. struct stf_softc {
  159. struct ifnet *sc_ifp;
  160. in_addr_t braddr; /* Border relay IPv4 address */
  161. in_addr_t srcv4_addr; /* Our IPv4 WAN address */
  162. u_int v4prefixlen; /* How much of the v4 address to include in our address. */
  163. u_int sc_fibnum;
  164. const struct encaptab *encap_cookie;
  165. };
  166. #define STF2IFP(sc) ((sc)->sc_ifp)
  167. static const char stfname[] = "stf";
  168. static MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface");
  169. static const int ip_stf_ttl = 40;
  170. static int in_stf_input(struct mbuf *, int, int, void *);
  171. static char *stfnames[] = {"stf0", "stf", "6to4", NULL};
  172. static int stfmodevent(module_t, int, void *);
  173. static int stf_encapcheck(const struct mbuf *, int, int, void *);
  174. static int stf_getsrcifa6(struct ifnet *, struct in6_addr *, struct in6_addr *);
  175. static int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
  176. struct route *);
  177. static int isrfc1918addr(struct in_addr *);
  178. static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
  179. struct ifnet *);
  180. static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
  181. struct ifnet *);
  182. static struct sockaddr_in *stf_getin4addr_in6(struct stf_softc *,
  183. struct sockaddr_in *, struct in6_addr, struct in6_addr,
  184. struct in6_addr);
  185. static struct sockaddr_in *stf_getin4addr(struct stf_softc *,
  186. struct sockaddr_in *, struct in6_addr, struct in6_addr);
  187. static int stf_ioctl(struct ifnet *, u_long, caddr_t);
  188. VNET_DEFINE_STATIC(struct if_clone *, stf_cloner);
  189. #define V_stf_cloner VNET(stf_cloner)
  190. static const struct encap_config ipv4_encap_cfg = {
  191. .proto = IPPROTO_IPV6,
  192. .min_length = sizeof(struct ip),
  193. .exact_match = (sizeof(in_addr_t) << 3) + 8,
  194. .check = stf_encapcheck,
  195. .input = in_stf_input
  196. };
  197. static int
  198. stf_clone_match(struct if_clone *ifc, const char *name)
  199. {
  200. int i;
  201. for(i = 0; stfnames[i] != NULL; i++) {
  202. if (strcmp(stfnames[i], name) == 0)
  203. return (1);
  204. }
  205. return (0);
  206. }
  207. static int
  208. stf_clone_create(struct if_clone *ifc, char *name, size_t len,
  209. struct ifc_data *ifd, struct ifnet **ifpp)
  210. {
  211. char *dp;
  212. int err, unit, wildcard;
  213. struct stf_softc *sc;
  214. struct ifnet *ifp;
  215. err = ifc_name2unit(name, &unit);
  216. if (err != 0)
  217. return (err);
  218. wildcard = (unit < 0);
  219. /*
  220. * We can only have one unit, but since unit allocation is
  221. * already locked, we use it to keep from allocating extra
  222. * interfaces.
  223. */
  224. unit = STFUNIT;
  225. err = ifc_alloc_unit(ifc, &unit);
  226. if (err != 0)
  227. return (err);
  228. sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
  229. ifp = STF2IFP(sc) = if_alloc(IFT_STF);
  230. ifp->if_softc = sc;
  231. sc->sc_fibnum = curthread->td_proc->p_fibnum;
  232. /*
  233. * Set the name manually rather then using if_initname because
  234. * we don't conform to the default naming convention for interfaces.
  235. * In the wildcard case, we need to update the name.
  236. */
  237. if (wildcard) {
  238. for (dp = name; *dp != '\0'; dp++);
  239. if (snprintf(dp, len - (dp-name), "%d", unit) >
  240. len - (dp-name) - 1) {
  241. /*
  242. * This can only be a programmer error and
  243. * there's no straightforward way to recover if
  244. * it happens.
  245. */
  246. panic("if_clone_create(): interface name too long");
  247. }
  248. }
  249. strlcpy(ifp->if_xname, name, IFNAMSIZ);
  250. ifp->if_dname = stfname;
  251. ifp->if_dunit = IF_DUNIT_NONE;
  252. sc->encap_cookie = ip_encap_attach(&ipv4_encap_cfg, sc, M_WAITOK);
  253. ifp->if_mtu = IPV6_MMTU;
  254. ifp->if_ioctl = stf_ioctl;
  255. ifp->if_output = stf_output;
  256. ifp->if_snd.ifq_maxlen = ifqmaxlen;
  257. if_attach(ifp);
  258. bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
  259. *ifpp = ifp;
  260. return (0);
  261. }
  262. static int
  263. stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
  264. {
  265. struct stf_softc *sc = ifp->if_softc;
  266. int err __unused;
  267. err = ip_encap_detach(sc->encap_cookie);
  268. KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
  269. bpfdetach(ifp);
  270. if_detach(ifp);
  271. if_free(ifp);
  272. free(sc, M_STF);
  273. ifc_free_unit(ifc, STFUNIT);
  274. return (0);
  275. }
  276. static void
  277. vnet_stf_init(const void *unused __unused)
  278. {
  279. struct if_clone_addreq req = {
  280. .match_f = stf_clone_match,
  281. .create_f = stf_clone_create,
  282. .destroy_f = stf_clone_destroy,
  283. };
  284. V_stf_cloner = ifc_attach_cloner(stfname, &req);
  285. }
  286. VNET_SYSINIT(vnet_stf_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_init, NULL);
  287. static void
  288. vnet_stf_uninit(const void *unused __unused)
  289. {
  290. if_clone_detach(V_stf_cloner);
  291. V_stf_cloner = NULL;
  292. }
  293. VNET_SYSUNINIT(vnet_stf_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_uninit,
  294. NULL);
  295. static int
  296. stfmodevent(module_t mod, int type, void *data)
  297. {
  298. switch (type) {
  299. case MOD_LOAD:
  300. /* Done in vnet_stf_init() */
  301. break;
  302. case MOD_UNLOAD:
  303. /* Done in vnet_stf_uninit() */
  304. break;
  305. default:
  306. return (EOPNOTSUPP);
  307. }
  308. return (0);
  309. }
  310. static moduledata_t stf_mod = {
  311. "if_stf",
  312. stfmodevent,
  313. 0
  314. };
  315. DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  316. MODULE_VERSION(if_stf, 2);
  317. static int
  318. stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
  319. {
  320. struct ip ip;
  321. struct stf_softc *sc;
  322. struct in6_addr addr6, mask6;
  323. struct sockaddr_in sin4addr, sin4mask;
  324. SDT_PROBE3(if_stf, , encapcheck, in, m, off, proto);
  325. sc = (struct stf_softc *)arg;
  326. if (sc == NULL)
  327. return (0);
  328. if ((STF2IFP(sc)->if_flags & IFF_UP) == 0)
  329. return (0);
  330. /* IFF_LINK0 means "no decapsulation" */
  331. if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0)
  332. return (0);
  333. if (proto != IPPROTO_IPV6)
  334. return (0);
  335. m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
  336. if (ip.ip_v != 4)
  337. return (0);
  338. if (stf_getsrcifa6(STF2IFP(sc), &addr6, &mask6) != 0)
  339. return (0);
  340. if (sc->srcv4_addr != INADDR_ANY) {
  341. sin4addr.sin_addr.s_addr = sc->srcv4_addr;
  342. sin4addr.sin_family = AF_INET;
  343. } else
  344. if (stf_getin4addr(sc, &sin4addr, addr6, mask6) == NULL)
  345. return (0);
  346. if (sin4addr.sin_addr.s_addr != ip.ip_dst.s_addr)
  347. return (0);
  348. if (IN6_IS_ADDR_6TO4(&addr6)) {
  349. /*
  350. * 6to4 (RFC 3056).
  351. * Check if IPv4 src matches the IPv4 address derived
  352. * from the local 6to4 address masked by prefixmask.
  353. * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
  354. * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
  355. */
  356. memcpy(&sin4mask.sin_addr, GET_V4(&mask6),
  357. sizeof(sin4mask.sin_addr));
  358. if ((sin4addr.sin_addr.s_addr & sin4mask.sin_addr.s_addr) !=
  359. (ip.ip_src.s_addr & sin4mask.sin_addr.s_addr))
  360. return (0);
  361. } else {
  362. /* 6rd (RFC 5569) */
  363. /*
  364. * No restriction on the src address in the case of
  365. * 6rd because the stf(4) interface always has a
  366. * prefix which covers whole of IPv4 src address
  367. * range. So, stf_output() will catch all of
  368. * 6rd-capsuled IPv4 traffic with suspicious inner dst
  369. * IPv4 address (i.e. the IPv6 destination address is
  370. * one the admin does not like to route to outside),
  371. * and then it discard them silently.
  372. */
  373. }
  374. SDT_PROBE0(if_stf, , encapcheck, accept);
  375. /* stf interface makes single side match only */
  376. return (32);
  377. }
  378. static int
  379. stf_getsrcifa6(struct ifnet *ifp, struct in6_addr *addr, struct in6_addr *mask)
  380. {
  381. struct ifaddr *ia;
  382. struct in_ifaddr *ia4;
  383. struct in6_addr addr6, mask6;
  384. struct sockaddr_in sin4;
  385. struct stf_softc *sc;
  386. struct in_addr in;
  387. NET_EPOCH_ASSERT();
  388. sc = ifp->if_softc;
  389. SDT_PROBE3(if_stf, , getsrcifa6, in, ifp, addr, mask);
  390. CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
  391. if (ia->ifa_addr->sa_family != AF_INET6)
  392. continue;
  393. addr6 = *IFA_IN6(ia);
  394. mask6 = *IFA_MASKIN6(ia);
  395. if (sc->srcv4_addr != INADDR_ANY)
  396. bcopy(&sc->srcv4_addr, &in, sizeof(in));
  397. else {
  398. if (stf_getin4addr(sc, &sin4, addr6, mask6) == NULL)
  399. continue;
  400. bcopy(&sin4.sin_addr, &in, sizeof(in));
  401. }
  402. CK_LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
  403. if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
  404. break;
  405. if (ia4 == NULL)
  406. continue;
  407. *addr = addr6;
  408. *mask = mask6;
  409. SDT_PROBE2(if_stf, , getsrcifa6, found, addr, mask);
  410. return (0);
  411. }
  412. SDT_PROBE0(if_stf, , getsrcifa6, notfound);
  413. return (ENOENT);
  414. }
  415. static int
  416. stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
  417. struct route *ro)
  418. {
  419. struct stf_softc *sc;
  420. const struct sockaddr_in6 *dst6;
  421. struct sockaddr_in dst4, src4;
  422. u_int8_t tos;
  423. struct ip *ip;
  424. struct ip6_hdr *ip6;
  425. struct in6_addr addr6, mask6;
  426. int error;
  427. SDT_PROBE4(if_stf, , stf_output, in, ifp, m, dst, ro);
  428. #ifdef MAC
  429. error = mac_ifnet_check_transmit(ifp, m);
  430. if (error) {
  431. m_freem(m);
  432. SDT_PROBE2(if_stf, , stf_output, error, error, __LINE__);
  433. return (error);
  434. }
  435. #endif
  436. sc = ifp->if_softc;
  437. dst6 = (const struct sockaddr_in6 *)dst;
  438. /* just in case */
  439. if ((ifp->if_flags & IFF_UP) == 0) {
  440. m_freem(m);
  441. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  442. SDT_PROBE2(if_stf, , stf_output, error, ENETDOWN, __LINE__);
  443. return (ENETDOWN);
  444. }
  445. /*
  446. * If we don't have an ip4 address that match my inner ip6 address,
  447. * we shouldn't generate output. Without this check, we'll end up
  448. * using wrong IPv4 source.
  449. */
  450. if (stf_getsrcifa6(ifp, &addr6, &mask6) != 0) {
  451. m_freem(m);
  452. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  453. SDT_PROBE2(if_stf, , stf_output, error, ENETDOWN, __LINE__);
  454. return (ENETDOWN);
  455. }
  456. if (m->m_len < sizeof(*ip6)) {
  457. m = m_pullup(m, sizeof(*ip6));
  458. if (!m) {
  459. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  460. SDT_PROBE2(if_stf, , stf_output, error, ENOBUFS,
  461. __LINE__);
  462. return (ENOBUFS);
  463. }
  464. }
  465. ip6 = mtod(m, struct ip6_hdr *);
  466. tos = IPV6_TRAFFIC_CLASS(ip6);
  467. /*
  468. * Pickup the right outer dst addr from the list of candidates.
  469. * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
  470. */
  471. if (stf_getin4addr_in6(sc, &dst4, addr6, mask6,
  472. ip6->ip6_dst) == NULL) {
  473. if (sc->braddr != INADDR_ANY)
  474. dst4.sin_addr.s_addr = sc->braddr;
  475. else if (stf_getin4addr_in6(sc, &dst4, addr6, mask6,
  476. dst6->sin6_addr) == NULL) {
  477. m_freem(m);
  478. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  479. SDT_PROBE2(if_stf, , stf_output, error, ENETUNREACH,
  480. __LINE__);
  481. return (ENETUNREACH);
  482. }
  483. }
  484. if (bpf_peers_present(ifp->if_bpf)) {
  485. /*
  486. * We need to prepend the address family as
  487. * a four byte field. Cons up a dummy header
  488. * to pacify bpf. This is safe because bpf
  489. * will only read from the mbuf (i.e., it won't
  490. * try to free it or keep a pointer a to it).
  491. */
  492. u_int af = AF_INET6;
  493. bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
  494. }
  495. M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
  496. if (m == NULL) {
  497. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  498. SDT_PROBE2(if_stf, , stf_output, error, ENOBUFS, __LINE__);
  499. return (ENOBUFS);
  500. }
  501. ip = mtod(m, struct ip *);
  502. bzero(ip, sizeof(*ip));
  503. if (sc->srcv4_addr != INADDR_ANY)
  504. src4.sin_addr.s_addr = sc->srcv4_addr;
  505. else if (stf_getin4addr(sc, &src4, addr6, mask6) == NULL) {
  506. m_freem(m);
  507. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  508. SDT_PROBE2(if_stf, , stf_output, error, ENETUNREACH, __LINE__);
  509. return (ENETUNREACH);
  510. }
  511. bcopy(&src4.sin_addr, &ip->ip_src, sizeof(ip->ip_src));
  512. bcopy(&dst4.sin_addr, &ip->ip_dst, sizeof(ip->ip_dst));
  513. ip->ip_p = IPPROTO_IPV6;
  514. ip->ip_ttl = ip_stf_ttl;
  515. ip->ip_len = htons(m->m_pkthdr.len);
  516. if (ifp->if_flags & IFF_LINK1)
  517. ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
  518. else
  519. ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
  520. M_SETFIB(m, sc->sc_fibnum);
  521. if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
  522. error = ip_output(m, NULL, NULL, 0, NULL, NULL);
  523. SDT_PROBE1(if_stf, , stf_output, out, error);
  524. return (error);
  525. }
  526. static int
  527. isrfc1918addr(struct in_addr *in)
  528. {
  529. /*
  530. * returns 1 if private address range:
  531. * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
  532. */
  533. if (stf_permit_rfc1918 == 0 && (
  534. (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
  535. (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
  536. (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168))
  537. return (1);
  538. return (0);
  539. }
  540. static int
  541. stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp)
  542. {
  543. struct in_ifaddr *ia4;
  544. /*
  545. * reject packets with the following address:
  546. * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
  547. */
  548. if (IN_MULTICAST(ntohl(in->s_addr)))
  549. return (-1);
  550. switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
  551. case 0: case 127: case 255:
  552. return (-1);
  553. }
  554. /*
  555. * reject packets with broadcast
  556. */
  557. CK_STAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
  558. if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
  559. continue;
  560. if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
  561. return (-1);
  562. }
  563. }
  564. /*
  565. * perform ingress filter
  566. */
  567. if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) {
  568. struct nhop_object *nh;
  569. NET_EPOCH_ASSERT();
  570. nh = fib4_lookup(sc->sc_fibnum, *in, 0, 0, 0);
  571. if (nh == NULL)
  572. return (-1);
  573. if (nh->nh_ifp != inifp)
  574. return (-1);
  575. }
  576. return (0);
  577. }
  578. static int
  579. stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp)
  580. {
  581. SDT_PROBE3(if_stf, , checkaddr6, in, sc, in6, inifp);
  582. /*
  583. * check 6to4 addresses
  584. */
  585. if (IN6_IS_ADDR_6TO4(in6)) {
  586. struct in_addr in4;
  587. int ret;
  588. bcopy(GET_V4(in6), &in4, sizeof(in4));
  589. ret = stf_checkaddr4(sc, &in4, inifp);
  590. SDT_PROBE2(if_stf, , checkaddr6, out, ret, __LINE__);
  591. return (ret);
  592. }
  593. /*
  594. * reject anything that look suspicious. the test is implemented
  595. * in ip6_input too, but we check here as well to
  596. * (1) reject bad packets earlier, and
  597. * (2) to be safe against future ip6_input change.
  598. */
  599. if (IN6_IS_ADDR_V4COMPAT(in6)) {
  600. SDT_PROBE2(if_stf, , checkaddr6, out, -1, __LINE__);
  601. return (-1);
  602. }
  603. if (IN6_IS_ADDR_V4MAPPED(in6)) {
  604. SDT_PROBE2(if_stf, , checkaddr6, out, -1, __LINE__);
  605. return (-1);
  606. }
  607. SDT_PROBE2(if_stf, , checkaddr6, out, 0, __LINE__);
  608. return (0);
  609. }
  610. static int
  611. in_stf_input(struct mbuf *m, int off, int proto, void *arg)
  612. {
  613. struct stf_softc *sc = arg;
  614. struct ip ip;
  615. struct ip6_hdr *ip6;
  616. u_int8_t otos, itos;
  617. struct ifnet *ifp;
  618. struct nhop_object *nh;
  619. NET_EPOCH_ASSERT();
  620. SDT_PROBE3(if_stf, , stf_input, in, m, off, proto);
  621. if (proto != IPPROTO_IPV6) {
  622. m_freem(m);
  623. SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
  624. return (IPPROTO_DONE);
  625. }
  626. m_copydata(m, 0, sizeof(struct ip), (caddr_t)&ip);
  627. if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) {
  628. m_freem(m);
  629. SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
  630. return (IPPROTO_DONE);
  631. }
  632. ifp = STF2IFP(sc);
  633. #ifdef MAC
  634. mac_ifnet_create_mbuf(ifp, m);
  635. #endif
  636. /*
  637. * perform sanity check against outer src/dst.
  638. * for source, perform ingress filter as well.
  639. */
  640. if (stf_checkaddr4(sc, &ip.ip_dst, NULL) < 0 ||
  641. stf_checkaddr4(sc, &ip.ip_src, m->m_pkthdr.rcvif) < 0) {
  642. m_freem(m);
  643. SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
  644. return (IPPROTO_DONE);
  645. }
  646. otos = ip.ip_tos;
  647. m_adj(m, off);
  648. if (m->m_len < sizeof(*ip6)) {
  649. m = m_pullup(m, sizeof(*ip6));
  650. if (!m) {
  651. SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE,
  652. __LINE__);
  653. return (IPPROTO_DONE);
  654. }
  655. }
  656. ip6 = mtod(m, struct ip6_hdr *);
  657. /*
  658. * perform sanity check against inner src/dst.
  659. * for source, perform ingress filter as well.
  660. */
  661. if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
  662. stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
  663. m_freem(m);
  664. SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
  665. return (IPPROTO_DONE);
  666. }
  667. /*
  668. * reject packets with private address range.
  669. * (requirement from RFC3056 section 2 1st paragraph)
  670. */
  671. if ((IN6_IS_ADDR_6TO4(&ip6->ip6_src) && isrfc1918addr(&ip.ip_src)) ||
  672. (IN6_IS_ADDR_6TO4(&ip6->ip6_dst) && isrfc1918addr(&ip.ip_dst))) {
  673. m_freem(m);
  674. SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
  675. return (IPPROTO_DONE);
  676. }
  677. /*
  678. * Ignore if the destination is the same stf interface because
  679. * all of valid IPv6 outgoing traffic should go interfaces
  680. * except for it.
  681. */
  682. nh = fib6_lookup(sc->sc_fibnum, &ip6->ip6_dst, 0, 0, 0);
  683. if (nh == NULL) {
  684. m_free(m);
  685. SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
  686. return (IPPROTO_DONE);
  687. }
  688. if ((nh->nh_ifp == ifp) &&
  689. (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &nh->gw6_sa.sin6_addr))) {
  690. m_free(m);
  691. SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
  692. return (IPPROTO_DONE);
  693. }
  694. itos = IPV6_TRAFFIC_CLASS(ip6);
  695. if ((ifp->if_flags & IFF_LINK1) != 0)
  696. ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
  697. else
  698. ip_ecn_egress(ECN_NOCARE, &otos, &itos);
  699. ip6->ip6_flow &= ~htonl(0xff << 20);
  700. ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
  701. m->m_pkthdr.rcvif = ifp;
  702. if (bpf_peers_present(ifp->if_bpf)) {
  703. /*
  704. * We need to prepend the address family as
  705. * a four byte field. Cons up a dummy header
  706. * to pacify bpf. This is safe because bpf
  707. * will only read from the mbuf (i.e., it won't
  708. * try to free it or keep a pointer a to it).
  709. */
  710. u_int32_t af = AF_INET6;
  711. bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
  712. }
  713. /*
  714. * Put the packet to the network layer input queue according to the
  715. * specified address family.
  716. * See net/if_gif.c for possible issues with packet processing
  717. * reorder due to extra queueing.
  718. */
  719. if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
  720. if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
  721. M_SETFIB(m, ifp->if_fib);
  722. netisr_dispatch(NETISR_IPV6, m);
  723. SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
  724. return (IPPROTO_DONE);
  725. }
  726. static struct sockaddr_in *
  727. stf_getin4addr_in6(struct stf_softc *sc, struct sockaddr_in *sin,
  728. struct in6_addr addr6, struct in6_addr mask6, struct in6_addr in6)
  729. {
  730. int i;
  731. struct sockaddr_in *out;
  732. /*
  733. * When (src addr & src mask) != (in6 & src mask),
  734. * the dst is not in the 6rd domain. The IPv4 address must
  735. * not be used.
  736. */
  737. for (i = 0; i < sizeof(addr6); i++) {
  738. if ((((u_char *)&addr6)[i] & ((u_char *)&mask6)[i]) !=
  739. (((u_char *)&in6)[i] & ((u_char *)&mask6)[i])) {
  740. SDT_PROBE4(if_stf, , getin4addr_in6, out, &addr6,
  741. &mask6, &in6, NULL);
  742. return (NULL);
  743. }
  744. }
  745. /* After the mask check, use in6 instead of addr6. */
  746. out = stf_getin4addr(sc, sin, in6, mask6);
  747. SDT_PROBE4(if_stf, , getin4addr_in6, out, &addr6, &mask6, &in6, out);
  748. return (out);
  749. }
  750. static struct sockaddr_in *
  751. stf_getin4addr(struct stf_softc *sc, struct sockaddr_in *sin,
  752. struct in6_addr addr6, struct in6_addr mask6)
  753. {
  754. struct in_addr *in;
  755. SDT_PROBE2(if_stf, , getin4addr, in, &addr6, &mask6);
  756. memset(sin, 0, sizeof(*sin));
  757. in = &sin->sin_addr;
  758. if (IN6_IS_ADDR_6TO4(&addr6)) {
  759. /* 6to4 (RFC 3056) */
  760. bcopy(GET_V4(&addr6), in, sizeof(*in));
  761. if (isrfc1918addr(in))
  762. return (NULL);
  763. } else {
  764. /* 6rd (RFC 5569) */
  765. in_addr_t v4prefix;
  766. uint8_t *v6 = (uint8_t*)&addr6;
  767. uint64_t v6prefix;
  768. u_int plen;
  769. u_int v4suffixlen;
  770. v4prefix = 0;
  771. if (sc->v4prefixlen < 32) {
  772. v4suffixlen = 32 - sc->v4prefixlen;
  773. v4prefix = ntohl(sc->srcv4_addr) &
  774. (0xffffffffU << v4suffixlen);
  775. } else {
  776. MPASS(sc->v4prefixlen == 32);
  777. v4suffixlen = 32;
  778. }
  779. plen = in6_mask2len(&mask6, NULL);
  780. if (plen > 64)
  781. return (NULL);
  782. /* To make this simple we do not support prefixes longer than
  783. * 64 bits. RFC5969 says "a 6rd delegated prefix SHOULD be /64
  784. * or shorter." so this is a moderately safe assumption. */
  785. v6prefix = be64toh(*(uint64_t *)v6);
  786. /* Shift away the v6 prefix itself. */
  787. v6prefix <<= plen;
  788. v6prefix >>= plen;
  789. /* Now shift away everything after the v4 address. */
  790. v6prefix >>= 64 - plen - v4suffixlen;
  791. sin->sin_addr.s_addr = htonl(v4prefix | (uint32_t)v6prefix);
  792. }
  793. SDT_PROBE1(if_stf, , getin4addr, out, sin);
  794. return (sin);
  795. }
  796. static int
  797. stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  798. {
  799. struct ifaddr *ifa;
  800. struct ifdrv *ifd;
  801. struct ifreq *ifr;
  802. struct sockaddr_in sin4;
  803. struct stf_softc *sc_cur;
  804. struct stfv4args args;
  805. int error, mtu;
  806. error = 0;
  807. sc_cur = ifp->if_softc;
  808. switch (cmd) {
  809. case SIOCSDRVSPEC:
  810. ifd = (struct ifdrv *)data;
  811. error = priv_check(curthread, PRIV_NET_ADDIFADDR);
  812. if (error)
  813. break;
  814. if (ifd->ifd_cmd == STF6RD_SV4NET) {
  815. if (ifd->ifd_len != sizeof(args)) {
  816. error = EINVAL;
  817. break;
  818. }
  819. bzero(&args, sizeof(args));
  820. error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
  821. if (error)
  822. break;
  823. if (args.v4_prefixlen < 1 || args.v4_prefixlen > 32) {
  824. error = EINVAL;
  825. break;
  826. }
  827. bcopy(&args.srcv4_addr, &sc_cur->srcv4_addr,
  828. sizeof(sc_cur->srcv4_addr));
  829. sc_cur->v4prefixlen = args.v4_prefixlen;
  830. SDT_PROBE3(if_stf, , ioctl, sv4net, sc_cur->srcv4_addr,
  831. sc_cur->srcv4_addr, sc_cur->v4prefixlen);
  832. } else if (ifd->ifd_cmd == STF6RD_SBR) {
  833. if (ifd->ifd_len != sizeof(args)) {
  834. error = EINVAL;
  835. break;
  836. }
  837. bzero(&args, sizeof(args));
  838. error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
  839. if (error)
  840. break;
  841. sc_cur->braddr = args.braddr.s_addr;
  842. SDT_PROBE1(if_stf, , ioctl, sdstv4,
  843. sc_cur->braddr);
  844. } else
  845. error = EINVAL;
  846. break;
  847. case SIOCGDRVSPEC:
  848. ifd = (struct ifdrv *)data;
  849. if (ifd->ifd_cmd != STF6RD_GV4NET) {
  850. error = EINVAL;
  851. break;
  852. }
  853. if (ifd->ifd_len != sizeof(args)) {
  854. error = EINVAL;
  855. break;
  856. }
  857. bzero(&args, sizeof(args));
  858. args.srcv4_addr.s_addr = sc_cur->srcv4_addr;
  859. args.braddr.s_addr = sc_cur->braddr;
  860. args.v4_prefixlen = sc_cur->v4prefixlen;
  861. error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
  862. break;
  863. case SIOCSIFADDR:
  864. ifa = (struct ifaddr *)data;
  865. SDT_PROBE1(if_stf, , ioctl, ifaddr, ifa);
  866. if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
  867. error = EAFNOSUPPORT;
  868. break;
  869. }
  870. if (stf_getin4addr(sc_cur, &sin4,
  871. satosin6(ifa->ifa_addr)->sin6_addr,
  872. satosin6(ifa->ifa_netmask)->sin6_addr) == NULL) {
  873. error = EINVAL;
  874. break;
  875. }
  876. ifp->if_flags |= IFF_UP;
  877. ifp->if_drv_flags |= IFF_DRV_RUNNING;
  878. break;
  879. case SIOCADDMULTI:
  880. case SIOCDELMULTI:
  881. ifr = (struct ifreq *)data;
  882. if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
  883. ;
  884. else
  885. error = EAFNOSUPPORT;
  886. break;
  887. case SIOCGIFMTU:
  888. break;
  889. case SIOCSIFMTU:
  890. ifr = (struct ifreq *)data;
  891. mtu = ifr->ifr_mtu;
  892. /* RFC 4213 3.2 ideal world MTU */
  893. if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20)
  894. return (EINVAL);
  895. ifp->if_mtu = mtu;
  896. break;
  897. default:
  898. error = EINVAL;
  899. break;
  900. }
  901. return (error);
  902. }