ip_output.c 40 KB

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