route.c 49 KB

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