route.c 94 KB

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