exthdrs.c 21 KB

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