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

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 Neighbour Functions (Adjacency Database and 
  7.  *                                                        On-Ethernet Cache)
  8.  *
  9.  * Author:      Steve Whitehouse <SteveW@ACM.org>
  10.  *
  11.  *
  12.  * Changes:
  13.  *     Steve Whitehouse     : Fixed router listing routine
  14.  *     Steve Whitehouse     : Added error_report functions
  15.  *     Steve Whitehouse     : Added default router detection
  16.  *     Steve Whitehouse     : Hop counts in outgoing messages
  17.  *     Steve Whitehouse     : Fixed src/dst in outgoing messages so
  18.  *                            forwarding now stands a good chance of
  19.  *                            working.
  20.  *     Steve Whitehouse     : Fixed neighbour states (for now anyway).
  21.  *     Steve Whitehouse     : Made error_report functions dummies. This
  22.  *                            is not the right place to return skbs.
  23.  *
  24.  */
  25. #include <linux/config.h>
  26. #include <linux/net.h>
  27. #include <linux/socket.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/init.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/string.h>
  33. #include <linux/netfilter_decnet.h>
  34. #include <linux/spinlock.h>
  35. #include <asm/atomic.h>
  36. #include <net/neighbour.h>
  37. #include <net/dst.h>
  38. #include <net/dn.h>
  39. #include <net/dn_dev.h>
  40. #include <net/dn_neigh.h>
  41. #include <net/dn_route.h>
  42. static u32 dn_neigh_hash(const void *pkey, const struct net_device *dev);
  43. static int dn_neigh_construct(struct neighbour *);
  44. static void dn_long_error_report(struct neighbour *, struct sk_buff *);
  45. static void dn_short_error_report(struct neighbour *, struct sk_buff *);
  46. static int dn_long_output(struct sk_buff *);
  47. static int dn_short_output(struct sk_buff *);
  48. static int dn_phase3_output(struct sk_buff *);
  49. /*
  50.  * For talking to broadcast devices: Ethernet & PPP
  51.  */
  52. static struct neigh_ops dn_long_ops = {
  53. family: AF_DECnet,
  54. error_report: dn_long_error_report,
  55. output: dn_long_output,
  56. connected_output: dn_long_output,
  57. hh_output: dev_queue_xmit,
  58. queue_xmit: dev_queue_xmit,
  59. };
  60. /*
  61.  * For talking to pointopoint and multidrop devices: DDCMP and X.25
  62.  */
  63. static struct neigh_ops dn_short_ops = {
  64. family: AF_DECnet,
  65. error_report: dn_short_error_report,
  66. output: dn_short_output,
  67. connected_output: dn_short_output,
  68. hh_output: dev_queue_xmit,
  69. queue_xmit: dev_queue_xmit,
  70. };
  71. /*
  72.  * For talking to DECnet phase III nodes
  73.  */
  74. static struct neigh_ops dn_phase3_ops = {
  75. family: AF_DECnet,
  76. error_report: dn_short_error_report, /* Can use short version here */
  77. output: dn_phase3_output,
  78. connected_output: dn_phase3_output,
  79. hh_output: dev_queue_xmit,
  80. queue_xmit: dev_queue_xmit
  81. };
  82. struct neigh_table dn_neigh_table = {
  83. family: PF_DECnet,
  84. entry_size: sizeof(struct dn_neigh),
  85. key_len: sizeof(dn_address),
  86. hash: dn_neigh_hash,
  87. constructor: dn_neigh_construct,
  88. id: "dn_neigh_cache",
  89. parms: {
  90. tbl: &dn_neigh_table,
  91. entries: 0,
  92. base_reachable_time: 30 * HZ,
  93. retrans_time: 1 * HZ,
  94. gc_staletime: 60 * HZ,
  95. reachable_time: 30 * HZ,
  96. delay_probe_time: 5 * HZ,
  97. queue_len: 3,
  98. ucast_probes: 0,
  99. app_probes: 0,
  100. mcast_probes: 0,
  101. anycast_delay: 0,
  102. proxy_delay: 0,
  103. proxy_qlen: 0,
  104. locktime: 1 * HZ,
  105. },
  106. gc_interval: 30 * HZ,
  107. gc_thresh1: 128,
  108. gc_thresh2: 512,
  109. gc_thresh3: 1024,
  110. };
  111. static u32 dn_neigh_hash(const void *pkey, const struct net_device *dev)
  112. {
  113. u32 hash_val;
  114. hash_val = *(dn_address *)pkey;
  115. hash_val ^= (hash_val >> 10);
  116. hash_val ^= (hash_val >> 3);
  117. return hash_val & NEIGH_HASHMASK;
  118. }
  119. static int dn_neigh_construct(struct neighbour *neigh)
  120. {
  121. struct net_device *dev = neigh->dev;
  122. struct dn_neigh *dn = (struct dn_neigh *)neigh;
  123. struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
  124. if (dn_db == NULL)
  125. return -EINVAL;
  126. if (dn_db->neigh_parms)
  127. neigh->parms = dn_db->neigh_parms;
  128. if (dn_db->use_long)
  129. neigh->ops = &dn_long_ops;
  130. else
  131. neigh->ops = &dn_short_ops;
  132. if (dn->flags & DN_NDFLAG_P3)
  133. neigh->ops = &dn_phase3_ops;
  134. neigh->nud_state = NUD_NOARP;
  135. neigh->output = neigh->ops->connected_output;
  136. if ((dev->type == ARPHRD_IPGRE) || (dev->flags & IFF_POINTOPOINT))
  137. memcpy(neigh->ha, dev->broadcast, dev->addr_len);
  138. else if ((dev->type == ARPHRD_ETHER) || (dev->type == ARPHRD_LOOPBACK))
  139. dn_dn2eth(neigh->ha, dn->addr);
  140. else {
  141. if (net_ratelimit())
  142. printk(KERN_DEBUG "Trying to create neigh for hw %dn",  dev->type);
  143. return -EINVAL;
  144. }
  145. dn->blksize = 230;
  146. return 0;
  147. }
  148. static void dn_long_error_report(struct neighbour *neigh, struct sk_buff *skb)
  149. {
  150. printk(KERN_DEBUG "dn_long_error_report: calledn");
  151. kfree_skb(skb);
  152. }
  153. static void dn_short_error_report(struct neighbour *neigh, struct sk_buff *skb)
  154. {
  155. printk(KERN_DEBUG "dn_short_error_report: calledn");
  156. kfree_skb(skb);
  157. }
  158. static int dn_neigh_output_packet(struct sk_buff *skb)
  159. {
  160. struct dst_entry *dst = skb->dst;
  161. struct neighbour *neigh = dst->neighbour;
  162. struct net_device *dev = neigh->dev;
  163. if (!dev->hard_header || dev->hard_header(skb, dev, ntohs(skb->protocol), neigh->ha, NULL, skb->len) >= 0)
  164. return neigh->ops->queue_xmit(skb);
  165. if (net_ratelimit())
  166. printk(KERN_DEBUG "dn_neigh_output_packet: oops, can't send packetn");
  167. kfree_skb(skb);
  168. return -EINVAL;
  169. }
  170. static int dn_long_output(struct sk_buff *skb)
  171. {
  172. struct dst_entry *dst = skb->dst;
  173. struct neighbour *neigh = dst->neighbour;
  174. struct net_device *dev = neigh->dev;
  175. int headroom = dev->hard_header_len + sizeof(struct dn_long_packet) + 3;
  176. unsigned char *data;
  177. struct dn_long_packet *lp;
  178. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  179. if (skb_headroom(skb) < headroom) {
  180. struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
  181. if (skb2 == NULL) {
  182. if (net_ratelimit())
  183. printk(KERN_CRIT "dn_long_output: no memoryn");
  184. kfree_skb(skb);
  185. return -ENOBUFS;
  186. }
  187. kfree_skb(skb);
  188. skb = skb2;
  189. if (net_ratelimit())
  190. printk(KERN_INFO "dn_long_output: Increasing headroomn");
  191. }
  192. data = skb_push(skb, sizeof(struct dn_long_packet) + 3);
  193. lp = (struct dn_long_packet *)(data+3);
  194. *((unsigned short *)data) = dn_htons(skb->len - 2);
  195. *(data + 2) = 1 | DN_RT_F_PF; /* Padding */
  196. lp->msgflg   = DN_RT_PKT_LONG|(cb->rt_flags&(DN_RT_F_IE|DN_RT_F_RQR|DN_RT_F_RTS));
  197. lp->d_area   = lp->d_subarea = 0;
  198. dn_dn2eth(lp->d_id, dn_ntohs(cb->dst));
  199. lp->s_area   = lp->s_subarea = 0;
  200. dn_dn2eth(lp->s_id, dn_ntohs(cb->src));
  201. lp->nl2      = 0;
  202. lp->visit_ct = cb->hops & 0x3f;
  203. lp->s_class  = 0;
  204. lp->pt       = 0;
  205. skb->nh.raw = skb->data;
  206. return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
  207. }
  208. static int dn_short_output(struct sk_buff *skb)
  209. {
  210. struct dst_entry *dst = skb->dst;
  211. struct neighbour *neigh = dst->neighbour;
  212. struct net_device *dev = neigh->dev;
  213. int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
  214. struct dn_short_packet *sp;
  215. unsigned char *data;
  216. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  217.         if (skb_headroom(skb) < headroom) {
  218.                 struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
  219.                 if (skb2 == NULL) {
  220. if (net_ratelimit())
  221.                          printk(KERN_CRIT "dn_short_output: no memoryn");
  222.                         kfree_skb(skb);
  223.                         return -ENOBUFS;
  224.                 }
  225.                 kfree_skb(skb);
  226.                 skb = skb2;
  227. if (net_ratelimit())
  228.                  printk(KERN_INFO "dn_short_output: Increasing headroomn");
  229.         }
  230. data = skb_push(skb, sizeof(struct dn_short_packet) + 2);
  231. *((unsigned short *)data) = dn_htons(skb->len - 2);
  232. sp = (struct dn_short_packet *)(data+2);
  233. sp->msgflg     = DN_RT_PKT_SHORT|(cb->rt_flags&(DN_RT_F_RQR|DN_RT_F_RTS));
  234. sp->dstnode    = cb->dst;
  235. sp->srcnode    = cb->src;
  236. sp->forward    = cb->hops & 0x3f;
  237. skb->nh.raw = skb->data;
  238. return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
  239. }
  240. /*
  241.  * Phase 3 output is the same is short output, execpt that
  242.  * it clears the area bits before transmission.
  243.  */
  244. static int dn_phase3_output(struct sk_buff *skb)
  245. {
  246. struct dst_entry *dst = skb->dst;
  247. struct neighbour *neigh = dst->neighbour;
  248. struct net_device *dev = neigh->dev;
  249. int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
  250. struct dn_short_packet *sp;
  251. unsigned char *data;
  252. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  253. if (skb_headroom(skb) < headroom) {
  254. struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
  255. if (skb2 == NULL) {
  256. if (net_ratelimit())
  257. printk(KERN_CRIT "dn_phase3_output: no memoryn");
  258. kfree_skb(skb);
  259. return -ENOBUFS;
  260. }
  261. kfree_skb(skb);
  262. skb = skb2;
  263. if (net_ratelimit())
  264. printk(KERN_INFO "dn_phase3_output: Increasing headroomn");
  265. }
  266. data = skb_push(skb, sizeof(struct dn_short_packet) + 2);
  267. ((unsigned short *)data) = dn_htons(skb->len - 2);
  268. sp = (struct dn_short_packet *)(data + 2);
  269. sp->msgflg   = DN_RT_PKT_SHORT|(cb->rt_flags&(DN_RT_F_RQR|DN_RT_F_RTS));
  270. sp->dstnode  = cb->dst & dn_htons(0x03ff);
  271. sp->srcnode  = cb->src & dn_htons(0x03ff);
  272. sp->forward  = cb->hops & 0x3f;
  273. skb->nh.raw = skb->data;
  274. return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
  275. }
  276. /*
  277.  * Unfortunately, the neighbour code uses the device in its hash
  278.  * function, so we don't get any advantage from it. This function
  279.  * basically does a neigh_lookup(), but without comparing the device
  280.  * field. This is required for the On-Ethernet cache
  281.  */
  282. struct neighbour *dn_neigh_lookup(struct neigh_table *tbl, void *ptr)
  283. {
  284. struct neighbour *neigh;
  285. u32 hash_val;
  286. hash_val = tbl->hash(ptr, NULL);
  287. read_lock_bh(&tbl->lock);
  288. for(neigh = tbl->hash_buckets[hash_val]; neigh != NULL; neigh = neigh->next) {
  289. if (memcmp(neigh->primary_key, ptr, tbl->key_len) == 0) {
  290. atomic_inc(&neigh->refcnt);
  291. read_unlock_bh(&tbl->lock);
  292. return neigh;
  293. }
  294. }
  295. read_unlock_bh(&tbl->lock);
  296. return NULL;
  297. }
  298. /*
  299.  * Any traffic on a pointopoint link causes the timer to be reset
  300.  * for the entry in the neighbour table.
  301.  */
  302. void dn_neigh_pointopoint_notify(struct sk_buff *skb)
  303. {
  304. return;
  305. }
  306. /*
  307.  * Pointopoint link receives a hello message
  308.  */
  309. void dn_neigh_pointopoint_hello(struct sk_buff *skb)
  310. {
  311. kfree_skb(skb);
  312. }
  313. /*
  314.  * Ethernet router hello message received
  315.  */
  316. int dn_neigh_router_hello(struct sk_buff *skb)
  317. {
  318. struct rtnode_hello_message *msg = (struct rtnode_hello_message *)skb->data;
  319. struct neighbour *neigh;
  320. struct dn_neigh *dn;
  321. struct dn_dev *dn_db;
  322. dn_address src;
  323. src = dn_htons(dn_eth2dn(msg->id));
  324. neigh = __neigh_lookup(&dn_neigh_table, &src, skb->dev, 1);
  325. dn = (struct dn_neigh *)neigh;
  326. if (neigh) {
  327. write_lock(&neigh->lock);
  328. neigh->used = jiffies;
  329. dn_db = (struct dn_dev *)neigh->dev->dn_ptr;
  330. if (!(neigh->nud_state & NUD_PERMANENT)) {
  331. neigh->updated = jiffies;
  332. if (neigh->dev->type == ARPHRD_ETHER)
  333. memcpy(neigh->ha, &skb->mac.ethernet->h_source, ETH_ALEN);
  334. dn->blksize  = dn_ntohs(msg->blksize);
  335. dn->priority = msg->priority;
  336. dn->flags &= ~DN_NDFLAG_P3;
  337. switch(msg->iinfo & DN_RT_INFO_TYPE) {
  338. case DN_RT_INFO_L1RT:
  339. dn->flags &=~DN_NDFLAG_R2;
  340. dn->flags |= DN_NDFLAG_R1;
  341. break;
  342. case DN_RT_INFO_L2RT:
  343. dn->flags |= DN_NDFLAG_R2;
  344. }
  345. }
  346. if (!dn_db->router) {
  347. dn_db->router = neigh_clone(neigh);
  348. } else {
  349. if (msg->priority > ((struct dn_neigh *)dn_db->router)->priority)
  350. neigh_release(xchg(&dn_db->router, neigh_clone(neigh)));
  351. }
  352. write_unlock(&neigh->lock);
  353. neigh_release(neigh);
  354. }
  355. kfree_skb(skb);
  356. return 0;
  357. }
  358. /*
  359.  * Endnode hello message received
  360.  */
  361. int dn_neigh_endnode_hello(struct sk_buff *skb)
  362. {
  363. struct endnode_hello_message *msg = (struct endnode_hello_message *)skb->data;
  364. struct neighbour *neigh;
  365. struct dn_neigh *dn;
  366. dn_address src;
  367. src = dn_htons(dn_eth2dn(msg->id));
  368. neigh = __neigh_lookup(&dn_neigh_table, &src, skb->dev, 1);
  369. dn = (struct dn_neigh *)neigh;
  370. if (neigh) {
  371. write_lock(&neigh->lock);
  372. neigh->used = jiffies;
  373. if (!(neigh->nud_state & NUD_PERMANENT)) {
  374. neigh->updated = jiffies;
  375. if (neigh->dev->type == ARPHRD_ETHER)
  376. memcpy(neigh->ha, &skb->mac.ethernet->h_source, ETH_ALEN);
  377. dn->flags   &= ~(DN_NDFLAG_R1 | DN_NDFLAG_R2);
  378. dn->blksize  = dn_ntohs(msg->blksize);
  379. dn->priority = 0;
  380. }
  381. write_unlock(&neigh->lock);
  382. neigh_release(neigh);
  383. }
  384. kfree_skb(skb);
  385. return 0;
  386. }
  387. #ifdef CONFIG_DECNET_ROUTER
  388. static char *dn_find_slot(char *base, int max, int priority)
  389. {
  390. int i;
  391. unsigned char *min = NULL;
  392. base += 6; /* skip first id */
  393. for(i = 0; i < max; i++) {
  394. if (!min || (*base < *min))
  395. min = base;
  396. base += 7; /* find next priority */
  397. }
  398. if (!min)
  399. return NULL;
  400. return (*min < priority) ? (min - 6) : NULL;
  401. }
  402. int dn_neigh_elist(struct net_device *dev, unsigned char *ptr, int n)
  403. {
  404. int t = 0;
  405. int i;
  406. struct neighbour *neigh;
  407. struct dn_neigh *dn;
  408. struct neigh_table *tbl = &dn_neigh_table;
  409. unsigned char *rs = ptr;
  410. struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
  411. read_lock_bh(&tbl->lock);
  412. for(i = 0; i < NEIGH_HASHMASK; i++) {
  413. for(neigh = tbl->hash_buckets[i]; neigh != NULL; neigh = neigh->next) {
  414. if (neigh->dev != dev)
  415. continue;
  416. dn = (struct dn_neigh *)neigh;
  417. if (!(dn->flags & (DN_NDFLAG_R1|DN_NDFLAG_R2)))
  418. continue;
  419. if (dn_db->parms.forwarding == 1 && (dn->flags & DN_NDFLAG_R2))
  420. continue;
  421. if (t == n)
  422. rs = dn_find_slot(ptr, n, dn->priority);
  423. else
  424. t++;
  425. if (rs == NULL)
  426. continue;
  427. dn_dn2eth(rs, dn->addr);
  428. rs += 6;
  429. *rs = neigh->nud_state & NUD_CONNECTED ? 0x80 : 0x0;
  430. *rs |= dn->priority;
  431. rs++;
  432. }
  433. }
  434. read_unlock_bh(&tbl->lock);
  435. return t;
  436. }
  437. #endif /* CONFIG_DECNET_ROUTER */
  438. #ifdef CONFIG_PROC_FS
  439. static int dn_neigh_get_info(char *buffer, char **start, off_t offset, int length)
  440. {
  441.         int len     = 0;
  442.         off_t pos   = 0;
  443.         off_t begin = 0;
  444. struct neighbour *n;
  445. int i;
  446. char buf[DN_ASCBUF_LEN];
  447. len += sprintf(buffer + len, "Addr    Flags State Use Blksize Devn");
  448. for(i=0;i <= NEIGH_HASHMASK; i++) {
  449. read_lock_bh(&dn_neigh_table.lock);
  450. n = dn_neigh_table.hash_buckets[i];
  451. for(; n != NULL; n = n->next) {
  452. struct dn_neigh *dn = (struct dn_neigh *)n;
  453. read_lock(&n->lock);
  454. len += sprintf(buffer+len, "%-7s %s%s%s   %02x    %02d  %07ld %-8sn",
  455. dn_addr2asc(dn_ntohs(dn->addr), buf),
  456. (dn->flags&DN_NDFLAG_R1) ? "1" : "-",
  457. (dn->flags&DN_NDFLAG_R2) ? "2" : "-",
  458. (dn->flags&DN_NDFLAG_P3) ? "3" : "-",
  459. dn->n.nud_state,
  460. atomic_read(&dn->n.refcnt),
  461. dn->blksize,
  462. (dn->n.dev) ? dn->n.dev->name : "?");
  463. read_unlock(&n->lock);
  464. pos = begin + len;
  465.                  if (pos < offset) {
  466.                          len = 0;
  467.                          begin = pos;
  468.                  }
  469.                  if (pos > offset + length) {
  470. read_unlock_bh(&dn_neigh_table.lock);
  471.                         goto done;
  472. }
  473. }
  474. read_unlock_bh(&dn_neigh_table.lock);
  475. }
  476. done:
  477.         *start = buffer + (offset - begin);
  478.         len   -= offset - begin;
  479.         if (len > length) len = length;
  480.         return len;
  481. }
  482. #endif
  483. void __init dn_neigh_init(void)
  484. {
  485. neigh_table_init(&dn_neigh_table);
  486. #ifdef CONFIG_PROC_FS
  487. proc_net_create("decnet_neigh",0,dn_neigh_get_info);
  488. #endif /* CONFIG_PROC_FS */
  489. }
  490. void __exit dn_neigh_cleanup(void)
  491. {
  492. proc_net_remove("decnet_neigh");
  493. neigh_table_clear(&dn_neigh_table);
  494. }