ip6_output.c 35 KB

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