ip_output.c 39 KB

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