exthdrs.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /*
  2. * Extension Header handling for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Andi Kleen <ak@muc.de>
  8. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /* Changes:
  16. * yoshfuji : ensure not to overrun while parsing
  17. * tlv options.
  18. * Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs().
  19. * YOSHIFUJI Hideaki @USAGI Register inbound extension header
  20. * handlers as inet6_protocol{}.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/net.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/in6.h>
  29. #include <linux/icmpv6.h>
  30. #include <linux/slab.h>
  31. #include <linux/export.h>
  32. #include <net/dst.h>
  33. #include <net/sock.h>
  34. #include <net/snmp.h>
  35. #include <net/ipv6.h>
  36. #include <net/protocol.h>
  37. #include <net/transp_v6.h>
  38. #include <net/rawv6.h>
  39. #include <net/ndisc.h>
  40. #include <net/ip6_route.h>
  41. #include <net/addrconf.h>
  42. #include <net/calipso.h>
  43. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  44. #include <net/xfrm.h>
  45. #endif
  46. #include <linux/seg6.h>
  47. #include <net/seg6.h>
  48. #ifdef CONFIG_IPV6_SEG6_HMAC
  49. #include <net/seg6_hmac.h>
  50. #endif
  51. #include <linux/uaccess.h>
  52. /*
  53. * Parsing tlv encoded headers.
  54. *
  55. * Parsing function "func" returns true, if parsing succeed
  56. * and false, if it failed.
  57. * It MUST NOT touch skb->h.
  58. */
  59. struct tlvtype_proc {
  60. int type;
  61. bool (*func)(struct sk_buff *skb, int offset);
  62. };
  63. /*********************
  64. Generic functions
  65. *********************/
  66. /* An unknown option is detected, decide what to do */
  67. static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
  68. {
  69. switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
  70. case 0: /* ignore */
  71. return true;
  72. case 1: /* drop packet */
  73. break;
  74. case 3: /* Send ICMP if not a multicast address and drop packet */
  75. /* Actually, it is redundant check. icmp_send
  76. will recheck in any case.
  77. */
  78. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
  79. break;
  80. case 2: /* send ICMP PARM PROB regardless and drop packet */
  81. icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff);
  82. return false;
  83. }
  84. kfree_skb(skb);
  85. return false;
  86. }
  87. /* Parse tlv encoded option header (hop-by-hop or destination) */
  88. static bool ip6_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb)
  89. {
  90. const struct tlvtype_proc *curr;
  91. const unsigned char *nh = skb_network_header(skb);
  92. int off = skb_network_header_len(skb);
  93. int len = (skb_transport_header(skb)[1] + 1) << 3;
  94. int padlen = 0;
  95. if (skb_transport_offset(skb) + len > skb_headlen(skb))
  96. goto bad;
  97. off += 2;
  98. len -= 2;
  99. while (len > 0) {
  100. int optlen = nh[off + 1] + 2;
  101. int i;
  102. switch (nh[off]) {
  103. case IPV6_TLV_PAD1:
  104. optlen = 1;
  105. padlen++;
  106. if (padlen > 7)
  107. goto bad;
  108. break;
  109. case IPV6_TLV_PADN:
  110. /* RFC 2460 states that the purpose of PadN is
  111. * to align the containing header to multiples
  112. * of 8. 7 is therefore the highest valid value.
  113. * See also RFC 4942, Section 2.1.9.5.
  114. */
  115. padlen += optlen;
  116. if (padlen > 7)
  117. goto bad;
  118. /* RFC 4942 recommends receiving hosts to
  119. * actively check PadN payload to contain
  120. * only zeroes.
  121. */
  122. for (i = 2; i < optlen; i++) {
  123. if (nh[off + i] != 0)
  124. goto bad;
  125. }
  126. break;
  127. default: /* Other TLV code so scan list */
  128. if (optlen > len)
  129. goto bad;
  130. for (curr = procs; curr->type >= 0; curr++) {
  131. if (curr->type == nh[off]) {
  132. /* type specific length/alignment
  133. checks will be performed in the
  134. func(). */
  135. if (curr->func(skb, off) == false)
  136. return false;
  137. break;
  138. }
  139. }
  140. if (curr->type < 0) {
  141. if (ip6_tlvopt_unknown(skb, off) == 0)
  142. return false;
  143. }
  144. padlen = 0;
  145. break;
  146. }
  147. off += optlen;
  148. len -= optlen;
  149. }
  150. if (len == 0)
  151. return true;
  152. bad:
  153. kfree_skb(skb);
  154. return false;
  155. }
  156. /*****************************
  157. Destination options header.
  158. *****************************/
  159. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  160. static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
  161. {
  162. struct ipv6_destopt_hao *hao;
  163. struct inet6_skb_parm *opt = IP6CB(skb);
  164. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  165. struct in6_addr tmp_addr;
  166. int ret;
  167. if (opt->dsthao) {
  168. net_dbg_ratelimited("hao duplicated\n");
  169. goto discard;
  170. }
  171. opt->dsthao = opt->dst1;
  172. opt->dst1 = 0;
  173. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
  174. if (hao->length != 16) {
  175. net_dbg_ratelimited("hao invalid option length = %d\n",
  176. hao->length);
  177. goto discard;
  178. }
  179. if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
  180. net_dbg_ratelimited("hao is not an unicast addr: %pI6\n",
  181. &hao->addr);
  182. goto discard;
  183. }
  184. ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
  185. (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
  186. if (unlikely(ret < 0))
  187. goto discard;
  188. if (skb_cloned(skb)) {
  189. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  190. goto discard;
  191. /* update all variable using below by copied skbuff */
  192. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
  193. optoff);
  194. ipv6h = ipv6_hdr(skb);
  195. }
  196. if (skb->ip_summed == CHECKSUM_COMPLETE)
  197. skb->ip_summed = CHECKSUM_NONE;
  198. tmp_addr = ipv6h->saddr;
  199. ipv6h->saddr = hao->addr;
  200. hao->addr = tmp_addr;
  201. if (skb->tstamp == 0)
  202. __net_timestamp(skb);
  203. return true;
  204. discard:
  205. kfree_skb(skb);
  206. return false;
  207. }
  208. #endif
  209. static const struct tlvtype_proc tlvprocdestopt_lst[] = {
  210. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  211. {
  212. .type = IPV6_TLV_HAO,
  213. .func = ipv6_dest_hao,
  214. },
  215. #endif
  216. {-1, NULL}
  217. };
  218. static int ipv6_destopt_rcv(struct sk_buff *skb)
  219. {
  220. struct inet6_skb_parm *opt = IP6CB(skb);
  221. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  222. __u16 dstbuf;
  223. #endif
  224. struct dst_entry *dst = skb_dst(skb);
  225. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  226. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  227. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  228. __IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
  229. IPSTATS_MIB_INHDRERRORS);
  230. kfree_skb(skb);
  231. return -1;
  232. }
  233. opt->lastopt = opt->dst1 = skb_network_header_len(skb);
  234. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  235. dstbuf = opt->dst1;
  236. #endif
  237. if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
  238. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  239. opt = IP6CB(skb);
  240. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  241. opt->nhoff = dstbuf;
  242. #else
  243. opt->nhoff = opt->dst1;
  244. #endif
  245. return 1;
  246. }
  247. __IP6_INC_STATS(dev_net(dst->dev),
  248. ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
  249. return -1;
  250. }
  251. static void seg6_update_csum(struct sk_buff *skb)
  252. {
  253. struct ipv6_sr_hdr *hdr;
  254. struct in6_addr *addr;
  255. __be32 from, to;
  256. /* srh is at transport offset and seg_left is already decremented
  257. * but daddr is not yet updated with next segment
  258. */
  259. hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  260. addr = hdr->segments + hdr->segments_left;
  261. hdr->segments_left++;
  262. from = *(__be32 *)hdr;
  263. hdr->segments_left--;
  264. to = *(__be32 *)hdr;
  265. /* update skb csum with diff resulting from seg_left decrement */
  266. update_csum_diff4(skb, from, to);
  267. /* compute csum diff between current and next segment and update */
  268. update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr),
  269. (__be32 *)addr);
  270. }
  271. static int ipv6_srh_rcv(struct sk_buff *skb)
  272. {
  273. struct inet6_skb_parm *opt = IP6CB(skb);
  274. struct net *net = dev_net(skb->dev);
  275. struct ipv6_sr_hdr *hdr;
  276. struct inet6_dev *idev;
  277. struct in6_addr *addr;
  278. bool cleanup = false;
  279. int accept_seg6;
  280. hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  281. idev = __in6_dev_get(skb->dev);
  282. accept_seg6 = net->ipv6.devconf_all->seg6_enabled;
  283. if (accept_seg6 > idev->cnf.seg6_enabled)
  284. accept_seg6 = idev->cnf.seg6_enabled;
  285. if (!accept_seg6) {
  286. kfree_skb(skb);
  287. return -1;
  288. }
  289. #ifdef CONFIG_IPV6_SEG6_HMAC
  290. if (!seg6_hmac_validate_skb(skb)) {
  291. kfree_skb(skb);
  292. return -1;
  293. }
  294. #endif
  295. looped_back:
  296. if (hdr->segments_left > 0) {
  297. if (hdr->nexthdr != NEXTHDR_IPV6 && hdr->segments_left == 1 &&
  298. sr_has_cleanup(hdr))
  299. cleanup = true;
  300. } else {
  301. if (hdr->nexthdr == NEXTHDR_IPV6) {
  302. int offset = (hdr->hdrlen + 1) << 3;
  303. skb_postpull_rcsum(skb, skb_network_header(skb),
  304. skb_network_header_len(skb));
  305. if (!pskb_pull(skb, offset)) {
  306. kfree_skb(skb);
  307. return -1;
  308. }
  309. skb_postpull_rcsum(skb, skb_transport_header(skb),
  310. offset);
  311. skb_reset_network_header(skb);
  312. skb_reset_transport_header(skb);
  313. skb->encapsulation = 0;
  314. __skb_tunnel_rx(skb, skb->dev, net);
  315. netif_rx(skb);
  316. return -1;
  317. }
  318. opt->srcrt = skb_network_header_len(skb);
  319. opt->lastopt = opt->srcrt;
  320. skb->transport_header += (hdr->hdrlen + 1) << 3;
  321. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  322. return 1;
  323. }
  324. if (hdr->segments_left >= (hdr->hdrlen >> 1)) {
  325. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  326. IPSTATS_MIB_INHDRERRORS);
  327. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  328. ((&hdr->segments_left) -
  329. skb_network_header(skb)));
  330. kfree_skb(skb);
  331. return -1;
  332. }
  333. if (skb_cloned(skb)) {
  334. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  335. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  336. IPSTATS_MIB_OUTDISCARDS);
  337. kfree_skb(skb);
  338. return -1;
  339. }
  340. }
  341. hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  342. hdr->segments_left--;
  343. addr = hdr->segments + hdr->segments_left;
  344. skb_push(skb, sizeof(struct ipv6hdr));
  345. if (skb->ip_summed == CHECKSUM_COMPLETE)
  346. seg6_update_csum(skb);
  347. ipv6_hdr(skb)->daddr = *addr;
  348. if (cleanup) {
  349. int srhlen = (hdr->hdrlen + 1) << 3;
  350. int nh = hdr->nexthdr;
  351. skb_pull_rcsum(skb, sizeof(struct ipv6hdr) + srhlen);
  352. memmove(skb_network_header(skb) + srhlen,
  353. skb_network_header(skb),
  354. (unsigned char *)hdr - skb_network_header(skb));
  355. skb->network_header += srhlen;
  356. ipv6_hdr(skb)->nexthdr = nh;
  357. ipv6_hdr(skb)->payload_len = htons(skb->len -
  358. sizeof(struct ipv6hdr));
  359. skb_push_rcsum(skb, sizeof(struct ipv6hdr));
  360. }
  361. skb_dst_drop(skb);
  362. ip6_route_input(skb);
  363. if (skb_dst(skb)->error) {
  364. dst_input(skb);
  365. return -1;
  366. }
  367. if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
  368. if (ipv6_hdr(skb)->hop_limit <= 1) {
  369. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  370. IPSTATS_MIB_INHDRERRORS);
  371. icmpv6_send(skb, ICMPV6_TIME_EXCEED,
  372. ICMPV6_EXC_HOPLIMIT, 0);
  373. kfree_skb(skb);
  374. return -1;
  375. }
  376. ipv6_hdr(skb)->hop_limit--;
  377. /* be sure that srh is still present before reinjecting */
  378. if (!cleanup) {
  379. skb_pull(skb, sizeof(struct ipv6hdr));
  380. goto looped_back;
  381. }
  382. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  383. IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
  384. }
  385. dst_input(skb);
  386. return -1;
  387. }
  388. /********************************
  389. Routing header.
  390. ********************************/
  391. /* called with rcu_read_lock() */
  392. static int ipv6_rthdr_rcv(struct sk_buff *skb)
  393. {
  394. struct inet6_skb_parm *opt = IP6CB(skb);
  395. struct in6_addr *addr = NULL;
  396. struct in6_addr daddr;
  397. struct inet6_dev *idev;
  398. int n, i;
  399. struct ipv6_rt_hdr *hdr;
  400. struct rt0_hdr *rthdr;
  401. struct net *net = dev_net(skb->dev);
  402. int accept_source_route = net->ipv6.devconf_all->accept_source_route;
  403. idev = __in6_dev_get(skb->dev);
  404. if (idev && accept_source_route > idev->cnf.accept_source_route)
  405. accept_source_route = idev->cnf.accept_source_route;
  406. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  407. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  408. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  409. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  410. IPSTATS_MIB_INHDRERRORS);
  411. kfree_skb(skb);
  412. return -1;
  413. }
  414. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  415. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
  416. skb->pkt_type != PACKET_HOST) {
  417. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  418. IPSTATS_MIB_INADDRERRORS);
  419. kfree_skb(skb);
  420. return -1;
  421. }
  422. /* segment routing */
  423. if (hdr->type == IPV6_SRCRT_TYPE_4)
  424. return ipv6_srh_rcv(skb);
  425. looped_back:
  426. if (hdr->segments_left == 0) {
  427. switch (hdr->type) {
  428. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  429. case IPV6_SRCRT_TYPE_2:
  430. /* Silently discard type 2 header unless it was
  431. * processed by own
  432. */
  433. if (!addr) {
  434. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  435. IPSTATS_MIB_INADDRERRORS);
  436. kfree_skb(skb);
  437. return -1;
  438. }
  439. break;
  440. #endif
  441. default:
  442. break;
  443. }
  444. opt->lastopt = opt->srcrt = skb_network_header_len(skb);
  445. skb->transport_header += (hdr->hdrlen + 1) << 3;
  446. opt->dst0 = opt->dst1;
  447. opt->dst1 = 0;
  448. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  449. return 1;
  450. }
  451. switch (hdr->type) {
  452. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  453. case IPV6_SRCRT_TYPE_2:
  454. if (accept_source_route < 0)
  455. goto unknown_rh;
  456. /* Silently discard invalid RTH type 2 */
  457. if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
  458. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  459. IPSTATS_MIB_INHDRERRORS);
  460. kfree_skb(skb);
  461. return -1;
  462. }
  463. break;
  464. #endif
  465. default:
  466. goto unknown_rh;
  467. }
  468. /*
  469. * This is the routing header forwarding algorithm from
  470. * RFC 2460, page 16.
  471. */
  472. n = hdr->hdrlen >> 1;
  473. if (hdr->segments_left > n) {
  474. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  475. IPSTATS_MIB_INHDRERRORS);
  476. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  477. ((&hdr->segments_left) -
  478. skb_network_header(skb)));
  479. return -1;
  480. }
  481. /* We are about to mangle packet header. Be careful!
  482. Do not damage packets queued somewhere.
  483. */
  484. if (skb_cloned(skb)) {
  485. /* the copy is a forwarded packet */
  486. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  487. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  488. IPSTATS_MIB_OUTDISCARDS);
  489. kfree_skb(skb);
  490. return -1;
  491. }
  492. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  493. }
  494. if (skb->ip_summed == CHECKSUM_COMPLETE)
  495. skb->ip_summed = CHECKSUM_NONE;
  496. i = n - --hdr->segments_left;
  497. rthdr = (struct rt0_hdr *) hdr;
  498. addr = rthdr->addr;
  499. addr += i - 1;
  500. switch (hdr->type) {
  501. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  502. case IPV6_SRCRT_TYPE_2:
  503. if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
  504. (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  505. IPPROTO_ROUTING) < 0) {
  506. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  507. IPSTATS_MIB_INADDRERRORS);
  508. kfree_skb(skb);
  509. return -1;
  510. }
  511. if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
  512. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  513. IPSTATS_MIB_INADDRERRORS);
  514. kfree_skb(skb);
  515. return -1;
  516. }
  517. break;
  518. #endif
  519. default:
  520. break;
  521. }
  522. if (ipv6_addr_is_multicast(addr)) {
  523. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  524. IPSTATS_MIB_INADDRERRORS);
  525. kfree_skb(skb);
  526. return -1;
  527. }
  528. daddr = *addr;
  529. *addr = ipv6_hdr(skb)->daddr;
  530. ipv6_hdr(skb)->daddr = daddr;
  531. skb_dst_drop(skb);
  532. ip6_route_input(skb);
  533. if (skb_dst(skb)->error) {
  534. skb_push(skb, skb->data - skb_network_header(skb));
  535. dst_input(skb);
  536. return -1;
  537. }
  538. if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
  539. if (ipv6_hdr(skb)->hop_limit <= 1) {
  540. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  541. IPSTATS_MIB_INHDRERRORS);
  542. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  543. 0);
  544. kfree_skb(skb);
  545. return -1;
  546. }
  547. ipv6_hdr(skb)->hop_limit--;
  548. goto looped_back;
  549. }
  550. skb_push(skb, skb->data - skb_network_header(skb));
  551. dst_input(skb);
  552. return -1;
  553. unknown_rh:
  554. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
  555. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  556. (&hdr->type) - skb_network_header(skb));
  557. return -1;
  558. }
  559. static const struct inet6_protocol rthdr_protocol = {
  560. .handler = ipv6_rthdr_rcv,
  561. .flags = INET6_PROTO_NOPOLICY,
  562. };
  563. static const struct inet6_protocol destopt_protocol = {
  564. .handler = ipv6_destopt_rcv,
  565. .flags = INET6_PROTO_NOPOLICY,
  566. };
  567. static const struct inet6_protocol nodata_protocol = {
  568. .handler = dst_discard,
  569. .flags = INET6_PROTO_NOPOLICY,
  570. };
  571. int __init ipv6_exthdrs_init(void)
  572. {
  573. int ret;
  574. ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  575. if (ret)
  576. goto out;
  577. ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  578. if (ret)
  579. goto out_rthdr;
  580. ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
  581. if (ret)
  582. goto out_destopt;
  583. out:
  584. return ret;
  585. out_destopt:
  586. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  587. out_rthdr:
  588. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  589. goto out;
  590. };
  591. void ipv6_exthdrs_exit(void)
  592. {
  593. inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
  594. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  595. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  596. }
  597. /**********************************
  598. Hop-by-hop options.
  599. **********************************/
  600. /*
  601. * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
  602. */
  603. static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
  604. {
  605. return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
  606. }
  607. static inline struct net *ipv6_skb_net(struct sk_buff *skb)
  608. {
  609. return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
  610. }
  611. /* Router Alert as of RFC 2711 */
  612. static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
  613. {
  614. const unsigned char *nh = skb_network_header(skb);
  615. if (nh[optoff + 1] == 2) {
  616. IP6CB(skb)->flags |= IP6SKB_ROUTERALERT;
  617. memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra));
  618. return true;
  619. }
  620. net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n",
  621. nh[optoff + 1]);
  622. kfree_skb(skb);
  623. return false;
  624. }
  625. /* Jumbo payload */
  626. static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
  627. {
  628. const unsigned char *nh = skb_network_header(skb);
  629. struct net *net = ipv6_skb_net(skb);
  630. u32 pkt_len;
  631. if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
  632. net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
  633. nh[optoff+1]);
  634. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  635. IPSTATS_MIB_INHDRERRORS);
  636. goto drop;
  637. }
  638. pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
  639. if (pkt_len <= IPV6_MAXPLEN) {
  640. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  641. IPSTATS_MIB_INHDRERRORS);
  642. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
  643. return false;
  644. }
  645. if (ipv6_hdr(skb)->payload_len) {
  646. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  647. IPSTATS_MIB_INHDRERRORS);
  648. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
  649. return false;
  650. }
  651. if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
  652. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  653. IPSTATS_MIB_INTRUNCATEDPKTS);
  654. goto drop;
  655. }
  656. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  657. goto drop;
  658. return true;
  659. drop:
  660. kfree_skb(skb);
  661. return false;
  662. }
  663. /* CALIPSO RFC 5570 */
  664. static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff)
  665. {
  666. const unsigned char *nh = skb_network_header(skb);
  667. if (nh[optoff + 1] < 8)
  668. goto drop;
  669. if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1])
  670. goto drop;
  671. if (!calipso_validate(skb, nh + optoff))
  672. goto drop;
  673. return true;
  674. drop:
  675. kfree_skb(skb);
  676. return false;
  677. }
  678. static const struct tlvtype_proc tlvprochopopt_lst[] = {
  679. {
  680. .type = IPV6_TLV_ROUTERALERT,
  681. .func = ipv6_hop_ra,
  682. },
  683. {
  684. .type = IPV6_TLV_JUMBO,
  685. .func = ipv6_hop_jumbo,
  686. },
  687. {
  688. .type = IPV6_TLV_CALIPSO,
  689. .func = ipv6_hop_calipso,
  690. },
  691. { -1, }
  692. };
  693. int ipv6_parse_hopopts(struct sk_buff *skb)
  694. {
  695. struct inet6_skb_parm *opt = IP6CB(skb);
  696. /*
  697. * skb_network_header(skb) is equal to skb->data, and
  698. * skb_network_header_len(skb) is always equal to
  699. * sizeof(struct ipv6hdr) by definition of
  700. * hop-by-hop options.
  701. */
  702. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
  703. !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
  704. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  705. kfree_skb(skb);
  706. return -1;
  707. }
  708. opt->flags |= IP6SKB_HOPBYHOP;
  709. if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
  710. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  711. opt = IP6CB(skb);
  712. opt->nhoff = sizeof(struct ipv6hdr);
  713. return 1;
  714. }
  715. return -1;
  716. }
  717. /*
  718. * Creating outbound headers.
  719. *
  720. * "build" functions work when skb is filled from head to tail (datagram)
  721. * "push" functions work when headers are added from tail to head (tcp)
  722. *
  723. * In both cases we assume, that caller reserved enough room
  724. * for headers.
  725. */
  726. static void ipv6_push_rthdr0(struct sk_buff *skb, u8 *proto,
  727. struct ipv6_rt_hdr *opt,
  728. struct in6_addr **addr_p, struct in6_addr *saddr)
  729. {
  730. struct rt0_hdr *phdr, *ihdr;
  731. int hops;
  732. ihdr = (struct rt0_hdr *) opt;
  733. phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
  734. memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
  735. hops = ihdr->rt_hdr.hdrlen >> 1;
  736. if (hops > 1)
  737. memcpy(phdr->addr, ihdr->addr + 1,
  738. (hops - 1) * sizeof(struct in6_addr));
  739. phdr->addr[hops - 1] = **addr_p;
  740. *addr_p = ihdr->addr;
  741. phdr->rt_hdr.nexthdr = *proto;
  742. *proto = NEXTHDR_ROUTING;
  743. }
  744. static void ipv6_push_rthdr4(struct sk_buff *skb, u8 *proto,
  745. struct ipv6_rt_hdr *opt,
  746. struct in6_addr **addr_p, struct in6_addr *saddr)
  747. {
  748. struct ipv6_sr_hdr *sr_phdr, *sr_ihdr;
  749. int plen, hops;
  750. sr_ihdr = (struct ipv6_sr_hdr *)opt;
  751. plen = (sr_ihdr->hdrlen + 1) << 3;
  752. sr_phdr = (struct ipv6_sr_hdr *)skb_push(skb, plen);
  753. memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr));
  754. hops = sr_ihdr->first_segment + 1;
  755. memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1,
  756. (hops - 1) * sizeof(struct in6_addr));
  757. sr_phdr->segments[0] = **addr_p;
  758. *addr_p = &sr_ihdr->segments[hops - 1];
  759. #ifdef CONFIG_IPV6_SEG6_HMAC
  760. if (sr_has_hmac(sr_phdr)) {
  761. struct net *net = NULL;
  762. if (skb->dev)
  763. net = dev_net(skb->dev);
  764. else if (skb->sk)
  765. net = sock_net(skb->sk);
  766. WARN_ON(!net);
  767. if (net)
  768. seg6_push_hmac(net, saddr, sr_phdr);
  769. }
  770. #endif
  771. sr_phdr->nexthdr = *proto;
  772. *proto = NEXTHDR_ROUTING;
  773. }
  774. static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
  775. struct ipv6_rt_hdr *opt,
  776. struct in6_addr **addr_p, struct in6_addr *saddr)
  777. {
  778. switch (opt->type) {
  779. case IPV6_SRCRT_TYPE_0:
  780. ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr);
  781. break;
  782. case IPV6_SRCRT_TYPE_4:
  783. ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr);
  784. break;
  785. default:
  786. break;
  787. }
  788. }
  789. static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
  790. {
  791. struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
  792. memcpy(h, opt, ipv6_optlen(opt));
  793. h->nexthdr = *proto;
  794. *proto = type;
  795. }
  796. void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  797. u8 *proto,
  798. struct in6_addr **daddr, struct in6_addr *saddr)
  799. {
  800. if (opt->srcrt) {
  801. ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr);
  802. /*
  803. * IPV6_RTHDRDSTOPTS is ignored
  804. * unless IPV6_RTHDR is set (RFC3542).
  805. */
  806. if (opt->dst0opt)
  807. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
  808. }
  809. if (opt->hopopt)
  810. ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
  811. }
  812. EXPORT_SYMBOL(ipv6_push_nfrag_opts);
  813. void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
  814. {
  815. if (opt->dst1opt)
  816. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
  817. }
  818. struct ipv6_txoptions *
  819. ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
  820. {
  821. struct ipv6_txoptions *opt2;
  822. opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
  823. if (opt2) {
  824. long dif = (char *)opt2 - (char *)opt;
  825. memcpy(opt2, opt, opt->tot_len);
  826. if (opt2->hopopt)
  827. *((char **)&opt2->hopopt) += dif;
  828. if (opt2->dst0opt)
  829. *((char **)&opt2->dst0opt) += dif;
  830. if (opt2->dst1opt)
  831. *((char **)&opt2->dst1opt) += dif;
  832. if (opt2->srcrt)
  833. *((char **)&opt2->srcrt) += dif;
  834. atomic_set(&opt2->refcnt, 1);
  835. }
  836. return opt2;
  837. }
  838. EXPORT_SYMBOL_GPL(ipv6_dup_options);
  839. static int ipv6_renew_option(void *ohdr,
  840. struct ipv6_opt_hdr __user *newopt, int newoptlen,
  841. int inherit,
  842. struct ipv6_opt_hdr **hdr,
  843. char **p)
  844. {
  845. if (inherit) {
  846. if (ohdr) {
  847. memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
  848. *hdr = (struct ipv6_opt_hdr *)*p;
  849. *p += CMSG_ALIGN(ipv6_optlen(*hdr));
  850. }
  851. } else {
  852. if (newopt) {
  853. if (copy_from_user(*p, newopt, newoptlen))
  854. return -EFAULT;
  855. *hdr = (struct ipv6_opt_hdr *)*p;
  856. if (ipv6_optlen(*hdr) > newoptlen)
  857. return -EINVAL;
  858. *p += CMSG_ALIGN(newoptlen);
  859. }
  860. }
  861. return 0;
  862. }
  863. /**
  864. * ipv6_renew_options - replace a specific ext hdr with a new one.
  865. *
  866. * @sk: sock from which to allocate memory
  867. * @opt: original options
  868. * @newtype: option type to replace in @opt
  869. * @newopt: new option of type @newtype to replace (user-mem)
  870. * @newoptlen: length of @newopt
  871. *
  872. * Returns a new set of options which is a copy of @opt with the
  873. * option type @newtype replaced with @newopt.
  874. *
  875. * @opt may be NULL, in which case a new set of options is returned
  876. * containing just @newopt.
  877. *
  878. * @newopt may be NULL, in which case the specified option type is
  879. * not copied into the new set of options.
  880. *
  881. * The new set of options is allocated from the socket option memory
  882. * buffer of @sk.
  883. */
  884. struct ipv6_txoptions *
  885. ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
  886. int newtype,
  887. struct ipv6_opt_hdr __user *newopt, int newoptlen)
  888. {
  889. int tot_len = 0;
  890. char *p;
  891. struct ipv6_txoptions *opt2;
  892. int err;
  893. if (opt) {
  894. if (newtype != IPV6_HOPOPTS && opt->hopopt)
  895. tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
  896. if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
  897. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
  898. if (newtype != IPV6_RTHDR && opt->srcrt)
  899. tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
  900. if (newtype != IPV6_DSTOPTS && opt->dst1opt)
  901. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
  902. }
  903. if (newopt && newoptlen)
  904. tot_len += CMSG_ALIGN(newoptlen);
  905. if (!tot_len)
  906. return NULL;
  907. tot_len += sizeof(*opt2);
  908. opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
  909. if (!opt2)
  910. return ERR_PTR(-ENOBUFS);
  911. memset(opt2, 0, tot_len);
  912. atomic_set(&opt2->refcnt, 1);
  913. opt2->tot_len = tot_len;
  914. p = (char *)(opt2 + 1);
  915. err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
  916. newtype != IPV6_HOPOPTS,
  917. &opt2->hopopt, &p);
  918. if (err)
  919. goto out;
  920. err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
  921. newtype != IPV6_RTHDRDSTOPTS,
  922. &opt2->dst0opt, &p);
  923. if (err)
  924. goto out;
  925. err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
  926. newtype != IPV6_RTHDR,
  927. (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
  928. if (err)
  929. goto out;
  930. err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
  931. newtype != IPV6_DSTOPTS,
  932. &opt2->dst1opt, &p);
  933. if (err)
  934. goto out;
  935. opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
  936. (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
  937. (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
  938. opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
  939. return opt2;
  940. out:
  941. sock_kfree_s(sk, opt2, opt2->tot_len);
  942. return ERR_PTR(err);
  943. }
  944. /**
  945. * ipv6_renew_options_kern - replace a specific ext hdr with a new one.
  946. *
  947. * @sk: sock from which to allocate memory
  948. * @opt: original options
  949. * @newtype: option type to replace in @opt
  950. * @newopt: new option of type @newtype to replace (kernel-mem)
  951. * @newoptlen: length of @newopt
  952. *
  953. * See ipv6_renew_options(). The difference is that @newopt is
  954. * kernel memory, rather than user memory.
  955. */
  956. struct ipv6_txoptions *
  957. ipv6_renew_options_kern(struct sock *sk, struct ipv6_txoptions *opt,
  958. int newtype, struct ipv6_opt_hdr *newopt,
  959. int newoptlen)
  960. {
  961. struct ipv6_txoptions *ret_val;
  962. const mm_segment_t old_fs = get_fs();
  963. set_fs(KERNEL_DS);
  964. ret_val = ipv6_renew_options(sk, opt, newtype,
  965. (struct ipv6_opt_hdr __user *)newopt,
  966. newoptlen);
  967. set_fs(old_fs);
  968. return ret_val;
  969. }
  970. struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  971. struct ipv6_txoptions *opt)
  972. {
  973. /*
  974. * ignore the dest before srcrt unless srcrt is being included.
  975. * --yoshfuji
  976. */
  977. if (opt && opt->dst0opt && !opt->srcrt) {
  978. if (opt_space != opt) {
  979. memcpy(opt_space, opt, sizeof(*opt_space));
  980. opt = opt_space;
  981. }
  982. opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
  983. opt->dst0opt = NULL;
  984. }
  985. return opt;
  986. }
  987. EXPORT_SYMBOL_GPL(ipv6_fixup_options);
  988. /**
  989. * fl6_update_dst - update flowi destination address with info given
  990. * by srcrt option, if any.
  991. *
  992. * @fl6: flowi6 for which daddr is to be updated
  993. * @opt: struct ipv6_txoptions in which to look for srcrt opt
  994. * @orig: copy of original daddr address if modified
  995. *
  996. * Returns NULL if no txoptions or no srcrt, otherwise returns orig
  997. * and initial value of fl6->daddr set in orig
  998. */
  999. struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
  1000. const struct ipv6_txoptions *opt,
  1001. struct in6_addr *orig)
  1002. {
  1003. if (!opt || !opt->srcrt)
  1004. return NULL;
  1005. *orig = fl6->daddr;
  1006. switch (opt->srcrt->type) {
  1007. case IPV6_SRCRT_TYPE_0:
  1008. fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
  1009. break;
  1010. case IPV6_SRCRT_TYPE_4:
  1011. {
  1012. struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt;
  1013. fl6->daddr = srh->segments[srh->first_segment];
  1014. break;
  1015. }
  1016. default:
  1017. return NULL;
  1018. }
  1019. return orig;
  1020. }
  1021. EXPORT_SYMBOL_GPL(fl6_update_dst);