ip_output.c 38 KB

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