route.c 53 KB

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