ip_output.c 40 KB

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