ip_output.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * The Internet Protocol (IP) output module.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Donald Becker, <becker@super.org>
  11. * Alan Cox, <Alan.Cox@linux.org>
  12. * Richard Underwood
  13. * Stefan Becker, <stefanb@yello.ping.de>
  14. * Jorge Cwik, <jorge@laser.satlink.net>
  15. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  16. * Hirokazu Takahashi, <taka@valinux.co.jp>
  17. *
  18. * See ip_input.c for original log
  19. *
  20. * Fixes:
  21. * Alan Cox : Missing nonblock feature in ip_build_xmit.
  22. * Mike Kilburn : htons() missing in ip_build_xmit.
  23. * Bradford Johnson: Fix faulty handling of some frames when
  24. * no route is found.
  25. * Alexander Demenshin: Missing sk/skb free in ip_queue_xmit
  26. * (in case if packet not accepted by
  27. * output firewall rules)
  28. * Mike McLagan : Routing by source
  29. * Alexey Kuznetsov: use new route cache
  30. * Andi Kleen: Fix broken PMTU recovery and remove
  31. * some redundant tests.
  32. * Vitaly E. Lavrov : Transparent proxy revived after year coma.
  33. * Andi Kleen : Replace ip_reply with ip_send_reply.
  34. * Andi Kleen : Split fast and slow ip_build_xmit path
  35. * for decreased register pressure on x86
  36. * and more readibility.
  37. * Marc Boucher : When call_out_firewall returns FW_QUEUE,
  38. * silently drop skb instead of failing with -EPERM.
  39. * Detlev Wengorz : Copy protocol for fragments.
  40. * Hirokazu Takahashi: HW checksumming for outgoing UDP
  41. * datagrams.
  42. * Hirokazu Takahashi: sendfile() on UDP works now.
  43. */
  44. #include <asm/uaccess.h>
  45. #include <linux/module.h>
  46. #include <linux/types.h>
  47. #include <linux/kernel.h>
  48. #include <linux/mm.h>
  49. #include <linux/string.h>
  50. #include <linux/errno.h>
  51. #include <linux/highmem.h>
  52. #include <linux/slab.h>
  53. #include <linux/socket.h>
  54. #include <linux/sockios.h>
  55. #include <linux/in.h>
  56. #include <linux/inet.h>
  57. #include <linux/netdevice.h>
  58. #include <linux/etherdevice.h>
  59. #include <linux/proc_fs.h>
  60. #include <linux/stat.h>
  61. #include <linux/init.h>
  62. #include <net/snmp.h>
  63. #include <net/ip.h>
  64. #include <net/protocol.h>
  65. #include <net/route.h>
  66. #include <net/xfrm.h>
  67. #include <linux/skbuff.h>
  68. #include <net/sock.h>
  69. #include <net/arp.h>
  70. #include <net/icmp.h>
  71. #include <net/checksum.h>
  72. #include <net/inetpeer.h>
  73. #include <linux/igmp.h>
  74. #include <linux/netfilter_ipv4.h>
  75. #include <linux/netfilter_bridge.h>
  76. #include <linux/mroute.h>
  77. #include <linux/netlink.h>
  78. #include <linux/tcp.h>
  79. int sysctl_ip_default_ttl __read_mostly = IPDEFTTL;
  80. EXPORT_SYMBOL(sysctl_ip_default_ttl);
  81. /* Generate a checksum for an outgoing IP datagram. */
  82. __inline__ void ip_send_check(struct iphdr *iph)
  83. {
  84. iph->check = 0;
  85. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  86. }
  87. EXPORT_SYMBOL(ip_send_check);
  88. int __ip_local_out(struct sk_buff *skb)
  89. {
  90. struct iphdr *iph = ip_hdr(skb);
  91. iph->tot_len = htons(skb->len);
  92. ip_send_check(iph);
  93. return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL,
  94. skb_dst(skb)->dev, dst_output);
  95. }
  96. int ip_local_out(struct sk_buff *skb)
  97. {
  98. int err;
  99. err = __ip_local_out(skb);
  100. if (likely(err == 1))
  101. err = dst_output(skb);
  102. return err;
  103. }
  104. EXPORT_SYMBOL_GPL(ip_local_out);
  105. static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
  106. {
  107. int ttl = inet->uc_ttl;
  108. if (ttl < 0)
  109. ttl = ip4_dst_hoplimit(dst);
  110. return ttl;
  111. }
  112. /*
  113. * Add an ip header to a skbuff and send it out.
  114. *
  115. */
  116. int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk,
  117. __be32 saddr, __be32 daddr, struct ip_options_rcu *opt)
  118. {
  119. struct inet_sock *inet = inet_sk(sk);
  120. struct rtable *rt = skb_rtable(skb);
  121. struct iphdr *iph;
  122. /* Build the IP header. */
  123. skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
  124. skb_reset_network_header(skb);
  125. iph = ip_hdr(skb);
  126. iph->version = 4;
  127. iph->ihl = 5;
  128. iph->tos = inet->tos;
  129. if (ip_dont_fragment(sk, &rt->dst))
  130. iph->frag_off = htons(IP_DF);
  131. else
  132. iph->frag_off = 0;
  133. iph->ttl = ip_select_ttl(inet, &rt->dst);
  134. iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
  135. iph->saddr = saddr;
  136. iph->protocol = sk->sk_protocol;
  137. ip_select_ident(iph, &rt->dst, sk);
  138. if (opt && opt->opt.optlen) {
  139. iph->ihl += opt->opt.optlen>>2;
  140. ip_options_build(skb, &opt->opt, daddr, rt, 0);
  141. }
  142. skb->priority = sk->sk_priority;
  143. skb->mark = sk->sk_mark;
  144. /* Send it out. */
  145. return ip_local_out(skb);
  146. }
  147. EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
  148. static inline int ip_finish_output2(struct sk_buff *skb)
  149. {
  150. struct dst_entry *dst = skb_dst(skb);
  151. struct rtable *rt = (struct rtable *)dst;
  152. struct net_device *dev = dst->dev;
  153. unsigned int hh_len = LL_RESERVED_SPACE(dev);
  154. struct neighbour *neigh;
  155. u32 nexthop;
  156. if (rt->rt_type == RTN_MULTICAST) {
  157. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTMCAST, skb->len);
  158. } else if (rt->rt_type == RTN_BROADCAST)
  159. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTBCAST, skb->len);
  160. /* Be paranoid, rather than too clever. */
  161. if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
  162. struct sk_buff *skb2;
  163. skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
  164. if (skb2 == NULL) {
  165. kfree_skb(skb);
  166. return -ENOMEM;
  167. }
  168. if (skb->sk)
  169. skb_set_owner_w(skb2, skb->sk);
  170. consume_skb(skb);
  171. skb = skb2;
  172. }
  173. rcu_read_lock_bh();
  174. nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
  175. neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
  176. if (unlikely(!neigh))
  177. neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
  178. if (!IS_ERR(neigh)) {
  179. int res = dst_neigh_output(dst, neigh, skb);
  180. rcu_read_unlock_bh();
  181. return res;
  182. }
  183. rcu_read_unlock_bh();
  184. net_dbg_ratelimited("%s: No header cache and no neighbour!\n",
  185. __func__);
  186. kfree_skb(skb);
  187. return -EINVAL;
  188. }
  189. static inline int ip_skb_dst_mtu(struct sk_buff *skb)
  190. {
  191. struct inet_sock *inet = skb->sk ? inet_sk(skb->sk) : NULL;
  192. return (inet && inet->pmtudisc == IP_PMTUDISC_PROBE) ?
  193. skb_dst(skb)->dev->mtu : dst_mtu(skb_dst(skb));
  194. }
  195. static int ip_finish_output(struct sk_buff *skb)
  196. {
  197. #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
  198. /* Policy lookup after SNAT yielded a new policy */
  199. if (skb_dst(skb)->xfrm != NULL) {
  200. IPCB(skb)->flags |= IPSKB_REROUTED;
  201. return dst_output(skb);
  202. }
  203. #endif
  204. if (skb->len > ip_skb_dst_mtu(skb) && !skb_is_gso(skb))
  205. return ip_fragment(skb, ip_finish_output2);
  206. else
  207. return ip_finish_output2(skb);
  208. }
  209. int ip_mc_output(struct sk_buff *skb)
  210. {
  211. struct sock *sk = skb->sk;
  212. struct rtable *rt = skb_rtable(skb);
  213. struct net_device *dev = rt->dst.dev;
  214. /*
  215. * If the indicated interface is up and running, send the packet.
  216. */
  217. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
  218. skb->dev = dev;
  219. skb->protocol = htons(ETH_P_IP);
  220. /*
  221. * Multicasts are looped back for other local users
  222. */
  223. if (rt->rt_flags&RTCF_MULTICAST) {
  224. if (sk_mc_loop(sk)
  225. #ifdef CONFIG_IP_MROUTE
  226. /* Small optimization: do not loopback not local frames,
  227. which returned after forwarding; they will be dropped
  228. by ip_mr_input in any case.
  229. Note, that local frames are looped back to be delivered
  230. to local recipients.
  231. This check is duplicated in ip_mr_input at the moment.
  232. */
  233. &&
  234. ((rt->rt_flags & RTCF_LOCAL) ||
  235. !(IPCB(skb)->flags & IPSKB_FORWARDED))
  236. #endif
  237. ) {
  238. struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
  239. if (newskb)
  240. NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
  241. newskb, NULL, newskb->dev,
  242. dev_loopback_xmit);
  243. }
  244. /* Multicasts with ttl 0 must not go beyond the host */
  245. if (ip_hdr(skb)->ttl == 0) {
  246. kfree_skb(skb);
  247. return 0;
  248. }
  249. }
  250. if (rt->rt_flags&RTCF_BROADCAST) {
  251. struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
  252. if (newskb)
  253. NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, newskb,
  254. NULL, newskb->dev, dev_loopback_xmit);
  255. }
  256. return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL,
  257. skb->dev, ip_finish_output,
  258. !(IPCB(skb)->flags & IPSKB_REROUTED));
  259. }
  260. int ip_output(struct sk_buff *skb)
  261. {
  262. struct net_device *dev = skb_dst(skb)->dev;
  263. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
  264. skb->dev = dev;
  265. skb->protocol = htons(ETH_P_IP);
  266. return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL, dev,
  267. ip_finish_output,
  268. !(IPCB(skb)->flags & IPSKB_REROUTED));
  269. }
  270. /*
  271. * copy saddr and daddr, possibly using 64bit load/stores
  272. * Equivalent to :
  273. * iph->saddr = fl4->saddr;
  274. * iph->daddr = fl4->daddr;
  275. */
  276. static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
  277. {
  278. BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
  279. offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
  280. memcpy(&iph->saddr, &fl4->saddr,
  281. sizeof(fl4->saddr) + sizeof(fl4->daddr));
  282. }
  283. int ip_queue_xmit(struct sk_buff *skb, struct flowi *fl)
  284. {
  285. struct sock *sk = skb->sk;
  286. struct inet_sock *inet = inet_sk(sk);
  287. struct ip_options_rcu *inet_opt;
  288. struct flowi4 *fl4;
  289. struct rtable *rt;
  290. struct iphdr *iph;
  291. int res;
  292. /* Skip all of this if the packet is already routed,
  293. * f.e. by something like SCTP.
  294. */
  295. rcu_read_lock();
  296. inet_opt = rcu_dereference(inet->inet_opt);
  297. fl4 = &fl->u.ip4;
  298. rt = skb_rtable(skb);
  299. if (rt != NULL)
  300. goto packet_routed;
  301. /* Make sure we can route this packet. */
  302. rt = (struct rtable *)__sk_dst_check(sk, 0);
  303. if (rt == NULL) {
  304. __be32 daddr;
  305. /* Use correct destination address if we have options. */
  306. daddr = inet->inet_daddr;
  307. if (inet_opt && inet_opt->opt.srr)
  308. daddr = inet_opt->opt.faddr;
  309. /* If this fails, retransmit mechanism of transport layer will
  310. * keep trying until route appears or the connection times
  311. * itself out.
  312. */
  313. rt = ip_route_output_ports(sock_net(sk), fl4, sk,
  314. daddr, inet->inet_saddr,
  315. inet->inet_dport,
  316. inet->inet_sport,
  317. sk->sk_protocol,
  318. RT_CONN_FLAGS(sk),
  319. sk->sk_bound_dev_if);
  320. if (IS_ERR(rt))
  321. goto no_route;
  322. sk_setup_caps(sk, &rt->dst);
  323. }
  324. skb_dst_set_noref(skb, &rt->dst);
  325. packet_routed:
  326. if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
  327. goto no_route;
  328. /* OK, we know where to send it, allocate and build IP header. */
  329. skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
  330. skb_reset_network_header(skb);
  331. iph = ip_hdr(skb);
  332. *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
  333. if (ip_dont_fragment(sk, &rt->dst) && !skb->local_df)
  334. iph->frag_off = htons(IP_DF);
  335. else
  336. iph->frag_off = 0;
  337. iph->ttl = ip_select_ttl(inet, &rt->dst);
  338. iph->protocol = sk->sk_protocol;
  339. ip_copy_addrs(iph, fl4);
  340. /* Transport layer set skb->h.foo itself. */
  341. if (inet_opt && inet_opt->opt.optlen) {
  342. iph->ihl += inet_opt->opt.optlen >> 2;
  343. ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
  344. }
  345. ip_select_ident_more(iph, &rt->dst, sk,
  346. (skb_shinfo(skb)->gso_segs ?: 1) - 1);
  347. skb->priority = sk->sk_priority;
  348. skb->mark = sk->sk_mark;
  349. res = ip_local_out(skb);
  350. rcu_read_unlock();
  351. return res;
  352. no_route:
  353. rcu_read_unlock();
  354. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
  355. kfree_skb(skb);
  356. return -EHOSTUNREACH;
  357. }
  358. EXPORT_SYMBOL(ip_queue_xmit);
  359. static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
  360. {
  361. to->pkt_type = from->pkt_type;
  362. to->priority = from->priority;
  363. to->protocol = from->protocol;
  364. skb_dst_drop(to);
  365. skb_dst_copy(to, from);
  366. to->dev = from->dev;
  367. to->mark = from->mark;
  368. /* Copy the flags to each fragment. */
  369. IPCB(to)->flags = IPCB(from)->flags;
  370. #ifdef CONFIG_NET_SCHED
  371. to->tc_index = from->tc_index;
  372. #endif
  373. nf_copy(to, from);
  374. #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
  375. defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
  376. to->nf_trace = from->nf_trace;
  377. #endif
  378. #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
  379. to->ipvs_property = from->ipvs_property;
  380. #endif
  381. skb_copy_secmark(to, from);
  382. }
  383. /*
  384. * This IP datagram is too large to be sent in one piece. Break it up into
  385. * smaller pieces (each of size equal to IP header plus
  386. * a block of the data of the original IP data part) that will yet fit in a
  387. * single device frame, and queue such a frame for sending.
  388. */
  389. int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
  390. {
  391. struct iphdr *iph;
  392. int ptr;
  393. struct net_device *dev;
  394. struct sk_buff *skb2;
  395. unsigned int mtu, hlen, left, len, ll_rs;
  396. int offset;
  397. __be16 not_last_frag;
  398. struct rtable *rt = skb_rtable(skb);
  399. int err = 0;
  400. dev = rt->dst.dev;
  401. /*
  402. * Point into the IP datagram header.
  403. */
  404. iph = ip_hdr(skb);
  405. if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->local_df) ||
  406. (IPCB(skb)->frag_max_size &&
  407. IPCB(skb)->frag_max_size > dst_mtu(&rt->dst)))) {
  408. IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
  409. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  410. htonl(ip_skb_dst_mtu(skb)));
  411. kfree_skb(skb);
  412. return -EMSGSIZE;
  413. }
  414. /*
  415. * Setup starting values.
  416. */
  417. hlen = iph->ihl * 4;
  418. mtu = dst_mtu(&rt->dst) - hlen; /* Size of data space */
  419. #ifdef CONFIG_BRIDGE_NETFILTER
  420. if (skb->nf_bridge)
  421. mtu -= nf_bridge_mtu_reduction(skb);
  422. #endif
  423. IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
  424. /* When frag_list is given, use it. First, check its validity:
  425. * some transformers could create wrong frag_list or break existing
  426. * one, it is not prohibited. In this case fall back to copying.
  427. *
  428. * LATER: this step can be merged to real generation of fragments,
  429. * we can switch to copy when see the first bad fragment.
  430. */
  431. if (skb_has_frag_list(skb)) {
  432. struct sk_buff *frag, *frag2;
  433. int first_len = skb_pagelen(skb);
  434. if (first_len - hlen > mtu ||
  435. ((first_len - hlen) & 7) ||
  436. ip_is_fragment(iph) ||
  437. skb_cloned(skb))
  438. goto slow_path;
  439. skb_walk_frags(skb, frag) {
  440. /* Correct geometry. */
  441. if (frag->len > mtu ||
  442. ((frag->len & 7) && frag->next) ||
  443. skb_headroom(frag) < hlen)
  444. goto slow_path_clean;
  445. /* Partially cloned skb? */
  446. if (skb_shared(frag))
  447. goto slow_path_clean;
  448. BUG_ON(frag->sk);
  449. if (skb->sk) {
  450. frag->sk = skb->sk;
  451. frag->destructor = sock_wfree;
  452. }
  453. skb->truesize -= frag->truesize;
  454. }
  455. /* Everything is OK. Generate! */
  456. err = 0;
  457. offset = 0;
  458. frag = skb_shinfo(skb)->frag_list;
  459. skb_frag_list_init(skb);
  460. skb->data_len = first_len - skb_headlen(skb);
  461. skb->len = first_len;
  462. iph->tot_len = htons(first_len);
  463. iph->frag_off = htons(IP_MF);
  464. ip_send_check(iph);
  465. for (;;) {
  466. /* Prepare header of the next frame,
  467. * before previous one went down. */
  468. if (frag) {
  469. frag->ip_summed = CHECKSUM_NONE;
  470. skb_reset_transport_header(frag);
  471. __skb_push(frag, hlen);
  472. skb_reset_network_header(frag);
  473. memcpy(skb_network_header(frag), iph, hlen);
  474. iph = ip_hdr(frag);
  475. iph->tot_len = htons(frag->len);
  476. ip_copy_metadata(frag, skb);
  477. if (offset == 0)
  478. ip_options_fragment(frag);
  479. offset += skb->len - hlen;
  480. iph->frag_off = htons(offset>>3);
  481. if (frag->next != NULL)
  482. iph->frag_off |= htons(IP_MF);
  483. /* Ready, complete checksum */
  484. ip_send_check(iph);
  485. }
  486. err = output(skb);
  487. if (!err)
  488. IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
  489. if (err || !frag)
  490. break;
  491. skb = frag;
  492. frag = skb->next;
  493. skb->next = NULL;
  494. }
  495. if (err == 0) {
  496. IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS);
  497. return 0;
  498. }
  499. while (frag) {
  500. skb = frag->next;
  501. kfree_skb(frag);
  502. frag = skb;
  503. }
  504. IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
  505. return err;
  506. slow_path_clean:
  507. skb_walk_frags(skb, frag2) {
  508. if (frag2 == frag)
  509. break;
  510. frag2->sk = NULL;
  511. frag2->destructor = NULL;
  512. skb->truesize += frag2->truesize;
  513. }
  514. }
  515. slow_path:
  516. /* for offloaded checksums cleanup checksum before fragmentation */
  517. if ((skb->ip_summed == CHECKSUM_PARTIAL) && skb_checksum_help(skb))
  518. goto fail;
  519. iph = ip_hdr(skb);
  520. left = skb->len - hlen; /* Space per frame */
  521. ptr = hlen; /* Where to start from */
  522. /* for bridged IP traffic encapsulated inside f.e. a vlan header,
  523. * we need to make room for the encapsulating header
  524. */
  525. ll_rs = LL_RESERVED_SPACE_EXTRA(rt->dst.dev, nf_bridge_pad(skb));
  526. /*
  527. * Fragment the datagram.
  528. */
  529. offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
  530. not_last_frag = iph->frag_off & htons(IP_MF);
  531. /*
  532. * Keep copying data until we run out.
  533. */
  534. while (left > 0) {
  535. len = left;
  536. /* IF: it doesn't fit, use 'mtu' - the data space left */
  537. if (len > mtu)
  538. len = mtu;
  539. /* IF: we are not sending up to and including the packet end
  540. then align the next start on an eight byte boundary */
  541. if (len < left) {
  542. len &= ~7;
  543. }
  544. /*
  545. * Allocate buffer.
  546. */
  547. if ((skb2 = alloc_skb(len+hlen+ll_rs, GFP_ATOMIC)) == NULL) {
  548. NETDEBUG(KERN_INFO "IP: frag: no memory for new fragment!\n");
  549. err = -ENOMEM;
  550. goto fail;
  551. }
  552. /*
  553. * Set up data on packet
  554. */
  555. ip_copy_metadata(skb2, skb);
  556. skb_reserve(skb2, ll_rs);
  557. skb_put(skb2, len + hlen);
  558. skb_reset_network_header(skb2);
  559. skb2->transport_header = skb2->network_header + hlen;
  560. /*
  561. * Charge the memory for the fragment to any owner
  562. * it might possess
  563. */
  564. if (skb->sk)
  565. skb_set_owner_w(skb2, skb->sk);
  566. /*
  567. * Copy the packet header into the new buffer.
  568. */
  569. skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
  570. /*
  571. * Copy a block of the IP datagram.
  572. */
  573. if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
  574. BUG();
  575. left -= len;
  576. /*
  577. * Fill in the new header fields.
  578. */
  579. iph = ip_hdr(skb2);
  580. iph->frag_off = htons((offset >> 3));
  581. /* ANK: dirty, but effective trick. Upgrade options only if
  582. * the segment to be fragmented was THE FIRST (otherwise,
  583. * options are already fixed) and make it ONCE
  584. * on the initial skb, so that all the following fragments
  585. * will inherit fixed options.
  586. */
  587. if (offset == 0)
  588. ip_options_fragment(skb);
  589. /*
  590. * Added AC : If we are fragmenting a fragment that's not the
  591. * last fragment then keep MF on each bit
  592. */
  593. if (left > 0 || not_last_frag)
  594. iph->frag_off |= htons(IP_MF);
  595. ptr += len;
  596. offset += len;
  597. /*
  598. * Put this fragment into the sending queue.
  599. */
  600. iph->tot_len = htons(len + hlen);
  601. ip_send_check(iph);
  602. err = output(skb2);
  603. if (err)
  604. goto fail;
  605. IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
  606. }
  607. consume_skb(skb);
  608. IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS);
  609. return err;
  610. fail:
  611. kfree_skb(skb);
  612. IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
  613. return err;
  614. }
  615. EXPORT_SYMBOL(ip_fragment);
  616. int
  617. ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
  618. {
  619. struct iovec *iov = from;
  620. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  621. if (memcpy_fromiovecend(to, iov, offset, len) < 0)
  622. return -EFAULT;
  623. } else {
  624. __wsum csum = 0;
  625. if (csum_partial_copy_fromiovecend(to, iov, offset, len, &csum) < 0)
  626. return -EFAULT;
  627. skb->csum = csum_block_add(skb->csum, csum, odd);
  628. }
  629. return 0;
  630. }
  631. EXPORT_SYMBOL(ip_generic_getfrag);
  632. static inline __wsum
  633. csum_page(struct page *page, int offset, int copy)
  634. {
  635. char *kaddr;
  636. __wsum csum;
  637. kaddr = kmap(page);
  638. csum = csum_partial(kaddr + offset, copy, 0);
  639. kunmap(page);
  640. return csum;
  641. }
  642. static inline int ip_ufo_append_data(struct sock *sk,
  643. struct sk_buff_head *queue,
  644. int getfrag(void *from, char *to, int offset, int len,
  645. int odd, struct sk_buff *skb),
  646. void *from, int length, int hh_len, int fragheaderlen,
  647. int transhdrlen, int maxfraglen, unsigned int flags)
  648. {
  649. struct sk_buff *skb;
  650. int err;
  651. /* There is support for UDP fragmentation offload by network
  652. * device, so create one single skb packet containing complete
  653. * udp datagram
  654. */
  655. if ((skb = skb_peek_tail(queue)) == NULL) {
  656. skb = sock_alloc_send_skb(sk,
  657. hh_len + fragheaderlen + transhdrlen + 20,
  658. (flags & MSG_DONTWAIT), &err);
  659. if (skb == NULL)
  660. return err;
  661. /* reserve space for Hardware header */
  662. skb_reserve(skb, hh_len);
  663. /* create space for UDP/IP header */
  664. skb_put(skb, fragheaderlen + transhdrlen);
  665. /* initialize network header pointer */
  666. skb_reset_network_header(skb);
  667. /* initialize protocol header pointer */
  668. skb->transport_header = skb->network_header + fragheaderlen;
  669. skb->ip_summed = CHECKSUM_PARTIAL;
  670. skb->csum = 0;
  671. /* specify the length of each IP datagram fragment */
  672. skb_shinfo(skb)->gso_size = maxfraglen - fragheaderlen;
  673. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  674. __skb_queue_tail(queue, skb);
  675. }
  676. return skb_append_datato_frags(sk, skb, getfrag, from,
  677. (length - transhdrlen));
  678. }
  679. static int __ip_append_data(struct sock *sk,
  680. struct flowi4 *fl4,
  681. struct sk_buff_head *queue,
  682. struct inet_cork *cork,
  683. struct page_frag *pfrag,
  684. int getfrag(void *from, char *to, int offset,
  685. int len, int odd, struct sk_buff *skb),
  686. void *from, int length, int transhdrlen,
  687. unsigned int flags)
  688. {
  689. struct inet_sock *inet = inet_sk(sk);
  690. struct sk_buff *skb;
  691. struct ip_options *opt = cork->opt;
  692. int hh_len;
  693. int exthdrlen;
  694. int mtu;
  695. int copy;
  696. int err;
  697. int offset = 0;
  698. unsigned int maxfraglen, fragheaderlen;
  699. int csummode = CHECKSUM_NONE;
  700. struct rtable *rt = (struct rtable *)cork->dst;
  701. skb = skb_peek_tail(queue);
  702. exthdrlen = !skb ? rt->dst.header_len : 0;
  703. mtu = cork->fragsize;
  704. hh_len = LL_RESERVED_SPACE(rt->dst.dev);
  705. fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
  706. maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
  707. if (cork->length + length > 0xFFFF - fragheaderlen) {
  708. ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
  709. mtu-exthdrlen);
  710. return -EMSGSIZE;
  711. }
  712. /*
  713. * transhdrlen > 0 means that this is the first fragment and we wish
  714. * it won't be fragmented in the future.
  715. */
  716. if (transhdrlen &&
  717. length + fragheaderlen <= mtu &&
  718. rt->dst.dev->features & NETIF_F_V4_CSUM &&
  719. !exthdrlen)
  720. csummode = CHECKSUM_PARTIAL;
  721. cork->length += length;
  722. if (((length > mtu) || (skb && skb_is_gso(skb))) &&
  723. (sk->sk_protocol == IPPROTO_UDP) &&
  724. (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len) {
  725. err = ip_ufo_append_data(sk, queue, getfrag, from, length,
  726. hh_len, fragheaderlen, transhdrlen,
  727. maxfraglen, flags);
  728. if (err)
  729. goto error;
  730. return 0;
  731. }
  732. /* So, what's going on in the loop below?
  733. *
  734. * We use calculated fragment length to generate chained skb,
  735. * each of segments is IP fragment ready for sending to network after
  736. * adding appropriate IP header.
  737. */
  738. if (!skb)
  739. goto alloc_new_skb;
  740. while (length > 0) {
  741. /* Check if the remaining data fits into current packet. */
  742. copy = mtu - skb->len;
  743. if (copy < length)
  744. copy = maxfraglen - skb->len;
  745. if (copy <= 0) {
  746. char *data;
  747. unsigned int datalen;
  748. unsigned int fraglen;
  749. unsigned int fraggap;
  750. unsigned int alloclen;
  751. struct sk_buff *skb_prev;
  752. alloc_new_skb:
  753. skb_prev = skb;
  754. if (skb_prev)
  755. fraggap = skb_prev->len - maxfraglen;
  756. else
  757. fraggap = 0;
  758. /*
  759. * If remaining data exceeds the mtu,
  760. * we know we need more fragment(s).
  761. */
  762. datalen = length + fraggap;
  763. if (datalen > mtu - fragheaderlen)
  764. datalen = maxfraglen - fragheaderlen;
  765. fraglen = datalen + fragheaderlen;
  766. if ((flags & MSG_MORE) &&
  767. !(rt->dst.dev->features&NETIF_F_SG))
  768. alloclen = mtu;
  769. else
  770. alloclen = fraglen;
  771. alloclen += exthdrlen;
  772. /* The last fragment gets additional space at tail.
  773. * Note, with MSG_MORE we overallocate on fragments,
  774. * because we have no idea what fragment will be
  775. * the last.
  776. */
  777. if (datalen == length + fraggap)
  778. alloclen += rt->dst.trailer_len;
  779. if (transhdrlen) {
  780. skb = sock_alloc_send_skb(sk,
  781. alloclen + hh_len + 15,
  782. (flags & MSG_DONTWAIT), &err);
  783. } else {
  784. skb = NULL;
  785. if (atomic_read(&sk->sk_wmem_alloc) <=
  786. 2 * sk->sk_sndbuf)
  787. skb = sock_wmalloc(sk,
  788. alloclen + hh_len + 15, 1,
  789. sk->sk_allocation);
  790. if (unlikely(skb == NULL))
  791. err = -ENOBUFS;
  792. else
  793. /* only the initial fragment is
  794. time stamped */
  795. cork->tx_flags = 0;
  796. }
  797. if (skb == NULL)
  798. goto error;
  799. /*
  800. * Fill in the control structures
  801. */
  802. skb->ip_summed = csummode;
  803. skb->csum = 0;
  804. skb_reserve(skb, hh_len);
  805. skb_shinfo(skb)->tx_flags = cork->tx_flags;
  806. /*
  807. * Find where to start putting bytes.
  808. */
  809. data = skb_put(skb, fraglen + exthdrlen);
  810. skb_set_network_header(skb, exthdrlen);
  811. skb->transport_header = (skb->network_header +
  812. fragheaderlen);
  813. data += fragheaderlen + exthdrlen;
  814. if (fraggap) {
  815. skb->csum = skb_copy_and_csum_bits(
  816. skb_prev, maxfraglen,
  817. data + transhdrlen, fraggap, 0);
  818. skb_prev->csum = csum_sub(skb_prev->csum,
  819. skb->csum);
  820. data += fraggap;
  821. pskb_trim_unique(skb_prev, maxfraglen);
  822. }
  823. copy = datalen - transhdrlen - fraggap;
  824. if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
  825. err = -EFAULT;
  826. kfree_skb(skb);
  827. goto error;
  828. }
  829. offset += copy;
  830. length -= datalen - fraggap;
  831. transhdrlen = 0;
  832. exthdrlen = 0;
  833. csummode = CHECKSUM_NONE;
  834. /*
  835. * Put the packet on the pending queue.
  836. */
  837. __skb_queue_tail(queue, skb);
  838. continue;
  839. }
  840. if (copy > length)
  841. copy = length;
  842. if (!(rt->dst.dev->features&NETIF_F_SG)) {
  843. unsigned int off;
  844. off = skb->len;
  845. if (getfrag(from, skb_put(skb, copy),
  846. offset, copy, off, skb) < 0) {
  847. __skb_trim(skb, off);
  848. err = -EFAULT;
  849. goto error;
  850. }
  851. } else {
  852. int i = skb_shinfo(skb)->nr_frags;
  853. err = -ENOMEM;
  854. if (!sk_page_frag_refill(sk, pfrag))
  855. goto error;
  856. if (!skb_can_coalesce(skb, i, pfrag->page,
  857. pfrag->offset)) {
  858. err = -EMSGSIZE;
  859. if (i == MAX_SKB_FRAGS)
  860. goto error;
  861. __skb_fill_page_desc(skb, i, pfrag->page,
  862. pfrag->offset, 0);
  863. skb_shinfo(skb)->nr_frags = ++i;
  864. get_page(pfrag->page);
  865. }
  866. copy = min_t(int, copy, pfrag->size - pfrag->offset);
  867. if (getfrag(from,
  868. page_address(pfrag->page) + pfrag->offset,
  869. offset, copy, skb->len, skb) < 0)
  870. goto error_efault;
  871. pfrag->offset += copy;
  872. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
  873. skb->len += copy;
  874. skb->data_len += copy;
  875. skb->truesize += copy;
  876. atomic_add(copy, &sk->sk_wmem_alloc);
  877. }
  878. offset += copy;
  879. length -= copy;
  880. }
  881. return 0;
  882. error_efault:
  883. err = -EFAULT;
  884. error:
  885. cork->length -= length;
  886. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
  887. return err;
  888. }
  889. static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
  890. struct ipcm_cookie *ipc, struct rtable **rtp)
  891. {
  892. struct inet_sock *inet = inet_sk(sk);
  893. struct ip_options_rcu *opt;
  894. struct rtable *rt;
  895. /*
  896. * setup for corking.
  897. */
  898. opt = ipc->opt;
  899. if (opt) {
  900. if (cork->opt == NULL) {
  901. cork->opt = kmalloc(sizeof(struct ip_options) + 40,
  902. sk->sk_allocation);
  903. if (unlikely(cork->opt == NULL))
  904. return -ENOBUFS;
  905. }
  906. memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
  907. cork->flags |= IPCORK_OPT;
  908. cork->addr = ipc->addr;
  909. }
  910. rt = *rtp;
  911. if (unlikely(!rt))
  912. return -EFAULT;
  913. /*
  914. * We steal reference to this route, caller should not release it
  915. */
  916. *rtp = NULL;
  917. cork->fragsize = inet->pmtudisc == IP_PMTUDISC_PROBE ?
  918. rt->dst.dev->mtu : dst_mtu(&rt->dst);
  919. cork->dst = &rt->dst;
  920. cork->length = 0;
  921. cork->tx_flags = ipc->tx_flags;
  922. return 0;
  923. }
  924. /*
  925. * ip_append_data() and ip_append_page() can make one large IP datagram
  926. * from many pieces of data. Each pieces will be holded on the socket
  927. * until ip_push_pending_frames() is called. Each piece can be a page
  928. * or non-page data.
  929. *
  930. * Not only UDP, other transport protocols - e.g. raw sockets - can use
  931. * this interface potentially.
  932. *
  933. * LATER: length must be adjusted by pad at tail, when it is required.
  934. */
  935. int ip_append_data(struct sock *sk, struct flowi4 *fl4,
  936. int getfrag(void *from, char *to, int offset, int len,
  937. int odd, struct sk_buff *skb),
  938. void *from, int length, int transhdrlen,
  939. struct ipcm_cookie *ipc, struct rtable **rtp,
  940. unsigned int flags)
  941. {
  942. struct inet_sock *inet = inet_sk(sk);
  943. int err;
  944. if (flags&MSG_PROBE)
  945. return 0;
  946. if (skb_queue_empty(&sk->sk_write_queue)) {
  947. err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
  948. if (err)
  949. return err;
  950. } else {
  951. transhdrlen = 0;
  952. }
  953. return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
  954. sk_page_frag(sk), getfrag,
  955. from, length, transhdrlen, flags);
  956. }
  957. ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
  958. int offset, size_t size, int flags)
  959. {
  960. struct inet_sock *inet = inet_sk(sk);
  961. struct sk_buff *skb;
  962. struct rtable *rt;
  963. struct ip_options *opt = NULL;
  964. struct inet_cork *cork;
  965. int hh_len;
  966. int mtu;
  967. int len;
  968. int err;
  969. unsigned int maxfraglen, fragheaderlen, fraggap;
  970. if (inet->hdrincl)
  971. return -EPERM;
  972. if (flags&MSG_PROBE)
  973. return 0;
  974. if (skb_queue_empty(&sk->sk_write_queue))
  975. return -EINVAL;
  976. cork = &inet->cork.base;
  977. rt = (struct rtable *)cork->dst;
  978. if (cork->flags & IPCORK_OPT)
  979. opt = cork->opt;
  980. if (!(rt->dst.dev->features&NETIF_F_SG))
  981. return -EOPNOTSUPP;
  982. hh_len = LL_RESERVED_SPACE(rt->dst.dev);
  983. mtu = cork->fragsize;
  984. fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
  985. maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
  986. if (cork->length + size > 0xFFFF - fragheaderlen) {
  987. ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, mtu);
  988. return -EMSGSIZE;
  989. }
  990. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
  991. return -EINVAL;
  992. cork->length += size;
  993. if ((size + skb->len > mtu) &&
  994. (sk->sk_protocol == IPPROTO_UDP) &&
  995. (rt->dst.dev->features & NETIF_F_UFO)) {
  996. skb_shinfo(skb)->gso_size = mtu - fragheaderlen;
  997. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  998. }
  999. while (size > 0) {
  1000. int i;
  1001. if (skb_is_gso(skb))
  1002. len = size;
  1003. else {
  1004. /* Check if the remaining data fits into current packet. */
  1005. len = mtu - skb->len;
  1006. if (len < size)
  1007. len = maxfraglen - skb->len;
  1008. }
  1009. if (len <= 0) {
  1010. struct sk_buff *skb_prev;
  1011. int alloclen;
  1012. skb_prev = skb;
  1013. fraggap = skb_prev->len - maxfraglen;
  1014. alloclen = fragheaderlen + hh_len + fraggap + 15;
  1015. skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
  1016. if (unlikely(!skb)) {
  1017. err = -ENOBUFS;
  1018. goto error;
  1019. }
  1020. /*
  1021. * Fill in the control structures
  1022. */
  1023. skb->ip_summed = CHECKSUM_NONE;
  1024. skb->csum = 0;
  1025. skb_reserve(skb, hh_len);
  1026. /*
  1027. * Find where to start putting bytes.
  1028. */
  1029. skb_put(skb, fragheaderlen + fraggap);
  1030. skb_reset_network_header(skb);
  1031. skb->transport_header = (skb->network_header +
  1032. fragheaderlen);
  1033. if (fraggap) {
  1034. skb->csum = skb_copy_and_csum_bits(skb_prev,
  1035. maxfraglen,
  1036. skb_transport_header(skb),
  1037. fraggap, 0);
  1038. skb_prev->csum = csum_sub(skb_prev->csum,
  1039. skb->csum);
  1040. pskb_trim_unique(skb_prev, maxfraglen);
  1041. }
  1042. /*
  1043. * Put the packet on the pending queue.
  1044. */
  1045. __skb_queue_tail(&sk->sk_write_queue, skb);
  1046. continue;
  1047. }
  1048. i = skb_shinfo(skb)->nr_frags;
  1049. if (len > size)
  1050. len = size;
  1051. if (skb_can_coalesce(skb, i, page, offset)) {
  1052. skb_frag_size_add(&skb_shinfo(skb)->frags[i-1], len);
  1053. } else if (i < MAX_SKB_FRAGS) {
  1054. get_page(page);
  1055. skb_fill_page_desc(skb, i, page, offset, len);
  1056. } else {
  1057. err = -EMSGSIZE;
  1058. goto error;
  1059. }
  1060. if (skb->ip_summed == CHECKSUM_NONE) {
  1061. __wsum csum;
  1062. csum = csum_page(page, offset, len);
  1063. skb->csum = csum_block_add(skb->csum, csum, skb->len);
  1064. }
  1065. skb->len += len;
  1066. skb->data_len += len;
  1067. skb->truesize += len;
  1068. atomic_add(len, &sk->sk_wmem_alloc);
  1069. offset += len;
  1070. size -= len;
  1071. }
  1072. return 0;
  1073. error:
  1074. cork->length -= size;
  1075. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
  1076. return err;
  1077. }
  1078. static void ip_cork_release(struct inet_cork *cork)
  1079. {
  1080. cork->flags &= ~IPCORK_OPT;
  1081. kfree(cork->opt);
  1082. cork->opt = NULL;
  1083. dst_release(cork->dst);
  1084. cork->dst = NULL;
  1085. }
  1086. /*
  1087. * Combined all pending IP fragments on the socket as one IP datagram
  1088. * and push them out.
  1089. */
  1090. struct sk_buff *__ip_make_skb(struct sock *sk,
  1091. struct flowi4 *fl4,
  1092. struct sk_buff_head *queue,
  1093. struct inet_cork *cork)
  1094. {
  1095. struct sk_buff *skb, *tmp_skb;
  1096. struct sk_buff **tail_skb;
  1097. struct inet_sock *inet = inet_sk(sk);
  1098. struct net *net = sock_net(sk);
  1099. struct ip_options *opt = NULL;
  1100. struct rtable *rt = (struct rtable *)cork->dst;
  1101. struct iphdr *iph;
  1102. __be16 df = 0;
  1103. __u8 ttl;
  1104. if ((skb = __skb_dequeue(queue)) == NULL)
  1105. goto out;
  1106. tail_skb = &(skb_shinfo(skb)->frag_list);
  1107. /* move skb->data to ip header from ext header */
  1108. if (skb->data < skb_network_header(skb))
  1109. __skb_pull(skb, skb_network_offset(skb));
  1110. while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
  1111. __skb_pull(tmp_skb, skb_network_header_len(skb));
  1112. *tail_skb = tmp_skb;
  1113. tail_skb = &(tmp_skb->next);
  1114. skb->len += tmp_skb->len;
  1115. skb->data_len += tmp_skb->len;
  1116. skb->truesize += tmp_skb->truesize;
  1117. tmp_skb->destructor = NULL;
  1118. tmp_skb->sk = NULL;
  1119. }
  1120. /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
  1121. * to fragment the frame generated here. No matter, what transforms
  1122. * how transforms change size of the packet, it will come out.
  1123. */
  1124. if (inet->pmtudisc < IP_PMTUDISC_DO)
  1125. skb->local_df = 1;
  1126. /* DF bit is set when we want to see DF on outgoing frames.
  1127. * If local_df is set too, we still allow to fragment this frame
  1128. * locally. */
  1129. if (inet->pmtudisc >= IP_PMTUDISC_DO ||
  1130. (skb->len <= dst_mtu(&rt->dst) &&
  1131. ip_dont_fragment(sk, &rt->dst)))
  1132. df = htons(IP_DF);
  1133. if (cork->flags & IPCORK_OPT)
  1134. opt = cork->opt;
  1135. if (rt->rt_type == RTN_MULTICAST)
  1136. ttl = inet->mc_ttl;
  1137. else
  1138. ttl = ip_select_ttl(inet, &rt->dst);
  1139. iph = (struct iphdr *)skb->data;
  1140. iph->version = 4;
  1141. iph->ihl = 5;
  1142. iph->tos = inet->tos;
  1143. iph->frag_off = df;
  1144. iph->ttl = ttl;
  1145. iph->protocol = sk->sk_protocol;
  1146. ip_copy_addrs(iph, fl4);
  1147. ip_select_ident(iph, &rt->dst, sk);
  1148. if (opt) {
  1149. iph->ihl += opt->optlen>>2;
  1150. ip_options_build(skb, opt, cork->addr, rt, 0);
  1151. }
  1152. skb->priority = sk->sk_priority;
  1153. skb->mark = sk->sk_mark;
  1154. /*
  1155. * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
  1156. * on dst refcount
  1157. */
  1158. cork->dst = NULL;
  1159. skb_dst_set(skb, &rt->dst);
  1160. if (iph->protocol == IPPROTO_ICMP)
  1161. icmp_out_count(net, ((struct icmphdr *)
  1162. skb_transport_header(skb))->type);
  1163. ip_cork_release(cork);
  1164. out:
  1165. return skb;
  1166. }
  1167. int ip_send_skb(struct net *net, struct sk_buff *skb)
  1168. {
  1169. int err;
  1170. err = ip_local_out(skb);
  1171. if (err) {
  1172. if (err > 0)
  1173. err = net_xmit_errno(err);
  1174. if (err)
  1175. IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
  1176. }
  1177. return err;
  1178. }
  1179. int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
  1180. {
  1181. struct sk_buff *skb;
  1182. skb = ip_finish_skb(sk, fl4);
  1183. if (!skb)
  1184. return 0;
  1185. /* Netfilter gets whole the not fragmented skb. */
  1186. return ip_send_skb(sock_net(sk), skb);
  1187. }
  1188. /*
  1189. * Throw away all pending data on the socket.
  1190. */
  1191. static void __ip_flush_pending_frames(struct sock *sk,
  1192. struct sk_buff_head *queue,
  1193. struct inet_cork *cork)
  1194. {
  1195. struct sk_buff *skb;
  1196. while ((skb = __skb_dequeue_tail(queue)) != NULL)
  1197. kfree_skb(skb);
  1198. ip_cork_release(cork);
  1199. }
  1200. void ip_flush_pending_frames(struct sock *sk)
  1201. {
  1202. __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
  1203. }
  1204. struct sk_buff *ip_make_skb(struct sock *sk,
  1205. struct flowi4 *fl4,
  1206. int getfrag(void *from, char *to, int offset,
  1207. int len, int odd, struct sk_buff *skb),
  1208. void *from, int length, int transhdrlen,
  1209. struct ipcm_cookie *ipc, struct rtable **rtp,
  1210. unsigned int flags)
  1211. {
  1212. struct inet_cork cork;
  1213. struct sk_buff_head queue;
  1214. int err;
  1215. if (flags & MSG_PROBE)
  1216. return NULL;
  1217. __skb_queue_head_init(&queue);
  1218. cork.flags = 0;
  1219. cork.addr = 0;
  1220. cork.opt = NULL;
  1221. err = ip_setup_cork(sk, &cork, ipc, rtp);
  1222. if (err)
  1223. return ERR_PTR(err);
  1224. err = __ip_append_data(sk, fl4, &queue, &cork,
  1225. &current->task_frag, getfrag,
  1226. from, length, transhdrlen, flags);
  1227. if (err) {
  1228. __ip_flush_pending_frames(sk, &queue, &cork);
  1229. return ERR_PTR(err);
  1230. }
  1231. return __ip_make_skb(sk, fl4, &queue, &cork);
  1232. }
  1233. /*
  1234. * Fetch data from kernel space and fill in checksum if needed.
  1235. */
  1236. static int ip_reply_glue_bits(void *dptr, char *to, int offset,
  1237. int len, int odd, struct sk_buff *skb)
  1238. {
  1239. __wsum csum;
  1240. csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
  1241. skb->csum = csum_block_add(skb->csum, csum, odd);
  1242. return 0;
  1243. }
  1244. /*
  1245. * Generic function to send a packet as reply to another packet.
  1246. * Used to send some TCP resets/acks so far.
  1247. *
  1248. * Use a fake percpu inet socket to avoid false sharing and contention.
  1249. */
  1250. static DEFINE_PER_CPU(struct inet_sock, unicast_sock) = {
  1251. .sk = {
  1252. .__sk_common = {
  1253. .skc_refcnt = ATOMIC_INIT(1),
  1254. },
  1255. .sk_wmem_alloc = ATOMIC_INIT(1),
  1256. .sk_allocation = GFP_ATOMIC,
  1257. .sk_flags = (1UL << SOCK_USE_WRITE_QUEUE),
  1258. },
  1259. .pmtudisc = IP_PMTUDISC_WANT,
  1260. .uc_ttl = -1,
  1261. };
  1262. void ip_send_unicast_reply(struct net *net, struct sk_buff *skb, __be32 daddr,
  1263. __be32 saddr, const struct ip_reply_arg *arg,
  1264. unsigned int len)
  1265. {
  1266. struct ip_options_data replyopts;
  1267. struct ipcm_cookie ipc;
  1268. struct flowi4 fl4;
  1269. struct rtable *rt = skb_rtable(skb);
  1270. struct sk_buff *nskb;
  1271. struct sock *sk;
  1272. struct inet_sock *inet;
  1273. if (ip_options_echo(&replyopts.opt.opt, skb))
  1274. return;
  1275. ipc.addr = daddr;
  1276. ipc.opt = NULL;
  1277. ipc.tx_flags = 0;
  1278. if (replyopts.opt.opt.optlen) {
  1279. ipc.opt = &replyopts.opt;
  1280. if (replyopts.opt.opt.srr)
  1281. daddr = replyopts.opt.opt.faddr;
  1282. }
  1283. flowi4_init_output(&fl4, arg->bound_dev_if, 0,
  1284. RT_TOS(arg->tos),
  1285. RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
  1286. ip_reply_arg_flowi_flags(arg),
  1287. daddr, saddr,
  1288. tcp_hdr(skb)->source, tcp_hdr(skb)->dest);
  1289. security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
  1290. rt = ip_route_output_key(net, &fl4);
  1291. if (IS_ERR(rt))
  1292. return;
  1293. inet = &get_cpu_var(unicast_sock);
  1294. inet->tos = arg->tos;
  1295. sk = &inet->sk;
  1296. sk->sk_priority = skb->priority;
  1297. sk->sk_protocol = ip_hdr(skb)->protocol;
  1298. sk->sk_bound_dev_if = arg->bound_dev_if;
  1299. sock_net_set(sk, net);
  1300. __skb_queue_head_init(&sk->sk_write_queue);
  1301. sk->sk_sndbuf = sysctl_wmem_default;
  1302. ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base, len, 0,
  1303. &ipc, &rt, MSG_DONTWAIT);
  1304. nskb = skb_peek(&sk->sk_write_queue);
  1305. if (nskb) {
  1306. if (arg->csumoffset >= 0)
  1307. *((__sum16 *)skb_transport_header(nskb) +
  1308. arg->csumoffset) = csum_fold(csum_add(nskb->csum,
  1309. arg->csum));
  1310. nskb->ip_summed = CHECKSUM_NONE;
  1311. skb_orphan(nskb);
  1312. skb_set_queue_mapping(nskb, skb_get_queue_mapping(skb));
  1313. ip_push_pending_frames(sk, &fl4);
  1314. }
  1315. put_cpu_var(unicast_sock);
  1316. ip_rt_put(rt);
  1317. }
  1318. void __init ip_init(void)
  1319. {
  1320. ip_rt_init();
  1321. inet_initpeers();
  1322. #if defined(CONFIG_IP_MULTICAST) && defined(CONFIG_PROC_FS)
  1323. igmp_mc_proc_init();
  1324. #endif
  1325. }