ip6_output.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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_forward_csum(skb);
  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;
  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. /* We must not fragment if the socket is set to force MTU discovery
  480. * or if the skb it not generated by a local socket. (This last
  481. * check should be redundant, but it's free.)
  482. */
  483. if (!np || np->pmtudisc >= IPV6_PMTUDISC_DO) {
  484. skb->dev = skb->dst->dev;
  485. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
  486. IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_FRAGFAILS);
  487. kfree_skb(skb);
  488. return -EMSGSIZE;
  489. }
  490. if (np && np->frag_size < mtu) {
  491. if (np->frag_size)
  492. mtu = np->frag_size;
  493. }
  494. mtu -= hlen + sizeof(struct frag_hdr);
  495. if (skb_shinfo(skb)->frag_list) {
  496. int first_len = skb_pagelen(skb);
  497. if (first_len - hlen > mtu ||
  498. ((first_len - hlen) & 7) ||
  499. skb_cloned(skb))
  500. goto slow_path;
  501. for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
  502. /* Correct geometry. */
  503. if (frag->len > mtu ||
  504. ((frag->len & 7) && frag->next) ||
  505. skb_headroom(frag) < hlen)
  506. goto slow_path;
  507. /* Partially cloned skb? */
  508. if (skb_shared(frag))
  509. goto slow_path;
  510. BUG_ON(frag->sk);
  511. if (skb->sk) {
  512. sock_hold(skb->sk);
  513. frag->sk = skb->sk;
  514. frag->destructor = sock_wfree;
  515. skb->truesize -= frag->truesize;
  516. }
  517. }
  518. err = 0;
  519. offset = 0;
  520. frag = skb_shinfo(skb)->frag_list;
  521. skb_shinfo(skb)->frag_list = NULL;
  522. /* BUILD HEADER */
  523. *prevhdr = NEXTHDR_FRAGMENT;
  524. tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
  525. if (!tmp_hdr) {
  526. IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_FRAGFAILS);
  527. return -ENOMEM;
  528. }
  529. __skb_pull(skb, hlen);
  530. fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
  531. __skb_push(skb, hlen);
  532. skb_reset_network_header(skb);
  533. memcpy(skb_network_header(skb), tmp_hdr, hlen);
  534. ipv6_select_ident(skb, fh);
  535. fh->nexthdr = nexthdr;
  536. fh->reserved = 0;
  537. fh->frag_off = htons(IP6_MF);
  538. frag_id = fh->identification;
  539. first_len = skb_pagelen(skb);
  540. skb->data_len = first_len - skb_headlen(skb);
  541. skb->len = first_len;
  542. ipv6_hdr(skb)->payload_len = htons(first_len -
  543. sizeof(struct ipv6hdr));
  544. dst_hold(&rt->u.dst);
  545. for (;;) {
  546. /* Prepare header of the next frame,
  547. * before previous one went down. */
  548. if (frag) {
  549. frag->ip_summed = CHECKSUM_NONE;
  550. skb_reset_transport_header(frag);
  551. fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
  552. __skb_push(frag, hlen);
  553. skb_reset_network_header(frag);
  554. memcpy(skb_network_header(frag), tmp_hdr,
  555. hlen);
  556. offset += skb->len - hlen - sizeof(struct frag_hdr);
  557. fh->nexthdr = nexthdr;
  558. fh->reserved = 0;
  559. fh->frag_off = htons(offset);
  560. if (frag->next != NULL)
  561. fh->frag_off |= htons(IP6_MF);
  562. fh->identification = frag_id;
  563. ipv6_hdr(frag)->payload_len =
  564. htons(frag->len -
  565. sizeof(struct ipv6hdr));
  566. ip6_copy_metadata(frag, skb);
  567. }
  568. err = output(skb);
  569. if(!err)
  570. IP6_INC_STATS(ip6_dst_idev(&rt->u.dst), IPSTATS_MIB_FRAGCREATES);
  571. if (err || !frag)
  572. break;
  573. skb = frag;
  574. frag = skb->next;
  575. skb->next = NULL;
  576. }
  577. kfree(tmp_hdr);
  578. if (err == 0) {
  579. IP6_INC_STATS(ip6_dst_idev(&rt->u.dst), IPSTATS_MIB_FRAGOKS);
  580. dst_release(&rt->u.dst);
  581. return 0;
  582. }
  583. while (frag) {
  584. skb = frag->next;
  585. kfree_skb(frag);
  586. frag = skb;
  587. }
  588. IP6_INC_STATS(ip6_dst_idev(&rt->u.dst), IPSTATS_MIB_FRAGFAILS);
  589. dst_release(&rt->u.dst);
  590. return err;
  591. }
  592. slow_path:
  593. left = skb->len - hlen; /* Space per frame */
  594. ptr = hlen; /* Where to start from */
  595. /*
  596. * Fragment the datagram.
  597. */
  598. *prevhdr = NEXTHDR_FRAGMENT;
  599. /*
  600. * Keep copying data until we run out.
  601. */
  602. while(left > 0) {
  603. len = left;
  604. /* IF: it doesn't fit, use 'mtu' - the data space left */
  605. if (len > mtu)
  606. len = mtu;
  607. /* IF: we are not sending upto and including the packet end
  608. then align the next start on an eight byte boundary */
  609. if (len < left) {
  610. len &= ~7;
  611. }
  612. /*
  613. * Allocate buffer.
  614. */
  615. if ((frag = alloc_skb(len+hlen+sizeof(struct frag_hdr)+LL_RESERVED_SPACE(rt->u.dst.dev), GFP_ATOMIC)) == NULL) {
  616. NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
  617. IP6_INC_STATS(ip6_dst_idev(skb->dst),
  618. IPSTATS_MIB_FRAGFAILS);
  619. err = -ENOMEM;
  620. goto fail;
  621. }
  622. /*
  623. * Set up data on packet
  624. */
  625. ip6_copy_metadata(frag, skb);
  626. skb_reserve(frag, LL_RESERVED_SPACE(rt->u.dst.dev));
  627. skb_put(frag, len + hlen + sizeof(struct frag_hdr));
  628. skb_reset_network_header(frag);
  629. fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
  630. frag->transport_header = (frag->network_header + hlen +
  631. sizeof(struct frag_hdr));
  632. /*
  633. * Charge the memory for the fragment to any owner
  634. * it might possess
  635. */
  636. if (skb->sk)
  637. skb_set_owner_w(frag, skb->sk);
  638. /*
  639. * Copy the packet header into the new buffer.
  640. */
  641. skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
  642. /*
  643. * Build fragment header.
  644. */
  645. fh->nexthdr = nexthdr;
  646. fh->reserved = 0;
  647. if (!frag_id) {
  648. ipv6_select_ident(skb, fh);
  649. frag_id = fh->identification;
  650. } else
  651. fh->identification = frag_id;
  652. /*
  653. * Copy a block of the IP datagram.
  654. */
  655. if (skb_copy_bits(skb, ptr, skb_transport_header(skb), len))
  656. BUG();
  657. left -= len;
  658. fh->frag_off = htons(offset);
  659. if (left > 0)
  660. fh->frag_off |= htons(IP6_MF);
  661. ipv6_hdr(frag)->payload_len = htons(frag->len -
  662. sizeof(struct ipv6hdr));
  663. ptr += len;
  664. offset += len;
  665. /*
  666. * Put this fragment into the sending queue.
  667. */
  668. err = output(frag);
  669. if (err)
  670. goto fail;
  671. IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_FRAGCREATES);
  672. }
  673. IP6_INC_STATS(ip6_dst_idev(skb->dst),
  674. IPSTATS_MIB_FRAGOKS);
  675. kfree_skb(skb);
  676. return err;
  677. fail:
  678. IP6_INC_STATS(ip6_dst_idev(skb->dst),
  679. IPSTATS_MIB_FRAGFAILS);
  680. kfree_skb(skb);
  681. return err;
  682. }
  683. static inline int ip6_rt_check(struct rt6key *rt_key,
  684. struct in6_addr *fl_addr,
  685. struct in6_addr *addr_cache)
  686. {
  687. return ((rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
  688. (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache)));
  689. }
  690. static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
  691. struct dst_entry *dst,
  692. struct flowi *fl)
  693. {
  694. struct ipv6_pinfo *np = inet6_sk(sk);
  695. struct rt6_info *rt = (struct rt6_info *)dst;
  696. if (!dst)
  697. goto out;
  698. /* Yes, checking route validity in not connected
  699. * case is not very simple. Take into account,
  700. * that we do not support routing by source, TOS,
  701. * and MSG_DONTROUTE --ANK (980726)
  702. *
  703. * 1. ip6_rt_check(): If route was host route,
  704. * check that cached destination is current.
  705. * If it is network route, we still may
  706. * check its validity using saved pointer
  707. * to the last used address: daddr_cache.
  708. * We do not want to save whole address now,
  709. * (because main consumer of this service
  710. * is tcp, which has not this problem),
  711. * so that the last trick works only on connected
  712. * sockets.
  713. * 2. oif also should be the same.
  714. */
  715. if (ip6_rt_check(&rt->rt6i_dst, &fl->fl6_dst, np->daddr_cache) ||
  716. #ifdef CONFIG_IPV6_SUBTREES
  717. ip6_rt_check(&rt->rt6i_src, &fl->fl6_src, np->saddr_cache) ||
  718. #endif
  719. (fl->oif && fl->oif != dst->dev->ifindex)) {
  720. dst_release(dst);
  721. dst = NULL;
  722. }
  723. out:
  724. return dst;
  725. }
  726. static int ip6_dst_lookup_tail(struct sock *sk,
  727. struct dst_entry **dst, struct flowi *fl)
  728. {
  729. int err;
  730. if (*dst == NULL)
  731. *dst = ip6_route_output(sk, fl);
  732. if ((err = (*dst)->error))
  733. goto out_err_release;
  734. if (ipv6_addr_any(&fl->fl6_src)) {
  735. err = ipv6_get_saddr(*dst, &fl->fl6_dst, &fl->fl6_src);
  736. if (err)
  737. goto out_err_release;
  738. }
  739. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  740. /*
  741. * Here if the dst entry we've looked up
  742. * has a neighbour entry that is in the INCOMPLETE
  743. * state and the src address from the flow is
  744. * marked as OPTIMISTIC, we release the found
  745. * dst entry and replace it instead with the
  746. * dst entry of the nexthop router
  747. */
  748. if (!((*dst)->neighbour->nud_state & NUD_VALID)) {
  749. struct inet6_ifaddr *ifp;
  750. struct flowi fl_gw;
  751. int redirect;
  752. ifp = ipv6_get_ifaddr(&fl->fl6_src, (*dst)->dev, 1);
  753. redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
  754. if (ifp)
  755. in6_ifa_put(ifp);
  756. if (redirect) {
  757. /*
  758. * We need to get the dst entry for the
  759. * default router instead
  760. */
  761. dst_release(*dst);
  762. memcpy(&fl_gw, fl, sizeof(struct flowi));
  763. memset(&fl_gw.fl6_dst, 0, sizeof(struct in6_addr));
  764. *dst = ip6_route_output(sk, &fl_gw);
  765. if ((err = (*dst)->error))
  766. goto out_err_release;
  767. }
  768. }
  769. #endif
  770. return 0;
  771. out_err_release:
  772. dst_release(*dst);
  773. *dst = NULL;
  774. return err;
  775. }
  776. /**
  777. * ip6_dst_lookup - perform route lookup on flow
  778. * @sk: socket which provides route info
  779. * @dst: pointer to dst_entry * for result
  780. * @fl: flow to lookup
  781. *
  782. * This function performs a route lookup on the given flow.
  783. *
  784. * It returns zero on success, or a standard errno code on error.
  785. */
  786. int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi *fl)
  787. {
  788. *dst = NULL;
  789. return ip6_dst_lookup_tail(sk, dst, fl);
  790. }
  791. EXPORT_SYMBOL_GPL(ip6_dst_lookup);
  792. /**
  793. * ip6_sk_dst_lookup - perform socket cached route lookup on flow
  794. * @sk: socket which provides the dst cache and 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 with the
  799. * possibility of using the cached route in the socket if it is valid.
  800. * It will take the socket dst lock when operating on the dst cache.
  801. * As a result, this function can only be used in process context.
  802. *
  803. * It returns zero on success, or a standard errno code on error.
  804. */
  805. int ip6_sk_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi *fl)
  806. {
  807. *dst = NULL;
  808. if (sk) {
  809. *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
  810. *dst = ip6_sk_dst_check(sk, *dst, fl);
  811. }
  812. return ip6_dst_lookup_tail(sk, dst, fl);
  813. }
  814. EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup);
  815. static inline int ip6_ufo_append_data(struct sock *sk,
  816. int getfrag(void *from, char *to, int offset, int len,
  817. int odd, struct sk_buff *skb),
  818. void *from, int length, int hh_len, int fragheaderlen,
  819. int transhdrlen, int mtu,unsigned int flags)
  820. {
  821. struct sk_buff *skb;
  822. int err;
  823. /* There is support for UDP large send offload by network
  824. * device, so create one single skb packet containing complete
  825. * udp datagram
  826. */
  827. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
  828. skb = sock_alloc_send_skb(sk,
  829. hh_len + fragheaderlen + transhdrlen + 20,
  830. (flags & MSG_DONTWAIT), &err);
  831. if (skb == NULL)
  832. return -ENOMEM;
  833. /* reserve space for Hardware header */
  834. skb_reserve(skb, hh_len);
  835. /* create space for UDP/IP header */
  836. skb_put(skb,fragheaderlen + transhdrlen);
  837. /* initialize network header pointer */
  838. skb_reset_network_header(skb);
  839. /* initialize protocol header pointer */
  840. skb->transport_header = skb->network_header + fragheaderlen;
  841. skb->ip_summed = CHECKSUM_PARTIAL;
  842. skb->csum = 0;
  843. sk->sk_sndmsg_off = 0;
  844. }
  845. err = skb_append_datato_frags(sk,skb, getfrag, from,
  846. (length - transhdrlen));
  847. if (!err) {
  848. struct frag_hdr fhdr;
  849. /* specify the length of each IP datagram fragment*/
  850. skb_shinfo(skb)->gso_size = mtu - fragheaderlen -
  851. sizeof(struct frag_hdr);
  852. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  853. ipv6_select_ident(skb, &fhdr);
  854. skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
  855. __skb_queue_tail(&sk->sk_write_queue, skb);
  856. return 0;
  857. }
  858. /* There is not enough support do UPD LSO,
  859. * so follow normal path
  860. */
  861. kfree_skb(skb);
  862. return err;
  863. }
  864. int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
  865. int offset, int len, int odd, struct sk_buff *skb),
  866. void *from, int length, int transhdrlen,
  867. int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi *fl,
  868. struct rt6_info *rt, unsigned int flags)
  869. {
  870. struct inet_sock *inet = inet_sk(sk);
  871. struct ipv6_pinfo *np = inet6_sk(sk);
  872. struct sk_buff *skb;
  873. unsigned int maxfraglen, fragheaderlen;
  874. int exthdrlen;
  875. int hh_len;
  876. int mtu;
  877. int copy;
  878. int err;
  879. int offset = 0;
  880. int csummode = CHECKSUM_NONE;
  881. if (flags&MSG_PROBE)
  882. return 0;
  883. if (skb_queue_empty(&sk->sk_write_queue)) {
  884. /*
  885. * setup for corking
  886. */
  887. if (opt) {
  888. if (np->cork.opt == NULL) {
  889. np->cork.opt = kmalloc(opt->tot_len,
  890. sk->sk_allocation);
  891. if (unlikely(np->cork.opt == NULL))
  892. return -ENOBUFS;
  893. } else if (np->cork.opt->tot_len < opt->tot_len) {
  894. printk(KERN_DEBUG "ip6_append_data: invalid option length\n");
  895. return -EINVAL;
  896. }
  897. memcpy(np->cork.opt, opt, opt->tot_len);
  898. inet->cork.flags |= IPCORK_OPT;
  899. /* need source address above miyazawa*/
  900. }
  901. dst_hold(&rt->u.dst);
  902. np->cork.rt = rt;
  903. inet->cork.fl = *fl;
  904. np->cork.hop_limit = hlimit;
  905. np->cork.tclass = tclass;
  906. mtu = dst_mtu(rt->u.dst.path);
  907. if (np->frag_size < mtu) {
  908. if (np->frag_size)
  909. mtu = np->frag_size;
  910. }
  911. inet->cork.fragsize = mtu;
  912. if (dst_allfrag(rt->u.dst.path))
  913. inet->cork.flags |= IPCORK_ALLFRAG;
  914. inet->cork.length = 0;
  915. sk->sk_sndmsg_page = NULL;
  916. sk->sk_sndmsg_off = 0;
  917. exthdrlen = rt->u.dst.header_len + (opt ? opt->opt_flen : 0);
  918. length += exthdrlen;
  919. transhdrlen += exthdrlen;
  920. } else {
  921. rt = np->cork.rt;
  922. fl = &inet->cork.fl;
  923. if (inet->cork.flags & IPCORK_OPT)
  924. opt = np->cork.opt;
  925. transhdrlen = 0;
  926. exthdrlen = 0;
  927. mtu = inet->cork.fragsize;
  928. }
  929. hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
  930. fragheaderlen = sizeof(struct ipv6hdr) + rt->u.dst.nfheader_len + (opt ? opt->opt_nflen : 0);
  931. maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
  932. if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
  933. if (inet->cork.length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
  934. ipv6_local_error(sk, EMSGSIZE, fl, mtu-exthdrlen);
  935. return -EMSGSIZE;
  936. }
  937. }
  938. /*
  939. * Let's try using as much space as possible.
  940. * Use MTU if total length of the message fits into the MTU.
  941. * Otherwise, we need to reserve fragment header and
  942. * fragment alignment (= 8-15 octects, in total).
  943. *
  944. * Note that we may need to "move" the data from the tail of
  945. * of the buffer to the new fragment when we split
  946. * the message.
  947. *
  948. * FIXME: It may be fragmented into multiple chunks
  949. * at once if non-fragmentable extension headers
  950. * are too large.
  951. * --yoshfuji
  952. */
  953. inet->cork.length += length;
  954. if (((length > mtu) && (sk->sk_protocol == IPPROTO_UDP)) &&
  955. (rt->u.dst.dev->features & NETIF_F_UFO)) {
  956. err = ip6_ufo_append_data(sk, getfrag, from, length, hh_len,
  957. fragheaderlen, transhdrlen, mtu,
  958. flags);
  959. if (err)
  960. goto error;
  961. return 0;
  962. }
  963. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
  964. goto alloc_new_skb;
  965. while (length > 0) {
  966. /* Check if the remaining data fits into current packet. */
  967. copy = (inet->cork.length <= mtu && !(inet->cork.flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
  968. if (copy < length)
  969. copy = maxfraglen - skb->len;
  970. if (copy <= 0) {
  971. char *data;
  972. unsigned int datalen;
  973. unsigned int fraglen;
  974. unsigned int fraggap;
  975. unsigned int alloclen;
  976. struct sk_buff *skb_prev;
  977. alloc_new_skb:
  978. skb_prev = skb;
  979. /* There's no room in the current skb */
  980. if (skb_prev)
  981. fraggap = skb_prev->len - maxfraglen;
  982. else
  983. fraggap = 0;
  984. /*
  985. * If remaining data exceeds the mtu,
  986. * we know we need more fragment(s).
  987. */
  988. datalen = length + fraggap;
  989. if (datalen > (inet->cork.length <= mtu && !(inet->cork.flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
  990. datalen = maxfraglen - fragheaderlen;
  991. fraglen = datalen + fragheaderlen;
  992. if ((flags & MSG_MORE) &&
  993. !(rt->u.dst.dev->features&NETIF_F_SG))
  994. alloclen = mtu;
  995. else
  996. alloclen = datalen + fragheaderlen;
  997. /*
  998. * The last fragment gets additional space at tail.
  999. * Note: we overallocate on fragments with MSG_MODE
  1000. * because we have no idea if we're the last one.
  1001. */
  1002. if (datalen == length + fraggap)
  1003. alloclen += rt->u.dst.trailer_len;
  1004. /*
  1005. * We just reserve space for fragment header.
  1006. * Note: this may be overallocation if the message
  1007. * (without MSG_MORE) fits into the MTU.
  1008. */
  1009. alloclen += sizeof(struct frag_hdr);
  1010. if (transhdrlen) {
  1011. skb = sock_alloc_send_skb(sk,
  1012. alloclen + hh_len,
  1013. (flags & MSG_DONTWAIT), &err);
  1014. } else {
  1015. skb = NULL;
  1016. if (atomic_read(&sk->sk_wmem_alloc) <=
  1017. 2 * sk->sk_sndbuf)
  1018. skb = sock_wmalloc(sk,
  1019. alloclen + hh_len, 1,
  1020. sk->sk_allocation);
  1021. if (unlikely(skb == NULL))
  1022. err = -ENOBUFS;
  1023. }
  1024. if (skb == NULL)
  1025. goto error;
  1026. /*
  1027. * Fill in the control structures
  1028. */
  1029. skb->ip_summed = csummode;
  1030. skb->csum = 0;
  1031. /* reserve for fragmentation */
  1032. skb_reserve(skb, hh_len+sizeof(struct frag_hdr));
  1033. /*
  1034. * Find where to start putting bytes
  1035. */
  1036. data = skb_put(skb, fraglen);
  1037. skb_set_network_header(skb, exthdrlen);
  1038. data += fragheaderlen;
  1039. skb->transport_header = (skb->network_header +
  1040. fragheaderlen);
  1041. if (fraggap) {
  1042. skb->csum = skb_copy_and_csum_bits(
  1043. skb_prev, maxfraglen,
  1044. data + transhdrlen, fraggap, 0);
  1045. skb_prev->csum = csum_sub(skb_prev->csum,
  1046. skb->csum);
  1047. data += fraggap;
  1048. pskb_trim_unique(skb_prev, maxfraglen);
  1049. }
  1050. copy = datalen - transhdrlen - fraggap;
  1051. if (copy < 0) {
  1052. err = -EINVAL;
  1053. kfree_skb(skb);
  1054. goto error;
  1055. } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
  1056. err = -EFAULT;
  1057. kfree_skb(skb);
  1058. goto error;
  1059. }
  1060. offset += copy;
  1061. length -= datalen - fraggap;
  1062. transhdrlen = 0;
  1063. exthdrlen = 0;
  1064. csummode = CHECKSUM_NONE;
  1065. /*
  1066. * Put the packet on the pending queue
  1067. */
  1068. __skb_queue_tail(&sk->sk_write_queue, skb);
  1069. continue;
  1070. }
  1071. if (copy > length)
  1072. copy = length;
  1073. if (!(rt->u.dst.dev->features&NETIF_F_SG)) {
  1074. unsigned int off;
  1075. off = skb->len;
  1076. if (getfrag(from, skb_put(skb, copy),
  1077. offset, copy, off, skb) < 0) {
  1078. __skb_trim(skb, off);
  1079. err = -EFAULT;
  1080. goto error;
  1081. }
  1082. } else {
  1083. int i = skb_shinfo(skb)->nr_frags;
  1084. skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
  1085. struct page *page = sk->sk_sndmsg_page;
  1086. int off = sk->sk_sndmsg_off;
  1087. unsigned int left;
  1088. if (page && (left = PAGE_SIZE - off) > 0) {
  1089. if (copy >= left)
  1090. copy = left;
  1091. if (page != frag->page) {
  1092. if (i == MAX_SKB_FRAGS) {
  1093. err = -EMSGSIZE;
  1094. goto error;
  1095. }
  1096. get_page(page);
  1097. skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
  1098. frag = &skb_shinfo(skb)->frags[i];
  1099. }
  1100. } else if(i < MAX_SKB_FRAGS) {
  1101. if (copy > PAGE_SIZE)
  1102. copy = PAGE_SIZE;
  1103. page = alloc_pages(sk->sk_allocation, 0);
  1104. if (page == NULL) {
  1105. err = -ENOMEM;
  1106. goto error;
  1107. }
  1108. sk->sk_sndmsg_page = page;
  1109. sk->sk_sndmsg_off = 0;
  1110. skb_fill_page_desc(skb, i, page, 0, 0);
  1111. frag = &skb_shinfo(skb)->frags[i];
  1112. skb->truesize += PAGE_SIZE;
  1113. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  1114. } else {
  1115. err = -EMSGSIZE;
  1116. goto error;
  1117. }
  1118. if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
  1119. err = -EFAULT;
  1120. goto error;
  1121. }
  1122. sk->sk_sndmsg_off += copy;
  1123. frag->size += copy;
  1124. skb->len += copy;
  1125. skb->data_len += copy;
  1126. }
  1127. offset += copy;
  1128. length -= copy;
  1129. }
  1130. return 0;
  1131. error:
  1132. inet->cork.length -= length;
  1133. IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  1134. return err;
  1135. }
  1136. int ip6_push_pending_frames(struct sock *sk)
  1137. {
  1138. struct sk_buff *skb, *tmp_skb;
  1139. struct sk_buff **tail_skb;
  1140. struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
  1141. struct inet_sock *inet = inet_sk(sk);
  1142. struct ipv6_pinfo *np = inet6_sk(sk);
  1143. struct ipv6hdr *hdr;
  1144. struct ipv6_txoptions *opt = np->cork.opt;
  1145. struct rt6_info *rt = np->cork.rt;
  1146. struct flowi *fl = &inet->cork.fl;
  1147. unsigned char proto = fl->proto;
  1148. int err = 0;
  1149. if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
  1150. goto out;
  1151. tail_skb = &(skb_shinfo(skb)->frag_list);
  1152. /* move skb->data to ip header from ext header */
  1153. if (skb->data < skb_network_header(skb))
  1154. __skb_pull(skb, skb_network_offset(skb));
  1155. while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
  1156. __skb_pull(tmp_skb, skb_network_header_len(skb));
  1157. *tail_skb = tmp_skb;
  1158. tail_skb = &(tmp_skb->next);
  1159. skb->len += tmp_skb->len;
  1160. skb->data_len += tmp_skb->len;
  1161. skb->truesize += tmp_skb->truesize;
  1162. __sock_put(tmp_skb->sk);
  1163. tmp_skb->destructor = NULL;
  1164. tmp_skb->sk = NULL;
  1165. }
  1166. ipv6_addr_copy(final_dst, &fl->fl6_dst);
  1167. __skb_pull(skb, skb_network_header_len(skb));
  1168. if (opt && opt->opt_flen)
  1169. ipv6_push_frag_opts(skb, opt, &proto);
  1170. if (opt && opt->opt_nflen)
  1171. ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
  1172. skb_push(skb, sizeof(struct ipv6hdr));
  1173. skb_reset_network_header(skb);
  1174. hdr = ipv6_hdr(skb);
  1175. *(__be32*)hdr = fl->fl6_flowlabel |
  1176. htonl(0x60000000 | ((int)np->cork.tclass << 20));
  1177. if (skb->len <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN)
  1178. hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  1179. else
  1180. hdr->payload_len = 0;
  1181. hdr->hop_limit = np->cork.hop_limit;
  1182. hdr->nexthdr = proto;
  1183. ipv6_addr_copy(&hdr->saddr, &fl->fl6_src);
  1184. ipv6_addr_copy(&hdr->daddr, final_dst);
  1185. skb->priority = sk->sk_priority;
  1186. skb->dst = dst_clone(&rt->u.dst);
  1187. IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS);
  1188. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dst->dev, dst_output);
  1189. if (err) {
  1190. if (err > 0)
  1191. err = np->recverr ? net_xmit_errno(err) : 0;
  1192. if (err)
  1193. goto error;
  1194. }
  1195. out:
  1196. inet->cork.flags &= ~IPCORK_OPT;
  1197. kfree(np->cork.opt);
  1198. np->cork.opt = NULL;
  1199. if (np->cork.rt) {
  1200. dst_release(&np->cork.rt->u.dst);
  1201. np->cork.rt = NULL;
  1202. inet->cork.flags &= ~IPCORK_ALLFRAG;
  1203. }
  1204. memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
  1205. return err;
  1206. error:
  1207. goto out;
  1208. }
  1209. void ip6_flush_pending_frames(struct sock *sk)
  1210. {
  1211. struct inet_sock *inet = inet_sk(sk);
  1212. struct ipv6_pinfo *np = inet6_sk(sk);
  1213. struct sk_buff *skb;
  1214. while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
  1215. IP6_INC_STATS(ip6_dst_idev(skb->dst),
  1216. IPSTATS_MIB_OUTDISCARDS);
  1217. kfree_skb(skb);
  1218. }
  1219. inet->cork.flags &= ~IPCORK_OPT;
  1220. kfree(np->cork.opt);
  1221. np->cork.opt = NULL;
  1222. if (np->cork.rt) {
  1223. dst_release(&np->cork.rt->u.dst);
  1224. np->cork.rt = NULL;
  1225. inet->cork.flags &= ~IPCORK_ALLFRAG;
  1226. }
  1227. memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
  1228. }