ip_output.c 40 KB

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