ip6_output.c 44 KB

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