ip6_output.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPv6 output functions
  4. * Linux INET6 implementation
  5. *
  6. * Authors:
  7. * Pedro Roque <roque@di.fc.ul.pt>
  8. *
  9. * Based on linux/net/ipv4/ip_output.c
  10. *
  11. * Changes:
  12. * A.N.Kuznetsov : airthmetics in fragmentation.
  13. * extension headers are implemented.
  14. * route changes now work.
  15. * ip6_forward does not confuse sniffers.
  16. * etc.
  17. *
  18. * H. von Brand : Added missing #include <linux/string.h>
  19. * Imran Patel : frag id should be in NBO
  20. * Kazunori MIYAZAWA @USAGI
  21. * : add ip6_append_data and related functions
  22. * for datagram xmit
  23. */
  24. #include <linux/errno.h>
  25. #include <linux/kernel.h>
  26. #include <linux/string.h>
  27. #include <linux/socket.h>
  28. #include <linux/net.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/in6.h>
  32. #include <linux/tcp.h>
  33. #include <linux/route.h>
  34. #include <linux/module.h>
  35. #include <linux/slab.h>
  36. #include <linux/bpf-cgroup.h>
  37. #include <linux/netfilter.h>
  38. #include <linux/netfilter_ipv6.h>
  39. #include <net/sock.h>
  40. #include <net/snmp.h>
  41. #include <net/gso.h>
  42. #include <net/ipv6.h>
  43. #include <net/ndisc.h>
  44. #include <net/protocol.h>
  45. #include <net/ip6_route.h>
  46. #include <net/addrconf.h>
  47. #include <net/rawv6.h>
  48. #include <net/icmp.h>
  49. #include <net/xfrm.h>
  50. #include <net/checksum.h>
  51. #include <linux/mroute6.h>
  52. #include <net/l3mdev.h>
  53. #include <net/lwtunnel.h>
  54. #include <net/ip_tunnels.h>
  55. static int ip6_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
  56. {
  57. struct dst_entry *dst = skb_dst(skb);
  58. struct net_device *dev = dst_dev_rcu(dst);
  59. struct inet6_dev *idev = ip6_dst_idev(dst);
  60. unsigned int hh_len = LL_RESERVED_SPACE(dev);
  61. const struct in6_addr *daddr, *nexthop;
  62. struct ipv6hdr *hdr;
  63. struct neighbour *neigh;
  64. int ret;
  65. /* Be paranoid, rather than too clever. */
  66. if (unlikely(hh_len > skb_headroom(skb)) && dev->header_ops) {
  67. /* idev stays alive because we hold rcu_read_lock(). */
  68. skb = skb_expand_head(skb, hh_len);
  69. if (!skb) {
  70. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
  71. return -ENOMEM;
  72. }
  73. }
  74. hdr = ipv6_hdr(skb);
  75. daddr = &hdr->daddr;
  76. if (unlikely(ipv6_addr_is_multicast(daddr))) {
  77. if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) &&
  78. ((mroute6_is_socket(net, skb) &&
  79. !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
  80. ipv6_chk_mcast_addr(dev, daddr, &hdr->saddr))) {
  81. struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
  82. /* Do not check for IFF_ALLMULTI; multicast routing
  83. is not supported in any case.
  84. */
  85. if (newskb)
  86. NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  87. net, sk, newskb, NULL, newskb->dev,
  88. dev_loopback_xmit);
  89. if (hdr->hop_limit == 0) {
  90. IP6_INC_STATS(net, idev,
  91. IPSTATS_MIB_OUTDISCARDS);
  92. kfree_skb(skb);
  93. return 0;
  94. }
  95. }
  96. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, skb->len);
  97. if (IPV6_ADDR_MC_SCOPE(daddr) <= IPV6_ADDR_SCOPE_NODELOCAL &&
  98. !(dev->flags & IFF_LOOPBACK)) {
  99. kfree_skb(skb);
  100. return 0;
  101. }
  102. }
  103. if (lwtunnel_xmit_redirect(dst->lwtstate)) {
  104. int res = lwtunnel_xmit(skb);
  105. if (res != LWTUNNEL_XMIT_CONTINUE)
  106. return res;
  107. }
  108. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
  109. nexthop = rt6_nexthop(dst_rt6_info(dst), daddr);
  110. neigh = __ipv6_neigh_lookup_noref(dev, nexthop);
  111. if (IS_ERR_OR_NULL(neigh)) {
  112. if (unlikely(!neigh))
  113. neigh = __neigh_create(&nd_tbl, nexthop, dev, false);
  114. if (IS_ERR(neigh)) {
  115. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTNOROUTES);
  116. kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_CREATEFAIL);
  117. return -EINVAL;
  118. }
  119. }
  120. sock_confirm_neigh(skb, neigh);
  121. ret = neigh_output(neigh, skb, false);
  122. return ret;
  123. }
  124. static int
  125. ip6_finish_output_gso_slowpath_drop(struct net *net, struct sock *sk,
  126. struct sk_buff *skb, unsigned int mtu)
  127. {
  128. struct sk_buff *segs, *nskb;
  129. netdev_features_t features;
  130. int ret = 0;
  131. /* Please see corresponding comment in ip_finish_output_gso
  132. * describing the cases where GSO segment length exceeds the
  133. * egress MTU.
  134. */
  135. features = netif_skb_features(skb);
  136. segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
  137. if (IS_ERR_OR_NULL(segs)) {
  138. kfree_skb(skb);
  139. return -ENOMEM;
  140. }
  141. consume_skb(skb);
  142. skb_list_walk_safe(segs, segs, nskb) {
  143. int err;
  144. skb_mark_not_on_list(segs);
  145. /* Last GSO segment can be smaller than gso_size (and MTU).
  146. * Adding a fragment header would produce an "atomic fragment",
  147. * which is considered harmful (RFC-8021). Avoid that.
  148. */
  149. err = segs->len > mtu ?
  150. ip6_fragment(net, sk, segs, ip6_finish_output2) :
  151. ip6_finish_output2(net, sk, segs);
  152. if (err && ret == 0)
  153. ret = err;
  154. }
  155. return ret;
  156. }
  157. static int ip6_finish_output_gso(struct net *net, struct sock *sk,
  158. struct sk_buff *skb, unsigned int mtu)
  159. {
  160. if (unlikely(!skb_gso_validate_network_len(skb, mtu)))
  161. return ip6_finish_output_gso_slowpath_drop(net, sk, skb, mtu);
  162. return ip6_finish_output2(net, sk, skb);
  163. }
  164. static int __ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  165. {
  166. unsigned int mtu;
  167. #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
  168. /* Policy lookup after SNAT yielded a new policy */
  169. if (skb_dst(skb)->xfrm) {
  170. IP6CB(skb)->flags |= IP6SKB_REROUTED;
  171. return dst_output(net, sk, skb);
  172. }
  173. #endif
  174. mtu = ip6_skb_dst_mtu(skb);
  175. if (skb_is_gso(skb))
  176. return ip6_finish_output_gso(net, sk, skb, mtu);
  177. if (unlikely(skb->len > mtu ||
  178. (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size)))
  179. return ip6_fragment(net, sk, skb, ip6_finish_output2);
  180. return ip6_finish_output2(net, sk, skb);
  181. }
  182. static int ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  183. {
  184. int ret;
  185. ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
  186. switch (ret) {
  187. case NET_XMIT_SUCCESS:
  188. case NET_XMIT_CN:
  189. return __ip6_finish_output(net, sk, skb) ? : ret;
  190. default:
  191. kfree_skb_reason(skb, SKB_DROP_REASON_BPF_CGROUP_EGRESS);
  192. return ret;
  193. }
  194. }
  195. int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  196. {
  197. struct dst_entry *dst = skb_dst(skb);
  198. struct net_device *dev, *indev = skb->dev;
  199. struct inet6_dev *idev;
  200. int ret;
  201. skb->protocol = htons(ETH_P_IPV6);
  202. rcu_read_lock();
  203. dev = dst_dev_rcu(dst);
  204. idev = ip6_dst_idev(dst);
  205. skb->dev = dev;
  206. if (unlikely(!idev || READ_ONCE(idev->cnf.disable_ipv6))) {
  207. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
  208. rcu_read_unlock();
  209. kfree_skb_reason(skb, SKB_DROP_REASON_IPV6DISABLED);
  210. return 0;
  211. }
  212. ret = NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  213. net, sk, skb, indev, dev,
  214. ip6_finish_output,
  215. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  216. rcu_read_unlock();
  217. return ret;
  218. }
  219. EXPORT_SYMBOL(ip6_output);
  220. bool ip6_autoflowlabel(struct net *net, const struct sock *sk)
  221. {
  222. if (!inet6_test_bit(AUTOFLOWLABEL_SET, sk))
  223. return ip6_default_np_autolabel(net);
  224. return inet6_test_bit(AUTOFLOWLABEL, sk);
  225. }
  226. /*
  227. * xmit an sk_buff (used by TCP and SCTP)
  228. * Note : socket lock is not held for SYNACK packets, but might be modified
  229. * by calls to skb_set_owner_w() and ipv6_local_error(),
  230. * which are using proper atomic operations or spinlocks.
  231. */
  232. int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
  233. __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority)
  234. {
  235. const struct ipv6_pinfo *np = inet6_sk(sk);
  236. struct in6_addr *first_hop = &fl6->daddr;
  237. struct dst_entry *dst = skb_dst(skb);
  238. struct inet6_dev *idev = ip6_dst_idev(dst);
  239. struct net *net = sock_net(sk);
  240. unsigned int head_room;
  241. struct net_device *dev;
  242. struct ipv6hdr *hdr;
  243. u8 proto = fl6->flowi6_proto;
  244. int seg_len = skb->len;
  245. int ret, hlimit = -1;
  246. u32 mtu;
  247. rcu_read_lock();
  248. dev = dst_dev_rcu(dst);
  249. head_room = sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dev);
  250. if (opt)
  251. head_room += opt->opt_nflen + opt->opt_flen;
  252. if (unlikely(head_room > skb_headroom(skb))) {
  253. /* idev stays alive while we hold rcu_read_lock(). */
  254. skb = skb_expand_head(skb, head_room);
  255. if (!skb) {
  256. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
  257. ret = -ENOBUFS;
  258. goto unlock;
  259. }
  260. }
  261. if (unlikely(opt)) {
  262. seg_len += opt->opt_nflen + opt->opt_flen;
  263. if (opt->opt_flen)
  264. proto = ipv6_push_frag_opts(skb, opt, proto);
  265. if (opt->opt_nflen)
  266. proto = ipv6_push_nfrag_opts(skb, opt, proto,
  267. &first_hop,
  268. &fl6->saddr);
  269. }
  270. if (unlikely(seg_len > IPV6_MAXPLEN))
  271. seg_len = 0;
  272. __skb_push(skb, sizeof(struct ipv6hdr));
  273. skb_reset_network_header(skb);
  274. hdr = ipv6_hdr(skb);
  275. /*
  276. * Fill in the IPv6 header
  277. */
  278. if (np)
  279. hlimit = READ_ONCE(np->hop_limit);
  280. if (hlimit < 0)
  281. hlimit = ip6_dst_hoplimit(dst);
  282. ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
  283. ip6_autoflowlabel(net, sk), fl6));
  284. hdr->payload_len = htons(seg_len);
  285. hdr->nexthdr = proto;
  286. hdr->hop_limit = hlimit;
  287. hdr->saddr = fl6->saddr;
  288. hdr->daddr = *first_hop;
  289. skb->protocol = htons(ETH_P_IPV6);
  290. skb->priority = priority;
  291. skb->mark = mark;
  292. mtu = dst6_mtu(dst);
  293. if (likely((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb))) {
  294. IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTREQUESTS);
  295. /* if egress device is enslaved to an L3 master device pass the
  296. * skb to its handler for processing
  297. */
  298. skb = l3mdev_ip6_out((struct sock *)sk, skb);
  299. if (unlikely(!skb)) {
  300. ret = 0;
  301. goto unlock;
  302. }
  303. /* hooks should never assume socket lock is held.
  304. * we promote our socket to non const
  305. */
  306. ret = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
  307. net, (struct sock *)sk, skb, NULL, dev,
  308. dst_output);
  309. goto unlock;
  310. }
  311. ret = -EMSGSIZE;
  312. skb->dev = dev;
  313. /* ipv6_local_error() does not require socket lock,
  314. * we promote our socket to non const
  315. */
  316. ipv6_local_error((struct sock *)sk, EMSGSIZE, fl6, mtu);
  317. IP6_INC_STATS(net, idev, IPSTATS_MIB_FRAGFAILS);
  318. kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_BIG);
  319. unlock:
  320. rcu_read_unlock();
  321. return ret;
  322. }
  323. EXPORT_SYMBOL(ip6_xmit);
  324. static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
  325. {
  326. struct ip6_ra_chain *ra;
  327. struct sock *last = NULL;
  328. read_lock(&ip6_ra_lock);
  329. for (ra = ip6_ra_chain; ra; ra = ra->next) {
  330. struct sock *sk = ra->sk;
  331. if (sk && ra->sel == sel &&
  332. (!sk->sk_bound_dev_if ||
  333. sk->sk_bound_dev_if == skb->dev->ifindex)) {
  334. if (inet6_test_bit(RTALERT_ISOLATE, sk) &&
  335. !net_eq(sock_net(sk), dev_net(skb->dev))) {
  336. continue;
  337. }
  338. if (last) {
  339. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  340. if (skb2)
  341. rawv6_rcv(last, skb2);
  342. }
  343. last = sk;
  344. }
  345. }
  346. if (last) {
  347. rawv6_rcv(last, skb);
  348. read_unlock(&ip6_ra_lock);
  349. return 1;
  350. }
  351. read_unlock(&ip6_ra_lock);
  352. return 0;
  353. }
  354. static int ip6_forward_proxy_check(struct sk_buff *skb)
  355. {
  356. struct ipv6hdr *hdr = ipv6_hdr(skb);
  357. u8 nexthdr = hdr->nexthdr;
  358. __be16 frag_off;
  359. int offset;
  360. if (ipv6_ext_hdr(nexthdr)) {
  361. offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
  362. if (offset < 0)
  363. return 0;
  364. } else
  365. offset = sizeof(struct ipv6hdr);
  366. if (nexthdr == IPPROTO_ICMPV6) {
  367. struct icmp6hdr *icmp6;
  368. if (!pskb_may_pull(skb, (skb_network_header(skb) +
  369. offset + 1 - skb->data)))
  370. return 0;
  371. icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  372. switch (icmp6->icmp6_type) {
  373. case NDISC_ROUTER_SOLICITATION:
  374. case NDISC_ROUTER_ADVERTISEMENT:
  375. case NDISC_NEIGHBOUR_SOLICITATION:
  376. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  377. case NDISC_REDIRECT:
  378. /* For reaction involving unicast neighbor discovery
  379. * message destined to the proxied address, pass it to
  380. * input function.
  381. */
  382. return 1;
  383. default:
  384. break;
  385. }
  386. }
  387. /*
  388. * The proxying router can't forward traffic sent to a link-local
  389. * address, so signal the sender and discard the packet. This
  390. * behavior is clarified by the MIPv6 specification.
  391. */
  392. if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
  393. dst_link_failure(skb);
  394. return -1;
  395. }
  396. return 0;
  397. }
  398. static inline int ip6_forward_finish(struct net *net, struct sock *sk,
  399. struct sk_buff *skb)
  400. {
  401. #ifdef CONFIG_NET_SWITCHDEV
  402. if (skb->offload_l3_fwd_mark) {
  403. consume_skb(skb);
  404. return 0;
  405. }
  406. #endif
  407. skb_clear_tstamp(skb);
  408. return dst_output(net, sk, skb);
  409. }
  410. static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  411. {
  412. if (skb->len <= mtu)
  413. return false;
  414. /* ipv6 conntrack defrag sets max_frag_size + ignore_df */
  415. if (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu)
  416. return true;
  417. if (skb->ignore_df)
  418. return false;
  419. if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
  420. return false;
  421. return true;
  422. }
  423. int ip6_forward(struct sk_buff *skb)
  424. {
  425. struct dst_entry *dst = skb_dst(skb);
  426. struct ipv6hdr *hdr = ipv6_hdr(skb);
  427. struct inet6_skb_parm *opt = IP6CB(skb);
  428. struct net *net = dev_net(dst_dev(dst));
  429. struct net_device *dev;
  430. struct inet6_dev *idev;
  431. SKB_DR(reason);
  432. u32 mtu;
  433. idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
  434. if (!READ_ONCE(net->ipv6.devconf_all->forwarding) &&
  435. (!idev || !READ_ONCE(idev->cnf.force_forwarding)))
  436. goto error;
  437. if (skb->pkt_type != PACKET_HOST)
  438. goto drop;
  439. if (unlikely(skb->sk))
  440. goto drop;
  441. if (skb_warn_if_lro(skb))
  442. goto drop;
  443. if (!READ_ONCE(net->ipv6.devconf_all->disable_policy) &&
  444. (!idev || !READ_ONCE(idev->cnf.disable_policy)) &&
  445. !xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
  446. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
  447. goto drop;
  448. }
  449. skb_forward_csum(skb);
  450. /*
  451. * We DO NOT make any processing on
  452. * RA packets, pushing them to user level AS IS
  453. * without ane WARRANTY that application will be able
  454. * to interpret them. The reason is that we
  455. * cannot make anything clever here.
  456. *
  457. * We are not end-node, so that if packet contains
  458. * AH/ESP, we cannot make anything.
  459. * Defragmentation also would be mistake, RA packets
  460. * cannot be fragmented, because there is no warranty
  461. * that different fragments will go along one path. --ANK
  462. */
  463. if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
  464. if (ip6_call_ra_chain(skb, ntohs(opt->ra)))
  465. return 0;
  466. }
  467. /*
  468. * check and decrement ttl
  469. */
  470. if (hdr->hop_limit <= 1) {
  471. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
  472. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
  473. kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
  474. return -ETIMEDOUT;
  475. }
  476. /* XXX: idev->cnf.proxy_ndp? */
  477. if (READ_ONCE(net->ipv6.devconf_all->proxy_ndp) &&
  478. pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev)) {
  479. int proxied = ip6_forward_proxy_check(skb);
  480. if (proxied > 0) {
  481. /* It's tempting to decrease the hop limit
  482. * here by 1, as we do at the end of the
  483. * function too.
  484. *
  485. * But that would be incorrect, as proxying is
  486. * not forwarding. The ip6_input function
  487. * will handle this packet locally, and it
  488. * depends on the hop limit being unchanged.
  489. *
  490. * One example is the NDP hop limit, that
  491. * always has to stay 255, but other would be
  492. * similar checks around RA packets, where the
  493. * user can even change the desired limit.
  494. */
  495. return ip6_input(skb);
  496. } else if (proxied < 0) {
  497. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
  498. goto drop;
  499. }
  500. }
  501. if (!xfrm6_route_forward(skb)) {
  502. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
  503. SKB_DR_SET(reason, XFRM_POLICY);
  504. goto drop;
  505. }
  506. dst = skb_dst(skb);
  507. dev = dst_dev(dst);
  508. /* IPv6 specs say nothing about it, but it is clear that we cannot
  509. send redirects to source routed frames.
  510. We don't send redirects to frames decapsulated from IPsec.
  511. */
  512. if (IP6CB(skb)->iif == dev->ifindex &&
  513. opt->srcrt == 0 && !skb_sec_path(skb)) {
  514. struct in6_addr *target = NULL;
  515. struct inet_peer *peer;
  516. struct rt6_info *rt;
  517. /*
  518. * incoming and outgoing devices are the same
  519. * send a redirect.
  520. */
  521. rt = dst_rt6_info(dst);
  522. if (rt->rt6i_flags & RTF_GATEWAY)
  523. target = &rt->rt6i_gateway;
  524. else
  525. target = &hdr->daddr;
  526. rcu_read_lock();
  527. peer = inet_getpeer_v6(net->ipv6.peers, &hdr->daddr);
  528. /* Limit redirects both by destination (here)
  529. and by source (inside ndisc_send_redirect)
  530. */
  531. if (inet_peer_xrlim_allow(peer, 1*HZ))
  532. ndisc_send_redirect(skb, target);
  533. rcu_read_unlock();
  534. } else {
  535. int addrtype = ipv6_addr_type(&hdr->saddr);
  536. /* This check is security critical. */
  537. if (addrtype == IPV6_ADDR_ANY ||
  538. addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
  539. goto error;
  540. if (addrtype & IPV6_ADDR_LINKLOCAL) {
  541. icmpv6_send(skb, ICMPV6_DEST_UNREACH,
  542. ICMPV6_NOT_NEIGHBOUR, 0);
  543. goto error;
  544. }
  545. }
  546. __IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
  547. mtu = ip6_dst_mtu_maybe_forward(dst, true);
  548. if (mtu < IPV6_MIN_MTU)
  549. mtu = IPV6_MIN_MTU;
  550. if (unlikely(ip6_pkt_too_big(skb, mtu))) {
  551. /* Again, force OUTPUT device used as source address */
  552. skb->dev = dev;
  553. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  554. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INTOOBIGERRORS);
  555. __IP6_INC_STATS(net, ip6_dst_idev(dst),
  556. IPSTATS_MIB_FRAGFAILS);
  557. kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_BIG);
  558. return -EMSGSIZE;
  559. }
  560. if (skb_cow(skb, dev->hard_header_len)) {
  561. __IP6_INC_STATS(net, ip6_dst_idev(dst),
  562. IPSTATS_MIB_OUTDISCARDS);
  563. goto drop;
  564. }
  565. hdr = ipv6_hdr(skb);
  566. /* Mangling hops number delayed to point after skb COW */
  567. hdr->hop_limit--;
  568. return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
  569. net, NULL, skb, skb->dev, dev,
  570. ip6_forward_finish);
  571. error:
  572. __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
  573. SKB_DR_SET(reason, IP_INADDRERRORS);
  574. drop:
  575. kfree_skb_reason(skb, reason);
  576. return -EINVAL;
  577. }
  578. static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
  579. {
  580. to->pkt_type = from->pkt_type;
  581. to->priority = from->priority;
  582. to->protocol = from->protocol;
  583. skb_dst_drop(to);
  584. skb_dst_set(to, dst_clone(skb_dst(from)));
  585. to->dev = from->dev;
  586. to->mark = from->mark;
  587. skb_copy_hash(to, from);
  588. #ifdef CONFIG_NET_SCHED
  589. to->tc_index = from->tc_index;
  590. #endif
  591. nf_copy(to, from);
  592. skb_ext_copy(to, from);
  593. skb_copy_secmark(to, from);
  594. }
  595. int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr,
  596. u8 nexthdr, __be32 frag_id,
  597. struct ip6_fraglist_iter *iter)
  598. {
  599. unsigned int first_len;
  600. struct frag_hdr *fh;
  601. /* BUILD HEADER */
  602. *prevhdr = NEXTHDR_FRAGMENT;
  603. iter->tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
  604. if (!iter->tmp_hdr)
  605. return -ENOMEM;
  606. iter->frag = skb_shinfo(skb)->frag_list;
  607. skb_frag_list_init(skb);
  608. iter->offset = 0;
  609. iter->hlen = hlen;
  610. iter->frag_id = frag_id;
  611. iter->nexthdr = nexthdr;
  612. __skb_pull(skb, hlen);
  613. fh = __skb_push(skb, sizeof(struct frag_hdr));
  614. __skb_push(skb, hlen);
  615. skb_reset_network_header(skb);
  616. memcpy(skb_network_header(skb), iter->tmp_hdr, hlen);
  617. fh->nexthdr = nexthdr;
  618. fh->reserved = 0;
  619. fh->frag_off = htons(IP6_MF);
  620. fh->identification = frag_id;
  621. first_len = skb_pagelen(skb);
  622. skb->data_len = first_len - skb_headlen(skb);
  623. skb->len = first_len;
  624. ipv6_hdr(skb)->payload_len = htons(first_len - sizeof(struct ipv6hdr));
  625. return 0;
  626. }
  627. EXPORT_SYMBOL(ip6_fraglist_init);
  628. void ip6_fraglist_prepare(struct sk_buff *skb,
  629. struct ip6_fraglist_iter *iter)
  630. {
  631. struct sk_buff *frag = iter->frag;
  632. unsigned int hlen = iter->hlen;
  633. struct frag_hdr *fh;
  634. frag->ip_summed = CHECKSUM_NONE;
  635. skb_reset_transport_header(frag);
  636. fh = __skb_push(frag, sizeof(struct frag_hdr));
  637. __skb_push(frag, hlen);
  638. skb_reset_network_header(frag);
  639. memcpy(skb_network_header(frag), iter->tmp_hdr, hlen);
  640. iter->offset += skb->len - hlen - sizeof(struct frag_hdr);
  641. fh->nexthdr = iter->nexthdr;
  642. fh->reserved = 0;
  643. fh->frag_off = htons(iter->offset);
  644. if (frag->next)
  645. fh->frag_off |= htons(IP6_MF);
  646. fh->identification = iter->frag_id;
  647. ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
  648. ip6_copy_metadata(frag, skb);
  649. }
  650. EXPORT_SYMBOL(ip6_fraglist_prepare);
  651. void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu,
  652. unsigned short needed_tailroom, int hdr_room, u8 *prevhdr,
  653. u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state)
  654. {
  655. state->prevhdr = prevhdr;
  656. state->nexthdr = nexthdr;
  657. state->frag_id = frag_id;
  658. state->hlen = hlen;
  659. state->mtu = mtu;
  660. state->left = skb->len - hlen; /* Space per frame */
  661. state->ptr = hlen; /* Where to start from */
  662. state->hroom = hdr_room;
  663. state->troom = needed_tailroom;
  664. state->offset = 0;
  665. }
  666. EXPORT_SYMBOL(ip6_frag_init);
  667. struct sk_buff *ip6_frag_next(struct sk_buff *skb, struct ip6_frag_state *state)
  668. {
  669. u8 *prevhdr = state->prevhdr, *fragnexthdr_offset;
  670. struct sk_buff *frag;
  671. struct frag_hdr *fh;
  672. unsigned int len;
  673. len = state->left;
  674. /* IF: it doesn't fit, use 'mtu' - the data space left */
  675. if (len > state->mtu)
  676. len = state->mtu;
  677. /* IF: we are not sending up to and including the packet end
  678. then align the next start on an eight byte boundary */
  679. if (len < state->left)
  680. len &= ~7;
  681. /* Allocate buffer */
  682. frag = alloc_skb(len + state->hlen + sizeof(struct frag_hdr) +
  683. state->hroom + state->troom, GFP_ATOMIC);
  684. if (!frag)
  685. return ERR_PTR(-ENOMEM);
  686. /*
  687. * Set up data on packet
  688. */
  689. ip6_copy_metadata(frag, skb);
  690. skb_reserve(frag, state->hroom);
  691. skb_put(frag, len + state->hlen + sizeof(struct frag_hdr));
  692. skb_reset_network_header(frag);
  693. fh = (struct frag_hdr *)(skb_network_header(frag) + state->hlen);
  694. frag->transport_header = (frag->network_header + state->hlen +
  695. sizeof(struct frag_hdr));
  696. /*
  697. * Charge the memory for the fragment to any owner
  698. * it might possess
  699. */
  700. if (skb->sk)
  701. skb_set_owner_w(frag, skb->sk);
  702. /*
  703. * Copy the packet header into the new buffer.
  704. */
  705. skb_copy_from_linear_data(skb, skb_network_header(frag), state->hlen);
  706. fragnexthdr_offset = skb_network_header(frag);
  707. fragnexthdr_offset += prevhdr - skb_network_header(skb);
  708. *fragnexthdr_offset = NEXTHDR_FRAGMENT;
  709. /*
  710. * Build fragment header.
  711. */
  712. fh->nexthdr = state->nexthdr;
  713. fh->reserved = 0;
  714. fh->identification = state->frag_id;
  715. /*
  716. * Copy a block of the IP datagram.
  717. */
  718. BUG_ON(skb_copy_bits(skb, state->ptr, skb_transport_header(frag),
  719. len));
  720. state->left -= len;
  721. fh->frag_off = htons(state->offset);
  722. if (state->left > 0)
  723. fh->frag_off |= htons(IP6_MF);
  724. ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
  725. state->ptr += len;
  726. state->offset += len;
  727. return frag;
  728. }
  729. EXPORT_SYMBOL(ip6_frag_next);
  730. int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
  731. int (*output)(struct net *, struct sock *, struct sk_buff *))
  732. {
  733. struct sk_buff *frag;
  734. struct rt6_info *rt = dst_rt6_info(skb_dst(skb));
  735. struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
  736. inet6_sk(skb->sk) : NULL;
  737. u8 tstamp_type = skb->tstamp_type;
  738. struct ip6_frag_state state;
  739. unsigned int mtu, hlen, nexthdr_offset;
  740. ktime_t tstamp = skb->tstamp;
  741. int hroom, err = 0;
  742. __be32 frag_id;
  743. u8 *prevhdr, nexthdr = 0;
  744. err = ip6_find_1stfragopt(skb, &prevhdr);
  745. if (err < 0)
  746. goto fail;
  747. hlen = err;
  748. nexthdr = *prevhdr;
  749. nexthdr_offset = prevhdr - skb_network_header(skb);
  750. mtu = ip6_skb_dst_mtu(skb);
  751. /* We must not fragment if the socket is set to force MTU discovery
  752. * or if the skb it not generated by a local socket.
  753. */
  754. if (unlikely(!skb->ignore_df && skb->len > mtu))
  755. goto fail_toobig;
  756. if (IP6CB(skb)->frag_max_size) {
  757. if (IP6CB(skb)->frag_max_size > mtu)
  758. goto fail_toobig;
  759. /* don't send fragments larger than what we received */
  760. mtu = IP6CB(skb)->frag_max_size;
  761. if (mtu < IPV6_MIN_MTU)
  762. mtu = IPV6_MIN_MTU;
  763. }
  764. if (np) {
  765. u32 frag_size = READ_ONCE(np->frag_size);
  766. if (frag_size && frag_size < mtu)
  767. mtu = frag_size;
  768. }
  769. if (mtu < hlen + sizeof(struct frag_hdr) + 8)
  770. goto fail_toobig;
  771. mtu -= hlen + sizeof(struct frag_hdr);
  772. frag_id = ipv6_select_ident(net, &ipv6_hdr(skb)->daddr,
  773. &ipv6_hdr(skb)->saddr);
  774. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  775. (err = skb_checksum_help(skb)))
  776. goto fail;
  777. prevhdr = skb_network_header(skb) + nexthdr_offset;
  778. hroom = LL_RESERVED_SPACE(rt->dst.dev);
  779. if (skb_has_frag_list(skb)) {
  780. unsigned int first_len = skb_pagelen(skb);
  781. struct ip6_fraglist_iter iter;
  782. struct sk_buff *frag2;
  783. if (first_len - hlen > mtu ||
  784. ((first_len - hlen) & 7) ||
  785. skb_cloned(skb) ||
  786. skb_headroom(skb) < (hroom + sizeof(struct frag_hdr)))
  787. goto slow_path;
  788. skb_walk_frags(skb, frag) {
  789. /* Correct geometry. */
  790. if (frag->len > mtu ||
  791. ((frag->len & 7) && frag->next) ||
  792. skb_headroom(frag) < (hlen + hroom + sizeof(struct frag_hdr)))
  793. goto slow_path_clean;
  794. /* Partially cloned skb? */
  795. if (skb_shared(frag))
  796. goto slow_path_clean;
  797. BUG_ON(frag->sk);
  798. if (skb->sk) {
  799. frag->sk = skb->sk;
  800. frag->destructor = sock_wfree;
  801. }
  802. skb->truesize -= frag->truesize;
  803. }
  804. err = ip6_fraglist_init(skb, hlen, prevhdr, nexthdr, frag_id,
  805. &iter);
  806. if (err < 0)
  807. goto fail;
  808. /* We prevent @rt from being freed. */
  809. rcu_read_lock();
  810. for (;;) {
  811. /* Prepare header of the next frame,
  812. * before previous one went down. */
  813. if (iter.frag)
  814. ip6_fraglist_prepare(skb, &iter);
  815. skb_set_delivery_time(skb, tstamp, tstamp_type);
  816. err = output(net, sk, skb);
  817. if (!err)
  818. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  819. IPSTATS_MIB_FRAGCREATES);
  820. if (err || !iter.frag)
  821. break;
  822. skb = ip6_fraglist_next(&iter);
  823. }
  824. kfree(iter.tmp_hdr);
  825. if (err == 0) {
  826. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  827. IPSTATS_MIB_FRAGOKS);
  828. rcu_read_unlock();
  829. return 0;
  830. }
  831. kfree_skb_list(iter.frag);
  832. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  833. IPSTATS_MIB_FRAGFAILS);
  834. rcu_read_unlock();
  835. return err;
  836. slow_path_clean:
  837. skb_walk_frags(skb, frag2) {
  838. if (frag2 == frag)
  839. break;
  840. frag2->sk = NULL;
  841. frag2->destructor = NULL;
  842. skb->truesize += frag2->truesize;
  843. }
  844. }
  845. slow_path:
  846. /*
  847. * Fragment the datagram.
  848. */
  849. ip6_frag_init(skb, hlen, mtu, rt->dst.dev->needed_tailroom,
  850. LL_RESERVED_SPACE(rt->dst.dev), prevhdr, nexthdr, frag_id,
  851. &state);
  852. /*
  853. * Keep copying data until we run out.
  854. */
  855. while (state.left > 0) {
  856. frag = ip6_frag_next(skb, &state);
  857. if (IS_ERR(frag)) {
  858. err = PTR_ERR(frag);
  859. goto fail;
  860. }
  861. /*
  862. * Put this fragment into the sending queue.
  863. */
  864. skb_set_delivery_time(frag, tstamp, tstamp_type);
  865. err = output(net, sk, frag);
  866. if (err)
  867. goto fail;
  868. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  869. IPSTATS_MIB_FRAGCREATES);
  870. }
  871. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  872. IPSTATS_MIB_FRAGOKS);
  873. consume_skb(skb);
  874. return err;
  875. fail_toobig:
  876. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  877. err = -EMSGSIZE;
  878. fail:
  879. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  880. IPSTATS_MIB_FRAGFAILS);
  881. kfree_skb(skb);
  882. return err;
  883. }
  884. static inline int ip6_rt_check(const struct rt6key *rt_key,
  885. const struct in6_addr *fl_addr,
  886. const struct in6_addr *addr_cache)
  887. {
  888. return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
  889. (!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache));
  890. }
  891. static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
  892. struct dst_entry *dst,
  893. const struct flowi6 *fl6)
  894. {
  895. struct ipv6_pinfo *np = inet6_sk(sk);
  896. struct rt6_info *rt;
  897. if (!dst)
  898. goto out;
  899. if (dst->ops->family != AF_INET6) {
  900. dst_release(dst);
  901. return NULL;
  902. }
  903. rt = dst_rt6_info(dst);
  904. /* Yes, checking route validity in not connected
  905. * case is not very simple. Take into account,
  906. * that we do not support routing by source, TOS,
  907. * and MSG_DONTROUTE --ANK (980726)
  908. *
  909. * 1. ip6_rt_check(): If route was host route,
  910. * check that cached destination is current.
  911. * If it is network route, we still may
  912. * check its validity using saved pointer
  913. * to the last used address: daddr_cache.
  914. * We do not want to save whole address now,
  915. * (because main consumer of this service
  916. * is tcp, which has not this problem),
  917. * so that the last trick works only on connected
  918. * sockets.
  919. * 2. oif also should be the same.
  920. */
  921. if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr,
  922. np->daddr_cache ? &sk->sk_v6_daddr : NULL) ||
  923. #ifdef CONFIG_IPV6_SUBTREES
  924. ip6_rt_check(&rt->rt6i_src, &fl6->saddr,
  925. np->saddr_cache ? &np->saddr : NULL) ||
  926. #endif
  927. (fl6->flowi6_oif && fl6->flowi6_oif != dst_dev(dst)->ifindex)) {
  928. dst_release(dst);
  929. dst = NULL;
  930. }
  931. out:
  932. return dst;
  933. }
  934. static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
  935. struct dst_entry **dst, struct flowi6 *fl6)
  936. {
  937. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  938. struct neighbour *n;
  939. struct rt6_info *rt;
  940. #endif
  941. int err;
  942. int flags = 0;
  943. /* The correct way to handle this would be to do
  944. * ip6_route_get_saddr, and then ip6_route_output; however,
  945. * the route-specific preferred source forces the
  946. * ip6_route_output call _before_ ip6_route_get_saddr.
  947. *
  948. * In source specific routing (no src=any default route),
  949. * ip6_route_output will fail given src=any saddr, though, so
  950. * that's why we try it again later.
  951. */
  952. if (ipv6_addr_any(&fl6->saddr)) {
  953. struct fib6_info *from;
  954. struct rt6_info *rt;
  955. *dst = ip6_route_output(net, sk, fl6);
  956. rt = (*dst)->error ? NULL : dst_rt6_info(*dst);
  957. rcu_read_lock();
  958. from = rt ? rcu_dereference(rt->from) : NULL;
  959. err = ip6_route_get_saddr(net, from, &fl6->daddr,
  960. sk ? READ_ONCE(inet6_sk(sk)->srcprefs) : 0,
  961. fl6->flowi6_l3mdev,
  962. &fl6->saddr);
  963. rcu_read_unlock();
  964. if (err)
  965. goto out_err_release;
  966. /* If we had an erroneous initial result, pretend it
  967. * never existed and let the SA-enabled version take
  968. * over.
  969. */
  970. if ((*dst)->error) {
  971. dst_release(*dst);
  972. *dst = NULL;
  973. }
  974. if (fl6->flowi6_oif)
  975. flags |= RT6_LOOKUP_F_IFACE;
  976. }
  977. if (!*dst)
  978. *dst = ip6_route_output_flags(net, sk, fl6, flags);
  979. err = (*dst)->error;
  980. if (err)
  981. goto out_err_release;
  982. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  983. /*
  984. * Here if the dst entry we've looked up
  985. * has a neighbour entry that is in the INCOMPLETE
  986. * state and the src address from the flow is
  987. * marked as OPTIMISTIC, we release the found
  988. * dst entry and replace it instead with the
  989. * dst entry of the nexthop router
  990. */
  991. rt = dst_rt6_info(*dst);
  992. rcu_read_lock();
  993. n = __ipv6_neigh_lookup_noref(rt->dst.dev,
  994. rt6_nexthop(rt, &fl6->daddr));
  995. err = n && !(READ_ONCE(n->nud_state) & NUD_VALID) ? -EINVAL : 0;
  996. rcu_read_unlock();
  997. if (err) {
  998. struct inet6_ifaddr *ifp;
  999. struct flowi6 fl_gw6;
  1000. int redirect;
  1001. ifp = ipv6_get_ifaddr(net, &fl6->saddr,
  1002. (*dst)->dev, 1);
  1003. redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
  1004. if (ifp)
  1005. in6_ifa_put(ifp);
  1006. if (redirect) {
  1007. /*
  1008. * We need to get the dst entry for the
  1009. * default router instead
  1010. */
  1011. dst_release(*dst);
  1012. memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
  1013. memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
  1014. *dst = ip6_route_output(net, sk, &fl_gw6);
  1015. err = (*dst)->error;
  1016. if (err)
  1017. goto out_err_release;
  1018. }
  1019. }
  1020. #endif
  1021. if (ipv6_addr_v4mapped(&fl6->saddr) &&
  1022. !(ipv6_addr_v4mapped(&fl6->daddr) || ipv6_addr_any(&fl6->daddr))) {
  1023. err = -EAFNOSUPPORT;
  1024. goto out_err_release;
  1025. }
  1026. return 0;
  1027. out_err_release:
  1028. dst_release(*dst);
  1029. *dst = NULL;
  1030. if (err == -ENETUNREACH)
  1031. IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES);
  1032. return err;
  1033. }
  1034. /**
  1035. * ip6_dst_lookup - perform route lookup on flow
  1036. * @net: Network namespace to perform lookup in
  1037. * @sk: socket which provides route info
  1038. * @dst: pointer to dst_entry * for result
  1039. * @fl6: flow to lookup
  1040. *
  1041. * This function performs a route lookup on the given flow.
  1042. *
  1043. * It returns zero on success, or a standard errno code on error.
  1044. */
  1045. int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
  1046. struct flowi6 *fl6)
  1047. {
  1048. *dst = NULL;
  1049. return ip6_dst_lookup_tail(net, sk, dst, fl6);
  1050. }
  1051. EXPORT_SYMBOL_GPL(ip6_dst_lookup);
  1052. /**
  1053. * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
  1054. * @net: Network namespace to perform lookup in
  1055. * @sk: socket which provides route info
  1056. * @fl6: flow to lookup
  1057. * @final_dst: final destination address for ipsec lookup
  1058. *
  1059. * This function performs a route lookup on the given flow.
  1060. *
  1061. * It returns a valid dst pointer on success, or a pointer encoded
  1062. * error code.
  1063. */
  1064. struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6,
  1065. const struct in6_addr *final_dst)
  1066. {
  1067. struct dst_entry *dst = NULL;
  1068. int err;
  1069. err = ip6_dst_lookup_tail(net, sk, &dst, fl6);
  1070. if (err)
  1071. return ERR_PTR(err);
  1072. if (final_dst)
  1073. fl6->daddr = *final_dst;
  1074. return xfrm_lookup_route(net, dst, flowi6_to_flowi(fl6), sk, 0);
  1075. }
  1076. EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
  1077. /**
  1078. * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
  1079. * @sk: socket which provides the dst cache and route info
  1080. * @fl6: flow to lookup
  1081. * @final_dst: final destination address for ipsec lookup
  1082. * @connected: whether @sk is connected or not
  1083. *
  1084. * This function performs a route lookup on the given flow with the
  1085. * possibility of using the cached route in the socket if it is valid.
  1086. * It will take the socket dst lock when operating on the dst cache.
  1087. * As a result, this function can only be used in process context.
  1088. *
  1089. * In addition, for a connected socket, cache the dst in the socket
  1090. * if the current cache is not valid.
  1091. *
  1092. * It returns a valid dst pointer on success, or a pointer encoded
  1093. * error code.
  1094. */
  1095. struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
  1096. const struct in6_addr *final_dst,
  1097. bool connected)
  1098. {
  1099. struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
  1100. dst = ip6_sk_dst_check(sk, dst, fl6);
  1101. if (dst)
  1102. return dst;
  1103. dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_dst);
  1104. if (connected && !IS_ERR(dst))
  1105. ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6);
  1106. return dst;
  1107. }
  1108. EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
  1109. static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
  1110. gfp_t gfp)
  1111. {
  1112. return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
  1113. }
  1114. static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
  1115. gfp_t gfp)
  1116. {
  1117. return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
  1118. }
  1119. static void ip6_append_data_mtu(unsigned int *mtu,
  1120. int *maxfraglen,
  1121. unsigned int fragheaderlen,
  1122. struct sk_buff *skb,
  1123. struct rt6_info *rt,
  1124. unsigned int orig_mtu)
  1125. {
  1126. if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
  1127. if (!skb) {
  1128. /* first fragment, reserve header_len */
  1129. *mtu = orig_mtu - rt->dst.header_len;
  1130. } else {
  1131. /*
  1132. * this fragment is not first, the headers
  1133. * space is regarded as data space.
  1134. */
  1135. *mtu = orig_mtu;
  1136. }
  1137. *maxfraglen = ((*mtu - fragheaderlen) & ~7)
  1138. + fragheaderlen - sizeof(struct frag_hdr);
  1139. }
  1140. }
  1141. static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
  1142. struct ipcm6_cookie *ipc6,
  1143. struct rt6_info *rt)
  1144. {
  1145. struct ipv6_txoptions *nopt, *opt = ipc6->opt;
  1146. struct inet6_cork *v6_cork = &cork->base6;
  1147. struct ipv6_pinfo *np = inet6_sk(sk);
  1148. unsigned int mtu, frag_size;
  1149. /* callers pass dst together with a reference, set it first so
  1150. * ip6_cork_release() can put it down even in case of an error.
  1151. */
  1152. cork->base.dst = &rt->dst;
  1153. /*
  1154. * setup for corking
  1155. */
  1156. if (unlikely(opt)) {
  1157. if (WARN_ON(v6_cork->opt))
  1158. return -EINVAL;
  1159. nopt = v6_cork->opt = kzalloc_obj(*opt, sk->sk_allocation);
  1160. if (unlikely(!nopt))
  1161. return -ENOBUFS;
  1162. nopt->tot_len = sizeof(*opt);
  1163. nopt->opt_flen = opt->opt_flen;
  1164. nopt->opt_nflen = opt->opt_nflen;
  1165. nopt->dst0opt = ip6_opt_dup(opt->dst0opt, sk->sk_allocation);
  1166. if (opt->dst0opt && !nopt->dst0opt)
  1167. return -ENOBUFS;
  1168. nopt->dst1opt = ip6_opt_dup(opt->dst1opt, sk->sk_allocation);
  1169. if (opt->dst1opt && !nopt->dst1opt)
  1170. return -ENOBUFS;
  1171. nopt->hopopt = ip6_opt_dup(opt->hopopt, sk->sk_allocation);
  1172. if (opt->hopopt && !nopt->hopopt)
  1173. return -ENOBUFS;
  1174. nopt->srcrt = ip6_rthdr_dup(opt->srcrt, sk->sk_allocation);
  1175. if (opt->srcrt && !nopt->srcrt)
  1176. return -ENOBUFS;
  1177. /* need source address above miyazawa*/
  1178. }
  1179. v6_cork->hop_limit = ipc6->hlimit;
  1180. v6_cork->tclass = ipc6->tclass;
  1181. v6_cork->dontfrag = ipc6->dontfrag;
  1182. if (rt->dst.flags & DST_XFRM_TUNNEL)
  1183. mtu = READ_ONCE(np->pmtudisc) >= IPV6_PMTUDISC_PROBE ?
  1184. READ_ONCE(rt->dst.dev->mtu) : dst6_mtu(&rt->dst);
  1185. else
  1186. mtu = READ_ONCE(np->pmtudisc) >= IPV6_PMTUDISC_PROBE ?
  1187. READ_ONCE(rt->dst.dev->mtu) : dst6_mtu(xfrm_dst_path(&rt->dst));
  1188. frag_size = READ_ONCE(np->frag_size);
  1189. if (frag_size && frag_size < mtu)
  1190. mtu = frag_size;
  1191. cork->base.fragsize = mtu;
  1192. cork->base.gso_size = ipc6->gso_size;
  1193. cork->base.tx_flags = 0;
  1194. cork->base.mark = ipc6->sockc.mark;
  1195. cork->base.priority = ipc6->sockc.priority;
  1196. sock_tx_timestamp(sk, &ipc6->sockc, &cork->base.tx_flags);
  1197. if (ipc6->sockc.tsflags & SOCKCM_FLAG_TS_OPT_ID) {
  1198. cork->base.flags |= IPCORK_TS_OPT_ID;
  1199. cork->base.ts_opt_id = ipc6->sockc.ts_opt_id;
  1200. }
  1201. cork->base.length = 0;
  1202. cork->base.transmit_time = ipc6->sockc.transmit_time;
  1203. return 0;
  1204. }
  1205. static int __ip6_append_data(struct sock *sk,
  1206. struct sk_buff_head *queue,
  1207. struct inet_cork_full *cork_full,
  1208. struct page_frag *pfrag,
  1209. int getfrag(void *from, char *to, int offset,
  1210. int len, int odd, struct sk_buff *skb),
  1211. void *from, size_t length, int transhdrlen,
  1212. unsigned int flags)
  1213. {
  1214. unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu, pmtu;
  1215. struct inet6_cork *v6_cork = &cork_full->base6;
  1216. struct inet_cork *cork = &cork_full->base;
  1217. struct flowi6 *fl6 = &cork_full->fl.u.ip6;
  1218. struct sk_buff *skb, *skb_prev = NULL;
  1219. struct ubuf_info *uarg = NULL;
  1220. int exthdrlen = 0;
  1221. int dst_exthdrlen = 0;
  1222. int hh_len;
  1223. int copy;
  1224. int err;
  1225. int offset = 0;
  1226. bool zc = false;
  1227. u32 tskey = 0;
  1228. struct rt6_info *rt = dst_rt6_info(cork->dst);
  1229. bool paged, hold_tskey = false, extra_uref = false;
  1230. struct ipv6_txoptions *opt = v6_cork->opt;
  1231. int csummode = CHECKSUM_NONE;
  1232. unsigned int maxnonfragsize, headersize;
  1233. unsigned int wmem_alloc_delta = 0;
  1234. skb = skb_peek_tail(queue);
  1235. if (!skb) {
  1236. exthdrlen = opt ? opt->opt_flen : 0;
  1237. dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len;
  1238. }
  1239. paged = !!cork->gso_size;
  1240. mtu = cork->gso_size ? IP6_MAX_MTU : cork->fragsize;
  1241. orig_mtu = mtu;
  1242. hh_len = LL_RESERVED_SPACE(rt->dst.dev);
  1243. fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
  1244. (opt ? opt->opt_nflen : 0);
  1245. headersize = sizeof(struct ipv6hdr) +
  1246. (opt ? opt->opt_flen + opt->opt_nflen : 0) +
  1247. rt->rt6i_nfheader_len;
  1248. if (mtu <= fragheaderlen ||
  1249. ((mtu - fragheaderlen) & ~7) + fragheaderlen <= sizeof(struct frag_hdr))
  1250. goto emsgsize;
  1251. maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen -
  1252. sizeof(struct frag_hdr);
  1253. /* as per RFC 7112 section 5, the entire IPv6 Header Chain must fit
  1254. * the first fragment
  1255. */
  1256. if (headersize + transhdrlen > mtu)
  1257. goto emsgsize;
  1258. if (cork->length + length > mtu - headersize && v6_cork->dontfrag &&
  1259. (sk->sk_protocol == IPPROTO_UDP ||
  1260. sk->sk_protocol == IPPROTO_ICMPV6 ||
  1261. sk->sk_protocol == IPPROTO_RAW)) {
  1262. ipv6_local_rxpmtu(sk, fl6, mtu - headersize +
  1263. sizeof(struct ipv6hdr));
  1264. goto emsgsize;
  1265. }
  1266. if (ip6_sk_ignore_df(sk))
  1267. maxnonfragsize = sizeof(struct ipv6hdr) + IPV6_MAXPLEN;
  1268. else
  1269. maxnonfragsize = mtu;
  1270. if (cork->length + length > maxnonfragsize - headersize) {
  1271. emsgsize:
  1272. pmtu = max_t(int, mtu - headersize + sizeof(struct ipv6hdr), 0);
  1273. ipv6_local_error(sk, EMSGSIZE, fl6, pmtu);
  1274. return -EMSGSIZE;
  1275. }
  1276. /* CHECKSUM_PARTIAL only with no extension headers and when
  1277. * we are not going to fragment
  1278. */
  1279. if (transhdrlen && sk->sk_protocol == IPPROTO_UDP &&
  1280. headersize == sizeof(struct ipv6hdr) &&
  1281. length <= mtu - headersize &&
  1282. (!(flags & MSG_MORE) || cork->gso_size) &&
  1283. rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
  1284. csummode = CHECKSUM_PARTIAL;
  1285. if ((flags & MSG_ZEROCOPY) && length) {
  1286. struct msghdr *msg = from;
  1287. if (getfrag == ip_generic_getfrag && msg->msg_ubuf) {
  1288. if (skb_zcopy(skb) && msg->msg_ubuf != skb_zcopy(skb))
  1289. return -EINVAL;
  1290. /* Leave uarg NULL if can't zerocopy, callers should
  1291. * be able to handle it.
  1292. */
  1293. if ((rt->dst.dev->features & NETIF_F_SG) &&
  1294. csummode == CHECKSUM_PARTIAL) {
  1295. paged = true;
  1296. zc = true;
  1297. uarg = msg->msg_ubuf;
  1298. }
  1299. } else if (sock_flag(sk, SOCK_ZEROCOPY)) {
  1300. uarg = msg_zerocopy_realloc(sk, length, skb_zcopy(skb),
  1301. false);
  1302. if (!uarg)
  1303. return -ENOBUFS;
  1304. extra_uref = !skb_zcopy(skb); /* only ref on new uarg */
  1305. if (rt->dst.dev->features & NETIF_F_SG &&
  1306. csummode == CHECKSUM_PARTIAL) {
  1307. paged = true;
  1308. zc = true;
  1309. } else {
  1310. uarg_to_msgzc(uarg)->zerocopy = 0;
  1311. skb_zcopy_set(skb, uarg, &extra_uref);
  1312. }
  1313. }
  1314. } else if ((flags & MSG_SPLICE_PAGES) && length) {
  1315. if (inet_test_bit(HDRINCL, sk))
  1316. return -EPERM;
  1317. if (rt->dst.dev->features & NETIF_F_SG &&
  1318. getfrag == ip_generic_getfrag)
  1319. /* We need an empty buffer to attach stuff to */
  1320. paged = true;
  1321. else
  1322. flags &= ~MSG_SPLICE_PAGES;
  1323. }
  1324. if (cork->tx_flags & SKBTX_ANY_TSTAMP &&
  1325. READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID) {
  1326. if (cork->flags & IPCORK_TS_OPT_ID) {
  1327. tskey = cork->ts_opt_id;
  1328. } else {
  1329. tskey = atomic_inc_return(&sk->sk_tskey) - 1;
  1330. hold_tskey = true;
  1331. }
  1332. }
  1333. /*
  1334. * Let's try using as much space as possible.
  1335. * Use MTU if total length of the message fits into the MTU.
  1336. * Otherwise, we need to reserve fragment header and
  1337. * fragment alignment (= 8-15 octects, in total).
  1338. *
  1339. * Note that we may need to "move" the data from the tail
  1340. * of the buffer to the new fragment when we split
  1341. * the message.
  1342. *
  1343. * FIXME: It may be fragmented into multiple chunks
  1344. * at once if non-fragmentable extension headers
  1345. * are too large.
  1346. * --yoshfuji
  1347. */
  1348. cork->length += length;
  1349. if (!skb)
  1350. goto alloc_new_skb;
  1351. while (length > 0) {
  1352. /* Check if the remaining data fits into current packet. */
  1353. copy = (cork->length <= mtu ? mtu : maxfraglen) - skb->len;
  1354. if (copy < length)
  1355. copy = maxfraglen - skb->len;
  1356. if (copy <= 0) {
  1357. char *data;
  1358. unsigned int datalen;
  1359. unsigned int fraglen;
  1360. unsigned int fraggap;
  1361. unsigned int alloclen, alloc_extra;
  1362. unsigned int pagedlen;
  1363. alloc_new_skb:
  1364. /* There's no room in the current skb */
  1365. if (skb)
  1366. fraggap = skb->len - maxfraglen;
  1367. else
  1368. fraggap = 0;
  1369. /* update mtu and maxfraglen if necessary */
  1370. if (!skb || !skb_prev)
  1371. ip6_append_data_mtu(&mtu, &maxfraglen,
  1372. fragheaderlen, skb, rt,
  1373. orig_mtu);
  1374. skb_prev = skb;
  1375. /*
  1376. * If remaining data exceeds the mtu,
  1377. * we know we need more fragment(s).
  1378. */
  1379. datalen = length + fraggap;
  1380. if (datalen > (cork->length <= mtu ? mtu : maxfraglen) - fragheaderlen)
  1381. datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
  1382. fraglen = datalen + fragheaderlen;
  1383. pagedlen = 0;
  1384. alloc_extra = hh_len;
  1385. alloc_extra += dst_exthdrlen;
  1386. alloc_extra += rt->dst.trailer_len;
  1387. /* We just reserve space for fragment header.
  1388. * Note: this may be overallocation if the message
  1389. * (without MSG_MORE) fits into the MTU.
  1390. */
  1391. alloc_extra += sizeof(struct frag_hdr);
  1392. if ((flags & MSG_MORE) &&
  1393. !(rt->dst.dev->features&NETIF_F_SG))
  1394. alloclen = mtu;
  1395. else if (!paged &&
  1396. (fraglen + alloc_extra < SKB_MAX_ALLOC ||
  1397. !(rt->dst.dev->features & NETIF_F_SG)))
  1398. alloclen = fraglen;
  1399. else {
  1400. alloclen = fragheaderlen + transhdrlen;
  1401. pagedlen = datalen - transhdrlen;
  1402. }
  1403. alloclen += alloc_extra;
  1404. if (datalen != length + fraggap) {
  1405. /*
  1406. * this is not the last fragment, the trailer
  1407. * space is regarded as data space.
  1408. */
  1409. datalen += rt->dst.trailer_len;
  1410. }
  1411. fraglen = datalen + fragheaderlen;
  1412. copy = datalen - transhdrlen - fraggap - pagedlen;
  1413. /* [!] NOTE: copy may be negative if pagedlen>0
  1414. * because then the equation may reduces to -fraggap.
  1415. */
  1416. if (copy < 0 && !(flags & MSG_SPLICE_PAGES)) {
  1417. err = -EINVAL;
  1418. goto error;
  1419. }
  1420. if (transhdrlen) {
  1421. skb = sock_alloc_send_skb(sk, alloclen,
  1422. (flags & MSG_DONTWAIT), &err);
  1423. } else {
  1424. skb = NULL;
  1425. if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <=
  1426. 2 * sk->sk_sndbuf)
  1427. skb = alloc_skb(alloclen,
  1428. sk->sk_allocation);
  1429. if (unlikely(!skb))
  1430. err = -ENOBUFS;
  1431. }
  1432. if (!skb)
  1433. goto error;
  1434. /*
  1435. * Fill in the control structures
  1436. */
  1437. skb->protocol = htons(ETH_P_IPV6);
  1438. skb->ip_summed = csummode;
  1439. skb->csum = 0;
  1440. /* reserve for fragmentation and ipsec header */
  1441. skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
  1442. dst_exthdrlen);
  1443. /*
  1444. * Find where to start putting bytes
  1445. */
  1446. data = skb_put(skb, fraglen - pagedlen);
  1447. skb_set_network_header(skb, exthdrlen);
  1448. data += fragheaderlen;
  1449. skb->transport_header = (skb->network_header +
  1450. fragheaderlen);
  1451. if (fraggap) {
  1452. skb->csum = skb_copy_and_csum_bits(
  1453. skb_prev, maxfraglen,
  1454. data + transhdrlen, fraggap);
  1455. skb_prev->csum = csum_sub(skb_prev->csum,
  1456. skb->csum);
  1457. data += fraggap;
  1458. pskb_trim_unique(skb_prev, maxfraglen);
  1459. }
  1460. if (copy > 0 &&
  1461. INDIRECT_CALL_1(getfrag, ip_generic_getfrag,
  1462. from, data + transhdrlen, offset,
  1463. copy, fraggap, skb) < 0) {
  1464. err = -EFAULT;
  1465. kfree_skb(skb);
  1466. goto error;
  1467. } else if (flags & MSG_SPLICE_PAGES) {
  1468. copy = 0;
  1469. }
  1470. offset += copy;
  1471. length -= copy + transhdrlen;
  1472. transhdrlen = 0;
  1473. exthdrlen = 0;
  1474. dst_exthdrlen = 0;
  1475. /* Only the initial fragment is time stamped */
  1476. skb_shinfo(skb)->tx_flags = cork->tx_flags;
  1477. cork->tx_flags = 0;
  1478. skb_shinfo(skb)->tskey = tskey;
  1479. tskey = 0;
  1480. skb_zcopy_set(skb, uarg, &extra_uref);
  1481. if ((flags & MSG_CONFIRM) && !skb_prev)
  1482. skb_set_dst_pending_confirm(skb, 1);
  1483. /*
  1484. * Put the packet on the pending queue
  1485. */
  1486. if (!skb->destructor) {
  1487. skb->destructor = sock_wfree;
  1488. skb->sk = sk;
  1489. wmem_alloc_delta += skb->truesize;
  1490. }
  1491. __skb_queue_tail(queue, skb);
  1492. continue;
  1493. }
  1494. if (copy > length)
  1495. copy = length;
  1496. if (!(rt->dst.dev->features&NETIF_F_SG) &&
  1497. skb_tailroom(skb) >= copy) {
  1498. unsigned int off;
  1499. off = skb->len;
  1500. if (INDIRECT_CALL_1(getfrag, ip_generic_getfrag,
  1501. from, skb_put(skb, copy),
  1502. offset, copy, off, skb) < 0) {
  1503. __skb_trim(skb, off);
  1504. err = -EFAULT;
  1505. goto error;
  1506. }
  1507. } else if (flags & MSG_SPLICE_PAGES) {
  1508. struct msghdr *msg = from;
  1509. err = -EIO;
  1510. if (WARN_ON_ONCE(copy > msg->msg_iter.count))
  1511. goto error;
  1512. err = skb_splice_from_iter(skb, &msg->msg_iter, copy);
  1513. if (err < 0)
  1514. goto error;
  1515. copy = err;
  1516. wmem_alloc_delta += copy;
  1517. } else if (!zc) {
  1518. int i = skb_shinfo(skb)->nr_frags;
  1519. err = -ENOMEM;
  1520. if (!sk_page_frag_refill(sk, pfrag))
  1521. goto error;
  1522. skb_zcopy_downgrade_managed(skb);
  1523. if (!skb_can_coalesce(skb, i, pfrag->page,
  1524. pfrag->offset)) {
  1525. err = -EMSGSIZE;
  1526. if (i == MAX_SKB_FRAGS)
  1527. goto error;
  1528. __skb_fill_page_desc(skb, i, pfrag->page,
  1529. pfrag->offset, 0);
  1530. skb_shinfo(skb)->nr_frags = ++i;
  1531. get_page(pfrag->page);
  1532. }
  1533. copy = min_t(int, copy, pfrag->size - pfrag->offset);
  1534. if (INDIRECT_CALL_1(getfrag, ip_generic_getfrag,
  1535. from,
  1536. page_address(pfrag->page) + pfrag->offset,
  1537. offset, copy, skb->len, skb) < 0)
  1538. goto error_efault;
  1539. pfrag->offset += copy;
  1540. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
  1541. skb->len += copy;
  1542. skb->data_len += copy;
  1543. skb->truesize += copy;
  1544. wmem_alloc_delta += copy;
  1545. } else {
  1546. err = skb_zerocopy_iter_dgram(skb, from, copy);
  1547. if (err < 0)
  1548. goto error;
  1549. }
  1550. offset += copy;
  1551. length -= copy;
  1552. }
  1553. if (wmem_alloc_delta)
  1554. refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
  1555. return 0;
  1556. error_efault:
  1557. err = -EFAULT;
  1558. error:
  1559. net_zcopy_put_abort(uarg, extra_uref);
  1560. cork->length -= length;
  1561. IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  1562. refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
  1563. if (hold_tskey)
  1564. atomic_dec(&sk->sk_tskey);
  1565. return err;
  1566. }
  1567. int ip6_append_data(struct sock *sk,
  1568. int getfrag(void *from, char *to, int offset, int len,
  1569. int odd, struct sk_buff *skb),
  1570. void *from, size_t length, int transhdrlen,
  1571. struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
  1572. struct rt6_info *rt, unsigned int flags)
  1573. {
  1574. struct inet_sock *inet = inet_sk(sk);
  1575. int exthdrlen;
  1576. int err;
  1577. if (flags&MSG_PROBE)
  1578. return 0;
  1579. if (skb_queue_empty(&sk->sk_write_queue)) {
  1580. /*
  1581. * setup for corking
  1582. */
  1583. dst_hold(&rt->dst);
  1584. err = ip6_setup_cork(sk, &inet->cork,
  1585. ipc6, rt);
  1586. if (err)
  1587. return err;
  1588. inet->cork.fl.u.ip6 = *fl6;
  1589. exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
  1590. length += exthdrlen;
  1591. transhdrlen += exthdrlen;
  1592. } else {
  1593. transhdrlen = 0;
  1594. }
  1595. return __ip6_append_data(sk, &sk->sk_write_queue, &inet->cork,
  1596. sk_page_frag(sk), getfrag,
  1597. from, length, transhdrlen, flags);
  1598. }
  1599. EXPORT_SYMBOL_GPL(ip6_append_data);
  1600. static void ip6_cork_steal_dst(struct sk_buff *skb, struct inet_cork_full *cork)
  1601. {
  1602. struct dst_entry *dst = cork->base.dst;
  1603. cork->base.dst = NULL;
  1604. skb_dst_set(skb, dst);
  1605. }
  1606. static void ip6_cork_release(struct inet_cork_full *cork)
  1607. {
  1608. struct inet6_cork *v6_cork = &cork->base6;
  1609. if (unlikely(v6_cork->opt)) {
  1610. struct ipv6_txoptions *opt = v6_cork->opt;
  1611. kfree(opt->dst0opt);
  1612. kfree(opt->dst1opt);
  1613. kfree(opt->hopopt);
  1614. kfree(opt->srcrt);
  1615. kfree(opt);
  1616. v6_cork->opt = NULL;
  1617. }
  1618. if (cork->base.dst) {
  1619. dst_release(cork->base.dst);
  1620. cork->base.dst = NULL;
  1621. }
  1622. }
  1623. struct sk_buff *__ip6_make_skb(struct sock *sk,
  1624. struct sk_buff_head *queue,
  1625. struct inet_cork_full *cork)
  1626. {
  1627. struct sk_buff *skb, *tmp_skb;
  1628. struct sk_buff **tail_skb;
  1629. struct in6_addr *final_dst;
  1630. struct net *net = sock_net(sk);
  1631. struct ipv6hdr *hdr;
  1632. struct ipv6_txoptions *opt;
  1633. struct rt6_info *rt = dst_rt6_info(cork->base.dst);
  1634. struct flowi6 *fl6 = &cork->fl.u.ip6;
  1635. unsigned char proto = fl6->flowi6_proto;
  1636. skb = __skb_dequeue(queue);
  1637. if (!skb)
  1638. goto out;
  1639. tail_skb = &(skb_shinfo(skb)->frag_list);
  1640. /* move skb->data to ip header from ext header */
  1641. if (skb->data < skb_network_header(skb))
  1642. __skb_pull(skb, skb_network_offset(skb));
  1643. while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
  1644. __skb_pull(tmp_skb, skb_network_header_len(skb));
  1645. *tail_skb = tmp_skb;
  1646. tail_skb = &(tmp_skb->next);
  1647. skb->len += tmp_skb->len;
  1648. skb->data_len += tmp_skb->len;
  1649. skb->truesize += tmp_skb->truesize;
  1650. tmp_skb->destructor = NULL;
  1651. tmp_skb->sk = NULL;
  1652. }
  1653. /* Allow local fragmentation. */
  1654. skb->ignore_df = ip6_sk_ignore_df(sk);
  1655. __skb_pull(skb, skb_network_header_len(skb));
  1656. final_dst = &fl6->daddr;
  1657. opt = cork->base6.opt;
  1658. if (unlikely(opt)) {
  1659. if (opt->opt_flen)
  1660. proto = ipv6_push_frag_opts(skb, opt, proto);
  1661. if (opt->opt_nflen)
  1662. proto = ipv6_push_nfrag_opts(skb, opt, proto,
  1663. &final_dst, &fl6->saddr);
  1664. }
  1665. skb_push(skb, sizeof(struct ipv6hdr));
  1666. skb_reset_network_header(skb);
  1667. hdr = ipv6_hdr(skb);
  1668. ip6_flow_hdr(hdr, cork->base6.tclass,
  1669. ip6_make_flowlabel(net, skb, fl6->flowlabel,
  1670. ip6_autoflowlabel(net, sk), fl6));
  1671. hdr->hop_limit = cork->base6.hop_limit;
  1672. hdr->nexthdr = proto;
  1673. hdr->saddr = fl6->saddr;
  1674. hdr->daddr = *final_dst;
  1675. skb->priority = cork->base.priority;
  1676. skb->mark = cork->base.mark;
  1677. if (sk_is_tcp(sk))
  1678. skb_set_delivery_time(skb, cork->base.transmit_time, SKB_CLOCK_MONOTONIC);
  1679. else
  1680. skb_set_delivery_type_by_clockid(skb, cork->base.transmit_time, sk->sk_clockid);
  1681. ip6_cork_steal_dst(skb, cork);
  1682. IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS);
  1683. if (unlikely(proto == IPPROTO_ICMPV6)) {
  1684. struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
  1685. u8 icmp6_type;
  1686. if (sk->sk_socket->type == SOCK_RAW &&
  1687. !(fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH))
  1688. icmp6_type = fl6->fl6_icmp_type;
  1689. else
  1690. icmp6_type = icmp6_hdr(skb)->icmp6_type;
  1691. ICMP6MSGOUT_INC_STATS(net, idev, icmp6_type);
  1692. ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
  1693. }
  1694. ip6_cork_release(cork);
  1695. out:
  1696. return skb;
  1697. }
  1698. int ip6_send_skb(struct sk_buff *skb)
  1699. {
  1700. struct net *net = sock_net(skb->sk);
  1701. struct rt6_info *rt = dst_rt6_info(skb_dst(skb));
  1702. int err;
  1703. rcu_read_lock();
  1704. err = ip6_local_out(net, skb->sk, skb);
  1705. if (err) {
  1706. if (err > 0)
  1707. err = net_xmit_errno(err);
  1708. if (err)
  1709. IP6_INC_STATS(net, rt->rt6i_idev,
  1710. IPSTATS_MIB_OUTDISCARDS);
  1711. }
  1712. rcu_read_unlock();
  1713. return err;
  1714. }
  1715. int ip6_push_pending_frames(struct sock *sk)
  1716. {
  1717. struct sk_buff *skb;
  1718. skb = ip6_finish_skb(sk);
  1719. if (!skb)
  1720. return 0;
  1721. return ip6_send_skb(skb);
  1722. }
  1723. EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
  1724. static void __ip6_flush_pending_frames(struct sock *sk,
  1725. struct sk_buff_head *queue,
  1726. struct inet_cork_full *cork)
  1727. {
  1728. struct sk_buff *skb;
  1729. while ((skb = __skb_dequeue_tail(queue)) != NULL) {
  1730. if (skb_dst(skb))
  1731. IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
  1732. IPSTATS_MIB_OUTDISCARDS);
  1733. kfree_skb(skb);
  1734. }
  1735. ip6_cork_release(cork);
  1736. }
  1737. void ip6_flush_pending_frames(struct sock *sk)
  1738. {
  1739. __ip6_flush_pending_frames(sk, &sk->sk_write_queue,
  1740. &inet_sk(sk)->cork);
  1741. }
  1742. EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);
  1743. struct sk_buff *ip6_make_skb(struct sock *sk,
  1744. int getfrag(void *from, char *to, int offset,
  1745. int len, int odd, struct sk_buff *skb),
  1746. void *from, size_t length, int transhdrlen,
  1747. struct ipcm6_cookie *ipc6, struct rt6_info *rt,
  1748. unsigned int flags, struct inet_cork_full *cork)
  1749. {
  1750. int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
  1751. struct sk_buff_head queue;
  1752. int err;
  1753. if (flags & MSG_PROBE) {
  1754. dst_release(&rt->dst);
  1755. return NULL;
  1756. }
  1757. __skb_queue_head_init(&queue);
  1758. cork->base.flags = 0;
  1759. cork->base.addr = 0;
  1760. cork->base.opt = NULL;
  1761. cork->base6.opt = NULL;
  1762. err = ip6_setup_cork(sk, cork, ipc6, rt);
  1763. if (err) {
  1764. ip6_cork_release(cork);
  1765. return ERR_PTR(err);
  1766. }
  1767. err = __ip6_append_data(sk, &queue, cork,
  1768. &current->task_frag, getfrag, from,
  1769. length + exthdrlen, transhdrlen + exthdrlen,
  1770. flags);
  1771. if (err) {
  1772. __ip6_flush_pending_frames(sk, &queue, cork);
  1773. return ERR_PTR(err);
  1774. }
  1775. return __ip6_make_skb(sk, &queue, cork);
  1776. }