route.c 71 KB

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