ip_output.c 39 KB

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