ip_output.c 39 KB

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