exthdrs.c 21 KB

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