exthdrs.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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 <[email protected]>
  8. * Andi Kleen <[email protected]>
  9. * Alexey Kuznetsov <[email protected]>
  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 <net/ioam6.h>
  50. #include <net/dst_metadata.h>
  51. #include <linux/uaccess.h>
  52. /*********************
  53. Generic functions
  54. *********************/
  55. /* An unknown option is detected, decide what to do */
  56. static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff,
  57. bool disallow_unknowns)
  58. {
  59. if (disallow_unknowns) {
  60. /* If unknown TLVs are disallowed by configuration
  61. * then always silently drop packet. Note this also
  62. * means no ICMP parameter problem is sent which
  63. * could be a good property to mitigate a reflection DOS
  64. * attack.
  65. */
  66. goto drop;
  67. }
  68. switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
  69. case 0: /* ignore */
  70. return true;
  71. case 1: /* drop packet */
  72. break;
  73. case 3: /* Send ICMP if not a multicast address and drop packet */
  74. /* Actually, it is redundant check. icmp_send
  75. will recheck in any case.
  76. */
  77. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
  78. break;
  79. fallthrough;
  80. case 2: /* send ICMP PARM PROB regardless and drop packet */
  81. icmpv6_param_prob_reason(skb, ICMPV6_UNK_OPTION, optoff,
  82. SKB_DROP_REASON_UNHANDLED_PROTO);
  83. return false;
  84. }
  85. drop:
  86. kfree_skb_reason(skb, SKB_DROP_REASON_UNHANDLED_PROTO);
  87. return false;
  88. }
  89. static bool ipv6_hop_ra(struct sk_buff *skb, int optoff);
  90. static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff);
  91. static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff);
  92. static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff);
  93. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  94. static bool ipv6_dest_hao(struct sk_buff *skb, int optoff);
  95. #endif
  96. /* Parse tlv encoded option header (hop-by-hop or destination) */
  97. static bool ip6_parse_tlv(bool hopbyhop,
  98. struct sk_buff *skb,
  99. int max_count)
  100. {
  101. int len = (skb_transport_header(skb)[1] + 1) << 3;
  102. const unsigned char *nh = skb_network_header(skb);
  103. int off = skb_network_header_len(skb);
  104. bool disallow_unknowns = false;
  105. int tlv_count = 0;
  106. int padlen = 0;
  107. if (unlikely(max_count < 0)) {
  108. disallow_unknowns = true;
  109. max_count = -max_count;
  110. }
  111. if (skb_transport_offset(skb) + len > skb_headlen(skb))
  112. goto bad;
  113. off += 2;
  114. len -= 2;
  115. while (len > 0) {
  116. int optlen, i;
  117. if (nh[off] == IPV6_TLV_PAD1) {
  118. padlen++;
  119. if (padlen > 7)
  120. goto bad;
  121. off++;
  122. len--;
  123. continue;
  124. }
  125. if (len < 2)
  126. goto bad;
  127. optlen = nh[off + 1] + 2;
  128. if (optlen > len)
  129. goto bad;
  130. if (nh[off] == IPV6_TLV_PADN) {
  131. /* RFC 2460 states that the purpose of PadN is
  132. * to align the containing header to multiples
  133. * of 8. 7 is therefore the highest valid value.
  134. * See also RFC 4942, Section 2.1.9.5.
  135. */
  136. padlen += optlen;
  137. if (padlen > 7)
  138. goto bad;
  139. /* RFC 4942 recommends receiving hosts to
  140. * actively check PadN payload to contain
  141. * only zeroes.
  142. */
  143. for (i = 2; i < optlen; i++) {
  144. if (nh[off + i] != 0)
  145. goto bad;
  146. }
  147. } else {
  148. tlv_count++;
  149. if (tlv_count > max_count)
  150. goto bad;
  151. if (hopbyhop) {
  152. switch (nh[off]) {
  153. case IPV6_TLV_ROUTERALERT:
  154. if (!ipv6_hop_ra(skb, off))
  155. return false;
  156. break;
  157. case IPV6_TLV_IOAM:
  158. if (!ipv6_hop_ioam(skb, off))
  159. return false;
  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 = net->ipv6.devconf_all->seg6_enabled;
  326. if (accept_seg6 > idev->cnf.seg6_enabled)
  327. accept_seg6 = idev->cnf.seg6_enabled;
  328. if (!accept_seg6) {
  329. kfree_skb(skb);
  330. return -1;
  331. }
  332. #ifdef CONFIG_IPV6_SEG6_HMAC
  333. if (!seg6_hmac_validate_skb(skb)) {
  334. kfree_skb(skb);
  335. return -1;
  336. }
  337. #endif
  338. looped_back:
  339. if (hdr->segments_left == 0) {
  340. if (hdr->nexthdr == NEXTHDR_IPV6 || hdr->nexthdr == NEXTHDR_IPV4) {
  341. int offset = (hdr->hdrlen + 1) << 3;
  342. skb_postpull_rcsum(skb, skb_network_header(skb),
  343. skb_network_header_len(skb));
  344. if (!pskb_pull(skb, offset)) {
  345. kfree_skb(skb);
  346. return -1;
  347. }
  348. skb_postpull_rcsum(skb, skb_transport_header(skb),
  349. offset);
  350. skb_reset_network_header(skb);
  351. skb_reset_transport_header(skb);
  352. skb->encapsulation = 0;
  353. if (hdr->nexthdr == NEXTHDR_IPV4)
  354. skb->protocol = htons(ETH_P_IP);
  355. __skb_tunnel_rx(skb, skb->dev, net);
  356. netif_rx(skb);
  357. return -1;
  358. }
  359. opt->srcrt = skb_network_header_len(skb);
  360. opt->lastopt = opt->srcrt;
  361. skb->transport_header += (hdr->hdrlen + 1) << 3;
  362. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  363. return 1;
  364. }
  365. if (hdr->segments_left >= (hdr->hdrlen >> 1)) {
  366. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  367. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  368. ((&hdr->segments_left) -
  369. skb_network_header(skb)));
  370. return -1;
  371. }
  372. if (skb_cloned(skb)) {
  373. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  374. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  375. IPSTATS_MIB_OUTDISCARDS);
  376. kfree_skb(skb);
  377. return -1;
  378. }
  379. }
  380. hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  381. hdr->segments_left--;
  382. addr = hdr->segments + hdr->segments_left;
  383. skb_push(skb, sizeof(struct ipv6hdr));
  384. if (skb->ip_summed == CHECKSUM_COMPLETE)
  385. seg6_update_csum(skb);
  386. ipv6_hdr(skb)->daddr = *addr;
  387. skb_dst_drop(skb);
  388. ip6_route_input(skb);
  389. if (skb_dst(skb)->error) {
  390. dst_input(skb);
  391. return -1;
  392. }
  393. if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
  394. if (ipv6_hdr(skb)->hop_limit <= 1) {
  395. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  396. icmpv6_send(skb, ICMPV6_TIME_EXCEED,
  397. ICMPV6_EXC_HOPLIMIT, 0);
  398. kfree_skb(skb);
  399. return -1;
  400. }
  401. ipv6_hdr(skb)->hop_limit--;
  402. skb_pull(skb, sizeof(struct ipv6hdr));
  403. goto looped_back;
  404. }
  405. dst_input(skb);
  406. return -1;
  407. }
  408. static int ipv6_rpl_srh_rcv(struct sk_buff *skb)
  409. {
  410. struct ipv6_rpl_sr_hdr *hdr, *ohdr, *chdr;
  411. struct inet6_skb_parm *opt = IP6CB(skb);
  412. struct net *net = dev_net(skb->dev);
  413. struct inet6_dev *idev;
  414. struct ipv6hdr *oldhdr;
  415. unsigned char *buf;
  416. int accept_rpl_seg;
  417. int i, err;
  418. u64 n = 0;
  419. u32 r;
  420. idev = __in6_dev_get(skb->dev);
  421. accept_rpl_seg = net->ipv6.devconf_all->rpl_seg_enabled;
  422. if (accept_rpl_seg > idev->cnf.rpl_seg_enabled)
  423. accept_rpl_seg = idev->cnf.rpl_seg_enabled;
  424. if (!accept_rpl_seg) {
  425. kfree_skb(skb);
  426. return -1;
  427. }
  428. looped_back:
  429. hdr = (struct ipv6_rpl_sr_hdr *)skb_transport_header(skb);
  430. if (hdr->segments_left == 0) {
  431. if (hdr->nexthdr == NEXTHDR_IPV6) {
  432. int offset = (hdr->hdrlen + 1) << 3;
  433. skb_postpull_rcsum(skb, skb_network_header(skb),
  434. skb_network_header_len(skb));
  435. if (!pskb_pull(skb, offset)) {
  436. kfree_skb(skb);
  437. return -1;
  438. }
  439. skb_postpull_rcsum(skb, skb_transport_header(skb),
  440. offset);
  441. skb_reset_network_header(skb);
  442. skb_reset_transport_header(skb);
  443. skb->encapsulation = 0;
  444. __skb_tunnel_rx(skb, skb->dev, net);
  445. netif_rx(skb);
  446. return -1;
  447. }
  448. opt->srcrt = skb_network_header_len(skb);
  449. opt->lastopt = opt->srcrt;
  450. skb->transport_header += (hdr->hdrlen + 1) << 3;
  451. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  452. return 1;
  453. }
  454. if (!pskb_may_pull(skb, sizeof(*hdr))) {
  455. kfree_skb(skb);
  456. return -1;
  457. }
  458. n = (hdr->hdrlen << 3) - hdr->pad - (16 - hdr->cmpre);
  459. r = do_div(n, (16 - hdr->cmpri));
  460. /* checks if calculation was without remainder and n fits into
  461. * unsigned char which is segments_left field. Should not be
  462. * higher than that.
  463. */
  464. if (r || (n + 1) > 255) {
  465. kfree_skb(skb);
  466. return -1;
  467. }
  468. if (hdr->segments_left > n + 1) {
  469. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  470. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  471. ((&hdr->segments_left) -
  472. skb_network_header(skb)));
  473. return -1;
  474. }
  475. if (!pskb_may_pull(skb, ipv6_rpl_srh_size(n, hdr->cmpri,
  476. hdr->cmpre))) {
  477. kfree_skb(skb);
  478. return -1;
  479. }
  480. hdr->segments_left--;
  481. i = n - hdr->segments_left;
  482. buf = kcalloc(struct_size(hdr, segments.addr, n + 2), 2, GFP_ATOMIC);
  483. if (unlikely(!buf)) {
  484. kfree_skb(skb);
  485. return -1;
  486. }
  487. ohdr = (struct ipv6_rpl_sr_hdr *)buf;
  488. ipv6_rpl_srh_decompress(ohdr, hdr, &ipv6_hdr(skb)->daddr, n);
  489. chdr = (struct ipv6_rpl_sr_hdr *)(buf + ((ohdr->hdrlen + 1) << 3));
  490. if ((ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST) ||
  491. (ipv6_addr_type(&ohdr->rpl_segaddr[i]) & IPV6_ADDR_MULTICAST)) {
  492. kfree_skb(skb);
  493. kfree(buf);
  494. return -1;
  495. }
  496. err = ipv6_chk_rpl_srh_loop(net, ohdr->rpl_segaddr, n + 1);
  497. if (err) {
  498. icmpv6_send(skb, ICMPV6_PARAMPROB, 0, 0);
  499. kfree_skb(skb);
  500. kfree(buf);
  501. return -1;
  502. }
  503. swap(ipv6_hdr(skb)->daddr, ohdr->rpl_segaddr[i]);
  504. ipv6_rpl_srh_compress(chdr, ohdr, &ipv6_hdr(skb)->daddr, n);
  505. oldhdr = ipv6_hdr(skb);
  506. skb_pull(skb, ((hdr->hdrlen + 1) << 3));
  507. skb_postpull_rcsum(skb, oldhdr,
  508. sizeof(struct ipv6hdr) + ((hdr->hdrlen + 1) << 3));
  509. if (unlikely(!hdr->segments_left)) {
  510. if (pskb_expand_head(skb, sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3), 0,
  511. GFP_ATOMIC)) {
  512. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_OUTDISCARDS);
  513. kfree_skb(skb);
  514. kfree(buf);
  515. return -1;
  516. }
  517. oldhdr = ipv6_hdr(skb);
  518. }
  519. skb_push(skb, ((chdr->hdrlen + 1) << 3) + sizeof(struct ipv6hdr));
  520. skb_reset_network_header(skb);
  521. skb_mac_header_rebuild(skb);
  522. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  523. memmove(ipv6_hdr(skb), oldhdr, sizeof(struct ipv6hdr));
  524. memcpy(skb_transport_header(skb), chdr, (chdr->hdrlen + 1) << 3);
  525. ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  526. skb_postpush_rcsum(skb, ipv6_hdr(skb),
  527. sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3));
  528. kfree(buf);
  529. skb_dst_drop(skb);
  530. ip6_route_input(skb);
  531. if (skb_dst(skb)->error) {
  532. dst_input(skb);
  533. return -1;
  534. }
  535. if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) {
  536. if (ipv6_hdr(skb)->hop_limit <= 1) {
  537. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  538. icmpv6_send(skb, ICMPV6_TIME_EXCEED,
  539. ICMPV6_EXC_HOPLIMIT, 0);
  540. kfree_skb(skb);
  541. return -1;
  542. }
  543. ipv6_hdr(skb)->hop_limit--;
  544. skb_pull(skb, sizeof(struct ipv6hdr));
  545. goto looped_back;
  546. }
  547. dst_input(skb);
  548. return -1;
  549. }
  550. /********************************
  551. Routing header.
  552. ********************************/
  553. /* called with rcu_read_lock() */
  554. static int ipv6_rthdr_rcv(struct sk_buff *skb)
  555. {
  556. struct inet6_dev *idev = __in6_dev_get(skb->dev);
  557. struct inet6_skb_parm *opt = IP6CB(skb);
  558. struct in6_addr *addr = NULL;
  559. struct in6_addr daddr;
  560. int n, i;
  561. struct ipv6_rt_hdr *hdr;
  562. struct rt0_hdr *rthdr;
  563. struct net *net = dev_net(skb->dev);
  564. int accept_source_route = net->ipv6.devconf_all->accept_source_route;
  565. if (idev && accept_source_route > idev->cnf.accept_source_route)
  566. accept_source_route = idev->cnf.accept_source_route;
  567. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  568. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  569. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  570. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  571. kfree_skb(skb);
  572. return -1;
  573. }
  574. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  575. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
  576. skb->pkt_type != PACKET_HOST) {
  577. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
  578. kfree_skb(skb);
  579. return -1;
  580. }
  581. switch (hdr->type) {
  582. case IPV6_SRCRT_TYPE_4:
  583. /* segment routing */
  584. return ipv6_srh_rcv(skb);
  585. case IPV6_SRCRT_TYPE_3:
  586. /* rpl segment routing */
  587. return ipv6_rpl_srh_rcv(skb);
  588. default:
  589. break;
  590. }
  591. looped_back:
  592. if (hdr->segments_left == 0) {
  593. switch (hdr->type) {
  594. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  595. case IPV6_SRCRT_TYPE_2:
  596. /* Silently discard type 2 header unless it was
  597. * processed by own
  598. */
  599. if (!addr) {
  600. __IP6_INC_STATS(net, idev,
  601. IPSTATS_MIB_INADDRERRORS);
  602. kfree_skb(skb);
  603. return -1;
  604. }
  605. break;
  606. #endif
  607. default:
  608. break;
  609. }
  610. opt->lastopt = opt->srcrt = skb_network_header_len(skb);
  611. skb->transport_header += (hdr->hdrlen + 1) << 3;
  612. opt->dst0 = opt->dst1;
  613. opt->dst1 = 0;
  614. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  615. return 1;
  616. }
  617. switch (hdr->type) {
  618. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  619. case IPV6_SRCRT_TYPE_2:
  620. if (accept_source_route < 0)
  621. goto unknown_rh;
  622. /* Silently discard invalid RTH type 2 */
  623. if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
  624. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  625. kfree_skb(skb);
  626. return -1;
  627. }
  628. break;
  629. #endif
  630. default:
  631. goto unknown_rh;
  632. }
  633. /*
  634. * This is the routing header forwarding algorithm from
  635. * RFC 2460, page 16.
  636. */
  637. n = hdr->hdrlen >> 1;
  638. if (hdr->segments_left > n) {
  639. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  640. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  641. ((&hdr->segments_left) -
  642. skb_network_header(skb)));
  643. return -1;
  644. }
  645. /* We are about to mangle packet header. Be careful!
  646. Do not damage packets queued somewhere.
  647. */
  648. if (skb_cloned(skb)) {
  649. /* the copy is a forwarded packet */
  650. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  651. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  652. IPSTATS_MIB_OUTDISCARDS);
  653. kfree_skb(skb);
  654. return -1;
  655. }
  656. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  657. }
  658. if (skb->ip_summed == CHECKSUM_COMPLETE)
  659. skb->ip_summed = CHECKSUM_NONE;
  660. i = n - --hdr->segments_left;
  661. rthdr = (struct rt0_hdr *) hdr;
  662. addr = rthdr->addr;
  663. addr += i - 1;
  664. switch (hdr->type) {
  665. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  666. case IPV6_SRCRT_TYPE_2:
  667. if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
  668. (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  669. IPPROTO_ROUTING) < 0) {
  670. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
  671. kfree_skb(skb);
  672. return -1;
  673. }
  674. if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
  675. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
  676. kfree_skb(skb);
  677. return -1;
  678. }
  679. break;
  680. #endif
  681. default:
  682. break;
  683. }
  684. if (ipv6_addr_is_multicast(addr)) {
  685. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
  686. kfree_skb(skb);
  687. return -1;
  688. }
  689. daddr = *addr;
  690. *addr = ipv6_hdr(skb)->daddr;
  691. ipv6_hdr(skb)->daddr = daddr;
  692. skb_dst_drop(skb);
  693. ip6_route_input(skb);
  694. if (skb_dst(skb)->error) {
  695. skb_push(skb, skb->data - skb_network_header(skb));
  696. dst_input(skb);
  697. return -1;
  698. }
  699. if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
  700. if (ipv6_hdr(skb)->hop_limit <= 1) {
  701. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  702. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  703. 0);
  704. kfree_skb(skb);
  705. return -1;
  706. }
  707. ipv6_hdr(skb)->hop_limit--;
  708. goto looped_back;
  709. }
  710. skb_push(skb, skb->data - skb_network_header(skb));
  711. dst_input(skb);
  712. return -1;
  713. unknown_rh:
  714. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  715. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  716. (&hdr->type) - skb_network_header(skb));
  717. return -1;
  718. }
  719. static const struct inet6_protocol rthdr_protocol = {
  720. .handler = ipv6_rthdr_rcv,
  721. .flags = INET6_PROTO_NOPOLICY,
  722. };
  723. static const struct inet6_protocol destopt_protocol = {
  724. .handler = ipv6_destopt_rcv,
  725. .flags = INET6_PROTO_NOPOLICY,
  726. };
  727. static const struct inet6_protocol nodata_protocol = {
  728. .handler = dst_discard,
  729. .flags = INET6_PROTO_NOPOLICY,
  730. };
  731. int __init ipv6_exthdrs_init(void)
  732. {
  733. int ret;
  734. ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  735. if (ret)
  736. goto out;
  737. ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  738. if (ret)
  739. goto out_rthdr;
  740. ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
  741. if (ret)
  742. goto out_destopt;
  743. out:
  744. return ret;
  745. out_destopt:
  746. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  747. out_rthdr:
  748. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  749. goto out;
  750. };
  751. void ipv6_exthdrs_exit(void)
  752. {
  753. inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
  754. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  755. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  756. }
  757. /**********************************
  758. Hop-by-hop options.
  759. **********************************/
  760. /*
  761. * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
  762. */
  763. static inline struct net *ipv6_skb_net(struct sk_buff *skb)
  764. {
  765. return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
  766. }
  767. /* Router Alert as of RFC 2711 */
  768. static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
  769. {
  770. const unsigned char *nh = skb_network_header(skb);
  771. if (nh[optoff + 1] == 2) {
  772. IP6CB(skb)->flags |= IP6SKB_ROUTERALERT;
  773. memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra));
  774. return true;
  775. }
  776. net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n",
  777. nh[optoff + 1]);
  778. kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
  779. return false;
  780. }
  781. /* IOAM */
  782. static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff)
  783. {
  784. struct ioam6_trace_hdr *trace;
  785. struct ioam6_namespace *ns;
  786. struct ioam6_hdr *hdr;
  787. /* Bad alignment (must be 4n-aligned) */
  788. if (optoff & 3)
  789. goto drop;
  790. /* Ignore if IOAM is not enabled on ingress */
  791. if (!__in6_dev_get(skb->dev)->cnf.ioam6_enabled)
  792. goto ignore;
  793. /* Truncated Option header */
  794. hdr = (struct ioam6_hdr *)(skb_network_header(skb) + optoff);
  795. if (hdr->opt_len < 2)
  796. goto drop;
  797. switch (hdr->type) {
  798. case IOAM6_TYPE_PREALLOC:
  799. /* Truncated Pre-allocated Trace header */
  800. if (hdr->opt_len < 2 + sizeof(*trace))
  801. goto drop;
  802. /* Malformed Pre-allocated Trace header */
  803. trace = (struct ioam6_trace_hdr *)((u8 *)hdr + sizeof(*hdr));
  804. if (hdr->opt_len < 2 + sizeof(*trace) + trace->remlen * 4)
  805. goto drop;
  806. /* Ignore if the IOAM namespace is unknown */
  807. ns = ioam6_namespace(ipv6_skb_net(skb), trace->namespace_id);
  808. if (!ns)
  809. goto ignore;
  810. if (!skb_valid_dst(skb))
  811. ip6_route_input(skb);
  812. ioam6_fill_trace_data(skb, ns, trace, true);
  813. break;
  814. default:
  815. break;
  816. }
  817. ignore:
  818. return true;
  819. drop:
  820. kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
  821. return false;
  822. }
  823. /* Jumbo payload */
  824. static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
  825. {
  826. const unsigned char *nh = skb_network_header(skb);
  827. SKB_DR(reason);
  828. u32 pkt_len;
  829. if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
  830. net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
  831. nh[optoff+1]);
  832. SKB_DR_SET(reason, IP_INHDR);
  833. goto drop;
  834. }
  835. pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
  836. if (pkt_len <= IPV6_MAXPLEN) {
  837. icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff + 2,
  838. SKB_DROP_REASON_IP_INHDR);
  839. return false;
  840. }
  841. if (ipv6_hdr(skb)->payload_len) {
  842. icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff,
  843. SKB_DROP_REASON_IP_INHDR);
  844. return false;
  845. }
  846. if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
  847. SKB_DR_SET(reason, PKT_TOO_SMALL);
  848. goto drop;
  849. }
  850. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  851. goto drop;
  852. IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM;
  853. return true;
  854. drop:
  855. kfree_skb_reason(skb, reason);
  856. return false;
  857. }
  858. /* CALIPSO RFC 5570 */
  859. static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff)
  860. {
  861. const unsigned char *nh = skb_network_header(skb);
  862. if (nh[optoff + 1] < 8)
  863. goto drop;
  864. if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1])
  865. goto drop;
  866. if (!calipso_validate(skb, nh + optoff))
  867. goto drop;
  868. return true;
  869. drop:
  870. kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
  871. return false;
  872. }
  873. int ipv6_parse_hopopts(struct sk_buff *skb)
  874. {
  875. struct inet6_skb_parm *opt = IP6CB(skb);
  876. struct net *net = dev_net(skb->dev);
  877. int extlen;
  878. /*
  879. * skb_network_header(skb) is equal to skb->data, and
  880. * skb_network_header_len(skb) is always equal to
  881. * sizeof(struct ipv6hdr) by definition of
  882. * hop-by-hop options.
  883. */
  884. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
  885. !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
  886. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  887. fail_and_free:
  888. kfree_skb(skb);
  889. return -1;
  890. }
  891. extlen = (skb_transport_header(skb)[1] + 1) << 3;
  892. if (extlen > net->ipv6.sysctl.max_hbh_opts_len)
  893. goto fail_and_free;
  894. opt->flags |= IP6SKB_HOPBYHOP;
  895. if (ip6_parse_tlv(true, skb, net->ipv6.sysctl.max_hbh_opts_cnt)) {
  896. skb->transport_header += extlen;
  897. opt = IP6CB(skb);
  898. opt->nhoff = sizeof(struct ipv6hdr);
  899. return 1;
  900. }
  901. return -1;
  902. }
  903. /*
  904. * Creating outbound headers.
  905. *
  906. * "build" functions work when skb is filled from head to tail (datagram)
  907. * "push" functions work when headers are added from tail to head (tcp)
  908. *
  909. * In both cases we assume, that caller reserved enough room
  910. * for headers.
  911. */
  912. static void ipv6_push_rthdr0(struct sk_buff *skb, u8 *proto,
  913. struct ipv6_rt_hdr *opt,
  914. struct in6_addr **addr_p, struct in6_addr *saddr)
  915. {
  916. struct rt0_hdr *phdr, *ihdr;
  917. int hops;
  918. ihdr = (struct rt0_hdr *) opt;
  919. phdr = skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
  920. memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
  921. hops = ihdr->rt_hdr.hdrlen >> 1;
  922. if (hops > 1)
  923. memcpy(phdr->addr, ihdr->addr + 1,
  924. (hops - 1) * sizeof(struct in6_addr));
  925. phdr->addr[hops - 1] = **addr_p;
  926. *addr_p = ihdr->addr;
  927. phdr->rt_hdr.nexthdr = *proto;
  928. *proto = NEXTHDR_ROUTING;
  929. }
  930. static void ipv6_push_rthdr4(struct sk_buff *skb, u8 *proto,
  931. struct ipv6_rt_hdr *opt,
  932. struct in6_addr **addr_p, struct in6_addr *saddr)
  933. {
  934. struct ipv6_sr_hdr *sr_phdr, *sr_ihdr;
  935. int plen, hops;
  936. sr_ihdr = (struct ipv6_sr_hdr *)opt;
  937. plen = (sr_ihdr->hdrlen + 1) << 3;
  938. sr_phdr = skb_push(skb, plen);
  939. memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr));
  940. hops = sr_ihdr->first_segment + 1;
  941. memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1,
  942. (hops - 1) * sizeof(struct in6_addr));
  943. sr_phdr->segments[0] = **addr_p;
  944. *addr_p = &sr_ihdr->segments[sr_ihdr->segments_left];
  945. if (sr_ihdr->hdrlen > hops * 2) {
  946. int tlvs_offset, tlvs_length;
  947. tlvs_offset = (1 + hops * 2) << 3;
  948. tlvs_length = (sr_ihdr->hdrlen - hops * 2) << 3;
  949. memcpy((char *)sr_phdr + tlvs_offset,
  950. (char *)sr_ihdr + tlvs_offset, tlvs_length);
  951. }
  952. #ifdef CONFIG_IPV6_SEG6_HMAC
  953. if (sr_has_hmac(sr_phdr)) {
  954. struct net *net = NULL;
  955. if (skb->dev)
  956. net = dev_net(skb->dev);
  957. else if (skb->sk)
  958. net = sock_net(skb->sk);
  959. WARN_ON(!net);
  960. if (net)
  961. seg6_push_hmac(net, saddr, sr_phdr);
  962. }
  963. #endif
  964. sr_phdr->nexthdr = *proto;
  965. *proto = NEXTHDR_ROUTING;
  966. }
  967. static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
  968. struct ipv6_rt_hdr *opt,
  969. struct in6_addr **addr_p, struct in6_addr *saddr)
  970. {
  971. switch (opt->type) {
  972. case IPV6_SRCRT_TYPE_0:
  973. case IPV6_SRCRT_STRICT:
  974. case IPV6_SRCRT_TYPE_2:
  975. ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr);
  976. break;
  977. case IPV6_SRCRT_TYPE_4:
  978. ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr);
  979. break;
  980. default:
  981. break;
  982. }
  983. }
  984. static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
  985. {
  986. struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt));
  987. memcpy(h, opt, ipv6_optlen(opt));
  988. h->nexthdr = *proto;
  989. *proto = type;
  990. }
  991. void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  992. u8 *proto,
  993. struct in6_addr **daddr, struct in6_addr *saddr)
  994. {
  995. if (opt->srcrt) {
  996. ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr);
  997. /*
  998. * IPV6_RTHDRDSTOPTS is ignored
  999. * unless IPV6_RTHDR is set (RFC3542).
  1000. */
  1001. if (opt->dst0opt)
  1002. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
  1003. }
  1004. if (opt->hopopt)
  1005. ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
  1006. }
  1007. void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
  1008. {
  1009. if (opt->dst1opt)
  1010. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
  1011. }
  1012. EXPORT_SYMBOL(ipv6_push_frag_opts);
  1013. struct ipv6_txoptions *
  1014. ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
  1015. {
  1016. struct ipv6_txoptions *opt2;
  1017. opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
  1018. if (opt2) {
  1019. long dif = (char *)opt2 - (char *)opt;
  1020. memcpy(opt2, opt, opt->tot_len);
  1021. if (opt2->hopopt)
  1022. *((char **)&opt2->hopopt) += dif;
  1023. if (opt2->dst0opt)
  1024. *((char **)&opt2->dst0opt) += dif;
  1025. if (opt2->dst1opt)
  1026. *((char **)&opt2->dst1opt) += dif;
  1027. if (opt2->srcrt)
  1028. *((char **)&opt2->srcrt) += dif;
  1029. refcount_set(&opt2->refcnt, 1);
  1030. }
  1031. return opt2;
  1032. }
  1033. EXPORT_SYMBOL_GPL(ipv6_dup_options);
  1034. static void ipv6_renew_option(int renewtype,
  1035. struct ipv6_opt_hdr **dest,
  1036. struct ipv6_opt_hdr *old,
  1037. struct ipv6_opt_hdr *new,
  1038. int newtype, char **p)
  1039. {
  1040. struct ipv6_opt_hdr *src;
  1041. src = (renewtype == newtype ? new : old);
  1042. if (!src)
  1043. return;
  1044. memcpy(*p, src, ipv6_optlen(src));
  1045. *dest = (struct ipv6_opt_hdr *)*p;
  1046. *p += CMSG_ALIGN(ipv6_optlen(*dest));
  1047. }
  1048. /**
  1049. * ipv6_renew_options - replace a specific ext hdr with a new one.
  1050. *
  1051. * @sk: sock from which to allocate memory
  1052. * @opt: original options
  1053. * @newtype: option type to replace in @opt
  1054. * @newopt: new option of type @newtype to replace (user-mem)
  1055. *
  1056. * Returns a new set of options which is a copy of @opt with the
  1057. * option type @newtype replaced with @newopt.
  1058. *
  1059. * @opt may be NULL, in which case a new set of options is returned
  1060. * containing just @newopt.
  1061. *
  1062. * @newopt may be NULL, in which case the specified option type is
  1063. * not copied into the new set of options.
  1064. *
  1065. * The new set of options is allocated from the socket option memory
  1066. * buffer of @sk.
  1067. */
  1068. struct ipv6_txoptions *
  1069. ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
  1070. int newtype, struct ipv6_opt_hdr *newopt)
  1071. {
  1072. int tot_len = 0;
  1073. char *p;
  1074. struct ipv6_txoptions *opt2;
  1075. if (opt) {
  1076. if (newtype != IPV6_HOPOPTS && opt->hopopt)
  1077. tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
  1078. if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
  1079. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
  1080. if (newtype != IPV6_RTHDR && opt->srcrt)
  1081. tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
  1082. if (newtype != IPV6_DSTOPTS && opt->dst1opt)
  1083. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
  1084. }
  1085. if (newopt)
  1086. tot_len += CMSG_ALIGN(ipv6_optlen(newopt));
  1087. if (!tot_len)
  1088. return NULL;
  1089. tot_len += sizeof(*opt2);
  1090. opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
  1091. if (!opt2)
  1092. return ERR_PTR(-ENOBUFS);
  1093. memset(opt2, 0, tot_len);
  1094. refcount_set(&opt2->refcnt, 1);
  1095. opt2->tot_len = tot_len;
  1096. p = (char *)(opt2 + 1);
  1097. ipv6_renew_option(IPV6_HOPOPTS, &opt2->hopopt,
  1098. (opt ? opt->hopopt : NULL),
  1099. newopt, newtype, &p);
  1100. ipv6_renew_option(IPV6_RTHDRDSTOPTS, &opt2->dst0opt,
  1101. (opt ? opt->dst0opt : NULL),
  1102. newopt, newtype, &p);
  1103. ipv6_renew_option(IPV6_RTHDR,
  1104. (struct ipv6_opt_hdr **)&opt2->srcrt,
  1105. (opt ? (struct ipv6_opt_hdr *)opt->srcrt : NULL),
  1106. newopt, newtype, &p);
  1107. ipv6_renew_option(IPV6_DSTOPTS, &opt2->dst1opt,
  1108. (opt ? opt->dst1opt : NULL),
  1109. newopt, newtype, &p);
  1110. opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
  1111. (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
  1112. (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
  1113. opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
  1114. return opt2;
  1115. }
  1116. struct ipv6_txoptions *__ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  1117. struct ipv6_txoptions *opt)
  1118. {
  1119. /*
  1120. * ignore the dest before srcrt unless srcrt is being included.
  1121. * --yoshfuji
  1122. */
  1123. if (opt->dst0opt && !opt->srcrt) {
  1124. if (opt_space != opt) {
  1125. memcpy(opt_space, opt, sizeof(*opt_space));
  1126. opt = opt_space;
  1127. }
  1128. opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
  1129. opt->dst0opt = NULL;
  1130. }
  1131. return opt;
  1132. }
  1133. EXPORT_SYMBOL_GPL(__ipv6_fixup_options);
  1134. /**
  1135. * fl6_update_dst - update flowi destination address with info given
  1136. * by srcrt option, if any.
  1137. *
  1138. * @fl6: flowi6 for which daddr is to be updated
  1139. * @opt: struct ipv6_txoptions in which to look for srcrt opt
  1140. * @orig: copy of original daddr address if modified
  1141. *
  1142. * Returns NULL if no txoptions or no srcrt, otherwise returns orig
  1143. * and initial value of fl6->daddr set in orig
  1144. */
  1145. struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
  1146. const struct ipv6_txoptions *opt,
  1147. struct in6_addr *orig)
  1148. {
  1149. if (!opt || !opt->srcrt)
  1150. return NULL;
  1151. *orig = fl6->daddr;
  1152. switch (opt->srcrt->type) {
  1153. case IPV6_SRCRT_TYPE_0:
  1154. case IPV6_SRCRT_STRICT:
  1155. case IPV6_SRCRT_TYPE_2:
  1156. fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
  1157. break;
  1158. case IPV6_SRCRT_TYPE_4:
  1159. {
  1160. struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt;
  1161. fl6->daddr = srh->segments[srh->segments_left];
  1162. break;
  1163. }
  1164. default:
  1165. return NULL;
  1166. }
  1167. return orig;
  1168. }
  1169. EXPORT_SYMBOL_GPL(fl6_update_dst);