ip_output.c 44 KB

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