ip_output.c 43 KB

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