exthdrs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. * Extension Header handling for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Andi Kleen <ak@muc.de>
  8. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /* Changes:
  16. * yoshfuji : ensure not to overrun while parsing
  17. * tlv options.
  18. * Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs().
  19. * YOSHIFUJI Hideaki @USAGI Register inbound extension header
  20. * handlers as inet6_protocol{}.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/net.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/in6.h>
  29. #include <linux/icmpv6.h>
  30. #include <linux/slab.h>
  31. #include <linux/export.h>
  32. #include <net/dst.h>
  33. #include <net/sock.h>
  34. #include <net/snmp.h>
  35. #include <net/ipv6.h>
  36. #include <net/protocol.h>
  37. #include <net/transp_v6.h>
  38. #include <net/rawv6.h>
  39. #include <net/ndisc.h>
  40. #include <net/ip6_route.h>
  41. #include <net/addrconf.h>
  42. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  43. #include <net/xfrm.h>
  44. #endif
  45. #include <asm/uaccess.h>
  46. /*
  47. * Parsing tlv encoded headers.
  48. *
  49. * Parsing function "func" returns true, if parsing succeed
  50. * and false, if it failed.
  51. * It MUST NOT touch skb->h.
  52. */
  53. struct tlvtype_proc {
  54. int type;
  55. bool (*func)(struct sk_buff *skb, int offset);
  56. };
  57. /*********************
  58. Generic functions
  59. *********************/
  60. /* An unknown option is detected, decide what to do */
  61. static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
  62. {
  63. switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
  64. case 0: /* ignore */
  65. return true;
  66. case 1: /* drop packet */
  67. break;
  68. case 3: /* Send ICMP if not a multicast address and drop packet */
  69. /* Actually, it is redundant check. icmp_send
  70. will recheck in any case.
  71. */
  72. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
  73. break;
  74. case 2: /* send ICMP PARM PROB regardless and drop packet */
  75. icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff);
  76. return false;
  77. }
  78. kfree_skb(skb);
  79. return false;
  80. }
  81. /* Parse tlv encoded option header (hop-by-hop or destination) */
  82. static bool ip6_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb)
  83. {
  84. const struct tlvtype_proc *curr;
  85. const unsigned char *nh = skb_network_header(skb);
  86. int off = skb_network_header_len(skb);
  87. int len = (skb_transport_header(skb)[1] + 1) << 3;
  88. int padlen = 0;
  89. if (skb_transport_offset(skb) + len > skb_headlen(skb))
  90. goto bad;
  91. off += 2;
  92. len -= 2;
  93. while (len > 0) {
  94. int optlen = nh[off + 1] + 2;
  95. int i;
  96. switch (nh[off]) {
  97. case IPV6_TLV_PAD1:
  98. optlen = 1;
  99. padlen++;
  100. if (padlen > 7)
  101. goto bad;
  102. break;
  103. case IPV6_TLV_PADN:
  104. /* RFC 2460 states that the purpose of PadN is
  105. * to align the containing header to multiples
  106. * of 8. 7 is therefore the highest valid value.
  107. * See also RFC 4942, Section 2.1.9.5.
  108. */
  109. padlen += optlen;
  110. if (padlen > 7)
  111. goto bad;
  112. /* RFC 4942 recommends receiving hosts to
  113. * actively check PadN payload to contain
  114. * only zeroes.
  115. */
  116. for (i = 2; i < optlen; i++) {
  117. if (nh[off + i] != 0)
  118. goto bad;
  119. }
  120. break;
  121. default: /* Other TLV code so scan list */
  122. if (optlen > len)
  123. goto bad;
  124. for (curr=procs; curr->type >= 0; curr++) {
  125. if (curr->type == nh[off]) {
  126. /* type specific length/alignment
  127. checks will be performed in the
  128. func(). */
  129. if (curr->func(skb, off) == false)
  130. return false;
  131. break;
  132. }
  133. }
  134. if (curr->type < 0) {
  135. if (ip6_tlvopt_unknown(skb, off) == 0)
  136. return false;
  137. }
  138. padlen = 0;
  139. break;
  140. }
  141. off += optlen;
  142. len -= optlen;
  143. }
  144. /* This case will not be caught by above check since its padding
  145. * length is smaller than 7:
  146. * 1 byte NH + 1 byte Length + 6 bytes Padding
  147. */
  148. if ((padlen == 6) && ((off - skb_network_header_len(skb)) == 8))
  149. goto bad;
  150. if (len == 0)
  151. return true;
  152. bad:
  153. kfree_skb(skb);
  154. return false;
  155. }
  156. /*****************************
  157. Destination options header.
  158. *****************************/
  159. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  160. static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
  161. {
  162. struct ipv6_destopt_hao *hao;
  163. struct inet6_skb_parm *opt = IP6CB(skb);
  164. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  165. struct in6_addr tmp_addr;
  166. int ret;
  167. if (opt->dsthao) {
  168. LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n");
  169. goto discard;
  170. }
  171. opt->dsthao = opt->dst1;
  172. opt->dst1 = 0;
  173. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
  174. if (hao->length != 16) {
  175. LIMIT_NETDEBUG(
  176. KERN_DEBUG "hao invalid option length = %d\n", hao->length);
  177. goto discard;
  178. }
  179. if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
  180. LIMIT_NETDEBUG(
  181. KERN_DEBUG "hao is not an unicast addr: %pI6\n", &hao->addr);
  182. goto discard;
  183. }
  184. ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
  185. (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
  186. if (unlikely(ret < 0))
  187. goto discard;
  188. if (skb_cloned(skb)) {
  189. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  190. goto discard;
  191. /* update all variable using below by copied skbuff */
  192. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
  193. optoff);
  194. ipv6h = ipv6_hdr(skb);
  195. }
  196. if (skb->ip_summed == CHECKSUM_COMPLETE)
  197. skb->ip_summed = CHECKSUM_NONE;
  198. tmp_addr = ipv6h->saddr;
  199. ipv6h->saddr = hao->addr;
  200. hao->addr = tmp_addr;
  201. if (skb->tstamp.tv64 == 0)
  202. __net_timestamp(skb);
  203. return true;
  204. discard:
  205. kfree_skb(skb);
  206. return false;
  207. }
  208. #endif
  209. static const struct tlvtype_proc tlvprocdestopt_lst[] = {
  210. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  211. {
  212. .type = IPV6_TLV_HAO,
  213. .func = ipv6_dest_hao,
  214. },
  215. #endif
  216. {-1, NULL}
  217. };
  218. static int ipv6_destopt_rcv(struct sk_buff *skb)
  219. {
  220. struct inet6_skb_parm *opt = IP6CB(skb);
  221. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  222. __u16 dstbuf;
  223. #endif
  224. struct dst_entry *dst = skb_dst(skb);
  225. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  226. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  227. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  228. IP6_INC_STATS_BH(dev_net(dst->dev), ip6_dst_idev(dst),
  229. IPSTATS_MIB_INHDRERRORS);
  230. kfree_skb(skb);
  231. return -1;
  232. }
  233. opt->lastopt = opt->dst1 = skb_network_header_len(skb);
  234. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  235. dstbuf = opt->dst1;
  236. #endif
  237. if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
  238. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  239. opt = IP6CB(skb);
  240. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  241. opt->nhoff = dstbuf;
  242. #else
  243. opt->nhoff = opt->dst1;
  244. #endif
  245. return 1;
  246. }
  247. IP6_INC_STATS_BH(dev_net(dst->dev),
  248. ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
  249. return -1;
  250. }
  251. /********************************
  252. Routing header.
  253. ********************************/
  254. /* called with rcu_read_lock() */
  255. static int ipv6_rthdr_rcv(struct sk_buff *skb)
  256. {
  257. struct inet6_skb_parm *opt = IP6CB(skb);
  258. struct in6_addr *addr = NULL;
  259. struct in6_addr daddr;
  260. struct inet6_dev *idev;
  261. int n, i;
  262. struct ipv6_rt_hdr *hdr;
  263. struct rt0_hdr *rthdr;
  264. struct net *net = dev_net(skb->dev);
  265. int accept_source_route = net->ipv6.devconf_all->accept_source_route;
  266. idev = __in6_dev_get(skb->dev);
  267. if (idev && accept_source_route > idev->cnf.accept_source_route)
  268. accept_source_route = idev->cnf.accept_source_route;
  269. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  270. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  271. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  272. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  273. IPSTATS_MIB_INHDRERRORS);
  274. kfree_skb(skb);
  275. return -1;
  276. }
  277. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  278. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
  279. skb->pkt_type != PACKET_HOST) {
  280. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  281. IPSTATS_MIB_INADDRERRORS);
  282. kfree_skb(skb);
  283. return -1;
  284. }
  285. looped_back:
  286. if (hdr->segments_left == 0) {
  287. switch (hdr->type) {
  288. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  289. case IPV6_SRCRT_TYPE_2:
  290. /* Silently discard type 2 header unless it was
  291. * processed by own
  292. */
  293. if (!addr) {
  294. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  295. IPSTATS_MIB_INADDRERRORS);
  296. kfree_skb(skb);
  297. return -1;
  298. }
  299. break;
  300. #endif
  301. default:
  302. break;
  303. }
  304. opt->lastopt = opt->srcrt = skb_network_header_len(skb);
  305. skb->transport_header += (hdr->hdrlen + 1) << 3;
  306. opt->dst0 = opt->dst1;
  307. opt->dst1 = 0;
  308. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  309. return 1;
  310. }
  311. switch (hdr->type) {
  312. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  313. case IPV6_SRCRT_TYPE_2:
  314. if (accept_source_route < 0)
  315. goto unknown_rh;
  316. /* Silently discard invalid RTH type 2 */
  317. if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
  318. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  319. IPSTATS_MIB_INHDRERRORS);
  320. kfree_skb(skb);
  321. return -1;
  322. }
  323. break;
  324. #endif
  325. default:
  326. goto unknown_rh;
  327. }
  328. /*
  329. * This is the routing header forwarding algorithm from
  330. * RFC 2460, page 16.
  331. */
  332. n = hdr->hdrlen >> 1;
  333. if (hdr->segments_left > n) {
  334. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  335. IPSTATS_MIB_INHDRERRORS);
  336. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  337. ((&hdr->segments_left) -
  338. skb_network_header(skb)));
  339. return -1;
  340. }
  341. /* We are about to mangle packet header. Be careful!
  342. Do not damage packets queued somewhere.
  343. */
  344. if (skb_cloned(skb)) {
  345. /* the copy is a forwarded packet */
  346. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  347. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  348. IPSTATS_MIB_OUTDISCARDS);
  349. kfree_skb(skb);
  350. return -1;
  351. }
  352. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  353. }
  354. if (skb->ip_summed == CHECKSUM_COMPLETE)
  355. skb->ip_summed = CHECKSUM_NONE;
  356. i = n - --hdr->segments_left;
  357. rthdr = (struct rt0_hdr *) hdr;
  358. addr = rthdr->addr;
  359. addr += i - 1;
  360. switch (hdr->type) {
  361. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  362. case IPV6_SRCRT_TYPE_2:
  363. if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
  364. (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  365. IPPROTO_ROUTING) < 0) {
  366. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  367. IPSTATS_MIB_INADDRERRORS);
  368. kfree_skb(skb);
  369. return -1;
  370. }
  371. if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
  372. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  373. IPSTATS_MIB_INADDRERRORS);
  374. kfree_skb(skb);
  375. return -1;
  376. }
  377. break;
  378. #endif
  379. default:
  380. break;
  381. }
  382. if (ipv6_addr_is_multicast(addr)) {
  383. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  384. IPSTATS_MIB_INADDRERRORS);
  385. kfree_skb(skb);
  386. return -1;
  387. }
  388. daddr = *addr;
  389. *addr = ipv6_hdr(skb)->daddr;
  390. ipv6_hdr(skb)->daddr = daddr;
  391. skb_dst_drop(skb);
  392. ip6_route_input(skb);
  393. if (skb_dst(skb)->error) {
  394. skb_push(skb, skb->data - skb_network_header(skb));
  395. dst_input(skb);
  396. return -1;
  397. }
  398. if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
  399. if (ipv6_hdr(skb)->hop_limit <= 1) {
  400. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  401. IPSTATS_MIB_INHDRERRORS);
  402. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  403. 0);
  404. kfree_skb(skb);
  405. return -1;
  406. }
  407. ipv6_hdr(skb)->hop_limit--;
  408. goto looped_back;
  409. }
  410. skb_push(skb, skb->data - skb_network_header(skb));
  411. dst_input(skb);
  412. return -1;
  413. unknown_rh:
  414. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
  415. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  416. (&hdr->type) - skb_network_header(skb));
  417. return -1;
  418. }
  419. static const struct inet6_protocol rthdr_protocol = {
  420. .handler = ipv6_rthdr_rcv,
  421. .flags = INET6_PROTO_NOPOLICY,
  422. };
  423. static const struct inet6_protocol destopt_protocol = {
  424. .handler = ipv6_destopt_rcv,
  425. .flags = INET6_PROTO_NOPOLICY,
  426. };
  427. static const struct inet6_protocol nodata_protocol = {
  428. .handler = dst_discard,
  429. .flags = INET6_PROTO_NOPOLICY,
  430. };
  431. int __init ipv6_exthdrs_init(void)
  432. {
  433. int ret;
  434. ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  435. if (ret)
  436. goto out;
  437. ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  438. if (ret)
  439. goto out_rthdr;
  440. ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
  441. if (ret)
  442. goto out_destopt;
  443. out:
  444. return ret;
  445. out_destopt:
  446. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  447. out_rthdr:
  448. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  449. goto out;
  450. };
  451. void ipv6_exthdrs_exit(void)
  452. {
  453. inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
  454. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  455. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  456. }
  457. /**********************************
  458. Hop-by-hop options.
  459. **********************************/
  460. /*
  461. * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
  462. */
  463. static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
  464. {
  465. return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
  466. }
  467. static inline struct net *ipv6_skb_net(struct sk_buff *skb)
  468. {
  469. return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
  470. }
  471. /* Router Alert as of RFC 2711 */
  472. static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
  473. {
  474. const unsigned char *nh = skb_network_header(skb);
  475. if (nh[optoff + 1] == 2) {
  476. IP6CB(skb)->ra = optoff;
  477. return true;
  478. }
  479. LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n",
  480. nh[optoff + 1]);
  481. kfree_skb(skb);
  482. return false;
  483. }
  484. /* Jumbo payload */
  485. static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
  486. {
  487. const unsigned char *nh = skb_network_header(skb);
  488. struct net *net = ipv6_skb_net(skb);
  489. u32 pkt_len;
  490. if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
  491. LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
  492. nh[optoff+1]);
  493. IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
  494. IPSTATS_MIB_INHDRERRORS);
  495. goto drop;
  496. }
  497. pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
  498. if (pkt_len <= IPV6_MAXPLEN) {
  499. IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
  500. IPSTATS_MIB_INHDRERRORS);
  501. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
  502. return false;
  503. }
  504. if (ipv6_hdr(skb)->payload_len) {
  505. IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
  506. IPSTATS_MIB_INHDRERRORS);
  507. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
  508. return false;
  509. }
  510. if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
  511. IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
  512. IPSTATS_MIB_INTRUNCATEDPKTS);
  513. goto drop;
  514. }
  515. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  516. goto drop;
  517. return true;
  518. drop:
  519. kfree_skb(skb);
  520. return false;
  521. }
  522. static const struct tlvtype_proc tlvprochopopt_lst[] = {
  523. {
  524. .type = IPV6_TLV_ROUTERALERT,
  525. .func = ipv6_hop_ra,
  526. },
  527. {
  528. .type = IPV6_TLV_JUMBO,
  529. .func = ipv6_hop_jumbo,
  530. },
  531. { -1, }
  532. };
  533. int ipv6_parse_hopopts(struct sk_buff *skb)
  534. {
  535. struct inet6_skb_parm *opt = IP6CB(skb);
  536. /*
  537. * skb_network_header(skb) is equal to skb->data, and
  538. * skb_network_header_len(skb) is always equal to
  539. * sizeof(struct ipv6hdr) by definition of
  540. * hop-by-hop options.
  541. */
  542. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
  543. !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
  544. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  545. kfree_skb(skb);
  546. return -1;
  547. }
  548. opt->hop = sizeof(struct ipv6hdr);
  549. if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
  550. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  551. opt = IP6CB(skb);
  552. opt->nhoff = sizeof(struct ipv6hdr);
  553. return 1;
  554. }
  555. return -1;
  556. }
  557. /*
  558. * Creating outbound headers.
  559. *
  560. * "build" functions work when skb is filled from head to tail (datagram)
  561. * "push" functions work when headers are added from tail to head (tcp)
  562. *
  563. * In both cases we assume, that caller reserved enough room
  564. * for headers.
  565. */
  566. static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
  567. struct ipv6_rt_hdr *opt,
  568. struct in6_addr **addr_p)
  569. {
  570. struct rt0_hdr *phdr, *ihdr;
  571. int hops;
  572. ihdr = (struct rt0_hdr *) opt;
  573. phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
  574. memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
  575. hops = ihdr->rt_hdr.hdrlen >> 1;
  576. if (hops > 1)
  577. memcpy(phdr->addr, ihdr->addr + 1,
  578. (hops - 1) * sizeof(struct in6_addr));
  579. phdr->addr[hops - 1] = **addr_p;
  580. *addr_p = ihdr->addr;
  581. phdr->rt_hdr.nexthdr = *proto;
  582. *proto = NEXTHDR_ROUTING;
  583. }
  584. static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
  585. {
  586. struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
  587. memcpy(h, opt, ipv6_optlen(opt));
  588. h->nexthdr = *proto;
  589. *proto = type;
  590. }
  591. void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  592. u8 *proto,
  593. struct in6_addr **daddr)
  594. {
  595. if (opt->srcrt) {
  596. ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
  597. /*
  598. * IPV6_RTHDRDSTOPTS is ignored
  599. * unless IPV6_RTHDR is set (RFC3542).
  600. */
  601. if (opt->dst0opt)
  602. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
  603. }
  604. if (opt->hopopt)
  605. ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
  606. }
  607. EXPORT_SYMBOL(ipv6_push_nfrag_opts);
  608. void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
  609. {
  610. if (opt->dst1opt)
  611. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
  612. }
  613. struct ipv6_txoptions *
  614. ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
  615. {
  616. struct ipv6_txoptions *opt2;
  617. opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
  618. if (opt2) {
  619. long dif = (char *)opt2 - (char *)opt;
  620. memcpy(opt2, opt, opt->tot_len);
  621. if (opt2->hopopt)
  622. *((char **)&opt2->hopopt) += dif;
  623. if (opt2->dst0opt)
  624. *((char **)&opt2->dst0opt) += dif;
  625. if (opt2->dst1opt)
  626. *((char **)&opt2->dst1opt) += dif;
  627. if (opt2->srcrt)
  628. *((char **)&opt2->srcrt) += dif;
  629. }
  630. return opt2;
  631. }
  632. EXPORT_SYMBOL_GPL(ipv6_dup_options);
  633. static int ipv6_renew_option(void *ohdr,
  634. struct ipv6_opt_hdr __user *newopt, int newoptlen,
  635. int inherit,
  636. struct ipv6_opt_hdr **hdr,
  637. char **p)
  638. {
  639. if (inherit) {
  640. if (ohdr) {
  641. memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
  642. *hdr = (struct ipv6_opt_hdr *)*p;
  643. *p += CMSG_ALIGN(ipv6_optlen(*hdr));
  644. }
  645. } else {
  646. if (newopt) {
  647. if (copy_from_user(*p, newopt, newoptlen))
  648. return -EFAULT;
  649. *hdr = (struct ipv6_opt_hdr *)*p;
  650. if (ipv6_optlen(*hdr) > newoptlen)
  651. return -EINVAL;
  652. *p += CMSG_ALIGN(newoptlen);
  653. }
  654. }
  655. return 0;
  656. }
  657. struct ipv6_txoptions *
  658. ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
  659. int newtype,
  660. struct ipv6_opt_hdr __user *newopt, int newoptlen)
  661. {
  662. int tot_len = 0;
  663. char *p;
  664. struct ipv6_txoptions *opt2;
  665. int err;
  666. if (opt) {
  667. if (newtype != IPV6_HOPOPTS && opt->hopopt)
  668. tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
  669. if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
  670. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
  671. if (newtype != IPV6_RTHDR && opt->srcrt)
  672. tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
  673. if (newtype != IPV6_DSTOPTS && opt->dst1opt)
  674. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
  675. }
  676. if (newopt && newoptlen)
  677. tot_len += CMSG_ALIGN(newoptlen);
  678. if (!tot_len)
  679. return NULL;
  680. tot_len += sizeof(*opt2);
  681. opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
  682. if (!opt2)
  683. return ERR_PTR(-ENOBUFS);
  684. memset(opt2, 0, tot_len);
  685. opt2->tot_len = tot_len;
  686. p = (char *)(opt2 + 1);
  687. err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
  688. newtype != IPV6_HOPOPTS,
  689. &opt2->hopopt, &p);
  690. if (err)
  691. goto out;
  692. err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
  693. newtype != IPV6_RTHDRDSTOPTS,
  694. &opt2->dst0opt, &p);
  695. if (err)
  696. goto out;
  697. err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
  698. newtype != IPV6_RTHDR,
  699. (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
  700. if (err)
  701. goto out;
  702. err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
  703. newtype != IPV6_DSTOPTS,
  704. &opt2->dst1opt, &p);
  705. if (err)
  706. goto out;
  707. opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
  708. (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
  709. (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
  710. opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
  711. return opt2;
  712. out:
  713. sock_kfree_s(sk, opt2, opt2->tot_len);
  714. return ERR_PTR(err);
  715. }
  716. struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  717. struct ipv6_txoptions *opt)
  718. {
  719. /*
  720. * ignore the dest before srcrt unless srcrt is being included.
  721. * --yoshfuji
  722. */
  723. if (opt && opt->dst0opt && !opt->srcrt) {
  724. if (opt_space != opt) {
  725. memcpy(opt_space, opt, sizeof(*opt_space));
  726. opt = opt_space;
  727. }
  728. opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
  729. opt->dst0opt = NULL;
  730. }
  731. return opt;
  732. }
  733. EXPORT_SYMBOL_GPL(ipv6_fixup_options);
  734. /**
  735. * fl6_update_dst - update flowi destination address with info given
  736. * by srcrt option, if any.
  737. *
  738. * @fl6: flowi6 for which daddr is to be updated
  739. * @opt: struct ipv6_txoptions in which to look for srcrt opt
  740. * @orig: copy of original daddr address if modified
  741. *
  742. * Returns NULL if no txoptions or no srcrt, otherwise returns orig
  743. * and initial value of fl6->daddr set in orig
  744. */
  745. struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
  746. const struct ipv6_txoptions *opt,
  747. struct in6_addr *orig)
  748. {
  749. if (!opt || !opt->srcrt)
  750. return NULL;
  751. *orig = fl6->daddr;
  752. fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
  753. return orig;
  754. }
  755. EXPORT_SYMBOL_GPL(fl6_update_dst);