route.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016
  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. * ROUTE - implementation of the IP router.
  7. *
  8. * Version: $Id: route.c,v 1.103 2002/01/12 07:44:09 davem Exp $
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  13. * Linus Torvalds, <Linus.Torvalds@helsinki.fi>
  14. * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  15. *
  16. * Fixes:
  17. * Alan Cox : Verify area fixes.
  18. * Alan Cox : cli() protects routing changes
  19. * Rui Oliveira : ICMP routing table updates
  20. * (rco@di.uminho.pt) Routing table insertion and update
  21. * Linus Torvalds : Rewrote bits to be sensible
  22. * Alan Cox : Added BSD route gw semantics
  23. * Alan Cox : Super /proc >4K
  24. * Alan Cox : MTU in route table
  25. * Alan Cox : MSS actually. Also added the window
  26. * clamper.
  27. * Sam Lantinga : Fixed route matching in rt_del()
  28. * Alan Cox : Routing cache support.
  29. * Alan Cox : Removed compatibility cruft.
  30. * Alan Cox : RTF_REJECT support.
  31. * Alan Cox : TCP irtt support.
  32. * Jonathan Naylor : Added Metric support.
  33. * Miquel van Smoorenburg : BSD API fixes.
  34. * Miquel van Smoorenburg : Metrics.
  35. * Alan Cox : Use __u32 properly
  36. * Alan Cox : Aligned routing errors more closely with BSD
  37. * our system is still very different.
  38. * Alan Cox : Faster /proc handling
  39. * Alexey Kuznetsov : Massive rework to support tree based routing,
  40. * routing caches and better behaviour.
  41. *
  42. * Olaf Erb : irtt wasn't being copied right.
  43. * Bjorn Ekwall : Kerneld route support.
  44. * Alan Cox : Multicast fixed (I hope)
  45. * Pavel Krauz : Limited broadcast fixed
  46. * Mike McLagan : Routing by source
  47. * Alexey Kuznetsov : End of old history. Split to fib.c and
  48. * route.c and rewritten from scratch.
  49. * Andi Kleen : Load-limit warning messages.
  50. * Vitaly E. Lavrov : Transparent proxy revived after year coma.
  51. * Vitaly E. Lavrov : Race condition in ip_route_input_slow.
  52. * Tobias Ringstrom : Uninitialized res.type in ip_route_output_slow.
  53. * Vladimir V. Ivanov : IP rule info (flowid) is really useful.
  54. * Marc Boucher : routing by fwmark
  55. * Robert Olsson : Added rt_cache statistics
  56. * Arnaldo C. Melo : Convert proc stuff to seq_file
  57. * Eric Dumazet : hashed spinlocks and rt_check_expire() fixes.
  58. * Ilia Sotnikov : Ignore TOS on PMTUD and Redirect
  59. * Ilia Sotnikov : Removed TOS from hash calculations
  60. *
  61. * This program is free software; you can redistribute it and/or
  62. * modify it under the terms of the GNU General Public License
  63. * as published by the Free Software Foundation; either version
  64. * 2 of the License, or (at your option) any later version.
  65. */
  66. #include <linux/module.h>
  67. #include <asm/uaccess.h>
  68. #include <asm/system.h>
  69. #include <linux/bitops.h>
  70. #include <linux/types.h>
  71. #include <linux/kernel.h>
  72. #include <linux/mm.h>
  73. #include <linux/bootmem.h>
  74. #include <linux/string.h>
  75. #include <linux/socket.h>
  76. #include <linux/sockios.h>
  77. #include <linux/errno.h>
  78. #include <linux/in.h>
  79. #include <linux/inet.h>
  80. #include <linux/netdevice.h>
  81. #include <linux/proc_fs.h>
  82. #include <linux/init.h>
  83. #include <linux/workqueue.h>
  84. #include <linux/skbuff.h>
  85. #include <linux/inetdevice.h>
  86. #include <linux/igmp.h>
  87. #include <linux/pkt_sched.h>
  88. #include <linux/mroute.h>
  89. #include <linux/netfilter_ipv4.h>
  90. #include <linux/random.h>
  91. #include <linux/jhash.h>
  92. #include <linux/rcupdate.h>
  93. #include <linux/times.h>
  94. #include <net/net_namespace.h>
  95. #include <net/protocol.h>
  96. #include <net/ip.h>
  97. #include <net/route.h>
  98. #include <net/inetpeer.h>
  99. #include <net/sock.h>
  100. #include <net/ip_fib.h>
  101. #include <net/arp.h>
  102. #include <net/tcp.h>
  103. #include <net/icmp.h>
  104. #include <net/xfrm.h>
  105. #include <net/netevent.h>
  106. #include <net/rtnetlink.h>
  107. #ifdef CONFIG_SYSCTL
  108. #include <linux/sysctl.h>
  109. #endif
  110. #define RT_FL_TOS(oldflp) \
  111. ((u32)(oldflp->fl4_tos & (IPTOS_RT_MASK | RTO_ONLINK)))
  112. #define IP_MAX_MTU 0xFFF0
  113. #define RT_GC_TIMEOUT (300*HZ)
  114. static int ip_rt_min_delay = 2 * HZ;
  115. static int ip_rt_max_delay = 10 * HZ;
  116. static int ip_rt_max_size;
  117. static int ip_rt_gc_timeout = RT_GC_TIMEOUT;
  118. static int ip_rt_gc_interval = 60 * HZ;
  119. static int ip_rt_gc_min_interval = HZ / 2;
  120. static int ip_rt_redirect_number = 9;
  121. static int ip_rt_redirect_load = HZ / 50;
  122. static int ip_rt_redirect_silence = ((HZ / 50) << (9 + 1));
  123. static int ip_rt_error_cost = HZ;
  124. static int ip_rt_error_burst = 5 * HZ;
  125. static int ip_rt_gc_elasticity = 8;
  126. static int ip_rt_mtu_expires = 10 * 60 * HZ;
  127. static int ip_rt_min_pmtu = 512 + 20 + 20;
  128. static int ip_rt_min_advmss = 256;
  129. static int ip_rt_secret_interval = 10 * 60 * HZ;
  130. static unsigned long rt_deadline;
  131. #define RTprint(a...) printk(KERN_DEBUG a)
  132. static struct timer_list rt_flush_timer;
  133. static void rt_check_expire(struct work_struct *work);
  134. static DECLARE_DELAYED_WORK(expires_work, rt_check_expire);
  135. static struct timer_list rt_secret_timer;
  136. /*
  137. * Interface to generic destination cache.
  138. */
  139. static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
  140. static void ipv4_dst_destroy(struct dst_entry *dst);
  141. static void ipv4_dst_ifdown(struct dst_entry *dst,
  142. struct net_device *dev, int how);
  143. static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
  144. static void ipv4_link_failure(struct sk_buff *skb);
  145. static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
  146. static int rt_garbage_collect(void);
  147. static struct dst_ops ipv4_dst_ops = {
  148. .family = AF_INET,
  149. .protocol = __constant_htons(ETH_P_IP),
  150. .gc = rt_garbage_collect,
  151. .check = ipv4_dst_check,
  152. .destroy = ipv4_dst_destroy,
  153. .ifdown = ipv4_dst_ifdown,
  154. .negative_advice = ipv4_negative_advice,
  155. .link_failure = ipv4_link_failure,
  156. .update_pmtu = ip_rt_update_pmtu,
  157. .entry_size = sizeof(struct rtable),
  158. };
  159. #define ECN_OR_COST(class) TC_PRIO_##class
  160. const __u8 ip_tos2prio[16] = {
  161. TC_PRIO_BESTEFFORT,
  162. ECN_OR_COST(FILLER),
  163. TC_PRIO_BESTEFFORT,
  164. ECN_OR_COST(BESTEFFORT),
  165. TC_PRIO_BULK,
  166. ECN_OR_COST(BULK),
  167. TC_PRIO_BULK,
  168. ECN_OR_COST(BULK),
  169. TC_PRIO_INTERACTIVE,
  170. ECN_OR_COST(INTERACTIVE),
  171. TC_PRIO_INTERACTIVE,
  172. ECN_OR_COST(INTERACTIVE),
  173. TC_PRIO_INTERACTIVE_BULK,
  174. ECN_OR_COST(INTERACTIVE_BULK),
  175. TC_PRIO_INTERACTIVE_BULK,
  176. ECN_OR_COST(INTERACTIVE_BULK)
  177. };
  178. /*
  179. * Route cache.
  180. */
  181. /* The locking scheme is rather straight forward:
  182. *
  183. * 1) Read-Copy Update protects the buckets of the central route hash.
  184. * 2) Only writers remove entries, and they hold the lock
  185. * as they look at rtable reference counts.
  186. * 3) Only readers acquire references to rtable entries,
  187. * they do so with atomic increments and with the
  188. * lock held.
  189. */
  190. struct rt_hash_bucket {
  191. struct rtable *chain;
  192. };
  193. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
  194. defined(CONFIG_PROVE_LOCKING)
  195. /*
  196. * Instead of using one spinlock for each rt_hash_bucket, we use a table of spinlocks
  197. * The size of this table is a power of two and depends on the number of CPUS.
  198. * (on lockdep we have a quite big spinlock_t, so keep the size down there)
  199. */
  200. #ifdef CONFIG_LOCKDEP
  201. # define RT_HASH_LOCK_SZ 256
  202. #else
  203. # if NR_CPUS >= 32
  204. # define RT_HASH_LOCK_SZ 4096
  205. # elif NR_CPUS >= 16
  206. # define RT_HASH_LOCK_SZ 2048
  207. # elif NR_CPUS >= 8
  208. # define RT_HASH_LOCK_SZ 1024
  209. # elif NR_CPUS >= 4
  210. # define RT_HASH_LOCK_SZ 512
  211. # else
  212. # define RT_HASH_LOCK_SZ 256
  213. # endif
  214. #endif
  215. static spinlock_t *rt_hash_locks;
  216. # define rt_hash_lock_addr(slot) &rt_hash_locks[(slot) & (RT_HASH_LOCK_SZ - 1)]
  217. # define rt_hash_lock_init() { \
  218. int i; \
  219. rt_hash_locks = kmalloc(sizeof(spinlock_t) * RT_HASH_LOCK_SZ, GFP_KERNEL); \
  220. if (!rt_hash_locks) panic("IP: failed to allocate rt_hash_locks\n"); \
  221. for (i = 0; i < RT_HASH_LOCK_SZ; i++) \
  222. spin_lock_init(&rt_hash_locks[i]); \
  223. }
  224. #else
  225. # define rt_hash_lock_addr(slot) NULL
  226. # define rt_hash_lock_init()
  227. #endif
  228. static struct rt_hash_bucket *rt_hash_table;
  229. static unsigned rt_hash_mask;
  230. static unsigned int rt_hash_log;
  231. static unsigned int rt_hash_rnd;
  232. static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
  233. #define RT_CACHE_STAT_INC(field) \
  234. (__raw_get_cpu_var(rt_cache_stat).field++)
  235. static int rt_intern_hash(unsigned hash, struct rtable *rth,
  236. struct rtable **res);
  237. static unsigned int rt_hash_code(u32 daddr, u32 saddr)
  238. {
  239. return (jhash_2words(daddr, saddr, rt_hash_rnd)
  240. & rt_hash_mask);
  241. }
  242. #define rt_hash(daddr, saddr, idx) \
  243. rt_hash_code((__force u32)(__be32)(daddr),\
  244. (__force u32)(__be32)(saddr) ^ ((idx) << 5))
  245. #ifdef CONFIG_PROC_FS
  246. struct rt_cache_iter_state {
  247. int bucket;
  248. };
  249. static struct rtable *rt_cache_get_first(struct seq_file *seq)
  250. {
  251. struct rtable *r = NULL;
  252. struct rt_cache_iter_state *st = seq->private;
  253. for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
  254. rcu_read_lock_bh();
  255. r = rt_hash_table[st->bucket].chain;
  256. if (r)
  257. break;
  258. rcu_read_unlock_bh();
  259. }
  260. return r;
  261. }
  262. static struct rtable *rt_cache_get_next(struct seq_file *seq, struct rtable *r)
  263. {
  264. struct rt_cache_iter_state *st = rcu_dereference(seq->private);
  265. r = r->u.dst.rt_next;
  266. while (!r) {
  267. rcu_read_unlock_bh();
  268. if (--st->bucket < 0)
  269. break;
  270. rcu_read_lock_bh();
  271. r = rt_hash_table[st->bucket].chain;
  272. }
  273. return r;
  274. }
  275. static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
  276. {
  277. struct rtable *r = rt_cache_get_first(seq);
  278. if (r)
  279. while (pos && (r = rt_cache_get_next(seq, r)))
  280. --pos;
  281. return pos ? NULL : r;
  282. }
  283. static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
  284. {
  285. return *pos ? rt_cache_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  286. }
  287. static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  288. {
  289. struct rtable *r = NULL;
  290. if (v == SEQ_START_TOKEN)
  291. r = rt_cache_get_first(seq);
  292. else
  293. r = rt_cache_get_next(seq, v);
  294. ++*pos;
  295. return r;
  296. }
  297. static void rt_cache_seq_stop(struct seq_file *seq, void *v)
  298. {
  299. if (v && v != SEQ_START_TOKEN)
  300. rcu_read_unlock_bh();
  301. }
  302. static int rt_cache_seq_show(struct seq_file *seq, void *v)
  303. {
  304. if (v == SEQ_START_TOKEN)
  305. seq_printf(seq, "%-127s\n",
  306. "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
  307. "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
  308. "HHUptod\tSpecDst");
  309. else {
  310. struct rtable *r = v;
  311. char temp[256];
  312. sprintf(temp, "%s\t%08lX\t%08lX\t%8X\t%d\t%u\t%d\t"
  313. "%08lX\t%d\t%u\t%u\t%02X\t%d\t%1d\t%08X",
  314. r->u.dst.dev ? r->u.dst.dev->name : "*",
  315. (unsigned long)r->rt_dst, (unsigned long)r->rt_gateway,
  316. r->rt_flags, atomic_read(&r->u.dst.__refcnt),
  317. r->u.dst.__use, 0, (unsigned long)r->rt_src,
  318. (dst_metric(&r->u.dst, RTAX_ADVMSS) ?
  319. (int)dst_metric(&r->u.dst, RTAX_ADVMSS) + 40 : 0),
  320. dst_metric(&r->u.dst, RTAX_WINDOW),
  321. (int)((dst_metric(&r->u.dst, RTAX_RTT) >> 3) +
  322. dst_metric(&r->u.dst, RTAX_RTTVAR)),
  323. r->fl.fl4_tos,
  324. r->u.dst.hh ? atomic_read(&r->u.dst.hh->hh_refcnt) : -1,
  325. r->u.dst.hh ? (r->u.dst.hh->hh_output ==
  326. dev_queue_xmit) : 0,
  327. r->rt_spec_dst);
  328. seq_printf(seq, "%-127s\n", temp);
  329. }
  330. return 0;
  331. }
  332. static const struct seq_operations rt_cache_seq_ops = {
  333. .start = rt_cache_seq_start,
  334. .next = rt_cache_seq_next,
  335. .stop = rt_cache_seq_stop,
  336. .show = rt_cache_seq_show,
  337. };
  338. static int rt_cache_seq_open(struct inode *inode, struct file *file)
  339. {
  340. return seq_open_private(file, &rt_cache_seq_ops,
  341. sizeof(struct rt_cache_iter_state));
  342. }
  343. static const struct file_operations rt_cache_seq_fops = {
  344. .owner = THIS_MODULE,
  345. .open = rt_cache_seq_open,
  346. .read = seq_read,
  347. .llseek = seq_lseek,
  348. .release = seq_release_private,
  349. };
  350. static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
  351. {
  352. int cpu;
  353. if (*pos == 0)
  354. return SEQ_START_TOKEN;
  355. for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
  356. if (!cpu_possible(cpu))
  357. continue;
  358. *pos = cpu+1;
  359. return &per_cpu(rt_cache_stat, cpu);
  360. }
  361. return NULL;
  362. }
  363. static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  364. {
  365. int cpu;
  366. for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
  367. if (!cpu_possible(cpu))
  368. continue;
  369. *pos = cpu+1;
  370. return &per_cpu(rt_cache_stat, cpu);
  371. }
  372. return NULL;
  373. }
  374. static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
  375. {
  376. }
  377. static int rt_cpu_seq_show(struct seq_file *seq, void *v)
  378. {
  379. struct rt_cache_stat *st = v;
  380. if (v == SEQ_START_TOKEN) {
  381. seq_printf(seq, "entries in_hit in_slow_tot in_slow_mc in_no_route in_brd in_martian_dst in_martian_src out_hit out_slow_tot out_slow_mc gc_total gc_ignored gc_goal_miss gc_dst_overflow in_hlist_search out_hlist_search\n");
  382. return 0;
  383. }
  384. seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x "
  385. " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
  386. atomic_read(&ipv4_dst_ops.entries),
  387. st->in_hit,
  388. st->in_slow_tot,
  389. st->in_slow_mc,
  390. st->in_no_route,
  391. st->in_brd,
  392. st->in_martian_dst,
  393. st->in_martian_src,
  394. st->out_hit,
  395. st->out_slow_tot,
  396. st->out_slow_mc,
  397. st->gc_total,
  398. st->gc_ignored,
  399. st->gc_goal_miss,
  400. st->gc_dst_overflow,
  401. st->in_hlist_search,
  402. st->out_hlist_search
  403. );
  404. return 0;
  405. }
  406. static const struct seq_operations rt_cpu_seq_ops = {
  407. .start = rt_cpu_seq_start,
  408. .next = rt_cpu_seq_next,
  409. .stop = rt_cpu_seq_stop,
  410. .show = rt_cpu_seq_show,
  411. };
  412. static int rt_cpu_seq_open(struct inode *inode, struct file *file)
  413. {
  414. return seq_open(file, &rt_cpu_seq_ops);
  415. }
  416. static const struct file_operations rt_cpu_seq_fops = {
  417. .owner = THIS_MODULE,
  418. .open = rt_cpu_seq_open,
  419. .read = seq_read,
  420. .llseek = seq_lseek,
  421. .release = seq_release,
  422. };
  423. #endif /* CONFIG_PROC_FS */
  424. static __inline__ void rt_free(struct rtable *rt)
  425. {
  426. call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
  427. }
  428. static __inline__ void rt_drop(struct rtable *rt)
  429. {
  430. ip_rt_put(rt);
  431. call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
  432. }
  433. static __inline__ int rt_fast_clean(struct rtable *rth)
  434. {
  435. /* Kill broadcast/multicast entries very aggresively, if they
  436. collide in hash table with more useful entries */
  437. return (rth->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) &&
  438. rth->fl.iif && rth->u.dst.rt_next;
  439. }
  440. static __inline__ int rt_valuable(struct rtable *rth)
  441. {
  442. return (rth->rt_flags & (RTCF_REDIRECTED | RTCF_NOTIFY)) ||
  443. rth->u.dst.expires;
  444. }
  445. static int rt_may_expire(struct rtable *rth, unsigned long tmo1, unsigned long tmo2)
  446. {
  447. unsigned long age;
  448. int ret = 0;
  449. if (atomic_read(&rth->u.dst.__refcnt))
  450. goto out;
  451. ret = 1;
  452. if (rth->u.dst.expires &&
  453. time_after_eq(jiffies, rth->u.dst.expires))
  454. goto out;
  455. age = jiffies - rth->u.dst.lastuse;
  456. ret = 0;
  457. if ((age <= tmo1 && !rt_fast_clean(rth)) ||
  458. (age <= tmo2 && rt_valuable(rth)))
  459. goto out;
  460. ret = 1;
  461. out: return ret;
  462. }
  463. /* Bits of score are:
  464. * 31: very valuable
  465. * 30: not quite useless
  466. * 29..0: usage counter
  467. */
  468. static inline u32 rt_score(struct rtable *rt)
  469. {
  470. u32 score = jiffies - rt->u.dst.lastuse;
  471. score = ~score & ~(3<<30);
  472. if (rt_valuable(rt))
  473. score |= (1<<31);
  474. if (!rt->fl.iif ||
  475. !(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL)))
  476. score |= (1<<30);
  477. return score;
  478. }
  479. static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
  480. {
  481. return ((__force u32)((fl1->nl_u.ip4_u.daddr ^ fl2->nl_u.ip4_u.daddr) |
  482. (fl1->nl_u.ip4_u.saddr ^ fl2->nl_u.ip4_u.saddr)) |
  483. (fl1->mark ^ fl2->mark) |
  484. (*(u16 *)&fl1->nl_u.ip4_u.tos ^
  485. *(u16 *)&fl2->nl_u.ip4_u.tos) |
  486. (fl1->oif ^ fl2->oif) |
  487. (fl1->iif ^ fl2->iif)) == 0;
  488. }
  489. static void rt_check_expire(struct work_struct *work)
  490. {
  491. static unsigned int rover;
  492. unsigned int i = rover, goal;
  493. struct rtable *rth, **rthp;
  494. u64 mult;
  495. mult = ((u64)ip_rt_gc_interval) << rt_hash_log;
  496. if (ip_rt_gc_timeout > 1)
  497. do_div(mult, ip_rt_gc_timeout);
  498. goal = (unsigned int)mult;
  499. if (goal > rt_hash_mask)
  500. goal = rt_hash_mask + 1;
  501. for (; goal > 0; goal--) {
  502. unsigned long tmo = ip_rt_gc_timeout;
  503. i = (i + 1) & rt_hash_mask;
  504. rthp = &rt_hash_table[i].chain;
  505. if (*rthp == NULL)
  506. continue;
  507. spin_lock_bh(rt_hash_lock_addr(i));
  508. while ((rth = *rthp) != NULL) {
  509. if (rth->u.dst.expires) {
  510. /* Entry is expired even if it is in use */
  511. if (time_before_eq(jiffies, rth->u.dst.expires)) {
  512. tmo >>= 1;
  513. rthp = &rth->u.dst.rt_next;
  514. continue;
  515. }
  516. } else if (!rt_may_expire(rth, tmo, ip_rt_gc_timeout)) {
  517. tmo >>= 1;
  518. rthp = &rth->u.dst.rt_next;
  519. continue;
  520. }
  521. /* Cleanup aged off entries. */
  522. *rthp = rth->u.dst.rt_next;
  523. rt_free(rth);
  524. }
  525. spin_unlock_bh(rt_hash_lock_addr(i));
  526. }
  527. rover = i;
  528. schedule_delayed_work(&expires_work, ip_rt_gc_interval);
  529. }
  530. /* This can run from both BH and non-BH contexts, the latter
  531. * in the case of a forced flush event.
  532. */
  533. static void rt_run_flush(unsigned long dummy)
  534. {
  535. int i;
  536. struct rtable *rth, *next;
  537. rt_deadline = 0;
  538. get_random_bytes(&rt_hash_rnd, 4);
  539. for (i = rt_hash_mask; i >= 0; i--) {
  540. spin_lock_bh(rt_hash_lock_addr(i));
  541. rth = rt_hash_table[i].chain;
  542. if (rth)
  543. rt_hash_table[i].chain = NULL;
  544. spin_unlock_bh(rt_hash_lock_addr(i));
  545. for (; rth; rth = next) {
  546. next = rth->u.dst.rt_next;
  547. rt_free(rth);
  548. }
  549. }
  550. }
  551. static DEFINE_SPINLOCK(rt_flush_lock);
  552. void rt_cache_flush(int delay)
  553. {
  554. unsigned long now = jiffies;
  555. int user_mode = !in_softirq();
  556. if (delay < 0)
  557. delay = ip_rt_min_delay;
  558. spin_lock_bh(&rt_flush_lock);
  559. if (del_timer(&rt_flush_timer) && delay > 0 && rt_deadline) {
  560. long tmo = (long)(rt_deadline - now);
  561. /* If flush timer is already running
  562. and flush request is not immediate (delay > 0):
  563. if deadline is not achieved, prolongate timer to "delay",
  564. otherwise fire it at deadline time.
  565. */
  566. if (user_mode && tmo < ip_rt_max_delay-ip_rt_min_delay)
  567. tmo = 0;
  568. if (delay > tmo)
  569. delay = tmo;
  570. }
  571. if (delay <= 0) {
  572. spin_unlock_bh(&rt_flush_lock);
  573. rt_run_flush(0);
  574. return;
  575. }
  576. if (rt_deadline == 0)
  577. rt_deadline = now + ip_rt_max_delay;
  578. mod_timer(&rt_flush_timer, now+delay);
  579. spin_unlock_bh(&rt_flush_lock);
  580. }
  581. static void rt_secret_rebuild(unsigned long dummy)
  582. {
  583. unsigned long now = jiffies;
  584. rt_cache_flush(0);
  585. mod_timer(&rt_secret_timer, now + ip_rt_secret_interval);
  586. }
  587. /*
  588. Short description of GC goals.
  589. We want to build algorithm, which will keep routing cache
  590. at some equilibrium point, when number of aged off entries
  591. is kept approximately equal to newly generated ones.
  592. Current expiration strength is variable "expire".
  593. We try to adjust it dynamically, so that if networking
  594. is idle expires is large enough to keep enough of warm entries,
  595. and when load increases it reduces to limit cache size.
  596. */
  597. static int rt_garbage_collect(void)
  598. {
  599. static unsigned long expire = RT_GC_TIMEOUT;
  600. static unsigned long last_gc;
  601. static int rover;
  602. static int equilibrium;
  603. struct rtable *rth, **rthp;
  604. unsigned long now = jiffies;
  605. int goal;
  606. /*
  607. * Garbage collection is pretty expensive,
  608. * do not make it too frequently.
  609. */
  610. RT_CACHE_STAT_INC(gc_total);
  611. if (now - last_gc < ip_rt_gc_min_interval &&
  612. atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size) {
  613. RT_CACHE_STAT_INC(gc_ignored);
  614. goto out;
  615. }
  616. /* Calculate number of entries, which we want to expire now. */
  617. goal = atomic_read(&ipv4_dst_ops.entries) -
  618. (ip_rt_gc_elasticity << rt_hash_log);
  619. if (goal <= 0) {
  620. if (equilibrium < ipv4_dst_ops.gc_thresh)
  621. equilibrium = ipv4_dst_ops.gc_thresh;
  622. goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
  623. if (goal > 0) {
  624. equilibrium += min_t(unsigned int, goal / 2, rt_hash_mask + 1);
  625. goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
  626. }
  627. } else {
  628. /* We are in dangerous area. Try to reduce cache really
  629. * aggressively.
  630. */
  631. goal = max_t(unsigned int, goal / 2, rt_hash_mask + 1);
  632. equilibrium = atomic_read(&ipv4_dst_ops.entries) - goal;
  633. }
  634. if (now - last_gc >= ip_rt_gc_min_interval)
  635. last_gc = now;
  636. if (goal <= 0) {
  637. equilibrium += goal;
  638. goto work_done;
  639. }
  640. do {
  641. int i, k;
  642. for (i = rt_hash_mask, k = rover; i >= 0; i--) {
  643. unsigned long tmo = expire;
  644. k = (k + 1) & rt_hash_mask;
  645. rthp = &rt_hash_table[k].chain;
  646. spin_lock_bh(rt_hash_lock_addr(k));
  647. while ((rth = *rthp) != NULL) {
  648. if (!rt_may_expire(rth, tmo, expire)) {
  649. tmo >>= 1;
  650. rthp = &rth->u.dst.rt_next;
  651. continue;
  652. }
  653. *rthp = rth->u.dst.rt_next;
  654. rt_free(rth);
  655. goal--;
  656. }
  657. spin_unlock_bh(rt_hash_lock_addr(k));
  658. if (goal <= 0)
  659. break;
  660. }
  661. rover = k;
  662. if (goal <= 0)
  663. goto work_done;
  664. /* Goal is not achieved. We stop process if:
  665. - if expire reduced to zero. Otherwise, expire is halfed.
  666. - if table is not full.
  667. - if we are called from interrupt.
  668. - jiffies check is just fallback/debug loop breaker.
  669. We will not spin here for long time in any case.
  670. */
  671. RT_CACHE_STAT_INC(gc_goal_miss);
  672. if (expire == 0)
  673. break;
  674. expire >>= 1;
  675. #if RT_CACHE_DEBUG >= 2
  676. printk(KERN_DEBUG "expire>> %u %d %d %d\n", expire,
  677. atomic_read(&ipv4_dst_ops.entries), goal, i);
  678. #endif
  679. if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
  680. goto out;
  681. } while (!in_softirq() && time_before_eq(jiffies, now));
  682. if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
  683. goto out;
  684. if (net_ratelimit())
  685. printk(KERN_WARNING "dst cache overflow\n");
  686. RT_CACHE_STAT_INC(gc_dst_overflow);
  687. return 1;
  688. work_done:
  689. expire += ip_rt_gc_min_interval;
  690. if (expire > ip_rt_gc_timeout ||
  691. atomic_read(&ipv4_dst_ops.entries) < ipv4_dst_ops.gc_thresh)
  692. expire = ip_rt_gc_timeout;
  693. #if RT_CACHE_DEBUG >= 2
  694. printk(KERN_DEBUG "expire++ %u %d %d %d\n", expire,
  695. atomic_read(&ipv4_dst_ops.entries), goal, rover);
  696. #endif
  697. out: return 0;
  698. }
  699. static int rt_intern_hash(unsigned hash, struct rtable *rt, struct rtable **rp)
  700. {
  701. struct rtable *rth, **rthp;
  702. unsigned long now;
  703. struct rtable *cand, **candp;
  704. u32 min_score;
  705. int chain_length;
  706. int attempts = !in_softirq();
  707. restart:
  708. chain_length = 0;
  709. min_score = ~(u32)0;
  710. cand = NULL;
  711. candp = NULL;
  712. now = jiffies;
  713. rthp = &rt_hash_table[hash].chain;
  714. spin_lock_bh(rt_hash_lock_addr(hash));
  715. while ((rth = *rthp) != NULL) {
  716. if (compare_keys(&rth->fl, &rt->fl)) {
  717. /* Put it first */
  718. *rthp = rth->u.dst.rt_next;
  719. /*
  720. * Since lookup is lockfree, the deletion
  721. * must be visible to another weakly ordered CPU before
  722. * the insertion at the start of the hash chain.
  723. */
  724. rcu_assign_pointer(rth->u.dst.rt_next,
  725. rt_hash_table[hash].chain);
  726. /*
  727. * Since lookup is lockfree, the update writes
  728. * must be ordered for consistency on SMP.
  729. */
  730. rcu_assign_pointer(rt_hash_table[hash].chain, rth);
  731. rth->u.dst.__use++;
  732. dst_hold(&rth->u.dst);
  733. rth->u.dst.lastuse = now;
  734. spin_unlock_bh(rt_hash_lock_addr(hash));
  735. rt_drop(rt);
  736. *rp = rth;
  737. return 0;
  738. }
  739. if (!atomic_read(&rth->u.dst.__refcnt)) {
  740. u32 score = rt_score(rth);
  741. if (score <= min_score) {
  742. cand = rth;
  743. candp = rthp;
  744. min_score = score;
  745. }
  746. }
  747. chain_length++;
  748. rthp = &rth->u.dst.rt_next;
  749. }
  750. if (cand) {
  751. /* ip_rt_gc_elasticity used to be average length of chain
  752. * length, when exceeded gc becomes really aggressive.
  753. *
  754. * The second limit is less certain. At the moment it allows
  755. * only 2 entries per bucket. We will see.
  756. */
  757. if (chain_length > ip_rt_gc_elasticity) {
  758. *candp = cand->u.dst.rt_next;
  759. rt_free(cand);
  760. }
  761. }
  762. /* Try to bind route to arp only if it is output
  763. route or unicast forwarding path.
  764. */
  765. if (rt->rt_type == RTN_UNICAST || rt->fl.iif == 0) {
  766. int err = arp_bind_neighbour(&rt->u.dst);
  767. if (err) {
  768. spin_unlock_bh(rt_hash_lock_addr(hash));
  769. if (err != -ENOBUFS) {
  770. rt_drop(rt);
  771. return err;
  772. }
  773. /* Neighbour tables are full and nothing
  774. can be released. Try to shrink route cache,
  775. it is most likely it holds some neighbour records.
  776. */
  777. if (attempts-- > 0) {
  778. int saved_elasticity = ip_rt_gc_elasticity;
  779. int saved_int = ip_rt_gc_min_interval;
  780. ip_rt_gc_elasticity = 1;
  781. ip_rt_gc_min_interval = 0;
  782. rt_garbage_collect();
  783. ip_rt_gc_min_interval = saved_int;
  784. ip_rt_gc_elasticity = saved_elasticity;
  785. goto restart;
  786. }
  787. if (net_ratelimit())
  788. printk(KERN_WARNING "Neighbour table overflow.\n");
  789. rt_drop(rt);
  790. return -ENOBUFS;
  791. }
  792. }
  793. rt->u.dst.rt_next = rt_hash_table[hash].chain;
  794. #if RT_CACHE_DEBUG >= 2
  795. if (rt->u.dst.rt_next) {
  796. struct rtable *trt;
  797. printk(KERN_DEBUG "rt_cache @%02x: %u.%u.%u.%u", hash,
  798. NIPQUAD(rt->rt_dst));
  799. for (trt = rt->u.dst.rt_next; trt; trt = trt->u.dst.rt_next)
  800. printk(" . %u.%u.%u.%u", NIPQUAD(trt->rt_dst));
  801. printk("\n");
  802. }
  803. #endif
  804. rt_hash_table[hash].chain = rt;
  805. spin_unlock_bh(rt_hash_lock_addr(hash));
  806. *rp = rt;
  807. return 0;
  808. }
  809. void rt_bind_peer(struct rtable *rt, int create)
  810. {
  811. static DEFINE_SPINLOCK(rt_peer_lock);
  812. struct inet_peer *peer;
  813. peer = inet_getpeer(rt->rt_dst, create);
  814. spin_lock_bh(&rt_peer_lock);
  815. if (rt->peer == NULL) {
  816. rt->peer = peer;
  817. peer = NULL;
  818. }
  819. spin_unlock_bh(&rt_peer_lock);
  820. if (peer)
  821. inet_putpeer(peer);
  822. }
  823. /*
  824. * Peer allocation may fail only in serious out-of-memory conditions. However
  825. * we still can generate some output.
  826. * Random ID selection looks a bit dangerous because we have no chances to
  827. * select ID being unique in a reasonable period of time.
  828. * But broken packet identifier may be better than no packet at all.
  829. */
  830. static void ip_select_fb_ident(struct iphdr *iph)
  831. {
  832. static DEFINE_SPINLOCK(ip_fb_id_lock);
  833. static u32 ip_fallback_id;
  834. u32 salt;
  835. spin_lock_bh(&ip_fb_id_lock);
  836. salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr);
  837. iph->id = htons(salt & 0xFFFF);
  838. ip_fallback_id = salt;
  839. spin_unlock_bh(&ip_fb_id_lock);
  840. }
  841. void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more)
  842. {
  843. struct rtable *rt = (struct rtable *) dst;
  844. if (rt) {
  845. if (rt->peer == NULL)
  846. rt_bind_peer(rt, 1);
  847. /* If peer is attached to destination, it is never detached,
  848. so that we need not to grab a lock to dereference it.
  849. */
  850. if (rt->peer) {
  851. iph->id = htons(inet_getid(rt->peer, more));
  852. return;
  853. }
  854. } else
  855. printk(KERN_DEBUG "rt_bind_peer(0) @%p\n",
  856. __builtin_return_address(0));
  857. ip_select_fb_ident(iph);
  858. }
  859. static void rt_del(unsigned hash, struct rtable *rt)
  860. {
  861. struct rtable **rthp;
  862. spin_lock_bh(rt_hash_lock_addr(hash));
  863. ip_rt_put(rt);
  864. for (rthp = &rt_hash_table[hash].chain; *rthp;
  865. rthp = &(*rthp)->u.dst.rt_next)
  866. if (*rthp == rt) {
  867. *rthp = rt->u.dst.rt_next;
  868. rt_free(rt);
  869. break;
  870. }
  871. spin_unlock_bh(rt_hash_lock_addr(hash));
  872. }
  873. void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
  874. __be32 saddr, struct net_device *dev)
  875. {
  876. int i, k;
  877. struct in_device *in_dev = in_dev_get(dev);
  878. struct rtable *rth, **rthp;
  879. __be32 skeys[2] = { saddr, 0 };
  880. int ikeys[2] = { dev->ifindex, 0 };
  881. struct netevent_redirect netevent;
  882. if (!in_dev)
  883. return;
  884. if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev)
  885. || MULTICAST(new_gw) || BADCLASS(new_gw) || ZERONET(new_gw))
  886. goto reject_redirect;
  887. if (!IN_DEV_SHARED_MEDIA(in_dev)) {
  888. if (!inet_addr_onlink(in_dev, new_gw, old_gw))
  889. goto reject_redirect;
  890. if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
  891. goto reject_redirect;
  892. } else {
  893. if (inet_addr_type(new_gw) != RTN_UNICAST)
  894. goto reject_redirect;
  895. }
  896. for (i = 0; i < 2; i++) {
  897. for (k = 0; k < 2; k++) {
  898. unsigned hash = rt_hash(daddr, skeys[i], ikeys[k]);
  899. rthp=&rt_hash_table[hash].chain;
  900. rcu_read_lock();
  901. while ((rth = rcu_dereference(*rthp)) != NULL) {
  902. struct rtable *rt;
  903. if (rth->fl.fl4_dst != daddr ||
  904. rth->fl.fl4_src != skeys[i] ||
  905. rth->fl.oif != ikeys[k] ||
  906. rth->fl.iif != 0) {
  907. rthp = &rth->u.dst.rt_next;
  908. continue;
  909. }
  910. if (rth->rt_dst != daddr ||
  911. rth->rt_src != saddr ||
  912. rth->u.dst.error ||
  913. rth->rt_gateway != old_gw ||
  914. rth->u.dst.dev != dev)
  915. break;
  916. dst_hold(&rth->u.dst);
  917. rcu_read_unlock();
  918. rt = dst_alloc(&ipv4_dst_ops);
  919. if (rt == NULL) {
  920. ip_rt_put(rth);
  921. in_dev_put(in_dev);
  922. return;
  923. }
  924. /* Copy all the information. */
  925. *rt = *rth;
  926. INIT_RCU_HEAD(&rt->u.dst.rcu_head);
  927. rt->u.dst.__use = 1;
  928. atomic_set(&rt->u.dst.__refcnt, 1);
  929. rt->u.dst.child = NULL;
  930. if (rt->u.dst.dev)
  931. dev_hold(rt->u.dst.dev);
  932. if (rt->idev)
  933. in_dev_hold(rt->idev);
  934. rt->u.dst.obsolete = 0;
  935. rt->u.dst.lastuse = jiffies;
  936. rt->u.dst.path = &rt->u.dst;
  937. rt->u.dst.neighbour = NULL;
  938. rt->u.dst.hh = NULL;
  939. rt->u.dst.xfrm = NULL;
  940. rt->rt_flags |= RTCF_REDIRECTED;
  941. /* Gateway is different ... */
  942. rt->rt_gateway = new_gw;
  943. /* Redirect received -> path was valid */
  944. dst_confirm(&rth->u.dst);
  945. if (rt->peer)
  946. atomic_inc(&rt->peer->refcnt);
  947. if (arp_bind_neighbour(&rt->u.dst) ||
  948. !(rt->u.dst.neighbour->nud_state &
  949. NUD_VALID)) {
  950. if (rt->u.dst.neighbour)
  951. neigh_event_send(rt->u.dst.neighbour, NULL);
  952. ip_rt_put(rth);
  953. rt_drop(rt);
  954. goto do_next;
  955. }
  956. netevent.old = &rth->u.dst;
  957. netevent.new = &rt->u.dst;
  958. call_netevent_notifiers(NETEVENT_REDIRECT,
  959. &netevent);
  960. rt_del(hash, rth);
  961. if (!rt_intern_hash(hash, rt, &rt))
  962. ip_rt_put(rt);
  963. goto do_next;
  964. }
  965. rcu_read_unlock();
  966. do_next:
  967. ;
  968. }
  969. }
  970. in_dev_put(in_dev);
  971. return;
  972. reject_redirect:
  973. #ifdef CONFIG_IP_ROUTE_VERBOSE
  974. if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
  975. printk(KERN_INFO "Redirect from %u.%u.%u.%u on %s about "
  976. "%u.%u.%u.%u ignored.\n"
  977. " Advised path = %u.%u.%u.%u -> %u.%u.%u.%u\n",
  978. NIPQUAD(old_gw), dev->name, NIPQUAD(new_gw),
  979. NIPQUAD(saddr), NIPQUAD(daddr));
  980. #endif
  981. in_dev_put(in_dev);
  982. }
  983. static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
  984. {
  985. struct rtable *rt = (struct rtable*)dst;
  986. struct dst_entry *ret = dst;
  987. if (rt) {
  988. if (dst->obsolete) {
  989. ip_rt_put(rt);
  990. ret = NULL;
  991. } else if ((rt->rt_flags & RTCF_REDIRECTED) ||
  992. rt->u.dst.expires) {
  993. unsigned hash = rt_hash(rt->fl.fl4_dst, rt->fl.fl4_src,
  994. rt->fl.oif);
  995. #if RT_CACHE_DEBUG >= 1
  996. printk(KERN_DEBUG "ip_rt_advice: redirect to "
  997. "%u.%u.%u.%u/%02x dropped\n",
  998. NIPQUAD(rt->rt_dst), rt->fl.fl4_tos);
  999. #endif
  1000. rt_del(hash, rt);
  1001. ret = NULL;
  1002. }
  1003. }
  1004. return ret;
  1005. }
  1006. /*
  1007. * Algorithm:
  1008. * 1. The first ip_rt_redirect_number redirects are sent
  1009. * with exponential backoff, then we stop sending them at all,
  1010. * assuming that the host ignores our redirects.
  1011. * 2. If we did not see packets requiring redirects
  1012. * during ip_rt_redirect_silence, we assume that the host
  1013. * forgot redirected route and start to send redirects again.
  1014. *
  1015. * This algorithm is much cheaper and more intelligent than dumb load limiting
  1016. * in icmp.c.
  1017. *
  1018. * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
  1019. * and "frag. need" (breaks PMTU discovery) in icmp.c.
  1020. */
  1021. void ip_rt_send_redirect(struct sk_buff *skb)
  1022. {
  1023. struct rtable *rt = (struct rtable*)skb->dst;
  1024. struct in_device *in_dev = in_dev_get(rt->u.dst.dev);
  1025. if (!in_dev)
  1026. return;
  1027. if (!IN_DEV_TX_REDIRECTS(in_dev))
  1028. goto out;
  1029. /* No redirected packets during ip_rt_redirect_silence;
  1030. * reset the algorithm.
  1031. */
  1032. if (time_after(jiffies, rt->u.dst.rate_last + ip_rt_redirect_silence))
  1033. rt->u.dst.rate_tokens = 0;
  1034. /* Too many ignored redirects; do not send anything
  1035. * set u.dst.rate_last to the last seen redirected packet.
  1036. */
  1037. if (rt->u.dst.rate_tokens >= ip_rt_redirect_number) {
  1038. rt->u.dst.rate_last = jiffies;
  1039. goto out;
  1040. }
  1041. /* Check for load limit; set rate_last to the latest sent
  1042. * redirect.
  1043. */
  1044. if (rt->u.dst.rate_tokens == 0 ||
  1045. time_after(jiffies,
  1046. (rt->u.dst.rate_last +
  1047. (ip_rt_redirect_load << rt->u.dst.rate_tokens)))) {
  1048. icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
  1049. rt->u.dst.rate_last = jiffies;
  1050. ++rt->u.dst.rate_tokens;
  1051. #ifdef CONFIG_IP_ROUTE_VERBOSE
  1052. if (IN_DEV_LOG_MARTIANS(in_dev) &&
  1053. rt->u.dst.rate_tokens == ip_rt_redirect_number &&
  1054. net_ratelimit())
  1055. printk(KERN_WARNING "host %u.%u.%u.%u/if%d ignores "
  1056. "redirects for %u.%u.%u.%u to %u.%u.%u.%u.\n",
  1057. NIPQUAD(rt->rt_src), rt->rt_iif,
  1058. NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_gateway));
  1059. #endif
  1060. }
  1061. out:
  1062. in_dev_put(in_dev);
  1063. }
  1064. static int ip_error(struct sk_buff *skb)
  1065. {
  1066. struct rtable *rt = (struct rtable*)skb->dst;
  1067. unsigned long now;
  1068. int code;
  1069. switch (rt->u.dst.error) {
  1070. case EINVAL:
  1071. default:
  1072. goto out;
  1073. case EHOSTUNREACH:
  1074. code = ICMP_HOST_UNREACH;
  1075. break;
  1076. case ENETUNREACH:
  1077. code = ICMP_NET_UNREACH;
  1078. break;
  1079. case EACCES:
  1080. code = ICMP_PKT_FILTERED;
  1081. break;
  1082. }
  1083. now = jiffies;
  1084. rt->u.dst.rate_tokens += now - rt->u.dst.rate_last;
  1085. if (rt->u.dst.rate_tokens > ip_rt_error_burst)
  1086. rt->u.dst.rate_tokens = ip_rt_error_burst;
  1087. rt->u.dst.rate_last = now;
  1088. if (rt->u.dst.rate_tokens >= ip_rt_error_cost) {
  1089. rt->u.dst.rate_tokens -= ip_rt_error_cost;
  1090. icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
  1091. }
  1092. out: kfree_skb(skb);
  1093. return 0;
  1094. }
  1095. /*
  1096. * The last two values are not from the RFC but
  1097. * are needed for AMPRnet AX.25 paths.
  1098. */
  1099. static const unsigned short mtu_plateau[] =
  1100. {32000, 17914, 8166, 4352, 2002, 1492, 576, 296, 216, 128 };
  1101. static __inline__ unsigned short guess_mtu(unsigned short old_mtu)
  1102. {
  1103. int i;
  1104. for (i = 0; i < ARRAY_SIZE(mtu_plateau); i++)
  1105. if (old_mtu > mtu_plateau[i])
  1106. return mtu_plateau[i];
  1107. return 68;
  1108. }
  1109. unsigned short ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mtu)
  1110. {
  1111. int i;
  1112. unsigned short old_mtu = ntohs(iph->tot_len);
  1113. struct rtable *rth;
  1114. __be32 skeys[2] = { iph->saddr, 0, };
  1115. __be32 daddr = iph->daddr;
  1116. unsigned short est_mtu = 0;
  1117. if (ipv4_config.no_pmtu_disc)
  1118. return 0;
  1119. for (i = 0; i < 2; i++) {
  1120. unsigned hash = rt_hash(daddr, skeys[i], 0);
  1121. rcu_read_lock();
  1122. for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
  1123. rth = rcu_dereference(rth->u.dst.rt_next)) {
  1124. if (rth->fl.fl4_dst == daddr &&
  1125. rth->fl.fl4_src == skeys[i] &&
  1126. rth->rt_dst == daddr &&
  1127. rth->rt_src == iph->saddr &&
  1128. rth->fl.iif == 0 &&
  1129. !(dst_metric_locked(&rth->u.dst, RTAX_MTU))) {
  1130. unsigned short mtu = new_mtu;
  1131. if (new_mtu < 68 || new_mtu >= old_mtu) {
  1132. /* BSD 4.2 compatibility hack :-( */
  1133. if (mtu == 0 &&
  1134. old_mtu >= rth->u.dst.metrics[RTAX_MTU-1] &&
  1135. old_mtu >= 68 + (iph->ihl << 2))
  1136. old_mtu -= iph->ihl << 2;
  1137. mtu = guess_mtu(old_mtu);
  1138. }
  1139. if (mtu <= rth->u.dst.metrics[RTAX_MTU-1]) {
  1140. if (mtu < rth->u.dst.metrics[RTAX_MTU-1]) {
  1141. dst_confirm(&rth->u.dst);
  1142. if (mtu < ip_rt_min_pmtu) {
  1143. mtu = ip_rt_min_pmtu;
  1144. rth->u.dst.metrics[RTAX_LOCK-1] |=
  1145. (1 << RTAX_MTU);
  1146. }
  1147. rth->u.dst.metrics[RTAX_MTU-1] = mtu;
  1148. dst_set_expires(&rth->u.dst,
  1149. ip_rt_mtu_expires);
  1150. }
  1151. est_mtu = mtu;
  1152. }
  1153. }
  1154. }
  1155. rcu_read_unlock();
  1156. }
  1157. return est_mtu ? : new_mtu;
  1158. }
  1159. static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
  1160. {
  1161. if (dst->metrics[RTAX_MTU-1] > mtu && mtu >= 68 &&
  1162. !(dst_metric_locked(dst, RTAX_MTU))) {
  1163. if (mtu < ip_rt_min_pmtu) {
  1164. mtu = ip_rt_min_pmtu;
  1165. dst->metrics[RTAX_LOCK-1] |= (1 << RTAX_MTU);
  1166. }
  1167. dst->metrics[RTAX_MTU-1] = mtu;
  1168. dst_set_expires(dst, ip_rt_mtu_expires);
  1169. call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
  1170. }
  1171. }
  1172. static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
  1173. {
  1174. return NULL;
  1175. }
  1176. static void ipv4_dst_destroy(struct dst_entry *dst)
  1177. {
  1178. struct rtable *rt = (struct rtable *) dst;
  1179. struct inet_peer *peer = rt->peer;
  1180. struct in_device *idev = rt->idev;
  1181. if (peer) {
  1182. rt->peer = NULL;
  1183. inet_putpeer(peer);
  1184. }
  1185. if (idev) {
  1186. rt->idev = NULL;
  1187. in_dev_put(idev);
  1188. }
  1189. }
  1190. static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  1191. int how)
  1192. {
  1193. struct rtable *rt = (struct rtable *) dst;
  1194. struct in_device *idev = rt->idev;
  1195. if (dev != init_net.loopback_dev && idev && idev->dev == dev) {
  1196. struct in_device *loopback_idev = in_dev_get(init_net.loopback_dev);
  1197. if (loopback_idev) {
  1198. rt->idev = loopback_idev;
  1199. in_dev_put(idev);
  1200. }
  1201. }
  1202. }
  1203. static void ipv4_link_failure(struct sk_buff *skb)
  1204. {
  1205. struct rtable *rt;
  1206. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
  1207. rt = (struct rtable *) skb->dst;
  1208. if (rt)
  1209. dst_set_expires(&rt->u.dst, 0);
  1210. }
  1211. static int ip_rt_bug(struct sk_buff *skb)
  1212. {
  1213. printk(KERN_DEBUG "ip_rt_bug: %u.%u.%u.%u -> %u.%u.%u.%u, %s\n",
  1214. NIPQUAD(ip_hdr(skb)->saddr), NIPQUAD(ip_hdr(skb)->daddr),
  1215. skb->dev ? skb->dev->name : "?");
  1216. kfree_skb(skb);
  1217. return 0;
  1218. }
  1219. /*
  1220. We do not cache source address of outgoing interface,
  1221. because it is used only by IP RR, TS and SRR options,
  1222. so that it out of fast path.
  1223. BTW remember: "addr" is allowed to be not aligned
  1224. in IP options!
  1225. */
  1226. void ip_rt_get_source(u8 *addr, struct rtable *rt)
  1227. {
  1228. __be32 src;
  1229. struct fib_result res;
  1230. if (rt->fl.iif == 0)
  1231. src = rt->rt_src;
  1232. else if (fib_lookup(&rt->fl, &res) == 0) {
  1233. src = FIB_RES_PREFSRC(res);
  1234. fib_res_put(&res);
  1235. } else
  1236. src = inet_select_addr(rt->u.dst.dev, rt->rt_gateway,
  1237. RT_SCOPE_UNIVERSE);
  1238. memcpy(addr, &src, 4);
  1239. }
  1240. #ifdef CONFIG_NET_CLS_ROUTE
  1241. static void set_class_tag(struct rtable *rt, u32 tag)
  1242. {
  1243. if (!(rt->u.dst.tclassid & 0xFFFF))
  1244. rt->u.dst.tclassid |= tag & 0xFFFF;
  1245. if (!(rt->u.dst.tclassid & 0xFFFF0000))
  1246. rt->u.dst.tclassid |= tag & 0xFFFF0000;
  1247. }
  1248. #endif
  1249. static void rt_set_nexthop(struct rtable *rt, struct fib_result *res, u32 itag)
  1250. {
  1251. struct fib_info *fi = res->fi;
  1252. if (fi) {
  1253. if (FIB_RES_GW(*res) &&
  1254. FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
  1255. rt->rt_gateway = FIB_RES_GW(*res);
  1256. memcpy(rt->u.dst.metrics, fi->fib_metrics,
  1257. sizeof(rt->u.dst.metrics));
  1258. if (fi->fib_mtu == 0) {
  1259. rt->u.dst.metrics[RTAX_MTU-1] = rt->u.dst.dev->mtu;
  1260. if (rt->u.dst.metrics[RTAX_LOCK-1] & (1 << RTAX_MTU) &&
  1261. rt->rt_gateway != rt->rt_dst &&
  1262. rt->u.dst.dev->mtu > 576)
  1263. rt->u.dst.metrics[RTAX_MTU-1] = 576;
  1264. }
  1265. #ifdef CONFIG_NET_CLS_ROUTE
  1266. rt->u.dst.tclassid = FIB_RES_NH(*res).nh_tclassid;
  1267. #endif
  1268. } else
  1269. rt->u.dst.metrics[RTAX_MTU-1]= rt->u.dst.dev->mtu;
  1270. if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
  1271. rt->u.dst.metrics[RTAX_HOPLIMIT-1] = sysctl_ip_default_ttl;
  1272. if (rt->u.dst.metrics[RTAX_MTU-1] > IP_MAX_MTU)
  1273. rt->u.dst.metrics[RTAX_MTU-1] = IP_MAX_MTU;
  1274. if (rt->u.dst.metrics[RTAX_ADVMSS-1] == 0)
  1275. rt->u.dst.metrics[RTAX_ADVMSS-1] = max_t(unsigned int, rt->u.dst.dev->mtu - 40,
  1276. ip_rt_min_advmss);
  1277. if (rt->u.dst.metrics[RTAX_ADVMSS-1] > 65535 - 40)
  1278. rt->u.dst.metrics[RTAX_ADVMSS-1] = 65535 - 40;
  1279. #ifdef CONFIG_NET_CLS_ROUTE
  1280. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1281. set_class_tag(rt, fib_rules_tclass(res));
  1282. #endif
  1283. set_class_tag(rt, itag);
  1284. #endif
  1285. rt->rt_type = res->type;
  1286. }
  1287. static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1288. u8 tos, struct net_device *dev, int our)
  1289. {
  1290. unsigned hash;
  1291. struct rtable *rth;
  1292. __be32 spec_dst;
  1293. struct in_device *in_dev = in_dev_get(dev);
  1294. u32 itag = 0;
  1295. /* Primary sanity checks. */
  1296. if (in_dev == NULL)
  1297. return -EINVAL;
  1298. if (MULTICAST(saddr) || BADCLASS(saddr) || LOOPBACK(saddr) ||
  1299. skb->protocol != htons(ETH_P_IP))
  1300. goto e_inval;
  1301. if (ZERONET(saddr)) {
  1302. if (!LOCAL_MCAST(daddr))
  1303. goto e_inval;
  1304. spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  1305. } else if (fib_validate_source(saddr, 0, tos, 0,
  1306. dev, &spec_dst, &itag) < 0)
  1307. goto e_inval;
  1308. rth = dst_alloc(&ipv4_dst_ops);
  1309. if (!rth)
  1310. goto e_nobufs;
  1311. rth->u.dst.output= ip_rt_bug;
  1312. atomic_set(&rth->u.dst.__refcnt, 1);
  1313. rth->u.dst.flags= DST_HOST;
  1314. if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
  1315. rth->u.dst.flags |= DST_NOPOLICY;
  1316. rth->fl.fl4_dst = daddr;
  1317. rth->rt_dst = daddr;
  1318. rth->fl.fl4_tos = tos;
  1319. rth->fl.mark = skb->mark;
  1320. rth->fl.fl4_src = saddr;
  1321. rth->rt_src = saddr;
  1322. #ifdef CONFIG_NET_CLS_ROUTE
  1323. rth->u.dst.tclassid = itag;
  1324. #endif
  1325. rth->rt_iif =
  1326. rth->fl.iif = dev->ifindex;
  1327. rth->u.dst.dev = init_net.loopback_dev;
  1328. dev_hold(rth->u.dst.dev);
  1329. rth->idev = in_dev_get(rth->u.dst.dev);
  1330. rth->fl.oif = 0;
  1331. rth->rt_gateway = daddr;
  1332. rth->rt_spec_dst= spec_dst;
  1333. rth->rt_type = RTN_MULTICAST;
  1334. rth->rt_flags = RTCF_MULTICAST;
  1335. if (our) {
  1336. rth->u.dst.input= ip_local_deliver;
  1337. rth->rt_flags |= RTCF_LOCAL;
  1338. }
  1339. #ifdef CONFIG_IP_MROUTE
  1340. if (!LOCAL_MCAST(daddr) && IN_DEV_MFORWARD(in_dev))
  1341. rth->u.dst.input = ip_mr_input;
  1342. #endif
  1343. RT_CACHE_STAT_INC(in_slow_mc);
  1344. in_dev_put(in_dev);
  1345. hash = rt_hash(daddr, saddr, dev->ifindex);
  1346. return rt_intern_hash(hash, rth, (struct rtable**) &skb->dst);
  1347. e_nobufs:
  1348. in_dev_put(in_dev);
  1349. return -ENOBUFS;
  1350. e_inval:
  1351. in_dev_put(in_dev);
  1352. return -EINVAL;
  1353. }
  1354. static void ip_handle_martian_source(struct net_device *dev,
  1355. struct in_device *in_dev,
  1356. struct sk_buff *skb,
  1357. __be32 daddr,
  1358. __be32 saddr)
  1359. {
  1360. RT_CACHE_STAT_INC(in_martian_src);
  1361. #ifdef CONFIG_IP_ROUTE_VERBOSE
  1362. if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
  1363. /*
  1364. * RFC1812 recommendation, if source is martian,
  1365. * the only hint is MAC header.
  1366. */
  1367. printk(KERN_WARNING "martian source %u.%u.%u.%u from "
  1368. "%u.%u.%u.%u, on dev %s\n",
  1369. NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
  1370. if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
  1371. int i;
  1372. const unsigned char *p = skb_mac_header(skb);
  1373. printk(KERN_WARNING "ll header: ");
  1374. for (i = 0; i < dev->hard_header_len; i++, p++) {
  1375. printk("%02x", *p);
  1376. if (i < (dev->hard_header_len - 1))
  1377. printk(":");
  1378. }
  1379. printk("\n");
  1380. }
  1381. }
  1382. #endif
  1383. }
  1384. static inline int __mkroute_input(struct sk_buff *skb,
  1385. struct fib_result* res,
  1386. struct in_device *in_dev,
  1387. __be32 daddr, __be32 saddr, u32 tos,
  1388. struct rtable **result)
  1389. {
  1390. struct rtable *rth;
  1391. int err;
  1392. struct in_device *out_dev;
  1393. unsigned flags = 0;
  1394. __be32 spec_dst;
  1395. u32 itag;
  1396. /* get a working reference to the output device */
  1397. out_dev = in_dev_get(FIB_RES_DEV(*res));
  1398. if (out_dev == NULL) {
  1399. if (net_ratelimit())
  1400. printk(KERN_CRIT "Bug in ip_route_input" \
  1401. "_slow(). Please, report\n");
  1402. return -EINVAL;
  1403. }
  1404. err = fib_validate_source(saddr, daddr, tos, FIB_RES_OIF(*res),
  1405. in_dev->dev, &spec_dst, &itag);
  1406. if (err < 0) {
  1407. ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
  1408. saddr);
  1409. err = -EINVAL;
  1410. goto cleanup;
  1411. }
  1412. if (err)
  1413. flags |= RTCF_DIRECTSRC;
  1414. if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
  1415. (IN_DEV_SHARED_MEDIA(out_dev) ||
  1416. inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
  1417. flags |= RTCF_DOREDIRECT;
  1418. if (skb->protocol != htons(ETH_P_IP)) {
  1419. /* Not IP (i.e. ARP). Do not create route, if it is
  1420. * invalid for proxy arp. DNAT routes are always valid.
  1421. */
  1422. if (out_dev == in_dev && !(flags & RTCF_DNAT)) {
  1423. err = -EINVAL;
  1424. goto cleanup;
  1425. }
  1426. }
  1427. rth = dst_alloc(&ipv4_dst_ops);
  1428. if (!rth) {
  1429. err = -ENOBUFS;
  1430. goto cleanup;
  1431. }
  1432. atomic_set(&rth->u.dst.__refcnt, 1);
  1433. rth->u.dst.flags= DST_HOST;
  1434. if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
  1435. rth->u.dst.flags |= DST_NOPOLICY;
  1436. if (IN_DEV_CONF_GET(out_dev, NOXFRM))
  1437. rth->u.dst.flags |= DST_NOXFRM;
  1438. rth->fl.fl4_dst = daddr;
  1439. rth->rt_dst = daddr;
  1440. rth->fl.fl4_tos = tos;
  1441. rth->fl.mark = skb->mark;
  1442. rth->fl.fl4_src = saddr;
  1443. rth->rt_src = saddr;
  1444. rth->rt_gateway = daddr;
  1445. rth->rt_iif =
  1446. rth->fl.iif = in_dev->dev->ifindex;
  1447. rth->u.dst.dev = (out_dev)->dev;
  1448. dev_hold(rth->u.dst.dev);
  1449. rth->idev = in_dev_get(rth->u.dst.dev);
  1450. rth->fl.oif = 0;
  1451. rth->rt_spec_dst= spec_dst;
  1452. rth->u.dst.input = ip_forward;
  1453. rth->u.dst.output = ip_output;
  1454. rt_set_nexthop(rth, res, itag);
  1455. rth->rt_flags = flags;
  1456. *result = rth;
  1457. err = 0;
  1458. cleanup:
  1459. /* release the working reference to the output device */
  1460. in_dev_put(out_dev);
  1461. return err;
  1462. }
  1463. static inline int ip_mkroute_input(struct sk_buff *skb,
  1464. struct fib_result* res,
  1465. const struct flowi *fl,
  1466. struct in_device *in_dev,
  1467. __be32 daddr, __be32 saddr, u32 tos)
  1468. {
  1469. struct rtable* rth = NULL;
  1470. int err;
  1471. unsigned hash;
  1472. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1473. if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
  1474. fib_select_multipath(fl, res);
  1475. #endif
  1476. /* create a routing cache entry */
  1477. err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
  1478. if (err)
  1479. return err;
  1480. /* put it into the cache */
  1481. hash = rt_hash(daddr, saddr, fl->iif);
  1482. return rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
  1483. }
  1484. /*
  1485. * NOTE. We drop all the packets that has local source
  1486. * addresses, because every properly looped back packet
  1487. * must have correct destination already attached by output routine.
  1488. *
  1489. * Such approach solves two big problems:
  1490. * 1. Not simplex devices are handled properly.
  1491. * 2. IP spoofing attempts are filtered with 100% of guarantee.
  1492. */
  1493. static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1494. u8 tos, struct net_device *dev)
  1495. {
  1496. struct fib_result res;
  1497. struct in_device *in_dev = in_dev_get(dev);
  1498. struct flowi fl = { .nl_u = { .ip4_u =
  1499. { .daddr = daddr,
  1500. .saddr = saddr,
  1501. .tos = tos,
  1502. .scope = RT_SCOPE_UNIVERSE,
  1503. } },
  1504. .mark = skb->mark,
  1505. .iif = dev->ifindex };
  1506. unsigned flags = 0;
  1507. u32 itag = 0;
  1508. struct rtable * rth;
  1509. unsigned hash;
  1510. __be32 spec_dst;
  1511. int err = -EINVAL;
  1512. int free_res = 0;
  1513. /* IP on this device is disabled. */
  1514. if (!in_dev)
  1515. goto out;
  1516. /* Check for the most weird martians, which can be not detected
  1517. by fib_lookup.
  1518. */
  1519. if (MULTICAST(saddr) || BADCLASS(saddr) || LOOPBACK(saddr))
  1520. goto martian_source;
  1521. if (daddr == htonl(0xFFFFFFFF) || (saddr == 0 && daddr == 0))
  1522. goto brd_input;
  1523. /* Accept zero addresses only to limited broadcast;
  1524. * I even do not know to fix it or not. Waiting for complains :-)
  1525. */
  1526. if (ZERONET(saddr))
  1527. goto martian_source;
  1528. if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
  1529. goto martian_destination;
  1530. /*
  1531. * Now we are ready to route packet.
  1532. */
  1533. if ((err = fib_lookup(&fl, &res)) != 0) {
  1534. if (!IN_DEV_FORWARD(in_dev))
  1535. goto e_hostunreach;
  1536. goto no_route;
  1537. }
  1538. free_res = 1;
  1539. RT_CACHE_STAT_INC(in_slow_tot);
  1540. if (res.type == RTN_BROADCAST)
  1541. goto brd_input;
  1542. if (res.type == RTN_LOCAL) {
  1543. int result;
  1544. result = fib_validate_source(saddr, daddr, tos,
  1545. init_net.loopback_dev->ifindex,
  1546. dev, &spec_dst, &itag);
  1547. if (result < 0)
  1548. goto martian_source;
  1549. if (result)
  1550. flags |= RTCF_DIRECTSRC;
  1551. spec_dst = daddr;
  1552. goto local_input;
  1553. }
  1554. if (!IN_DEV_FORWARD(in_dev))
  1555. goto e_hostunreach;
  1556. if (res.type != RTN_UNICAST)
  1557. goto martian_destination;
  1558. err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
  1559. if (err == -ENOBUFS)
  1560. goto e_nobufs;
  1561. if (err == -EINVAL)
  1562. goto e_inval;
  1563. done:
  1564. in_dev_put(in_dev);
  1565. if (free_res)
  1566. fib_res_put(&res);
  1567. out: return err;
  1568. brd_input:
  1569. if (skb->protocol != htons(ETH_P_IP))
  1570. goto e_inval;
  1571. if (ZERONET(saddr))
  1572. spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  1573. else {
  1574. err = fib_validate_source(saddr, 0, tos, 0, dev, &spec_dst,
  1575. &itag);
  1576. if (err < 0)
  1577. goto martian_source;
  1578. if (err)
  1579. flags |= RTCF_DIRECTSRC;
  1580. }
  1581. flags |= RTCF_BROADCAST;
  1582. res.type = RTN_BROADCAST;
  1583. RT_CACHE_STAT_INC(in_brd);
  1584. local_input:
  1585. rth = dst_alloc(&ipv4_dst_ops);
  1586. if (!rth)
  1587. goto e_nobufs;
  1588. rth->u.dst.output= ip_rt_bug;
  1589. atomic_set(&rth->u.dst.__refcnt, 1);
  1590. rth->u.dst.flags= DST_HOST;
  1591. if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
  1592. rth->u.dst.flags |= DST_NOPOLICY;
  1593. rth->fl.fl4_dst = daddr;
  1594. rth->rt_dst = daddr;
  1595. rth->fl.fl4_tos = tos;
  1596. rth->fl.mark = skb->mark;
  1597. rth->fl.fl4_src = saddr;
  1598. rth->rt_src = saddr;
  1599. #ifdef CONFIG_NET_CLS_ROUTE
  1600. rth->u.dst.tclassid = itag;
  1601. #endif
  1602. rth->rt_iif =
  1603. rth->fl.iif = dev->ifindex;
  1604. rth->u.dst.dev = init_net.loopback_dev;
  1605. dev_hold(rth->u.dst.dev);
  1606. rth->idev = in_dev_get(rth->u.dst.dev);
  1607. rth->rt_gateway = daddr;
  1608. rth->rt_spec_dst= spec_dst;
  1609. rth->u.dst.input= ip_local_deliver;
  1610. rth->rt_flags = flags|RTCF_LOCAL;
  1611. if (res.type == RTN_UNREACHABLE) {
  1612. rth->u.dst.input= ip_error;
  1613. rth->u.dst.error= -err;
  1614. rth->rt_flags &= ~RTCF_LOCAL;
  1615. }
  1616. rth->rt_type = res.type;
  1617. hash = rt_hash(daddr, saddr, fl.iif);
  1618. err = rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
  1619. goto done;
  1620. no_route:
  1621. RT_CACHE_STAT_INC(in_no_route);
  1622. spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
  1623. res.type = RTN_UNREACHABLE;
  1624. goto local_input;
  1625. /*
  1626. * Do not cache martian addresses: they should be logged (RFC1812)
  1627. */
  1628. martian_destination:
  1629. RT_CACHE_STAT_INC(in_martian_dst);
  1630. #ifdef CONFIG_IP_ROUTE_VERBOSE
  1631. if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
  1632. printk(KERN_WARNING "martian destination %u.%u.%u.%u from "
  1633. "%u.%u.%u.%u, dev %s\n",
  1634. NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
  1635. #endif
  1636. e_hostunreach:
  1637. err = -EHOSTUNREACH;
  1638. goto done;
  1639. e_inval:
  1640. err = -EINVAL;
  1641. goto done;
  1642. e_nobufs:
  1643. err = -ENOBUFS;
  1644. goto done;
  1645. martian_source:
  1646. ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
  1647. goto e_inval;
  1648. }
  1649. int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  1650. u8 tos, struct net_device *dev)
  1651. {
  1652. struct rtable * rth;
  1653. unsigned hash;
  1654. int iif = dev->ifindex;
  1655. tos &= IPTOS_RT_MASK;
  1656. hash = rt_hash(daddr, saddr, iif);
  1657. rcu_read_lock();
  1658. for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
  1659. rth = rcu_dereference(rth->u.dst.rt_next)) {
  1660. if (rth->fl.fl4_dst == daddr &&
  1661. rth->fl.fl4_src == saddr &&
  1662. rth->fl.iif == iif &&
  1663. rth->fl.oif == 0 &&
  1664. rth->fl.mark == skb->mark &&
  1665. rth->fl.fl4_tos == tos) {
  1666. rth->u.dst.lastuse = jiffies;
  1667. dst_hold(&rth->u.dst);
  1668. rth->u.dst.__use++;
  1669. RT_CACHE_STAT_INC(in_hit);
  1670. rcu_read_unlock();
  1671. skb->dst = (struct dst_entry*)rth;
  1672. return 0;
  1673. }
  1674. RT_CACHE_STAT_INC(in_hlist_search);
  1675. }
  1676. rcu_read_unlock();
  1677. /* Multicast recognition logic is moved from route cache to here.
  1678. The problem was that too many Ethernet cards have broken/missing
  1679. hardware multicast filters :-( As result the host on multicasting
  1680. network acquires a lot of useless route cache entries, sort of
  1681. SDR messages from all the world. Now we try to get rid of them.
  1682. Really, provided software IP multicast filter is organized
  1683. reasonably (at least, hashed), it does not result in a slowdown
  1684. comparing with route cache reject entries.
  1685. Note, that multicast routers are not affected, because
  1686. route cache entry is created eventually.
  1687. */
  1688. if (MULTICAST(daddr)) {
  1689. struct in_device *in_dev;
  1690. rcu_read_lock();
  1691. if ((in_dev = __in_dev_get_rcu(dev)) != NULL) {
  1692. int our = ip_check_mc(in_dev, daddr, saddr,
  1693. ip_hdr(skb)->protocol);
  1694. if (our
  1695. #ifdef CONFIG_IP_MROUTE
  1696. || (!LOCAL_MCAST(daddr) && IN_DEV_MFORWARD(in_dev))
  1697. #endif
  1698. ) {
  1699. rcu_read_unlock();
  1700. return ip_route_input_mc(skb, daddr, saddr,
  1701. tos, dev, our);
  1702. }
  1703. }
  1704. rcu_read_unlock();
  1705. return -EINVAL;
  1706. }
  1707. return ip_route_input_slow(skb, daddr, saddr, tos, dev);
  1708. }
  1709. static inline int __mkroute_output(struct rtable **result,
  1710. struct fib_result* res,
  1711. const struct flowi *fl,
  1712. const struct flowi *oldflp,
  1713. struct net_device *dev_out,
  1714. unsigned flags)
  1715. {
  1716. struct rtable *rth;
  1717. struct in_device *in_dev;
  1718. u32 tos = RT_FL_TOS(oldflp);
  1719. int err = 0;
  1720. if (LOOPBACK(fl->fl4_src) && !(dev_out->flags&IFF_LOOPBACK))
  1721. return -EINVAL;
  1722. if (fl->fl4_dst == htonl(0xFFFFFFFF))
  1723. res->type = RTN_BROADCAST;
  1724. else if (MULTICAST(fl->fl4_dst))
  1725. res->type = RTN_MULTICAST;
  1726. else if (BADCLASS(fl->fl4_dst) || ZERONET(fl->fl4_dst))
  1727. return -EINVAL;
  1728. if (dev_out->flags & IFF_LOOPBACK)
  1729. flags |= RTCF_LOCAL;
  1730. /* get work reference to inet device */
  1731. in_dev = in_dev_get(dev_out);
  1732. if (!in_dev)
  1733. return -EINVAL;
  1734. if (res->type == RTN_BROADCAST) {
  1735. flags |= RTCF_BROADCAST | RTCF_LOCAL;
  1736. if (res->fi) {
  1737. fib_info_put(res->fi);
  1738. res->fi = NULL;
  1739. }
  1740. } else if (res->type == RTN_MULTICAST) {
  1741. flags |= RTCF_MULTICAST|RTCF_LOCAL;
  1742. if (!ip_check_mc(in_dev, oldflp->fl4_dst, oldflp->fl4_src,
  1743. oldflp->proto))
  1744. flags &= ~RTCF_LOCAL;
  1745. /* If multicast route do not exist use
  1746. default one, but do not gateway in this case.
  1747. Yes, it is hack.
  1748. */
  1749. if (res->fi && res->prefixlen < 4) {
  1750. fib_info_put(res->fi);
  1751. res->fi = NULL;
  1752. }
  1753. }
  1754. rth = dst_alloc(&ipv4_dst_ops);
  1755. if (!rth) {
  1756. err = -ENOBUFS;
  1757. goto cleanup;
  1758. }
  1759. atomic_set(&rth->u.dst.__refcnt, 1);
  1760. rth->u.dst.flags= DST_HOST;
  1761. if (IN_DEV_CONF_GET(in_dev, NOXFRM))
  1762. rth->u.dst.flags |= DST_NOXFRM;
  1763. if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
  1764. rth->u.dst.flags |= DST_NOPOLICY;
  1765. rth->fl.fl4_dst = oldflp->fl4_dst;
  1766. rth->fl.fl4_tos = tos;
  1767. rth->fl.fl4_src = oldflp->fl4_src;
  1768. rth->fl.oif = oldflp->oif;
  1769. rth->fl.mark = oldflp->mark;
  1770. rth->rt_dst = fl->fl4_dst;
  1771. rth->rt_src = fl->fl4_src;
  1772. rth->rt_iif = oldflp->oif ? : dev_out->ifindex;
  1773. /* get references to the devices that are to be hold by the routing
  1774. cache entry */
  1775. rth->u.dst.dev = dev_out;
  1776. dev_hold(dev_out);
  1777. rth->idev = in_dev_get(dev_out);
  1778. rth->rt_gateway = fl->fl4_dst;
  1779. rth->rt_spec_dst= fl->fl4_src;
  1780. rth->u.dst.output=ip_output;
  1781. RT_CACHE_STAT_INC(out_slow_tot);
  1782. if (flags & RTCF_LOCAL) {
  1783. rth->u.dst.input = ip_local_deliver;
  1784. rth->rt_spec_dst = fl->fl4_dst;
  1785. }
  1786. if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  1787. rth->rt_spec_dst = fl->fl4_src;
  1788. if (flags & RTCF_LOCAL &&
  1789. !(dev_out->flags & IFF_LOOPBACK)) {
  1790. rth->u.dst.output = ip_mc_output;
  1791. RT_CACHE_STAT_INC(out_slow_mc);
  1792. }
  1793. #ifdef CONFIG_IP_MROUTE
  1794. if (res->type == RTN_MULTICAST) {
  1795. if (IN_DEV_MFORWARD(in_dev) &&
  1796. !LOCAL_MCAST(oldflp->fl4_dst)) {
  1797. rth->u.dst.input = ip_mr_input;
  1798. rth->u.dst.output = ip_mc_output;
  1799. }
  1800. }
  1801. #endif
  1802. }
  1803. rt_set_nexthop(rth, res, 0);
  1804. rth->rt_flags = flags;
  1805. *result = rth;
  1806. cleanup:
  1807. /* release work reference to inet device */
  1808. in_dev_put(in_dev);
  1809. return err;
  1810. }
  1811. static inline int ip_mkroute_output(struct rtable **rp,
  1812. struct fib_result* res,
  1813. const struct flowi *fl,
  1814. const struct flowi *oldflp,
  1815. struct net_device *dev_out,
  1816. unsigned flags)
  1817. {
  1818. struct rtable *rth = NULL;
  1819. int err = __mkroute_output(&rth, res, fl, oldflp, dev_out, flags);
  1820. unsigned hash;
  1821. if (err == 0) {
  1822. hash = rt_hash(oldflp->fl4_dst, oldflp->fl4_src, oldflp->oif);
  1823. err = rt_intern_hash(hash, rth, rp);
  1824. }
  1825. return err;
  1826. }
  1827. /*
  1828. * Major route resolver routine.
  1829. */
  1830. static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
  1831. {
  1832. u32 tos = RT_FL_TOS(oldflp);
  1833. struct flowi fl = { .nl_u = { .ip4_u =
  1834. { .daddr = oldflp->fl4_dst,
  1835. .saddr = oldflp->fl4_src,
  1836. .tos = tos & IPTOS_RT_MASK,
  1837. .scope = ((tos & RTO_ONLINK) ?
  1838. RT_SCOPE_LINK :
  1839. RT_SCOPE_UNIVERSE),
  1840. } },
  1841. .mark = oldflp->mark,
  1842. .iif = init_net.loopback_dev->ifindex,
  1843. .oif = oldflp->oif };
  1844. struct fib_result res;
  1845. unsigned flags = 0;
  1846. struct net_device *dev_out = NULL;
  1847. int free_res = 0;
  1848. int err;
  1849. res.fi = NULL;
  1850. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1851. res.r = NULL;
  1852. #endif
  1853. if (oldflp->fl4_src) {
  1854. err = -EINVAL;
  1855. if (MULTICAST(oldflp->fl4_src) ||
  1856. BADCLASS(oldflp->fl4_src) ||
  1857. ZERONET(oldflp->fl4_src))
  1858. goto out;
  1859. /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
  1860. dev_out = ip_dev_find(oldflp->fl4_src);
  1861. if (dev_out == NULL)
  1862. goto out;
  1863. /* I removed check for oif == dev_out->oif here.
  1864. It was wrong for two reasons:
  1865. 1. ip_dev_find(saddr) can return wrong iface, if saddr is
  1866. assigned to multiple interfaces.
  1867. 2. Moreover, we are allowed to send packets with saddr
  1868. of another iface. --ANK
  1869. */
  1870. if (oldflp->oif == 0
  1871. && (MULTICAST(oldflp->fl4_dst) || oldflp->fl4_dst == htonl(0xFFFFFFFF))) {
  1872. /* Special hack: user can direct multicasts
  1873. and limited broadcast via necessary interface
  1874. without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
  1875. This hack is not just for fun, it allows
  1876. vic,vat and friends to work.
  1877. They bind socket to loopback, set ttl to zero
  1878. and expect that it will work.
  1879. From the viewpoint of routing cache they are broken,
  1880. because we are not allowed to build multicast path
  1881. with loopback source addr (look, routing cache
  1882. cannot know, that ttl is zero, so that packet
  1883. will not leave this host and route is valid).
  1884. Luckily, this hack is good workaround.
  1885. */
  1886. fl.oif = dev_out->ifindex;
  1887. goto make_route;
  1888. }
  1889. if (dev_out)
  1890. dev_put(dev_out);
  1891. dev_out = NULL;
  1892. }
  1893. if (oldflp->oif) {
  1894. dev_out = dev_get_by_index(&init_net, oldflp->oif);
  1895. err = -ENODEV;
  1896. if (dev_out == NULL)
  1897. goto out;
  1898. /* RACE: Check return value of inet_select_addr instead. */
  1899. if (__in_dev_get_rtnl(dev_out) == NULL) {
  1900. dev_put(dev_out);
  1901. goto out; /* Wrong error code */
  1902. }
  1903. if (LOCAL_MCAST(oldflp->fl4_dst) || oldflp->fl4_dst == htonl(0xFFFFFFFF)) {
  1904. if (!fl.fl4_src)
  1905. fl.fl4_src = inet_select_addr(dev_out, 0,
  1906. RT_SCOPE_LINK);
  1907. goto make_route;
  1908. }
  1909. if (!fl.fl4_src) {
  1910. if (MULTICAST(oldflp->fl4_dst))
  1911. fl.fl4_src = inet_select_addr(dev_out, 0,
  1912. fl.fl4_scope);
  1913. else if (!oldflp->fl4_dst)
  1914. fl.fl4_src = inet_select_addr(dev_out, 0,
  1915. RT_SCOPE_HOST);
  1916. }
  1917. }
  1918. if (!fl.fl4_dst) {
  1919. fl.fl4_dst = fl.fl4_src;
  1920. if (!fl.fl4_dst)
  1921. fl.fl4_dst = fl.fl4_src = htonl(INADDR_LOOPBACK);
  1922. if (dev_out)
  1923. dev_put(dev_out);
  1924. dev_out = init_net.loopback_dev;
  1925. dev_hold(dev_out);
  1926. fl.oif = init_net.loopback_dev->ifindex;
  1927. res.type = RTN_LOCAL;
  1928. flags |= RTCF_LOCAL;
  1929. goto make_route;
  1930. }
  1931. if (fib_lookup(&fl, &res)) {
  1932. res.fi = NULL;
  1933. if (oldflp->oif) {
  1934. /* Apparently, routing tables are wrong. Assume,
  1935. that the destination is on link.
  1936. WHY? DW.
  1937. Because we are allowed to send to iface
  1938. even if it has NO routes and NO assigned
  1939. addresses. When oif is specified, routing
  1940. tables are looked up with only one purpose:
  1941. to catch if destination is gatewayed, rather than
  1942. direct. Moreover, if MSG_DONTROUTE is set,
  1943. we send packet, ignoring both routing tables
  1944. and ifaddr state. --ANK
  1945. We could make it even if oif is unknown,
  1946. likely IPv6, but we do not.
  1947. */
  1948. if (fl.fl4_src == 0)
  1949. fl.fl4_src = inet_select_addr(dev_out, 0,
  1950. RT_SCOPE_LINK);
  1951. res.type = RTN_UNICAST;
  1952. goto make_route;
  1953. }
  1954. if (dev_out)
  1955. dev_put(dev_out);
  1956. err = -ENETUNREACH;
  1957. goto out;
  1958. }
  1959. free_res = 1;
  1960. if (res.type == RTN_LOCAL) {
  1961. if (!fl.fl4_src)
  1962. fl.fl4_src = fl.fl4_dst;
  1963. if (dev_out)
  1964. dev_put(dev_out);
  1965. dev_out = init_net.loopback_dev;
  1966. dev_hold(dev_out);
  1967. fl.oif = dev_out->ifindex;
  1968. if (res.fi)
  1969. fib_info_put(res.fi);
  1970. res.fi = NULL;
  1971. flags |= RTCF_LOCAL;
  1972. goto make_route;
  1973. }
  1974. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1975. if (res.fi->fib_nhs > 1 && fl.oif == 0)
  1976. fib_select_multipath(&fl, &res);
  1977. else
  1978. #endif
  1979. if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
  1980. fib_select_default(&fl, &res);
  1981. if (!fl.fl4_src)
  1982. fl.fl4_src = FIB_RES_PREFSRC(res);
  1983. if (dev_out)
  1984. dev_put(dev_out);
  1985. dev_out = FIB_RES_DEV(res);
  1986. dev_hold(dev_out);
  1987. fl.oif = dev_out->ifindex;
  1988. make_route:
  1989. err = ip_mkroute_output(rp, &res, &fl, oldflp, dev_out, flags);
  1990. if (free_res)
  1991. fib_res_put(&res);
  1992. if (dev_out)
  1993. dev_put(dev_out);
  1994. out: return err;
  1995. }
  1996. int __ip_route_output_key(struct rtable **rp, const struct flowi *flp)
  1997. {
  1998. unsigned hash;
  1999. struct rtable *rth;
  2000. hash = rt_hash(flp->fl4_dst, flp->fl4_src, flp->oif);
  2001. rcu_read_lock_bh();
  2002. for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
  2003. rth = rcu_dereference(rth->u.dst.rt_next)) {
  2004. if (rth->fl.fl4_dst == flp->fl4_dst &&
  2005. rth->fl.fl4_src == flp->fl4_src &&
  2006. rth->fl.iif == 0 &&
  2007. rth->fl.oif == flp->oif &&
  2008. rth->fl.mark == flp->mark &&
  2009. !((rth->fl.fl4_tos ^ flp->fl4_tos) &
  2010. (IPTOS_RT_MASK | RTO_ONLINK))) {
  2011. rth->u.dst.lastuse = jiffies;
  2012. dst_hold(&rth->u.dst);
  2013. rth->u.dst.__use++;
  2014. RT_CACHE_STAT_INC(out_hit);
  2015. rcu_read_unlock_bh();
  2016. *rp = rth;
  2017. return 0;
  2018. }
  2019. RT_CACHE_STAT_INC(out_hlist_search);
  2020. }
  2021. rcu_read_unlock_bh();
  2022. return ip_route_output_slow(rp, flp);
  2023. }
  2024. EXPORT_SYMBOL_GPL(__ip_route_output_key);
  2025. static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
  2026. {
  2027. }
  2028. static struct dst_ops ipv4_dst_blackhole_ops = {
  2029. .family = AF_INET,
  2030. .protocol = __constant_htons(ETH_P_IP),
  2031. .destroy = ipv4_dst_destroy,
  2032. .check = ipv4_dst_check,
  2033. .update_pmtu = ipv4_rt_blackhole_update_pmtu,
  2034. .entry_size = sizeof(struct rtable),
  2035. };
  2036. static int ipv4_blackhole_output(struct sk_buff *skb)
  2037. {
  2038. kfree_skb(skb);
  2039. return 0;
  2040. }
  2041. static int ipv4_dst_blackhole(struct rtable **rp, struct flowi *flp, struct sock *sk)
  2042. {
  2043. struct rtable *ort = *rp;
  2044. struct rtable *rt = (struct rtable *)
  2045. dst_alloc(&ipv4_dst_blackhole_ops);
  2046. if (rt) {
  2047. struct dst_entry *new = &rt->u.dst;
  2048. atomic_set(&new->__refcnt, 1);
  2049. new->__use = 1;
  2050. new->input = ipv4_blackhole_output;
  2051. new->output = ipv4_blackhole_output;
  2052. memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
  2053. new->dev = ort->u.dst.dev;
  2054. if (new->dev)
  2055. dev_hold(new->dev);
  2056. rt->fl = ort->fl;
  2057. rt->idev = ort->idev;
  2058. if (rt->idev)
  2059. in_dev_hold(rt->idev);
  2060. rt->rt_flags = ort->rt_flags;
  2061. rt->rt_type = ort->rt_type;
  2062. rt->rt_dst = ort->rt_dst;
  2063. rt->rt_src = ort->rt_src;
  2064. rt->rt_iif = ort->rt_iif;
  2065. rt->rt_gateway = ort->rt_gateway;
  2066. rt->rt_spec_dst = ort->rt_spec_dst;
  2067. rt->peer = ort->peer;
  2068. if (rt->peer)
  2069. atomic_inc(&rt->peer->refcnt);
  2070. dst_free(new);
  2071. }
  2072. dst_release(&(*rp)->u.dst);
  2073. *rp = rt;
  2074. return (rt ? 0 : -ENOMEM);
  2075. }
  2076. int ip_route_output_flow(struct rtable **rp, struct flowi *flp, struct sock *sk, int flags)
  2077. {
  2078. int err;
  2079. if ((err = __ip_route_output_key(rp, flp)) != 0)
  2080. return err;
  2081. if (flp->proto) {
  2082. if (!flp->fl4_src)
  2083. flp->fl4_src = (*rp)->rt_src;
  2084. if (!flp->fl4_dst)
  2085. flp->fl4_dst = (*rp)->rt_dst;
  2086. err = __xfrm_lookup((struct dst_entry **)rp, flp, sk, flags);
  2087. if (err == -EREMOTE)
  2088. err = ipv4_dst_blackhole(rp, flp, sk);
  2089. return err;
  2090. }
  2091. return 0;
  2092. }
  2093. EXPORT_SYMBOL_GPL(ip_route_output_flow);
  2094. int ip_route_output_key(struct rtable **rp, struct flowi *flp)
  2095. {
  2096. return ip_route_output_flow(rp, flp, NULL, 0);
  2097. }
  2098. static int rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
  2099. int nowait, unsigned int flags)
  2100. {
  2101. struct rtable *rt = (struct rtable*)skb->dst;
  2102. struct rtmsg *r;
  2103. struct nlmsghdr *nlh;
  2104. long expires;
  2105. u32 id = 0, ts = 0, tsage = 0, error;
  2106. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
  2107. if (nlh == NULL)
  2108. return -EMSGSIZE;
  2109. r = nlmsg_data(nlh);
  2110. r->rtm_family = AF_INET;
  2111. r->rtm_dst_len = 32;
  2112. r->rtm_src_len = 0;
  2113. r->rtm_tos = rt->fl.fl4_tos;
  2114. r->rtm_table = RT_TABLE_MAIN;
  2115. NLA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
  2116. r->rtm_type = rt->rt_type;
  2117. r->rtm_scope = RT_SCOPE_UNIVERSE;
  2118. r->rtm_protocol = RTPROT_UNSPEC;
  2119. r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
  2120. if (rt->rt_flags & RTCF_NOTIFY)
  2121. r->rtm_flags |= RTM_F_NOTIFY;
  2122. NLA_PUT_BE32(skb, RTA_DST, rt->rt_dst);
  2123. if (rt->fl.fl4_src) {
  2124. r->rtm_src_len = 32;
  2125. NLA_PUT_BE32(skb, RTA_SRC, rt->fl.fl4_src);
  2126. }
  2127. if (rt->u.dst.dev)
  2128. NLA_PUT_U32(skb, RTA_OIF, rt->u.dst.dev->ifindex);
  2129. #ifdef CONFIG_NET_CLS_ROUTE
  2130. if (rt->u.dst.tclassid)
  2131. NLA_PUT_U32(skb, RTA_FLOW, rt->u.dst.tclassid);
  2132. #endif
  2133. if (rt->fl.iif)
  2134. NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_spec_dst);
  2135. else if (rt->rt_src != rt->fl.fl4_src)
  2136. NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_src);
  2137. if (rt->rt_dst != rt->rt_gateway)
  2138. NLA_PUT_BE32(skb, RTA_GATEWAY, rt->rt_gateway);
  2139. if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
  2140. goto nla_put_failure;
  2141. error = rt->u.dst.error;
  2142. expires = rt->u.dst.expires ? rt->u.dst.expires - jiffies : 0;
  2143. if (rt->peer) {
  2144. id = rt->peer->ip_id_count;
  2145. if (rt->peer->tcp_ts_stamp) {
  2146. ts = rt->peer->tcp_ts;
  2147. tsage = get_seconds() - rt->peer->tcp_ts_stamp;
  2148. }
  2149. }
  2150. if (rt->fl.iif) {
  2151. #ifdef CONFIG_IP_MROUTE
  2152. __be32 dst = rt->rt_dst;
  2153. if (MULTICAST(dst) && !LOCAL_MCAST(dst) &&
  2154. IPV4_DEVCONF_ALL(MC_FORWARDING)) {
  2155. int err = ipmr_get_route(skb, r, nowait);
  2156. if (err <= 0) {
  2157. if (!nowait) {
  2158. if (err == 0)
  2159. return 0;
  2160. goto nla_put_failure;
  2161. } else {
  2162. if (err == -EMSGSIZE)
  2163. goto nla_put_failure;
  2164. error = err;
  2165. }
  2166. }
  2167. } else
  2168. #endif
  2169. NLA_PUT_U32(skb, RTA_IIF, rt->fl.iif);
  2170. }
  2171. if (rtnl_put_cacheinfo(skb, &rt->u.dst, id, ts, tsage,
  2172. expires, error) < 0)
  2173. goto nla_put_failure;
  2174. return nlmsg_end(skb, nlh);
  2175. nla_put_failure:
  2176. nlmsg_cancel(skb, nlh);
  2177. return -EMSGSIZE;
  2178. }
  2179. static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
  2180. {
  2181. struct rtmsg *rtm;
  2182. struct nlattr *tb[RTA_MAX+1];
  2183. struct rtable *rt = NULL;
  2184. __be32 dst = 0;
  2185. __be32 src = 0;
  2186. u32 iif;
  2187. int err;
  2188. struct sk_buff *skb;
  2189. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
  2190. if (err < 0)
  2191. goto errout;
  2192. rtm = nlmsg_data(nlh);
  2193. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2194. if (skb == NULL) {
  2195. err = -ENOBUFS;
  2196. goto errout;
  2197. }
  2198. /* Reserve room for dummy headers, this skb can pass
  2199. through good chunk of routing engine.
  2200. */
  2201. skb_reset_mac_header(skb);
  2202. skb_reset_network_header(skb);
  2203. /* Bugfix: need to give ip_route_input enough of an IP header to not gag. */
  2204. ip_hdr(skb)->protocol = IPPROTO_ICMP;
  2205. skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
  2206. src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0;
  2207. dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0;
  2208. iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
  2209. if (iif) {
  2210. struct net_device *dev;
  2211. dev = __dev_get_by_index(&init_net, iif);
  2212. if (dev == NULL) {
  2213. err = -ENODEV;
  2214. goto errout_free;
  2215. }
  2216. skb->protocol = htons(ETH_P_IP);
  2217. skb->dev = dev;
  2218. local_bh_disable();
  2219. err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
  2220. local_bh_enable();
  2221. rt = (struct rtable*) skb->dst;
  2222. if (err == 0 && rt->u.dst.error)
  2223. err = -rt->u.dst.error;
  2224. } else {
  2225. struct flowi fl = {
  2226. .nl_u = {
  2227. .ip4_u = {
  2228. .daddr = dst,
  2229. .saddr = src,
  2230. .tos = rtm->rtm_tos,
  2231. },
  2232. },
  2233. .oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0,
  2234. };
  2235. err = ip_route_output_key(&rt, &fl);
  2236. }
  2237. if (err)
  2238. goto errout_free;
  2239. skb->dst = &rt->u.dst;
  2240. if (rtm->rtm_flags & RTM_F_NOTIFY)
  2241. rt->rt_flags |= RTCF_NOTIFY;
  2242. err = rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
  2243. RTM_NEWROUTE, 0, 0);
  2244. if (err <= 0)
  2245. goto errout_free;
  2246. err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
  2247. errout:
  2248. return err;
  2249. errout_free:
  2250. kfree_skb(skb);
  2251. goto errout;
  2252. }
  2253. int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2254. {
  2255. struct rtable *rt;
  2256. int h, s_h;
  2257. int idx, s_idx;
  2258. s_h = cb->args[0];
  2259. s_idx = idx = cb->args[1];
  2260. for (h = 0; h <= rt_hash_mask; h++) {
  2261. if (h < s_h) continue;
  2262. if (h > s_h)
  2263. s_idx = 0;
  2264. rcu_read_lock_bh();
  2265. for (rt = rcu_dereference(rt_hash_table[h].chain), idx = 0; rt;
  2266. rt = rcu_dereference(rt->u.dst.rt_next), idx++) {
  2267. if (idx < s_idx)
  2268. continue;
  2269. skb->dst = dst_clone(&rt->u.dst);
  2270. if (rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
  2271. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  2272. 1, NLM_F_MULTI) <= 0) {
  2273. dst_release(xchg(&skb->dst, NULL));
  2274. rcu_read_unlock_bh();
  2275. goto done;
  2276. }
  2277. dst_release(xchg(&skb->dst, NULL));
  2278. }
  2279. rcu_read_unlock_bh();
  2280. }
  2281. done:
  2282. cb->args[0] = h;
  2283. cb->args[1] = idx;
  2284. return skb->len;
  2285. }
  2286. void ip_rt_multicast_event(struct in_device *in_dev)
  2287. {
  2288. rt_cache_flush(0);
  2289. }
  2290. #ifdef CONFIG_SYSCTL
  2291. static int flush_delay;
  2292. static int ipv4_sysctl_rtcache_flush(ctl_table *ctl, int write,
  2293. struct file *filp, void __user *buffer,
  2294. size_t *lenp, loff_t *ppos)
  2295. {
  2296. if (write) {
  2297. proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  2298. rt_cache_flush(flush_delay);
  2299. return 0;
  2300. }
  2301. return -EINVAL;
  2302. }
  2303. static int ipv4_sysctl_rtcache_flush_strategy(ctl_table *table,
  2304. int __user *name,
  2305. int nlen,
  2306. void __user *oldval,
  2307. size_t __user *oldlenp,
  2308. void __user *newval,
  2309. size_t newlen)
  2310. {
  2311. int delay;
  2312. if (newlen != sizeof(int))
  2313. return -EINVAL;
  2314. if (get_user(delay, (int __user *)newval))
  2315. return -EFAULT;
  2316. rt_cache_flush(delay);
  2317. return 0;
  2318. }
  2319. ctl_table ipv4_route_table[] = {
  2320. {
  2321. .ctl_name = NET_IPV4_ROUTE_FLUSH,
  2322. .procname = "flush",
  2323. .data = &flush_delay,
  2324. .maxlen = sizeof(int),
  2325. .mode = 0200,
  2326. .proc_handler = &ipv4_sysctl_rtcache_flush,
  2327. .strategy = &ipv4_sysctl_rtcache_flush_strategy,
  2328. },
  2329. {
  2330. .ctl_name = NET_IPV4_ROUTE_MIN_DELAY,
  2331. .procname = "min_delay",
  2332. .data = &ip_rt_min_delay,
  2333. .maxlen = sizeof(int),
  2334. .mode = 0644,
  2335. .proc_handler = &proc_dointvec_jiffies,
  2336. .strategy = &sysctl_jiffies,
  2337. },
  2338. {
  2339. .ctl_name = NET_IPV4_ROUTE_MAX_DELAY,
  2340. .procname = "max_delay",
  2341. .data = &ip_rt_max_delay,
  2342. .maxlen = sizeof(int),
  2343. .mode = 0644,
  2344. .proc_handler = &proc_dointvec_jiffies,
  2345. .strategy = &sysctl_jiffies,
  2346. },
  2347. {
  2348. .ctl_name = NET_IPV4_ROUTE_GC_THRESH,
  2349. .procname = "gc_thresh",
  2350. .data = &ipv4_dst_ops.gc_thresh,
  2351. .maxlen = sizeof(int),
  2352. .mode = 0644,
  2353. .proc_handler = &proc_dointvec,
  2354. },
  2355. {
  2356. .ctl_name = NET_IPV4_ROUTE_MAX_SIZE,
  2357. .procname = "max_size",
  2358. .data = &ip_rt_max_size,
  2359. .maxlen = sizeof(int),
  2360. .mode = 0644,
  2361. .proc_handler = &proc_dointvec,
  2362. },
  2363. {
  2364. /* Deprecated. Use gc_min_interval_ms */
  2365. .ctl_name = NET_IPV4_ROUTE_GC_MIN_INTERVAL,
  2366. .procname = "gc_min_interval",
  2367. .data = &ip_rt_gc_min_interval,
  2368. .maxlen = sizeof(int),
  2369. .mode = 0644,
  2370. .proc_handler = &proc_dointvec_jiffies,
  2371. .strategy = &sysctl_jiffies,
  2372. },
  2373. {
  2374. .ctl_name = NET_IPV4_ROUTE_GC_MIN_INTERVAL_MS,
  2375. .procname = "gc_min_interval_ms",
  2376. .data = &ip_rt_gc_min_interval,
  2377. .maxlen = sizeof(int),
  2378. .mode = 0644,
  2379. .proc_handler = &proc_dointvec_ms_jiffies,
  2380. .strategy = &sysctl_ms_jiffies,
  2381. },
  2382. {
  2383. .ctl_name = NET_IPV4_ROUTE_GC_TIMEOUT,
  2384. .procname = "gc_timeout",
  2385. .data = &ip_rt_gc_timeout,
  2386. .maxlen = sizeof(int),
  2387. .mode = 0644,
  2388. .proc_handler = &proc_dointvec_jiffies,
  2389. .strategy = &sysctl_jiffies,
  2390. },
  2391. {
  2392. .ctl_name = NET_IPV4_ROUTE_GC_INTERVAL,
  2393. .procname = "gc_interval",
  2394. .data = &ip_rt_gc_interval,
  2395. .maxlen = sizeof(int),
  2396. .mode = 0644,
  2397. .proc_handler = &proc_dointvec_jiffies,
  2398. .strategy = &sysctl_jiffies,
  2399. },
  2400. {
  2401. .ctl_name = NET_IPV4_ROUTE_REDIRECT_LOAD,
  2402. .procname = "redirect_load",
  2403. .data = &ip_rt_redirect_load,
  2404. .maxlen = sizeof(int),
  2405. .mode = 0644,
  2406. .proc_handler = &proc_dointvec,
  2407. },
  2408. {
  2409. .ctl_name = NET_IPV4_ROUTE_REDIRECT_NUMBER,
  2410. .procname = "redirect_number",
  2411. .data = &ip_rt_redirect_number,
  2412. .maxlen = sizeof(int),
  2413. .mode = 0644,
  2414. .proc_handler = &proc_dointvec,
  2415. },
  2416. {
  2417. .ctl_name = NET_IPV4_ROUTE_REDIRECT_SILENCE,
  2418. .procname = "redirect_silence",
  2419. .data = &ip_rt_redirect_silence,
  2420. .maxlen = sizeof(int),
  2421. .mode = 0644,
  2422. .proc_handler = &proc_dointvec,
  2423. },
  2424. {
  2425. .ctl_name = NET_IPV4_ROUTE_ERROR_COST,
  2426. .procname = "error_cost",
  2427. .data = &ip_rt_error_cost,
  2428. .maxlen = sizeof(int),
  2429. .mode = 0644,
  2430. .proc_handler = &proc_dointvec,
  2431. },
  2432. {
  2433. .ctl_name = NET_IPV4_ROUTE_ERROR_BURST,
  2434. .procname = "error_burst",
  2435. .data = &ip_rt_error_burst,
  2436. .maxlen = sizeof(int),
  2437. .mode = 0644,
  2438. .proc_handler = &proc_dointvec,
  2439. },
  2440. {
  2441. .ctl_name = NET_IPV4_ROUTE_GC_ELASTICITY,
  2442. .procname = "gc_elasticity",
  2443. .data = &ip_rt_gc_elasticity,
  2444. .maxlen = sizeof(int),
  2445. .mode = 0644,
  2446. .proc_handler = &proc_dointvec,
  2447. },
  2448. {
  2449. .ctl_name = NET_IPV4_ROUTE_MTU_EXPIRES,
  2450. .procname = "mtu_expires",
  2451. .data = &ip_rt_mtu_expires,
  2452. .maxlen = sizeof(int),
  2453. .mode = 0644,
  2454. .proc_handler = &proc_dointvec_jiffies,
  2455. .strategy = &sysctl_jiffies,
  2456. },
  2457. {
  2458. .ctl_name = NET_IPV4_ROUTE_MIN_PMTU,
  2459. .procname = "min_pmtu",
  2460. .data = &ip_rt_min_pmtu,
  2461. .maxlen = sizeof(int),
  2462. .mode = 0644,
  2463. .proc_handler = &proc_dointvec,
  2464. },
  2465. {
  2466. .ctl_name = NET_IPV4_ROUTE_MIN_ADVMSS,
  2467. .procname = "min_adv_mss",
  2468. .data = &ip_rt_min_advmss,
  2469. .maxlen = sizeof(int),
  2470. .mode = 0644,
  2471. .proc_handler = &proc_dointvec,
  2472. },
  2473. {
  2474. .ctl_name = NET_IPV4_ROUTE_SECRET_INTERVAL,
  2475. .procname = "secret_interval",
  2476. .data = &ip_rt_secret_interval,
  2477. .maxlen = sizeof(int),
  2478. .mode = 0644,
  2479. .proc_handler = &proc_dointvec_jiffies,
  2480. .strategy = &sysctl_jiffies,
  2481. },
  2482. { .ctl_name = 0 }
  2483. };
  2484. #endif
  2485. #ifdef CONFIG_NET_CLS_ROUTE
  2486. struct ip_rt_acct *ip_rt_acct;
  2487. /* This code sucks. But you should have seen it before! --RR */
  2488. /* IP route accounting ptr for this logical cpu number. */
  2489. #define IP_RT_ACCT_CPU(i) (ip_rt_acct + i * 256)
  2490. #ifdef CONFIG_PROC_FS
  2491. static int ip_rt_acct_read(char *buffer, char **start, off_t offset,
  2492. int length, int *eof, void *data)
  2493. {
  2494. unsigned int i;
  2495. if ((offset & 3) || (length & 3))
  2496. return -EIO;
  2497. if (offset >= sizeof(struct ip_rt_acct) * 256) {
  2498. *eof = 1;
  2499. return 0;
  2500. }
  2501. if (offset + length >= sizeof(struct ip_rt_acct) * 256) {
  2502. length = sizeof(struct ip_rt_acct) * 256 - offset;
  2503. *eof = 1;
  2504. }
  2505. offset /= sizeof(u32);
  2506. if (length > 0) {
  2507. u32 *src = ((u32 *) IP_RT_ACCT_CPU(0)) + offset;
  2508. u32 *dst = (u32 *) buffer;
  2509. /* Copy first cpu. */
  2510. *start = buffer;
  2511. memcpy(dst, src, length);
  2512. /* Add the other cpus in, one int at a time */
  2513. for_each_possible_cpu(i) {
  2514. unsigned int j;
  2515. src = ((u32 *) IP_RT_ACCT_CPU(i)) + offset;
  2516. for (j = 0; j < length/4; j++)
  2517. dst[j] += src[j];
  2518. }
  2519. }
  2520. return length;
  2521. }
  2522. #endif /* CONFIG_PROC_FS */
  2523. #endif /* CONFIG_NET_CLS_ROUTE */
  2524. static __initdata unsigned long rhash_entries;
  2525. static int __init set_rhash_entries(char *str)
  2526. {
  2527. if (!str)
  2528. return 0;
  2529. rhash_entries = simple_strtoul(str, &str, 0);
  2530. return 1;
  2531. }
  2532. __setup("rhash_entries=", set_rhash_entries);
  2533. int __init ip_rt_init(void)
  2534. {
  2535. int rc = 0;
  2536. rt_hash_rnd = (int) ((num_physpages ^ (num_physpages>>8)) ^
  2537. (jiffies ^ (jiffies >> 7)));
  2538. #ifdef CONFIG_NET_CLS_ROUTE
  2539. {
  2540. int order;
  2541. for (order = 0;
  2542. (PAGE_SIZE << order) < 256 * sizeof(struct ip_rt_acct) * NR_CPUS; order++)
  2543. /* NOTHING */;
  2544. ip_rt_acct = (struct ip_rt_acct *)__get_free_pages(GFP_KERNEL, order);
  2545. if (!ip_rt_acct)
  2546. panic("IP: failed to allocate ip_rt_acct\n");
  2547. memset(ip_rt_acct, 0, PAGE_SIZE << order);
  2548. }
  2549. #endif
  2550. ipv4_dst_ops.kmem_cachep =
  2551. kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
  2552. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  2553. ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
  2554. rt_hash_table = (struct rt_hash_bucket *)
  2555. alloc_large_system_hash("IP route cache",
  2556. sizeof(struct rt_hash_bucket),
  2557. rhash_entries,
  2558. (num_physpages >= 128 * 1024) ?
  2559. 15 : 17,
  2560. 0,
  2561. &rt_hash_log,
  2562. &rt_hash_mask,
  2563. 0);
  2564. memset(rt_hash_table, 0, (rt_hash_mask + 1) * sizeof(struct rt_hash_bucket));
  2565. rt_hash_lock_init();
  2566. ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1);
  2567. ip_rt_max_size = (rt_hash_mask + 1) * 16;
  2568. devinet_init();
  2569. ip_fib_init();
  2570. init_timer(&rt_flush_timer);
  2571. rt_flush_timer.function = rt_run_flush;
  2572. init_timer(&rt_secret_timer);
  2573. rt_secret_timer.function = rt_secret_rebuild;
  2574. /* All the timers, started at system startup tend
  2575. to synchronize. Perturb it a bit.
  2576. */
  2577. schedule_delayed_work(&expires_work,
  2578. net_random() % ip_rt_gc_interval + ip_rt_gc_interval);
  2579. rt_secret_timer.expires = jiffies + net_random() % ip_rt_secret_interval +
  2580. ip_rt_secret_interval;
  2581. add_timer(&rt_secret_timer);
  2582. #ifdef CONFIG_PROC_FS
  2583. {
  2584. struct proc_dir_entry *rtstat_pde = NULL; /* keep gcc happy */
  2585. if (!proc_net_fops_create(&init_net, "rt_cache", S_IRUGO, &rt_cache_seq_fops) ||
  2586. !(rtstat_pde = create_proc_entry("rt_cache", S_IRUGO,
  2587. init_net.proc_net_stat))) {
  2588. return -ENOMEM;
  2589. }
  2590. rtstat_pde->proc_fops = &rt_cpu_seq_fops;
  2591. }
  2592. #ifdef CONFIG_NET_CLS_ROUTE
  2593. create_proc_read_entry("rt_acct", 0, init_net.proc_net, ip_rt_acct_read, NULL);
  2594. #endif
  2595. #endif
  2596. #ifdef CONFIG_XFRM
  2597. xfrm_init();
  2598. xfrm4_init();
  2599. #endif
  2600. rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL);
  2601. return rc;
  2602. }
  2603. EXPORT_SYMBOL(__ip_select_ident);
  2604. EXPORT_SYMBOL(ip_route_input);
  2605. EXPORT_SYMBOL(ip_route_output_key);