neighbour.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250
  1. /*
  2. * Generic address resolution entity
  3. *
  4. * Authors:
  5. * Pedro Roque <roque@di.fc.ul.pt>
  6. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Fixes:
  14. * Vitaly E. Lavrov releasing NULL neighbor in neigh_add.
  15. * Harald Welte Add neighbour cache statistics like rtstat
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/slab.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/socket.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/proc_fs.h>
  25. #ifdef CONFIG_SYSCTL
  26. #include <linux/sysctl.h>
  27. #endif
  28. #include <linux/times.h>
  29. #include <net/net_namespace.h>
  30. #include <net/neighbour.h>
  31. #include <net/dst.h>
  32. #include <net/sock.h>
  33. #include <net/netevent.h>
  34. #include <net/netlink.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/random.h>
  37. #include <linux/string.h>
  38. #include <linux/log2.h>
  39. #include <linux/inetdevice.h>
  40. #include <net/addrconf.h>
  41. #define DEBUG
  42. #define NEIGH_DEBUG 1
  43. #define neigh_dbg(level, fmt, ...) \
  44. do { \
  45. if (level <= NEIGH_DEBUG) \
  46. pr_debug(fmt, ##__VA_ARGS__); \
  47. } while (0)
  48. #define PNEIGH_HASHMASK 0xF
  49. static void neigh_timer_handler(unsigned long arg);
  50. static void __neigh_notify(struct neighbour *n, int type, int flags);
  51. static void neigh_update_notify(struct neighbour *neigh);
  52. static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
  53. struct net_device *dev);
  54. #ifdef CONFIG_PROC_FS
  55. static const struct file_operations neigh_stat_seq_fops;
  56. #endif
  57. /*
  58. Neighbour hash table buckets are protected with rwlock tbl->lock.
  59. - All the scans/updates to hash buckets MUST be made under this lock.
  60. - NOTHING clever should be made under this lock: no callbacks
  61. to protocol backends, no attempts to send something to network.
  62. It will result in deadlocks, if backend/driver wants to use neighbour
  63. cache.
  64. - If the entry requires some non-trivial actions, increase
  65. its reference count and release table lock.
  66. Neighbour entries are protected:
  67. - with reference count.
  68. - with rwlock neigh->lock
  69. Reference count prevents destruction.
  70. neigh->lock mainly serializes ll address data and its validity state.
  71. However, the same lock is used to protect another entry fields:
  72. - timer
  73. - resolution queue
  74. Again, nothing clever shall be made under neigh->lock,
  75. the most complicated procedure, which we allow is dev->hard_header.
  76. It is supposed, that dev->hard_header is simplistic and does
  77. not make callbacks to neighbour tables.
  78. */
  79. static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
  80. {
  81. kfree_skb(skb);
  82. return -ENETDOWN;
  83. }
  84. static void neigh_cleanup_and_release(struct neighbour *neigh)
  85. {
  86. if (neigh->parms->neigh_cleanup)
  87. neigh->parms->neigh_cleanup(neigh);
  88. __neigh_notify(neigh, RTM_DELNEIGH, 0);
  89. neigh_release(neigh);
  90. }
  91. /*
  92. * It is random distribution in the interval (1/2)*base...(3/2)*base.
  93. * It corresponds to default IPv6 settings and is not overridable,
  94. * because it is really reasonable choice.
  95. */
  96. unsigned long neigh_rand_reach_time(unsigned long base)
  97. {
  98. return base ? (prandom_u32() % base) + (base >> 1) : 0;
  99. }
  100. EXPORT_SYMBOL(neigh_rand_reach_time);
  101. static int neigh_forced_gc(struct neigh_table *tbl)
  102. {
  103. int shrunk = 0;
  104. int i;
  105. struct neigh_hash_table *nht;
  106. NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
  107. write_lock_bh(&tbl->lock);
  108. nht = rcu_dereference_protected(tbl->nht,
  109. lockdep_is_held(&tbl->lock));
  110. for (i = 0; i < (1 << nht->hash_shift); i++) {
  111. struct neighbour *n;
  112. struct neighbour __rcu **np;
  113. np = &nht->hash_buckets[i];
  114. while ((n = rcu_dereference_protected(*np,
  115. lockdep_is_held(&tbl->lock))) != NULL) {
  116. /* Neighbour record may be discarded if:
  117. * - nobody refers to it.
  118. * - it is not permanent
  119. */
  120. write_lock(&n->lock);
  121. if (atomic_read(&n->refcnt) == 1 &&
  122. !(n->nud_state & NUD_PERMANENT)) {
  123. rcu_assign_pointer(*np,
  124. rcu_dereference_protected(n->next,
  125. lockdep_is_held(&tbl->lock)));
  126. n->dead = 1;
  127. shrunk = 1;
  128. write_unlock(&n->lock);
  129. neigh_cleanup_and_release(n);
  130. continue;
  131. }
  132. write_unlock(&n->lock);
  133. np = &n->next;
  134. }
  135. }
  136. tbl->last_flush = jiffies;
  137. write_unlock_bh(&tbl->lock);
  138. return shrunk;
  139. }
  140. static void neigh_add_timer(struct neighbour *n, unsigned long when)
  141. {
  142. neigh_hold(n);
  143. if (unlikely(mod_timer(&n->timer, when))) {
  144. printk("NEIGH: BUG, double timer add, state is %x\n",
  145. n->nud_state);
  146. dump_stack();
  147. }
  148. }
  149. static int neigh_del_timer(struct neighbour *n)
  150. {
  151. if ((n->nud_state & NUD_IN_TIMER) &&
  152. del_timer(&n->timer)) {
  153. neigh_release(n);
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. static void pneigh_queue_purge(struct sk_buff_head *list)
  159. {
  160. struct sk_buff *skb;
  161. while ((skb = skb_dequeue(list)) != NULL) {
  162. dev_put(skb->dev);
  163. kfree_skb(skb);
  164. }
  165. }
  166. static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
  167. {
  168. int i;
  169. struct neigh_hash_table *nht;
  170. nht = rcu_dereference_protected(tbl->nht,
  171. lockdep_is_held(&tbl->lock));
  172. for (i = 0; i < (1 << nht->hash_shift); i++) {
  173. struct neighbour *n;
  174. struct neighbour __rcu **np = &nht->hash_buckets[i];
  175. while ((n = rcu_dereference_protected(*np,
  176. lockdep_is_held(&tbl->lock))) != NULL) {
  177. if (dev && n->dev != dev) {
  178. np = &n->next;
  179. continue;
  180. }
  181. rcu_assign_pointer(*np,
  182. rcu_dereference_protected(n->next,
  183. lockdep_is_held(&tbl->lock)));
  184. write_lock(&n->lock);
  185. neigh_del_timer(n);
  186. n->dead = 1;
  187. if (atomic_read(&n->refcnt) != 1) {
  188. /* The most unpleasant situation.
  189. We must destroy neighbour entry,
  190. but someone still uses it.
  191. The destroy will be delayed until
  192. the last user releases us, but
  193. we must kill timers etc. and move
  194. it to safe state.
  195. */
  196. __skb_queue_purge(&n->arp_queue);
  197. n->arp_queue_len_bytes = 0;
  198. n->output = neigh_blackhole;
  199. if (n->nud_state & NUD_VALID)
  200. n->nud_state = NUD_NOARP;
  201. else
  202. n->nud_state = NUD_NONE;
  203. neigh_dbg(2, "neigh %p is stray\n", n);
  204. }
  205. write_unlock(&n->lock);
  206. neigh_cleanup_and_release(n);
  207. }
  208. }
  209. }
  210. void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
  211. {
  212. write_lock_bh(&tbl->lock);
  213. neigh_flush_dev(tbl, dev);
  214. write_unlock_bh(&tbl->lock);
  215. }
  216. EXPORT_SYMBOL(neigh_changeaddr);
  217. int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
  218. {
  219. write_lock_bh(&tbl->lock);
  220. neigh_flush_dev(tbl, dev);
  221. pneigh_ifdown_and_unlock(tbl, dev);
  222. del_timer_sync(&tbl->proxy_timer);
  223. pneigh_queue_purge(&tbl->proxy_queue);
  224. return 0;
  225. }
  226. EXPORT_SYMBOL(neigh_ifdown);
  227. static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev)
  228. {
  229. struct neighbour *n = NULL;
  230. unsigned long now = jiffies;
  231. int entries;
  232. entries = atomic_inc_return(&tbl->entries) - 1;
  233. if (entries >= tbl->gc_thresh3 ||
  234. (entries >= tbl->gc_thresh2 &&
  235. time_after(now, tbl->last_flush + 5 * HZ))) {
  236. if (!neigh_forced_gc(tbl) &&
  237. entries >= tbl->gc_thresh3) {
  238. net_info_ratelimited("%s: neighbor table overflow!\n",
  239. tbl->id);
  240. NEIGH_CACHE_STAT_INC(tbl, table_fulls);
  241. goto out_entries;
  242. }
  243. }
  244. n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
  245. if (!n)
  246. goto out_entries;
  247. __skb_queue_head_init(&n->arp_queue);
  248. rwlock_init(&n->lock);
  249. seqlock_init(&n->ha_lock);
  250. n->updated = n->used = now;
  251. n->nud_state = NUD_NONE;
  252. n->output = neigh_blackhole;
  253. seqlock_init(&n->hh.hh_lock);
  254. n->parms = neigh_parms_clone(&tbl->parms);
  255. setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n);
  256. NEIGH_CACHE_STAT_INC(tbl, allocs);
  257. n->tbl = tbl;
  258. atomic_set(&n->refcnt, 1);
  259. n->dead = 1;
  260. out:
  261. return n;
  262. out_entries:
  263. atomic_dec(&tbl->entries);
  264. goto out;
  265. }
  266. static void neigh_get_hash_rnd(u32 *x)
  267. {
  268. get_random_bytes(x, sizeof(*x));
  269. *x |= 1;
  270. }
  271. static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
  272. {
  273. size_t size = (1 << shift) * sizeof(struct neighbour *);
  274. struct neigh_hash_table *ret;
  275. struct neighbour __rcu **buckets;
  276. int i;
  277. ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
  278. if (!ret)
  279. return NULL;
  280. if (size <= PAGE_SIZE)
  281. buckets = kzalloc(size, GFP_ATOMIC);
  282. else
  283. buckets = (struct neighbour __rcu **)
  284. __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
  285. get_order(size));
  286. if (!buckets) {
  287. kfree(ret);
  288. return NULL;
  289. }
  290. ret->hash_buckets = buckets;
  291. ret->hash_shift = shift;
  292. for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
  293. neigh_get_hash_rnd(&ret->hash_rnd[i]);
  294. return ret;
  295. }
  296. static void neigh_hash_free_rcu(struct rcu_head *head)
  297. {
  298. struct neigh_hash_table *nht = container_of(head,
  299. struct neigh_hash_table,
  300. rcu);
  301. size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *);
  302. struct neighbour __rcu **buckets = nht->hash_buckets;
  303. if (size <= PAGE_SIZE)
  304. kfree(buckets);
  305. else
  306. free_pages((unsigned long)buckets, get_order(size));
  307. kfree(nht);
  308. }
  309. static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
  310. unsigned long new_shift)
  311. {
  312. unsigned int i, hash;
  313. struct neigh_hash_table *new_nht, *old_nht;
  314. NEIGH_CACHE_STAT_INC(tbl, hash_grows);
  315. old_nht = rcu_dereference_protected(tbl->nht,
  316. lockdep_is_held(&tbl->lock));
  317. new_nht = neigh_hash_alloc(new_shift);
  318. if (!new_nht)
  319. return old_nht;
  320. for (i = 0; i < (1 << old_nht->hash_shift); i++) {
  321. struct neighbour *n, *next;
  322. for (n = rcu_dereference_protected(old_nht->hash_buckets[i],
  323. lockdep_is_held(&tbl->lock));
  324. n != NULL;
  325. n = next) {
  326. hash = tbl->hash(n->primary_key, n->dev,
  327. new_nht->hash_rnd);
  328. hash >>= (32 - new_nht->hash_shift);
  329. next = rcu_dereference_protected(n->next,
  330. lockdep_is_held(&tbl->lock));
  331. rcu_assign_pointer(n->next,
  332. rcu_dereference_protected(
  333. new_nht->hash_buckets[hash],
  334. lockdep_is_held(&tbl->lock)));
  335. rcu_assign_pointer(new_nht->hash_buckets[hash], n);
  336. }
  337. }
  338. rcu_assign_pointer(tbl->nht, new_nht);
  339. call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
  340. return new_nht;
  341. }
  342. struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
  343. struct net_device *dev)
  344. {
  345. struct neighbour *n;
  346. NEIGH_CACHE_STAT_INC(tbl, lookups);
  347. rcu_read_lock_bh();
  348. n = __neigh_lookup_noref(tbl, pkey, dev);
  349. if (n) {
  350. if (!atomic_inc_not_zero(&n->refcnt))
  351. n = NULL;
  352. NEIGH_CACHE_STAT_INC(tbl, hits);
  353. }
  354. rcu_read_unlock_bh();
  355. return n;
  356. }
  357. EXPORT_SYMBOL(neigh_lookup);
  358. struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
  359. const void *pkey)
  360. {
  361. struct neighbour *n;
  362. int key_len = tbl->key_len;
  363. u32 hash_val;
  364. struct neigh_hash_table *nht;
  365. NEIGH_CACHE_STAT_INC(tbl, lookups);
  366. rcu_read_lock_bh();
  367. nht = rcu_dereference_bh(tbl->nht);
  368. hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift);
  369. for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
  370. n != NULL;
  371. n = rcu_dereference_bh(n->next)) {
  372. if (!memcmp(n->primary_key, pkey, key_len) &&
  373. net_eq(dev_net(n->dev), net)) {
  374. if (!atomic_inc_not_zero(&n->refcnt))
  375. n = NULL;
  376. NEIGH_CACHE_STAT_INC(tbl, hits);
  377. break;
  378. }
  379. }
  380. rcu_read_unlock_bh();
  381. return n;
  382. }
  383. EXPORT_SYMBOL(neigh_lookup_nodev);
  384. struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
  385. struct net_device *dev, bool want_ref)
  386. {
  387. u32 hash_val;
  388. int key_len = tbl->key_len;
  389. int error;
  390. struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev);
  391. struct neigh_hash_table *nht;
  392. if (!n) {
  393. rc = ERR_PTR(-ENOBUFS);
  394. goto out;
  395. }
  396. memcpy(n->primary_key, pkey, key_len);
  397. n->dev = dev;
  398. dev_hold(dev);
  399. /* Protocol specific setup. */
  400. if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
  401. rc = ERR_PTR(error);
  402. goto out_neigh_release;
  403. }
  404. if (dev->netdev_ops->ndo_neigh_construct) {
  405. error = dev->netdev_ops->ndo_neigh_construct(n);
  406. if (error < 0) {
  407. rc = ERR_PTR(error);
  408. goto out_neigh_release;
  409. }
  410. }
  411. /* Device specific setup. */
  412. if (n->parms->neigh_setup &&
  413. (error = n->parms->neigh_setup(n)) < 0) {
  414. rc = ERR_PTR(error);
  415. goto out_neigh_release;
  416. }
  417. n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
  418. write_lock_bh(&tbl->lock);
  419. nht = rcu_dereference_protected(tbl->nht,
  420. lockdep_is_held(&tbl->lock));
  421. if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
  422. nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
  423. hash_val = tbl->hash(n->primary_key, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
  424. if (n->parms->dead) {
  425. rc = ERR_PTR(-EINVAL);
  426. goto out_tbl_unlock;
  427. }
  428. for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val],
  429. lockdep_is_held(&tbl->lock));
  430. n1 != NULL;
  431. n1 = rcu_dereference_protected(n1->next,
  432. lockdep_is_held(&tbl->lock))) {
  433. if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
  434. if (want_ref)
  435. neigh_hold(n1);
  436. rc = n1;
  437. goto out_tbl_unlock;
  438. }
  439. }
  440. n->dead = 0;
  441. if (want_ref)
  442. neigh_hold(n);
  443. rcu_assign_pointer(n->next,
  444. rcu_dereference_protected(nht->hash_buckets[hash_val],
  445. lockdep_is_held(&tbl->lock)));
  446. rcu_assign_pointer(nht->hash_buckets[hash_val], n);
  447. write_unlock_bh(&tbl->lock);
  448. neigh_dbg(2, "neigh %p is created\n", n);
  449. rc = n;
  450. out:
  451. return rc;
  452. out_tbl_unlock:
  453. write_unlock_bh(&tbl->lock);
  454. out_neigh_release:
  455. neigh_release(n);
  456. goto out;
  457. }
  458. EXPORT_SYMBOL(__neigh_create);
  459. static u32 pneigh_hash(const void *pkey, int key_len)
  460. {
  461. u32 hash_val = *(u32 *)(pkey + key_len - 4);
  462. hash_val ^= (hash_val >> 16);
  463. hash_val ^= hash_val >> 8;
  464. hash_val ^= hash_val >> 4;
  465. hash_val &= PNEIGH_HASHMASK;
  466. return hash_val;
  467. }
  468. static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n,
  469. struct net *net,
  470. const void *pkey,
  471. int key_len,
  472. struct net_device *dev)
  473. {
  474. while (n) {
  475. if (!memcmp(n->key, pkey, key_len) &&
  476. net_eq(pneigh_net(n), net) &&
  477. (n->dev == dev || !n->dev))
  478. return n;
  479. n = n->next;
  480. }
  481. return NULL;
  482. }
  483. struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
  484. struct net *net, const void *pkey, struct net_device *dev)
  485. {
  486. int key_len = tbl->key_len;
  487. u32 hash_val = pneigh_hash(pkey, key_len);
  488. return __pneigh_lookup_1(tbl->phash_buckets[hash_val],
  489. net, pkey, key_len, dev);
  490. }
  491. EXPORT_SYMBOL_GPL(__pneigh_lookup);
  492. struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
  493. struct net *net, const void *pkey,
  494. struct net_device *dev, int creat)
  495. {
  496. struct pneigh_entry *n;
  497. int key_len = tbl->key_len;
  498. u32 hash_val = pneigh_hash(pkey, key_len);
  499. read_lock_bh(&tbl->lock);
  500. n = __pneigh_lookup_1(tbl->phash_buckets[hash_val],
  501. net, pkey, key_len, dev);
  502. read_unlock_bh(&tbl->lock);
  503. if (n || !creat)
  504. goto out;
  505. ASSERT_RTNL();
  506. n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
  507. if (!n)
  508. goto out;
  509. write_pnet(&n->net, net);
  510. memcpy(n->key, pkey, key_len);
  511. n->dev = dev;
  512. if (dev)
  513. dev_hold(dev);
  514. if (tbl->pconstructor && tbl->pconstructor(n)) {
  515. if (dev)
  516. dev_put(dev);
  517. kfree(n);
  518. n = NULL;
  519. goto out;
  520. }
  521. write_lock_bh(&tbl->lock);
  522. n->next = tbl->phash_buckets[hash_val];
  523. tbl->phash_buckets[hash_val] = n;
  524. write_unlock_bh(&tbl->lock);
  525. out:
  526. return n;
  527. }
  528. EXPORT_SYMBOL(pneigh_lookup);
  529. int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
  530. struct net_device *dev)
  531. {
  532. struct pneigh_entry *n, **np;
  533. int key_len = tbl->key_len;
  534. u32 hash_val = pneigh_hash(pkey, key_len);
  535. write_lock_bh(&tbl->lock);
  536. for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
  537. np = &n->next) {
  538. if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
  539. net_eq(pneigh_net(n), net)) {
  540. *np = n->next;
  541. write_unlock_bh(&tbl->lock);
  542. if (tbl->pdestructor)
  543. tbl->pdestructor(n);
  544. if (n->dev)
  545. dev_put(n->dev);
  546. kfree(n);
  547. return 0;
  548. }
  549. }
  550. write_unlock_bh(&tbl->lock);
  551. return -ENOENT;
  552. }
  553. static int pneigh_ifdown_and_unlock(struct neigh_table *tbl,
  554. struct net_device *dev)
  555. {
  556. struct pneigh_entry *n, **np, *freelist = NULL;
  557. u32 h;
  558. for (h = 0; h <= PNEIGH_HASHMASK; h++) {
  559. np = &tbl->phash_buckets[h];
  560. while ((n = *np) != NULL) {
  561. if (!dev || n->dev == dev) {
  562. *np = n->next;
  563. n->next = freelist;
  564. freelist = n;
  565. continue;
  566. }
  567. np = &n->next;
  568. }
  569. }
  570. write_unlock_bh(&tbl->lock);
  571. while ((n = freelist)) {
  572. freelist = n->next;
  573. n->next = NULL;
  574. if (tbl->pdestructor)
  575. tbl->pdestructor(n);
  576. if (n->dev)
  577. dev_put(n->dev);
  578. kfree(n);
  579. }
  580. return -ENOENT;
  581. }
  582. static void neigh_parms_destroy(struct neigh_parms *parms);
  583. static inline void neigh_parms_put(struct neigh_parms *parms)
  584. {
  585. if (atomic_dec_and_test(&parms->refcnt))
  586. neigh_parms_destroy(parms);
  587. }
  588. /*
  589. * neighbour must already be out of the table;
  590. *
  591. */
  592. void neigh_destroy(struct neighbour *neigh)
  593. {
  594. struct net_device *dev = neigh->dev;
  595. NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
  596. if (!neigh->dead) {
  597. pr_warn("Destroying alive neighbour %p\n", neigh);
  598. dump_stack();
  599. return;
  600. }
  601. if (neigh_del_timer(neigh))
  602. pr_warn("Impossible event\n");
  603. write_lock_bh(&neigh->lock);
  604. __skb_queue_purge(&neigh->arp_queue);
  605. write_unlock_bh(&neigh->lock);
  606. neigh->arp_queue_len_bytes = 0;
  607. if (dev->netdev_ops->ndo_neigh_destroy)
  608. dev->netdev_ops->ndo_neigh_destroy(neigh);
  609. dev_put(dev);
  610. neigh_parms_put(neigh->parms);
  611. neigh_dbg(2, "neigh %p is destroyed\n", neigh);
  612. atomic_dec(&neigh->tbl->entries);
  613. kfree_rcu(neigh, rcu);
  614. }
  615. EXPORT_SYMBOL(neigh_destroy);
  616. /* Neighbour state is suspicious;
  617. disable fast path.
  618. Called with write_locked neigh.
  619. */
  620. static void neigh_suspect(struct neighbour *neigh)
  621. {
  622. neigh_dbg(2, "neigh %p is suspected\n", neigh);
  623. neigh->output = neigh->ops->output;
  624. }
  625. /* Neighbour state is OK;
  626. enable fast path.
  627. Called with write_locked neigh.
  628. */
  629. static void neigh_connect(struct neighbour *neigh)
  630. {
  631. neigh_dbg(2, "neigh %p is connected\n", neigh);
  632. neigh->output = neigh->ops->connected_output;
  633. }
  634. static void neigh_periodic_work(struct work_struct *work)
  635. {
  636. struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
  637. struct neighbour *n;
  638. struct neighbour __rcu **np;
  639. unsigned int i;
  640. struct neigh_hash_table *nht;
  641. NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
  642. write_lock_bh(&tbl->lock);
  643. nht = rcu_dereference_protected(tbl->nht,
  644. lockdep_is_held(&tbl->lock));
  645. /*
  646. * periodically recompute ReachableTime from random function
  647. */
  648. if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
  649. struct neigh_parms *p;
  650. tbl->last_rand = jiffies;
  651. list_for_each_entry(p, &tbl->parms_list, list)
  652. p->reachable_time =
  653. neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
  654. }
  655. if (atomic_read(&tbl->entries) < tbl->gc_thresh1)
  656. goto out;
  657. for (i = 0 ; i < (1 << nht->hash_shift); i++) {
  658. np = &nht->hash_buckets[i];
  659. while ((n = rcu_dereference_protected(*np,
  660. lockdep_is_held(&tbl->lock))) != NULL) {
  661. unsigned int state;
  662. write_lock(&n->lock);
  663. state = n->nud_state;
  664. if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
  665. write_unlock(&n->lock);
  666. goto next_elt;
  667. }
  668. if (time_before(n->used, n->confirmed))
  669. n->used = n->confirmed;
  670. if (atomic_read(&n->refcnt) == 1 &&
  671. (state == NUD_FAILED ||
  672. time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
  673. *np = n->next;
  674. n->dead = 1;
  675. write_unlock(&n->lock);
  676. neigh_cleanup_and_release(n);
  677. continue;
  678. }
  679. write_unlock(&n->lock);
  680. next_elt:
  681. np = &n->next;
  682. }
  683. /*
  684. * It's fine to release lock here, even if hash table
  685. * grows while we are preempted.
  686. */
  687. write_unlock_bh(&tbl->lock);
  688. cond_resched();
  689. write_lock_bh(&tbl->lock);
  690. nht = rcu_dereference_protected(tbl->nht,
  691. lockdep_is_held(&tbl->lock));
  692. }
  693. out:
  694. /* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
  695. * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
  696. * BASE_REACHABLE_TIME.
  697. */
  698. queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
  699. NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
  700. write_unlock_bh(&tbl->lock);
  701. }
  702. static __inline__ int neigh_max_probes(struct neighbour *n)
  703. {
  704. struct neigh_parms *p = n->parms;
  705. return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
  706. (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
  707. NEIGH_VAR(p, MCAST_PROBES));
  708. }
  709. static void neigh_invalidate(struct neighbour *neigh)
  710. __releases(neigh->lock)
  711. __acquires(neigh->lock)
  712. {
  713. struct sk_buff *skb;
  714. NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
  715. neigh_dbg(2, "neigh %p is failed\n", neigh);
  716. neigh->updated = jiffies;
  717. /* It is very thin place. report_unreachable is very complicated
  718. routine. Particularly, it can hit the same neighbour entry!
  719. So that, we try to be accurate and avoid dead loop. --ANK
  720. */
  721. while (neigh->nud_state == NUD_FAILED &&
  722. (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
  723. write_unlock(&neigh->lock);
  724. neigh->ops->error_report(neigh, skb);
  725. write_lock(&neigh->lock);
  726. }
  727. __skb_queue_purge(&neigh->arp_queue);
  728. neigh->arp_queue_len_bytes = 0;
  729. }
  730. static void neigh_probe(struct neighbour *neigh)
  731. __releases(neigh->lock)
  732. {
  733. struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
  734. /* keep skb alive even if arp_queue overflows */
  735. if (skb)
  736. skb = skb_clone(skb, GFP_ATOMIC);
  737. write_unlock(&neigh->lock);
  738. if (neigh->ops->solicit)
  739. neigh->ops->solicit(neigh, skb);
  740. atomic_inc(&neigh->probes);
  741. kfree_skb(skb);
  742. }
  743. /* Called when a timer expires for a neighbour entry. */
  744. static void neigh_timer_handler(unsigned long arg)
  745. {
  746. unsigned long now, next;
  747. struct neighbour *neigh = (struct neighbour *)arg;
  748. unsigned int state;
  749. int notify = 0;
  750. write_lock(&neigh->lock);
  751. state = neigh->nud_state;
  752. now = jiffies;
  753. next = now + HZ;
  754. if (!(state & NUD_IN_TIMER))
  755. goto out;
  756. if (state & NUD_REACHABLE) {
  757. if (time_before_eq(now,
  758. neigh->confirmed + neigh->parms->reachable_time)) {
  759. neigh_dbg(2, "neigh %p is still alive\n", neigh);
  760. next = neigh->confirmed + neigh->parms->reachable_time;
  761. } else if (time_before_eq(now,
  762. neigh->used +
  763. NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
  764. neigh_dbg(2, "neigh %p is delayed\n", neigh);
  765. neigh->nud_state = NUD_DELAY;
  766. neigh->updated = jiffies;
  767. neigh_suspect(neigh);
  768. next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
  769. } else {
  770. neigh_dbg(2, "neigh %p is suspected\n", neigh);
  771. neigh->nud_state = NUD_STALE;
  772. neigh->updated = jiffies;
  773. neigh_suspect(neigh);
  774. notify = 1;
  775. }
  776. } else if (state & NUD_DELAY) {
  777. if (time_before_eq(now,
  778. neigh->confirmed +
  779. NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
  780. neigh_dbg(2, "neigh %p is now reachable\n", neigh);
  781. neigh->nud_state = NUD_REACHABLE;
  782. neigh->updated = jiffies;
  783. neigh_connect(neigh);
  784. notify = 1;
  785. next = neigh->confirmed + neigh->parms->reachable_time;
  786. } else {
  787. neigh_dbg(2, "neigh %p is probed\n", neigh);
  788. neigh->nud_state = NUD_PROBE;
  789. neigh->updated = jiffies;
  790. atomic_set(&neigh->probes, 0);
  791. notify = 1;
  792. next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
  793. }
  794. } else {
  795. /* NUD_PROBE|NUD_INCOMPLETE */
  796. next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME);
  797. }
  798. if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
  799. atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
  800. neigh->nud_state = NUD_FAILED;
  801. notify = 1;
  802. neigh_invalidate(neigh);
  803. goto out;
  804. }
  805. if (neigh->nud_state & NUD_IN_TIMER) {
  806. if (time_before(next, jiffies + HZ/2))
  807. next = jiffies + HZ/2;
  808. if (!mod_timer(&neigh->timer, next))
  809. neigh_hold(neigh);
  810. }
  811. if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
  812. neigh_probe(neigh);
  813. } else {
  814. out:
  815. write_unlock(&neigh->lock);
  816. }
  817. if (notify)
  818. neigh_update_notify(neigh);
  819. neigh_release(neigh);
  820. }
  821. int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
  822. {
  823. int rc;
  824. bool immediate_probe = false;
  825. write_lock_bh(&neigh->lock);
  826. rc = 0;
  827. if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
  828. goto out_unlock_bh;
  829. if (neigh->dead)
  830. goto out_dead;
  831. if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
  832. if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
  833. NEIGH_VAR(neigh->parms, APP_PROBES)) {
  834. unsigned long next, now = jiffies;
  835. atomic_set(&neigh->probes,
  836. NEIGH_VAR(neigh->parms, UCAST_PROBES));
  837. neigh->nud_state = NUD_INCOMPLETE;
  838. neigh->updated = now;
  839. next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
  840. HZ/2);
  841. neigh_add_timer(neigh, next);
  842. immediate_probe = true;
  843. } else {
  844. neigh->nud_state = NUD_FAILED;
  845. neigh->updated = jiffies;
  846. write_unlock_bh(&neigh->lock);
  847. kfree_skb(skb);
  848. return 1;
  849. }
  850. } else if (neigh->nud_state & NUD_STALE) {
  851. neigh_dbg(2, "neigh %p is delayed\n", neigh);
  852. neigh->nud_state = NUD_DELAY;
  853. neigh->updated = jiffies;
  854. neigh_add_timer(neigh, jiffies +
  855. NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
  856. }
  857. if (neigh->nud_state == NUD_INCOMPLETE) {
  858. if (skb) {
  859. while (neigh->arp_queue_len_bytes + skb->truesize >
  860. NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
  861. struct sk_buff *buff;
  862. buff = __skb_dequeue(&neigh->arp_queue);
  863. if (!buff)
  864. break;
  865. neigh->arp_queue_len_bytes -= buff->truesize;
  866. kfree_skb(buff);
  867. NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
  868. }
  869. skb_dst_force(skb);
  870. __skb_queue_tail(&neigh->arp_queue, skb);
  871. neigh->arp_queue_len_bytes += skb->truesize;
  872. }
  873. rc = 1;
  874. }
  875. out_unlock_bh:
  876. if (immediate_probe)
  877. neigh_probe(neigh);
  878. else
  879. write_unlock(&neigh->lock);
  880. local_bh_enable();
  881. return rc;
  882. out_dead:
  883. if (neigh->nud_state & NUD_STALE)
  884. goto out_unlock_bh;
  885. write_unlock_bh(&neigh->lock);
  886. kfree_skb(skb);
  887. return 1;
  888. }
  889. EXPORT_SYMBOL(__neigh_event_send);
  890. static void neigh_update_hhs(struct neighbour *neigh)
  891. {
  892. struct hh_cache *hh;
  893. void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
  894. = NULL;
  895. if (neigh->dev->header_ops)
  896. update = neigh->dev->header_ops->cache_update;
  897. if (update) {
  898. hh = &neigh->hh;
  899. if (hh->hh_len) {
  900. write_seqlock_bh(&hh->hh_lock);
  901. update(hh, neigh->dev, neigh->ha);
  902. write_sequnlock_bh(&hh->hh_lock);
  903. }
  904. }
  905. }
  906. /* Generic update routine.
  907. -- lladdr is new lladdr or NULL, if it is not supplied.
  908. -- new is new state.
  909. -- flags
  910. NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
  911. if it is different.
  912. NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
  913. lladdr instead of overriding it
  914. if it is different.
  915. It also allows to retain current state
  916. if lladdr is unchanged.
  917. NEIGH_UPDATE_F_ADMIN means that the change is administrative.
  918. NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
  919. NTF_ROUTER flag.
  920. NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
  921. a router.
  922. Caller MUST hold reference count on the entry.
  923. */
  924. int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
  925. u32 flags)
  926. {
  927. u8 old;
  928. int err;
  929. int notify = 0;
  930. struct net_device *dev;
  931. int update_isrouter = 0;
  932. write_lock_bh(&neigh->lock);
  933. dev = neigh->dev;
  934. old = neigh->nud_state;
  935. err = -EPERM;
  936. if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
  937. (old & (NUD_NOARP | NUD_PERMANENT)))
  938. goto out;
  939. if (neigh->dead)
  940. goto out;
  941. if (!(new & NUD_VALID)) {
  942. neigh_del_timer(neigh);
  943. if (old & NUD_CONNECTED)
  944. neigh_suspect(neigh);
  945. neigh->nud_state = new;
  946. err = 0;
  947. notify = old & NUD_VALID;
  948. if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
  949. (new & NUD_FAILED)) {
  950. neigh_invalidate(neigh);
  951. notify = 1;
  952. }
  953. goto out;
  954. }
  955. /* Compare new lladdr with cached one */
  956. if (!dev->addr_len) {
  957. /* First case: device needs no address. */
  958. lladdr = neigh->ha;
  959. } else if (lladdr) {
  960. /* The second case: if something is already cached
  961. and a new address is proposed:
  962. - compare new & old
  963. - if they are different, check override flag
  964. */
  965. if ((old & NUD_VALID) &&
  966. !memcmp(lladdr, neigh->ha, dev->addr_len))
  967. lladdr = neigh->ha;
  968. } else {
  969. /* No address is supplied; if we know something,
  970. use it, otherwise discard the request.
  971. */
  972. err = -EINVAL;
  973. if (!(old & NUD_VALID))
  974. goto out;
  975. lladdr = neigh->ha;
  976. }
  977. /* Update confirmed timestamp for neighbour entry after we
  978. * received ARP packet even if it doesn't change IP to MAC binding.
  979. */
  980. if (new & NUD_CONNECTED)
  981. neigh->confirmed = jiffies;
  982. /* If entry was valid and address is not changed,
  983. do not change entry state, if new one is STALE.
  984. */
  985. err = 0;
  986. update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
  987. if (old & NUD_VALID) {
  988. if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
  989. update_isrouter = 0;
  990. if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
  991. (old & NUD_CONNECTED)) {
  992. lladdr = neigh->ha;
  993. new = NUD_STALE;
  994. } else
  995. goto out;
  996. } else {
  997. if (lladdr == neigh->ha && new == NUD_STALE &&
  998. ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) ||
  999. (old & NUD_CONNECTED))
  1000. )
  1001. new = old;
  1002. }
  1003. }
  1004. /* Update timestamp only once we know we will make a change to the
  1005. * neighbour entry. Otherwise we risk to move the locktime window with
  1006. * noop updates and ignore relevant ARP updates.
  1007. */
  1008. if (new != old || lladdr != neigh->ha)
  1009. neigh->updated = jiffies;
  1010. if (new != old) {
  1011. neigh_del_timer(neigh);
  1012. if (new & NUD_PROBE)
  1013. atomic_set(&neigh->probes, 0);
  1014. if (new & NUD_IN_TIMER)
  1015. neigh_add_timer(neigh, (jiffies +
  1016. ((new & NUD_REACHABLE) ?
  1017. neigh->parms->reachable_time :
  1018. 0)));
  1019. neigh->nud_state = new;
  1020. notify = 1;
  1021. }
  1022. if (lladdr != neigh->ha) {
  1023. write_seqlock(&neigh->ha_lock);
  1024. memcpy(&neigh->ha, lladdr, dev->addr_len);
  1025. write_sequnlock(&neigh->ha_lock);
  1026. neigh_update_hhs(neigh);
  1027. if (!(new & NUD_CONNECTED))
  1028. neigh->confirmed = jiffies -
  1029. (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
  1030. notify = 1;
  1031. }
  1032. if (new == old)
  1033. goto out;
  1034. if (new & NUD_CONNECTED)
  1035. neigh_connect(neigh);
  1036. else
  1037. neigh_suspect(neigh);
  1038. if (!(old & NUD_VALID)) {
  1039. struct sk_buff *skb;
  1040. /* Again: avoid dead loop if something went wrong */
  1041. while (neigh->nud_state & NUD_VALID &&
  1042. (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
  1043. struct dst_entry *dst = skb_dst(skb);
  1044. struct neighbour *n2, *n1 = neigh;
  1045. write_unlock_bh(&neigh->lock);
  1046. rcu_read_lock();
  1047. /* Why not just use 'neigh' as-is? The problem is that
  1048. * things such as shaper, eql, and sch_teql can end up
  1049. * using alternative, different, neigh objects to output
  1050. * the packet in the output path. So what we need to do
  1051. * here is re-lookup the top-level neigh in the path so
  1052. * we can reinject the packet there.
  1053. */
  1054. n2 = NULL;
  1055. if (dst) {
  1056. n2 = dst_neigh_lookup_skb(dst, skb);
  1057. if (n2)
  1058. n1 = n2;
  1059. }
  1060. n1->output(n1, skb);
  1061. if (n2)
  1062. neigh_release(n2);
  1063. rcu_read_unlock();
  1064. write_lock_bh(&neigh->lock);
  1065. }
  1066. __skb_queue_purge(&neigh->arp_queue);
  1067. neigh->arp_queue_len_bytes = 0;
  1068. }
  1069. out:
  1070. if (update_isrouter) {
  1071. neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
  1072. (neigh->flags | NTF_ROUTER) :
  1073. (neigh->flags & ~NTF_ROUTER);
  1074. }
  1075. write_unlock_bh(&neigh->lock);
  1076. if (notify)
  1077. neigh_update_notify(neigh);
  1078. return err;
  1079. }
  1080. EXPORT_SYMBOL(neigh_update);
  1081. /* Update the neigh to listen temporarily for probe responses, even if it is
  1082. * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
  1083. */
  1084. void __neigh_set_probe_once(struct neighbour *neigh)
  1085. {
  1086. if (neigh->dead)
  1087. return;
  1088. neigh->updated = jiffies;
  1089. if (!(neigh->nud_state & NUD_FAILED))
  1090. return;
  1091. neigh->nud_state = NUD_INCOMPLETE;
  1092. atomic_set(&neigh->probes, neigh_max_probes(neigh));
  1093. neigh_add_timer(neigh,
  1094. jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME));
  1095. }
  1096. EXPORT_SYMBOL(__neigh_set_probe_once);
  1097. struct neighbour *neigh_event_ns(struct neigh_table *tbl,
  1098. u8 *lladdr, void *saddr,
  1099. struct net_device *dev)
  1100. {
  1101. struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
  1102. lladdr || !dev->addr_len);
  1103. if (neigh)
  1104. neigh_update(neigh, lladdr, NUD_STALE,
  1105. NEIGH_UPDATE_F_OVERRIDE);
  1106. return neigh;
  1107. }
  1108. EXPORT_SYMBOL(neigh_event_ns);
  1109. /* called with read_lock_bh(&n->lock); */
  1110. static void neigh_hh_init(struct neighbour *n)
  1111. {
  1112. struct net_device *dev = n->dev;
  1113. __be16 prot = n->tbl->protocol;
  1114. struct hh_cache *hh = &n->hh;
  1115. write_lock_bh(&n->lock);
  1116. /* Only one thread can come in here and initialize the
  1117. * hh_cache entry.
  1118. */
  1119. if (!hh->hh_len)
  1120. dev->header_ops->cache(n, hh, prot);
  1121. write_unlock_bh(&n->lock);
  1122. }
  1123. /* Slow and careful. */
  1124. int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
  1125. {
  1126. int rc = 0;
  1127. if (!neigh_event_send(neigh, skb)) {
  1128. int err;
  1129. struct net_device *dev = neigh->dev;
  1130. unsigned int seq;
  1131. if (dev->header_ops->cache && !neigh->hh.hh_len)
  1132. neigh_hh_init(neigh);
  1133. do {
  1134. __skb_pull(skb, skb_network_offset(skb));
  1135. seq = read_seqbegin(&neigh->ha_lock);
  1136. err = dev_hard_header(skb, dev, ntohs(skb->protocol),
  1137. neigh->ha, NULL, skb->len);
  1138. } while (read_seqretry(&neigh->ha_lock, seq));
  1139. if (err >= 0)
  1140. rc = dev_queue_xmit(skb);
  1141. else
  1142. goto out_kfree_skb;
  1143. }
  1144. out:
  1145. return rc;
  1146. out_kfree_skb:
  1147. rc = -EINVAL;
  1148. kfree_skb(skb);
  1149. goto out;
  1150. }
  1151. EXPORT_SYMBOL(neigh_resolve_output);
  1152. /* As fast as possible without hh cache */
  1153. int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
  1154. {
  1155. struct net_device *dev = neigh->dev;
  1156. unsigned int seq;
  1157. int err;
  1158. do {
  1159. __skb_pull(skb, skb_network_offset(skb));
  1160. seq = read_seqbegin(&neigh->ha_lock);
  1161. err = dev_hard_header(skb, dev, ntohs(skb->protocol),
  1162. neigh->ha, NULL, skb->len);
  1163. } while (read_seqretry(&neigh->ha_lock, seq));
  1164. if (err >= 0)
  1165. err = dev_queue_xmit(skb);
  1166. else {
  1167. err = -EINVAL;
  1168. kfree_skb(skb);
  1169. }
  1170. return err;
  1171. }
  1172. EXPORT_SYMBOL(neigh_connected_output);
  1173. int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
  1174. {
  1175. return dev_queue_xmit(skb);
  1176. }
  1177. EXPORT_SYMBOL(neigh_direct_output);
  1178. static void neigh_proxy_process(unsigned long arg)
  1179. {
  1180. struct neigh_table *tbl = (struct neigh_table *)arg;
  1181. long sched_next = 0;
  1182. unsigned long now = jiffies;
  1183. struct sk_buff *skb, *n;
  1184. spin_lock(&tbl->proxy_queue.lock);
  1185. skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
  1186. long tdif = NEIGH_CB(skb)->sched_next - now;
  1187. if (tdif <= 0) {
  1188. struct net_device *dev = skb->dev;
  1189. __skb_unlink(skb, &tbl->proxy_queue);
  1190. if (tbl->proxy_redo && netif_running(dev)) {
  1191. rcu_read_lock();
  1192. tbl->proxy_redo(skb);
  1193. rcu_read_unlock();
  1194. } else {
  1195. kfree_skb(skb);
  1196. }
  1197. dev_put(dev);
  1198. } else if (!sched_next || tdif < sched_next)
  1199. sched_next = tdif;
  1200. }
  1201. del_timer(&tbl->proxy_timer);
  1202. if (sched_next)
  1203. mod_timer(&tbl->proxy_timer, jiffies + sched_next);
  1204. spin_unlock(&tbl->proxy_queue.lock);
  1205. }
  1206. void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
  1207. struct sk_buff *skb)
  1208. {
  1209. unsigned long now = jiffies;
  1210. unsigned long sched_next = now + (prandom_u32() %
  1211. NEIGH_VAR(p, PROXY_DELAY));
  1212. if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
  1213. kfree_skb(skb);
  1214. return;
  1215. }
  1216. NEIGH_CB(skb)->sched_next = sched_next;
  1217. NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
  1218. spin_lock(&tbl->proxy_queue.lock);
  1219. if (del_timer(&tbl->proxy_timer)) {
  1220. if (time_before(tbl->proxy_timer.expires, sched_next))
  1221. sched_next = tbl->proxy_timer.expires;
  1222. }
  1223. skb_dst_drop(skb);
  1224. dev_hold(skb->dev);
  1225. __skb_queue_tail(&tbl->proxy_queue, skb);
  1226. mod_timer(&tbl->proxy_timer, sched_next);
  1227. spin_unlock(&tbl->proxy_queue.lock);
  1228. }
  1229. EXPORT_SYMBOL(pneigh_enqueue);
  1230. static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
  1231. struct net *net, int ifindex)
  1232. {
  1233. struct neigh_parms *p;
  1234. list_for_each_entry(p, &tbl->parms_list, list) {
  1235. if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
  1236. (!p->dev && !ifindex && net_eq(net, &init_net)))
  1237. return p;
  1238. }
  1239. return NULL;
  1240. }
  1241. struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
  1242. struct neigh_table *tbl)
  1243. {
  1244. struct neigh_parms *p;
  1245. struct net *net = dev_net(dev);
  1246. const struct net_device_ops *ops = dev->netdev_ops;
  1247. p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
  1248. if (p) {
  1249. p->tbl = tbl;
  1250. atomic_set(&p->refcnt, 1);
  1251. p->reachable_time =
  1252. neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
  1253. dev_hold(dev);
  1254. p->dev = dev;
  1255. write_pnet(&p->net, net);
  1256. p->sysctl_table = NULL;
  1257. if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
  1258. dev_put(dev);
  1259. kfree(p);
  1260. return NULL;
  1261. }
  1262. write_lock_bh(&tbl->lock);
  1263. list_add(&p->list, &tbl->parms.list);
  1264. write_unlock_bh(&tbl->lock);
  1265. neigh_parms_data_state_cleanall(p);
  1266. }
  1267. return p;
  1268. }
  1269. EXPORT_SYMBOL(neigh_parms_alloc);
  1270. static void neigh_rcu_free_parms(struct rcu_head *head)
  1271. {
  1272. struct neigh_parms *parms =
  1273. container_of(head, struct neigh_parms, rcu_head);
  1274. neigh_parms_put(parms);
  1275. }
  1276. void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
  1277. {
  1278. if (!parms || parms == &tbl->parms)
  1279. return;
  1280. write_lock_bh(&tbl->lock);
  1281. list_del(&parms->list);
  1282. parms->dead = 1;
  1283. write_unlock_bh(&tbl->lock);
  1284. if (parms->dev)
  1285. dev_put(parms->dev);
  1286. call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
  1287. }
  1288. EXPORT_SYMBOL(neigh_parms_release);
  1289. static void neigh_parms_destroy(struct neigh_parms *parms)
  1290. {
  1291. kfree(parms);
  1292. }
  1293. static struct lock_class_key neigh_table_proxy_queue_class;
  1294. static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
  1295. void neigh_table_init(int index, struct neigh_table *tbl)
  1296. {
  1297. unsigned long now = jiffies;
  1298. unsigned long phsize;
  1299. INIT_LIST_HEAD(&tbl->parms_list);
  1300. list_add(&tbl->parms.list, &tbl->parms_list);
  1301. write_pnet(&tbl->parms.net, &init_net);
  1302. atomic_set(&tbl->parms.refcnt, 1);
  1303. tbl->parms.reachable_time =
  1304. neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
  1305. tbl->stats = alloc_percpu(struct neigh_statistics);
  1306. if (!tbl->stats)
  1307. panic("cannot create neighbour cache statistics");
  1308. #ifdef CONFIG_PROC_FS
  1309. if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat,
  1310. &neigh_stat_seq_fops, tbl))
  1311. panic("cannot create neighbour proc dir entry");
  1312. #endif
  1313. RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
  1314. phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
  1315. tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
  1316. if (!tbl->nht || !tbl->phash_buckets)
  1317. panic("cannot allocate neighbour cache hashes");
  1318. if (!tbl->entry_size)
  1319. tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
  1320. tbl->key_len, NEIGH_PRIV_ALIGN);
  1321. else
  1322. WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
  1323. rwlock_init(&tbl->lock);
  1324. INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
  1325. queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
  1326. tbl->parms.reachable_time);
  1327. setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl);
  1328. skb_queue_head_init_class(&tbl->proxy_queue,
  1329. &neigh_table_proxy_queue_class);
  1330. tbl->last_flush = now;
  1331. tbl->last_rand = now + tbl->parms.reachable_time * 20;
  1332. neigh_tables[index] = tbl;
  1333. }
  1334. EXPORT_SYMBOL(neigh_table_init);
  1335. int neigh_table_clear(int index, struct neigh_table *tbl)
  1336. {
  1337. neigh_tables[index] = NULL;
  1338. /* It is not clean... Fix it to unload IPv6 module safely */
  1339. cancel_delayed_work_sync(&tbl->gc_work);
  1340. del_timer_sync(&tbl->proxy_timer);
  1341. pneigh_queue_purge(&tbl->proxy_queue);
  1342. neigh_ifdown(tbl, NULL);
  1343. if (atomic_read(&tbl->entries))
  1344. pr_crit("neighbour leakage\n");
  1345. call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
  1346. neigh_hash_free_rcu);
  1347. tbl->nht = NULL;
  1348. kfree(tbl->phash_buckets);
  1349. tbl->phash_buckets = NULL;
  1350. remove_proc_entry(tbl->id, init_net.proc_net_stat);
  1351. free_percpu(tbl->stats);
  1352. tbl->stats = NULL;
  1353. return 0;
  1354. }
  1355. EXPORT_SYMBOL(neigh_table_clear);
  1356. static struct neigh_table *neigh_find_table(int family)
  1357. {
  1358. struct neigh_table *tbl = NULL;
  1359. switch (family) {
  1360. case AF_INET:
  1361. tbl = neigh_tables[NEIGH_ARP_TABLE];
  1362. break;
  1363. case AF_INET6:
  1364. tbl = neigh_tables[NEIGH_ND_TABLE];
  1365. break;
  1366. case AF_DECnet:
  1367. tbl = neigh_tables[NEIGH_DN_TABLE];
  1368. break;
  1369. }
  1370. return tbl;
  1371. }
  1372. static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh)
  1373. {
  1374. struct net *net = sock_net(skb->sk);
  1375. struct ndmsg *ndm;
  1376. struct nlattr *dst_attr;
  1377. struct neigh_table *tbl;
  1378. struct neighbour *neigh;
  1379. struct net_device *dev = NULL;
  1380. int err = -EINVAL;
  1381. ASSERT_RTNL();
  1382. if (nlmsg_len(nlh) < sizeof(*ndm))
  1383. goto out;
  1384. dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
  1385. if (dst_attr == NULL)
  1386. goto out;
  1387. ndm = nlmsg_data(nlh);
  1388. if (ndm->ndm_ifindex) {
  1389. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  1390. if (dev == NULL) {
  1391. err = -ENODEV;
  1392. goto out;
  1393. }
  1394. }
  1395. tbl = neigh_find_table(ndm->ndm_family);
  1396. if (tbl == NULL)
  1397. return -EAFNOSUPPORT;
  1398. if (nla_len(dst_attr) < tbl->key_len)
  1399. goto out;
  1400. if (ndm->ndm_flags & NTF_PROXY) {
  1401. err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
  1402. goto out;
  1403. }
  1404. if (dev == NULL)
  1405. goto out;
  1406. neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
  1407. if (neigh == NULL) {
  1408. err = -ENOENT;
  1409. goto out;
  1410. }
  1411. err = neigh_update(neigh, NULL, NUD_FAILED,
  1412. NEIGH_UPDATE_F_OVERRIDE |
  1413. NEIGH_UPDATE_F_ADMIN);
  1414. neigh_release(neigh);
  1415. out:
  1416. return err;
  1417. }
  1418. static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh)
  1419. {
  1420. int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
  1421. struct net *net = sock_net(skb->sk);
  1422. struct ndmsg *ndm;
  1423. struct nlattr *tb[NDA_MAX+1];
  1424. struct neigh_table *tbl;
  1425. struct net_device *dev = NULL;
  1426. struct neighbour *neigh;
  1427. void *dst, *lladdr;
  1428. int err;
  1429. ASSERT_RTNL();
  1430. err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
  1431. if (err < 0)
  1432. goto out;
  1433. err = -EINVAL;
  1434. if (tb[NDA_DST] == NULL)
  1435. goto out;
  1436. ndm = nlmsg_data(nlh);
  1437. if (ndm->ndm_ifindex) {
  1438. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  1439. if (dev == NULL) {
  1440. err = -ENODEV;
  1441. goto out;
  1442. }
  1443. if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
  1444. goto out;
  1445. }
  1446. tbl = neigh_find_table(ndm->ndm_family);
  1447. if (tbl == NULL)
  1448. return -EAFNOSUPPORT;
  1449. if (nla_len(tb[NDA_DST]) < tbl->key_len)
  1450. goto out;
  1451. dst = nla_data(tb[NDA_DST]);
  1452. lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
  1453. if (ndm->ndm_flags & NTF_PROXY) {
  1454. struct pneigh_entry *pn;
  1455. err = -ENOBUFS;
  1456. pn = pneigh_lookup(tbl, net, dst, dev, 1);
  1457. if (pn) {
  1458. pn->flags = ndm->ndm_flags;
  1459. err = 0;
  1460. }
  1461. goto out;
  1462. }
  1463. if (dev == NULL)
  1464. goto out;
  1465. neigh = neigh_lookup(tbl, dst, dev);
  1466. if (neigh == NULL) {
  1467. if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
  1468. err = -ENOENT;
  1469. goto out;
  1470. }
  1471. neigh = __neigh_lookup_errno(tbl, dst, dev);
  1472. if (IS_ERR(neigh)) {
  1473. err = PTR_ERR(neigh);
  1474. goto out;
  1475. }
  1476. } else {
  1477. if (nlh->nlmsg_flags & NLM_F_EXCL) {
  1478. err = -EEXIST;
  1479. neigh_release(neigh);
  1480. goto out;
  1481. }
  1482. if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
  1483. flags &= ~NEIGH_UPDATE_F_OVERRIDE;
  1484. }
  1485. if (ndm->ndm_flags & NTF_USE) {
  1486. neigh_event_send(neigh, NULL);
  1487. err = 0;
  1488. } else
  1489. err = neigh_update(neigh, lladdr, ndm->ndm_state, flags);
  1490. neigh_release(neigh);
  1491. out:
  1492. return err;
  1493. }
  1494. static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
  1495. {
  1496. struct nlattr *nest;
  1497. nest = nla_nest_start(skb, NDTA_PARMS);
  1498. if (nest == NULL)
  1499. return -ENOBUFS;
  1500. if ((parms->dev &&
  1501. nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
  1502. nla_put_u32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)) ||
  1503. nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
  1504. NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
  1505. /* approximative value for deprecated QUEUE_LEN (in packets) */
  1506. nla_put_u32(skb, NDTPA_QUEUE_LEN,
  1507. NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
  1508. nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
  1509. nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
  1510. nla_put_u32(skb, NDTPA_UCAST_PROBES,
  1511. NEIGH_VAR(parms, UCAST_PROBES)) ||
  1512. nla_put_u32(skb, NDTPA_MCAST_PROBES,
  1513. NEIGH_VAR(parms, MCAST_PROBES)) ||
  1514. nla_put_u32(skb, NDTPA_MCAST_REPROBES,
  1515. NEIGH_VAR(parms, MCAST_REPROBES)) ||
  1516. nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time) ||
  1517. nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
  1518. NEIGH_VAR(parms, BASE_REACHABLE_TIME)) ||
  1519. nla_put_msecs(skb, NDTPA_GC_STALETIME,
  1520. NEIGH_VAR(parms, GC_STALETIME)) ||
  1521. nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
  1522. NEIGH_VAR(parms, DELAY_PROBE_TIME)) ||
  1523. nla_put_msecs(skb, NDTPA_RETRANS_TIME,
  1524. NEIGH_VAR(parms, RETRANS_TIME)) ||
  1525. nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
  1526. NEIGH_VAR(parms, ANYCAST_DELAY)) ||
  1527. nla_put_msecs(skb, NDTPA_PROXY_DELAY,
  1528. NEIGH_VAR(parms, PROXY_DELAY)) ||
  1529. nla_put_msecs(skb, NDTPA_LOCKTIME,
  1530. NEIGH_VAR(parms, LOCKTIME)))
  1531. goto nla_put_failure;
  1532. return nla_nest_end(skb, nest);
  1533. nla_put_failure:
  1534. nla_nest_cancel(skb, nest);
  1535. return -EMSGSIZE;
  1536. }
  1537. static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
  1538. u32 pid, u32 seq, int type, int flags)
  1539. {
  1540. struct nlmsghdr *nlh;
  1541. struct ndtmsg *ndtmsg;
  1542. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
  1543. if (nlh == NULL)
  1544. return -EMSGSIZE;
  1545. ndtmsg = nlmsg_data(nlh);
  1546. read_lock_bh(&tbl->lock);
  1547. ndtmsg->ndtm_family = tbl->family;
  1548. ndtmsg->ndtm_pad1 = 0;
  1549. ndtmsg->ndtm_pad2 = 0;
  1550. if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
  1551. nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval) ||
  1552. nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
  1553. nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
  1554. nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
  1555. goto nla_put_failure;
  1556. {
  1557. unsigned long now = jiffies;
  1558. unsigned int flush_delta = now - tbl->last_flush;
  1559. unsigned int rand_delta = now - tbl->last_rand;
  1560. struct neigh_hash_table *nht;
  1561. struct ndt_config ndc = {
  1562. .ndtc_key_len = tbl->key_len,
  1563. .ndtc_entry_size = tbl->entry_size,
  1564. .ndtc_entries = atomic_read(&tbl->entries),
  1565. .ndtc_last_flush = jiffies_to_msecs(flush_delta),
  1566. .ndtc_last_rand = jiffies_to_msecs(rand_delta),
  1567. .ndtc_proxy_qlen = tbl->proxy_queue.qlen,
  1568. };
  1569. rcu_read_lock_bh();
  1570. nht = rcu_dereference_bh(tbl->nht);
  1571. ndc.ndtc_hash_rnd = nht->hash_rnd[0];
  1572. ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
  1573. rcu_read_unlock_bh();
  1574. if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
  1575. goto nla_put_failure;
  1576. }
  1577. {
  1578. int cpu;
  1579. struct ndt_stats ndst;
  1580. memset(&ndst, 0, sizeof(ndst));
  1581. for_each_possible_cpu(cpu) {
  1582. struct neigh_statistics *st;
  1583. st = per_cpu_ptr(tbl->stats, cpu);
  1584. ndst.ndts_allocs += st->allocs;
  1585. ndst.ndts_destroys += st->destroys;
  1586. ndst.ndts_hash_grows += st->hash_grows;
  1587. ndst.ndts_res_failed += st->res_failed;
  1588. ndst.ndts_lookups += st->lookups;
  1589. ndst.ndts_hits += st->hits;
  1590. ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast;
  1591. ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast;
  1592. ndst.ndts_periodic_gc_runs += st->periodic_gc_runs;
  1593. ndst.ndts_forced_gc_runs += st->forced_gc_runs;
  1594. ndst.ndts_table_fulls += st->table_fulls;
  1595. }
  1596. if (nla_put(skb, NDTA_STATS, sizeof(ndst), &ndst))
  1597. goto nla_put_failure;
  1598. }
  1599. BUG_ON(tbl->parms.dev);
  1600. if (neightbl_fill_parms(skb, &tbl->parms) < 0)
  1601. goto nla_put_failure;
  1602. read_unlock_bh(&tbl->lock);
  1603. nlmsg_end(skb, nlh);
  1604. return 0;
  1605. nla_put_failure:
  1606. read_unlock_bh(&tbl->lock);
  1607. nlmsg_cancel(skb, nlh);
  1608. return -EMSGSIZE;
  1609. }
  1610. static int neightbl_fill_param_info(struct sk_buff *skb,
  1611. struct neigh_table *tbl,
  1612. struct neigh_parms *parms,
  1613. u32 pid, u32 seq, int type,
  1614. unsigned int flags)
  1615. {
  1616. struct ndtmsg *ndtmsg;
  1617. struct nlmsghdr *nlh;
  1618. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
  1619. if (nlh == NULL)
  1620. return -EMSGSIZE;
  1621. ndtmsg = nlmsg_data(nlh);
  1622. read_lock_bh(&tbl->lock);
  1623. ndtmsg->ndtm_family = tbl->family;
  1624. ndtmsg->ndtm_pad1 = 0;
  1625. ndtmsg->ndtm_pad2 = 0;
  1626. if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
  1627. neightbl_fill_parms(skb, parms) < 0)
  1628. goto errout;
  1629. read_unlock_bh(&tbl->lock);
  1630. nlmsg_end(skb, nlh);
  1631. return 0;
  1632. errout:
  1633. read_unlock_bh(&tbl->lock);
  1634. nlmsg_cancel(skb, nlh);
  1635. return -EMSGSIZE;
  1636. }
  1637. static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
  1638. [NDTA_NAME] = { .type = NLA_STRING },
  1639. [NDTA_THRESH1] = { .type = NLA_U32 },
  1640. [NDTA_THRESH2] = { .type = NLA_U32 },
  1641. [NDTA_THRESH3] = { .type = NLA_U32 },
  1642. [NDTA_GC_INTERVAL] = { .type = NLA_U64 },
  1643. [NDTA_PARMS] = { .type = NLA_NESTED },
  1644. };
  1645. static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
  1646. [NDTPA_IFINDEX] = { .type = NLA_U32 },
  1647. [NDTPA_QUEUE_LEN] = { .type = NLA_U32 },
  1648. [NDTPA_PROXY_QLEN] = { .type = NLA_U32 },
  1649. [NDTPA_APP_PROBES] = { .type = NLA_U32 },
  1650. [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
  1651. [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
  1652. [NDTPA_MCAST_REPROBES] = { .type = NLA_U32 },
  1653. [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
  1654. [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
  1655. [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
  1656. [NDTPA_RETRANS_TIME] = { .type = NLA_U64 },
  1657. [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 },
  1658. [NDTPA_PROXY_DELAY] = { .type = NLA_U64 },
  1659. [NDTPA_LOCKTIME] = { .type = NLA_U64 },
  1660. };
  1661. static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh)
  1662. {
  1663. struct net *net = sock_net(skb->sk);
  1664. struct neigh_table *tbl;
  1665. struct ndtmsg *ndtmsg;
  1666. struct nlattr *tb[NDTA_MAX+1];
  1667. bool found = false;
  1668. int err, tidx;
  1669. err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
  1670. nl_neightbl_policy);
  1671. if (err < 0)
  1672. goto errout;
  1673. if (tb[NDTA_NAME] == NULL) {
  1674. err = -EINVAL;
  1675. goto errout;
  1676. }
  1677. ndtmsg = nlmsg_data(nlh);
  1678. for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
  1679. tbl = neigh_tables[tidx];
  1680. if (!tbl)
  1681. continue;
  1682. if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
  1683. continue;
  1684. if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
  1685. found = true;
  1686. break;
  1687. }
  1688. }
  1689. if (!found)
  1690. return -ENOENT;
  1691. /*
  1692. * We acquire tbl->lock to be nice to the periodic timers and
  1693. * make sure they always see a consistent set of values.
  1694. */
  1695. write_lock_bh(&tbl->lock);
  1696. if (tb[NDTA_PARMS]) {
  1697. struct nlattr *tbp[NDTPA_MAX+1];
  1698. struct neigh_parms *p;
  1699. int i, ifindex = 0;
  1700. err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
  1701. nl_ntbl_parm_policy);
  1702. if (err < 0)
  1703. goto errout_tbl_lock;
  1704. if (tbp[NDTPA_IFINDEX])
  1705. ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
  1706. p = lookup_neigh_parms(tbl, net, ifindex);
  1707. if (p == NULL) {
  1708. err = -ENOENT;
  1709. goto errout_tbl_lock;
  1710. }
  1711. for (i = 1; i <= NDTPA_MAX; i++) {
  1712. if (tbp[i] == NULL)
  1713. continue;
  1714. switch (i) {
  1715. case NDTPA_QUEUE_LEN:
  1716. NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
  1717. nla_get_u32(tbp[i]) *
  1718. SKB_TRUESIZE(ETH_FRAME_LEN));
  1719. break;
  1720. case NDTPA_QUEUE_LENBYTES:
  1721. NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
  1722. nla_get_u32(tbp[i]));
  1723. break;
  1724. case NDTPA_PROXY_QLEN:
  1725. NEIGH_VAR_SET(p, PROXY_QLEN,
  1726. nla_get_u32(tbp[i]));
  1727. break;
  1728. case NDTPA_APP_PROBES:
  1729. NEIGH_VAR_SET(p, APP_PROBES,
  1730. nla_get_u32(tbp[i]));
  1731. break;
  1732. case NDTPA_UCAST_PROBES:
  1733. NEIGH_VAR_SET(p, UCAST_PROBES,
  1734. nla_get_u32(tbp[i]));
  1735. break;
  1736. case NDTPA_MCAST_PROBES:
  1737. NEIGH_VAR_SET(p, MCAST_PROBES,
  1738. nla_get_u32(tbp[i]));
  1739. break;
  1740. case NDTPA_MCAST_REPROBES:
  1741. NEIGH_VAR_SET(p, MCAST_REPROBES,
  1742. nla_get_u32(tbp[i]));
  1743. break;
  1744. case NDTPA_BASE_REACHABLE_TIME:
  1745. NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
  1746. nla_get_msecs(tbp[i]));
  1747. /* update reachable_time as well, otherwise, the change will
  1748. * only be effective after the next time neigh_periodic_work
  1749. * decides to recompute it (can be multiple minutes)
  1750. */
  1751. p->reachable_time =
  1752. neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
  1753. break;
  1754. case NDTPA_GC_STALETIME:
  1755. NEIGH_VAR_SET(p, GC_STALETIME,
  1756. nla_get_msecs(tbp[i]));
  1757. break;
  1758. case NDTPA_DELAY_PROBE_TIME:
  1759. NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
  1760. nla_get_msecs(tbp[i]));
  1761. break;
  1762. case NDTPA_RETRANS_TIME:
  1763. NEIGH_VAR_SET(p, RETRANS_TIME,
  1764. nla_get_msecs(tbp[i]));
  1765. break;
  1766. case NDTPA_ANYCAST_DELAY:
  1767. NEIGH_VAR_SET(p, ANYCAST_DELAY,
  1768. nla_get_msecs(tbp[i]));
  1769. break;
  1770. case NDTPA_PROXY_DELAY:
  1771. NEIGH_VAR_SET(p, PROXY_DELAY,
  1772. nla_get_msecs(tbp[i]));
  1773. break;
  1774. case NDTPA_LOCKTIME:
  1775. NEIGH_VAR_SET(p, LOCKTIME,
  1776. nla_get_msecs(tbp[i]));
  1777. break;
  1778. }
  1779. }
  1780. }
  1781. err = -ENOENT;
  1782. if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
  1783. tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
  1784. !net_eq(net, &init_net))
  1785. goto errout_tbl_lock;
  1786. if (tb[NDTA_THRESH1])
  1787. tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
  1788. if (tb[NDTA_THRESH2])
  1789. tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
  1790. if (tb[NDTA_THRESH3])
  1791. tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
  1792. if (tb[NDTA_GC_INTERVAL])
  1793. tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
  1794. err = 0;
  1795. errout_tbl_lock:
  1796. write_unlock_bh(&tbl->lock);
  1797. errout:
  1798. return err;
  1799. }
  1800. static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
  1801. {
  1802. struct net *net = sock_net(skb->sk);
  1803. int family, tidx, nidx = 0;
  1804. int tbl_skip = cb->args[0];
  1805. int neigh_skip = cb->args[1];
  1806. struct neigh_table *tbl;
  1807. family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
  1808. for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
  1809. struct neigh_parms *p;
  1810. tbl = neigh_tables[tidx];
  1811. if (!tbl)
  1812. continue;
  1813. if (tidx < tbl_skip || (family && tbl->family != family))
  1814. continue;
  1815. if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
  1816. cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
  1817. NLM_F_MULTI) < 0)
  1818. break;
  1819. nidx = 0;
  1820. p = list_next_entry(&tbl->parms, list);
  1821. list_for_each_entry_from(p, &tbl->parms_list, list) {
  1822. if (!net_eq(neigh_parms_net(p), net))
  1823. continue;
  1824. if (nidx < neigh_skip)
  1825. goto next;
  1826. if (neightbl_fill_param_info(skb, tbl, p,
  1827. NETLINK_CB(cb->skb).portid,
  1828. cb->nlh->nlmsg_seq,
  1829. RTM_NEWNEIGHTBL,
  1830. NLM_F_MULTI) < 0)
  1831. goto out;
  1832. next:
  1833. nidx++;
  1834. }
  1835. neigh_skip = 0;
  1836. }
  1837. out:
  1838. cb->args[0] = tidx;
  1839. cb->args[1] = nidx;
  1840. return skb->len;
  1841. }
  1842. static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
  1843. u32 pid, u32 seq, int type, unsigned int flags)
  1844. {
  1845. unsigned long now = jiffies;
  1846. struct nda_cacheinfo ci;
  1847. struct nlmsghdr *nlh;
  1848. struct ndmsg *ndm;
  1849. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
  1850. if (nlh == NULL)
  1851. return -EMSGSIZE;
  1852. ndm = nlmsg_data(nlh);
  1853. ndm->ndm_family = neigh->ops->family;
  1854. ndm->ndm_pad1 = 0;
  1855. ndm->ndm_pad2 = 0;
  1856. ndm->ndm_flags = neigh->flags;
  1857. ndm->ndm_type = neigh->type;
  1858. ndm->ndm_ifindex = neigh->dev->ifindex;
  1859. if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
  1860. goto nla_put_failure;
  1861. read_lock_bh(&neigh->lock);
  1862. ndm->ndm_state = neigh->nud_state;
  1863. if (neigh->nud_state & NUD_VALID) {
  1864. char haddr[MAX_ADDR_LEN];
  1865. neigh_ha_snapshot(haddr, neigh, neigh->dev);
  1866. if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
  1867. read_unlock_bh(&neigh->lock);
  1868. goto nla_put_failure;
  1869. }
  1870. }
  1871. ci.ndm_used = jiffies_to_clock_t(now - neigh->used);
  1872. ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
  1873. ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated);
  1874. ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1;
  1875. read_unlock_bh(&neigh->lock);
  1876. if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
  1877. nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  1878. goto nla_put_failure;
  1879. nlmsg_end(skb, nlh);
  1880. return 0;
  1881. nla_put_failure:
  1882. nlmsg_cancel(skb, nlh);
  1883. return -EMSGSIZE;
  1884. }
  1885. static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
  1886. u32 pid, u32 seq, int type, unsigned int flags,
  1887. struct neigh_table *tbl)
  1888. {
  1889. struct nlmsghdr *nlh;
  1890. struct ndmsg *ndm;
  1891. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
  1892. if (nlh == NULL)
  1893. return -EMSGSIZE;
  1894. ndm = nlmsg_data(nlh);
  1895. ndm->ndm_family = tbl->family;
  1896. ndm->ndm_pad1 = 0;
  1897. ndm->ndm_pad2 = 0;
  1898. ndm->ndm_flags = pn->flags | NTF_PROXY;
  1899. ndm->ndm_type = RTN_UNICAST;
  1900. ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
  1901. ndm->ndm_state = NUD_NONE;
  1902. if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
  1903. goto nla_put_failure;
  1904. nlmsg_end(skb, nlh);
  1905. return 0;
  1906. nla_put_failure:
  1907. nlmsg_cancel(skb, nlh);
  1908. return -EMSGSIZE;
  1909. }
  1910. static void neigh_update_notify(struct neighbour *neigh)
  1911. {
  1912. call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
  1913. __neigh_notify(neigh, RTM_NEWNEIGH, 0);
  1914. }
  1915. static bool neigh_master_filtered(struct net_device *dev, int master_idx)
  1916. {
  1917. struct net_device *master;
  1918. if (!master_idx)
  1919. return false;
  1920. master = netdev_master_upper_dev_get(dev);
  1921. if (!master || master->ifindex != master_idx)
  1922. return true;
  1923. return false;
  1924. }
  1925. static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
  1926. {
  1927. if (filter_idx && dev->ifindex != filter_idx)
  1928. return true;
  1929. return false;
  1930. }
  1931. static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
  1932. struct netlink_callback *cb)
  1933. {
  1934. struct net *net = sock_net(skb->sk);
  1935. const struct nlmsghdr *nlh = cb->nlh;
  1936. struct nlattr *tb[NDA_MAX + 1];
  1937. struct neighbour *n;
  1938. int rc, h, s_h = cb->args[1];
  1939. int idx, s_idx = idx = cb->args[2];
  1940. struct neigh_hash_table *nht;
  1941. int filter_master_idx = 0, filter_idx = 0;
  1942. unsigned int flags = NLM_F_MULTI;
  1943. int err;
  1944. err = nlmsg_parse(nlh, sizeof(struct ndmsg), tb, NDA_MAX, NULL);
  1945. if (!err) {
  1946. if (tb[NDA_IFINDEX]) {
  1947. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
  1948. return -EINVAL;
  1949. filter_idx = nla_get_u32(tb[NDA_IFINDEX]);
  1950. }
  1951. if (tb[NDA_MASTER]) {
  1952. if (nla_len(tb[NDA_MASTER]) != sizeof(u32))
  1953. return -EINVAL;
  1954. filter_master_idx = nla_get_u32(tb[NDA_MASTER]);
  1955. }
  1956. if (filter_idx || filter_master_idx)
  1957. flags |= NLM_F_DUMP_FILTERED;
  1958. }
  1959. rcu_read_lock_bh();
  1960. nht = rcu_dereference_bh(tbl->nht);
  1961. for (h = s_h; h < (1 << nht->hash_shift); h++) {
  1962. if (h > s_h)
  1963. s_idx = 0;
  1964. for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
  1965. n != NULL;
  1966. n = rcu_dereference_bh(n->next)) {
  1967. if (!net_eq(dev_net(n->dev), net))
  1968. continue;
  1969. if (neigh_ifindex_filtered(n->dev, filter_idx))
  1970. continue;
  1971. if (neigh_master_filtered(n->dev, filter_master_idx))
  1972. continue;
  1973. if (idx < s_idx)
  1974. goto next;
  1975. if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
  1976. cb->nlh->nlmsg_seq,
  1977. RTM_NEWNEIGH,
  1978. flags) < 0) {
  1979. rc = -1;
  1980. goto out;
  1981. }
  1982. next:
  1983. idx++;
  1984. }
  1985. }
  1986. rc = skb->len;
  1987. out:
  1988. rcu_read_unlock_bh();
  1989. cb->args[1] = h;
  1990. cb->args[2] = idx;
  1991. return rc;
  1992. }
  1993. static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
  1994. struct netlink_callback *cb)
  1995. {
  1996. struct pneigh_entry *n;
  1997. struct net *net = sock_net(skb->sk);
  1998. int rc, h, s_h = cb->args[3];
  1999. int idx, s_idx = idx = cb->args[4];
  2000. read_lock_bh(&tbl->lock);
  2001. for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
  2002. if (h > s_h)
  2003. s_idx = 0;
  2004. for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
  2005. if (pneigh_net(n) != net)
  2006. continue;
  2007. if (idx < s_idx)
  2008. goto next;
  2009. if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
  2010. cb->nlh->nlmsg_seq,
  2011. RTM_NEWNEIGH,
  2012. NLM_F_MULTI, tbl) < 0) {
  2013. read_unlock_bh(&tbl->lock);
  2014. rc = -1;
  2015. goto out;
  2016. }
  2017. next:
  2018. idx++;
  2019. }
  2020. }
  2021. read_unlock_bh(&tbl->lock);
  2022. rc = skb->len;
  2023. out:
  2024. cb->args[3] = h;
  2025. cb->args[4] = idx;
  2026. return rc;
  2027. }
  2028. static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
  2029. {
  2030. struct neigh_table *tbl;
  2031. int t, family, s_t;
  2032. int proxy = 0;
  2033. int err;
  2034. family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
  2035. /* check for full ndmsg structure presence, family member is
  2036. * the same for both structures
  2037. */
  2038. if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) &&
  2039. ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY)
  2040. proxy = 1;
  2041. s_t = cb->args[0];
  2042. for (t = 0; t < NEIGH_NR_TABLES; t++) {
  2043. tbl = neigh_tables[t];
  2044. if (!tbl)
  2045. continue;
  2046. if (t < s_t || (family && tbl->family != family))
  2047. continue;
  2048. if (t > s_t)
  2049. memset(&cb->args[1], 0, sizeof(cb->args) -
  2050. sizeof(cb->args[0]));
  2051. if (proxy)
  2052. err = pneigh_dump_table(tbl, skb, cb);
  2053. else
  2054. err = neigh_dump_table(tbl, skb, cb);
  2055. if (err < 0)
  2056. break;
  2057. }
  2058. cb->args[0] = t;
  2059. return skb->len;
  2060. }
  2061. void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
  2062. {
  2063. int chain;
  2064. struct neigh_hash_table *nht;
  2065. rcu_read_lock_bh();
  2066. nht = rcu_dereference_bh(tbl->nht);
  2067. read_lock(&tbl->lock); /* avoid resizes */
  2068. for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
  2069. struct neighbour *n;
  2070. for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
  2071. n != NULL;
  2072. n = rcu_dereference_bh(n->next))
  2073. cb(n, cookie);
  2074. }
  2075. read_unlock(&tbl->lock);
  2076. rcu_read_unlock_bh();
  2077. }
  2078. EXPORT_SYMBOL(neigh_for_each);
  2079. /* The tbl->lock must be held as a writer and BH disabled. */
  2080. void __neigh_for_each_release(struct neigh_table *tbl,
  2081. int (*cb)(struct neighbour *))
  2082. {
  2083. int chain;
  2084. struct neigh_hash_table *nht;
  2085. nht = rcu_dereference_protected(tbl->nht,
  2086. lockdep_is_held(&tbl->lock));
  2087. for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
  2088. struct neighbour *n;
  2089. struct neighbour __rcu **np;
  2090. np = &nht->hash_buckets[chain];
  2091. while ((n = rcu_dereference_protected(*np,
  2092. lockdep_is_held(&tbl->lock))) != NULL) {
  2093. int release;
  2094. write_lock(&n->lock);
  2095. release = cb(n);
  2096. if (release) {
  2097. rcu_assign_pointer(*np,
  2098. rcu_dereference_protected(n->next,
  2099. lockdep_is_held(&tbl->lock)));
  2100. n->dead = 1;
  2101. } else
  2102. np = &n->next;
  2103. write_unlock(&n->lock);
  2104. if (release)
  2105. neigh_cleanup_and_release(n);
  2106. }
  2107. }
  2108. }
  2109. EXPORT_SYMBOL(__neigh_for_each_release);
  2110. int neigh_xmit(int index, struct net_device *dev,
  2111. const void *addr, struct sk_buff *skb)
  2112. {
  2113. int err = -EAFNOSUPPORT;
  2114. if (likely(index < NEIGH_NR_TABLES)) {
  2115. struct neigh_table *tbl;
  2116. struct neighbour *neigh;
  2117. tbl = neigh_tables[index];
  2118. if (!tbl)
  2119. goto out;
  2120. rcu_read_lock_bh();
  2121. neigh = __neigh_lookup_noref(tbl, addr, dev);
  2122. if (!neigh)
  2123. neigh = __neigh_create(tbl, addr, dev, false);
  2124. err = PTR_ERR(neigh);
  2125. if (IS_ERR(neigh)) {
  2126. rcu_read_unlock_bh();
  2127. goto out_kfree_skb;
  2128. }
  2129. err = neigh->output(neigh, skb);
  2130. rcu_read_unlock_bh();
  2131. }
  2132. else if (index == NEIGH_LINK_TABLE) {
  2133. err = dev_hard_header(skb, dev, ntohs(skb->protocol),
  2134. addr, NULL, skb->len);
  2135. if (err < 0)
  2136. goto out_kfree_skb;
  2137. err = dev_queue_xmit(skb);
  2138. }
  2139. out:
  2140. return err;
  2141. out_kfree_skb:
  2142. kfree_skb(skb);
  2143. goto out;
  2144. }
  2145. EXPORT_SYMBOL(neigh_xmit);
  2146. #ifdef CONFIG_PROC_FS
  2147. static struct neighbour *neigh_get_first(struct seq_file *seq)
  2148. {
  2149. struct neigh_seq_state *state = seq->private;
  2150. struct net *net = seq_file_net(seq);
  2151. struct neigh_hash_table *nht = state->nht;
  2152. struct neighbour *n = NULL;
  2153. int bucket = state->bucket;
  2154. state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
  2155. for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
  2156. n = rcu_dereference_bh(nht->hash_buckets[bucket]);
  2157. while (n) {
  2158. if (!net_eq(dev_net(n->dev), net))
  2159. goto next;
  2160. if (state->neigh_sub_iter) {
  2161. loff_t fakep = 0;
  2162. void *v;
  2163. v = state->neigh_sub_iter(state, n, &fakep);
  2164. if (!v)
  2165. goto next;
  2166. }
  2167. if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
  2168. break;
  2169. if (n->nud_state & ~NUD_NOARP)
  2170. break;
  2171. next:
  2172. n = rcu_dereference_bh(n->next);
  2173. }
  2174. if (n)
  2175. break;
  2176. }
  2177. state->bucket = bucket;
  2178. return n;
  2179. }
  2180. static struct neighbour *neigh_get_next(struct seq_file *seq,
  2181. struct neighbour *n,
  2182. loff_t *pos)
  2183. {
  2184. struct neigh_seq_state *state = seq->private;
  2185. struct net *net = seq_file_net(seq);
  2186. struct neigh_hash_table *nht = state->nht;
  2187. if (state->neigh_sub_iter) {
  2188. void *v = state->neigh_sub_iter(state, n, pos);
  2189. if (v)
  2190. return n;
  2191. }
  2192. n = rcu_dereference_bh(n->next);
  2193. while (1) {
  2194. while (n) {
  2195. if (!net_eq(dev_net(n->dev), net))
  2196. goto next;
  2197. if (state->neigh_sub_iter) {
  2198. void *v = state->neigh_sub_iter(state, n, pos);
  2199. if (v)
  2200. return n;
  2201. goto next;
  2202. }
  2203. if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
  2204. break;
  2205. if (n->nud_state & ~NUD_NOARP)
  2206. break;
  2207. next:
  2208. n = rcu_dereference_bh(n->next);
  2209. }
  2210. if (n)
  2211. break;
  2212. if (++state->bucket >= (1 << nht->hash_shift))
  2213. break;
  2214. n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
  2215. }
  2216. if (n && pos)
  2217. --(*pos);
  2218. return n;
  2219. }
  2220. static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
  2221. {
  2222. struct neighbour *n = neigh_get_first(seq);
  2223. if (n) {
  2224. --(*pos);
  2225. while (*pos) {
  2226. n = neigh_get_next(seq, n, pos);
  2227. if (!n)
  2228. break;
  2229. }
  2230. }
  2231. return *pos ? NULL : n;
  2232. }
  2233. static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
  2234. {
  2235. struct neigh_seq_state *state = seq->private;
  2236. struct net *net = seq_file_net(seq);
  2237. struct neigh_table *tbl = state->tbl;
  2238. struct pneigh_entry *pn = NULL;
  2239. int bucket = state->bucket;
  2240. state->flags |= NEIGH_SEQ_IS_PNEIGH;
  2241. for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
  2242. pn = tbl->phash_buckets[bucket];
  2243. while (pn && !net_eq(pneigh_net(pn), net))
  2244. pn = pn->next;
  2245. if (pn)
  2246. break;
  2247. }
  2248. state->bucket = bucket;
  2249. return pn;
  2250. }
  2251. static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
  2252. struct pneigh_entry *pn,
  2253. loff_t *pos)
  2254. {
  2255. struct neigh_seq_state *state = seq->private;
  2256. struct net *net = seq_file_net(seq);
  2257. struct neigh_table *tbl = state->tbl;
  2258. do {
  2259. pn = pn->next;
  2260. } while (pn && !net_eq(pneigh_net(pn), net));
  2261. while (!pn) {
  2262. if (++state->bucket > PNEIGH_HASHMASK)
  2263. break;
  2264. pn = tbl->phash_buckets[state->bucket];
  2265. while (pn && !net_eq(pneigh_net(pn), net))
  2266. pn = pn->next;
  2267. if (pn)
  2268. break;
  2269. }
  2270. if (pn && pos)
  2271. --(*pos);
  2272. return pn;
  2273. }
  2274. static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
  2275. {
  2276. struct pneigh_entry *pn = pneigh_get_first(seq);
  2277. if (pn) {
  2278. --(*pos);
  2279. while (*pos) {
  2280. pn = pneigh_get_next(seq, pn, pos);
  2281. if (!pn)
  2282. break;
  2283. }
  2284. }
  2285. return *pos ? NULL : pn;
  2286. }
  2287. static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
  2288. {
  2289. struct neigh_seq_state *state = seq->private;
  2290. void *rc;
  2291. loff_t idxpos = *pos;
  2292. rc = neigh_get_idx(seq, &idxpos);
  2293. if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
  2294. rc = pneigh_get_idx(seq, &idxpos);
  2295. return rc;
  2296. }
  2297. void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
  2298. __acquires(rcu_bh)
  2299. {
  2300. struct neigh_seq_state *state = seq->private;
  2301. state->tbl = tbl;
  2302. state->bucket = 0;
  2303. state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
  2304. rcu_read_lock_bh();
  2305. state->nht = rcu_dereference_bh(tbl->nht);
  2306. return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
  2307. }
  2308. EXPORT_SYMBOL(neigh_seq_start);
  2309. void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2310. {
  2311. struct neigh_seq_state *state;
  2312. void *rc;
  2313. if (v == SEQ_START_TOKEN) {
  2314. rc = neigh_get_first(seq);
  2315. goto out;
  2316. }
  2317. state = seq->private;
  2318. if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
  2319. rc = neigh_get_next(seq, v, NULL);
  2320. if (rc)
  2321. goto out;
  2322. if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
  2323. rc = pneigh_get_first(seq);
  2324. } else {
  2325. BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
  2326. rc = pneigh_get_next(seq, v, NULL);
  2327. }
  2328. out:
  2329. ++(*pos);
  2330. return rc;
  2331. }
  2332. EXPORT_SYMBOL(neigh_seq_next);
  2333. void neigh_seq_stop(struct seq_file *seq, void *v)
  2334. __releases(rcu_bh)
  2335. {
  2336. rcu_read_unlock_bh();
  2337. }
  2338. EXPORT_SYMBOL(neigh_seq_stop);
  2339. /* statistics via seq_file */
  2340. static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
  2341. {
  2342. struct neigh_table *tbl = seq->private;
  2343. int cpu;
  2344. if (*pos == 0)
  2345. return SEQ_START_TOKEN;
  2346. for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
  2347. if (!cpu_possible(cpu))
  2348. continue;
  2349. *pos = cpu+1;
  2350. return per_cpu_ptr(tbl->stats, cpu);
  2351. }
  2352. return NULL;
  2353. }
  2354. static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2355. {
  2356. struct neigh_table *tbl = seq->private;
  2357. int cpu;
  2358. for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
  2359. if (!cpu_possible(cpu))
  2360. continue;
  2361. *pos = cpu+1;
  2362. return per_cpu_ptr(tbl->stats, cpu);
  2363. }
  2364. return NULL;
  2365. }
  2366. static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
  2367. {
  2368. }
  2369. static int neigh_stat_seq_show(struct seq_file *seq, void *v)
  2370. {
  2371. struct neigh_table *tbl = seq->private;
  2372. struct neigh_statistics *st = v;
  2373. if (v == SEQ_START_TOKEN) {
  2374. seq_printf(seq, "entries allocs destroys hash_grows lookups hits res_failed rcv_probes_mcast rcv_probes_ucast periodic_gc_runs forced_gc_runs unresolved_discards table_fulls\n");
  2375. return 0;
  2376. }
  2377. seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
  2378. "%08lx %08lx %08lx %08lx %08lx %08lx\n",
  2379. atomic_read(&tbl->entries),
  2380. st->allocs,
  2381. st->destroys,
  2382. st->hash_grows,
  2383. st->lookups,
  2384. st->hits,
  2385. st->res_failed,
  2386. st->rcv_probes_mcast,
  2387. st->rcv_probes_ucast,
  2388. st->periodic_gc_runs,
  2389. st->forced_gc_runs,
  2390. st->unres_discards,
  2391. st->table_fulls
  2392. );
  2393. return 0;
  2394. }
  2395. static const struct seq_operations neigh_stat_seq_ops = {
  2396. .start = neigh_stat_seq_start,
  2397. .next = neigh_stat_seq_next,
  2398. .stop = neigh_stat_seq_stop,
  2399. .show = neigh_stat_seq_show,
  2400. };
  2401. static int neigh_stat_seq_open(struct inode *inode, struct file *file)
  2402. {
  2403. int ret = seq_open(file, &neigh_stat_seq_ops);
  2404. if (!ret) {
  2405. struct seq_file *sf = file->private_data;
  2406. sf->private = PDE_DATA(inode);
  2407. }
  2408. return ret;
  2409. };
  2410. static const struct file_operations neigh_stat_seq_fops = {
  2411. .owner = THIS_MODULE,
  2412. .open = neigh_stat_seq_open,
  2413. .read = seq_read,
  2414. .llseek = seq_lseek,
  2415. .release = seq_release,
  2416. };
  2417. #endif /* CONFIG_PROC_FS */
  2418. static inline size_t neigh_nlmsg_size(void)
  2419. {
  2420. return NLMSG_ALIGN(sizeof(struct ndmsg))
  2421. + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
  2422. + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
  2423. + nla_total_size(sizeof(struct nda_cacheinfo))
  2424. + nla_total_size(4); /* NDA_PROBES */
  2425. }
  2426. static void __neigh_notify(struct neighbour *n, int type, int flags)
  2427. {
  2428. struct net *net = dev_net(n->dev);
  2429. struct sk_buff *skb;
  2430. int err = -ENOBUFS;
  2431. skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
  2432. if (skb == NULL)
  2433. goto errout;
  2434. err = neigh_fill_info(skb, n, 0, 0, type, flags);
  2435. if (err < 0) {
  2436. /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
  2437. WARN_ON(err == -EMSGSIZE);
  2438. kfree_skb(skb);
  2439. goto errout;
  2440. }
  2441. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  2442. return;
  2443. errout:
  2444. if (err < 0)
  2445. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  2446. }
  2447. void neigh_app_ns(struct neighbour *n)
  2448. {
  2449. __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST);
  2450. }
  2451. EXPORT_SYMBOL(neigh_app_ns);
  2452. #ifdef CONFIG_SYSCTL
  2453. static int zero;
  2454. static int int_max = INT_MAX;
  2455. static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
  2456. static int proc_unres_qlen(struct ctl_table *ctl, int write,
  2457. void __user *buffer, size_t *lenp, loff_t *ppos)
  2458. {
  2459. int size, ret;
  2460. struct ctl_table tmp = *ctl;
  2461. tmp.extra1 = &zero;
  2462. tmp.extra2 = &unres_qlen_max;
  2463. tmp.data = &size;
  2464. size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
  2465. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  2466. if (write && !ret)
  2467. *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
  2468. return ret;
  2469. }
  2470. static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
  2471. int family)
  2472. {
  2473. switch (family) {
  2474. case AF_INET:
  2475. return __in_dev_arp_parms_get_rcu(dev);
  2476. case AF_INET6:
  2477. return __in6_dev_nd_parms_get_rcu(dev);
  2478. }
  2479. return NULL;
  2480. }
  2481. static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
  2482. int index)
  2483. {
  2484. struct net_device *dev;
  2485. int family = neigh_parms_family(p);
  2486. rcu_read_lock();
  2487. for_each_netdev_rcu(net, dev) {
  2488. struct neigh_parms *dst_p =
  2489. neigh_get_dev_parms_rcu(dev, family);
  2490. if (dst_p && !test_bit(index, dst_p->data_state))
  2491. dst_p->data[index] = p->data[index];
  2492. }
  2493. rcu_read_unlock();
  2494. }
  2495. static void neigh_proc_update(struct ctl_table *ctl, int write)
  2496. {
  2497. struct net_device *dev = ctl->extra1;
  2498. struct neigh_parms *p = ctl->extra2;
  2499. struct net *net = neigh_parms_net(p);
  2500. int index = (int *) ctl->data - p->data;
  2501. if (!write)
  2502. return;
  2503. set_bit(index, p->data_state);
  2504. if (!dev) /* NULL dev means this is default value */
  2505. neigh_copy_dflt_parms(net, p, index);
  2506. }
  2507. static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
  2508. void __user *buffer,
  2509. size_t *lenp, loff_t *ppos)
  2510. {
  2511. struct ctl_table tmp = *ctl;
  2512. int ret;
  2513. tmp.extra1 = &zero;
  2514. tmp.extra2 = &int_max;
  2515. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  2516. neigh_proc_update(ctl, write);
  2517. return ret;
  2518. }
  2519. int neigh_proc_dointvec(struct ctl_table *ctl, int write,
  2520. void __user *buffer, size_t *lenp, loff_t *ppos)
  2521. {
  2522. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  2523. neigh_proc_update(ctl, write);
  2524. return ret;
  2525. }
  2526. EXPORT_SYMBOL(neigh_proc_dointvec);
  2527. int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
  2528. void __user *buffer,
  2529. size_t *lenp, loff_t *ppos)
  2530. {
  2531. int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
  2532. neigh_proc_update(ctl, write);
  2533. return ret;
  2534. }
  2535. EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
  2536. static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
  2537. void __user *buffer,
  2538. size_t *lenp, loff_t *ppos)
  2539. {
  2540. int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
  2541. neigh_proc_update(ctl, write);
  2542. return ret;
  2543. }
  2544. int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
  2545. void __user *buffer,
  2546. size_t *lenp, loff_t *ppos)
  2547. {
  2548. int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
  2549. neigh_proc_update(ctl, write);
  2550. return ret;
  2551. }
  2552. EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
  2553. static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
  2554. void __user *buffer,
  2555. size_t *lenp, loff_t *ppos)
  2556. {
  2557. int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
  2558. neigh_proc_update(ctl, write);
  2559. return ret;
  2560. }
  2561. static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
  2562. void __user *buffer,
  2563. size_t *lenp, loff_t *ppos)
  2564. {
  2565. struct neigh_parms *p = ctl->extra2;
  2566. int ret;
  2567. if (strcmp(ctl->procname, "base_reachable_time") == 0)
  2568. ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
  2569. else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
  2570. ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
  2571. else
  2572. ret = -1;
  2573. if (write && ret == 0) {
  2574. /* update reachable_time as well, otherwise, the change will
  2575. * only be effective after the next time neigh_periodic_work
  2576. * decides to recompute it
  2577. */
  2578. p->reachable_time =
  2579. neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
  2580. }
  2581. return ret;
  2582. }
  2583. #define NEIGH_PARMS_DATA_OFFSET(index) \
  2584. (&((struct neigh_parms *) 0)->data[index])
  2585. #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
  2586. [NEIGH_VAR_ ## attr] = { \
  2587. .procname = name, \
  2588. .data = NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
  2589. .maxlen = sizeof(int), \
  2590. .mode = mval, \
  2591. .proc_handler = proc, \
  2592. }
  2593. #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
  2594. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
  2595. #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
  2596. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
  2597. #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
  2598. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
  2599. #define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \
  2600. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
  2601. #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
  2602. NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
  2603. #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
  2604. NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
  2605. static struct neigh_sysctl_table {
  2606. struct ctl_table_header *sysctl_header;
  2607. struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
  2608. } neigh_sysctl_template __read_mostly = {
  2609. .neigh_vars = {
  2610. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
  2611. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
  2612. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
  2613. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
  2614. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
  2615. NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
  2616. NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
  2617. NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
  2618. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
  2619. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
  2620. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
  2621. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
  2622. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
  2623. NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
  2624. NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
  2625. NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
  2626. [NEIGH_VAR_GC_INTERVAL] = {
  2627. .procname = "gc_interval",
  2628. .maxlen = sizeof(int),
  2629. .mode = 0644,
  2630. .proc_handler = proc_dointvec_jiffies,
  2631. },
  2632. [NEIGH_VAR_GC_THRESH1] = {
  2633. .procname = "gc_thresh1",
  2634. .maxlen = sizeof(int),
  2635. .mode = 0644,
  2636. .extra1 = &zero,
  2637. .extra2 = &int_max,
  2638. .proc_handler = proc_dointvec_minmax,
  2639. },
  2640. [NEIGH_VAR_GC_THRESH2] = {
  2641. .procname = "gc_thresh2",
  2642. .maxlen = sizeof(int),
  2643. .mode = 0644,
  2644. .extra1 = &zero,
  2645. .extra2 = &int_max,
  2646. .proc_handler = proc_dointvec_minmax,
  2647. },
  2648. [NEIGH_VAR_GC_THRESH3] = {
  2649. .procname = "gc_thresh3",
  2650. .maxlen = sizeof(int),
  2651. .mode = 0644,
  2652. .extra1 = &zero,
  2653. .extra2 = &int_max,
  2654. .proc_handler = proc_dointvec_minmax,
  2655. },
  2656. {},
  2657. },
  2658. };
  2659. int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
  2660. proc_handler *handler)
  2661. {
  2662. int i;
  2663. struct neigh_sysctl_table *t;
  2664. const char *dev_name_source;
  2665. char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
  2666. char *p_name;
  2667. t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
  2668. if (!t)
  2669. goto err;
  2670. for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
  2671. t->neigh_vars[i].data += (long) p;
  2672. t->neigh_vars[i].extra1 = dev;
  2673. t->neigh_vars[i].extra2 = p;
  2674. }
  2675. if (dev) {
  2676. dev_name_source = dev->name;
  2677. /* Terminate the table early */
  2678. memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
  2679. sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
  2680. } else {
  2681. struct neigh_table *tbl = p->tbl;
  2682. dev_name_source = "default";
  2683. t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
  2684. t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
  2685. t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
  2686. t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
  2687. }
  2688. if (handler) {
  2689. /* RetransTime */
  2690. t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
  2691. /* ReachableTime */
  2692. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
  2693. /* RetransTime (in milliseconds)*/
  2694. t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
  2695. /* ReachableTime (in milliseconds) */
  2696. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
  2697. } else {
  2698. /* Those handlers will update p->reachable_time after
  2699. * base_reachable_time(_ms) is set to ensure the new timer starts being
  2700. * applied after the next neighbour update instead of waiting for
  2701. * neigh_periodic_work to update its value (can be multiple minutes)
  2702. * So any handler that replaces them should do this as well
  2703. */
  2704. /* ReachableTime */
  2705. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
  2706. neigh_proc_base_reachable_time;
  2707. /* ReachableTime (in milliseconds) */
  2708. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
  2709. neigh_proc_base_reachable_time;
  2710. }
  2711. /* Don't export sysctls to unprivileged users */
  2712. if (neigh_parms_net(p)->user_ns != &init_user_ns)
  2713. t->neigh_vars[0].procname = NULL;
  2714. switch (neigh_parms_family(p)) {
  2715. case AF_INET:
  2716. p_name = "ipv4";
  2717. break;
  2718. case AF_INET6:
  2719. p_name = "ipv6";
  2720. break;
  2721. default:
  2722. BUG();
  2723. }
  2724. snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
  2725. p_name, dev_name_source);
  2726. t->sysctl_header =
  2727. register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
  2728. if (!t->sysctl_header)
  2729. goto free;
  2730. p->sysctl_table = t;
  2731. return 0;
  2732. free:
  2733. kfree(t);
  2734. err:
  2735. return -ENOBUFS;
  2736. }
  2737. EXPORT_SYMBOL(neigh_sysctl_register);
  2738. void neigh_sysctl_unregister(struct neigh_parms *p)
  2739. {
  2740. if (p->sysctl_table) {
  2741. struct neigh_sysctl_table *t = p->sysctl_table;
  2742. p->sysctl_table = NULL;
  2743. unregister_net_sysctl_table(t->sysctl_header);
  2744. kfree(t);
  2745. }
  2746. }
  2747. EXPORT_SYMBOL(neigh_sysctl_unregister);
  2748. #endif /* CONFIG_SYSCTL */
  2749. static int __init neigh_init(void)
  2750. {
  2751. rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, NULL);
  2752. rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, NULL);
  2753. rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, NULL);
  2754. rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
  2755. NULL);
  2756. rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, NULL);
  2757. return 0;
  2758. }
  2759. subsys_initcall(neigh_init);