ip_output.c 41 KB

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