ip_output.c 38 KB

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