exthdrs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. * $Id: exthdrs.c,v 1.13 2001/06/19 15:58:56 davem Exp $
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. /* Changes:
  18. * yoshfuji : ensure not to overrun while parsing
  19. * tlv options.
  20. * Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs().
  21. * YOSHIFUJI Hideaki @USAGI Register inbound extension header
  22. * handlers as inet6_protocol{}.
  23. */
  24. #include <linux/errno.h>
  25. #include <linux/types.h>
  26. #include <linux/socket.h>
  27. #include <linux/sockios.h>
  28. #include <linux/net.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/in6.h>
  31. #include <linux/icmpv6.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 **skbp, 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 **skbp, int optoff)
  98. {
  99. struct sk_buff *skb = *skbp;
  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 **skbp)
  120. {
  121. struct sk_buff *skb = *skbp;
  122. struct tlvtype_proc *curr;
  123. const unsigned char *nh = skb_network_header(skb);
  124. int off = skb_network_header_len(skb);
  125. int len = (skb_transport_header(skb)[1] + 1) << 3;
  126. if (skb_transport_offset(skb) + len > skb_headlen(skb))
  127. goto bad;
  128. off += 2;
  129. len -= 2;
  130. while (len > 0) {
  131. int optlen = nh[off + 1] + 2;
  132. switch (nh[off]) {
  133. case IPV6_TLV_PAD0:
  134. optlen = 1;
  135. break;
  136. case IPV6_TLV_PADN:
  137. break;
  138. default: /* Other TLV code so scan list */
  139. if (optlen > len)
  140. goto bad;
  141. for (curr=procs; curr->type >= 0; curr++) {
  142. if (curr->type == nh[off]) {
  143. /* type specific length/alignment
  144. checks will be performed in the
  145. func(). */
  146. if (curr->func(skbp, off) == 0)
  147. return 0;
  148. break;
  149. }
  150. }
  151. if (curr->type < 0) {
  152. if (ip6_tlvopt_unknown(skbp, off) == 0)
  153. return 0;
  154. }
  155. break;
  156. }
  157. off += optlen;
  158. len -= optlen;
  159. }
  160. if (len == 0)
  161. return 1;
  162. bad:
  163. kfree_skb(skb);
  164. return 0;
  165. }
  166. /*****************************
  167. Destination options header.
  168. *****************************/
  169. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  170. static int ipv6_dest_hao(struct sk_buff **skbp, int optoff)
  171. {
  172. struct sk_buff *skb = *skbp;
  173. struct ipv6_destopt_hao *hao;
  174. struct inet6_skb_parm *opt = IP6CB(skb);
  175. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  176. struct in6_addr tmp_addr;
  177. int ret;
  178. if (opt->dsthao) {
  179. LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n");
  180. goto discard;
  181. }
  182. opt->dsthao = opt->dst1;
  183. opt->dst1 = 0;
  184. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
  185. if (hao->length != 16) {
  186. LIMIT_NETDEBUG(
  187. KERN_DEBUG "hao invalid option length = %d\n", hao->length);
  188. goto discard;
  189. }
  190. if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
  191. LIMIT_NETDEBUG(
  192. KERN_DEBUG "hao is not an unicast addr: " NIP6_FMT "\n", NIP6(hao->addr));
  193. goto discard;
  194. }
  195. ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
  196. (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
  197. if (unlikely(ret < 0))
  198. goto discard;
  199. if (skb_cloned(skb)) {
  200. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  201. goto discard;
  202. /* update all variable using below by copied skbuff */
  203. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
  204. optoff);
  205. ipv6h = ipv6_hdr(skb);
  206. }
  207. if (skb->ip_summed == CHECKSUM_COMPLETE)
  208. skb->ip_summed = CHECKSUM_NONE;
  209. ipv6_addr_copy(&tmp_addr, &ipv6h->saddr);
  210. ipv6_addr_copy(&ipv6h->saddr, &hao->addr);
  211. ipv6_addr_copy(&hao->addr, &tmp_addr);
  212. if (skb->tstamp.tv64 == 0)
  213. __net_timestamp(skb);
  214. return 1;
  215. discard:
  216. kfree_skb(skb);
  217. return 0;
  218. }
  219. #endif
  220. static struct tlvtype_proc tlvprocdestopt_lst[] = {
  221. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  222. {
  223. .type = IPV6_TLV_HAO,
  224. .func = ipv6_dest_hao,
  225. },
  226. #endif
  227. {-1, NULL}
  228. };
  229. static int ipv6_destopt_rcv(struct sk_buff **skbp)
  230. {
  231. struct sk_buff *skb = *skbp;
  232. struct inet6_skb_parm *opt = IP6CB(skb);
  233. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  234. __u16 dstbuf;
  235. #endif
  236. struct dst_entry *dst;
  237. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  238. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  239. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  240. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  241. IPSTATS_MIB_INHDRERRORS);
  242. kfree_skb(skb);
  243. return -1;
  244. }
  245. opt->lastopt = opt->dst1 = skb_network_header_len(skb);
  246. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  247. dstbuf = opt->dst1;
  248. #endif
  249. dst = dst_clone(skb->dst);
  250. if (ip6_parse_tlv(tlvprocdestopt_lst, skbp)) {
  251. dst_release(dst);
  252. skb = *skbp;
  253. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  254. opt = IP6CB(skb);
  255. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  256. opt->nhoff = dstbuf;
  257. #else
  258. opt->nhoff = opt->dst1;
  259. #endif
  260. return 1;
  261. }
  262. IP6_INC_STATS_BH(ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
  263. dst_release(dst);
  264. return -1;
  265. }
  266. static struct inet6_protocol destopt_protocol = {
  267. .handler = ipv6_destopt_rcv,
  268. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
  269. };
  270. void __init ipv6_destopt_init(void)
  271. {
  272. if (inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS) < 0)
  273. printk(KERN_ERR "ipv6_destopt_init: Could not register protocol\n");
  274. }
  275. /********************************
  276. NONE header. No data in packet.
  277. ********************************/
  278. static int ipv6_nodata_rcv(struct sk_buff **skbp)
  279. {
  280. struct sk_buff *skb = *skbp;
  281. kfree_skb(skb);
  282. return 0;
  283. }
  284. static struct inet6_protocol nodata_protocol = {
  285. .handler = ipv6_nodata_rcv,
  286. .flags = INET6_PROTO_NOPOLICY,
  287. };
  288. void __init ipv6_nodata_init(void)
  289. {
  290. if (inet6_add_protocol(&nodata_protocol, IPPROTO_NONE) < 0)
  291. printk(KERN_ERR "ipv6_nodata_init: Could not register protocol\n");
  292. }
  293. /********************************
  294. Routing header.
  295. ********************************/
  296. static int ipv6_rthdr_rcv(struct sk_buff **skbp)
  297. {
  298. struct sk_buff *skb = *skbp;
  299. struct inet6_skb_parm *opt = IP6CB(skb);
  300. struct in6_addr *addr = NULL;
  301. struct in6_addr daddr;
  302. struct inet6_dev *idev;
  303. int n, i;
  304. struct ipv6_rt_hdr *hdr;
  305. struct rt0_hdr *rthdr;
  306. int accept_source_route = ipv6_devconf.accept_source_route;
  307. idev = in6_dev_get(skb->dev);
  308. if (idev) {
  309. if (accept_source_route > idev->cnf.accept_source_route)
  310. accept_source_route = idev->cnf.accept_source_route;
  311. in6_dev_put(idev);
  312. }
  313. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  314. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  315. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  316. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  317. IPSTATS_MIB_INHDRERRORS);
  318. kfree_skb(skb);
  319. return -1;
  320. }
  321. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  322. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
  323. skb->pkt_type != PACKET_HOST) {
  324. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  325. IPSTATS_MIB_INADDRERRORS);
  326. kfree_skb(skb);
  327. return -1;
  328. }
  329. looped_back:
  330. if (hdr->segments_left == 0) {
  331. switch (hdr->type) {
  332. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  333. case IPV6_SRCRT_TYPE_2:
  334. /* Silently discard type 2 header unless it was
  335. * processed by own
  336. */
  337. if (!addr) {
  338. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  339. IPSTATS_MIB_INADDRERRORS);
  340. kfree_skb(skb);
  341. return -1;
  342. }
  343. break;
  344. #endif
  345. default:
  346. break;
  347. }
  348. opt->lastopt = opt->srcrt = skb_network_header_len(skb);
  349. skb->transport_header += (hdr->hdrlen + 1) << 3;
  350. opt->dst0 = opt->dst1;
  351. opt->dst1 = 0;
  352. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  353. return 1;
  354. }
  355. switch (hdr->type) {
  356. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  357. case IPV6_SRCRT_TYPE_2:
  358. if (accept_source_route < 0)
  359. goto unknown_rh;
  360. /* Silently discard invalid RTH type 2 */
  361. if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
  362. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  363. IPSTATS_MIB_INHDRERRORS);
  364. kfree_skb(skb);
  365. return -1;
  366. }
  367. break;
  368. #endif
  369. default:
  370. goto unknown_rh;
  371. }
  372. /*
  373. * This is the routing header forwarding algorithm from
  374. * RFC 2460, page 16.
  375. */
  376. n = hdr->hdrlen >> 1;
  377. if (hdr->segments_left > n) {
  378. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  379. IPSTATS_MIB_INHDRERRORS);
  380. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  381. ((&hdr->segments_left) -
  382. skb_network_header(skb)));
  383. return -1;
  384. }
  385. /* We are about to mangle packet header. Be careful!
  386. Do not damage packets queued somewhere.
  387. */
  388. if (skb_cloned(skb)) {
  389. /* the copy is a forwarded packet */
  390. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  391. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  392. IPSTATS_MIB_OUTDISCARDS);
  393. kfree_skb(skb);
  394. return -1;
  395. }
  396. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  397. }
  398. if (skb->ip_summed == CHECKSUM_COMPLETE)
  399. skb->ip_summed = CHECKSUM_NONE;
  400. i = n - --hdr->segments_left;
  401. rthdr = (struct rt0_hdr *) hdr;
  402. addr = rthdr->addr;
  403. addr += i - 1;
  404. switch (hdr->type) {
  405. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  406. case IPV6_SRCRT_TYPE_2:
  407. if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
  408. (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  409. IPPROTO_ROUTING) < 0) {
  410. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  411. IPSTATS_MIB_INADDRERRORS);
  412. kfree_skb(skb);
  413. return -1;
  414. }
  415. if (!ipv6_chk_home_addr(addr)) {
  416. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  417. IPSTATS_MIB_INADDRERRORS);
  418. kfree_skb(skb);
  419. return -1;
  420. }
  421. break;
  422. #endif
  423. default:
  424. break;
  425. }
  426. if (ipv6_addr_is_multicast(addr)) {
  427. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  428. IPSTATS_MIB_INADDRERRORS);
  429. kfree_skb(skb);
  430. return -1;
  431. }
  432. ipv6_addr_copy(&daddr, addr);
  433. ipv6_addr_copy(addr, &ipv6_hdr(skb)->daddr);
  434. ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &daddr);
  435. dst_release(xchg(&skb->dst, NULL));
  436. ip6_route_input(skb);
  437. if (skb->dst->error) {
  438. skb_push(skb, skb->data - skb_network_header(skb));
  439. dst_input(skb);
  440. return -1;
  441. }
  442. if (skb->dst->dev->flags&IFF_LOOPBACK) {
  443. if (ipv6_hdr(skb)->hop_limit <= 1) {
  444. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  445. IPSTATS_MIB_INHDRERRORS);
  446. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  447. 0, skb->dev);
  448. kfree_skb(skb);
  449. return -1;
  450. }
  451. ipv6_hdr(skb)->hop_limit--;
  452. goto looped_back;
  453. }
  454. skb_push(skb, skb->data - skb_network_header(skb));
  455. dst_input(skb);
  456. return -1;
  457. unknown_rh:
  458. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
  459. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  460. (&hdr->type) - skb_network_header(skb));
  461. return -1;
  462. }
  463. static struct inet6_protocol rthdr_protocol = {
  464. .handler = ipv6_rthdr_rcv,
  465. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
  466. };
  467. void __init ipv6_rthdr_init(void)
  468. {
  469. if (inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING) < 0)
  470. printk(KERN_ERR "ipv6_rthdr_init: Could not register protocol\n");
  471. };
  472. /**********************************
  473. Hop-by-hop options.
  474. **********************************/
  475. /*
  476. * Note: we cannot rely on skb->dst before we assign it in ip6_route_input().
  477. */
  478. static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
  479. {
  480. return skb->dst ? ip6_dst_idev(skb->dst) : __in6_dev_get(skb->dev);
  481. }
  482. /* Router Alert as of RFC 2711 */
  483. static int ipv6_hop_ra(struct sk_buff **skbp, int optoff)
  484. {
  485. struct sk_buff *skb = *skbp;
  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 **skbp, int optoff)
  498. {
  499. struct sk_buff *skb = *skbp;
  500. const unsigned char *nh = skb_network_header(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(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(ipv6_skb_idev(skb), 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(ipv6_skb_idev(skb), IPSTATS_MIB_INHDRERRORS);
  517. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
  518. return 0;
  519. }
  520. if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
  521. IP6_INC_STATS_BH(ipv6_skb_idev(skb), 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 **skbp)
  543. {
  544. struct sk_buff *skb = *skbp;
  545. struct inet6_skb_parm *opt = IP6CB(skb);
  546. /*
  547. * skb_network_header(skb) is equal to skb->data, and
  548. * skb_network_header_len(skb) is always equal to
  549. * sizeof(struct ipv6hdr) by definition of
  550. * hop-by-hop options.
  551. */
  552. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
  553. !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
  554. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  555. kfree_skb(skb);
  556. return -1;
  557. }
  558. opt->hop = sizeof(struct ipv6hdr);
  559. if (ip6_parse_tlv(tlvprochopopt_lst, skbp)) {
  560. skb = *skbp;
  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. }