exthdrs.c 21 KB

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