neighbour.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic address resolution entity
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  8. *
  9. * Fixes:
  10. * Vitaly E. Lavrov releasing NULL neighbor in neigh_add.
  11. * Harald Welte Add neighbour cache statistics like rtstat
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/socket.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/proc_fs.h>
  21. #ifdef CONFIG_SYSCTL
  22. #include <linux/sysctl.h>
  23. #endif
  24. #include <linux/times.h>
  25. #include <net/net_namespace.h>
  26. #include <net/neighbour.h>
  27. #include <net/arp.h>
  28. #include <net/dst.h>
  29. #include <net/ip.h>
  30. #include <net/sock.h>
  31. #include <net/netevent.h>
  32. #include <net/netlink.h>
  33. #include <linux/rtnetlink.h>
  34. #include <linux/random.h>
  35. #include <linux/string.h>
  36. #include <linux/log2.h>
  37. #include <linux/inetdevice.h>
  38. #include <net/addrconf.h>
  39. #include <trace/events/neigh.h>
  40. #define NEIGH_DEBUG 1
  41. #define neigh_dbg(level, fmt, ...) \
  42. do { \
  43. if (level <= NEIGH_DEBUG) \
  44. pr_debug(fmt, ##__VA_ARGS__); \
  45. } while (0)
  46. #define PNEIGH_HASHMASK 0xF
  47. static void neigh_timer_handler(struct timer_list *t);
  48. static void neigh_notify(struct neighbour *n, int type, int flags, u32 pid);
  49. static void __neigh_notify(struct neighbour *n, int type, int flags, u32 pid);
  50. static void pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
  51. bool skip_perm);
  52. #ifdef CONFIG_PROC_FS
  53. static const struct seq_operations neigh_stat_seq_ops;
  54. #endif
  55. static struct hlist_head *neigh_get_dev_table(struct net_device *dev, int family)
  56. {
  57. int i;
  58. switch (family) {
  59. default:
  60. DEBUG_NET_WARN_ON_ONCE(1);
  61. fallthrough; /* to avoid panic by null-ptr-deref */
  62. case AF_INET:
  63. i = NEIGH_ARP_TABLE;
  64. break;
  65. case AF_INET6:
  66. i = NEIGH_ND_TABLE;
  67. break;
  68. }
  69. return &dev->neighbours[i];
  70. }
  71. /*
  72. Neighbour hash table buckets are protected with tbl->lock.
  73. - All the scans/updates to hash buckets MUST be made under this lock.
  74. - NOTHING clever should be made under this lock: no callbacks
  75. to protocol backends, no attempts to send something to network.
  76. It will result in deadlocks, if backend/driver wants to use neighbour
  77. cache.
  78. - If the entry requires some non-trivial actions, increase
  79. its reference count and release table lock.
  80. Neighbour entries are protected:
  81. - with reference count.
  82. - with rwlock neigh->lock
  83. Reference count prevents destruction.
  84. neigh->lock mainly serializes ll address data and its validity state.
  85. However, the same lock is used to protect another entry fields:
  86. - timer
  87. - resolution queue
  88. Again, nothing clever shall be made under neigh->lock,
  89. the most complicated procedure, which we allow is dev->hard_header.
  90. It is supposed, that dev->hard_header is simplistic and does
  91. not make callbacks to neighbour tables.
  92. */
  93. static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb)
  94. {
  95. kfree_skb(skb);
  96. return -ENETDOWN;
  97. }
  98. static void neigh_cleanup_and_release(struct neighbour *neigh)
  99. {
  100. trace_neigh_cleanup_and_release(neigh, 0);
  101. neigh_notify(neigh, RTM_DELNEIGH, 0, 0);
  102. call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
  103. neigh_release(neigh);
  104. }
  105. /*
  106. * It is random distribution in the interval (1/2)*base...(3/2)*base.
  107. * It corresponds to default IPv6 settings and is not overridable,
  108. * because it is really reasonable choice.
  109. */
  110. unsigned long neigh_rand_reach_time(unsigned long base)
  111. {
  112. return base ? get_random_u32_below(base) + (base >> 1) : 0;
  113. }
  114. EXPORT_SYMBOL(neigh_rand_reach_time);
  115. static void neigh_mark_dead(struct neighbour *n)
  116. {
  117. n->dead = 1;
  118. if (!list_empty(&n->gc_list)) {
  119. list_del_init(&n->gc_list);
  120. atomic_dec(&n->tbl->gc_entries);
  121. }
  122. if (!list_empty(&n->managed_list))
  123. list_del_init(&n->managed_list);
  124. }
  125. static void neigh_update_gc_list(struct neighbour *n)
  126. {
  127. bool on_gc_list, exempt_from_gc;
  128. spin_lock_bh(&n->tbl->lock);
  129. write_lock(&n->lock);
  130. if (n->dead)
  131. goto out;
  132. /* remove from the gc list if new state is permanent or if neighbor is
  133. * externally learned / validated; otherwise entry should be on the gc
  134. * list
  135. */
  136. exempt_from_gc = n->nud_state & NUD_PERMANENT ||
  137. n->flags & (NTF_EXT_LEARNED | NTF_EXT_VALIDATED);
  138. on_gc_list = !list_empty(&n->gc_list);
  139. if (exempt_from_gc && on_gc_list) {
  140. list_del_init(&n->gc_list);
  141. atomic_dec(&n->tbl->gc_entries);
  142. } else if (!exempt_from_gc && !on_gc_list) {
  143. /* add entries to the tail; cleaning removes from the front */
  144. list_add_tail(&n->gc_list, &n->tbl->gc_list);
  145. atomic_inc(&n->tbl->gc_entries);
  146. }
  147. out:
  148. write_unlock(&n->lock);
  149. spin_unlock_bh(&n->tbl->lock);
  150. }
  151. static void neigh_update_managed_list(struct neighbour *n)
  152. {
  153. bool on_managed_list, add_to_managed;
  154. spin_lock_bh(&n->tbl->lock);
  155. write_lock(&n->lock);
  156. if (n->dead)
  157. goto out;
  158. add_to_managed = n->flags & NTF_MANAGED;
  159. on_managed_list = !list_empty(&n->managed_list);
  160. if (!add_to_managed && on_managed_list)
  161. list_del_init(&n->managed_list);
  162. else if (add_to_managed && !on_managed_list)
  163. list_add_tail(&n->managed_list, &n->tbl->managed_list);
  164. out:
  165. write_unlock(&n->lock);
  166. spin_unlock_bh(&n->tbl->lock);
  167. }
  168. static void neigh_update_flags(struct neighbour *neigh, u32 flags, int *notify,
  169. bool *gc_update, bool *managed_update)
  170. {
  171. u32 ndm_flags, old_flags = neigh->flags;
  172. if (!(flags & NEIGH_UPDATE_F_ADMIN))
  173. return;
  174. ndm_flags = (flags & NEIGH_UPDATE_F_EXT_LEARNED) ? NTF_EXT_LEARNED : 0;
  175. ndm_flags |= (flags & NEIGH_UPDATE_F_MANAGED) ? NTF_MANAGED : 0;
  176. ndm_flags |= (flags & NEIGH_UPDATE_F_EXT_VALIDATED) ? NTF_EXT_VALIDATED : 0;
  177. if ((old_flags ^ ndm_flags) & NTF_EXT_LEARNED) {
  178. if (ndm_flags & NTF_EXT_LEARNED)
  179. neigh->flags |= NTF_EXT_LEARNED;
  180. else
  181. neigh->flags &= ~NTF_EXT_LEARNED;
  182. *notify = 1;
  183. *gc_update = true;
  184. }
  185. if ((old_flags ^ ndm_flags) & NTF_MANAGED) {
  186. if (ndm_flags & NTF_MANAGED)
  187. neigh->flags |= NTF_MANAGED;
  188. else
  189. neigh->flags &= ~NTF_MANAGED;
  190. *notify = 1;
  191. *managed_update = true;
  192. }
  193. if ((old_flags ^ ndm_flags) & NTF_EXT_VALIDATED) {
  194. if (ndm_flags & NTF_EXT_VALIDATED)
  195. neigh->flags |= NTF_EXT_VALIDATED;
  196. else
  197. neigh->flags &= ~NTF_EXT_VALIDATED;
  198. *notify = 1;
  199. *gc_update = true;
  200. }
  201. }
  202. bool neigh_remove_one(struct neighbour *n)
  203. {
  204. bool retval = false;
  205. write_lock(&n->lock);
  206. if (refcount_read(&n->refcnt) == 1) {
  207. hlist_del_rcu(&n->hash);
  208. hlist_del_rcu(&n->dev_list);
  209. neigh_mark_dead(n);
  210. retval = true;
  211. }
  212. write_unlock(&n->lock);
  213. if (retval)
  214. neigh_cleanup_and_release(n);
  215. return retval;
  216. }
  217. static int neigh_forced_gc(struct neigh_table *tbl)
  218. {
  219. int max_clean = atomic_read(&tbl->gc_entries) -
  220. READ_ONCE(tbl->gc_thresh2);
  221. u64 tmax = ktime_get_ns() + NSEC_PER_MSEC;
  222. unsigned long tref = jiffies - 5 * HZ;
  223. struct neighbour *n, *tmp;
  224. int shrunk = 0;
  225. int loop = 0;
  226. NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
  227. spin_lock_bh(&tbl->lock);
  228. list_for_each_entry_safe(n, tmp, &tbl->gc_list, gc_list) {
  229. if (refcount_read(&n->refcnt) == 1) {
  230. bool remove = false;
  231. write_lock(&n->lock);
  232. if ((n->nud_state == NUD_FAILED) ||
  233. (n->nud_state == NUD_NOARP) ||
  234. (tbl->is_multicast &&
  235. tbl->is_multicast(n->primary_key)) ||
  236. !time_in_range(n->updated, tref, jiffies))
  237. remove = true;
  238. write_unlock(&n->lock);
  239. if (remove && neigh_remove_one(n))
  240. shrunk++;
  241. if (shrunk >= max_clean)
  242. break;
  243. if (++loop == 16) {
  244. if (ktime_get_ns() > tmax)
  245. goto unlock;
  246. loop = 0;
  247. }
  248. }
  249. }
  250. WRITE_ONCE(tbl->last_flush, jiffies);
  251. unlock:
  252. spin_unlock_bh(&tbl->lock);
  253. return shrunk;
  254. }
  255. static void neigh_add_timer(struct neighbour *n, unsigned long when)
  256. {
  257. /* Use safe distance from the jiffies - LONG_MAX point while timer
  258. * is running in DELAY/PROBE state but still show to user space
  259. * large times in the past.
  260. */
  261. unsigned long mint = jiffies - (LONG_MAX - 86400 * HZ);
  262. neigh_hold(n);
  263. if (!time_in_range(n->confirmed, mint, jiffies))
  264. n->confirmed = mint;
  265. if (time_before(n->used, n->confirmed))
  266. n->used = n->confirmed;
  267. if (unlikely(mod_timer(&n->timer, when))) {
  268. printk("NEIGH: BUG, double timer add, state is %x\n",
  269. n->nud_state);
  270. dump_stack();
  271. }
  272. }
  273. static int neigh_del_timer(struct neighbour *n)
  274. {
  275. if ((n->nud_state & NUD_IN_TIMER) &&
  276. timer_delete(&n->timer)) {
  277. neigh_release(n);
  278. return 1;
  279. }
  280. return 0;
  281. }
  282. static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev,
  283. int family)
  284. {
  285. switch (family) {
  286. case AF_INET:
  287. return __in_dev_arp_parms_get_rcu(dev);
  288. case AF_INET6:
  289. return __in6_dev_nd_parms_get_rcu(dev);
  290. }
  291. return NULL;
  292. }
  293. static void neigh_parms_qlen_dec(struct net_device *dev, int family)
  294. {
  295. struct neigh_parms *p;
  296. rcu_read_lock();
  297. p = neigh_get_dev_parms_rcu(dev, family);
  298. if (p)
  299. p->qlen--;
  300. rcu_read_unlock();
  301. }
  302. static void pneigh_queue_purge(struct sk_buff_head *list, struct net *net,
  303. int family)
  304. {
  305. struct sk_buff_head tmp;
  306. unsigned long flags;
  307. struct sk_buff *skb;
  308. skb_queue_head_init(&tmp);
  309. spin_lock_irqsave(&list->lock, flags);
  310. skb = skb_peek(list);
  311. while (skb != NULL) {
  312. struct sk_buff *skb_next = skb_peek_next(skb, list);
  313. struct net_device *dev = skb->dev;
  314. if (net == NULL || net_eq(dev_net(dev), net)) {
  315. neigh_parms_qlen_dec(dev, family);
  316. __skb_unlink(skb, list);
  317. __skb_queue_tail(&tmp, skb);
  318. }
  319. skb = skb_next;
  320. }
  321. spin_unlock_irqrestore(&list->lock, flags);
  322. while ((skb = __skb_dequeue(&tmp))) {
  323. dev_put(skb->dev);
  324. kfree_skb(skb);
  325. }
  326. }
  327. static void neigh_flush_one(struct neighbour *n)
  328. {
  329. hlist_del_rcu(&n->hash);
  330. hlist_del_rcu(&n->dev_list);
  331. write_lock(&n->lock);
  332. neigh_del_timer(n);
  333. neigh_mark_dead(n);
  334. if (refcount_read(&n->refcnt) != 1) {
  335. /* The most unpleasant situation.
  336. * We must destroy neighbour entry,
  337. * but someone still uses it.
  338. *
  339. * The destroy will be delayed until
  340. * the last user releases us, but
  341. * we must kill timers etc. and move
  342. * it to safe state.
  343. */
  344. __skb_queue_purge(&n->arp_queue);
  345. n->arp_queue_len_bytes = 0;
  346. WRITE_ONCE(n->output, neigh_blackhole);
  347. if (n->nud_state & NUD_VALID)
  348. n->nud_state = NUD_NOARP;
  349. else
  350. n->nud_state = NUD_NONE;
  351. neigh_dbg(2, "neigh %p is stray\n", n);
  352. }
  353. write_unlock(&n->lock);
  354. neigh_cleanup_and_release(n);
  355. }
  356. static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,
  357. bool skip_perm)
  358. {
  359. struct hlist_head *dev_head;
  360. struct hlist_node *tmp;
  361. struct neighbour *n;
  362. dev_head = neigh_get_dev_table(dev, tbl->family);
  363. hlist_for_each_entry_safe(n, tmp, dev_head, dev_list) {
  364. if (skip_perm &&
  365. (n->nud_state & NUD_PERMANENT ||
  366. n->flags & NTF_EXT_VALIDATED))
  367. continue;
  368. neigh_flush_one(n);
  369. }
  370. }
  371. static void neigh_flush_table(struct neigh_table *tbl)
  372. {
  373. struct neigh_hash_table *nht;
  374. int i;
  375. nht = rcu_dereference_protected(tbl->nht,
  376. lockdep_is_held(&tbl->lock));
  377. for (i = 0; i < (1 << nht->hash_shift); i++) {
  378. struct hlist_node *tmp;
  379. struct neighbour *n;
  380. neigh_for_each_in_bucket_safe(n, tmp, &nht->hash_heads[i])
  381. neigh_flush_one(n);
  382. }
  383. }
  384. void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
  385. {
  386. spin_lock_bh(&tbl->lock);
  387. neigh_flush_dev(tbl, dev, false);
  388. spin_unlock_bh(&tbl->lock);
  389. }
  390. EXPORT_SYMBOL(neigh_changeaddr);
  391. static int __neigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
  392. bool skip_perm)
  393. {
  394. spin_lock_bh(&tbl->lock);
  395. if (likely(dev)) {
  396. neigh_flush_dev(tbl, dev, skip_perm);
  397. } else {
  398. DEBUG_NET_WARN_ON_ONCE(skip_perm);
  399. neigh_flush_table(tbl);
  400. }
  401. spin_unlock_bh(&tbl->lock);
  402. pneigh_ifdown(tbl, dev, skip_perm);
  403. pneigh_queue_purge(&tbl->proxy_queue, dev ? dev_net(dev) : NULL,
  404. tbl->family);
  405. if (skb_queue_empty_lockless(&tbl->proxy_queue))
  406. timer_delete_sync(&tbl->proxy_timer);
  407. return 0;
  408. }
  409. int neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev)
  410. {
  411. __neigh_ifdown(tbl, dev, true);
  412. return 0;
  413. }
  414. EXPORT_SYMBOL(neigh_carrier_down);
  415. int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
  416. {
  417. __neigh_ifdown(tbl, dev, false);
  418. return 0;
  419. }
  420. EXPORT_SYMBOL(neigh_ifdown);
  421. static struct neighbour *neigh_alloc(struct neigh_table *tbl,
  422. struct net_device *dev,
  423. u32 flags, bool exempt_from_gc)
  424. {
  425. struct neighbour *n = NULL;
  426. unsigned long now = jiffies;
  427. int entries, gc_thresh3;
  428. if (exempt_from_gc)
  429. goto do_alloc;
  430. entries = atomic_inc_return(&tbl->gc_entries) - 1;
  431. gc_thresh3 = READ_ONCE(tbl->gc_thresh3);
  432. if (entries >= gc_thresh3 ||
  433. (entries >= READ_ONCE(tbl->gc_thresh2) &&
  434. time_after(now, READ_ONCE(tbl->last_flush) + 5 * HZ))) {
  435. if (!neigh_forced_gc(tbl) && entries >= gc_thresh3) {
  436. net_info_ratelimited("%s: neighbor table overflow!\n",
  437. tbl->id);
  438. NEIGH_CACHE_STAT_INC(tbl, table_fulls);
  439. goto out_entries;
  440. }
  441. }
  442. do_alloc:
  443. n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC);
  444. if (!n)
  445. goto out_entries;
  446. __skb_queue_head_init(&n->arp_queue);
  447. rwlock_init(&n->lock);
  448. seqlock_init(&n->ha_lock);
  449. n->updated = n->used = now;
  450. n->nud_state = NUD_NONE;
  451. n->output = neigh_blackhole;
  452. n->flags = flags;
  453. seqlock_init(&n->hh.hh_lock);
  454. n->parms = neigh_parms_clone(&tbl->parms);
  455. timer_setup(&n->timer, neigh_timer_handler, 0);
  456. NEIGH_CACHE_STAT_INC(tbl, allocs);
  457. n->tbl = tbl;
  458. refcount_set(&n->refcnt, 1);
  459. n->dead = 1;
  460. INIT_LIST_HEAD(&n->gc_list);
  461. INIT_LIST_HEAD(&n->managed_list);
  462. atomic_inc(&tbl->entries);
  463. out:
  464. return n;
  465. out_entries:
  466. if (!exempt_from_gc)
  467. atomic_dec(&tbl->gc_entries);
  468. goto out;
  469. }
  470. static void neigh_get_hash_rnd(u32 *x)
  471. {
  472. *x = get_random_u32() | 1;
  473. }
  474. static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
  475. {
  476. size_t size = (1 << shift) * sizeof(struct hlist_head);
  477. struct hlist_head *hash_heads;
  478. struct neigh_hash_table *ret;
  479. int i;
  480. ret = kmalloc_obj(*ret, GFP_ATOMIC);
  481. if (!ret)
  482. return NULL;
  483. hash_heads = kzalloc(size, GFP_ATOMIC);
  484. if (!hash_heads) {
  485. kfree(ret);
  486. return NULL;
  487. }
  488. ret->hash_heads = hash_heads;
  489. ret->hash_shift = shift;
  490. for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
  491. neigh_get_hash_rnd(&ret->hash_rnd[i]);
  492. return ret;
  493. }
  494. static void neigh_hash_free_rcu(struct rcu_head *head)
  495. {
  496. struct neigh_hash_table *nht = container_of(head,
  497. struct neigh_hash_table,
  498. rcu);
  499. kfree(nht->hash_heads);
  500. kfree(nht);
  501. }
  502. static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl,
  503. unsigned long new_shift)
  504. {
  505. unsigned int i, hash;
  506. struct neigh_hash_table *new_nht, *old_nht;
  507. NEIGH_CACHE_STAT_INC(tbl, hash_grows);
  508. old_nht = rcu_dereference_protected(tbl->nht,
  509. lockdep_is_held(&tbl->lock));
  510. new_nht = neigh_hash_alloc(new_shift);
  511. if (!new_nht)
  512. return old_nht;
  513. for (i = 0; i < (1 << old_nht->hash_shift); i++) {
  514. struct hlist_node *tmp;
  515. struct neighbour *n;
  516. neigh_for_each_in_bucket_safe(n, tmp, &old_nht->hash_heads[i]) {
  517. hash = tbl->hash(n->primary_key, n->dev,
  518. new_nht->hash_rnd);
  519. hash >>= (32 - new_nht->hash_shift);
  520. hlist_del_rcu(&n->hash);
  521. hlist_add_head_rcu(&n->hash, &new_nht->hash_heads[hash]);
  522. }
  523. }
  524. rcu_assign_pointer(tbl->nht, new_nht);
  525. call_rcu(&old_nht->rcu, neigh_hash_free_rcu);
  526. return new_nht;
  527. }
  528. struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
  529. struct net_device *dev)
  530. {
  531. struct neighbour *n;
  532. NEIGH_CACHE_STAT_INC(tbl, lookups);
  533. rcu_read_lock();
  534. n = __neigh_lookup_noref(tbl, pkey, dev);
  535. if (n) {
  536. if (!refcount_inc_not_zero(&n->refcnt))
  537. n = NULL;
  538. NEIGH_CACHE_STAT_INC(tbl, hits);
  539. }
  540. rcu_read_unlock();
  541. return n;
  542. }
  543. EXPORT_SYMBOL(neigh_lookup);
  544. static struct neighbour *
  545. ___neigh_create(struct neigh_table *tbl, const void *pkey,
  546. struct net_device *dev, u32 flags,
  547. bool exempt_from_gc, bool want_ref)
  548. {
  549. u32 hash_val, key_len = tbl->key_len;
  550. struct neighbour *n1, *rc, *n;
  551. struct neigh_hash_table *nht;
  552. int error;
  553. n = neigh_alloc(tbl, dev, flags, exempt_from_gc);
  554. trace_neigh_create(tbl, dev, pkey, n, exempt_from_gc);
  555. if (!n) {
  556. rc = ERR_PTR(-ENOBUFS);
  557. goto out;
  558. }
  559. memcpy(n->primary_key, pkey, key_len);
  560. n->dev = dev;
  561. netdev_hold(dev, &n->dev_tracker, GFP_ATOMIC);
  562. /* Protocol specific setup. */
  563. if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
  564. rc = ERR_PTR(error);
  565. goto out_neigh_release;
  566. }
  567. if (dev->netdev_ops->ndo_neigh_construct) {
  568. error = dev->netdev_ops->ndo_neigh_construct(dev, n);
  569. if (error < 0) {
  570. rc = ERR_PTR(error);
  571. goto out_neigh_release;
  572. }
  573. }
  574. /* Device specific setup. */
  575. if (n->parms->neigh_setup &&
  576. (error = n->parms->neigh_setup(n)) < 0) {
  577. rc = ERR_PTR(error);
  578. goto out_neigh_release;
  579. }
  580. n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1);
  581. spin_lock_bh(&tbl->lock);
  582. nht = rcu_dereference_protected(tbl->nht,
  583. lockdep_is_held(&tbl->lock));
  584. if (atomic_read(&tbl->entries) > (1 << nht->hash_shift))
  585. nht = neigh_hash_grow(tbl, nht->hash_shift + 1);
  586. hash_val = tbl->hash(n->primary_key, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
  587. if (n->parms->dead) {
  588. rc = ERR_PTR(-EINVAL);
  589. goto out_tbl_unlock;
  590. }
  591. neigh_for_each_in_bucket(n1, &nht->hash_heads[hash_val]) {
  592. if (dev == n1->dev && !memcmp(n1->primary_key, n->primary_key, key_len)) {
  593. if (want_ref)
  594. neigh_hold(n1);
  595. rc = n1;
  596. goto out_tbl_unlock;
  597. }
  598. }
  599. n->dead = 0;
  600. if (!exempt_from_gc)
  601. list_add_tail(&n->gc_list, &n->tbl->gc_list);
  602. if (n->flags & NTF_MANAGED)
  603. list_add_tail(&n->managed_list, &n->tbl->managed_list);
  604. if (want_ref)
  605. neigh_hold(n);
  606. hlist_add_head_rcu(&n->hash, &nht->hash_heads[hash_val]);
  607. hlist_add_head_rcu(&n->dev_list,
  608. neigh_get_dev_table(dev, tbl->family));
  609. spin_unlock_bh(&tbl->lock);
  610. neigh_dbg(2, "neigh %p is created\n", n);
  611. rc = n;
  612. out:
  613. return rc;
  614. out_tbl_unlock:
  615. spin_unlock_bh(&tbl->lock);
  616. out_neigh_release:
  617. if (!exempt_from_gc)
  618. atomic_dec(&tbl->gc_entries);
  619. neigh_release(n);
  620. goto out;
  621. }
  622. struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
  623. struct net_device *dev, bool want_ref)
  624. {
  625. bool exempt_from_gc = !!(dev->flags & IFF_LOOPBACK);
  626. return ___neigh_create(tbl, pkey, dev, 0, exempt_from_gc, want_ref);
  627. }
  628. EXPORT_SYMBOL(__neigh_create);
  629. static u32 pneigh_hash(const void *pkey, unsigned int key_len)
  630. {
  631. u32 hash_val = *(u32 *)(pkey + key_len - 4);
  632. hash_val ^= (hash_val >> 16);
  633. hash_val ^= hash_val >> 8;
  634. hash_val ^= hash_val >> 4;
  635. hash_val &= PNEIGH_HASHMASK;
  636. return hash_val;
  637. }
  638. struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl,
  639. struct net *net, const void *pkey,
  640. struct net_device *dev)
  641. {
  642. struct pneigh_entry *n;
  643. unsigned int key_len;
  644. u32 hash_val;
  645. key_len = tbl->key_len;
  646. hash_val = pneigh_hash(pkey, key_len);
  647. n = rcu_dereference_check(tbl->phash_buckets[hash_val],
  648. lockdep_is_held(&tbl->phash_lock));
  649. while (n) {
  650. if (!memcmp(n->key, pkey, key_len) &&
  651. net_eq(pneigh_net(n), net) &&
  652. (n->dev == dev || !n->dev))
  653. return n;
  654. n = rcu_dereference_check(n->next, lockdep_is_held(&tbl->phash_lock));
  655. }
  656. return NULL;
  657. }
  658. EXPORT_IPV6_MOD(pneigh_lookup);
  659. int pneigh_create(struct neigh_table *tbl, struct net *net,
  660. const void *pkey, struct net_device *dev,
  661. u32 flags, u8 protocol, bool permanent)
  662. {
  663. struct pneigh_entry *n;
  664. unsigned int key_len;
  665. u32 hash_val;
  666. int err = 0;
  667. mutex_lock(&tbl->phash_lock);
  668. n = pneigh_lookup(tbl, net, pkey, dev);
  669. if (n)
  670. goto update;
  671. key_len = tbl->key_len;
  672. n = kzalloc(sizeof(*n) + key_len, GFP_KERNEL);
  673. if (!n) {
  674. err = -ENOBUFS;
  675. goto out;
  676. }
  677. write_pnet(&n->net, net);
  678. memcpy(n->key, pkey, key_len);
  679. n->dev = dev;
  680. netdev_hold(dev, &n->dev_tracker, GFP_KERNEL);
  681. if (tbl->pconstructor && tbl->pconstructor(n)) {
  682. netdev_put(dev, &n->dev_tracker);
  683. kfree(n);
  684. err = -ENOBUFS;
  685. goto out;
  686. }
  687. hash_val = pneigh_hash(pkey, key_len);
  688. n->next = tbl->phash_buckets[hash_val];
  689. rcu_assign_pointer(tbl->phash_buckets[hash_val], n);
  690. update:
  691. WRITE_ONCE(n->flags, flags);
  692. n->permanent = permanent;
  693. if (protocol)
  694. WRITE_ONCE(n->protocol, protocol);
  695. out:
  696. mutex_unlock(&tbl->phash_lock);
  697. return err;
  698. }
  699. static void pneigh_destroy(struct rcu_head *rcu)
  700. {
  701. struct pneigh_entry *n = container_of(rcu, struct pneigh_entry, rcu);
  702. netdev_put(n->dev, &n->dev_tracker);
  703. kfree(n);
  704. }
  705. int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
  706. struct net_device *dev)
  707. {
  708. struct pneigh_entry *n, __rcu **np;
  709. unsigned int key_len;
  710. u32 hash_val;
  711. key_len = tbl->key_len;
  712. hash_val = pneigh_hash(pkey, key_len);
  713. mutex_lock(&tbl->phash_lock);
  714. for (np = &tbl->phash_buckets[hash_val];
  715. (n = rcu_dereference_protected(*np, 1)) != NULL;
  716. np = &n->next) {
  717. if (!memcmp(n->key, pkey, key_len) && n->dev == dev &&
  718. net_eq(pneigh_net(n), net)) {
  719. rcu_assign_pointer(*np, n->next);
  720. mutex_unlock(&tbl->phash_lock);
  721. if (tbl->pdestructor)
  722. tbl->pdestructor(n);
  723. call_rcu(&n->rcu, pneigh_destroy);
  724. return 0;
  725. }
  726. }
  727. mutex_unlock(&tbl->phash_lock);
  728. return -ENOENT;
  729. }
  730. static void pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
  731. bool skip_perm)
  732. {
  733. struct pneigh_entry *n, __rcu **np;
  734. LIST_HEAD(head);
  735. u32 h;
  736. mutex_lock(&tbl->phash_lock);
  737. for (h = 0; h <= PNEIGH_HASHMASK; h++) {
  738. np = &tbl->phash_buckets[h];
  739. while ((n = rcu_dereference_protected(*np, 1)) != NULL) {
  740. if (skip_perm && n->permanent)
  741. goto skip;
  742. if (!dev || n->dev == dev) {
  743. rcu_assign_pointer(*np, n->next);
  744. list_add(&n->free_node, &head);
  745. continue;
  746. }
  747. skip:
  748. np = &n->next;
  749. }
  750. }
  751. mutex_unlock(&tbl->phash_lock);
  752. while (!list_empty(&head)) {
  753. n = list_first_entry(&head, typeof(*n), free_node);
  754. list_del(&n->free_node);
  755. if (tbl->pdestructor)
  756. tbl->pdestructor(n);
  757. call_rcu(&n->rcu, pneigh_destroy);
  758. }
  759. }
  760. static inline void neigh_parms_put(struct neigh_parms *parms)
  761. {
  762. if (refcount_dec_and_test(&parms->refcnt))
  763. kfree(parms);
  764. }
  765. /*
  766. * neighbour must already be out of the table;
  767. *
  768. */
  769. void neigh_destroy(struct neighbour *neigh)
  770. {
  771. struct net_device *dev = neigh->dev;
  772. NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
  773. if (!neigh->dead) {
  774. pr_warn("Destroying alive neighbour %p\n", neigh);
  775. dump_stack();
  776. return;
  777. }
  778. if (neigh_del_timer(neigh))
  779. pr_warn("Impossible event\n");
  780. write_lock_bh(&neigh->lock);
  781. __skb_queue_purge(&neigh->arp_queue);
  782. write_unlock_bh(&neigh->lock);
  783. neigh->arp_queue_len_bytes = 0;
  784. if (dev->netdev_ops->ndo_neigh_destroy)
  785. dev->netdev_ops->ndo_neigh_destroy(dev, neigh);
  786. netdev_put(dev, &neigh->dev_tracker);
  787. neigh_parms_put(neigh->parms);
  788. neigh_dbg(2, "neigh %p is destroyed\n", neigh);
  789. atomic_dec(&neigh->tbl->entries);
  790. kfree_rcu(neigh, rcu);
  791. }
  792. EXPORT_SYMBOL(neigh_destroy);
  793. /* Neighbour state is suspicious;
  794. disable fast path.
  795. Called with write_locked neigh.
  796. */
  797. static void neigh_suspect(struct neighbour *neigh)
  798. {
  799. neigh_dbg(2, "neigh %p is suspected\n", neigh);
  800. WRITE_ONCE(neigh->output, neigh->ops->output);
  801. }
  802. /* Neighbour state is OK;
  803. enable fast path.
  804. Called with write_locked neigh.
  805. */
  806. static void neigh_connect(struct neighbour *neigh)
  807. {
  808. neigh_dbg(2, "neigh %p is connected\n", neigh);
  809. WRITE_ONCE(neigh->output, neigh->ops->connected_output);
  810. }
  811. static void neigh_periodic_work(struct work_struct *work)
  812. {
  813. struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work);
  814. struct neigh_hash_table *nht;
  815. struct hlist_node *tmp;
  816. struct neighbour *n;
  817. unsigned int i;
  818. NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
  819. spin_lock_bh(&tbl->lock);
  820. nht = rcu_dereference_protected(tbl->nht,
  821. lockdep_is_held(&tbl->lock));
  822. /*
  823. * periodically recompute ReachableTime from random function
  824. */
  825. if (time_after(jiffies, tbl->last_rand + 300 * HZ)) {
  826. struct neigh_parms *p;
  827. WRITE_ONCE(tbl->last_rand, jiffies);
  828. list_for_each_entry(p, &tbl->parms_list, list)
  829. neigh_set_reach_time(p);
  830. }
  831. if (atomic_read(&tbl->entries) < READ_ONCE(tbl->gc_thresh1))
  832. goto out;
  833. for (i = 0 ; i < (1 << nht->hash_shift); i++) {
  834. neigh_for_each_in_bucket_safe(n, tmp, &nht->hash_heads[i]) {
  835. unsigned int state;
  836. write_lock(&n->lock);
  837. state = n->nud_state;
  838. if ((state & (NUD_PERMANENT | NUD_IN_TIMER)) ||
  839. (n->flags &
  840. (NTF_EXT_LEARNED | NTF_EXT_VALIDATED))) {
  841. write_unlock(&n->lock);
  842. continue;
  843. }
  844. if (time_before(n->used, n->confirmed) &&
  845. time_is_before_eq_jiffies(n->confirmed))
  846. n->used = n->confirmed;
  847. if (refcount_read(&n->refcnt) == 1 &&
  848. (state == NUD_FAILED ||
  849. !time_in_range_open(jiffies, n->used,
  850. n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) {
  851. hlist_del_rcu(&n->hash);
  852. hlist_del_rcu(&n->dev_list);
  853. neigh_mark_dead(n);
  854. write_unlock(&n->lock);
  855. neigh_cleanup_and_release(n);
  856. continue;
  857. }
  858. write_unlock(&n->lock);
  859. }
  860. /*
  861. * It's fine to release lock here, even if hash table
  862. * grows while we are preempted.
  863. */
  864. spin_unlock_bh(&tbl->lock);
  865. cond_resched();
  866. spin_lock_bh(&tbl->lock);
  867. nht = rcu_dereference_protected(tbl->nht,
  868. lockdep_is_held(&tbl->lock));
  869. }
  870. out:
  871. /* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks.
  872. * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2
  873. * BASE_REACHABLE_TIME.
  874. */
  875. queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
  876. NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1);
  877. spin_unlock_bh(&tbl->lock);
  878. }
  879. static __inline__ int neigh_max_probes(struct neighbour *n)
  880. {
  881. struct neigh_parms *p = n->parms;
  882. return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
  883. (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
  884. NEIGH_VAR(p, MCAST_PROBES));
  885. }
  886. static void neigh_invalidate(struct neighbour *neigh)
  887. __releases(neigh->lock)
  888. __acquires(neigh->lock)
  889. {
  890. struct sk_buff *skb;
  891. NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
  892. neigh_dbg(2, "neigh %p is failed\n", neigh);
  893. neigh->updated = jiffies;
  894. /* It is very thin place. report_unreachable is very complicated
  895. routine. Particularly, it can hit the same neighbour entry!
  896. So that, we try to be accurate and avoid dead loop. --ANK
  897. */
  898. while (neigh->nud_state == NUD_FAILED &&
  899. (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
  900. write_unlock(&neigh->lock);
  901. neigh->ops->error_report(neigh, skb);
  902. write_lock(&neigh->lock);
  903. }
  904. __skb_queue_purge(&neigh->arp_queue);
  905. neigh->arp_queue_len_bytes = 0;
  906. }
  907. static void neigh_probe(struct neighbour *neigh)
  908. __releases(neigh->lock)
  909. {
  910. struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
  911. /* keep skb alive even if arp_queue overflows */
  912. if (skb)
  913. skb = skb_clone(skb, GFP_ATOMIC);
  914. write_unlock(&neigh->lock);
  915. if (neigh->ops->solicit)
  916. neigh->ops->solicit(neigh, skb);
  917. atomic_inc(&neigh->probes);
  918. consume_skb(skb);
  919. }
  920. /* Called when a timer expires for a neighbour entry. */
  921. static void neigh_timer_handler(struct timer_list *t)
  922. {
  923. unsigned long now, next;
  924. struct neighbour *neigh = timer_container_of(neigh, t, timer);
  925. bool skip_probe = false;
  926. unsigned int state;
  927. int notify = 0;
  928. write_lock(&neigh->lock);
  929. state = neigh->nud_state;
  930. now = jiffies;
  931. next = now + HZ;
  932. if (!(state & NUD_IN_TIMER))
  933. goto out;
  934. if (state & NUD_REACHABLE) {
  935. if (time_before_eq(now,
  936. neigh->confirmed + neigh->parms->reachable_time)) {
  937. neigh_dbg(2, "neigh %p is still alive\n", neigh);
  938. next = neigh->confirmed + neigh->parms->reachable_time;
  939. } else if (time_before_eq(now,
  940. neigh->used +
  941. NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
  942. neigh_dbg(2, "neigh %p is delayed\n", neigh);
  943. WRITE_ONCE(neigh->nud_state, NUD_DELAY);
  944. neigh->updated = jiffies;
  945. neigh_suspect(neigh);
  946. next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME);
  947. } else {
  948. neigh_dbg(2, "neigh %p is suspected\n", neigh);
  949. WRITE_ONCE(neigh->nud_state, NUD_STALE);
  950. neigh->updated = jiffies;
  951. neigh_suspect(neigh);
  952. notify = 1;
  953. }
  954. } else if (state & NUD_DELAY) {
  955. if (time_before_eq(now,
  956. neigh->confirmed +
  957. NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) {
  958. neigh_dbg(2, "neigh %p is now reachable\n", neigh);
  959. WRITE_ONCE(neigh->nud_state, NUD_REACHABLE);
  960. neigh->updated = jiffies;
  961. neigh_connect(neigh);
  962. notify = 1;
  963. next = neigh->confirmed + neigh->parms->reachable_time;
  964. } else {
  965. neigh_dbg(2, "neigh %p is probed\n", neigh);
  966. WRITE_ONCE(neigh->nud_state, NUD_PROBE);
  967. neigh->updated = jiffies;
  968. atomic_set(&neigh->probes, 0);
  969. notify = 1;
  970. next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
  971. HZ/100);
  972. }
  973. } else {
  974. /* NUD_PROBE|NUD_INCOMPLETE */
  975. next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME), HZ/100);
  976. }
  977. if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
  978. atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
  979. if (neigh->nud_state == NUD_PROBE &&
  980. neigh->flags & NTF_EXT_VALIDATED) {
  981. WRITE_ONCE(neigh->nud_state, NUD_STALE);
  982. neigh->updated = jiffies;
  983. } else {
  984. WRITE_ONCE(neigh->nud_state, NUD_FAILED);
  985. neigh_invalidate(neigh);
  986. }
  987. notify = 1;
  988. skip_probe = true;
  989. }
  990. if (notify)
  991. __neigh_notify(neigh, RTM_NEWNEIGH, 0, 0);
  992. if (skip_probe)
  993. goto out;
  994. if (neigh->nud_state & NUD_IN_TIMER) {
  995. if (time_before(next, jiffies + HZ/100))
  996. next = jiffies + HZ/100;
  997. if (!mod_timer(&neigh->timer, next))
  998. neigh_hold(neigh);
  999. }
  1000. if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
  1001. neigh_probe(neigh);
  1002. } else {
  1003. out:
  1004. write_unlock(&neigh->lock);
  1005. }
  1006. if (notify)
  1007. call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
  1008. trace_neigh_timer_handler(neigh, 0);
  1009. neigh_release(neigh);
  1010. }
  1011. int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb,
  1012. const bool immediate_ok)
  1013. {
  1014. int rc;
  1015. bool immediate_probe = false;
  1016. write_lock_bh(&neigh->lock);
  1017. rc = 0;
  1018. if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
  1019. goto out_unlock_bh;
  1020. if (neigh->dead)
  1021. goto out_dead;
  1022. if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
  1023. if (NEIGH_VAR(neigh->parms, MCAST_PROBES) +
  1024. NEIGH_VAR(neigh->parms, APP_PROBES)) {
  1025. unsigned long next, now = jiffies;
  1026. atomic_set(&neigh->probes,
  1027. NEIGH_VAR(neigh->parms, UCAST_PROBES));
  1028. neigh_del_timer(neigh);
  1029. WRITE_ONCE(neigh->nud_state, NUD_INCOMPLETE);
  1030. neigh->updated = now;
  1031. if (!immediate_ok) {
  1032. next = now + 1;
  1033. } else {
  1034. immediate_probe = true;
  1035. next = now + max(NEIGH_VAR(neigh->parms,
  1036. RETRANS_TIME),
  1037. HZ / 100);
  1038. }
  1039. neigh_add_timer(neigh, next);
  1040. } else {
  1041. WRITE_ONCE(neigh->nud_state, NUD_FAILED);
  1042. neigh->updated = jiffies;
  1043. write_unlock_bh(&neigh->lock);
  1044. kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_FAILED);
  1045. return 1;
  1046. }
  1047. } else if (neigh->nud_state & NUD_STALE) {
  1048. neigh_dbg(2, "neigh %p is delayed\n", neigh);
  1049. neigh_del_timer(neigh);
  1050. WRITE_ONCE(neigh->nud_state, NUD_DELAY);
  1051. neigh->updated = jiffies;
  1052. neigh_add_timer(neigh, jiffies +
  1053. NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME));
  1054. }
  1055. if (neigh->nud_state == NUD_INCOMPLETE) {
  1056. if (skb) {
  1057. while (neigh->arp_queue_len_bytes + skb->truesize >
  1058. NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) {
  1059. struct sk_buff *buff;
  1060. buff = __skb_dequeue(&neigh->arp_queue);
  1061. if (!buff)
  1062. break;
  1063. neigh->arp_queue_len_bytes -= buff->truesize;
  1064. kfree_skb_reason(buff, SKB_DROP_REASON_NEIGH_QUEUEFULL);
  1065. NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards);
  1066. }
  1067. skb_dst_force(skb);
  1068. __skb_queue_tail(&neigh->arp_queue, skb);
  1069. neigh->arp_queue_len_bytes += skb->truesize;
  1070. }
  1071. rc = 1;
  1072. }
  1073. out_unlock_bh:
  1074. if (immediate_probe)
  1075. neigh_probe(neigh);
  1076. else
  1077. write_unlock(&neigh->lock);
  1078. local_bh_enable();
  1079. trace_neigh_event_send_done(neigh, rc);
  1080. return rc;
  1081. out_dead:
  1082. if (neigh->nud_state & NUD_STALE)
  1083. goto out_unlock_bh;
  1084. write_unlock_bh(&neigh->lock);
  1085. kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_DEAD);
  1086. trace_neigh_event_send_dead(neigh, 1);
  1087. return 1;
  1088. }
  1089. EXPORT_SYMBOL(__neigh_event_send);
  1090. static void neigh_update_hhs(struct neighbour *neigh)
  1091. {
  1092. struct hh_cache *hh;
  1093. void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *)
  1094. = NULL;
  1095. if (neigh->dev->header_ops)
  1096. update = neigh->dev->header_ops->cache_update;
  1097. if (update) {
  1098. hh = &neigh->hh;
  1099. if (READ_ONCE(hh->hh_len)) {
  1100. write_seqlock_bh(&hh->hh_lock);
  1101. update(hh, neigh->dev, neigh->ha);
  1102. write_sequnlock_bh(&hh->hh_lock);
  1103. }
  1104. }
  1105. }
  1106. static void neigh_update_process_arp_queue(struct neighbour *neigh)
  1107. __releases(neigh->lock)
  1108. __acquires(neigh->lock)
  1109. {
  1110. struct sk_buff *skb;
  1111. /* Again: avoid deadlock if something went wrong. */
  1112. while (neigh->nud_state & NUD_VALID &&
  1113. (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
  1114. struct dst_entry *dst = skb_dst(skb);
  1115. struct neighbour *n2, *n1 = neigh;
  1116. write_unlock_bh(&neigh->lock);
  1117. rcu_read_lock();
  1118. /* Why not just use 'neigh' as-is? The problem is that
  1119. * things such as shaper, eql, and sch_teql can end up
  1120. * using alternative, different, neigh objects to output
  1121. * the packet in the output path. So what we need to do
  1122. * here is re-lookup the top-level neigh in the path so
  1123. * we can reinject the packet there.
  1124. */
  1125. n2 = NULL;
  1126. if (dst &&
  1127. READ_ONCE(dst->obsolete) != DST_OBSOLETE_DEAD) {
  1128. n2 = dst_neigh_lookup_skb(dst, skb);
  1129. if (n2)
  1130. n1 = n2;
  1131. }
  1132. READ_ONCE(n1->output)(n1, skb);
  1133. if (n2)
  1134. neigh_release(n2);
  1135. rcu_read_unlock();
  1136. write_lock_bh(&neigh->lock);
  1137. }
  1138. __skb_queue_purge(&neigh->arp_queue);
  1139. neigh->arp_queue_len_bytes = 0;
  1140. }
  1141. /* Generic update routine.
  1142. -- lladdr is new lladdr or NULL, if it is not supplied.
  1143. -- new is new state.
  1144. -- flags
  1145. NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
  1146. if it is different.
  1147. NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
  1148. lladdr instead of overriding it
  1149. if it is different.
  1150. NEIGH_UPDATE_F_ADMIN means that the change is administrative.
  1151. NEIGH_UPDATE_F_USE means that the entry is user triggered.
  1152. NEIGH_UPDATE_F_MANAGED means that the entry will be auto-refreshed.
  1153. NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
  1154. NTF_ROUTER flag.
  1155. NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as
  1156. a router.
  1157. NEIGH_UPDATE_F_EXT_VALIDATED means that the entry will not be removed
  1158. or invalidated.
  1159. Caller MUST hold reference count on the entry.
  1160. */
  1161. static int __neigh_update(struct neighbour *neigh, const u8 *lladdr,
  1162. u8 new, u32 flags, u32 nlmsg_pid,
  1163. struct netlink_ext_ack *extack)
  1164. {
  1165. bool gc_update = false, managed_update = false;
  1166. bool process_arp_queue = false;
  1167. int update_isrouter = 0;
  1168. struct net_device *dev;
  1169. int err, notify = 0;
  1170. u8 old;
  1171. trace_neigh_update(neigh, lladdr, new, flags, nlmsg_pid);
  1172. write_lock_bh(&neigh->lock);
  1173. dev = neigh->dev;
  1174. old = neigh->nud_state;
  1175. err = -EPERM;
  1176. if (neigh->dead) {
  1177. NL_SET_ERR_MSG(extack, "Neighbor entry is now dead");
  1178. new = old;
  1179. goto out;
  1180. }
  1181. if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
  1182. (old & (NUD_NOARP | NUD_PERMANENT)))
  1183. goto out;
  1184. neigh_update_flags(neigh, flags, &notify, &gc_update, &managed_update);
  1185. if (flags & (NEIGH_UPDATE_F_USE | NEIGH_UPDATE_F_MANAGED)) {
  1186. new = old & ~NUD_PERMANENT;
  1187. WRITE_ONCE(neigh->nud_state, new);
  1188. err = 0;
  1189. goto out;
  1190. }
  1191. if (!(new & NUD_VALID)) {
  1192. neigh_del_timer(neigh);
  1193. if (old & NUD_CONNECTED)
  1194. neigh_suspect(neigh);
  1195. WRITE_ONCE(neigh->nud_state, new);
  1196. err = 0;
  1197. notify = old & NUD_VALID;
  1198. if ((old & (NUD_INCOMPLETE | NUD_PROBE)) &&
  1199. (new & NUD_FAILED)) {
  1200. neigh_invalidate(neigh);
  1201. notify = 1;
  1202. }
  1203. goto out;
  1204. }
  1205. /* Compare new lladdr with cached one */
  1206. if (!dev->addr_len) {
  1207. /* First case: device needs no address. */
  1208. lladdr = neigh->ha;
  1209. } else if (lladdr) {
  1210. /* The second case: if something is already cached
  1211. and a new address is proposed:
  1212. - compare new & old
  1213. - if they are different, check override flag
  1214. */
  1215. if ((old & NUD_VALID) &&
  1216. !memcmp(lladdr, neigh->ha, dev->addr_len))
  1217. lladdr = neigh->ha;
  1218. } else {
  1219. /* No address is supplied; if we know something,
  1220. use it, otherwise discard the request.
  1221. */
  1222. err = -EINVAL;
  1223. if (!(old & NUD_VALID)) {
  1224. NL_SET_ERR_MSG(extack, "No link layer address given");
  1225. goto out;
  1226. }
  1227. lladdr = neigh->ha;
  1228. }
  1229. /* Update confirmed timestamp for neighbour entry after we
  1230. * received ARP packet even if it doesn't change IP to MAC binding.
  1231. */
  1232. if (new & NUD_CONNECTED)
  1233. neigh->confirmed = jiffies;
  1234. /* If entry was valid and address is not changed,
  1235. do not change entry state, if new one is STALE.
  1236. */
  1237. err = 0;
  1238. update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
  1239. if (old & NUD_VALID) {
  1240. if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
  1241. update_isrouter = 0;
  1242. if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
  1243. (old & NUD_CONNECTED)) {
  1244. lladdr = neigh->ha;
  1245. new = NUD_STALE;
  1246. } else
  1247. goto out;
  1248. } else {
  1249. if (lladdr == neigh->ha && new == NUD_STALE &&
  1250. !(flags & NEIGH_UPDATE_F_ADMIN))
  1251. new = old;
  1252. }
  1253. }
  1254. /* Update timestamp only once we know we will make a change to the
  1255. * neighbour entry. Otherwise we risk to move the locktime window with
  1256. * noop updates and ignore relevant ARP updates.
  1257. */
  1258. if (new != old || lladdr != neigh->ha)
  1259. neigh->updated = jiffies;
  1260. if (new != old) {
  1261. neigh_del_timer(neigh);
  1262. if (new & NUD_PROBE)
  1263. atomic_set(&neigh->probes, 0);
  1264. if (new & NUD_IN_TIMER)
  1265. neigh_add_timer(neigh, (jiffies +
  1266. ((new & NUD_REACHABLE) ?
  1267. neigh->parms->reachable_time :
  1268. 0)));
  1269. WRITE_ONCE(neigh->nud_state, new);
  1270. notify = 1;
  1271. }
  1272. if (lladdr != neigh->ha) {
  1273. write_seqlock(&neigh->ha_lock);
  1274. memcpy(&neigh->ha, lladdr, dev->addr_len);
  1275. write_sequnlock(&neigh->ha_lock);
  1276. neigh_update_hhs(neigh);
  1277. if (!(new & NUD_CONNECTED))
  1278. neigh->confirmed = jiffies -
  1279. (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1);
  1280. notify = 1;
  1281. }
  1282. if (new == old)
  1283. goto out;
  1284. if (new & NUD_CONNECTED)
  1285. neigh_connect(neigh);
  1286. else
  1287. neigh_suspect(neigh);
  1288. if (!(old & NUD_VALID))
  1289. process_arp_queue = true;
  1290. out:
  1291. if (update_isrouter)
  1292. neigh_update_is_router(neigh, flags, &notify);
  1293. if (notify)
  1294. __neigh_notify(neigh, RTM_NEWNEIGH, 0, nlmsg_pid);
  1295. if (process_arp_queue)
  1296. neigh_update_process_arp_queue(neigh);
  1297. write_unlock_bh(&neigh->lock);
  1298. if (((new ^ old) & NUD_PERMANENT) || gc_update)
  1299. neigh_update_gc_list(neigh);
  1300. if (managed_update)
  1301. neigh_update_managed_list(neigh);
  1302. if (notify)
  1303. call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh);
  1304. trace_neigh_update_done(neigh, err);
  1305. return err;
  1306. }
  1307. int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
  1308. u32 flags, u32 nlmsg_pid)
  1309. {
  1310. return __neigh_update(neigh, lladdr, new, flags, nlmsg_pid, NULL);
  1311. }
  1312. EXPORT_SYMBOL(neigh_update);
  1313. /* Update the neigh to listen temporarily for probe responses, even if it is
  1314. * in a NUD_FAILED state. The caller has to hold neigh->lock for writing.
  1315. */
  1316. void __neigh_set_probe_once(struct neighbour *neigh)
  1317. {
  1318. if (neigh->dead)
  1319. return;
  1320. neigh->updated = jiffies;
  1321. if (!(neigh->nud_state & NUD_FAILED))
  1322. return;
  1323. WRITE_ONCE(neigh->nud_state, NUD_INCOMPLETE);
  1324. atomic_set(&neigh->probes, neigh_max_probes(neigh));
  1325. neigh_add_timer(neigh,
  1326. jiffies + max(NEIGH_VAR(neigh->parms, RETRANS_TIME),
  1327. HZ/100));
  1328. }
  1329. EXPORT_SYMBOL(__neigh_set_probe_once);
  1330. struct neighbour *neigh_event_ns(struct neigh_table *tbl,
  1331. u8 *lladdr, void *saddr,
  1332. struct net_device *dev)
  1333. {
  1334. struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
  1335. lladdr || !dev->addr_len);
  1336. if (neigh)
  1337. neigh_update(neigh, lladdr, NUD_STALE,
  1338. NEIGH_UPDATE_F_OVERRIDE, 0);
  1339. return neigh;
  1340. }
  1341. EXPORT_SYMBOL(neigh_event_ns);
  1342. /* called with read_lock_bh(&n->lock); */
  1343. static void neigh_hh_init(struct neighbour *n)
  1344. {
  1345. struct net_device *dev = n->dev;
  1346. __be16 prot = n->tbl->protocol;
  1347. struct hh_cache *hh = &n->hh;
  1348. write_lock_bh(&n->lock);
  1349. /* Only one thread can come in here and initialize the
  1350. * hh_cache entry.
  1351. */
  1352. if (!hh->hh_len)
  1353. dev->header_ops->cache(n, hh, prot);
  1354. write_unlock_bh(&n->lock);
  1355. }
  1356. /* Slow and careful. */
  1357. int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
  1358. {
  1359. int rc = 0;
  1360. if (!neigh_event_send(neigh, skb)) {
  1361. int err;
  1362. struct net_device *dev = neigh->dev;
  1363. unsigned int seq;
  1364. if (dev->header_ops->cache && !READ_ONCE(neigh->hh.hh_len))
  1365. neigh_hh_init(neigh);
  1366. do {
  1367. __skb_pull(skb, skb_network_offset(skb));
  1368. seq = read_seqbegin(&neigh->ha_lock);
  1369. err = dev_hard_header(skb, dev, ntohs(skb->protocol),
  1370. neigh->ha, NULL, skb->len);
  1371. } while (read_seqretry(&neigh->ha_lock, seq));
  1372. if (err >= 0)
  1373. rc = dev_queue_xmit(skb);
  1374. else
  1375. goto out_kfree_skb;
  1376. }
  1377. out:
  1378. return rc;
  1379. out_kfree_skb:
  1380. rc = -EINVAL;
  1381. kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_HH_FILLFAIL);
  1382. goto out;
  1383. }
  1384. EXPORT_SYMBOL(neigh_resolve_output);
  1385. /* As fast as possible without hh cache */
  1386. int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb)
  1387. {
  1388. struct net_device *dev = neigh->dev;
  1389. unsigned int seq;
  1390. int err;
  1391. do {
  1392. __skb_pull(skb, skb_network_offset(skb));
  1393. seq = read_seqbegin(&neigh->ha_lock);
  1394. err = dev_hard_header(skb, dev, ntohs(skb->protocol),
  1395. neigh->ha, NULL, skb->len);
  1396. } while (read_seqretry(&neigh->ha_lock, seq));
  1397. if (err >= 0)
  1398. err = dev_queue_xmit(skb);
  1399. else {
  1400. err = -EINVAL;
  1401. kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_HH_FILLFAIL);
  1402. }
  1403. return err;
  1404. }
  1405. EXPORT_SYMBOL(neigh_connected_output);
  1406. int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb)
  1407. {
  1408. return dev_queue_xmit(skb);
  1409. }
  1410. EXPORT_SYMBOL(neigh_direct_output);
  1411. static void neigh_managed_work(struct work_struct *work)
  1412. {
  1413. struct neigh_table *tbl = container_of(work, struct neigh_table,
  1414. managed_work.work);
  1415. struct neighbour *neigh;
  1416. spin_lock_bh(&tbl->lock);
  1417. list_for_each_entry(neigh, &tbl->managed_list, managed_list)
  1418. neigh_event_send_probe(neigh, NULL, false);
  1419. queue_delayed_work(system_power_efficient_wq, &tbl->managed_work,
  1420. NEIGH_VAR(&tbl->parms, INTERVAL_PROBE_TIME_MS));
  1421. spin_unlock_bh(&tbl->lock);
  1422. }
  1423. static void neigh_proxy_process(struct timer_list *t)
  1424. {
  1425. struct neigh_table *tbl = timer_container_of(tbl, t, proxy_timer);
  1426. long sched_next = 0;
  1427. unsigned long now = jiffies;
  1428. struct sk_buff *skb, *n;
  1429. spin_lock(&tbl->proxy_queue.lock);
  1430. skb_queue_walk_safe(&tbl->proxy_queue, skb, n) {
  1431. long tdif = NEIGH_CB(skb)->sched_next - now;
  1432. if (tdif <= 0) {
  1433. struct net_device *dev = skb->dev;
  1434. neigh_parms_qlen_dec(dev, tbl->family);
  1435. __skb_unlink(skb, &tbl->proxy_queue);
  1436. if (tbl->proxy_redo && netif_running(dev)) {
  1437. rcu_read_lock();
  1438. tbl->proxy_redo(skb);
  1439. rcu_read_unlock();
  1440. } else {
  1441. kfree_skb(skb);
  1442. }
  1443. dev_put(dev);
  1444. } else if (!sched_next || tdif < sched_next)
  1445. sched_next = tdif;
  1446. }
  1447. timer_delete(&tbl->proxy_timer);
  1448. if (sched_next)
  1449. mod_timer(&tbl->proxy_timer, jiffies + sched_next);
  1450. spin_unlock(&tbl->proxy_queue.lock);
  1451. }
  1452. static unsigned long neigh_proxy_delay(struct neigh_parms *p)
  1453. {
  1454. /* If proxy_delay is zero, do not call get_random_u32_below()
  1455. * as it is undefined behavior.
  1456. */
  1457. unsigned long proxy_delay = NEIGH_VAR(p, PROXY_DELAY);
  1458. return proxy_delay ?
  1459. jiffies + get_random_u32_below(proxy_delay) : jiffies;
  1460. }
  1461. void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
  1462. struct sk_buff *skb)
  1463. {
  1464. unsigned long sched_next = neigh_proxy_delay(p);
  1465. if (p->qlen > NEIGH_VAR(p, PROXY_QLEN)) {
  1466. kfree_skb(skb);
  1467. return;
  1468. }
  1469. NEIGH_CB(skb)->sched_next = sched_next;
  1470. NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
  1471. spin_lock(&tbl->proxy_queue.lock);
  1472. if (timer_delete(&tbl->proxy_timer)) {
  1473. if (time_before(tbl->proxy_timer.expires, sched_next))
  1474. sched_next = tbl->proxy_timer.expires;
  1475. }
  1476. skb_dst_drop(skb);
  1477. dev_hold(skb->dev);
  1478. __skb_queue_tail(&tbl->proxy_queue, skb);
  1479. p->qlen++;
  1480. mod_timer(&tbl->proxy_timer, sched_next);
  1481. spin_unlock(&tbl->proxy_queue.lock);
  1482. }
  1483. EXPORT_SYMBOL(pneigh_enqueue);
  1484. static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,
  1485. struct net *net, int ifindex)
  1486. {
  1487. struct neigh_parms *p;
  1488. list_for_each_entry(p, &tbl->parms_list, list) {
  1489. if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) ||
  1490. (!p->dev && !ifindex && net_eq(net, &init_net)))
  1491. return p;
  1492. }
  1493. return NULL;
  1494. }
  1495. struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
  1496. struct neigh_table *tbl)
  1497. {
  1498. struct neigh_parms *p;
  1499. struct net *net = dev_net(dev);
  1500. const struct net_device_ops *ops = dev->netdev_ops;
  1501. p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL);
  1502. if (p) {
  1503. p->tbl = tbl;
  1504. refcount_set(&p->refcnt, 1);
  1505. neigh_set_reach_time(p);
  1506. p->qlen = 0;
  1507. netdev_hold(dev, &p->dev_tracker, GFP_KERNEL);
  1508. p->dev = dev;
  1509. write_pnet(&p->net, net);
  1510. p->sysctl_table = NULL;
  1511. if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
  1512. netdev_put(dev, &p->dev_tracker);
  1513. kfree(p);
  1514. return NULL;
  1515. }
  1516. spin_lock_bh(&tbl->lock);
  1517. list_add_rcu(&p->list, &tbl->parms.list);
  1518. spin_unlock_bh(&tbl->lock);
  1519. neigh_parms_data_state_cleanall(p);
  1520. }
  1521. return p;
  1522. }
  1523. EXPORT_SYMBOL(neigh_parms_alloc);
  1524. static void neigh_rcu_free_parms(struct rcu_head *head)
  1525. {
  1526. struct neigh_parms *parms =
  1527. container_of(head, struct neigh_parms, rcu_head);
  1528. neigh_parms_put(parms);
  1529. }
  1530. void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
  1531. {
  1532. if (!parms || parms == &tbl->parms)
  1533. return;
  1534. spin_lock_bh(&tbl->lock);
  1535. list_del_rcu(&parms->list);
  1536. parms->dead = 1;
  1537. spin_unlock_bh(&tbl->lock);
  1538. netdev_put(parms->dev, &parms->dev_tracker);
  1539. call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
  1540. }
  1541. EXPORT_SYMBOL(neigh_parms_release);
  1542. static struct lock_class_key neigh_table_proxy_queue_class;
  1543. static struct neigh_table __rcu *neigh_tables[NEIGH_NR_TABLES] __read_mostly;
  1544. void neigh_table_init(int index, struct neigh_table *tbl)
  1545. {
  1546. unsigned long now = jiffies;
  1547. unsigned long phsize;
  1548. INIT_LIST_HEAD(&tbl->parms_list);
  1549. INIT_LIST_HEAD(&tbl->gc_list);
  1550. INIT_LIST_HEAD(&tbl->managed_list);
  1551. list_add(&tbl->parms.list, &tbl->parms_list);
  1552. write_pnet(&tbl->parms.net, &init_net);
  1553. refcount_set(&tbl->parms.refcnt, 1);
  1554. neigh_set_reach_time(&tbl->parms);
  1555. tbl->parms.qlen = 0;
  1556. tbl->stats = alloc_percpu(struct neigh_statistics);
  1557. if (!tbl->stats)
  1558. panic("cannot create neighbour cache statistics");
  1559. #ifdef CONFIG_PROC_FS
  1560. if (!proc_create_seq_data(tbl->id, 0, init_net.proc_net_stat,
  1561. &neigh_stat_seq_ops, tbl))
  1562. panic("cannot create neighbour proc dir entry");
  1563. #endif
  1564. RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3));
  1565. phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
  1566. tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL);
  1567. if (!tbl->nht || !tbl->phash_buckets)
  1568. panic("cannot allocate neighbour cache hashes");
  1569. if (!tbl->entry_size)
  1570. tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) +
  1571. tbl->key_len, NEIGH_PRIV_ALIGN);
  1572. else
  1573. WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN);
  1574. spin_lock_init(&tbl->lock);
  1575. mutex_init(&tbl->phash_lock);
  1576. INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work);
  1577. queue_delayed_work(system_power_efficient_wq, &tbl->gc_work,
  1578. tbl->parms.reachable_time);
  1579. INIT_DEFERRABLE_WORK(&tbl->managed_work, neigh_managed_work);
  1580. queue_delayed_work(system_power_efficient_wq, &tbl->managed_work, 0);
  1581. timer_setup(&tbl->proxy_timer, neigh_proxy_process, 0);
  1582. skb_queue_head_init_class(&tbl->proxy_queue,
  1583. &neigh_table_proxy_queue_class);
  1584. tbl->last_flush = now;
  1585. tbl->last_rand = now + tbl->parms.reachable_time * 20;
  1586. rcu_assign_pointer(neigh_tables[index], tbl);
  1587. }
  1588. EXPORT_SYMBOL(neigh_table_init);
  1589. /*
  1590. * Only called from ndisc_cleanup(), which means this is dead code
  1591. * because we no longer can unload IPv6 module.
  1592. */
  1593. int neigh_table_clear(int index, struct neigh_table *tbl)
  1594. {
  1595. RCU_INIT_POINTER(neigh_tables[index], NULL);
  1596. synchronize_rcu();
  1597. /* It is not clean... Fix it to unload IPv6 module safely */
  1598. cancel_delayed_work_sync(&tbl->managed_work);
  1599. cancel_delayed_work_sync(&tbl->gc_work);
  1600. timer_delete_sync(&tbl->proxy_timer);
  1601. pneigh_queue_purge(&tbl->proxy_queue, NULL, tbl->family);
  1602. neigh_ifdown(tbl, NULL);
  1603. if (atomic_read(&tbl->entries))
  1604. pr_crit("neighbour leakage\n");
  1605. call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu,
  1606. neigh_hash_free_rcu);
  1607. tbl->nht = NULL;
  1608. kfree(tbl->phash_buckets);
  1609. tbl->phash_buckets = NULL;
  1610. remove_proc_entry(tbl->id, init_net.proc_net_stat);
  1611. free_percpu(tbl->stats);
  1612. tbl->stats = NULL;
  1613. return 0;
  1614. }
  1615. EXPORT_SYMBOL(neigh_table_clear);
  1616. static struct neigh_table *neigh_find_table(int family)
  1617. {
  1618. struct neigh_table *tbl = NULL;
  1619. switch (family) {
  1620. case AF_INET:
  1621. tbl = rcu_dereference_rtnl(neigh_tables[NEIGH_ARP_TABLE]);
  1622. break;
  1623. case AF_INET6:
  1624. tbl = rcu_dereference_rtnl(neigh_tables[NEIGH_ND_TABLE]);
  1625. break;
  1626. }
  1627. return tbl;
  1628. }
  1629. const struct nla_policy nda_policy[NDA_MAX+1] = {
  1630. [NDA_UNSPEC] = { .strict_start_type = NDA_NH_ID },
  1631. [NDA_DST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  1632. [NDA_LLADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  1633. [NDA_CACHEINFO] = { .len = sizeof(struct nda_cacheinfo) },
  1634. [NDA_PROBES] = { .type = NLA_U32 },
  1635. [NDA_VLAN] = { .type = NLA_U16 },
  1636. [NDA_PORT] = { .type = NLA_U16 },
  1637. [NDA_VNI] = { .type = NLA_U32 },
  1638. [NDA_IFINDEX] = { .type = NLA_U32 },
  1639. [NDA_MASTER] = { .type = NLA_U32 },
  1640. [NDA_PROTOCOL] = { .type = NLA_U8 },
  1641. [NDA_NH_ID] = { .type = NLA_U32 },
  1642. [NDA_FLAGS_EXT] = NLA_POLICY_MASK(NLA_U32, NTF_EXT_MASK),
  1643. [NDA_FDB_EXT_ATTRS] = { .type = NLA_NESTED },
  1644. };
  1645. static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh,
  1646. struct netlink_ext_ack *extack)
  1647. {
  1648. struct net *net = sock_net(skb->sk);
  1649. struct ndmsg *ndm;
  1650. struct nlattr *dst_attr;
  1651. struct neigh_table *tbl;
  1652. struct neighbour *neigh;
  1653. struct net_device *dev = NULL;
  1654. int err = -EINVAL;
  1655. ASSERT_RTNL();
  1656. if (nlmsg_len(nlh) < sizeof(*ndm))
  1657. goto out;
  1658. dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST);
  1659. if (!dst_attr) {
  1660. NL_SET_ERR_MSG(extack, "Network address not specified");
  1661. goto out;
  1662. }
  1663. ndm = nlmsg_data(nlh);
  1664. if (ndm->ndm_ifindex) {
  1665. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  1666. if (dev == NULL) {
  1667. err = -ENODEV;
  1668. goto out;
  1669. }
  1670. }
  1671. tbl = neigh_find_table(ndm->ndm_family);
  1672. if (tbl == NULL)
  1673. return -EAFNOSUPPORT;
  1674. if (nla_len(dst_attr) < (int)tbl->key_len) {
  1675. NL_SET_ERR_MSG(extack, "Invalid network address");
  1676. goto out;
  1677. }
  1678. if (ndm->ndm_flags & NTF_PROXY) {
  1679. err = pneigh_delete(tbl, net, nla_data(dst_attr), dev);
  1680. goto out;
  1681. }
  1682. if (dev == NULL)
  1683. goto out;
  1684. neigh = neigh_lookup(tbl, nla_data(dst_attr), dev);
  1685. if (neigh == NULL) {
  1686. err = -ENOENT;
  1687. goto out;
  1688. }
  1689. err = __neigh_update(neigh, NULL, NUD_FAILED,
  1690. NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN,
  1691. NETLINK_CB(skb).portid, extack);
  1692. spin_lock_bh(&tbl->lock);
  1693. neigh_release(neigh);
  1694. neigh_remove_one(neigh);
  1695. spin_unlock_bh(&tbl->lock);
  1696. out:
  1697. return err;
  1698. }
  1699. static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
  1700. struct netlink_ext_ack *extack)
  1701. {
  1702. int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE |
  1703. NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
  1704. struct net *net = sock_net(skb->sk);
  1705. struct ndmsg *ndm;
  1706. struct nlattr *tb[NDA_MAX+1];
  1707. struct neigh_table *tbl;
  1708. struct net_device *dev = NULL;
  1709. struct neighbour *neigh;
  1710. void *dst, *lladdr;
  1711. u8 protocol = 0;
  1712. u32 ndm_flags;
  1713. int err;
  1714. ASSERT_RTNL();
  1715. err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
  1716. nda_policy, extack);
  1717. if (err < 0)
  1718. goto out;
  1719. err = -EINVAL;
  1720. if (!tb[NDA_DST]) {
  1721. NL_SET_ERR_MSG(extack, "Network address not specified");
  1722. goto out;
  1723. }
  1724. ndm = nlmsg_data(nlh);
  1725. ndm_flags = ndm->ndm_flags;
  1726. if (tb[NDA_FLAGS_EXT]) {
  1727. u32 ext = nla_get_u32(tb[NDA_FLAGS_EXT]);
  1728. BUILD_BUG_ON(sizeof(neigh->flags) * BITS_PER_BYTE <
  1729. (sizeof(ndm->ndm_flags) * BITS_PER_BYTE +
  1730. hweight32(NTF_EXT_MASK)));
  1731. ndm_flags |= (ext << NTF_EXT_SHIFT);
  1732. }
  1733. if (ndm->ndm_ifindex) {
  1734. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  1735. if (dev == NULL) {
  1736. err = -ENODEV;
  1737. goto out;
  1738. }
  1739. if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) {
  1740. NL_SET_ERR_MSG(extack, "Invalid link address");
  1741. goto out;
  1742. }
  1743. }
  1744. tbl = neigh_find_table(ndm->ndm_family);
  1745. if (tbl == NULL)
  1746. return -EAFNOSUPPORT;
  1747. if (nla_len(tb[NDA_DST]) < (int)tbl->key_len) {
  1748. NL_SET_ERR_MSG(extack, "Invalid network address");
  1749. goto out;
  1750. }
  1751. dst = nla_data(tb[NDA_DST]);
  1752. lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL;
  1753. if (tb[NDA_PROTOCOL])
  1754. protocol = nla_get_u8(tb[NDA_PROTOCOL]);
  1755. if (ndm_flags & NTF_PROXY) {
  1756. if (ndm_flags & (NTF_MANAGED | NTF_EXT_VALIDATED)) {
  1757. NL_SET_ERR_MSG(extack, "Invalid NTF_* flag combination");
  1758. goto out;
  1759. }
  1760. err = pneigh_create(tbl, net, dst, dev, ndm_flags, protocol,
  1761. !!(ndm->ndm_state & NUD_PERMANENT));
  1762. goto out;
  1763. }
  1764. if (!dev) {
  1765. NL_SET_ERR_MSG(extack, "Device not specified");
  1766. goto out;
  1767. }
  1768. if (tbl->allow_add && !tbl->allow_add(dev, extack)) {
  1769. err = -EINVAL;
  1770. goto out;
  1771. }
  1772. neigh = neigh_lookup(tbl, dst, dev);
  1773. if (neigh == NULL) {
  1774. bool ndm_permanent = ndm->ndm_state & NUD_PERMANENT;
  1775. bool exempt_from_gc = ndm_permanent ||
  1776. ndm_flags & (NTF_EXT_LEARNED |
  1777. NTF_EXT_VALIDATED);
  1778. if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
  1779. err = -ENOENT;
  1780. goto out;
  1781. }
  1782. if (ndm_permanent && (ndm_flags & NTF_MANAGED)) {
  1783. NL_SET_ERR_MSG(extack, "Invalid NTF_* flag for permanent entry");
  1784. err = -EINVAL;
  1785. goto out;
  1786. }
  1787. if (ndm_flags & NTF_EXT_VALIDATED) {
  1788. u8 state = ndm->ndm_state;
  1789. /* NTF_USE and NTF_MANAGED will result in the neighbor
  1790. * being created with an invalid state (NUD_NONE).
  1791. */
  1792. if (ndm_flags & (NTF_USE | NTF_MANAGED))
  1793. state = NUD_NONE;
  1794. if (!(state & NUD_VALID)) {
  1795. NL_SET_ERR_MSG(extack,
  1796. "Cannot create externally validated neighbor with an invalid state");
  1797. err = -EINVAL;
  1798. goto out;
  1799. }
  1800. }
  1801. neigh = ___neigh_create(tbl, dst, dev,
  1802. ndm_flags &
  1803. (NTF_EXT_LEARNED | NTF_MANAGED |
  1804. NTF_EXT_VALIDATED),
  1805. exempt_from_gc, true);
  1806. if (IS_ERR(neigh)) {
  1807. err = PTR_ERR(neigh);
  1808. goto out;
  1809. }
  1810. } else {
  1811. if (nlh->nlmsg_flags & NLM_F_EXCL) {
  1812. err = -EEXIST;
  1813. neigh_release(neigh);
  1814. goto out;
  1815. }
  1816. if (ndm_flags & NTF_EXT_VALIDATED) {
  1817. u8 state = ndm->ndm_state;
  1818. /* NTF_USE and NTF_MANAGED do not update the existing
  1819. * state other than clearing it if it was
  1820. * NUD_PERMANENT.
  1821. */
  1822. if (ndm_flags & (NTF_USE | NTF_MANAGED))
  1823. state = READ_ONCE(neigh->nud_state) & ~NUD_PERMANENT;
  1824. if (!(state & NUD_VALID)) {
  1825. NL_SET_ERR_MSG(extack,
  1826. "Cannot mark neighbor as externally validated with an invalid state");
  1827. err = -EINVAL;
  1828. neigh_release(neigh);
  1829. goto out;
  1830. }
  1831. }
  1832. if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
  1833. flags &= ~(NEIGH_UPDATE_F_OVERRIDE |
  1834. NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
  1835. }
  1836. if (protocol)
  1837. neigh->protocol = protocol;
  1838. if (ndm_flags & NTF_EXT_LEARNED)
  1839. flags |= NEIGH_UPDATE_F_EXT_LEARNED;
  1840. if (ndm_flags & NTF_ROUTER)
  1841. flags |= NEIGH_UPDATE_F_ISROUTER;
  1842. if (ndm_flags & NTF_MANAGED)
  1843. flags |= NEIGH_UPDATE_F_MANAGED;
  1844. if (ndm_flags & NTF_USE)
  1845. flags |= NEIGH_UPDATE_F_USE;
  1846. if (ndm_flags & NTF_EXT_VALIDATED)
  1847. flags |= NEIGH_UPDATE_F_EXT_VALIDATED;
  1848. err = __neigh_update(neigh, lladdr, ndm->ndm_state, flags,
  1849. NETLINK_CB(skb).portid, extack);
  1850. if (!err && ndm_flags & (NTF_USE | NTF_MANAGED))
  1851. neigh_event_send(neigh, NULL);
  1852. neigh_release(neigh);
  1853. out:
  1854. return err;
  1855. }
  1856. static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
  1857. {
  1858. struct nlattr *nest;
  1859. nest = nla_nest_start_noflag(skb, NDTA_PARMS);
  1860. if (nest == NULL)
  1861. return -ENOBUFS;
  1862. if ((parms->dev &&
  1863. nla_put_u32(skb, NDTPA_IFINDEX, READ_ONCE(parms->dev->ifindex))) ||
  1864. nla_put_u32(skb, NDTPA_REFCNT, refcount_read(&parms->refcnt)) ||
  1865. nla_put_u32(skb, NDTPA_QUEUE_LENBYTES,
  1866. NEIGH_VAR(parms, QUEUE_LEN_BYTES)) ||
  1867. /* approximative value for deprecated QUEUE_LEN (in packets) */
  1868. nla_put_u32(skb, NDTPA_QUEUE_LEN,
  1869. NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) ||
  1870. nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) ||
  1871. nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) ||
  1872. nla_put_u32(skb, NDTPA_UCAST_PROBES,
  1873. NEIGH_VAR(parms, UCAST_PROBES)) ||
  1874. nla_put_u32(skb, NDTPA_MCAST_PROBES,
  1875. NEIGH_VAR(parms, MCAST_PROBES)) ||
  1876. nla_put_u32(skb, NDTPA_MCAST_REPROBES,
  1877. NEIGH_VAR(parms, MCAST_REPROBES)) ||
  1878. nla_put_msecs(skb, NDTPA_REACHABLE_TIME, READ_ONCE(parms->reachable_time),
  1879. NDTPA_PAD) ||
  1880. nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
  1881. NEIGH_VAR(parms, BASE_REACHABLE_TIME), NDTPA_PAD) ||
  1882. nla_put_msecs(skb, NDTPA_GC_STALETIME,
  1883. NEIGH_VAR(parms, GC_STALETIME), NDTPA_PAD) ||
  1884. nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME,
  1885. NEIGH_VAR(parms, DELAY_PROBE_TIME), NDTPA_PAD) ||
  1886. nla_put_msecs(skb, NDTPA_RETRANS_TIME,
  1887. NEIGH_VAR(parms, RETRANS_TIME), NDTPA_PAD) ||
  1888. nla_put_msecs(skb, NDTPA_ANYCAST_DELAY,
  1889. NEIGH_VAR(parms, ANYCAST_DELAY), NDTPA_PAD) ||
  1890. nla_put_msecs(skb, NDTPA_PROXY_DELAY,
  1891. NEIGH_VAR(parms, PROXY_DELAY), NDTPA_PAD) ||
  1892. nla_put_msecs(skb, NDTPA_LOCKTIME,
  1893. NEIGH_VAR(parms, LOCKTIME), NDTPA_PAD) ||
  1894. nla_put_msecs(skb, NDTPA_INTERVAL_PROBE_TIME_MS,
  1895. NEIGH_VAR(parms, INTERVAL_PROBE_TIME_MS), NDTPA_PAD))
  1896. goto nla_put_failure;
  1897. return nla_nest_end(skb, nest);
  1898. nla_put_failure:
  1899. nla_nest_cancel(skb, nest);
  1900. return -EMSGSIZE;
  1901. }
  1902. static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
  1903. u32 pid, u32 seq, int type, int flags)
  1904. {
  1905. struct nlmsghdr *nlh;
  1906. struct ndtmsg *ndtmsg;
  1907. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
  1908. if (nlh == NULL)
  1909. return -EMSGSIZE;
  1910. ndtmsg = nlmsg_data(nlh);
  1911. ndtmsg->ndtm_family = tbl->family;
  1912. ndtmsg->ndtm_pad1 = 0;
  1913. ndtmsg->ndtm_pad2 = 0;
  1914. if (nla_put_string(skb, NDTA_NAME, tbl->id) ||
  1915. nla_put_msecs(skb, NDTA_GC_INTERVAL, READ_ONCE(tbl->gc_interval),
  1916. NDTA_PAD) ||
  1917. nla_put_u32(skb, NDTA_THRESH1, READ_ONCE(tbl->gc_thresh1)) ||
  1918. nla_put_u32(skb, NDTA_THRESH2, READ_ONCE(tbl->gc_thresh2)) ||
  1919. nla_put_u32(skb, NDTA_THRESH3, READ_ONCE(tbl->gc_thresh3)))
  1920. goto nla_put_failure;
  1921. {
  1922. unsigned long now = jiffies;
  1923. long flush_delta = now - READ_ONCE(tbl->last_flush);
  1924. long rand_delta = now - READ_ONCE(tbl->last_rand);
  1925. struct neigh_hash_table *nht;
  1926. struct ndt_config ndc = {
  1927. .ndtc_key_len = tbl->key_len,
  1928. .ndtc_entry_size = tbl->entry_size,
  1929. .ndtc_entries = atomic_read(&tbl->entries),
  1930. .ndtc_last_flush = jiffies_to_msecs(flush_delta),
  1931. .ndtc_last_rand = jiffies_to_msecs(rand_delta),
  1932. .ndtc_proxy_qlen = READ_ONCE(tbl->proxy_queue.qlen),
  1933. };
  1934. nht = rcu_dereference(tbl->nht);
  1935. ndc.ndtc_hash_rnd = nht->hash_rnd[0];
  1936. ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
  1937. if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc))
  1938. goto nla_put_failure;
  1939. }
  1940. {
  1941. int cpu;
  1942. struct ndt_stats ndst;
  1943. memset(&ndst, 0, sizeof(ndst));
  1944. for_each_possible_cpu(cpu) {
  1945. struct neigh_statistics *st;
  1946. st = per_cpu_ptr(tbl->stats, cpu);
  1947. ndst.ndts_allocs += READ_ONCE(st->allocs);
  1948. ndst.ndts_destroys += READ_ONCE(st->destroys);
  1949. ndst.ndts_hash_grows += READ_ONCE(st->hash_grows);
  1950. ndst.ndts_res_failed += READ_ONCE(st->res_failed);
  1951. ndst.ndts_lookups += READ_ONCE(st->lookups);
  1952. ndst.ndts_hits += READ_ONCE(st->hits);
  1953. ndst.ndts_rcv_probes_mcast += READ_ONCE(st->rcv_probes_mcast);
  1954. ndst.ndts_rcv_probes_ucast += READ_ONCE(st->rcv_probes_ucast);
  1955. ndst.ndts_periodic_gc_runs += READ_ONCE(st->periodic_gc_runs);
  1956. ndst.ndts_forced_gc_runs += READ_ONCE(st->forced_gc_runs);
  1957. ndst.ndts_table_fulls += READ_ONCE(st->table_fulls);
  1958. }
  1959. if (nla_put_64bit(skb, NDTA_STATS, sizeof(ndst), &ndst,
  1960. NDTA_PAD))
  1961. goto nla_put_failure;
  1962. }
  1963. BUG_ON(tbl->parms.dev);
  1964. if (neightbl_fill_parms(skb, &tbl->parms) < 0)
  1965. goto nla_put_failure;
  1966. nlmsg_end(skb, nlh);
  1967. return 0;
  1968. nla_put_failure:
  1969. nlmsg_cancel(skb, nlh);
  1970. return -EMSGSIZE;
  1971. }
  1972. static int neightbl_fill_param_info(struct sk_buff *skb,
  1973. struct neigh_table *tbl,
  1974. struct neigh_parms *parms,
  1975. u32 pid, u32 seq, int type,
  1976. unsigned int flags)
  1977. {
  1978. struct ndtmsg *ndtmsg;
  1979. struct nlmsghdr *nlh;
  1980. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags);
  1981. if (nlh == NULL)
  1982. return -EMSGSIZE;
  1983. ndtmsg = nlmsg_data(nlh);
  1984. ndtmsg->ndtm_family = tbl->family;
  1985. ndtmsg->ndtm_pad1 = 0;
  1986. ndtmsg->ndtm_pad2 = 0;
  1987. if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 ||
  1988. neightbl_fill_parms(skb, parms) < 0)
  1989. goto errout;
  1990. nlmsg_end(skb, nlh);
  1991. return 0;
  1992. errout:
  1993. nlmsg_cancel(skb, nlh);
  1994. return -EMSGSIZE;
  1995. }
  1996. static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = {
  1997. [NDTA_NAME] = { .type = NLA_STRING },
  1998. [NDTA_THRESH1] = { .type = NLA_U32 },
  1999. [NDTA_THRESH2] = { .type = NLA_U32 },
  2000. [NDTA_THRESH3] = { .type = NLA_U32 },
  2001. [NDTA_GC_INTERVAL] = { .type = NLA_U64 },
  2002. [NDTA_PARMS] = { .type = NLA_NESTED },
  2003. };
  2004. static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
  2005. [NDTPA_IFINDEX] = { .type = NLA_U32 },
  2006. [NDTPA_QUEUE_LEN] = { .type = NLA_U32 },
  2007. [NDTPA_QUEUE_LENBYTES] = { .type = NLA_U32 },
  2008. [NDTPA_PROXY_QLEN] = { .type = NLA_U32 },
  2009. [NDTPA_APP_PROBES] = { .type = NLA_U32 },
  2010. [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
  2011. [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
  2012. [NDTPA_MCAST_REPROBES] = { .type = NLA_U32 },
  2013. [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
  2014. [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
  2015. [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
  2016. [NDTPA_RETRANS_TIME] = { .type = NLA_U64 },
  2017. [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 },
  2018. [NDTPA_PROXY_DELAY] = { .type = NLA_U64 },
  2019. [NDTPA_LOCKTIME] = { .type = NLA_U64 },
  2020. [NDTPA_INTERVAL_PROBE_TIME_MS] = { .type = NLA_U64, .min = 1 },
  2021. };
  2022. static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh,
  2023. struct netlink_ext_ack *extack)
  2024. {
  2025. struct net *net = sock_net(skb->sk);
  2026. struct nlattr *tb[NDTA_MAX + 1];
  2027. struct neigh_table *tbl;
  2028. struct ndtmsg *ndtmsg;
  2029. bool found = false;
  2030. int err, tidx;
  2031. err = nlmsg_parse_deprecated(nlh, sizeof(*ndtmsg), tb, NDTA_MAX,
  2032. nl_neightbl_policy, extack);
  2033. if (err < 0)
  2034. goto errout;
  2035. if (tb[NDTA_NAME] == NULL) {
  2036. err = -EINVAL;
  2037. goto errout;
  2038. }
  2039. ndtmsg = nlmsg_data(nlh);
  2040. rcu_read_lock();
  2041. for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
  2042. tbl = rcu_dereference(neigh_tables[tidx]);
  2043. if (!tbl)
  2044. continue;
  2045. if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
  2046. continue;
  2047. if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) {
  2048. found = true;
  2049. break;
  2050. }
  2051. }
  2052. if (!found) {
  2053. rcu_read_unlock();
  2054. err = -ENOENT;
  2055. goto errout;
  2056. }
  2057. /*
  2058. * We acquire tbl->lock to be nice to the periodic timers and
  2059. * make sure they always see a consistent set of values.
  2060. */
  2061. spin_lock_bh(&tbl->lock);
  2062. if (tb[NDTA_PARMS]) {
  2063. struct nlattr *tbp[NDTPA_MAX+1];
  2064. struct neigh_parms *p;
  2065. int i, ifindex = 0;
  2066. err = nla_parse_nested_deprecated(tbp, NDTPA_MAX,
  2067. tb[NDTA_PARMS],
  2068. nl_ntbl_parm_policy, extack);
  2069. if (err < 0)
  2070. goto errout_tbl_lock;
  2071. if (tbp[NDTPA_IFINDEX])
  2072. ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]);
  2073. p = lookup_neigh_parms(tbl, net, ifindex);
  2074. if (p == NULL) {
  2075. err = -ENOENT;
  2076. goto errout_tbl_lock;
  2077. }
  2078. for (i = 1; i <= NDTPA_MAX; i++) {
  2079. if (tbp[i] == NULL)
  2080. continue;
  2081. switch (i) {
  2082. case NDTPA_QUEUE_LEN:
  2083. NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
  2084. nla_get_u32(tbp[i]) *
  2085. SKB_TRUESIZE(ETH_FRAME_LEN));
  2086. break;
  2087. case NDTPA_QUEUE_LENBYTES:
  2088. NEIGH_VAR_SET(p, QUEUE_LEN_BYTES,
  2089. nla_get_u32(tbp[i]));
  2090. break;
  2091. case NDTPA_PROXY_QLEN:
  2092. NEIGH_VAR_SET(p, PROXY_QLEN,
  2093. nla_get_u32(tbp[i]));
  2094. break;
  2095. case NDTPA_APP_PROBES:
  2096. NEIGH_VAR_SET(p, APP_PROBES,
  2097. nla_get_u32(tbp[i]));
  2098. break;
  2099. case NDTPA_UCAST_PROBES:
  2100. NEIGH_VAR_SET(p, UCAST_PROBES,
  2101. nla_get_u32(tbp[i]));
  2102. break;
  2103. case NDTPA_MCAST_PROBES:
  2104. NEIGH_VAR_SET(p, MCAST_PROBES,
  2105. nla_get_u32(tbp[i]));
  2106. break;
  2107. case NDTPA_MCAST_REPROBES:
  2108. NEIGH_VAR_SET(p, MCAST_REPROBES,
  2109. nla_get_u32(tbp[i]));
  2110. break;
  2111. case NDTPA_BASE_REACHABLE_TIME:
  2112. NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
  2113. nla_get_msecs(tbp[i]));
  2114. /* update reachable_time as well, otherwise, the change will
  2115. * only be effective after the next time neigh_periodic_work
  2116. * decides to recompute it (can be multiple minutes)
  2117. */
  2118. neigh_set_reach_time(p);
  2119. break;
  2120. case NDTPA_GC_STALETIME:
  2121. NEIGH_VAR_SET(p, GC_STALETIME,
  2122. nla_get_msecs(tbp[i]));
  2123. break;
  2124. case NDTPA_DELAY_PROBE_TIME:
  2125. NEIGH_VAR_SET(p, DELAY_PROBE_TIME,
  2126. nla_get_msecs(tbp[i]));
  2127. call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
  2128. break;
  2129. case NDTPA_INTERVAL_PROBE_TIME_MS:
  2130. NEIGH_VAR_SET(p, INTERVAL_PROBE_TIME_MS,
  2131. nla_get_msecs(tbp[i]));
  2132. break;
  2133. case NDTPA_RETRANS_TIME:
  2134. NEIGH_VAR_SET(p, RETRANS_TIME,
  2135. nla_get_msecs(tbp[i]));
  2136. break;
  2137. case NDTPA_ANYCAST_DELAY:
  2138. NEIGH_VAR_SET(p, ANYCAST_DELAY,
  2139. nla_get_msecs(tbp[i]));
  2140. break;
  2141. case NDTPA_PROXY_DELAY:
  2142. NEIGH_VAR_SET(p, PROXY_DELAY,
  2143. nla_get_msecs(tbp[i]));
  2144. break;
  2145. case NDTPA_LOCKTIME:
  2146. NEIGH_VAR_SET(p, LOCKTIME,
  2147. nla_get_msecs(tbp[i]));
  2148. break;
  2149. }
  2150. }
  2151. }
  2152. err = -ENOENT;
  2153. if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] ||
  2154. tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) &&
  2155. !net_eq(net, &init_net))
  2156. goto errout_tbl_lock;
  2157. if (tb[NDTA_THRESH1])
  2158. WRITE_ONCE(tbl->gc_thresh1, nla_get_u32(tb[NDTA_THRESH1]));
  2159. if (tb[NDTA_THRESH2])
  2160. WRITE_ONCE(tbl->gc_thresh2, nla_get_u32(tb[NDTA_THRESH2]));
  2161. if (tb[NDTA_THRESH3])
  2162. WRITE_ONCE(tbl->gc_thresh3, nla_get_u32(tb[NDTA_THRESH3]));
  2163. if (tb[NDTA_GC_INTERVAL])
  2164. WRITE_ONCE(tbl->gc_interval, nla_get_msecs(tb[NDTA_GC_INTERVAL]));
  2165. err = 0;
  2166. errout_tbl_lock:
  2167. spin_unlock_bh(&tbl->lock);
  2168. rcu_read_unlock();
  2169. errout:
  2170. return err;
  2171. }
  2172. static int neightbl_valid_dump_info(const struct nlmsghdr *nlh,
  2173. struct netlink_ext_ack *extack)
  2174. {
  2175. struct ndtmsg *ndtm;
  2176. ndtm = nlmsg_payload(nlh, sizeof(*ndtm));
  2177. if (!ndtm) {
  2178. NL_SET_ERR_MSG(extack, "Invalid header for neighbor table dump request");
  2179. return -EINVAL;
  2180. }
  2181. if (ndtm->ndtm_pad1 || ndtm->ndtm_pad2) {
  2182. NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor table dump request");
  2183. return -EINVAL;
  2184. }
  2185. if (nlmsg_attrlen(nlh, sizeof(*ndtm))) {
  2186. NL_SET_ERR_MSG(extack, "Invalid data after header in neighbor table dump request");
  2187. return -EINVAL;
  2188. }
  2189. return 0;
  2190. }
  2191. static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
  2192. {
  2193. const struct nlmsghdr *nlh = cb->nlh;
  2194. struct net *net = sock_net(skb->sk);
  2195. int family, tidx, nidx = 0;
  2196. int tbl_skip = cb->args[0];
  2197. int neigh_skip = cb->args[1];
  2198. struct neigh_table *tbl;
  2199. if (cb->strict_check) {
  2200. int err = neightbl_valid_dump_info(nlh, cb->extack);
  2201. if (err < 0)
  2202. return err;
  2203. }
  2204. family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
  2205. rcu_read_lock();
  2206. for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
  2207. struct neigh_parms *p;
  2208. tbl = rcu_dereference(neigh_tables[tidx]);
  2209. if (!tbl)
  2210. continue;
  2211. if (tidx < tbl_skip || (family && tbl->family != family))
  2212. continue;
  2213. if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
  2214. nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
  2215. NLM_F_MULTI) < 0)
  2216. break;
  2217. nidx = 0;
  2218. p = list_next_entry(&tbl->parms, list);
  2219. list_for_each_entry_from_rcu(p, &tbl->parms_list, list) {
  2220. if (!net_eq(neigh_parms_net(p), net))
  2221. continue;
  2222. if (nidx < neigh_skip)
  2223. goto next;
  2224. if (neightbl_fill_param_info(skb, tbl, p,
  2225. NETLINK_CB(cb->skb).portid,
  2226. nlh->nlmsg_seq,
  2227. RTM_NEWNEIGHTBL,
  2228. NLM_F_MULTI) < 0)
  2229. goto out;
  2230. next:
  2231. nidx++;
  2232. }
  2233. neigh_skip = 0;
  2234. }
  2235. out:
  2236. rcu_read_unlock();
  2237. cb->args[0] = tidx;
  2238. cb->args[1] = nidx;
  2239. return skb->len;
  2240. }
  2241. static int __neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
  2242. u32 pid, u32 seq, int type, unsigned int flags)
  2243. {
  2244. u32 neigh_flags, neigh_flags_ext;
  2245. unsigned long now = jiffies;
  2246. struct nda_cacheinfo ci;
  2247. struct nlmsghdr *nlh;
  2248. struct ndmsg *ndm;
  2249. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
  2250. if (nlh == NULL)
  2251. return -EMSGSIZE;
  2252. neigh_flags_ext = neigh->flags >> NTF_EXT_SHIFT;
  2253. neigh_flags = neigh->flags & NTF_OLD_MASK;
  2254. ndm = nlmsg_data(nlh);
  2255. ndm->ndm_family = neigh->ops->family;
  2256. ndm->ndm_pad1 = 0;
  2257. ndm->ndm_pad2 = 0;
  2258. ndm->ndm_flags = neigh_flags;
  2259. ndm->ndm_type = neigh->type;
  2260. ndm->ndm_ifindex = neigh->dev->ifindex;
  2261. if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key))
  2262. goto nla_put_failure;
  2263. ndm->ndm_state = neigh->nud_state;
  2264. if (neigh->nud_state & NUD_VALID) {
  2265. char haddr[MAX_ADDR_LEN];
  2266. neigh_ha_snapshot(haddr, neigh, neigh->dev);
  2267. if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0)
  2268. goto nla_put_failure;
  2269. }
  2270. ci.ndm_used = jiffies_to_clock_t(now - neigh->used);
  2271. ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed);
  2272. ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated);
  2273. ci.ndm_refcnt = refcount_read(&neigh->refcnt) - 1;
  2274. if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) ||
  2275. nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  2276. goto nla_put_failure;
  2277. if (neigh->protocol && nla_put_u8(skb, NDA_PROTOCOL, neigh->protocol))
  2278. goto nla_put_failure;
  2279. if (neigh_flags_ext && nla_put_u32(skb, NDA_FLAGS_EXT, neigh_flags_ext))
  2280. goto nla_put_failure;
  2281. nlmsg_end(skb, nlh);
  2282. return 0;
  2283. nla_put_failure:
  2284. nlmsg_cancel(skb, nlh);
  2285. return -EMSGSIZE;
  2286. }
  2287. static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
  2288. u32 pid, u32 seq, int type, unsigned int flags)
  2289. __releases(neigh->lock)
  2290. __acquires(neigh->lock)
  2291. {
  2292. int err;
  2293. read_lock_bh(&neigh->lock);
  2294. err = __neigh_fill_info(skb, neigh, pid, seq, type, flags);
  2295. read_unlock_bh(&neigh->lock);
  2296. return err;
  2297. }
  2298. static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
  2299. u32 pid, u32 seq, int type, unsigned int flags,
  2300. struct neigh_table *tbl)
  2301. {
  2302. u32 neigh_flags, neigh_flags_ext;
  2303. struct nlmsghdr *nlh;
  2304. struct ndmsg *ndm;
  2305. u8 protocol;
  2306. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
  2307. if (nlh == NULL)
  2308. return -EMSGSIZE;
  2309. neigh_flags = READ_ONCE(pn->flags);
  2310. neigh_flags_ext = neigh_flags >> NTF_EXT_SHIFT;
  2311. neigh_flags &= NTF_OLD_MASK;
  2312. ndm = nlmsg_data(nlh);
  2313. ndm->ndm_family = tbl->family;
  2314. ndm->ndm_pad1 = 0;
  2315. ndm->ndm_pad2 = 0;
  2316. ndm->ndm_flags = neigh_flags | NTF_PROXY;
  2317. ndm->ndm_type = RTN_UNICAST;
  2318. ndm->ndm_ifindex = pn->dev ? pn->dev->ifindex : 0;
  2319. ndm->ndm_state = NUD_NONE;
  2320. if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
  2321. goto nla_put_failure;
  2322. protocol = READ_ONCE(pn->protocol);
  2323. if (protocol && nla_put_u8(skb, NDA_PROTOCOL, protocol))
  2324. goto nla_put_failure;
  2325. if (neigh_flags_ext && nla_put_u32(skb, NDA_FLAGS_EXT, neigh_flags_ext))
  2326. goto nla_put_failure;
  2327. nlmsg_end(skb, nlh);
  2328. return 0;
  2329. nla_put_failure:
  2330. nlmsg_cancel(skb, nlh);
  2331. return -EMSGSIZE;
  2332. }
  2333. static bool neigh_master_filtered(struct net_device *dev, int master_idx)
  2334. {
  2335. struct net_device *master;
  2336. if (!master_idx)
  2337. return false;
  2338. master = dev ? netdev_master_upper_dev_get_rcu(dev) : NULL;
  2339. /* 0 is already used to denote NDA_MASTER wasn't passed, therefore need another
  2340. * invalid value for ifindex to denote "no master".
  2341. */
  2342. if (master_idx == -1)
  2343. return !!master;
  2344. if (!master || master->ifindex != master_idx)
  2345. return true;
  2346. return false;
  2347. }
  2348. static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
  2349. {
  2350. if (filter_idx && (!dev || dev->ifindex != filter_idx))
  2351. return true;
  2352. return false;
  2353. }
  2354. struct neigh_dump_filter {
  2355. int master_idx;
  2356. int dev_idx;
  2357. };
  2358. static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
  2359. struct netlink_callback *cb,
  2360. struct neigh_dump_filter *filter)
  2361. {
  2362. struct net *net = sock_net(skb->sk);
  2363. struct neighbour *n;
  2364. int err = 0, h, s_h = cb->args[1];
  2365. int idx, s_idx = idx = cb->args[2];
  2366. struct neigh_hash_table *nht;
  2367. unsigned int flags = NLM_F_MULTI;
  2368. if (filter->dev_idx || filter->master_idx)
  2369. flags |= NLM_F_DUMP_FILTERED;
  2370. nht = rcu_dereference(tbl->nht);
  2371. for (h = s_h; h < (1 << nht->hash_shift); h++) {
  2372. if (h > s_h)
  2373. s_idx = 0;
  2374. idx = 0;
  2375. neigh_for_each_in_bucket_rcu(n, &nht->hash_heads[h]) {
  2376. if (idx < s_idx || !net_eq(dev_net(n->dev), net))
  2377. goto next;
  2378. if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
  2379. neigh_master_filtered(n->dev, filter->master_idx))
  2380. goto next;
  2381. err = neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
  2382. cb->nlh->nlmsg_seq,
  2383. RTM_NEWNEIGH, flags);
  2384. if (err < 0)
  2385. goto out;
  2386. next:
  2387. idx++;
  2388. }
  2389. }
  2390. out:
  2391. cb->args[1] = h;
  2392. cb->args[2] = idx;
  2393. return err;
  2394. }
  2395. static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
  2396. struct netlink_callback *cb,
  2397. struct neigh_dump_filter *filter)
  2398. {
  2399. struct pneigh_entry *n;
  2400. struct net *net = sock_net(skb->sk);
  2401. int err = 0, h, s_h = cb->args[3];
  2402. int idx, s_idx = idx = cb->args[4];
  2403. unsigned int flags = NLM_F_MULTI;
  2404. if (filter->dev_idx || filter->master_idx)
  2405. flags |= NLM_F_DUMP_FILTERED;
  2406. for (h = s_h; h <= PNEIGH_HASHMASK; h++) {
  2407. if (h > s_h)
  2408. s_idx = 0;
  2409. for (n = rcu_dereference(tbl->phash_buckets[h]), idx = 0;
  2410. n;
  2411. n = rcu_dereference(n->next)) {
  2412. if (idx < s_idx || pneigh_net(n) != net)
  2413. goto next;
  2414. if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
  2415. neigh_master_filtered(n->dev, filter->master_idx))
  2416. goto next;
  2417. err = pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
  2418. cb->nlh->nlmsg_seq,
  2419. RTM_NEWNEIGH, flags, tbl);
  2420. if (err < 0)
  2421. goto out;
  2422. next:
  2423. idx++;
  2424. }
  2425. }
  2426. out:
  2427. cb->args[3] = h;
  2428. cb->args[4] = idx;
  2429. return err;
  2430. }
  2431. static int neigh_valid_dump_req(const struct nlmsghdr *nlh,
  2432. bool strict_check,
  2433. struct neigh_dump_filter *filter,
  2434. struct netlink_ext_ack *extack)
  2435. {
  2436. struct nlattr *tb[NDA_MAX + 1];
  2437. int err, i;
  2438. if (strict_check) {
  2439. struct ndmsg *ndm;
  2440. ndm = nlmsg_payload(nlh, sizeof(*ndm));
  2441. if (!ndm) {
  2442. NL_SET_ERR_MSG(extack, "Invalid header for neighbor dump request");
  2443. return -EINVAL;
  2444. }
  2445. if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_ifindex ||
  2446. ndm->ndm_state || ndm->ndm_type) {
  2447. NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor dump request");
  2448. return -EINVAL;
  2449. }
  2450. if (ndm->ndm_flags & ~NTF_PROXY) {
  2451. NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor dump request");
  2452. return -EINVAL;
  2453. }
  2454. err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg),
  2455. tb, NDA_MAX, nda_policy,
  2456. extack);
  2457. } else {
  2458. err = nlmsg_parse_deprecated(nlh, sizeof(struct ndmsg), tb,
  2459. NDA_MAX, nda_policy, extack);
  2460. }
  2461. if (err < 0)
  2462. return err;
  2463. for (i = 0; i <= NDA_MAX; ++i) {
  2464. if (!tb[i])
  2465. continue;
  2466. /* all new attributes should require strict_check */
  2467. switch (i) {
  2468. case NDA_IFINDEX:
  2469. filter->dev_idx = nla_get_u32(tb[i]);
  2470. break;
  2471. case NDA_MASTER:
  2472. filter->master_idx = nla_get_u32(tb[i]);
  2473. break;
  2474. default:
  2475. if (strict_check) {
  2476. NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor dump request");
  2477. return -EINVAL;
  2478. }
  2479. }
  2480. }
  2481. return 0;
  2482. }
  2483. static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
  2484. {
  2485. const struct nlmsghdr *nlh = cb->nlh;
  2486. struct neigh_dump_filter filter = {};
  2487. struct neigh_table *tbl;
  2488. int t, family, s_t;
  2489. int proxy = 0;
  2490. int err;
  2491. family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
  2492. /* check for full ndmsg structure presence, family member is
  2493. * the same for both structures
  2494. */
  2495. if (nlmsg_len(nlh) >= sizeof(struct ndmsg) &&
  2496. ((struct ndmsg *)nlmsg_data(nlh))->ndm_flags == NTF_PROXY)
  2497. proxy = 1;
  2498. err = neigh_valid_dump_req(nlh, cb->strict_check, &filter, cb->extack);
  2499. if (err < 0 && cb->strict_check)
  2500. return err;
  2501. err = 0;
  2502. s_t = cb->args[0];
  2503. rcu_read_lock();
  2504. for (t = 0; t < NEIGH_NR_TABLES; t++) {
  2505. tbl = rcu_dereference(neigh_tables[t]);
  2506. if (!tbl)
  2507. continue;
  2508. if (t < s_t || (family && tbl->family != family))
  2509. continue;
  2510. if (t > s_t)
  2511. memset(&cb->args[1], 0, sizeof(cb->args) -
  2512. sizeof(cb->args[0]));
  2513. if (proxy)
  2514. err = pneigh_dump_table(tbl, skb, cb, &filter);
  2515. else
  2516. err = neigh_dump_table(tbl, skb, cb, &filter);
  2517. if (err < 0)
  2518. break;
  2519. }
  2520. rcu_read_unlock();
  2521. cb->args[0] = t;
  2522. return err;
  2523. }
  2524. static struct ndmsg *neigh_valid_get_req(const struct nlmsghdr *nlh,
  2525. struct nlattr **tb,
  2526. struct netlink_ext_ack *extack)
  2527. {
  2528. struct ndmsg *ndm;
  2529. int err, i;
  2530. ndm = nlmsg_payload(nlh, sizeof(*ndm));
  2531. if (!ndm) {
  2532. NL_SET_ERR_MSG(extack, "Invalid header for neighbor get request");
  2533. return ERR_PTR(-EINVAL);
  2534. }
  2535. if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
  2536. ndm->ndm_type) {
  2537. NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor get request");
  2538. return ERR_PTR(-EINVAL);
  2539. }
  2540. if (ndm->ndm_flags & ~NTF_PROXY) {
  2541. NL_SET_ERR_MSG(extack, "Invalid flags in header for neighbor get request");
  2542. return ERR_PTR(-EINVAL);
  2543. }
  2544. if (!(ndm->ndm_flags & NTF_PROXY) && !ndm->ndm_ifindex) {
  2545. NL_SET_ERR_MSG(extack, "No device specified");
  2546. return ERR_PTR(-EINVAL);
  2547. }
  2548. err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
  2549. NDA_MAX, nda_policy, extack);
  2550. if (err < 0)
  2551. return ERR_PTR(err);
  2552. for (i = 0; i <= NDA_MAX; ++i) {
  2553. switch (i) {
  2554. case NDA_DST:
  2555. if (!tb[i]) {
  2556. NL_SET_ERR_ATTR_MISS(extack, NULL, NDA_DST);
  2557. return ERR_PTR(-EINVAL);
  2558. }
  2559. break;
  2560. default:
  2561. if (!tb[i])
  2562. continue;
  2563. NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor get request");
  2564. return ERR_PTR(-EINVAL);
  2565. }
  2566. }
  2567. return ndm;
  2568. }
  2569. static inline size_t neigh_nlmsg_size(void)
  2570. {
  2571. return NLMSG_ALIGN(sizeof(struct ndmsg))
  2572. + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
  2573. + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */
  2574. + nla_total_size(sizeof(struct nda_cacheinfo))
  2575. + nla_total_size(4) /* NDA_PROBES */
  2576. + nla_total_size(4) /* NDA_FLAGS_EXT */
  2577. + nla_total_size(1); /* NDA_PROTOCOL */
  2578. }
  2579. static inline size_t pneigh_nlmsg_size(void)
  2580. {
  2581. return NLMSG_ALIGN(sizeof(struct ndmsg))
  2582. + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */
  2583. + nla_total_size(4) /* NDA_FLAGS_EXT */
  2584. + nla_total_size(1); /* NDA_PROTOCOL */
  2585. }
  2586. static int neigh_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
  2587. struct netlink_ext_ack *extack)
  2588. {
  2589. struct net *net = sock_net(in_skb->sk);
  2590. u32 pid = NETLINK_CB(in_skb).portid;
  2591. struct nlattr *tb[NDA_MAX + 1];
  2592. struct net_device *dev = NULL;
  2593. u32 seq = nlh->nlmsg_seq;
  2594. struct neigh_table *tbl;
  2595. struct neighbour *neigh;
  2596. struct sk_buff *skb;
  2597. struct ndmsg *ndm;
  2598. void *dst;
  2599. int err;
  2600. ndm = neigh_valid_get_req(nlh, tb, extack);
  2601. if (IS_ERR(ndm))
  2602. return PTR_ERR(ndm);
  2603. if (ndm->ndm_flags & NTF_PROXY)
  2604. skb = nlmsg_new(neigh_nlmsg_size(), GFP_KERNEL);
  2605. else
  2606. skb = nlmsg_new(pneigh_nlmsg_size(), GFP_KERNEL);
  2607. if (!skb)
  2608. return -ENOBUFS;
  2609. rcu_read_lock();
  2610. tbl = neigh_find_table(ndm->ndm_family);
  2611. if (!tbl) {
  2612. NL_SET_ERR_MSG(extack, "Unsupported family in header for neighbor get request");
  2613. err = -EAFNOSUPPORT;
  2614. goto err_unlock;
  2615. }
  2616. if (nla_len(tb[NDA_DST]) != (int)tbl->key_len) {
  2617. NL_SET_ERR_MSG(extack, "Invalid network address in neighbor get request");
  2618. err = -EINVAL;
  2619. goto err_unlock;
  2620. }
  2621. dst = nla_data(tb[NDA_DST]);
  2622. if (ndm->ndm_ifindex) {
  2623. dev = dev_get_by_index_rcu(net, ndm->ndm_ifindex);
  2624. if (!dev) {
  2625. NL_SET_ERR_MSG(extack, "Unknown device ifindex");
  2626. err = -ENODEV;
  2627. goto err_unlock;
  2628. }
  2629. }
  2630. if (ndm->ndm_flags & NTF_PROXY) {
  2631. struct pneigh_entry *pn;
  2632. pn = pneigh_lookup(tbl, net, dst, dev);
  2633. if (!pn) {
  2634. NL_SET_ERR_MSG(extack, "Proxy neighbour entry not found");
  2635. err = -ENOENT;
  2636. goto err_unlock;
  2637. }
  2638. err = pneigh_fill_info(skb, pn, pid, seq, RTM_NEWNEIGH, 0, tbl);
  2639. if (err)
  2640. goto err_unlock;
  2641. } else {
  2642. neigh = neigh_lookup(tbl, dst, dev);
  2643. if (!neigh) {
  2644. NL_SET_ERR_MSG(extack, "Neighbour entry not found");
  2645. err = -ENOENT;
  2646. goto err_unlock;
  2647. }
  2648. err = neigh_fill_info(skb, neigh, pid, seq, RTM_NEWNEIGH, 0);
  2649. neigh_release(neigh);
  2650. if (err)
  2651. goto err_unlock;
  2652. }
  2653. rcu_read_unlock();
  2654. return rtnl_unicast(skb, net, pid);
  2655. err_unlock:
  2656. rcu_read_unlock();
  2657. kfree_skb(skb);
  2658. return err;
  2659. }
  2660. void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
  2661. {
  2662. int chain;
  2663. struct neigh_hash_table *nht;
  2664. rcu_read_lock();
  2665. nht = rcu_dereference(tbl->nht);
  2666. spin_lock_bh(&tbl->lock); /* avoid resizes */
  2667. for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
  2668. struct neighbour *n;
  2669. neigh_for_each_in_bucket(n, &nht->hash_heads[chain])
  2670. cb(n, cookie);
  2671. }
  2672. spin_unlock_bh(&tbl->lock);
  2673. rcu_read_unlock();
  2674. }
  2675. EXPORT_SYMBOL(neigh_for_each);
  2676. /* The tbl->lock must be held as a writer and BH disabled. */
  2677. void __neigh_for_each_release(struct neigh_table *tbl,
  2678. int (*cb)(struct neighbour *))
  2679. {
  2680. struct neigh_hash_table *nht;
  2681. int chain;
  2682. nht = rcu_dereference_protected(tbl->nht,
  2683. lockdep_is_held(&tbl->lock));
  2684. for (chain = 0; chain < (1 << nht->hash_shift); chain++) {
  2685. struct hlist_node *tmp;
  2686. struct neighbour *n;
  2687. neigh_for_each_in_bucket_safe(n, tmp, &nht->hash_heads[chain]) {
  2688. int release;
  2689. write_lock(&n->lock);
  2690. release = cb(n);
  2691. if (release) {
  2692. hlist_del_rcu(&n->hash);
  2693. hlist_del_rcu(&n->dev_list);
  2694. neigh_mark_dead(n);
  2695. }
  2696. write_unlock(&n->lock);
  2697. if (release)
  2698. neigh_cleanup_and_release(n);
  2699. }
  2700. }
  2701. }
  2702. EXPORT_SYMBOL(__neigh_for_each_release);
  2703. int neigh_xmit(int index, struct net_device *dev,
  2704. const void *addr, struct sk_buff *skb)
  2705. {
  2706. int err = -EAFNOSUPPORT;
  2707. if (likely(index < NEIGH_NR_TABLES)) {
  2708. struct neigh_table *tbl;
  2709. struct neighbour *neigh;
  2710. rcu_read_lock();
  2711. tbl = rcu_dereference(neigh_tables[index]);
  2712. if (!tbl)
  2713. goto out_unlock;
  2714. if (index == NEIGH_ARP_TABLE) {
  2715. u32 key = *((u32 *)addr);
  2716. neigh = __ipv4_neigh_lookup_noref(dev, key);
  2717. } else {
  2718. neigh = __neigh_lookup_noref(tbl, addr, dev);
  2719. }
  2720. if (!neigh)
  2721. neigh = __neigh_create(tbl, addr, dev, false);
  2722. err = PTR_ERR(neigh);
  2723. if (IS_ERR(neigh)) {
  2724. rcu_read_unlock();
  2725. goto out_kfree_skb;
  2726. }
  2727. err = READ_ONCE(neigh->output)(neigh, skb);
  2728. out_unlock:
  2729. rcu_read_unlock();
  2730. }
  2731. else if (index == NEIGH_LINK_TABLE) {
  2732. err = dev_hard_header(skb, dev, ntohs(skb->protocol),
  2733. addr, NULL, skb->len);
  2734. if (err < 0)
  2735. goto out_kfree_skb;
  2736. err = dev_queue_xmit(skb);
  2737. }
  2738. out:
  2739. return err;
  2740. out_kfree_skb:
  2741. kfree_skb(skb);
  2742. goto out;
  2743. }
  2744. EXPORT_SYMBOL(neigh_xmit);
  2745. #ifdef CONFIG_PROC_FS
  2746. static struct neighbour *neigh_get_valid(struct seq_file *seq,
  2747. struct neighbour *n,
  2748. loff_t *pos)
  2749. {
  2750. struct neigh_seq_state *state = seq->private;
  2751. struct net *net = seq_file_net(seq);
  2752. if (!net_eq(dev_net(n->dev), net))
  2753. return NULL;
  2754. if (state->neigh_sub_iter) {
  2755. loff_t fakep = 0;
  2756. void *v;
  2757. v = state->neigh_sub_iter(state, n, pos ? pos : &fakep);
  2758. if (!v)
  2759. return NULL;
  2760. if (pos)
  2761. return v;
  2762. }
  2763. if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
  2764. return n;
  2765. if (READ_ONCE(n->nud_state) & ~NUD_NOARP)
  2766. return n;
  2767. return NULL;
  2768. }
  2769. static struct neighbour *neigh_get_first(struct seq_file *seq)
  2770. {
  2771. struct neigh_seq_state *state = seq->private;
  2772. struct neigh_hash_table *nht = state->nht;
  2773. struct neighbour *n, *tmp;
  2774. state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
  2775. while (++state->bucket < (1 << nht->hash_shift)) {
  2776. neigh_for_each_in_bucket(n, &nht->hash_heads[state->bucket]) {
  2777. tmp = neigh_get_valid(seq, n, NULL);
  2778. if (tmp)
  2779. return tmp;
  2780. }
  2781. }
  2782. return NULL;
  2783. }
  2784. static struct neighbour *neigh_get_next(struct seq_file *seq,
  2785. struct neighbour *n,
  2786. loff_t *pos)
  2787. {
  2788. struct neigh_seq_state *state = seq->private;
  2789. struct neighbour *tmp;
  2790. if (state->neigh_sub_iter) {
  2791. void *v = state->neigh_sub_iter(state, n, pos);
  2792. if (v)
  2793. return n;
  2794. }
  2795. hlist_for_each_entry_continue(n, hash) {
  2796. tmp = neigh_get_valid(seq, n, pos);
  2797. if (tmp) {
  2798. n = tmp;
  2799. goto out;
  2800. }
  2801. }
  2802. n = neigh_get_first(seq);
  2803. out:
  2804. if (n && pos)
  2805. --(*pos);
  2806. return n;
  2807. }
  2808. static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
  2809. {
  2810. struct neighbour *n = neigh_get_first(seq);
  2811. if (n) {
  2812. --(*pos);
  2813. while (*pos) {
  2814. n = neigh_get_next(seq, n, pos);
  2815. if (!n)
  2816. break;
  2817. }
  2818. }
  2819. return *pos ? NULL : n;
  2820. }
  2821. static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
  2822. {
  2823. struct neigh_seq_state *state = seq->private;
  2824. struct net *net = seq_file_net(seq);
  2825. struct neigh_table *tbl = state->tbl;
  2826. struct pneigh_entry *pn = NULL;
  2827. int bucket;
  2828. state->flags |= NEIGH_SEQ_IS_PNEIGH;
  2829. for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
  2830. pn = rcu_dereference(tbl->phash_buckets[bucket]);
  2831. while (pn && !net_eq(pneigh_net(pn), net))
  2832. pn = rcu_dereference(pn->next);
  2833. if (pn)
  2834. break;
  2835. }
  2836. state->bucket = bucket;
  2837. return pn;
  2838. }
  2839. static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
  2840. struct pneigh_entry *pn,
  2841. loff_t *pos)
  2842. {
  2843. struct neigh_seq_state *state = seq->private;
  2844. struct net *net = seq_file_net(seq);
  2845. struct neigh_table *tbl = state->tbl;
  2846. do {
  2847. pn = rcu_dereference(pn->next);
  2848. } while (pn && !net_eq(pneigh_net(pn), net));
  2849. while (!pn) {
  2850. if (++state->bucket > PNEIGH_HASHMASK)
  2851. break;
  2852. pn = rcu_dereference(tbl->phash_buckets[state->bucket]);
  2853. while (pn && !net_eq(pneigh_net(pn), net))
  2854. pn = rcu_dereference(pn->next);
  2855. if (pn)
  2856. break;
  2857. }
  2858. if (pn && pos)
  2859. --(*pos);
  2860. return pn;
  2861. }
  2862. static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
  2863. {
  2864. struct pneigh_entry *pn = pneigh_get_first(seq);
  2865. if (pn) {
  2866. --(*pos);
  2867. while (*pos) {
  2868. pn = pneigh_get_next(seq, pn, pos);
  2869. if (!pn)
  2870. break;
  2871. }
  2872. }
  2873. return *pos ? NULL : pn;
  2874. }
  2875. static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
  2876. {
  2877. struct neigh_seq_state *state = seq->private;
  2878. void *rc;
  2879. loff_t idxpos = *pos;
  2880. rc = neigh_get_idx(seq, &idxpos);
  2881. if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
  2882. rc = pneigh_get_idx(seq, &idxpos);
  2883. return rc;
  2884. }
  2885. void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
  2886. __acquires(tbl->lock)
  2887. __acquires(rcu)
  2888. {
  2889. struct neigh_seq_state *state = seq->private;
  2890. state->tbl = tbl;
  2891. state->bucket = -1;
  2892. state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
  2893. rcu_read_lock();
  2894. state->nht = rcu_dereference(tbl->nht);
  2895. spin_lock_bh(&tbl->lock);
  2896. return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN;
  2897. }
  2898. EXPORT_SYMBOL(neigh_seq_start);
  2899. void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2900. {
  2901. struct neigh_seq_state *state;
  2902. void *rc;
  2903. if (v == SEQ_START_TOKEN) {
  2904. rc = neigh_get_first(seq);
  2905. goto out;
  2906. }
  2907. state = seq->private;
  2908. if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
  2909. rc = neigh_get_next(seq, v, NULL);
  2910. if (rc)
  2911. goto out;
  2912. if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
  2913. rc = pneigh_get_first(seq);
  2914. } else {
  2915. BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
  2916. rc = pneigh_get_next(seq, v, NULL);
  2917. }
  2918. out:
  2919. ++(*pos);
  2920. return rc;
  2921. }
  2922. EXPORT_SYMBOL(neigh_seq_next);
  2923. void neigh_seq_stop(struct seq_file *seq, void *v)
  2924. __releases(tbl->lock)
  2925. __releases(rcu)
  2926. {
  2927. struct neigh_seq_state *state = seq->private;
  2928. struct neigh_table *tbl = state->tbl;
  2929. spin_unlock_bh(&tbl->lock);
  2930. rcu_read_unlock();
  2931. }
  2932. EXPORT_SYMBOL(neigh_seq_stop);
  2933. /* statistics via seq_file */
  2934. static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
  2935. {
  2936. struct neigh_table *tbl = pde_data(file_inode(seq->file));
  2937. int cpu;
  2938. if (*pos == 0)
  2939. return SEQ_START_TOKEN;
  2940. for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
  2941. if (!cpu_possible(cpu))
  2942. continue;
  2943. *pos = cpu+1;
  2944. return per_cpu_ptr(tbl->stats, cpu);
  2945. }
  2946. return NULL;
  2947. }
  2948. static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2949. {
  2950. struct neigh_table *tbl = pde_data(file_inode(seq->file));
  2951. int cpu;
  2952. for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
  2953. if (!cpu_possible(cpu))
  2954. continue;
  2955. *pos = cpu+1;
  2956. return per_cpu_ptr(tbl->stats, cpu);
  2957. }
  2958. (*pos)++;
  2959. return NULL;
  2960. }
  2961. static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
  2962. {
  2963. }
  2964. static int neigh_stat_seq_show(struct seq_file *seq, void *v)
  2965. {
  2966. struct neigh_table *tbl = pde_data(file_inode(seq->file));
  2967. struct neigh_statistics *st = v;
  2968. if (v == SEQ_START_TOKEN) {
  2969. seq_puts(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");
  2970. return 0;
  2971. }
  2972. seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx "
  2973. "%08lx %08lx %08lx "
  2974. "%08lx %08lx %08lx\n",
  2975. atomic_read(&tbl->entries),
  2976. st->allocs,
  2977. st->destroys,
  2978. st->hash_grows,
  2979. st->lookups,
  2980. st->hits,
  2981. st->res_failed,
  2982. st->rcv_probes_mcast,
  2983. st->rcv_probes_ucast,
  2984. st->periodic_gc_runs,
  2985. st->forced_gc_runs,
  2986. st->unres_discards,
  2987. st->table_fulls
  2988. );
  2989. return 0;
  2990. }
  2991. static const struct seq_operations neigh_stat_seq_ops = {
  2992. .start = neigh_stat_seq_start,
  2993. .next = neigh_stat_seq_next,
  2994. .stop = neigh_stat_seq_stop,
  2995. .show = neigh_stat_seq_show,
  2996. };
  2997. #endif /* CONFIG_PROC_FS */
  2998. static void __neigh_notify(struct neighbour *n, int type, int flags,
  2999. u32 pid)
  3000. {
  3001. struct sk_buff *skb;
  3002. int err = -ENOBUFS;
  3003. struct net *net;
  3004. rcu_read_lock();
  3005. net = dev_net_rcu(n->dev);
  3006. skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC);
  3007. if (skb == NULL)
  3008. goto errout;
  3009. err = __neigh_fill_info(skb, n, pid, 0, type, flags);
  3010. if (err < 0) {
  3011. /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */
  3012. WARN_ON(err == -EMSGSIZE);
  3013. kfree_skb(skb);
  3014. goto errout;
  3015. }
  3016. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  3017. goto out;
  3018. errout:
  3019. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  3020. out:
  3021. rcu_read_unlock();
  3022. }
  3023. static void neigh_notify(struct neighbour *neigh, int type, int flags, u32 pid)
  3024. {
  3025. read_lock_bh(&neigh->lock);
  3026. __neigh_notify(neigh, type, flags, pid);
  3027. read_unlock_bh(&neigh->lock);
  3028. }
  3029. void neigh_app_ns(struct neighbour *n)
  3030. {
  3031. neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST, 0);
  3032. }
  3033. EXPORT_SYMBOL(neigh_app_ns);
  3034. #ifdef CONFIG_SYSCTL
  3035. static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN);
  3036. static int proc_unres_qlen(const struct ctl_table *ctl, int write,
  3037. void *buffer, size_t *lenp, loff_t *ppos)
  3038. {
  3039. int size, ret;
  3040. struct ctl_table tmp = *ctl;
  3041. tmp.extra1 = SYSCTL_ZERO;
  3042. tmp.extra2 = &unres_qlen_max;
  3043. tmp.data = &size;
  3044. size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN);
  3045. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  3046. if (write && !ret)
  3047. *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN);
  3048. return ret;
  3049. }
  3050. static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p,
  3051. int index)
  3052. {
  3053. struct net_device *dev;
  3054. int family = neigh_parms_family(p);
  3055. rcu_read_lock();
  3056. for_each_netdev_rcu(net, dev) {
  3057. struct neigh_parms *dst_p =
  3058. neigh_get_dev_parms_rcu(dev, family);
  3059. if (dst_p && !test_bit(index, dst_p->data_state))
  3060. dst_p->data[index] = p->data[index];
  3061. }
  3062. rcu_read_unlock();
  3063. }
  3064. static void neigh_proc_update(const struct ctl_table *ctl, int write)
  3065. {
  3066. struct net_device *dev = ctl->extra1;
  3067. struct neigh_parms *p = ctl->extra2;
  3068. struct net *net = neigh_parms_net(p);
  3069. int index = (int *) ctl->data - p->data;
  3070. if (!write)
  3071. return;
  3072. set_bit(index, p->data_state);
  3073. if (index == NEIGH_VAR_DELAY_PROBE_TIME)
  3074. call_netevent_notifiers(NETEVENT_DELAY_PROBE_TIME_UPDATE, p);
  3075. if (!dev) /* NULL dev means this is default value */
  3076. neigh_copy_dflt_parms(net, p, index);
  3077. }
  3078. static int neigh_proc_dointvec_zero_intmax(const struct ctl_table *ctl, int write,
  3079. void *buffer, size_t *lenp,
  3080. loff_t *ppos)
  3081. {
  3082. struct ctl_table tmp = *ctl;
  3083. int ret;
  3084. tmp.extra1 = SYSCTL_ZERO;
  3085. tmp.extra2 = SYSCTL_INT_MAX;
  3086. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  3087. neigh_proc_update(ctl, write);
  3088. return ret;
  3089. }
  3090. static int neigh_proc_dointvec_ms_jiffies_positive(const struct ctl_table *ctl, int write,
  3091. void *buffer, size_t *lenp, loff_t *ppos)
  3092. {
  3093. struct ctl_table tmp = *ctl;
  3094. int ret;
  3095. int min = msecs_to_jiffies(1);
  3096. tmp.extra1 = &min;
  3097. tmp.extra2 = NULL;
  3098. ret = proc_dointvec_ms_jiffies_minmax(&tmp, write, buffer, lenp, ppos);
  3099. neigh_proc_update(ctl, write);
  3100. return ret;
  3101. }
  3102. int neigh_proc_dointvec(const struct ctl_table *ctl, int write, void *buffer,
  3103. size_t *lenp, loff_t *ppos)
  3104. {
  3105. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  3106. neigh_proc_update(ctl, write);
  3107. return ret;
  3108. }
  3109. EXPORT_SYMBOL(neigh_proc_dointvec);
  3110. int neigh_proc_dointvec_jiffies(const struct ctl_table *ctl, int write, void *buffer,
  3111. size_t *lenp, loff_t *ppos)
  3112. {
  3113. int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
  3114. neigh_proc_update(ctl, write);
  3115. return ret;
  3116. }
  3117. EXPORT_SYMBOL(neigh_proc_dointvec_jiffies);
  3118. static int neigh_proc_dointvec_userhz_jiffies(const struct ctl_table *ctl, int write,
  3119. void *buffer, size_t *lenp,
  3120. loff_t *ppos)
  3121. {
  3122. int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos);
  3123. neigh_proc_update(ctl, write);
  3124. return ret;
  3125. }
  3126. int neigh_proc_dointvec_ms_jiffies(const struct ctl_table *ctl, int write,
  3127. void *buffer, size_t *lenp, loff_t *ppos)
  3128. {
  3129. int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
  3130. neigh_proc_update(ctl, write);
  3131. return ret;
  3132. }
  3133. EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies);
  3134. static int neigh_proc_dointvec_unres_qlen(const struct ctl_table *ctl, int write,
  3135. void *buffer, size_t *lenp,
  3136. loff_t *ppos)
  3137. {
  3138. int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos);
  3139. neigh_proc_update(ctl, write);
  3140. return ret;
  3141. }
  3142. static int neigh_proc_base_reachable_time(const struct ctl_table *ctl, int write,
  3143. void *buffer, size_t *lenp,
  3144. loff_t *ppos)
  3145. {
  3146. struct neigh_parms *p = ctl->extra2;
  3147. int ret;
  3148. if (strcmp(ctl->procname, "base_reachable_time") == 0)
  3149. ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos);
  3150. else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0)
  3151. ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos);
  3152. else
  3153. ret = -1;
  3154. if (write && ret == 0) {
  3155. /* update reachable_time as well, otherwise, the change will
  3156. * only be effective after the next time neigh_periodic_work
  3157. * decides to recompute it
  3158. */
  3159. neigh_set_reach_time(p);
  3160. }
  3161. return ret;
  3162. }
  3163. #define NEIGH_PARMS_DATA_OFFSET(index) \
  3164. (&((struct neigh_parms *) 0)->data[index])
  3165. #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \
  3166. [NEIGH_VAR_ ## attr] = { \
  3167. .procname = name, \
  3168. .data = NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \
  3169. .maxlen = sizeof(int), \
  3170. .mode = mval, \
  3171. .proc_handler = proc, \
  3172. }
  3173. #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \
  3174. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax)
  3175. #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \
  3176. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies)
  3177. #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \
  3178. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies)
  3179. #define NEIGH_SYSCTL_MS_JIFFIES_POSITIVE_ENTRY(attr, name) \
  3180. NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies_positive)
  3181. #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \
  3182. NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies)
  3183. #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \
  3184. NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen)
  3185. static struct neigh_sysctl_table {
  3186. struct ctl_table_header *sysctl_header;
  3187. struct ctl_table neigh_vars[NEIGH_VAR_MAX];
  3188. } neigh_sysctl_template __read_mostly = {
  3189. .neigh_vars = {
  3190. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
  3191. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
  3192. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
  3193. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
  3194. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
  3195. NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
  3196. NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
  3197. NEIGH_SYSCTL_MS_JIFFIES_POSITIVE_ENTRY(INTERVAL_PROBE_TIME_MS,
  3198. "interval_probe_time_ms"),
  3199. NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"),
  3200. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"),
  3201. NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"),
  3202. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"),
  3203. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"),
  3204. NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"),
  3205. NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"),
  3206. NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"),
  3207. NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"),
  3208. [NEIGH_VAR_GC_INTERVAL] = {
  3209. .procname = "gc_interval",
  3210. .maxlen = sizeof(int),
  3211. .mode = 0644,
  3212. .proc_handler = proc_dointvec_jiffies,
  3213. },
  3214. [NEIGH_VAR_GC_THRESH1] = {
  3215. .procname = "gc_thresh1",
  3216. .maxlen = sizeof(int),
  3217. .mode = 0644,
  3218. .extra1 = SYSCTL_ZERO,
  3219. .extra2 = SYSCTL_INT_MAX,
  3220. .proc_handler = proc_dointvec_minmax,
  3221. },
  3222. [NEIGH_VAR_GC_THRESH2] = {
  3223. .procname = "gc_thresh2",
  3224. .maxlen = sizeof(int),
  3225. .mode = 0644,
  3226. .extra1 = SYSCTL_ZERO,
  3227. .extra2 = SYSCTL_INT_MAX,
  3228. .proc_handler = proc_dointvec_minmax,
  3229. },
  3230. [NEIGH_VAR_GC_THRESH3] = {
  3231. .procname = "gc_thresh3",
  3232. .maxlen = sizeof(int),
  3233. .mode = 0644,
  3234. .extra1 = SYSCTL_ZERO,
  3235. .extra2 = SYSCTL_INT_MAX,
  3236. .proc_handler = proc_dointvec_minmax,
  3237. },
  3238. },
  3239. };
  3240. int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
  3241. proc_handler *handler)
  3242. {
  3243. int i;
  3244. struct neigh_sysctl_table *t;
  3245. const char *dev_name_source;
  3246. char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ];
  3247. char *p_name;
  3248. size_t neigh_vars_size;
  3249. t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL_ACCOUNT);
  3250. if (!t)
  3251. goto err;
  3252. for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) {
  3253. t->neigh_vars[i].data += (long) p;
  3254. t->neigh_vars[i].extra1 = dev;
  3255. t->neigh_vars[i].extra2 = p;
  3256. }
  3257. neigh_vars_size = ARRAY_SIZE(t->neigh_vars);
  3258. if (dev) {
  3259. dev_name_source = dev->name;
  3260. /* Terminate the table early */
  3261. neigh_vars_size = NEIGH_VAR_BASE_REACHABLE_TIME_MS + 1;
  3262. } else {
  3263. struct neigh_table *tbl = p->tbl;
  3264. dev_name_source = "default";
  3265. t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval;
  3266. t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1;
  3267. t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2;
  3268. t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3;
  3269. }
  3270. if (handler) {
  3271. /* RetransTime */
  3272. t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler;
  3273. /* ReachableTime */
  3274. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler;
  3275. /* RetransTime (in milliseconds)*/
  3276. t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler;
  3277. /* ReachableTime (in milliseconds) */
  3278. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler;
  3279. } else {
  3280. /* Those handlers will update p->reachable_time after
  3281. * base_reachable_time(_ms) is set to ensure the new timer starts being
  3282. * applied after the next neighbour update instead of waiting for
  3283. * neigh_periodic_work to update its value (can be multiple minutes)
  3284. * So any handler that replaces them should do this as well
  3285. */
  3286. /* ReachableTime */
  3287. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler =
  3288. neigh_proc_base_reachable_time;
  3289. /* ReachableTime (in milliseconds) */
  3290. t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler =
  3291. neigh_proc_base_reachable_time;
  3292. }
  3293. switch (neigh_parms_family(p)) {
  3294. case AF_INET:
  3295. p_name = "ipv4";
  3296. break;
  3297. case AF_INET6:
  3298. p_name = "ipv6";
  3299. break;
  3300. default:
  3301. BUG();
  3302. }
  3303. snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s",
  3304. p_name, dev_name_source);
  3305. t->sysctl_header = register_net_sysctl_sz(neigh_parms_net(p),
  3306. neigh_path, t->neigh_vars,
  3307. neigh_vars_size);
  3308. if (!t->sysctl_header)
  3309. goto free;
  3310. p->sysctl_table = t;
  3311. return 0;
  3312. free:
  3313. kfree(t);
  3314. err:
  3315. return -ENOBUFS;
  3316. }
  3317. EXPORT_SYMBOL(neigh_sysctl_register);
  3318. void neigh_sysctl_unregister(struct neigh_parms *p)
  3319. {
  3320. if (p->sysctl_table) {
  3321. struct neigh_sysctl_table *t = p->sysctl_table;
  3322. p->sysctl_table = NULL;
  3323. unregister_net_sysctl_table(t->sysctl_header);
  3324. kfree(t);
  3325. }
  3326. }
  3327. EXPORT_SYMBOL(neigh_sysctl_unregister);
  3328. #endif /* CONFIG_SYSCTL */
  3329. static const struct rtnl_msg_handler neigh_rtnl_msg_handlers[] __initconst = {
  3330. {.msgtype = RTM_NEWNEIGH, .doit = neigh_add},
  3331. {.msgtype = RTM_DELNEIGH, .doit = neigh_delete},
  3332. {.msgtype = RTM_GETNEIGH, .doit = neigh_get, .dumpit = neigh_dump_info,
  3333. .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
  3334. {.msgtype = RTM_GETNEIGHTBL, .dumpit = neightbl_dump_info,
  3335. .flags = RTNL_FLAG_DUMP_UNLOCKED},
  3336. {.msgtype = RTM_SETNEIGHTBL, .doit = neightbl_set,
  3337. .flags = RTNL_FLAG_DOIT_UNLOCKED},
  3338. };
  3339. static int __init neigh_init(void)
  3340. {
  3341. rtnl_register_many(neigh_rtnl_msg_handlers);
  3342. return 0;
  3343. }
  3344. subsys_initcall(neigh_init);