route.c 59 KB

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