route.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892
  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. #include <linux/capability.h>
  26. #include <linux/errno.h>
  27. #include <linux/types.h>
  28. #include <linux/times.h>
  29. #include <linux/socket.h>
  30. #include <linux/sockios.h>
  31. #include <linux/net.h>
  32. #include <linux/route.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/in6.h>
  35. #include <linux/mroute6.h>
  36. #include <linux/init.h>
  37. #include <linux/if_arp.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/nsproxy.h>
  41. #include <linux/slab.h>
  42. #include <net/net_namespace.h>
  43. #include <net/snmp.h>
  44. #include <net/ipv6.h>
  45. #include <net/ip6_fib.h>
  46. #include <net/ip6_route.h>
  47. #include <net/ndisc.h>
  48. #include <net/addrconf.h>
  49. #include <net/tcp.h>
  50. #include <linux/rtnetlink.h>
  51. #include <net/dst.h>
  52. #include <net/xfrm.h>
  53. #include <net/netevent.h>
  54. #include <net/netlink.h>
  55. #include <asm/uaccess.h>
  56. #ifdef CONFIG_SYSCTL
  57. #include <linux/sysctl.h>
  58. #endif
  59. /* Set to 3 to get tracing. */
  60. #define RT6_DEBUG 2
  61. #if RT6_DEBUG >= 3
  62. #define RDBG(x) printk x
  63. #define RT6_TRACE(x...) printk(KERN_DEBUG x)
  64. #else
  65. #define RDBG(x)
  66. #define RT6_TRACE(x...) do { ; } while (0)
  67. #endif
  68. static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
  69. static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
  70. static unsigned int ip6_default_advmss(const struct dst_entry *dst);
  71. static unsigned int ip6_default_mtu(const struct dst_entry *dst);
  72. static struct dst_entry *ip6_negative_advice(struct dst_entry *);
  73. static void ip6_dst_destroy(struct dst_entry *);
  74. static void ip6_dst_ifdown(struct dst_entry *,
  75. struct net_device *dev, int how);
  76. static int ip6_dst_gc(struct dst_ops *ops);
  77. static int ip6_pkt_discard(struct sk_buff *skb);
  78. static int ip6_pkt_discard_out(struct sk_buff *skb);
  79. static void ip6_link_failure(struct sk_buff *skb);
  80. static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
  81. #ifdef CONFIG_IPV6_ROUTE_INFO
  82. static struct rt6_info *rt6_add_route_info(struct net *net,
  83. struct in6_addr *prefix, int prefixlen,
  84. struct in6_addr *gwaddr, int ifindex,
  85. unsigned pref);
  86. static struct rt6_info *rt6_get_route_info(struct net *net,
  87. struct in6_addr *prefix, int prefixlen,
  88. struct in6_addr *gwaddr, int ifindex);
  89. #endif
  90. static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
  91. {
  92. struct rt6_info *rt = (struct rt6_info *) dst;
  93. struct inet_peer *peer;
  94. u32 *p = NULL;
  95. if (!rt->rt6i_peer)
  96. rt6_bind_peer(rt, 1);
  97. peer = rt->rt6i_peer;
  98. if (peer) {
  99. u32 *old_p = __DST_METRICS_PTR(old);
  100. unsigned long prev, new;
  101. p = peer->metrics;
  102. if (inet_metrics_new(peer))
  103. memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
  104. new = (unsigned long) p;
  105. prev = cmpxchg(&dst->_metrics, old, new);
  106. if (prev != old) {
  107. p = __DST_METRICS_PTR(prev);
  108. if (prev & DST_METRICS_READ_ONLY)
  109. p = NULL;
  110. }
  111. }
  112. return p;
  113. }
  114. static struct dst_ops ip6_dst_ops_template = {
  115. .family = AF_INET6,
  116. .protocol = cpu_to_be16(ETH_P_IPV6),
  117. .gc = ip6_dst_gc,
  118. .gc_thresh = 1024,
  119. .check = ip6_dst_check,
  120. .default_advmss = ip6_default_advmss,
  121. .default_mtu = ip6_default_mtu,
  122. .cow_metrics = ipv6_cow_metrics,
  123. .destroy = ip6_dst_destroy,
  124. .ifdown = ip6_dst_ifdown,
  125. .negative_advice = ip6_negative_advice,
  126. .link_failure = ip6_link_failure,
  127. .update_pmtu = ip6_rt_update_pmtu,
  128. .local_out = __ip6_local_out,
  129. };
  130. static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst)
  131. {
  132. return 0;
  133. }
  134. static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
  135. {
  136. }
  137. static struct dst_ops ip6_dst_blackhole_ops = {
  138. .family = AF_INET6,
  139. .protocol = cpu_to_be16(ETH_P_IPV6),
  140. .destroy = ip6_dst_destroy,
  141. .check = ip6_dst_check,
  142. .default_mtu = ip6_blackhole_default_mtu,
  143. .default_advmss = ip6_default_advmss,
  144. .update_pmtu = ip6_rt_blackhole_update_pmtu,
  145. };
  146. static const u32 ip6_template_metrics[RTAX_MAX] = {
  147. [RTAX_HOPLIMIT - 1] = 255,
  148. };
  149. static struct rt6_info ip6_null_entry_template = {
  150. .dst = {
  151. .__refcnt = ATOMIC_INIT(1),
  152. .__use = 1,
  153. .obsolete = -1,
  154. .error = -ENETUNREACH,
  155. .input = ip6_pkt_discard,
  156. .output = ip6_pkt_discard_out,
  157. },
  158. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  159. .rt6i_protocol = RTPROT_KERNEL,
  160. .rt6i_metric = ~(u32) 0,
  161. .rt6i_ref = ATOMIC_INIT(1),
  162. };
  163. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  164. static int ip6_pkt_prohibit(struct sk_buff *skb);
  165. static int ip6_pkt_prohibit_out(struct sk_buff *skb);
  166. static struct rt6_info ip6_prohibit_entry_template = {
  167. .dst = {
  168. .__refcnt = ATOMIC_INIT(1),
  169. .__use = 1,
  170. .obsolete = -1,
  171. .error = -EACCES,
  172. .input = ip6_pkt_prohibit,
  173. .output = ip6_pkt_prohibit_out,
  174. },
  175. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  176. .rt6i_protocol = RTPROT_KERNEL,
  177. .rt6i_metric = ~(u32) 0,
  178. .rt6i_ref = ATOMIC_INIT(1),
  179. };
  180. static struct rt6_info ip6_blk_hole_entry_template = {
  181. .dst = {
  182. .__refcnt = ATOMIC_INIT(1),
  183. .__use = 1,
  184. .obsolete = -1,
  185. .error = -EINVAL,
  186. .input = dst_discard,
  187. .output = dst_discard,
  188. },
  189. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  190. .rt6i_protocol = RTPROT_KERNEL,
  191. .rt6i_metric = ~(u32) 0,
  192. .rt6i_ref = ATOMIC_INIT(1),
  193. };
  194. #endif
  195. /* allocate dst with ip6_dst_ops */
  196. static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
  197. {
  198. return (struct rt6_info *)dst_alloc(ops, 0);
  199. }
  200. static void ip6_dst_destroy(struct dst_entry *dst)
  201. {
  202. struct rt6_info *rt = (struct rt6_info *)dst;
  203. struct inet6_dev *idev = rt->rt6i_idev;
  204. struct inet_peer *peer = rt->rt6i_peer;
  205. if (idev != NULL) {
  206. rt->rt6i_idev = NULL;
  207. in6_dev_put(idev);
  208. }
  209. if (peer) {
  210. rt->rt6i_peer = NULL;
  211. inet_putpeer(peer);
  212. }
  213. }
  214. static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
  215. static u32 rt6_peer_genid(void)
  216. {
  217. return atomic_read(&__rt6_peer_genid);
  218. }
  219. void rt6_bind_peer(struct rt6_info *rt, int create)
  220. {
  221. struct inet_peer *peer;
  222. peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
  223. if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
  224. inet_putpeer(peer);
  225. else
  226. rt->rt6i_peer_genid = rt6_peer_genid();
  227. }
  228. static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  229. int how)
  230. {
  231. struct rt6_info *rt = (struct rt6_info *)dst;
  232. struct inet6_dev *idev = rt->rt6i_idev;
  233. struct net_device *loopback_dev =
  234. dev_net(dev)->loopback_dev;
  235. if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
  236. struct inet6_dev *loopback_idev =
  237. in6_dev_get(loopback_dev);
  238. if (loopback_idev != NULL) {
  239. rt->rt6i_idev = loopback_idev;
  240. in6_dev_put(idev);
  241. }
  242. }
  243. }
  244. static __inline__ int rt6_check_expired(const struct rt6_info *rt)
  245. {
  246. return (rt->rt6i_flags & RTF_EXPIRES) &&
  247. time_after(jiffies, rt->rt6i_expires);
  248. }
  249. static inline int rt6_need_strict(struct in6_addr *daddr)
  250. {
  251. return ipv6_addr_type(daddr) &
  252. (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
  253. }
  254. /*
  255. * Route lookup. Any table->tb6_lock is implied.
  256. */
  257. static inline struct rt6_info *rt6_device_match(struct net *net,
  258. struct rt6_info *rt,
  259. struct in6_addr *saddr,
  260. int oif,
  261. int flags)
  262. {
  263. struct rt6_info *local = NULL;
  264. struct rt6_info *sprt;
  265. if (!oif && ipv6_addr_any(saddr))
  266. goto out;
  267. for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
  268. struct net_device *dev = sprt->rt6i_dev;
  269. if (oif) {
  270. if (dev->ifindex == oif)
  271. return sprt;
  272. if (dev->flags & IFF_LOOPBACK) {
  273. if (sprt->rt6i_idev == NULL ||
  274. sprt->rt6i_idev->dev->ifindex != oif) {
  275. if (flags & RT6_LOOKUP_F_IFACE && oif)
  276. continue;
  277. if (local && (!oif ||
  278. local->rt6i_idev->dev->ifindex == oif))
  279. continue;
  280. }
  281. local = sprt;
  282. }
  283. } else {
  284. if (ipv6_chk_addr(net, saddr, dev,
  285. flags & RT6_LOOKUP_F_IFACE))
  286. return sprt;
  287. }
  288. }
  289. if (oif) {
  290. if (local)
  291. return local;
  292. if (flags & RT6_LOOKUP_F_IFACE)
  293. return net->ipv6.ip6_null_entry;
  294. }
  295. out:
  296. return rt;
  297. }
  298. #ifdef CONFIG_IPV6_ROUTER_PREF
  299. static void rt6_probe(struct rt6_info *rt)
  300. {
  301. struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
  302. /*
  303. * Okay, this does not seem to be appropriate
  304. * for now, however, we need to check if it
  305. * is really so; aka Router Reachability Probing.
  306. *
  307. * Router Reachability Probe MUST be rate-limited
  308. * to no more than one per minute.
  309. */
  310. if (!neigh || (neigh->nud_state & NUD_VALID))
  311. return;
  312. read_lock_bh(&neigh->lock);
  313. if (!(neigh->nud_state & NUD_VALID) &&
  314. time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
  315. struct in6_addr mcaddr;
  316. struct in6_addr *target;
  317. neigh->updated = jiffies;
  318. read_unlock_bh(&neigh->lock);
  319. target = (struct in6_addr *)&neigh->primary_key;
  320. addrconf_addr_solict_mult(target, &mcaddr);
  321. ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
  322. } else
  323. read_unlock_bh(&neigh->lock);
  324. }
  325. #else
  326. static inline void rt6_probe(struct rt6_info *rt)
  327. {
  328. }
  329. #endif
  330. /*
  331. * Default Router Selection (RFC 2461 6.3.6)
  332. */
  333. static inline int rt6_check_dev(struct rt6_info *rt, int oif)
  334. {
  335. struct net_device *dev = rt->rt6i_dev;
  336. if (!oif || dev->ifindex == oif)
  337. return 2;
  338. if ((dev->flags & IFF_LOOPBACK) &&
  339. rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
  340. return 1;
  341. return 0;
  342. }
  343. static inline int rt6_check_neigh(struct rt6_info *rt)
  344. {
  345. struct neighbour *neigh = rt->rt6i_nexthop;
  346. int m;
  347. if (rt->rt6i_flags & RTF_NONEXTHOP ||
  348. !(rt->rt6i_flags & RTF_GATEWAY))
  349. m = 1;
  350. else if (neigh) {
  351. read_lock_bh(&neigh->lock);
  352. if (neigh->nud_state & NUD_VALID)
  353. m = 2;
  354. #ifdef CONFIG_IPV6_ROUTER_PREF
  355. else if (neigh->nud_state & NUD_FAILED)
  356. m = 0;
  357. #endif
  358. else
  359. m = 1;
  360. read_unlock_bh(&neigh->lock);
  361. } else
  362. m = 0;
  363. return m;
  364. }
  365. static int rt6_score_route(struct rt6_info *rt, int oif,
  366. int strict)
  367. {
  368. int m, n;
  369. m = rt6_check_dev(rt, oif);
  370. if (!m && (strict & RT6_LOOKUP_F_IFACE))
  371. return -1;
  372. #ifdef CONFIG_IPV6_ROUTER_PREF
  373. m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
  374. #endif
  375. n = rt6_check_neigh(rt);
  376. if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
  377. return -1;
  378. return m;
  379. }
  380. static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
  381. int *mpri, struct rt6_info *match)
  382. {
  383. int m;
  384. if (rt6_check_expired(rt))
  385. goto out;
  386. m = rt6_score_route(rt, oif, strict);
  387. if (m < 0)
  388. goto out;
  389. if (m > *mpri) {
  390. if (strict & RT6_LOOKUP_F_REACHABLE)
  391. rt6_probe(match);
  392. *mpri = m;
  393. match = rt;
  394. } else if (strict & RT6_LOOKUP_F_REACHABLE) {
  395. rt6_probe(rt);
  396. }
  397. out:
  398. return match;
  399. }
  400. static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
  401. struct rt6_info *rr_head,
  402. u32 metric, int oif, int strict)
  403. {
  404. struct rt6_info *rt, *match;
  405. int mpri = -1;
  406. match = NULL;
  407. for (rt = rr_head; rt && rt->rt6i_metric == metric;
  408. rt = rt->dst.rt6_next)
  409. match = find_match(rt, oif, strict, &mpri, match);
  410. for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
  411. rt = rt->dst.rt6_next)
  412. match = find_match(rt, oif, strict, &mpri, match);
  413. return match;
  414. }
  415. static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
  416. {
  417. struct rt6_info *match, *rt0;
  418. struct net *net;
  419. RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
  420. __func__, fn->leaf, oif);
  421. rt0 = fn->rr_ptr;
  422. if (!rt0)
  423. fn->rr_ptr = rt0 = fn->leaf;
  424. match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
  425. if (!match &&
  426. (strict & RT6_LOOKUP_F_REACHABLE)) {
  427. struct rt6_info *next = rt0->dst.rt6_next;
  428. /* no entries matched; do round-robin */
  429. if (!next || next->rt6i_metric != rt0->rt6i_metric)
  430. next = fn->leaf;
  431. if (next != rt0)
  432. fn->rr_ptr = next;
  433. }
  434. RT6_TRACE("%s() => %p\n",
  435. __func__, match);
  436. net = dev_net(rt0->rt6i_dev);
  437. return match ? match : net->ipv6.ip6_null_entry;
  438. }
  439. #ifdef CONFIG_IPV6_ROUTE_INFO
  440. int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
  441. struct in6_addr *gwaddr)
  442. {
  443. struct net *net = dev_net(dev);
  444. struct route_info *rinfo = (struct route_info *) opt;
  445. struct in6_addr prefix_buf, *prefix;
  446. unsigned int pref;
  447. unsigned long lifetime;
  448. struct rt6_info *rt;
  449. if (len < sizeof(struct route_info)) {
  450. return -EINVAL;
  451. }
  452. /* Sanity check for prefix_len and length */
  453. if (rinfo->length > 3) {
  454. return -EINVAL;
  455. } else if (rinfo->prefix_len > 128) {
  456. return -EINVAL;
  457. } else if (rinfo->prefix_len > 64) {
  458. if (rinfo->length < 2) {
  459. return -EINVAL;
  460. }
  461. } else if (rinfo->prefix_len > 0) {
  462. if (rinfo->length < 1) {
  463. return -EINVAL;
  464. }
  465. }
  466. pref = rinfo->route_pref;
  467. if (pref == ICMPV6_ROUTER_PREF_INVALID)
  468. return -EINVAL;
  469. lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
  470. if (rinfo->length == 3)
  471. prefix = (struct in6_addr *)rinfo->prefix;
  472. else {
  473. /* this function is safe */
  474. ipv6_addr_prefix(&prefix_buf,
  475. (struct in6_addr *)rinfo->prefix,
  476. rinfo->prefix_len);
  477. prefix = &prefix_buf;
  478. }
  479. rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
  480. dev->ifindex);
  481. if (rt && !lifetime) {
  482. ip6_del_rt(rt);
  483. rt = NULL;
  484. }
  485. if (!rt && lifetime)
  486. rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
  487. pref);
  488. else if (rt)
  489. rt->rt6i_flags = RTF_ROUTEINFO |
  490. (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
  491. if (rt) {
  492. if (!addrconf_finite_timeout(lifetime)) {
  493. rt->rt6i_flags &= ~RTF_EXPIRES;
  494. } else {
  495. rt->rt6i_expires = jiffies + HZ * lifetime;
  496. rt->rt6i_flags |= RTF_EXPIRES;
  497. }
  498. dst_release(&rt->dst);
  499. }
  500. return 0;
  501. }
  502. #endif
  503. #define BACKTRACK(__net, saddr) \
  504. do { \
  505. if (rt == __net->ipv6.ip6_null_entry) { \
  506. struct fib6_node *pn; \
  507. while (1) { \
  508. if (fn->fn_flags & RTN_TL_ROOT) \
  509. goto out; \
  510. pn = fn->parent; \
  511. if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
  512. fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
  513. else \
  514. fn = pn; \
  515. if (fn->fn_flags & RTN_RTINFO) \
  516. goto restart; \
  517. } \
  518. } \
  519. } while(0)
  520. static struct rt6_info *ip6_pol_route_lookup(struct net *net,
  521. struct fib6_table *table,
  522. struct flowi6 *fl6, int flags)
  523. {
  524. struct fib6_node *fn;
  525. struct rt6_info *rt;
  526. read_lock_bh(&table->tb6_lock);
  527. fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  528. restart:
  529. rt = fn->leaf;
  530. rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
  531. BACKTRACK(net, &fl6->saddr);
  532. out:
  533. dst_use(&rt->dst, jiffies);
  534. read_unlock_bh(&table->tb6_lock);
  535. return rt;
  536. }
  537. struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
  538. const struct in6_addr *saddr, int oif, int strict)
  539. {
  540. struct flowi6 fl6 = {
  541. .flowi6_oif = oif,
  542. .daddr = *daddr,
  543. };
  544. struct dst_entry *dst;
  545. int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
  546. if (saddr) {
  547. memcpy(&fl6.saddr, saddr, sizeof(*saddr));
  548. flags |= RT6_LOOKUP_F_HAS_SADDR;
  549. }
  550. dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
  551. if (dst->error == 0)
  552. return (struct rt6_info *) dst;
  553. dst_release(dst);
  554. return NULL;
  555. }
  556. EXPORT_SYMBOL(rt6_lookup);
  557. /* ip6_ins_rt is called with FREE table->tb6_lock.
  558. It takes new route entry, the addition fails by any reason the
  559. route is freed. In any case, if caller does not hold it, it may
  560. be destroyed.
  561. */
  562. static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
  563. {
  564. int err;
  565. struct fib6_table *table;
  566. table = rt->rt6i_table;
  567. write_lock_bh(&table->tb6_lock);
  568. err = fib6_add(&table->tb6_root, rt, info);
  569. write_unlock_bh(&table->tb6_lock);
  570. return err;
  571. }
  572. int ip6_ins_rt(struct rt6_info *rt)
  573. {
  574. struct nl_info info = {
  575. .nl_net = dev_net(rt->rt6i_dev),
  576. };
  577. return __ip6_ins_rt(rt, &info);
  578. }
  579. static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
  580. struct in6_addr *saddr)
  581. {
  582. struct rt6_info *rt;
  583. /*
  584. * Clone the route.
  585. */
  586. rt = ip6_rt_copy(ort);
  587. if (rt) {
  588. struct neighbour *neigh;
  589. int attempts = !in_softirq();
  590. if (!(rt->rt6i_flags&RTF_GATEWAY)) {
  591. if (rt->rt6i_dst.plen != 128 &&
  592. ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
  593. rt->rt6i_flags |= RTF_ANYCAST;
  594. ipv6_addr_copy(&rt->rt6i_gateway, daddr);
  595. }
  596. ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
  597. rt->rt6i_dst.plen = 128;
  598. rt->rt6i_flags |= RTF_CACHE;
  599. rt->dst.flags |= DST_HOST;
  600. #ifdef CONFIG_IPV6_SUBTREES
  601. if (rt->rt6i_src.plen && saddr) {
  602. ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
  603. rt->rt6i_src.plen = 128;
  604. }
  605. #endif
  606. retry:
  607. neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
  608. if (IS_ERR(neigh)) {
  609. struct net *net = dev_net(rt->rt6i_dev);
  610. int saved_rt_min_interval =
  611. net->ipv6.sysctl.ip6_rt_gc_min_interval;
  612. int saved_rt_elasticity =
  613. net->ipv6.sysctl.ip6_rt_gc_elasticity;
  614. if (attempts-- > 0) {
  615. net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
  616. net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
  617. ip6_dst_gc(&net->ipv6.ip6_dst_ops);
  618. net->ipv6.sysctl.ip6_rt_gc_elasticity =
  619. saved_rt_elasticity;
  620. net->ipv6.sysctl.ip6_rt_gc_min_interval =
  621. saved_rt_min_interval;
  622. goto retry;
  623. }
  624. if (net_ratelimit())
  625. printk(KERN_WARNING
  626. "ipv6: Neighbour table overflow.\n");
  627. dst_free(&rt->dst);
  628. return NULL;
  629. }
  630. rt->rt6i_nexthop = neigh;
  631. }
  632. return rt;
  633. }
  634. static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
  635. {
  636. struct rt6_info *rt = ip6_rt_copy(ort);
  637. if (rt) {
  638. ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
  639. rt->rt6i_dst.plen = 128;
  640. rt->rt6i_flags |= RTF_CACHE;
  641. rt->dst.flags |= DST_HOST;
  642. rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
  643. }
  644. return rt;
  645. }
  646. static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
  647. struct flowi6 *fl6, int flags)
  648. {
  649. struct fib6_node *fn;
  650. struct rt6_info *rt, *nrt;
  651. int strict = 0;
  652. int attempts = 3;
  653. int err;
  654. int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
  655. strict |= flags & RT6_LOOKUP_F_IFACE;
  656. relookup:
  657. read_lock_bh(&table->tb6_lock);
  658. restart_2:
  659. fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  660. restart:
  661. rt = rt6_select(fn, oif, strict | reachable);
  662. BACKTRACK(net, &fl6->saddr);
  663. if (rt == net->ipv6.ip6_null_entry ||
  664. rt->rt6i_flags & RTF_CACHE)
  665. goto out;
  666. dst_hold(&rt->dst);
  667. read_unlock_bh(&table->tb6_lock);
  668. if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
  669. nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
  670. else if (!(rt->dst.flags & DST_HOST))
  671. nrt = rt6_alloc_clone(rt, &fl6->daddr);
  672. else
  673. goto out2;
  674. dst_release(&rt->dst);
  675. rt = nrt ? : net->ipv6.ip6_null_entry;
  676. dst_hold(&rt->dst);
  677. if (nrt) {
  678. err = ip6_ins_rt(nrt);
  679. if (!err)
  680. goto out2;
  681. }
  682. if (--attempts <= 0)
  683. goto out2;
  684. /*
  685. * Race condition! In the gap, when table->tb6_lock was
  686. * released someone could insert this route. Relookup.
  687. */
  688. dst_release(&rt->dst);
  689. goto relookup;
  690. out:
  691. if (reachable) {
  692. reachable = 0;
  693. goto restart_2;
  694. }
  695. dst_hold(&rt->dst);
  696. read_unlock_bh(&table->tb6_lock);
  697. out2:
  698. rt->dst.lastuse = jiffies;
  699. rt->dst.__use++;
  700. return rt;
  701. }
  702. static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
  703. struct flowi6 *fl6, int flags)
  704. {
  705. return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
  706. }
  707. void ip6_route_input(struct sk_buff *skb)
  708. {
  709. struct ipv6hdr *iph = ipv6_hdr(skb);
  710. struct net *net = dev_net(skb->dev);
  711. int flags = RT6_LOOKUP_F_HAS_SADDR;
  712. struct flowi6 fl6 = {
  713. .flowi6_iif = skb->dev->ifindex,
  714. .daddr = iph->daddr,
  715. .saddr = iph->saddr,
  716. .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
  717. .flowi6_mark = skb->mark,
  718. .flowi6_proto = iph->nexthdr,
  719. };
  720. if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
  721. flags |= RT6_LOOKUP_F_IFACE;
  722. skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
  723. }
  724. static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
  725. struct flowi6 *fl6, int flags)
  726. {
  727. return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
  728. }
  729. struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
  730. struct flowi6 *fl6)
  731. {
  732. int flags = 0;
  733. if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
  734. flags |= RT6_LOOKUP_F_IFACE;
  735. if (!ipv6_addr_any(&fl6->saddr))
  736. flags |= RT6_LOOKUP_F_HAS_SADDR;
  737. else if (sk)
  738. flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
  739. return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
  740. }
  741. EXPORT_SYMBOL(ip6_route_output);
  742. struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
  743. {
  744. struct rt6_info *rt = dst_alloc(&ip6_dst_blackhole_ops, 1);
  745. struct rt6_info *ort = (struct rt6_info *) dst_orig;
  746. struct dst_entry *new = NULL;
  747. if (rt) {
  748. new = &rt->dst;
  749. new->__use = 1;
  750. new->input = dst_discard;
  751. new->output = dst_discard;
  752. dst_copy_metrics(new, &ort->dst);
  753. new->dev = ort->dst.dev;
  754. if (new->dev)
  755. dev_hold(new->dev);
  756. rt->rt6i_idev = ort->rt6i_idev;
  757. if (rt->rt6i_idev)
  758. in6_dev_hold(rt->rt6i_idev);
  759. rt->rt6i_expires = 0;
  760. ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
  761. rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
  762. rt->rt6i_metric = 0;
  763. memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
  764. #ifdef CONFIG_IPV6_SUBTREES
  765. memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
  766. #endif
  767. dst_free(new);
  768. }
  769. dst_release(dst_orig);
  770. return new ? new : ERR_PTR(-ENOMEM);
  771. }
  772. /*
  773. * Destination cache support functions
  774. */
  775. static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
  776. {
  777. struct rt6_info *rt;
  778. rt = (struct rt6_info *) dst;
  779. if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
  780. if (rt->rt6i_peer_genid != rt6_peer_genid()) {
  781. if (!rt->rt6i_peer)
  782. rt6_bind_peer(rt, 0);
  783. rt->rt6i_peer_genid = rt6_peer_genid();
  784. }
  785. return dst;
  786. }
  787. return NULL;
  788. }
  789. static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
  790. {
  791. struct rt6_info *rt = (struct rt6_info *) dst;
  792. if (rt) {
  793. if (rt->rt6i_flags & RTF_CACHE) {
  794. if (rt6_check_expired(rt)) {
  795. ip6_del_rt(rt);
  796. dst = NULL;
  797. }
  798. } else {
  799. dst_release(dst);
  800. dst = NULL;
  801. }
  802. }
  803. return dst;
  804. }
  805. static void ip6_link_failure(struct sk_buff *skb)
  806. {
  807. struct rt6_info *rt;
  808. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
  809. rt = (struct rt6_info *) skb_dst(skb);
  810. if (rt) {
  811. if (rt->rt6i_flags&RTF_CACHE) {
  812. dst_set_expires(&rt->dst, 0);
  813. rt->rt6i_flags |= RTF_EXPIRES;
  814. } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
  815. rt->rt6i_node->fn_sernum = -1;
  816. }
  817. }
  818. static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
  819. {
  820. struct rt6_info *rt6 = (struct rt6_info*)dst;
  821. if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
  822. rt6->rt6i_flags |= RTF_MODIFIED;
  823. if (mtu < IPV6_MIN_MTU) {
  824. u32 features = dst_metric(dst, RTAX_FEATURES);
  825. mtu = IPV6_MIN_MTU;
  826. features |= RTAX_FEATURE_ALLFRAG;
  827. dst_metric_set(dst, RTAX_FEATURES, features);
  828. }
  829. dst_metric_set(dst, RTAX_MTU, mtu);
  830. }
  831. }
  832. static unsigned int ip6_default_advmss(const struct dst_entry *dst)
  833. {
  834. struct net_device *dev = dst->dev;
  835. unsigned int mtu = dst_mtu(dst);
  836. struct net *net = dev_net(dev);
  837. mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
  838. if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
  839. mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
  840. /*
  841. * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
  842. * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
  843. * IPV6_MAXPLEN is also valid and means: "any MSS,
  844. * rely only on pmtu discovery"
  845. */
  846. if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
  847. mtu = IPV6_MAXPLEN;
  848. return mtu;
  849. }
  850. static unsigned int ip6_default_mtu(const struct dst_entry *dst)
  851. {
  852. unsigned int mtu = IPV6_MIN_MTU;
  853. struct inet6_dev *idev;
  854. rcu_read_lock();
  855. idev = __in6_dev_get(dst->dev);
  856. if (idev)
  857. mtu = idev->cnf.mtu6;
  858. rcu_read_unlock();
  859. return mtu;
  860. }
  861. static struct dst_entry *icmp6_dst_gc_list;
  862. static DEFINE_SPINLOCK(icmp6_dst_lock);
  863. struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
  864. struct neighbour *neigh,
  865. const struct in6_addr *addr)
  866. {
  867. struct rt6_info *rt;
  868. struct inet6_dev *idev = in6_dev_get(dev);
  869. struct net *net = dev_net(dev);
  870. if (unlikely(idev == NULL))
  871. return NULL;
  872. rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
  873. if (unlikely(rt == NULL)) {
  874. in6_dev_put(idev);
  875. goto out;
  876. }
  877. dev_hold(dev);
  878. if (neigh)
  879. neigh_hold(neigh);
  880. else {
  881. neigh = ndisc_get_neigh(dev, addr);
  882. if (IS_ERR(neigh))
  883. neigh = NULL;
  884. }
  885. rt->rt6i_dev = dev;
  886. rt->rt6i_idev = idev;
  887. rt->rt6i_nexthop = neigh;
  888. atomic_set(&rt->dst.__refcnt, 1);
  889. dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
  890. rt->dst.output = ip6_output;
  891. #if 0 /* there's no chance to use these for ndisc */
  892. rt->dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
  893. ? DST_HOST
  894. : 0;
  895. ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
  896. rt->rt6i_dst.plen = 128;
  897. #endif
  898. spin_lock_bh(&icmp6_dst_lock);
  899. rt->dst.next = icmp6_dst_gc_list;
  900. icmp6_dst_gc_list = &rt->dst;
  901. spin_unlock_bh(&icmp6_dst_lock);
  902. fib6_force_start_gc(net);
  903. out:
  904. return &rt->dst;
  905. }
  906. int icmp6_dst_gc(void)
  907. {
  908. struct dst_entry *dst, **pprev;
  909. int more = 0;
  910. spin_lock_bh(&icmp6_dst_lock);
  911. pprev = &icmp6_dst_gc_list;
  912. while ((dst = *pprev) != NULL) {
  913. if (!atomic_read(&dst->__refcnt)) {
  914. *pprev = dst->next;
  915. dst_free(dst);
  916. } else {
  917. pprev = &dst->next;
  918. ++more;
  919. }
  920. }
  921. spin_unlock_bh(&icmp6_dst_lock);
  922. return more;
  923. }
  924. static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
  925. void *arg)
  926. {
  927. struct dst_entry *dst, **pprev;
  928. spin_lock_bh(&icmp6_dst_lock);
  929. pprev = &icmp6_dst_gc_list;
  930. while ((dst = *pprev) != NULL) {
  931. struct rt6_info *rt = (struct rt6_info *) dst;
  932. if (func(rt, arg)) {
  933. *pprev = dst->next;
  934. dst_free(dst);
  935. } else {
  936. pprev = &dst->next;
  937. }
  938. }
  939. spin_unlock_bh(&icmp6_dst_lock);
  940. }
  941. static int ip6_dst_gc(struct dst_ops *ops)
  942. {
  943. unsigned long now = jiffies;
  944. struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
  945. int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
  946. int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
  947. int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
  948. int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
  949. unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
  950. int entries;
  951. entries = dst_entries_get_fast(ops);
  952. if (time_after(rt_last_gc + rt_min_interval, now) &&
  953. entries <= rt_max_size)
  954. goto out;
  955. net->ipv6.ip6_rt_gc_expire++;
  956. fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
  957. net->ipv6.ip6_rt_last_gc = now;
  958. entries = dst_entries_get_slow(ops);
  959. if (entries < ops->gc_thresh)
  960. net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
  961. out:
  962. net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
  963. return entries > rt_max_size;
  964. }
  965. /* Clean host part of a prefix. Not necessary in radix tree,
  966. but results in cleaner routing tables.
  967. Remove it only when all the things will work!
  968. */
  969. int ip6_dst_hoplimit(struct dst_entry *dst)
  970. {
  971. int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
  972. if (hoplimit == 0) {
  973. struct net_device *dev = dst->dev;
  974. struct inet6_dev *idev;
  975. rcu_read_lock();
  976. idev = __in6_dev_get(dev);
  977. if (idev)
  978. hoplimit = idev->cnf.hop_limit;
  979. else
  980. hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
  981. rcu_read_unlock();
  982. }
  983. return hoplimit;
  984. }
  985. EXPORT_SYMBOL(ip6_dst_hoplimit);
  986. /*
  987. *
  988. */
  989. int ip6_route_add(struct fib6_config *cfg)
  990. {
  991. int err;
  992. struct net *net = cfg->fc_nlinfo.nl_net;
  993. struct rt6_info *rt = NULL;
  994. struct net_device *dev = NULL;
  995. struct inet6_dev *idev = NULL;
  996. struct fib6_table *table;
  997. int addr_type;
  998. if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
  999. return -EINVAL;
  1000. #ifndef CONFIG_IPV6_SUBTREES
  1001. if (cfg->fc_src_len)
  1002. return -EINVAL;
  1003. #endif
  1004. if (cfg->fc_ifindex) {
  1005. err = -ENODEV;
  1006. dev = dev_get_by_index(net, cfg->fc_ifindex);
  1007. if (!dev)
  1008. goto out;
  1009. idev = in6_dev_get(dev);
  1010. if (!idev)
  1011. goto out;
  1012. }
  1013. if (cfg->fc_metric == 0)
  1014. cfg->fc_metric = IP6_RT_PRIO_USER;
  1015. table = fib6_new_table(net, cfg->fc_table);
  1016. if (table == NULL) {
  1017. err = -ENOBUFS;
  1018. goto out;
  1019. }
  1020. rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
  1021. if (rt == NULL) {
  1022. err = -ENOMEM;
  1023. goto out;
  1024. }
  1025. rt->dst.obsolete = -1;
  1026. rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
  1027. jiffies + clock_t_to_jiffies(cfg->fc_expires) :
  1028. 0;
  1029. if (cfg->fc_protocol == RTPROT_UNSPEC)
  1030. cfg->fc_protocol = RTPROT_BOOT;
  1031. rt->rt6i_protocol = cfg->fc_protocol;
  1032. addr_type = ipv6_addr_type(&cfg->fc_dst);
  1033. if (addr_type & IPV6_ADDR_MULTICAST)
  1034. rt->dst.input = ip6_mc_input;
  1035. else if (cfg->fc_flags & RTF_LOCAL)
  1036. rt->dst.input = ip6_input;
  1037. else
  1038. rt->dst.input = ip6_forward;
  1039. rt->dst.output = ip6_output;
  1040. ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
  1041. rt->rt6i_dst.plen = cfg->fc_dst_len;
  1042. if (rt->rt6i_dst.plen == 128)
  1043. rt->dst.flags = DST_HOST;
  1044. #ifdef CONFIG_IPV6_SUBTREES
  1045. ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
  1046. rt->rt6i_src.plen = cfg->fc_src_len;
  1047. #endif
  1048. rt->rt6i_metric = cfg->fc_metric;
  1049. /* We cannot add true routes via loopback here,
  1050. they would result in kernel looping; promote them to reject routes
  1051. */
  1052. if ((cfg->fc_flags & RTF_REJECT) ||
  1053. (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
  1054. && !(cfg->fc_flags&RTF_LOCAL))) {
  1055. /* hold loopback dev/idev if we haven't done so. */
  1056. if (dev != net->loopback_dev) {
  1057. if (dev) {
  1058. dev_put(dev);
  1059. in6_dev_put(idev);
  1060. }
  1061. dev = net->loopback_dev;
  1062. dev_hold(dev);
  1063. idev = in6_dev_get(dev);
  1064. if (!idev) {
  1065. err = -ENODEV;
  1066. goto out;
  1067. }
  1068. }
  1069. rt->dst.output = ip6_pkt_discard_out;
  1070. rt->dst.input = ip6_pkt_discard;
  1071. rt->dst.error = -ENETUNREACH;
  1072. rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
  1073. goto install_route;
  1074. }
  1075. if (cfg->fc_flags & RTF_GATEWAY) {
  1076. struct in6_addr *gw_addr;
  1077. int gwa_type;
  1078. gw_addr = &cfg->fc_gateway;
  1079. ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
  1080. gwa_type = ipv6_addr_type(gw_addr);
  1081. if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
  1082. struct rt6_info *grt;
  1083. /* IPv6 strictly inhibits using not link-local
  1084. addresses as nexthop address.
  1085. Otherwise, router will not able to send redirects.
  1086. It is very good, but in some (rare!) circumstances
  1087. (SIT, PtP, NBMA NOARP links) it is handy to allow
  1088. some exceptions. --ANK
  1089. */
  1090. err = -EINVAL;
  1091. if (!(gwa_type&IPV6_ADDR_UNICAST))
  1092. goto out;
  1093. grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
  1094. err = -EHOSTUNREACH;
  1095. if (grt == NULL)
  1096. goto out;
  1097. if (dev) {
  1098. if (dev != grt->rt6i_dev) {
  1099. dst_release(&grt->dst);
  1100. goto out;
  1101. }
  1102. } else {
  1103. dev = grt->rt6i_dev;
  1104. idev = grt->rt6i_idev;
  1105. dev_hold(dev);
  1106. in6_dev_hold(grt->rt6i_idev);
  1107. }
  1108. if (!(grt->rt6i_flags&RTF_GATEWAY))
  1109. err = 0;
  1110. dst_release(&grt->dst);
  1111. if (err)
  1112. goto out;
  1113. }
  1114. err = -EINVAL;
  1115. if (dev == NULL || (dev->flags&IFF_LOOPBACK))
  1116. goto out;
  1117. }
  1118. err = -ENODEV;
  1119. if (dev == NULL)
  1120. goto out;
  1121. if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
  1122. rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
  1123. if (IS_ERR(rt->rt6i_nexthop)) {
  1124. err = PTR_ERR(rt->rt6i_nexthop);
  1125. rt->rt6i_nexthop = NULL;
  1126. goto out;
  1127. }
  1128. }
  1129. rt->rt6i_flags = cfg->fc_flags;
  1130. install_route:
  1131. if (cfg->fc_mx) {
  1132. struct nlattr *nla;
  1133. int remaining;
  1134. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  1135. int type = nla_type(nla);
  1136. if (type) {
  1137. if (type > RTAX_MAX) {
  1138. err = -EINVAL;
  1139. goto out;
  1140. }
  1141. dst_metric_set(&rt->dst, type, nla_get_u32(nla));
  1142. }
  1143. }
  1144. }
  1145. rt->dst.dev = dev;
  1146. rt->rt6i_idev = idev;
  1147. rt->rt6i_table = table;
  1148. cfg->fc_nlinfo.nl_net = dev_net(dev);
  1149. return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
  1150. out:
  1151. if (dev)
  1152. dev_put(dev);
  1153. if (idev)
  1154. in6_dev_put(idev);
  1155. if (rt)
  1156. dst_free(&rt->dst);
  1157. return err;
  1158. }
  1159. static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
  1160. {
  1161. int err;
  1162. struct fib6_table *table;
  1163. struct net *net = dev_net(rt->rt6i_dev);
  1164. if (rt == net->ipv6.ip6_null_entry)
  1165. return -ENOENT;
  1166. table = rt->rt6i_table;
  1167. write_lock_bh(&table->tb6_lock);
  1168. err = fib6_del(rt, info);
  1169. dst_release(&rt->dst);
  1170. write_unlock_bh(&table->tb6_lock);
  1171. return err;
  1172. }
  1173. int ip6_del_rt(struct rt6_info *rt)
  1174. {
  1175. struct nl_info info = {
  1176. .nl_net = dev_net(rt->rt6i_dev),
  1177. };
  1178. return __ip6_del_rt(rt, &info);
  1179. }
  1180. static int ip6_route_del(struct fib6_config *cfg)
  1181. {
  1182. struct fib6_table *table;
  1183. struct fib6_node *fn;
  1184. struct rt6_info *rt;
  1185. int err = -ESRCH;
  1186. table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
  1187. if (table == NULL)
  1188. return err;
  1189. read_lock_bh(&table->tb6_lock);
  1190. fn = fib6_locate(&table->tb6_root,
  1191. &cfg->fc_dst, cfg->fc_dst_len,
  1192. &cfg->fc_src, cfg->fc_src_len);
  1193. if (fn) {
  1194. for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
  1195. if (cfg->fc_ifindex &&
  1196. (rt->rt6i_dev == NULL ||
  1197. rt->rt6i_dev->ifindex != cfg->fc_ifindex))
  1198. continue;
  1199. if (cfg->fc_flags & RTF_GATEWAY &&
  1200. !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
  1201. continue;
  1202. if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
  1203. continue;
  1204. dst_hold(&rt->dst);
  1205. read_unlock_bh(&table->tb6_lock);
  1206. return __ip6_del_rt(rt, &cfg->fc_nlinfo);
  1207. }
  1208. }
  1209. read_unlock_bh(&table->tb6_lock);
  1210. return err;
  1211. }
  1212. /*
  1213. * Handle redirects
  1214. */
  1215. struct ip6rd_flowi {
  1216. struct flowi6 fl6;
  1217. struct in6_addr gateway;
  1218. };
  1219. static struct rt6_info *__ip6_route_redirect(struct net *net,
  1220. struct fib6_table *table,
  1221. struct flowi6 *fl6,
  1222. int flags)
  1223. {
  1224. struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
  1225. struct rt6_info *rt;
  1226. struct fib6_node *fn;
  1227. /*
  1228. * Get the "current" route for this destination and
  1229. * check if the redirect has come from approriate router.
  1230. *
  1231. * RFC 2461 specifies that redirects should only be
  1232. * accepted if they come from the nexthop to the target.
  1233. * Due to the way the routes are chosen, this notion
  1234. * is a bit fuzzy and one might need to check all possible
  1235. * routes.
  1236. */
  1237. read_lock_bh(&table->tb6_lock);
  1238. fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  1239. restart:
  1240. for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
  1241. /*
  1242. * Current route is on-link; redirect is always invalid.
  1243. *
  1244. * Seems, previous statement is not true. It could
  1245. * be node, which looks for us as on-link (f.e. proxy ndisc)
  1246. * But then router serving it might decide, that we should
  1247. * know truth 8)8) --ANK (980726).
  1248. */
  1249. if (rt6_check_expired(rt))
  1250. continue;
  1251. if (!(rt->rt6i_flags & RTF_GATEWAY))
  1252. continue;
  1253. if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
  1254. continue;
  1255. if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
  1256. continue;
  1257. break;
  1258. }
  1259. if (!rt)
  1260. rt = net->ipv6.ip6_null_entry;
  1261. BACKTRACK(net, &fl6->saddr);
  1262. out:
  1263. dst_hold(&rt->dst);
  1264. read_unlock_bh(&table->tb6_lock);
  1265. return rt;
  1266. };
  1267. static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
  1268. struct in6_addr *src,
  1269. struct in6_addr *gateway,
  1270. struct net_device *dev)
  1271. {
  1272. int flags = RT6_LOOKUP_F_HAS_SADDR;
  1273. struct net *net = dev_net(dev);
  1274. struct ip6rd_flowi rdfl = {
  1275. .fl6 = {
  1276. .flowi6_oif = dev->ifindex,
  1277. .daddr = *dest,
  1278. .saddr = *src,
  1279. },
  1280. };
  1281. ipv6_addr_copy(&rdfl.gateway, gateway);
  1282. if (rt6_need_strict(dest))
  1283. flags |= RT6_LOOKUP_F_IFACE;
  1284. return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
  1285. flags, __ip6_route_redirect);
  1286. }
  1287. void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
  1288. struct in6_addr *saddr,
  1289. struct neighbour *neigh, u8 *lladdr, int on_link)
  1290. {
  1291. struct rt6_info *rt, *nrt = NULL;
  1292. struct netevent_redirect netevent;
  1293. struct net *net = dev_net(neigh->dev);
  1294. rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
  1295. if (rt == net->ipv6.ip6_null_entry) {
  1296. if (net_ratelimit())
  1297. printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
  1298. "for redirect target\n");
  1299. goto out;
  1300. }
  1301. /*
  1302. * We have finally decided to accept it.
  1303. */
  1304. neigh_update(neigh, lladdr, NUD_STALE,
  1305. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  1306. NEIGH_UPDATE_F_OVERRIDE|
  1307. (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  1308. NEIGH_UPDATE_F_ISROUTER))
  1309. );
  1310. /*
  1311. * Redirect received -> path was valid.
  1312. * Look, redirects are sent only in response to data packets,
  1313. * so that this nexthop apparently is reachable. --ANK
  1314. */
  1315. dst_confirm(&rt->dst);
  1316. /* Duplicate redirect: silently ignore. */
  1317. if (neigh == rt->dst.neighbour)
  1318. goto out;
  1319. nrt = ip6_rt_copy(rt);
  1320. if (nrt == NULL)
  1321. goto out;
  1322. nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
  1323. if (on_link)
  1324. nrt->rt6i_flags &= ~RTF_GATEWAY;
  1325. ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
  1326. nrt->rt6i_dst.plen = 128;
  1327. nrt->dst.flags |= DST_HOST;
  1328. ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
  1329. nrt->rt6i_nexthop = neigh_clone(neigh);
  1330. if (ip6_ins_rt(nrt))
  1331. goto out;
  1332. netevent.old = &rt->dst;
  1333. netevent.new = &nrt->dst;
  1334. call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
  1335. if (rt->rt6i_flags&RTF_CACHE) {
  1336. ip6_del_rt(rt);
  1337. return;
  1338. }
  1339. out:
  1340. dst_release(&rt->dst);
  1341. }
  1342. /*
  1343. * Handle ICMP "packet too big" messages
  1344. * i.e. Path MTU discovery
  1345. */
  1346. static void rt6_do_pmtu_disc(struct in6_addr *daddr, struct in6_addr *saddr,
  1347. struct net *net, u32 pmtu, int ifindex)
  1348. {
  1349. struct rt6_info *rt, *nrt;
  1350. int allfrag = 0;
  1351. again:
  1352. rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
  1353. if (rt == NULL)
  1354. return;
  1355. if (rt6_check_expired(rt)) {
  1356. ip6_del_rt(rt);
  1357. goto again;
  1358. }
  1359. if (pmtu >= dst_mtu(&rt->dst))
  1360. goto out;
  1361. if (pmtu < IPV6_MIN_MTU) {
  1362. /*
  1363. * According to RFC2460, PMTU is set to the IPv6 Minimum Link
  1364. * MTU (1280) and a fragment header should always be included
  1365. * after a node receiving Too Big message reporting PMTU is
  1366. * less than the IPv6 Minimum Link MTU.
  1367. */
  1368. pmtu = IPV6_MIN_MTU;
  1369. allfrag = 1;
  1370. }
  1371. /* New mtu received -> path was valid.
  1372. They are sent only in response to data packets,
  1373. so that this nexthop apparently is reachable. --ANK
  1374. */
  1375. dst_confirm(&rt->dst);
  1376. /* Host route. If it is static, it would be better
  1377. not to override it, but add new one, so that
  1378. when cache entry will expire old pmtu
  1379. would return automatically.
  1380. */
  1381. if (rt->rt6i_flags & RTF_CACHE) {
  1382. dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
  1383. if (allfrag) {
  1384. u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
  1385. features |= RTAX_FEATURE_ALLFRAG;
  1386. dst_metric_set(&rt->dst, RTAX_FEATURES, features);
  1387. }
  1388. dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
  1389. rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
  1390. goto out;
  1391. }
  1392. /* Network route.
  1393. Two cases are possible:
  1394. 1. It is connected route. Action: COW
  1395. 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
  1396. */
  1397. if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
  1398. nrt = rt6_alloc_cow(rt, daddr, saddr);
  1399. else
  1400. nrt = rt6_alloc_clone(rt, daddr);
  1401. if (nrt) {
  1402. dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
  1403. if (allfrag) {
  1404. u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
  1405. features |= RTAX_FEATURE_ALLFRAG;
  1406. dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
  1407. }
  1408. /* According to RFC 1981, detecting PMTU increase shouldn't be
  1409. * happened within 5 mins, the recommended timer is 10 mins.
  1410. * Here this route expiration time is set to ip6_rt_mtu_expires
  1411. * which is 10 mins. After 10 mins the decreased pmtu is expired
  1412. * and detecting PMTU increase will be automatically happened.
  1413. */
  1414. dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
  1415. nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
  1416. ip6_ins_rt(nrt);
  1417. }
  1418. out:
  1419. dst_release(&rt->dst);
  1420. }
  1421. void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
  1422. struct net_device *dev, u32 pmtu)
  1423. {
  1424. struct net *net = dev_net(dev);
  1425. /*
  1426. * RFC 1981 states that a node "MUST reduce the size of the packets it
  1427. * is sending along the path" that caused the Packet Too Big message.
  1428. * Since it's not possible in the general case to determine which
  1429. * interface was used to send the original packet, we update the MTU
  1430. * on the interface that will be used to send future packets. We also
  1431. * update the MTU on the interface that received the Packet Too Big in
  1432. * case the original packet was forced out that interface with
  1433. * SO_BINDTODEVICE or similar. This is the next best thing to the
  1434. * correct behaviour, which would be to update the MTU on all
  1435. * interfaces.
  1436. */
  1437. rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
  1438. rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
  1439. }
  1440. /*
  1441. * Misc support functions
  1442. */
  1443. static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
  1444. {
  1445. struct net *net = dev_net(ort->rt6i_dev);
  1446. struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
  1447. if (rt) {
  1448. rt->dst.input = ort->dst.input;
  1449. rt->dst.output = ort->dst.output;
  1450. dst_copy_metrics(&rt->dst, &ort->dst);
  1451. rt->dst.error = ort->dst.error;
  1452. rt->dst.dev = ort->dst.dev;
  1453. if (rt->dst.dev)
  1454. dev_hold(rt->dst.dev);
  1455. rt->rt6i_idev = ort->rt6i_idev;
  1456. if (rt->rt6i_idev)
  1457. in6_dev_hold(rt->rt6i_idev);
  1458. rt->dst.lastuse = jiffies;
  1459. rt->rt6i_expires = 0;
  1460. ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
  1461. rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
  1462. rt->rt6i_metric = 0;
  1463. memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
  1464. #ifdef CONFIG_IPV6_SUBTREES
  1465. memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
  1466. #endif
  1467. rt->rt6i_table = ort->rt6i_table;
  1468. }
  1469. return rt;
  1470. }
  1471. #ifdef CONFIG_IPV6_ROUTE_INFO
  1472. static struct rt6_info *rt6_get_route_info(struct net *net,
  1473. struct in6_addr *prefix, int prefixlen,
  1474. struct in6_addr *gwaddr, int ifindex)
  1475. {
  1476. struct fib6_node *fn;
  1477. struct rt6_info *rt = NULL;
  1478. struct fib6_table *table;
  1479. table = fib6_get_table(net, RT6_TABLE_INFO);
  1480. if (table == NULL)
  1481. return NULL;
  1482. write_lock_bh(&table->tb6_lock);
  1483. fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
  1484. if (!fn)
  1485. goto out;
  1486. for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
  1487. if (rt->rt6i_dev->ifindex != ifindex)
  1488. continue;
  1489. if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
  1490. continue;
  1491. if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
  1492. continue;
  1493. dst_hold(&rt->dst);
  1494. break;
  1495. }
  1496. out:
  1497. write_unlock_bh(&table->tb6_lock);
  1498. return rt;
  1499. }
  1500. static struct rt6_info *rt6_add_route_info(struct net *net,
  1501. struct in6_addr *prefix, int prefixlen,
  1502. struct in6_addr *gwaddr, int ifindex,
  1503. unsigned pref)
  1504. {
  1505. struct fib6_config cfg = {
  1506. .fc_table = RT6_TABLE_INFO,
  1507. .fc_metric = IP6_RT_PRIO_USER,
  1508. .fc_ifindex = ifindex,
  1509. .fc_dst_len = prefixlen,
  1510. .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
  1511. RTF_UP | RTF_PREF(pref),
  1512. .fc_nlinfo.pid = 0,
  1513. .fc_nlinfo.nlh = NULL,
  1514. .fc_nlinfo.nl_net = net,
  1515. };
  1516. ipv6_addr_copy(&cfg.fc_dst, prefix);
  1517. ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
  1518. /* We should treat it as a default route if prefix length is 0. */
  1519. if (!prefixlen)
  1520. cfg.fc_flags |= RTF_DEFAULT;
  1521. ip6_route_add(&cfg);
  1522. return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
  1523. }
  1524. #endif
  1525. struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
  1526. {
  1527. struct rt6_info *rt;
  1528. struct fib6_table *table;
  1529. table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
  1530. if (table == NULL)
  1531. return NULL;
  1532. write_lock_bh(&table->tb6_lock);
  1533. for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
  1534. if (dev == rt->rt6i_dev &&
  1535. ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
  1536. ipv6_addr_equal(&rt->rt6i_gateway, addr))
  1537. break;
  1538. }
  1539. if (rt)
  1540. dst_hold(&rt->dst);
  1541. write_unlock_bh(&table->tb6_lock);
  1542. return rt;
  1543. }
  1544. struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
  1545. struct net_device *dev,
  1546. unsigned int pref)
  1547. {
  1548. struct fib6_config cfg = {
  1549. .fc_table = RT6_TABLE_DFLT,
  1550. .fc_metric = IP6_RT_PRIO_USER,
  1551. .fc_ifindex = dev->ifindex,
  1552. .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
  1553. RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
  1554. .fc_nlinfo.pid = 0,
  1555. .fc_nlinfo.nlh = NULL,
  1556. .fc_nlinfo.nl_net = dev_net(dev),
  1557. };
  1558. ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
  1559. ip6_route_add(&cfg);
  1560. return rt6_get_dflt_router(gwaddr, dev);
  1561. }
  1562. void rt6_purge_dflt_routers(struct net *net)
  1563. {
  1564. struct rt6_info *rt;
  1565. struct fib6_table *table;
  1566. /* NOTE: Keep consistent with rt6_get_dflt_router */
  1567. table = fib6_get_table(net, RT6_TABLE_DFLT);
  1568. if (table == NULL)
  1569. return;
  1570. restart:
  1571. read_lock_bh(&table->tb6_lock);
  1572. for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
  1573. if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
  1574. dst_hold(&rt->dst);
  1575. read_unlock_bh(&table->tb6_lock);
  1576. ip6_del_rt(rt);
  1577. goto restart;
  1578. }
  1579. }
  1580. read_unlock_bh(&table->tb6_lock);
  1581. }
  1582. static void rtmsg_to_fib6_config(struct net *net,
  1583. struct in6_rtmsg *rtmsg,
  1584. struct fib6_config *cfg)
  1585. {
  1586. memset(cfg, 0, sizeof(*cfg));
  1587. cfg->fc_table = RT6_TABLE_MAIN;
  1588. cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
  1589. cfg->fc_metric = rtmsg->rtmsg_metric;
  1590. cfg->fc_expires = rtmsg->rtmsg_info;
  1591. cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
  1592. cfg->fc_src_len = rtmsg->rtmsg_src_len;
  1593. cfg->fc_flags = rtmsg->rtmsg_flags;
  1594. cfg->fc_nlinfo.nl_net = net;
  1595. ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
  1596. ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
  1597. ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
  1598. }
  1599. int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  1600. {
  1601. struct fib6_config cfg;
  1602. struct in6_rtmsg rtmsg;
  1603. int err;
  1604. switch(cmd) {
  1605. case SIOCADDRT: /* Add a route */
  1606. case SIOCDELRT: /* Delete a route */
  1607. if (!capable(CAP_NET_ADMIN))
  1608. return -EPERM;
  1609. err = copy_from_user(&rtmsg, arg,
  1610. sizeof(struct in6_rtmsg));
  1611. if (err)
  1612. return -EFAULT;
  1613. rtmsg_to_fib6_config(net, &rtmsg, &cfg);
  1614. rtnl_lock();
  1615. switch (cmd) {
  1616. case SIOCADDRT:
  1617. err = ip6_route_add(&cfg);
  1618. break;
  1619. case SIOCDELRT:
  1620. err = ip6_route_del(&cfg);
  1621. break;
  1622. default:
  1623. err = -EINVAL;
  1624. }
  1625. rtnl_unlock();
  1626. return err;
  1627. }
  1628. return -EINVAL;
  1629. }
  1630. /*
  1631. * Drop the packet on the floor
  1632. */
  1633. static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
  1634. {
  1635. int type;
  1636. struct dst_entry *dst = skb_dst(skb);
  1637. switch (ipstats_mib_noroutes) {
  1638. case IPSTATS_MIB_INNOROUTES:
  1639. type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
  1640. if (type == IPV6_ADDR_ANY) {
  1641. IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
  1642. IPSTATS_MIB_INADDRERRORS);
  1643. break;
  1644. }
  1645. /* FALLTHROUGH */
  1646. case IPSTATS_MIB_OUTNOROUTES:
  1647. IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
  1648. ipstats_mib_noroutes);
  1649. break;
  1650. }
  1651. icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
  1652. kfree_skb(skb);
  1653. return 0;
  1654. }
  1655. static int ip6_pkt_discard(struct sk_buff *skb)
  1656. {
  1657. return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
  1658. }
  1659. static int ip6_pkt_discard_out(struct sk_buff *skb)
  1660. {
  1661. skb->dev = skb_dst(skb)->dev;
  1662. return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
  1663. }
  1664. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1665. static int ip6_pkt_prohibit(struct sk_buff *skb)
  1666. {
  1667. return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
  1668. }
  1669. static int ip6_pkt_prohibit_out(struct sk_buff *skb)
  1670. {
  1671. skb->dev = skb_dst(skb)->dev;
  1672. return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
  1673. }
  1674. #endif
  1675. /*
  1676. * Allocate a dst for local (unicast / anycast) address.
  1677. */
  1678. struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
  1679. const struct in6_addr *addr,
  1680. int anycast)
  1681. {
  1682. struct net *net = dev_net(idev->dev);
  1683. struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
  1684. struct neighbour *neigh;
  1685. if (rt == NULL) {
  1686. if (net_ratelimit())
  1687. pr_warning("IPv6: Maximum number of routes reached,"
  1688. " consider increasing route/max_size.\n");
  1689. return ERR_PTR(-ENOMEM);
  1690. }
  1691. dev_hold(net->loopback_dev);
  1692. in6_dev_hold(idev);
  1693. rt->dst.flags = DST_HOST;
  1694. rt->dst.input = ip6_input;
  1695. rt->dst.output = ip6_output;
  1696. rt->rt6i_dev = net->loopback_dev;
  1697. rt->rt6i_idev = idev;
  1698. dst_metric_set(&rt->dst, RTAX_HOPLIMIT, -1);
  1699. rt->dst.obsolete = -1;
  1700. rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
  1701. if (anycast)
  1702. rt->rt6i_flags |= RTF_ANYCAST;
  1703. else
  1704. rt->rt6i_flags |= RTF_LOCAL;
  1705. neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
  1706. if (IS_ERR(neigh)) {
  1707. dst_free(&rt->dst);
  1708. return ERR_CAST(neigh);
  1709. }
  1710. rt->rt6i_nexthop = neigh;
  1711. ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
  1712. rt->rt6i_dst.plen = 128;
  1713. rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
  1714. atomic_set(&rt->dst.__refcnt, 1);
  1715. return rt;
  1716. }
  1717. struct arg_dev_net {
  1718. struct net_device *dev;
  1719. struct net *net;
  1720. };
  1721. static int fib6_ifdown(struct rt6_info *rt, void *arg)
  1722. {
  1723. const struct arg_dev_net *adn = arg;
  1724. const struct net_device *dev = adn->dev;
  1725. if ((rt->rt6i_dev == dev || dev == NULL) &&
  1726. rt != adn->net->ipv6.ip6_null_entry) {
  1727. RT6_TRACE("deleted by ifdown %p\n", rt);
  1728. return -1;
  1729. }
  1730. return 0;
  1731. }
  1732. void rt6_ifdown(struct net *net, struct net_device *dev)
  1733. {
  1734. struct arg_dev_net adn = {
  1735. .dev = dev,
  1736. .net = net,
  1737. };
  1738. fib6_clean_all(net, fib6_ifdown, 0, &adn);
  1739. icmp6_clean_all(fib6_ifdown, &adn);
  1740. }
  1741. struct rt6_mtu_change_arg
  1742. {
  1743. struct net_device *dev;
  1744. unsigned mtu;
  1745. };
  1746. static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
  1747. {
  1748. struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
  1749. struct inet6_dev *idev;
  1750. /* In IPv6 pmtu discovery is not optional,
  1751. so that RTAX_MTU lock cannot disable it.
  1752. We still use this lock to block changes
  1753. caused by addrconf/ndisc.
  1754. */
  1755. idev = __in6_dev_get(arg->dev);
  1756. if (idev == NULL)
  1757. return 0;
  1758. /* For administrative MTU increase, there is no way to discover
  1759. IPv6 PMTU increase, so PMTU increase should be updated here.
  1760. Since RFC 1981 doesn't include administrative MTU increase
  1761. update PMTU increase is a MUST. (i.e. jumbo frame)
  1762. */
  1763. /*
  1764. If new MTU is less than route PMTU, this new MTU will be the
  1765. lowest MTU in the path, update the route PMTU to reflect PMTU
  1766. decreases; if new MTU is greater than route PMTU, and the
  1767. old MTU is the lowest MTU in the path, update the route PMTU
  1768. to reflect the increase. In this case if the other nodes' MTU
  1769. also have the lowest MTU, TOO BIG MESSAGE will be lead to
  1770. PMTU discouvery.
  1771. */
  1772. if (rt->rt6i_dev == arg->dev &&
  1773. !dst_metric_locked(&rt->dst, RTAX_MTU) &&
  1774. (dst_mtu(&rt->dst) >= arg->mtu ||
  1775. (dst_mtu(&rt->dst) < arg->mtu &&
  1776. dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
  1777. dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
  1778. }
  1779. return 0;
  1780. }
  1781. void rt6_mtu_change(struct net_device *dev, unsigned mtu)
  1782. {
  1783. struct rt6_mtu_change_arg arg = {
  1784. .dev = dev,
  1785. .mtu = mtu,
  1786. };
  1787. fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
  1788. }
  1789. static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
  1790. [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
  1791. [RTA_OIF] = { .type = NLA_U32 },
  1792. [RTA_IIF] = { .type = NLA_U32 },
  1793. [RTA_PRIORITY] = { .type = NLA_U32 },
  1794. [RTA_METRICS] = { .type = NLA_NESTED },
  1795. };
  1796. static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  1797. struct fib6_config *cfg)
  1798. {
  1799. struct rtmsg *rtm;
  1800. struct nlattr *tb[RTA_MAX+1];
  1801. int err;
  1802. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
  1803. if (err < 0)
  1804. goto errout;
  1805. err = -EINVAL;
  1806. rtm = nlmsg_data(nlh);
  1807. memset(cfg, 0, sizeof(*cfg));
  1808. cfg->fc_table = rtm->rtm_table;
  1809. cfg->fc_dst_len = rtm->rtm_dst_len;
  1810. cfg->fc_src_len = rtm->rtm_src_len;
  1811. cfg->fc_flags = RTF_UP;
  1812. cfg->fc_protocol = rtm->rtm_protocol;
  1813. if (rtm->rtm_type == RTN_UNREACHABLE)
  1814. cfg->fc_flags |= RTF_REJECT;
  1815. if (rtm->rtm_type == RTN_LOCAL)
  1816. cfg->fc_flags |= RTF_LOCAL;
  1817. cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
  1818. cfg->fc_nlinfo.nlh = nlh;
  1819. cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
  1820. if (tb[RTA_GATEWAY]) {
  1821. nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
  1822. cfg->fc_flags |= RTF_GATEWAY;
  1823. }
  1824. if (tb[RTA_DST]) {
  1825. int plen = (rtm->rtm_dst_len + 7) >> 3;
  1826. if (nla_len(tb[RTA_DST]) < plen)
  1827. goto errout;
  1828. nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
  1829. }
  1830. if (tb[RTA_SRC]) {
  1831. int plen = (rtm->rtm_src_len + 7) >> 3;
  1832. if (nla_len(tb[RTA_SRC]) < plen)
  1833. goto errout;
  1834. nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
  1835. }
  1836. if (tb[RTA_OIF])
  1837. cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
  1838. if (tb[RTA_PRIORITY])
  1839. cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
  1840. if (tb[RTA_METRICS]) {
  1841. cfg->fc_mx = nla_data(tb[RTA_METRICS]);
  1842. cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
  1843. }
  1844. if (tb[RTA_TABLE])
  1845. cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
  1846. err = 0;
  1847. errout:
  1848. return err;
  1849. }
  1850. static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  1851. {
  1852. struct fib6_config cfg;
  1853. int err;
  1854. err = rtm_to_fib6_config(skb, nlh, &cfg);
  1855. if (err < 0)
  1856. return err;
  1857. return ip6_route_del(&cfg);
  1858. }
  1859. static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  1860. {
  1861. struct fib6_config cfg;
  1862. int err;
  1863. err = rtm_to_fib6_config(skb, nlh, &cfg);
  1864. if (err < 0)
  1865. return err;
  1866. return ip6_route_add(&cfg);
  1867. }
  1868. static inline size_t rt6_nlmsg_size(void)
  1869. {
  1870. return NLMSG_ALIGN(sizeof(struct rtmsg))
  1871. + nla_total_size(16) /* RTA_SRC */
  1872. + nla_total_size(16) /* RTA_DST */
  1873. + nla_total_size(16) /* RTA_GATEWAY */
  1874. + nla_total_size(16) /* RTA_PREFSRC */
  1875. + nla_total_size(4) /* RTA_TABLE */
  1876. + nla_total_size(4) /* RTA_IIF */
  1877. + nla_total_size(4) /* RTA_OIF */
  1878. + nla_total_size(4) /* RTA_PRIORITY */
  1879. + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
  1880. + nla_total_size(sizeof(struct rta_cacheinfo));
  1881. }
  1882. static int rt6_fill_node(struct net *net,
  1883. struct sk_buff *skb, struct rt6_info *rt,
  1884. struct in6_addr *dst, struct in6_addr *src,
  1885. int iif, int type, u32 pid, u32 seq,
  1886. int prefix, int nowait, unsigned int flags)
  1887. {
  1888. struct rtmsg *rtm;
  1889. struct nlmsghdr *nlh;
  1890. long expires;
  1891. u32 table;
  1892. if (prefix) { /* user wants prefix routes only */
  1893. if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
  1894. /* success since this is not a prefix route */
  1895. return 1;
  1896. }
  1897. }
  1898. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
  1899. if (nlh == NULL)
  1900. return -EMSGSIZE;
  1901. rtm = nlmsg_data(nlh);
  1902. rtm->rtm_family = AF_INET6;
  1903. rtm->rtm_dst_len = rt->rt6i_dst.plen;
  1904. rtm->rtm_src_len = rt->rt6i_src.plen;
  1905. rtm->rtm_tos = 0;
  1906. if (rt->rt6i_table)
  1907. table = rt->rt6i_table->tb6_id;
  1908. else
  1909. table = RT6_TABLE_UNSPEC;
  1910. rtm->rtm_table = table;
  1911. NLA_PUT_U32(skb, RTA_TABLE, table);
  1912. if (rt->rt6i_flags&RTF_REJECT)
  1913. rtm->rtm_type = RTN_UNREACHABLE;
  1914. else if (rt->rt6i_flags&RTF_LOCAL)
  1915. rtm->rtm_type = RTN_LOCAL;
  1916. else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
  1917. rtm->rtm_type = RTN_LOCAL;
  1918. else
  1919. rtm->rtm_type = RTN_UNICAST;
  1920. rtm->rtm_flags = 0;
  1921. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1922. rtm->rtm_protocol = rt->rt6i_protocol;
  1923. if (rt->rt6i_flags&RTF_DYNAMIC)
  1924. rtm->rtm_protocol = RTPROT_REDIRECT;
  1925. else if (rt->rt6i_flags & RTF_ADDRCONF)
  1926. rtm->rtm_protocol = RTPROT_KERNEL;
  1927. else if (rt->rt6i_flags&RTF_DEFAULT)
  1928. rtm->rtm_protocol = RTPROT_RA;
  1929. if (rt->rt6i_flags&RTF_CACHE)
  1930. rtm->rtm_flags |= RTM_F_CLONED;
  1931. if (dst) {
  1932. NLA_PUT(skb, RTA_DST, 16, dst);
  1933. rtm->rtm_dst_len = 128;
  1934. } else if (rtm->rtm_dst_len)
  1935. NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
  1936. #ifdef CONFIG_IPV6_SUBTREES
  1937. if (src) {
  1938. NLA_PUT(skb, RTA_SRC, 16, src);
  1939. rtm->rtm_src_len = 128;
  1940. } else if (rtm->rtm_src_len)
  1941. NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
  1942. #endif
  1943. if (iif) {
  1944. #ifdef CONFIG_IPV6_MROUTE
  1945. if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
  1946. int err = ip6mr_get_route(net, skb, rtm, nowait);
  1947. if (err <= 0) {
  1948. if (!nowait) {
  1949. if (err == 0)
  1950. return 0;
  1951. goto nla_put_failure;
  1952. } else {
  1953. if (err == -EMSGSIZE)
  1954. goto nla_put_failure;
  1955. }
  1956. }
  1957. } else
  1958. #endif
  1959. NLA_PUT_U32(skb, RTA_IIF, iif);
  1960. } else if (dst) {
  1961. struct inet6_dev *idev = ip6_dst_idev(&rt->dst);
  1962. struct in6_addr saddr_buf;
  1963. if (ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
  1964. dst, 0, &saddr_buf) == 0)
  1965. NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
  1966. }
  1967. if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
  1968. goto nla_put_failure;
  1969. if (rt->dst.neighbour)
  1970. NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
  1971. if (rt->dst.dev)
  1972. NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
  1973. NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
  1974. if (!(rt->rt6i_flags & RTF_EXPIRES))
  1975. expires = 0;
  1976. else if (rt->rt6i_expires - jiffies < INT_MAX)
  1977. expires = rt->rt6i_expires - jiffies;
  1978. else
  1979. expires = INT_MAX;
  1980. if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
  1981. expires, rt->dst.error) < 0)
  1982. goto nla_put_failure;
  1983. return nlmsg_end(skb, nlh);
  1984. nla_put_failure:
  1985. nlmsg_cancel(skb, nlh);
  1986. return -EMSGSIZE;
  1987. }
  1988. int rt6_dump_route(struct rt6_info *rt, void *p_arg)
  1989. {
  1990. struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
  1991. int prefix;
  1992. if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
  1993. struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
  1994. prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
  1995. } else
  1996. prefix = 0;
  1997. return rt6_fill_node(arg->net,
  1998. arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
  1999. NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
  2000. prefix, 0, NLM_F_MULTI);
  2001. }
  2002. static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
  2003. {
  2004. struct net *net = sock_net(in_skb->sk);
  2005. struct nlattr *tb[RTA_MAX+1];
  2006. struct rt6_info *rt;
  2007. struct sk_buff *skb;
  2008. struct rtmsg *rtm;
  2009. struct flowi6 fl6;
  2010. int err, iif = 0;
  2011. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
  2012. if (err < 0)
  2013. goto errout;
  2014. err = -EINVAL;
  2015. memset(&fl6, 0, sizeof(fl6));
  2016. if (tb[RTA_SRC]) {
  2017. if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
  2018. goto errout;
  2019. ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC]));
  2020. }
  2021. if (tb[RTA_DST]) {
  2022. if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
  2023. goto errout;
  2024. ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST]));
  2025. }
  2026. if (tb[RTA_IIF])
  2027. iif = nla_get_u32(tb[RTA_IIF]);
  2028. if (tb[RTA_OIF])
  2029. fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
  2030. if (iif) {
  2031. struct net_device *dev;
  2032. dev = __dev_get_by_index(net, iif);
  2033. if (!dev) {
  2034. err = -ENODEV;
  2035. goto errout;
  2036. }
  2037. }
  2038. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2039. if (skb == NULL) {
  2040. err = -ENOBUFS;
  2041. goto errout;
  2042. }
  2043. /* Reserve room for dummy headers, this skb can pass
  2044. through good chunk of routing engine.
  2045. */
  2046. skb_reset_mac_header(skb);
  2047. skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
  2048. rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
  2049. skb_dst_set(skb, &rt->dst);
  2050. err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
  2051. RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
  2052. nlh->nlmsg_seq, 0, 0, 0);
  2053. if (err < 0) {
  2054. kfree_skb(skb);
  2055. goto errout;
  2056. }
  2057. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
  2058. errout:
  2059. return err;
  2060. }
  2061. void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
  2062. {
  2063. struct sk_buff *skb;
  2064. struct net *net = info->nl_net;
  2065. u32 seq;
  2066. int err;
  2067. err = -ENOBUFS;
  2068. seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
  2069. skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
  2070. if (skb == NULL)
  2071. goto errout;
  2072. err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
  2073. event, info->pid, seq, 0, 0, 0);
  2074. if (err < 0) {
  2075. /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
  2076. WARN_ON(err == -EMSGSIZE);
  2077. kfree_skb(skb);
  2078. goto errout;
  2079. }
  2080. rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
  2081. info->nlh, gfp_any());
  2082. return;
  2083. errout:
  2084. if (err < 0)
  2085. rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
  2086. }
  2087. static int ip6_route_dev_notify(struct notifier_block *this,
  2088. unsigned long event, void *data)
  2089. {
  2090. struct net_device *dev = (struct net_device *)data;
  2091. struct net *net = dev_net(dev);
  2092. if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
  2093. net->ipv6.ip6_null_entry->dst.dev = dev;
  2094. net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
  2095. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2096. net->ipv6.ip6_prohibit_entry->dst.dev = dev;
  2097. net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
  2098. net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
  2099. net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
  2100. #endif
  2101. }
  2102. return NOTIFY_OK;
  2103. }
  2104. /*
  2105. * /proc
  2106. */
  2107. #ifdef CONFIG_PROC_FS
  2108. struct rt6_proc_arg
  2109. {
  2110. char *buffer;
  2111. int offset;
  2112. int length;
  2113. int skip;
  2114. int len;
  2115. };
  2116. static int rt6_info_route(struct rt6_info *rt, void *p_arg)
  2117. {
  2118. struct seq_file *m = p_arg;
  2119. seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
  2120. #ifdef CONFIG_IPV6_SUBTREES
  2121. seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
  2122. #else
  2123. seq_puts(m, "00000000000000000000000000000000 00 ");
  2124. #endif
  2125. if (rt->rt6i_nexthop) {
  2126. seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
  2127. } else {
  2128. seq_puts(m, "00000000000000000000000000000000");
  2129. }
  2130. seq_printf(m, " %08x %08x %08x %08x %8s\n",
  2131. rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
  2132. rt->dst.__use, rt->rt6i_flags,
  2133. rt->rt6i_dev ? rt->rt6i_dev->name : "");
  2134. return 0;
  2135. }
  2136. static int ipv6_route_show(struct seq_file *m, void *v)
  2137. {
  2138. struct net *net = (struct net *)m->private;
  2139. fib6_clean_all(net, rt6_info_route, 0, m);
  2140. return 0;
  2141. }
  2142. static int ipv6_route_open(struct inode *inode, struct file *file)
  2143. {
  2144. return single_open_net(inode, file, ipv6_route_show);
  2145. }
  2146. static const struct file_operations ipv6_route_proc_fops = {
  2147. .owner = THIS_MODULE,
  2148. .open = ipv6_route_open,
  2149. .read = seq_read,
  2150. .llseek = seq_lseek,
  2151. .release = single_release_net,
  2152. };
  2153. static int rt6_stats_seq_show(struct seq_file *seq, void *v)
  2154. {
  2155. struct net *net = (struct net *)seq->private;
  2156. seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
  2157. net->ipv6.rt6_stats->fib_nodes,
  2158. net->ipv6.rt6_stats->fib_route_nodes,
  2159. net->ipv6.rt6_stats->fib_rt_alloc,
  2160. net->ipv6.rt6_stats->fib_rt_entries,
  2161. net->ipv6.rt6_stats->fib_rt_cache,
  2162. dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
  2163. net->ipv6.rt6_stats->fib_discarded_routes);
  2164. return 0;
  2165. }
  2166. static int rt6_stats_seq_open(struct inode *inode, struct file *file)
  2167. {
  2168. return single_open_net(inode, file, rt6_stats_seq_show);
  2169. }
  2170. static const struct file_operations rt6_stats_seq_fops = {
  2171. .owner = THIS_MODULE,
  2172. .open = rt6_stats_seq_open,
  2173. .read = seq_read,
  2174. .llseek = seq_lseek,
  2175. .release = single_release_net,
  2176. };
  2177. #endif /* CONFIG_PROC_FS */
  2178. #ifdef CONFIG_SYSCTL
  2179. static
  2180. int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
  2181. void __user *buffer, size_t *lenp, loff_t *ppos)
  2182. {
  2183. struct net *net;
  2184. int delay;
  2185. if (!write)
  2186. return -EINVAL;
  2187. net = (struct net *)ctl->extra1;
  2188. delay = net->ipv6.sysctl.flush_delay;
  2189. proc_dointvec(ctl, write, buffer, lenp, ppos);
  2190. fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
  2191. return 0;
  2192. }
  2193. ctl_table ipv6_route_table_template[] = {
  2194. {
  2195. .procname = "flush",
  2196. .data = &init_net.ipv6.sysctl.flush_delay,
  2197. .maxlen = sizeof(int),
  2198. .mode = 0200,
  2199. .proc_handler = ipv6_sysctl_rtcache_flush
  2200. },
  2201. {
  2202. .procname = "gc_thresh",
  2203. .data = &ip6_dst_ops_template.gc_thresh,
  2204. .maxlen = sizeof(int),
  2205. .mode = 0644,
  2206. .proc_handler = proc_dointvec,
  2207. },
  2208. {
  2209. .procname = "max_size",
  2210. .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
  2211. .maxlen = sizeof(int),
  2212. .mode = 0644,
  2213. .proc_handler = proc_dointvec,
  2214. },
  2215. {
  2216. .procname = "gc_min_interval",
  2217. .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
  2218. .maxlen = sizeof(int),
  2219. .mode = 0644,
  2220. .proc_handler = proc_dointvec_jiffies,
  2221. },
  2222. {
  2223. .procname = "gc_timeout",
  2224. .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
  2225. .maxlen = sizeof(int),
  2226. .mode = 0644,
  2227. .proc_handler = proc_dointvec_jiffies,
  2228. },
  2229. {
  2230. .procname = "gc_interval",
  2231. .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
  2232. .maxlen = sizeof(int),
  2233. .mode = 0644,
  2234. .proc_handler = proc_dointvec_jiffies,
  2235. },
  2236. {
  2237. .procname = "gc_elasticity",
  2238. .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
  2239. .maxlen = sizeof(int),
  2240. .mode = 0644,
  2241. .proc_handler = proc_dointvec,
  2242. },
  2243. {
  2244. .procname = "mtu_expires",
  2245. .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
  2246. .maxlen = sizeof(int),
  2247. .mode = 0644,
  2248. .proc_handler = proc_dointvec_jiffies,
  2249. },
  2250. {
  2251. .procname = "min_adv_mss",
  2252. .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
  2253. .maxlen = sizeof(int),
  2254. .mode = 0644,
  2255. .proc_handler = proc_dointvec,
  2256. },
  2257. {
  2258. .procname = "gc_min_interval_ms",
  2259. .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
  2260. .maxlen = sizeof(int),
  2261. .mode = 0644,
  2262. .proc_handler = proc_dointvec_ms_jiffies,
  2263. },
  2264. { }
  2265. };
  2266. struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
  2267. {
  2268. struct ctl_table *table;
  2269. table = kmemdup(ipv6_route_table_template,
  2270. sizeof(ipv6_route_table_template),
  2271. GFP_KERNEL);
  2272. if (table) {
  2273. table[0].data = &net->ipv6.sysctl.flush_delay;
  2274. table[0].extra1 = net;
  2275. table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
  2276. table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
  2277. table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
  2278. table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
  2279. table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
  2280. table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
  2281. table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
  2282. table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
  2283. table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
  2284. }
  2285. return table;
  2286. }
  2287. #endif
  2288. static int __net_init ip6_route_net_init(struct net *net)
  2289. {
  2290. int ret = -ENOMEM;
  2291. memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
  2292. sizeof(net->ipv6.ip6_dst_ops));
  2293. if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
  2294. goto out_ip6_dst_ops;
  2295. net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
  2296. sizeof(*net->ipv6.ip6_null_entry),
  2297. GFP_KERNEL);
  2298. if (!net->ipv6.ip6_null_entry)
  2299. goto out_ip6_dst_entries;
  2300. net->ipv6.ip6_null_entry->dst.path =
  2301. (struct dst_entry *)net->ipv6.ip6_null_entry;
  2302. net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  2303. dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
  2304. ip6_template_metrics, true);
  2305. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2306. net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
  2307. sizeof(*net->ipv6.ip6_prohibit_entry),
  2308. GFP_KERNEL);
  2309. if (!net->ipv6.ip6_prohibit_entry)
  2310. goto out_ip6_null_entry;
  2311. net->ipv6.ip6_prohibit_entry->dst.path =
  2312. (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
  2313. net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  2314. dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
  2315. ip6_template_metrics, true);
  2316. net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
  2317. sizeof(*net->ipv6.ip6_blk_hole_entry),
  2318. GFP_KERNEL);
  2319. if (!net->ipv6.ip6_blk_hole_entry)
  2320. goto out_ip6_prohibit_entry;
  2321. net->ipv6.ip6_blk_hole_entry->dst.path =
  2322. (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
  2323. net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  2324. dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
  2325. ip6_template_metrics, true);
  2326. #endif
  2327. net->ipv6.sysctl.flush_delay = 0;
  2328. net->ipv6.sysctl.ip6_rt_max_size = 4096;
  2329. net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
  2330. net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
  2331. net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
  2332. net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
  2333. net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
  2334. net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
  2335. #ifdef CONFIG_PROC_FS
  2336. proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
  2337. proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
  2338. #endif
  2339. net->ipv6.ip6_rt_gc_expire = 30*HZ;
  2340. ret = 0;
  2341. out:
  2342. return ret;
  2343. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2344. out_ip6_prohibit_entry:
  2345. kfree(net->ipv6.ip6_prohibit_entry);
  2346. out_ip6_null_entry:
  2347. kfree(net->ipv6.ip6_null_entry);
  2348. #endif
  2349. out_ip6_dst_entries:
  2350. dst_entries_destroy(&net->ipv6.ip6_dst_ops);
  2351. out_ip6_dst_ops:
  2352. goto out;
  2353. }
  2354. static void __net_exit ip6_route_net_exit(struct net *net)
  2355. {
  2356. #ifdef CONFIG_PROC_FS
  2357. proc_net_remove(net, "ipv6_route");
  2358. proc_net_remove(net, "rt6_stats");
  2359. #endif
  2360. kfree(net->ipv6.ip6_null_entry);
  2361. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2362. kfree(net->ipv6.ip6_prohibit_entry);
  2363. kfree(net->ipv6.ip6_blk_hole_entry);
  2364. #endif
  2365. dst_entries_destroy(&net->ipv6.ip6_dst_ops);
  2366. }
  2367. static struct pernet_operations ip6_route_net_ops = {
  2368. .init = ip6_route_net_init,
  2369. .exit = ip6_route_net_exit,
  2370. };
  2371. static struct notifier_block ip6_route_dev_notifier = {
  2372. .notifier_call = ip6_route_dev_notify,
  2373. .priority = 0,
  2374. };
  2375. int __init ip6_route_init(void)
  2376. {
  2377. int ret;
  2378. ret = -ENOMEM;
  2379. ip6_dst_ops_template.kmem_cachep =
  2380. kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
  2381. SLAB_HWCACHE_ALIGN, NULL);
  2382. if (!ip6_dst_ops_template.kmem_cachep)
  2383. goto out;
  2384. ret = dst_entries_init(&ip6_dst_blackhole_ops);
  2385. if (ret)
  2386. goto out_kmem_cache;
  2387. ret = register_pernet_subsys(&ip6_route_net_ops);
  2388. if (ret)
  2389. goto out_dst_entries;
  2390. ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
  2391. /* Registering of the loopback is done before this portion of code,
  2392. * the loopback reference in rt6_info will not be taken, do it
  2393. * manually for init_net */
  2394. init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
  2395. init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  2396. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2397. init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
  2398. init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  2399. init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
  2400. init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  2401. #endif
  2402. ret = fib6_init();
  2403. if (ret)
  2404. goto out_register_subsys;
  2405. ret = xfrm6_init();
  2406. if (ret)
  2407. goto out_fib6_init;
  2408. ret = fib6_rules_init();
  2409. if (ret)
  2410. goto xfrm6_init;
  2411. ret = -ENOBUFS;
  2412. if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
  2413. __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
  2414. __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
  2415. goto fib6_rules_init;
  2416. ret = register_netdevice_notifier(&ip6_route_dev_notifier);
  2417. if (ret)
  2418. goto fib6_rules_init;
  2419. out:
  2420. return ret;
  2421. fib6_rules_init:
  2422. fib6_rules_cleanup();
  2423. xfrm6_init:
  2424. xfrm6_fini();
  2425. out_fib6_init:
  2426. fib6_gc_cleanup();
  2427. out_register_subsys:
  2428. unregister_pernet_subsys(&ip6_route_net_ops);
  2429. out_dst_entries:
  2430. dst_entries_destroy(&ip6_dst_blackhole_ops);
  2431. out_kmem_cache:
  2432. kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
  2433. goto out;
  2434. }
  2435. void ip6_route_cleanup(void)
  2436. {
  2437. unregister_netdevice_notifier(&ip6_route_dev_notifier);
  2438. fib6_rules_cleanup();
  2439. xfrm6_fini();
  2440. fib6_gc_cleanup();
  2441. unregister_pernet_subsys(&ip6_route_net_ops);
  2442. dst_entries_destroy(&ip6_dst_blackhole_ops);
  2443. kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
  2444. }