ip6_output.c 37 KB

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