exthdrs.c 32 KB

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