ip6_output.c 46 KB

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