ip6_output.c 40 KB

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