dn_neigh.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DECnet An implementation of the DECnet protocol suite for the LINUX
  4. * operating system. DECnet is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * DECnet Neighbour Functions (Adjacency Database and
  8. * On-Ethernet Cache)
  9. *
  10. * Author: Steve Whitehouse <SteveW@ACM.org>
  11. *
  12. *
  13. * Changes:
  14. * Steve Whitehouse : Fixed router listing routine
  15. * Steve Whitehouse : Added error_report functions
  16. * Steve Whitehouse : Added default router detection
  17. * Steve Whitehouse : Hop counts in outgoing messages
  18. * Steve Whitehouse : Fixed src/dst in outgoing messages so
  19. * forwarding now stands a good chance of
  20. * working.
  21. * Steve Whitehouse : Fixed neighbour states (for now anyway).
  22. * Steve Whitehouse : Made error_report functions dummies. This
  23. * is not the right place to return skbs.
  24. * Steve Whitehouse : Convert to seq_file
  25. *
  26. */
  27. #include <linux/net.h>
  28. #include <linux/module.h>
  29. #include <linux/socket.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/slab.h>
  32. #include <linux/if_ether.h>
  33. #include <linux/init.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/string.h>
  36. #include <linux/netfilter_decnet.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/rcupdate.h>
  40. #include <linux/jhash.h>
  41. #include <linux/atomic.h>
  42. #include <net/net_namespace.h>
  43. #include <net/neighbour.h>
  44. #include <net/dst.h>
  45. #include <net/flow.h>
  46. #include <net/dn.h>
  47. #include <net/dn_dev.h>
  48. #include <net/dn_neigh.h>
  49. #include <net/dn_route.h>
  50. static int dn_neigh_construct(struct neighbour *);
  51. static void dn_neigh_error_report(struct neighbour *, struct sk_buff *);
  52. static int dn_neigh_output(struct neighbour *neigh, struct sk_buff *skb);
  53. /*
  54. * Operations for adding the link layer header.
  55. */
  56. static const struct neigh_ops dn_neigh_ops = {
  57. .family = AF_DECnet,
  58. .error_report = dn_neigh_error_report,
  59. .output = dn_neigh_output,
  60. .connected_output = dn_neigh_output,
  61. };
  62. static u32 dn_neigh_hash(const void *pkey,
  63. const struct net_device *dev,
  64. __u32 *hash_rnd)
  65. {
  66. return jhash_2words(*(__u16 *)pkey, 0, hash_rnd[0]);
  67. }
  68. static bool dn_key_eq(const struct neighbour *neigh, const void *pkey)
  69. {
  70. return neigh_key_eq16(neigh, pkey);
  71. }
  72. struct neigh_table dn_neigh_table = {
  73. .family = PF_DECnet,
  74. .entry_size = NEIGH_ENTRY_SIZE(sizeof(struct dn_neigh)),
  75. .key_len = sizeof(__le16),
  76. .protocol = cpu_to_be16(ETH_P_DNA_RT),
  77. .hash = dn_neigh_hash,
  78. .key_eq = dn_key_eq,
  79. .constructor = dn_neigh_construct,
  80. .id = "dn_neigh_cache",
  81. .parms ={
  82. .tbl = &dn_neigh_table,
  83. .reachable_time = 30 * HZ,
  84. .data = {
  85. [NEIGH_VAR_MCAST_PROBES] = 0,
  86. [NEIGH_VAR_UCAST_PROBES] = 0,
  87. [NEIGH_VAR_APP_PROBES] = 0,
  88. [NEIGH_VAR_RETRANS_TIME] = 1 * HZ,
  89. [NEIGH_VAR_BASE_REACHABLE_TIME] = 30 * HZ,
  90. [NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
  91. [NEIGH_VAR_GC_STALETIME] = 60 * HZ,
  92. [NEIGH_VAR_QUEUE_LEN_BYTES] = SK_WMEM_MAX,
  93. [NEIGH_VAR_PROXY_QLEN] = 0,
  94. [NEIGH_VAR_ANYCAST_DELAY] = 0,
  95. [NEIGH_VAR_PROXY_DELAY] = 0,
  96. [NEIGH_VAR_LOCKTIME] = 1 * HZ,
  97. },
  98. },
  99. .gc_interval = 30 * HZ,
  100. .gc_thresh1 = 128,
  101. .gc_thresh2 = 512,
  102. .gc_thresh3 = 1024,
  103. };
  104. static int dn_neigh_construct(struct neighbour *neigh)
  105. {
  106. struct net_device *dev = neigh->dev;
  107. struct dn_neigh *dn = container_of(neigh, struct dn_neigh, n);
  108. struct dn_dev *dn_db;
  109. struct neigh_parms *parms;
  110. rcu_read_lock();
  111. dn_db = rcu_dereference(dev->dn_ptr);
  112. if (dn_db == NULL) {
  113. rcu_read_unlock();
  114. return -EINVAL;
  115. }
  116. parms = dn_db->neigh_parms;
  117. if (!parms) {
  118. rcu_read_unlock();
  119. return -EINVAL;
  120. }
  121. __neigh_parms_put(neigh->parms);
  122. neigh->parms = neigh_parms_clone(parms);
  123. rcu_read_unlock();
  124. neigh->ops = &dn_neigh_ops;
  125. neigh->nud_state = NUD_NOARP;
  126. neigh->output = neigh->ops->connected_output;
  127. if ((dev->type == ARPHRD_IPGRE) || (dev->flags & IFF_POINTOPOINT))
  128. memcpy(neigh->ha, dev->broadcast, dev->addr_len);
  129. else if ((dev->type == ARPHRD_ETHER) || (dev->type == ARPHRD_LOOPBACK))
  130. dn_dn2eth(neigh->ha, dn->addr);
  131. else {
  132. net_dbg_ratelimited("Trying to create neigh for hw %d\n",
  133. dev->type);
  134. return -EINVAL;
  135. }
  136. /*
  137. * Make an estimate of the remote block size by assuming that its
  138. * two less then the device mtu, which it true for ethernet (and
  139. * other things which support long format headers) since there is
  140. * an extra length field (of 16 bits) which isn't part of the
  141. * ethernet headers and which the DECnet specs won't admit is part
  142. * of the DECnet routing headers either.
  143. *
  144. * If we over estimate here its no big deal, the NSP negotiations
  145. * will prevent us from sending packets which are too large for the
  146. * remote node to handle. In any case this figure is normally updated
  147. * by a hello message in most cases.
  148. */
  149. dn->blksize = dev->mtu - 2;
  150. return 0;
  151. }
  152. static void dn_neigh_error_report(struct neighbour *neigh, struct sk_buff *skb)
  153. {
  154. printk(KERN_DEBUG "dn_neigh_error_report: called\n");
  155. kfree_skb(skb);
  156. }
  157. static int dn_neigh_output(struct neighbour *neigh, struct sk_buff *skb)
  158. {
  159. struct dst_entry *dst = skb_dst(skb);
  160. struct dn_route *rt = (struct dn_route *)dst;
  161. struct net_device *dev = neigh->dev;
  162. char mac_addr[ETH_ALEN];
  163. unsigned int seq;
  164. int err;
  165. dn_dn2eth(mac_addr, rt->rt_local_src);
  166. do {
  167. seq = read_seqbegin(&neigh->ha_lock);
  168. err = dev_hard_header(skb, dev, ntohs(skb->protocol),
  169. neigh->ha, mac_addr, skb->len);
  170. } while (read_seqretry(&neigh->ha_lock, seq));
  171. if (err >= 0)
  172. err = dev_queue_xmit(skb);
  173. else {
  174. kfree_skb(skb);
  175. err = -EINVAL;
  176. }
  177. return err;
  178. }
  179. static int dn_neigh_output_packet(struct net *net, struct sock *sk, struct sk_buff *skb)
  180. {
  181. struct dst_entry *dst = skb_dst(skb);
  182. struct dn_route *rt = (struct dn_route *)dst;
  183. struct neighbour *neigh = rt->n;
  184. return neigh->output(neigh, skb);
  185. }
  186. /*
  187. * For talking to broadcast devices: Ethernet & PPP
  188. */
  189. static int dn_long_output(struct neighbour *neigh, struct sock *sk,
  190. struct sk_buff *skb)
  191. {
  192. struct net_device *dev = neigh->dev;
  193. int headroom = dev->hard_header_len + sizeof(struct dn_long_packet) + 3;
  194. unsigned char *data;
  195. struct dn_long_packet *lp;
  196. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  197. if (skb_headroom(skb) < headroom) {
  198. struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
  199. if (skb2 == NULL) {
  200. net_crit_ratelimited("dn_long_output: no memory\n");
  201. kfree_skb(skb);
  202. return -ENOBUFS;
  203. }
  204. consume_skb(skb);
  205. skb = skb2;
  206. net_info_ratelimited("dn_long_output: Increasing headroom\n");
  207. }
  208. data = skb_push(skb, sizeof(struct dn_long_packet) + 3);
  209. lp = (struct dn_long_packet *)(data+3);
  210. *((__le16 *)data) = cpu_to_le16(skb->len - 2);
  211. *(data + 2) = 1 | DN_RT_F_PF; /* Padding */
  212. lp->msgflg = DN_RT_PKT_LONG|(cb->rt_flags&(DN_RT_F_IE|DN_RT_F_RQR|DN_RT_F_RTS));
  213. lp->d_area = lp->d_subarea = 0;
  214. dn_dn2eth(lp->d_id, cb->dst);
  215. lp->s_area = lp->s_subarea = 0;
  216. dn_dn2eth(lp->s_id, cb->src);
  217. lp->nl2 = 0;
  218. lp->visit_ct = cb->hops & 0x3f;
  219. lp->s_class = 0;
  220. lp->pt = 0;
  221. skb_reset_network_header(skb);
  222. return NF_HOOK(NFPROTO_DECNET, NF_DN_POST_ROUTING,
  223. &init_net, sk, skb, NULL, neigh->dev,
  224. dn_neigh_output_packet);
  225. }
  226. /*
  227. * For talking to pointopoint and multidrop devices: DDCMP and X.25
  228. */
  229. static int dn_short_output(struct neighbour *neigh, struct sock *sk,
  230. struct sk_buff *skb)
  231. {
  232. struct net_device *dev = neigh->dev;
  233. int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
  234. struct dn_short_packet *sp;
  235. unsigned char *data;
  236. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  237. if (skb_headroom(skb) < headroom) {
  238. struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
  239. if (skb2 == NULL) {
  240. net_crit_ratelimited("dn_short_output: no memory\n");
  241. kfree_skb(skb);
  242. return -ENOBUFS;
  243. }
  244. consume_skb(skb);
  245. skb = skb2;
  246. net_info_ratelimited("dn_short_output: Increasing headroom\n");
  247. }
  248. data = skb_push(skb, sizeof(struct dn_short_packet) + 2);
  249. *((__le16 *)data) = cpu_to_le16(skb->len - 2);
  250. sp = (struct dn_short_packet *)(data+2);
  251. sp->msgflg = DN_RT_PKT_SHORT|(cb->rt_flags&(DN_RT_F_RQR|DN_RT_F_RTS));
  252. sp->dstnode = cb->dst;
  253. sp->srcnode = cb->src;
  254. sp->forward = cb->hops & 0x3f;
  255. skb_reset_network_header(skb);
  256. return NF_HOOK(NFPROTO_DECNET, NF_DN_POST_ROUTING,
  257. &init_net, sk, skb, NULL, neigh->dev,
  258. dn_neigh_output_packet);
  259. }
  260. /*
  261. * For talking to DECnet phase III nodes
  262. * Phase 3 output is the same as short output, execpt that
  263. * it clears the area bits before transmission.
  264. */
  265. static int dn_phase3_output(struct neighbour *neigh, struct sock *sk,
  266. struct sk_buff *skb)
  267. {
  268. struct net_device *dev = neigh->dev;
  269. int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
  270. struct dn_short_packet *sp;
  271. unsigned char *data;
  272. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  273. if (skb_headroom(skb) < headroom) {
  274. struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
  275. if (skb2 == NULL) {
  276. net_crit_ratelimited("dn_phase3_output: no memory\n");
  277. kfree_skb(skb);
  278. return -ENOBUFS;
  279. }
  280. consume_skb(skb);
  281. skb = skb2;
  282. net_info_ratelimited("dn_phase3_output: Increasing headroom\n");
  283. }
  284. data = skb_push(skb, sizeof(struct dn_short_packet) + 2);
  285. *((__le16 *)data) = cpu_to_le16(skb->len - 2);
  286. sp = (struct dn_short_packet *)(data + 2);
  287. sp->msgflg = DN_RT_PKT_SHORT|(cb->rt_flags&(DN_RT_F_RQR|DN_RT_F_RTS));
  288. sp->dstnode = cb->dst & cpu_to_le16(0x03ff);
  289. sp->srcnode = cb->src & cpu_to_le16(0x03ff);
  290. sp->forward = cb->hops & 0x3f;
  291. skb_reset_network_header(skb);
  292. return NF_HOOK(NFPROTO_DECNET, NF_DN_POST_ROUTING,
  293. &init_net, sk, skb, NULL, neigh->dev,
  294. dn_neigh_output_packet);
  295. }
  296. int dn_to_neigh_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  297. {
  298. struct dst_entry *dst = skb_dst(skb);
  299. struct dn_route *rt = (struct dn_route *) dst;
  300. struct neighbour *neigh = rt->n;
  301. struct dn_neigh *dn = container_of(neigh, struct dn_neigh, n);
  302. struct dn_dev *dn_db;
  303. bool use_long;
  304. rcu_read_lock();
  305. dn_db = rcu_dereference(neigh->dev->dn_ptr);
  306. if (dn_db == NULL) {
  307. rcu_read_unlock();
  308. return -EINVAL;
  309. }
  310. use_long = dn_db->use_long;
  311. rcu_read_unlock();
  312. if (dn->flags & DN_NDFLAG_P3)
  313. return dn_phase3_output(neigh, sk, skb);
  314. if (use_long)
  315. return dn_long_output(neigh, sk, skb);
  316. else
  317. return dn_short_output(neigh, sk, skb);
  318. }
  319. /*
  320. * Unfortunately, the neighbour code uses the device in its hash
  321. * function, so we don't get any advantage from it. This function
  322. * basically does a neigh_lookup(), but without comparing the device
  323. * field. This is required for the On-Ethernet cache
  324. */
  325. /*
  326. * Pointopoint link receives a hello message
  327. */
  328. void dn_neigh_pointopoint_hello(struct sk_buff *skb)
  329. {
  330. kfree_skb(skb);
  331. }
  332. /*
  333. * Ethernet router hello message received
  334. */
  335. int dn_neigh_router_hello(struct net *net, struct sock *sk, struct sk_buff *skb)
  336. {
  337. struct rtnode_hello_message *msg = (struct rtnode_hello_message *)skb->data;
  338. struct neighbour *neigh;
  339. struct dn_neigh *dn;
  340. struct dn_dev *dn_db;
  341. __le16 src;
  342. src = dn_eth2dn(msg->id);
  343. neigh = __neigh_lookup(&dn_neigh_table, &src, skb->dev, 1);
  344. dn = container_of(neigh, struct dn_neigh, n);
  345. if (neigh) {
  346. write_lock(&neigh->lock);
  347. neigh->used = jiffies;
  348. dn_db = rcu_dereference(neigh->dev->dn_ptr);
  349. if (!(neigh->nud_state & NUD_PERMANENT)) {
  350. neigh->updated = jiffies;
  351. if (neigh->dev->type == ARPHRD_ETHER)
  352. memcpy(neigh->ha, &eth_hdr(skb)->h_source, ETH_ALEN);
  353. dn->blksize = le16_to_cpu(msg->blksize);
  354. dn->priority = msg->priority;
  355. dn->flags &= ~DN_NDFLAG_P3;
  356. switch (msg->iinfo & DN_RT_INFO_TYPE) {
  357. case DN_RT_INFO_L1RT:
  358. dn->flags &=~DN_NDFLAG_R2;
  359. dn->flags |= DN_NDFLAG_R1;
  360. break;
  361. case DN_RT_INFO_L2RT:
  362. dn->flags |= DN_NDFLAG_R2;
  363. }
  364. }
  365. /* Only use routers in our area */
  366. if ((le16_to_cpu(src)>>10) == (le16_to_cpu((decnet_address))>>10)) {
  367. if (!dn_db->router) {
  368. dn_db->router = neigh_clone(neigh);
  369. } else {
  370. if (msg->priority > ((struct dn_neigh *)dn_db->router)->priority)
  371. neigh_release(xchg(&dn_db->router, neigh_clone(neigh)));
  372. }
  373. }
  374. write_unlock(&neigh->lock);
  375. neigh_release(neigh);
  376. }
  377. kfree_skb(skb);
  378. return 0;
  379. }
  380. /*
  381. * Endnode hello message received
  382. */
  383. int dn_neigh_endnode_hello(struct net *net, struct sock *sk, struct sk_buff *skb)
  384. {
  385. struct endnode_hello_message *msg = (struct endnode_hello_message *)skb->data;
  386. struct neighbour *neigh;
  387. struct dn_neigh *dn;
  388. __le16 src;
  389. src = dn_eth2dn(msg->id);
  390. neigh = __neigh_lookup(&dn_neigh_table, &src, skb->dev, 1);
  391. dn = container_of(neigh, struct dn_neigh, n);
  392. if (neigh) {
  393. write_lock(&neigh->lock);
  394. neigh->used = jiffies;
  395. if (!(neigh->nud_state & NUD_PERMANENT)) {
  396. neigh->updated = jiffies;
  397. if (neigh->dev->type == ARPHRD_ETHER)
  398. memcpy(neigh->ha, &eth_hdr(skb)->h_source, ETH_ALEN);
  399. dn->flags &= ~(DN_NDFLAG_R1 | DN_NDFLAG_R2);
  400. dn->blksize = le16_to_cpu(msg->blksize);
  401. dn->priority = 0;
  402. }
  403. write_unlock(&neigh->lock);
  404. neigh_release(neigh);
  405. }
  406. kfree_skb(skb);
  407. return 0;
  408. }
  409. static char *dn_find_slot(char *base, int max, int priority)
  410. {
  411. int i;
  412. unsigned char *min = NULL;
  413. base += 6; /* skip first id */
  414. for(i = 0; i < max; i++) {
  415. if (!min || (*base < *min))
  416. min = base;
  417. base += 7; /* find next priority */
  418. }
  419. if (!min)
  420. return NULL;
  421. return (*min < priority) ? (min - 6) : NULL;
  422. }
  423. struct elist_cb_state {
  424. struct net_device *dev;
  425. unsigned char *ptr;
  426. unsigned char *rs;
  427. int t, n;
  428. };
  429. static void neigh_elist_cb(struct neighbour *neigh, void *_info)
  430. {
  431. struct elist_cb_state *s = _info;
  432. struct dn_neigh *dn;
  433. if (neigh->dev != s->dev)
  434. return;
  435. dn = container_of(neigh, struct dn_neigh, n);
  436. if (!(dn->flags & (DN_NDFLAG_R1|DN_NDFLAG_R2)))
  437. return;
  438. if (s->t == s->n)
  439. s->rs = dn_find_slot(s->ptr, s->n, dn->priority);
  440. else
  441. s->t++;
  442. if (s->rs == NULL)
  443. return;
  444. dn_dn2eth(s->rs, dn->addr);
  445. s->rs += 6;
  446. *(s->rs) = neigh->nud_state & NUD_CONNECTED ? 0x80 : 0x0;
  447. *(s->rs) |= dn->priority;
  448. s->rs++;
  449. }
  450. int dn_neigh_elist(struct net_device *dev, unsigned char *ptr, int n)
  451. {
  452. struct elist_cb_state state;
  453. state.dev = dev;
  454. state.t = 0;
  455. state.n = n;
  456. state.ptr = ptr;
  457. state.rs = ptr;
  458. neigh_for_each(&dn_neigh_table, neigh_elist_cb, &state);
  459. return state.t;
  460. }
  461. #ifdef CONFIG_PROC_FS
  462. static inline void dn_neigh_format_entry(struct seq_file *seq,
  463. struct neighbour *n)
  464. {
  465. struct dn_neigh *dn = container_of(n, struct dn_neigh, n);
  466. char buf[DN_ASCBUF_LEN];
  467. read_lock(&n->lock);
  468. seq_printf(seq, "%-7s %s%s%s %02x %02d %07ld %-8s\n",
  469. dn_addr2asc(le16_to_cpu(dn->addr), buf),
  470. (dn->flags&DN_NDFLAG_R1) ? "1" : "-",
  471. (dn->flags&DN_NDFLAG_R2) ? "2" : "-",
  472. (dn->flags&DN_NDFLAG_P3) ? "3" : "-",
  473. dn->n.nud_state,
  474. refcount_read(&dn->n.refcnt),
  475. dn->blksize,
  476. (dn->n.dev) ? dn->n.dev->name : "?");
  477. read_unlock(&n->lock);
  478. }
  479. static int dn_neigh_seq_show(struct seq_file *seq, void *v)
  480. {
  481. if (v == SEQ_START_TOKEN) {
  482. seq_puts(seq, "Addr Flags State Use Blksize Dev\n");
  483. } else {
  484. dn_neigh_format_entry(seq, v);
  485. }
  486. return 0;
  487. }
  488. static void *dn_neigh_seq_start(struct seq_file *seq, loff_t *pos)
  489. {
  490. return neigh_seq_start(seq, pos, &dn_neigh_table,
  491. NEIGH_SEQ_NEIGH_ONLY);
  492. }
  493. static const struct seq_operations dn_neigh_seq_ops = {
  494. .start = dn_neigh_seq_start,
  495. .next = neigh_seq_next,
  496. .stop = neigh_seq_stop,
  497. .show = dn_neigh_seq_show,
  498. };
  499. #endif
  500. void __init dn_neigh_init(void)
  501. {
  502. neigh_table_init(NEIGH_DN_TABLE, &dn_neigh_table);
  503. proc_create_net("decnet_neigh", 0444, init_net.proc_net,
  504. &dn_neigh_seq_ops, sizeof(struct neigh_seq_state));
  505. }
  506. void __exit dn_neigh_cleanup(void)
  507. {
  508. remove_proc_entry("decnet_neigh", init_net.proc_net);
  509. neigh_table_clear(NEIGH_DN_TABLE, &dn_neigh_table);
  510. }