ip6_output.c 40 KB

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