exthdrs.c 22 KB

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