route.c 50 KB

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