ip6_output.c 38 KB

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