ip_output.c 39 KB

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