ip6_output.c 36 KB

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