exthdrs.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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(dst)), 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 > READ_ONCE(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,
  283. READ_ONCE(net->ipv6.sysctl.max_dst_opts_cnt))) {
  284. skb->transport_header += extlen;
  285. opt = IP6CB(skb);
  286. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  287. opt->nhoff = dstbuf;
  288. #else
  289. opt->nhoff = opt->dst1;
  290. #endif
  291. return 1;
  292. }
  293. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  294. return -1;
  295. }
  296. static void seg6_update_csum(struct sk_buff *skb)
  297. {
  298. struct ipv6_sr_hdr *hdr;
  299. struct in6_addr *addr;
  300. __be32 from, to;
  301. /* srh is at transport offset and seg_left is already decremented
  302. * but daddr is not yet updated with next segment
  303. */
  304. hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  305. addr = hdr->segments + hdr->segments_left;
  306. hdr->segments_left++;
  307. from = *(__be32 *)hdr;
  308. hdr->segments_left--;
  309. to = *(__be32 *)hdr;
  310. /* update skb csum with diff resulting from seg_left decrement */
  311. update_csum_diff4(skb, from, to);
  312. /* compute csum diff between current and next segment and update */
  313. update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr),
  314. (__be32 *)addr);
  315. }
  316. static int ipv6_srh_rcv(struct sk_buff *skb)
  317. {
  318. struct inet6_skb_parm *opt = IP6CB(skb);
  319. struct net *net = dev_net(skb->dev);
  320. struct ipv6_sr_hdr *hdr;
  321. struct inet6_dev *idev;
  322. struct in6_addr *addr;
  323. int accept_seg6;
  324. hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  325. idev = __in6_dev_get(skb->dev);
  326. if (!idev) {
  327. kfree_skb(skb);
  328. return -1;
  329. }
  330. accept_seg6 = min(READ_ONCE(net->ipv6.devconf_all->seg6_enabled),
  331. READ_ONCE(idev->cnf.seg6_enabled));
  332. if (!accept_seg6) {
  333. kfree_skb(skb);
  334. return -1;
  335. }
  336. #ifdef CONFIG_IPV6_SEG6_HMAC
  337. if (!seg6_hmac_validate_skb(skb)) {
  338. kfree_skb(skb);
  339. return -1;
  340. }
  341. #endif
  342. looped_back:
  343. if (hdr->segments_left == 0) {
  344. if (hdr->nexthdr == NEXTHDR_IPV6 || hdr->nexthdr == NEXTHDR_IPV4) {
  345. int offset = (hdr->hdrlen + 1) << 3;
  346. skb_postpull_rcsum(skb, skb_network_header(skb),
  347. skb_network_header_len(skb));
  348. skb_pull(skb, offset);
  349. skb_postpull_rcsum(skb, skb_transport_header(skb),
  350. offset);
  351. skb_reset_network_header(skb);
  352. skb_reset_transport_header(skb);
  353. skb->encapsulation = 0;
  354. if (hdr->nexthdr == NEXTHDR_IPV4)
  355. skb->protocol = htons(ETH_P_IP);
  356. __skb_tunnel_rx(skb, skb->dev, net);
  357. netif_rx(skb);
  358. return -1;
  359. }
  360. opt->srcrt = skb_network_header_len(skb);
  361. opt->lastopt = opt->srcrt;
  362. skb->transport_header += (hdr->hdrlen + 1) << 3;
  363. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  364. return 1;
  365. }
  366. if (hdr->segments_left >= (hdr->hdrlen >> 1)) {
  367. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  368. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  369. ((&hdr->segments_left) -
  370. skb_network_header(skb)));
  371. return -1;
  372. }
  373. if (skb_cloned(skb)) {
  374. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  375. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  376. IPSTATS_MIB_OUTDISCARDS);
  377. kfree_skb(skb);
  378. return -1;
  379. }
  380. hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  381. }
  382. hdr->segments_left--;
  383. addr = hdr->segments + hdr->segments_left;
  384. skb_push(skb, sizeof(struct ipv6hdr));
  385. if (skb->ip_summed == CHECKSUM_COMPLETE)
  386. seg6_update_csum(skb);
  387. ipv6_hdr(skb)->daddr = *addr;
  388. ip6_route_input(skb);
  389. if (skb_dst(skb)->error) {
  390. dst_input(skb);
  391. return -1;
  392. }
  393. if (skb_dst_dev(skb)->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 = min(READ_ONCE(net->ipv6.devconf_all->rpl_seg_enabled),
  422. READ_ONCE(idev->cnf.rpl_seg_enabled));
  423. if (!accept_rpl_seg) {
  424. kfree_skb(skb);
  425. return -1;
  426. }
  427. looped_back:
  428. hdr = (struct ipv6_rpl_sr_hdr *)skb_transport_header(skb);
  429. if (hdr->segments_left == 0) {
  430. if (hdr->nexthdr == NEXTHDR_IPV6) {
  431. int offset = (hdr->hdrlen + 1) << 3;
  432. skb_postpull_rcsum(skb, skb_network_header(skb),
  433. skb_network_header_len(skb));
  434. skb_pull(skb, offset);
  435. skb_postpull_rcsum(skb, skb_transport_header(skb),
  436. offset);
  437. skb_reset_network_header(skb);
  438. skb_reset_transport_header(skb);
  439. skb->encapsulation = 0;
  440. __skb_tunnel_rx(skb, skb->dev, net);
  441. netif_rx(skb);
  442. return -1;
  443. }
  444. opt->srcrt = skb_network_header_len(skb);
  445. opt->lastopt = opt->srcrt;
  446. skb->transport_header += (hdr->hdrlen + 1) << 3;
  447. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  448. return 1;
  449. }
  450. n = (hdr->hdrlen << 3) - hdr->pad - (16 - hdr->cmpre);
  451. r = do_div(n, (16 - hdr->cmpri));
  452. /* checks if calculation was without remainder and n fits into
  453. * unsigned char which is segments_left field. Should not be
  454. * higher than that.
  455. */
  456. if (r || (n + 1) > 255) {
  457. kfree_skb(skb);
  458. return -1;
  459. }
  460. if (hdr->segments_left > n + 1) {
  461. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  462. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  463. ((&hdr->segments_left) -
  464. skb_network_header(skb)));
  465. return -1;
  466. }
  467. hdr->segments_left--;
  468. i = n - hdr->segments_left;
  469. buf = kcalloc(struct_size(hdr, segments.addr, n + 2), 2, GFP_ATOMIC);
  470. if (unlikely(!buf)) {
  471. kfree_skb(skb);
  472. return -1;
  473. }
  474. ohdr = (struct ipv6_rpl_sr_hdr *)buf;
  475. ipv6_rpl_srh_decompress(ohdr, hdr, &ipv6_hdr(skb)->daddr, n);
  476. chdr = (struct ipv6_rpl_sr_hdr *)(buf + ((ohdr->hdrlen + 1) << 3));
  477. if (ipv6_addr_is_multicast(&ohdr->rpl_segaddr[i])) {
  478. kfree_skb(skb);
  479. kfree(buf);
  480. return -1;
  481. }
  482. err = ipv6_chk_rpl_srh_loop(net, ohdr->rpl_segaddr, n + 1);
  483. if (err) {
  484. icmpv6_send(skb, ICMPV6_PARAMPROB, 0, 0);
  485. kfree_skb(skb);
  486. kfree(buf);
  487. return -1;
  488. }
  489. swap(ipv6_hdr(skb)->daddr, ohdr->rpl_segaddr[i]);
  490. ipv6_rpl_srh_compress(chdr, ohdr, &ipv6_hdr(skb)->daddr, n);
  491. oldhdr = ipv6_hdr(skb);
  492. skb_pull(skb, ((hdr->hdrlen + 1) << 3));
  493. skb_postpull_rcsum(skb, oldhdr,
  494. sizeof(struct ipv6hdr) + ((hdr->hdrlen + 1) << 3));
  495. if (unlikely(!hdr->segments_left)) {
  496. if (pskb_expand_head(skb, sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3), 0,
  497. GFP_ATOMIC)) {
  498. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_OUTDISCARDS);
  499. kfree_skb(skb);
  500. kfree(buf);
  501. return -1;
  502. }
  503. oldhdr = ipv6_hdr(skb);
  504. }
  505. skb_push(skb, ((chdr->hdrlen + 1) << 3) + sizeof(struct ipv6hdr));
  506. skb_reset_network_header(skb);
  507. skb_mac_header_rebuild(skb);
  508. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  509. memmove(ipv6_hdr(skb), oldhdr, sizeof(struct ipv6hdr));
  510. memcpy(skb_transport_header(skb), chdr, (chdr->hdrlen + 1) << 3);
  511. ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  512. skb_postpush_rcsum(skb, ipv6_hdr(skb),
  513. sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3));
  514. kfree(buf);
  515. ip6_route_input(skb);
  516. if (skb_dst(skb)->error) {
  517. dst_input(skb);
  518. return -1;
  519. }
  520. if (skb_dst_dev(skb)->flags & IFF_LOOPBACK) {
  521. if (ipv6_hdr(skb)->hop_limit <= 1) {
  522. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  523. icmpv6_send(skb, ICMPV6_TIME_EXCEED,
  524. ICMPV6_EXC_HOPLIMIT, 0);
  525. kfree_skb(skb);
  526. return -1;
  527. }
  528. ipv6_hdr(skb)->hop_limit--;
  529. skb_pull(skb, sizeof(struct ipv6hdr));
  530. goto looped_back;
  531. }
  532. dst_input(skb);
  533. return -1;
  534. }
  535. /********************************
  536. Routing header.
  537. ********************************/
  538. /* called with rcu_read_lock() */
  539. static int ipv6_rthdr_rcv(struct sk_buff *skb)
  540. {
  541. struct inet6_dev *idev = __in6_dev_get(skb->dev);
  542. struct inet6_skb_parm *opt = IP6CB(skb);
  543. struct in6_addr *addr = NULL;
  544. int n, i;
  545. struct ipv6_rt_hdr *hdr;
  546. struct rt0_hdr *rthdr;
  547. struct net *net = dev_net(skb->dev);
  548. int accept_source_route;
  549. accept_source_route = READ_ONCE(net->ipv6.devconf_all->accept_source_route);
  550. if (idev)
  551. accept_source_route = min(accept_source_route,
  552. READ_ONCE(idev->cnf.accept_source_route));
  553. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  554. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  555. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  556. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  557. kfree_skb(skb);
  558. return -1;
  559. }
  560. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  561. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
  562. skb->pkt_type != PACKET_HOST) {
  563. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
  564. kfree_skb(skb);
  565. return -1;
  566. }
  567. switch (hdr->type) {
  568. case IPV6_SRCRT_TYPE_4:
  569. /* segment routing */
  570. return ipv6_srh_rcv(skb);
  571. case IPV6_SRCRT_TYPE_3:
  572. /* rpl segment routing */
  573. return ipv6_rpl_srh_rcv(skb);
  574. default:
  575. break;
  576. }
  577. looped_back:
  578. if (hdr->segments_left == 0) {
  579. switch (hdr->type) {
  580. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  581. case IPV6_SRCRT_TYPE_2:
  582. /* Silently discard type 2 header unless it was
  583. * processed by own
  584. */
  585. if (!addr) {
  586. __IP6_INC_STATS(net, idev,
  587. IPSTATS_MIB_INADDRERRORS);
  588. kfree_skb(skb);
  589. return -1;
  590. }
  591. break;
  592. #endif
  593. default:
  594. break;
  595. }
  596. opt->lastopt = opt->srcrt = skb_network_header_len(skb);
  597. skb->transport_header += (hdr->hdrlen + 1) << 3;
  598. opt->dst0 = opt->dst1;
  599. opt->dst1 = 0;
  600. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  601. return 1;
  602. }
  603. switch (hdr->type) {
  604. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  605. case IPV6_SRCRT_TYPE_2:
  606. if (accept_source_route < 0)
  607. goto unknown_rh;
  608. /* Silently discard invalid RTH type 2 */
  609. if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
  610. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  611. kfree_skb(skb);
  612. return -1;
  613. }
  614. break;
  615. #endif
  616. default:
  617. goto unknown_rh;
  618. }
  619. /*
  620. * This is the routing header forwarding algorithm from
  621. * RFC 2460, page 16.
  622. */
  623. n = hdr->hdrlen >> 1;
  624. if (hdr->segments_left > n) {
  625. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  626. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  627. ((&hdr->segments_left) -
  628. skb_network_header(skb)));
  629. return -1;
  630. }
  631. /* We are about to mangle packet header. Be careful!
  632. Do not damage packets queued somewhere.
  633. */
  634. if (skb_cloned(skb)) {
  635. /* the copy is a forwarded packet */
  636. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  637. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  638. IPSTATS_MIB_OUTDISCARDS);
  639. kfree_skb(skb);
  640. return -1;
  641. }
  642. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  643. }
  644. if (skb->ip_summed == CHECKSUM_COMPLETE)
  645. skb->ip_summed = CHECKSUM_NONE;
  646. i = n - --hdr->segments_left;
  647. rthdr = (struct rt0_hdr *) hdr;
  648. addr = rthdr->addr;
  649. addr += i - 1;
  650. switch (hdr->type) {
  651. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  652. case IPV6_SRCRT_TYPE_2:
  653. if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
  654. (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  655. IPPROTO_ROUTING) < 0) {
  656. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
  657. kfree_skb(skb);
  658. return -1;
  659. }
  660. if (!ipv6_chk_home_addr(skb_dst_dev_net(skb), addr)) {
  661. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
  662. kfree_skb(skb);
  663. return -1;
  664. }
  665. break;
  666. #endif
  667. default:
  668. break;
  669. }
  670. if (ipv6_addr_is_multicast(addr)) {
  671. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
  672. kfree_skb(skb);
  673. return -1;
  674. }
  675. swap(*addr, ipv6_hdr(skb)->daddr);
  676. ip6_route_input(skb);
  677. if (skb_dst(skb)->error) {
  678. skb_push(skb, -skb_network_offset(skb));
  679. dst_input(skb);
  680. return -1;
  681. }
  682. if (skb_dst_dev(skb)->flags & IFF_LOOPBACK) {
  683. if (ipv6_hdr(skb)->hop_limit <= 1) {
  684. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  685. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  686. 0);
  687. kfree_skb(skb);
  688. return -1;
  689. }
  690. ipv6_hdr(skb)->hop_limit--;
  691. goto looped_back;
  692. }
  693. skb_push(skb, -skb_network_offset(skb));
  694. dst_input(skb);
  695. return -1;
  696. unknown_rh:
  697. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  698. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  699. (&hdr->type) - skb_network_header(skb));
  700. return -1;
  701. }
  702. static const struct inet6_protocol rthdr_protocol = {
  703. .handler = ipv6_rthdr_rcv,
  704. .flags = INET6_PROTO_NOPOLICY,
  705. };
  706. static const struct inet6_protocol destopt_protocol = {
  707. .handler = ipv6_destopt_rcv,
  708. .flags = INET6_PROTO_NOPOLICY,
  709. };
  710. static const struct inet6_protocol nodata_protocol = {
  711. .handler = dst_discard,
  712. .flags = INET6_PROTO_NOPOLICY,
  713. };
  714. int __init ipv6_exthdrs_init(void)
  715. {
  716. int ret;
  717. ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  718. if (ret)
  719. goto out;
  720. ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  721. if (ret)
  722. goto out_rthdr;
  723. ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
  724. if (ret)
  725. goto out_destopt;
  726. out:
  727. return ret;
  728. out_destopt:
  729. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  730. out_rthdr:
  731. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  732. goto out;
  733. };
  734. void ipv6_exthdrs_exit(void)
  735. {
  736. inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
  737. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  738. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  739. }
  740. /**********************************
  741. Hop-by-hop options.
  742. **********************************/
  743. /* Router Alert as of RFC 2711 */
  744. static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
  745. {
  746. const unsigned char *nh = skb_network_header(skb);
  747. if (nh[optoff + 1] == 2) {
  748. IP6CB(skb)->flags |= IP6SKB_ROUTERALERT;
  749. memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra));
  750. return true;
  751. }
  752. net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n",
  753. nh[optoff + 1]);
  754. kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
  755. return false;
  756. }
  757. /* IOAM */
  758. static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff)
  759. {
  760. struct ioam6_trace_hdr *trace;
  761. struct ioam6_namespace *ns;
  762. struct ioam6_hdr *hdr;
  763. /* Bad alignment (must be 4n-aligned) */
  764. if (optoff & 3)
  765. goto drop;
  766. /* Ignore if IOAM is not enabled on ingress */
  767. if (!READ_ONCE(__in6_dev_get(skb->dev)->cnf.ioam6_enabled))
  768. goto ignore;
  769. /* Truncated Option header */
  770. hdr = (struct ioam6_hdr *)(skb_network_header(skb) + optoff);
  771. if (hdr->opt_len < 2)
  772. goto drop;
  773. switch (hdr->type) {
  774. case IOAM6_TYPE_PREALLOC:
  775. /* Truncated Pre-allocated Trace header */
  776. if (hdr->opt_len < 2 + sizeof(*trace))
  777. goto drop;
  778. /* Malformed Pre-allocated Trace header */
  779. trace = (struct ioam6_trace_hdr *)((u8 *)hdr + sizeof(*hdr));
  780. if (hdr->opt_len < 2 + sizeof(*trace) + trace->remlen * 4)
  781. goto drop;
  782. /* Inconsistent Pre-allocated Trace header */
  783. if (trace->nodelen !=
  784. ioam6_trace_compute_nodelen(be32_to_cpu(trace->type_be32)))
  785. goto drop;
  786. /* Ignore if the IOAM namespace is unknown */
  787. ns = ioam6_namespace(dev_net(skb->dev), trace->namespace_id);
  788. if (!ns)
  789. goto ignore;
  790. if (!skb_valid_dst(skb))
  791. ip6_route_input(skb);
  792. /* About to mangle packet header */
  793. if (skb_ensure_writable(skb, optoff + 2 + hdr->opt_len))
  794. goto drop;
  795. /* Trace pointer may have changed */
  796. trace = (struct ioam6_trace_hdr *)(skb_network_header(skb)
  797. + optoff + sizeof(*hdr));
  798. ioam6_fill_trace_data(skb, ns, trace, true);
  799. ioam6_event(IOAM6_EVENT_TRACE, dev_net(skb->dev),
  800. GFP_ATOMIC, (void *)trace, hdr->opt_len - 2);
  801. break;
  802. default:
  803. break;
  804. }
  805. ignore:
  806. return true;
  807. drop:
  808. kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
  809. return false;
  810. }
  811. /* Jumbo payload */
  812. static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
  813. {
  814. const unsigned char *nh = skb_network_header(skb);
  815. SKB_DR(reason);
  816. u32 pkt_len;
  817. if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
  818. net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
  819. nh[optoff+1]);
  820. SKB_DR_SET(reason, IP_INHDR);
  821. goto drop;
  822. }
  823. pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
  824. if (pkt_len <= IPV6_MAXPLEN) {
  825. icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff + 2,
  826. SKB_DROP_REASON_IP_INHDR);
  827. return false;
  828. }
  829. if (ipv6_hdr(skb)->payload_len) {
  830. icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff,
  831. SKB_DROP_REASON_IP_INHDR);
  832. return false;
  833. }
  834. if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
  835. SKB_DR_SET(reason, PKT_TOO_SMALL);
  836. goto drop;
  837. }
  838. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  839. goto drop;
  840. IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM;
  841. return true;
  842. drop:
  843. kfree_skb_reason(skb, reason);
  844. return false;
  845. }
  846. /* CALIPSO RFC 5570 */
  847. static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff)
  848. {
  849. const unsigned char *nh = skb_network_header(skb);
  850. if (nh[optoff + 1] < 8)
  851. goto drop;
  852. if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1])
  853. goto drop;
  854. if (!calipso_validate(skb, nh + optoff))
  855. goto drop;
  856. return true;
  857. drop:
  858. kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
  859. return false;
  860. }
  861. int ipv6_parse_hopopts(struct sk_buff *skb)
  862. {
  863. struct inet6_skb_parm *opt = IP6CB(skb);
  864. struct net *net = dev_net(skb->dev);
  865. int extlen;
  866. /*
  867. * skb_network_header(skb) is equal to skb->data, and
  868. * skb_network_header_len(skb) is always equal to
  869. * sizeof(struct ipv6hdr) by definition of
  870. * hop-by-hop options.
  871. */
  872. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
  873. !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
  874. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  875. fail_and_free:
  876. kfree_skb(skb);
  877. return -1;
  878. }
  879. extlen = (skb_transport_header(skb)[1] + 1) << 3;
  880. if (extlen > READ_ONCE(net->ipv6.sysctl.max_hbh_opts_len))
  881. goto fail_and_free;
  882. opt->flags |= IP6SKB_HOPBYHOP;
  883. if (ip6_parse_tlv(true, skb,
  884. READ_ONCE(net->ipv6.sysctl.max_hbh_opts_cnt))) {
  885. skb->transport_header += extlen;
  886. opt = IP6CB(skb);
  887. opt->nhoff = sizeof(struct ipv6hdr);
  888. return 1;
  889. }
  890. return -1;
  891. }
  892. /*
  893. * Creating outbound headers.
  894. *
  895. * "build" functions work when skb is filled from head to tail (datagram)
  896. * "push" functions work when headers are added from tail to head (tcp)
  897. *
  898. * In both cases we assume, that caller reserved enough room
  899. * for headers.
  900. */
  901. static u8 ipv6_push_rthdr0(struct sk_buff *skb, u8 proto,
  902. struct ipv6_rt_hdr *opt,
  903. struct in6_addr **addr_p, struct in6_addr *saddr)
  904. {
  905. struct rt0_hdr *phdr, *ihdr;
  906. int hops;
  907. ihdr = (struct rt0_hdr *) opt;
  908. phdr = skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
  909. memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
  910. hops = ihdr->rt_hdr.hdrlen >> 1;
  911. if (hops > 1)
  912. memcpy(phdr->addr, ihdr->addr + 1,
  913. (hops - 1) * sizeof(struct in6_addr));
  914. phdr->addr[hops - 1] = **addr_p;
  915. *addr_p = ihdr->addr;
  916. phdr->rt_hdr.nexthdr = proto;
  917. return NEXTHDR_ROUTING;
  918. }
  919. static u8 ipv6_push_rthdr4(struct sk_buff *skb, u8 proto,
  920. struct ipv6_rt_hdr *opt,
  921. struct in6_addr **addr_p, struct in6_addr *saddr)
  922. {
  923. struct ipv6_sr_hdr *sr_phdr, *sr_ihdr;
  924. int plen, hops;
  925. sr_ihdr = (struct ipv6_sr_hdr *)opt;
  926. plen = (sr_ihdr->hdrlen + 1) << 3;
  927. sr_phdr = skb_push(skb, plen);
  928. memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr));
  929. hops = sr_ihdr->first_segment + 1;
  930. memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1,
  931. (hops - 1) * sizeof(struct in6_addr));
  932. sr_phdr->segments[0] = **addr_p;
  933. *addr_p = &sr_ihdr->segments[sr_ihdr->segments_left];
  934. if (sr_ihdr->hdrlen > hops * 2) {
  935. int tlvs_offset, tlvs_length;
  936. tlvs_offset = (1 + hops * 2) << 3;
  937. tlvs_length = (sr_ihdr->hdrlen - hops * 2) << 3;
  938. memcpy((char *)sr_phdr + tlvs_offset,
  939. (char *)sr_ihdr + tlvs_offset, tlvs_length);
  940. }
  941. #ifdef CONFIG_IPV6_SEG6_HMAC
  942. if (sr_has_hmac(sr_phdr)) {
  943. struct net *net = NULL;
  944. if (skb->dev)
  945. net = dev_net(skb->dev);
  946. else if (skb->sk)
  947. net = sock_net(skb->sk);
  948. WARN_ON(!net);
  949. if (net)
  950. seg6_push_hmac(net, saddr, sr_phdr);
  951. }
  952. #endif
  953. sr_phdr->nexthdr = proto;
  954. return NEXTHDR_ROUTING;
  955. }
  956. static u8 ipv6_push_rthdr(struct sk_buff *skb, u8 proto,
  957. struct ipv6_rt_hdr *opt,
  958. struct in6_addr **addr_p, struct in6_addr *saddr)
  959. {
  960. switch (opt->type) {
  961. case IPV6_SRCRT_TYPE_0:
  962. case IPV6_SRCRT_STRICT:
  963. case IPV6_SRCRT_TYPE_2:
  964. proto = ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr);
  965. break;
  966. case IPV6_SRCRT_TYPE_4:
  967. proto = ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr);
  968. break;
  969. default:
  970. break;
  971. }
  972. return proto;
  973. }
  974. static u8 ipv6_push_exthdr(struct sk_buff *skb, u8 proto, u8 type, struct ipv6_opt_hdr *opt)
  975. {
  976. struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt));
  977. memcpy(h, opt, ipv6_optlen(opt));
  978. h->nexthdr = proto;
  979. return type;
  980. }
  981. u8 ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  982. u8 proto,
  983. struct in6_addr **daddr, struct in6_addr *saddr)
  984. {
  985. if (opt->srcrt) {
  986. proto = ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr);
  987. /*
  988. * IPV6_RTHDRDSTOPTS is ignored
  989. * unless IPV6_RTHDR is set (RFC3542).
  990. */
  991. if (opt->dst0opt)
  992. proto = ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
  993. }
  994. if (opt->hopopt)
  995. proto = ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
  996. return proto;
  997. }
  998. u8 ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 proto)
  999. {
  1000. if (opt->dst1opt)
  1001. proto = ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
  1002. return proto;
  1003. }
  1004. EXPORT_SYMBOL(ipv6_push_frag_opts);
  1005. struct ipv6_txoptions *
  1006. ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
  1007. {
  1008. struct ipv6_txoptions *opt2;
  1009. opt2 = sock_kmemdup(sk, opt, opt->tot_len, GFP_ATOMIC);
  1010. if (opt2) {
  1011. long dif = (char *)opt2 - (char *)opt;
  1012. if (opt2->hopopt)
  1013. *((char **)&opt2->hopopt) += dif;
  1014. if (opt2->dst0opt)
  1015. *((char **)&opt2->dst0opt) += dif;
  1016. if (opt2->dst1opt)
  1017. *((char **)&opt2->dst1opt) += dif;
  1018. if (opt2->srcrt)
  1019. *((char **)&opt2->srcrt) += dif;
  1020. refcount_set(&opt2->refcnt, 1);
  1021. }
  1022. return opt2;
  1023. }
  1024. EXPORT_SYMBOL_GPL(ipv6_dup_options);
  1025. static void ipv6_renew_option(int renewtype,
  1026. struct ipv6_opt_hdr **dest,
  1027. struct ipv6_opt_hdr *old,
  1028. struct ipv6_opt_hdr *new,
  1029. int newtype, char **p)
  1030. {
  1031. struct ipv6_opt_hdr *src;
  1032. src = (renewtype == newtype ? new : old);
  1033. if (!src)
  1034. return;
  1035. memcpy(*p, src, ipv6_optlen(src));
  1036. *dest = (struct ipv6_opt_hdr *)*p;
  1037. *p += CMSG_ALIGN(ipv6_optlen(*dest));
  1038. }
  1039. /**
  1040. * ipv6_renew_options - replace a specific ext hdr with a new one.
  1041. *
  1042. * @sk: sock from which to allocate memory
  1043. * @opt: original options
  1044. * @newtype: option type to replace in @opt
  1045. * @newopt: new option of type @newtype to replace (user-mem)
  1046. *
  1047. * Returns a new set of options which is a copy of @opt with the
  1048. * option type @newtype replaced with @newopt.
  1049. *
  1050. * @opt may be NULL, in which case a new set of options is returned
  1051. * containing just @newopt.
  1052. *
  1053. * @newopt may be NULL, in which case the specified option type is
  1054. * not copied into the new set of options.
  1055. *
  1056. * The new set of options is allocated from the socket option memory
  1057. * buffer of @sk.
  1058. */
  1059. struct ipv6_txoptions *
  1060. ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
  1061. int newtype, struct ipv6_opt_hdr *newopt)
  1062. {
  1063. int tot_len = 0;
  1064. char *p;
  1065. struct ipv6_txoptions *opt2;
  1066. if (opt) {
  1067. if (newtype != IPV6_HOPOPTS && opt->hopopt)
  1068. tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
  1069. if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
  1070. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
  1071. if (newtype != IPV6_RTHDR && opt->srcrt)
  1072. tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
  1073. if (newtype != IPV6_DSTOPTS && opt->dst1opt)
  1074. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
  1075. }
  1076. if (newopt)
  1077. tot_len += CMSG_ALIGN(ipv6_optlen(newopt));
  1078. if (!tot_len)
  1079. return NULL;
  1080. tot_len += sizeof(*opt2);
  1081. opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
  1082. if (!opt2)
  1083. return ERR_PTR(-ENOBUFS);
  1084. memset(opt2, 0, tot_len);
  1085. refcount_set(&opt2->refcnt, 1);
  1086. opt2->tot_len = tot_len;
  1087. p = (char *)(opt2 + 1);
  1088. ipv6_renew_option(IPV6_HOPOPTS, &opt2->hopopt,
  1089. (opt ? opt->hopopt : NULL),
  1090. newopt, newtype, &p);
  1091. ipv6_renew_option(IPV6_RTHDRDSTOPTS, &opt2->dst0opt,
  1092. (opt ? opt->dst0opt : NULL),
  1093. newopt, newtype, &p);
  1094. ipv6_renew_option(IPV6_RTHDR,
  1095. (struct ipv6_opt_hdr **)&opt2->srcrt,
  1096. (opt ? (struct ipv6_opt_hdr *)opt->srcrt : NULL),
  1097. newopt, newtype, &p);
  1098. ipv6_renew_option(IPV6_DSTOPTS, &opt2->dst1opt,
  1099. (opt ? opt->dst1opt : NULL),
  1100. newopt, newtype, &p);
  1101. opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
  1102. (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
  1103. (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
  1104. opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
  1105. return opt2;
  1106. }
  1107. struct ipv6_txoptions *__ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  1108. struct ipv6_txoptions *opt)
  1109. {
  1110. /*
  1111. * ignore the dest before srcrt unless srcrt is being included.
  1112. * --yoshfuji
  1113. */
  1114. if (opt->dst0opt && !opt->srcrt) {
  1115. if (opt_space != opt) {
  1116. memcpy(opt_space, opt, sizeof(*opt_space));
  1117. opt = opt_space;
  1118. }
  1119. opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
  1120. opt->dst0opt = NULL;
  1121. }
  1122. return opt;
  1123. }
  1124. EXPORT_SYMBOL_GPL(__ipv6_fixup_options);
  1125. /**
  1126. * __fl6_update_dst - update flowi destination address with info given
  1127. * by srcrt option, if any.
  1128. *
  1129. * @fl6: flowi6 for which daddr is to be updated
  1130. * @opt: struct ipv6_txoptions in which to look for srcrt opt
  1131. * @orig: copy of original daddr address if modified
  1132. *
  1133. * Return: NULL if no srcrt or invalid srcrt type, otherwise returns orig
  1134. * and initial value of fl6->daddr set in orig
  1135. */
  1136. struct in6_addr *__fl6_update_dst(struct flowi6 *fl6,
  1137. const struct ipv6_txoptions *opt,
  1138. struct in6_addr *orig)
  1139. {
  1140. if (!opt->srcrt)
  1141. return NULL;
  1142. *orig = fl6->daddr;
  1143. switch (opt->srcrt->type) {
  1144. case IPV6_SRCRT_TYPE_0:
  1145. case IPV6_SRCRT_STRICT:
  1146. case IPV6_SRCRT_TYPE_2:
  1147. fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
  1148. break;
  1149. case IPV6_SRCRT_TYPE_4:
  1150. {
  1151. struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt;
  1152. fl6->daddr = srh->segments[srh->segments_left];
  1153. break;
  1154. }
  1155. default:
  1156. return NULL;
  1157. }
  1158. return orig;
  1159. }
  1160. EXPORT_SYMBOL_GPL(__fl6_update_dst);