ip6_output.c 39 KB

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