ip_output.c 38 KB

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