ip_output.c 40 KB

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