dn_route.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:31k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  *                                 
  40.  */
  41. /******************************************************************************
  42.     (c) 1995-1998 E.M. Serrat emserrat@geocities.com
  43.     
  44.     This program is free software; you can redistribute it and/or modify
  45.     it under the terms of the GNU General Public License as published by
  46.     the Free Software Foundation; either version 2 of the License, or
  47.     any later version.
  48.     This program is distributed in the hope that it will be useful,
  49.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  50.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  51.     GNU General Public License for more details.
  52. *******************************************************************************/
  53. #include <linux/config.h>
  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 <net/sock.h>
  65. #include <linux/fcntl.h>
  66. #include <linux/mm.h>
  67. #include <linux/proc_fs.h>
  68. #include <linux/init.h>
  69. #include <linux/rtnetlink.h>
  70. #include <linux/string.h>
  71. #include <linux/netfilter_decnet.h>
  72. #include <asm/errno.h>
  73. #include <net/neighbour.h>
  74. #include <net/dst.h>
  75. #include <net/dn.h>
  76. #include <net/dn_dev.h>
  77. #include <net/dn_nsp.h>
  78. #include <net/dn_route.h>
  79. #include <net/dn_neigh.h>
  80. #include <net/dn_fib.h>
  81. struct dn_rt_hash_bucket
  82. {
  83. struct dn_route *chain;
  84. rwlock_t lock;
  85. } __attribute__((__aligned__(8)));
  86. extern struct neigh_table dn_neigh_table;
  87. static unsigned char dn_hiord_addr[6] = {0xAA,0x00,0x04,0x00,0x00,0x00};
  88. int dn_rt_min_delay = 2*HZ;
  89. int dn_rt_max_delay = 10*HZ;
  90. static unsigned long dn_rt_deadline = 0;
  91. static int dn_dst_gc(void);
  92. static struct dst_entry *dn_dst_check(struct dst_entry *, __u32);
  93. static struct dst_entry *dn_dst_reroute(struct dst_entry *, struct sk_buff *skb);
  94. static struct dst_entry *dn_dst_negative_advice(struct dst_entry *);
  95. static void dn_dst_link_failure(struct sk_buff *);
  96. static int dn_route_input(struct sk_buff *);
  97. static void dn_run_flush(unsigned long dummy);
  98. static struct dn_rt_hash_bucket *dn_rt_hash_table;
  99. static unsigned dn_rt_hash_mask;
  100. static struct timer_list dn_route_timer;
  101. static struct timer_list dn_rt_flush_timer = { function: dn_run_flush };
  102. int decnet_dst_gc_interval = 2;
  103. static struct dst_ops dn_dst_ops = {
  104. family: PF_DECnet,
  105. protocol: __constant_htons(ETH_P_DNA_RT),
  106. gc_thresh: 128,
  107. gc: dn_dst_gc,
  108. check: dn_dst_check,
  109. reroute: dn_dst_reroute,
  110. negative_advice: dn_dst_negative_advice,
  111. link_failure: dn_dst_link_failure,
  112. entry_size: sizeof(struct dn_route),
  113. entries: ATOMIC_INIT(0),
  114. };
  115. static __inline__ unsigned dn_hash(unsigned short src, unsigned short dst)
  116. {
  117. unsigned short tmp = src ^ dst;
  118. tmp ^= (tmp >> 3);
  119. tmp ^= (tmp >> 5);
  120. tmp ^= (tmp >> 10);
  121. return dn_rt_hash_mask & (unsigned)tmp;
  122. }
  123. static void SMP_TIMER_NAME(dn_dst_check_expire)(unsigned long dummy)
  124. {
  125. int i;
  126. struct dn_route *rt, **rtp;
  127. unsigned long now = jiffies;
  128. unsigned long expire = 120 * HZ;
  129. for(i = 0; i <= dn_rt_hash_mask; i++) {
  130. rtp = &dn_rt_hash_table[i].chain;
  131. write_lock(&dn_rt_hash_table[i].lock);
  132. while((rt=*rtp) != NULL) {
  133. if (atomic_read(&rt->u.dst.__refcnt) ||
  134. (now - rt->u.dst.lastuse) < expire) {
  135. rtp = &rt->u.rt_next;
  136. continue;
  137. }
  138. *rtp = rt->u.rt_next;
  139. rt->u.rt_next = NULL;
  140. dst_free(&rt->u.dst);
  141. }
  142. write_unlock(&dn_rt_hash_table[i].lock);
  143. if ((jiffies - now) > 0)
  144. break;
  145. }
  146. mod_timer(&dn_route_timer, now + decnet_dst_gc_interval * HZ);
  147. }
  148. SMP_TIMER_DEFINE(dn_dst_check_expire, dn_dst_task);
  149. static int dn_dst_gc(void)
  150. {
  151. struct dn_route *rt, **rtp;
  152. int i;
  153. unsigned long now = jiffies;
  154. unsigned long expire = 10 * HZ;
  155. for(i = 0; i <= dn_rt_hash_mask; i++) {
  156. write_lock_bh(&dn_rt_hash_table[i].lock);
  157. rtp = &dn_rt_hash_table[i].chain;
  158. while((rt=*rtp) != NULL) {
  159. if (atomic_read(&rt->u.dst.__refcnt) ||
  160. (now - rt->u.dst.lastuse) < expire) {
  161. rtp = &rt->u.rt_next;
  162. continue;
  163. }
  164. *rtp = rt->u.rt_next;
  165. rt->u.rt_next = NULL;
  166. dst_free(&rt->u.dst);
  167. break;
  168. }
  169. write_unlock_bh(&dn_rt_hash_table[i].lock);
  170. }
  171. return 0;
  172. }
  173. static struct dst_entry *dn_dst_check(struct dst_entry *dst, __u32 cookie)
  174. {
  175. dst_release(dst);
  176. return NULL;
  177. }
  178. static struct dst_entry *dn_dst_reroute(struct dst_entry *dst,
  179. struct sk_buff *skb)
  180. {
  181. return NULL;
  182. }
  183. /*
  184.  * This is called through sendmsg() when you specify MSG_TRYHARD
  185.  * and there is already a route in cache.
  186.  */
  187. static struct dst_entry *dn_dst_negative_advice(struct dst_entry *dst)
  188. {
  189. dst_release(dst);
  190. return NULL;
  191. }
  192. static void dn_dst_link_failure(struct sk_buff *skb)
  193. {
  194. return;
  195. }
  196. static void dn_insert_route(struct dn_route *rt, unsigned hash)
  197. {
  198. unsigned long now = jiffies;
  199. write_lock_bh(&dn_rt_hash_table[hash].lock);
  200. rt->u.rt_next = dn_rt_hash_table[hash].chain;
  201. dn_rt_hash_table[hash].chain = rt;
  202. dst_hold(&rt->u.dst);
  203. rt->u.dst.__use++;
  204. rt->u.dst.lastuse = now;
  205. write_unlock_bh(&dn_rt_hash_table[hash].lock);
  206. }
  207. void SMP_TIMER_NAME(dn_run_flush)(unsigned long dummy)
  208. {
  209. int i;
  210. struct dn_route *rt, *next;
  211. for(i = 0; i < dn_rt_hash_mask; i++) {
  212. write_lock_bh(&dn_rt_hash_table[i].lock);
  213. if ((rt = xchg(&dn_rt_hash_table[i].chain, NULL)) == NULL)
  214. goto nothing_to_declare;
  215. for(; rt; rt=next) {
  216. next = rt->u.rt_next;
  217. rt->u.rt_next = NULL;
  218. dst_free((struct dst_entry *)rt);
  219. }
  220. nothing_to_declare:
  221. write_unlock_bh(&dn_rt_hash_table[i].lock);
  222. }
  223. }
  224. SMP_TIMER_DEFINE(dn_run_flush, dn_flush_task);
  225. static spinlock_t dn_rt_flush_lock = SPIN_LOCK_UNLOCKED;
  226. void dn_rt_cache_flush(int delay)
  227. {
  228. unsigned long now = jiffies;
  229. int user_mode = !in_interrupt();
  230. if (delay < 0)
  231. delay = dn_rt_min_delay;
  232. spin_lock_bh(&dn_rt_flush_lock);
  233. if (del_timer(&dn_rt_flush_timer) && delay > 0 && dn_rt_deadline) {
  234. long tmo = (long)(dn_rt_deadline - now);
  235. if (user_mode && tmo < dn_rt_max_delay - dn_rt_min_delay)
  236. tmo = 0;
  237. if (delay > tmo)
  238. delay = tmo;
  239. }
  240. if (delay <= 0) {
  241. spin_unlock_bh(&dn_rt_flush_lock);
  242. dn_run_flush(0);
  243. return;
  244. }
  245. if (dn_rt_deadline == 0)
  246. dn_rt_deadline = now + dn_rt_max_delay;
  247. dn_rt_flush_timer.expires = now + delay;
  248. add_timer(&dn_rt_flush_timer);
  249. spin_unlock_bh(&dn_rt_flush_lock);
  250. }
  251. /**
  252.  * dn_return_short - Return a short packet to its sender
  253.  * @skb: The packet to return
  254.  *
  255.  */
  256. static int dn_return_short(struct sk_buff *skb)
  257. {
  258. struct dn_skb_cb *cb;
  259. unsigned char *ptr;
  260. dn_address *src;
  261. dn_address *dst;
  262. dn_address tmp;
  263. /* Add back headers */
  264. skb_push(skb, skb->data - skb->nh.raw);
  265. if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
  266. return NET_RX_DROP;
  267. cb = DN_SKB_CB(skb);
  268. /* Skip packet length and point to flags */
  269. ptr = skb->data + 2;
  270. *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
  271. dst = (dn_address *)ptr;
  272. ptr += 2;
  273. src = (dn_address *)ptr;
  274. ptr += 2;
  275. *ptr = 0; /* Zero hop count */
  276. /* Swap source and destination */
  277. tmp  = *src;
  278. *src = *dst;
  279. *dst = tmp;
  280. skb->pkt_type = PACKET_OUTGOING;
  281. dn_rt_finish_output(skb, NULL);
  282. return NET_RX_SUCCESS;
  283. }
  284. /**
  285.  * dn_return_long - Return a long packet to its sender
  286.  * @skb: The long format packet to return
  287.  *
  288.  */
  289. static int dn_return_long(struct sk_buff *skb)
  290. {
  291. struct dn_skb_cb *cb;
  292. unsigned char *ptr;
  293. unsigned char *src_addr, *dst_addr;
  294. unsigned char tmp[ETH_ALEN];
  295. /* Add back all headers */
  296. skb_push(skb, skb->data - skb->nh.raw);
  297. if ((skb = skb_unshare(skb, GFP_ATOMIC)) == NULL)
  298. return NET_RX_DROP;
  299. cb = DN_SKB_CB(skb);
  300. /* Ignore packet length and point to flags */
  301. ptr = skb->data + 2;
  302. /* Skip padding */
  303. if (*ptr & DN_RT_F_PF) {
  304. char padlen = (*ptr & ~DN_RT_F_PF);
  305. ptr += padlen;
  306. }
  307. *ptr++ = (cb->rt_flags & ~DN_RT_F_RQR) | DN_RT_F_RTS;
  308. ptr += 2;
  309. dst_addr = ptr;
  310. ptr += 8;
  311. src_addr = ptr;
  312. ptr += 6;
  313. *ptr = 0; /* Zero hop count */
  314. /* Swap source and destination */
  315. memcpy(tmp, src_addr, ETH_ALEN);
  316. memcpy(src_addr, dst_addr, ETH_ALEN);
  317. memcpy(dst_addr, tmp, ETH_ALEN);
  318. skb->pkt_type = PACKET_OUTGOING;
  319. dn_rt_finish_output(skb, tmp);
  320. return NET_RX_SUCCESS;
  321. }
  322. /**
  323.  * dn_route_rx_packet - Try and find a route for an incoming packet
  324.  * @skb: The packet to find a route for
  325.  *
  326.  * Returns: result of input function if route is found, error code otherwise
  327.  */
  328. static int dn_route_rx_packet(struct sk_buff *skb)
  329. {
  330. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  331. int err;
  332. if ((err = dn_route_input(skb)) == 0)
  333. return skb->dst->input(skb);
  334. if (decnet_debug_level & 4) {
  335. char *devname = skb->dev ? skb->dev->name : "???";
  336. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  337. printk(KERN_DEBUG
  338. "DECnet: dn_route_rx_packet: rt_flags=0x%02x dev=%s len=%d src=0x%04hx dst=0x%04hx err=%d type=%dn",
  339. (int)cb->rt_flags, devname, skb->len, cb->src, cb->dst, 
  340. err, skb->pkt_type);
  341. }
  342. if ((skb->pkt_type == PACKET_HOST) && (cb->rt_flags & DN_RT_F_RQR)) {
  343. switch(cb->rt_flags & DN_RT_PKT_MSK) {
  344. case DN_RT_PKT_SHORT:
  345. return dn_return_short(skb);
  346. case DN_RT_PKT_LONG:
  347. return dn_return_long(skb);
  348. }
  349. }
  350. kfree_skb(skb);
  351. return NET_RX_DROP;
  352. }
  353. static int dn_route_rx_long(struct sk_buff *skb)
  354. {
  355. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  356. unsigned char *ptr = skb->data;
  357. if (skb->len < 21) /* 20 for long header, 1 for shortest nsp */
  358. goto drop_it;
  359. skb_pull(skb, 20);
  360. skb->h.raw = skb->data;
  361.         /* Destination info */
  362.         ptr += 2;
  363. cb->dst = dn_htons(dn_eth2dn(ptr));
  364.         if (memcmp(ptr, dn_hiord_addr, 4) != 0)
  365.                 goto drop_it;
  366.         ptr += 6;
  367.         /* Source info */
  368.         ptr += 2;
  369. cb->src = dn_htons(dn_eth2dn(ptr));
  370.         if (memcmp(ptr, dn_hiord_addr, 4) != 0)
  371.                 goto drop_it;
  372.         ptr += 6;
  373.         /* Other junk */
  374.         ptr++;
  375.         cb->hops = *ptr++; /* Visit Count */
  376. return NF_HOOK(PF_DECnet, NF_DN_PRE_ROUTING, skb, skb->dev, NULL, dn_route_rx_packet);
  377. drop_it:
  378. kfree_skb(skb);
  379. return NET_RX_DROP;
  380. }
  381. static int dn_route_rx_short(struct sk_buff *skb)
  382. {
  383. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  384. unsigned char *ptr = skb->data;
  385. if (skb->len < 6) /* 5 for short header + 1 for shortest nsp */
  386. goto drop_it;
  387. skb_pull(skb, 5);
  388. skb->h.raw = skb->data;
  389. cb->dst = *(dn_address *)ptr;
  390.         ptr += 2;
  391.         cb->src = *(dn_address *)ptr;
  392.         ptr += 2;
  393.         cb->hops = *ptr & 0x3f;
  394. return NF_HOOK(PF_DECnet, NF_DN_PRE_ROUTING, skb, skb->dev, NULL, dn_route_rx_packet);
  395. drop_it:
  396.         kfree_skb(skb);
  397.         return NET_RX_DROP;
  398. }
  399. static int dn_route_discard(struct sk_buff *skb)
  400. {
  401. /*
  402.  * I know we drop the packet here, but thats considered success in
  403.  * this case
  404.  */
  405. kfree_skb(skb);
  406. return NET_RX_SUCCESS;
  407. }
  408. static int dn_route_ptp_hello(struct sk_buff *skb)
  409. {
  410. dn_dev_hello(skb);
  411. dn_neigh_pointopoint_hello(skb);
  412. return NET_RX_SUCCESS;
  413. }
  414. int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
  415. {
  416. struct dn_skb_cb *cb;
  417. unsigned char flags = 0;
  418. __u16 len = dn_ntohs(*(__u16 *)skb->data);
  419. struct dn_dev *dn = (struct dn_dev *)dev->dn_ptr;
  420. unsigned char padlen = 0;
  421. if (dn == NULL)
  422. goto dump_it;
  423. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  424. goto out;
  425. skb_pull(skb, 2);
  426. if (len > skb->len)
  427. goto dump_it;
  428. skb_trim(skb, len);
  429. flags = *skb->data;
  430. cb = DN_SKB_CB(skb);
  431. cb->stamp = jiffies;
  432. cb->iif = dev->ifindex;
  433. /*
  434.  * If we have padding, remove it.
  435.  */
  436. if (flags & DN_RT_F_PF) {
  437. padlen = flags & ~DN_RT_F_PF;
  438. skb_pull(skb, padlen);
  439. flags = *skb->data;
  440. }
  441. skb->nh.raw = skb->data;
  442. /*
  443.  * Weed out future version DECnet
  444.  */
  445. if (flags & DN_RT_F_VER)
  446. goto dump_it;
  447. cb->rt_flags = flags;
  448. if (decnet_debug_level & 1)
  449. printk(KERN_DEBUG 
  450. "dn_route_rcv: got 0x%02x from %s [%d %d %d]n",
  451. (int)flags, (dev) ? dev->name : "???", len, skb->len, 
  452. padlen);
  453.         if (flags & DN_RT_PKT_CNTL) {
  454.                 switch(flags & DN_RT_CNTL_MSK) {
  455.                  case DN_RT_PKT_INIT:
  456. dn_dev_init_pkt(skb);
  457. break;
  458.                  case DN_RT_PKT_VERI:
  459. dn_dev_veri_pkt(skb);
  460. break;
  461. }
  462. if (dn->parms.state != DN_DEV_S_RU)
  463. goto dump_it;
  464. switch(flags & DN_RT_CNTL_MSK) {
  465.                  case DN_RT_PKT_HELO:
  466. return NF_HOOK(PF_DECnet, NF_DN_HELLO, skb, skb->dev, NULL, dn_route_ptp_hello);
  467.                  case DN_RT_PKT_L1RT:
  468.                  case DN_RT_PKT_L2RT:
  469.                                 return NF_HOOK(PF_DECnet, NF_DN_ROUTE, skb, skb->dev, NULL, dn_route_discard);
  470.                  case DN_RT_PKT_ERTH:
  471. return NF_HOOK(PF_DECnet, NF_DN_HELLO, skb, skb->dev, NULL, dn_neigh_router_hello);
  472.                  case DN_RT_PKT_EEDH:
  473. return NF_HOOK(PF_DECnet, NF_DN_HELLO, skb, skb->dev, NULL, dn_neigh_endnode_hello);
  474.                 }
  475.         } else {
  476. if (dn->parms.state != DN_DEV_S_RU)
  477. goto dump_it;
  478. skb_pull(skb, 1); /* Pull flags */
  479.                 switch(flags & DN_RT_PKT_MSK) {
  480.                  case DN_RT_PKT_LONG:
  481.                          return dn_route_rx_long(skb);
  482.                  case DN_RT_PKT_SHORT:
  483.                          return dn_route_rx_short(skb);
  484. }
  485.         }
  486. dump_it:
  487. kfree_skb(skb);
  488. out:
  489. return NET_RX_DROP;
  490. }
  491. static int dn_output(struct sk_buff *skb)
  492. {
  493. struct dst_entry *dst = skb->dst;
  494. struct dn_route *rt = (struct dn_route *)dst;
  495. struct net_device *dev = dst->dev;
  496. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  497. struct neighbour *neigh;
  498. int err = -EINVAL;
  499. if ((neigh = dst->neighbour) == NULL)
  500. goto error;
  501. skb->dev = dev;
  502. cb->src = rt->rt_saddr;
  503. cb->dst = rt->rt_daddr;
  504. /*
  505.  * Always set the Intra-Ethernet bit on all outgoing packets
  506.  * originated on this node. Only valid flag from upper layers
  507.  * is return-to-sender-requested. Set hop count to 0 too.
  508.  */
  509. cb->rt_flags &= ~DN_RT_F_RQR;
  510. cb->rt_flags |= DN_RT_F_IE;
  511. cb->hops = 0;
  512. return NF_HOOK(PF_DECnet, NF_DN_LOCAL_OUT, skb, NULL, dev, neigh->output);
  513. error:
  514. if (net_ratelimit())
  515. printk(KERN_DEBUG "dn_output: This should not happenn");
  516. kfree_skb(skb);
  517. return err;
  518. }
  519. #ifdef CONFIG_DECNET_ROUTER
  520. static int dn_forward(struct sk_buff *skb)
  521. {
  522. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  523. struct dst_entry *dst = skb->dst;
  524. struct neighbour *neigh;
  525. struct net_device *dev = skb->dev;
  526. int err = -EINVAL;
  527. if ((neigh = dst->neighbour) == NULL)
  528. goto error;
  529. /*
  530.  * Hop count exceeded.
  531.  */
  532. err = NET_RX_DROP;
  533. if (++cb->hops > 30)
  534. goto drop;
  535. skb->dev = dst->dev;
  536. /*
  537.  * If packet goes out same interface it came in on, then set
  538.  * the Intra-Ethernet bit. This has no effect for short
  539.  * packets, so we don't need to test for them here.
  540.  */
  541. if (cb->iif == dst->dev->ifindex)
  542. cb->rt_flags |= DN_RT_F_IE;
  543. else
  544. cb->rt_flags &= ~DN_RT_F_IE;
  545. return NF_HOOK(PF_DECnet, NF_DN_FORWARD, skb, dev, skb->dev, neigh->output);
  546. error:
  547. if (net_ratelimit())
  548. printk(KERN_DEBUG "dn_forward: This should not happenn");
  549. drop:
  550. kfree_skb(skb);
  551. return err;
  552. }
  553. #endif
  554. /*
  555.  * Drop packet. This is used for endnodes and for
  556.  * when we should not be forwarding packets from
  557.  * this dest.
  558.  */
  559. static int dn_blackhole(struct sk_buff *skb)
  560. {
  561. kfree_skb(skb);
  562. return NET_RX_DROP;
  563. }
  564. /*
  565.  * Used to catch bugs. This should never normally get
  566.  * called.
  567.  */
  568. static int dn_rt_bug(struct sk_buff *skb)
  569. {
  570. if (net_ratelimit()) {
  571. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  572. printk(KERN_DEBUG "dn_rt_bug: skb from:%04x to:%04xn",
  573. cb->src, cb->dst);
  574. }
  575. kfree_skb(skb);
  576. return NET_RX_BAD;
  577. }
  578. static int dn_route_output_slow(struct dst_entry **pprt, dn_address dst, dn_address src, int flags)
  579. {
  580. struct dn_route *rt = NULL;
  581. struct net_device *dev = decnet_default_device;
  582. struct neighbour *neigh = NULL;
  583. struct dn_dev *dn_db;
  584. unsigned hash;
  585. #ifdef CONFIG_DECNET_ROUTER
  586. struct dn_fib_key key;
  587. struct dn_fib_res res;
  588. int err;
  589. key.src = src;
  590. key.dst = dst;
  591. key.iif = 0;
  592. key.oif = 0;
  593. key.fwmark = 0;
  594. key.scope = RT_SCOPE_UNIVERSE;
  595. if ((err = dn_fib_lookup(&key, &res)) == 0) {
  596. switch(res.type) {
  597. case RTN_UNICAST:
  598. /*
  599.  * This method of handling multipath
  600.  * routes is a hack and will change.
  601.  * It works for now though.
  602.  */
  603. if (res.fi->fib_nhs)
  604. dn_fib_select_multipath(&key, &res);
  605. neigh = __neigh_lookup(&dn_neigh_table, &DN_FIB_RES_GW(res), DN_FIB_RES_DEV(res), 1);
  606. err = -ENOBUFS;
  607. if (!neigh)
  608. break;
  609. err = 0;
  610. break;
  611. case RTN_UNREACHABLE:
  612. err = -EHOSTUNREACH;
  613. break;
  614. default:
  615. err = -EINVAL;
  616. }
  617. dn_fib_res_put(&res);
  618. if (err < 0)
  619. return err;
  620. goto got_route;
  621. }
  622. if (err != -ESRCH)
  623. return err;
  624. #endif 
  625. /* Look in On-Ethernet cache first */
  626. if (!(flags & MSG_TRYHARD)) {
  627. if ((neigh = dn_neigh_lookup(&dn_neigh_table, &dst)) != NULL)
  628. goto got_route;
  629. }
  630. if (dev == NULL)
  631. return -EINVAL;
  632. dn_db = dev->dn_ptr;
  633. if (dn_db == NULL)
  634. return -EINVAL;
  635. /* Try default router */
  636. if ((neigh = neigh_clone(dn_db->router)) != NULL)
  637. goto got_route;
  638. /* Send to default device (and hope for the best) if above fail */
  639. if ((neigh = __neigh_lookup(&dn_neigh_table, &dst, dev, 1)) != NULL)
  640. goto got_route;
  641. return -EINVAL;
  642. got_route:
  643. if ((rt = dst_alloc(&dn_dst_ops)) == NULL) {
  644. neigh_release(neigh);
  645. return -EINVAL;
  646. }
  647. dn_db = (struct dn_dev *)neigh->dev->dn_ptr;
  648. rt->key.saddr  = src;
  649. rt->rt_saddr   = src;
  650. rt->key.daddr  = dst;
  651. rt->rt_daddr   = dst;
  652. rt->key.oif    = neigh ? neigh->dev->ifindex : -1;
  653. rt->key.iif    = 0;
  654. rt->key.fwmark = 0;
  655. rt->u.dst.neighbour = neigh;
  656. rt->u.dst.dev = neigh ? neigh->dev : NULL;
  657. if (rt->u.dst.dev)
  658. dev_hold(rt->u.dst.dev);
  659. rt->u.dst.lastuse = jiffies;
  660. rt->u.dst.output = dn_output;
  661. rt->u.dst.input  = dn_rt_bug;
  662. if (dn_dev_islocal(neigh->dev, rt->rt_daddr))
  663. rt->u.dst.input = dn_nsp_rx;
  664. hash = dn_hash(rt->key.saddr, rt->key.daddr);
  665. dn_insert_route(rt, hash);
  666. *pprt = &rt->u.dst;
  667. return 0;
  668. }
  669. int dn_route_output(struct dst_entry **pprt, dn_address dst, dn_address src, int flags)
  670. {
  671. unsigned hash = dn_hash(src, dst);
  672. struct dn_route *rt = NULL;
  673. if (!(flags & MSG_TRYHARD)) {
  674. read_lock_bh(&dn_rt_hash_table[hash].lock);
  675. for(rt = dn_rt_hash_table[hash].chain; rt; rt = rt->u.rt_next) {
  676. if ((dst == rt->key.daddr) &&
  677. (src == rt->key.saddr) &&
  678. (rt->key.iif == 0) &&
  679. (rt->key.oif != 0)) {
  680. rt->u.dst.lastuse = jiffies;
  681. dst_hold(&rt->u.dst);
  682. rt->u.dst.__use++;
  683. read_unlock_bh(&dn_rt_hash_table[hash].lock);
  684. *pprt = &rt->u.dst;
  685. return 0;
  686. }
  687. }
  688. read_unlock_bh(&dn_rt_hash_table[hash].lock);
  689. }
  690. return dn_route_output_slow(pprt, dst, src, flags);
  691. }
  692. static int dn_route_input_slow(struct sk_buff *skb)
  693. {
  694. struct dn_route *rt = NULL;
  695. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  696. struct net_device *dev = skb->dev;
  697. struct dn_dev *dn_db;
  698. struct neighbour *neigh = NULL;
  699. int (*dnrt_input)(struct sk_buff *skb);
  700. int (*dnrt_output)(struct sk_buff *skb);
  701. u32 fwmark = 0;
  702. unsigned hash;
  703. dn_address saddr = cb->src;
  704. dn_address daddr = cb->dst;
  705. #ifdef CONFIG_DECNET_ROUTER
  706. struct dn_fib_key key;
  707. struct dn_fib_res res;
  708. int err;
  709. #endif
  710. if (dev == NULL)
  711. return -EINVAL;
  712. if ((dn_db = dev->dn_ptr) == NULL)
  713. return -EINVAL;
  714. /*
  715.  * In this case we've just received a packet from a source
  716.  * outside ourselves pretending to come from us. We don't
  717.  * allow it any further to prevent routing loops, spoofing and
  718.  * other nasties. Loopback packets already have the dst attached
  719.  * so this only affects packets which have originated elsewhere.
  720.  */
  721. if (dn_dev_islocal(dev, cb->src))
  722. return -ENOTUNIQ;
  723. /*
  724.  * Default is to create a drop everything entry
  725.  */
  726. dnrt_input  = dn_blackhole;
  727. dnrt_output = dn_rt_bug;
  728. /*
  729.  * Is the destination us ?
  730.  */
  731. if (!dn_dev_islocal(dev, cb->dst))
  732. goto non_local_input;
  733. /*
  734.  * Local input... find source of skb
  735.  */
  736. dnrt_input  = dn_nsp_rx;
  737. dnrt_output = dn_output;
  738. saddr = cb->dst;
  739. daddr = cb->src;
  740. if ((neigh = neigh_lookup(&dn_neigh_table, &cb->src, dev)) != NULL)
  741. goto add_entry;
  742. if (dn_db->router && ((neigh = neigh_clone(dn_db->router)) != NULL))
  743. goto add_entry;
  744. neigh = neigh_create(&dn_neigh_table, &cb->src, dev);
  745. if (!IS_ERR(neigh)) {
  746. if (dev->type == ARPHRD_ETHER)
  747. memcpy(neigh->ha, skb->mac.ethernet->h_source, ETH_ALEN);
  748. goto add_entry;
  749. }
  750. return PTR_ERR(neigh);
  751. non_local_input:
  752. #ifdef CONFIG_DECNET_ROUTER
  753. /*
  754.  * Destination is another node... find next hop in
  755.  * routing table here.
  756.  */
  757. key.src = cb->src;
  758. key.dst = cb->dst;
  759. key.iif = dev->ifindex;
  760. key.oif = 0;
  761. key.scope = RT_SCOPE_UNIVERSE;
  762. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  763. key.fwmark = skb->nfmark;
  764. #else
  765. key.fwmark = 0;
  766. #endif
  767. if ((err = dn_fib_lookup(&key, &res)) == 0) {
  768. switch(res.type) {
  769. case RTN_UNICAST:
  770. if (res.fi->fib_nhs)
  771. dn_fib_select_multipath(&key, &res);
  772. neigh = __neigh_lookup(&dn_neigh_table, &DN_FIB_RES_GW(res), DN_FIB_RES_DEV(res), 1);
  773. err = -ENOBUFS;
  774. if (!neigh)
  775. break;
  776. err = 0;
  777. dnrt_input = dn_forward;
  778. fwmark = key.fwmark;
  779. break;
  780. case RTN_UNREACHABLE:
  781. dnrt_input = dn_blackhole;
  782. fwmark = key.fwmark;
  783. break;
  784. default:
  785. err = -EINVAL;
  786. }
  787. dn_fib_res_put(&res);
  788. if (err < 0)
  789. return err;
  790. goto add_entry;
  791. }
  792. return err;
  793. #endif /* CONFIG_DECNET_ROUTER */
  794. add_entry:
  795. if ((rt = dst_alloc(&dn_dst_ops)) == NULL) {
  796.                 neigh_release(neigh);
  797.                 return -EINVAL;
  798.         }
  799. rt->key.saddr  = cb->src;
  800. rt->rt_saddr   = saddr;
  801. rt->key.daddr  = cb->dst;
  802. rt->rt_daddr   = daddr;
  803. rt->key.oif    = 0;
  804. rt->key.iif    = dev->ifindex;
  805. rt->key.fwmark = fwmark;
  806. rt->u.dst.neighbour = neigh;
  807. rt->u.dst.dev = neigh ? neigh->dev : NULL;
  808. if (rt->u.dst.dev)
  809. dev_hold(rt->u.dst.dev);
  810. rt->u.dst.lastuse = jiffies;
  811. rt->u.dst.output = dnrt_output;
  812. rt->u.dst.input = dnrt_input;
  813. hash = dn_hash(rt->key.saddr, rt->key.daddr);
  814. dn_insert_route(rt, hash);
  815. skb->dst = (struct dst_entry *)rt;
  816. return 0;
  817. }
  818. int dn_route_input(struct sk_buff *skb)
  819. {
  820. struct dn_route *rt;
  821. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  822. unsigned hash = dn_hash(cb->src, cb->dst);
  823. if (skb->dst)
  824. return 0;
  825. read_lock(&dn_rt_hash_table[hash].lock);
  826. for(rt = dn_rt_hash_table[hash].chain; rt != NULL; rt = rt->u.rt_next) {
  827. if ((rt->key.saddr == cb->src) &&
  828. (rt->key.daddr == cb->dst) &&
  829. (rt->key.oif == 0) &&
  830. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  831. (rt->key.fwmark == skb->nfmark) &&
  832. #endif
  833. (rt->key.iif == cb->iif)) {
  834. rt->u.dst.lastuse = jiffies;
  835. dst_hold(&rt->u.dst);
  836. rt->u.dst.__use++;
  837. read_unlock(&dn_rt_hash_table[hash].lock);
  838. skb->dst = (struct dst_entry *)rt;
  839. return 0;
  840. }
  841. }
  842. read_unlock(&dn_rt_hash_table[hash].lock);
  843. return dn_route_input_slow(skb);
  844. }
  845. static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq, int event, int nowait)
  846. {
  847. struct dn_route *rt = (struct dn_route *)skb->dst;
  848. struct rtmsg *r;
  849. struct nlmsghdr *nlh;
  850. unsigned char *b = skb->tail;
  851. nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*r));
  852. r = NLMSG_DATA(nlh);
  853. nlh->nlmsg_flags = nowait ? NLM_F_MULTI : 0;
  854. r->rtm_family = AF_DECnet;
  855. r->rtm_dst_len = 16;
  856. r->rtm_src_len = 16;
  857. r->rtm_tos = 0;
  858. r->rtm_table = 0;
  859. r->rtm_type = 0;
  860. r->rtm_flags = 0;
  861. r->rtm_scope = RT_SCOPE_UNIVERSE;
  862. r->rtm_protocol = RTPROT_UNSPEC;
  863. RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
  864. RTA_PUT(skb, RTA_SRC, 2, &rt->rt_saddr);
  865. if (rt->u.dst.dev)
  866. RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->u.dst.dev->ifindex);
  867. if (rt->u.dst.window)
  868. RTA_PUT(skb, RTAX_WINDOW, sizeof(unsigned), &rt->u.dst.window);
  869. if (rt->u.dst.rtt)
  870. RTA_PUT(skb, RTAX_RTT, sizeof(unsigned), &rt->u.dst.rtt);
  871. nlh->nlmsg_len = skb->tail - b;
  872. return skb->len;
  873. nlmsg_failure:
  874. rtattr_failure:
  875.         skb_trim(skb, b - skb->data);
  876.         return -1;
  877. }
  878. /*
  879.  * This is called by both endnodes and routers now.
  880.  */
  881. int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void *arg)
  882. {
  883. struct rtattr **rta = arg;
  884. struct dn_route *rt = NULL;
  885. struct dn_skb_cb *cb;
  886. dn_address dst = 0;
  887. dn_address src = 0;
  888. int iif = 0;
  889. int err;
  890. struct sk_buff *skb;
  891. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  892. if (skb == NULL)
  893. return -ENOBUFS;
  894. skb->mac.raw = skb->data;
  895. cb = DN_SKB_CB(skb);
  896. if (rta[RTA_SRC-1])
  897. memcpy(&src, RTA_DATA(rta[RTA_SRC-1]), 2);
  898. if (rta[RTA_DST-1])
  899. memcpy(&dst, RTA_DATA(rta[RTA_DST-1]), 2);
  900. if (rta[RTA_IIF-1])
  901. memcpy(&iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
  902. if (iif) {
  903. struct net_device *dev;
  904. if ((dev = dev_get_by_index(iif)) == NULL) {
  905. kfree_skb(skb);
  906. return -ENODEV;
  907. }
  908. if (!dev->dn_ptr) {
  909. dev_put(dev);
  910. kfree_skb(skb);
  911. return -ENODEV;
  912. }
  913. skb->protocol = __constant_htons(ETH_P_DNA_RT);
  914. skb->dev = dev;
  915. cb->src = src;
  916. cb->dst = dst;
  917. local_bh_disable();
  918. err = dn_route_input(skb);
  919. local_bh_enable();
  920. memset(cb, 0, sizeof(struct dn_skb_cb));
  921. rt = (struct dn_route *)skb->dst;
  922. } else {
  923. err = dn_route_output((struct dst_entry **)&rt, dst, src, 0);
  924. }
  925. if (!err && rt->u.dst.error)
  926. err = rt->u.dst.error;
  927. if (skb->dev)
  928. dev_put(skb->dev);
  929. skb->dev = NULL;
  930. if (err)
  931. goto out_free;
  932. skb->dst = &rt->u.dst;
  933. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  934. err = dn_rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, RTM_NEWROUTE, 0);
  935. if (err == 0)
  936. goto out_free;
  937. if (err < 0) {
  938. err = -EMSGSIZE;
  939. goto out_free;
  940. }
  941. err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
  942. return err;
  943. out_free:
  944. kfree_skb(skb);
  945. return err;
  946. }
  947. /*
  948.  * For routers, this is called from dn_fib_dump, but for endnodes its
  949.  * called directly from the rtnetlink dispatch table.
  950.  */
  951. int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
  952. {
  953. struct dn_route *rt;
  954. int h, s_h;
  955. int idx, s_idx;
  956. if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
  957. return -EINVAL;
  958. if (!(((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED))
  959. return 0;
  960. s_h = cb->args[0];
  961. s_idx = idx = cb->args[1];
  962. for(h = 0; h <= dn_rt_hash_mask; h++) {
  963. if (h < s_h)
  964. continue;
  965. if (h > s_h)
  966. s_idx = 0;
  967. read_lock_bh(&dn_rt_hash_table[h].lock);
  968. for(rt = dn_rt_hash_table[h].chain, idx = 0; rt; rt = rt->u.rt_next, idx++) {
  969. if (idx < s_idx)
  970. continue;
  971. skb->dst = dst_clone(&rt->u.dst);
  972. if (dn_rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
  973. cb->nlh->nlmsg_seq, RTM_NEWROUTE, 1) <= 0) {
  974. dst_release(xchg(&skb->dst, NULL));
  975. read_unlock_bh(&dn_rt_hash_table[h].lock);
  976. goto done;
  977. }
  978. dst_release(xchg(&skb->dst, NULL));
  979. }
  980. read_unlock_bh(&dn_rt_hash_table[h].lock);
  981. }
  982. done:
  983. cb->args[0] = h;
  984. cb->args[1] = idx;
  985. return skb->len;
  986. }
  987. #ifdef CONFIG_PROC_FS
  988. static int decnet_cache_get_info(char *buffer, char **start, off_t offset, int length)
  989. {
  990.         int len = 0;
  991.         off_t pos = 0;
  992.         off_t begin = 0;
  993. struct dn_route *rt;
  994. int i;
  995. char buf1[DN_ASCBUF_LEN], buf2[DN_ASCBUF_LEN];
  996. for(i = 0; i <= dn_rt_hash_mask; i++) {
  997. read_lock_bh(&dn_rt_hash_table[i].lock);
  998. rt = dn_rt_hash_table[i].chain;
  999. for(; rt != NULL; rt = rt->u.rt_next) {
  1000. len += sprintf(buffer + len, "%-8s %-7s %-7s %04d %04d %04dn",
  1001. rt->u.dst.dev ? rt->u.dst.dev->name : "*",
  1002. dn_addr2asc(dn_ntohs(rt->rt_daddr), buf1),
  1003. dn_addr2asc(dn_ntohs(rt->rt_saddr), buf2),
  1004. atomic_read(&rt->u.dst.__refcnt),
  1005. rt->u.dst.__use,
  1006. (int)rt->u.dst.rtt
  1007. );
  1008.                 pos = begin + len;
  1009.                  if (pos < offset) {
  1010.                          len   = 0;
  1011.                          begin = pos;
  1012.                  }
  1013.                if (pos > offset + length)
  1014.                          break;
  1015. }
  1016. read_unlock_bh(&dn_rt_hash_table[i].lock);
  1017. if (pos > offset + length)
  1018. break;
  1019. }
  1020.         *start = buffer + (offset - begin);
  1021.         len   -= (offset - begin);
  1022.         if (len > length) len = length;
  1023.         return(len);
  1024. #endif /* CONFIG_PROC_FS */
  1025. void __init dn_route_init(void)
  1026. {
  1027. int i, goal, order;
  1028. dn_dst_ops.kmem_cachep = kmem_cache_create("dn_dst_cache",
  1029.    sizeof(struct dn_route),
  1030.    0, SLAB_HWCACHE_ALIGN,
  1031.    NULL, NULL);
  1032. if (!dn_dst_ops.kmem_cachep)
  1033. panic("DECnet: Failed to allocate dn_dst_cachen");
  1034. dn_route_timer.function = dn_dst_check_expire;
  1035. dn_route_timer.expires = jiffies + decnet_dst_gc_interval * HZ;
  1036. add_timer(&dn_route_timer);
  1037. goal = num_physpages >> (26 - PAGE_SHIFT);
  1038. for(order = 0; (1UL << order) < goal; order++)
  1039. /* NOTHING */;
  1040.         /*
  1041.          * Only want 1024 entries max, since the table is very, very unlikely
  1042.          * to be larger than that.
  1043.          */
  1044.         while(order && ((((1UL << order) * PAGE_SIZE) / 
  1045.                                 sizeof(struct dn_rt_hash_bucket)) >= 2048))
  1046.                 order--;
  1047.         do {
  1048.                 dn_rt_hash_mask = (1UL << order) * PAGE_SIZE /
  1049.                         sizeof(struct dn_rt_hash_bucket);
  1050.                 while(dn_rt_hash_mask & (dn_rt_hash_mask - 1))
  1051.                         dn_rt_hash_mask--;
  1052.                 dn_rt_hash_table = (struct dn_rt_hash_bucket *)
  1053.                         __get_free_pages(GFP_ATOMIC, order);
  1054.         } while (dn_rt_hash_table == NULL && --order > 0);
  1055. if (!dn_rt_hash_table)
  1056.                 panic("Failed to allocate DECnet route cache hash tablen");
  1057. printk(KERN_INFO 
  1058. "DECnet: Routing cache hash table of %u buckets, %ldKbytesn", 
  1059. dn_rt_hash_mask, 
  1060. (long)(dn_rt_hash_mask*sizeof(struct dn_rt_hash_bucket))/1024);
  1061. dn_rt_hash_mask--;
  1062.         for(i = 0; i <= dn_rt_hash_mask; i++) {
  1063.                 dn_rt_hash_table[i].lock = RW_LOCK_UNLOCKED;
  1064.                 dn_rt_hash_table[i].chain = NULL;
  1065.         }
  1066.         dn_dst_ops.gc_thresh = (dn_rt_hash_mask + 1);
  1067. #ifdef CONFIG_PROC_FS
  1068. proc_net_create("decnet_cache",0,decnet_cache_get_info);
  1069. #endif /* CONFIG_PROC_FS */
  1070. }
  1071. void __exit dn_route_cleanup(void)
  1072. {
  1073. del_timer(&dn_route_timer);
  1074. dn_run_flush(0);
  1075. proc_net_remove("decnet_cache");
  1076. }