dn_route.c 47 KB

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