ip_output.c 41 KB

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