exthdrs.c 21 KB

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