dn_route.c 47 KB

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