ip6_output.c 34 KB

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