ip_output.c 40 KB

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