route.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525
  1. /*
  2. * Linux INET6 implementation
  3. * FIB front-end.
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. /* Changes:
  14. *
  15. * YOSHIFUJI Hideaki @USAGI
  16. * reworked default router selection.
  17. * - respect outgoing interface
  18. * - select from (probably) reachable routers (i.e.
  19. * routers in REACHABLE, STALE, DELAY or PROBE states).
  20. * - always select the same router if it is (probably)
  21. * reachable. otherwise, round-robin the list.
  22. * Ville Nuorvala
  23. * Fixed routing subtrees.
  24. */
  25. #define pr_fmt(fmt) "IPv6: " fmt
  26. #include <linux/capability.h>
  27. #include <linux/errno.h>
  28. #include <linux/export.h>
  29. #include <linux/types.h>
  30. #include <linux/times.h>
  31. #include <linux/socket.h>
  32. #include <linux/sockios.h>
  33. #include <linux/net.h>
  34. #include <linux/route.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/in6.h>
  37. #include <linux/mroute6.h>
  38. #include <linux/init.h>
  39. #include <linux/if_arp.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/nsproxy.h>
  43. #include <linux/slab.h>
  44. #include <net/net_namespace.h>
  45. #include <net/snmp.h>
  46. #include <net/ipv6.h>
  47. #include <net/ip6_fib.h>
  48. #include <net/ip6_route.h>
  49. #include <net/ndisc.h>
  50. #include <net/addrconf.h>
  51. #include <net/tcp.h>
  52. #include <linux/rtnetlink.h>
  53. #include <net/dst.h>
  54. #include <net/xfrm.h>
  55. #include <net/netevent.h>
  56. #include <net/netlink.h>
  57. #include <net/nexthop.h>
  58. #include <asm/uaccess.h>
  59. #ifdef CONFIG_SYSCTL
  60. #include <linux/sysctl.h>
  61. #endif
  62. enum rt6_nud_state {
  63. RT6_NUD_FAIL_HARD = -3,
  64. RT6_NUD_FAIL_PROBE = -2,
  65. RT6_NUD_FAIL_DO_RR = -1,
  66. RT6_NUD_SUCCEED = 1
  67. };
  68. static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort);
  69. static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
  70. static unsigned int ip6_default_advmss(const struct dst_entry *dst);
  71. static unsigned int ip6_mtu(const struct dst_entry *dst);
  72. static struct dst_entry *ip6_negative_advice(struct dst_entry *);
  73. static void ip6_dst_destroy(struct dst_entry *);
  74. static void ip6_dst_ifdown(struct dst_entry *,
  75. struct net_device *dev, int how);
  76. static int ip6_dst_gc(struct dst_ops *ops);
  77. static int ip6_pkt_discard(struct sk_buff *skb);
  78. static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb);
  79. static int ip6_pkt_prohibit(struct sk_buff *skb);
  80. static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb);
  81. static void ip6_link_failure(struct sk_buff *skb);
  82. static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  83. struct sk_buff *skb, u32 mtu);
  84. static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
  85. struct sk_buff *skb);
  86. static void rt6_dst_from_metrics_check(struct rt6_info *rt);
  87. static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
  88. #ifdef CONFIG_IPV6_ROUTE_INFO
  89. static struct rt6_info *rt6_add_route_info(struct net *net,
  90. const struct in6_addr *prefix, int prefixlen,
  91. const struct in6_addr *gwaddr, int ifindex,
  92. unsigned int pref);
  93. static struct rt6_info *rt6_get_route_info(struct net *net,
  94. const struct in6_addr *prefix, int prefixlen,
  95. const struct in6_addr *gwaddr, int ifindex);
  96. #endif
  97. struct uncached_list {
  98. spinlock_t lock;
  99. struct list_head head;
  100. };
  101. static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
  102. static void rt6_uncached_list_add(struct rt6_info *rt)
  103. {
  104. struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
  105. rt->dst.flags |= DST_NOCACHE;
  106. rt->rt6i_uncached_list = ul;
  107. spin_lock_bh(&ul->lock);
  108. list_add_tail(&rt->rt6i_uncached, &ul->head);
  109. spin_unlock_bh(&ul->lock);
  110. }
  111. static void rt6_uncached_list_del(struct rt6_info *rt)
  112. {
  113. if (!list_empty(&rt->rt6i_uncached)) {
  114. struct uncached_list *ul = rt->rt6i_uncached_list;
  115. spin_lock_bh(&ul->lock);
  116. list_del(&rt->rt6i_uncached);
  117. spin_unlock_bh(&ul->lock);
  118. }
  119. }
  120. static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
  121. {
  122. struct net_device *loopback_dev = net->loopback_dev;
  123. int cpu;
  124. for_each_possible_cpu(cpu) {
  125. struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
  126. struct rt6_info *rt;
  127. spin_lock_bh(&ul->lock);
  128. list_for_each_entry(rt, &ul->head, rt6i_uncached) {
  129. struct inet6_dev *rt_idev = rt->rt6i_idev;
  130. struct net_device *rt_dev = rt->dst.dev;
  131. if (rt_idev && (rt_idev->dev == dev || !dev) &&
  132. rt_idev->dev != loopback_dev) {
  133. rt->rt6i_idev = in6_dev_get(loopback_dev);
  134. in6_dev_put(rt_idev);
  135. }
  136. if (rt_dev && (rt_dev == dev || !dev) &&
  137. rt_dev != loopback_dev) {
  138. rt->dst.dev = loopback_dev;
  139. dev_hold(rt->dst.dev);
  140. dev_put(rt_dev);
  141. }
  142. }
  143. spin_unlock_bh(&ul->lock);
  144. }
  145. }
  146. static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt)
  147. {
  148. return dst_metrics_write_ptr(rt->dst.from);
  149. }
  150. static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
  151. {
  152. struct rt6_info *rt = (struct rt6_info *)dst;
  153. if (rt->rt6i_flags & RTF_PCPU)
  154. return rt6_pcpu_cow_metrics(rt);
  155. else if (rt->rt6i_flags & RTF_CACHE)
  156. return NULL;
  157. else
  158. return dst_cow_metrics_generic(dst, old);
  159. }
  160. static inline const void *choose_neigh_daddr(struct rt6_info *rt,
  161. struct sk_buff *skb,
  162. const void *daddr)
  163. {
  164. struct in6_addr *p = &rt->rt6i_gateway;
  165. if (!ipv6_addr_any(p))
  166. return (const void *) p;
  167. else if (skb)
  168. return &ipv6_hdr(skb)->daddr;
  169. return daddr;
  170. }
  171. static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
  172. struct sk_buff *skb,
  173. const void *daddr)
  174. {
  175. struct rt6_info *rt = (struct rt6_info *) dst;
  176. struct neighbour *n;
  177. daddr = choose_neigh_daddr(rt, skb, daddr);
  178. n = __ipv6_neigh_lookup(dst->dev, daddr);
  179. if (n)
  180. return n;
  181. return neigh_create(&nd_tbl, daddr, dst->dev);
  182. }
  183. static struct dst_ops ip6_dst_ops_template = {
  184. .family = AF_INET6,
  185. .gc = ip6_dst_gc,
  186. .gc_thresh = 1024,
  187. .check = ip6_dst_check,
  188. .default_advmss = ip6_default_advmss,
  189. .mtu = ip6_mtu,
  190. .cow_metrics = ipv6_cow_metrics,
  191. .destroy = ip6_dst_destroy,
  192. .ifdown = ip6_dst_ifdown,
  193. .negative_advice = ip6_negative_advice,
  194. .link_failure = ip6_link_failure,
  195. .update_pmtu = ip6_rt_update_pmtu,
  196. .redirect = rt6_do_redirect,
  197. .local_out = __ip6_local_out,
  198. .neigh_lookup = ip6_neigh_lookup,
  199. };
  200. static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
  201. {
  202. unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
  203. return mtu ? : dst->dev->mtu;
  204. }
  205. static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
  206. struct sk_buff *skb, u32 mtu)
  207. {
  208. }
  209. static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
  210. struct sk_buff *skb)
  211. {
  212. }
  213. static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
  214. unsigned long old)
  215. {
  216. return NULL;
  217. }
  218. static struct dst_ops ip6_dst_blackhole_ops = {
  219. .family = AF_INET6,
  220. .destroy = ip6_dst_destroy,
  221. .check = ip6_dst_check,
  222. .mtu = ip6_blackhole_mtu,
  223. .default_advmss = ip6_default_advmss,
  224. .update_pmtu = ip6_rt_blackhole_update_pmtu,
  225. .redirect = ip6_rt_blackhole_redirect,
  226. .cow_metrics = ip6_rt_blackhole_cow_metrics,
  227. .neigh_lookup = ip6_neigh_lookup,
  228. };
  229. static const u32 ip6_template_metrics[RTAX_MAX] = {
  230. [RTAX_HOPLIMIT - 1] = 0,
  231. };
  232. static const struct rt6_info ip6_null_entry_template = {
  233. .dst = {
  234. .__refcnt = ATOMIC_INIT(1),
  235. .__use = 1,
  236. .obsolete = DST_OBSOLETE_FORCE_CHK,
  237. .error = -ENETUNREACH,
  238. .input = ip6_pkt_discard,
  239. .output = ip6_pkt_discard_out,
  240. },
  241. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  242. .rt6i_protocol = RTPROT_KERNEL,
  243. .rt6i_metric = ~(u32) 0,
  244. .rt6i_ref = ATOMIC_INIT(1),
  245. };
  246. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  247. static const struct rt6_info ip6_prohibit_entry_template = {
  248. .dst = {
  249. .__refcnt = ATOMIC_INIT(1),
  250. .__use = 1,
  251. .obsolete = DST_OBSOLETE_FORCE_CHK,
  252. .error = -EACCES,
  253. .input = ip6_pkt_prohibit,
  254. .output = ip6_pkt_prohibit_out,
  255. },
  256. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  257. .rt6i_protocol = RTPROT_KERNEL,
  258. .rt6i_metric = ~(u32) 0,
  259. .rt6i_ref = ATOMIC_INIT(1),
  260. };
  261. static const struct rt6_info ip6_blk_hole_entry_template = {
  262. .dst = {
  263. .__refcnt = ATOMIC_INIT(1),
  264. .__use = 1,
  265. .obsolete = DST_OBSOLETE_FORCE_CHK,
  266. .error = -EINVAL,
  267. .input = dst_discard,
  268. .output = dst_discard_sk,
  269. },
  270. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  271. .rt6i_protocol = RTPROT_KERNEL,
  272. .rt6i_metric = ~(u32) 0,
  273. .rt6i_ref = ATOMIC_INIT(1),
  274. };
  275. #endif
  276. /* allocate dst with ip6_dst_ops */
  277. static struct rt6_info *__ip6_dst_alloc(struct net *net,
  278. struct net_device *dev,
  279. int flags,
  280. struct fib6_table *table)
  281. {
  282. struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
  283. 0, DST_OBSOLETE_FORCE_CHK, flags);
  284. if (rt) {
  285. struct dst_entry *dst = &rt->dst;
  286. memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
  287. INIT_LIST_HEAD(&rt->rt6i_siblings);
  288. INIT_LIST_HEAD(&rt->rt6i_uncached);
  289. }
  290. return rt;
  291. }
  292. static struct rt6_info *ip6_dst_alloc(struct net *net,
  293. struct net_device *dev,
  294. int flags,
  295. struct fib6_table *table)
  296. {
  297. struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags, table);
  298. if (rt) {
  299. rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC);
  300. if (rt->rt6i_pcpu) {
  301. int cpu;
  302. for_each_possible_cpu(cpu) {
  303. struct rt6_info **p;
  304. p = per_cpu_ptr(rt->rt6i_pcpu, cpu);
  305. /* no one shares rt */
  306. *p = NULL;
  307. }
  308. } else {
  309. dst_destroy((struct dst_entry *)rt);
  310. return NULL;
  311. }
  312. }
  313. return rt;
  314. }
  315. static void ip6_dst_destroy(struct dst_entry *dst)
  316. {
  317. struct rt6_info *rt = (struct rt6_info *)dst;
  318. struct dst_entry *from = dst->from;
  319. struct inet6_dev *idev;
  320. dst_destroy_metrics_generic(dst);
  321. if (rt->rt6i_pcpu)
  322. free_percpu(rt->rt6i_pcpu);
  323. rt6_uncached_list_del(rt);
  324. idev = rt->rt6i_idev;
  325. if (idev) {
  326. rt->rt6i_idev = NULL;
  327. in6_dev_put(idev);
  328. }
  329. dst->from = NULL;
  330. dst_release(from);
  331. }
  332. static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  333. int how)
  334. {
  335. struct rt6_info *rt = (struct rt6_info *)dst;
  336. struct inet6_dev *idev = rt->rt6i_idev;
  337. struct net_device *loopback_dev =
  338. dev_net(dev)->loopback_dev;
  339. if (dev != loopback_dev) {
  340. if (idev && idev->dev == dev) {
  341. struct inet6_dev *loopback_idev =
  342. in6_dev_get(loopback_dev);
  343. if (loopback_idev) {
  344. rt->rt6i_idev = loopback_idev;
  345. in6_dev_put(idev);
  346. }
  347. }
  348. }
  349. }
  350. static bool rt6_check_expired(const struct rt6_info *rt)
  351. {
  352. if (rt->rt6i_flags & RTF_EXPIRES) {
  353. if (time_after(jiffies, rt->dst.expires))
  354. return true;
  355. } else if (rt->dst.from) {
  356. return rt6_check_expired((struct rt6_info *) rt->dst.from);
  357. }
  358. return false;
  359. }
  360. /* Multipath route selection:
  361. * Hash based function using packet header and flowlabel.
  362. * Adapted from fib_info_hashfn()
  363. */
  364. static int rt6_info_hash_nhsfn(unsigned int candidate_count,
  365. const struct flowi6 *fl6)
  366. {
  367. unsigned int val = fl6->flowi6_proto;
  368. val ^= ipv6_addr_hash(&fl6->daddr);
  369. val ^= ipv6_addr_hash(&fl6->saddr);
  370. /* Work only if this not encapsulated */
  371. switch (fl6->flowi6_proto) {
  372. case IPPROTO_UDP:
  373. case IPPROTO_TCP:
  374. case IPPROTO_SCTP:
  375. val ^= (__force u16)fl6->fl6_sport;
  376. val ^= (__force u16)fl6->fl6_dport;
  377. break;
  378. case IPPROTO_ICMPV6:
  379. val ^= (__force u16)fl6->fl6_icmp_type;
  380. val ^= (__force u16)fl6->fl6_icmp_code;
  381. break;
  382. }
  383. /* RFC6438 recommands to use flowlabel */
  384. val ^= (__force u32)fl6->flowlabel;
  385. /* Perhaps, we need to tune, this function? */
  386. val = val ^ (val >> 7) ^ (val >> 12);
  387. return val % candidate_count;
  388. }
  389. static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
  390. struct flowi6 *fl6, int oif,
  391. int strict)
  392. {
  393. struct rt6_info *sibling, *next_sibling;
  394. int route_choosen;
  395. route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
  396. /* Don't change the route, if route_choosen == 0
  397. * (siblings does not include ourself)
  398. */
  399. if (route_choosen)
  400. list_for_each_entry_safe(sibling, next_sibling,
  401. &match->rt6i_siblings, rt6i_siblings) {
  402. route_choosen--;
  403. if (route_choosen == 0) {
  404. if (rt6_score_route(sibling, oif, strict) < 0)
  405. break;
  406. match = sibling;
  407. break;
  408. }
  409. }
  410. return match;
  411. }
  412. /*
  413. * Route lookup. Any table->tb6_lock is implied.
  414. */
  415. static inline struct rt6_info *rt6_device_match(struct net *net,
  416. struct rt6_info *rt,
  417. const struct in6_addr *saddr,
  418. int oif,
  419. int flags)
  420. {
  421. struct rt6_info *local = NULL;
  422. struct rt6_info *sprt;
  423. if (!oif && ipv6_addr_any(saddr))
  424. goto out;
  425. for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
  426. struct net_device *dev = sprt->dst.dev;
  427. if (oif) {
  428. if (dev->ifindex == oif)
  429. return sprt;
  430. if (dev->flags & IFF_LOOPBACK) {
  431. if (!sprt->rt6i_idev ||
  432. sprt->rt6i_idev->dev->ifindex != oif) {
  433. if (flags & RT6_LOOKUP_F_IFACE && oif)
  434. continue;
  435. if (local && (!oif ||
  436. local->rt6i_idev->dev->ifindex == oif))
  437. continue;
  438. }
  439. local = sprt;
  440. }
  441. } else {
  442. if (ipv6_chk_addr(net, saddr, dev,
  443. flags & RT6_LOOKUP_F_IFACE))
  444. return sprt;
  445. }
  446. }
  447. if (oif) {
  448. if (local)
  449. return local;
  450. if (flags & RT6_LOOKUP_F_IFACE)
  451. return net->ipv6.ip6_null_entry;
  452. }
  453. out:
  454. return rt;
  455. }
  456. #ifdef CONFIG_IPV6_ROUTER_PREF
  457. struct __rt6_probe_work {
  458. struct work_struct work;
  459. struct in6_addr target;
  460. struct net_device *dev;
  461. };
  462. static void rt6_probe_deferred(struct work_struct *w)
  463. {
  464. struct in6_addr mcaddr;
  465. struct __rt6_probe_work *work =
  466. container_of(w, struct __rt6_probe_work, work);
  467. addrconf_addr_solict_mult(&work->target, &mcaddr);
  468. ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL);
  469. dev_put(work->dev);
  470. kfree(work);
  471. }
  472. static void rt6_probe(struct rt6_info *rt)
  473. {
  474. struct neighbour *neigh;
  475. /*
  476. * Okay, this does not seem to be appropriate
  477. * for now, however, we need to check if it
  478. * is really so; aka Router Reachability Probing.
  479. *
  480. * Router Reachability Probe MUST be rate-limited
  481. * to no more than one per minute.
  482. */
  483. if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
  484. return;
  485. rcu_read_lock_bh();
  486. neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
  487. if (neigh) {
  488. write_lock(&neigh->lock);
  489. if (neigh->nud_state & NUD_VALID)
  490. goto out;
  491. }
  492. if (!neigh ||
  493. time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
  494. struct __rt6_probe_work *work;
  495. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  496. if (neigh && work)
  497. __neigh_set_probe_once(neigh);
  498. if (neigh)
  499. write_unlock(&neigh->lock);
  500. if (work) {
  501. INIT_WORK(&work->work, rt6_probe_deferred);
  502. work->target = rt->rt6i_gateway;
  503. dev_hold(rt->dst.dev);
  504. work->dev = rt->dst.dev;
  505. schedule_work(&work->work);
  506. }
  507. } else {
  508. out:
  509. write_unlock(&neigh->lock);
  510. }
  511. rcu_read_unlock_bh();
  512. }
  513. #else
  514. static inline void rt6_probe(struct rt6_info *rt)
  515. {
  516. }
  517. #endif
  518. /*
  519. * Default Router Selection (RFC 2461 6.3.6)
  520. */
  521. static inline int rt6_check_dev(struct rt6_info *rt, int oif)
  522. {
  523. struct net_device *dev = rt->dst.dev;
  524. if (!oif || dev->ifindex == oif)
  525. return 2;
  526. if ((dev->flags & IFF_LOOPBACK) &&
  527. rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
  528. return 1;
  529. return 0;
  530. }
  531. static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
  532. {
  533. struct neighbour *neigh;
  534. enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
  535. if (rt->rt6i_flags & RTF_NONEXTHOP ||
  536. !(rt->rt6i_flags & RTF_GATEWAY))
  537. return RT6_NUD_SUCCEED;
  538. rcu_read_lock_bh();
  539. neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
  540. if (neigh) {
  541. read_lock(&neigh->lock);
  542. if (neigh->nud_state & NUD_VALID)
  543. ret = RT6_NUD_SUCCEED;
  544. #ifdef CONFIG_IPV6_ROUTER_PREF
  545. else if (!(neigh->nud_state & NUD_FAILED))
  546. ret = RT6_NUD_SUCCEED;
  547. else
  548. ret = RT6_NUD_FAIL_PROBE;
  549. #endif
  550. read_unlock(&neigh->lock);
  551. } else {
  552. ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
  553. RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
  554. }
  555. rcu_read_unlock_bh();
  556. return ret;
  557. }
  558. static int rt6_score_route(struct rt6_info *rt, int oif,
  559. int strict)
  560. {
  561. int m;
  562. m = rt6_check_dev(rt, oif);
  563. if (!m && (strict & RT6_LOOKUP_F_IFACE))
  564. return RT6_NUD_FAIL_HARD;
  565. #ifdef CONFIG_IPV6_ROUTER_PREF
  566. m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
  567. #endif
  568. if (strict & RT6_LOOKUP_F_REACHABLE) {
  569. int n = rt6_check_neigh(rt);
  570. if (n < 0)
  571. return n;
  572. }
  573. return m;
  574. }
  575. static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
  576. int *mpri, struct rt6_info *match,
  577. bool *do_rr)
  578. {
  579. int m;
  580. bool match_do_rr = false;
  581. if (rt6_check_expired(rt))
  582. goto out;
  583. m = rt6_score_route(rt, oif, strict);
  584. if (m == RT6_NUD_FAIL_DO_RR) {
  585. match_do_rr = true;
  586. m = 0; /* lowest valid score */
  587. } else if (m == RT6_NUD_FAIL_HARD) {
  588. goto out;
  589. }
  590. if (strict & RT6_LOOKUP_F_REACHABLE)
  591. rt6_probe(rt);
  592. /* note that m can be RT6_NUD_FAIL_PROBE at this point */
  593. if (m > *mpri) {
  594. *do_rr = match_do_rr;
  595. *mpri = m;
  596. match = rt;
  597. }
  598. out:
  599. return match;
  600. }
  601. static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
  602. struct rt6_info *rr_head,
  603. u32 metric, int oif, int strict,
  604. bool *do_rr)
  605. {
  606. struct rt6_info *rt, *match, *cont;
  607. int mpri = -1;
  608. match = NULL;
  609. cont = NULL;
  610. for (rt = rr_head; rt; rt = rt->dst.rt6_next) {
  611. if (rt->rt6i_metric != metric) {
  612. cont = rt;
  613. break;
  614. }
  615. match = find_match(rt, oif, strict, &mpri, match, do_rr);
  616. }
  617. for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) {
  618. if (rt->rt6i_metric != metric) {
  619. cont = rt;
  620. break;
  621. }
  622. match = find_match(rt, oif, strict, &mpri, match, do_rr);
  623. }
  624. if (match || !cont)
  625. return match;
  626. for (rt = cont; rt; rt = rt->dst.rt6_next)
  627. match = find_match(rt, oif, strict, &mpri, match, do_rr);
  628. return match;
  629. }
  630. static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
  631. {
  632. struct rt6_info *match, *rt0;
  633. struct net *net;
  634. bool do_rr = false;
  635. rt0 = fn->rr_ptr;
  636. if (!rt0)
  637. fn->rr_ptr = rt0 = fn->leaf;
  638. match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
  639. &do_rr);
  640. if (do_rr) {
  641. struct rt6_info *next = rt0->dst.rt6_next;
  642. /* no entries matched; do round-robin */
  643. if (!next || next->rt6i_metric != rt0->rt6i_metric)
  644. next = fn->leaf;
  645. if (next != rt0)
  646. fn->rr_ptr = next;
  647. }
  648. net = dev_net(rt0->dst.dev);
  649. return match ? match : net->ipv6.ip6_null_entry;
  650. }
  651. static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
  652. {
  653. return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
  654. }
  655. #ifdef CONFIG_IPV6_ROUTE_INFO
  656. int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
  657. const struct in6_addr *gwaddr)
  658. {
  659. struct net *net = dev_net(dev);
  660. struct route_info *rinfo = (struct route_info *) opt;
  661. struct in6_addr prefix_buf, *prefix;
  662. unsigned int pref;
  663. unsigned long lifetime;
  664. struct rt6_info *rt;
  665. if (len < sizeof(struct route_info)) {
  666. return -EINVAL;
  667. }
  668. /* Sanity check for prefix_len and length */
  669. if (rinfo->length > 3) {
  670. return -EINVAL;
  671. } else if (rinfo->prefix_len > 128) {
  672. return -EINVAL;
  673. } else if (rinfo->prefix_len > 64) {
  674. if (rinfo->length < 2) {
  675. return -EINVAL;
  676. }
  677. } else if (rinfo->prefix_len > 0) {
  678. if (rinfo->length < 1) {
  679. return -EINVAL;
  680. }
  681. }
  682. pref = rinfo->route_pref;
  683. if (pref == ICMPV6_ROUTER_PREF_INVALID)
  684. return -EINVAL;
  685. lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
  686. if (rinfo->length == 3)
  687. prefix = (struct in6_addr *)rinfo->prefix;
  688. else {
  689. /* this function is safe */
  690. ipv6_addr_prefix(&prefix_buf,
  691. (struct in6_addr *)rinfo->prefix,
  692. rinfo->prefix_len);
  693. prefix = &prefix_buf;
  694. }
  695. if (rinfo->prefix_len == 0)
  696. rt = rt6_get_dflt_router(gwaddr, dev);
  697. else
  698. rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
  699. gwaddr, dev->ifindex);
  700. if (rt && !lifetime) {
  701. ip6_del_rt(rt);
  702. rt = NULL;
  703. }
  704. if (!rt && lifetime)
  705. rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
  706. pref);
  707. else if (rt)
  708. rt->rt6i_flags = RTF_ROUTEINFO |
  709. (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
  710. if (rt) {
  711. if (!addrconf_finite_timeout(lifetime))
  712. rt6_clean_expires(rt);
  713. else
  714. rt6_set_expires(rt, jiffies + HZ * lifetime);
  715. ip6_rt_put(rt);
  716. }
  717. return 0;
  718. }
  719. #endif
  720. static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
  721. struct in6_addr *saddr)
  722. {
  723. struct fib6_node *pn;
  724. while (1) {
  725. if (fn->fn_flags & RTN_TL_ROOT)
  726. return NULL;
  727. pn = fn->parent;
  728. if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn)
  729. fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr);
  730. else
  731. fn = pn;
  732. if (fn->fn_flags & RTN_RTINFO)
  733. return fn;
  734. }
  735. }
  736. static struct rt6_info *ip6_pol_route_lookup(struct net *net,
  737. struct fib6_table *table,
  738. struct flowi6 *fl6, int flags)
  739. {
  740. struct fib6_node *fn;
  741. struct rt6_info *rt;
  742. read_lock_bh(&table->tb6_lock);
  743. fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  744. restart:
  745. rt = fn->leaf;
  746. rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
  747. if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
  748. rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
  749. if (rt == net->ipv6.ip6_null_entry) {
  750. fn = fib6_backtrack(fn, &fl6->saddr);
  751. if (fn)
  752. goto restart;
  753. }
  754. dst_use(&rt->dst, jiffies);
  755. read_unlock_bh(&table->tb6_lock);
  756. return rt;
  757. }
  758. struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
  759. int flags)
  760. {
  761. return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
  762. }
  763. EXPORT_SYMBOL_GPL(ip6_route_lookup);
  764. struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
  765. const struct in6_addr *saddr, int oif, int strict)
  766. {
  767. struct flowi6 fl6 = {
  768. .flowi6_oif = oif,
  769. .daddr = *daddr,
  770. };
  771. struct dst_entry *dst;
  772. int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
  773. if (saddr) {
  774. memcpy(&fl6.saddr, saddr, sizeof(*saddr));
  775. flags |= RT6_LOOKUP_F_HAS_SADDR;
  776. }
  777. dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
  778. if (dst->error == 0)
  779. return (struct rt6_info *) dst;
  780. dst_release(dst);
  781. return NULL;
  782. }
  783. EXPORT_SYMBOL(rt6_lookup);
  784. /* ip6_ins_rt is called with FREE table->tb6_lock.
  785. It takes new route entry, the addition fails by any reason the
  786. route is freed. In any case, if caller does not hold it, it may
  787. be destroyed.
  788. */
  789. static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
  790. struct mx6_config *mxc)
  791. {
  792. int err;
  793. struct fib6_table *table;
  794. table = rt->rt6i_table;
  795. write_lock_bh(&table->tb6_lock);
  796. err = fib6_add(&table->tb6_root, rt, info, mxc);
  797. write_unlock_bh(&table->tb6_lock);
  798. return err;
  799. }
  800. int ip6_ins_rt(struct rt6_info *rt)
  801. {
  802. struct nl_info info = { .nl_net = dev_net(rt->dst.dev), };
  803. struct mx6_config mxc = { .mx = NULL, };
  804. return __ip6_ins_rt(rt, &info, &mxc);
  805. }
  806. static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
  807. const struct in6_addr *daddr,
  808. const struct in6_addr *saddr)
  809. {
  810. struct rt6_info *rt;
  811. /*
  812. * Clone the route.
  813. */
  814. if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
  815. ort = (struct rt6_info *)ort->dst.from;
  816. rt = __ip6_dst_alloc(dev_net(ort->dst.dev), ort->dst.dev,
  817. 0, ort->rt6i_table);
  818. if (!rt)
  819. return NULL;
  820. ip6_rt_copy_init(rt, ort);
  821. rt->rt6i_flags |= RTF_CACHE;
  822. rt->rt6i_metric = 0;
  823. rt->dst.flags |= DST_HOST;
  824. rt->rt6i_dst.addr = *daddr;
  825. rt->rt6i_dst.plen = 128;
  826. if (!rt6_is_gw_or_nonexthop(ort)) {
  827. if (ort->rt6i_dst.plen != 128 &&
  828. ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
  829. rt->rt6i_flags |= RTF_ANYCAST;
  830. #ifdef CONFIG_IPV6_SUBTREES
  831. if (rt->rt6i_src.plen && saddr) {
  832. rt->rt6i_src.addr = *saddr;
  833. rt->rt6i_src.plen = 128;
  834. }
  835. #endif
  836. }
  837. return rt;
  838. }
  839. static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt)
  840. {
  841. struct rt6_info *pcpu_rt;
  842. pcpu_rt = __ip6_dst_alloc(dev_net(rt->dst.dev),
  843. rt->dst.dev, rt->dst.flags,
  844. rt->rt6i_table);
  845. if (!pcpu_rt)
  846. return NULL;
  847. ip6_rt_copy_init(pcpu_rt, rt);
  848. pcpu_rt->rt6i_protocol = rt->rt6i_protocol;
  849. pcpu_rt->rt6i_flags |= RTF_PCPU;
  850. return pcpu_rt;
  851. }
  852. /* It should be called with read_lock_bh(&tb6_lock) acquired */
  853. static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt)
  854. {
  855. struct rt6_info *pcpu_rt, *prev, **p;
  856. p = this_cpu_ptr(rt->rt6i_pcpu);
  857. pcpu_rt = *p;
  858. if (pcpu_rt)
  859. goto done;
  860. pcpu_rt = ip6_rt_pcpu_alloc(rt);
  861. if (!pcpu_rt) {
  862. struct net *net = dev_net(rt->dst.dev);
  863. pcpu_rt = net->ipv6.ip6_null_entry;
  864. goto done;
  865. }
  866. prev = cmpxchg(p, NULL, pcpu_rt);
  867. if (prev) {
  868. /* If someone did it before us, return prev instead */
  869. dst_destroy(&pcpu_rt->dst);
  870. pcpu_rt = prev;
  871. }
  872. done:
  873. dst_hold(&pcpu_rt->dst);
  874. rt6_dst_from_metrics_check(pcpu_rt);
  875. return pcpu_rt;
  876. }
  877. static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
  878. struct flowi6 *fl6, int flags)
  879. {
  880. struct fib6_node *fn, *saved_fn;
  881. struct rt6_info *rt;
  882. int strict = 0;
  883. strict |= flags & RT6_LOOKUP_F_IFACE;
  884. if (net->ipv6.devconf_all->forwarding == 0)
  885. strict |= RT6_LOOKUP_F_REACHABLE;
  886. read_lock_bh(&table->tb6_lock);
  887. fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  888. saved_fn = fn;
  889. redo_rt6_select:
  890. rt = rt6_select(fn, oif, strict);
  891. if (rt->rt6i_nsiblings)
  892. rt = rt6_multipath_select(rt, fl6, oif, strict);
  893. if (rt == net->ipv6.ip6_null_entry) {
  894. fn = fib6_backtrack(fn, &fl6->saddr);
  895. if (fn)
  896. goto redo_rt6_select;
  897. else if (strict & RT6_LOOKUP_F_REACHABLE) {
  898. /* also consider unreachable route */
  899. strict &= ~RT6_LOOKUP_F_REACHABLE;
  900. fn = saved_fn;
  901. goto redo_rt6_select;
  902. }
  903. }
  904. if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) {
  905. dst_use(&rt->dst, jiffies);
  906. read_unlock_bh(&table->tb6_lock);
  907. rt6_dst_from_metrics_check(rt);
  908. return rt;
  909. } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
  910. !(rt->rt6i_flags & RTF_GATEWAY))) {
  911. /* Create a RTF_CACHE clone which will not be
  912. * owned by the fib6 tree. It is for the special case where
  913. * the daddr in the skb during the neighbor look-up is different
  914. * from the fl6->daddr used to look-up route here.
  915. */
  916. struct rt6_info *uncached_rt;
  917. dst_use(&rt->dst, jiffies);
  918. read_unlock_bh(&table->tb6_lock);
  919. uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
  920. dst_release(&rt->dst);
  921. if (uncached_rt)
  922. rt6_uncached_list_add(uncached_rt);
  923. else
  924. uncached_rt = net->ipv6.ip6_null_entry;
  925. dst_hold(&uncached_rt->dst);
  926. return uncached_rt;
  927. } else {
  928. /* Get a percpu copy */
  929. struct rt6_info *pcpu_rt;
  930. rt->dst.lastuse = jiffies;
  931. rt->dst.__use++;
  932. pcpu_rt = rt6_get_pcpu_route(rt);
  933. read_unlock_bh(&table->tb6_lock);
  934. return pcpu_rt;
  935. }
  936. }
  937. static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
  938. struct flowi6 *fl6, int flags)
  939. {
  940. return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
  941. }
  942. static struct dst_entry *ip6_route_input_lookup(struct net *net,
  943. struct net_device *dev,
  944. struct flowi6 *fl6, int flags)
  945. {
  946. if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
  947. flags |= RT6_LOOKUP_F_IFACE;
  948. return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
  949. }
  950. void ip6_route_input(struct sk_buff *skb)
  951. {
  952. const struct ipv6hdr *iph = ipv6_hdr(skb);
  953. struct net *net = dev_net(skb->dev);
  954. int flags = RT6_LOOKUP_F_HAS_SADDR;
  955. struct flowi6 fl6 = {
  956. .flowi6_iif = skb->dev->ifindex,
  957. .daddr = iph->daddr,
  958. .saddr = iph->saddr,
  959. .flowlabel = ip6_flowinfo(iph),
  960. .flowi6_mark = skb->mark,
  961. .flowi6_proto = iph->nexthdr,
  962. };
  963. skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
  964. }
  965. static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
  966. struct flowi6 *fl6, int flags)
  967. {
  968. return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
  969. }
  970. struct dst_entry *ip6_route_output(struct net *net, const struct sock *sk,
  971. struct flowi6 *fl6)
  972. {
  973. int flags = 0;
  974. fl6->flowi6_iif = LOOPBACK_IFINDEX;
  975. if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
  976. flags |= RT6_LOOKUP_F_IFACE;
  977. if (!ipv6_addr_any(&fl6->saddr))
  978. flags |= RT6_LOOKUP_F_HAS_SADDR;
  979. else if (sk)
  980. flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
  981. return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
  982. }
  983. EXPORT_SYMBOL(ip6_route_output);
  984. struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
  985. {
  986. struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
  987. struct dst_entry *new = NULL;
  988. rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
  989. if (rt) {
  990. new = &rt->dst;
  991. memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
  992. new->__use = 1;
  993. new->input = dst_discard;
  994. new->output = dst_discard_sk;
  995. if (dst_metrics_read_only(&ort->dst))
  996. new->_metrics = ort->dst._metrics;
  997. else
  998. dst_copy_metrics(new, &ort->dst);
  999. rt->rt6i_idev = ort->rt6i_idev;
  1000. if (rt->rt6i_idev)
  1001. in6_dev_hold(rt->rt6i_idev);
  1002. rt->rt6i_gateway = ort->rt6i_gateway;
  1003. rt->rt6i_flags = ort->rt6i_flags;
  1004. rt->rt6i_metric = 0;
  1005. memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
  1006. #ifdef CONFIG_IPV6_SUBTREES
  1007. memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
  1008. #endif
  1009. dst_free(new);
  1010. }
  1011. dst_release(dst_orig);
  1012. return new ? new : ERR_PTR(-ENOMEM);
  1013. }
  1014. /*
  1015. * Destination cache support functions
  1016. */
  1017. static void rt6_dst_from_metrics_check(struct rt6_info *rt)
  1018. {
  1019. if (rt->dst.from &&
  1020. dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from))
  1021. dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true);
  1022. }
  1023. static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
  1024. {
  1025. if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
  1026. return NULL;
  1027. if (rt6_check_expired(rt))
  1028. return NULL;
  1029. return &rt->dst;
  1030. }
  1031. static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
  1032. {
  1033. if (rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
  1034. rt6_check((struct rt6_info *)(rt->dst.from), cookie))
  1035. return &rt->dst;
  1036. else
  1037. return NULL;
  1038. }
  1039. static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
  1040. {
  1041. struct rt6_info *rt;
  1042. rt = (struct rt6_info *) dst;
  1043. /* All IPV6 dsts are created with ->obsolete set to the value
  1044. * DST_OBSOLETE_FORCE_CHK which forces validation calls down
  1045. * into this function always.
  1046. */
  1047. rt6_dst_from_metrics_check(rt);
  1048. if ((rt->rt6i_flags & RTF_PCPU) || unlikely(dst->flags & DST_NOCACHE))
  1049. return rt6_dst_from_check(rt, cookie);
  1050. else
  1051. return rt6_check(rt, cookie);
  1052. }
  1053. static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
  1054. {
  1055. struct rt6_info *rt = (struct rt6_info *) dst;
  1056. if (rt) {
  1057. if (rt->rt6i_flags & RTF_CACHE) {
  1058. if (rt6_check_expired(rt)) {
  1059. ip6_del_rt(rt);
  1060. dst = NULL;
  1061. }
  1062. } else {
  1063. dst_release(dst);
  1064. dst = NULL;
  1065. }
  1066. }
  1067. return dst;
  1068. }
  1069. static void ip6_link_failure(struct sk_buff *skb)
  1070. {
  1071. struct rt6_info *rt;
  1072. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
  1073. rt = (struct rt6_info *) skb_dst(skb);
  1074. if (rt) {
  1075. if (rt->rt6i_flags & RTF_CACHE) {
  1076. dst_hold(&rt->dst);
  1077. if (ip6_del_rt(rt))
  1078. dst_free(&rt->dst);
  1079. } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
  1080. rt->rt6i_node->fn_sernum = -1;
  1081. }
  1082. }
  1083. }
  1084. static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
  1085. {
  1086. struct net *net = dev_net(rt->dst.dev);
  1087. rt->rt6i_flags |= RTF_MODIFIED;
  1088. rt->rt6i_pmtu = mtu;
  1089. rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
  1090. }
  1091. static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
  1092. const struct ipv6hdr *iph, u32 mtu)
  1093. {
  1094. struct rt6_info *rt6 = (struct rt6_info *)dst;
  1095. if (rt6->rt6i_flags & RTF_LOCAL)
  1096. return;
  1097. dst_confirm(dst);
  1098. mtu = max_t(u32, mtu, IPV6_MIN_MTU);
  1099. if (mtu >= dst_mtu(dst))
  1100. return;
  1101. if (rt6->rt6i_flags & RTF_CACHE) {
  1102. rt6_do_update_pmtu(rt6, mtu);
  1103. } else {
  1104. const struct in6_addr *daddr, *saddr;
  1105. struct rt6_info *nrt6;
  1106. if (iph) {
  1107. daddr = &iph->daddr;
  1108. saddr = &iph->saddr;
  1109. } else if (sk) {
  1110. daddr = &sk->sk_v6_daddr;
  1111. saddr = &inet6_sk(sk)->saddr;
  1112. } else {
  1113. return;
  1114. }
  1115. nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
  1116. if (nrt6) {
  1117. rt6_do_update_pmtu(nrt6, mtu);
  1118. /* ip6_ins_rt(nrt6) will bump the
  1119. * rt6->rt6i_node->fn_sernum
  1120. * which will fail the next rt6_check() and
  1121. * invalidate the sk->sk_dst_cache.
  1122. */
  1123. ip6_ins_rt(nrt6);
  1124. }
  1125. }
  1126. }
  1127. static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  1128. struct sk_buff *skb, u32 mtu)
  1129. {
  1130. __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
  1131. }
  1132. void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
  1133. int oif, u32 mark)
  1134. {
  1135. const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
  1136. struct dst_entry *dst;
  1137. struct flowi6 fl6;
  1138. memset(&fl6, 0, sizeof(fl6));
  1139. fl6.flowi6_oif = oif;
  1140. fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
  1141. fl6.daddr = iph->daddr;
  1142. fl6.saddr = iph->saddr;
  1143. fl6.flowlabel = ip6_flowinfo(iph);
  1144. dst = ip6_route_output(net, NULL, &fl6);
  1145. if (!dst->error)
  1146. __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
  1147. dst_release(dst);
  1148. }
  1149. EXPORT_SYMBOL_GPL(ip6_update_pmtu);
  1150. void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
  1151. {
  1152. ip6_update_pmtu(skb, sock_net(sk), mtu,
  1153. sk->sk_bound_dev_if, sk->sk_mark);
  1154. }
  1155. EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
  1156. /* Handle redirects */
  1157. struct ip6rd_flowi {
  1158. struct flowi6 fl6;
  1159. struct in6_addr gateway;
  1160. };
  1161. static struct rt6_info *__ip6_route_redirect(struct net *net,
  1162. struct fib6_table *table,
  1163. struct flowi6 *fl6,
  1164. int flags)
  1165. {
  1166. struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
  1167. struct rt6_info *rt;
  1168. struct fib6_node *fn;
  1169. /* Get the "current" route for this destination and
  1170. * check if the redirect has come from approriate router.
  1171. *
  1172. * RFC 4861 specifies that redirects should only be
  1173. * accepted if they come from the nexthop to the target.
  1174. * Due to the way the routes are chosen, this notion
  1175. * is a bit fuzzy and one might need to check all possible
  1176. * routes.
  1177. */
  1178. read_lock_bh(&table->tb6_lock);
  1179. fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  1180. restart:
  1181. for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
  1182. if (rt6_check_expired(rt))
  1183. continue;
  1184. if (rt->dst.error)
  1185. break;
  1186. if (!(rt->rt6i_flags & RTF_GATEWAY))
  1187. continue;
  1188. if (fl6->flowi6_oif != rt->dst.dev->ifindex)
  1189. continue;
  1190. if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
  1191. continue;
  1192. break;
  1193. }
  1194. if (!rt)
  1195. rt = net->ipv6.ip6_null_entry;
  1196. else if (rt->dst.error) {
  1197. rt = net->ipv6.ip6_null_entry;
  1198. goto out;
  1199. }
  1200. if (rt == net->ipv6.ip6_null_entry) {
  1201. fn = fib6_backtrack(fn, &fl6->saddr);
  1202. if (fn)
  1203. goto restart;
  1204. }
  1205. out:
  1206. dst_hold(&rt->dst);
  1207. read_unlock_bh(&table->tb6_lock);
  1208. return rt;
  1209. };
  1210. static struct dst_entry *ip6_route_redirect(struct net *net,
  1211. const struct flowi6 *fl6,
  1212. const struct in6_addr *gateway)
  1213. {
  1214. int flags = RT6_LOOKUP_F_HAS_SADDR;
  1215. struct ip6rd_flowi rdfl;
  1216. rdfl.fl6 = *fl6;
  1217. rdfl.gateway = *gateway;
  1218. return fib6_rule_lookup(net, &rdfl.fl6,
  1219. flags, __ip6_route_redirect);
  1220. }
  1221. void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
  1222. {
  1223. const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
  1224. struct dst_entry *dst;
  1225. struct flowi6 fl6;
  1226. memset(&fl6, 0, sizeof(fl6));
  1227. fl6.flowi6_iif = LOOPBACK_IFINDEX;
  1228. fl6.flowi6_oif = oif;
  1229. fl6.flowi6_mark = mark;
  1230. fl6.daddr = iph->daddr;
  1231. fl6.saddr = iph->saddr;
  1232. fl6.flowlabel = ip6_flowinfo(iph);
  1233. dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
  1234. rt6_do_redirect(dst, NULL, skb);
  1235. dst_release(dst);
  1236. }
  1237. EXPORT_SYMBOL_GPL(ip6_redirect);
  1238. void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
  1239. u32 mark)
  1240. {
  1241. const struct ipv6hdr *iph = ipv6_hdr(skb);
  1242. const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
  1243. struct dst_entry *dst;
  1244. struct flowi6 fl6;
  1245. memset(&fl6, 0, sizeof(fl6));
  1246. fl6.flowi6_iif = LOOPBACK_IFINDEX;
  1247. fl6.flowi6_oif = oif;
  1248. fl6.flowi6_mark = mark;
  1249. fl6.daddr = msg->dest;
  1250. fl6.saddr = iph->daddr;
  1251. dst = ip6_route_redirect(net, &fl6, &iph->saddr);
  1252. rt6_do_redirect(dst, NULL, skb);
  1253. dst_release(dst);
  1254. }
  1255. void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
  1256. {
  1257. ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
  1258. }
  1259. EXPORT_SYMBOL_GPL(ip6_sk_redirect);
  1260. static unsigned int ip6_default_advmss(const struct dst_entry *dst)
  1261. {
  1262. struct net_device *dev = dst->dev;
  1263. unsigned int mtu = dst_mtu(dst);
  1264. struct net *net = dev_net(dev);
  1265. mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
  1266. if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
  1267. mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
  1268. /*
  1269. * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
  1270. * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
  1271. * IPV6_MAXPLEN is also valid and means: "any MSS,
  1272. * rely only on pmtu discovery"
  1273. */
  1274. if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
  1275. mtu = IPV6_MAXPLEN;
  1276. return mtu;
  1277. }
  1278. static unsigned int ip6_mtu(const struct dst_entry *dst)
  1279. {
  1280. const struct rt6_info *rt = (const struct rt6_info *)dst;
  1281. unsigned int mtu = rt->rt6i_pmtu;
  1282. struct inet6_dev *idev;
  1283. if (mtu)
  1284. goto out;
  1285. mtu = dst_metric_raw(dst, RTAX_MTU);
  1286. if (mtu)
  1287. goto out;
  1288. mtu = IPV6_MIN_MTU;
  1289. rcu_read_lock();
  1290. idev = __in6_dev_get(dst->dev);
  1291. if (idev)
  1292. mtu = idev->cnf.mtu6;
  1293. rcu_read_unlock();
  1294. out:
  1295. return min_t(unsigned int, mtu, IP6_MAX_MTU);
  1296. }
  1297. static struct dst_entry *icmp6_dst_gc_list;
  1298. static DEFINE_SPINLOCK(icmp6_dst_lock);
  1299. struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
  1300. struct flowi6 *fl6)
  1301. {
  1302. struct dst_entry *dst;
  1303. struct rt6_info *rt;
  1304. struct inet6_dev *idev = in6_dev_get(dev);
  1305. struct net *net = dev_net(dev);
  1306. if (unlikely(!idev))
  1307. return ERR_PTR(-ENODEV);
  1308. rt = ip6_dst_alloc(net, dev, 0, NULL);
  1309. if (unlikely(!rt)) {
  1310. in6_dev_put(idev);
  1311. dst = ERR_PTR(-ENOMEM);
  1312. goto out;
  1313. }
  1314. rt->dst.flags |= DST_HOST;
  1315. rt->dst.output = ip6_output;
  1316. atomic_set(&rt->dst.__refcnt, 1);
  1317. rt->rt6i_gateway = fl6->daddr;
  1318. rt->rt6i_dst.addr = fl6->daddr;
  1319. rt->rt6i_dst.plen = 128;
  1320. rt->rt6i_idev = idev;
  1321. dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
  1322. spin_lock_bh(&icmp6_dst_lock);
  1323. rt->dst.next = icmp6_dst_gc_list;
  1324. icmp6_dst_gc_list = &rt->dst;
  1325. spin_unlock_bh(&icmp6_dst_lock);
  1326. fib6_force_start_gc(net);
  1327. dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
  1328. out:
  1329. return dst;
  1330. }
  1331. int icmp6_dst_gc(void)
  1332. {
  1333. struct dst_entry *dst, **pprev;
  1334. int more = 0;
  1335. spin_lock_bh(&icmp6_dst_lock);
  1336. pprev = &icmp6_dst_gc_list;
  1337. while ((dst = *pprev) != NULL) {
  1338. if (!atomic_read(&dst->__refcnt)) {
  1339. *pprev = dst->next;
  1340. dst_free(dst);
  1341. } else {
  1342. pprev = &dst->next;
  1343. ++more;
  1344. }
  1345. }
  1346. spin_unlock_bh(&icmp6_dst_lock);
  1347. return more;
  1348. }
  1349. static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
  1350. void *arg)
  1351. {
  1352. struct dst_entry *dst, **pprev;
  1353. spin_lock_bh(&icmp6_dst_lock);
  1354. pprev = &icmp6_dst_gc_list;
  1355. while ((dst = *pprev) != NULL) {
  1356. struct rt6_info *rt = (struct rt6_info *) dst;
  1357. if (func(rt, arg)) {
  1358. *pprev = dst->next;
  1359. dst_free(dst);
  1360. } else {
  1361. pprev = &dst->next;
  1362. }
  1363. }
  1364. spin_unlock_bh(&icmp6_dst_lock);
  1365. }
  1366. static int ip6_dst_gc(struct dst_ops *ops)
  1367. {
  1368. struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
  1369. int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
  1370. int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
  1371. int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
  1372. int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
  1373. unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
  1374. int entries;
  1375. entries = dst_entries_get_fast(ops);
  1376. if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
  1377. entries <= rt_max_size)
  1378. goto out;
  1379. net->ipv6.ip6_rt_gc_expire++;
  1380. fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
  1381. entries = dst_entries_get_slow(ops);
  1382. if (entries < ops->gc_thresh)
  1383. net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
  1384. out:
  1385. net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
  1386. return entries > rt_max_size;
  1387. }
  1388. static int ip6_convert_metrics(struct mx6_config *mxc,
  1389. const struct fib6_config *cfg)
  1390. {
  1391. struct nlattr *nla;
  1392. int remaining;
  1393. u32 *mp;
  1394. if (!cfg->fc_mx)
  1395. return 0;
  1396. mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
  1397. if (unlikely(!mp))
  1398. return -ENOMEM;
  1399. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  1400. int type = nla_type(nla);
  1401. if (type) {
  1402. u32 val;
  1403. if (unlikely(type > RTAX_MAX))
  1404. goto err;
  1405. if (type == RTAX_CC_ALGO) {
  1406. char tmp[TCP_CA_NAME_MAX];
  1407. nla_strlcpy(tmp, nla, sizeof(tmp));
  1408. val = tcp_ca_get_key_by_name(tmp);
  1409. if (val == TCP_CA_UNSPEC)
  1410. goto err;
  1411. } else {
  1412. val = nla_get_u32(nla);
  1413. }
  1414. mp[type - 1] = val;
  1415. __set_bit(type - 1, mxc->mx_valid);
  1416. }
  1417. }
  1418. mxc->mx = mp;
  1419. return 0;
  1420. err:
  1421. kfree(mp);
  1422. return -EINVAL;
  1423. }
  1424. int ip6_route_add(struct fib6_config *cfg)
  1425. {
  1426. int err;
  1427. struct net *net = cfg->fc_nlinfo.nl_net;
  1428. struct rt6_info *rt = NULL;
  1429. struct net_device *dev = NULL;
  1430. struct inet6_dev *idev = NULL;
  1431. struct fib6_table *table;
  1432. struct mx6_config mxc = { .mx = NULL, };
  1433. int addr_type;
  1434. if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
  1435. return -EINVAL;
  1436. #ifndef CONFIG_IPV6_SUBTREES
  1437. if (cfg->fc_src_len)
  1438. return -EINVAL;
  1439. #endif
  1440. if (cfg->fc_ifindex) {
  1441. err = -ENODEV;
  1442. dev = dev_get_by_index(net, cfg->fc_ifindex);
  1443. if (!dev)
  1444. goto out;
  1445. idev = in6_dev_get(dev);
  1446. if (!idev)
  1447. goto out;
  1448. }
  1449. if (cfg->fc_metric == 0)
  1450. cfg->fc_metric = IP6_RT_PRIO_USER;
  1451. err = -ENOBUFS;
  1452. if (cfg->fc_nlinfo.nlh &&
  1453. !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
  1454. table = fib6_get_table(net, cfg->fc_table);
  1455. if (!table) {
  1456. pr_warn("NLM_F_CREATE should be specified when creating new route\n");
  1457. table = fib6_new_table(net, cfg->fc_table);
  1458. }
  1459. } else {
  1460. table = fib6_new_table(net, cfg->fc_table);
  1461. }
  1462. if (!table)
  1463. goto out;
  1464. rt = ip6_dst_alloc(net, NULL, (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT, table);
  1465. if (!rt) {
  1466. err = -ENOMEM;
  1467. goto out;
  1468. }
  1469. if (cfg->fc_flags & RTF_EXPIRES)
  1470. rt6_set_expires(rt, jiffies +
  1471. clock_t_to_jiffies(cfg->fc_expires));
  1472. else
  1473. rt6_clean_expires(rt);
  1474. if (cfg->fc_protocol == RTPROT_UNSPEC)
  1475. cfg->fc_protocol = RTPROT_BOOT;
  1476. rt->rt6i_protocol = cfg->fc_protocol;
  1477. addr_type = ipv6_addr_type(&cfg->fc_dst);
  1478. if (addr_type & IPV6_ADDR_MULTICAST)
  1479. rt->dst.input = ip6_mc_input;
  1480. else if (cfg->fc_flags & RTF_LOCAL)
  1481. rt->dst.input = ip6_input;
  1482. else
  1483. rt->dst.input = ip6_forward;
  1484. rt->dst.output = ip6_output;
  1485. ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
  1486. rt->rt6i_dst.plen = cfg->fc_dst_len;
  1487. if (rt->rt6i_dst.plen == 128)
  1488. rt->dst.flags |= DST_HOST;
  1489. #ifdef CONFIG_IPV6_SUBTREES
  1490. ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
  1491. rt->rt6i_src.plen = cfg->fc_src_len;
  1492. #endif
  1493. rt->rt6i_metric = cfg->fc_metric;
  1494. /* We cannot add true routes via loopback here,
  1495. they would result in kernel looping; promote them to reject routes
  1496. */
  1497. if ((cfg->fc_flags & RTF_REJECT) ||
  1498. (dev && (dev->flags & IFF_LOOPBACK) &&
  1499. !(addr_type & IPV6_ADDR_LOOPBACK) &&
  1500. !(cfg->fc_flags & RTF_LOCAL))) {
  1501. /* hold loopback dev/idev if we haven't done so. */
  1502. if (dev != net->loopback_dev) {
  1503. if (dev) {
  1504. dev_put(dev);
  1505. in6_dev_put(idev);
  1506. }
  1507. dev = net->loopback_dev;
  1508. dev_hold(dev);
  1509. idev = in6_dev_get(dev);
  1510. if (!idev) {
  1511. err = -ENODEV;
  1512. goto out;
  1513. }
  1514. }
  1515. rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
  1516. switch (cfg->fc_type) {
  1517. case RTN_BLACKHOLE:
  1518. rt->dst.error = -EINVAL;
  1519. rt->dst.output = dst_discard_sk;
  1520. rt->dst.input = dst_discard;
  1521. break;
  1522. case RTN_PROHIBIT:
  1523. rt->dst.error = -EACCES;
  1524. rt->dst.output = ip6_pkt_prohibit_out;
  1525. rt->dst.input = ip6_pkt_prohibit;
  1526. break;
  1527. case RTN_THROW:
  1528. default:
  1529. rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
  1530. : -ENETUNREACH;
  1531. rt->dst.output = ip6_pkt_discard_out;
  1532. rt->dst.input = ip6_pkt_discard;
  1533. break;
  1534. }
  1535. goto install_route;
  1536. }
  1537. if (cfg->fc_flags & RTF_GATEWAY) {
  1538. const struct in6_addr *gw_addr;
  1539. int gwa_type;
  1540. gw_addr = &cfg->fc_gateway;
  1541. /* if gw_addr is local we will fail to detect this in case
  1542. * address is still TENTATIVE (DAD in progress). rt6_lookup()
  1543. * will return already-added prefix route via interface that
  1544. * prefix route was assigned to, which might be non-loopback.
  1545. */
  1546. err = -EINVAL;
  1547. if (ipv6_chk_addr_and_flags(net, gw_addr, NULL, 0, 0))
  1548. goto out;
  1549. rt->rt6i_gateway = *gw_addr;
  1550. gwa_type = ipv6_addr_type(gw_addr);
  1551. if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
  1552. struct rt6_info *grt;
  1553. /* IPv6 strictly inhibits using not link-local
  1554. addresses as nexthop address.
  1555. Otherwise, router will not able to send redirects.
  1556. It is very good, but in some (rare!) circumstances
  1557. (SIT, PtP, NBMA NOARP links) it is handy to allow
  1558. some exceptions. --ANK
  1559. */
  1560. if (!(gwa_type & IPV6_ADDR_UNICAST))
  1561. goto out;
  1562. grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
  1563. err = -EHOSTUNREACH;
  1564. if (!grt)
  1565. goto out;
  1566. if (dev) {
  1567. if (dev != grt->dst.dev) {
  1568. ip6_rt_put(grt);
  1569. goto out;
  1570. }
  1571. } else {
  1572. dev = grt->dst.dev;
  1573. idev = grt->rt6i_idev;
  1574. dev_hold(dev);
  1575. in6_dev_hold(grt->rt6i_idev);
  1576. }
  1577. if (!(grt->rt6i_flags & RTF_GATEWAY))
  1578. err = 0;
  1579. ip6_rt_put(grt);
  1580. if (err)
  1581. goto out;
  1582. }
  1583. err = -EINVAL;
  1584. if (!dev || (dev->flags & IFF_LOOPBACK))
  1585. goto out;
  1586. }
  1587. err = -ENODEV;
  1588. if (!dev)
  1589. goto out;
  1590. if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
  1591. if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
  1592. err = -EINVAL;
  1593. goto out;
  1594. }
  1595. rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
  1596. rt->rt6i_prefsrc.plen = 128;
  1597. } else
  1598. rt->rt6i_prefsrc.plen = 0;
  1599. rt->rt6i_flags = cfg->fc_flags;
  1600. install_route:
  1601. rt->dst.dev = dev;
  1602. rt->rt6i_idev = idev;
  1603. rt->rt6i_table = table;
  1604. cfg->fc_nlinfo.nl_net = dev_net(dev);
  1605. err = ip6_convert_metrics(&mxc, cfg);
  1606. if (err)
  1607. goto out;
  1608. err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc);
  1609. kfree(mxc.mx);
  1610. return err;
  1611. out:
  1612. if (dev)
  1613. dev_put(dev);
  1614. if (idev)
  1615. in6_dev_put(idev);
  1616. if (rt)
  1617. dst_free(&rt->dst);
  1618. return err;
  1619. }
  1620. static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
  1621. {
  1622. int err;
  1623. struct fib6_table *table;
  1624. struct net *net = dev_net(rt->dst.dev);
  1625. if (rt == net->ipv6.ip6_null_entry) {
  1626. err = -ENOENT;
  1627. goto out;
  1628. }
  1629. table = rt->rt6i_table;
  1630. write_lock_bh(&table->tb6_lock);
  1631. err = fib6_del(rt, info);
  1632. write_unlock_bh(&table->tb6_lock);
  1633. out:
  1634. ip6_rt_put(rt);
  1635. return err;
  1636. }
  1637. int ip6_del_rt(struct rt6_info *rt)
  1638. {
  1639. struct nl_info info = {
  1640. .nl_net = dev_net(rt->dst.dev),
  1641. };
  1642. return __ip6_del_rt(rt, &info);
  1643. }
  1644. static int ip6_route_del(struct fib6_config *cfg)
  1645. {
  1646. struct fib6_table *table;
  1647. struct fib6_node *fn;
  1648. struct rt6_info *rt;
  1649. int err = -ESRCH;
  1650. table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
  1651. if (!table)
  1652. return err;
  1653. read_lock_bh(&table->tb6_lock);
  1654. fn = fib6_locate(&table->tb6_root,
  1655. &cfg->fc_dst, cfg->fc_dst_len,
  1656. &cfg->fc_src, cfg->fc_src_len);
  1657. if (fn) {
  1658. for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
  1659. if ((rt->rt6i_flags & RTF_CACHE) &&
  1660. !(cfg->fc_flags & RTF_CACHE))
  1661. continue;
  1662. if (cfg->fc_ifindex &&
  1663. (!rt->dst.dev ||
  1664. rt->dst.dev->ifindex != cfg->fc_ifindex))
  1665. continue;
  1666. if (cfg->fc_flags & RTF_GATEWAY &&
  1667. !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
  1668. continue;
  1669. if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
  1670. continue;
  1671. dst_hold(&rt->dst);
  1672. read_unlock_bh(&table->tb6_lock);
  1673. return __ip6_del_rt(rt, &cfg->fc_nlinfo);
  1674. }
  1675. }
  1676. read_unlock_bh(&table->tb6_lock);
  1677. return err;
  1678. }
  1679. static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
  1680. {
  1681. struct net *net = dev_net(skb->dev);
  1682. struct netevent_redirect netevent;
  1683. struct rt6_info *rt, *nrt = NULL;
  1684. struct ndisc_options ndopts;
  1685. struct inet6_dev *in6_dev;
  1686. struct neighbour *neigh;
  1687. struct rd_msg *msg;
  1688. int optlen, on_link;
  1689. u8 *lladdr;
  1690. optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
  1691. optlen -= sizeof(*msg);
  1692. if (optlen < 0) {
  1693. net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
  1694. return;
  1695. }
  1696. msg = (struct rd_msg *)icmp6_hdr(skb);
  1697. if (ipv6_addr_is_multicast(&msg->dest)) {
  1698. net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
  1699. return;
  1700. }
  1701. on_link = 0;
  1702. if (ipv6_addr_equal(&msg->dest, &msg->target)) {
  1703. on_link = 1;
  1704. } else if (ipv6_addr_type(&msg->target) !=
  1705. (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
  1706. net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
  1707. return;
  1708. }
  1709. in6_dev = __in6_dev_get(skb->dev);
  1710. if (!in6_dev)
  1711. return;
  1712. if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
  1713. return;
  1714. /* RFC2461 8.1:
  1715. * The IP source address of the Redirect MUST be the same as the current
  1716. * first-hop router for the specified ICMP Destination Address.
  1717. */
  1718. if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
  1719. net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
  1720. return;
  1721. }
  1722. lladdr = NULL;
  1723. if (ndopts.nd_opts_tgt_lladdr) {
  1724. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
  1725. skb->dev);
  1726. if (!lladdr) {
  1727. net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
  1728. return;
  1729. }
  1730. }
  1731. rt = (struct rt6_info *) dst;
  1732. if (rt == net->ipv6.ip6_null_entry) {
  1733. net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
  1734. return;
  1735. }
  1736. /* Redirect received -> path was valid.
  1737. * Look, redirects are sent only in response to data packets,
  1738. * so that this nexthop apparently is reachable. --ANK
  1739. */
  1740. dst_confirm(&rt->dst);
  1741. neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
  1742. if (!neigh)
  1743. return;
  1744. /*
  1745. * We have finally decided to accept it.
  1746. */
  1747. neigh_update(neigh, lladdr, NUD_STALE,
  1748. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  1749. NEIGH_UPDATE_F_OVERRIDE|
  1750. (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  1751. NEIGH_UPDATE_F_ISROUTER))
  1752. );
  1753. nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
  1754. if (!nrt)
  1755. goto out;
  1756. nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
  1757. if (on_link)
  1758. nrt->rt6i_flags &= ~RTF_GATEWAY;
  1759. nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
  1760. if (ip6_ins_rt(nrt))
  1761. goto out;
  1762. netevent.old = &rt->dst;
  1763. netevent.new = &nrt->dst;
  1764. netevent.daddr = &msg->dest;
  1765. netevent.neigh = neigh;
  1766. call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
  1767. if (rt->rt6i_flags & RTF_CACHE) {
  1768. rt = (struct rt6_info *) dst_clone(&rt->dst);
  1769. ip6_del_rt(rt);
  1770. }
  1771. out:
  1772. neigh_release(neigh);
  1773. }
  1774. /*
  1775. * Misc support functions
  1776. */
  1777. static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
  1778. {
  1779. BUG_ON(from->dst.from);
  1780. rt->rt6i_flags &= ~RTF_EXPIRES;
  1781. dst_hold(&from->dst);
  1782. rt->dst.from = &from->dst;
  1783. dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
  1784. }
  1785. static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
  1786. {
  1787. rt->dst.input = ort->dst.input;
  1788. rt->dst.output = ort->dst.output;
  1789. rt->rt6i_dst = ort->rt6i_dst;
  1790. rt->dst.error = ort->dst.error;
  1791. rt->rt6i_idev = ort->rt6i_idev;
  1792. if (rt->rt6i_idev)
  1793. in6_dev_hold(rt->rt6i_idev);
  1794. rt->dst.lastuse = jiffies;
  1795. rt->rt6i_gateway = ort->rt6i_gateway;
  1796. rt->rt6i_flags = ort->rt6i_flags;
  1797. rt6_set_from(rt, ort);
  1798. rt->rt6i_metric = ort->rt6i_metric;
  1799. #ifdef CONFIG_IPV6_SUBTREES
  1800. rt->rt6i_src = ort->rt6i_src;
  1801. #endif
  1802. rt->rt6i_prefsrc = ort->rt6i_prefsrc;
  1803. rt->rt6i_table = ort->rt6i_table;
  1804. }
  1805. #ifdef CONFIG_IPV6_ROUTE_INFO
  1806. static struct rt6_info *rt6_get_route_info(struct net *net,
  1807. const struct in6_addr *prefix, int prefixlen,
  1808. const struct in6_addr *gwaddr, int ifindex)
  1809. {
  1810. struct fib6_node *fn;
  1811. struct rt6_info *rt = NULL;
  1812. struct fib6_table *table;
  1813. table = fib6_get_table(net, RT6_TABLE_INFO);
  1814. if (!table)
  1815. return NULL;
  1816. read_lock_bh(&table->tb6_lock);
  1817. fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0);
  1818. if (!fn)
  1819. goto out;
  1820. for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
  1821. if (rt->dst.dev->ifindex != ifindex)
  1822. continue;
  1823. if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
  1824. continue;
  1825. if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
  1826. continue;
  1827. dst_hold(&rt->dst);
  1828. break;
  1829. }
  1830. out:
  1831. read_unlock_bh(&table->tb6_lock);
  1832. return rt;
  1833. }
  1834. static struct rt6_info *rt6_add_route_info(struct net *net,
  1835. const struct in6_addr *prefix, int prefixlen,
  1836. const struct in6_addr *gwaddr, int ifindex,
  1837. unsigned int pref)
  1838. {
  1839. struct fib6_config cfg = {
  1840. .fc_table = RT6_TABLE_INFO,
  1841. .fc_metric = IP6_RT_PRIO_USER,
  1842. .fc_ifindex = ifindex,
  1843. .fc_dst_len = prefixlen,
  1844. .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
  1845. RTF_UP | RTF_PREF(pref),
  1846. .fc_nlinfo.portid = 0,
  1847. .fc_nlinfo.nlh = NULL,
  1848. .fc_nlinfo.nl_net = net,
  1849. };
  1850. cfg.fc_dst = *prefix;
  1851. cfg.fc_gateway = *gwaddr;
  1852. /* We should treat it as a default route if prefix length is 0. */
  1853. if (!prefixlen)
  1854. cfg.fc_flags |= RTF_DEFAULT;
  1855. ip6_route_add(&cfg);
  1856. return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
  1857. }
  1858. #endif
  1859. struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
  1860. {
  1861. struct rt6_info *rt;
  1862. struct fib6_table *table;
  1863. table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
  1864. if (!table)
  1865. return NULL;
  1866. read_lock_bh(&table->tb6_lock);
  1867. for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
  1868. if (dev == rt->dst.dev &&
  1869. ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
  1870. ipv6_addr_equal(&rt->rt6i_gateway, addr))
  1871. break;
  1872. }
  1873. if (rt)
  1874. dst_hold(&rt->dst);
  1875. read_unlock_bh(&table->tb6_lock);
  1876. return rt;
  1877. }
  1878. struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
  1879. struct net_device *dev,
  1880. unsigned int pref)
  1881. {
  1882. struct fib6_config cfg = {
  1883. .fc_table = RT6_TABLE_DFLT,
  1884. .fc_metric = IP6_RT_PRIO_USER,
  1885. .fc_ifindex = dev->ifindex,
  1886. .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
  1887. RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
  1888. .fc_nlinfo.portid = 0,
  1889. .fc_nlinfo.nlh = NULL,
  1890. .fc_nlinfo.nl_net = dev_net(dev),
  1891. };
  1892. cfg.fc_gateway = *gwaddr;
  1893. ip6_route_add(&cfg);
  1894. return rt6_get_dflt_router(gwaddr, dev);
  1895. }
  1896. void rt6_purge_dflt_routers(struct net *net)
  1897. {
  1898. struct rt6_info *rt;
  1899. struct fib6_table *table;
  1900. /* NOTE: Keep consistent with rt6_get_dflt_router */
  1901. table = fib6_get_table(net, RT6_TABLE_DFLT);
  1902. if (!table)
  1903. return;
  1904. restart:
  1905. read_lock_bh(&table->tb6_lock);
  1906. for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
  1907. if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
  1908. (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
  1909. dst_hold(&rt->dst);
  1910. read_unlock_bh(&table->tb6_lock);
  1911. ip6_del_rt(rt);
  1912. goto restart;
  1913. }
  1914. }
  1915. read_unlock_bh(&table->tb6_lock);
  1916. }
  1917. static void rtmsg_to_fib6_config(struct net *net,
  1918. struct in6_rtmsg *rtmsg,
  1919. struct fib6_config *cfg)
  1920. {
  1921. memset(cfg, 0, sizeof(*cfg));
  1922. cfg->fc_table = RT6_TABLE_MAIN;
  1923. cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
  1924. cfg->fc_metric = rtmsg->rtmsg_metric;
  1925. cfg->fc_expires = rtmsg->rtmsg_info;
  1926. cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
  1927. cfg->fc_src_len = rtmsg->rtmsg_src_len;
  1928. cfg->fc_flags = rtmsg->rtmsg_flags;
  1929. cfg->fc_nlinfo.nl_net = net;
  1930. cfg->fc_dst = rtmsg->rtmsg_dst;
  1931. cfg->fc_src = rtmsg->rtmsg_src;
  1932. cfg->fc_gateway = rtmsg->rtmsg_gateway;
  1933. }
  1934. int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  1935. {
  1936. struct fib6_config cfg;
  1937. struct in6_rtmsg rtmsg;
  1938. int err;
  1939. switch (cmd) {
  1940. case SIOCADDRT: /* Add a route */
  1941. case SIOCDELRT: /* Delete a route */
  1942. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1943. return -EPERM;
  1944. err = copy_from_user(&rtmsg, arg,
  1945. sizeof(struct in6_rtmsg));
  1946. if (err)
  1947. return -EFAULT;
  1948. rtmsg_to_fib6_config(net, &rtmsg, &cfg);
  1949. rtnl_lock();
  1950. switch (cmd) {
  1951. case SIOCADDRT:
  1952. err = ip6_route_add(&cfg);
  1953. break;
  1954. case SIOCDELRT:
  1955. err = ip6_route_del(&cfg);
  1956. break;
  1957. default:
  1958. err = -EINVAL;
  1959. }
  1960. rtnl_unlock();
  1961. return err;
  1962. }
  1963. return -EINVAL;
  1964. }
  1965. /*
  1966. * Drop the packet on the floor
  1967. */
  1968. static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
  1969. {
  1970. int type;
  1971. struct dst_entry *dst = skb_dst(skb);
  1972. switch (ipstats_mib_noroutes) {
  1973. case IPSTATS_MIB_INNOROUTES:
  1974. type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
  1975. if (type == IPV6_ADDR_ANY) {
  1976. IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
  1977. IPSTATS_MIB_INADDRERRORS);
  1978. break;
  1979. }
  1980. /* FALLTHROUGH */
  1981. case IPSTATS_MIB_OUTNOROUTES:
  1982. IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
  1983. ipstats_mib_noroutes);
  1984. break;
  1985. }
  1986. icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
  1987. kfree_skb(skb);
  1988. return 0;
  1989. }
  1990. static int ip6_pkt_discard(struct sk_buff *skb)
  1991. {
  1992. return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
  1993. }
  1994. static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb)
  1995. {
  1996. skb->dev = skb_dst(skb)->dev;
  1997. return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
  1998. }
  1999. static int ip6_pkt_prohibit(struct sk_buff *skb)
  2000. {
  2001. return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
  2002. }
  2003. static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb)
  2004. {
  2005. skb->dev = skb_dst(skb)->dev;
  2006. return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
  2007. }
  2008. /*
  2009. * Allocate a dst for local (unicast / anycast) address.
  2010. */
  2011. struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
  2012. const struct in6_addr *addr,
  2013. bool anycast)
  2014. {
  2015. struct net *net = dev_net(idev->dev);
  2016. struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
  2017. DST_NOCOUNT, NULL);
  2018. if (!rt)
  2019. return ERR_PTR(-ENOMEM);
  2020. in6_dev_hold(idev);
  2021. rt->dst.flags |= DST_HOST;
  2022. rt->dst.input = ip6_input;
  2023. rt->dst.output = ip6_output;
  2024. rt->rt6i_idev = idev;
  2025. rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
  2026. if (anycast)
  2027. rt->rt6i_flags |= RTF_ANYCAST;
  2028. else
  2029. rt->rt6i_flags |= RTF_LOCAL;
  2030. rt->rt6i_gateway = *addr;
  2031. rt->rt6i_dst.addr = *addr;
  2032. rt->rt6i_dst.plen = 128;
  2033. rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
  2034. atomic_set(&rt->dst.__refcnt, 1);
  2035. return rt;
  2036. }
  2037. int ip6_route_get_saddr(struct net *net,
  2038. struct rt6_info *rt,
  2039. const struct in6_addr *daddr,
  2040. unsigned int prefs,
  2041. struct in6_addr *saddr)
  2042. {
  2043. struct inet6_dev *idev =
  2044. rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
  2045. int err = 0;
  2046. if (rt && rt->rt6i_prefsrc.plen)
  2047. *saddr = rt->rt6i_prefsrc.addr;
  2048. else
  2049. err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
  2050. daddr, prefs, saddr);
  2051. return err;
  2052. }
  2053. /* remove deleted ip from prefsrc entries */
  2054. struct arg_dev_net_ip {
  2055. struct net_device *dev;
  2056. struct net *net;
  2057. struct in6_addr *addr;
  2058. };
  2059. static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
  2060. {
  2061. struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
  2062. struct net *net = ((struct arg_dev_net_ip *)arg)->net;
  2063. struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
  2064. if (((void *)rt->dst.dev == dev || !dev) &&
  2065. rt != net->ipv6.ip6_null_entry &&
  2066. ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
  2067. /* remove prefsrc entry */
  2068. rt->rt6i_prefsrc.plen = 0;
  2069. }
  2070. return 0;
  2071. }
  2072. void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
  2073. {
  2074. struct net *net = dev_net(ifp->idev->dev);
  2075. struct arg_dev_net_ip adni = {
  2076. .dev = ifp->idev->dev,
  2077. .net = net,
  2078. .addr = &ifp->addr,
  2079. };
  2080. fib6_clean_all(net, fib6_remove_prefsrc, &adni);
  2081. }
  2082. #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
  2083. #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
  2084. /* Remove routers and update dst entries when gateway turn into host. */
  2085. static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
  2086. {
  2087. struct in6_addr *gateway = (struct in6_addr *)arg;
  2088. if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) ||
  2089. ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) &&
  2090. ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
  2091. return -1;
  2092. }
  2093. return 0;
  2094. }
  2095. void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
  2096. {
  2097. fib6_clean_all(net, fib6_clean_tohost, gateway);
  2098. }
  2099. struct arg_dev_net {
  2100. struct net_device *dev;
  2101. struct net *net;
  2102. };
  2103. static int fib6_ifdown(struct rt6_info *rt, void *arg)
  2104. {
  2105. const struct arg_dev_net *adn = arg;
  2106. const struct net_device *dev = adn->dev;
  2107. if ((rt->dst.dev == dev || !dev) &&
  2108. rt != adn->net->ipv6.ip6_null_entry)
  2109. return -1;
  2110. return 0;
  2111. }
  2112. void rt6_ifdown(struct net *net, struct net_device *dev)
  2113. {
  2114. struct arg_dev_net adn = {
  2115. .dev = dev,
  2116. .net = net,
  2117. };
  2118. fib6_clean_all(net, fib6_ifdown, &adn);
  2119. icmp6_clean_all(fib6_ifdown, &adn);
  2120. rt6_uncached_list_flush_dev(net, dev);
  2121. }
  2122. struct rt6_mtu_change_arg {
  2123. struct net_device *dev;
  2124. unsigned int mtu;
  2125. };
  2126. static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
  2127. {
  2128. struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
  2129. struct inet6_dev *idev;
  2130. /* In IPv6 pmtu discovery is not optional,
  2131. so that RTAX_MTU lock cannot disable it.
  2132. We still use this lock to block changes
  2133. caused by addrconf/ndisc.
  2134. */
  2135. idev = __in6_dev_get(arg->dev);
  2136. if (!idev)
  2137. return 0;
  2138. /* For administrative MTU increase, there is no way to discover
  2139. IPv6 PMTU increase, so PMTU increase should be updated here.
  2140. Since RFC 1981 doesn't include administrative MTU increase
  2141. update PMTU increase is a MUST. (i.e. jumbo frame)
  2142. */
  2143. /*
  2144. If new MTU is less than route PMTU, this new MTU will be the
  2145. lowest MTU in the path, update the route PMTU to reflect PMTU
  2146. decreases; if new MTU is greater than route PMTU, and the
  2147. old MTU is the lowest MTU in the path, update the route PMTU
  2148. to reflect the increase. In this case if the other nodes' MTU
  2149. also have the lowest MTU, TOO BIG MESSAGE will be lead to
  2150. PMTU discouvery.
  2151. */
  2152. if (rt->dst.dev == arg->dev &&
  2153. !dst_metric_locked(&rt->dst, RTAX_MTU)) {
  2154. if (rt->rt6i_flags & RTF_CACHE) {
  2155. /* For RTF_CACHE with rt6i_pmtu == 0
  2156. * (i.e. a redirected route),
  2157. * the metrics of its rt->dst.from has already
  2158. * been updated.
  2159. */
  2160. if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu)
  2161. rt->rt6i_pmtu = arg->mtu;
  2162. } else if (dst_mtu(&rt->dst) >= arg->mtu ||
  2163. (dst_mtu(&rt->dst) < arg->mtu &&
  2164. dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
  2165. dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
  2166. }
  2167. }
  2168. return 0;
  2169. }
  2170. void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
  2171. {
  2172. struct rt6_mtu_change_arg arg = {
  2173. .dev = dev,
  2174. .mtu = mtu,
  2175. };
  2176. fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
  2177. }
  2178. static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
  2179. [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
  2180. [RTA_OIF] = { .type = NLA_U32 },
  2181. [RTA_IIF] = { .type = NLA_U32 },
  2182. [RTA_PRIORITY] = { .type = NLA_U32 },
  2183. [RTA_METRICS] = { .type = NLA_NESTED },
  2184. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  2185. [RTA_PREF] = { .type = NLA_U8 },
  2186. };
  2187. static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  2188. struct fib6_config *cfg)
  2189. {
  2190. struct rtmsg *rtm;
  2191. struct nlattr *tb[RTA_MAX+1];
  2192. unsigned int pref;
  2193. int err;
  2194. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
  2195. if (err < 0)
  2196. goto errout;
  2197. err = -EINVAL;
  2198. rtm = nlmsg_data(nlh);
  2199. memset(cfg, 0, sizeof(*cfg));
  2200. cfg->fc_table = rtm->rtm_table;
  2201. cfg->fc_dst_len = rtm->rtm_dst_len;
  2202. cfg->fc_src_len = rtm->rtm_src_len;
  2203. cfg->fc_flags = RTF_UP;
  2204. cfg->fc_protocol = rtm->rtm_protocol;
  2205. cfg->fc_type = rtm->rtm_type;
  2206. if (rtm->rtm_type == RTN_UNREACHABLE ||
  2207. rtm->rtm_type == RTN_BLACKHOLE ||
  2208. rtm->rtm_type == RTN_PROHIBIT ||
  2209. rtm->rtm_type == RTN_THROW)
  2210. cfg->fc_flags |= RTF_REJECT;
  2211. if (rtm->rtm_type == RTN_LOCAL)
  2212. cfg->fc_flags |= RTF_LOCAL;
  2213. if (rtm->rtm_flags & RTM_F_CLONED)
  2214. cfg->fc_flags |= RTF_CACHE;
  2215. cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
  2216. cfg->fc_nlinfo.nlh = nlh;
  2217. cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
  2218. if (tb[RTA_GATEWAY]) {
  2219. cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
  2220. cfg->fc_flags |= RTF_GATEWAY;
  2221. }
  2222. if (tb[RTA_DST]) {
  2223. int plen = (rtm->rtm_dst_len + 7) >> 3;
  2224. if (nla_len(tb[RTA_DST]) < plen)
  2225. goto errout;
  2226. nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
  2227. }
  2228. if (tb[RTA_SRC]) {
  2229. int plen = (rtm->rtm_src_len + 7) >> 3;
  2230. if (nla_len(tb[RTA_SRC]) < plen)
  2231. goto errout;
  2232. nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
  2233. }
  2234. if (tb[RTA_PREFSRC])
  2235. cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
  2236. if (tb[RTA_OIF])
  2237. cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
  2238. if (tb[RTA_PRIORITY])
  2239. cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
  2240. if (tb[RTA_METRICS]) {
  2241. cfg->fc_mx = nla_data(tb[RTA_METRICS]);
  2242. cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
  2243. }
  2244. if (tb[RTA_TABLE])
  2245. cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
  2246. if (tb[RTA_MULTIPATH]) {
  2247. cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
  2248. cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
  2249. }
  2250. if (tb[RTA_PREF]) {
  2251. pref = nla_get_u8(tb[RTA_PREF]);
  2252. if (pref != ICMPV6_ROUTER_PREF_LOW &&
  2253. pref != ICMPV6_ROUTER_PREF_HIGH)
  2254. pref = ICMPV6_ROUTER_PREF_MEDIUM;
  2255. cfg->fc_flags |= RTF_PREF(pref);
  2256. }
  2257. err = 0;
  2258. errout:
  2259. return err;
  2260. }
  2261. static int ip6_route_multipath(struct fib6_config *cfg, int add)
  2262. {
  2263. struct fib6_config r_cfg;
  2264. struct rtnexthop *rtnh;
  2265. int remaining;
  2266. int attrlen;
  2267. int err = 0, last_err = 0;
  2268. remaining = cfg->fc_mp_len;
  2269. beginning:
  2270. rtnh = (struct rtnexthop *)cfg->fc_mp;
  2271. /* Parse a Multipath Entry */
  2272. while (rtnh_ok(rtnh, remaining)) {
  2273. memcpy(&r_cfg, cfg, sizeof(*cfg));
  2274. if (rtnh->rtnh_ifindex)
  2275. r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
  2276. attrlen = rtnh_attrlen(rtnh);
  2277. if (attrlen > 0) {
  2278. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  2279. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  2280. if (nla) {
  2281. r_cfg.fc_gateway = nla_get_in6_addr(nla);
  2282. r_cfg.fc_flags |= RTF_GATEWAY;
  2283. }
  2284. }
  2285. err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
  2286. if (err) {
  2287. last_err = err;
  2288. /* If we are trying to remove a route, do not stop the
  2289. * loop when ip6_route_del() fails (because next hop is
  2290. * already gone), we should try to remove all next hops.
  2291. */
  2292. if (add) {
  2293. /* If add fails, we should try to delete all
  2294. * next hops that have been already added.
  2295. */
  2296. add = 0;
  2297. remaining = cfg->fc_mp_len - remaining;
  2298. goto beginning;
  2299. }
  2300. }
  2301. /* Because each route is added like a single route we remove
  2302. * these flags after the first nexthop: if there is a collision,
  2303. * we have already failed to add the first nexthop:
  2304. * fib6_add_rt2node() has rejected it; when replacing, old
  2305. * nexthops have been replaced by first new, the rest should
  2306. * be added to it.
  2307. */
  2308. cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
  2309. NLM_F_REPLACE);
  2310. rtnh = rtnh_next(rtnh, &remaining);
  2311. }
  2312. return last_err;
  2313. }
  2314. static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  2315. {
  2316. struct fib6_config cfg;
  2317. int err;
  2318. err = rtm_to_fib6_config(skb, nlh, &cfg);
  2319. if (err < 0)
  2320. return err;
  2321. if (cfg.fc_mp)
  2322. return ip6_route_multipath(&cfg, 0);
  2323. else
  2324. return ip6_route_del(&cfg);
  2325. }
  2326. static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  2327. {
  2328. struct fib6_config cfg;
  2329. int err;
  2330. err = rtm_to_fib6_config(skb, nlh, &cfg);
  2331. if (err < 0)
  2332. return err;
  2333. if (cfg.fc_mp)
  2334. return ip6_route_multipath(&cfg, 1);
  2335. else
  2336. return ip6_route_add(&cfg);
  2337. }
  2338. static inline size_t rt6_nlmsg_size(void)
  2339. {
  2340. return NLMSG_ALIGN(sizeof(struct rtmsg))
  2341. + nla_total_size(16) /* RTA_SRC */
  2342. + nla_total_size(16) /* RTA_DST */
  2343. + nla_total_size(16) /* RTA_GATEWAY */
  2344. + nla_total_size(16) /* RTA_PREFSRC */
  2345. + nla_total_size(4) /* RTA_TABLE */
  2346. + nla_total_size(4) /* RTA_IIF */
  2347. + nla_total_size(4) /* RTA_OIF */
  2348. + nla_total_size(4) /* RTA_PRIORITY */
  2349. + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
  2350. + nla_total_size(sizeof(struct rta_cacheinfo))
  2351. + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
  2352. + nla_total_size(1); /* RTA_PREF */
  2353. }
  2354. static int rt6_fill_node(struct net *net,
  2355. struct sk_buff *skb, struct rt6_info *rt,
  2356. struct in6_addr *dst, struct in6_addr *src,
  2357. int iif, int type, u32 portid, u32 seq,
  2358. int prefix, int nowait, unsigned int flags)
  2359. {
  2360. u32 metrics[RTAX_MAX];
  2361. struct rtmsg *rtm;
  2362. struct nlmsghdr *nlh;
  2363. long expires;
  2364. u32 table;
  2365. if (prefix) { /* user wants prefix routes only */
  2366. if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
  2367. /* success since this is not a prefix route */
  2368. return 1;
  2369. }
  2370. }
  2371. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
  2372. if (!nlh)
  2373. return -EMSGSIZE;
  2374. rtm = nlmsg_data(nlh);
  2375. rtm->rtm_family = AF_INET6;
  2376. rtm->rtm_dst_len = rt->rt6i_dst.plen;
  2377. rtm->rtm_src_len = rt->rt6i_src.plen;
  2378. rtm->rtm_tos = 0;
  2379. if (rt->rt6i_table)
  2380. table = rt->rt6i_table->tb6_id;
  2381. else
  2382. table = RT6_TABLE_UNSPEC;
  2383. rtm->rtm_table = table;
  2384. if (nla_put_u32(skb, RTA_TABLE, table))
  2385. goto nla_put_failure;
  2386. if (rt->rt6i_flags & RTF_REJECT) {
  2387. switch (rt->dst.error) {
  2388. case -EINVAL:
  2389. rtm->rtm_type = RTN_BLACKHOLE;
  2390. break;
  2391. case -EACCES:
  2392. rtm->rtm_type = RTN_PROHIBIT;
  2393. break;
  2394. case -EAGAIN:
  2395. rtm->rtm_type = RTN_THROW;
  2396. break;
  2397. default:
  2398. rtm->rtm_type = RTN_UNREACHABLE;
  2399. break;
  2400. }
  2401. }
  2402. else if (rt->rt6i_flags & RTF_LOCAL)
  2403. rtm->rtm_type = RTN_LOCAL;
  2404. else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
  2405. rtm->rtm_type = RTN_LOCAL;
  2406. else
  2407. rtm->rtm_type = RTN_UNICAST;
  2408. rtm->rtm_flags = 0;
  2409. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  2410. rtm->rtm_protocol = rt->rt6i_protocol;
  2411. if (rt->rt6i_flags & RTF_DYNAMIC)
  2412. rtm->rtm_protocol = RTPROT_REDIRECT;
  2413. else if (rt->rt6i_flags & RTF_ADDRCONF) {
  2414. if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
  2415. rtm->rtm_protocol = RTPROT_RA;
  2416. else
  2417. rtm->rtm_protocol = RTPROT_KERNEL;
  2418. }
  2419. if (rt->rt6i_flags & RTF_CACHE)
  2420. rtm->rtm_flags |= RTM_F_CLONED;
  2421. if (dst) {
  2422. if (nla_put_in6_addr(skb, RTA_DST, dst))
  2423. goto nla_put_failure;
  2424. rtm->rtm_dst_len = 128;
  2425. } else if (rtm->rtm_dst_len)
  2426. if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
  2427. goto nla_put_failure;
  2428. #ifdef CONFIG_IPV6_SUBTREES
  2429. if (src) {
  2430. if (nla_put_in6_addr(skb, RTA_SRC, src))
  2431. goto nla_put_failure;
  2432. rtm->rtm_src_len = 128;
  2433. } else if (rtm->rtm_src_len &&
  2434. nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
  2435. goto nla_put_failure;
  2436. #endif
  2437. if (iif) {
  2438. #ifdef CONFIG_IPV6_MROUTE
  2439. if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
  2440. int err = ip6mr_get_route(net, skb, rtm, nowait);
  2441. if (err <= 0) {
  2442. if (!nowait) {
  2443. if (err == 0)
  2444. return 0;
  2445. goto nla_put_failure;
  2446. } else {
  2447. if (err == -EMSGSIZE)
  2448. goto nla_put_failure;
  2449. }
  2450. }
  2451. } else
  2452. #endif
  2453. if (nla_put_u32(skb, RTA_IIF, iif))
  2454. goto nla_put_failure;
  2455. } else if (dst) {
  2456. struct in6_addr saddr_buf;
  2457. if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
  2458. nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
  2459. goto nla_put_failure;
  2460. }
  2461. if (rt->rt6i_prefsrc.plen) {
  2462. struct in6_addr saddr_buf;
  2463. saddr_buf = rt->rt6i_prefsrc.addr;
  2464. if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
  2465. goto nla_put_failure;
  2466. }
  2467. memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
  2468. if (rt->rt6i_pmtu)
  2469. metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
  2470. if (rtnetlink_put_metrics(skb, metrics) < 0)
  2471. goto nla_put_failure;
  2472. if (rt->rt6i_flags & RTF_GATEWAY) {
  2473. if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
  2474. goto nla_put_failure;
  2475. }
  2476. if (rt->dst.dev &&
  2477. nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
  2478. goto nla_put_failure;
  2479. if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
  2480. goto nla_put_failure;
  2481. expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
  2482. if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
  2483. goto nla_put_failure;
  2484. if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
  2485. goto nla_put_failure;
  2486. nlmsg_end(skb, nlh);
  2487. return 0;
  2488. nla_put_failure:
  2489. nlmsg_cancel(skb, nlh);
  2490. return -EMSGSIZE;
  2491. }
  2492. int rt6_dump_route(struct rt6_info *rt, void *p_arg)
  2493. {
  2494. struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
  2495. int prefix;
  2496. if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
  2497. struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
  2498. prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
  2499. } else
  2500. prefix = 0;
  2501. return rt6_fill_node(arg->net,
  2502. arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
  2503. NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
  2504. prefix, 0, NLM_F_MULTI);
  2505. }
  2506. static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
  2507. {
  2508. struct net *net = sock_net(in_skb->sk);
  2509. struct nlattr *tb[RTA_MAX+1];
  2510. struct rt6_info *rt;
  2511. struct sk_buff *skb;
  2512. struct rtmsg *rtm;
  2513. struct flowi6 fl6;
  2514. int err, iif = 0, oif = 0;
  2515. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
  2516. if (err < 0)
  2517. goto errout;
  2518. err = -EINVAL;
  2519. memset(&fl6, 0, sizeof(fl6));
  2520. if (tb[RTA_SRC]) {
  2521. if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
  2522. goto errout;
  2523. fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
  2524. }
  2525. if (tb[RTA_DST]) {
  2526. if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
  2527. goto errout;
  2528. fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
  2529. }
  2530. if (tb[RTA_IIF])
  2531. iif = nla_get_u32(tb[RTA_IIF]);
  2532. if (tb[RTA_OIF])
  2533. oif = nla_get_u32(tb[RTA_OIF]);
  2534. if (tb[RTA_MARK])
  2535. fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
  2536. if (iif) {
  2537. struct net_device *dev;
  2538. int flags = 0;
  2539. dev = __dev_get_by_index(net, iif);
  2540. if (!dev) {
  2541. err = -ENODEV;
  2542. goto errout;
  2543. }
  2544. fl6.flowi6_iif = iif;
  2545. if (!ipv6_addr_any(&fl6.saddr))
  2546. flags |= RT6_LOOKUP_F_HAS_SADDR;
  2547. rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
  2548. flags);
  2549. } else {
  2550. fl6.flowi6_oif = oif;
  2551. rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
  2552. }
  2553. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2554. if (!skb) {
  2555. ip6_rt_put(rt);
  2556. err = -ENOBUFS;
  2557. goto errout;
  2558. }
  2559. /* Reserve room for dummy headers, this skb can pass
  2560. through good chunk of routing engine.
  2561. */
  2562. skb_reset_mac_header(skb);
  2563. skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
  2564. skb_dst_set(skb, &rt->dst);
  2565. err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
  2566. RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
  2567. nlh->nlmsg_seq, 0, 0, 0);
  2568. if (err < 0) {
  2569. kfree_skb(skb);
  2570. goto errout;
  2571. }
  2572. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  2573. errout:
  2574. return err;
  2575. }
  2576. void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
  2577. {
  2578. struct sk_buff *skb;
  2579. struct net *net = info->nl_net;
  2580. u32 seq;
  2581. int err;
  2582. err = -ENOBUFS;
  2583. seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  2584. skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
  2585. if (!skb)
  2586. goto errout;
  2587. err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
  2588. event, info->portid, seq, 0, 0, 0);
  2589. if (err < 0) {
  2590. /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
  2591. WARN_ON(err == -EMSGSIZE);
  2592. kfree_skb(skb);
  2593. goto errout;
  2594. }
  2595. rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
  2596. info->nlh, gfp_any());
  2597. return;
  2598. errout:
  2599. if (err < 0)
  2600. rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
  2601. }
  2602. static int ip6_route_dev_notify(struct notifier_block *this,
  2603. unsigned long event, void *ptr)
  2604. {
  2605. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2606. struct net *net = dev_net(dev);
  2607. if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
  2608. net->ipv6.ip6_null_entry->dst.dev = dev;
  2609. net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
  2610. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2611. net->ipv6.ip6_prohibit_entry->dst.dev = dev;
  2612. net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
  2613. net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
  2614. net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
  2615. #endif
  2616. }
  2617. return NOTIFY_OK;
  2618. }
  2619. /*
  2620. * /proc
  2621. */
  2622. #ifdef CONFIG_PROC_FS
  2623. static const struct file_operations ipv6_route_proc_fops = {
  2624. .owner = THIS_MODULE,
  2625. .open = ipv6_route_open,
  2626. .read = seq_read,
  2627. .llseek = seq_lseek,
  2628. .release = seq_release_net,
  2629. };
  2630. static int rt6_stats_seq_show(struct seq_file *seq, void *v)
  2631. {
  2632. struct net *net = (struct net *)seq->private;
  2633. seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
  2634. net->ipv6.rt6_stats->fib_nodes,
  2635. net->ipv6.rt6_stats->fib_route_nodes,
  2636. net->ipv6.rt6_stats->fib_rt_alloc,
  2637. net->ipv6.rt6_stats->fib_rt_entries,
  2638. net->ipv6.rt6_stats->fib_rt_cache,
  2639. dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
  2640. net->ipv6.rt6_stats->fib_discarded_routes);
  2641. return 0;
  2642. }
  2643. static int rt6_stats_seq_open(struct inode *inode, struct file *file)
  2644. {
  2645. return single_open_net(inode, file, rt6_stats_seq_show);
  2646. }
  2647. static const struct file_operations rt6_stats_seq_fops = {
  2648. .owner = THIS_MODULE,
  2649. .open = rt6_stats_seq_open,
  2650. .read = seq_read,
  2651. .llseek = seq_lseek,
  2652. .release = single_release_net,
  2653. };
  2654. #endif /* CONFIG_PROC_FS */
  2655. #ifdef CONFIG_SYSCTL
  2656. static
  2657. int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
  2658. void __user *buffer, size_t *lenp, loff_t *ppos)
  2659. {
  2660. struct net *net;
  2661. int delay;
  2662. if (!write)
  2663. return -EINVAL;
  2664. net = (struct net *)ctl->extra1;
  2665. delay = net->ipv6.sysctl.flush_delay;
  2666. proc_dointvec(ctl, write, buffer, lenp, ppos);
  2667. fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
  2668. return 0;
  2669. }
  2670. struct ctl_table ipv6_route_table_template[] = {
  2671. {
  2672. .procname = "flush",
  2673. .data = &init_net.ipv6.sysctl.flush_delay,
  2674. .maxlen = sizeof(int),
  2675. .mode = 0200,
  2676. .proc_handler = ipv6_sysctl_rtcache_flush
  2677. },
  2678. {
  2679. .procname = "gc_thresh",
  2680. .data = &ip6_dst_ops_template.gc_thresh,
  2681. .maxlen = sizeof(int),
  2682. .mode = 0644,
  2683. .proc_handler = proc_dointvec,
  2684. },
  2685. {
  2686. .procname = "max_size",
  2687. .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
  2688. .maxlen = sizeof(int),
  2689. .mode = 0644,
  2690. .proc_handler = proc_dointvec,
  2691. },
  2692. {
  2693. .procname = "gc_min_interval",
  2694. .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
  2695. .maxlen = sizeof(int),
  2696. .mode = 0644,
  2697. .proc_handler = proc_dointvec_jiffies,
  2698. },
  2699. {
  2700. .procname = "gc_timeout",
  2701. .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
  2702. .maxlen = sizeof(int),
  2703. .mode = 0644,
  2704. .proc_handler = proc_dointvec_jiffies,
  2705. },
  2706. {
  2707. .procname = "gc_interval",
  2708. .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
  2709. .maxlen = sizeof(int),
  2710. .mode = 0644,
  2711. .proc_handler = proc_dointvec_jiffies,
  2712. },
  2713. {
  2714. .procname = "gc_elasticity",
  2715. .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
  2716. .maxlen = sizeof(int),
  2717. .mode = 0644,
  2718. .proc_handler = proc_dointvec,
  2719. },
  2720. {
  2721. .procname = "mtu_expires",
  2722. .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
  2723. .maxlen = sizeof(int),
  2724. .mode = 0644,
  2725. .proc_handler = proc_dointvec_jiffies,
  2726. },
  2727. {
  2728. .procname = "min_adv_mss",
  2729. .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
  2730. .maxlen = sizeof(int),
  2731. .mode = 0644,
  2732. .proc_handler = proc_dointvec,
  2733. },
  2734. {
  2735. .procname = "gc_min_interval_ms",
  2736. .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
  2737. .maxlen = sizeof(int),
  2738. .mode = 0644,
  2739. .proc_handler = proc_dointvec_ms_jiffies,
  2740. },
  2741. { }
  2742. };
  2743. struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
  2744. {
  2745. struct ctl_table *table;
  2746. table = kmemdup(ipv6_route_table_template,
  2747. sizeof(ipv6_route_table_template),
  2748. GFP_KERNEL);
  2749. if (table) {
  2750. table[0].data = &net->ipv6.sysctl.flush_delay;
  2751. table[0].extra1 = net;
  2752. table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
  2753. table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
  2754. table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
  2755. table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
  2756. table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
  2757. table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
  2758. table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
  2759. table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
  2760. table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
  2761. /* Don't export sysctls to unprivileged users */
  2762. if (net->user_ns != &init_user_ns)
  2763. table[0].procname = NULL;
  2764. }
  2765. return table;
  2766. }
  2767. #endif
  2768. static int __net_init ip6_route_net_init(struct net *net)
  2769. {
  2770. int ret = -ENOMEM;
  2771. memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
  2772. sizeof(net->ipv6.ip6_dst_ops));
  2773. if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
  2774. goto out_ip6_dst_ops;
  2775. net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
  2776. sizeof(*net->ipv6.ip6_null_entry),
  2777. GFP_KERNEL);
  2778. if (!net->ipv6.ip6_null_entry)
  2779. goto out_ip6_dst_entries;
  2780. net->ipv6.ip6_null_entry->dst.path =
  2781. (struct dst_entry *)net->ipv6.ip6_null_entry;
  2782. net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  2783. dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
  2784. ip6_template_metrics, true);
  2785. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2786. net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
  2787. sizeof(*net->ipv6.ip6_prohibit_entry),
  2788. GFP_KERNEL);
  2789. if (!net->ipv6.ip6_prohibit_entry)
  2790. goto out_ip6_null_entry;
  2791. net->ipv6.ip6_prohibit_entry->dst.path =
  2792. (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
  2793. net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  2794. dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
  2795. ip6_template_metrics, true);
  2796. net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
  2797. sizeof(*net->ipv6.ip6_blk_hole_entry),
  2798. GFP_KERNEL);
  2799. if (!net->ipv6.ip6_blk_hole_entry)
  2800. goto out_ip6_prohibit_entry;
  2801. net->ipv6.ip6_blk_hole_entry->dst.path =
  2802. (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
  2803. net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  2804. dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
  2805. ip6_template_metrics, true);
  2806. #endif
  2807. net->ipv6.sysctl.flush_delay = 0;
  2808. net->ipv6.sysctl.ip6_rt_max_size = 4096;
  2809. net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
  2810. net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
  2811. net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
  2812. net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
  2813. net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
  2814. net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
  2815. net->ipv6.ip6_rt_gc_expire = 30*HZ;
  2816. ret = 0;
  2817. out:
  2818. return ret;
  2819. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2820. out_ip6_prohibit_entry:
  2821. kfree(net->ipv6.ip6_prohibit_entry);
  2822. out_ip6_null_entry:
  2823. kfree(net->ipv6.ip6_null_entry);
  2824. #endif
  2825. out_ip6_dst_entries:
  2826. dst_entries_destroy(&net->ipv6.ip6_dst_ops);
  2827. out_ip6_dst_ops:
  2828. goto out;
  2829. }
  2830. static void __net_exit ip6_route_net_exit(struct net *net)
  2831. {
  2832. kfree(net->ipv6.ip6_null_entry);
  2833. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2834. kfree(net->ipv6.ip6_prohibit_entry);
  2835. kfree(net->ipv6.ip6_blk_hole_entry);
  2836. #endif
  2837. dst_entries_destroy(&net->ipv6.ip6_dst_ops);
  2838. }
  2839. static int __net_init ip6_route_net_init_late(struct net *net)
  2840. {
  2841. #ifdef CONFIG_PROC_FS
  2842. proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
  2843. proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
  2844. #endif
  2845. return 0;
  2846. }
  2847. static void __net_exit ip6_route_net_exit_late(struct net *net)
  2848. {
  2849. #ifdef CONFIG_PROC_FS
  2850. remove_proc_entry("ipv6_route", net->proc_net);
  2851. remove_proc_entry("rt6_stats", net->proc_net);
  2852. #endif
  2853. }
  2854. static struct pernet_operations ip6_route_net_ops = {
  2855. .init = ip6_route_net_init,
  2856. .exit = ip6_route_net_exit,
  2857. };
  2858. static int __net_init ipv6_inetpeer_init(struct net *net)
  2859. {
  2860. struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
  2861. if (!bp)
  2862. return -ENOMEM;
  2863. inet_peer_base_init(bp);
  2864. net->ipv6.peers = bp;
  2865. return 0;
  2866. }
  2867. static void __net_exit ipv6_inetpeer_exit(struct net *net)
  2868. {
  2869. struct inet_peer_base *bp = net->ipv6.peers;
  2870. net->ipv6.peers = NULL;
  2871. inetpeer_invalidate_tree(bp);
  2872. kfree(bp);
  2873. }
  2874. static struct pernet_operations ipv6_inetpeer_ops = {
  2875. .init = ipv6_inetpeer_init,
  2876. .exit = ipv6_inetpeer_exit,
  2877. };
  2878. static struct pernet_operations ip6_route_net_late_ops = {
  2879. .init = ip6_route_net_init_late,
  2880. .exit = ip6_route_net_exit_late,
  2881. };
  2882. static struct notifier_block ip6_route_dev_notifier = {
  2883. .notifier_call = ip6_route_dev_notify,
  2884. .priority = 0,
  2885. };
  2886. int __init ip6_route_init(void)
  2887. {
  2888. int ret;
  2889. int cpu;
  2890. ret = -ENOMEM;
  2891. ip6_dst_ops_template.kmem_cachep =
  2892. kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
  2893. SLAB_HWCACHE_ALIGN, NULL);
  2894. if (!ip6_dst_ops_template.kmem_cachep)
  2895. goto out;
  2896. ret = dst_entries_init(&ip6_dst_blackhole_ops);
  2897. if (ret)
  2898. goto out_kmem_cache;
  2899. ret = register_pernet_subsys(&ipv6_inetpeer_ops);
  2900. if (ret)
  2901. goto out_dst_entries;
  2902. ret = register_pernet_subsys(&ip6_route_net_ops);
  2903. if (ret)
  2904. goto out_register_inetpeer;
  2905. ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
  2906. /* Registering of the loopback is done before this portion of code,
  2907. * the loopback reference in rt6_info will not be taken, do it
  2908. * manually for init_net */
  2909. init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
  2910. init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  2911. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2912. init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
  2913. init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  2914. init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
  2915. init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  2916. #endif
  2917. ret = fib6_init();
  2918. if (ret)
  2919. goto out_register_subsys;
  2920. ret = xfrm6_init();
  2921. if (ret)
  2922. goto out_fib6_init;
  2923. ret = fib6_rules_init();
  2924. if (ret)
  2925. goto xfrm6_init;
  2926. ret = register_pernet_subsys(&ip6_route_net_late_ops);
  2927. if (ret)
  2928. goto fib6_rules_init;
  2929. ret = -ENOBUFS;
  2930. if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
  2931. __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
  2932. __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
  2933. goto out_register_late_subsys;
  2934. ret = register_netdevice_notifier(&ip6_route_dev_notifier);
  2935. if (ret)
  2936. goto out_register_late_subsys;
  2937. for_each_possible_cpu(cpu) {
  2938. struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
  2939. INIT_LIST_HEAD(&ul->head);
  2940. spin_lock_init(&ul->lock);
  2941. }
  2942. out:
  2943. return ret;
  2944. out_register_late_subsys:
  2945. unregister_pernet_subsys(&ip6_route_net_late_ops);
  2946. fib6_rules_init:
  2947. fib6_rules_cleanup();
  2948. xfrm6_init:
  2949. xfrm6_fini();
  2950. out_fib6_init:
  2951. fib6_gc_cleanup();
  2952. out_register_subsys:
  2953. unregister_pernet_subsys(&ip6_route_net_ops);
  2954. out_register_inetpeer:
  2955. unregister_pernet_subsys(&ipv6_inetpeer_ops);
  2956. out_dst_entries:
  2957. dst_entries_destroy(&ip6_dst_blackhole_ops);
  2958. out_kmem_cache:
  2959. kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
  2960. goto out;
  2961. }
  2962. void ip6_route_cleanup(void)
  2963. {
  2964. unregister_netdevice_notifier(&ip6_route_dev_notifier);
  2965. unregister_pernet_subsys(&ip6_route_net_late_ops);
  2966. fib6_rules_cleanup();
  2967. xfrm6_fini();
  2968. fib6_gc_cleanup();
  2969. unregister_pernet_subsys(&ipv6_inetpeer_ops);
  2970. unregister_pernet_subsys(&ip6_route_net_ops);
  2971. dst_entries_destroy(&ip6_dst_blackhole_ops);
  2972. kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
  2973. }