route.c 59 KB

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