ip6_forward.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /* $OpenBSD: ip6_forward.c,v 1.79 2015/07/16 21:14:21 mpi Exp $ */
  2. /* $KAME: ip6_forward.c,v 1.75 2001/06/29 12:42:13 jinmei Exp $ */
  3. /*
  4. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  5. * 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 project 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 PROJECT 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 PROJECT 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. #include "pf.h"
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/mbuf.h>
  35. #include <sys/domain.h>
  36. #include <sys/protosw.h>
  37. #include <sys/socket.h>
  38. #include <sys/errno.h>
  39. #include <sys/time.h>
  40. #include <sys/kernel.h>
  41. #include <sys/syslog.h>
  42. #include <net/if.h>
  43. #include <net/if_var.h>
  44. #include <net/if_enc.h>
  45. #include <net/route.h>
  46. #include <netinet/in.h>
  47. #include <netinet/ip_var.h>
  48. #include <netinet6/in6_var.h>
  49. #include <netinet/ip6.h>
  50. #include <netinet6/ip6_var.h>
  51. #include <netinet/icmp6.h>
  52. #include <netinet6/nd6.h>
  53. #if NPF > 0
  54. #include <net/pfvar.h>
  55. #endif
  56. #ifdef IPSEC
  57. #include <netinet/ip_ipsp.h>
  58. #include <netinet/ip_ah.h>
  59. #include <netinet/ip_esp.h>
  60. #include <netinet/udp.h>
  61. #include <netinet/tcp.h>
  62. #endif
  63. struct route_in6 ip6_forward_rt;
  64. /*
  65. * Forward a packet. If some error occurs return the sender
  66. * an icmp packet. Note we can't always generate a meaningful
  67. * icmp message because icmp doesn't have a large enough repertoire
  68. * of codes and types.
  69. *
  70. * If not forwarding, just drop the packet. This could be confusing
  71. * if ipforwarding was zero but some routing protocol was advancing
  72. * us as a gateway to somewhere. However, we must let the routing
  73. * protocol deal with that.
  74. *
  75. */
  76. void
  77. ip6_forward(struct mbuf *m, int srcrt)
  78. {
  79. struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  80. struct sockaddr_in6 *dst;
  81. struct rtentry *rt;
  82. int error = 0, type = 0, code = 0;
  83. struct mbuf *mcopy = NULL;
  84. #ifdef IPSEC
  85. u_int8_t sproto = 0;
  86. struct m_tag *mtag;
  87. union sockaddr_union sdst;
  88. struct tdb_ident *tdbi;
  89. u_int32_t sspi;
  90. struct tdb *tdb;
  91. #if NPF > 0
  92. struct ifnet *encif;
  93. #endif
  94. #endif /* IPSEC */
  95. u_int rtableid = 0;
  96. char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN];
  97. /*
  98. * Do not forward packets to multicast destination (should be handled
  99. * by ip6_mforward().
  100. * Do not forward packets with unspecified source. It was discussed
  101. * in July 2000, on ipngwg mailing list.
  102. */
  103. if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
  104. IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
  105. IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
  106. ip6stat.ip6s_cantforward++;
  107. /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
  108. if (ip6_log_time + ip6_log_interval < time_second) {
  109. ip6_log_time = time_second;
  110. inet_ntop(AF_INET6, &ip6->ip6_src, src6, sizeof(src6));
  111. inet_ntop(AF_INET6, &ip6->ip6_dst, dst6, sizeof(dst6));
  112. log(LOG_DEBUG,
  113. "cannot forward "
  114. "from %s to %s nxt %d received on inteface %u\n",
  115. src6, dst6,
  116. ip6->ip6_nxt,
  117. m->m_pkthdr.ph_ifidx);
  118. }
  119. m_freem(m);
  120. return;
  121. }
  122. if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
  123. /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
  124. icmp6_error(m, ICMP6_TIME_EXCEEDED,
  125. ICMP6_TIME_EXCEED_TRANSIT, 0);
  126. return;
  127. }
  128. ip6->ip6_hlim -= IPV6_HLIMDEC;
  129. /*
  130. * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
  131. * size of IPv6 + ICMPv6 headers) bytes of the packet in case
  132. * we need to generate an ICMP6 message to the src.
  133. * Thanks to M_EXT, in most cases copy will not occur.
  134. *
  135. * It is important to save it before IPsec processing as IPsec
  136. * processing may modify the mbuf.
  137. */
  138. mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
  139. M_NOWAIT);
  140. #if NPF > 0
  141. reroute:
  142. #endif
  143. #ifdef IPSEC
  144. if (!ipsec_in_use)
  145. goto done_spd;
  146. /*
  147. * Check if there was an outgoing SA bound to the flow
  148. * from a transport protocol.
  149. */
  150. /* Do we have any pending SAs to apply ? */
  151. tdb = ipsp_spd_lookup(m, AF_INET6, sizeof(struct ip6_hdr),
  152. &error, IPSP_DIRECTION_OUT, NULL, NULL, 0);
  153. if (tdb == NULL) {
  154. if (error == 0) {
  155. /*
  156. * No IPsec processing required, we'll just send the
  157. * packet out.
  158. */
  159. sproto = 0;
  160. /* Fall through to routing/multicast handling */
  161. } else {
  162. /*
  163. * -EINVAL is used to indicate that the packet should
  164. * be silently dropped, typically because we've asked
  165. * key management for an SA.
  166. */
  167. if (error == -EINVAL) /* Should silently drop packet */
  168. error = 0;
  169. m_freem(m);
  170. goto freecopy;
  171. }
  172. } else {
  173. /* Loop detection */
  174. for (mtag = m_tag_first(m); mtag != NULL;
  175. mtag = m_tag_next(m, mtag)) {
  176. if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE)
  177. continue;
  178. tdbi = (struct tdb_ident *)(mtag + 1);
  179. if (tdbi->spi == tdb->tdb_spi &&
  180. tdbi->proto == tdb->tdb_sproto &&
  181. tdbi->rdomain == tdb->tdb_rdomain &&
  182. !bcmp(&tdbi->dst, &tdb->tdb_dst,
  183. sizeof(union sockaddr_union))) {
  184. sproto = 0; /* mark as no-IPsec-needed */
  185. goto done_spd;
  186. }
  187. }
  188. /* We need to do IPsec */
  189. bcopy(&tdb->tdb_dst, &sdst, sizeof(sdst));
  190. sspi = tdb->tdb_spi;
  191. sproto = tdb->tdb_sproto;
  192. }
  193. /* Fall through to the routing/multicast handling code */
  194. done_spd:
  195. #endif /* IPSEC */
  196. #if NPF > 0
  197. rtableid = m->m_pkthdr.ph_rtableid;
  198. #endif
  199. dst = &ip6_forward_rt.ro_dst;
  200. if (!srcrt) {
  201. /*
  202. * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
  203. */
  204. if (ip6_forward_rt.ro_rt == NULL ||
  205. (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0 ||
  206. ip6_forward_rt.ro_tableid != rtableid) {
  207. if (ip6_forward_rt.ro_rt) {
  208. rtfree(ip6_forward_rt.ro_rt);
  209. ip6_forward_rt.ro_rt = NULL;
  210. }
  211. /* this probably fails but give it a try again */
  212. ip6_forward_rt.ro_tableid = rtableid;
  213. ip6_forward_rt.ro_rt = rtalloc_mpath(
  214. sin6tosa(&ip6_forward_rt.ro_dst),
  215. &ip6->ip6_src.s6_addr32[0],
  216. ip6_forward_rt.ro_tableid);
  217. }
  218. if (ip6_forward_rt.ro_rt == NULL) {
  219. ip6stat.ip6s_noroute++;
  220. /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
  221. if (mcopy) {
  222. icmp6_error(mcopy, ICMP6_DST_UNREACH,
  223. ICMP6_DST_UNREACH_NOROUTE, 0);
  224. }
  225. m_freem(m);
  226. return;
  227. }
  228. } else if (ip6_forward_rt.ro_rt == NULL ||
  229. (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0 ||
  230. !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr) ||
  231. ip6_forward_rt.ro_tableid != rtableid) {
  232. if (ip6_forward_rt.ro_rt) {
  233. rtfree(ip6_forward_rt.ro_rt);
  234. ip6_forward_rt.ro_rt = NULL;
  235. }
  236. bzero(dst, sizeof(*dst));
  237. dst->sin6_len = sizeof(struct sockaddr_in6);
  238. dst->sin6_family = AF_INET6;
  239. dst->sin6_addr = ip6->ip6_dst;
  240. ip6_forward_rt.ro_tableid = rtableid;
  241. ip6_forward_rt.ro_rt = rtalloc_mpath(
  242. sin6tosa(&ip6_forward_rt.ro_dst),
  243. &ip6->ip6_src.s6_addr32[0],
  244. ip6_forward_rt.ro_tableid);
  245. if (ip6_forward_rt.ro_rt == NULL) {
  246. ip6stat.ip6s_noroute++;
  247. /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
  248. if (mcopy) {
  249. icmp6_error(mcopy, ICMP6_DST_UNREACH,
  250. ICMP6_DST_UNREACH_NOROUTE, 0);
  251. }
  252. m_freem(m);
  253. return;
  254. }
  255. }
  256. rt = ip6_forward_rt.ro_rt;
  257. /*
  258. * Scope check: if a packet can't be delivered to its destination
  259. * for the reason that the destination is beyond the scope of the
  260. * source address, discard the packet and return an icmp6 destination
  261. * unreachable error with Code 2 (beyond scope of source address).
  262. * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1]
  263. */
  264. if (in6_addr2scopeid(m->m_pkthdr.ph_ifidx, &ip6->ip6_src) !=
  265. in6_addr2scopeid(rt->rt_ifp->if_index, &ip6->ip6_src)) {
  266. ip6stat.ip6s_cantforward++;
  267. ip6stat.ip6s_badscope++;
  268. in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
  269. if (ip6_log_time + ip6_log_interval < time_second) {
  270. ip6_log_time = time_second;
  271. inet_ntop(AF_INET6, &ip6->ip6_src, src6, sizeof(src6));
  272. inet_ntop(AF_INET6, &ip6->ip6_dst, dst6, sizeof(dst6));
  273. log(LOG_DEBUG,
  274. "cannot forward "
  275. "src %s, dst %s, nxt %d, rcvif %u, outif %u\n",
  276. src6, dst6,
  277. ip6->ip6_nxt,
  278. m->m_pkthdr.ph_ifidx, rt->rt_ifp->if_index);
  279. }
  280. if (mcopy)
  281. icmp6_error(mcopy, ICMP6_DST_UNREACH,
  282. ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
  283. m_freem(m);
  284. goto freert;
  285. }
  286. #ifdef IPSEC
  287. /*
  288. * Check if the packet needs encapsulation.
  289. * ipsp_process_packet will never come back to here.
  290. * XXX ipsp_process_packet() calls ip6_output(), and there'll be no
  291. * PMTU notification. is it okay?
  292. */
  293. if (sproto != 0) {
  294. tdb = gettdb(rtable_l2(m->m_pkthdr.ph_rtableid),
  295. sspi, &sdst, sproto);
  296. if (tdb == NULL) {
  297. error = EHOSTUNREACH;
  298. m_freem(m);
  299. goto senderr; /*XXX*/
  300. }
  301. #if NPF > 0
  302. if ((encif = enc_getif(tdb->tdb_rdomain,
  303. tdb->tdb_tap)) == NULL ||
  304. pf_test(AF_INET6, PF_FWD, encif, &m) != PF_PASS) {
  305. error = EHOSTUNREACH;
  306. m_freem(m);
  307. goto senderr;
  308. }
  309. if (m == NULL)
  310. goto senderr;
  311. ip6 = mtod(m, struct ip6_hdr *);
  312. /*
  313. * PF_TAG_REROUTE handling or not...
  314. * Packet is entering IPsec so the routing is
  315. * already overruled by the IPsec policy.
  316. * Until now the change was not reconsidered.
  317. * What's the behaviour?
  318. */
  319. #endif
  320. in6_proto_cksum_out(m, encif);
  321. m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
  322. /* Callee frees mbuf */
  323. error = ipsp_process_packet(m, tdb, AF_INET6, 0);
  324. m_freem(mcopy);
  325. goto freert;
  326. }
  327. #endif /* IPSEC */
  328. if (rt->rt_flags & RTF_GATEWAY)
  329. dst = satosin6(rt->rt_gateway);
  330. /*
  331. * If we are to forward the packet using the same interface
  332. * as one we got the packet from, perhaps we should send a redirect
  333. * to sender to shortcut a hop.
  334. * Only send redirect if source is sending directly to us,
  335. * and if packet was not source routed (or has any options).
  336. * Also, don't send redirect if forwarding using a route
  337. * modified by a redirect.
  338. */
  339. if (rt->rt_ifp->if_index == m->m_pkthdr.ph_ifidx && !srcrt &&
  340. ip6_sendredirects &&
  341. (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
  342. if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
  343. nd6_is_addr_neighbor(&ip6_forward_rt.ro_dst, rt->rt_ifp)) {
  344. /*
  345. * If the incoming interface is equal to the outgoing
  346. * one, the link attached to the interface is
  347. * point-to-point, and the IPv6 destination is
  348. * regarded as on-link on the link, then it will be
  349. * highly probable that the destination address does
  350. * not exist on the link and that the packet is going
  351. * to loop. Thus, we immediately drop the packet and
  352. * send an ICMPv6 error message.
  353. * For other routing loops, we dare to let the packet
  354. * go to the loop, so that a remote diagnosing host
  355. * can detect the loop by traceroute.
  356. * type/code is based on suggestion by Rich Draves.
  357. * not sure if it is the best pick.
  358. */
  359. if (mcopy)
  360. icmp6_error(mcopy, ICMP6_DST_UNREACH,
  361. ICMP6_DST_UNREACH_ADDR, 0);
  362. m_freem(m);
  363. goto freert;
  364. }
  365. type = ND_REDIRECT;
  366. }
  367. /*
  368. * Fake scoped addresses. Note that even link-local source or
  369. * destinaion can appear, if the originating node just sends the
  370. * packet to us (without address resolution for the destination).
  371. * Since both icmp6_error and icmp6_redirect_output fill the embedded
  372. * link identifiers, we can do this stuff after making a copy for
  373. * returning an error.
  374. */
  375. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
  376. ip6->ip6_src.s6_addr16[1] = 0;
  377. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
  378. ip6->ip6_dst.s6_addr16[1] = 0;
  379. #if NPF > 0
  380. if (pf_test(AF_INET6, PF_FWD, rt->rt_ifp, &m) != PF_PASS) {
  381. m_freem(m);
  382. goto senderr;
  383. }
  384. if (m == NULL)
  385. goto senderr;
  386. ip6 = mtod(m, struct ip6_hdr *);
  387. if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) ==
  388. (PF_TAG_REROUTE | PF_TAG_GENERATED)) {
  389. /* already rerun the route lookup, go on */
  390. m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE);
  391. } else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) {
  392. /* tag as generated to skip over pf_test on rerun */
  393. m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
  394. srcrt = 1;
  395. goto reroute;
  396. }
  397. #endif
  398. in6_proto_cksum_out(m, rt->rt_ifp);
  399. /* Check the size after pf_test to give pf a chance to refragment. */
  400. if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
  401. in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
  402. if (mcopy) {
  403. u_long mtu;
  404. mtu = IN6_LINKMTU(rt->rt_ifp);
  405. icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
  406. }
  407. m_freem(m);
  408. goto freert;
  409. }
  410. error = nd6_output(rt->rt_ifp, m, dst, rt);
  411. if (error) {
  412. in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
  413. ip6stat.ip6s_cantforward++;
  414. } else {
  415. ip6stat.ip6s_forward++;
  416. in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
  417. if (type)
  418. ip6stat.ip6s_redirectsent++;
  419. else {
  420. if (mcopy)
  421. goto freecopy;
  422. }
  423. }
  424. #if NPF > 0 || defined(IPSEC)
  425. senderr:
  426. #endif
  427. if (mcopy == NULL)
  428. goto freert;
  429. switch (error) {
  430. case 0:
  431. if (type == ND_REDIRECT) {
  432. icmp6_redirect_output(mcopy, rt);
  433. goto freert;
  434. }
  435. goto freecopy;
  436. case EMSGSIZE:
  437. /* xxx MTU is constant in PPP? */
  438. goto freecopy;
  439. case ENOBUFS:
  440. /* Tell source to slow down like source quench in IP? */
  441. goto freecopy;
  442. case ENETUNREACH: /* shouldn't happen, checked above */
  443. case EHOSTUNREACH:
  444. case ENETDOWN:
  445. case EHOSTDOWN:
  446. default:
  447. type = ICMP6_DST_UNREACH;
  448. code = ICMP6_DST_UNREACH_ADDR;
  449. break;
  450. }
  451. icmp6_error(mcopy, type, code, 0);
  452. goto freert;
  453. freecopy:
  454. m_freem(mcopy);
  455. freert:
  456. #ifndef SMALL_KERNEL
  457. if (ip6_multipath && ip6_forward_rt.ro_rt &&
  458. (ip6_forward_rt.ro_rt->rt_flags & RTF_MPATH)) {
  459. rtfree(ip6_forward_rt.ro_rt);
  460. ip6_forward_rt.ro_rt = NULL;
  461. }
  462. #endif
  463. return;
  464. }