ip_output.c 39 KB

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