route.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456
  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. * Ville Nuorvala
  25. * Fixed routing subtrees.
  26. */
  27. #include <linux/capability.h>
  28. #include <linux/errno.h>
  29. #include <linux/types.h>
  30. #include <linux/times.h>
  31. #include <linux/socket.h>
  32. #include <linux/sockios.h>
  33. #include <linux/net.h>
  34. #include <linux/route.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/in6.h>
  37. #include <linux/init.h>
  38. #include <linux/if_arp.h>
  39. #ifdef CONFIG_PROC_FS
  40. #include <linux/proc_fs.h>
  41. #include <linux/seq_file.h>
  42. #endif
  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. #define CLONE_OFFLINK_ROUTE 0
  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_LOOKUP_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_LOOKUP_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_LOOKUP_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);
  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(saddr) \
  425. do { \
  426. if (rt == &ip6_null_entry) { \
  427. struct fib6_node *pn; \
  428. while (fn) { \
  429. if (fn->fn_flags & RTN_TL_ROOT) \
  430. goto out; \
  431. pn = fn->parent; \
  432. if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
  433. fn = fib6_lookup(pn->subtree, NULL, saddr); \
  434. else \
  435. fn = pn; \
  436. if (fn->fn_flags & RTN_RTINFO) \
  437. goto restart; \
  438. } \
  439. } \
  440. } while(0)
  441. static struct rt6_info *ip6_pol_route_lookup(struct fib6_table *table,
  442. struct flowi *fl, int flags)
  443. {
  444. struct fib6_node *fn;
  445. struct rt6_info *rt;
  446. read_lock_bh(&table->tb6_lock);
  447. fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
  448. restart:
  449. rt = fn->leaf;
  450. rt = rt6_device_match(rt, fl->oif, flags);
  451. BACKTRACK(&fl->fl6_src);
  452. out:
  453. dst_hold(&rt->u.dst);
  454. read_unlock_bh(&table->tb6_lock);
  455. rt->u.dst.lastuse = jiffies;
  456. rt->u.dst.__use++;
  457. return rt;
  458. }
  459. struct rt6_info *rt6_lookup(struct in6_addr *daddr, struct in6_addr *saddr,
  460. int oif, int strict)
  461. {
  462. struct flowi fl = {
  463. .oif = oif,
  464. .nl_u = {
  465. .ip6_u = {
  466. .daddr = *daddr,
  467. /* TODO: saddr */
  468. },
  469. },
  470. };
  471. struct dst_entry *dst;
  472. int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
  473. dst = fib6_rule_lookup(&fl, flags, ip6_pol_route_lookup);
  474. if (dst->error == 0)
  475. return (struct rt6_info *) dst;
  476. dst_release(dst);
  477. return NULL;
  478. }
  479. /* ip6_ins_rt is called with FREE table->tb6_lock.
  480. It takes new route entry, the addition fails by any reason the
  481. route is freed. In any case, if caller does not hold it, it may
  482. be destroyed.
  483. */
  484. static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
  485. {
  486. int err;
  487. struct fib6_table *table;
  488. table = rt->rt6i_table;
  489. write_lock_bh(&table->tb6_lock);
  490. err = fib6_add(&table->tb6_root, rt, info);
  491. write_unlock_bh(&table->tb6_lock);
  492. return err;
  493. }
  494. int ip6_ins_rt(struct rt6_info *rt)
  495. {
  496. return __ip6_ins_rt(rt, NULL);
  497. }
  498. static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
  499. struct in6_addr *saddr)
  500. {
  501. struct rt6_info *rt;
  502. /*
  503. * Clone the route.
  504. */
  505. rt = ip6_rt_copy(ort);
  506. if (rt) {
  507. if (!(rt->rt6i_flags&RTF_GATEWAY)) {
  508. if (rt->rt6i_dst.plen != 128 &&
  509. ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
  510. rt->rt6i_flags |= RTF_ANYCAST;
  511. ipv6_addr_copy(&rt->rt6i_gateway, daddr);
  512. }
  513. ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
  514. rt->rt6i_dst.plen = 128;
  515. rt->rt6i_flags |= RTF_CACHE;
  516. rt->u.dst.flags |= DST_HOST;
  517. #ifdef CONFIG_IPV6_SUBTREES
  518. if (rt->rt6i_src.plen && saddr) {
  519. ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
  520. rt->rt6i_src.plen = 128;
  521. }
  522. #endif
  523. rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
  524. }
  525. return rt;
  526. }
  527. static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
  528. {
  529. struct rt6_info *rt = ip6_rt_copy(ort);
  530. if (rt) {
  531. ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
  532. rt->rt6i_dst.plen = 128;
  533. rt->rt6i_flags |= RTF_CACHE;
  534. if (rt->rt6i_flags & RTF_REJECT)
  535. rt->u.dst.error = ort->u.dst.error;
  536. rt->u.dst.flags |= DST_HOST;
  537. rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
  538. }
  539. return rt;
  540. }
  541. static struct rt6_info *ip6_pol_route_input(struct fib6_table *table,
  542. struct flowi *fl, int flags)
  543. {
  544. struct fib6_node *fn;
  545. struct rt6_info *rt, *nrt;
  546. int strict = 0;
  547. int attempts = 3;
  548. int err;
  549. int reachable = RT6_LOOKUP_F_REACHABLE;
  550. strict |= flags & RT6_LOOKUP_F_IFACE;
  551. relookup:
  552. read_lock_bh(&table->tb6_lock);
  553. restart_2:
  554. fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
  555. restart:
  556. rt = rt6_select(&fn->leaf, fl->iif, strict | reachable);
  557. BACKTRACK(&fl->fl6_src);
  558. if (rt == &ip6_null_entry ||
  559. rt->rt6i_flags & RTF_CACHE)
  560. goto out;
  561. dst_hold(&rt->u.dst);
  562. read_unlock_bh(&table->tb6_lock);
  563. if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
  564. nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
  565. else {
  566. #if CLONE_OFFLINK_ROUTE
  567. nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
  568. #else
  569. goto out2;
  570. #endif
  571. }
  572. dst_release(&rt->u.dst);
  573. rt = nrt ? : &ip6_null_entry;
  574. dst_hold(&rt->u.dst);
  575. if (nrt) {
  576. err = ip6_ins_rt(nrt);
  577. if (!err)
  578. goto out2;
  579. }
  580. if (--attempts <= 0)
  581. goto out2;
  582. /*
  583. * Race condition! In the gap, when table->tb6_lock was
  584. * released someone could insert this route. Relookup.
  585. */
  586. dst_release(&rt->u.dst);
  587. goto relookup;
  588. out:
  589. if (reachable) {
  590. reachable = 0;
  591. goto restart_2;
  592. }
  593. dst_hold(&rt->u.dst);
  594. read_unlock_bh(&table->tb6_lock);
  595. out2:
  596. rt->u.dst.lastuse = jiffies;
  597. rt->u.dst.__use++;
  598. return rt;
  599. }
  600. void ip6_route_input(struct sk_buff *skb)
  601. {
  602. struct ipv6hdr *iph = skb->nh.ipv6h;
  603. struct flowi fl = {
  604. .iif = skb->dev->ifindex,
  605. .nl_u = {
  606. .ip6_u = {
  607. .daddr = iph->daddr,
  608. .saddr = iph->saddr,
  609. #ifdef CONFIG_IPV6_ROUTE_FWMARK
  610. .fwmark = skb->nfmark,
  611. #endif
  612. .flowlabel = (* (u32 *) iph)&IPV6_FLOWINFO_MASK,
  613. },
  614. },
  615. .proto = iph->nexthdr,
  616. };
  617. int flags = rt6_need_strict(&iph->daddr) ? RT6_LOOKUP_F_IFACE : 0;
  618. skb->dst = fib6_rule_lookup(&fl, flags, ip6_pol_route_input);
  619. }
  620. static struct rt6_info *ip6_pol_route_output(struct fib6_table *table,
  621. struct flowi *fl, int flags)
  622. {
  623. struct fib6_node *fn;
  624. struct rt6_info *rt, *nrt;
  625. int strict = 0;
  626. int attempts = 3;
  627. int err;
  628. int reachable = RT6_LOOKUP_F_REACHABLE;
  629. strict |= flags & RT6_LOOKUP_F_IFACE;
  630. relookup:
  631. read_lock_bh(&table->tb6_lock);
  632. restart_2:
  633. fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
  634. restart:
  635. rt = rt6_select(&fn->leaf, fl->oif, strict | reachable);
  636. BACKTRACK(&fl->fl6_src);
  637. if (rt == &ip6_null_entry ||
  638. rt->rt6i_flags & RTF_CACHE)
  639. goto out;
  640. dst_hold(&rt->u.dst);
  641. read_unlock_bh(&table->tb6_lock);
  642. if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
  643. nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
  644. else {
  645. #if CLONE_OFFLINK_ROUTE
  646. nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
  647. #else
  648. goto out2;
  649. #endif
  650. }
  651. dst_release(&rt->u.dst);
  652. rt = nrt ? : &ip6_null_entry;
  653. dst_hold(&rt->u.dst);
  654. if (nrt) {
  655. err = ip6_ins_rt(nrt);
  656. if (!err)
  657. goto out2;
  658. }
  659. if (--attempts <= 0)
  660. goto out2;
  661. /*
  662. * Race condition! In the gap, when table->tb6_lock was
  663. * released someone could insert this route. Relookup.
  664. */
  665. dst_release(&rt->u.dst);
  666. goto relookup;
  667. out:
  668. if (reachable) {
  669. reachable = 0;
  670. goto restart_2;
  671. }
  672. dst_hold(&rt->u.dst);
  673. read_unlock_bh(&table->tb6_lock);
  674. out2:
  675. rt->u.dst.lastuse = jiffies;
  676. rt->u.dst.__use++;
  677. return rt;
  678. }
  679. struct dst_entry * ip6_route_output(struct sock *sk, struct flowi *fl)
  680. {
  681. int flags = 0;
  682. if (rt6_need_strict(&fl->fl6_dst))
  683. flags |= RT6_LOOKUP_F_IFACE;
  684. return fib6_rule_lookup(fl, flags, ip6_pol_route_output);
  685. }
  686. /*
  687. * Destination cache support functions
  688. */
  689. static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
  690. {
  691. struct rt6_info *rt;
  692. rt = (struct rt6_info *) dst;
  693. if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
  694. return dst;
  695. return NULL;
  696. }
  697. static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
  698. {
  699. struct rt6_info *rt = (struct rt6_info *) dst;
  700. if (rt) {
  701. if (rt->rt6i_flags & RTF_CACHE)
  702. ip6_del_rt(rt);
  703. else
  704. dst_release(dst);
  705. }
  706. return NULL;
  707. }
  708. static void ip6_link_failure(struct sk_buff *skb)
  709. {
  710. struct rt6_info *rt;
  711. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
  712. rt = (struct rt6_info *) skb->dst;
  713. if (rt) {
  714. if (rt->rt6i_flags&RTF_CACHE) {
  715. dst_set_expires(&rt->u.dst, 0);
  716. rt->rt6i_flags |= RTF_EXPIRES;
  717. } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
  718. rt->rt6i_node->fn_sernum = -1;
  719. }
  720. }
  721. static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
  722. {
  723. struct rt6_info *rt6 = (struct rt6_info*)dst;
  724. if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
  725. rt6->rt6i_flags |= RTF_MODIFIED;
  726. if (mtu < IPV6_MIN_MTU) {
  727. mtu = IPV6_MIN_MTU;
  728. dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
  729. }
  730. dst->metrics[RTAX_MTU-1] = mtu;
  731. call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
  732. }
  733. }
  734. static int ipv6_get_mtu(struct net_device *dev);
  735. static inline unsigned int ipv6_advmss(unsigned int mtu)
  736. {
  737. mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
  738. if (mtu < ip6_rt_min_advmss)
  739. mtu = ip6_rt_min_advmss;
  740. /*
  741. * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
  742. * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
  743. * IPV6_MAXPLEN is also valid and means: "any MSS,
  744. * rely only on pmtu discovery"
  745. */
  746. if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
  747. mtu = IPV6_MAXPLEN;
  748. return mtu;
  749. }
  750. static struct dst_entry *ndisc_dst_gc_list;
  751. static DEFINE_SPINLOCK(ndisc_lock);
  752. struct dst_entry *ndisc_dst_alloc(struct net_device *dev,
  753. struct neighbour *neigh,
  754. struct in6_addr *addr,
  755. int (*output)(struct sk_buff *))
  756. {
  757. struct rt6_info *rt;
  758. struct inet6_dev *idev = in6_dev_get(dev);
  759. if (unlikely(idev == NULL))
  760. return NULL;
  761. rt = ip6_dst_alloc();
  762. if (unlikely(rt == NULL)) {
  763. in6_dev_put(idev);
  764. goto out;
  765. }
  766. dev_hold(dev);
  767. if (neigh)
  768. neigh_hold(neigh);
  769. else
  770. neigh = ndisc_get_neigh(dev, addr);
  771. rt->rt6i_dev = dev;
  772. rt->rt6i_idev = idev;
  773. rt->rt6i_nexthop = neigh;
  774. atomic_set(&rt->u.dst.__refcnt, 1);
  775. rt->u.dst.metrics[RTAX_HOPLIMIT-1] = 255;
  776. rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
  777. rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
  778. rt->u.dst.output = output;
  779. #if 0 /* there's no chance to use these for ndisc */
  780. rt->u.dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
  781. ? DST_HOST
  782. : 0;
  783. ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
  784. rt->rt6i_dst.plen = 128;
  785. #endif
  786. spin_lock_bh(&ndisc_lock);
  787. rt->u.dst.next = ndisc_dst_gc_list;
  788. ndisc_dst_gc_list = &rt->u.dst;
  789. spin_unlock_bh(&ndisc_lock);
  790. fib6_force_start_gc();
  791. out:
  792. return (struct dst_entry *)rt;
  793. }
  794. int ndisc_dst_gc(int *more)
  795. {
  796. struct dst_entry *dst, *next, **pprev;
  797. int freed;
  798. next = NULL;
  799. freed = 0;
  800. spin_lock_bh(&ndisc_lock);
  801. pprev = &ndisc_dst_gc_list;
  802. while ((dst = *pprev) != NULL) {
  803. if (!atomic_read(&dst->__refcnt)) {
  804. *pprev = dst->next;
  805. dst_free(dst);
  806. freed++;
  807. } else {
  808. pprev = &dst->next;
  809. (*more)++;
  810. }
  811. }
  812. spin_unlock_bh(&ndisc_lock);
  813. return freed;
  814. }
  815. static int ip6_dst_gc(void)
  816. {
  817. static unsigned expire = 30*HZ;
  818. static unsigned long last_gc;
  819. unsigned long now = jiffies;
  820. if (time_after(last_gc + ip6_rt_gc_min_interval, now) &&
  821. atomic_read(&ip6_dst_ops.entries) <= ip6_rt_max_size)
  822. goto out;
  823. expire++;
  824. fib6_run_gc(expire);
  825. last_gc = now;
  826. if (atomic_read(&ip6_dst_ops.entries) < ip6_dst_ops.gc_thresh)
  827. expire = ip6_rt_gc_timeout>>1;
  828. out:
  829. expire -= expire>>ip6_rt_gc_elasticity;
  830. return (atomic_read(&ip6_dst_ops.entries) > ip6_rt_max_size);
  831. }
  832. /* Clean host part of a prefix. Not necessary in radix tree,
  833. but results in cleaner routing tables.
  834. Remove it only when all the things will work!
  835. */
  836. static int ipv6_get_mtu(struct net_device *dev)
  837. {
  838. int mtu = IPV6_MIN_MTU;
  839. struct inet6_dev *idev;
  840. idev = in6_dev_get(dev);
  841. if (idev) {
  842. mtu = idev->cnf.mtu6;
  843. in6_dev_put(idev);
  844. }
  845. return mtu;
  846. }
  847. int ipv6_get_hoplimit(struct net_device *dev)
  848. {
  849. int hoplimit = ipv6_devconf.hop_limit;
  850. struct inet6_dev *idev;
  851. idev = in6_dev_get(dev);
  852. if (idev) {
  853. hoplimit = idev->cnf.hop_limit;
  854. in6_dev_put(idev);
  855. }
  856. return hoplimit;
  857. }
  858. /*
  859. *
  860. */
  861. int ip6_route_add(struct fib6_config *cfg)
  862. {
  863. int err;
  864. struct rt6_info *rt = NULL;
  865. struct net_device *dev = NULL;
  866. struct inet6_dev *idev = NULL;
  867. struct fib6_table *table;
  868. int addr_type;
  869. if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
  870. return -EINVAL;
  871. #ifndef CONFIG_IPV6_SUBTREES
  872. if (cfg->fc_src_len)
  873. return -EINVAL;
  874. #endif
  875. if (cfg->fc_ifindex) {
  876. err = -ENODEV;
  877. dev = dev_get_by_index(cfg->fc_ifindex);
  878. if (!dev)
  879. goto out;
  880. idev = in6_dev_get(dev);
  881. if (!idev)
  882. goto out;
  883. }
  884. if (cfg->fc_metric == 0)
  885. cfg->fc_metric = IP6_RT_PRIO_USER;
  886. table = fib6_new_table(cfg->fc_table);
  887. if (table == NULL) {
  888. err = -ENOBUFS;
  889. goto out;
  890. }
  891. rt = ip6_dst_alloc();
  892. if (rt == NULL) {
  893. err = -ENOMEM;
  894. goto out;
  895. }
  896. rt->u.dst.obsolete = -1;
  897. rt->rt6i_expires = jiffies + clock_t_to_jiffies(cfg->fc_expires);
  898. if (cfg->fc_protocol == RTPROT_UNSPEC)
  899. cfg->fc_protocol = RTPROT_BOOT;
  900. rt->rt6i_protocol = cfg->fc_protocol;
  901. addr_type = ipv6_addr_type(&cfg->fc_dst);
  902. if (addr_type & IPV6_ADDR_MULTICAST)
  903. rt->u.dst.input = ip6_mc_input;
  904. else
  905. rt->u.dst.input = ip6_forward;
  906. rt->u.dst.output = ip6_output;
  907. ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
  908. rt->rt6i_dst.plen = cfg->fc_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, &cfg->fc_src, cfg->fc_src_len);
  913. rt->rt6i_src.plen = cfg->fc_src_len;
  914. #endif
  915. rt->rt6i_metric = cfg->fc_metric;
  916. /* We cannot add true routes via loopback here,
  917. they would result in kernel looping; promote them to reject routes
  918. */
  919. if ((cfg->fc_flags & RTF_REJECT) ||
  920. (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) {
  921. /* hold loopback dev/idev if we haven't done so. */
  922. if (dev != &loopback_dev) {
  923. if (dev) {
  924. dev_put(dev);
  925. in6_dev_put(idev);
  926. }
  927. dev = &loopback_dev;
  928. dev_hold(dev);
  929. idev = in6_dev_get(dev);
  930. if (!idev) {
  931. err = -ENODEV;
  932. goto out;
  933. }
  934. }
  935. rt->u.dst.output = ip6_pkt_discard_out;
  936. rt->u.dst.input = ip6_pkt_discard;
  937. rt->u.dst.error = -ENETUNREACH;
  938. rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
  939. goto install_route;
  940. }
  941. if (cfg->fc_flags & RTF_GATEWAY) {
  942. struct in6_addr *gw_addr;
  943. int gwa_type;
  944. gw_addr = &cfg->fc_gateway;
  945. ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
  946. gwa_type = ipv6_addr_type(gw_addr);
  947. if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
  948. struct rt6_info *grt;
  949. /* IPv6 strictly inhibits using not link-local
  950. addresses as nexthop address.
  951. Otherwise, router will not able to send redirects.
  952. It is very good, but in some (rare!) circumstances
  953. (SIT, PtP, NBMA NOARP links) it is handy to allow
  954. some exceptions. --ANK
  955. */
  956. err = -EINVAL;
  957. if (!(gwa_type&IPV6_ADDR_UNICAST))
  958. goto out;
  959. grt = rt6_lookup(gw_addr, NULL, cfg->fc_ifindex, 1);
  960. err = -EHOSTUNREACH;
  961. if (grt == NULL)
  962. goto out;
  963. if (dev) {
  964. if (dev != grt->rt6i_dev) {
  965. dst_release(&grt->u.dst);
  966. goto out;
  967. }
  968. } else {
  969. dev = grt->rt6i_dev;
  970. idev = grt->rt6i_idev;
  971. dev_hold(dev);
  972. in6_dev_hold(grt->rt6i_idev);
  973. }
  974. if (!(grt->rt6i_flags&RTF_GATEWAY))
  975. err = 0;
  976. dst_release(&grt->u.dst);
  977. if (err)
  978. goto out;
  979. }
  980. err = -EINVAL;
  981. if (dev == NULL || (dev->flags&IFF_LOOPBACK))
  982. goto out;
  983. }
  984. err = -ENODEV;
  985. if (dev == NULL)
  986. goto out;
  987. if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
  988. rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
  989. if (IS_ERR(rt->rt6i_nexthop)) {
  990. err = PTR_ERR(rt->rt6i_nexthop);
  991. rt->rt6i_nexthop = NULL;
  992. goto out;
  993. }
  994. }
  995. rt->rt6i_flags = cfg->fc_flags;
  996. install_route:
  997. if (cfg->fc_mx) {
  998. struct nlattr *nla;
  999. int remaining;
  1000. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  1001. int type = nla->nla_type;
  1002. if (type) {
  1003. if (type > RTAX_MAX) {
  1004. err = -EINVAL;
  1005. goto out;
  1006. }
  1007. rt->u.dst.metrics[type - 1] = nla_get_u32(nla);
  1008. }
  1009. }
  1010. }
  1011. if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
  1012. rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
  1013. if (!rt->u.dst.metrics[RTAX_MTU-1])
  1014. rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
  1015. if (!rt->u.dst.metrics[RTAX_ADVMSS-1])
  1016. rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
  1017. rt->u.dst.dev = dev;
  1018. rt->rt6i_idev = idev;
  1019. rt->rt6i_table = table;
  1020. return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
  1021. out:
  1022. if (dev)
  1023. dev_put(dev);
  1024. if (idev)
  1025. in6_dev_put(idev);
  1026. if (rt)
  1027. dst_free((struct dst_entry *) rt);
  1028. return err;
  1029. }
  1030. static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
  1031. {
  1032. int err;
  1033. struct fib6_table *table;
  1034. if (rt == &ip6_null_entry)
  1035. return -ENOENT;
  1036. table = rt->rt6i_table;
  1037. write_lock_bh(&table->tb6_lock);
  1038. err = fib6_del(rt, info);
  1039. dst_release(&rt->u.dst);
  1040. write_unlock_bh(&table->tb6_lock);
  1041. return err;
  1042. }
  1043. int ip6_del_rt(struct rt6_info *rt)
  1044. {
  1045. return __ip6_del_rt(rt, NULL);
  1046. }
  1047. static int ip6_route_del(struct fib6_config *cfg)
  1048. {
  1049. struct fib6_table *table;
  1050. struct fib6_node *fn;
  1051. struct rt6_info *rt;
  1052. int err = -ESRCH;
  1053. table = fib6_get_table(cfg->fc_table);
  1054. if (table == NULL)
  1055. return err;
  1056. read_lock_bh(&table->tb6_lock);
  1057. fn = fib6_locate(&table->tb6_root,
  1058. &cfg->fc_dst, cfg->fc_dst_len,
  1059. &cfg->fc_src, cfg->fc_src_len);
  1060. if (fn) {
  1061. for (rt = fn->leaf; rt; rt = rt->u.next) {
  1062. if (cfg->fc_ifindex &&
  1063. (rt->rt6i_dev == NULL ||
  1064. rt->rt6i_dev->ifindex != cfg->fc_ifindex))
  1065. continue;
  1066. if (cfg->fc_flags & RTF_GATEWAY &&
  1067. !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
  1068. continue;
  1069. if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
  1070. continue;
  1071. dst_hold(&rt->u.dst);
  1072. read_unlock_bh(&table->tb6_lock);
  1073. return __ip6_del_rt(rt, &cfg->fc_nlinfo);
  1074. }
  1075. }
  1076. read_unlock_bh(&table->tb6_lock);
  1077. return err;
  1078. }
  1079. /*
  1080. * Handle redirects
  1081. */
  1082. struct ip6rd_flowi {
  1083. struct flowi fl;
  1084. struct in6_addr gateway;
  1085. };
  1086. static struct rt6_info *__ip6_route_redirect(struct fib6_table *table,
  1087. struct flowi *fl,
  1088. int flags)
  1089. {
  1090. struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
  1091. struct rt6_info *rt;
  1092. struct fib6_node *fn;
  1093. /*
  1094. * Get the "current" route for this destination and
  1095. * check if the redirect has come from approriate router.
  1096. *
  1097. * RFC 2461 specifies that redirects should only be
  1098. * accepted if they come from the nexthop to the target.
  1099. * Due to the way the routes are chosen, this notion
  1100. * is a bit fuzzy and one might need to check all possible
  1101. * routes.
  1102. */
  1103. read_lock_bh(&table->tb6_lock);
  1104. fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
  1105. restart:
  1106. for (rt = fn->leaf; rt; rt = rt->u.next) {
  1107. /*
  1108. * Current route is on-link; redirect is always invalid.
  1109. *
  1110. * Seems, previous statement is not true. It could
  1111. * be node, which looks for us as on-link (f.e. proxy ndisc)
  1112. * But then router serving it might decide, that we should
  1113. * know truth 8)8) --ANK (980726).
  1114. */
  1115. if (rt6_check_expired(rt))
  1116. continue;
  1117. if (!(rt->rt6i_flags & RTF_GATEWAY))
  1118. continue;
  1119. if (fl->oif != rt->rt6i_dev->ifindex)
  1120. continue;
  1121. if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
  1122. continue;
  1123. break;
  1124. }
  1125. if (!rt)
  1126. rt = &ip6_null_entry;
  1127. BACKTRACK(&fl->fl6_src);
  1128. out:
  1129. dst_hold(&rt->u.dst);
  1130. read_unlock_bh(&table->tb6_lock);
  1131. return rt;
  1132. };
  1133. static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
  1134. struct in6_addr *src,
  1135. struct in6_addr *gateway,
  1136. struct net_device *dev)
  1137. {
  1138. struct ip6rd_flowi rdfl = {
  1139. .fl = {
  1140. .oif = dev->ifindex,
  1141. .nl_u = {
  1142. .ip6_u = {
  1143. .daddr = *dest,
  1144. .saddr = *src,
  1145. },
  1146. },
  1147. },
  1148. .gateway = *gateway,
  1149. };
  1150. int flags = rt6_need_strict(dest) ? RT6_LOOKUP_F_IFACE : 0;
  1151. return (struct rt6_info *)fib6_rule_lookup((struct flowi *)&rdfl, flags, __ip6_route_redirect);
  1152. }
  1153. void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
  1154. struct in6_addr *saddr,
  1155. struct neighbour *neigh, u8 *lladdr, int on_link)
  1156. {
  1157. struct rt6_info *rt, *nrt = NULL;
  1158. struct netevent_redirect netevent;
  1159. rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
  1160. if (rt == &ip6_null_entry) {
  1161. if (net_ratelimit())
  1162. printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
  1163. "for redirect target\n");
  1164. goto out;
  1165. }
  1166. /*
  1167. * We have finally decided to accept it.
  1168. */
  1169. neigh_update(neigh, lladdr, NUD_STALE,
  1170. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  1171. NEIGH_UPDATE_F_OVERRIDE|
  1172. (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  1173. NEIGH_UPDATE_F_ISROUTER))
  1174. );
  1175. /*
  1176. * Redirect received -> path was valid.
  1177. * Look, redirects are sent only in response to data packets,
  1178. * so that this nexthop apparently is reachable. --ANK
  1179. */
  1180. dst_confirm(&rt->u.dst);
  1181. /* Duplicate redirect: silently ignore. */
  1182. if (neigh == rt->u.dst.neighbour)
  1183. goto out;
  1184. nrt = ip6_rt_copy(rt);
  1185. if (nrt == NULL)
  1186. goto out;
  1187. nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
  1188. if (on_link)
  1189. nrt->rt6i_flags &= ~RTF_GATEWAY;
  1190. ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
  1191. nrt->rt6i_dst.plen = 128;
  1192. nrt->u.dst.flags |= DST_HOST;
  1193. ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
  1194. nrt->rt6i_nexthop = neigh_clone(neigh);
  1195. /* Reset pmtu, it may be better */
  1196. nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
  1197. nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&nrt->u.dst));
  1198. if (ip6_ins_rt(nrt))
  1199. goto out;
  1200. netevent.old = &rt->u.dst;
  1201. netevent.new = &nrt->u.dst;
  1202. call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
  1203. if (rt->rt6i_flags&RTF_CACHE) {
  1204. ip6_del_rt(rt);
  1205. return;
  1206. }
  1207. out:
  1208. dst_release(&rt->u.dst);
  1209. return;
  1210. }
  1211. /*
  1212. * Handle ICMP "packet too big" messages
  1213. * i.e. Path MTU discovery
  1214. */
  1215. void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
  1216. struct net_device *dev, u32 pmtu)
  1217. {
  1218. struct rt6_info *rt, *nrt;
  1219. int allfrag = 0;
  1220. rt = rt6_lookup(daddr, saddr, dev->ifindex, 0);
  1221. if (rt == NULL)
  1222. return;
  1223. if (pmtu >= dst_mtu(&rt->u.dst))
  1224. goto out;
  1225. if (pmtu < IPV6_MIN_MTU) {
  1226. /*
  1227. * According to RFC2460, PMTU is set to the IPv6 Minimum Link
  1228. * MTU (1280) and a fragment header should always be included
  1229. * after a node receiving Too Big message reporting PMTU is
  1230. * less than the IPv6 Minimum Link MTU.
  1231. */
  1232. pmtu = IPV6_MIN_MTU;
  1233. allfrag = 1;
  1234. }
  1235. /* New mtu received -> path was valid.
  1236. They are sent only in response to data packets,
  1237. so that this nexthop apparently is reachable. --ANK
  1238. */
  1239. dst_confirm(&rt->u.dst);
  1240. /* Host route. If it is static, it would be better
  1241. not to override it, but add new one, so that
  1242. when cache entry will expire old pmtu
  1243. would return automatically.
  1244. */
  1245. if (rt->rt6i_flags & RTF_CACHE) {
  1246. rt->u.dst.metrics[RTAX_MTU-1] = pmtu;
  1247. if (allfrag)
  1248. rt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
  1249. dst_set_expires(&rt->u.dst, ip6_rt_mtu_expires);
  1250. rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
  1251. goto out;
  1252. }
  1253. /* Network route.
  1254. Two cases are possible:
  1255. 1. It is connected route. Action: COW
  1256. 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
  1257. */
  1258. if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
  1259. nrt = rt6_alloc_cow(rt, daddr, saddr);
  1260. else
  1261. nrt = rt6_alloc_clone(rt, daddr);
  1262. if (nrt) {
  1263. nrt->u.dst.metrics[RTAX_MTU-1] = pmtu;
  1264. if (allfrag)
  1265. nrt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
  1266. /* According to RFC 1981, detecting PMTU increase shouldn't be
  1267. * happened within 5 mins, the recommended timer is 10 mins.
  1268. * Here this route expiration time is set to ip6_rt_mtu_expires
  1269. * which is 10 mins. After 10 mins the decreased pmtu is expired
  1270. * and detecting PMTU increase will be automatically happened.
  1271. */
  1272. dst_set_expires(&nrt->u.dst, ip6_rt_mtu_expires);
  1273. nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
  1274. ip6_ins_rt(nrt);
  1275. }
  1276. out:
  1277. dst_release(&rt->u.dst);
  1278. }
  1279. /*
  1280. * Misc support functions
  1281. */
  1282. static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
  1283. {
  1284. struct rt6_info *rt = ip6_dst_alloc();
  1285. if (rt) {
  1286. rt->u.dst.input = ort->u.dst.input;
  1287. rt->u.dst.output = ort->u.dst.output;
  1288. memcpy(rt->u.dst.metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
  1289. rt->u.dst.dev = ort->u.dst.dev;
  1290. if (rt->u.dst.dev)
  1291. dev_hold(rt->u.dst.dev);
  1292. rt->rt6i_idev = ort->rt6i_idev;
  1293. if (rt->rt6i_idev)
  1294. in6_dev_hold(rt->rt6i_idev);
  1295. rt->u.dst.lastuse = jiffies;
  1296. rt->rt6i_expires = 0;
  1297. ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
  1298. rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
  1299. rt->rt6i_metric = 0;
  1300. memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
  1301. #ifdef CONFIG_IPV6_SUBTREES
  1302. memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
  1303. #endif
  1304. rt->rt6i_table = ort->rt6i_table;
  1305. }
  1306. return rt;
  1307. }
  1308. #ifdef CONFIG_IPV6_ROUTE_INFO
  1309. static struct rt6_info *rt6_get_route_info(struct in6_addr *prefix, int prefixlen,
  1310. struct in6_addr *gwaddr, int ifindex)
  1311. {
  1312. struct fib6_node *fn;
  1313. struct rt6_info *rt = NULL;
  1314. struct fib6_table *table;
  1315. table = fib6_get_table(RT6_TABLE_INFO);
  1316. if (table == NULL)
  1317. return NULL;
  1318. write_lock_bh(&table->tb6_lock);
  1319. fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
  1320. if (!fn)
  1321. goto out;
  1322. for (rt = fn->leaf; rt; rt = rt->u.next) {
  1323. if (rt->rt6i_dev->ifindex != ifindex)
  1324. continue;
  1325. if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
  1326. continue;
  1327. if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
  1328. continue;
  1329. dst_hold(&rt->u.dst);
  1330. break;
  1331. }
  1332. out:
  1333. write_unlock_bh(&table->tb6_lock);
  1334. return rt;
  1335. }
  1336. static struct rt6_info *rt6_add_route_info(struct in6_addr *prefix, int prefixlen,
  1337. struct in6_addr *gwaddr, int ifindex,
  1338. unsigned pref)
  1339. {
  1340. struct fib6_config cfg = {
  1341. .fc_table = RT6_TABLE_INFO,
  1342. .fc_metric = 1024,
  1343. .fc_ifindex = ifindex,
  1344. .fc_dst_len = prefixlen,
  1345. .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
  1346. RTF_UP | RTF_PREF(pref),
  1347. };
  1348. ipv6_addr_copy(&cfg.fc_dst, prefix);
  1349. ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
  1350. /* We should treat it as a default route if prefix length is 0. */
  1351. if (!prefixlen)
  1352. cfg.fc_flags |= RTF_DEFAULT;
  1353. ip6_route_add(&cfg);
  1354. return rt6_get_route_info(prefix, prefixlen, gwaddr, ifindex);
  1355. }
  1356. #endif
  1357. struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
  1358. {
  1359. struct rt6_info *rt;
  1360. struct fib6_table *table;
  1361. table = fib6_get_table(RT6_TABLE_DFLT);
  1362. if (table == NULL)
  1363. return NULL;
  1364. write_lock_bh(&table->tb6_lock);
  1365. for (rt = table->tb6_root.leaf; rt; rt=rt->u.next) {
  1366. if (dev == rt->rt6i_dev &&
  1367. ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
  1368. ipv6_addr_equal(&rt->rt6i_gateway, addr))
  1369. break;
  1370. }
  1371. if (rt)
  1372. dst_hold(&rt->u.dst);
  1373. write_unlock_bh(&table->tb6_lock);
  1374. return rt;
  1375. }
  1376. struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
  1377. struct net_device *dev,
  1378. unsigned int pref)
  1379. {
  1380. struct fib6_config cfg = {
  1381. .fc_table = RT6_TABLE_DFLT,
  1382. .fc_metric = 1024,
  1383. .fc_ifindex = dev->ifindex,
  1384. .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
  1385. RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
  1386. };
  1387. ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
  1388. ip6_route_add(&cfg);
  1389. return rt6_get_dflt_router(gwaddr, dev);
  1390. }
  1391. void rt6_purge_dflt_routers(void)
  1392. {
  1393. struct rt6_info *rt;
  1394. struct fib6_table *table;
  1395. /* NOTE: Keep consistent with rt6_get_dflt_router */
  1396. table = fib6_get_table(RT6_TABLE_DFLT);
  1397. if (table == NULL)
  1398. return;
  1399. restart:
  1400. read_lock_bh(&table->tb6_lock);
  1401. for (rt = table->tb6_root.leaf; rt; rt = rt->u.next) {
  1402. if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
  1403. dst_hold(&rt->u.dst);
  1404. read_unlock_bh(&table->tb6_lock);
  1405. ip6_del_rt(rt);
  1406. goto restart;
  1407. }
  1408. }
  1409. read_unlock_bh(&table->tb6_lock);
  1410. }
  1411. static void rtmsg_to_fib6_config(struct in6_rtmsg *rtmsg,
  1412. struct fib6_config *cfg)
  1413. {
  1414. memset(cfg, 0, sizeof(*cfg));
  1415. cfg->fc_table = RT6_TABLE_MAIN;
  1416. cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
  1417. cfg->fc_metric = rtmsg->rtmsg_metric;
  1418. cfg->fc_expires = rtmsg->rtmsg_info;
  1419. cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
  1420. cfg->fc_src_len = rtmsg->rtmsg_src_len;
  1421. cfg->fc_flags = rtmsg->rtmsg_flags;
  1422. ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
  1423. ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
  1424. ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
  1425. }
  1426. int ipv6_route_ioctl(unsigned int cmd, void __user *arg)
  1427. {
  1428. struct fib6_config cfg;
  1429. struct in6_rtmsg rtmsg;
  1430. int err;
  1431. switch(cmd) {
  1432. case SIOCADDRT: /* Add a route */
  1433. case SIOCDELRT: /* Delete a route */
  1434. if (!capable(CAP_NET_ADMIN))
  1435. return -EPERM;
  1436. err = copy_from_user(&rtmsg, arg,
  1437. sizeof(struct in6_rtmsg));
  1438. if (err)
  1439. return -EFAULT;
  1440. rtmsg_to_fib6_config(&rtmsg, &cfg);
  1441. rtnl_lock();
  1442. switch (cmd) {
  1443. case SIOCADDRT:
  1444. err = ip6_route_add(&cfg);
  1445. break;
  1446. case SIOCDELRT:
  1447. err = ip6_route_del(&cfg);
  1448. break;
  1449. default:
  1450. err = -EINVAL;
  1451. }
  1452. rtnl_unlock();
  1453. return err;
  1454. };
  1455. return -EINVAL;
  1456. }
  1457. /*
  1458. * Drop the packet on the floor
  1459. */
  1460. static int ip6_pkt_discard(struct sk_buff *skb)
  1461. {
  1462. int type = ipv6_addr_type(&skb->nh.ipv6h->daddr);
  1463. if (type == IPV6_ADDR_ANY || type == IPV6_ADDR_RESERVED)
  1464. IP6_INC_STATS(IPSTATS_MIB_INADDRERRORS);
  1465. IP6_INC_STATS(IPSTATS_MIB_OUTNOROUTES);
  1466. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_NOROUTE, 0, skb->dev);
  1467. kfree_skb(skb);
  1468. return 0;
  1469. }
  1470. static int ip6_pkt_discard_out(struct sk_buff *skb)
  1471. {
  1472. skb->dev = skb->dst->dev;
  1473. return ip6_pkt_discard(skb);
  1474. }
  1475. /*
  1476. * Allocate a dst for local (unicast / anycast) address.
  1477. */
  1478. struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
  1479. const struct in6_addr *addr,
  1480. int anycast)
  1481. {
  1482. struct rt6_info *rt = ip6_dst_alloc();
  1483. if (rt == NULL)
  1484. return ERR_PTR(-ENOMEM);
  1485. dev_hold(&loopback_dev);
  1486. in6_dev_hold(idev);
  1487. rt->u.dst.flags = DST_HOST;
  1488. rt->u.dst.input = ip6_input;
  1489. rt->u.dst.output = ip6_output;
  1490. rt->rt6i_dev = &loopback_dev;
  1491. rt->rt6i_idev = idev;
  1492. rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
  1493. rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst));
  1494. rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
  1495. rt->u.dst.obsolete = -1;
  1496. rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
  1497. if (anycast)
  1498. rt->rt6i_flags |= RTF_ANYCAST;
  1499. else
  1500. rt->rt6i_flags |= RTF_LOCAL;
  1501. rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
  1502. if (rt->rt6i_nexthop == NULL) {
  1503. dst_free((struct dst_entry *) rt);
  1504. return ERR_PTR(-ENOMEM);
  1505. }
  1506. ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
  1507. rt->rt6i_dst.plen = 128;
  1508. rt->rt6i_table = fib6_get_table(RT6_TABLE_LOCAL);
  1509. atomic_set(&rt->u.dst.__refcnt, 1);
  1510. return rt;
  1511. }
  1512. static int fib6_ifdown(struct rt6_info *rt, void *arg)
  1513. {
  1514. if (((void*)rt->rt6i_dev == arg || arg == NULL) &&
  1515. rt != &ip6_null_entry) {
  1516. RT6_TRACE("deleted by ifdown %p\n", rt);
  1517. return -1;
  1518. }
  1519. return 0;
  1520. }
  1521. void rt6_ifdown(struct net_device *dev)
  1522. {
  1523. fib6_clean_all(fib6_ifdown, 0, dev);
  1524. }
  1525. struct rt6_mtu_change_arg
  1526. {
  1527. struct net_device *dev;
  1528. unsigned mtu;
  1529. };
  1530. static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
  1531. {
  1532. struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
  1533. struct inet6_dev *idev;
  1534. /* In IPv6 pmtu discovery is not optional,
  1535. so that RTAX_MTU lock cannot disable it.
  1536. We still use this lock to block changes
  1537. caused by addrconf/ndisc.
  1538. */
  1539. idev = __in6_dev_get(arg->dev);
  1540. if (idev == NULL)
  1541. return 0;
  1542. /* For administrative MTU increase, there is no way to discover
  1543. IPv6 PMTU increase, so PMTU increase should be updated here.
  1544. Since RFC 1981 doesn't include administrative MTU increase
  1545. update PMTU increase is a MUST. (i.e. jumbo frame)
  1546. */
  1547. /*
  1548. If new MTU is less than route PMTU, this new MTU will be the
  1549. lowest MTU in the path, update the route PMTU to reflect PMTU
  1550. decreases; if new MTU is greater than route PMTU, and the
  1551. old MTU is the lowest MTU in the path, update the route PMTU
  1552. to reflect the increase. In this case if the other nodes' MTU
  1553. also have the lowest MTU, TOO BIG MESSAGE will be lead to
  1554. PMTU discouvery.
  1555. */
  1556. if (rt->rt6i_dev == arg->dev &&
  1557. !dst_metric_locked(&rt->u.dst, RTAX_MTU) &&
  1558. (dst_mtu(&rt->u.dst) > arg->mtu ||
  1559. (dst_mtu(&rt->u.dst) < arg->mtu &&
  1560. dst_mtu(&rt->u.dst) == idev->cnf.mtu6)))
  1561. rt->u.dst.metrics[RTAX_MTU-1] = arg->mtu;
  1562. rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(arg->mtu);
  1563. return 0;
  1564. }
  1565. void rt6_mtu_change(struct net_device *dev, unsigned mtu)
  1566. {
  1567. struct rt6_mtu_change_arg arg = {
  1568. .dev = dev,
  1569. .mtu = mtu,
  1570. };
  1571. fib6_clean_all(rt6_mtu_change_route, 0, &arg);
  1572. }
  1573. static struct nla_policy rtm_ipv6_policy[RTA_MAX+1] __read_mostly = {
  1574. [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
  1575. [RTA_OIF] = { .type = NLA_U32 },
  1576. [RTA_IIF] = { .type = NLA_U32 },
  1577. [RTA_PRIORITY] = { .type = NLA_U32 },
  1578. [RTA_METRICS] = { .type = NLA_NESTED },
  1579. };
  1580. static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  1581. struct fib6_config *cfg)
  1582. {
  1583. struct rtmsg *rtm;
  1584. struct nlattr *tb[RTA_MAX+1];
  1585. int err;
  1586. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
  1587. if (err < 0)
  1588. goto errout;
  1589. err = -EINVAL;
  1590. rtm = nlmsg_data(nlh);
  1591. memset(cfg, 0, sizeof(*cfg));
  1592. cfg->fc_table = rtm->rtm_table;
  1593. cfg->fc_dst_len = rtm->rtm_dst_len;
  1594. cfg->fc_src_len = rtm->rtm_src_len;
  1595. cfg->fc_flags = RTF_UP;
  1596. cfg->fc_protocol = rtm->rtm_protocol;
  1597. if (rtm->rtm_type == RTN_UNREACHABLE)
  1598. cfg->fc_flags |= RTF_REJECT;
  1599. cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
  1600. cfg->fc_nlinfo.nlh = nlh;
  1601. if (tb[RTA_GATEWAY]) {
  1602. nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
  1603. cfg->fc_flags |= RTF_GATEWAY;
  1604. }
  1605. if (tb[RTA_DST]) {
  1606. int plen = (rtm->rtm_dst_len + 7) >> 3;
  1607. if (nla_len(tb[RTA_DST]) < plen)
  1608. goto errout;
  1609. nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
  1610. }
  1611. if (tb[RTA_SRC]) {
  1612. int plen = (rtm->rtm_src_len + 7) >> 3;
  1613. if (nla_len(tb[RTA_SRC]) < plen)
  1614. goto errout;
  1615. nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
  1616. }
  1617. if (tb[RTA_OIF])
  1618. cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
  1619. if (tb[RTA_PRIORITY])
  1620. cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
  1621. if (tb[RTA_METRICS]) {
  1622. cfg->fc_mx = nla_data(tb[RTA_METRICS]);
  1623. cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
  1624. }
  1625. if (tb[RTA_TABLE])
  1626. cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
  1627. err = 0;
  1628. errout:
  1629. return err;
  1630. }
  1631. int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  1632. {
  1633. struct fib6_config cfg;
  1634. int err;
  1635. err = rtm_to_fib6_config(skb, nlh, &cfg);
  1636. if (err < 0)
  1637. return err;
  1638. return ip6_route_del(&cfg);
  1639. }
  1640. int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  1641. {
  1642. struct fib6_config cfg;
  1643. int err;
  1644. err = rtm_to_fib6_config(skb, nlh, &cfg);
  1645. if (err < 0)
  1646. return err;
  1647. return ip6_route_add(&cfg);
  1648. }
  1649. static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
  1650. struct in6_addr *dst, struct in6_addr *src,
  1651. int iif, int type, u32 pid, u32 seq,
  1652. int prefix, unsigned int flags)
  1653. {
  1654. struct rtmsg *rtm;
  1655. struct nlmsghdr *nlh;
  1656. struct rta_cacheinfo ci;
  1657. u32 table;
  1658. if (prefix) { /* user wants prefix routes only */
  1659. if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
  1660. /* success since this is not a prefix route */
  1661. return 1;
  1662. }
  1663. }
  1664. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
  1665. if (nlh == NULL)
  1666. return -ENOBUFS;
  1667. rtm = nlmsg_data(nlh);
  1668. rtm->rtm_family = AF_INET6;
  1669. rtm->rtm_dst_len = rt->rt6i_dst.plen;
  1670. rtm->rtm_src_len = rt->rt6i_src.plen;
  1671. rtm->rtm_tos = 0;
  1672. if (rt->rt6i_table)
  1673. table = rt->rt6i_table->tb6_id;
  1674. else
  1675. table = RT6_TABLE_UNSPEC;
  1676. rtm->rtm_table = table;
  1677. NLA_PUT_U32(skb, RTA_TABLE, table);
  1678. if (rt->rt6i_flags&RTF_REJECT)
  1679. rtm->rtm_type = RTN_UNREACHABLE;
  1680. else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
  1681. rtm->rtm_type = RTN_LOCAL;
  1682. else
  1683. rtm->rtm_type = RTN_UNICAST;
  1684. rtm->rtm_flags = 0;
  1685. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1686. rtm->rtm_protocol = rt->rt6i_protocol;
  1687. if (rt->rt6i_flags&RTF_DYNAMIC)
  1688. rtm->rtm_protocol = RTPROT_REDIRECT;
  1689. else if (rt->rt6i_flags & RTF_ADDRCONF)
  1690. rtm->rtm_protocol = RTPROT_KERNEL;
  1691. else if (rt->rt6i_flags&RTF_DEFAULT)
  1692. rtm->rtm_protocol = RTPROT_RA;
  1693. if (rt->rt6i_flags&RTF_CACHE)
  1694. rtm->rtm_flags |= RTM_F_CLONED;
  1695. if (dst) {
  1696. NLA_PUT(skb, RTA_DST, 16, dst);
  1697. rtm->rtm_dst_len = 128;
  1698. } else if (rtm->rtm_dst_len)
  1699. NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
  1700. #ifdef CONFIG_IPV6_SUBTREES
  1701. if (src) {
  1702. NLA_PUT(skb, RTA_SRC, 16, src);
  1703. rtm->rtm_src_len = 128;
  1704. } else if (rtm->rtm_src_len)
  1705. NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
  1706. #endif
  1707. if (iif)
  1708. NLA_PUT_U32(skb, RTA_IIF, iif);
  1709. else if (dst) {
  1710. struct in6_addr saddr_buf;
  1711. if (ipv6_get_saddr(&rt->u.dst, dst, &saddr_buf) == 0)
  1712. NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
  1713. }
  1714. if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
  1715. goto nla_put_failure;
  1716. if (rt->u.dst.neighbour)
  1717. NLA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key);
  1718. if (rt->u.dst.dev)
  1719. NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
  1720. NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
  1721. ci.rta_lastuse = jiffies_to_clock_t(jiffies - rt->u.dst.lastuse);
  1722. if (rt->rt6i_expires)
  1723. ci.rta_expires = jiffies_to_clock_t(rt->rt6i_expires - jiffies);
  1724. else
  1725. ci.rta_expires = 0;
  1726. ci.rta_used = rt->u.dst.__use;
  1727. ci.rta_clntref = atomic_read(&rt->u.dst.__refcnt);
  1728. ci.rta_error = rt->u.dst.error;
  1729. ci.rta_id = 0;
  1730. ci.rta_ts = 0;
  1731. ci.rta_tsage = 0;
  1732. NLA_PUT(skb, RTA_CACHEINFO, sizeof(ci), &ci);
  1733. return nlmsg_end(skb, nlh);
  1734. nla_put_failure:
  1735. return nlmsg_cancel(skb, nlh);
  1736. }
  1737. int rt6_dump_route(struct rt6_info *rt, void *p_arg)
  1738. {
  1739. struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
  1740. int prefix;
  1741. if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
  1742. struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
  1743. prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
  1744. } else
  1745. prefix = 0;
  1746. return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
  1747. NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
  1748. prefix, NLM_F_MULTI);
  1749. }
  1750. int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
  1751. {
  1752. struct nlattr *tb[RTA_MAX+1];
  1753. struct rt6_info *rt;
  1754. struct sk_buff *skb;
  1755. struct rtmsg *rtm;
  1756. struct flowi fl;
  1757. int err, iif = 0;
  1758. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
  1759. if (err < 0)
  1760. goto errout;
  1761. err = -EINVAL;
  1762. memset(&fl, 0, sizeof(fl));
  1763. if (tb[RTA_SRC]) {
  1764. if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
  1765. goto errout;
  1766. ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
  1767. }
  1768. if (tb[RTA_DST]) {
  1769. if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
  1770. goto errout;
  1771. ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
  1772. }
  1773. if (tb[RTA_IIF])
  1774. iif = nla_get_u32(tb[RTA_IIF]);
  1775. if (tb[RTA_OIF])
  1776. fl.oif = nla_get_u32(tb[RTA_OIF]);
  1777. if (iif) {
  1778. struct net_device *dev;
  1779. dev = __dev_get_by_index(iif);
  1780. if (!dev) {
  1781. err = -ENODEV;
  1782. goto errout;
  1783. }
  1784. }
  1785. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1786. if (skb == NULL) {
  1787. err = -ENOBUFS;
  1788. goto errout;
  1789. }
  1790. /* Reserve room for dummy headers, this skb can pass
  1791. through good chunk of routing engine.
  1792. */
  1793. skb->mac.raw = skb->data;
  1794. skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
  1795. rt = (struct rt6_info*) ip6_route_output(NULL, &fl);
  1796. skb->dst = &rt->u.dst;
  1797. err = rt6_fill_node(skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
  1798. RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
  1799. nlh->nlmsg_seq, 0, 0);
  1800. if (err < 0) {
  1801. kfree_skb(skb);
  1802. goto errout;
  1803. }
  1804. err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
  1805. errout:
  1806. return err;
  1807. }
  1808. void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
  1809. {
  1810. struct sk_buff *skb;
  1811. u32 pid = 0, seq = 0;
  1812. struct nlmsghdr *nlh = NULL;
  1813. int payload = sizeof(struct rtmsg) + 256;
  1814. int err = -ENOBUFS;
  1815. if (info) {
  1816. pid = info->pid;
  1817. nlh = info->nlh;
  1818. if (nlh)
  1819. seq = nlh->nlmsg_seq;
  1820. }
  1821. skb = nlmsg_new(nlmsg_total_size(payload), gfp_any());
  1822. if (skb == NULL)
  1823. goto errout;
  1824. err = rt6_fill_node(skb, rt, NULL, NULL, 0, event, pid, seq, 0, 0);
  1825. if (err < 0) {
  1826. kfree_skb(skb);
  1827. goto errout;
  1828. }
  1829. err = rtnl_notify(skb, pid, RTNLGRP_IPV6_ROUTE, nlh, gfp_any());
  1830. errout:
  1831. if (err < 0)
  1832. rtnl_set_sk_err(RTNLGRP_IPV6_ROUTE, err);
  1833. }
  1834. /*
  1835. * /proc
  1836. */
  1837. #ifdef CONFIG_PROC_FS
  1838. #define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
  1839. struct rt6_proc_arg
  1840. {
  1841. char *buffer;
  1842. int offset;
  1843. int length;
  1844. int skip;
  1845. int len;
  1846. };
  1847. static int rt6_info_route(struct rt6_info *rt, void *p_arg)
  1848. {
  1849. struct rt6_proc_arg *arg = (struct rt6_proc_arg *) p_arg;
  1850. int i;
  1851. if (arg->skip < arg->offset / RT6_INFO_LEN) {
  1852. arg->skip++;
  1853. return 0;
  1854. }
  1855. if (arg->len >= arg->length)
  1856. return 0;
  1857. for (i=0; i<16; i++) {
  1858. sprintf(arg->buffer + arg->len, "%02x",
  1859. rt->rt6i_dst.addr.s6_addr[i]);
  1860. arg->len += 2;
  1861. }
  1862. arg->len += sprintf(arg->buffer + arg->len, " %02x ",
  1863. rt->rt6i_dst.plen);
  1864. #ifdef CONFIG_IPV6_SUBTREES
  1865. for (i=0; i<16; i++) {
  1866. sprintf(arg->buffer + arg->len, "%02x",
  1867. rt->rt6i_src.addr.s6_addr[i]);
  1868. arg->len += 2;
  1869. }
  1870. arg->len += sprintf(arg->buffer + arg->len, " %02x ",
  1871. rt->rt6i_src.plen);
  1872. #else
  1873. sprintf(arg->buffer + arg->len,
  1874. "00000000000000000000000000000000 00 ");
  1875. arg->len += 36;
  1876. #endif
  1877. if (rt->rt6i_nexthop) {
  1878. for (i=0; i<16; i++) {
  1879. sprintf(arg->buffer + arg->len, "%02x",
  1880. rt->rt6i_nexthop->primary_key[i]);
  1881. arg->len += 2;
  1882. }
  1883. } else {
  1884. sprintf(arg->buffer + arg->len,
  1885. "00000000000000000000000000000000");
  1886. arg->len += 32;
  1887. }
  1888. arg->len += sprintf(arg->buffer + arg->len,
  1889. " %08x %08x %08x %08x %8s\n",
  1890. rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
  1891. rt->u.dst.__use, rt->rt6i_flags,
  1892. rt->rt6i_dev ? rt->rt6i_dev->name : "");
  1893. return 0;
  1894. }
  1895. static int rt6_proc_info(char *buffer, char **start, off_t offset, int length)
  1896. {
  1897. struct rt6_proc_arg arg = {
  1898. .buffer = buffer,
  1899. .offset = offset,
  1900. .length = length,
  1901. };
  1902. fib6_clean_all(rt6_info_route, 0, &arg);
  1903. *start = buffer;
  1904. if (offset)
  1905. *start += offset % RT6_INFO_LEN;
  1906. arg.len -= offset % RT6_INFO_LEN;
  1907. if (arg.len > length)
  1908. arg.len = length;
  1909. if (arg.len < 0)
  1910. arg.len = 0;
  1911. return arg.len;
  1912. }
  1913. static int rt6_stats_seq_show(struct seq_file *seq, void *v)
  1914. {
  1915. seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
  1916. rt6_stats.fib_nodes, rt6_stats.fib_route_nodes,
  1917. rt6_stats.fib_rt_alloc, rt6_stats.fib_rt_entries,
  1918. rt6_stats.fib_rt_cache,
  1919. atomic_read(&ip6_dst_ops.entries),
  1920. rt6_stats.fib_discarded_routes);
  1921. return 0;
  1922. }
  1923. static int rt6_stats_seq_open(struct inode *inode, struct file *file)
  1924. {
  1925. return single_open(file, rt6_stats_seq_show, NULL);
  1926. }
  1927. static struct file_operations rt6_stats_seq_fops = {
  1928. .owner = THIS_MODULE,
  1929. .open = rt6_stats_seq_open,
  1930. .read = seq_read,
  1931. .llseek = seq_lseek,
  1932. .release = single_release,
  1933. };
  1934. #endif /* CONFIG_PROC_FS */
  1935. #ifdef CONFIG_SYSCTL
  1936. static int flush_delay;
  1937. static
  1938. int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp,
  1939. void __user *buffer, size_t *lenp, loff_t *ppos)
  1940. {
  1941. if (write) {
  1942. proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  1943. fib6_run_gc(flush_delay <= 0 ? ~0UL : (unsigned long)flush_delay);
  1944. return 0;
  1945. } else
  1946. return -EINVAL;
  1947. }
  1948. ctl_table ipv6_route_table[] = {
  1949. {
  1950. .ctl_name = NET_IPV6_ROUTE_FLUSH,
  1951. .procname = "flush",
  1952. .data = &flush_delay,
  1953. .maxlen = sizeof(int),
  1954. .mode = 0200,
  1955. .proc_handler = &ipv6_sysctl_rtcache_flush
  1956. },
  1957. {
  1958. .ctl_name = NET_IPV6_ROUTE_GC_THRESH,
  1959. .procname = "gc_thresh",
  1960. .data = &ip6_dst_ops.gc_thresh,
  1961. .maxlen = sizeof(int),
  1962. .mode = 0644,
  1963. .proc_handler = &proc_dointvec,
  1964. },
  1965. {
  1966. .ctl_name = NET_IPV6_ROUTE_MAX_SIZE,
  1967. .procname = "max_size",
  1968. .data = &ip6_rt_max_size,
  1969. .maxlen = sizeof(int),
  1970. .mode = 0644,
  1971. .proc_handler = &proc_dointvec,
  1972. },
  1973. {
  1974. .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL,
  1975. .procname = "gc_min_interval",
  1976. .data = &ip6_rt_gc_min_interval,
  1977. .maxlen = sizeof(int),
  1978. .mode = 0644,
  1979. .proc_handler = &proc_dointvec_jiffies,
  1980. .strategy = &sysctl_jiffies,
  1981. },
  1982. {
  1983. .ctl_name = NET_IPV6_ROUTE_GC_TIMEOUT,
  1984. .procname = "gc_timeout",
  1985. .data = &ip6_rt_gc_timeout,
  1986. .maxlen = sizeof(int),
  1987. .mode = 0644,
  1988. .proc_handler = &proc_dointvec_jiffies,
  1989. .strategy = &sysctl_jiffies,
  1990. },
  1991. {
  1992. .ctl_name = NET_IPV6_ROUTE_GC_INTERVAL,
  1993. .procname = "gc_interval",
  1994. .data = &ip6_rt_gc_interval,
  1995. .maxlen = sizeof(int),
  1996. .mode = 0644,
  1997. .proc_handler = &proc_dointvec_jiffies,
  1998. .strategy = &sysctl_jiffies,
  1999. },
  2000. {
  2001. .ctl_name = NET_IPV6_ROUTE_GC_ELASTICITY,
  2002. .procname = "gc_elasticity",
  2003. .data = &ip6_rt_gc_elasticity,
  2004. .maxlen = sizeof(int),
  2005. .mode = 0644,
  2006. .proc_handler = &proc_dointvec_jiffies,
  2007. .strategy = &sysctl_jiffies,
  2008. },
  2009. {
  2010. .ctl_name = NET_IPV6_ROUTE_MTU_EXPIRES,
  2011. .procname = "mtu_expires",
  2012. .data = &ip6_rt_mtu_expires,
  2013. .maxlen = sizeof(int),
  2014. .mode = 0644,
  2015. .proc_handler = &proc_dointvec_jiffies,
  2016. .strategy = &sysctl_jiffies,
  2017. },
  2018. {
  2019. .ctl_name = NET_IPV6_ROUTE_MIN_ADVMSS,
  2020. .procname = "min_adv_mss",
  2021. .data = &ip6_rt_min_advmss,
  2022. .maxlen = sizeof(int),
  2023. .mode = 0644,
  2024. .proc_handler = &proc_dointvec_jiffies,
  2025. .strategy = &sysctl_jiffies,
  2026. },
  2027. {
  2028. .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS,
  2029. .procname = "gc_min_interval_ms",
  2030. .data = &ip6_rt_gc_min_interval,
  2031. .maxlen = sizeof(int),
  2032. .mode = 0644,
  2033. .proc_handler = &proc_dointvec_ms_jiffies,
  2034. .strategy = &sysctl_ms_jiffies,
  2035. },
  2036. { .ctl_name = 0 }
  2037. };
  2038. #endif
  2039. void __init ip6_route_init(void)
  2040. {
  2041. struct proc_dir_entry *p;
  2042. ip6_dst_ops.kmem_cachep =
  2043. kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
  2044. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  2045. fib6_init();
  2046. #ifdef CONFIG_PROC_FS
  2047. p = proc_net_create("ipv6_route", 0, rt6_proc_info);
  2048. if (p)
  2049. p->owner = THIS_MODULE;
  2050. proc_net_fops_create("rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
  2051. #endif
  2052. #ifdef CONFIG_XFRM
  2053. xfrm6_init();
  2054. #endif
  2055. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2056. fib6_rules_init();
  2057. #endif
  2058. }
  2059. void ip6_route_cleanup(void)
  2060. {
  2061. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  2062. fib6_rules_cleanup();
  2063. #endif
  2064. #ifdef CONFIG_PROC_FS
  2065. proc_net_remove("ipv6_route");
  2066. proc_net_remove("rt6_stats");
  2067. #endif
  2068. #ifdef CONFIG_XFRM
  2069. xfrm6_fini();
  2070. #endif
  2071. rt6_ifdown(NULL);
  2072. fib6_gc_cleanup();
  2073. kmem_cache_destroy(ip6_dst_ops.kmem_cachep);
  2074. }