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. * 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 <linux/slab.h>
  31. #include <linux/export.h>
  32. #include <net/dst.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. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  43. #include <net/xfrm.h>
  44. #endif
  45. #include <asm/uaccess.h>
  46. #include "ip6_offload.h"
  47. /*
  48. * Parsing tlv encoded headers.
  49. *
  50. * Parsing function "func" returns true, if parsing succeed
  51. * and false, if it failed.
  52. * It MUST NOT touch skb->h.
  53. */
  54. struct tlvtype_proc {
  55. int type;
  56. bool (*func)(struct sk_buff *skb, int offset);
  57. };
  58. /*********************
  59. Generic functions
  60. *********************/
  61. /* An unknown option is detected, decide what to do */
  62. static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
  63. {
  64. switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) {
  65. case 0: /* ignore */
  66. return true;
  67. case 1: /* drop packet */
  68. break;
  69. case 3: /* Send ICMP if not a multicast address and drop packet */
  70. /* Actually, it is redundant check. icmp_send
  71. will recheck in any case.
  72. */
  73. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr))
  74. break;
  75. case 2: /* send ICMP PARM PROB regardless and drop packet */
  76. icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff);
  77. return false;
  78. }
  79. kfree_skb(skb);
  80. return false;
  81. }
  82. /* Parse tlv encoded option header (hop-by-hop or destination) */
  83. static bool ip6_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb)
  84. {
  85. const struct tlvtype_proc *curr;
  86. const unsigned char *nh = skb_network_header(skb);
  87. int off = skb_network_header_len(skb);
  88. int len = (skb_transport_header(skb)[1] + 1) << 3;
  89. int padlen = 0;
  90. if (skb_transport_offset(skb) + len > skb_headlen(skb))
  91. goto bad;
  92. off += 2;
  93. len -= 2;
  94. while (len > 0) {
  95. int optlen = nh[off + 1] + 2;
  96. int i;
  97. switch (nh[off]) {
  98. case IPV6_TLV_PAD1:
  99. optlen = 1;
  100. padlen++;
  101. if (padlen > 7)
  102. goto bad;
  103. break;
  104. case IPV6_TLV_PADN:
  105. /* RFC 2460 states that the purpose of PadN is
  106. * to align the containing header to multiples
  107. * of 8. 7 is therefore the highest valid value.
  108. * See also RFC 4942, Section 2.1.9.5.
  109. */
  110. padlen += optlen;
  111. if (padlen > 7)
  112. goto bad;
  113. /* RFC 4942 recommends receiving hosts to
  114. * actively check PadN payload to contain
  115. * only zeroes.
  116. */
  117. for (i = 2; i < optlen; i++) {
  118. if (nh[off + i] != 0)
  119. goto bad;
  120. }
  121. break;
  122. default: /* Other TLV code so scan list */
  123. if (optlen > len)
  124. goto bad;
  125. for (curr=procs; curr->type >= 0; curr++) {
  126. if (curr->type == nh[off]) {
  127. /* type specific length/alignment
  128. checks will be performed in the
  129. func(). */
  130. if (curr->func(skb, off) == false)
  131. return false;
  132. break;
  133. }
  134. }
  135. if (curr->type < 0) {
  136. if (ip6_tlvopt_unknown(skb, off) == 0)
  137. return false;
  138. }
  139. padlen = 0;
  140. break;
  141. }
  142. off += optlen;
  143. len -= optlen;
  144. }
  145. /* This case will not be caught by above check since its padding
  146. * length is smaller than 7:
  147. * 1 byte NH + 1 byte Length + 6 bytes Padding
  148. */
  149. if ((padlen == 6) && ((off - skb_network_header_len(skb)) == 8))
  150. goto bad;
  151. if (len == 0)
  152. return true;
  153. bad:
  154. kfree_skb(skb);
  155. return false;
  156. }
  157. /*****************************
  158. Destination options header.
  159. *****************************/
  160. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  161. static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
  162. {
  163. struct ipv6_destopt_hao *hao;
  164. struct inet6_skb_parm *opt = IP6CB(skb);
  165. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  166. struct in6_addr tmp_addr;
  167. int ret;
  168. if (opt->dsthao) {
  169. LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n");
  170. goto discard;
  171. }
  172. opt->dsthao = opt->dst1;
  173. opt->dst1 = 0;
  174. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
  175. if (hao->length != 16) {
  176. LIMIT_NETDEBUG(
  177. KERN_DEBUG "hao invalid option length = %d\n", hao->length);
  178. goto discard;
  179. }
  180. if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
  181. LIMIT_NETDEBUG(
  182. KERN_DEBUG "hao is not an unicast addr: %pI6\n", &hao->addr);
  183. goto discard;
  184. }
  185. ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
  186. (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
  187. if (unlikely(ret < 0))
  188. goto discard;
  189. if (skb_cloned(skb)) {
  190. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  191. goto discard;
  192. /* update all variable using below by copied skbuff */
  193. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
  194. optoff);
  195. ipv6h = ipv6_hdr(skb);
  196. }
  197. if (skb->ip_summed == CHECKSUM_COMPLETE)
  198. skb->ip_summed = CHECKSUM_NONE;
  199. tmp_addr = ipv6h->saddr;
  200. ipv6h->saddr = hao->addr;
  201. hao->addr = tmp_addr;
  202. if (skb->tstamp.tv64 == 0)
  203. __net_timestamp(skb);
  204. return true;
  205. discard:
  206. kfree_skb(skb);
  207. return false;
  208. }
  209. #endif
  210. static const struct tlvtype_proc tlvprocdestopt_lst[] = {
  211. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  212. {
  213. .type = IPV6_TLV_HAO,
  214. .func = ipv6_dest_hao,
  215. },
  216. #endif
  217. {-1, NULL}
  218. };
  219. static int ipv6_destopt_rcv(struct sk_buff *skb)
  220. {
  221. struct inet6_skb_parm *opt = IP6CB(skb);
  222. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  223. __u16 dstbuf;
  224. #endif
  225. struct dst_entry *dst = skb_dst(skb);
  226. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  227. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  228. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  229. IP6_INC_STATS_BH(dev_net(dst->dev), ip6_dst_idev(dst),
  230. IPSTATS_MIB_INHDRERRORS);
  231. kfree_skb(skb);
  232. return -1;
  233. }
  234. opt->lastopt = opt->dst1 = skb_network_header_len(skb);
  235. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  236. dstbuf = opt->dst1;
  237. #endif
  238. if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
  239. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  240. opt = IP6CB(skb);
  241. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  242. opt->nhoff = dstbuf;
  243. #else
  244. opt->nhoff = opt->dst1;
  245. #endif
  246. return 1;
  247. }
  248. IP6_INC_STATS_BH(dev_net(dst->dev),
  249. ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
  250. return -1;
  251. }
  252. /********************************
  253. Routing header.
  254. ********************************/
  255. /* called with rcu_read_lock() */
  256. static int ipv6_rthdr_rcv(struct sk_buff *skb)
  257. {
  258. struct inet6_skb_parm *opt = IP6CB(skb);
  259. struct in6_addr *addr = NULL;
  260. struct in6_addr daddr;
  261. struct inet6_dev *idev;
  262. int n, i;
  263. struct ipv6_rt_hdr *hdr;
  264. struct rt0_hdr *rthdr;
  265. struct net *net = dev_net(skb->dev);
  266. int accept_source_route = net->ipv6.devconf_all->accept_source_route;
  267. idev = __in6_dev_get(skb->dev);
  268. if (idev && accept_source_route > idev->cnf.accept_source_route)
  269. accept_source_route = idev->cnf.accept_source_route;
  270. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  271. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  272. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  273. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  274. IPSTATS_MIB_INHDRERRORS);
  275. kfree_skb(skb);
  276. return -1;
  277. }
  278. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  279. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
  280. skb->pkt_type != PACKET_HOST) {
  281. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  282. IPSTATS_MIB_INADDRERRORS);
  283. kfree_skb(skb);
  284. return -1;
  285. }
  286. looped_back:
  287. if (hdr->segments_left == 0) {
  288. switch (hdr->type) {
  289. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  290. case IPV6_SRCRT_TYPE_2:
  291. /* Silently discard type 2 header unless it was
  292. * processed by own
  293. */
  294. if (!addr) {
  295. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  296. IPSTATS_MIB_INADDRERRORS);
  297. kfree_skb(skb);
  298. return -1;
  299. }
  300. break;
  301. #endif
  302. default:
  303. break;
  304. }
  305. opt->lastopt = opt->srcrt = skb_network_header_len(skb);
  306. skb->transport_header += (hdr->hdrlen + 1) << 3;
  307. opt->dst0 = opt->dst1;
  308. opt->dst1 = 0;
  309. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  310. return 1;
  311. }
  312. switch (hdr->type) {
  313. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  314. case IPV6_SRCRT_TYPE_2:
  315. if (accept_source_route < 0)
  316. goto unknown_rh;
  317. /* Silently discard invalid RTH type 2 */
  318. if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
  319. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  320. IPSTATS_MIB_INHDRERRORS);
  321. kfree_skb(skb);
  322. return -1;
  323. }
  324. break;
  325. #endif
  326. default:
  327. goto unknown_rh;
  328. }
  329. /*
  330. * This is the routing header forwarding algorithm from
  331. * RFC 2460, page 16.
  332. */
  333. n = hdr->hdrlen >> 1;
  334. if (hdr->segments_left > n) {
  335. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  336. IPSTATS_MIB_INHDRERRORS);
  337. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  338. ((&hdr->segments_left) -
  339. skb_network_header(skb)));
  340. return -1;
  341. }
  342. /* We are about to mangle packet header. Be careful!
  343. Do not damage packets queued somewhere.
  344. */
  345. if (skb_cloned(skb)) {
  346. /* the copy is a forwarded packet */
  347. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  348. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  349. IPSTATS_MIB_OUTDISCARDS);
  350. kfree_skb(skb);
  351. return -1;
  352. }
  353. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  354. }
  355. if (skb->ip_summed == CHECKSUM_COMPLETE)
  356. skb->ip_summed = CHECKSUM_NONE;
  357. i = n - --hdr->segments_left;
  358. rthdr = (struct rt0_hdr *) hdr;
  359. addr = rthdr->addr;
  360. addr += i - 1;
  361. switch (hdr->type) {
  362. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  363. case IPV6_SRCRT_TYPE_2:
  364. if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
  365. (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  366. IPPROTO_ROUTING) < 0) {
  367. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  368. IPSTATS_MIB_INADDRERRORS);
  369. kfree_skb(skb);
  370. return -1;
  371. }
  372. if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
  373. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  374. IPSTATS_MIB_INADDRERRORS);
  375. kfree_skb(skb);
  376. return -1;
  377. }
  378. break;
  379. #endif
  380. default:
  381. break;
  382. }
  383. if (ipv6_addr_is_multicast(addr)) {
  384. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  385. IPSTATS_MIB_INADDRERRORS);
  386. kfree_skb(skb);
  387. return -1;
  388. }
  389. daddr = *addr;
  390. *addr = ipv6_hdr(skb)->daddr;
  391. ipv6_hdr(skb)->daddr = daddr;
  392. skb_dst_drop(skb);
  393. ip6_route_input(skb);
  394. if (skb_dst(skb)->error) {
  395. skb_push(skb, skb->data - skb_network_header(skb));
  396. dst_input(skb);
  397. return -1;
  398. }
  399. if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
  400. if (ipv6_hdr(skb)->hop_limit <= 1) {
  401. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  402. IPSTATS_MIB_INHDRERRORS);
  403. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  404. 0);
  405. kfree_skb(skb);
  406. return -1;
  407. }
  408. ipv6_hdr(skb)->hop_limit--;
  409. goto looped_back;
  410. }
  411. skb_push(skb, skb->data - skb_network_header(skb));
  412. dst_input(skb);
  413. return -1;
  414. unknown_rh:
  415. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
  416. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  417. (&hdr->type) - skb_network_header(skb));
  418. return -1;
  419. }
  420. static const struct inet6_protocol rthdr_protocol = {
  421. .handler = ipv6_rthdr_rcv,
  422. .flags = INET6_PROTO_NOPOLICY,
  423. };
  424. static const struct inet6_protocol destopt_protocol = {
  425. .handler = ipv6_destopt_rcv,
  426. .flags = INET6_PROTO_NOPOLICY,
  427. };
  428. static const struct inet6_protocol nodata_protocol = {
  429. .handler = dst_discard,
  430. .flags = INET6_PROTO_NOPOLICY,
  431. };
  432. int __init ipv6_exthdrs_init(void)
  433. {
  434. int ret;
  435. ret = ipv6_exthdrs_offload_init();
  436. if (ret)
  437. goto out;
  438. ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  439. if (ret)
  440. goto out_offload;
  441. ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  442. if (ret)
  443. goto out_rthdr;
  444. ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
  445. if (ret)
  446. goto out_destopt;
  447. out:
  448. return ret;
  449. out_destopt:
  450. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  451. out_rthdr:
  452. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  453. out_offload:
  454. ipv6_exthdrs_offload_exit();
  455. goto out;
  456. };
  457. void ipv6_exthdrs_exit(void)
  458. {
  459. ipv6_exthdrs_offload_exit();
  460. inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
  461. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  462. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  463. }
  464. /**********************************
  465. Hop-by-hop options.
  466. **********************************/
  467. /*
  468. * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
  469. */
  470. static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
  471. {
  472. return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
  473. }
  474. static inline struct net *ipv6_skb_net(struct sk_buff *skb)
  475. {
  476. return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
  477. }
  478. /* Router Alert as of RFC 2711 */
  479. static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
  480. {
  481. const unsigned char *nh = skb_network_header(skb);
  482. if (nh[optoff + 1] == 2) {
  483. IP6CB(skb)->ra = optoff;
  484. return true;
  485. }
  486. LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n",
  487. nh[optoff + 1]);
  488. kfree_skb(skb);
  489. return false;
  490. }
  491. /* Jumbo payload */
  492. static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
  493. {
  494. const unsigned char *nh = skb_network_header(skb);
  495. struct net *net = ipv6_skb_net(skb);
  496. u32 pkt_len;
  497. if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
  498. LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
  499. nh[optoff+1]);
  500. IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
  501. IPSTATS_MIB_INHDRERRORS);
  502. goto drop;
  503. }
  504. pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
  505. if (pkt_len <= IPV6_MAXPLEN) {
  506. IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
  507. IPSTATS_MIB_INHDRERRORS);
  508. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
  509. return false;
  510. }
  511. if (ipv6_hdr(skb)->payload_len) {
  512. IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
  513. IPSTATS_MIB_INHDRERRORS);
  514. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
  515. return false;
  516. }
  517. if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
  518. IP6_INC_STATS_BH(net, ipv6_skb_idev(skb),
  519. IPSTATS_MIB_INTRUNCATEDPKTS);
  520. goto drop;
  521. }
  522. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  523. goto drop;
  524. return true;
  525. drop:
  526. kfree_skb(skb);
  527. return false;
  528. }
  529. static const struct tlvtype_proc tlvprochopopt_lst[] = {
  530. {
  531. .type = IPV6_TLV_ROUTERALERT,
  532. .func = ipv6_hop_ra,
  533. },
  534. {
  535. .type = IPV6_TLV_JUMBO,
  536. .func = ipv6_hop_jumbo,
  537. },
  538. { -1, }
  539. };
  540. int ipv6_parse_hopopts(struct sk_buff *skb)
  541. {
  542. struct inet6_skb_parm *opt = IP6CB(skb);
  543. /*
  544. * skb_network_header(skb) is equal to skb->data, and
  545. * skb_network_header_len(skb) is always equal to
  546. * sizeof(struct ipv6hdr) by definition of
  547. * hop-by-hop options.
  548. */
  549. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
  550. !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
  551. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  552. kfree_skb(skb);
  553. return -1;
  554. }
  555. opt->hop = sizeof(struct ipv6hdr);
  556. if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
  557. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  558. opt = IP6CB(skb);
  559. opt->nhoff = sizeof(struct ipv6hdr);
  560. return 1;
  561. }
  562. return -1;
  563. }
  564. /*
  565. * Creating outbound headers.
  566. *
  567. * "build" functions work when skb is filled from head to tail (datagram)
  568. * "push" functions work when headers are added from tail to head (tcp)
  569. *
  570. * In both cases we assume, that caller reserved enough room
  571. * for headers.
  572. */
  573. static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
  574. struct ipv6_rt_hdr *opt,
  575. struct in6_addr **addr_p)
  576. {
  577. struct rt0_hdr *phdr, *ihdr;
  578. int hops;
  579. ihdr = (struct rt0_hdr *) opt;
  580. phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
  581. memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
  582. hops = ihdr->rt_hdr.hdrlen >> 1;
  583. if (hops > 1)
  584. memcpy(phdr->addr, ihdr->addr + 1,
  585. (hops - 1) * sizeof(struct in6_addr));
  586. phdr->addr[hops - 1] = **addr_p;
  587. *addr_p = ihdr->addr;
  588. phdr->rt_hdr.nexthdr = *proto;
  589. *proto = NEXTHDR_ROUTING;
  590. }
  591. static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
  592. {
  593. struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
  594. memcpy(h, opt, ipv6_optlen(opt));
  595. h->nexthdr = *proto;
  596. *proto = type;
  597. }
  598. void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  599. u8 *proto,
  600. struct in6_addr **daddr)
  601. {
  602. if (opt->srcrt) {
  603. ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
  604. /*
  605. * IPV6_RTHDRDSTOPTS is ignored
  606. * unless IPV6_RTHDR is set (RFC3542).
  607. */
  608. if (opt->dst0opt)
  609. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
  610. }
  611. if (opt->hopopt)
  612. ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
  613. }
  614. EXPORT_SYMBOL(ipv6_push_nfrag_opts);
  615. void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
  616. {
  617. if (opt->dst1opt)
  618. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
  619. }
  620. struct ipv6_txoptions *
  621. ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
  622. {
  623. struct ipv6_txoptions *opt2;
  624. opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
  625. if (opt2) {
  626. long dif = (char *)opt2 - (char *)opt;
  627. memcpy(opt2, opt, opt->tot_len);
  628. if (opt2->hopopt)
  629. *((char **)&opt2->hopopt) += dif;
  630. if (opt2->dst0opt)
  631. *((char **)&opt2->dst0opt) += dif;
  632. if (opt2->dst1opt)
  633. *((char **)&opt2->dst1opt) += dif;
  634. if (opt2->srcrt)
  635. *((char **)&opt2->srcrt) += dif;
  636. }
  637. return opt2;
  638. }
  639. EXPORT_SYMBOL_GPL(ipv6_dup_options);
  640. static int ipv6_renew_option(void *ohdr,
  641. struct ipv6_opt_hdr __user *newopt, int newoptlen,
  642. int inherit,
  643. struct ipv6_opt_hdr **hdr,
  644. char **p)
  645. {
  646. if (inherit) {
  647. if (ohdr) {
  648. memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
  649. *hdr = (struct ipv6_opt_hdr *)*p;
  650. *p += CMSG_ALIGN(ipv6_optlen(*hdr));
  651. }
  652. } else {
  653. if (newopt) {
  654. if (copy_from_user(*p, newopt, newoptlen))
  655. return -EFAULT;
  656. *hdr = (struct ipv6_opt_hdr *)*p;
  657. if (ipv6_optlen(*hdr) > newoptlen)
  658. return -EINVAL;
  659. *p += CMSG_ALIGN(newoptlen);
  660. }
  661. }
  662. return 0;
  663. }
  664. struct ipv6_txoptions *
  665. ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
  666. int newtype,
  667. struct ipv6_opt_hdr __user *newopt, int newoptlen)
  668. {
  669. int tot_len = 0;
  670. char *p;
  671. struct ipv6_txoptions *opt2;
  672. int err;
  673. if (opt) {
  674. if (newtype != IPV6_HOPOPTS && opt->hopopt)
  675. tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
  676. if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
  677. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
  678. if (newtype != IPV6_RTHDR && opt->srcrt)
  679. tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
  680. if (newtype != IPV6_DSTOPTS && opt->dst1opt)
  681. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
  682. }
  683. if (newopt && newoptlen)
  684. tot_len += CMSG_ALIGN(newoptlen);
  685. if (!tot_len)
  686. return NULL;
  687. tot_len += sizeof(*opt2);
  688. opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
  689. if (!opt2)
  690. return ERR_PTR(-ENOBUFS);
  691. memset(opt2, 0, tot_len);
  692. opt2->tot_len = tot_len;
  693. p = (char *)(opt2 + 1);
  694. err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
  695. newtype != IPV6_HOPOPTS,
  696. &opt2->hopopt, &p);
  697. if (err)
  698. goto out;
  699. err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
  700. newtype != IPV6_RTHDRDSTOPTS,
  701. &opt2->dst0opt, &p);
  702. if (err)
  703. goto out;
  704. err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
  705. newtype != IPV6_RTHDR,
  706. (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
  707. if (err)
  708. goto out;
  709. err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
  710. newtype != IPV6_DSTOPTS,
  711. &opt2->dst1opt, &p);
  712. if (err)
  713. goto out;
  714. opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
  715. (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
  716. (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
  717. opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
  718. return opt2;
  719. out:
  720. sock_kfree_s(sk, opt2, opt2->tot_len);
  721. return ERR_PTR(err);
  722. }
  723. struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  724. struct ipv6_txoptions *opt)
  725. {
  726. /*
  727. * ignore the dest before srcrt unless srcrt is being included.
  728. * --yoshfuji
  729. */
  730. if (opt && opt->dst0opt && !opt->srcrt) {
  731. if (opt_space != opt) {
  732. memcpy(opt_space, opt, sizeof(*opt_space));
  733. opt = opt_space;
  734. }
  735. opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
  736. opt->dst0opt = NULL;
  737. }
  738. return opt;
  739. }
  740. EXPORT_SYMBOL_GPL(ipv6_fixup_options);
  741. /**
  742. * fl6_update_dst - update flowi destination address with info given
  743. * by srcrt option, if any.
  744. *
  745. * @fl6: flowi6 for which daddr is to be updated
  746. * @opt: struct ipv6_txoptions in which to look for srcrt opt
  747. * @orig: copy of original daddr address if modified
  748. *
  749. * Returns NULL if no txoptions or no srcrt, otherwise returns orig
  750. * and initial value of fl6->daddr set in orig
  751. */
  752. struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
  753. const struct ipv6_txoptions *opt,
  754. struct in6_addr *orig)
  755. {
  756. if (!opt || !opt->srcrt)
  757. return NULL;
  758. *orig = fl6->daddr;
  759. fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
  760. return orig;
  761. }
  762. EXPORT_SYMBOL_GPL(fl6_update_dst);