exthdrs.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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. int accept_seg6;
  279. hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  280. idev = __in6_dev_get(skb->dev);
  281. accept_seg6 = net->ipv6.devconf_all->seg6_enabled;
  282. if (accept_seg6 > idev->cnf.seg6_enabled)
  283. accept_seg6 = idev->cnf.seg6_enabled;
  284. if (!accept_seg6) {
  285. kfree_skb(skb);
  286. return -1;
  287. }
  288. #ifdef CONFIG_IPV6_SEG6_HMAC
  289. if (!seg6_hmac_validate_skb(skb)) {
  290. kfree_skb(skb);
  291. return -1;
  292. }
  293. #endif
  294. looped_back:
  295. if (hdr->segments_left == 0) {
  296. if (hdr->nexthdr == NEXTHDR_IPV6) {
  297. int offset = (hdr->hdrlen + 1) << 3;
  298. skb_postpull_rcsum(skb, skb_network_header(skb),
  299. skb_network_header_len(skb));
  300. if (!pskb_pull(skb, offset)) {
  301. kfree_skb(skb);
  302. return -1;
  303. }
  304. skb_postpull_rcsum(skb, skb_transport_header(skb),
  305. offset);
  306. skb_reset_network_header(skb);
  307. skb_reset_transport_header(skb);
  308. skb->encapsulation = 0;
  309. __skb_tunnel_rx(skb, skb->dev, net);
  310. netif_rx(skb);
  311. return -1;
  312. }
  313. opt->srcrt = skb_network_header_len(skb);
  314. opt->lastopt = opt->srcrt;
  315. skb->transport_header += (hdr->hdrlen + 1) << 3;
  316. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  317. return 1;
  318. }
  319. if (hdr->segments_left >= (hdr->hdrlen >> 1)) {
  320. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  321. IPSTATS_MIB_INHDRERRORS);
  322. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  323. ((&hdr->segments_left) -
  324. skb_network_header(skb)));
  325. return -1;
  326. }
  327. if (skb_cloned(skb)) {
  328. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  329. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  330. IPSTATS_MIB_OUTDISCARDS);
  331. kfree_skb(skb);
  332. return -1;
  333. }
  334. }
  335. hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  336. hdr->segments_left--;
  337. addr = hdr->segments + hdr->segments_left;
  338. skb_push(skb, sizeof(struct ipv6hdr));
  339. if (skb->ip_summed == CHECKSUM_COMPLETE)
  340. seg6_update_csum(skb);
  341. ipv6_hdr(skb)->daddr = *addr;
  342. skb_dst_drop(skb);
  343. ip6_route_input(skb);
  344. if (skb_dst(skb)->error) {
  345. dst_input(skb);
  346. return -1;
  347. }
  348. if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
  349. if (ipv6_hdr(skb)->hop_limit <= 1) {
  350. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  351. IPSTATS_MIB_INHDRERRORS);
  352. icmpv6_send(skb, ICMPV6_TIME_EXCEED,
  353. ICMPV6_EXC_HOPLIMIT, 0);
  354. kfree_skb(skb);
  355. return -1;
  356. }
  357. ipv6_hdr(skb)->hop_limit--;
  358. skb_pull(skb, sizeof(struct ipv6hdr));
  359. goto looped_back;
  360. }
  361. dst_input(skb);
  362. return -1;
  363. }
  364. /********************************
  365. Routing header.
  366. ********************************/
  367. /* called with rcu_read_lock() */
  368. static int ipv6_rthdr_rcv(struct sk_buff *skb)
  369. {
  370. struct inet6_skb_parm *opt = IP6CB(skb);
  371. struct in6_addr *addr = NULL;
  372. struct in6_addr daddr;
  373. struct inet6_dev *idev;
  374. int n, i;
  375. struct ipv6_rt_hdr *hdr;
  376. struct rt0_hdr *rthdr;
  377. struct net *net = dev_net(skb->dev);
  378. int accept_source_route = net->ipv6.devconf_all->accept_source_route;
  379. idev = __in6_dev_get(skb->dev);
  380. if (idev && accept_source_route > idev->cnf.accept_source_route)
  381. accept_source_route = idev->cnf.accept_source_route;
  382. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  383. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  384. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  385. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  386. IPSTATS_MIB_INHDRERRORS);
  387. kfree_skb(skb);
  388. return -1;
  389. }
  390. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  391. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
  392. skb->pkt_type != PACKET_HOST) {
  393. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  394. IPSTATS_MIB_INADDRERRORS);
  395. kfree_skb(skb);
  396. return -1;
  397. }
  398. /* segment routing */
  399. if (hdr->type == IPV6_SRCRT_TYPE_4)
  400. return ipv6_srh_rcv(skb);
  401. looped_back:
  402. if (hdr->segments_left == 0) {
  403. switch (hdr->type) {
  404. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  405. case IPV6_SRCRT_TYPE_2:
  406. /* Silently discard type 2 header unless it was
  407. * processed by own
  408. */
  409. if (!addr) {
  410. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  411. IPSTATS_MIB_INADDRERRORS);
  412. kfree_skb(skb);
  413. return -1;
  414. }
  415. break;
  416. #endif
  417. default:
  418. break;
  419. }
  420. opt->lastopt = opt->srcrt = skb_network_header_len(skb);
  421. skb->transport_header += (hdr->hdrlen + 1) << 3;
  422. opt->dst0 = opt->dst1;
  423. opt->dst1 = 0;
  424. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  425. return 1;
  426. }
  427. switch (hdr->type) {
  428. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  429. case IPV6_SRCRT_TYPE_2:
  430. if (accept_source_route < 0)
  431. goto unknown_rh;
  432. /* Silently discard invalid RTH type 2 */
  433. if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
  434. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  435. IPSTATS_MIB_INHDRERRORS);
  436. kfree_skb(skb);
  437. return -1;
  438. }
  439. break;
  440. #endif
  441. default:
  442. goto unknown_rh;
  443. }
  444. /*
  445. * This is the routing header forwarding algorithm from
  446. * RFC 2460, page 16.
  447. */
  448. n = hdr->hdrlen >> 1;
  449. if (hdr->segments_left > n) {
  450. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  451. IPSTATS_MIB_INHDRERRORS);
  452. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  453. ((&hdr->segments_left) -
  454. skb_network_header(skb)));
  455. return -1;
  456. }
  457. /* We are about to mangle packet header. Be careful!
  458. Do not damage packets queued somewhere.
  459. */
  460. if (skb_cloned(skb)) {
  461. /* the copy is a forwarded packet */
  462. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  463. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  464. IPSTATS_MIB_OUTDISCARDS);
  465. kfree_skb(skb);
  466. return -1;
  467. }
  468. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  469. }
  470. if (skb->ip_summed == CHECKSUM_COMPLETE)
  471. skb->ip_summed = CHECKSUM_NONE;
  472. i = n - --hdr->segments_left;
  473. rthdr = (struct rt0_hdr *) hdr;
  474. addr = rthdr->addr;
  475. addr += i - 1;
  476. switch (hdr->type) {
  477. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  478. case IPV6_SRCRT_TYPE_2:
  479. if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
  480. (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  481. IPPROTO_ROUTING) < 0) {
  482. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  483. IPSTATS_MIB_INADDRERRORS);
  484. kfree_skb(skb);
  485. return -1;
  486. }
  487. if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
  488. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  489. IPSTATS_MIB_INADDRERRORS);
  490. kfree_skb(skb);
  491. return -1;
  492. }
  493. break;
  494. #endif
  495. default:
  496. break;
  497. }
  498. if (ipv6_addr_is_multicast(addr)) {
  499. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  500. IPSTATS_MIB_INADDRERRORS);
  501. kfree_skb(skb);
  502. return -1;
  503. }
  504. daddr = *addr;
  505. *addr = ipv6_hdr(skb)->daddr;
  506. ipv6_hdr(skb)->daddr = daddr;
  507. skb_dst_drop(skb);
  508. ip6_route_input(skb);
  509. if (skb_dst(skb)->error) {
  510. skb_push(skb, skb->data - skb_network_header(skb));
  511. dst_input(skb);
  512. return -1;
  513. }
  514. if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
  515. if (ipv6_hdr(skb)->hop_limit <= 1) {
  516. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  517. IPSTATS_MIB_INHDRERRORS);
  518. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  519. 0);
  520. kfree_skb(skb);
  521. return -1;
  522. }
  523. ipv6_hdr(skb)->hop_limit--;
  524. goto looped_back;
  525. }
  526. skb_push(skb, skb->data - skb_network_header(skb));
  527. dst_input(skb);
  528. return -1;
  529. unknown_rh:
  530. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
  531. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  532. (&hdr->type) - skb_network_header(skb));
  533. return -1;
  534. }
  535. static const struct inet6_protocol rthdr_protocol = {
  536. .handler = ipv6_rthdr_rcv,
  537. .flags = INET6_PROTO_NOPOLICY,
  538. };
  539. static const struct inet6_protocol destopt_protocol = {
  540. .handler = ipv6_destopt_rcv,
  541. .flags = INET6_PROTO_NOPOLICY,
  542. };
  543. static const struct inet6_protocol nodata_protocol = {
  544. .handler = dst_discard,
  545. .flags = INET6_PROTO_NOPOLICY,
  546. };
  547. int __init ipv6_exthdrs_init(void)
  548. {
  549. int ret;
  550. ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  551. if (ret)
  552. goto out;
  553. ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  554. if (ret)
  555. goto out_rthdr;
  556. ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
  557. if (ret)
  558. goto out_destopt;
  559. out:
  560. return ret;
  561. out_destopt:
  562. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  563. out_rthdr:
  564. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  565. goto out;
  566. };
  567. void ipv6_exthdrs_exit(void)
  568. {
  569. inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
  570. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  571. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  572. }
  573. /**********************************
  574. Hop-by-hop options.
  575. **********************************/
  576. /*
  577. * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
  578. */
  579. static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
  580. {
  581. return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
  582. }
  583. static inline struct net *ipv6_skb_net(struct sk_buff *skb)
  584. {
  585. return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
  586. }
  587. /* Router Alert as of RFC 2711 */
  588. static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
  589. {
  590. const unsigned char *nh = skb_network_header(skb);
  591. if (nh[optoff + 1] == 2) {
  592. IP6CB(skb)->flags |= IP6SKB_ROUTERALERT;
  593. memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra));
  594. return true;
  595. }
  596. net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n",
  597. nh[optoff + 1]);
  598. kfree_skb(skb);
  599. return false;
  600. }
  601. /* Jumbo payload */
  602. static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
  603. {
  604. const unsigned char *nh = skb_network_header(skb);
  605. struct net *net = ipv6_skb_net(skb);
  606. u32 pkt_len;
  607. if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
  608. net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
  609. nh[optoff+1]);
  610. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  611. IPSTATS_MIB_INHDRERRORS);
  612. goto drop;
  613. }
  614. pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
  615. if (pkt_len <= IPV6_MAXPLEN) {
  616. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  617. IPSTATS_MIB_INHDRERRORS);
  618. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
  619. return false;
  620. }
  621. if (ipv6_hdr(skb)->payload_len) {
  622. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  623. IPSTATS_MIB_INHDRERRORS);
  624. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
  625. return false;
  626. }
  627. if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
  628. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  629. IPSTATS_MIB_INTRUNCATEDPKTS);
  630. goto drop;
  631. }
  632. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  633. goto drop;
  634. IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM;
  635. return true;
  636. drop:
  637. kfree_skb(skb);
  638. return false;
  639. }
  640. /* CALIPSO RFC 5570 */
  641. static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff)
  642. {
  643. const unsigned char *nh = skb_network_header(skb);
  644. if (nh[optoff + 1] < 8)
  645. goto drop;
  646. if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1])
  647. goto drop;
  648. if (!calipso_validate(skb, nh + optoff))
  649. goto drop;
  650. return true;
  651. drop:
  652. kfree_skb(skb);
  653. return false;
  654. }
  655. static const struct tlvtype_proc tlvprochopopt_lst[] = {
  656. {
  657. .type = IPV6_TLV_ROUTERALERT,
  658. .func = ipv6_hop_ra,
  659. },
  660. {
  661. .type = IPV6_TLV_JUMBO,
  662. .func = ipv6_hop_jumbo,
  663. },
  664. {
  665. .type = IPV6_TLV_CALIPSO,
  666. .func = ipv6_hop_calipso,
  667. },
  668. { -1, }
  669. };
  670. int ipv6_parse_hopopts(struct sk_buff *skb)
  671. {
  672. struct inet6_skb_parm *opt = IP6CB(skb);
  673. /*
  674. * skb_network_header(skb) is equal to skb->data, and
  675. * skb_network_header_len(skb) is always equal to
  676. * sizeof(struct ipv6hdr) by definition of
  677. * hop-by-hop options.
  678. */
  679. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
  680. !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
  681. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  682. kfree_skb(skb);
  683. return -1;
  684. }
  685. opt->flags |= IP6SKB_HOPBYHOP;
  686. if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
  687. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  688. opt = IP6CB(skb);
  689. opt->nhoff = sizeof(struct ipv6hdr);
  690. return 1;
  691. }
  692. return -1;
  693. }
  694. /*
  695. * Creating outbound headers.
  696. *
  697. * "build" functions work when skb is filled from head to tail (datagram)
  698. * "push" functions work when headers are added from tail to head (tcp)
  699. *
  700. * In both cases we assume, that caller reserved enough room
  701. * for headers.
  702. */
  703. static void ipv6_push_rthdr0(struct sk_buff *skb, u8 *proto,
  704. struct ipv6_rt_hdr *opt,
  705. struct in6_addr **addr_p, struct in6_addr *saddr)
  706. {
  707. struct rt0_hdr *phdr, *ihdr;
  708. int hops;
  709. ihdr = (struct rt0_hdr *) opt;
  710. phdr = skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
  711. memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
  712. hops = ihdr->rt_hdr.hdrlen >> 1;
  713. if (hops > 1)
  714. memcpy(phdr->addr, ihdr->addr + 1,
  715. (hops - 1) * sizeof(struct in6_addr));
  716. phdr->addr[hops - 1] = **addr_p;
  717. *addr_p = ihdr->addr;
  718. phdr->rt_hdr.nexthdr = *proto;
  719. *proto = NEXTHDR_ROUTING;
  720. }
  721. static void ipv6_push_rthdr4(struct sk_buff *skb, u8 *proto,
  722. struct ipv6_rt_hdr *opt,
  723. struct in6_addr **addr_p, struct in6_addr *saddr)
  724. {
  725. struct ipv6_sr_hdr *sr_phdr, *sr_ihdr;
  726. int plen, hops;
  727. sr_ihdr = (struct ipv6_sr_hdr *)opt;
  728. plen = (sr_ihdr->hdrlen + 1) << 3;
  729. sr_phdr = skb_push(skb, plen);
  730. memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr));
  731. hops = sr_ihdr->first_segment + 1;
  732. memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1,
  733. (hops - 1) * sizeof(struct in6_addr));
  734. sr_phdr->segments[0] = **addr_p;
  735. *addr_p = &sr_ihdr->segments[sr_ihdr->segments_left];
  736. #ifdef CONFIG_IPV6_SEG6_HMAC
  737. if (sr_has_hmac(sr_phdr)) {
  738. struct net *net = NULL;
  739. if (skb->dev)
  740. net = dev_net(skb->dev);
  741. else if (skb->sk)
  742. net = sock_net(skb->sk);
  743. WARN_ON(!net);
  744. if (net)
  745. seg6_push_hmac(net, saddr, sr_phdr);
  746. }
  747. #endif
  748. sr_phdr->nexthdr = *proto;
  749. *proto = NEXTHDR_ROUTING;
  750. }
  751. static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
  752. struct ipv6_rt_hdr *opt,
  753. struct in6_addr **addr_p, struct in6_addr *saddr)
  754. {
  755. switch (opt->type) {
  756. case IPV6_SRCRT_TYPE_0:
  757. case IPV6_SRCRT_STRICT:
  758. case IPV6_SRCRT_TYPE_2:
  759. ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr);
  760. break;
  761. case IPV6_SRCRT_TYPE_4:
  762. ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr);
  763. break;
  764. default:
  765. break;
  766. }
  767. }
  768. static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
  769. {
  770. struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt));
  771. memcpy(h, opt, ipv6_optlen(opt));
  772. h->nexthdr = *proto;
  773. *proto = type;
  774. }
  775. void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  776. u8 *proto,
  777. struct in6_addr **daddr, struct in6_addr *saddr)
  778. {
  779. if (opt->srcrt) {
  780. ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr);
  781. /*
  782. * IPV6_RTHDRDSTOPTS is ignored
  783. * unless IPV6_RTHDR is set (RFC3542).
  784. */
  785. if (opt->dst0opt)
  786. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
  787. }
  788. if (opt->hopopt)
  789. ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
  790. }
  791. void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
  792. {
  793. if (opt->dst1opt)
  794. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
  795. }
  796. EXPORT_SYMBOL(ipv6_push_frag_opts);
  797. struct ipv6_txoptions *
  798. ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
  799. {
  800. struct ipv6_txoptions *opt2;
  801. opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
  802. if (opt2) {
  803. long dif = (char *)opt2 - (char *)opt;
  804. memcpy(opt2, opt, opt->tot_len);
  805. if (opt2->hopopt)
  806. *((char **)&opt2->hopopt) += dif;
  807. if (opt2->dst0opt)
  808. *((char **)&opt2->dst0opt) += dif;
  809. if (opt2->dst1opt)
  810. *((char **)&opt2->dst1opt) += dif;
  811. if (opt2->srcrt)
  812. *((char **)&opt2->srcrt) += dif;
  813. refcount_set(&opt2->refcnt, 1);
  814. }
  815. return opt2;
  816. }
  817. EXPORT_SYMBOL_GPL(ipv6_dup_options);
  818. static int ipv6_renew_option(void *ohdr,
  819. struct ipv6_opt_hdr __user *newopt, int newoptlen,
  820. int inherit,
  821. struct ipv6_opt_hdr **hdr,
  822. char **p)
  823. {
  824. if (inherit) {
  825. if (ohdr) {
  826. memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
  827. *hdr = (struct ipv6_opt_hdr *)*p;
  828. *p += CMSG_ALIGN(ipv6_optlen(*hdr));
  829. }
  830. } else {
  831. if (newopt) {
  832. if (copy_from_user(*p, newopt, newoptlen))
  833. return -EFAULT;
  834. *hdr = (struct ipv6_opt_hdr *)*p;
  835. if (ipv6_optlen(*hdr) > newoptlen)
  836. return -EINVAL;
  837. *p += CMSG_ALIGN(newoptlen);
  838. }
  839. }
  840. return 0;
  841. }
  842. /**
  843. * ipv6_renew_options - replace a specific ext hdr with a new one.
  844. *
  845. * @sk: sock from which to allocate memory
  846. * @opt: original options
  847. * @newtype: option type to replace in @opt
  848. * @newopt: new option of type @newtype to replace (user-mem)
  849. * @newoptlen: length of @newopt
  850. *
  851. * Returns a new set of options which is a copy of @opt with the
  852. * option type @newtype replaced with @newopt.
  853. *
  854. * @opt may be NULL, in which case a new set of options is returned
  855. * containing just @newopt.
  856. *
  857. * @newopt may be NULL, in which case the specified option type is
  858. * not copied into the new set of options.
  859. *
  860. * The new set of options is allocated from the socket option memory
  861. * buffer of @sk.
  862. */
  863. struct ipv6_txoptions *
  864. ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
  865. int newtype,
  866. struct ipv6_opt_hdr __user *newopt, int newoptlen)
  867. {
  868. int tot_len = 0;
  869. char *p;
  870. struct ipv6_txoptions *opt2;
  871. int err;
  872. if (opt) {
  873. if (newtype != IPV6_HOPOPTS && opt->hopopt)
  874. tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
  875. if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
  876. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
  877. if (newtype != IPV6_RTHDR && opt->srcrt)
  878. tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
  879. if (newtype != IPV6_DSTOPTS && opt->dst1opt)
  880. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
  881. }
  882. if (newopt && newoptlen)
  883. tot_len += CMSG_ALIGN(newoptlen);
  884. if (!tot_len)
  885. return NULL;
  886. tot_len += sizeof(*opt2);
  887. opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
  888. if (!opt2)
  889. return ERR_PTR(-ENOBUFS);
  890. memset(opt2, 0, tot_len);
  891. refcount_set(&opt2->refcnt, 1);
  892. opt2->tot_len = tot_len;
  893. p = (char *)(opt2 + 1);
  894. err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
  895. newtype != IPV6_HOPOPTS,
  896. &opt2->hopopt, &p);
  897. if (err)
  898. goto out;
  899. err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
  900. newtype != IPV6_RTHDRDSTOPTS,
  901. &opt2->dst0opt, &p);
  902. if (err)
  903. goto out;
  904. err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
  905. newtype != IPV6_RTHDR,
  906. (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
  907. if (err)
  908. goto out;
  909. err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
  910. newtype != IPV6_DSTOPTS,
  911. &opt2->dst1opt, &p);
  912. if (err)
  913. goto out;
  914. opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
  915. (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
  916. (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
  917. opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
  918. return opt2;
  919. out:
  920. sock_kfree_s(sk, opt2, opt2->tot_len);
  921. return ERR_PTR(err);
  922. }
  923. /**
  924. * ipv6_renew_options_kern - replace a specific ext hdr with a new one.
  925. *
  926. * @sk: sock from which to allocate memory
  927. * @opt: original options
  928. * @newtype: option type to replace in @opt
  929. * @newopt: new option of type @newtype to replace (kernel-mem)
  930. * @newoptlen: length of @newopt
  931. *
  932. * See ipv6_renew_options(). The difference is that @newopt is
  933. * kernel memory, rather than user memory.
  934. */
  935. struct ipv6_txoptions *
  936. ipv6_renew_options_kern(struct sock *sk, struct ipv6_txoptions *opt,
  937. int newtype, struct ipv6_opt_hdr *newopt,
  938. int newoptlen)
  939. {
  940. struct ipv6_txoptions *ret_val;
  941. const mm_segment_t old_fs = get_fs();
  942. set_fs(KERNEL_DS);
  943. ret_val = ipv6_renew_options(sk, opt, newtype,
  944. (struct ipv6_opt_hdr __user *)newopt,
  945. newoptlen);
  946. set_fs(old_fs);
  947. return ret_val;
  948. }
  949. struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  950. struct ipv6_txoptions *opt)
  951. {
  952. /*
  953. * ignore the dest before srcrt unless srcrt is being included.
  954. * --yoshfuji
  955. */
  956. if (opt && opt->dst0opt && !opt->srcrt) {
  957. if (opt_space != opt) {
  958. memcpy(opt_space, opt, sizeof(*opt_space));
  959. opt = opt_space;
  960. }
  961. opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
  962. opt->dst0opt = NULL;
  963. }
  964. return opt;
  965. }
  966. EXPORT_SYMBOL_GPL(ipv6_fixup_options);
  967. /**
  968. * fl6_update_dst - update flowi destination address with info given
  969. * by srcrt option, if any.
  970. *
  971. * @fl6: flowi6 for which daddr is to be updated
  972. * @opt: struct ipv6_txoptions in which to look for srcrt opt
  973. * @orig: copy of original daddr address if modified
  974. *
  975. * Returns NULL if no txoptions or no srcrt, otherwise returns orig
  976. * and initial value of fl6->daddr set in orig
  977. */
  978. struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
  979. const struct ipv6_txoptions *opt,
  980. struct in6_addr *orig)
  981. {
  982. if (!opt || !opt->srcrt)
  983. return NULL;
  984. *orig = fl6->daddr;
  985. switch (opt->srcrt->type) {
  986. case IPV6_SRCRT_TYPE_0:
  987. case IPV6_SRCRT_STRICT:
  988. case IPV6_SRCRT_TYPE_2:
  989. fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
  990. break;
  991. case IPV6_SRCRT_TYPE_4:
  992. {
  993. struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt;
  994. fl6->daddr = srh->segments[srh->segments_left];
  995. break;
  996. }
  997. default:
  998. return NULL;
  999. }
  1000. return orig;
  1001. }
  1002. EXPORT_SYMBOL_GPL(fl6_update_dst);