exthdrs.c 22 KB

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