ip6_output.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /*
  2. * IPv6 output functions
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on linux/net/ipv4/ip_output.c
  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. * A.N.Kuznetsov : airthmetics in fragmentation.
  17. * extension headers are implemented.
  18. * route changes now work.
  19. * ip6_forward does not confuse sniffers.
  20. * etc.
  21. *
  22. * H. von Brand : Added missing #include <linux/string.h>
  23. * Imran Patel : frag id should be in NBO
  24. * Kazunori MIYAZAWA @USAGI
  25. * : add ip6_append_data and related functions
  26. * for datagram xmit
  27. */
  28. #include <linux/errno.h>
  29. #include <linux/kernel.h>
  30. #include <linux/string.h>
  31. #include <linux/socket.h>
  32. #include <linux/net.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/if_arp.h>
  35. #include <linux/in6.h>
  36. #include <linux/tcp.h>
  37. #include <linux/route.h>
  38. #include <linux/module.h>
  39. #include <linux/slab.h>
  40. #include <linux/netfilter.h>
  41. #include <linux/netfilter_ipv6.h>
  42. #include <net/sock.h>
  43. #include <net/snmp.h>
  44. #include <net/ipv6.h>
  45. #include <net/ndisc.h>
  46. #include <net/protocol.h>
  47. #include <net/ip6_route.h>
  48. #include <net/addrconf.h>
  49. #include <net/rawv6.h>
  50. #include <net/icmp.h>
  51. #include <net/xfrm.h>
  52. #include <net/checksum.h>
  53. #include <linux/mroute6.h>
  54. static int ip6_finish_output2(struct sk_buff *skb)
  55. {
  56. struct dst_entry *dst = skb_dst(skb);
  57. struct net_device *dev = dst->dev;
  58. struct neighbour *neigh;
  59. struct in6_addr *nexthop;
  60. int ret;
  61. skb->protocol = htons(ETH_P_IPV6);
  62. skb->dev = dev;
  63. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
  64. struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
  65. if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(skb->sk) &&
  66. ((mroute6_socket(dev_net(dev), skb) &&
  67. !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
  68. ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
  69. &ipv6_hdr(skb)->saddr))) {
  70. struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
  71. /* Do not check for IFF_ALLMULTI; multicast routing
  72. is not supported in any case.
  73. */
  74. if (newskb)
  75. NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  76. newskb, NULL, newskb->dev,
  77. dev_loopback_xmit);
  78. if (ipv6_hdr(skb)->hop_limit == 0) {
  79. IP6_INC_STATS(dev_net(dev), idev,
  80. IPSTATS_MIB_OUTDISCARDS);
  81. kfree_skb(skb);
  82. return 0;
  83. }
  84. }
  85. IP6_UPD_PO_STATS(dev_net(dev), idev, IPSTATS_MIB_OUTMCAST,
  86. skb->len);
  87. if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <=
  88. IPV6_ADDR_SCOPE_NODELOCAL &&
  89. !(dev->flags & IFF_LOOPBACK)) {
  90. kfree_skb(skb);
  91. return 0;
  92. }
  93. }
  94. rcu_read_lock_bh();
  95. nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
  96. neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
  97. if (unlikely(!neigh))
  98. neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
  99. if (!IS_ERR(neigh)) {
  100. ret = dst_neigh_output(dst, neigh, skb);
  101. rcu_read_unlock_bh();
  102. return ret;
  103. }
  104. rcu_read_unlock_bh();
  105. IP6_INC_STATS_BH(dev_net(dst->dev),
  106. ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  107. kfree_skb(skb);
  108. return -EINVAL;
  109. }
  110. static int ip6_finish_output(struct sk_buff *skb)
  111. {
  112. if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
  113. dst_allfrag(skb_dst(skb)))
  114. return ip6_fragment(skb, ip6_finish_output2);
  115. else
  116. return ip6_finish_output2(skb);
  117. }
  118. int ip6_output(struct sk_buff *skb)
  119. {
  120. struct net_device *dev = skb_dst(skb)->dev;
  121. struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
  122. if (unlikely(idev->cnf.disable_ipv6)) {
  123. IP6_INC_STATS(dev_net(dev), idev,
  124. IPSTATS_MIB_OUTDISCARDS);
  125. kfree_skb(skb);
  126. return 0;
  127. }
  128. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
  129. ip6_finish_output,
  130. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  131. }
  132. /*
  133. * xmit an sk_buff (used by TCP, SCTP and DCCP)
  134. */
  135. int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
  136. struct ipv6_txoptions *opt, int tclass)
  137. {
  138. struct net *net = sock_net(sk);
  139. struct ipv6_pinfo *np = inet6_sk(sk);
  140. struct in6_addr *first_hop = &fl6->daddr;
  141. struct dst_entry *dst = skb_dst(skb);
  142. struct ipv6hdr *hdr;
  143. u8 proto = fl6->flowi6_proto;
  144. int seg_len = skb->len;
  145. int hlimit = -1;
  146. u32 mtu;
  147. if (opt) {
  148. unsigned int head_room;
  149. /* First: exthdrs may take lots of space (~8K for now)
  150. MAX_HEADER is not enough.
  151. */
  152. head_room = opt->opt_nflen + opt->opt_flen;
  153. seg_len += head_room;
  154. head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
  155. if (skb_headroom(skb) < head_room) {
  156. struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
  157. if (skb2 == NULL) {
  158. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  159. IPSTATS_MIB_OUTDISCARDS);
  160. kfree_skb(skb);
  161. return -ENOBUFS;
  162. }
  163. consume_skb(skb);
  164. skb = skb2;
  165. skb_set_owner_w(skb, sk);
  166. }
  167. if (opt->opt_flen)
  168. ipv6_push_frag_opts(skb, opt, &proto);
  169. if (opt->opt_nflen)
  170. ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
  171. }
  172. skb_push(skb, sizeof(struct ipv6hdr));
  173. skb_reset_network_header(skb);
  174. hdr = ipv6_hdr(skb);
  175. /*
  176. * Fill in the IPv6 header
  177. */
  178. if (np)
  179. hlimit = np->hop_limit;
  180. if (hlimit < 0)
  181. hlimit = ip6_dst_hoplimit(dst);
  182. ip6_flow_hdr(hdr, tclass, fl6->flowlabel);
  183. hdr->payload_len = htons(seg_len);
  184. hdr->nexthdr = proto;
  185. hdr->hop_limit = hlimit;
  186. hdr->saddr = fl6->saddr;
  187. hdr->daddr = *first_hop;
  188. skb->protocol = htons(ETH_P_IPV6);
  189. skb->priority = sk->sk_priority;
  190. skb->mark = sk->sk_mark;
  191. mtu = dst_mtu(dst);
  192. if ((skb->len <= mtu) || skb->local_df || skb_is_gso(skb)) {
  193. IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
  194. IPSTATS_MIB_OUT, skb->len);
  195. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
  196. dst->dev, dst_output);
  197. }
  198. skb->dev = dst->dev;
  199. ipv6_local_error(sk, EMSGSIZE, fl6, mtu);
  200. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
  201. kfree_skb(skb);
  202. return -EMSGSIZE;
  203. }
  204. EXPORT_SYMBOL(ip6_xmit);
  205. static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
  206. {
  207. struct ip6_ra_chain *ra;
  208. struct sock *last = NULL;
  209. read_lock(&ip6_ra_lock);
  210. for (ra = ip6_ra_chain; ra; ra = ra->next) {
  211. struct sock *sk = ra->sk;
  212. if (sk && ra->sel == sel &&
  213. (!sk->sk_bound_dev_if ||
  214. sk->sk_bound_dev_if == skb->dev->ifindex)) {
  215. if (last) {
  216. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  217. if (skb2)
  218. rawv6_rcv(last, skb2);
  219. }
  220. last = sk;
  221. }
  222. }
  223. if (last) {
  224. rawv6_rcv(last, skb);
  225. read_unlock(&ip6_ra_lock);
  226. return 1;
  227. }
  228. read_unlock(&ip6_ra_lock);
  229. return 0;
  230. }
  231. static int ip6_forward_proxy_check(struct sk_buff *skb)
  232. {
  233. struct ipv6hdr *hdr = ipv6_hdr(skb);
  234. u8 nexthdr = hdr->nexthdr;
  235. __be16 frag_off;
  236. int offset;
  237. if (ipv6_ext_hdr(nexthdr)) {
  238. offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
  239. if (offset < 0)
  240. return 0;
  241. } else
  242. offset = sizeof(struct ipv6hdr);
  243. if (nexthdr == IPPROTO_ICMPV6) {
  244. struct icmp6hdr *icmp6;
  245. if (!pskb_may_pull(skb, (skb_network_header(skb) +
  246. offset + 1 - skb->data)))
  247. return 0;
  248. icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  249. switch (icmp6->icmp6_type) {
  250. case NDISC_ROUTER_SOLICITATION:
  251. case NDISC_ROUTER_ADVERTISEMENT:
  252. case NDISC_NEIGHBOUR_SOLICITATION:
  253. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  254. case NDISC_REDIRECT:
  255. /* For reaction involving unicast neighbor discovery
  256. * message destined to the proxied address, pass it to
  257. * input function.
  258. */
  259. return 1;
  260. default:
  261. break;
  262. }
  263. }
  264. /*
  265. * The proxying router can't forward traffic sent to a link-local
  266. * address, so signal the sender and discard the packet. This
  267. * behavior is clarified by the MIPv6 specification.
  268. */
  269. if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
  270. dst_link_failure(skb);
  271. return -1;
  272. }
  273. return 0;
  274. }
  275. static inline int ip6_forward_finish(struct sk_buff *skb)
  276. {
  277. return dst_output(skb);
  278. }
  279. int ip6_forward(struct sk_buff *skb)
  280. {
  281. struct dst_entry *dst = skb_dst(skb);
  282. struct ipv6hdr *hdr = ipv6_hdr(skb);
  283. struct inet6_skb_parm *opt = IP6CB(skb);
  284. struct net *net = dev_net(dst->dev);
  285. u32 mtu;
  286. if (net->ipv6.devconf_all->forwarding == 0)
  287. goto error;
  288. if (skb_warn_if_lro(skb))
  289. goto drop;
  290. if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
  291. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
  292. goto drop;
  293. }
  294. if (skb->pkt_type != PACKET_HOST)
  295. goto drop;
  296. skb_forward_csum(skb);
  297. /*
  298. * We DO NOT make any processing on
  299. * RA packets, pushing them to user level AS IS
  300. * without ane WARRANTY that application will be able
  301. * to interpret them. The reason is that we
  302. * cannot make anything clever here.
  303. *
  304. * We are not end-node, so that if packet contains
  305. * AH/ESP, we cannot make anything.
  306. * Defragmentation also would be mistake, RA packets
  307. * cannot be fragmented, because there is no warranty
  308. * that different fragments will go along one path. --ANK
  309. */
  310. if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
  311. if (ip6_call_ra_chain(skb, ntohs(opt->ra)))
  312. return 0;
  313. }
  314. /*
  315. * check and decrement ttl
  316. */
  317. if (hdr->hop_limit <= 1) {
  318. /* Force OUTPUT device used as source address */
  319. skb->dev = dst->dev;
  320. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
  321. IP6_INC_STATS_BH(net,
  322. ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
  323. kfree_skb(skb);
  324. return -ETIMEDOUT;
  325. }
  326. /* XXX: idev->cnf.proxy_ndp? */
  327. if (net->ipv6.devconf_all->proxy_ndp &&
  328. pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
  329. int proxied = ip6_forward_proxy_check(skb);
  330. if (proxied > 0)
  331. return ip6_input(skb);
  332. else if (proxied < 0) {
  333. IP6_INC_STATS(net, ip6_dst_idev(dst),
  334. IPSTATS_MIB_INDISCARDS);
  335. goto drop;
  336. }
  337. }
  338. if (!xfrm6_route_forward(skb)) {
  339. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
  340. goto drop;
  341. }
  342. dst = skb_dst(skb);
  343. /* IPv6 specs say nothing about it, but it is clear that we cannot
  344. send redirects to source routed frames.
  345. We don't send redirects to frames decapsulated from IPsec.
  346. */
  347. if (skb->dev == dst->dev && opt->srcrt == 0 && !skb_sec_path(skb)) {
  348. struct in6_addr *target = NULL;
  349. struct inet_peer *peer;
  350. struct rt6_info *rt;
  351. /*
  352. * incoming and outgoing devices are the same
  353. * send a redirect.
  354. */
  355. rt = (struct rt6_info *) dst;
  356. if (rt->rt6i_flags & RTF_GATEWAY)
  357. target = &rt->rt6i_gateway;
  358. else
  359. target = &hdr->daddr;
  360. peer = inet_getpeer_v6(net->ipv6.peers, &rt->rt6i_dst.addr, 1);
  361. /* Limit redirects both by destination (here)
  362. and by source (inside ndisc_send_redirect)
  363. */
  364. if (inet_peer_xrlim_allow(peer, 1*HZ))
  365. ndisc_send_redirect(skb, target);
  366. if (peer)
  367. inet_putpeer(peer);
  368. } else {
  369. int addrtype = ipv6_addr_type(&hdr->saddr);
  370. /* This check is security critical. */
  371. if (addrtype == IPV6_ADDR_ANY ||
  372. addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
  373. goto error;
  374. if (addrtype & IPV6_ADDR_LINKLOCAL) {
  375. icmpv6_send(skb, ICMPV6_DEST_UNREACH,
  376. ICMPV6_NOT_NEIGHBOUR, 0);
  377. goto error;
  378. }
  379. }
  380. mtu = dst_mtu(dst);
  381. if (mtu < IPV6_MIN_MTU)
  382. mtu = IPV6_MIN_MTU;
  383. if ((!skb->local_df && skb->len > mtu && !skb_is_gso(skb)) ||
  384. (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu)) {
  385. /* Again, force OUTPUT device used as source address */
  386. skb->dev = dst->dev;
  387. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  388. IP6_INC_STATS_BH(net,
  389. ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS);
  390. IP6_INC_STATS_BH(net,
  391. ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS);
  392. kfree_skb(skb);
  393. return -EMSGSIZE;
  394. }
  395. if (skb_cow(skb, dst->dev->hard_header_len)) {
  396. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
  397. goto drop;
  398. }
  399. hdr = ipv6_hdr(skb);
  400. /* Mangling hops number delayed to point after skb COW */
  401. hdr->hop_limit--;
  402. IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
  403. IP6_ADD_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len);
  404. return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev,
  405. ip6_forward_finish);
  406. error:
  407. IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
  408. drop:
  409. kfree_skb(skb);
  410. return -EINVAL;
  411. }
  412. static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
  413. {
  414. to->pkt_type = from->pkt_type;
  415. to->priority = from->priority;
  416. to->protocol = from->protocol;
  417. skb_dst_drop(to);
  418. skb_dst_set(to, dst_clone(skb_dst(from)));
  419. to->dev = from->dev;
  420. to->mark = from->mark;
  421. #ifdef CONFIG_NET_SCHED
  422. to->tc_index = from->tc_index;
  423. #endif
  424. nf_copy(to, from);
  425. #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
  426. to->nf_trace = from->nf_trace;
  427. #endif
  428. skb_copy_secmark(to, from);
  429. }
  430. int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
  431. {
  432. struct sk_buff *frag;
  433. struct rt6_info *rt = (struct rt6_info*)skb_dst(skb);
  434. struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
  435. struct ipv6hdr *tmp_hdr;
  436. struct frag_hdr *fh;
  437. unsigned int mtu, hlen, left, len;
  438. int hroom, troom;
  439. __be32 frag_id = 0;
  440. int ptr, offset = 0, err=0;
  441. u8 *prevhdr, nexthdr = 0;
  442. struct net *net = dev_net(skb_dst(skb)->dev);
  443. hlen = ip6_find_1stfragopt(skb, &prevhdr);
  444. nexthdr = *prevhdr;
  445. mtu = ip6_skb_dst_mtu(skb);
  446. /* We must not fragment if the socket is set to force MTU discovery
  447. * or if the skb it not generated by a local socket.
  448. */
  449. if (unlikely(!skb->local_df && skb->len > mtu) ||
  450. (IP6CB(skb)->frag_max_size &&
  451. IP6CB(skb)->frag_max_size > mtu)) {
  452. if (skb->sk && dst_allfrag(skb_dst(skb)))
  453. sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK);
  454. skb->dev = skb_dst(skb)->dev;
  455. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  456. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  457. IPSTATS_MIB_FRAGFAILS);
  458. kfree_skb(skb);
  459. return -EMSGSIZE;
  460. }
  461. if (np && np->frag_size < mtu) {
  462. if (np->frag_size)
  463. mtu = np->frag_size;
  464. }
  465. mtu -= hlen + sizeof(struct frag_hdr);
  466. if (skb_has_frag_list(skb)) {
  467. int first_len = skb_pagelen(skb);
  468. struct sk_buff *frag2;
  469. if (first_len - hlen > mtu ||
  470. ((first_len - hlen) & 7) ||
  471. skb_cloned(skb))
  472. goto slow_path;
  473. skb_walk_frags(skb, frag) {
  474. /* Correct geometry. */
  475. if (frag->len > mtu ||
  476. ((frag->len & 7) && frag->next) ||
  477. skb_headroom(frag) < hlen)
  478. goto slow_path_clean;
  479. /* Partially cloned skb? */
  480. if (skb_shared(frag))
  481. goto slow_path_clean;
  482. BUG_ON(frag->sk);
  483. if (skb->sk) {
  484. frag->sk = skb->sk;
  485. frag->destructor = sock_wfree;
  486. }
  487. skb->truesize -= frag->truesize;
  488. }
  489. err = 0;
  490. offset = 0;
  491. frag = skb_shinfo(skb)->frag_list;
  492. skb_frag_list_init(skb);
  493. /* BUILD HEADER */
  494. *prevhdr = NEXTHDR_FRAGMENT;
  495. tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
  496. if (!tmp_hdr) {
  497. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  498. IPSTATS_MIB_FRAGFAILS);
  499. return -ENOMEM;
  500. }
  501. __skb_pull(skb, hlen);
  502. fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
  503. __skb_push(skb, hlen);
  504. skb_reset_network_header(skb);
  505. memcpy(skb_network_header(skb), tmp_hdr, hlen);
  506. ipv6_select_ident(fh, rt);
  507. fh->nexthdr = nexthdr;
  508. fh->reserved = 0;
  509. fh->frag_off = htons(IP6_MF);
  510. frag_id = fh->identification;
  511. first_len = skb_pagelen(skb);
  512. skb->data_len = first_len - skb_headlen(skb);
  513. skb->len = first_len;
  514. ipv6_hdr(skb)->payload_len = htons(first_len -
  515. sizeof(struct ipv6hdr));
  516. dst_hold(&rt->dst);
  517. for (;;) {
  518. /* Prepare header of the next frame,
  519. * before previous one went down. */
  520. if (frag) {
  521. frag->ip_summed = CHECKSUM_NONE;
  522. skb_reset_transport_header(frag);
  523. fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
  524. __skb_push(frag, hlen);
  525. skb_reset_network_header(frag);
  526. memcpy(skb_network_header(frag), tmp_hdr,
  527. hlen);
  528. offset += skb->len - hlen - sizeof(struct frag_hdr);
  529. fh->nexthdr = nexthdr;
  530. fh->reserved = 0;
  531. fh->frag_off = htons(offset);
  532. if (frag->next != NULL)
  533. fh->frag_off |= htons(IP6_MF);
  534. fh->identification = frag_id;
  535. ipv6_hdr(frag)->payload_len =
  536. htons(frag->len -
  537. sizeof(struct ipv6hdr));
  538. ip6_copy_metadata(frag, skb);
  539. }
  540. err = output(skb);
  541. if(!err)
  542. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  543. IPSTATS_MIB_FRAGCREATES);
  544. if (err || !frag)
  545. break;
  546. skb = frag;
  547. frag = skb->next;
  548. skb->next = NULL;
  549. }
  550. kfree(tmp_hdr);
  551. if (err == 0) {
  552. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  553. IPSTATS_MIB_FRAGOKS);
  554. ip6_rt_put(rt);
  555. return 0;
  556. }
  557. while (frag) {
  558. skb = frag->next;
  559. kfree_skb(frag);
  560. frag = skb;
  561. }
  562. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  563. IPSTATS_MIB_FRAGFAILS);
  564. ip6_rt_put(rt);
  565. return err;
  566. slow_path_clean:
  567. skb_walk_frags(skb, frag2) {
  568. if (frag2 == frag)
  569. break;
  570. frag2->sk = NULL;
  571. frag2->destructor = NULL;
  572. skb->truesize += frag2->truesize;
  573. }
  574. }
  575. slow_path:
  576. if ((skb->ip_summed == CHECKSUM_PARTIAL) &&
  577. skb_checksum_help(skb))
  578. goto fail;
  579. left = skb->len - hlen; /* Space per frame */
  580. ptr = hlen; /* Where to start from */
  581. /*
  582. * Fragment the datagram.
  583. */
  584. *prevhdr = NEXTHDR_FRAGMENT;
  585. hroom = LL_RESERVED_SPACE(rt->dst.dev);
  586. troom = rt->dst.dev->needed_tailroom;
  587. /*
  588. * Keep copying data until we run out.
  589. */
  590. while(left > 0) {
  591. len = left;
  592. /* IF: it doesn't fit, use 'mtu' - the data space left */
  593. if (len > mtu)
  594. len = mtu;
  595. /* IF: we are not sending up to and including the packet end
  596. then align the next start on an eight byte boundary */
  597. if (len < left) {
  598. len &= ~7;
  599. }
  600. /*
  601. * Allocate buffer.
  602. */
  603. if ((frag = alloc_skb(len + hlen + sizeof(struct frag_hdr) +
  604. hroom + troom, GFP_ATOMIC)) == NULL) {
  605. NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
  606. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  607. IPSTATS_MIB_FRAGFAILS);
  608. err = -ENOMEM;
  609. goto fail;
  610. }
  611. /*
  612. * Set up data on packet
  613. */
  614. ip6_copy_metadata(frag, skb);
  615. skb_reserve(frag, hroom);
  616. skb_put(frag, len + hlen + sizeof(struct frag_hdr));
  617. skb_reset_network_header(frag);
  618. fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
  619. frag->transport_header = (frag->network_header + hlen +
  620. sizeof(struct frag_hdr));
  621. /*
  622. * Charge the memory for the fragment to any owner
  623. * it might possess
  624. */
  625. if (skb->sk)
  626. skb_set_owner_w(frag, skb->sk);
  627. /*
  628. * Copy the packet header into the new buffer.
  629. */
  630. skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
  631. /*
  632. * Build fragment header.
  633. */
  634. fh->nexthdr = nexthdr;
  635. fh->reserved = 0;
  636. if (!frag_id) {
  637. ipv6_select_ident(fh, rt);
  638. frag_id = fh->identification;
  639. } else
  640. fh->identification = frag_id;
  641. /*
  642. * Copy a block of the IP datagram.
  643. */
  644. if (skb_copy_bits(skb, ptr, skb_transport_header(frag), len))
  645. BUG();
  646. left -= len;
  647. fh->frag_off = htons(offset);
  648. if (left > 0)
  649. fh->frag_off |= htons(IP6_MF);
  650. ipv6_hdr(frag)->payload_len = htons(frag->len -
  651. sizeof(struct ipv6hdr));
  652. ptr += len;
  653. offset += len;
  654. /*
  655. * Put this fragment into the sending queue.
  656. */
  657. err = output(frag);
  658. if (err)
  659. goto fail;
  660. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  661. IPSTATS_MIB_FRAGCREATES);
  662. }
  663. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  664. IPSTATS_MIB_FRAGOKS);
  665. consume_skb(skb);
  666. return err;
  667. fail:
  668. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  669. IPSTATS_MIB_FRAGFAILS);
  670. kfree_skb(skb);
  671. return err;
  672. }
  673. static inline int ip6_rt_check(const struct rt6key *rt_key,
  674. const struct in6_addr *fl_addr,
  675. const struct in6_addr *addr_cache)
  676. {
  677. return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
  678. (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache));
  679. }
  680. static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
  681. struct dst_entry *dst,
  682. const struct flowi6 *fl6)
  683. {
  684. struct ipv6_pinfo *np = inet6_sk(sk);
  685. struct rt6_info *rt;
  686. if (!dst)
  687. goto out;
  688. if (dst->ops->family != AF_INET6) {
  689. dst_release(dst);
  690. return NULL;
  691. }
  692. rt = (struct rt6_info *)dst;
  693. /* Yes, checking route validity in not connected
  694. * case is not very simple. Take into account,
  695. * that we do not support routing by source, TOS,
  696. * and MSG_DONTROUTE --ANK (980726)
  697. *
  698. * 1. ip6_rt_check(): If route was host route,
  699. * check that cached destination is current.
  700. * If it is network route, we still may
  701. * check its validity using saved pointer
  702. * to the last used address: daddr_cache.
  703. * We do not want to save whole address now,
  704. * (because main consumer of this service
  705. * is tcp, which has not this problem),
  706. * so that the last trick works only on connected
  707. * sockets.
  708. * 2. oif also should be the same.
  709. */
  710. if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
  711. #ifdef CONFIG_IPV6_SUBTREES
  712. ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
  713. #endif
  714. (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) {
  715. dst_release(dst);
  716. dst = NULL;
  717. }
  718. out:
  719. return dst;
  720. }
  721. static int ip6_dst_lookup_tail(struct sock *sk,
  722. struct dst_entry **dst, struct flowi6 *fl6)
  723. {
  724. struct net *net = sock_net(sk);
  725. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  726. struct neighbour *n;
  727. struct rt6_info *rt;
  728. #endif
  729. int err;
  730. if (*dst == NULL)
  731. *dst = ip6_route_output(net, sk, fl6);
  732. if ((err = (*dst)->error))
  733. goto out_err_release;
  734. if (ipv6_addr_any(&fl6->saddr)) {
  735. struct rt6_info *rt = (struct rt6_info *) *dst;
  736. err = ip6_route_get_saddr(net, rt, &fl6->daddr,
  737. sk ? inet6_sk(sk)->srcprefs : 0,
  738. &fl6->saddr);
  739. if (err)
  740. goto out_err_release;
  741. }
  742. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  743. /*
  744. * Here if the dst entry we've looked up
  745. * has a neighbour entry that is in the INCOMPLETE
  746. * state and the src address from the flow is
  747. * marked as OPTIMISTIC, we release the found
  748. * dst entry and replace it instead with the
  749. * dst entry of the nexthop router
  750. */
  751. rt = (struct rt6_info *) *dst;
  752. rcu_read_lock_bh();
  753. n = __ipv6_neigh_lookup_noref(rt->dst.dev, rt6_nexthop(rt, &fl6->daddr));
  754. err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0;
  755. rcu_read_unlock_bh();
  756. if (err) {
  757. struct inet6_ifaddr *ifp;
  758. struct flowi6 fl_gw6;
  759. int redirect;
  760. ifp = ipv6_get_ifaddr(net, &fl6->saddr,
  761. (*dst)->dev, 1);
  762. redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
  763. if (ifp)
  764. in6_ifa_put(ifp);
  765. if (redirect) {
  766. /*
  767. * We need to get the dst entry for the
  768. * default router instead
  769. */
  770. dst_release(*dst);
  771. memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
  772. memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
  773. *dst = ip6_route_output(net, sk, &fl_gw6);
  774. if ((err = (*dst)->error))
  775. goto out_err_release;
  776. }
  777. }
  778. #endif
  779. return 0;
  780. out_err_release:
  781. if (err == -ENETUNREACH)
  782. IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES);
  783. dst_release(*dst);
  784. *dst = NULL;
  785. return err;
  786. }
  787. /**
  788. * ip6_dst_lookup - perform route lookup on flow
  789. * @sk: socket which provides route info
  790. * @dst: pointer to dst_entry * for result
  791. * @fl6: flow to lookup
  792. *
  793. * This function performs a route lookup on the given flow.
  794. *
  795. * It returns zero on success, or a standard errno code on error.
  796. */
  797. int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6)
  798. {
  799. *dst = NULL;
  800. return ip6_dst_lookup_tail(sk, dst, fl6);
  801. }
  802. EXPORT_SYMBOL_GPL(ip6_dst_lookup);
  803. /**
  804. * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
  805. * @sk: socket which provides route info
  806. * @fl6: flow to lookup
  807. * @final_dst: final destination address for ipsec lookup
  808. * @can_sleep: we are in a sleepable context
  809. *
  810. * This function performs a route lookup on the given flow.
  811. *
  812. * It returns a valid dst pointer on success, or a pointer encoded
  813. * error code.
  814. */
  815. struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
  816. const struct in6_addr *final_dst,
  817. bool can_sleep)
  818. {
  819. struct dst_entry *dst = NULL;
  820. int err;
  821. err = ip6_dst_lookup_tail(sk, &dst, fl6);
  822. if (err)
  823. return ERR_PTR(err);
  824. if (final_dst)
  825. fl6->daddr = *final_dst;
  826. if (can_sleep)
  827. fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
  828. return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
  829. }
  830. EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
  831. /**
  832. * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
  833. * @sk: socket which provides the dst cache and route info
  834. * @fl6: flow to lookup
  835. * @final_dst: final destination address for ipsec lookup
  836. * @can_sleep: we are in a sleepable context
  837. *
  838. * This function performs a route lookup on the given flow with the
  839. * possibility of using the cached route in the socket if it is valid.
  840. * It will take the socket dst lock when operating on the dst cache.
  841. * As a result, this function can only be used in process context.
  842. *
  843. * It returns a valid dst pointer on success, or a pointer encoded
  844. * error code.
  845. */
  846. struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
  847. const struct in6_addr *final_dst,
  848. bool can_sleep)
  849. {
  850. struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
  851. int err;
  852. dst = ip6_sk_dst_check(sk, dst, fl6);
  853. err = ip6_dst_lookup_tail(sk, &dst, fl6);
  854. if (err)
  855. return ERR_PTR(err);
  856. if (final_dst)
  857. fl6->daddr = *final_dst;
  858. if (can_sleep)
  859. fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
  860. return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
  861. }
  862. EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
  863. static inline int ip6_ufo_append_data(struct sock *sk,
  864. int getfrag(void *from, char *to, int offset, int len,
  865. int odd, struct sk_buff *skb),
  866. void *from, int length, int hh_len, int fragheaderlen,
  867. int transhdrlen, int mtu,unsigned int flags,
  868. struct rt6_info *rt)
  869. {
  870. struct sk_buff *skb;
  871. int err;
  872. /* There is support for UDP large send offload by network
  873. * device, so create one single skb packet containing complete
  874. * udp datagram
  875. */
  876. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
  877. skb = sock_alloc_send_skb(sk,
  878. hh_len + fragheaderlen + transhdrlen + 20,
  879. (flags & MSG_DONTWAIT), &err);
  880. if (skb == NULL)
  881. return err;
  882. /* reserve space for Hardware header */
  883. skb_reserve(skb, hh_len);
  884. /* create space for UDP/IP header */
  885. skb_put(skb,fragheaderlen + transhdrlen);
  886. /* initialize network header pointer */
  887. skb_reset_network_header(skb);
  888. /* initialize protocol header pointer */
  889. skb->transport_header = skb->network_header + fragheaderlen;
  890. skb->protocol = htons(ETH_P_IPV6);
  891. skb->ip_summed = CHECKSUM_PARTIAL;
  892. skb->csum = 0;
  893. }
  894. err = skb_append_datato_frags(sk,skb, getfrag, from,
  895. (length - transhdrlen));
  896. if (!err) {
  897. struct frag_hdr fhdr;
  898. /* Specify the length of each IPv6 datagram fragment.
  899. * It has to be a multiple of 8.
  900. */
  901. skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
  902. sizeof(struct frag_hdr)) & ~7;
  903. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  904. ipv6_select_ident(&fhdr, rt);
  905. skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
  906. __skb_queue_tail(&sk->sk_write_queue, skb);
  907. return 0;
  908. }
  909. /* There is not enough support do UPD LSO,
  910. * so follow normal path
  911. */
  912. kfree_skb(skb);
  913. return err;
  914. }
  915. static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
  916. gfp_t gfp)
  917. {
  918. return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
  919. }
  920. static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
  921. gfp_t gfp)
  922. {
  923. return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
  924. }
  925. static void ip6_append_data_mtu(unsigned int *mtu,
  926. int *maxfraglen,
  927. unsigned int fragheaderlen,
  928. struct sk_buff *skb,
  929. struct rt6_info *rt,
  930. bool pmtuprobe)
  931. {
  932. if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
  933. if (skb == NULL) {
  934. /* first fragment, reserve header_len */
  935. *mtu = *mtu - rt->dst.header_len;
  936. } else {
  937. /*
  938. * this fragment is not first, the headers
  939. * space is regarded as data space.
  940. */
  941. *mtu = min(*mtu, pmtuprobe ?
  942. rt->dst.dev->mtu :
  943. dst_mtu(rt->dst.path));
  944. }
  945. *maxfraglen = ((*mtu - fragheaderlen) & ~7)
  946. + fragheaderlen - sizeof(struct frag_hdr);
  947. }
  948. }
  949. int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
  950. int offset, int len, int odd, struct sk_buff *skb),
  951. void *from, int length, int transhdrlen,
  952. int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
  953. struct rt6_info *rt, unsigned int flags, int dontfrag)
  954. {
  955. struct inet_sock *inet = inet_sk(sk);
  956. struct ipv6_pinfo *np = inet6_sk(sk);
  957. struct inet_cork *cork;
  958. struct sk_buff *skb, *skb_prev = NULL;
  959. unsigned int maxfraglen, fragheaderlen, mtu;
  960. int exthdrlen;
  961. int dst_exthdrlen;
  962. int hh_len;
  963. int copy;
  964. int err;
  965. int offset = 0;
  966. __u8 tx_flags = 0;
  967. if (flags&MSG_PROBE)
  968. return 0;
  969. cork = &inet->cork.base;
  970. if (skb_queue_empty(&sk->sk_write_queue)) {
  971. /*
  972. * setup for corking
  973. */
  974. if (opt) {
  975. if (WARN_ON(np->cork.opt))
  976. return -EINVAL;
  977. np->cork.opt = kzalloc(opt->tot_len, sk->sk_allocation);
  978. if (unlikely(np->cork.opt == NULL))
  979. return -ENOBUFS;
  980. np->cork.opt->tot_len = opt->tot_len;
  981. np->cork.opt->opt_flen = opt->opt_flen;
  982. np->cork.opt->opt_nflen = opt->opt_nflen;
  983. np->cork.opt->dst0opt = ip6_opt_dup(opt->dst0opt,
  984. sk->sk_allocation);
  985. if (opt->dst0opt && !np->cork.opt->dst0opt)
  986. return -ENOBUFS;
  987. np->cork.opt->dst1opt = ip6_opt_dup(opt->dst1opt,
  988. sk->sk_allocation);
  989. if (opt->dst1opt && !np->cork.opt->dst1opt)
  990. return -ENOBUFS;
  991. np->cork.opt->hopopt = ip6_opt_dup(opt->hopopt,
  992. sk->sk_allocation);
  993. if (opt->hopopt && !np->cork.opt->hopopt)
  994. return -ENOBUFS;
  995. np->cork.opt->srcrt = ip6_rthdr_dup(opt->srcrt,
  996. sk->sk_allocation);
  997. if (opt->srcrt && !np->cork.opt->srcrt)
  998. return -ENOBUFS;
  999. /* need source address above miyazawa*/
  1000. }
  1001. dst_hold(&rt->dst);
  1002. cork->dst = &rt->dst;
  1003. inet->cork.fl.u.ip6 = *fl6;
  1004. np->cork.hop_limit = hlimit;
  1005. np->cork.tclass = tclass;
  1006. if (rt->dst.flags & DST_XFRM_TUNNEL)
  1007. mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
  1008. rt->dst.dev->mtu : dst_mtu(&rt->dst);
  1009. else
  1010. mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
  1011. rt->dst.dev->mtu : dst_mtu(rt->dst.path);
  1012. if (np->frag_size < mtu) {
  1013. if (np->frag_size)
  1014. mtu = np->frag_size;
  1015. }
  1016. cork->fragsize = mtu;
  1017. if (dst_allfrag(rt->dst.path))
  1018. cork->flags |= IPCORK_ALLFRAG;
  1019. cork->length = 0;
  1020. exthdrlen = (opt ? opt->opt_flen : 0);
  1021. length += exthdrlen;
  1022. transhdrlen += exthdrlen;
  1023. dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len;
  1024. } else {
  1025. rt = (struct rt6_info *)cork->dst;
  1026. fl6 = &inet->cork.fl.u.ip6;
  1027. opt = np->cork.opt;
  1028. transhdrlen = 0;
  1029. exthdrlen = 0;
  1030. dst_exthdrlen = 0;
  1031. mtu = cork->fragsize;
  1032. }
  1033. hh_len = LL_RESERVED_SPACE(rt->dst.dev);
  1034. fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
  1035. (opt ? opt->opt_nflen : 0);
  1036. maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
  1037. if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
  1038. if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
  1039. ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen);
  1040. return -EMSGSIZE;
  1041. }
  1042. }
  1043. /* For UDP, check if TX timestamp is enabled */
  1044. if (sk->sk_type == SOCK_DGRAM)
  1045. sock_tx_timestamp(sk, &tx_flags);
  1046. /*
  1047. * Let's try using as much space as possible.
  1048. * Use MTU if total length of the message fits into the MTU.
  1049. * Otherwise, we need to reserve fragment header and
  1050. * fragment alignment (= 8-15 octects, in total).
  1051. *
  1052. * Note that we may need to "move" the data from the tail of
  1053. * of the buffer to the new fragment when we split
  1054. * the message.
  1055. *
  1056. * FIXME: It may be fragmented into multiple chunks
  1057. * at once if non-fragmentable extension headers
  1058. * are too large.
  1059. * --yoshfuji
  1060. */
  1061. cork->length += length;
  1062. if (length > mtu) {
  1063. int proto = sk->sk_protocol;
  1064. if (dontfrag && (proto == IPPROTO_UDP || proto == IPPROTO_RAW)){
  1065. ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen);
  1066. return -EMSGSIZE;
  1067. }
  1068. if (proto == IPPROTO_UDP &&
  1069. (rt->dst.dev->features & NETIF_F_UFO)) {
  1070. err = ip6_ufo_append_data(sk, getfrag, from, length,
  1071. hh_len, fragheaderlen,
  1072. transhdrlen, mtu, flags, rt);
  1073. if (err)
  1074. goto error;
  1075. return 0;
  1076. }
  1077. }
  1078. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
  1079. goto alloc_new_skb;
  1080. while (length > 0) {
  1081. /* Check if the remaining data fits into current packet. */
  1082. copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
  1083. if (copy < length)
  1084. copy = maxfraglen - skb->len;
  1085. if (copy <= 0) {
  1086. char *data;
  1087. unsigned int datalen;
  1088. unsigned int fraglen;
  1089. unsigned int fraggap;
  1090. unsigned int alloclen;
  1091. alloc_new_skb:
  1092. /* There's no room in the current skb */
  1093. if (skb)
  1094. fraggap = skb->len - maxfraglen;
  1095. else
  1096. fraggap = 0;
  1097. /* update mtu and maxfraglen if necessary */
  1098. if (skb == NULL || skb_prev == NULL)
  1099. ip6_append_data_mtu(&mtu, &maxfraglen,
  1100. fragheaderlen, skb, rt,
  1101. np->pmtudisc ==
  1102. IPV6_PMTUDISC_PROBE);
  1103. skb_prev = skb;
  1104. /*
  1105. * If remaining data exceeds the mtu,
  1106. * we know we need more fragment(s).
  1107. */
  1108. datalen = length + fraggap;
  1109. if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
  1110. datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
  1111. if ((flags & MSG_MORE) &&
  1112. !(rt->dst.dev->features&NETIF_F_SG))
  1113. alloclen = mtu;
  1114. else
  1115. alloclen = datalen + fragheaderlen;
  1116. alloclen += dst_exthdrlen;
  1117. if (datalen != length + fraggap) {
  1118. /*
  1119. * this is not the last fragment, the trailer
  1120. * space is regarded as data space.
  1121. */
  1122. datalen += rt->dst.trailer_len;
  1123. }
  1124. alloclen += rt->dst.trailer_len;
  1125. fraglen = datalen + fragheaderlen;
  1126. /*
  1127. * We just reserve space for fragment header.
  1128. * Note: this may be overallocation if the message
  1129. * (without MSG_MORE) fits into the MTU.
  1130. */
  1131. alloclen += sizeof(struct frag_hdr);
  1132. if (transhdrlen) {
  1133. skb = sock_alloc_send_skb(sk,
  1134. alloclen + hh_len,
  1135. (flags & MSG_DONTWAIT), &err);
  1136. } else {
  1137. skb = NULL;
  1138. if (atomic_read(&sk->sk_wmem_alloc) <=
  1139. 2 * sk->sk_sndbuf)
  1140. skb = sock_wmalloc(sk,
  1141. alloclen + hh_len, 1,
  1142. sk->sk_allocation);
  1143. if (unlikely(skb == NULL))
  1144. err = -ENOBUFS;
  1145. else {
  1146. /* Only the initial fragment
  1147. * is time stamped.
  1148. */
  1149. tx_flags = 0;
  1150. }
  1151. }
  1152. if (skb == NULL)
  1153. goto error;
  1154. /*
  1155. * Fill in the control structures
  1156. */
  1157. skb->protocol = htons(ETH_P_IPV6);
  1158. skb->ip_summed = CHECKSUM_NONE;
  1159. skb->csum = 0;
  1160. /* reserve for fragmentation and ipsec header */
  1161. skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
  1162. dst_exthdrlen);
  1163. if (sk->sk_type == SOCK_DGRAM)
  1164. skb_shinfo(skb)->tx_flags = tx_flags;
  1165. /*
  1166. * Find where to start putting bytes
  1167. */
  1168. data = skb_put(skb, fraglen);
  1169. skb_set_network_header(skb, exthdrlen);
  1170. data += fragheaderlen;
  1171. skb->transport_header = (skb->network_header +
  1172. fragheaderlen);
  1173. if (fraggap) {
  1174. skb->csum = skb_copy_and_csum_bits(
  1175. skb_prev, maxfraglen,
  1176. data + transhdrlen, fraggap, 0);
  1177. skb_prev->csum = csum_sub(skb_prev->csum,
  1178. skb->csum);
  1179. data += fraggap;
  1180. pskb_trim_unique(skb_prev, maxfraglen);
  1181. }
  1182. copy = datalen - transhdrlen - fraggap;
  1183. if (copy < 0) {
  1184. err = -EINVAL;
  1185. kfree_skb(skb);
  1186. goto error;
  1187. } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
  1188. err = -EFAULT;
  1189. kfree_skb(skb);
  1190. goto error;
  1191. }
  1192. offset += copy;
  1193. length -= datalen - fraggap;
  1194. transhdrlen = 0;
  1195. exthdrlen = 0;
  1196. dst_exthdrlen = 0;
  1197. /*
  1198. * Put the packet on the pending queue
  1199. */
  1200. __skb_queue_tail(&sk->sk_write_queue, skb);
  1201. continue;
  1202. }
  1203. if (copy > length)
  1204. copy = length;
  1205. if (!(rt->dst.dev->features&NETIF_F_SG)) {
  1206. unsigned int off;
  1207. off = skb->len;
  1208. if (getfrag(from, skb_put(skb, copy),
  1209. offset, copy, off, skb) < 0) {
  1210. __skb_trim(skb, off);
  1211. err = -EFAULT;
  1212. goto error;
  1213. }
  1214. } else {
  1215. int i = skb_shinfo(skb)->nr_frags;
  1216. struct page_frag *pfrag = sk_page_frag(sk);
  1217. err = -ENOMEM;
  1218. if (!sk_page_frag_refill(sk, pfrag))
  1219. goto error;
  1220. if (!skb_can_coalesce(skb, i, pfrag->page,
  1221. pfrag->offset)) {
  1222. err = -EMSGSIZE;
  1223. if (i == MAX_SKB_FRAGS)
  1224. goto error;
  1225. __skb_fill_page_desc(skb, i, pfrag->page,
  1226. pfrag->offset, 0);
  1227. skb_shinfo(skb)->nr_frags = ++i;
  1228. get_page(pfrag->page);
  1229. }
  1230. copy = min_t(int, copy, pfrag->size - pfrag->offset);
  1231. if (getfrag(from,
  1232. page_address(pfrag->page) + pfrag->offset,
  1233. offset, copy, skb->len, skb) < 0)
  1234. goto error_efault;
  1235. pfrag->offset += copy;
  1236. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
  1237. skb->len += copy;
  1238. skb->data_len += copy;
  1239. skb->truesize += copy;
  1240. atomic_add(copy, &sk->sk_wmem_alloc);
  1241. }
  1242. offset += copy;
  1243. length -= copy;
  1244. }
  1245. return 0;
  1246. error_efault:
  1247. err = -EFAULT;
  1248. error:
  1249. cork->length -= length;
  1250. IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  1251. return err;
  1252. }
  1253. EXPORT_SYMBOL_GPL(ip6_append_data);
  1254. static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np)
  1255. {
  1256. if (np->cork.opt) {
  1257. kfree(np->cork.opt->dst0opt);
  1258. kfree(np->cork.opt->dst1opt);
  1259. kfree(np->cork.opt->hopopt);
  1260. kfree(np->cork.opt->srcrt);
  1261. kfree(np->cork.opt);
  1262. np->cork.opt = NULL;
  1263. }
  1264. if (inet->cork.base.dst) {
  1265. dst_release(inet->cork.base.dst);
  1266. inet->cork.base.dst = NULL;
  1267. inet->cork.base.flags &= ~IPCORK_ALLFRAG;
  1268. }
  1269. memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
  1270. }
  1271. int ip6_push_pending_frames(struct sock *sk)
  1272. {
  1273. struct sk_buff *skb, *tmp_skb;
  1274. struct sk_buff **tail_skb;
  1275. struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
  1276. struct inet_sock *inet = inet_sk(sk);
  1277. struct ipv6_pinfo *np = inet6_sk(sk);
  1278. struct net *net = sock_net(sk);
  1279. struct ipv6hdr *hdr;
  1280. struct ipv6_txoptions *opt = np->cork.opt;
  1281. struct rt6_info *rt = (struct rt6_info *)inet->cork.base.dst;
  1282. struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
  1283. unsigned char proto = fl6->flowi6_proto;
  1284. int err = 0;
  1285. if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
  1286. goto out;
  1287. tail_skb = &(skb_shinfo(skb)->frag_list);
  1288. /* move skb->data to ip header from ext header */
  1289. if (skb->data < skb_network_header(skb))
  1290. __skb_pull(skb, skb_network_offset(skb));
  1291. while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
  1292. __skb_pull(tmp_skb, skb_network_header_len(skb));
  1293. *tail_skb = tmp_skb;
  1294. tail_skb = &(tmp_skb->next);
  1295. skb->len += tmp_skb->len;
  1296. skb->data_len += tmp_skb->len;
  1297. skb->truesize += tmp_skb->truesize;
  1298. tmp_skb->destructor = NULL;
  1299. tmp_skb->sk = NULL;
  1300. }
  1301. /* Allow local fragmentation. */
  1302. if (np->pmtudisc < IPV6_PMTUDISC_DO)
  1303. skb->local_df = 1;
  1304. *final_dst = fl6->daddr;
  1305. __skb_pull(skb, skb_network_header_len(skb));
  1306. if (opt && opt->opt_flen)
  1307. ipv6_push_frag_opts(skb, opt, &proto);
  1308. if (opt && opt->opt_nflen)
  1309. ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
  1310. skb_push(skb, sizeof(struct ipv6hdr));
  1311. skb_reset_network_header(skb);
  1312. hdr = ipv6_hdr(skb);
  1313. ip6_flow_hdr(hdr, np->cork.tclass, fl6->flowlabel);
  1314. hdr->hop_limit = np->cork.hop_limit;
  1315. hdr->nexthdr = proto;
  1316. hdr->saddr = fl6->saddr;
  1317. hdr->daddr = *final_dst;
  1318. skb->priority = sk->sk_priority;
  1319. skb->mark = sk->sk_mark;
  1320. skb_dst_set(skb, dst_clone(&rt->dst));
  1321. IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
  1322. if (proto == IPPROTO_ICMPV6) {
  1323. struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
  1324. ICMP6MSGOUT_INC_STATS_BH(net, idev, icmp6_hdr(skb)->icmp6_type);
  1325. ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
  1326. }
  1327. err = ip6_local_out(skb);
  1328. if (err) {
  1329. if (err > 0)
  1330. err = net_xmit_errno(err);
  1331. if (err)
  1332. goto error;
  1333. }
  1334. out:
  1335. ip6_cork_release(inet, np);
  1336. return err;
  1337. error:
  1338. IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  1339. goto out;
  1340. }
  1341. EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
  1342. void ip6_flush_pending_frames(struct sock *sk)
  1343. {
  1344. struct sk_buff *skb;
  1345. while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
  1346. if (skb_dst(skb))
  1347. IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
  1348. IPSTATS_MIB_OUTDISCARDS);
  1349. kfree_skb(skb);
  1350. }
  1351. ip6_cork_release(inet_sk(sk), inet6_sk(sk));
  1352. }
  1353. EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);