dn_route.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. /*
  2. * DECnet An implementation of the DECnet protocol suite for the LINUX
  3. * operating system. DECnet is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * DECnet Routing Functions (Endnode and Router)
  7. *
  8. * Authors: Steve Whitehouse <SteveW@ACM.org>
  9. * Eduardo Marcelo Serrat <emserrat@geocities.com>
  10. *
  11. * Changes:
  12. * Steve Whitehouse : Fixes to allow "intra-ethernet" and
  13. * "return-to-sender" bits on outgoing
  14. * packets.
  15. * Steve Whitehouse : Timeouts for cached routes.
  16. * Steve Whitehouse : Use dst cache for input routes too.
  17. * Steve Whitehouse : Fixed error values in dn_send_skb.
  18. * Steve Whitehouse : Rework routing functions to better fit
  19. * DECnet routing design
  20. * Alexey Kuznetsov : New SMP locking
  21. * Steve Whitehouse : More SMP locking changes & dn_cache_dump()
  22. * Steve Whitehouse : Prerouting NF hook, now really is prerouting.
  23. * Fixed possible skb leak in rtnetlink funcs.
  24. * Steve Whitehouse : Dave Miller's dynamic hash table sizing and
  25. * Alexey Kuznetsov's finer grained locking
  26. * from ipv4/route.c.
  27. * Steve Whitehouse : Routing is now starting to look like a
  28. * sensible set of code now, mainly due to
  29. * my copying the IPv4 routing code. The
  30. * hooks here are modified and will continue
  31. * to evolve for a while.
  32. * Steve Whitehouse : Real SMP at last :-) Also new netfilter
  33. * stuff. Look out raw sockets your days
  34. * are numbered!
  35. * Steve Whitehouse : Added return-to-sender functions. Added
  36. * backlog congestion level return codes.
  37. * Steve Whitehouse : Fixed bug where routes were set up with
  38. * no ref count on net devices.
  39. * Steve Whitehouse : RCU for the route cache
  40. * Steve Whitehouse : Preparations for the flow cache
  41. * Steve Whitehouse : Prepare for nonlinear skbs
  42. */
  43. /******************************************************************************
  44. (c) 1995-1998 E.M. Serrat emserrat@geocities.com
  45. This program is free software; you can redistribute it and/or modify
  46. it under the terms of the GNU General Public License as published by
  47. the Free Software Foundation; either version 2 of the License, or
  48. any later version.
  49. This program is distributed in the hope that it will be useful,
  50. but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. GNU General Public License for more details.
  53. *******************************************************************************/
  54. #include <linux/errno.h>
  55. #include <linux/types.h>
  56. #include <linux/socket.h>
  57. #include <linux/in.h>
  58. #include <linux/kernel.h>
  59. #include <linux/sockios.h>
  60. #include <linux/net.h>
  61. #include <linux/netdevice.h>
  62. #include <linux/inet.h>
  63. #include <linux/route.h>
  64. #include <linux/in_route.h>
  65. #include <linux/slab.h>
  66. #include <net/sock.h>
  67. #include <linux/mm.h>
  68. #include <linux/proc_fs.h>
  69. #include <linux/seq_file.h>
  70. #include <linux/init.h>
  71. #include <linux/rtnetlink.h>
  72. #include <linux/string.h>
  73. #include <linux/netfilter_decnet.h>
  74. #include <linux/rcupdate.h>
  75. #include <linux/times.h>
  76. #include <linux/export.h>
  77. #include <asm/errno.h>
  78. #include <net/net_namespace.h>
  79. #include <net/netlink.h>
  80. #include <net/neighbour.h>
  81. #include <net/dst.h>
  82. #include <net/flow.h>
  83. #include <net/fib_rules.h>
  84. #include <net/dn.h>
  85. #include <net/dn_dev.h>
  86. #include <net/dn_nsp.h>
  87. #include <net/dn_route.h>
  88. #include <net/dn_neigh.h>
  89. #include <net/dn_fib.h>
  90. struct dn_rt_hash_bucket
  91. {
  92. struct dn_route __rcu *chain;
  93. spinlock_t lock;
  94. };
  95. extern struct neigh_table dn_neigh_table;
  96. static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};
  97. static const int dn_rt_min_delay = 2 * HZ;
  98. static const int dn_rt_max_delay = 10 * HZ;
  99. static const int dn_rt_mtu_expires = 10 * 60 * HZ;
  100. static unsigned long dn_rt_deadline;
  101. static int dn_dst_gc(struct dst_ops *ops);
  102. static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);
  103. static unsigned int dn_dst_default_advmss(const struct dst_entry *dst);
  104. static unsigned int dn_dst_mtu(const struct dst_entry *dst);
  105. static void dn_dst_destroy(struct dst_entry *);
  106. static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
  107. static void dn_dst_link_failure(struct sk_buff *);
  108. static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu);
  109. static struct neighbour *dn_dst_neigh_lookup(const struct dst_entry *dst, const void *daddr);
  110. static int dn_route_input(struct sk_buff *);
  111. static void dn_run_flush(unsigned long dummy);
  112. static struct dn_rt_hash_bucket *dn_rt_hash_table;
  113. static unsigned int dn_rt_hash_mask;
  114. static struct timer_list dn_route_timer;
  115. static DEFINE_TIMER(dn_rt_flush_timer, dn_run_flush, 0, 0);
  116. int decnet_dst_gc_interval = 2;
  117. static struct dst_ops dn_dst_ops = {
  118. .family = PF_DECnet,
  119. .protocol = cpu_to_be16(ETH_P_DNA_RT),
  120. .gc_thresh = 128,
  121. .gc = dn_dst_gc,
  122. .check = dn_dst_check,
  123. .default_advmss = dn_dst_default_advmss,
  124. .mtu = dn_dst_mtu,
  125. .cow_metrics = dst_cow_metrics_generic,
  126. .destroy = dn_dst_destroy,
  127. .negative_advice = dn_dst_negative_advice,
  128. .link_failure = dn_dst_link_failure,
  129. .update_pmtu = dn_dst_update_pmtu,
  130. .neigh_lookup = dn_dst_neigh_lookup,
  131. };
  132. static void dn_dst_destroy(struct dst_entry *dst)
  133. {
  134. dst_destroy_metrics_generic(dst);
  135. }
  136. static __inline__ unsigned int dn_hash(__le16 src, __le16 dst)
  137. {
  138. __u16 tmp = (__u16 __force)(src ^ dst);
  139. tmp ^= (tmp >> 3);
  140. tmp ^= (tmp >> 5);
  141. tmp ^= (tmp >> 10);
  142. return dn_rt_hash_mask & (unsigned int)tmp;
  143. }
  144. static inline void dnrt_free(struct dn_route *rt)
  145. {
  146. call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
  147. }
  148. static inline void dnrt_drop(struct dn_route *rt)
  149. {
  150. dst_release(&rt->dst);
  151. call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
  152. }
  153. static void dn_dst_check_expire(unsigned long dummy)
  154. {
  155. int i;
  156. struct dn_route *rt;
  157. struct dn_route __rcu **rtp;
  158. unsigned long now = jiffies;
  159. unsigned long expire = 120 * HZ;
  160. for (i = 0; i <= dn_rt_hash_mask; i++) {
  161. rtp = &dn_rt_hash_table[i].chain;
  162. spin_lock(&dn_rt_hash_table[i].lock);
  163. while ((rt = rcu_dereference_protected(*rtp,
  164. lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
  165. if (atomic_read(&rt->dst.__refcnt) ||
  166. (now - rt->dst.lastuse) < expire) {
  167. rtp = &rt->dst.dn_next;
  168. continue;
  169. }
  170. *rtp = rt->dst.dn_next;
  171. rt->dst.dn_next = NULL;
  172. dnrt_free(rt);
  173. }
  174. spin_unlock(&dn_rt_hash_table[i].lock);
  175. if ((jiffies - now) > 0)
  176. break;
  177. }
  178. mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
  179. }
  180. static int dn_dst_gc(struct dst_ops *ops)
  181. {
  182. struct dn_route *rt;
  183. struct dn_route __rcu **rtp;
  184. int i;
  185. unsigned long now = jiffies;
  186. unsigned long expire = 10 * HZ;
  187. for (i = 0; i <= dn_rt_hash_mask; i++) {
  188. spin_lock_bh(&dn_rt_hash_table[i].lock);
  189. rtp = &dn_rt_hash_table[i].chain;
  190. while ((rt = rcu_dereference_protected(*rtp,
  191. lockdep_is_held(&dn_rt_hash_table[i].lock))) != NULL) {
  192. if (atomic_read(&rt->dst.__refcnt) ||
  193. (now - rt->dst.lastuse) < expire) {
  194. rtp = &rt->dst.dn_next;
  195. continue;
  196. }
  197. *rtp = rt->dst.dn_next;
  198. rt->dst.dn_next = NULL;
  199. dnrt_drop(rt);
  200. break;
  201. }
  202. spin_unlock_bh(&dn_rt_hash_table[i].lock);
  203. }
  204. return 0;
  205. }
  206. /*
  207. * The decnet standards don't impose a particular minimum mtu, what they
  208. * do insist on is that the routing layer accepts a datagram of at least
  209. * 230 bytes long. Here we have to subtract the routing header length from
  210. * 230 to get the minimum acceptable mtu. If there is no neighbour, then we
  211. * assume the worst and use a long header size.
  212. *
  213. * We update both the mtu and the advertised mss (i.e. the segment size we
  214. * advertise to the other end).
  215. */
  216. static void dn_dst_update_pmtu(struct dst_entry *dst, u32 mtu)
  217. {
  218. struct neighbour *n = dst_get_neighbour_noref(dst);
  219. u32 min_mtu = 230;
  220. struct dn_dev *dn;
  221. dn = n ? rcu_dereference_raw(n->dev->dn_ptr) : NULL;
  222. if (dn && dn->use_long == 0)
  223. min_mtu -= 6;
  224. else
  225. min_mtu -= 21;
  226. if (dst_metric(dst, RTAX_MTU) > mtu && mtu >= min_mtu) {
  227. if (!(dst_metric_locked(dst, RTAX_MTU))) {
  228. dst_metric_set(dst, RTAX_MTU, mtu);
  229. dst_set_expires(dst, dn_rt_mtu_expires);
  230. }
  231. if (!(dst_metric_locked(dst, RTAX_ADVMSS))) {
  232. u32 mss = mtu - DN_MAX_NSP_DATA_HEADER;
  233. u32 existing_mss = dst_metric_raw(dst, RTAX_ADVMSS);
  234. if (!existing_mss || existing_mss > mss)
  235. dst_metric_set(dst, RTAX_ADVMSS, mss);
  236. }
  237. }
  238. }
  239. /*
  240. * When a route has been marked obsolete. (e.g. routing cache flush)
  241. */
  242. static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
  243. {
  244. return NULL;
  245. }
  246. static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
  247. {
  248. dst_release(dst);
  249. return NULL;
  250. }
  251. static void dn_dst_link_failure(struct sk_buff *skb)
  252. {
  253. }
  254. static inline int compare_keys(struct flowidn *fl1, struct flowidn *fl2)
  255. {
  256. return ((fl1->daddr ^ fl2->daddr) |
  257. (fl1->saddr ^ fl2->saddr) |
  258. (fl1->flowidn_mark ^ fl2->flowidn_mark) |
  259. (fl1->flowidn_scope ^ fl2->flowidn_scope) |
  260. (fl1->flowidn_oif ^ fl2->flowidn_oif) |
  261. (fl1->flowidn_iif ^ fl2->flowidn_iif)) == 0;
  262. }
  263. static int dn_insert_route(struct dn_route *rt, unsigned int hash, struct dn_route **rp)
  264. {
  265. struct dn_route *rth;
  266. struct dn_route __rcu **rthp;
  267. unsigned long now = jiffies;
  268. rthp = &dn_rt_hash_table[hash].chain;
  269. spin_lock_bh(&dn_rt_hash_table[hash].lock);
  270. while ((rth = rcu_dereference_protected(*rthp,
  271. lockdep_is_held(&dn_rt_hash_table[hash].lock))) != NULL) {
  272. if (compare_keys(&rth->fld, &rt->fld)) {
  273. /* Put it first */
  274. *rthp = rth->dst.dn_next;
  275. rcu_assign_pointer(rth->dst.dn_next,
  276. dn_rt_hash_table[hash].chain);
  277. rcu_assign_pointer(dn_rt_hash_table[hash].chain, rth);
  278. dst_use(&rth->dst, now);
  279. spin_unlock_bh(&dn_rt_hash_table[hash].lock);
  280. dnrt_drop(rt);
  281. *rp = rth;
  282. return 0;
  283. }
  284. rthp = &rth->dst.dn_next;
  285. }
  286. rcu_assign_pointer(rt->dst.dn_next, dn_rt_hash_table[hash].chain);
  287. rcu_assign_pointer(dn_rt_hash_table[hash].chain, rt);
  288. dst_use(&rt->dst, now);
  289. spin_unlock_bh(&dn_rt_hash_table[hash].lock);
  290. *rp = rt;
  291. return 0;
  292. }
  293. static void dn_run_flush(unsigned long dummy)
  294. {
  295. int i;
  296. struct dn_route *rt, *next;
  297. for (i = 0; i < dn_rt_hash_mask; i++) {
  298. spin_lock_bh(&dn_rt_hash_table[i].lock);
  299. if ((rt = xchg((struct dn_route **)&dn_rt_hash_table[i].chain, NULL)) == NULL)
  300. goto nothing_to_declare;
  301. for(; rt; rt = next) {
  302. next = rcu_dereference_raw(rt->dst.dn_next);
  303. RCU_INIT_POINTER(rt->dst.dn_next, NULL);
  304. dst_free((struct dst_entry *)rt);
  305. }
  306. nothing_to_declare:
  307. spin_unlock_bh(&dn_rt_hash_table[i].lock);
  308. }
  309. }
  310. static DEFINE_SPINLOCK(dn_rt_flush_lock);
  311. void dn_rt_cache_flush(int delay)
  312. {
  313. unsigned long now = jiffies;
  314. int user_mode = !in_interrupt();
  315. if (delay < 0)
  316. delay = dn_rt_min_delay;
  317. spin_lock_bh(&dn_rt_flush_lock);
  318. if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
  319. long tmo = (long)(dn_rt_deadline - now);
  320. if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
  321. tmo = 0;
  322. if (delay > tmo)
  323. delay = tmo;
  324. }
  325. if (delay <= 0) {
  326. spin_unlock_bh(&dn_rt_flush_lock);
  327. dn_run_flush(0);
  328. return;
  329. }
  330. if (dn_rt_deadline == 0)
  331. dn_rt_deadline = now + dn_rt_max_delay;
  332. dn_rt_flush_timer.expires = now + delay;
  333. add_timer(&dn_rt_flush_timer);
  334. spin_unlock_bh(&dn_rt_flush_lock);
  335. }
  336. /**
  337. * dn_return_short - Return a short packet to its sender
  338. * @skb: The packet to return
  339. *
  340. */
  341. static int dn_return_short(struct sk_buff *skb)
  342. {
  343. struct dn_skb_cb *cb;
  344. unsigned char *ptr;
  345. __le16 *src;
  346. __le16 *dst;
  347. /* Add back headers */
  348. skb_push(skb, skb->data - skb_network_header(skb));
  349. if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
  350. return NET_RX_DROP;
  351. cb = DN_SKB_CB(skb);
  352. /* Skip packet length and point to flags */
  353. ptr = skb->data + 2;
  354. *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
  355. dst = (__le16 *)ptr;
  356. ptr += 2;
  357. src = (__le16 *)ptr;
  358. ptr += 2;
  359. *ptr = 0; /* Zero hop count */
  360. swap(*src, *dst);
  361. skb->pkt_type = PACKET_OUTGOING;
  362. dn_rt_finish_output(skb, NULL, NULL);
  363. return NET_RX_SUCCESS;
  364. }
  365. /**
  366. * dn_return_long - Return a long packet to its sender
  367. * @skb: The long format packet to return
  368. *
  369. */
  370. static int dn_return_long(struct sk_buff *skb)
  371. {
  372. struct dn_skb_cb *cb;
  373. unsigned char *ptr;
  374. unsigned char *src_addr, *dst_addr;
  375. unsigned char tmp[ETH_ALEN];
  376. /* Add back all headers */
  377. skb_push(skb, skb->data - skb_network_header(skb));
  378. if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
  379. return NET_RX_DROP;
  380. cb = DN_SKB_CB(skb);
  381. /* Ignore packet length and point to flags */
  382. ptr = skb->data + 2;
  383. /* Skip padding */
  384. if (*ptr & DN_RT_F_PF) {
  385. char padlen = (*ptr & ~DN_RT_F_PF);
  386. ptr += padlen;
  387. }
  388. *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
  389. ptr += 2;
  390. dst_addr = ptr;
  391. ptr += 8;
  392. src_addr = ptr;
  393. ptr += 6;
  394. *ptr = 0; /* Zero hop count */
  395. /* Swap source and destination */
  396. memcpy(tmp, src_addr, ETH_ALEN);
  397. memcpy(src_addr, dst_addr, ETH_ALEN);
  398. memcpy(dst_addr, tmp, ETH_ALEN);
  399. skb->pkt_type = PACKET_OUTGOING;
  400. dn_rt_finish_output(skb, dst_addr, src_addr);
  401. return NET_RX_SUCCESS;
  402. }
  403. /**
  404. * dn_route_rx_packet - Try and find a route for an incoming packet
  405. * @skb: The packet to find a route for
  406. *
  407. * Returns: result of input function if route is found, error code otherwise
  408. */
  409. static int dn_route_rx_packet(struct sk_buff *skb)
  410. {
  411. struct dn_skb_cb *cb;
  412. int err;
  413. if ((err = dn_route_input(skb)) == 0)
  414. return dst_input(skb);
  415. cb = DN_SKB_CB(skb);
  416. if (decnet_debug_level & 4) {
  417. char *devname = skb->dev ? skb->dev->name : "???";
  418. printk(KERN_DEBUG
  419. "DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%d\n",
  420. (int)cb->rt_flags, devname, skb->len,
  421. le16_to_cpu(cb->src), le16_to_cpu(cb->dst),
  422. err, skb->pkt_type);
  423. }
  424. if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
  425. switch (cb->rt_flags & DN_RT_PKT_MSK) {
  426. case DN_RT_PKT_SHORT:
  427. return dn_return_short(skb);
  428. case DN_RT_PKT_LONG:
  429. return dn_return_long(skb);
  430. }
  431. }
  432. kfree_skb(skb);
  433. return NET_RX_DROP;
  434. }
  435. static int dn_route_rx_long(struct sk_buff *skb)
  436. {
  437. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  438. unsigned char *ptr = skb->data;
  439. if (!pskb_may_pull(skb, 21)) /* 20 for long header, 1 for shortest nsp */
  440. goto drop_it;
  441. skb_pull(skb, 20);
  442. skb_reset_transport_header(skb);
  443. /* Destination info */
  444. ptr += 2;
  445. cb->dst = dn_eth2dn(ptr);
  446. if (memcmp(ptr, dn_hiord_addr, 4) != 0)
  447. goto drop_it;
  448. ptr += 6;
  449. /* Source info */
  450. ptr += 2;
  451. cb->src = dn_eth2dn(ptr);
  452. if (memcmp(ptr, dn_hiord_addr, 4) != 0)
  453. goto drop_it;
  454. ptr += 6;
  455. /* Other junk */
  456. ptr++;
  457. cb->hops = *ptr++; /* Visit Count */
  458. return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
  459. dn_route_rx_packet);
  460. drop_it:
  461. kfree_skb(skb);
  462. return NET_RX_DROP;
  463. }
  464. static int dn_route_rx_short(struct sk_buff *skb)
  465. {
  466. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  467. unsigned char *ptr = skb->data;
  468. if (!pskb_may_pull(skb, 6)) /* 5 for short header + 1 for shortest nsp */
  469. goto drop_it;
  470. skb_pull(skb, 5);
  471. skb_reset_transport_header(skb);
  472. cb->dst = *(__le16 *)ptr;
  473. ptr += 2;
  474. cb->src = *(__le16 *)ptr;
  475. ptr += 2;
  476. cb->hops = *ptr & 0x3f;
  477. return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL,
  478. dn_route_rx_packet);
  479. drop_it:
  480. kfree_skb(skb);
  481. return NET_RX_DROP;
  482. }
  483. static int dn_route_discard(struct sk_buff *skb)
  484. {
  485. /*
  486. * I know we drop the packet here, but thats considered success in
  487. * this case
  488. */
  489. kfree_skb(skb);
  490. return NET_RX_SUCCESS;
  491. }
  492. static int dn_route_ptp_hello(struct sk_buff *skb)
  493. {
  494. dn_dev_hello(skb);
  495. dn_neigh_pointopoint_hello(skb);
  496. return NET_RX_SUCCESS;
  497. }
  498. int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  499. {
  500. struct dn_skb_cb *cb;
  501. unsigned char flags = 0;
  502. __u16 len = le16_to_cpu(*(__le16 *)skb->data);
  503. struct dn_dev *dn = rcu_dereference(dev->dn_ptr);
  504. unsigned char padlen = 0;
  505. if (!net_eq(dev_net(dev), &init_net))
  506. goto dump_it;
  507. if (dn == NULL)
  508. goto dump_it;
  509. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  510. goto out;
  511. if (!pskb_may_pull(skb, 3))
  512. goto dump_it;
  513. skb_pull(skb, 2);
  514. if (len > skb->len)
  515. goto dump_it;
  516. skb_trim(skb, len);
  517. flags = *skb->data;
  518. cb = DN_SKB_CB(skb);
  519. cb->stamp = jiffies;
  520. cb->iif = dev->ifindex;
  521. /*
  522. * If we have padding, remove it.
  523. */
  524. if (flags & DN_RT_F_PF) {
  525. padlen = flags & ~DN_RT_F_PF;
  526. if (!pskb_may_pull(skb, padlen + 1))
  527. goto dump_it;
  528. skb_pull(skb, padlen);
  529. flags = *skb->data;
  530. }
  531. skb_reset_network_header(skb);
  532. /*
  533. * Weed out future version DECnet
  534. */
  535. if (flags & DN_RT_F_VER)
  536. goto dump_it;
  537. cb->rt_flags = flags;
  538. if (decnet_debug_level & 1)
  539. printk(KERN_DEBUG
  540. "dn_route_rcv: got 0x%02x from %s [%d %d %d]\n",
  541. (int)flags, (dev) ? dev->name : "???", len, skb->len,
  542. padlen);
  543. if (flags & DN_RT_PKT_CNTL) {
  544. if (unlikely(skb_linearize(skb)))
  545. goto dump_it;
  546. switch (flags & DN_RT_CNTL_MSK) {
  547. case DN_RT_PKT_INIT:
  548. dn_dev_init_pkt(skb);
  549. break;
  550. case DN_RT_PKT_VERI:
  551. dn_dev_veri_pkt(skb);
  552. break;
  553. }
  554. if (dn->parms.state != DN_DEV_S_RU)
  555. goto dump_it;
  556. switch (flags & DN_RT_CNTL_MSK) {
  557. case DN_RT_PKT_HELO:
  558. return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
  559. skb, skb->dev, NULL,
  560. dn_route_ptp_hello);
  561. case DN_RT_PKT_L1RT:
  562. case DN_RT_PKT_L2RT:
  563. return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
  564. skb, skb->dev, NULL,
  565. dn_route_discard);
  566. case DN_RT_PKT_ERTH:
  567. return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
  568. skb, skb->dev, NULL,
  569. dn_neigh_router_hello);
  570. case DN_RT_PKT_EEDH:
  571. return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
  572. skb, skb->dev, NULL,
  573. dn_neigh_endnode_hello);
  574. }
  575. } else {
  576. if (dn->parms.state != DN_DEV_S_RU)
  577. goto dump_it;
  578. skb_pull(skb, 1); /* Pull flags */
  579. switch (flags & DN_RT_PKT_MSK) {
  580. case DN_RT_PKT_LONG:
  581. return dn_route_rx_long(skb);
  582. case DN_RT_PKT_SHORT:
  583. return dn_route_rx_short(skb);
  584. }
  585. }
  586. dump_it:
  587. kfree_skb(skb);
  588. out:
  589. return NET_RX_DROP;
  590. }
  591. static int dn_to_neigh_output(struct sk_buff *skb)
  592. {
  593. struct dst_entry *dst = skb_dst(skb);
  594. struct neighbour *n = dst_get_neighbour_noref(dst);
  595. return n->output(n, skb);
  596. }
  597. static int dn_output(struct sk_buff *skb)
  598. {
  599. struct dst_entry *dst = skb_dst(skb);
  600. struct dn_route *rt = (struct dn_route *)dst;
  601. struct net_device *dev = dst->dev;
  602. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  603. int err = -EINVAL;
  604. if (dst_get_neighbour_noref(dst) == NULL)
  605. goto error;
  606. skb->dev = dev;
  607. cb->src = rt->rt_saddr;
  608. cb->dst = rt->rt_daddr;
  609. /*
  610. * Always set the Intra-Ethernet bit on all outgoing packets
  611. * originated on this node. Only valid flag from upper layers
  612. * is return-to-sender-requested. Set hop count to 0 too.
  613. */
  614. cb->rt_flags &= ~DN_RT_F_RQR;
  615. cb->rt_flags |= DN_RT_F_IE;
  616. cb->hops = 0;
  617. return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
  618. dn_to_neigh_output);
  619. error:
  620. net_dbg_ratelimited("dn_output: This should not happen\n");
  621. kfree_skb(skb);
  622. return err;
  623. }
  624. static int dn_forward(struct sk_buff *skb)
  625. {
  626. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  627. struct dst_entry *dst = skb_dst(skb);
  628. struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
  629. struct dn_route *rt;
  630. int header_len;
  631. #ifdef CONFIG_NETFILTER
  632. struct net_device *dev = skb->dev;
  633. #endif
  634. if (skb->pkt_type != PACKET_HOST)
  635. goto drop;
  636. /* Ensure that we have enough space for headers */
  637. rt = (struct dn_route *)skb_dst(skb);
  638. header_len = dn_db->use_long ? 21 : 6;
  639. if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
  640. goto drop;
  641. /*
  642. * Hop count exceeded.
  643. */
  644. if (++cb->hops > 30)
  645. goto drop;
  646. skb->dev = rt->dst.dev;
  647. /*
  648. * If packet goes out same interface it came in on, then set
  649. * the Intra-Ethernet bit. This has no effect for short
  650. * packets, so we don't need to test for them here.
  651. */
  652. cb->rt_flags &= ~DN_RT_F_IE;
  653. if (rt->rt_flags & RTCF_DOREDIRECT)
  654. cb->rt_flags |= DN_RT_F_IE;
  655. return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
  656. dn_to_neigh_output);
  657. drop:
  658. kfree_skb(skb);
  659. return NET_RX_DROP;
  660. }
  661. /*
  662. * Used to catch bugs. This should never normally get
  663. * called.
  664. */
  665. static int dn_rt_bug(struct sk_buff *skb)
  666. {
  667. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  668. net_dbg_ratelimited("dn_rt_bug: skb from:%04x to:%04x\n",
  669. le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
  670. kfree_skb(skb);
  671. return NET_RX_DROP;
  672. }
  673. static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
  674. {
  675. return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
  676. }
  677. static unsigned int dn_dst_mtu(const struct dst_entry *dst)
  678. {
  679. unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
  680. return mtu ? : dst->dev->mtu;
  681. }
  682. static struct neighbour *dn_dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
  683. {
  684. return __neigh_lookup_errno(&dn_neigh_table, daddr, dst->dev);
  685. }
  686. static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
  687. {
  688. struct dn_fib_info *fi = res->fi;
  689. struct net_device *dev = rt->dst.dev;
  690. unsigned int mss_metric;
  691. struct neighbour *n;
  692. if (fi) {
  693. if (DN_FIB_RES_GW(*res) &&
  694. DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
  695. rt->rt_gateway = DN_FIB_RES_GW(*res);
  696. dst_init_metrics(&rt->dst, fi->fib_metrics, true);
  697. }
  698. rt->rt_type = res->type;
  699. if (dev != NULL && dst_get_neighbour_noref(&rt->dst) == NULL) {
  700. n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
  701. if (IS_ERR(n))
  702. return PTR_ERR(n);
  703. dst_set_neighbour(&rt->dst, n);
  704. }
  705. if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
  706. dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
  707. mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
  708. if (mss_metric) {
  709. unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
  710. if (mss_metric > mss)
  711. dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
  712. }
  713. return 0;
  714. }
  715. static inline int dn_match_addr(__le16 addr1, __le16 addr2)
  716. {
  717. __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
  718. int match = 16;
  719. while(tmp) {
  720. tmp >>= 1;
  721. match--;
  722. }
  723. return match;
  724. }
  725. static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
  726. {
  727. __le16 saddr = 0;
  728. struct dn_dev *dn_db;
  729. struct dn_ifaddr *ifa;
  730. int best_match = 0;
  731. int ret;
  732. rcu_read_lock();
  733. dn_db = rcu_dereference(dev->dn_ptr);
  734. for (ifa = rcu_dereference(dn_db->ifa_list);
  735. ifa != NULL;
  736. ifa = rcu_dereference(ifa->ifa_next)) {
  737. if (ifa->ifa_scope > scope)
  738. continue;
  739. if (!daddr) {
  740. saddr = ifa->ifa_local;
  741. break;
  742. }
  743. ret = dn_match_addr(daddr, ifa->ifa_local);
  744. if (ret > best_match)
  745. saddr = ifa->ifa_local;
  746. if (best_match == 0)
  747. saddr = ifa->ifa_local;
  748. }
  749. rcu_read_unlock();
  750. return saddr;
  751. }
  752. static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
  753. {
  754. return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
  755. }
  756. static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
  757. {
  758. __le16 mask = dnet_make_mask(res->prefixlen);
  759. return (daddr&~mask)|res->fi->fib_nh->nh_gw;
  760. }
  761. static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oldflp, int try_hard)
  762. {
  763. struct flowidn fld = {
  764. .daddr = oldflp->daddr,
  765. .saddr = oldflp->saddr,
  766. .flowidn_scope = RT_SCOPE_UNIVERSE,
  767. .flowidn_mark = oldflp->flowidn_mark,
  768. .flowidn_iif = init_net.loopback_dev->ifindex,
  769. .flowidn_oif = oldflp->flowidn_oif,
  770. };
  771. struct dn_route *rt = NULL;
  772. struct net_device *dev_out = NULL, *dev;
  773. struct neighbour *neigh = NULL;
  774. unsigned int hash;
  775. unsigned int flags = 0;
  776. struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
  777. int err;
  778. int free_res = 0;
  779. __le16 gateway = 0;
  780. if (decnet_debug_level & 16)
  781. printk(KERN_DEBUG
  782. "dn_route_output_slow: dst=%04x src=%04x mark=%d"
  783. " iif=%d oif=%d\n", le16_to_cpu(oldflp->daddr),
  784. le16_to_cpu(oldflp->saddr),
  785. oldflp->flowidn_mark, init_net.loopback_dev->ifindex,
  786. oldflp->flowidn_oif);
  787. /* If we have an output interface, verify its a DECnet device */
  788. if (oldflp->flowidn_oif) {
  789. dev_out = dev_get_by_index(&init_net, oldflp->flowidn_oif);
  790. err = -ENODEV;
  791. if (dev_out && dev_out->dn_ptr == NULL) {
  792. dev_put(dev_out);
  793. dev_out = NULL;
  794. }
  795. if (dev_out == NULL)
  796. goto out;
  797. }
  798. /* If we have a source address, verify that its a local address */
  799. if (oldflp->saddr) {
  800. err = -EADDRNOTAVAIL;
  801. if (dev_out) {
  802. if (dn_dev_islocal(dev_out, oldflp->saddr))
  803. goto source_ok;
  804. dev_put(dev_out);
  805. goto out;
  806. }
  807. rcu_read_lock();
  808. for_each_netdev_rcu(&init_net, dev) {
  809. if (!dev->dn_ptr)
  810. continue;
  811. if (!dn_dev_islocal(dev, oldflp->saddr))
  812. continue;
  813. if ((dev->flags & IFF_LOOPBACK) &&
  814. oldflp->daddr &&
  815. !dn_dev_islocal(dev, oldflp->daddr))
  816. continue;
  817. dev_out = dev;
  818. break;
  819. }
  820. rcu_read_unlock();
  821. if (dev_out == NULL)
  822. goto out;
  823. dev_hold(dev_out);
  824. source_ok:
  825. ;
  826. }
  827. /* No destination? Assume its local */
  828. if (!fld.daddr) {
  829. fld.daddr = fld.saddr;
  830. err = -EADDRNOTAVAIL;
  831. if (dev_out)
  832. dev_put(dev_out);
  833. dev_out = init_net.loopback_dev;
  834. dev_hold(dev_out);
  835. if (!fld.daddr) {
  836. fld.daddr =
  837. fld.saddr = dnet_select_source(dev_out, 0,
  838. RT_SCOPE_HOST);
  839. if (!fld.daddr)
  840. goto out;
  841. }
  842. fld.flowidn_oif = init_net.loopback_dev->ifindex;
  843. res.type = RTN_LOCAL;
  844. goto make_route;
  845. }
  846. if (decnet_debug_level & 16)
  847. printk(KERN_DEBUG
  848. "dn_route_output_slow: initial checks complete."
  849. " dst=%o4x src=%04x oif=%d try_hard=%d\n",
  850. le16_to_cpu(fld.daddr), le16_to_cpu(fld.saddr),
  851. fld.flowidn_oif, try_hard);
  852. /*
  853. * N.B. If the kernel is compiled without router support then
  854. * dn_fib_lookup() will evaluate to non-zero so this if () block
  855. * will always be executed.
  856. */
  857. err = -ESRCH;
  858. if (try_hard || (err = dn_fib_lookup(&fld, &res)) != 0) {
  859. struct dn_dev *dn_db;
  860. if (err != -ESRCH)
  861. goto out;
  862. /*
  863. * Here the fallback is basically the standard algorithm for
  864. * routing in endnodes which is described in the DECnet routing
  865. * docs
  866. *
  867. * If we are not trying hard, look in neighbour cache.
  868. * The result is tested to ensure that if a specific output
  869. * device/source address was requested, then we honour that
  870. * here
  871. */
  872. if (!try_hard) {
  873. neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fld.daddr);
  874. if (neigh) {
  875. if ((oldflp->flowidn_oif &&
  876. (neigh->dev->ifindex != oldflp->flowidn_oif)) ||
  877. (oldflp->saddr &&
  878. (!dn_dev_islocal(neigh->dev,
  879. oldflp->saddr)))) {
  880. neigh_release(neigh);
  881. neigh = NULL;
  882. } else {
  883. if (dev_out)
  884. dev_put(dev_out);
  885. if (dn_dev_islocal(neigh->dev, fld.daddr)) {
  886. dev_out = init_net.loopback_dev;
  887. res.type = RTN_LOCAL;
  888. } else {
  889. dev_out = neigh->dev;
  890. }
  891. dev_hold(dev_out);
  892. goto select_source;
  893. }
  894. }
  895. }
  896. /* Not there? Perhaps its a local address */
  897. if (dev_out == NULL)
  898. dev_out = dn_dev_get_default();
  899. err = -ENODEV;
  900. if (dev_out == NULL)
  901. goto out;
  902. dn_db = rcu_dereference_raw(dev_out->dn_ptr);
  903. /* Possible improvement - check all devices for local addr */
  904. if (dn_dev_islocal(dev_out, fld.daddr)) {
  905. dev_put(dev_out);
  906. dev_out = init_net.loopback_dev;
  907. dev_hold(dev_out);
  908. res.type = RTN_LOCAL;
  909. goto select_source;
  910. }
  911. /* Not local either.... try sending it to the default router */
  912. neigh = neigh_clone(dn_db->router);
  913. BUG_ON(neigh && neigh->dev != dev_out);
  914. /* Ok then, we assume its directly connected and move on */
  915. select_source:
  916. if (neigh)
  917. gateway = ((struct dn_neigh *)neigh)->addr;
  918. if (gateway == 0)
  919. gateway = fld.daddr;
  920. if (fld.saddr == 0) {
  921. fld.saddr = dnet_select_source(dev_out, gateway,
  922. res.type == RTN_LOCAL ?
  923. RT_SCOPE_HOST :
  924. RT_SCOPE_LINK);
  925. if (fld.saddr == 0 && res.type != RTN_LOCAL)
  926. goto e_addr;
  927. }
  928. fld.flowidn_oif = dev_out->ifindex;
  929. goto make_route;
  930. }
  931. free_res = 1;
  932. if (res.type == RTN_NAT)
  933. goto e_inval;
  934. if (res.type == RTN_LOCAL) {
  935. if (!fld.saddr)
  936. fld.saddr = fld.daddr;
  937. if (dev_out)
  938. dev_put(dev_out);
  939. dev_out = init_net.loopback_dev;
  940. dev_hold(dev_out);
  941. fld.flowidn_oif = dev_out->ifindex;
  942. if (res.fi)
  943. dn_fib_info_put(res.fi);
  944. res.fi = NULL;
  945. goto make_route;
  946. }
  947. if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
  948. dn_fib_select_multipath(&fld, &res);
  949. /*
  950. * We could add some logic to deal with default routes here and
  951. * get rid of some of the special casing above.
  952. */
  953. if (!fld.saddr)
  954. fld.saddr = DN_FIB_RES_PREFSRC(res);
  955. if (dev_out)
  956. dev_put(dev_out);
  957. dev_out = DN_FIB_RES_DEV(res);
  958. dev_hold(dev_out);
  959. fld.flowidn_oif = dev_out->ifindex;
  960. gateway = DN_FIB_RES_GW(res);
  961. make_route:
  962. if (dev_out->flags & IFF_LOOPBACK)
  963. flags |= RTCF_LOCAL;
  964. rt = dst_alloc(&dn_dst_ops, dev_out, 1, 0, DST_HOST);
  965. if (rt == NULL)
  966. goto e_nobufs;
  967. memset(&rt->fld, 0, sizeof(rt->fld));
  968. rt->fld.saddr = oldflp->saddr;
  969. rt->fld.daddr = oldflp->daddr;
  970. rt->fld.flowidn_oif = oldflp->flowidn_oif;
  971. rt->fld.flowidn_iif = 0;
  972. rt->fld.flowidn_mark = oldflp->flowidn_mark;
  973. rt->rt_saddr = fld.saddr;
  974. rt->rt_daddr = fld.daddr;
  975. rt->rt_gateway = gateway ? gateway : fld.daddr;
  976. rt->rt_local_src = fld.saddr;
  977. rt->rt_dst_map = fld.daddr;
  978. rt->rt_src_map = fld.saddr;
  979. dst_set_neighbour(&rt->dst, neigh);
  980. neigh = NULL;
  981. rt->dst.lastuse = jiffies;
  982. rt->dst.output = dn_output;
  983. rt->dst.input = dn_rt_bug;
  984. rt->rt_flags = flags;
  985. if (flags & RTCF_LOCAL)
  986. rt->dst.input = dn_nsp_rx;
  987. err = dn_rt_set_next_hop(rt, &res);
  988. if (err)
  989. goto e_neighbour;
  990. hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
  991. dn_insert_route(rt, hash, (struct dn_route **)pprt);
  992. done:
  993. if (neigh)
  994. neigh_release(neigh);
  995. if (free_res)
  996. dn_fib_res_put(&res);
  997. if (dev_out)
  998. dev_put(dev_out);
  999. out:
  1000. return err;
  1001. e_addr:
  1002. err = -EADDRNOTAVAIL;
  1003. goto done;
  1004. e_inval:
  1005. err = -EINVAL;
  1006. goto done;
  1007. e_nobufs:
  1008. err = -ENOBUFS;
  1009. goto done;
  1010. e_neighbour:
  1011. dst_free(&rt->dst);
  1012. goto e_nobufs;
  1013. }
  1014. /*
  1015. * N.B. The flags may be moved into the flowi at some future stage.
  1016. */
  1017. static int __dn_route_output_key(struct dst_entry **pprt, const struct flowidn *flp, int flags)
  1018. {
  1019. unsigned int hash = dn_hash(flp->saddr, flp->daddr);
  1020. struct dn_route *rt = NULL;
  1021. if (!(flags & MSG_TRYHARD)) {
  1022. rcu_read_lock_bh();
  1023. for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
  1024. rt = rcu_dereference_bh(rt->dst.dn_next)) {
  1025. if ((flp->daddr == rt->fld.daddr) &&
  1026. (flp->saddr == rt->fld.saddr) &&
  1027. (flp->flowidn_mark == rt->fld.flowidn_mark) &&
  1028. dn_is_output_route(rt) &&
  1029. (rt->fld.flowidn_oif == flp->flowidn_oif)) {
  1030. dst_use(&rt->dst, jiffies);
  1031. rcu_read_unlock_bh();
  1032. *pprt = &rt->dst;
  1033. return 0;
  1034. }
  1035. }
  1036. rcu_read_unlock_bh();
  1037. }
  1038. return dn_route_output_slow(pprt, flp, flags);
  1039. }
  1040. static int dn_route_output_key(struct dst_entry **pprt, struct flowidn *flp, int flags)
  1041. {
  1042. int err;
  1043. err = __dn_route_output_key(pprt, flp, flags);
  1044. if (err == 0 && flp->flowidn_proto) {
  1045. *pprt = xfrm_lookup(&init_net, *pprt,
  1046. flowidn_to_flowi(flp), NULL, 0);
  1047. if (IS_ERR(*pprt)) {
  1048. err = PTR_ERR(*pprt);
  1049. *pprt = NULL;
  1050. }
  1051. }
  1052. return err;
  1053. }
  1054. int dn_route_output_sock(struct dst_entry **pprt, struct flowidn *fl, struct sock *sk, int flags)
  1055. {
  1056. int err;
  1057. err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
  1058. if (err == 0 && fl->flowidn_proto) {
  1059. if (!(flags & MSG_DONTWAIT))
  1060. fl->flowidn_flags |= FLOWI_FLAG_CAN_SLEEP;
  1061. *pprt = xfrm_lookup(&init_net, *pprt,
  1062. flowidn_to_flowi(fl), sk, 0);
  1063. if (IS_ERR(*pprt)) {
  1064. err = PTR_ERR(*pprt);
  1065. *pprt = NULL;
  1066. }
  1067. }
  1068. return err;
  1069. }
  1070. static int dn_route_input_slow(struct sk_buff *skb)
  1071. {
  1072. struct dn_route *rt = NULL;
  1073. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  1074. struct net_device *in_dev = skb->dev;
  1075. struct net_device *out_dev = NULL;
  1076. struct dn_dev *dn_db;
  1077. struct neighbour *neigh = NULL;
  1078. unsigned int hash;
  1079. int flags = 0;
  1080. __le16 gateway = 0;
  1081. __le16 local_src = 0;
  1082. struct flowidn fld = {
  1083. .daddr = cb->dst,
  1084. .saddr = cb->src,
  1085. .flowidn_scope = RT_SCOPE_UNIVERSE,
  1086. .flowidn_mark = skb->mark,
  1087. .flowidn_iif = skb->dev->ifindex,
  1088. };
  1089. struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
  1090. int err = -EINVAL;
  1091. int free_res = 0;
  1092. dev_hold(in_dev);
  1093. if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
  1094. goto out;
  1095. /* Zero source addresses are not allowed */
  1096. if (fld.saddr == 0)
  1097. goto out;
  1098. /*
  1099. * In this case we've just received a packet from a source
  1100. * outside ourselves pretending to come from us. We don't
  1101. * allow it any further to prevent routing loops, spoofing and
  1102. * other nasties. Loopback packets already have the dst attached
  1103. * so this only affects packets which have originated elsewhere.
  1104. */
  1105. err = -ENOTUNIQ;
  1106. if (dn_dev_islocal(in_dev, cb->src))
  1107. goto out;
  1108. err = dn_fib_lookup(&fld, &res);
  1109. if (err) {
  1110. if (err != -ESRCH)
  1111. goto out;
  1112. /*
  1113. * Is the destination us ?
  1114. */
  1115. if (!dn_dev_islocal(in_dev, cb->dst))
  1116. goto e_inval;
  1117. res.type = RTN_LOCAL;
  1118. } else {
  1119. __le16 src_map = fld.saddr;
  1120. free_res = 1;
  1121. out_dev = DN_FIB_RES_DEV(res);
  1122. if (out_dev == NULL) {
  1123. net_crit_ratelimited("Bug in dn_route_input_slow() No output device\n");
  1124. goto e_inval;
  1125. }
  1126. dev_hold(out_dev);
  1127. if (res.r)
  1128. src_map = fld.saddr; /* no NAT support for now */
  1129. gateway = DN_FIB_RES_GW(res);
  1130. if (res.type == RTN_NAT) {
  1131. fld.daddr = dn_fib_rules_map_destination(fld.daddr, &res);
  1132. dn_fib_res_put(&res);
  1133. free_res = 0;
  1134. if (dn_fib_lookup(&fld, &res))
  1135. goto e_inval;
  1136. free_res = 1;
  1137. if (res.type != RTN_UNICAST)
  1138. goto e_inval;
  1139. flags |= RTCF_DNAT;
  1140. gateway = fld.daddr;
  1141. }
  1142. fld.saddr = src_map;
  1143. }
  1144. switch(res.type) {
  1145. case RTN_UNICAST:
  1146. /*
  1147. * Forwarding check here, we only check for forwarding
  1148. * being turned off, if you want to only forward intra
  1149. * area, its up to you to set the routing tables up
  1150. * correctly.
  1151. */
  1152. if (dn_db->parms.forwarding == 0)
  1153. goto e_inval;
  1154. if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
  1155. dn_fib_select_multipath(&fld, &res);
  1156. /*
  1157. * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
  1158. * flag as a hint to set the intra-ethernet bit when
  1159. * forwarding. If we've got NAT in operation, we don't do
  1160. * this optimisation.
  1161. */
  1162. if (out_dev == in_dev && !(flags & RTCF_NAT))
  1163. flags |= RTCF_DOREDIRECT;
  1164. local_src = DN_FIB_RES_PREFSRC(res);
  1165. case RTN_BLACKHOLE:
  1166. case RTN_UNREACHABLE:
  1167. break;
  1168. case RTN_LOCAL:
  1169. flags |= RTCF_LOCAL;
  1170. fld.saddr = cb->dst;
  1171. fld.daddr = cb->src;
  1172. /* Routing tables gave us a gateway */
  1173. if (gateway)
  1174. goto make_route;
  1175. /* Packet was intra-ethernet, so we know its on-link */
  1176. if (cb->rt_flags & DN_RT_F_IE) {
  1177. gateway = cb->src;
  1178. flags |= RTCF_DIRECTSRC;
  1179. goto make_route;
  1180. }
  1181. /* Use the default router if there is one */
  1182. neigh = neigh_clone(dn_db->router);
  1183. if (neigh) {
  1184. gateway = ((struct dn_neigh *)neigh)->addr;
  1185. goto make_route;
  1186. }
  1187. /* Close eyes and pray */
  1188. gateway = cb->src;
  1189. flags |= RTCF_DIRECTSRC;
  1190. goto make_route;
  1191. default:
  1192. goto e_inval;
  1193. }
  1194. make_route:
  1195. rt = dst_alloc(&dn_dst_ops, out_dev, 0, 0, DST_HOST);
  1196. if (rt == NULL)
  1197. goto e_nobufs;
  1198. memset(&rt->fld, 0, sizeof(rt->fld));
  1199. rt->rt_saddr = fld.saddr;
  1200. rt->rt_daddr = fld.daddr;
  1201. rt->rt_gateway = fld.daddr;
  1202. if (gateway)
  1203. rt->rt_gateway = gateway;
  1204. rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
  1205. rt->rt_dst_map = fld.daddr;
  1206. rt->rt_src_map = fld.saddr;
  1207. rt->fld.saddr = cb->src;
  1208. rt->fld.daddr = cb->dst;
  1209. rt->fld.flowidn_oif = 0;
  1210. rt->fld.flowidn_iif = in_dev->ifindex;
  1211. rt->fld.flowidn_mark = fld.flowidn_mark;
  1212. dst_set_neighbour(&rt->dst, neigh);
  1213. rt->dst.lastuse = jiffies;
  1214. rt->dst.output = dn_rt_bug;
  1215. switch (res.type) {
  1216. case RTN_UNICAST:
  1217. rt->dst.input = dn_forward;
  1218. break;
  1219. case RTN_LOCAL:
  1220. rt->dst.output = dn_output;
  1221. rt->dst.input = dn_nsp_rx;
  1222. rt->dst.dev = in_dev;
  1223. flags |= RTCF_LOCAL;
  1224. break;
  1225. default:
  1226. case RTN_UNREACHABLE:
  1227. case RTN_BLACKHOLE:
  1228. rt->dst.input = dst_discard;
  1229. }
  1230. rt->rt_flags = flags;
  1231. err = dn_rt_set_next_hop(rt, &res);
  1232. if (err)
  1233. goto e_neighbour;
  1234. hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
  1235. dn_insert_route(rt, hash, &rt);
  1236. skb_dst_set(skb, &rt->dst);
  1237. done:
  1238. if (neigh)
  1239. neigh_release(neigh);
  1240. if (free_res)
  1241. dn_fib_res_put(&res);
  1242. dev_put(in_dev);
  1243. if (out_dev)
  1244. dev_put(out_dev);
  1245. out:
  1246. return err;
  1247. e_inval:
  1248. err = -EINVAL;
  1249. goto done;
  1250. e_nobufs:
  1251. err = -ENOBUFS;
  1252. goto done;
  1253. e_neighbour:
  1254. dst_free(&rt->dst);
  1255. goto done;
  1256. }
  1257. static int dn_route_input(struct sk_buff *skb)
  1258. {
  1259. struct dn_route *rt;
  1260. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  1261. unsigned int hash = dn_hash(cb->src, cb->dst);
  1262. if (skb_dst(skb))
  1263. return 0;
  1264. rcu_read_lock();
  1265. for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
  1266. rt = rcu_dereference(rt->dst.dn_next)) {
  1267. if ((rt->fld.saddr == cb->src) &&
  1268. (rt->fld.daddr == cb->dst) &&
  1269. (rt->fld.flowidn_oif == 0) &&
  1270. (rt->fld.flowidn_mark == skb->mark) &&
  1271. (rt->fld.flowidn_iif == cb->iif)) {
  1272. dst_use(&rt->dst, jiffies);
  1273. rcu_read_unlock();
  1274. skb_dst_set(skb, (struct dst_entry *)rt);
  1275. return 0;
  1276. }
  1277. }
  1278. rcu_read_unlock();
  1279. return dn_route_input_slow(skb);
  1280. }
  1281. static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
  1282. int event, int nowait, unsigned int flags)
  1283. {
  1284. struct dn_route *rt = (struct dn_route *)skb_dst(skb);
  1285. struct rtmsg *r;
  1286. struct nlmsghdr *nlh;
  1287. unsigned char *b = skb_tail_pointer(skb);
  1288. long expires;
  1289. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
  1290. r = NLMSG_DATA(nlh);
  1291. r->rtm_family = AF_DECnet;
  1292. r->rtm_dst_len = 16;
  1293. r->rtm_src_len = 0;
  1294. r->rtm_tos = 0;
  1295. r->rtm_table = RT_TABLE_MAIN;
  1296. RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
  1297. r->rtm_type = rt->rt_type;
  1298. r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
  1299. r->rtm_scope = RT_SCOPE_UNIVERSE;
  1300. r->rtm_protocol = RTPROT_UNSPEC;
  1301. if (rt->rt_flags & RTCF_NOTIFY)
  1302. r->rtm_flags |= RTM_F_NOTIFY;
  1303. RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
  1304. if (rt->fld.saddr) {
  1305. r->rtm_src_len = 16;
  1306. RTA_PUT(skb, RTA_SRC, 2, &rt->fld.saddr);
  1307. }
  1308. if (rt->dst.dev)
  1309. RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
  1310. /*
  1311. * Note to self - change this if input routes reverse direction when
  1312. * they deal only with inputs and not with replies like they do
  1313. * currently.
  1314. */
  1315. RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
  1316. if (rt->rt_daddr != rt->rt_gateway)
  1317. RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
  1318. if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
  1319. goto rtattr_failure;
  1320. expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
  1321. if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
  1322. rt->dst.error) < 0)
  1323. goto rtattr_failure;
  1324. if (dn_is_input_route(rt))
  1325. RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fld.flowidn_iif);
  1326. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1327. return skb->len;
  1328. nlmsg_failure:
  1329. rtattr_failure:
  1330. nlmsg_trim(skb, b);
  1331. return -1;
  1332. }
  1333. /*
  1334. * This is called by both endnodes and routers now.
  1335. */
  1336. static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
  1337. {
  1338. struct net *net = sock_net(in_skb->sk);
  1339. struct rtattr **rta = arg;
  1340. struct rtmsg *rtm = NLMSG_DATA(nlh);
  1341. struct dn_route *rt = NULL;
  1342. struct dn_skb_cb *cb;
  1343. int err;
  1344. struct sk_buff *skb;
  1345. struct flowidn fld;
  1346. if (!net_eq(net, &init_net))
  1347. return -EINVAL;
  1348. memset(&fld, 0, sizeof(fld));
  1349. fld.flowidn_proto = DNPROTO_NSP;
  1350. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1351. if (skb == NULL)
  1352. return -ENOBUFS;
  1353. skb_reset_mac_header(skb);
  1354. cb = DN_SKB_CB(skb);
  1355. if (rta[RTA_SRC-1])
  1356. memcpy(&fld.saddr, RTA_DATA(rta[RTA_SRC-1]), 2);
  1357. if (rta[RTA_DST-1])
  1358. memcpy(&fld.daddr, RTA_DATA(rta[RTA_DST-1]), 2);
  1359. if (rta[RTA_IIF-1])
  1360. memcpy(&fld.flowidn_iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
  1361. if (fld.flowidn_iif) {
  1362. struct net_device *dev;
  1363. if ((dev = dev_get_by_index(&init_net, fld.flowidn_iif)) == NULL) {
  1364. kfree_skb(skb);
  1365. return -ENODEV;
  1366. }
  1367. if (!dev->dn_ptr) {
  1368. dev_put(dev);
  1369. kfree_skb(skb);
  1370. return -ENODEV;
  1371. }
  1372. skb->protocol = htons(ETH_P_DNA_RT);
  1373. skb->dev = dev;
  1374. cb->src = fld.saddr;
  1375. cb->dst = fld.daddr;
  1376. local_bh_disable();
  1377. err = dn_route_input(skb);
  1378. local_bh_enable();
  1379. memset(cb, 0, sizeof(struct dn_skb_cb));
  1380. rt = (struct dn_route *)skb_dst(skb);
  1381. if (!err && -rt->dst.error)
  1382. err = rt->dst.error;
  1383. } else {
  1384. int oif = 0;
  1385. if (rta[RTA_OIF - 1])
  1386. memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
  1387. fld.flowidn_oif = oif;
  1388. err = dn_route_output_key((struct dst_entry **)&rt, &fld, 0);
  1389. }
  1390. if (skb->dev)
  1391. dev_put(skb->dev);
  1392. skb->dev = NULL;
  1393. if (err)
  1394. goto out_free;
  1395. skb_dst_set(skb, &rt->dst);
  1396. if (rtm->rtm_flags & RTM_F_NOTIFY)
  1397. rt->rt_flags |= RTCF_NOTIFY;
  1398. err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, RTM_NEWROUTE, 0, 0);
  1399. if (err == 0)
  1400. goto out_free;
  1401. if (err < 0) {
  1402. err = -EMSGSIZE;
  1403. goto out_free;
  1404. }
  1405. return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
  1406. out_free:
  1407. kfree_skb(skb);
  1408. return err;
  1409. }
  1410. /*
  1411. * For routers, this is called from dn_fib_dump, but for endnodes its
  1412. * called directly from the rtnetlink dispatch table.
  1413. */
  1414. int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1415. {
  1416. struct net *net = sock_net(skb->sk);
  1417. struct dn_route *rt;
  1418. int h, s_h;
  1419. int idx, s_idx;
  1420. if (!net_eq(net, &init_net))
  1421. return 0;
  1422. if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
  1423. return -EINVAL;
  1424. if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
  1425. return 0;
  1426. s_h = cb->args[0];
  1427. s_idx = idx = cb->args[1];
  1428. for(h = 0; h <= dn_rt_hash_mask; h++) {
  1429. if (h < s_h)
  1430. continue;
  1431. if (h > s_h)
  1432. s_idx = 0;
  1433. rcu_read_lock_bh();
  1434. for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
  1435. rt;
  1436. rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
  1437. if (idx < s_idx)
  1438. continue;
  1439. skb_dst_set(skb, dst_clone(&rt->dst));
  1440. if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
  1441. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1442. 1, NLM_F_MULTI) <= 0) {
  1443. skb_dst_drop(skb);
  1444. rcu_read_unlock_bh();
  1445. goto done;
  1446. }
  1447. skb_dst_drop(skb);
  1448. }
  1449. rcu_read_unlock_bh();
  1450. }
  1451. done:
  1452. cb->args[0] = h;
  1453. cb->args[1] = idx;
  1454. return skb->len;
  1455. }
  1456. #ifdef CONFIG_PROC_FS
  1457. struct dn_rt_cache_iter_state {
  1458. int bucket;
  1459. };
  1460. static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
  1461. {
  1462. struct dn_route *rt = NULL;
  1463. struct dn_rt_cache_iter_state *s = seq->private;
  1464. for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
  1465. rcu_read_lock_bh();
  1466. rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
  1467. if (rt)
  1468. break;
  1469. rcu_read_unlock_bh();
  1470. }
  1471. return rt;
  1472. }
  1473. static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
  1474. {
  1475. struct dn_rt_cache_iter_state *s = seq->private;
  1476. rt = rcu_dereference_bh(rt->dst.dn_next);
  1477. while (!rt) {
  1478. rcu_read_unlock_bh();
  1479. if (--s->bucket < 0)
  1480. break;
  1481. rcu_read_lock_bh();
  1482. rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
  1483. }
  1484. return rt;
  1485. }
  1486. static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
  1487. {
  1488. struct dn_route *rt = dn_rt_cache_get_first(seq);
  1489. if (rt) {
  1490. while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
  1491. --*pos;
  1492. }
  1493. return *pos ? NULL : rt;
  1494. }
  1495. static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1496. {
  1497. struct dn_route *rt = dn_rt_cache_get_next(seq, v);
  1498. ++*pos;
  1499. return rt;
  1500. }
  1501. static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
  1502. {
  1503. if (v)
  1504. rcu_read_unlock_bh();
  1505. }
  1506. static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
  1507. {
  1508. struct dn_route *rt = v;
  1509. char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
  1510. seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
  1511. rt->dst.dev ? rt->dst.dev->name : "*",
  1512. dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
  1513. dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
  1514. atomic_read(&rt->dst.__refcnt),
  1515. rt->dst.__use,
  1516. (int) dst_metric(&rt->dst, RTAX_RTT));
  1517. return 0;
  1518. }
  1519. static const struct seq_operations dn_rt_cache_seq_ops = {
  1520. .start = dn_rt_cache_seq_start,
  1521. .next = dn_rt_cache_seq_next,
  1522. .stop = dn_rt_cache_seq_stop,
  1523. .show = dn_rt_cache_seq_show,
  1524. };
  1525. static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
  1526. {
  1527. return seq_open_private(file, &dn_rt_cache_seq_ops,
  1528. sizeof(struct dn_rt_cache_iter_state));
  1529. }
  1530. static const struct file_operations dn_rt_cache_seq_fops = {
  1531. .owner = THIS_MODULE,
  1532. .open = dn_rt_cache_seq_open,
  1533. .read = seq_read,
  1534. .llseek = seq_lseek,
  1535. .release = seq_release_private,
  1536. };
  1537. #endif /* CONFIG_PROC_FS */
  1538. void __init dn_route_init(void)
  1539. {
  1540. int i, goal, order;
  1541. dn_dst_ops.kmem_cachep =
  1542. kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
  1543. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1544. dst_entries_init(&dn_dst_ops);
  1545. setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
  1546. dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
  1547. add_timer(&dn_route_timer);
  1548. goal = totalram_pages >> (26 - PAGE_SHIFT);
  1549. for(order = 0; (1UL << order) < goal; order++)
  1550. /* NOTHING */;
  1551. /*
  1552. * Only want 1024 entries max, since the table is very, very unlikely
  1553. * to be larger than that.
  1554. */
  1555. while(order && ((((1UL << order) * PAGE_SIZE) /
  1556. sizeof(struct dn_rt_hash_bucket)) >= 2048))
  1557. order--;
  1558. do {
  1559. dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
  1560. sizeof(struct dn_rt_hash_bucket);
  1561. while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
  1562. dn_rt_hash_mask--;
  1563. dn_rt_hash_table = (struct dn_rt_hash_bucket *)
  1564. __get_free_pages(GFP_ATOMIC, order);
  1565. } while (dn_rt_hash_table == NULL && --order > 0);
  1566. if (!dn_rt_hash_table)
  1567. panic("Failed to allocate DECnet route cache hash table\n");
  1568. printk(KERN_INFO
  1569. "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
  1570. dn_rt_hash_mask,
  1571. (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
  1572. dn_rt_hash_mask--;
  1573. for(i = 0; i <= dn_rt_hash_mask; i++) {
  1574. spin_lock_init(&dn_rt_hash_table[i].lock);
  1575. dn_rt_hash_table[i].chain = NULL;
  1576. }
  1577. dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
  1578. proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
  1579. #ifdef CONFIG_DECNET_ROUTER
  1580. rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
  1581. dn_fib_dump, NULL);
  1582. #else
  1583. rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
  1584. dn_cache_dump, NULL);
  1585. #endif
  1586. }
  1587. void __exit dn_route_cleanup(void)
  1588. {
  1589. del_timer(&dn_route_timer);
  1590. dn_run_flush(0);
  1591. proc_net_remove(&init_net, "decnet_cache");
  1592. dst_entries_destroy(&dn_dst_ops);
  1593. }