ip_output.c 41 KB

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