ip6_output.c 36 KB

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