dn_route.c 46 KB

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