exthdrs.c 22 KB

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