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