ip6_output.c 46 KB

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