ip_output.c 38 KB

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