ip6_output.c 40 KB

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