neighbour.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249
  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(dev, 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(dev, 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. NEIGH_UPDATE_F_ADMIN means that the change is administrative.
  916. NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
  917. NTF_ROUTER flag.
  918. NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
  919. a router.
  920. Caller MUST hold reference count on the entry.
  921. */
  922. int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
  923. u32 flags)
  924. {
  925. u8 old;
  926. int err;
  927. int notify = 0;
  928. struct net_device *dev;
  929. int update_isrouter = 0;
  930. write_lock_bh(&neigh->lock);
  931. dev = neigh->dev;
  932. old = neigh->nud_state;
  933. err = -EPERM;
  934. if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
  935. (old & (NUD_NOARP | NUD_PERMANENT)))
  936. goto out;
  937. if (neigh->dead)
  938. goto out;
  939. if (!(new & NUD_VALID)) {
  940. neigh_del_timer(neigh);
  941. if (old & NUD_CONNECTED)
  942. neigh_suspect(neigh);
  943. neigh->nud_state = new;
  944. err = 0;
  945. notify = old & NUD_VALID;
  946. if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
  947. (new & NUD_FAILED)) {
  948. neigh_invalidate(neigh);
  949. notify = 1;
  950. }
  951. goto out;
  952. }
  953. /* Compare new lladdr with cached one */
  954. if (!dev->addr_len) {
  955. /* First case: device needs no address. */
  956. lladdr = neigh->ha;
  957. } else if (lladdr) {
  958. /* The second case: if something is already cached
  959. and a new address is proposed:
  960. - compare new & old
  961. - if they are different, check override flag
  962. */
  963. if ((old & NUD_VALID) &&
  964. !memcmp(lladdr, neigh->ha, dev->addr_len))
  965. lladdr = neigh->ha;
  966. } else {
  967. /* No address is supplied; if we know something,
  968. use it, otherwise discard the request.
  969. */
  970. err = -EINVAL;
  971. if (!(old & NUD_VALID))
  972. goto out;
  973. lladdr = neigh->ha;
  974. }
  975. /* If entry was valid and address is not changed,
  976. do not change entry state, if new one is STALE.
  977. */
  978. err = 0;
  979. update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
  980. if (old & NUD_VALID) {
  981. if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
  982. update_isrouter = 0;
  983. if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
  984. (old & NUD_CONNECTED)) {
  985. lladdr = neigh->ha;
  986. new = NUD_STALE;
  987. } else
  988. goto out;
  989. } else {
  990. if (lladdr == neigh->ha && new == NUD_STALE &&
  991. !(flags & NEIGH_UPDATE_F_ADMIN))
  992. new = old;
  993. }
  994. }
  995. /* Update timestamps only once we know we will make a change to the
  996. * neighbour entry. Otherwise we risk to move the locktime window with
  997. * noop updates and ignore relevant ARP updates.
  998. */
  999. if (new != old || lladdr != neigh->ha) {
  1000. if (new & NUD_CONNECTED)
  1001. neigh->confirmed = jiffies;
  1002. neigh->updated = jiffies;
  1003. }
  1004. if (new != old) {
  1005. neigh_del_timer(neigh);
  1006. if (new & NUD_PROBE)
  1007. atomic_set(&neigh->probes, 0);
  1008. if (new & NUD_IN_TIMER)
  1009. neigh_add_timer(neigh, (jiffies +
  1010. ((new & NUD_REACHABLE) ?
  1011. neigh->parms->reachable_time :
  1012. 0)));
  1013. neigh->nud_state = new;
  1014. notify = 1;
  1015. }
  1016. if (lladdr != neigh->ha) {
  1017. write_seqlock(&neigh->ha_lock);
  1018. memcpy(&neigh->ha, lladdr, dev->addr_len);
  1019. write_sequnlock(&neigh->ha_lock);
  1020. neigh_update_hhs(neigh);
  1021. if (!(new & NUD_CONNECTED))
  1022. neigh->confirmed = jiffies -
  1023. (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
  1024. notify = 1;
  1025. }
  1026. if (new == old)
  1027. goto out;
  1028. if (new & NUD_CONNECTED)
  1029. neigh_connect(neigh);
  1030. else
  1031. neigh_suspect(neigh);
  1032. if (!(old & NUD_VALID)) {
  1033. struct sk_buff *skb;
  1034. /* Again: avoid dead loop if something went wrong */
  1035. while (neigh->nud_state & NUD_VALID &&
  1036. (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
  1037. struct dst_entry *dst = skb_dst(skb);
  1038. struct neighbour *n2, *n1 = neigh;
  1039. write_unlock_bh(&neigh->lock);
  1040. rcu_read_lock();
  1041. /* Why not just use 'neigh' as-is? The problem is that
  1042. * things such as shaper, eql, and sch_teql can end up
  1043. * using alternative, different, neigh objects to output
  1044. * the packet in the output path. So what we need to do
  1045. * here is re-lookup the top-level neigh in the path so
  1046. * we can reinject the packet there.
  1047. */
  1048. n2 = NULL;
  1049. if (dst) {
  1050. n2 = dst_neigh_lookup_skb(dst, skb);
  1051. if (n2)
  1052. n1 = n2;
  1053. }
  1054. n1->output(n1, skb);
  1055. if (n2)
  1056. neigh_release(n2);
  1057. rcu_read_unlock();
  1058. write_lock_bh(&neigh->lock);
  1059. }
  1060. __skb_queue_purge(&neigh->arp_queue);
  1061. neigh->arp_queue_len_bytes = 0;
  1062. }
  1063. out:
  1064. if (update_isrouter) {
  1065. neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
  1066. (neigh->flags | NTF_ROUTER) :
  1067. (neigh->flags & ~NTF_ROUTER);
  1068. }
  1069. write_unlock_bh(&neigh->lock);
  1070. if (notify)
  1071. neigh_update_notify(neigh);
  1072. return err;
  1073. }
  1074. EXPORT_SYMBOL(neigh_update);
  1075. /* Update the neigh to listen temporarily for probe responses, even if it is
  1076. * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
  1077. */
  1078. void __neigh_set_probe_once(struct neighbour *neigh)
  1079. {
  1080. if (neigh->dead)
  1081. return;
  1082. neigh->updated = jiffies;
  1083. if (!(neigh->nud_state & NUD_FAILED))
  1084. return;
  1085. neigh->nud_state = NUD_INCOMPLETE;
  1086. atomic_set(&neigh->probes, neigh_max_probes(neigh));
  1087. neigh_add_timer(neigh,
  1088. jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME));
  1089. }
  1090. EXPORT_SYMBOL(__neigh_set_probe_once);
  1091. struct neighbour *neigh_event_ns(struct neigh_table *tbl,
  1092. u8 *lladdr, void *saddr,
  1093. struct net_device *dev)
  1094. {
  1095. struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
  1096. lladdr || !dev->addr_len);
  1097. if (neigh)
  1098. neigh_update(neigh, lladdr, NUD_STALE,
  1099. NEIGH_UPDATE_F_OVERRIDE);
  1100. return neigh;
  1101. }
  1102. EXPORT_SYMBOL(neigh_event_ns);
  1103. /* called with read_lock_bh(&n->lock); */
  1104. static void neigh_hh_init(struct neighbour *n)
  1105. {
  1106. struct net_device *dev = n->dev;
  1107. __be16 prot = n->tbl->protocol;
  1108. struct hh_cache *hh = &n->hh;
  1109. write_lock_bh(&n->lock);
  1110. /* Only one thread can come in here and initialize the
  1111. * hh_cache entry.
  1112. */
  1113. if (!hh->hh_len)
  1114. dev->header_ops->cache(n, hh, prot);
  1115. write_unlock_bh(&n->lock);
  1116. }
  1117. /* Slow and careful. */
  1118. int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
  1119. {
  1120. int rc = 0;
  1121. if (!neigh_event_send(neigh, skb)) {
  1122. int err;
  1123. struct net_device *dev = neigh->dev;
  1124. unsigned int seq;
  1125. if (dev->header_ops->cache && !neigh->hh.hh_len)
  1126. neigh_hh_init(neigh);
  1127. do {
  1128. __skb_pull(skb, skb_network_offset(skb));
  1129. seq = read_seqbegin(&neigh->ha_lock);
  1130. err = dev_hard_header(skb, dev, ntohs(skb->protocol),
  1131. neigh->ha, NULL, skb->len);
  1132. } while (read_seqretry(&neigh->ha_lock, seq));
  1133. if (err >= 0)
  1134. rc = dev_queue_xmit(skb);
  1135. else
  1136. goto out_kfree_skb;
  1137. }
  1138. out:
  1139. return rc;
  1140. out_kfree_skb:
  1141. rc = -EINVAL;
  1142. kfree_skb(skb);
  1143. goto out;
  1144. }
  1145. EXPORT_SYMBOL(neigh_resolve_output);
  1146. /* As fast as possible without hh cache */
  1147. int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
  1148. {
  1149. struct net_device *dev = neigh->dev;
  1150. unsigned int seq;
  1151. int err;
  1152. do {
  1153. __skb_pull(skb, skb_network_offset(skb));
  1154. seq = read_seqbegin(&neigh->ha_lock);
  1155. err = dev_hard_header(skb, dev, ntohs(skb->protocol),
  1156. neigh->ha, NULL, skb->len);
  1157. } while (read_seqretry(&neigh->ha_lock, seq));
  1158. if (err >= 0)
  1159. err = dev_queue_xmit(skb);
  1160. else {
  1161. err = -EINVAL;
  1162. kfree_skb(skb);
  1163. }
  1164. return err;
  1165. }
  1166. EXPORT_SYMBOL(neigh_connected_output);
  1167. int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
  1168. {
  1169. return dev_queue_xmit(skb);
  1170. }
  1171. EXPORT_SYMBOL(neigh_direct_output);
  1172. static void neigh_proxy_process(unsigned long arg)
  1173. {
  1174. struct neigh_table *tbl = (struct neigh_table *)arg;
  1175. long sched_next = 0;
  1176. unsigned long now = jiffies;
  1177. struct sk_buff *skb, *n;
  1178. spin_lock(&tbl->proxy_queue.lock);
  1179. skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
  1180. long tdif = NEIGH_CB(skb)->sched_next - now;
  1181. if (tdif <= 0) {
  1182. struct net_device *dev = skb->dev;
  1183. __skb_unlink(skb, &tbl->proxy_queue);
  1184. if (tbl->proxy_redo && netif_running(dev)) {
  1185. rcu_read_lock();
  1186. tbl->proxy_redo(skb);
  1187. rcu_read_unlock();
  1188. } else {
  1189. kfree_skb(skb);
  1190. }
  1191. dev_put(dev);
  1192. } else if (!sched_next || tdif < sched_next)
  1193. sched_next = tdif;
  1194. }
  1195. del_timer(&tbl->proxy_timer);
  1196. if (sched_next)
  1197. mod_timer(&tbl->proxy_timer, jiffies + sched_next);
  1198. spin_unlock(&tbl->proxy_queue.lock);
  1199. }
  1200. void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
  1201. struct sk_buff *skb)
  1202. {
  1203. unsigned long now = jiffies;
  1204. unsigned long sched_next = now + (prandom_u32() %
  1205. NEIGH_VAR(p, PROXY_DELAY));
  1206. if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) {
  1207. kfree_skb(skb);
  1208. return;
  1209. }
  1210. NEIGH_CB(skb)->sched_next = sched_next;
  1211. NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
  1212. spin_lock(&tbl->proxy_queue.lock);
  1213. if (del_timer(&tbl->proxy_timer)) {
  1214. if (time_before(tbl->proxy_timer.expires, sched_next))
  1215. sched_next = tbl->proxy_timer.expires;
  1216. }
  1217. skb_dst_drop(skb);
  1218. dev_hold(skb->dev);
  1219. __skb_queue_tail(&tbl->proxy_queue, skb);
  1220. mod_timer(&tbl->proxy_timer, sched_next);
  1221. spin_unlock(&tbl->proxy_queue.lock);
  1222. }
  1223. EXPORT_SYMBOL(pneigh_enqueue);
  1224. static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
  1225. struct net *net, int ifindex)
  1226. {
  1227. struct neigh_parms *p;
  1228. list_for_each_entry(p, &tbl->parms_list, list) {
  1229. if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
  1230. (!p->dev && !ifindex && net_eq(net, &init_net)))
  1231. return p;
  1232. }
  1233. return NULL;
  1234. }
  1235. struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
  1236. struct neigh_table *tbl)
  1237. {
  1238. struct neigh_parms *p;
  1239. struct net *net = dev_net(dev);
  1240. const struct net_device_ops *ops = dev->netdev_ops;
  1241. p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
  1242. if (p) {
  1243. p->tbl = tbl;
  1244. atomic_set(&p->refcnt, 1);
  1245. p->reachable_time =
  1246. neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
  1247. dev_hold(dev);
  1248. p->dev = dev;
  1249. write_pnet(&p->net, net);
  1250. p->sysctl_table = NULL;
  1251. if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
  1252. dev_put(dev);
  1253. kfree(p);
  1254. return NULL;
  1255. }
  1256. write_lock_bh(&tbl->lock);
  1257. list_add(&p->list, &tbl->parms.list);
  1258. write_unlock_bh(&tbl->lock);
  1259. neigh_parms_data_state_cleanall(p);
  1260. }
  1261. return p;
  1262. }
  1263. EXPORT_SYMBOL(neigh_parms_alloc);
  1264. static void neigh_rcu_free_parms(struct rcu_head *head)
  1265. {
  1266. struct neigh_parms *parms =
  1267. container_of(head, struct neigh_parms, rcu_head);
  1268. neigh_parms_put(parms);
  1269. }
  1270. void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
  1271. {
  1272. if (!parms || parms == &tbl->parms)
  1273. return;
  1274. write_lock_bh(&tbl->lock);
  1275. list_del(&parms->list);
  1276. parms->dead = 1;
  1277. write_unlock_bh(&tbl->lock);
  1278. if (parms->dev)
  1279. dev_put(parms->dev);
  1280. call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
  1281. }
  1282. EXPORT_SYMBOL(neigh_parms_release);
  1283. static void neigh_parms_destroy(struct neigh_parms *parms)
  1284. {
  1285. kfree(parms);
  1286. }
  1287. static struct lock_class_key neigh_table_proxy_queue_class;
  1288. static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
  1289. void neigh_table_init(int index, struct neigh_table *tbl)
  1290. {
  1291. unsigned long now = jiffies;
  1292. unsigned long phsize;
  1293. INIT_LIST_HEAD(&tbl->parms_list);
  1294. list_add(&tbl->parms.list, &tbl->parms_list);
  1295. write_pnet(&tbl->parms.net, &init_net);
  1296. atomic_set(&tbl->parms.refcnt, 1);
  1297. tbl->parms.reachable_time =
  1298. neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME));
  1299. tbl->stats = alloc_percpu(struct neigh_statistics);
  1300. if (!tbl->stats)
  1301. panic("cannot create neighbour cache statistics");
  1302. #ifdef CONFIG_PROC_FS
  1303. if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat,
  1304. &neigh_stat_seq_fops, tbl))
  1305. panic("cannot create neighbour proc dir entry");
  1306. #endif
  1307. RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
  1308. phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
  1309. tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
  1310. if (!tbl->nht || !tbl->phash_buckets)
  1311. panic("cannot allocate neighbour cache hashes");
  1312. if (!tbl->entry_size)
  1313. tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
  1314. tbl->key_len, NEIGH_PRIV_ALIGN);
  1315. else
  1316. WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
  1317. rwlock_init(&tbl->lock);
  1318. INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
  1319. queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
  1320. tbl->parms.reachable_time);
  1321. setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl);
  1322. skb_queue_head_init_class(&tbl->proxy_queue,
  1323. &neigh_table_proxy_queue_class);
  1324. tbl->last_flush = now;
  1325. tbl->last_rand = now + tbl->parms.reachable_time * 20;
  1326. neigh_tables[index] = tbl;
  1327. }
  1328. EXPORT_SYMBOL(neigh_table_init);
  1329. int neigh_table_clear(int index, struct neigh_table *tbl)
  1330. {
  1331. neigh_tables[index] = NULL;
  1332. /* It is not clean... Fix it to unload IPv6 module safely */
  1333. cancel_delayed_work_sync(&tbl->gc_work);
  1334. del_timer_sync(&tbl->proxy_timer);
  1335. pneigh_queue_purge(&tbl->proxy_queue);
  1336. neigh_ifdown(tbl, NULL);
  1337. if (atomic_read(&tbl->entries))
  1338. pr_crit("neighbour leakage\n");
  1339. call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
  1340. neigh_hash_free_rcu);
  1341. tbl->nht = NULL;
  1342. kfree(tbl->phash_buckets);
  1343. tbl->phash_buckets = NULL;
  1344. remove_proc_entry(tbl->id, init_net.proc_net_stat);
  1345. free_percpu(tbl->stats);
  1346. tbl->stats = NULL;
  1347. return 0;
  1348. }
  1349. EXPORT_SYMBOL(neigh_table_clear);
  1350. static struct neigh_table *neigh_find_table(int family)
  1351. {
  1352. struct neigh_table *tbl = NULL;
  1353. switch (family) {
  1354. case AF_INET:
  1355. tbl = neigh_tables[NEIGH_ARP_TABLE];
  1356. break;
  1357. case AF_INET6:
  1358. tbl = neigh_tables[NEIGH_ND_TABLE];
  1359. break;
  1360. case AF_DECnet:
  1361. tbl = neigh_tables[NEIGH_DN_TABLE];
  1362. break;
  1363. }
  1364. return tbl;
  1365. }
  1366. static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh)
  1367. {
  1368. struct net *net = sock_net(skb->sk);
  1369. struct ndmsg *ndm;
  1370. struct nlattr *dst_attr;
  1371. struct neigh_table *tbl;
  1372. struct neighbour *neigh;
  1373. struct net_device *dev = NULL;
  1374. int err = -EINVAL;
  1375. ASSERT_RTNL();
  1376. if (nlmsg_len(nlh) < sizeof(*ndm))
  1377. goto out;
  1378. dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
  1379. if (dst_attr == NULL)
  1380. goto out;
  1381. ndm = nlmsg_data(nlh);
  1382. if (ndm->ndm_ifindex) {
  1383. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  1384. if (dev == NULL) {
  1385. err = -ENODEV;
  1386. goto out;
  1387. }
  1388. }
  1389. tbl = neigh_find_table(ndm->ndm_family);
  1390. if (tbl == NULL)
  1391. return -EAFNOSUPPORT;
  1392. if (nla_len(dst_attr) < tbl->key_len)
  1393. goto out;
  1394. if (ndm->ndm_flags & NTF_PROXY) {
  1395. err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
  1396. goto out;
  1397. }
  1398. if (dev == NULL)
  1399. goto out;
  1400. neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
  1401. if (neigh == NULL) {
  1402. err = -ENOENT;
  1403. goto out;
  1404. }
  1405. err = neigh_update(neigh, NULL, NUD_FAILED,
  1406. NEIGH_UPDATE_F_OVERRIDE |
  1407. NEIGH_UPDATE_F_ADMIN);
  1408. neigh_release(neigh);
  1409. out:
  1410. return err;
  1411. }
  1412. static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh)
  1413. {
  1414. int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
  1415. struct net *net = sock_net(skb->sk);
  1416. struct ndmsg *ndm;
  1417. struct nlattr *tb[NDA_MAX+1];
  1418. struct neigh_table *tbl;
  1419. struct net_device *dev = NULL;
  1420. struct neighbour *neigh;
  1421. void *dst, *lladdr;
  1422. int err;
  1423. ASSERT_RTNL();
  1424. err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
  1425. if (err < 0)
  1426. goto out;
  1427. err = -EINVAL;
  1428. if (tb[NDA_DST] == NULL)
  1429. goto out;
  1430. ndm = nlmsg_data(nlh);
  1431. if (ndm->ndm_ifindex) {
  1432. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  1433. if (dev == NULL) {
  1434. err = -ENODEV;
  1435. goto out;
  1436. }
  1437. if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len)
  1438. goto out;
  1439. }
  1440. tbl = neigh_find_table(ndm->ndm_family);
  1441. if (tbl == NULL)
  1442. return -EAFNOSUPPORT;
  1443. if (nla_len(tb[NDA_DST]) < tbl->key_len)
  1444. goto out;
  1445. dst = nla_data(tb[NDA_DST]);
  1446. lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
  1447. if (ndm->ndm_flags & NTF_PROXY) {
  1448. struct pneigh_entry *pn;
  1449. err = -ENOBUFS;
  1450. pn = pneigh_lookup(tbl, net, dst, dev, 1);
  1451. if (pn) {
  1452. pn->flags = ndm->ndm_flags;
  1453. err = 0;
  1454. }
  1455. goto out;
  1456. }
  1457. if (dev == NULL)
  1458. goto out;
  1459. neigh = neigh_lookup(tbl, dst, dev);
  1460. if (neigh == NULL) {
  1461. if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
  1462. err = -ENOENT;
  1463. goto out;
  1464. }
  1465. neigh = __neigh_lookup_errno(tbl, dst, dev);
  1466. if (IS_ERR(neigh)) {
  1467. err = PTR_ERR(neigh);
  1468. goto out;
  1469. }
  1470. } else {
  1471. if (nlh->nlmsg_flags & NLM_F_EXCL) {
  1472. err = -EEXIST;
  1473. neigh_release(neigh);
  1474. goto out;
  1475. }
  1476. if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
  1477. flags &= ~NEIGH_UPDATE_F_OVERRIDE;
  1478. }
  1479. if (ndm->ndm_flags & NTF_USE) {
  1480. neigh_event_send(neigh, NULL);
  1481. err = 0;
  1482. } else
  1483. err = neigh_update(neigh, lladdr, ndm->ndm_state, flags);
  1484. neigh_release(neigh);
  1485. out:
  1486. return err;
  1487. }
  1488. static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
  1489. {
  1490. struct nlattr *nest;
  1491. nest = nla_nest_start(skb, NDTA_PARMS);
  1492. if (nest == NULL)
  1493. return -ENOBUFS;
  1494. if ((parms->dev &&
  1495. nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) ||
  1496. nla_put_u32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)) ||
  1497. nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
  1498. NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
  1499. /* approximative value for deprecated QUEUE_LEN (in packets) */
  1500. nla_put_u32(skb, NDTPA_QUEUE_LEN,
  1501. NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
  1502. nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
  1503. nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
  1504. nla_put_u32(skb, NDTPA_UCAST_PROBES,
  1505. NEIGH_VAR(parms, UCAST_PROBES)) ||
  1506. nla_put_u32(skb, NDTPA_MCAST_PROBES,
  1507. NEIGH_VAR(parms, MCAST_PROBES)) ||
  1508. nla_put_u32(skb, NDTPA_MCAST_REPROBES,
  1509. NEIGH_VAR(parms, MCAST_REPROBES)) ||
  1510. nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time,
  1511. NDTPA_PAD) ||
  1512. nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
  1513. NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
  1514. nla_put_msecs(skb, NDTPA_GC_STALETIME,
  1515. NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
  1516. nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
  1517. NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
  1518. nla_put_msecs(skb, NDTPA_RETRANS_TIME,
  1519. NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
  1520. nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
  1521. NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
  1522. nla_put_msecs(skb, NDTPA_PROXY_DELAY,
  1523. NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
  1524. nla_put_msecs(skb, NDTPA_LOCKTIME,
  1525. NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD))
  1526. goto nla_put_failure;
  1527. return nla_nest_end(skb, nest);
  1528. nla_put_failure:
  1529. nla_nest_cancel(skb, nest);
  1530. return -EMSGSIZE;
  1531. }
  1532. static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
  1533. u32 pid, u32 seq, int type, int flags)
  1534. {
  1535. struct nlmsghdr *nlh;
  1536. struct ndtmsg *ndtmsg;
  1537. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
  1538. if (nlh == NULL)
  1539. return -EMSGSIZE;
  1540. ndtmsg = nlmsg_data(nlh);
  1541. read_lock_bh(&tbl->lock);
  1542. ndtmsg->ndtm_family = tbl->family;
  1543. ndtmsg->ndtm_pad1 = 0;
  1544. ndtmsg->ndtm_pad2 = 0;
  1545. if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
  1546. nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval, NDTA_PAD) ||
  1547. nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) ||
  1548. nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) ||
  1549. nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3))
  1550. goto nla_put_failure;
  1551. {
  1552. unsigned long now = jiffies;
  1553. unsigned int flush_delta = now - tbl->last_flush;
  1554. unsigned int rand_delta = now - tbl->last_rand;
  1555. struct neigh_hash_table *nht;
  1556. struct ndt_config ndc = {
  1557. .ndtc_key_len = tbl->key_len,
  1558. .ndtc_entry_size = tbl->entry_size,
  1559. .ndtc_entries = atomic_read(&tbl->entries),
  1560. .ndtc_last_flush = jiffies_to_msecs(flush_delta),
  1561. .ndtc_last_rand = jiffies_to_msecs(rand_delta),
  1562. .ndtc_proxy_qlen = tbl->proxy_queue.qlen,
  1563. };
  1564. rcu_read_lock_bh();
  1565. nht = rcu_dereference_bh(tbl->nht);
  1566. ndc.ndtc_hash_rnd = nht->hash_rnd[0];
  1567. ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
  1568. rcu_read_unlock_bh();
  1569. if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
  1570. goto nla_put_failure;
  1571. }
  1572. {
  1573. int cpu;
  1574. struct ndt_stats ndst;
  1575. memset(&ndst, 0, sizeof(ndst));
  1576. for_each_possible_cpu(cpu) {
  1577. struct neigh_statistics *st;
  1578. st = per_cpu_ptr(tbl->stats, cpu);
  1579. ndst.ndts_allocs += st->allocs;
  1580. ndst.ndts_destroys += st->destroys;
  1581. ndst.ndts_hash_grows += st->hash_grows;
  1582. ndst.ndts_res_failed += st->res_failed;
  1583. ndst.ndts_lookups += st->lookups;
  1584. ndst.ndts_hits += st->hits;
  1585. ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast;
  1586. ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast;
  1587. ndst.ndts_periodic_gc_runs += st->periodic_gc_runs;
  1588. ndst.ndts_forced_gc_runs += st->forced_gc_runs;
  1589. ndst.ndts_table_fulls += st->table_fulls;
  1590. }
  1591. if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
  1592. NDTA_PAD))
  1593. goto nla_put_failure;
  1594. }
  1595. BUG_ON(tbl->parms.dev);
  1596. if (neightbl_fill_parms(skb, &tbl->parms) < 0)
  1597. goto nla_put_failure;
  1598. read_unlock_bh(&tbl->lock);
  1599. nlmsg_end(skb, nlh);
  1600. return 0;
  1601. nla_put_failure:
  1602. read_unlock_bh(&tbl->lock);
  1603. nlmsg_cancel(skb, nlh);
  1604. return -EMSGSIZE;
  1605. }
  1606. static int neightbl_fill_param_info(struct sk_buff *skb,
  1607. struct neigh_table *tbl,
  1608. struct neigh_parms *parms,
  1609. u32 pid, u32 seq, int type,
  1610. unsigned int flags)
  1611. {
  1612. struct ndtmsg *ndtmsg;
  1613. struct nlmsghdr *nlh;
  1614. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
  1615. if (nlh == NULL)
  1616. return -EMSGSIZE;
  1617. ndtmsg = nlmsg_data(nlh);
  1618. read_lock_bh(&tbl->lock);
  1619. ndtmsg->ndtm_family = tbl->family;
  1620. ndtmsg->ndtm_pad1 = 0;
  1621. ndtmsg->ndtm_pad2 = 0;
  1622. if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
  1623. neightbl_fill_parms(skb, parms) < 0)
  1624. goto errout;
  1625. read_unlock_bh(&tbl->lock);
  1626. nlmsg_end(skb, nlh);
  1627. return 0;
  1628. errout:
  1629. read_unlock_bh(&tbl->lock);
  1630. nlmsg_cancel(skb, nlh);
  1631. return -EMSGSIZE;
  1632. }
  1633. static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
  1634. [NDTA_NAME] = { .type = NLA_STRING },
  1635. [NDTA_THRESH1] = { .type = NLA_U32 },
  1636. [NDTA_THRESH2] = { .type = NLA_U32 },
  1637. [NDTA_THRESH3] = { .type = NLA_U32 },
  1638. [NDTA_GC_INTERVAL] = { .type = NLA_U64 },
  1639. [NDTA_PARMS] = { .type = NLA_NESTED },
  1640. };
  1641. static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
  1642. [NDTPA_IFINDEX] = { .type = NLA_U32 },
  1643. [NDTPA_QUEUE_LEN] = { .type = NLA_U32 },
  1644. [NDTPA_PROXY_QLEN] = { .type = NLA_U32 },
  1645. [NDTPA_APP_PROBES] = { .type = NLA_U32 },
  1646. [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
  1647. [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
  1648. [NDTPA_MCAST_REPROBES] = { .type = NLA_U32 },
  1649. [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
  1650. [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
  1651. [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
  1652. [NDTPA_RETRANS_TIME] = { .type = NLA_U64 },
  1653. [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 },
  1654. [NDTPA_PROXY_DELAY] = { .type = NLA_U64 },
  1655. [NDTPA_LOCKTIME] = { .type = NLA_U64 },
  1656. };
  1657. static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh)
  1658. {
  1659. struct net *net = sock_net(skb->sk);
  1660. struct neigh_table *tbl;
  1661. struct ndtmsg *ndtmsg;
  1662. struct nlattr *tb[NDTA_MAX+1];
  1663. bool found = false;
  1664. int err, tidx;
  1665. err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
  1666. nl_neightbl_policy);
  1667. if (err < 0)
  1668. goto errout;
  1669. if (tb[NDTA_NAME] == NULL) {
  1670. err = -EINVAL;
  1671. goto errout;
  1672. }
  1673. ndtmsg = nlmsg_data(nlh);
  1674. for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
  1675. tbl = neigh_tables[tidx];
  1676. if (!tbl)
  1677. continue;
  1678. if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
  1679. continue;
  1680. if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
  1681. found = true;
  1682. break;
  1683. }
  1684. }
  1685. if (!found)
  1686. return -ENOENT;
  1687. /*
  1688. * We acquire tbl->lock to be nice to the periodic timers and
  1689. * make sure they always see a consistent set of values.
  1690. */
  1691. write_lock_bh(&tbl->lock);
  1692. if (tb[NDTA_PARMS]) {
  1693. struct nlattr *tbp[NDTPA_MAX+1];
  1694. struct neigh_parms *p;
  1695. int i, ifindex = 0;
  1696. err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS],
  1697. nl_ntbl_parm_policy);
  1698. if (err < 0)
  1699. goto errout_tbl_lock;
  1700. if (tbp[NDTPA_IFINDEX])
  1701. ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
  1702. p = lookup_neigh_parms(tbl, net, ifindex);
  1703. if (p == NULL) {
  1704. err = -ENOENT;
  1705. goto errout_tbl_lock;
  1706. }
  1707. for (i = 1; i <= NDTPA_MAX; i++) {
  1708. if (tbp[i] == NULL)
  1709. continue;
  1710. switch (i) {
  1711. case NDTPA_QUEUE_LEN:
  1712. NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
  1713. nla_get_u32(tbp[i]) *
  1714. SKB_TRUESIZE(ETH_FRAME_LEN));
  1715. break;
  1716. case NDTPA_QUEUE_LENBYTES:
  1717. NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
  1718. nla_get_u32(tbp[i]));
  1719. break;
  1720. case NDTPA_PROXY_QLEN:
  1721. NEIGH_VAR_SET(p, PROXY_QLEN,
  1722. nla_get_u32(tbp[i]));
  1723. break;
  1724. case NDTPA_APP_PROBES:
  1725. NEIGH_VAR_SET(p, APP_PROBES,
  1726. nla_get_u32(tbp[i]));
  1727. break;
  1728. case NDTPA_UCAST_PROBES:
  1729. NEIGH_VAR_SET(p, UCAST_PROBES,
  1730. nla_get_u32(tbp[i]));
  1731. break;
  1732. case NDTPA_MCAST_PROBES:
  1733. NEIGH_VAR_SET(p, MCAST_PROBES,
  1734. nla_get_u32(tbp[i]));
  1735. break;
  1736. case NDTPA_MCAST_REPROBES:
  1737. NEIGH_VAR_SET(p, MCAST_REPROBES,
  1738. nla_get_u32(tbp[i]));
  1739. break;
  1740. case NDTPA_BASE_REACHABLE_TIME:
  1741. NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
  1742. nla_get_msecs(tbp[i]));
  1743. /* update reachable_time as well, otherwise, the change will
  1744. * only be effective after the next time neigh_periodic_work
  1745. * decides to recompute it (can be multiple minutes)
  1746. */
  1747. p->reachable_time =
  1748. neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
  1749. break;
  1750. case NDTPA_GC_STALETIME:
  1751. NEIGH_VAR_SET(p, GC_STALETIME,
  1752. nla_get_msecs(tbp[i]));
  1753. break;
  1754. case NDTPA_DELAY_PROBE_TIME:
  1755. NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
  1756. nla_get_msecs(tbp[i]));
  1757. call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
  1758. break;
  1759. case NDTPA_RETRANS_TIME:
  1760. NEIGH_VAR_SET(p, RETRANS_TIME,
  1761. nla_get_msecs(tbp[i]));
  1762. break;
  1763. case NDTPA_ANYCAST_DELAY:
  1764. NEIGH_VAR_SET(p, ANYCAST_DELAY,
  1765. nla_get_msecs(tbp[i]));
  1766. break;
  1767. case NDTPA_PROXY_DELAY:
  1768. NEIGH_VAR_SET(p, PROXY_DELAY,
  1769. nla_get_msecs(tbp[i]));
  1770. break;
  1771. case NDTPA_LOCKTIME:
  1772. NEIGH_VAR_SET(p, LOCKTIME,
  1773. nla_get_msecs(tbp[i]));
  1774. break;
  1775. }
  1776. }
  1777. }
  1778. err = -ENOENT;
  1779. if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
  1780. tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
  1781. !net_eq(net, &init_net))
  1782. goto errout_tbl_lock;
  1783. if (tb[NDTA_THRESH1])
  1784. tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
  1785. if (tb[NDTA_THRESH2])
  1786. tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
  1787. if (tb[NDTA_THRESH3])
  1788. tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
  1789. if (tb[NDTA_GC_INTERVAL])
  1790. tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]);
  1791. err = 0;
  1792. errout_tbl_lock:
  1793. write_unlock_bh(&tbl->lock);
  1794. errout:
  1795. return err;
  1796. }
  1797. static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
  1798. {
  1799. struct net *net = sock_net(skb->sk);
  1800. int family, tidx, nidx = 0;
  1801. int tbl_skip = cb->args[0];
  1802. int neigh_skip = cb->args[1];
  1803. struct neigh_table *tbl;
  1804. family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
  1805. for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
  1806. struct neigh_parms *p;
  1807. tbl = neigh_tables[tidx];
  1808. if (!tbl)
  1809. continue;
  1810. if (tidx < tbl_skip || (family && tbl->family != family))
  1811. continue;
  1812. if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
  1813. cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
  1814. NLM_F_MULTI) < 0)
  1815. break;
  1816. nidx = 0;
  1817. p = list_next_entry(&tbl->parms, list);
  1818. list_for_each_entry_from(p, &tbl->parms_list, list) {
  1819. if (!net_eq(neigh_parms_net(p), net))
  1820. continue;
  1821. if (nidx < neigh_skip)
  1822. goto next;
  1823. if (neightbl_fill_param_info(skb, tbl, p,
  1824. NETLINK_CB(cb->skb).portid,
  1825. cb->nlh->nlmsg_seq,
  1826. RTM_NEWNEIGHTBL,
  1827. NLM_F_MULTI) < 0)
  1828. goto out;
  1829. next:
  1830. nidx++;
  1831. }
  1832. neigh_skip = 0;
  1833. }
  1834. out:
  1835. cb->args[0] = tidx;
  1836. cb->args[1] = nidx;
  1837. return skb->len;
  1838. }
  1839. static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
  1840. u32 pid, u32 seq, int type, unsigned int flags)
  1841. {
  1842. unsigned long now = jiffies;
  1843. struct nda_cacheinfo ci;
  1844. struct nlmsghdr *nlh;
  1845. struct ndmsg *ndm;
  1846. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
  1847. if (nlh == NULL)
  1848. return -EMSGSIZE;
  1849. ndm = nlmsg_data(nlh);
  1850. ndm->ndm_family = neigh->ops->family;
  1851. ndm->ndm_pad1 = 0;
  1852. ndm->ndm_pad2 = 0;
  1853. ndm->ndm_flags = neigh->flags;
  1854. ndm->ndm_type = neigh->type;
  1855. ndm->ndm_ifindex = neigh->dev->ifindex;
  1856. if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
  1857. goto nla_put_failure;
  1858. read_lock_bh(&neigh->lock);
  1859. ndm->ndm_state = neigh->nud_state;
  1860. if (neigh->nud_state & NUD_VALID) {
  1861. char haddr[MAX_ADDR_LEN];
  1862. neigh_ha_snapshot(haddr, neigh, neigh->dev);
  1863. if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) {
  1864. read_unlock_bh(&neigh->lock);
  1865. goto nla_put_failure;
  1866. }
  1867. }
  1868. ci.ndm_used = jiffies_to_clock_t(now - neigh->used);
  1869. ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
  1870. ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated);
  1871. ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1;
  1872. read_unlock_bh(&neigh->lock);
  1873. if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
  1874. nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  1875. goto nla_put_failure;
  1876. nlmsg_end(skb, nlh);
  1877. return 0;
  1878. nla_put_failure:
  1879. nlmsg_cancel(skb, nlh);
  1880. return -EMSGSIZE;
  1881. }
  1882. static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
  1883. u32 pid, u32 seq, int type, unsigned int flags,
  1884. struct neigh_table *tbl)
  1885. {
  1886. struct nlmsghdr *nlh;
  1887. struct ndmsg *ndm;
  1888. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
  1889. if (nlh == NULL)
  1890. return -EMSGSIZE;
  1891. ndm = nlmsg_data(nlh);
  1892. ndm->ndm_family = tbl->family;
  1893. ndm->ndm_pad1 = 0;
  1894. ndm->ndm_pad2 = 0;
  1895. ndm->ndm_flags = pn->flags | NTF_PROXY;
  1896. ndm->ndm_type = RTN_UNICAST;
  1897. ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
  1898. ndm->ndm_state = NUD_NONE;
  1899. if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
  1900. goto nla_put_failure;
  1901. nlmsg_end(skb, nlh);
  1902. return 0;
  1903. nla_put_failure:
  1904. nlmsg_cancel(skb, nlh);
  1905. return -EMSGSIZE;
  1906. }
  1907. static void neigh_update_notify(struct neighbour *neigh)
  1908. {
  1909. call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
  1910. __neigh_notify(neigh, RTM_NEWNEIGH, 0);
  1911. }
  1912. static bool neigh_master_filtered(struct net_device *dev, int master_idx)
  1913. {
  1914. struct net_device *master;
  1915. if (!master_idx)
  1916. return false;
  1917. master = netdev_master_upper_dev_get(dev);
  1918. if (!master || master->ifindex != master_idx)
  1919. return true;
  1920. return false;
  1921. }
  1922. static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
  1923. {
  1924. if (filter_idx && dev->ifindex != filter_idx)
  1925. return true;
  1926. return false;
  1927. }
  1928. static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
  1929. struct netlink_callback *cb)
  1930. {
  1931. struct net *net = sock_net(skb->sk);
  1932. const struct nlmsghdr *nlh = cb->nlh;
  1933. struct nlattr *tb[NDA_MAX + 1];
  1934. struct neighbour *n;
  1935. int rc, h, s_h = cb->args[1];
  1936. int idx, s_idx = idx = cb->args[2];
  1937. struct neigh_hash_table *nht;
  1938. int filter_master_idx = 0, filter_idx = 0;
  1939. unsigned int flags = NLM_F_MULTI;
  1940. int err;
  1941. err = nlmsg_parse(nlh, sizeof(struct ndmsg), tb, NDA_MAX, NULL);
  1942. if (!err) {
  1943. if (tb[NDA_IFINDEX]) {
  1944. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
  1945. return -EINVAL;
  1946. filter_idx = nla_get_u32(tb[NDA_IFINDEX]);
  1947. }
  1948. if (tb[NDA_MASTER]) {
  1949. if (nla_len(tb[NDA_MASTER]) != sizeof(u32))
  1950. return -EINVAL;
  1951. filter_master_idx = nla_get_u32(tb[NDA_MASTER]);
  1952. }
  1953. if (filter_idx || filter_master_idx)
  1954. flags |= NLM_F_DUMP_FILTERED;
  1955. }
  1956. rcu_read_lock_bh();
  1957. nht = rcu_dereference_bh(tbl->nht);
  1958. for (h = s_h; h < (1 << nht->hash_shift); h++) {
  1959. if (h > s_h)
  1960. s_idx = 0;
  1961. for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0;
  1962. n != NULL;
  1963. n = rcu_dereference_bh(n->next)) {
  1964. if (!net_eq(dev_net(n->dev), net))
  1965. continue;
  1966. if (neigh_ifindex_filtered(n->dev, filter_idx))
  1967. continue;
  1968. if (neigh_master_filtered(n->dev, filter_master_idx))
  1969. continue;
  1970. if (idx < s_idx)
  1971. goto next;
  1972. if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
  1973. cb->nlh->nlmsg_seq,
  1974. RTM_NEWNEIGH,
  1975. flags) < 0) {
  1976. rc = -1;
  1977. goto out;
  1978. }
  1979. next:
  1980. idx++;
  1981. }
  1982. }
  1983. rc = skb->len;
  1984. out:
  1985. rcu_read_unlock_bh();
  1986. cb->args[1] = h;
  1987. cb->args[2] = idx;
  1988. return rc;
  1989. }
  1990. static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
  1991. struct netlink_callback *cb)
  1992. {
  1993. struct pneigh_entry *n;
  1994. struct net *net = sock_net(skb->sk);
  1995. int rc, h, s_h = cb->args[3];
  1996. int idx, s_idx = idx = cb->args[4];
  1997. read_lock_bh(&tbl->lock);
  1998. for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
  1999. if (h > s_h)
  2000. s_idx = 0;
  2001. for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
  2002. if (pneigh_net(n) != net)
  2003. continue;
  2004. if (idx < s_idx)
  2005. goto next;
  2006. if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
  2007. cb->nlh->nlmsg_seq,
  2008. RTM_NEWNEIGH,
  2009. NLM_F_MULTI, tbl) < 0) {
  2010. read_unlock_bh(&tbl->lock);
  2011. rc = -1;
  2012. goto out;
  2013. }
  2014. next:
  2015. idx++;
  2016. }
  2017. }
  2018. read_unlock_bh(&tbl->lock);
  2019. rc = skb->len;
  2020. out:
  2021. cb->args[3] = h;
  2022. cb->args[4] = idx;
  2023. return rc;
  2024. }
  2025. static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
  2026. {
  2027. struct neigh_table *tbl;
  2028. int t, family, s_t;
  2029. int proxy = 0;
  2030. int err;
  2031. family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
  2032. /* check for full ndmsg structure presence, family member is
  2033. * the same for both structures
  2034. */
  2035. if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) &&
  2036. ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY)
  2037. proxy = 1;
  2038. s_t = cb->args[0];
  2039. for (t = 0; t < NEIGH_NR_TABLES; t++) {
  2040. tbl = neigh_tables[t];
  2041. if (!tbl)
  2042. continue;
  2043. if (t < s_t || (family && tbl->family != family))
  2044. continue;
  2045. if (t > s_t)
  2046. memset(&cb->args[1], 0, sizeof(cb->args) -
  2047. sizeof(cb->args[0]));
  2048. if (proxy)
  2049. err = pneigh_dump_table(tbl, skb, cb);
  2050. else
  2051. err = neigh_dump_table(tbl, skb, cb);
  2052. if (err < 0)
  2053. break;
  2054. }
  2055. cb->args[0] = t;
  2056. return skb->len;
  2057. }
  2058. void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
  2059. {
  2060. int chain;
  2061. struct neigh_hash_table *nht;
  2062. rcu_read_lock_bh();
  2063. nht = rcu_dereference_bh(tbl->nht);
  2064. read_lock(&tbl->lock); /* avoid resizes */
  2065. for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
  2066. struct neighbour *n;
  2067. for (n = rcu_dereference_bh(nht->hash_buckets[chain]);
  2068. n != NULL;
  2069. n = rcu_dereference_bh(n->next))
  2070. cb(n, cookie);
  2071. }
  2072. read_unlock(&tbl->lock);
  2073. rcu_read_unlock_bh();
  2074. }
  2075. EXPORT_SYMBOL(neigh_for_each);
  2076. /* The tbl->lock must be held as a writer and BH disabled. */
  2077. void __neigh_for_each_release(struct neigh_table *tbl,
  2078. int (*cb)(struct neighbour *))
  2079. {
  2080. int chain;
  2081. struct neigh_hash_table *nht;
  2082. nht = rcu_dereference_protected(tbl->nht,
  2083. lockdep_is_held(&tbl->lock));
  2084. for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
  2085. struct neighbour *n;
  2086. struct neighbour __rcu **np;
  2087. np = &nht->hash_buckets[chain];
  2088. while ((n = rcu_dereference_protected(*np,
  2089. lockdep_is_held(&tbl->lock))) != NULL) {
  2090. int release;
  2091. write_lock(&n->lock);
  2092. release = cb(n);
  2093. if (release) {
  2094. rcu_assign_pointer(*np,
  2095. rcu_dereference_protected(n->next,
  2096. lockdep_is_held(&tbl->lock)));
  2097. n->dead = 1;
  2098. } else
  2099. np = &n->next;
  2100. write_unlock(&n->lock);
  2101. if (release)
  2102. neigh_cleanup_and_release(n);
  2103. }
  2104. }
  2105. }
  2106. EXPORT_SYMBOL(__neigh_for_each_release);
  2107. int neigh_xmit(int index, struct net_device *dev,
  2108. const void *addr, struct sk_buff *skb)
  2109. {
  2110. int err = -EAFNOSUPPORT;
  2111. if (likely(index < NEIGH_NR_TABLES)) {
  2112. struct neigh_table *tbl;
  2113. struct neighbour *neigh;
  2114. tbl = neigh_tables[index];
  2115. if (!tbl)
  2116. goto out;
  2117. rcu_read_lock_bh();
  2118. neigh = __neigh_lookup_noref(tbl, addr, dev);
  2119. if (!neigh)
  2120. neigh = __neigh_create(tbl, addr, dev, false);
  2121. err = PTR_ERR(neigh);
  2122. if (IS_ERR(neigh)) {
  2123. rcu_read_unlock_bh();
  2124. goto out_kfree_skb;
  2125. }
  2126. err = neigh->output(neigh, skb);
  2127. rcu_read_unlock_bh();
  2128. }
  2129. else if (index == NEIGH_LINK_TABLE) {
  2130. err = dev_hard_header(skb, dev, ntohs(skb->protocol),
  2131. addr, NULL, skb->len);
  2132. if (err < 0)
  2133. goto out_kfree_skb;
  2134. err = dev_queue_xmit(skb);
  2135. }
  2136. out:
  2137. return err;
  2138. out_kfree_skb:
  2139. kfree_skb(skb);
  2140. goto out;
  2141. }
  2142. EXPORT_SYMBOL(neigh_xmit);
  2143. #ifdef CONFIG_PROC_FS
  2144. static struct neighbour *neigh_get_first(struct seq_file *seq)
  2145. {
  2146. struct neigh_seq_state *state = seq->private;
  2147. struct net *net = seq_file_net(seq);
  2148. struct neigh_hash_table *nht = state->nht;
  2149. struct neighbour *n = NULL;
  2150. int bucket = state->bucket;
  2151. state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
  2152. for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) {
  2153. n = rcu_dereference_bh(nht->hash_buckets[bucket]);
  2154. while (n) {
  2155. if (!net_eq(dev_net(n->dev), net))
  2156. goto next;
  2157. if (state->neigh_sub_iter) {
  2158. loff_t fakep = 0;
  2159. void *v;
  2160. v = state->neigh_sub_iter(state, n, &fakep);
  2161. if (!v)
  2162. goto next;
  2163. }
  2164. if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
  2165. break;
  2166. if (n->nud_state & ~NUD_NOARP)
  2167. break;
  2168. next:
  2169. n = rcu_dereference_bh(n->next);
  2170. }
  2171. if (n)
  2172. break;
  2173. }
  2174. state->bucket = bucket;
  2175. return n;
  2176. }
  2177. static struct neighbour *neigh_get_next(struct seq_file *seq,
  2178. struct neighbour *n,
  2179. loff_t *pos)
  2180. {
  2181. struct neigh_seq_state *state = seq->private;
  2182. struct net *net = seq_file_net(seq);
  2183. struct neigh_hash_table *nht = state->nht;
  2184. if (state->neigh_sub_iter) {
  2185. void *v = state->neigh_sub_iter(state, n, pos);
  2186. if (v)
  2187. return n;
  2188. }
  2189. n = rcu_dereference_bh(n->next);
  2190. while (1) {
  2191. while (n) {
  2192. if (!net_eq(dev_net(n->dev), net))
  2193. goto next;
  2194. if (state->neigh_sub_iter) {
  2195. void *v = state->neigh_sub_iter(state, n, pos);
  2196. if (v)
  2197. return n;
  2198. goto next;
  2199. }
  2200. if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
  2201. break;
  2202. if (n->nud_state & ~NUD_NOARP)
  2203. break;
  2204. next:
  2205. n = rcu_dereference_bh(n->next);
  2206. }
  2207. if (n)
  2208. break;
  2209. if (++state->bucket >= (1 << nht->hash_shift))
  2210. break;
  2211. n = rcu_dereference_bh(nht->hash_buckets[state->bucket]);
  2212. }
  2213. if (n && pos)
  2214. --(*pos);
  2215. return n;
  2216. }
  2217. static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
  2218. {
  2219. struct neighbour *n = neigh_get_first(seq);
  2220. if (n) {
  2221. --(*pos);
  2222. while (*pos) {
  2223. n = neigh_get_next(seq, n, pos);
  2224. if (!n)
  2225. break;
  2226. }
  2227. }
  2228. return *pos ? NULL : n;
  2229. }
  2230. static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
  2231. {
  2232. struct neigh_seq_state *state = seq->private;
  2233. struct net *net = seq_file_net(seq);
  2234. struct neigh_table *tbl = state->tbl;
  2235. struct pneigh_entry *pn = NULL;
  2236. int bucket = state->bucket;
  2237. state->flags |= NEIGH_SEQ_IS_PNEIGH;
  2238. for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
  2239. pn = tbl->phash_buckets[bucket];
  2240. while (pn && !net_eq(pneigh_net(pn), net))
  2241. pn = pn->next;
  2242. if (pn)
  2243. break;
  2244. }
  2245. state->bucket = bucket;
  2246. return pn;
  2247. }
  2248. static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
  2249. struct pneigh_entry *pn,
  2250. loff_t *pos)
  2251. {
  2252. struct neigh_seq_state *state = seq->private;
  2253. struct net *net = seq_file_net(seq);
  2254. struct neigh_table *tbl = state->tbl;
  2255. do {
  2256. pn = pn->next;
  2257. } while (pn && !net_eq(pneigh_net(pn), net));
  2258. while (!pn) {
  2259. if (++state->bucket > PNEIGH_HASHMASK)
  2260. break;
  2261. pn = tbl->phash_buckets[state->bucket];
  2262. while (pn && !net_eq(pneigh_net(pn), net))
  2263. pn = pn->next;
  2264. if (pn)
  2265. break;
  2266. }
  2267. if (pn && pos)
  2268. --(*pos);
  2269. return pn;
  2270. }
  2271. static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
  2272. {
  2273. struct pneigh_entry *pn = pneigh_get_first(seq);
  2274. if (pn) {
  2275. --(*pos);
  2276. while (*pos) {
  2277. pn = pneigh_get_next(seq, pn, pos);
  2278. if (!pn)
  2279. break;
  2280. }
  2281. }
  2282. return *pos ? NULL : pn;
  2283. }
  2284. static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
  2285. {
  2286. struct neigh_seq_state *state = seq->private;
  2287. void *rc;
  2288. loff_t idxpos = *pos;
  2289. rc = neigh_get_idx(seq, &idxpos);
  2290. if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
  2291. rc = pneigh_get_idx(seq, &idxpos);
  2292. return rc;
  2293. }
  2294. void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
  2295. __acquires(rcu_bh)
  2296. {
  2297. struct neigh_seq_state *state = seq->private;
  2298. state->tbl = tbl;
  2299. state->bucket = 0;
  2300. state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
  2301. rcu_read_lock_bh();
  2302. state->nht = rcu_dereference_bh(tbl->nht);
  2303. return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
  2304. }
  2305. EXPORT_SYMBOL(neigh_seq_start);
  2306. void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2307. {
  2308. struct neigh_seq_state *state;
  2309. void *rc;
  2310. if (v == SEQ_START_TOKEN) {
  2311. rc = neigh_get_first(seq);
  2312. goto out;
  2313. }
  2314. state = seq->private;
  2315. if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
  2316. rc = neigh_get_next(seq, v, NULL);
  2317. if (rc)
  2318. goto out;
  2319. if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
  2320. rc = pneigh_get_first(seq);
  2321. } else {
  2322. BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
  2323. rc = pneigh_get_next(seq, v, NULL);
  2324. }
  2325. out:
  2326. ++(*pos);
  2327. return rc;
  2328. }
  2329. EXPORT_SYMBOL(neigh_seq_next);
  2330. void neigh_seq_stop(struct seq_file *seq, void *v)
  2331. __releases(rcu_bh)
  2332. {
  2333. rcu_read_unlock_bh();
  2334. }
  2335. EXPORT_SYMBOL(neigh_seq_stop);
  2336. /* statistics via seq_file */
  2337. static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
  2338. {
  2339. struct neigh_table *tbl = seq->private;
  2340. int cpu;
  2341. if (*pos == 0)
  2342. return SEQ_START_TOKEN;
  2343. for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
  2344. if (!cpu_possible(cpu))
  2345. continue;
  2346. *pos = cpu+1;
  2347. return per_cpu_ptr(tbl->stats, cpu);
  2348. }
  2349. return NULL;
  2350. }
  2351. static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2352. {
  2353. struct neigh_table *tbl = seq->private;
  2354. int cpu;
  2355. for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
  2356. if (!cpu_possible(cpu))
  2357. continue;
  2358. *pos = cpu+1;
  2359. return per_cpu_ptr(tbl->stats, cpu);
  2360. }
  2361. return NULL;
  2362. }
  2363. static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
  2364. {
  2365. }
  2366. static int neigh_stat_seq_show(struct seq_file *seq, void *v)
  2367. {
  2368. struct neigh_table *tbl = seq->private;
  2369. struct neigh_statistics *st = v;
  2370. if (v == SEQ_START_TOKEN) {
  2371. 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");
  2372. return 0;
  2373. }
  2374. seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
  2375. "%08lx %08lx %08lx %08lx %08lx %08lx\n",
  2376. atomic_read(&tbl->entries),
  2377. st->allocs,
  2378. st->destroys,
  2379. st->hash_grows,
  2380. st->lookups,
  2381. st->hits,
  2382. st->res_failed,
  2383. st->rcv_probes_mcast,
  2384. st->rcv_probes_ucast,
  2385. st->periodic_gc_runs,
  2386. st->forced_gc_runs,
  2387. st->unres_discards,
  2388. st->table_fulls
  2389. );
  2390. return 0;
  2391. }
  2392. static const struct seq_operations neigh_stat_seq_ops = {
  2393. .start = neigh_stat_seq_start,
  2394. .next = neigh_stat_seq_next,
  2395. .stop = neigh_stat_seq_stop,
  2396. .show = neigh_stat_seq_show,
  2397. };
  2398. static int neigh_stat_seq_open(struct inode *inode, struct file *file)
  2399. {
  2400. int ret = seq_open(file, &neigh_stat_seq_ops);
  2401. if (!ret) {
  2402. struct seq_file *sf = file->private_data;
  2403. sf->private = PDE_DATA(inode);
  2404. }
  2405. return ret;
  2406. };
  2407. static const struct file_operations neigh_stat_seq_fops = {
  2408. .owner = THIS_MODULE,
  2409. .open = neigh_stat_seq_open,
  2410. .read = seq_read,
  2411. .llseek = seq_lseek,
  2412. .release = seq_release,
  2413. };
  2414. #endif /* CONFIG_PROC_FS */
  2415. static inline size_t neigh_nlmsg_size(void)
  2416. {
  2417. return NLMSG_ALIGN(sizeof(struct ndmsg))
  2418. + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
  2419. + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
  2420. + nla_total_size(sizeof(struct nda_cacheinfo))
  2421. + nla_total_size(4); /* NDA_PROBES */
  2422. }
  2423. static void __neigh_notify(struct neighbour *n, int type, int flags)
  2424. {
  2425. struct net *net = dev_net(n->dev);
  2426. struct sk_buff *skb;
  2427. int err = -ENOBUFS;
  2428. skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
  2429. if (skb == NULL)
  2430. goto errout;
  2431. err = neigh_fill_info(skb, n, 0, 0, type, flags);
  2432. if (err < 0) {
  2433. /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
  2434. WARN_ON(err == -EMSGSIZE);
  2435. kfree_skb(skb);
  2436. goto errout;
  2437. }
  2438. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  2439. return;
  2440. errout:
  2441. if (err < 0)
  2442. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  2443. }
  2444. void neigh_app_ns(struct neighbour *n)
  2445. {
  2446. __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST);
  2447. }
  2448. EXPORT_SYMBOL(neigh_app_ns);
  2449. #ifdef CONFIG_SYSCTL
  2450. static int zero;
  2451. static int int_max = INT_MAX;
  2452. static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
  2453. static int proc_unres_qlen(struct ctl_table *ctl, int write,
  2454. void __user *buffer, size_t *lenp, loff_t *ppos)
  2455. {
  2456. int size, ret;
  2457. struct ctl_table tmp = *ctl;
  2458. tmp.extra1 = &zero;
  2459. tmp.extra2 = &unres_qlen_max;
  2460. tmp.data = &size;
  2461. size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
  2462. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  2463. if (write && !ret)
  2464. *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
  2465. return ret;
  2466. }
  2467. static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
  2468. int family)
  2469. {
  2470. switch (family) {
  2471. case AF_INET:
  2472. return __in_dev_arp_parms_get_rcu(dev);
  2473. case AF_INET6:
  2474. return __in6_dev_nd_parms_get_rcu(dev);
  2475. }
  2476. return NULL;
  2477. }
  2478. static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
  2479. int index)
  2480. {
  2481. struct net_device *dev;
  2482. int family = neigh_parms_family(p);
  2483. rcu_read_lock();
  2484. for_each_netdev_rcu(net, dev) {
  2485. struct neigh_parms *dst_p =
  2486. neigh_get_dev_parms_rcu(dev, family);
  2487. if (dst_p && !test_bit(index, dst_p->data_state))
  2488. dst_p->data[index] = p->data[index];
  2489. }
  2490. rcu_read_unlock();
  2491. }
  2492. static void neigh_proc_update(struct ctl_table *ctl, int write)
  2493. {
  2494. struct net_device *dev = ctl->extra1;
  2495. struct neigh_parms *p = ctl->extra2;
  2496. struct net *net = neigh_parms_net(p);
  2497. int index = (int *) ctl->data - p->data;
  2498. if (!write)
  2499. return;
  2500. set_bit(index, p->data_state);
  2501. if (index == NEIGH_VAR_DELAY_PROBE_TIME)
  2502. call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
  2503. if (!dev) /* NULL dev means this is default value */
  2504. neigh_copy_dflt_parms(net, p, index);
  2505. }
  2506. static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write,
  2507. void __user *buffer,
  2508. size_t *lenp, loff_t *ppos)
  2509. {
  2510. struct ctl_table tmp = *ctl;
  2511. int ret;
  2512. tmp.extra1 = &zero;
  2513. tmp.extra2 = &int_max;
  2514. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  2515. neigh_proc_update(ctl, write);
  2516. return ret;
  2517. }
  2518. int neigh_proc_dointvec(struct ctl_table *ctl, int write,
  2519. void __user *buffer, size_t *lenp, loff_t *ppos)
  2520. {
  2521. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  2522. neigh_proc_update(ctl, write);
  2523. return ret;
  2524. }
  2525. EXPORT_SYMBOL(neigh_proc_dointvec);
  2526. int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
  2527. void __user *buffer,
  2528. size_t *lenp, loff_t *ppos)
  2529. {
  2530. int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
  2531. neigh_proc_update(ctl, write);
  2532. return ret;
  2533. }
  2534. EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
  2535. static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write,
  2536. void __user *buffer,
  2537. size_t *lenp, loff_t *ppos)
  2538. {
  2539. int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
  2540. neigh_proc_update(ctl, write);
  2541. return ret;
  2542. }
  2543. int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
  2544. void __user *buffer,
  2545. size_t *lenp, loff_t *ppos)
  2546. {
  2547. int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
  2548. neigh_proc_update(ctl, write);
  2549. return ret;
  2550. }
  2551. EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
  2552. static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write,
  2553. void __user *buffer,
  2554. size_t *lenp, loff_t *ppos)
  2555. {
  2556. int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
  2557. neigh_proc_update(ctl, write);
  2558. return ret;
  2559. }
  2560. static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write,
  2561. void __user *buffer,
  2562. size_t *lenp, loff_t *ppos)
  2563. {
  2564. struct neigh_parms *p = ctl->extra2;
  2565. int ret;
  2566. if (strcmp(ctl->procname, "base_reachable_time") == 0)
  2567. ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
  2568. else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
  2569. ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
  2570. else
  2571. ret = -1;
  2572. if (write && ret == 0) {
  2573. /* update reachable_time as well, otherwise, the change will
  2574. * only be effective after the next time neigh_periodic_work
  2575. * decides to recompute it
  2576. */
  2577. p->reachable_time =
  2578. neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
  2579. }
  2580. return ret;
  2581. }
  2582. #define NEIGH_PARMS_DATA_OFFSET(index) \
  2583. (&((struct neigh_parms *) 0)->data[index])
  2584. #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
  2585. [NEIGH_VAR_ ## attr] = { \
  2586. .procname = name, \
  2587. .data = NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
  2588. .maxlen = sizeof(int), \
  2589. .mode = mval, \
  2590. .proc_handler = proc, \
  2591. }
  2592. #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
  2593. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
  2594. #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
  2595. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
  2596. #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
  2597. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
  2598. #define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \
  2599. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
  2600. #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
  2601. NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
  2602. #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
  2603. NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
  2604. static struct neigh_sysctl_table {
  2605. struct ctl_table_header *sysctl_header;
  2606. struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1];
  2607. } neigh_sysctl_template __read_mostly = {
  2608. .neigh_vars = {
  2609. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
  2610. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
  2611. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
  2612. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
  2613. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
  2614. NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
  2615. NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
  2616. NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
  2617. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
  2618. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
  2619. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
  2620. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
  2621. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
  2622. NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
  2623. NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
  2624. NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
  2625. [NEIGH_VAR_GC_INTERVAL] = {
  2626. .procname = "gc_interval",
  2627. .maxlen = sizeof(int),
  2628. .mode = 0644,
  2629. .proc_handler = proc_dointvec_jiffies,
  2630. },
  2631. [NEIGH_VAR_GC_THRESH1] = {
  2632. .procname = "gc_thresh1",
  2633. .maxlen = sizeof(int),
  2634. .mode = 0644,
  2635. .extra1 = &zero,
  2636. .extra2 = &int_max,
  2637. .proc_handler = proc_dointvec_minmax,
  2638. },
  2639. [NEIGH_VAR_GC_THRESH2] = {
  2640. .procname = "gc_thresh2",
  2641. .maxlen = sizeof(int),
  2642. .mode = 0644,
  2643. .extra1 = &zero,
  2644. .extra2 = &int_max,
  2645. .proc_handler = proc_dointvec_minmax,
  2646. },
  2647. [NEIGH_VAR_GC_THRESH3] = {
  2648. .procname = "gc_thresh3",
  2649. .maxlen = sizeof(int),
  2650. .mode = 0644,
  2651. .extra1 = &zero,
  2652. .extra2 = &int_max,
  2653. .proc_handler = proc_dointvec_minmax,
  2654. },
  2655. {},
  2656. },
  2657. };
  2658. int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
  2659. proc_handler *handler)
  2660. {
  2661. int i;
  2662. struct neigh_sysctl_table *t;
  2663. const char *dev_name_source;
  2664. char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
  2665. char *p_name;
  2666. t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL);
  2667. if (!t)
  2668. goto err;
  2669. for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
  2670. t->neigh_vars[i].data += (long) p;
  2671. t->neigh_vars[i].extra1 = dev;
  2672. t->neigh_vars[i].extra2 = p;
  2673. }
  2674. if (dev) {
  2675. dev_name_source = dev->name;
  2676. /* Terminate the table early */
  2677. memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0,
  2678. sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL]));
  2679. } else {
  2680. struct neigh_table *tbl = p->tbl;
  2681. dev_name_source = "default";
  2682. t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
  2683. t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
  2684. t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
  2685. t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
  2686. }
  2687. if (handler) {
  2688. /* RetransTime */
  2689. t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
  2690. /* ReachableTime */
  2691. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
  2692. /* RetransTime (in milliseconds)*/
  2693. t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
  2694. /* ReachableTime (in milliseconds) */
  2695. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
  2696. } else {
  2697. /* Those handlers will update p->reachable_time after
  2698. * base_reachable_time(_ms) is set to ensure the new timer starts being
  2699. * applied after the next neighbour update instead of waiting for
  2700. * neigh_periodic_work to update its value (can be multiple minutes)
  2701. * So any handler that replaces them should do this as well
  2702. */
  2703. /* ReachableTime */
  2704. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
  2705. neigh_proc_base_reachable_time;
  2706. /* ReachableTime (in milliseconds) */
  2707. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
  2708. neigh_proc_base_reachable_time;
  2709. }
  2710. /* Don't export sysctls to unprivileged users */
  2711. if (neigh_parms_net(p)->user_ns != &init_user_ns)
  2712. t->neigh_vars[0].procname = NULL;
  2713. switch (neigh_parms_family(p)) {
  2714. case AF_INET:
  2715. p_name = "ipv4";
  2716. break;
  2717. case AF_INET6:
  2718. p_name = "ipv6";
  2719. break;
  2720. default:
  2721. BUG();
  2722. }
  2723. snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
  2724. p_name, dev_name_source);
  2725. t->sysctl_header =
  2726. register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars);
  2727. if (!t->sysctl_header)
  2728. goto free;
  2729. p->sysctl_table = t;
  2730. return 0;
  2731. free:
  2732. kfree(t);
  2733. err:
  2734. return -ENOBUFS;
  2735. }
  2736. EXPORT_SYMBOL(neigh_sysctl_register);
  2737. void neigh_sysctl_unregister(struct neigh_parms *p)
  2738. {
  2739. if (p->sysctl_table) {
  2740. struct neigh_sysctl_table *t = p->sysctl_table;
  2741. p->sysctl_table = NULL;
  2742. unregister_net_sysctl_table(t->sysctl_header);
  2743. kfree(t);
  2744. }
  2745. }
  2746. EXPORT_SYMBOL(neigh_sysctl_unregister);
  2747. #endif /* CONFIG_SYSCTL */
  2748. static int __init neigh_init(void)
  2749. {
  2750. rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, NULL);
  2751. rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, NULL);
  2752. rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, NULL);
  2753. rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info,
  2754. NULL);
  2755. rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, NULL);
  2756. return 0;
  2757. }
  2758. subsys_initcall(neigh_init);