ip6_output.c 46 KB

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