dn_route.c 45 KB

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