dn_route.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  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_output(struct sk_buff *skb)
  588. {
  589. struct dst_entry *dst = skb_dst(skb);
  590. struct dn_route *rt = (struct dn_route *)dst;
  591. struct net_device *dev = dst->dev;
  592. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  593. struct neighbour *neigh;
  594. int err = -EINVAL;
  595. if ((neigh = dst->neighbour) == NULL)
  596. goto error;
  597. skb->dev = dev;
  598. cb->src = rt->rt_saddr;
  599. cb->dst = rt->rt_daddr;
  600. /*
  601. * Always set the Intra-Ethernet bit on all outgoing packets
  602. * originated on this node. Only valid flag from upper layers
  603. * is return-to-sender-requested. Set hop count to 0 too.
  604. */
  605. cb->rt_flags &= ~DN_RT_F_RQR;
  606. cb->rt_flags |= DN_RT_F_IE;
  607. cb->hops = 0;
  608. return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev,
  609. neigh->output);
  610. error:
  611. if (net_ratelimit())
  612. printk(KERN_DEBUG "dn_output: This should not happen\n");
  613. kfree_skb(skb);
  614. return err;
  615. }
  616. static int dn_forward(struct sk_buff *skb)
  617. {
  618. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  619. struct dst_entry *dst = skb_dst(skb);
  620. struct dn_dev *dn_db = rcu_dereference(dst->dev->dn_ptr);
  621. struct dn_route *rt;
  622. struct neighbour *neigh = dst->neighbour;
  623. int header_len;
  624. #ifdef CONFIG_NETFILTER
  625. struct net_device *dev = skb->dev;
  626. #endif
  627. if (skb->pkt_type != PACKET_HOST)
  628. goto drop;
  629. /* Ensure that we have enough space for headers */
  630. rt = (struct dn_route *)skb_dst(skb);
  631. header_len = dn_db->use_long ? 21 : 6;
  632. if (skb_cow(skb, LL_RESERVED_SPACE(rt->dst.dev)+header_len))
  633. goto drop;
  634. /*
  635. * Hop count exceeded.
  636. */
  637. if (++cb->hops > 30)
  638. goto drop;
  639. skb->dev = rt->dst.dev;
  640. /*
  641. * If packet goes out same interface it came in on, then set
  642. * the Intra-Ethernet bit. This has no effect for short
  643. * packets, so we don't need to test for them here.
  644. */
  645. cb->rt_flags &= ~DN_RT_F_IE;
  646. if (rt->rt_flags & RTCF_DOREDIRECT)
  647. cb->rt_flags |= DN_RT_F_IE;
  648. return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev,
  649. neigh->output);
  650. drop:
  651. kfree_skb(skb);
  652. return NET_RX_DROP;
  653. }
  654. /*
  655. * Used to catch bugs. This should never normally get
  656. * called.
  657. */
  658. static int dn_rt_bug(struct sk_buff *skb)
  659. {
  660. if (net_ratelimit()) {
  661. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  662. printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04x\n",
  663. le16_to_cpu(cb->src), le16_to_cpu(cb->dst));
  664. }
  665. kfree_skb(skb);
  666. return NET_RX_DROP;
  667. }
  668. static unsigned int dn_dst_default_advmss(const struct dst_entry *dst)
  669. {
  670. return dn_mss_from_pmtu(dst->dev, dst_mtu(dst));
  671. }
  672. static unsigned int dn_dst_default_mtu(const struct dst_entry *dst)
  673. {
  674. return dst->dev->mtu;
  675. }
  676. static int dn_rt_set_next_hop(struct dn_route *rt, struct dn_fib_res *res)
  677. {
  678. struct dn_fib_info *fi = res->fi;
  679. struct net_device *dev = rt->dst.dev;
  680. unsigned int mss_metric;
  681. struct neighbour *n;
  682. if (fi) {
  683. if (DN_FIB_RES_GW(*res) &&
  684. DN_FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
  685. rt->rt_gateway = DN_FIB_RES_GW(*res);
  686. dst_init_metrics(&rt->dst, fi->fib_metrics, true);
  687. }
  688. rt->rt_type = res->type;
  689. if (dev != NULL && rt->dst.neighbour == NULL) {
  690. n = __neigh_lookup_errno(&dn_neigh_table, &rt->rt_gateway, dev);
  691. if (IS_ERR(n))
  692. return PTR_ERR(n);
  693. rt->dst.neighbour = n;
  694. }
  695. if (dst_metric(&rt->dst, RTAX_MTU) > rt->dst.dev->mtu)
  696. dst_metric_set(&rt->dst, RTAX_MTU, rt->dst.dev->mtu);
  697. mss_metric = dst_metric_raw(&rt->dst, RTAX_ADVMSS);
  698. if (mss_metric) {
  699. unsigned int mss = dn_mss_from_pmtu(dev, dst_mtu(&rt->dst));
  700. if (mss_metric > mss)
  701. dst_metric_set(&rt->dst, RTAX_ADVMSS, mss);
  702. }
  703. return 0;
  704. }
  705. static inline int dn_match_addr(__le16 addr1, __le16 addr2)
  706. {
  707. __u16 tmp = le16_to_cpu(addr1) ^ le16_to_cpu(addr2);
  708. int match = 16;
  709. while(tmp) {
  710. tmp >>= 1;
  711. match--;
  712. }
  713. return match;
  714. }
  715. static __le16 dnet_select_source(const struct net_device *dev, __le16 daddr, int scope)
  716. {
  717. __le16 saddr = 0;
  718. struct dn_dev *dn_db;
  719. struct dn_ifaddr *ifa;
  720. int best_match = 0;
  721. int ret;
  722. rcu_read_lock();
  723. dn_db = rcu_dereference(dev->dn_ptr);
  724. for (ifa = rcu_dereference(dn_db->ifa_list);
  725. ifa != NULL;
  726. ifa = rcu_dereference(ifa->ifa_next)) {
  727. if (ifa->ifa_scope > scope)
  728. continue;
  729. if (!daddr) {
  730. saddr = ifa->ifa_local;
  731. break;
  732. }
  733. ret = dn_match_addr(daddr, ifa->ifa_local);
  734. if (ret > best_match)
  735. saddr = ifa->ifa_local;
  736. if (best_match == 0)
  737. saddr = ifa->ifa_local;
  738. }
  739. rcu_read_unlock();
  740. return saddr;
  741. }
  742. static inline __le16 __dn_fib_res_prefsrc(struct dn_fib_res *res)
  743. {
  744. return dnet_select_source(DN_FIB_RES_DEV(*res), DN_FIB_RES_GW(*res), res->scope);
  745. }
  746. static inline __le16 dn_fib_rules_map_destination(__le16 daddr, struct dn_fib_res *res)
  747. {
  748. __le16 mask = dnet_make_mask(res->prefixlen);
  749. return (daddr&~mask)|res->fi->fib_nh->nh_gw;
  750. }
  751. static int dn_route_output_slow(struct dst_entry **pprt, const struct flowidn *oldflp, int try_hard)
  752. {
  753. struct flowidn fld = {
  754. .daddr = oldflp->daddr,
  755. .saddr = oldflp->saddr,
  756. .flowidn_scope = RT_SCOPE_UNIVERSE,
  757. .flowidn_mark = oldflp->flowidn_mark,
  758. .flowidn_iif = init_net.loopback_dev->ifindex,
  759. .flowidn_oif = oldflp->flowidn_oif,
  760. };
  761. struct dn_route *rt = NULL;
  762. struct net_device *dev_out = NULL, *dev;
  763. struct neighbour *neigh = NULL;
  764. unsigned hash;
  765. unsigned flags = 0;
  766. struct dn_fib_res res = { .fi = NULL, .type = RTN_UNICAST };
  767. int err;
  768. int free_res = 0;
  769. __le16 gateway = 0;
  770. if (decnet_debug_level & 16)
  771. printk(KERN_DEBUG
  772. "dn_route_output_slow: dst=%04x src=%04x mark=%d"
  773. " iif=%d oif=%d\n", le16_to_cpu(oldflp->daddr),
  774. le16_to_cpu(oldflp->saddr),
  775. oldflp->flowidn_mark, init_net.loopback_dev->ifindex,
  776. oldflp->flowidn_oif);
  777. /* If we have an output interface, verify its a DECnet device */
  778. if (oldflp->flowidn_oif) {
  779. dev_out = dev_get_by_index(&init_net, oldflp->flowidn_oif);
  780. err = -ENODEV;
  781. if (dev_out && dev_out->dn_ptr == NULL) {
  782. dev_put(dev_out);
  783. dev_out = NULL;
  784. }
  785. if (dev_out == NULL)
  786. goto out;
  787. }
  788. /* If we have a source address, verify that its a local address */
  789. if (oldflp->saddr) {
  790. err = -EADDRNOTAVAIL;
  791. if (dev_out) {
  792. if (dn_dev_islocal(dev_out, oldflp->saddr))
  793. goto source_ok;
  794. dev_put(dev_out);
  795. goto out;
  796. }
  797. rcu_read_lock();
  798. for_each_netdev_rcu(&init_net, dev) {
  799. if (!dev->dn_ptr)
  800. continue;
  801. if (!dn_dev_islocal(dev, oldflp->saddr))
  802. continue;
  803. if ((dev->flags & IFF_LOOPBACK) &&
  804. oldflp->daddr &&
  805. !dn_dev_islocal(dev, oldflp->daddr))
  806. continue;
  807. dev_out = dev;
  808. break;
  809. }
  810. rcu_read_unlock();
  811. if (dev_out == NULL)
  812. goto out;
  813. dev_hold(dev_out);
  814. source_ok:
  815. ;
  816. }
  817. /* No destination? Assume its local */
  818. if (!fld.daddr) {
  819. fld.daddr = fld.saddr;
  820. err = -EADDRNOTAVAIL;
  821. if (dev_out)
  822. dev_put(dev_out);
  823. dev_out = init_net.loopback_dev;
  824. dev_hold(dev_out);
  825. if (!fld.daddr) {
  826. fld.daddr =
  827. fld.saddr = dnet_select_source(dev_out, 0,
  828. RT_SCOPE_HOST);
  829. if (!fld.daddr)
  830. goto out;
  831. }
  832. fld.flowidn_oif = init_net.loopback_dev->ifindex;
  833. res.type = RTN_LOCAL;
  834. goto make_route;
  835. }
  836. if (decnet_debug_level & 16)
  837. printk(KERN_DEBUG
  838. "dn_route_output_slow: initial checks complete."
  839. " dst=%o4x src=%04x oif=%d try_hard=%d\n",
  840. le16_to_cpu(fld.daddr), le16_to_cpu(fld.saddr),
  841. fld.flowidn_oif, try_hard);
  842. /*
  843. * N.B. If the kernel is compiled without router support then
  844. * dn_fib_lookup() will evaluate to non-zero so this if () block
  845. * will always be executed.
  846. */
  847. err = -ESRCH;
  848. if (try_hard || (err = dn_fib_lookup(&fld, &res)) != 0) {
  849. struct dn_dev *dn_db;
  850. if (err != -ESRCH)
  851. goto out;
  852. /*
  853. * Here the fallback is basically the standard algorithm for
  854. * routing in endnodes which is described in the DECnet routing
  855. * docs
  856. *
  857. * If we are not trying hard, look in neighbour cache.
  858. * The result is tested to ensure that if a specific output
  859. * device/source address was requested, then we honour that
  860. * here
  861. */
  862. if (!try_hard) {
  863. neigh = neigh_lookup_nodev(&dn_neigh_table, &init_net, &fld.daddr);
  864. if (neigh) {
  865. if ((oldflp->flowidn_oif &&
  866. (neigh->dev->ifindex != oldflp->flowidn_oif)) ||
  867. (oldflp->saddr &&
  868. (!dn_dev_islocal(neigh->dev,
  869. oldflp->saddr)))) {
  870. neigh_release(neigh);
  871. neigh = NULL;
  872. } else {
  873. if (dev_out)
  874. dev_put(dev_out);
  875. if (dn_dev_islocal(neigh->dev, fld.daddr)) {
  876. dev_out = init_net.loopback_dev;
  877. res.type = RTN_LOCAL;
  878. } else {
  879. dev_out = neigh->dev;
  880. }
  881. dev_hold(dev_out);
  882. goto select_source;
  883. }
  884. }
  885. }
  886. /* Not there? Perhaps its a local address */
  887. if (dev_out == NULL)
  888. dev_out = dn_dev_get_default();
  889. err = -ENODEV;
  890. if (dev_out == NULL)
  891. goto out;
  892. dn_db = rcu_dereference_raw(dev_out->dn_ptr);
  893. /* Possible improvement - check all devices for local addr */
  894. if (dn_dev_islocal(dev_out, fld.daddr)) {
  895. dev_put(dev_out);
  896. dev_out = init_net.loopback_dev;
  897. dev_hold(dev_out);
  898. res.type = RTN_LOCAL;
  899. goto select_source;
  900. }
  901. /* Not local either.... try sending it to the default router */
  902. neigh = neigh_clone(dn_db->router);
  903. BUG_ON(neigh && neigh->dev != dev_out);
  904. /* Ok then, we assume its directly connected and move on */
  905. select_source:
  906. if (neigh)
  907. gateway = ((struct dn_neigh *)neigh)->addr;
  908. if (gateway == 0)
  909. gateway = fld.daddr;
  910. if (fld.saddr == 0) {
  911. fld.saddr = dnet_select_source(dev_out, gateway,
  912. res.type == RTN_LOCAL ?
  913. RT_SCOPE_HOST :
  914. RT_SCOPE_LINK);
  915. if (fld.saddr == 0 && res.type != RTN_LOCAL)
  916. goto e_addr;
  917. }
  918. fld.flowidn_oif = dev_out->ifindex;
  919. goto make_route;
  920. }
  921. free_res = 1;
  922. if (res.type == RTN_NAT)
  923. goto e_inval;
  924. if (res.type == RTN_LOCAL) {
  925. if (!fld.saddr)
  926. fld.saddr = fld.daddr;
  927. if (dev_out)
  928. dev_put(dev_out);
  929. dev_out = init_net.loopback_dev;
  930. dev_hold(dev_out);
  931. fld.flowidn_oif = dev_out->ifindex;
  932. if (res.fi)
  933. dn_fib_info_put(res.fi);
  934. res.fi = NULL;
  935. goto make_route;
  936. }
  937. if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
  938. dn_fib_select_multipath(&fld, &res);
  939. /*
  940. * We could add some logic to deal with default routes here and
  941. * get rid of some of the special casing above.
  942. */
  943. if (!fld.saddr)
  944. fld.saddr = DN_FIB_RES_PREFSRC(res);
  945. if (dev_out)
  946. dev_put(dev_out);
  947. dev_out = DN_FIB_RES_DEV(res);
  948. dev_hold(dev_out);
  949. fld.flowidn_oif = dev_out->ifindex;
  950. gateway = DN_FIB_RES_GW(res);
  951. make_route:
  952. if (dev_out->flags & IFF_LOOPBACK)
  953. flags |= RTCF_LOCAL;
  954. rt = dst_alloc(&dn_dst_ops, 0);
  955. if (rt == NULL)
  956. goto e_nobufs;
  957. atomic_set(&rt->dst.__refcnt, 1);
  958. rt->dst.flags = DST_HOST;
  959. rt->fld.saddr = oldflp->saddr;
  960. rt->fld.daddr = oldflp->daddr;
  961. rt->fld.flowidn_oif = oldflp->flowidn_oif;
  962. rt->fld.flowidn_iif = 0;
  963. rt->fld.flowidn_mark = oldflp->flowidn_mark;
  964. rt->rt_saddr = fld.saddr;
  965. rt->rt_daddr = fld.daddr;
  966. rt->rt_gateway = gateway ? gateway : fld.daddr;
  967. rt->rt_local_src = fld.saddr;
  968. rt->rt_dst_map = fld.daddr;
  969. rt->rt_src_map = fld.saddr;
  970. rt->dst.dev = dev_out;
  971. dev_hold(dev_out);
  972. rt->dst.neighbour = neigh;
  973. neigh = NULL;
  974. rt->dst.lastuse = jiffies;
  975. rt->dst.output = dn_output;
  976. rt->dst.input = dn_rt_bug;
  977. rt->rt_flags = flags;
  978. if (flags & RTCF_LOCAL)
  979. rt->dst.input = dn_nsp_rx;
  980. err = dn_rt_set_next_hop(rt, &res);
  981. if (err)
  982. goto e_neighbour;
  983. hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
  984. dn_insert_route(rt, hash, (struct dn_route **)pprt);
  985. done:
  986. if (neigh)
  987. neigh_release(neigh);
  988. if (free_res)
  989. dn_fib_res_put(&res);
  990. if (dev_out)
  991. dev_put(dev_out);
  992. out:
  993. return err;
  994. e_addr:
  995. err = -EADDRNOTAVAIL;
  996. goto done;
  997. e_inval:
  998. err = -EINVAL;
  999. goto done;
  1000. e_nobufs:
  1001. err = -ENOBUFS;
  1002. goto done;
  1003. e_neighbour:
  1004. dst_free(&rt->dst);
  1005. goto e_nobufs;
  1006. }
  1007. /*
  1008. * N.B. The flags may be moved into the flowi at some future stage.
  1009. */
  1010. static int __dn_route_output_key(struct dst_entry **pprt, const struct flowidn *flp, int flags)
  1011. {
  1012. unsigned hash = dn_hash(flp->saddr, flp->daddr);
  1013. struct dn_route *rt = NULL;
  1014. if (!(flags & MSG_TRYHARD)) {
  1015. rcu_read_lock_bh();
  1016. for (rt = rcu_dereference_bh(dn_rt_hash_table[hash].chain); rt;
  1017. rt = rcu_dereference_bh(rt->dst.dn_next)) {
  1018. if ((flp->daddr == rt->fld.daddr) &&
  1019. (flp->saddr == rt->fld.saddr) &&
  1020. (flp->flowidn_mark == rt->fld.flowidn_mark) &&
  1021. dn_is_output_route(rt) &&
  1022. (rt->fld.flowidn_oif == flp->flowidn_oif)) {
  1023. dst_use(&rt->dst, jiffies);
  1024. rcu_read_unlock_bh();
  1025. *pprt = &rt->dst;
  1026. return 0;
  1027. }
  1028. }
  1029. rcu_read_unlock_bh();
  1030. }
  1031. return dn_route_output_slow(pprt, flp, flags);
  1032. }
  1033. static int dn_route_output_key(struct dst_entry **pprt, struct flowidn *flp, int flags)
  1034. {
  1035. int err;
  1036. err = __dn_route_output_key(pprt, flp, flags);
  1037. if (err == 0 && flp->flowidn_proto) {
  1038. *pprt = xfrm_lookup(&init_net, *pprt,
  1039. flowidn_to_flowi(flp), NULL, 0);
  1040. if (IS_ERR(*pprt)) {
  1041. err = PTR_ERR(*pprt);
  1042. *pprt = NULL;
  1043. }
  1044. }
  1045. return err;
  1046. }
  1047. int dn_route_output_sock(struct dst_entry **pprt, struct flowidn *fl, struct sock *sk, int flags)
  1048. {
  1049. int err;
  1050. err = __dn_route_output_key(pprt, fl, flags & MSG_TRYHARD);
  1051. if (err == 0 && fl->flowidn_proto) {
  1052. if (!(flags & MSG_DONTWAIT))
  1053. fl->flowidn_flags |= FLOWI_FLAG_CAN_SLEEP;
  1054. *pprt = xfrm_lookup(&init_net, *pprt,
  1055. flowidn_to_flowi(fl), sk, 0);
  1056. if (IS_ERR(*pprt)) {
  1057. err = PTR_ERR(*pprt);
  1058. *pprt = NULL;
  1059. }
  1060. }
  1061. return err;
  1062. }
  1063. static int dn_route_input_slow(struct sk_buff *skb)
  1064. {
  1065. struct dn_route *rt = NULL;
  1066. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  1067. struct net_device *in_dev = skb->dev;
  1068. struct net_device *out_dev = NULL;
  1069. struct dn_dev *dn_db;
  1070. struct neighbour *neigh = NULL;
  1071. unsigned hash;
  1072. int flags = 0;
  1073. __le16 gateway = 0;
  1074. __le16 local_src = 0;
  1075. struct flowidn fld = {
  1076. .daddr = cb->dst,
  1077. .saddr = cb->src,
  1078. .flowidn_scope = RT_SCOPE_UNIVERSE,
  1079. .flowidn_mark = skb->mark,
  1080. .flowidn_iif = skb->dev->ifindex,
  1081. };
  1082. struct dn_fib_res res = { .fi = NULL, .type = RTN_UNREACHABLE };
  1083. int err = -EINVAL;
  1084. int free_res = 0;
  1085. dev_hold(in_dev);
  1086. if ((dn_db = rcu_dereference(in_dev->dn_ptr)) == NULL)
  1087. goto out;
  1088. /* Zero source addresses are not allowed */
  1089. if (fld.saddr == 0)
  1090. goto out;
  1091. /*
  1092. * In this case we've just received a packet from a source
  1093. * outside ourselves pretending to come from us. We don't
  1094. * allow it any further to prevent routing loops, spoofing and
  1095. * other nasties. Loopback packets already have the dst attached
  1096. * so this only affects packets which have originated elsewhere.
  1097. */
  1098. err = -ENOTUNIQ;
  1099. if (dn_dev_islocal(in_dev, cb->src))
  1100. goto out;
  1101. err = dn_fib_lookup(&fld, &res);
  1102. if (err) {
  1103. if (err != -ESRCH)
  1104. goto out;
  1105. /*
  1106. * Is the destination us ?
  1107. */
  1108. if (!dn_dev_islocal(in_dev, cb->dst))
  1109. goto e_inval;
  1110. res.type = RTN_LOCAL;
  1111. } else {
  1112. __le16 src_map = fld.saddr;
  1113. free_res = 1;
  1114. out_dev = DN_FIB_RES_DEV(res);
  1115. if (out_dev == NULL) {
  1116. if (net_ratelimit())
  1117. printk(KERN_CRIT "Bug in dn_route_input_slow() "
  1118. "No output device\n");
  1119. goto e_inval;
  1120. }
  1121. dev_hold(out_dev);
  1122. if (res.r)
  1123. src_map = fld.saddr; /* no NAT support for now */
  1124. gateway = DN_FIB_RES_GW(res);
  1125. if (res.type == RTN_NAT) {
  1126. fld.daddr = dn_fib_rules_map_destination(fld.daddr, &res);
  1127. dn_fib_res_put(&res);
  1128. free_res = 0;
  1129. if (dn_fib_lookup(&fld, &res))
  1130. goto e_inval;
  1131. free_res = 1;
  1132. if (res.type != RTN_UNICAST)
  1133. goto e_inval;
  1134. flags |= RTCF_DNAT;
  1135. gateway = fld.daddr;
  1136. }
  1137. fld.saddr = src_map;
  1138. }
  1139. switch(res.type) {
  1140. case RTN_UNICAST:
  1141. /*
  1142. * Forwarding check here, we only check for forwarding
  1143. * being turned off, if you want to only forward intra
  1144. * area, its up to you to set the routing tables up
  1145. * correctly.
  1146. */
  1147. if (dn_db->parms.forwarding == 0)
  1148. goto e_inval;
  1149. if (res.fi->fib_nhs > 1 && fld.flowidn_oif == 0)
  1150. dn_fib_select_multipath(&fld, &res);
  1151. /*
  1152. * Check for out_dev == in_dev. We use the RTCF_DOREDIRECT
  1153. * flag as a hint to set the intra-ethernet bit when
  1154. * forwarding. If we've got NAT in operation, we don't do
  1155. * this optimisation.
  1156. */
  1157. if (out_dev == in_dev && !(flags & RTCF_NAT))
  1158. flags |= RTCF_DOREDIRECT;
  1159. local_src = DN_FIB_RES_PREFSRC(res);
  1160. case RTN_BLACKHOLE:
  1161. case RTN_UNREACHABLE:
  1162. break;
  1163. case RTN_LOCAL:
  1164. flags |= RTCF_LOCAL;
  1165. fld.saddr = cb->dst;
  1166. fld.daddr = cb->src;
  1167. /* Routing tables gave us a gateway */
  1168. if (gateway)
  1169. goto make_route;
  1170. /* Packet was intra-ethernet, so we know its on-link */
  1171. if (cb->rt_flags & DN_RT_F_IE) {
  1172. gateway = cb->src;
  1173. flags |= RTCF_DIRECTSRC;
  1174. goto make_route;
  1175. }
  1176. /* Use the default router if there is one */
  1177. neigh = neigh_clone(dn_db->router);
  1178. if (neigh) {
  1179. gateway = ((struct dn_neigh *)neigh)->addr;
  1180. goto make_route;
  1181. }
  1182. /* Close eyes and pray */
  1183. gateway = cb->src;
  1184. flags |= RTCF_DIRECTSRC;
  1185. goto make_route;
  1186. default:
  1187. goto e_inval;
  1188. }
  1189. make_route:
  1190. rt = dst_alloc(&dn_dst_ops, 0);
  1191. if (rt == NULL)
  1192. goto e_nobufs;
  1193. rt->rt_saddr = fld.saddr;
  1194. rt->rt_daddr = fld.daddr;
  1195. rt->rt_gateway = fld.daddr;
  1196. if (gateway)
  1197. rt->rt_gateway = gateway;
  1198. rt->rt_local_src = local_src ? local_src : rt->rt_saddr;
  1199. rt->rt_dst_map = fld.daddr;
  1200. rt->rt_src_map = fld.saddr;
  1201. rt->fld.saddr = cb->src;
  1202. rt->fld.daddr = cb->dst;
  1203. rt->fld.flowidn_oif = 0;
  1204. rt->fld.flowidn_iif = in_dev->ifindex;
  1205. rt->fld.flowidn_mark = fld.flowidn_mark;
  1206. rt->dst.flags = DST_HOST;
  1207. rt->dst.neighbour = neigh;
  1208. rt->dst.dev = out_dev;
  1209. rt->dst.lastuse = jiffies;
  1210. rt->dst.output = dn_rt_bug;
  1211. switch(res.type) {
  1212. case RTN_UNICAST:
  1213. rt->dst.input = dn_forward;
  1214. break;
  1215. case RTN_LOCAL:
  1216. rt->dst.output = dn_output;
  1217. rt->dst.input = dn_nsp_rx;
  1218. rt->dst.dev = in_dev;
  1219. flags |= RTCF_LOCAL;
  1220. break;
  1221. default:
  1222. case RTN_UNREACHABLE:
  1223. case RTN_BLACKHOLE:
  1224. rt->dst.input = dst_discard;
  1225. }
  1226. rt->rt_flags = flags;
  1227. if (rt->dst.dev)
  1228. dev_hold(rt->dst.dev);
  1229. err = dn_rt_set_next_hop(rt, &res);
  1230. if (err)
  1231. goto e_neighbour;
  1232. hash = dn_hash(rt->fld.saddr, rt->fld.daddr);
  1233. dn_insert_route(rt, hash, &rt);
  1234. skb_dst_set(skb, &rt->dst);
  1235. done:
  1236. if (neigh)
  1237. neigh_release(neigh);
  1238. if (free_res)
  1239. dn_fib_res_put(&res);
  1240. dev_put(in_dev);
  1241. if (out_dev)
  1242. dev_put(out_dev);
  1243. out:
  1244. return err;
  1245. e_inval:
  1246. err = -EINVAL;
  1247. goto done;
  1248. e_nobufs:
  1249. err = -ENOBUFS;
  1250. goto done;
  1251. e_neighbour:
  1252. dst_free(&rt->dst);
  1253. goto done;
  1254. }
  1255. static int dn_route_input(struct sk_buff *skb)
  1256. {
  1257. struct dn_route *rt;
  1258. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  1259. unsigned hash = dn_hash(cb->src, cb->dst);
  1260. if (skb_dst(skb))
  1261. return 0;
  1262. rcu_read_lock();
  1263. for(rt = rcu_dereference(dn_rt_hash_table[hash].chain); rt != NULL;
  1264. rt = rcu_dereference(rt->dst.dn_next)) {
  1265. if ((rt->fld.saddr == cb->src) &&
  1266. (rt->fld.daddr == cb->dst) &&
  1267. (rt->fld.flowidn_oif == 0) &&
  1268. (rt->fld.flowidn_mark == skb->mark) &&
  1269. (rt->fld.flowidn_iif == cb->iif)) {
  1270. dst_use(&rt->dst, jiffies);
  1271. rcu_read_unlock();
  1272. skb_dst_set(skb, (struct dst_entry *)rt);
  1273. return 0;
  1274. }
  1275. }
  1276. rcu_read_unlock();
  1277. return dn_route_input_slow(skb);
  1278. }
  1279. static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
  1280. int event, int nowait, unsigned int flags)
  1281. {
  1282. struct dn_route *rt = (struct dn_route *)skb_dst(skb);
  1283. struct rtmsg *r;
  1284. struct nlmsghdr *nlh;
  1285. unsigned char *b = skb_tail_pointer(skb);
  1286. long expires;
  1287. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
  1288. r = NLMSG_DATA(nlh);
  1289. r->rtm_family = AF_DECnet;
  1290. r->rtm_dst_len = 16;
  1291. r->rtm_src_len = 0;
  1292. r->rtm_tos = 0;
  1293. r->rtm_table = RT_TABLE_MAIN;
  1294. RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
  1295. r->rtm_type = rt->rt_type;
  1296. r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
  1297. r->rtm_scope = RT_SCOPE_UNIVERSE;
  1298. r->rtm_protocol = RTPROT_UNSPEC;
  1299. if (rt->rt_flags & RTCF_NOTIFY)
  1300. r->rtm_flags |= RTM_F_NOTIFY;
  1301. RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
  1302. if (rt->fld.saddr) {
  1303. r->rtm_src_len = 16;
  1304. RTA_PUT(skb, RTA_SRC, 2, &rt->fld.saddr);
  1305. }
  1306. if (rt->dst.dev)
  1307. RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
  1308. /*
  1309. * Note to self - change this if input routes reverse direction when
  1310. * they deal only with inputs and not with replies like they do
  1311. * currently.
  1312. */
  1313. RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
  1314. if (rt->rt_daddr != rt->rt_gateway)
  1315. RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
  1316. if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
  1317. goto rtattr_failure;
  1318. expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
  1319. if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
  1320. rt->dst.error) < 0)
  1321. goto rtattr_failure;
  1322. if (dn_is_input_route(rt))
  1323. RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fld.flowidn_iif);
  1324. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1325. return skb->len;
  1326. nlmsg_failure:
  1327. rtattr_failure:
  1328. nlmsg_trim(skb, b);
  1329. return -1;
  1330. }
  1331. /*
  1332. * This is called by both endnodes and routers now.
  1333. */
  1334. static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
  1335. {
  1336. struct net *net = sock_net(in_skb->sk);
  1337. struct rtattr **rta = arg;
  1338. struct rtmsg *rtm = NLMSG_DATA(nlh);
  1339. struct dn_route *rt = NULL;
  1340. struct dn_skb_cb *cb;
  1341. int err;
  1342. struct sk_buff *skb;
  1343. struct flowidn fld;
  1344. if (!net_eq(net, &init_net))
  1345. return -EINVAL;
  1346. memset(&fld, 0, sizeof(fld));
  1347. fld.flowidn_proto = DNPROTO_NSP;
  1348. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1349. if (skb == NULL)
  1350. return -ENOBUFS;
  1351. skb_reset_mac_header(skb);
  1352. cb = DN_SKB_CB(skb);
  1353. if (rta[RTA_SRC-1])
  1354. memcpy(&fld.saddr, RTA_DATA(rta[RTA_SRC-1]), 2);
  1355. if (rta[RTA_DST-1])
  1356. memcpy(&fld.daddr, RTA_DATA(rta[RTA_DST-1]), 2);
  1357. if (rta[RTA_IIF-1])
  1358. memcpy(&fld.flowidn_iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
  1359. if (fld.flowidn_iif) {
  1360. struct net_device *dev;
  1361. if ((dev = dev_get_by_index(&init_net, fld.flowidn_iif)) == NULL) {
  1362. kfree_skb(skb);
  1363. return -ENODEV;
  1364. }
  1365. if (!dev->dn_ptr) {
  1366. dev_put(dev);
  1367. kfree_skb(skb);
  1368. return -ENODEV;
  1369. }
  1370. skb->protocol = htons(ETH_P_DNA_RT);
  1371. skb->dev = dev;
  1372. cb->src = fld.saddr;
  1373. cb->dst = fld.daddr;
  1374. local_bh_disable();
  1375. err = dn_route_input(skb);
  1376. local_bh_enable();
  1377. memset(cb, 0, sizeof(struct dn_skb_cb));
  1378. rt = (struct dn_route *)skb_dst(skb);
  1379. if (!err && -rt->dst.error)
  1380. err = rt->dst.error;
  1381. } else {
  1382. int oif = 0;
  1383. if (rta[RTA_OIF - 1])
  1384. memcpy(&oif, RTA_DATA(rta[RTA_OIF - 1]), sizeof(int));
  1385. fld.flowidn_oif = oif;
  1386. err = dn_route_output_key((struct dst_entry **)&rt, &fld, 0);
  1387. }
  1388. if (skb->dev)
  1389. dev_put(skb->dev);
  1390. skb->dev = NULL;
  1391. if (err)
  1392. goto out_free;
  1393. skb_dst_set(skb, &rt->dst);
  1394. if (rtm->rtm_flags & RTM_F_NOTIFY)
  1395. rt->rt_flags |= RTCF_NOTIFY;
  1396. err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, RTM_NEWROUTE, 0, 0);
  1397. if (err == 0)
  1398. goto out_free;
  1399. if (err < 0) {
  1400. err = -EMSGSIZE;
  1401. goto out_free;
  1402. }
  1403. return rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
  1404. out_free:
  1405. kfree_skb(skb);
  1406. return err;
  1407. }
  1408. /*
  1409. * For routers, this is called from dn_fib_dump, but for endnodes its
  1410. * called directly from the rtnetlink dispatch table.
  1411. */
  1412. int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1413. {
  1414. struct net *net = sock_net(skb->sk);
  1415. struct dn_route *rt;
  1416. int h, s_h;
  1417. int idx, s_idx;
  1418. if (!net_eq(net, &init_net))
  1419. return 0;
  1420. if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
  1421. return -EINVAL;
  1422. if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
  1423. return 0;
  1424. s_h = cb->args[0];
  1425. s_idx = idx = cb->args[1];
  1426. for(h = 0; h <= dn_rt_hash_mask; h++) {
  1427. if (h < s_h)
  1428. continue;
  1429. if (h > s_h)
  1430. s_idx = 0;
  1431. rcu_read_lock_bh();
  1432. for(rt = rcu_dereference_bh(dn_rt_hash_table[h].chain), idx = 0;
  1433. rt;
  1434. rt = rcu_dereference_bh(rt->dst.dn_next), idx++) {
  1435. if (idx < s_idx)
  1436. continue;
  1437. skb_dst_set(skb, dst_clone(&rt->dst));
  1438. if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
  1439. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1440. 1, NLM_F_MULTI) <= 0) {
  1441. skb_dst_drop(skb);
  1442. rcu_read_unlock_bh();
  1443. goto done;
  1444. }
  1445. skb_dst_drop(skb);
  1446. }
  1447. rcu_read_unlock_bh();
  1448. }
  1449. done:
  1450. cb->args[0] = h;
  1451. cb->args[1] = idx;
  1452. return skb->len;
  1453. }
  1454. #ifdef CONFIG_PROC_FS
  1455. struct dn_rt_cache_iter_state {
  1456. int bucket;
  1457. };
  1458. static struct dn_route *dn_rt_cache_get_first(struct seq_file *seq)
  1459. {
  1460. struct dn_route *rt = NULL;
  1461. struct dn_rt_cache_iter_state *s = seq->private;
  1462. for(s->bucket = dn_rt_hash_mask; s->bucket >= 0; --s->bucket) {
  1463. rcu_read_lock_bh();
  1464. rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
  1465. if (rt)
  1466. break;
  1467. rcu_read_unlock_bh();
  1468. }
  1469. return rt;
  1470. }
  1471. static struct dn_route *dn_rt_cache_get_next(struct seq_file *seq, struct dn_route *rt)
  1472. {
  1473. struct dn_rt_cache_iter_state *s = seq->private;
  1474. rt = rcu_dereference_bh(rt->dst.dn_next);
  1475. while (!rt) {
  1476. rcu_read_unlock_bh();
  1477. if (--s->bucket < 0)
  1478. break;
  1479. rcu_read_lock_bh();
  1480. rt = rcu_dereference_bh(dn_rt_hash_table[s->bucket].chain);
  1481. }
  1482. return rt;
  1483. }
  1484. static void *dn_rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
  1485. {
  1486. struct dn_route *rt = dn_rt_cache_get_first(seq);
  1487. if (rt) {
  1488. while(*pos && (rt = dn_rt_cache_get_next(seq, rt)))
  1489. --*pos;
  1490. }
  1491. return *pos ? NULL : rt;
  1492. }
  1493. static void *dn_rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1494. {
  1495. struct dn_route *rt = dn_rt_cache_get_next(seq, v);
  1496. ++*pos;
  1497. return rt;
  1498. }
  1499. static void dn_rt_cache_seq_stop(struct seq_file *seq, void *v)
  1500. {
  1501. if (v)
  1502. rcu_read_unlock_bh();
  1503. }
  1504. static int dn_rt_cache_seq_show(struct seq_file *seq, void *v)
  1505. {
  1506. struct dn_route *rt = v;
  1507. char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
  1508. seq_printf(seq, "%-8s %-7s %-7s %04d %04d %04d\n",
  1509. rt->dst.dev ? rt->dst.dev->name : "*",
  1510. dn_addr2asc(le16_to_cpu(rt->rt_daddr), buf1),
  1511. dn_addr2asc(le16_to_cpu(rt->rt_saddr), buf2),
  1512. atomic_read(&rt->dst.__refcnt),
  1513. rt->dst.__use,
  1514. (int) dst_metric(&rt->dst, RTAX_RTT));
  1515. return 0;
  1516. }
  1517. static const struct seq_operations dn_rt_cache_seq_ops = {
  1518. .start = dn_rt_cache_seq_start,
  1519. .next = dn_rt_cache_seq_next,
  1520. .stop = dn_rt_cache_seq_stop,
  1521. .show = dn_rt_cache_seq_show,
  1522. };
  1523. static int dn_rt_cache_seq_open(struct inode *inode, struct file *file)
  1524. {
  1525. return seq_open_private(file, &dn_rt_cache_seq_ops,
  1526. sizeof(struct dn_rt_cache_iter_state));
  1527. }
  1528. static const struct file_operations dn_rt_cache_seq_fops = {
  1529. .owner = THIS_MODULE,
  1530. .open = dn_rt_cache_seq_open,
  1531. .read = seq_read,
  1532. .llseek = seq_lseek,
  1533. .release = seq_release_private,
  1534. };
  1535. #endif /* CONFIG_PROC_FS */
  1536. void __init dn_route_init(void)
  1537. {
  1538. int i, goal, order;
  1539. dn_dst_ops.kmem_cachep =
  1540. kmem_cache_create("dn_dst_cache", sizeof(struct dn_route), 0,
  1541. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1542. dst_entries_init(&dn_dst_ops);
  1543. setup_timer(&dn_route_timer, dn_dst_check_expire, 0);
  1544. dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
  1545. add_timer(&dn_route_timer);
  1546. goal = totalram_pages >> (26 - PAGE_SHIFT);
  1547. for(order = 0; (1UL << order) < goal; order++)
  1548. /* NOTHING */;
  1549. /*
  1550. * Only want 1024 entries max, since the table is very, very unlikely
  1551. * to be larger than that.
  1552. */
  1553. while(order && ((((1UL << order) * PAGE_SIZE) /
  1554. sizeof(struct dn_rt_hash_bucket)) >= 2048))
  1555. order--;
  1556. do {
  1557. dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
  1558. sizeof(struct dn_rt_hash_bucket);
  1559. while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
  1560. dn_rt_hash_mask--;
  1561. dn_rt_hash_table = (struct dn_rt_hash_bucket *)
  1562. __get_free_pages(GFP_ATOMIC, order);
  1563. } while (dn_rt_hash_table == NULL && --order > 0);
  1564. if (!dn_rt_hash_table)
  1565. panic("Failed to allocate DECnet route cache hash table\n");
  1566. printk(KERN_INFO
  1567. "DECnet: Routing cache hash table of %u buckets, %ldKbytes\n",
  1568. dn_rt_hash_mask,
  1569. (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
  1570. dn_rt_hash_mask--;
  1571. for(i = 0; i <= dn_rt_hash_mask; i++) {
  1572. spin_lock_init(&dn_rt_hash_table[i].lock);
  1573. dn_rt_hash_table[i].chain = NULL;
  1574. }
  1575. dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
  1576. proc_net_fops_create(&init_net, "decnet_cache", S_IRUGO, &dn_rt_cache_seq_fops);
  1577. #ifdef CONFIG_DECNET_ROUTER
  1578. rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute, dn_fib_dump);
  1579. #else
  1580. rtnl_register(PF_DECnet, RTM_GETROUTE, dn_cache_getroute,
  1581. dn_cache_dump);
  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. }