exthdrs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. #include <net/calipso.h>
  43. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  44. #include <net/xfrm.h>
  45. #endif
  46. #include <linux/uaccess.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. if (len == 0)
  146. return true;
  147. bad:
  148. kfree_skb(skb);
  149. return false;
  150. }
  151. /*****************************
  152. Destination options header.
  153. *****************************/
  154. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  155. static bool ipv6_dest_hao(struct sk_buff *skb, int optoff)
  156. {
  157. struct ipv6_destopt_hao *hao;
  158. struct inet6_skb_parm *opt = IP6CB(skb);
  159. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  160. struct in6_addr tmp_addr;
  161. int ret;
  162. if (opt->dsthao) {
  163. net_dbg_ratelimited("hao duplicated\n");
  164. goto discard;
  165. }
  166. opt->dsthao = opt->dst1;
  167. opt->dst1 = 0;
  168. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff);
  169. if (hao->length != 16) {
  170. net_dbg_ratelimited("hao invalid option length = %d\n",
  171. hao->length);
  172. goto discard;
  173. }
  174. if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) {
  175. net_dbg_ratelimited("hao is not an unicast addr: %pI6\n",
  176. &hao->addr);
  177. goto discard;
  178. }
  179. ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr,
  180. (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS);
  181. if (unlikely(ret < 0))
  182. goto discard;
  183. if (skb_cloned(skb)) {
  184. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  185. goto discard;
  186. /* update all variable using below by copied skbuff */
  187. hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) +
  188. optoff);
  189. ipv6h = ipv6_hdr(skb);
  190. }
  191. if (skb->ip_summed == CHECKSUM_COMPLETE)
  192. skb->ip_summed = CHECKSUM_NONE;
  193. tmp_addr = ipv6h->saddr;
  194. ipv6h->saddr = hao->addr;
  195. hao->addr = tmp_addr;
  196. if (skb->tstamp.tv64 == 0)
  197. __net_timestamp(skb);
  198. return true;
  199. discard:
  200. kfree_skb(skb);
  201. return false;
  202. }
  203. #endif
  204. static const struct tlvtype_proc tlvprocdestopt_lst[] = {
  205. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  206. {
  207. .type = IPV6_TLV_HAO,
  208. .func = ipv6_dest_hao,
  209. },
  210. #endif
  211. {-1, NULL}
  212. };
  213. static int ipv6_destopt_rcv(struct sk_buff *skb)
  214. {
  215. struct inet6_skb_parm *opt = IP6CB(skb);
  216. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  217. __u16 dstbuf;
  218. #endif
  219. struct dst_entry *dst = skb_dst(skb);
  220. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  221. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  222. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  223. __IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
  224. IPSTATS_MIB_INHDRERRORS);
  225. kfree_skb(skb);
  226. return -1;
  227. }
  228. opt->lastopt = opt->dst1 = skb_network_header_len(skb);
  229. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  230. dstbuf = opt->dst1;
  231. #endif
  232. if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
  233. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  234. opt = IP6CB(skb);
  235. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  236. opt->nhoff = dstbuf;
  237. #else
  238. opt->nhoff = opt->dst1;
  239. #endif
  240. return 1;
  241. }
  242. __IP6_INC_STATS(dev_net(dst->dev),
  243. ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
  244. return -1;
  245. }
  246. /********************************
  247. Routing header.
  248. ********************************/
  249. /* called with rcu_read_lock() */
  250. static int ipv6_rthdr_rcv(struct sk_buff *skb)
  251. {
  252. struct inet6_skb_parm *opt = IP6CB(skb);
  253. struct in6_addr *addr = NULL;
  254. struct in6_addr daddr;
  255. struct inet6_dev *idev;
  256. int n, i;
  257. struct ipv6_rt_hdr *hdr;
  258. struct rt0_hdr *rthdr;
  259. struct net *net = dev_net(skb->dev);
  260. int accept_source_route = net->ipv6.devconf_all->accept_source_route;
  261. idev = __in6_dev_get(skb->dev);
  262. if (idev && accept_source_route > idev->cnf.accept_source_route)
  263. accept_source_route = idev->cnf.accept_source_route;
  264. if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) ||
  265. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  266. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  267. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  268. IPSTATS_MIB_INHDRERRORS);
  269. kfree_skb(skb);
  270. return -1;
  271. }
  272. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  273. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ||
  274. skb->pkt_type != PACKET_HOST) {
  275. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  276. IPSTATS_MIB_INADDRERRORS);
  277. kfree_skb(skb);
  278. return -1;
  279. }
  280. looped_back:
  281. if (hdr->segments_left == 0) {
  282. switch (hdr->type) {
  283. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  284. case IPV6_SRCRT_TYPE_2:
  285. /* Silently discard type 2 header unless it was
  286. * processed by own
  287. */
  288. if (!addr) {
  289. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  290. IPSTATS_MIB_INADDRERRORS);
  291. kfree_skb(skb);
  292. return -1;
  293. }
  294. break;
  295. #endif
  296. default:
  297. break;
  298. }
  299. opt->lastopt = opt->srcrt = skb_network_header_len(skb);
  300. skb->transport_header += (hdr->hdrlen + 1) << 3;
  301. opt->dst0 = opt->dst1;
  302. opt->dst1 = 0;
  303. opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb);
  304. return 1;
  305. }
  306. switch (hdr->type) {
  307. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  308. case IPV6_SRCRT_TYPE_2:
  309. if (accept_source_route < 0)
  310. goto unknown_rh;
  311. /* Silently discard invalid RTH type 2 */
  312. if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
  313. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  314. IPSTATS_MIB_INHDRERRORS);
  315. kfree_skb(skb);
  316. return -1;
  317. }
  318. break;
  319. #endif
  320. default:
  321. goto unknown_rh;
  322. }
  323. /*
  324. * This is the routing header forwarding algorithm from
  325. * RFC 2460, page 16.
  326. */
  327. n = hdr->hdrlen >> 1;
  328. if (hdr->segments_left > n) {
  329. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  330. IPSTATS_MIB_INHDRERRORS);
  331. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  332. ((&hdr->segments_left) -
  333. skb_network_header(skb)));
  334. return -1;
  335. }
  336. /* We are about to mangle packet header. Be careful!
  337. Do not damage packets queued somewhere.
  338. */
  339. if (skb_cloned(skb)) {
  340. /* the copy is a forwarded packet */
  341. if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  342. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  343. IPSTATS_MIB_OUTDISCARDS);
  344. kfree_skb(skb);
  345. return -1;
  346. }
  347. hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb);
  348. }
  349. if (skb->ip_summed == CHECKSUM_COMPLETE)
  350. skb->ip_summed = CHECKSUM_NONE;
  351. i = n - --hdr->segments_left;
  352. rthdr = (struct rt0_hdr *) hdr;
  353. addr = rthdr->addr;
  354. addr += i - 1;
  355. switch (hdr->type) {
  356. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  357. case IPV6_SRCRT_TYPE_2:
  358. if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
  359. (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  360. IPPROTO_ROUTING) < 0) {
  361. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  362. IPSTATS_MIB_INADDRERRORS);
  363. kfree_skb(skb);
  364. return -1;
  365. }
  366. if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) {
  367. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  368. IPSTATS_MIB_INADDRERRORS);
  369. kfree_skb(skb);
  370. return -1;
  371. }
  372. break;
  373. #endif
  374. default:
  375. break;
  376. }
  377. if (ipv6_addr_is_multicast(addr)) {
  378. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  379. IPSTATS_MIB_INADDRERRORS);
  380. kfree_skb(skb);
  381. return -1;
  382. }
  383. daddr = *addr;
  384. *addr = ipv6_hdr(skb)->daddr;
  385. ipv6_hdr(skb)->daddr = daddr;
  386. skb_dst_drop(skb);
  387. ip6_route_input(skb);
  388. if (skb_dst(skb)->error) {
  389. skb_push(skb, skb->data - skb_network_header(skb));
  390. dst_input(skb);
  391. return -1;
  392. }
  393. if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) {
  394. if (ipv6_hdr(skb)->hop_limit <= 1) {
  395. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  396. IPSTATS_MIB_INHDRERRORS);
  397. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  398. 0);
  399. kfree_skb(skb);
  400. return -1;
  401. }
  402. ipv6_hdr(skb)->hop_limit--;
  403. goto looped_back;
  404. }
  405. skb_push(skb, skb->data - skb_network_header(skb));
  406. dst_input(skb);
  407. return -1;
  408. unknown_rh:
  409. __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
  410. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  411. (&hdr->type) - skb_network_header(skb));
  412. return -1;
  413. }
  414. static const struct inet6_protocol rthdr_protocol = {
  415. .handler = ipv6_rthdr_rcv,
  416. .flags = INET6_PROTO_NOPOLICY,
  417. };
  418. static const struct inet6_protocol destopt_protocol = {
  419. .handler = ipv6_destopt_rcv,
  420. .flags = INET6_PROTO_NOPOLICY,
  421. };
  422. static const struct inet6_protocol nodata_protocol = {
  423. .handler = dst_discard,
  424. .flags = INET6_PROTO_NOPOLICY,
  425. };
  426. int __init ipv6_exthdrs_init(void)
  427. {
  428. int ret;
  429. ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  430. if (ret)
  431. goto out;
  432. ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  433. if (ret)
  434. goto out_rthdr;
  435. ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE);
  436. if (ret)
  437. goto out_destopt;
  438. out:
  439. return ret;
  440. out_destopt:
  441. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  442. out_rthdr:
  443. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  444. goto out;
  445. };
  446. void ipv6_exthdrs_exit(void)
  447. {
  448. inet6_del_protocol(&nodata_protocol, IPPROTO_NONE);
  449. inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS);
  450. inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING);
  451. }
  452. /**********************************
  453. Hop-by-hop options.
  454. **********************************/
  455. /*
  456. * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input().
  457. */
  458. static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb)
  459. {
  460. return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev);
  461. }
  462. static inline struct net *ipv6_skb_net(struct sk_buff *skb)
  463. {
  464. return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev);
  465. }
  466. /* Router Alert as of RFC 2711 */
  467. static bool ipv6_hop_ra(struct sk_buff *skb, int optoff)
  468. {
  469. const unsigned char *nh = skb_network_header(skb);
  470. if (nh[optoff + 1] == 2) {
  471. IP6CB(skb)->flags |= IP6SKB_ROUTERALERT;
  472. memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra));
  473. return true;
  474. }
  475. net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n",
  476. nh[optoff + 1]);
  477. kfree_skb(skb);
  478. return false;
  479. }
  480. /* Jumbo payload */
  481. static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
  482. {
  483. const unsigned char *nh = skb_network_header(skb);
  484. struct net *net = ipv6_skb_net(skb);
  485. u32 pkt_len;
  486. if (nh[optoff + 1] != 4 || (optoff & 3) != 2) {
  487. net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n",
  488. nh[optoff+1]);
  489. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  490. IPSTATS_MIB_INHDRERRORS);
  491. goto drop;
  492. }
  493. pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
  494. if (pkt_len <= IPV6_MAXPLEN) {
  495. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  496. IPSTATS_MIB_INHDRERRORS);
  497. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2);
  498. return false;
  499. }
  500. if (ipv6_hdr(skb)->payload_len) {
  501. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  502. IPSTATS_MIB_INHDRERRORS);
  503. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff);
  504. return false;
  505. }
  506. if (pkt_len > skb->len - sizeof(struct ipv6hdr)) {
  507. __IP6_INC_STATS(net, ipv6_skb_idev(skb),
  508. IPSTATS_MIB_INTRUNCATEDPKTS);
  509. goto drop;
  510. }
  511. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  512. goto drop;
  513. return true;
  514. drop:
  515. kfree_skb(skb);
  516. return false;
  517. }
  518. /* CALIPSO RFC 5570 */
  519. static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff)
  520. {
  521. const unsigned char *nh = skb_network_header(skb);
  522. if (nh[optoff + 1] < 8)
  523. goto drop;
  524. if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1])
  525. goto drop;
  526. if (!calipso_validate(skb, nh + optoff))
  527. goto drop;
  528. return true;
  529. drop:
  530. kfree_skb(skb);
  531. return false;
  532. }
  533. static const struct tlvtype_proc tlvprochopopt_lst[] = {
  534. {
  535. .type = IPV6_TLV_ROUTERALERT,
  536. .func = ipv6_hop_ra,
  537. },
  538. {
  539. .type = IPV6_TLV_JUMBO,
  540. .func = ipv6_hop_jumbo,
  541. },
  542. {
  543. .type = IPV6_TLV_CALIPSO,
  544. .func = ipv6_hop_calipso,
  545. },
  546. { -1, }
  547. };
  548. int ipv6_parse_hopopts(struct sk_buff *skb)
  549. {
  550. struct inet6_skb_parm *opt = IP6CB(skb);
  551. /*
  552. * skb_network_header(skb) is equal to skb->data, and
  553. * skb_network_header_len(skb) is always equal to
  554. * sizeof(struct ipv6hdr) by definition of
  555. * hop-by-hop options.
  556. */
  557. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) ||
  558. !pskb_may_pull(skb, (sizeof(struct ipv6hdr) +
  559. ((skb_transport_header(skb)[1] + 1) << 3)))) {
  560. kfree_skb(skb);
  561. return -1;
  562. }
  563. opt->flags |= IP6SKB_HOPBYHOP;
  564. if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
  565. skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3;
  566. opt = IP6CB(skb);
  567. opt->nhoff = sizeof(struct ipv6hdr);
  568. return 1;
  569. }
  570. return -1;
  571. }
  572. /*
  573. * Creating outbound headers.
  574. *
  575. * "build" functions work when skb is filled from head to tail (datagram)
  576. * "push" functions work when headers are added from tail to head (tcp)
  577. *
  578. * In both cases we assume, that caller reserved enough room
  579. * for headers.
  580. */
  581. static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto,
  582. struct ipv6_rt_hdr *opt,
  583. struct in6_addr **addr_p)
  584. {
  585. struct rt0_hdr *phdr, *ihdr;
  586. int hops;
  587. ihdr = (struct rt0_hdr *) opt;
  588. phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3);
  589. memcpy(phdr, ihdr, sizeof(struct rt0_hdr));
  590. hops = ihdr->rt_hdr.hdrlen >> 1;
  591. if (hops > 1)
  592. memcpy(phdr->addr, ihdr->addr + 1,
  593. (hops - 1) * sizeof(struct in6_addr));
  594. phdr->addr[hops - 1] = **addr_p;
  595. *addr_p = ihdr->addr;
  596. phdr->rt_hdr.nexthdr = *proto;
  597. *proto = NEXTHDR_ROUTING;
  598. }
  599. static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt)
  600. {
  601. struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt));
  602. memcpy(h, opt, ipv6_optlen(opt));
  603. h->nexthdr = *proto;
  604. *proto = type;
  605. }
  606. void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
  607. u8 *proto,
  608. struct in6_addr **daddr)
  609. {
  610. if (opt->srcrt) {
  611. ipv6_push_rthdr(skb, proto, opt->srcrt, daddr);
  612. /*
  613. * IPV6_RTHDRDSTOPTS is ignored
  614. * unless IPV6_RTHDR is set (RFC3542).
  615. */
  616. if (opt->dst0opt)
  617. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt);
  618. }
  619. if (opt->hopopt)
  620. ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt);
  621. }
  622. EXPORT_SYMBOL(ipv6_push_nfrag_opts);
  623. void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto)
  624. {
  625. if (opt->dst1opt)
  626. ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt);
  627. }
  628. struct ipv6_txoptions *
  629. ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt)
  630. {
  631. struct ipv6_txoptions *opt2;
  632. opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC);
  633. if (opt2) {
  634. long dif = (char *)opt2 - (char *)opt;
  635. memcpy(opt2, opt, opt->tot_len);
  636. if (opt2->hopopt)
  637. *((char **)&opt2->hopopt) += dif;
  638. if (opt2->dst0opt)
  639. *((char **)&opt2->dst0opt) += dif;
  640. if (opt2->dst1opt)
  641. *((char **)&opt2->dst1opt) += dif;
  642. if (opt2->srcrt)
  643. *((char **)&opt2->srcrt) += dif;
  644. atomic_set(&opt2->refcnt, 1);
  645. }
  646. return opt2;
  647. }
  648. EXPORT_SYMBOL_GPL(ipv6_dup_options);
  649. static int ipv6_renew_option(void *ohdr,
  650. struct ipv6_opt_hdr __user *newopt, int newoptlen,
  651. int inherit,
  652. struct ipv6_opt_hdr **hdr,
  653. char **p)
  654. {
  655. if (inherit) {
  656. if (ohdr) {
  657. memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
  658. *hdr = (struct ipv6_opt_hdr *)*p;
  659. *p += CMSG_ALIGN(ipv6_optlen(*hdr));
  660. }
  661. } else {
  662. if (newopt) {
  663. if (copy_from_user(*p, newopt, newoptlen))
  664. return -EFAULT;
  665. *hdr = (struct ipv6_opt_hdr *)*p;
  666. if (ipv6_optlen(*hdr) > newoptlen)
  667. return -EINVAL;
  668. *p += CMSG_ALIGN(newoptlen);
  669. }
  670. }
  671. return 0;
  672. }
  673. /**
  674. * ipv6_renew_options - replace a specific ext hdr with a new one.
  675. *
  676. * @sk: sock from which to allocate memory
  677. * @opt: original options
  678. * @newtype: option type to replace in @opt
  679. * @newopt: new option of type @newtype to replace (user-mem)
  680. * @newoptlen: length of @newopt
  681. *
  682. * Returns a new set of options which is a copy of @opt with the
  683. * option type @newtype replaced with @newopt.
  684. *
  685. * @opt may be NULL, in which case a new set of options is returned
  686. * containing just @newopt.
  687. *
  688. * @newopt may be NULL, in which case the specified option type is
  689. * not copied into the new set of options.
  690. *
  691. * The new set of options is allocated from the socket option memory
  692. * buffer of @sk.
  693. */
  694. struct ipv6_txoptions *
  695. ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
  696. int newtype,
  697. struct ipv6_opt_hdr __user *newopt, int newoptlen)
  698. {
  699. int tot_len = 0;
  700. char *p;
  701. struct ipv6_txoptions *opt2;
  702. int err;
  703. if (opt) {
  704. if (newtype != IPV6_HOPOPTS && opt->hopopt)
  705. tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
  706. if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
  707. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
  708. if (newtype != IPV6_RTHDR && opt->srcrt)
  709. tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
  710. if (newtype != IPV6_DSTOPTS && opt->dst1opt)
  711. tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
  712. }
  713. if (newopt && newoptlen)
  714. tot_len += CMSG_ALIGN(newoptlen);
  715. if (!tot_len)
  716. return NULL;
  717. tot_len += sizeof(*opt2);
  718. opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
  719. if (!opt2)
  720. return ERR_PTR(-ENOBUFS);
  721. memset(opt2, 0, tot_len);
  722. atomic_set(&opt2->refcnt, 1);
  723. opt2->tot_len = tot_len;
  724. p = (char *)(opt2 + 1);
  725. err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
  726. newtype != IPV6_HOPOPTS,
  727. &opt2->hopopt, &p);
  728. if (err)
  729. goto out;
  730. err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
  731. newtype != IPV6_RTHDRDSTOPTS,
  732. &opt2->dst0opt, &p);
  733. if (err)
  734. goto out;
  735. err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
  736. newtype != IPV6_RTHDR,
  737. (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
  738. if (err)
  739. goto out;
  740. err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
  741. newtype != IPV6_DSTOPTS,
  742. &opt2->dst1opt, &p);
  743. if (err)
  744. goto out;
  745. opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
  746. (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
  747. (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
  748. opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
  749. return opt2;
  750. out:
  751. sock_kfree_s(sk, opt2, opt2->tot_len);
  752. return ERR_PTR(err);
  753. }
  754. /**
  755. * ipv6_renew_options_kern - replace a specific ext hdr with a new one.
  756. *
  757. * @sk: sock from which to allocate memory
  758. * @opt: original options
  759. * @newtype: option type to replace in @opt
  760. * @newopt: new option of type @newtype to replace (kernel-mem)
  761. * @newoptlen: length of @newopt
  762. *
  763. * See ipv6_renew_options(). The difference is that @newopt is
  764. * kernel memory, rather than user memory.
  765. */
  766. struct ipv6_txoptions *
  767. ipv6_renew_options_kern(struct sock *sk, struct ipv6_txoptions *opt,
  768. int newtype, struct ipv6_opt_hdr *newopt,
  769. int newoptlen)
  770. {
  771. struct ipv6_txoptions *ret_val;
  772. const mm_segment_t old_fs = get_fs();
  773. set_fs(KERNEL_DS);
  774. ret_val = ipv6_renew_options(sk, opt, newtype,
  775. (struct ipv6_opt_hdr __user *)newopt,
  776. newoptlen);
  777. set_fs(old_fs);
  778. return ret_val;
  779. }
  780. struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
  781. struct ipv6_txoptions *opt)
  782. {
  783. /*
  784. * ignore the dest before srcrt unless srcrt is being included.
  785. * --yoshfuji
  786. */
  787. if (opt && opt->dst0opt && !opt->srcrt) {
  788. if (opt_space != opt) {
  789. memcpy(opt_space, opt, sizeof(*opt_space));
  790. opt = opt_space;
  791. }
  792. opt->opt_nflen -= ipv6_optlen(opt->dst0opt);
  793. opt->dst0opt = NULL;
  794. }
  795. return opt;
  796. }
  797. EXPORT_SYMBOL_GPL(ipv6_fixup_options);
  798. /**
  799. * fl6_update_dst - update flowi destination address with info given
  800. * by srcrt option, if any.
  801. *
  802. * @fl6: flowi6 for which daddr is to be updated
  803. * @opt: struct ipv6_txoptions in which to look for srcrt opt
  804. * @orig: copy of original daddr address if modified
  805. *
  806. * Returns NULL if no txoptions or no srcrt, otherwise returns orig
  807. * and initial value of fl6->daddr set in orig
  808. */
  809. struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
  810. const struct ipv6_txoptions *opt,
  811. struct in6_addr *orig)
  812. {
  813. if (!opt || !opt->srcrt)
  814. return NULL;
  815. *orig = fl6->daddr;
  816. fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr;
  817. return orig;
  818. }
  819. EXPORT_SYMBOL_GPL(fl6_update_dst);