route.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  1. /*
  2. * Linux INET6 implementation
  3. * FIB front-end.
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * $Id: route.c,v 1.56 2001/10/31 21:55:55 davem Exp $
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. /* Changes:
  16. *
  17. * YOSHIFUJI Hideaki @USAGI
  18. * reworked default router selection.
  19. * - respect outgoing interface
  20. * - select from (probably) reachable routers (i.e.
  21. * routers in REACHABLE, STALE, DELAY or PROBE states).
  22. * - always select the same router if it is (probably)
  23. * reachable. otherwise, round-robin the list.
  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/init.h>
  36. #include <linux/netlink.h>
  37. #include <linux/if_arp.h>
  38. #ifdef CONFIG_PROC_FS
  39. #include <linux/proc_fs.h>
  40. #include <linux/seq_file.h>
  41. #endif
  42. #include <net/snmp.h>
  43. #include <net/ipv6.h>
  44. #include <net/ip6_fib.h>
  45. #include <net/ip6_route.h>
  46. #include <net/ndisc.h>
  47. #include <net/addrconf.h>
  48. #include <net/tcp.h>
  49. #include <linux/rtnetlink.h>
  50. #include <net/dst.h>
  51. #include <net/xfrm.h>
  52. #include <net/netevent.h>
  53. #include <asm/uaccess.h>
  54. #ifdef CONFIG_SYSCTL
  55. #include <linux/sysctl.h>
  56. #endif
  57. /* Set to 3 to get tracing. */
  58. #define RT6_DEBUG 2
  59. #if RT6_DEBUG >= 3
  60. #define RDBG(x) printk x
  61. #define RT6_TRACE(x...) printk(KERN_DEBUG x)
  62. #else
  63. #define RDBG(x)
  64. #define RT6_TRACE(x...) do { ; } while (0)
  65. #endif
  66. #define CLONE_OFFLINK_ROUTE 0
  67. #define RT6_SELECT_F_IFACE 0x1
  68. #define RT6_SELECT_F_REACHABLE 0x2
  69. static int ip6_rt_max_size = 4096;
  70. static int ip6_rt_gc_min_interval = HZ / 2;
  71. static int ip6_rt_gc_timeout = 60*HZ;
  72. int ip6_rt_gc_interval = 30*HZ;
  73. static int ip6_rt_gc_elasticity = 9;
  74. static int ip6_rt_mtu_expires = 10*60*HZ;
  75. static int ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
  76. static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
  77. static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
  78. static struct dst_entry *ip6_negative_advice(struct dst_entry *);
  79. static void ip6_dst_destroy(struct dst_entry *);
  80. static void ip6_dst_ifdown(struct dst_entry *,
  81. struct net_device *dev, int how);
  82. static int ip6_dst_gc(void);
  83. static int ip6_pkt_discard(struct sk_buff *skb);
  84. static int ip6_pkt_discard_out(struct sk_buff *skb);
  85. static void ip6_link_failure(struct sk_buff *skb);
  86. static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
  87. #ifdef CONFIG_IPV6_ROUTE_INFO
  88. static struct rt6_info *rt6_add_route_info(struct in6_addr *prefix, int prefixlen,
  89. struct in6_addr *gwaddr, int ifindex,
  90. unsigned pref);
  91. static struct rt6_info *rt6_get_route_info(struct in6_addr *prefix, int prefixlen,
  92. struct in6_addr *gwaddr, int ifindex);
  93. #endif
  94. static struct dst_ops ip6_dst_ops = {
  95. .family = AF_INET6,
  96. .protocol = __constant_htons(ETH_P_IPV6),
  97. .gc = ip6_dst_gc,
  98. .gc_thresh = 1024,
  99. .check = ip6_dst_check,
  100. .destroy = ip6_dst_destroy,
  101. .ifdown = ip6_dst_ifdown,
  102. .negative_advice = ip6_negative_advice,
  103. .link_failure = ip6_link_failure,
  104. .update_pmtu = ip6_rt_update_pmtu,
  105. .entry_size = sizeof(struct rt6_info),
  106. };
  107. struct rt6_info ip6_null_entry = {
  108. .u = {
  109. .dst = {
  110. .__refcnt = ATOMIC_INIT(1),
  111. .__use = 1,
  112. .dev = &loopback_dev,
  113. .obsolete = -1,
  114. .error = -ENETUNREACH,
  115. .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
  116. .input = ip6_pkt_discard,
  117. .output = ip6_pkt_discard_out,
  118. .ops = &ip6_dst_ops,
  119. .path = (struct dst_entry*)&ip6_null_entry,
  120. }
  121. },
  122. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  123. .rt6i_metric = ~(u32) 0,
  124. .rt6i_ref = ATOMIC_INIT(1),
  125. };
  126. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  127. struct rt6_info ip6_prohibit_entry = {
  128. .u = {
  129. .dst = {
  130. .__refcnt = ATOMIC_INIT(1),
  131. .__use = 1,
  132. .dev = &loopback_dev,
  133. .obsolete = -1,
  134. .error = -EACCES,
  135. .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
  136. .input = ip6_pkt_discard,
  137. .output = ip6_pkt_discard_out,
  138. .ops = &ip6_dst_ops,
  139. .path = (struct dst_entry*)&ip6_prohibit_entry,
  140. }
  141. },
  142. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  143. .rt6i_metric = ~(u32) 0,
  144. .rt6i_ref = ATOMIC_INIT(1),
  145. };
  146. struct rt6_info ip6_blk_hole_entry = {
  147. .u = {
  148. .dst = {
  149. .__refcnt = ATOMIC_INIT(1),
  150. .__use = 1,
  151. .dev = &loopback_dev,
  152. .obsolete = -1,
  153. .error = -EINVAL,
  154. .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
  155. .input = ip6_pkt_discard,
  156. .output = ip6_pkt_discard_out,
  157. .ops = &ip6_dst_ops,
  158. .path = (struct dst_entry*)&ip6_blk_hole_entry,
  159. }
  160. },
  161. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  162. .rt6i_metric = ~(u32) 0,
  163. .rt6i_ref = ATOMIC_INIT(1),
  164. };
  165. #endif
  166. /* allocate dst with ip6_dst_ops */
  167. static __inline__ struct rt6_info *ip6_dst_alloc(void)
  168. {
  169. return (struct rt6_info *)dst_alloc(&ip6_dst_ops);
  170. }
  171. static void ip6_dst_destroy(struct dst_entry *dst)
  172. {
  173. struct rt6_info *rt = (struct rt6_info *)dst;
  174. struct inet6_dev *idev = rt->rt6i_idev;
  175. if (idev != NULL) {
  176. rt->rt6i_idev = NULL;
  177. in6_dev_put(idev);
  178. }
  179. }
  180. static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  181. int how)
  182. {
  183. struct rt6_info *rt = (struct rt6_info *)dst;
  184. struct inet6_dev *idev = rt->rt6i_idev;
  185. if (dev != &loopback_dev && idev != NULL && idev->dev == dev) {
  186. struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
  187. if (loopback_idev != NULL) {
  188. rt->rt6i_idev = loopback_idev;
  189. in6_dev_put(idev);
  190. }
  191. }
  192. }
  193. static __inline__ int rt6_check_expired(const struct rt6_info *rt)
  194. {
  195. return (rt->rt6i_flags & RTF_EXPIRES &&
  196. time_after(jiffies, rt->rt6i_expires));
  197. }
  198. static inline int rt6_need_strict(struct in6_addr *daddr)
  199. {
  200. return (ipv6_addr_type(daddr) &
  201. (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL));
  202. }
  203. /*
  204. * Route lookup. Any table->tb6_lock is implied.
  205. */
  206. static __inline__ struct rt6_info *rt6_device_match(struct rt6_info *rt,
  207. int oif,
  208. int strict)
  209. {
  210. struct rt6_info *local = NULL;
  211. struct rt6_info *sprt;
  212. if (oif) {
  213. for (sprt = rt; sprt; sprt = sprt->u.next) {
  214. struct net_device *dev = sprt->rt6i_dev;
  215. if (dev->ifindex == oif)
  216. return sprt;
  217. if (dev->flags & IFF_LOOPBACK) {
  218. if (sprt->rt6i_idev == NULL ||
  219. sprt->rt6i_idev->dev->ifindex != oif) {
  220. if (strict && oif)
  221. continue;
  222. if (local && (!oif ||
  223. local->rt6i_idev->dev->ifindex == oif))
  224. continue;
  225. }
  226. local = sprt;
  227. }
  228. }
  229. if (local)
  230. return local;
  231. if (strict)
  232. return &ip6_null_entry;
  233. }
  234. return rt;
  235. }
  236. #ifdef CONFIG_IPV6_ROUTER_PREF
  237. static void rt6_probe(struct rt6_info *rt)
  238. {
  239. struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
  240. /*
  241. * Okay, this does not seem to be appropriate
  242. * for now, however, we need to check if it
  243. * is really so; aka Router Reachability Probing.
  244. *
  245. * Router Reachability Probe MUST be rate-limited
  246. * to no more than one per minute.
  247. */
  248. if (!neigh || (neigh->nud_state & NUD_VALID))
  249. return;
  250. read_lock_bh(&neigh->lock);
  251. if (!(neigh->nud_state & NUD_VALID) &&
  252. time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
  253. struct in6_addr mcaddr;
  254. struct in6_addr *target;
  255. neigh->updated = jiffies;
  256. read_unlock_bh(&neigh->lock);
  257. target = (struct in6_addr *)&neigh->primary_key;
  258. addrconf_addr_solict_mult(target, &mcaddr);
  259. ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
  260. } else
  261. read_unlock_bh(&neigh->lock);
  262. }
  263. #else
  264. static inline void rt6_probe(struct rt6_info *rt)
  265. {
  266. return;
  267. }
  268. #endif
  269. /*
  270. * Default Router Selection (RFC 2461 6.3.6)
  271. */
  272. static int inline rt6_check_dev(struct rt6_info *rt, int oif)
  273. {
  274. struct net_device *dev = rt->rt6i_dev;
  275. if (!oif || dev->ifindex == oif)
  276. return 2;
  277. if ((dev->flags & IFF_LOOPBACK) &&
  278. rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
  279. return 1;
  280. return 0;
  281. }
  282. static int inline rt6_check_neigh(struct rt6_info *rt)
  283. {
  284. struct neighbour *neigh = rt->rt6i_nexthop;
  285. int m = 0;
  286. if (rt->rt6i_flags & RTF_NONEXTHOP ||
  287. !(rt->rt6i_flags & RTF_GATEWAY))
  288. m = 1;
  289. else if (neigh) {
  290. read_lock_bh(&neigh->lock);
  291. if (neigh->nud_state & NUD_VALID)
  292. m = 2;
  293. read_unlock_bh(&neigh->lock);
  294. }
  295. return m;
  296. }
  297. static int rt6_score_route(struct rt6_info *rt, int oif,
  298. int strict)
  299. {
  300. int m, n;
  301. m = rt6_check_dev(rt, oif);
  302. if (!m && (strict & RT6_SELECT_F_IFACE))
  303. return -1;
  304. #ifdef CONFIG_IPV6_ROUTER_PREF
  305. m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
  306. #endif
  307. n = rt6_check_neigh(rt);
  308. if (n > 1)
  309. m |= 16;
  310. else if (!n && strict & RT6_SELECT_F_REACHABLE)
  311. return -1;
  312. return m;
  313. }
  314. static struct rt6_info *rt6_select(struct rt6_info **head, int oif,
  315. int strict)
  316. {
  317. struct rt6_info *match = NULL, *last = NULL;
  318. struct rt6_info *rt, *rt0 = *head;
  319. u32 metric;
  320. int mpri = -1;
  321. RT6_TRACE("%s(head=%p(*head=%p), oif=%d)\n",
  322. __FUNCTION__, head, head ? *head : NULL, oif);
  323. for (rt = rt0, metric = rt0->rt6i_metric;
  324. rt && rt->rt6i_metric == metric && (!last || rt != rt0);
  325. rt = rt->u.next) {
  326. int m;
  327. if (rt6_check_expired(rt))
  328. continue;
  329. last = rt;
  330. m = rt6_score_route(rt, oif, strict);
  331. if (m < 0)
  332. continue;
  333. if (m > mpri) {
  334. rt6_probe(match);
  335. match = rt;
  336. mpri = m;
  337. } else {
  338. rt6_probe(rt);
  339. }
  340. }
  341. if (!match &&
  342. (strict & RT6_SELECT_F_REACHABLE) &&
  343. last && last != rt0) {
  344. /* no entries matched; do round-robin */
  345. static DEFINE_SPINLOCK(lock);
  346. spin_lock(&lock);
  347. *head = rt0->u.next;
  348. rt0->u.next = last->u.next;
  349. last->u.next = rt0;
  350. spin_unlock(&lock);
  351. }
  352. RT6_TRACE("%s() => %p, score=%d\n",
  353. __FUNCTION__, match, mpri);
  354. return (match ? match : &ip6_null_entry);
  355. }
  356. #ifdef CONFIG_IPV6_ROUTE_INFO
  357. int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
  358. struct in6_addr *gwaddr)
  359. {
  360. struct route_info *rinfo = (struct route_info *) opt;
  361. struct in6_addr prefix_buf, *prefix;
  362. unsigned int pref;
  363. u32 lifetime;
  364. struct rt6_info *rt;
  365. if (len < sizeof(struct route_info)) {
  366. return -EINVAL;
  367. }
  368. /* Sanity check for prefix_len and length */
  369. if (rinfo->length > 3) {
  370. return -EINVAL;
  371. } else if (rinfo->prefix_len > 128) {
  372. return -EINVAL;
  373. } else if (rinfo->prefix_len > 64) {
  374. if (rinfo->length < 2) {
  375. return -EINVAL;
  376. }
  377. } else if (rinfo->prefix_len > 0) {
  378. if (rinfo->length < 1) {
  379. return -EINVAL;
  380. }
  381. }
  382. pref = rinfo->route_pref;
  383. if (pref == ICMPV6_ROUTER_PREF_INVALID)
  384. pref = ICMPV6_ROUTER_PREF_MEDIUM;
  385. lifetime = htonl(rinfo->lifetime);
  386. if (lifetime == 0xffffffff) {
  387. /* infinity */
  388. } else if (lifetime > 0x7fffffff/HZ) {
  389. /* Avoid arithmetic overflow */
  390. lifetime = 0x7fffffff/HZ - 1;
  391. }
  392. if (rinfo->length == 3)
  393. prefix = (struct in6_addr *)rinfo->prefix;
  394. else {
  395. /* this function is safe */
  396. ipv6_addr_prefix(&prefix_buf,
  397. (struct in6_addr *)rinfo->prefix,
  398. rinfo->prefix_len);
  399. prefix = &prefix_buf;
  400. }
  401. rt = rt6_get_route_info(prefix, rinfo->prefix_len, gwaddr, dev->ifindex);
  402. if (rt && !lifetime) {
  403. ip6_del_rt(rt, NULL, NULL, NULL);
  404. rt = NULL;
  405. }
  406. if (!rt && lifetime)
  407. rt = rt6_add_route_info(prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
  408. pref);
  409. else if (rt)
  410. rt->rt6i_flags = RTF_ROUTEINFO |
  411. (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
  412. if (rt) {
  413. if (lifetime == 0xffffffff) {
  414. rt->rt6i_flags &= ~RTF_EXPIRES;
  415. } else {
  416. rt->rt6i_expires = jiffies + HZ * lifetime;
  417. rt->rt6i_flags |= RTF_EXPIRES;
  418. }
  419. dst_release(&rt->u.dst);
  420. }
  421. return 0;
  422. }
  423. #endif
  424. #define BACKTRACK() \
  425. if (rt == &ip6_null_entry && flags & RT6_F_STRICT) { \
  426. while ((fn = fn->parent) != NULL) { \
  427. if (fn->fn_flags & RTN_TL_ROOT) { \
  428. dst_hold(&rt->u.dst); \
  429. goto out; \
  430. } \
  431. if (fn->fn_flags & RTN_RTINFO) \
  432. goto restart; \
  433. } \
  434. }
  435. static struct rt6_info *ip6_pol_route_lookup(struct fib6_table *table,
  436. struct flowi *fl, int flags)
  437. {
  438. struct fib6_node *fn;
  439. struct rt6_info *rt;
  440. read_lock_bh(&table->tb6_lock);
  441. fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
  442. restart:
  443. rt = fn->leaf;
  444. rt = rt6_device_match(rt, fl->oif, flags & RT6_F_STRICT);
  445. BACKTRACK();
  446. dst_hold(&rt->u.dst);
  447. out:
  448. read_unlock_bh(&table->tb6_lock);
  449. rt->u.dst.lastuse = jiffies;
  450. rt->u.dst.__use++;
  451. return rt;
  452. }
  453. struct rt6_info *rt6_lookup(struct in6_addr *daddr, struct in6_addr *saddr,
  454. int oif, int strict)
  455. {
  456. struct flowi fl = {
  457. .oif = oif,
  458. .nl_u = {
  459. .ip6_u = {
  460. .daddr = *daddr,
  461. /* TODO: saddr */
  462. },
  463. },
  464. };
  465. struct dst_entry *dst;
  466. int flags = strict ? RT6_F_STRICT : 0;
  467. dst = fib6_rule_lookup(&fl, flags, ip6_pol_route_lookup);
  468. if (dst->error == 0)
  469. return (struct rt6_info *) dst;
  470. dst_release(dst);
  471. return NULL;
  472. }
  473. /* ip6_ins_rt is called with FREE table->tb6_lock.
  474. It takes new route entry, the addition fails by any reason the
  475. route is freed. In any case, if caller does not hold it, it may
  476. be destroyed.
  477. */
  478. int ip6_ins_rt(struct rt6_info *rt, struct nlmsghdr *nlh,
  479. void *_rtattr, struct netlink_skb_parms *req)
  480. {
  481. int err;
  482. struct fib6_table *table;
  483. table = rt->rt6i_table;
  484. write_lock_bh(&table->tb6_lock);
  485. err = fib6_add(&table->tb6_root, rt, nlh, _rtattr, req);
  486. write_unlock_bh(&table->tb6_lock);
  487. return err;
  488. }
  489. static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
  490. struct in6_addr *saddr)
  491. {
  492. struct rt6_info *rt;
  493. /*
  494. * Clone the route.
  495. */
  496. rt = ip6_rt_copy(ort);
  497. if (rt) {
  498. if (!(rt->rt6i_flags&RTF_GATEWAY)) {
  499. if (rt->rt6i_dst.plen != 128 &&
  500. ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
  501. rt->rt6i_flags |= RTF_ANYCAST;
  502. ipv6_addr_copy(&rt->rt6i_gateway, daddr);
  503. }
  504. ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
  505. rt->rt6i_dst.plen = 128;
  506. rt->rt6i_flags |= RTF_CACHE;
  507. rt->u.dst.flags |= DST_HOST;
  508. #ifdef CONFIG_IPV6_SUBTREES
  509. if (rt->rt6i_src.plen && saddr) {
  510. ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
  511. rt->rt6i_src.plen = 128;
  512. }
  513. #endif
  514. rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
  515. }
  516. return rt;
  517. }
  518. static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
  519. {
  520. struct rt6_info *rt = ip6_rt_copy(ort);
  521. if (rt) {
  522. ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
  523. rt->rt6i_dst.plen = 128;
  524. rt->rt6i_flags |= RTF_CACHE;
  525. if (rt->rt6i_flags & RTF_REJECT)
  526. rt->u.dst.error = ort->u.dst.error;
  527. rt->u.dst.flags |= DST_HOST;
  528. rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
  529. }
  530. return rt;
  531. }
  532. static struct rt6_info *ip6_pol_route_input(struct fib6_table *table,
  533. struct flowi *fl, int flags)
  534. {
  535. struct fib6_node *fn;
  536. struct rt6_info *rt, *nrt;
  537. int strict = 0;
  538. int attempts = 3;
  539. int err;
  540. int reachable = RT6_SELECT_F_REACHABLE;
  541. if (flags & RT6_F_STRICT)
  542. strict = RT6_SELECT_F_IFACE;
  543. relookup:
  544. read_lock_bh(&table->tb6_lock);
  545. restart_2:
  546. fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
  547. restart:
  548. rt = rt6_select(&fn->leaf, fl->iif, strict | reachable);
  549. BACKTRACK();
  550. if (rt == &ip6_null_entry ||
  551. rt->rt6i_flags & RTF_CACHE)
  552. goto out;
  553. dst_hold(&rt->u.dst);
  554. read_unlock_bh(&table->tb6_lock);
  555. if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
  556. nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
  557. else {
  558. #if CLONE_OFFLINK_ROUTE
  559. nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
  560. #else
  561. goto out2;
  562. #endif
  563. }
  564. dst_release(&rt->u.dst);
  565. rt = nrt ? : &ip6_null_entry;
  566. dst_hold(&rt->u.dst);
  567. if (nrt) {
  568. err = ip6_ins_rt(nrt, NULL, NULL, NULL);
  569. if (!err)
  570. goto out2;
  571. }
  572. if (--attempts <= 0)
  573. goto out2;
  574. /*
  575. * Race condition! In the gap, when table->tb6_lock was
  576. * released someone could insert this route. Relookup.
  577. */
  578. dst_release(&rt->u.dst);
  579. goto relookup;
  580. out:
  581. if (reachable) {
  582. reachable = 0;
  583. goto restart_2;
  584. }
  585. dst_hold(&rt->u.dst);
  586. read_unlock_bh(&table->tb6_lock);
  587. out2:
  588. rt->u.dst.lastuse = jiffies;
  589. rt->u.dst.__use++;
  590. return rt;
  591. }
  592. void ip6_route_input(struct sk_buff *skb)
  593. {
  594. struct ipv6hdr *iph = skb->nh.ipv6h;
  595. struct flowi fl = {
  596. .iif = skb->dev->ifindex,
  597. .nl_u = {
  598. .ip6_u = {
  599. .daddr = iph->daddr,
  600. .saddr = iph->saddr,
  601. .flowlabel = (* (u32 *) iph)&IPV6_FLOWINFO_MASK,
  602. },
  603. },
  604. .proto = iph->nexthdr,
  605. };
  606. int flags = 0;
  607. if (rt6_need_strict(&iph->daddr))
  608. flags |= RT6_F_STRICT;
  609. skb->dst = fib6_rule_lookup(&fl, flags, ip6_pol_route_input);
  610. }
  611. static struct rt6_info *ip6_pol_route_output(struct fib6_table *table,
  612. struct flowi *fl, int flags)
  613. {
  614. struct fib6_node *fn;
  615. struct rt6_info *rt, *nrt;
  616. int strict = 0;
  617. int attempts = 3;
  618. int err;
  619. int reachable = RT6_SELECT_F_REACHABLE;
  620. if (flags & RT6_F_STRICT)
  621. strict = RT6_SELECT_F_IFACE;
  622. relookup:
  623. read_lock_bh(&table->tb6_lock);
  624. restart_2:
  625. fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
  626. restart:
  627. rt = rt6_select(&fn->leaf, fl->oif, strict | reachable);
  628. BACKTRACK();
  629. if (rt == &ip6_null_entry ||
  630. rt->rt6i_flags & RTF_CACHE)
  631. goto out;
  632. dst_hold(&rt->u.dst);
  633. read_unlock_bh(&table->tb6_lock);
  634. if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
  635. nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
  636. else {
  637. #if CLONE_OFFLINK_ROUTE
  638. nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
  639. #else
  640. goto out2;
  641. #endif
  642. }
  643. dst_release(&rt->u.dst);
  644. rt = nrt ? : &ip6_null_entry;
  645. dst_hold(&rt->u.dst);
  646. if (nrt) {
  647. err = ip6_ins_rt(nrt, NULL, NULL, NULL);
  648. if (!err)
  649. goto out2;
  650. }
  651. if (--attempts <= 0)
  652. goto out2;
  653. /*
  654. * Race condition! In the gap, when table->tb6_lock was
  655. * released someone could insert this route. Relookup.
  656. */
  657. dst_release(&rt->u.dst);
  658. goto relookup;
  659. out:
  660. if (reachable) {
  661. reachable = 0;
  662. goto restart_2;
  663. }
  664. dst_hold(&rt->u.dst);
  665. read_unlock_bh(&table->tb6_lock);
  666. out2:
  667. rt->u.dst.lastuse = jiffies;
  668. rt->u.dst.__use++;
  669. return rt;
  670. }
  671. struct dst_entry * ip6_route_output(struct sock *sk, struct flowi *fl)
  672. {
  673. int flags = 0;
  674. if (rt6_need_strict(&fl->fl6_dst))
  675. flags |= RT6_F_STRICT;
  676. return fib6_rule_lookup(fl, flags, ip6_pol_route_output);
  677. }
  678. /*
  679. * Destination cache support functions
  680. */
  681. static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
  682. {
  683. struct rt6_info *rt;
  684. rt = (struct rt6_info *) dst;
  685. if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
  686. return dst;
  687. return NULL;
  688. }
  689. static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
  690. {
  691. struct rt6_info *rt = (struct rt6_info *) dst;
  692. if (rt) {
  693. if (rt->rt6i_flags & RTF_CACHE)
  694. ip6_del_rt(rt, NULL, NULL, NULL);
  695. else
  696. dst_release(dst);
  697. }
  698. return NULL;
  699. }
  700. static void ip6_link_failure(struct sk_buff *skb)
  701. {
  702. struct rt6_info *rt;
  703. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
  704. rt = (struct rt6_info *) skb->dst;
  705. if (rt) {
  706. if (rt->rt6i_flags&RTF_CACHE) {
  707. dst_set_expires(&rt->u.dst, 0);
  708. rt->rt6i_flags |= RTF_EXPIRES;
  709. } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
  710. rt->rt6i_node->fn_sernum = -1;
  711. }
  712. }
  713. static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
  714. {
  715. struct rt6_info *rt6 = (struct rt6_info*)dst;
  716. if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
  717. rt6->rt6i_flags |= RTF_MODIFIED;
  718. if (mtu < IPV6_MIN_MTU) {
  719. mtu = IPV6_MIN_MTU;
  720. dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
  721. }
  722. dst->metrics[RTAX_MTU-1] = mtu;
  723. call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
  724. }
  725. }
  726. static int ipv6_get_mtu(struct net_device *dev);
  727. static inline unsigned int ipv6_advmss(unsigned int mtu)
  728. {
  729. mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
  730. if (mtu < ip6_rt_min_advmss)
  731. mtu = ip6_rt_min_advmss;
  732. /*
  733. * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
  734. * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
  735. * IPV6_MAXPLEN is also valid and means: "any MSS,
  736. * rely only on pmtu discovery"
  737. */
  738. if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
  739. mtu = IPV6_MAXPLEN;
  740. return mtu;
  741. }
  742. static struct dst_entry *ndisc_dst_gc_list;
  743. static DEFINE_SPINLOCK(ndisc_lock);
  744. struct dst_entry *ndisc_dst_alloc(struct net_device *dev,
  745. struct neighbour *neigh,
  746. struct in6_addr *addr,
  747. int (*output)(struct sk_buff *))
  748. {
  749. struct rt6_info *rt;
  750. struct inet6_dev *idev = in6_dev_get(dev);
  751. if (unlikely(idev == NULL))
  752. return NULL;
  753. rt = ip6_dst_alloc();
  754. if (unlikely(rt == NULL)) {
  755. in6_dev_put(idev);
  756. goto out;
  757. }
  758. dev_hold(dev);
  759. if (neigh)
  760. neigh_hold(neigh);
  761. else
  762. neigh = ndisc_get_neigh(dev, addr);
  763. rt->rt6i_dev = dev;
  764. rt->rt6i_idev = idev;
  765. rt->rt6i_nexthop = neigh;
  766. atomic_set(&rt->u.dst.__refcnt, 1);
  767. rt->u.dst.metrics[RTAX_HOPLIMIT-1] = 255;
  768. rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
  769. rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
  770. rt->u.dst.output = output;
  771. #if 0 /* there's no chance to use these for ndisc */
  772. rt->u.dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
  773. ? DST_HOST
  774. : 0;
  775. ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
  776. rt->rt6i_dst.plen = 128;
  777. #endif
  778. spin_lock_bh(&ndisc_lock);
  779. rt->u.dst.next = ndisc_dst_gc_list;
  780. ndisc_dst_gc_list = &rt->u.dst;
  781. spin_unlock_bh(&ndisc_lock);
  782. fib6_force_start_gc();
  783. out:
  784. return (struct dst_entry *)rt;
  785. }
  786. int ndisc_dst_gc(int *more)
  787. {
  788. struct dst_entry *dst, *next, **pprev;
  789. int freed;
  790. next = NULL;
  791. freed = 0;
  792. spin_lock_bh(&ndisc_lock);
  793. pprev = &ndisc_dst_gc_list;
  794. while ((dst = *pprev) != NULL) {
  795. if (!atomic_read(&dst->__refcnt)) {
  796. *pprev = dst->next;
  797. dst_free(dst);
  798. freed++;
  799. } else {
  800. pprev = &dst->next;
  801. (*more)++;
  802. }
  803. }
  804. spin_unlock_bh(&ndisc_lock);
  805. return freed;
  806. }
  807. static int ip6_dst_gc(void)
  808. {
  809. static unsigned expire = 30*HZ;
  810. static unsigned long last_gc;
  811. unsigned long now = jiffies;
  812. if (time_after(last_gc + ip6_rt_gc_min_interval, now) &&
  813. atomic_read(&ip6_dst_ops.entries) <= ip6_rt_max_size)
  814. goto out;
  815. expire++;
  816. fib6_run_gc(expire);
  817. last_gc = now;
  818. if (atomic_read(&ip6_dst_ops.entries) < ip6_dst_ops.gc_thresh)
  819. expire = ip6_rt_gc_timeout>>1;
  820. out:
  821. expire -= expire>>ip6_rt_gc_elasticity;
  822. return (atomic_read(&ip6_dst_ops.entries) > ip6_rt_max_size);
  823. }
  824. /* Clean host part of a prefix. Not necessary in radix tree,
  825. but results in cleaner routing tables.
  826. Remove it only when all the things will work!
  827. */
  828. static int ipv6_get_mtu(struct net_device *dev)
  829. {
  830. int mtu = IPV6_MIN_MTU;
  831. struct inet6_dev *idev;
  832. idev = in6_dev_get(dev);
  833. if (idev) {
  834. mtu = idev->cnf.mtu6;
  835. in6_dev_put(idev);
  836. }
  837. return mtu;
  838. }
  839. int ipv6_get_hoplimit(struct net_device *dev)
  840. {
  841. int hoplimit = ipv6_devconf.hop_limit;
  842. struct inet6_dev *idev;
  843. idev = in6_dev_get(dev);
  844. if (idev) {
  845. hoplimit = idev->cnf.hop_limit;
  846. in6_dev_put(idev);
  847. }
  848. return hoplimit;
  849. }
  850. /*
  851. *
  852. */
  853. int ip6_route_add(struct in6_rtmsg *rtmsg, struct nlmsghdr *nlh,
  854. void *_rtattr, struct netlink_skb_parms *req,
  855. u32 table_id)
  856. {
  857. int err;
  858. struct rtmsg *r;
  859. struct rtattr **rta;
  860. struct rt6_info *rt = NULL;
  861. struct net_device *dev = NULL;
  862. struct inet6_dev *idev = NULL;
  863. struct fib6_table *table;
  864. int addr_type;
  865. rta = (struct rtattr **) _rtattr;
  866. if (rtmsg->rtmsg_dst_len > 128 || rtmsg->rtmsg_src_len > 128)
  867. return -EINVAL;
  868. #ifndef CONFIG_IPV6_SUBTREES
  869. if (rtmsg->rtmsg_src_len)
  870. return -EINVAL;
  871. #endif
  872. if (rtmsg->rtmsg_ifindex) {
  873. err = -ENODEV;
  874. dev = dev_get_by_index(rtmsg->rtmsg_ifindex);
  875. if (!dev)
  876. goto out;
  877. idev = in6_dev_get(dev);
  878. if (!idev)
  879. goto out;
  880. }
  881. if (rtmsg->rtmsg_metric == 0)
  882. rtmsg->rtmsg_metric = IP6_RT_PRIO_USER;
  883. table = fib6_new_table(table_id);
  884. if (table == NULL) {
  885. err = -ENOBUFS;
  886. goto out;
  887. }
  888. rt = ip6_dst_alloc();
  889. if (rt == NULL) {
  890. err = -ENOMEM;
  891. goto out;
  892. }
  893. rt->u.dst.obsolete = -1;
  894. rt->rt6i_expires = jiffies + clock_t_to_jiffies(rtmsg->rtmsg_info);
  895. if (nlh && (r = NLMSG_DATA(nlh))) {
  896. rt->rt6i_protocol = r->rtm_protocol;
  897. } else {
  898. rt->rt6i_protocol = RTPROT_BOOT;
  899. }
  900. addr_type = ipv6_addr_type(&rtmsg->rtmsg_dst);
  901. if (addr_type & IPV6_ADDR_MULTICAST)
  902. rt->u.dst.input = ip6_mc_input;
  903. else
  904. rt->u.dst.input = ip6_forward;
  905. rt->u.dst.output = ip6_output;
  906. ipv6_addr_prefix(&rt->rt6i_dst.addr,
  907. &rtmsg->rtmsg_dst, rtmsg->rtmsg_dst_len);
  908. rt->rt6i_dst.plen = rtmsg->rtmsg_dst_len;
  909. if (rt->rt6i_dst.plen == 128)
  910. rt->u.dst.flags = DST_HOST;
  911. #ifdef CONFIG_IPV6_SUBTREES
  912. ipv6_addr_prefix(&rt->rt6i_src.addr,
  913. &rtmsg->rtmsg_src, rtmsg->rtmsg_src_len);
  914. rt->rt6i_src.plen = rtmsg->rtmsg_src_len;
  915. #endif
  916. rt->rt6i_metric = rtmsg->rtmsg_metric;
  917. /* We cannot add true routes via loopback here,
  918. they would result in kernel looping; promote them to reject routes
  919. */
  920. if ((rtmsg->rtmsg_flags&RTF_REJECT) ||
  921. (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) {
  922. /* hold loopback dev/idev if we haven't done so. */
  923. if (dev != &loopback_dev) {
  924. if (dev) {
  925. dev_put(dev);
  926. in6_dev_put(idev);
  927. }
  928. dev = &loopback_dev;
  929. dev_hold(dev);
  930. idev = in6_dev_get(dev);
  931. if (!idev) {
  932. err = -ENODEV;
  933. goto out;
  934. }
  935. }
  936. rt->u.dst.output = ip6_pkt_discard_out;
  937. rt->u.dst.input = ip6_pkt_discard;
  938. rt->u.dst.error = -ENETUNREACH;
  939. rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
  940. goto install_route;
  941. }
  942. if (rtmsg->rtmsg_flags & RTF_GATEWAY) {
  943. struct in6_addr *gw_addr;
  944. int gwa_type;
  945. gw_addr = &rtmsg->rtmsg_gateway;
  946. ipv6_addr_copy(&rt->rt6i_gateway, &rtmsg->rtmsg_gateway);
  947. gwa_type = ipv6_addr_type(gw_addr);
  948. if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
  949. struct rt6_info *grt;
  950. /* IPv6 strictly inhibits using not link-local
  951. addresses as nexthop address.
  952. Otherwise, router will not able to send redirects.
  953. It is very good, but in some (rare!) circumstances
  954. (SIT, PtP, NBMA NOARP links) it is handy to allow
  955. some exceptions. --ANK
  956. */
  957. err = -EINVAL;
  958. if (!(gwa_type&IPV6_ADDR_UNICAST))
  959. goto out;
  960. grt = rt6_lookup(gw_addr, NULL, rtmsg->rtmsg_ifindex, 1);
  961. err = -EHOSTUNREACH;
  962. if (grt == NULL)
  963. goto out;
  964. if (dev) {
  965. if (dev != grt->rt6i_dev) {
  966. dst_release(&grt->u.dst);
  967. goto out;
  968. }
  969. } else {
  970. dev = grt->rt6i_dev;
  971. idev = grt->rt6i_idev;
  972. dev_hold(dev);
  973. in6_dev_hold(grt->rt6i_idev);
  974. }
  975. if (!(grt->rt6i_flags&RTF_GATEWAY))
  976. err = 0;
  977. dst_release(&grt->u.dst);
  978. if (err)
  979. goto out;
  980. }
  981. err = -EINVAL;
  982. if (dev == NULL || (dev->flags&IFF_LOOPBACK))
  983. goto out;
  984. }
  985. err = -ENODEV;
  986. if (dev == NULL)
  987. goto out;
  988. if (rtmsg->rtmsg_flags & (RTF_GATEWAY|RTF_NONEXTHOP)) {
  989. rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
  990. if (IS_ERR(rt->rt6i_nexthop)) {
  991. err = PTR_ERR(rt->rt6i_nexthop);
  992. rt->rt6i_nexthop = NULL;
  993. goto out;
  994. }
  995. }
  996. rt->rt6i_flags = rtmsg->rtmsg_flags;
  997. install_route:
  998. if (rta && rta[RTA_METRICS-1]) {
  999. int attrlen = RTA_PAYLOAD(rta[RTA_METRICS-1]);
  1000. struct rtattr *attr = RTA_DATA(rta[RTA_METRICS-1]);
  1001. while (RTA_OK(attr, attrlen)) {
  1002. unsigned flavor = attr->rta_type;
  1003. if (flavor) {
  1004. if (flavor > RTAX_MAX) {
  1005. err = -EINVAL;
  1006. goto out;
  1007. }
  1008. rt->u.dst.metrics[flavor-1] =
  1009. *(u32 *)RTA_DATA(attr);
  1010. }
  1011. attr = RTA_NEXT(attr, attrlen);
  1012. }
  1013. }
  1014. if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
  1015. rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
  1016. if (!rt->u.dst.metrics[RTAX_MTU-1])
  1017. rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
  1018. if (!rt->u.dst.metrics[RTAX_ADVMSS-1])
  1019. rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
  1020. rt->u.dst.dev = dev;
  1021. rt->rt6i_idev = idev;
  1022. rt->rt6i_table = table;
  1023. return ip6_ins_rt(rt, nlh, _rtattr, req);
  1024. out:
  1025. if (dev)
  1026. dev_put(dev);
  1027. if (idev)
  1028. in6_dev_put(idev);
  1029. if (rt)
  1030. dst_free((struct dst_entry *) rt);
  1031. return err;
  1032. }
  1033. int ip6_del_rt(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
  1034. {
  1035. int err;
  1036. struct fib6_table *table;
  1037. if (rt == &ip6_null_entry)
  1038. return -ENOENT;
  1039. table = rt->rt6i_table;
  1040. write_lock_bh(&table->tb6_lock);
  1041. err = fib6_del(rt, nlh, _rtattr, req);
  1042. dst_release(&rt->u.dst);
  1043. write_unlock_bh(&table->tb6_lock);
  1044. return err;
  1045. }
  1046. static int ip6_route_del(struct in6_rtmsg *rtmsg, struct nlmsghdr *nlh,
  1047. void *_rtattr, struct netlink_skb_parms *req,
  1048. u32 table_id)
  1049. {
  1050. struct fib6_table *table;
  1051. struct fib6_node *fn;
  1052. struct rt6_info *rt;
  1053. int err = -ESRCH;
  1054. table = fib6_get_table(table_id);
  1055. if (table == NULL)
  1056. return err;
  1057. read_lock_bh(&table->tb6_lock);
  1058. fn = fib6_locate(&table->tb6_root,
  1059. &rtmsg->rtmsg_dst, rtmsg->rtmsg_dst_len,
  1060. &rtmsg->rtmsg_src, rtmsg->rtmsg_src_len);
  1061. if (fn) {
  1062. for (rt = fn->leaf; rt; rt = rt->u.next) {
  1063. if (rtmsg->rtmsg_ifindex &&
  1064. (rt->rt6i_dev == NULL ||
  1065. rt->rt6i_dev->ifindex != rtmsg->rtmsg_ifindex))
  1066. continue;
  1067. if (rtmsg->rtmsg_flags&RTF_GATEWAY &&
  1068. !ipv6_addr_equal(&rtmsg->rtmsg_gateway, &rt->rt6i_gateway))
  1069. continue;
  1070. if (rtmsg->rtmsg_metric &&
  1071. rtmsg->rtmsg_metric != rt->rt6i_metric)
  1072. continue;
  1073. dst_hold(&rt->u.dst);
  1074. read_unlock_bh(&table->tb6_lock);
  1075. return ip6_del_rt(rt, nlh, _rtattr, req);
  1076. }
  1077. }
  1078. read_unlock_bh(&table->tb6_lock);
  1079. return err;
  1080. }
  1081. /*
  1082. * Handle redirects
  1083. */
  1084. void rt6_redirect(struct in6_addr *dest, struct in6_addr *saddr,
  1085. struct neighbour *neigh, u8 *lladdr, int on_link)
  1086. {
  1087. struct rt6_info *rt, *nrt = NULL;
  1088. struct fib6_node *fn;
  1089. struct fib6_table *table;
  1090. struct netevent_redirect netevent;
  1091. /* TODO: Very lazy, might need to check all tables */
  1092. table = fib6_get_table(RT6_TABLE_MAIN);
  1093. if (table == NULL)
  1094. return;
  1095. /*
  1096. * Get the "current" route for this destination and
  1097. * check if the redirect has come from approriate router.
  1098. *
  1099. * RFC 2461 specifies that redirects should only be
  1100. * accepted if they come from the nexthop to the target.
  1101. * Due to the way the routes are chosen, this notion
  1102. * is a bit fuzzy and one might need to check all possible
  1103. * routes.
  1104. */
  1105. read_lock_bh(&table->tb6_lock);
  1106. fn = fib6_lookup(&table->tb6_root, dest, NULL);
  1107. restart:
  1108. for (rt = fn->leaf; rt; rt = rt->u.next) {
  1109. /*
  1110. * Current route is on-link; redirect is always invalid.
  1111. *
  1112. * Seems, previous statement is not true. It could
  1113. * be node, which looks for us as on-link (f.e. proxy ndisc)
  1114. * But then router serving it might decide, that we should
  1115. * know truth 8)8) --ANK (980726).
  1116. */
  1117. if (rt6_check_expired(rt))
  1118. continue;
  1119. if (!(rt->rt6i_flags & RTF_GATEWAY))
  1120. continue;
  1121. if (neigh->dev != rt->rt6i_dev)
  1122. continue;
  1123. if (!ipv6_addr_equal(saddr, &rt->rt6i_gateway))
  1124. continue;
  1125. break;
  1126. }
  1127. if (rt)
  1128. dst_hold(&rt->u.dst);
  1129. else if (rt6_need_strict(dest)) {
  1130. while ((fn = fn->parent) != NULL) {
  1131. if (fn->fn_flags & RTN_ROOT)
  1132. break;
  1133. if (fn->fn_flags & RTN_RTINFO)
  1134. goto restart;
  1135. }
  1136. }
  1137. read_unlock_bh(&table->tb6_lock);
  1138. if (!rt) {
  1139. if (net_ratelimit())
  1140. printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
  1141. "for redirect target\n");
  1142. return;
  1143. }
  1144. /*
  1145. * We have finally decided to accept it.
  1146. */
  1147. neigh_update(neigh, lladdr, NUD_STALE,
  1148. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  1149. NEIGH_UPDATE_F_OVERRIDE|
  1150. (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  1151. NEIGH_UPDATE_F_ISROUTER))
  1152. );
  1153. /*
  1154. * Redirect received -> path was valid.
  1155. * Look, redirects are sent only in response to data packets,
  1156. * so that this nexthop apparently is reachable. --ANK
  1157. */
  1158. dst_confirm(&rt->u.dst);
  1159. /* Duplicate redirect: silently ignore. */
  1160. if (neigh == rt->u.dst.neighbour)
  1161. goto out;
  1162. nrt = ip6_rt_copy(rt);
  1163. if (nrt == NULL)
  1164. goto out;
  1165. nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
  1166. if (on_link)
  1167. nrt->rt6i_flags &= ~RTF_GATEWAY;
  1168. ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
  1169. nrt->rt6i_dst.plen = 128;
  1170. nrt->u.dst.flags |= DST_HOST;
  1171. ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
  1172. nrt->rt6i_nexthop = neigh_clone(neigh);
  1173. /* Reset pmtu, it may be better */
  1174. nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
  1175. nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&nrt->u.dst));
  1176. if (ip6_ins_rt(nrt, NULL, NULL, NULL))
  1177. goto out;
  1178. netevent.old = &rt->u.dst;
  1179. netevent.new = &nrt->u.dst;
  1180. call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
  1181. if (rt->rt6i_flags&RTF_CACHE) {
  1182. ip6_del_rt(rt, NULL, NULL, NULL);
  1183. return;
  1184. }
  1185. out:
  1186. dst_release(&rt->u.dst);
  1187. return;
  1188. }
  1189. /*
  1190. * Handle ICMP "packet too big" messages
  1191. * i.e. Path MTU discovery
  1192. */
  1193. void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
  1194. struct net_device *dev, u32 pmtu)
  1195. {
  1196. struct rt6_info *rt, *nrt;
  1197. int allfrag = 0;
  1198. rt = rt6_lookup(daddr, saddr, dev->ifindex, 0);
  1199. if (rt == NULL)
  1200. return;
  1201. if (pmtu >= dst_mtu(&rt->u.dst))
  1202. goto out;
  1203. if (pmtu < IPV6_MIN_MTU) {
  1204. /*
  1205. * According to RFC2460, PMTU is set to the IPv6 Minimum Link
  1206. * MTU (1280) and a fragment header should always be included
  1207. * after a node receiving Too Big message reporting PMTU is
  1208. * less than the IPv6 Minimum Link MTU.
  1209. */
  1210. pmtu = IPV6_MIN_MTU;
  1211. allfrag = 1;
  1212. }
  1213. /* New mtu received -> path was valid.
  1214. They are sent only in response to data packets,
  1215. so that this nexthop apparently is reachable. --ANK
  1216. */
  1217. dst_confirm(&rt->u.dst);
  1218. /* Host route. If it is static, it would be better
  1219. not to override it, but add new one, so that
  1220. when cache entry will expire old pmtu
  1221. would return automatically.
  1222. */
  1223. if (rt->rt6i_flags & RTF_CACHE) {
  1224. rt->u.dst.metrics[RTAX_MTU-1] = pmtu;
  1225. if (allfrag)
  1226. rt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
  1227. dst_set_expires(&rt->u.dst, ip6_rt_mtu_expires);
  1228. rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
  1229. goto out;
  1230. }
  1231. /* Network route.
  1232. Two cases are possible:
  1233. 1. It is connected route. Action: COW
  1234. 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
  1235. */
  1236. if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
  1237. nrt = rt6_alloc_cow(rt, daddr, saddr);
  1238. else
  1239. nrt = rt6_alloc_clone(rt, daddr);
  1240. if (nrt) {
  1241. nrt->u.dst.metrics[RTAX_MTU-1] = pmtu;
  1242. if (allfrag)
  1243. nrt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
  1244. /* According to RFC 1981, detecting PMTU increase shouldn't be
  1245. * happened within 5 mins, the recommended timer is 10 mins.
  1246. * Here this route expiration time is set to ip6_rt_mtu_expires
  1247. * which is 10 mins. After 10 mins the decreased pmtu is expired
  1248. * and detecting PMTU increase will be automatically happened.
  1249. */
  1250. dst_set_expires(&nrt->u.dst, ip6_rt_mtu_expires);
  1251. nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
  1252. ip6_ins_rt(nrt, NULL, NULL, NULL);
  1253. }
  1254. out:
  1255. dst_release(&rt->u.dst);
  1256. }
  1257. /*
  1258. * Misc support functions
  1259. */
  1260. static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
  1261. {
  1262. struct rt6_info *rt = ip6_dst_alloc();
  1263. if (rt) {
  1264. rt->u.dst.input = ort->u.dst.input;
  1265. rt->u.dst.output = ort->u.dst.output;
  1266. memcpy(rt->u.dst.metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
  1267. rt->u.dst.dev = ort->u.dst.dev;
  1268. if (rt->u.dst.dev)
  1269. dev_hold(rt->u.dst.dev);
  1270. rt->rt6i_idev = ort->rt6i_idev;
  1271. if (rt->rt6i_idev)
  1272. in6_dev_hold(rt->rt6i_idev);
  1273. rt->u.dst.lastuse = jiffies;
  1274. rt->rt6i_expires = 0;
  1275. ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
  1276. rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
  1277. rt->rt6i_metric = 0;
  1278. memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
  1279. #ifdef CONFIG_IPV6_SUBTREES
  1280. memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
  1281. #endif
  1282. rt->rt6i_table = ort->rt6i_table;
  1283. }
  1284. return rt;
  1285. }
  1286. #ifdef CONFIG_IPV6_ROUTE_INFO
  1287. static struct rt6_info *rt6_get_route_info(struct in6_addr *prefix, int prefixlen,
  1288. struct in6_addr *gwaddr, int ifindex)
  1289. {
  1290. struct fib6_node *fn;
  1291. struct rt6_info *rt = NULL;
  1292. struct fib6_table *table;
  1293. table = fib6_get_table(RT6_TABLE_INFO);
  1294. if (table == NULL)
  1295. return NULL;
  1296. write_lock_bh(&table->tb6_lock);
  1297. fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
  1298. if (!fn)
  1299. goto out;
  1300. for (rt = fn->leaf; rt; rt = rt->u.next) {
  1301. if (rt->rt6i_dev->ifindex != ifindex)
  1302. continue;
  1303. if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
  1304. continue;
  1305. if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
  1306. continue;
  1307. dst_hold(&rt->u.dst);
  1308. break;
  1309. }
  1310. out:
  1311. write_unlock_bh(&table->tb6_lock);
  1312. return rt;
  1313. }
  1314. static struct rt6_info *rt6_add_route_info(struct in6_addr *prefix, int prefixlen,
  1315. struct in6_addr *gwaddr, int ifindex,
  1316. unsigned pref)
  1317. {
  1318. struct in6_rtmsg rtmsg;
  1319. memset(&rtmsg, 0, sizeof(rtmsg));
  1320. rtmsg.rtmsg_type = RTMSG_NEWROUTE;
  1321. ipv6_addr_copy(&rtmsg.rtmsg_dst, prefix);
  1322. rtmsg.rtmsg_dst_len = prefixlen;
  1323. ipv6_addr_copy(&rtmsg.rtmsg_gateway, gwaddr);
  1324. rtmsg.rtmsg_metric = 1024;
  1325. rtmsg.rtmsg_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | RTF_UP | RTF_PREF(pref);
  1326. /* We should treat it as a default route if prefix length is 0. */
  1327. if (!prefixlen)
  1328. rtmsg.rtmsg_flags |= RTF_DEFAULT;
  1329. rtmsg.rtmsg_ifindex = ifindex;
  1330. ip6_route_add(&rtmsg, NULL, NULL, NULL, RT6_TABLE_INFO);
  1331. return rt6_get_route_info(prefix, prefixlen, gwaddr, ifindex);
  1332. }
  1333. #endif
  1334. struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
  1335. {
  1336. struct rt6_info *rt;
  1337. struct fib6_table *table;
  1338. table = fib6_get_table(RT6_TABLE_DFLT);
  1339. if (table == NULL)
  1340. return NULL;
  1341. write_lock_bh(&table->tb6_lock);
  1342. for (rt = table->tb6_root.leaf; rt; rt=rt->u.next) {
  1343. if (dev == rt->rt6i_dev &&
  1344. ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
  1345. ipv6_addr_equal(&rt->rt6i_gateway, addr))
  1346. break;
  1347. }
  1348. if (rt)
  1349. dst_hold(&rt->u.dst);
  1350. write_unlock_bh(&table->tb6_lock);
  1351. return rt;
  1352. }
  1353. struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
  1354. struct net_device *dev,
  1355. unsigned int pref)
  1356. {
  1357. struct in6_rtmsg rtmsg;
  1358. memset(&rtmsg, 0, sizeof(struct in6_rtmsg));
  1359. rtmsg.rtmsg_type = RTMSG_NEWROUTE;
  1360. ipv6_addr_copy(&rtmsg.rtmsg_gateway, gwaddr);
  1361. rtmsg.rtmsg_metric = 1024;
  1362. rtmsg.rtmsg_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | RTF_UP | RTF_EXPIRES |
  1363. RTF_PREF(pref);
  1364. rtmsg.rtmsg_ifindex = dev->ifindex;
  1365. ip6_route_add(&rtmsg, NULL, NULL, NULL, RT6_TABLE_DFLT);
  1366. return rt6_get_dflt_router(gwaddr, dev);
  1367. }
  1368. void rt6_purge_dflt_routers(void)
  1369. {
  1370. struct rt6_info *rt;
  1371. struct fib6_table *table;
  1372. /* NOTE: Keep consistent with rt6_get_dflt_router */
  1373. table = fib6_get_table(RT6_TABLE_DFLT);
  1374. if (table == NULL)
  1375. return;
  1376. restart:
  1377. read_lock_bh(&table->tb6_lock);
  1378. for (rt = table->tb6_root.leaf; rt; rt = rt->u.next) {
  1379. if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
  1380. dst_hold(&rt->u.dst);
  1381. read_unlock_bh(&table->tb6_lock);
  1382. ip6_del_rt(rt, NULL, NULL, NULL);
  1383. goto restart;
  1384. }
  1385. }
  1386. read_unlock_bh(&table->tb6_lock);
  1387. }
  1388. int ipv6_route_ioctl(unsigned int cmd, void __user *arg)
  1389. {
  1390. struct in6_rtmsg rtmsg;
  1391. int err;
  1392. switch(cmd) {
  1393. case SIOCADDRT: /* Add a route */
  1394. case SIOCDELRT: /* Delete a route */
  1395. if (!capable(CAP_NET_ADMIN))
  1396. return -EPERM;
  1397. err = copy_from_user(&rtmsg, arg,
  1398. sizeof(struct in6_rtmsg));
  1399. if (err)
  1400. return -EFAULT;
  1401. rtnl_lock();
  1402. switch (cmd) {
  1403. case SIOCADDRT:
  1404. err = ip6_route_add(&rtmsg, NULL, NULL, NULL,
  1405. RT6_TABLE_MAIN);
  1406. break;
  1407. case SIOCDELRT:
  1408. err = ip6_route_del(&rtmsg, NULL, NULL, NULL,
  1409. RT6_TABLE_MAIN);
  1410. break;
  1411. default:
  1412. err = -EINVAL;
  1413. }
  1414. rtnl_unlock();
  1415. return err;
  1416. };
  1417. return -EINVAL;
  1418. }
  1419. /*
  1420. * Drop the packet on the floor
  1421. */
  1422. static int ip6_pkt_discard(struct sk_buff *skb)
  1423. {
  1424. int type = ipv6_addr_type(&skb->nh.ipv6h->daddr);
  1425. if (type == IPV6_ADDR_ANY || type == IPV6_ADDR_RESERVED)
  1426. IP6_INC_STATS(IPSTATS_MIB_INADDRERRORS);
  1427. IP6_INC_STATS(IPSTATS_MIB_OUTNOROUTES);
  1428. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_NOROUTE, 0, skb->dev);
  1429. kfree_skb(skb);
  1430. return 0;
  1431. }
  1432. static int ip6_pkt_discard_out(struct sk_buff *skb)
  1433. {
  1434. skb->dev = skb->dst->dev;
  1435. return ip6_pkt_discard(skb);
  1436. }
  1437. /*
  1438. * Allocate a dst for local (unicast / anycast) address.
  1439. */
  1440. struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
  1441. const struct in6_addr *addr,
  1442. int anycast)
  1443. {
  1444. struct rt6_info *rt = ip6_dst_alloc();
  1445. if (rt == NULL)
  1446. return ERR_PTR(-ENOMEM);
  1447. dev_hold(&loopback_dev);
  1448. in6_dev_hold(idev);
  1449. rt->u.dst.flags = DST_HOST;
  1450. rt->u.dst.input = ip6_input;
  1451. rt->u.dst.output = ip6_output;
  1452. rt->rt6i_dev = &loopback_dev;
  1453. rt->rt6i_idev = idev;
  1454. rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
  1455. rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
  1456. rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
  1457. rt->u.dst.obsolete = -1;
  1458. rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
  1459. if (anycast)
  1460. rt->rt6i_flags |= RTF_ANYCAST;
  1461. else
  1462. rt->rt6i_flags |= RTF_LOCAL;
  1463. rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
  1464. if (rt->rt6i_nexthop == NULL) {
  1465. dst_free((struct dst_entry *) rt);
  1466. return ERR_PTR(-ENOMEM);
  1467. }
  1468. ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
  1469. rt->rt6i_dst.plen = 128;
  1470. rt->rt6i_table = fib6_get_table(RT6_TABLE_LOCAL);
  1471. atomic_set(&rt->u.dst.__refcnt, 1);
  1472. return rt;
  1473. }
  1474. static int fib6_ifdown(struct rt6_info *rt, void *arg)
  1475. {
  1476. if (((void*)rt->rt6i_dev == arg || arg == NULL) &&
  1477. rt != &ip6_null_entry) {
  1478. RT6_TRACE("deleted by ifdown %p\n", rt);
  1479. return -1;
  1480. }
  1481. return 0;
  1482. }
  1483. void rt6_ifdown(struct net_device *dev)
  1484. {
  1485. fib6_clean_all(fib6_ifdown, 0, dev);
  1486. }
  1487. struct rt6_mtu_change_arg
  1488. {
  1489. struct net_device *dev;
  1490. unsigned mtu;
  1491. };
  1492. static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
  1493. {
  1494. struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
  1495. struct inet6_dev *idev;
  1496. /* In IPv6 pmtu discovery is not optional,
  1497. so that RTAX_MTU lock cannot disable it.
  1498. We still use this lock to block changes
  1499. caused by addrconf/ndisc.
  1500. */
  1501. idev = __in6_dev_get(arg->dev);
  1502. if (idev == NULL)
  1503. return 0;
  1504. /* For administrative MTU increase, there is no way to discover
  1505. IPv6 PMTU increase, so PMTU increase should be updated here.
  1506. Since RFC 1981 doesn't include administrative MTU increase
  1507. update PMTU increase is a MUST. (i.e. jumbo frame)
  1508. */
  1509. /*
  1510. If new MTU is less than route PMTU, this new MTU will be the
  1511. lowest MTU in the path, update the route PMTU to reflect PMTU
  1512. decreases; if new MTU is greater than route PMTU, and the
  1513. old MTU is the lowest MTU in the path, update the route PMTU
  1514. to reflect the increase. In this case if the other nodes' MTU
  1515. also have the lowest MTU, TOO BIG MESSAGE will be lead to
  1516. PMTU discouvery.
  1517. */
  1518. if (rt->rt6i_dev == arg->dev &&
  1519. !dst_metric_locked(&rt->u.dst, RTAX_MTU) &&
  1520. (dst_mtu(&rt->u.dst) > arg->mtu ||
  1521. (dst_mtu(&rt->u.dst) < arg->mtu &&
  1522. dst_mtu(&rt->u.dst) == idev->cnf.mtu6)))
  1523. rt->u.dst.metrics[RTAX_MTU-1] = arg->mtu;
  1524. rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(arg->mtu);
  1525. return 0;
  1526. }
  1527. void rt6_mtu_change(struct net_device *dev, unsigned mtu)
  1528. {
  1529. struct rt6_mtu_change_arg arg = {
  1530. .dev = dev,
  1531. .mtu = mtu,
  1532. };
  1533. fib6_clean_all(rt6_mtu_change_route, 0, &arg);
  1534. }
  1535. static int inet6_rtm_to_rtmsg(struct rtmsg *r, struct rtattr **rta,
  1536. struct in6_rtmsg *rtmsg)
  1537. {
  1538. memset(rtmsg, 0, sizeof(*rtmsg));
  1539. rtmsg->rtmsg_dst_len = r->rtm_dst_len;
  1540. rtmsg->rtmsg_src_len = r->rtm_src_len;
  1541. rtmsg->rtmsg_flags = RTF_UP;
  1542. if (r->rtm_type == RTN_UNREACHABLE)
  1543. rtmsg->rtmsg_flags |= RTF_REJECT;
  1544. if (rta[RTA_GATEWAY-1]) {
  1545. if (rta[RTA_GATEWAY-1]->rta_len != RTA_LENGTH(16))
  1546. return -EINVAL;
  1547. memcpy(&rtmsg->rtmsg_gateway, RTA_DATA(rta[RTA_GATEWAY-1]), 16);
  1548. rtmsg->rtmsg_flags |= RTF_GATEWAY;
  1549. }
  1550. if (rta[RTA_DST-1]) {
  1551. if (RTA_PAYLOAD(rta[RTA_DST-1]) < ((r->rtm_dst_len+7)>>3))
  1552. return -EINVAL;
  1553. memcpy(&rtmsg->rtmsg_dst, RTA_DATA(rta[RTA_DST-1]), ((r->rtm_dst_len+7)>>3));
  1554. }
  1555. if (rta[RTA_SRC-1]) {
  1556. if (RTA_PAYLOAD(rta[RTA_SRC-1]) < ((r->rtm_src_len+7)>>3))
  1557. return -EINVAL;
  1558. memcpy(&rtmsg->rtmsg_src, RTA_DATA(rta[RTA_SRC-1]), ((r->rtm_src_len+7)>>3));
  1559. }
  1560. if (rta[RTA_OIF-1]) {
  1561. if (rta[RTA_OIF-1]->rta_len != RTA_LENGTH(sizeof(int)))
  1562. return -EINVAL;
  1563. memcpy(&rtmsg->rtmsg_ifindex, RTA_DATA(rta[RTA_OIF-1]), sizeof(int));
  1564. }
  1565. if (rta[RTA_PRIORITY-1]) {
  1566. if (rta[RTA_PRIORITY-1]->rta_len != RTA_LENGTH(4))
  1567. return -EINVAL;
  1568. memcpy(&rtmsg->rtmsg_metric, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
  1569. }
  1570. return 0;
  1571. }
  1572. int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  1573. {
  1574. struct rtmsg *r = NLMSG_DATA(nlh);
  1575. struct in6_rtmsg rtmsg;
  1576. if (inet6_rtm_to_rtmsg(r, arg, &rtmsg))
  1577. return -EINVAL;
  1578. return ip6_route_del(&rtmsg, nlh, arg, &NETLINK_CB(skb),
  1579. rtm_get_table(arg, r->rtm_table));
  1580. }
  1581. int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  1582. {
  1583. struct rtmsg *r = NLMSG_DATA(nlh);
  1584. struct in6_rtmsg rtmsg;
  1585. if (inet6_rtm_to_rtmsg(r, arg, &rtmsg))
  1586. return -EINVAL;
  1587. return ip6_route_add(&rtmsg, nlh, arg, &NETLINK_CB(skb),
  1588. rtm_get_table(arg, r->rtm_table));
  1589. }
  1590. static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
  1591. struct in6_addr *dst, struct in6_addr *src,
  1592. int iif, int type, u32 pid, u32 seq,
  1593. int prefix, unsigned int flags)
  1594. {
  1595. struct rtmsg *rtm;
  1596. struct nlmsghdr *nlh;
  1597. unsigned char *b = skb->tail;
  1598. struct rta_cacheinfo ci;
  1599. u32 table;
  1600. if (prefix) { /* user wants prefix routes only */
  1601. if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
  1602. /* success since this is not a prefix route */
  1603. return 1;
  1604. }
  1605. }
  1606. nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*rtm), flags);
  1607. rtm = NLMSG_DATA(nlh);
  1608. rtm->rtm_family = AF_INET6;
  1609. rtm->rtm_dst_len = rt->rt6i_dst.plen;
  1610. rtm->rtm_src_len = rt->rt6i_src.plen;
  1611. rtm->rtm_tos = 0;
  1612. if (rt->rt6i_table)
  1613. table = rt->rt6i_table->tb6_id;
  1614. else
  1615. table = RT6_TABLE_UNSPEC;
  1616. rtm->rtm_table = table;
  1617. RTA_PUT_U32(skb, RTA_TABLE, table);
  1618. if (rt->rt6i_flags&RTF_REJECT)
  1619. rtm->rtm_type = RTN_UNREACHABLE;
  1620. else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
  1621. rtm->rtm_type = RTN_LOCAL;
  1622. else
  1623. rtm->rtm_type = RTN_UNICAST;
  1624. rtm->rtm_flags = 0;
  1625. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1626. rtm->rtm_protocol = rt->rt6i_protocol;
  1627. if (rt->rt6i_flags&RTF_DYNAMIC)
  1628. rtm->rtm_protocol = RTPROT_REDIRECT;
  1629. else if (rt->rt6i_flags & RTF_ADDRCONF)
  1630. rtm->rtm_protocol = RTPROT_KERNEL;
  1631. else if (rt->rt6i_flags&RTF_DEFAULT)
  1632. rtm->rtm_protocol = RTPROT_RA;
  1633. if (rt->rt6i_flags&RTF_CACHE)
  1634. rtm->rtm_flags |= RTM_F_CLONED;
  1635. if (dst) {
  1636. RTA_PUT(skb, RTA_DST, 16, dst);
  1637. rtm->rtm_dst_len = 128;
  1638. } else if (rtm->rtm_dst_len)
  1639. RTA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
  1640. #ifdef CONFIG_IPV6_SUBTREES
  1641. if (src) {
  1642. RTA_PUT(skb, RTA_SRC, 16, src);
  1643. rtm->rtm_src_len = 128;
  1644. } else if (rtm->rtm_src_len)
  1645. RTA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
  1646. #endif
  1647. if (iif)
  1648. RTA_PUT(skb, RTA_IIF, 4, &iif);
  1649. else if (dst) {
  1650. struct in6_addr saddr_buf;
  1651. if (ipv6_get_saddr(&rt->u.dst, dst, &saddr_buf) == 0)
  1652. RTA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
  1653. }
  1654. if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
  1655. goto rtattr_failure;
  1656. if (rt->u.dst.neighbour)
  1657. RTA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key);
  1658. if (rt->u.dst.dev)
  1659. RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->rt6i_dev->ifindex);
  1660. RTA_PUT(skb, RTA_PRIORITY, 4, &rt->rt6i_metric);
  1661. ci.rta_lastuse = jiffies_to_clock_t(jiffies - rt->u.dst.lastuse);
  1662. if (rt->rt6i_expires)
  1663. ci.rta_expires = jiffies_to_clock_t(rt->rt6i_expires - jiffies);
  1664. else
  1665. ci.rta_expires = 0;
  1666. ci.rta_used = rt->u.dst.__use;
  1667. ci.rta_clntref = atomic_read(&rt->u.dst.__refcnt);
  1668. ci.rta_error = rt->u.dst.error;
  1669. ci.rta_id = 0;
  1670. ci.rta_ts = 0;
  1671. ci.rta_tsage = 0;
  1672. RTA_PUT(skb, RTA_CACHEINFO, sizeof(ci), &ci);
  1673. nlh->nlmsg_len = skb->tail - b;
  1674. return skb->len;
  1675. nlmsg_failure:
  1676. rtattr_failure:
  1677. skb_trim(skb, b - skb->data);
  1678. return -1;
  1679. }
  1680. int rt6_dump_route(struct rt6_info *rt, void *p_arg)
  1681. {
  1682. struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
  1683. int prefix;
  1684. if (arg->cb->nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(struct rtmsg))) {
  1685. struct rtmsg *rtm = NLMSG_DATA(arg->cb->nlh);
  1686. prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
  1687. } else
  1688. prefix = 0;
  1689. return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
  1690. NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
  1691. prefix, NLM_F_MULTI);
  1692. }
  1693. int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
  1694. {
  1695. struct rtattr **rta = arg;
  1696. int iif = 0;
  1697. int err = -ENOBUFS;
  1698. struct sk_buff *skb;
  1699. struct flowi fl;
  1700. struct rt6_info *rt;
  1701. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1702. if (skb == NULL)
  1703. goto out;
  1704. /* Reserve room for dummy headers, this skb can pass
  1705. through good chunk of routing engine.
  1706. */
  1707. skb->mac.raw = skb->data;
  1708. skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
  1709. memset(&fl, 0, sizeof(fl));
  1710. if (rta[RTA_SRC-1])
  1711. ipv6_addr_copy(&fl.fl6_src,
  1712. (struct in6_addr*)RTA_DATA(rta[RTA_SRC-1]));
  1713. if (rta[RTA_DST-1])
  1714. ipv6_addr_copy(&fl.fl6_dst,
  1715. (struct in6_addr*)RTA_DATA(rta[RTA_DST-1]));
  1716. if (rta[RTA_IIF-1])
  1717. memcpy(&iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
  1718. if (iif) {
  1719. struct net_device *dev;
  1720. dev = __dev_get_by_index(iif);
  1721. if (!dev) {
  1722. err = -ENODEV;
  1723. goto out_free;
  1724. }
  1725. }
  1726. fl.oif = 0;
  1727. if (rta[RTA_OIF-1])
  1728. memcpy(&fl.oif, RTA_DATA(rta[RTA_OIF-1]), sizeof(int));
  1729. rt = (struct rt6_info*)ip6_route_output(NULL, &fl);
  1730. skb->dst = &rt->u.dst;
  1731. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  1732. err = rt6_fill_node(skb, rt,
  1733. &fl.fl6_dst, &fl.fl6_src,
  1734. iif,
  1735. RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
  1736. nlh->nlmsg_seq, 0, 0);
  1737. if (err < 0) {
  1738. err = -EMSGSIZE;
  1739. goto out_free;
  1740. }
  1741. err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
  1742. out:
  1743. return err;
  1744. out_free:
  1745. kfree_skb(skb);
  1746. goto out;
  1747. }
  1748. void inet6_rt_notify(int event, struct rt6_info *rt, struct nlmsghdr *nlh,
  1749. struct netlink_skb_parms *req)
  1750. {
  1751. struct sk_buff *skb;
  1752. int size = NLMSG_SPACE(sizeof(struct rtmsg)+256);
  1753. u32 pid = current->pid;
  1754. u32 seq = 0;
  1755. if (req)
  1756. pid = req->pid;
  1757. if (nlh)
  1758. seq = nlh->nlmsg_seq;
  1759. skb = alloc_skb(size, gfp_any());
  1760. if (!skb) {
  1761. netlink_set_err(rtnl, 0, RTNLGRP_IPV6_ROUTE, ENOBUFS);
  1762. return;
  1763. }
  1764. if (rt6_fill_node(skb, rt, NULL, NULL, 0, event, pid, seq, 0, 0) < 0) {
  1765. kfree_skb(skb);
  1766. netlink_set_err(rtnl, 0, RTNLGRP_IPV6_ROUTE, EINVAL);
  1767. return;
  1768. }
  1769. NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_ROUTE;
  1770. netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_ROUTE, gfp_any());
  1771. }
  1772. /*
  1773. * /proc
  1774. */
  1775. #ifdef CONFIG_PROC_FS
  1776. #define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
  1777. struct rt6_proc_arg
  1778. {
  1779. char *buffer;
  1780. int offset;
  1781. int length;
  1782. int skip;
  1783. int len;
  1784. };
  1785. static int rt6_info_route(struct rt6_info *rt, void *p_arg)
  1786. {
  1787. struct rt6_proc_arg *arg = (struct rt6_proc_arg *) p_arg;
  1788. int i;
  1789. if (arg->skip < arg->offset / RT6_INFO_LEN) {
  1790. arg->skip++;
  1791. return 0;
  1792. }
  1793. if (arg->len >= arg->length)
  1794. return 0;
  1795. for (i=0; i<16; i++) {
  1796. sprintf(arg->buffer + arg->len, "%02x",
  1797. rt->rt6i_dst.addr.s6_addr[i]);
  1798. arg->len += 2;
  1799. }
  1800. arg->len += sprintf(arg->buffer + arg->len, " %02x ",
  1801. rt->rt6i_dst.plen);
  1802. #ifdef CONFIG_IPV6_SUBTREES
  1803. for (i=0; i<16; i++) {
  1804. sprintf(arg->buffer + arg->len, "%02x",
  1805. rt->rt6i_src.addr.s6_addr[i]);
  1806. arg->len += 2;
  1807. }
  1808. arg->len += sprintf(arg->buffer + arg->len, " %02x ",
  1809. rt->rt6i_src.plen);
  1810. #else
  1811. sprintf(arg->buffer + arg->len,
  1812. "00000000000000000000000000000000 00 ");
  1813. arg->len += 36;
  1814. #endif
  1815. if (rt->rt6i_nexthop) {
  1816. for (i=0; i<16; i++) {
  1817. sprintf(arg->buffer + arg->len, "%02x",
  1818. rt->rt6i_nexthop->primary_key[i]);
  1819. arg->len += 2;
  1820. }
  1821. } else {
  1822. sprintf(arg->buffer + arg->len,
  1823. "00000000000000000000000000000000");
  1824. arg->len += 32;
  1825. }
  1826. arg->len += sprintf(arg->buffer + arg->len,
  1827. " %08x %08x %08x %08x %8s\n",
  1828. rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
  1829. rt->u.dst.__use, rt->rt6i_flags,
  1830. rt->rt6i_dev ? rt->rt6i_dev->name : "");
  1831. return 0;
  1832. }
  1833. static int rt6_proc_info(char *buffer, char **start, off_t offset, int length)
  1834. {
  1835. struct rt6_proc_arg arg = {
  1836. .buffer = buffer,
  1837. .offset = offset,
  1838. .length = length,
  1839. };
  1840. fib6_clean_all(rt6_info_route, 0, &arg);
  1841. *start = buffer;
  1842. if (offset)
  1843. *start += offset % RT6_INFO_LEN;
  1844. arg.len -= offset % RT6_INFO_LEN;
  1845. if (arg.len > length)
  1846. arg.len = length;
  1847. if (arg.len < 0)
  1848. arg.len = 0;
  1849. return arg.len;
  1850. }
  1851. static int rt6_stats_seq_show(struct seq_file *seq, void *v)
  1852. {
  1853. seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
  1854. rt6_stats.fib_nodes, rt6_stats.fib_route_nodes,
  1855. rt6_stats.fib_rt_alloc, rt6_stats.fib_rt_entries,
  1856. rt6_stats.fib_rt_cache,
  1857. atomic_read(&ip6_dst_ops.entries),
  1858. rt6_stats.fib_discarded_routes);
  1859. return 0;
  1860. }
  1861. static int rt6_stats_seq_open(struct inode *inode, struct file *file)
  1862. {
  1863. return single_open(file, rt6_stats_seq_show, NULL);
  1864. }
  1865. static struct file_operations rt6_stats_seq_fops = {
  1866. .owner = THIS_MODULE,
  1867. .open = rt6_stats_seq_open,
  1868. .read = seq_read,
  1869. .llseek = seq_lseek,
  1870. .release = single_release,
  1871. };
  1872. #endif /* CONFIG_PROC_FS */
  1873. #ifdef CONFIG_SYSCTL
  1874. static int flush_delay;
  1875. static
  1876. int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp,
  1877. void __user *buffer, size_t *lenp, loff_t *ppos)
  1878. {
  1879. if (write) {
  1880. proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  1881. fib6_run_gc(flush_delay <= 0 ? ~0UL : (unsigned long)flush_delay);
  1882. return 0;
  1883. } else
  1884. return -EINVAL;
  1885. }
  1886. ctl_table ipv6_route_table[] = {
  1887. {
  1888. .ctl_name = NET_IPV6_ROUTE_FLUSH,
  1889. .procname = "flush",
  1890. .data = &flush_delay,
  1891. .maxlen = sizeof(int),
  1892. .mode = 0200,
  1893. .proc_handler = &ipv6_sysctl_rtcache_flush
  1894. },
  1895. {
  1896. .ctl_name = NET_IPV6_ROUTE_GC_THRESH,
  1897. .procname = "gc_thresh",
  1898. .data = &ip6_dst_ops.gc_thresh,
  1899. .maxlen = sizeof(int),
  1900. .mode = 0644,
  1901. .proc_handler = &proc_dointvec,
  1902. },
  1903. {
  1904. .ctl_name = NET_IPV6_ROUTE_MAX_SIZE,
  1905. .procname = "max_size",
  1906. .data = &ip6_rt_max_size,
  1907. .maxlen = sizeof(int),
  1908. .mode = 0644,
  1909. .proc_handler = &proc_dointvec,
  1910. },
  1911. {
  1912. .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL,
  1913. .procname = "gc_min_interval",
  1914. .data = &ip6_rt_gc_min_interval,
  1915. .maxlen = sizeof(int),
  1916. .mode = 0644,
  1917. .proc_handler = &proc_dointvec_jiffies,
  1918. .strategy = &sysctl_jiffies,
  1919. },
  1920. {
  1921. .ctl_name = NET_IPV6_ROUTE_GC_TIMEOUT,
  1922. .procname = "gc_timeout",
  1923. .data = &ip6_rt_gc_timeout,
  1924. .maxlen = sizeof(int),
  1925. .mode = 0644,
  1926. .proc_handler = &proc_dointvec_jiffies,
  1927. .strategy = &sysctl_jiffies,
  1928. },
  1929. {
  1930. .ctl_name = NET_IPV6_ROUTE_GC_INTERVAL,
  1931. .procname = "gc_interval",
  1932. .data = &ip6_rt_gc_interval,
  1933. .maxlen = sizeof(int),
  1934. .mode = 0644,
  1935. .proc_handler = &proc_dointvec_jiffies,
  1936. .strategy = &sysctl_jiffies,
  1937. },
  1938. {
  1939. .ctl_name = NET_IPV6_ROUTE_GC_ELASTICITY,
  1940. .procname = "gc_elasticity",
  1941. .data = &ip6_rt_gc_elasticity,
  1942. .maxlen = sizeof(int),
  1943. .mode = 0644,
  1944. .proc_handler = &proc_dointvec_jiffies,
  1945. .strategy = &sysctl_jiffies,
  1946. },
  1947. {
  1948. .ctl_name = NET_IPV6_ROUTE_MTU_EXPIRES,
  1949. .procname = "mtu_expires",
  1950. .data = &ip6_rt_mtu_expires,
  1951. .maxlen = sizeof(int),
  1952. .mode = 0644,
  1953. .proc_handler = &proc_dointvec_jiffies,
  1954. .strategy = &sysctl_jiffies,
  1955. },
  1956. {
  1957. .ctl_name = NET_IPV6_ROUTE_MIN_ADVMSS,
  1958. .procname = "min_adv_mss",
  1959. .data = &ip6_rt_min_advmss,
  1960. .maxlen = sizeof(int),
  1961. .mode = 0644,
  1962. .proc_handler = &proc_dointvec_jiffies,
  1963. .strategy = &sysctl_jiffies,
  1964. },
  1965. {
  1966. .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS,
  1967. .procname = "gc_min_interval_ms",
  1968. .data = &ip6_rt_gc_min_interval,
  1969. .maxlen = sizeof(int),
  1970. .mode = 0644,
  1971. .proc_handler = &proc_dointvec_ms_jiffies,
  1972. .strategy = &sysctl_ms_jiffies,
  1973. },
  1974. { .ctl_name = 0 }
  1975. };
  1976. #endif
  1977. void __init ip6_route_init(void)
  1978. {
  1979. struct proc_dir_entry *p;
  1980. ip6_dst_ops.kmem_cachep = kmem_cache_create("ip6_dst_cache",
  1981. sizeof(struct rt6_info),
  1982. 0, SLAB_HWCACHE_ALIGN,
  1983. NULL, NULL);
  1984. if (!ip6_dst_ops.kmem_cachep)
  1985. panic("cannot create ip6_dst_cache");
  1986. fib6_init();
  1987. #ifdef CONFIG_PROC_FS
  1988. p = proc_net_create("ipv6_route", 0, rt6_proc_info);
  1989. if (p)
  1990. p->owner = THIS_MODULE;
  1991. proc_net_fops_create("rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
  1992. #endif
  1993. #ifdef CONFIG_XFRM
  1994. xfrm6_init();
  1995. #endif
  1996. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1997. fib6_rules_init();
  1998. #endif
  1999. }
  2000. void ip6_route_cleanup(void)
  2001. {
  2002. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2003. fib6_rules_cleanup();
  2004. #endif
  2005. #ifdef CONFIG_PROC_FS
  2006. proc_net_remove("ipv6_route");
  2007. proc_net_remove("rt6_stats");
  2008. #endif
  2009. #ifdef CONFIG_XFRM
  2010. xfrm6_fini();
  2011. #endif
  2012. rt6_ifdown(NULL);
  2013. fib6_gc_cleanup();
  2014. kmem_cache_destroy(ip6_dst_ops.kmem_cachep);
  2015. }