ip_output.c 40 KB

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