ip6_output.c 36 KB

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