exthdrs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * Extension Header handling for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Andi Kleen <ak@muc.de>
  8. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /* Changes:
  16. * yoshfuji : ensure not to overrun while parsing
  17. * tlv options.
  18. * Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs().
  19. * YOSHIFUJI Hideaki @USAGI Register inbound extension header
  20. * handlers as inet6_protocol{}.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/net.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/in6.h>
  29. #include <linux/icmpv6.h>
  30. #include <net/dst.h>
  31. #include <net/sock.h>
  32. #include <net/snmp.h>
  33. #include <net/ipv6.h>
  34. #include <net/protocol.h>
  35. #include <net/transp_v6.h>
  36. #include <net/rawv6.h>
  37. #include <net/ndisc.h>
  38. #include <net/ip6_route.h>
  39. #include <net/addrconf.h>
  40. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  41. #include <net/xfrm.h>
  42. #endif
  43. #include <asm/uaccess.h>
  44. int ipv6_find_tlv(struct sk_buff *skb, int offset, int type)
  45. {
  46. const unsigned char *nh = skb_network_header(skb);
  47. int packet_len = skb->tail - skb->network_header;
  48. struct ipv6_opt_hdr *hdr;
  49. int len;
  50. if (offset + 2 > packet_len)
  51. goto bad;
  52. hdr = (struct ipv6_opt_hdr *)(nh + 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 = nh[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 = nh[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. EXPORT_SYMBOL_GPL(ipv6_find_tlv);
  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 *skb, 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 *skb, int optoff)
  97. {
  98. switch ((skb_network_header(skb)[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(&ipv6_hdr(skb)->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 *skb)
  118. {
  119. struct tlvtype_proc *curr;
  120. const unsigned char *nh = skb_network_header(skb);
  121. int off = skb_network_header_len(skb);
  122. int len = (skb_transport_header(skb)[1] + 1) << 3;
  123. if (skb_transport_offset(skb) + len > skb_headlen(skb))
  124. goto bad;
  125. off += 2;
  126. len -= 2;
  127. while (len > 0) {
  128. int optlen = nh[off + 1] + 2;
  129. switch (nh[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 == nh[off]) {
  140. /* type specific length/alignment
  141. checks will be performed in the
  142. func(). */
  143. if (curr->func(skb, off) == 0)
  144. return 0;
  145. break;
  146. }
  147. }
  148. if (curr->type < 0) {
  149. if (ip6_tlvopt_unknown(skb, 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. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  167. static int ipv6_dest_hao(struct sk_buff *skb, int optoff)
  168. {
  169. struct ipv6_destopt_hao *hao;
  170. struct inet6_skb_parm *opt = IP6CB(skb);
  171. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  172. struct in6_addr tmp_addr;
  173. int ret;
  174. if (opt->dsthao) {
  175. LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n");
  176. goto discard;
  177. }
  178. opt->dsthao = opt->dst1;
  179. opt->dst1 = 0;
  180. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
  181. if (hao->length != 16) {
  182. LIMIT_NETDEBUG(
  183. KERN_DEBUG "hao invalid option length = %d\n", hao->length);
  184. goto discard;
  185. }
  186. if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
  187. LIMIT_NETDEBUG(
  188. KERN_DEBUG "hao is not an unicast addr: " NIP6_FMT "\n", NIP6(hao->addr));
  189. goto discard;
  190. }
  191. ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
  192. (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
  193. if (unlikely(ret < 0))
  194. goto discard;
  195. if (skb_cloned(skb)) {
  196. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  197. goto discard;
  198. /* update all variable using below by copied skbuff */
  199. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
  200. optoff);
  201. ipv6h = ipv6_hdr(skb);
  202. }
  203. if (skb->ip_summed == CHECKSUM_COMPLETE)
  204. skb->ip_summed = CHECKSUM_NONE;
  205. ipv6_addr_copy(&tmp_addr, &ipv6h->saddr);
  206. ipv6_addr_copy(&ipv6h->saddr, &hao->addr);
  207. ipv6_addr_copy(&hao->addr, &tmp_addr);
  208. if (skb->tstamp.tv64 == 0)
  209. __net_timestamp(skb);
  210. return 1;
  211. discard:
  212. kfree_skb(skb);
  213. return 0;
  214. }
  215. #endif
  216. static struct tlvtype_proc tlvprocdestopt_lst[] = {
  217. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  218. {
  219. .type = IPV6_TLV_HAO,
  220. .func = ipv6_dest_hao,
  221. },
  222. #endif
  223. {-1, NULL}
  224. };
  225. static int ipv6_destopt_rcv(struct sk_buff *skb)
  226. {
  227. struct inet6_skb_parm *opt = IP6CB(skb);
  228. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  229. __u16 dstbuf;
  230. #endif
  231. struct dst_entry *dst;
  232. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  233. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  234. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  235. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  236. IPSTATS_MIB_INHDRERRORS);
  237. kfree_skb(skb);
  238. return -1;
  239. }
  240. opt->lastopt = opt->dst1 = skb_network_header_len(skb);
  241. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  242. dstbuf = opt->dst1;
  243. #endif
  244. dst = dst_clone(skb->dst);
  245. if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
  246. dst_release(dst);
  247. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  248. opt = IP6CB(skb);
  249. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  250. opt->nhoff = dstbuf;
  251. #else
  252. opt->nhoff = opt->dst1;
  253. #endif
  254. return 1;
  255. }
  256. IP6_INC_STATS_BH(ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
  257. dst_release(dst);
  258. return -1;
  259. }
  260. /********************************
  261. Routing header.
  262. ********************************/
  263. static int ipv6_rthdr_rcv(struct sk_buff *skb)
  264. {
  265. struct inet6_skb_parm *opt = IP6CB(skb);
  266. struct in6_addr *addr = NULL;
  267. struct in6_addr daddr;
  268. struct inet6_dev *idev;
  269. int n, i;
  270. struct ipv6_rt_hdr *hdr;
  271. struct rt0_hdr *rthdr;
  272. int accept_source_route = dev_net(skb->dev)->ipv6.devconf_all->accept_source_route;
  273. idev = in6_dev_get(skb->dev);
  274. if (idev) {
  275. if (accept_source_route > idev->cnf.accept_source_route)
  276. accept_source_route = idev->cnf.accept_source_route;
  277. in6_dev_put(idev);
  278. }
  279. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  280. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  281. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  282. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  283. IPSTATS_MIB_INHDRERRORS);
  284. kfree_skb(skb);
  285. return -1;
  286. }
  287. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  288. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
  289. skb->pkt_type != PACKET_HOST) {
  290. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  291. IPSTATS_MIB_INADDRERRORS);
  292. kfree_skb(skb);
  293. return -1;
  294. }
  295. looped_back:
  296. if (hdr->segments_left == 0) {
  297. switch (hdr->type) {
  298. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  299. case IPV6_SRCRT_TYPE_2:
  300. /* Silently discard type 2 header unless it was
  301. * processed by own
  302. */
  303. if (!addr) {
  304. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  305. IPSTATS_MIB_INADDRERRORS);
  306. kfree_skb(skb);
  307. return -1;
  308. }
  309. break;
  310. #endif
  311. default:
  312. break;
  313. }
  314. opt->lastopt = opt->srcrt = skb_network_header_len(skb);
  315. skb->transport_header += (hdr->hdrlen + 1) << 3;
  316. opt->dst0 = opt->dst1;
  317. opt->dst1 = 0;
  318. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  319. return 1;
  320. }
  321. switch (hdr->type) {
  322. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  323. case IPV6_SRCRT_TYPE_2:
  324. if (accept_source_route < 0)
  325. goto unknown_rh;
  326. /* Silently discard invalid RTH type 2 */
  327. if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
  328. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  329. IPSTATS_MIB_INHDRERRORS);
  330. kfree_skb(skb);
  331. return -1;
  332. }
  333. break;
  334. #endif
  335. default:
  336. goto unknown_rh;
  337. }
  338. /*
  339. * This is the routing header forwarding algorithm from
  340. * RFC 2460, page 16.
  341. */
  342. n = hdr->hdrlen >> 1;
  343. if (hdr->segments_left > n) {
  344. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  345. IPSTATS_MIB_INHDRERRORS);
  346. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  347. ((&hdr->segments_left) -
  348. skb_network_header(skb)));
  349. return -1;
  350. }
  351. /* We are about to mangle packet header. Be careful!
  352. Do not damage packets queued somewhere.
  353. */
  354. if (skb_cloned(skb)) {
  355. /* the copy is a forwarded packet */
  356. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  357. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  358. IPSTATS_MIB_OUTDISCARDS);
  359. kfree_skb(skb);
  360. return -1;
  361. }
  362. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  363. }
  364. if (skb->ip_summed == CHECKSUM_COMPLETE)
  365. skb->ip_summed = CHECKSUM_NONE;
  366. i = n - --hdr->segments_left;
  367. rthdr = (struct rt0_hdr *) hdr;
  368. addr = rthdr->addr;
  369. addr += i - 1;
  370. switch (hdr->type) {
  371. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  372. case IPV6_SRCRT_TYPE_2:
  373. if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
  374. (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  375. IPPROTO_ROUTING) < 0) {
  376. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  377. IPSTATS_MIB_INADDRERRORS);
  378. kfree_skb(skb);
  379. return -1;
  380. }
  381. if (!ipv6_chk_home_addr(dev_net(skb->dst->dev), addr)) {
  382. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  383. IPSTATS_MIB_INADDRERRORS);
  384. kfree_skb(skb);
  385. return -1;
  386. }
  387. break;
  388. #endif
  389. default:
  390. break;
  391. }
  392. if (ipv6_addr_is_multicast(addr)) {
  393. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  394. IPSTATS_MIB_INADDRERRORS);
  395. kfree_skb(skb);
  396. return -1;
  397. }
  398. ipv6_addr_copy(&daddr, addr);
  399. ipv6_addr_copy(addr, &ipv6_hdr(skb)->daddr);
  400. ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &daddr);
  401. dst_release(xchg(&skb->dst, NULL));
  402. ip6_route_input(skb);
  403. if (skb->dst->error) {
  404. skb_push(skb, skb->data - skb_network_header(skb));
  405. dst_input(skb);
  406. return -1;
  407. }
  408. if (skb->dst->dev->flags&IFF_LOOPBACK) {
  409. if (ipv6_hdr(skb)->hop_limit <= 1) {
  410. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
  411. IPSTATS_MIB_INHDRERRORS);
  412. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  413. 0, skb->dev);
  414. kfree_skb(skb);
  415. return -1;
  416. }
  417. ipv6_hdr(skb)->hop_limit--;
  418. goto looped_back;
  419. }
  420. skb_push(skb, skb->data - skb_network_header(skb));
  421. dst_input(skb);
  422. return -1;
  423. unknown_rh:
  424. IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
  425. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  426. (&hdr->type) - skb_network_header(skb));
  427. return -1;
  428. }
  429. static struct inet6_protocol rthdr_protocol = {
  430. .handler = ipv6_rthdr_rcv,
  431. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
  432. };
  433. static struct inet6_protocol destopt_protocol = {
  434. .handler = ipv6_destopt_rcv,
  435. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR,
  436. };
  437. static struct inet6_protocol nodata_protocol = {
  438. .handler = dst_discard,
  439. .flags = INET6_PROTO_NOPOLICY,
  440. };
  441. int __init ipv6_exthdrs_init(void)
  442. {
  443. int ret;
  444. ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  445. if (ret)
  446. goto out;
  447. ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  448. if (ret)
  449. goto out_rthdr;
  450. ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
  451. if (ret)
  452. goto out_destopt;
  453. out:
  454. return ret;
  455. out_rthdr:
  456. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  457. out_destopt:
  458. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  459. goto out;
  460. };
  461. void ipv6_exthdrs_exit(void)
  462. {
  463. inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
  464. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  465. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  466. }
  467. /**********************************
  468. Hop-by-hop options.
  469. **********************************/
  470. /*
  471. * Note: we cannot rely on skb->dst before we assign it in ip6_route_input().
  472. */
  473. static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
  474. {
  475. return skb->dst ? ip6_dst_idev(skb->dst) : __in6_dev_get(skb->dev);
  476. }
  477. /* Router Alert as of RFC 2711 */
  478. static int ipv6_hop_ra(struct sk_buff *skb, int optoff)
  479. {
  480. const unsigned char *nh = skb_network_header(skb);
  481. if (nh[optoff + 1] == 2) {
  482. IP6CB(skb)->ra = optoff;
  483. return 1;
  484. }
  485. LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n",
  486. nh[optoff + 1]);
  487. kfree_skb(skb);
  488. return 0;
  489. }
  490. /* Jumbo payload */
  491. static int ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
  492. {
  493. const unsigned char *nh = skb_network_header(skb);
  494. u32 pkt_len;
  495. if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
  496. LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
  497. nh[optoff+1]);
  498. IP6_INC_STATS_BH(ipv6_skb_idev(skb),
  499. IPSTATS_MIB_INHDRERRORS);
  500. goto drop;
  501. }
  502. pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
  503. if (pkt_len <= IPV6_MAXPLEN) {
  504. IP6_INC_STATS_BH(ipv6_skb_idev(skb), IPSTATS_MIB_INHDRERRORS);
  505. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
  506. return 0;
  507. }
  508. if (ipv6_hdr(skb)->payload_len) {
  509. IP6_INC_STATS_BH(ipv6_skb_idev(skb), IPSTATS_MIB_INHDRERRORS);
  510. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
  511. return 0;
  512. }
  513. if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
  514. IP6_INC_STATS_BH(ipv6_skb_idev(skb), IPSTATS_MIB_INTRUNCATEDPKTS);
  515. goto drop;
  516. }
  517. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  518. goto drop;
  519. return 1;
  520. drop:
  521. kfree_skb(skb);
  522. return 0;
  523. }
  524. static struct tlvtype_proc tlvprochopopt_lst[] = {
  525. {
  526. .type = IPV6_TLV_ROUTERALERT,
  527. .func = ipv6_hop_ra,
  528. },
  529. {
  530. .type = IPV6_TLV_JUMBO,
  531. .func = ipv6_hop_jumbo,
  532. },
  533. { -1, }
  534. };
  535. int ipv6_parse_hopopts(struct sk_buff *skb)
  536. {
  537. struct inet6_skb_parm *opt = IP6CB(skb);
  538. /*
  539. * skb_network_header(skb) is equal to skb->data, and
  540. * skb_network_header_len(skb) is always equal to
  541. * sizeof(struct ipv6hdr) by definition of
  542. * hop-by-hop options.
  543. */
  544. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
  545. !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
  546. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  547. kfree_skb(skb);
  548. return -1;
  549. }
  550. opt->hop = sizeof(struct ipv6hdr);
  551. if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
  552. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  553. opt = IP6CB(skb);
  554. opt->nhoff = sizeof(struct ipv6hdr);
  555. return 1;
  556. }
  557. return -1;
  558. }
  559. /*
  560. * Creating outbound headers.
  561. *
  562. * "build" functions work when skb is filled from head to tail (datagram)
  563. * "push" functions work when headers are added from tail to head (tcp)
  564. *
  565. * In both cases we assume, that caller reserved enough room
  566. * for headers.
  567. */
  568. static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
  569. struct ipv6_rt_hdr *opt,
  570. struct in6_addr **addr_p)
  571. {
  572. struct rt0_hdr *phdr, *ihdr;
  573. int hops;
  574. ihdr = (struct rt0_hdr *) opt;
  575. phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
  576. memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
  577. hops = ihdr->rt_hdr.hdrlen >> 1;
  578. if (hops > 1)
  579. memcpy(phdr->addr, ihdr->addr + 1,
  580. (hops - 1) * sizeof(struct in6_addr));
  581. ipv6_addr_copy(phdr->addr + (hops - 1), *addr_p);
  582. *addr_p = ihdr->addr;
  583. phdr->rt_hdr.nexthdr = *proto;
  584. *proto = NEXTHDR_ROUTING;
  585. }
  586. static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
  587. {
  588. struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
  589. memcpy(h, opt, ipv6_optlen(opt));
  590. h->nexthdr = *proto;
  591. *proto = type;
  592. }
  593. void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  594. u8 *proto,
  595. struct in6_addr **daddr)
  596. {
  597. if (opt->srcrt) {
  598. ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
  599. /*
  600. * IPV6_RTHDRDSTOPTS is ignored
  601. * unless IPV6_RTHDR is set (RFC3542).
  602. */
  603. if (opt->dst0opt)
  604. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
  605. }
  606. if (opt->hopopt)
  607. ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
  608. }
  609. EXPORT_SYMBOL(ipv6_push_nfrag_opts);
  610. void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
  611. {
  612. if (opt->dst1opt)
  613. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
  614. }
  615. struct ipv6_txoptions *
  616. ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
  617. {
  618. struct ipv6_txoptions *opt2;
  619. opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
  620. if (opt2) {
  621. long dif = (char*)opt2 - (char*)opt;
  622. memcpy(opt2, opt, opt->tot_len);
  623. if (opt2->hopopt)
  624. *((char**)&opt2->hopopt) += dif;
  625. if (opt2->dst0opt)
  626. *((char**)&opt2->dst0opt) += dif;
  627. if (opt2->dst1opt)
  628. *((char**)&opt2->dst1opt) += dif;
  629. if (opt2->srcrt)
  630. *((char**)&opt2->srcrt) += dif;
  631. }
  632. return opt2;
  633. }
  634. EXPORT_SYMBOL_GPL(ipv6_dup_options);
  635. static int ipv6_renew_option(void *ohdr,
  636. struct ipv6_opt_hdr __user *newopt, int newoptlen,
  637. int inherit,
  638. struct ipv6_opt_hdr **hdr,
  639. char **p)
  640. {
  641. if (inherit) {
  642. if (ohdr) {
  643. memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
  644. *hdr = (struct ipv6_opt_hdr *)*p;
  645. *p += CMSG_ALIGN(ipv6_optlen(*(struct ipv6_opt_hdr **)hdr));
  646. }
  647. } else {
  648. if (newopt) {
  649. if (copy_from_user(*p, newopt, newoptlen))
  650. return -EFAULT;
  651. *hdr = (struct ipv6_opt_hdr *)*p;
  652. if (ipv6_optlen(*(struct ipv6_opt_hdr **)hdr) > newoptlen)
  653. return -EINVAL;
  654. *p += CMSG_ALIGN(newoptlen);
  655. }
  656. }
  657. return 0;
  658. }
  659. struct ipv6_txoptions *
  660. ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
  661. int newtype,
  662. struct ipv6_opt_hdr __user *newopt, int newoptlen)
  663. {
  664. int tot_len = 0;
  665. char *p;
  666. struct ipv6_txoptions *opt2;
  667. int err;
  668. if (opt) {
  669. if (newtype != IPV6_HOPOPTS && opt->hopopt)
  670. tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
  671. if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
  672. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
  673. if (newtype != IPV6_RTHDR && opt->srcrt)
  674. tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
  675. if (newtype != IPV6_DSTOPTS && opt->dst1opt)
  676. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
  677. }
  678. if (newopt && newoptlen)
  679. tot_len += CMSG_ALIGN(newoptlen);
  680. if (!tot_len)
  681. return NULL;
  682. tot_len += sizeof(*opt2);
  683. opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
  684. if (!opt2)
  685. return ERR_PTR(-ENOBUFS);
  686. memset(opt2, 0, tot_len);
  687. opt2->tot_len = tot_len;
  688. p = (char *)(opt2 + 1);
  689. err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
  690. newtype != IPV6_HOPOPTS,
  691. &opt2->hopopt, &p);
  692. if (err)
  693. goto out;
  694. err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
  695. newtype != IPV6_RTHDRDSTOPTS,
  696. &opt2->dst0opt, &p);
  697. if (err)
  698. goto out;
  699. err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
  700. newtype != IPV6_RTHDR,
  701. (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
  702. if (err)
  703. goto out;
  704. err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
  705. newtype != IPV6_DSTOPTS,
  706. &opt2->dst1opt, &p);
  707. if (err)
  708. goto out;
  709. opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
  710. (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
  711. (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
  712. opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
  713. return opt2;
  714. out:
  715. sock_kfree_s(sk, opt2, opt2->tot_len);
  716. return ERR_PTR(err);
  717. }
  718. struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  719. struct ipv6_txoptions *opt)
  720. {
  721. /*
  722. * ignore the dest before srcrt unless srcrt is being included.
  723. * --yoshfuji
  724. */
  725. if (opt && opt->dst0opt && !opt->srcrt) {
  726. if (opt_space != opt) {
  727. memcpy(opt_space, opt, sizeof(*opt_space));
  728. opt = opt_space;
  729. }
  730. opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
  731. opt->dst0opt = NULL;
  732. }
  733. return opt;
  734. }