/kernel/2.6.32_froyo_photon_nightly/net/ipv6/exthdrs.c

http://photon-android.googlecode.com/ · C · 875 lines · 654 code · 131 blank · 90 comment · 133 complexity · 75aa319c1a2cef9666ce3f80288f12f0 MD5 · raw file

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