ip_output.c 34 KB

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