exthdrs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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/sched.h>
  29. #include <linux/net.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/in6.h>
  32. #include <linux/icmpv6.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. #ifdef CONFIG_IPV6_MIP6
  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. int packet_len = skb->tail - skb->nh.raw;
  49. struct ipv6_opt_hdr *hdr;
  50. int len;
  51. if (offset + 2 > packet_len)
  52. goto bad;
  53. hdr = (struct ipv6_opt_hdr*)(skb->nh.raw + 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 = skb->nh.raw[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 = skb->nh.raw[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. /*
  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 **skbp, 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 **skbp, int optoff)
  97. {
  98. struct sk_buff *skb = *skbp;
  99. switch ((skb->nh.raw[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(&skb->nh.ipv6h->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 **skbp)
  119. {
  120. struct sk_buff *skb = *skbp;
  121. struct tlvtype_proc *curr;
  122. int off = skb->h.raw - skb->nh.raw;
  123. int len = ((skb->h.raw[1]+1)<<3);
  124. if ((skb->h.raw + len) - skb->data > skb_headlen(skb))
  125. goto bad;
  126. off += 2;
  127. len -= 2;
  128. while (len > 0) {
  129. int optlen = skb->nh.raw[off+1]+2;
  130. switch (skb->nh.raw[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 == skb->nh.raw[off]) {
  141. /* type specific length/alignment
  142. checks will be performed in the
  143. func(). */
  144. if (curr->func(skbp, off) == 0)
  145. return 0;
  146. break;
  147. }
  148. }
  149. if (curr->type < 0) {
  150. if (ip6_tlvopt_unknown(skbp, 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. #ifdef CONFIG_IPV6_MIP6
  168. static int ipv6_dest_hao(struct sk_buff **skbp, int optoff)
  169. {
  170. struct sk_buff *skb = *skbp;
  171. struct ipv6_destopt_hao *hao;
  172. struct inet6_skb_parm *opt = IP6CB(skb);
  173. struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb->nh.raw;
  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->nh.raw + 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: " NIP6_FMT "\n", NIP6(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. struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
  199. struct inet6_skb_parm *opt2;
  200. if (skb2 == NULL)
  201. goto discard;
  202. opt2 = IP6CB(skb2);
  203. memcpy(opt2, opt, sizeof(*opt2));
  204. kfree_skb(skb);
  205. /* update all variable using below by copied skbuff */
  206. *skbp = skb = skb2;
  207. hao = (struct ipv6_destopt_hao *)(skb2->nh.raw + optoff);
  208. ipv6h = (struct ipv6hdr *)skb2->nh.raw;
  209. }
  210. if (skb->ip_summed == CHECKSUM_COMPLETE)
  211. skb->ip_summed = CHECKSUM_NONE;
  212. ipv6_addr_copy(&tmp_addr, &ipv6h->saddr);
  213. ipv6_addr_copy(&ipv6h->saddr, &hao->addr);
  214. ipv6_addr_copy(&hao->addr, &tmp_addr);
  215. if (skb->tstamp.off_sec == 0)
  216. __net_timestamp(skb);
  217. return 1;
  218. discard:
  219. kfree_skb(skb);
  220. return 0;
  221. }
  222. #endif
  223. static struct tlvtype_proc tlvprocdestopt_lst[] = {
  224. #ifdef CONFIG_IPV6_MIP6
  225. {
  226. .type = IPV6_TLV_HAO,
  227. .func = ipv6_dest_hao,
  228. },
  229. #endif
  230. {-1, NULL}
  231. };
  232. static int ipv6_destopt_rcv(struct sk_buff **skbp)
  233. {
  234. struct sk_buff *skb = *skbp;
  235. struct inet6_skb_parm *opt = IP6CB(skb);
  236. #ifdef CONFIG_IPV6_MIP6
  237. __u16 dstbuf;
  238. #endif
  239. if (!pskb_may_pull(skb, (skb->h.raw-skb->data)+8) ||
  240. !pskb_may_pull(skb, (skb->h.raw-skb->data)+((skb->h.raw[1]+1)<<3))) {
  241. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  242. kfree_skb(skb);
  243. return -1;
  244. }
  245. opt->lastopt = skb->h.raw - skb->nh.raw;
  246. opt->dst1 = skb->h.raw - skb->nh.raw;
  247. #ifdef CONFIG_IPV6_MIP6
  248. dstbuf = opt->dst1;
  249. #endif
  250. if (ip6_parse_tlv(tlvprocdestopt_lst, skbp)) {
  251. skb = *skbp;
  252. skb->h.raw += ((skb->h.raw[1]+1)<<3);
  253. opt = IP6CB(skb);
  254. #ifdef CONFIG_IPV6_MIP6
  255. opt->nhoff = dstbuf;
  256. #else
  257. opt->nhoff = opt->dst1;
  258. #endif
  259. return 1;
  260. }
  261. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  262. return -1;
  263. }
  264. static struct inet6_protocol destopt_protocol = {
  265. .handler = ipv6_destopt_rcv,
  266. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
  267. };
  268. void __init ipv6_destopt_init(void)
  269. {
  270. if (inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS) < 0)
  271. printk(KERN_ERR "ipv6_destopt_init: Could not register protocol\n");
  272. }
  273. /********************************
  274. NONE header. No data in packet.
  275. ********************************/
  276. static int ipv6_nodata_rcv(struct sk_buff **skbp)
  277. {
  278. struct sk_buff *skb = *skbp;
  279. kfree_skb(skb);
  280. return 0;
  281. }
  282. static struct inet6_protocol nodata_protocol = {
  283. .handler = ipv6_nodata_rcv,
  284. .flags = INET6_PROTO_NOPOLICY,
  285. };
  286. void __init ipv6_nodata_init(void)
  287. {
  288. if (inet6_add_protocol(&nodata_protocol, IPPROTO_NONE) < 0)
  289. printk(KERN_ERR "ipv6_nodata_init: Could not register protocol\n");
  290. }
  291. /********************************
  292. Routing header.
  293. ********************************/
  294. static int ipv6_rthdr_rcv(struct sk_buff **skbp)
  295. {
  296. struct sk_buff *skb = *skbp;
  297. struct inet6_skb_parm *opt = IP6CB(skb);
  298. struct in6_addr *addr = NULL;
  299. struct in6_addr daddr;
  300. int n, i;
  301. struct ipv6_rt_hdr *hdr;
  302. struct rt0_hdr *rthdr;
  303. if (!pskb_may_pull(skb, (skb->h.raw-skb->data)+8) ||
  304. !pskb_may_pull(skb, (skb->h.raw-skb->data)+((skb->h.raw[1]+1)<<3))) {
  305. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  306. kfree_skb(skb);
  307. return -1;
  308. }
  309. hdr = (struct ipv6_rt_hdr *) skb->h.raw;
  310. if (ipv6_addr_is_multicast(&skb->nh.ipv6h->daddr) ||
  311. skb->pkt_type != PACKET_HOST) {
  312. IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
  313. kfree_skb(skb);
  314. return -1;
  315. }
  316. looped_back:
  317. if (hdr->segments_left == 0) {
  318. switch (hdr->type) {
  319. #ifdef CONFIG_IPV6_MIP6
  320. case IPV6_SRCRT_TYPE_2:
  321. /* Silently discard type 2 header unless it was
  322. * processed by own
  323. */
  324. if (!addr) {
  325. IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
  326. kfree_skb(skb);
  327. return -1;
  328. }
  329. break;
  330. #endif
  331. default:
  332. break;
  333. }
  334. opt->lastopt = skb->h.raw - skb->nh.raw;
  335. opt->srcrt = skb->h.raw - skb->nh.raw;
  336. skb->h.raw += (hdr->hdrlen + 1) << 3;
  337. opt->dst0 = opt->dst1;
  338. opt->dst1 = 0;
  339. opt->nhoff = (&hdr->nexthdr) - skb->nh.raw;
  340. return 1;
  341. }
  342. switch (hdr->type) {
  343. case IPV6_SRCRT_TYPE_0:
  344. if (hdr->hdrlen & 0x01) {
  345. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  346. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, (&hdr->hdrlen) - skb->nh.raw);
  347. return -1;
  348. }
  349. break;
  350. #ifdef CONFIG_IPV6_MIP6
  351. case IPV6_SRCRT_TYPE_2:
  352. /* Silently discard invalid RTH type 2 */
  353. if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
  354. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  355. kfree_skb(skb);
  356. return -1;
  357. }
  358. break;
  359. #endif
  360. default:
  361. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  362. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, (&hdr->type) - skb->nh.raw);
  363. return -1;
  364. }
  365. /*
  366. * This is the routing header forwarding algorithm from
  367. * RFC 2460, page 16.
  368. */
  369. n = hdr->hdrlen >> 1;
  370. if (hdr->segments_left > n) {
  371. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  372. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, (&hdr->segments_left) - skb->nh.raw);
  373. return -1;
  374. }
  375. /* We are about to mangle packet header. Be careful!
  376. Do not damage packets queued somewhere.
  377. */
  378. if (skb_cloned(skb)) {
  379. struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
  380. kfree_skb(skb);
  381. /* the copy is a forwarded packet */
  382. if (skb2 == NULL) {
  383. IP6_INC_STATS_BH(IPSTATS_MIB_OUTDISCARDS);
  384. return -1;
  385. }
  386. *skbp = skb = skb2;
  387. opt = IP6CB(skb2);
  388. hdr = (struct ipv6_rt_hdr *) skb2->h.raw;
  389. }
  390. if (skb->ip_summed == CHECKSUM_COMPLETE)
  391. skb->ip_summed = CHECKSUM_NONE;
  392. i = n - --hdr->segments_left;
  393. rthdr = (struct rt0_hdr *) hdr;
  394. addr = rthdr->addr;
  395. addr += i - 1;
  396. switch (hdr->type) {
  397. #ifdef CONFIG_IPV6_MIP6
  398. case IPV6_SRCRT_TYPE_2:
  399. if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
  400. (xfrm_address_t *)&skb->nh.ipv6h->saddr,
  401. IPPROTO_ROUTING) < 0) {
  402. IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
  403. kfree_skb(skb);
  404. return -1;
  405. }
  406. if (!ipv6_chk_home_addr(addr)) {
  407. IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
  408. kfree_skb(skb);
  409. return -1;
  410. }
  411. break;
  412. #endif
  413. default:
  414. break;
  415. }
  416. if (ipv6_addr_is_multicast(addr)) {
  417. IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
  418. kfree_skb(skb);
  419. return -1;
  420. }
  421. ipv6_addr_copy(&daddr, addr);
  422. ipv6_addr_copy(addr, &skb->nh.ipv6h->daddr);
  423. ipv6_addr_copy(&skb->nh.ipv6h->daddr, &daddr);
  424. dst_release(xchg(&skb->dst, NULL));
  425. ip6_route_input(skb);
  426. if (skb->dst->error) {
  427. skb_push(skb, skb->data - skb->nh.raw);
  428. dst_input(skb);
  429. return -1;
  430. }
  431. if (skb->dst->dev->flags&IFF_LOOPBACK) {
  432. if (skb->nh.ipv6h->hop_limit <= 1) {
  433. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  434. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  435. 0, skb->dev);
  436. kfree_skb(skb);
  437. return -1;
  438. }
  439. skb->nh.ipv6h->hop_limit--;
  440. goto looped_back;
  441. }
  442. skb_push(skb, skb->data - skb->nh.raw);
  443. dst_input(skb);
  444. return -1;
  445. }
  446. static struct inet6_protocol rthdr_protocol = {
  447. .handler = ipv6_rthdr_rcv,
  448. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
  449. };
  450. void __init ipv6_rthdr_init(void)
  451. {
  452. if (inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING) < 0)
  453. printk(KERN_ERR "ipv6_rthdr_init: Could not register protocol\n");
  454. };
  455. /*
  456. This function inverts received rthdr.
  457. NOTE: specs allow to make it automatically only if
  458. packet authenticated.
  459. I will not discuss it here (though, I am really pissed off at
  460. this stupid requirement making rthdr idea useless)
  461. Actually, it creates severe problems for us.
  462. Embryonic requests has no associated sockets,
  463. so that user have no control over it and
  464. cannot not only to set reply options, but
  465. even to know, that someone wants to connect
  466. without success. :-(
  467. For now we need to test the engine, so that I created
  468. temporary (or permanent) backdoor.
  469. If listening socket set IPV6_RTHDR to 2, then we invert header.
  470. --ANK (980729)
  471. */
  472. struct ipv6_txoptions *
  473. ipv6_invert_rthdr(struct sock *sk, struct ipv6_rt_hdr *hdr)
  474. {
  475. /* Received rthdr:
  476. [ H1 -> H2 -> ... H_prev ] daddr=ME
  477. Inverted result:
  478. [ H_prev -> ... -> H1 ] daddr =sender
  479. Note, that IP output engine will rewrite this rthdr
  480. by rotating it left by one addr.
  481. */
  482. int n, i;
  483. struct rt0_hdr *rthdr = (struct rt0_hdr*)hdr;
  484. struct rt0_hdr *irthdr;
  485. struct ipv6_txoptions *opt;
  486. int hdrlen = ipv6_optlen(hdr);
  487. if (hdr->segments_left ||
  488. hdr->type != IPV6_SRCRT_TYPE_0 ||
  489. hdr->hdrlen & 0x01)
  490. return NULL;
  491. n = hdr->hdrlen >> 1;
  492. opt = sock_kmalloc(sk, sizeof(*opt) + hdrlen, GFP_ATOMIC);
  493. if (opt == NULL)
  494. return NULL;
  495. memset(opt, 0, sizeof(*opt));
  496. opt->tot_len = sizeof(*opt) + hdrlen;
  497. opt->srcrt = (void*)(opt+1);
  498. opt->opt_nflen = hdrlen;
  499. memcpy(opt->srcrt, hdr, sizeof(*hdr));
  500. irthdr = (struct rt0_hdr*)opt->srcrt;
  501. irthdr->reserved = 0;
  502. opt->srcrt->segments_left = n;
  503. for (i=0; i<n; i++)
  504. memcpy(irthdr->addr+i, rthdr->addr+(n-1-i), 16);
  505. return opt;
  506. }
  507. EXPORT_SYMBOL_GPL(ipv6_invert_rthdr);
  508. /**********************************
  509. Hop-by-hop options.
  510. **********************************/
  511. /* Router Alert as of RFC 2711 */
  512. static int ipv6_hop_ra(struct sk_buff **skbp, int optoff)
  513. {
  514. struct sk_buff *skb = *skbp;
  515. if (skb->nh.raw[optoff+1] == 2) {
  516. IP6CB(skb)->ra = optoff;
  517. return 1;
  518. }
  519. LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n",
  520. skb->nh.raw[optoff+1]);
  521. kfree_skb(skb);
  522. return 0;
  523. }
  524. /* Jumbo payload */
  525. static int ipv6_hop_jumbo(struct sk_buff **skbp, int optoff)
  526. {
  527. struct sk_buff *skb = *skbp;
  528. u32 pkt_len;
  529. if (skb->nh.raw[optoff+1] != 4 || (optoff&3) != 2) {
  530. LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
  531. skb->nh.raw[optoff+1]);
  532. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  533. goto drop;
  534. }
  535. pkt_len = ntohl(*(u32*)(skb->nh.raw+optoff+2));
  536. if (pkt_len <= IPV6_MAXPLEN) {
  537. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  538. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
  539. return 0;
  540. }
  541. if (skb->nh.ipv6h->payload_len) {
  542. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  543. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
  544. return 0;
  545. }
  546. if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
  547. IP6_INC_STATS_BH(IPSTATS_MIB_INTRUNCATEDPKTS);
  548. goto drop;
  549. }
  550. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  551. goto drop;
  552. return 1;
  553. drop:
  554. kfree_skb(skb);
  555. return 0;
  556. }
  557. static struct tlvtype_proc tlvprochopopt_lst[] = {
  558. {
  559. .type = IPV6_TLV_ROUTERALERT,
  560. .func = ipv6_hop_ra,
  561. },
  562. {
  563. .type = IPV6_TLV_JUMBO,
  564. .func = ipv6_hop_jumbo,
  565. },
  566. { -1, }
  567. };
  568. int ipv6_parse_hopopts(struct sk_buff **skbp)
  569. {
  570. struct sk_buff *skb = *skbp;
  571. struct inet6_skb_parm *opt = IP6CB(skb);
  572. /*
  573. * skb->nh.raw is equal to skb->data, and
  574. * skb->h.raw - skb->nh.raw is always equal to
  575. * sizeof(struct ipv6hdr) by definition of
  576. * hop-by-hop options.
  577. */
  578. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
  579. !pskb_may_pull(skb, sizeof(struct ipv6hdr) + ((skb->h.raw[1] + 1) << 3))) {
  580. kfree_skb(skb);
  581. return -1;
  582. }
  583. opt->hop = sizeof(struct ipv6hdr);
  584. if (ip6_parse_tlv(tlvprochopopt_lst, skbp)) {
  585. skb = *skbp;
  586. skb->h.raw += (skb->h.raw[1]+1)<<3;
  587. opt = IP6CB(skb);
  588. opt->nhoff = sizeof(struct ipv6hdr);
  589. return 1;
  590. }
  591. return -1;
  592. }
  593. /*
  594. * Creating outbound headers.
  595. *
  596. * "build" functions work when skb is filled from head to tail (datagram)
  597. * "push" functions work when headers are added from tail to head (tcp)
  598. *
  599. * In both cases we assume, that caller reserved enough room
  600. * for headers.
  601. */
  602. static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
  603. struct ipv6_rt_hdr *opt,
  604. struct in6_addr **addr_p)
  605. {
  606. struct rt0_hdr *phdr, *ihdr;
  607. int hops;
  608. ihdr = (struct rt0_hdr *) opt;
  609. phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
  610. memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
  611. hops = ihdr->rt_hdr.hdrlen >> 1;
  612. if (hops > 1)
  613. memcpy(phdr->addr, ihdr->addr + 1,
  614. (hops - 1) * sizeof(struct in6_addr));
  615. ipv6_addr_copy(phdr->addr + (hops - 1), *addr_p);
  616. *addr_p = ihdr->addr;
  617. phdr->rt_hdr.nexthdr = *proto;
  618. *proto = NEXTHDR_ROUTING;
  619. }
  620. static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
  621. {
  622. struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
  623. memcpy(h, opt, ipv6_optlen(opt));
  624. h->nexthdr = *proto;
  625. *proto = type;
  626. }
  627. void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  628. u8 *proto,
  629. struct in6_addr **daddr)
  630. {
  631. if (opt->srcrt) {
  632. ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
  633. /*
  634. * IPV6_RTHDRDSTOPTS is ignored
  635. * unless IPV6_RTHDR is set (RFC3542).
  636. */
  637. if (opt->dst0opt)
  638. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
  639. }
  640. if (opt->hopopt)
  641. ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
  642. }
  643. void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
  644. {
  645. if (opt->dst1opt)
  646. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
  647. }
  648. struct ipv6_txoptions *
  649. ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
  650. {
  651. struct ipv6_txoptions *opt2;
  652. opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
  653. if (opt2) {
  654. long dif = (char*)opt2 - (char*)opt;
  655. memcpy(opt2, opt, opt->tot_len);
  656. if (opt2->hopopt)
  657. *((char**)&opt2->hopopt) += dif;
  658. if (opt2->dst0opt)
  659. *((char**)&opt2->dst0opt) += dif;
  660. if (opt2->dst1opt)
  661. *((char**)&opt2->dst1opt) += dif;
  662. if (opt2->srcrt)
  663. *((char**)&opt2->srcrt) += dif;
  664. }
  665. return opt2;
  666. }
  667. EXPORT_SYMBOL_GPL(ipv6_dup_options);
  668. static int ipv6_renew_option(void *ohdr,
  669. struct ipv6_opt_hdr __user *newopt, int newoptlen,
  670. int inherit,
  671. struct ipv6_opt_hdr **hdr,
  672. char **p)
  673. {
  674. if (inherit) {
  675. if (ohdr) {
  676. memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
  677. *hdr = (struct ipv6_opt_hdr *)*p;
  678. *p += CMSG_ALIGN(ipv6_optlen(*(struct ipv6_opt_hdr **)hdr));
  679. }
  680. } else {
  681. if (newopt) {
  682. if (copy_from_user(*p, newopt, newoptlen))
  683. return -EFAULT;
  684. *hdr = (struct ipv6_opt_hdr *)*p;
  685. if (ipv6_optlen(*(struct ipv6_opt_hdr **)hdr) > newoptlen)
  686. return -EINVAL;
  687. *p += CMSG_ALIGN(newoptlen);
  688. }
  689. }
  690. return 0;
  691. }
  692. struct ipv6_txoptions *
  693. ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
  694. int newtype,
  695. struct ipv6_opt_hdr __user *newopt, int newoptlen)
  696. {
  697. int tot_len = 0;
  698. char *p;
  699. struct ipv6_txoptions *opt2;
  700. int err;
  701. if (opt) {
  702. if (newtype != IPV6_HOPOPTS && opt->hopopt)
  703. tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
  704. if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
  705. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
  706. if (newtype != IPV6_RTHDR && opt->srcrt)
  707. tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
  708. if (newtype != IPV6_DSTOPTS && opt->dst1opt)
  709. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
  710. }
  711. if (newopt && newoptlen)
  712. tot_len += CMSG_ALIGN(newoptlen);
  713. if (!tot_len)
  714. return NULL;
  715. tot_len += sizeof(*opt2);
  716. opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
  717. if (!opt2)
  718. return ERR_PTR(-ENOBUFS);
  719. memset(opt2, 0, tot_len);
  720. opt2->tot_len = tot_len;
  721. p = (char *)(opt2 + 1);
  722. err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
  723. newtype != IPV6_HOPOPTS,
  724. &opt2->hopopt, &p);
  725. if (err)
  726. goto out;
  727. err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
  728. newtype != IPV6_RTHDRDSTOPTS,
  729. &opt2->dst0opt, &p);
  730. if (err)
  731. goto out;
  732. err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
  733. newtype != IPV6_RTHDR,
  734. (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
  735. if (err)
  736. goto out;
  737. err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
  738. newtype != IPV6_DSTOPTS,
  739. &opt2->dst1opt, &p);
  740. if (err)
  741. goto out;
  742. opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
  743. (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
  744. (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
  745. opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
  746. return opt2;
  747. out:
  748. sock_kfree_s(sk, opt2, opt2->tot_len);
  749. return ERR_PTR(err);
  750. }
  751. struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  752. struct ipv6_txoptions *opt)
  753. {
  754. /*
  755. * ignore the dest before srcrt unless srcrt is being included.
  756. * --yoshfuji
  757. */
  758. if (opt && opt->dst0opt && !opt->srcrt) {
  759. if (opt_space != opt) {
  760. memcpy(opt_space, opt, sizeof(*opt_space));
  761. opt = opt_space;
  762. }
  763. opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
  764. opt->dst0opt = NULL;
  765. }
  766. return opt;
  767. }