dn_rules.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 Routing Forwarding Information Base (Rules)
  8. *
  9. * Author: Steve Whitehouse <SteveW@ACM.org>
  10. * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
  11. *
  12. *
  13. * Changes:
  14. * Steve Whitehouse <steve@chygwyn.com>
  15. * Updated for Thomas Graf's generic rules
  16. *
  17. */
  18. #include <linux/net.h>
  19. #include <linux/init.h>
  20. #include <linux/netlink.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/list.h>
  25. #include <linux/rcupdate.h>
  26. #include <linux/export.h>
  27. #include <net/neighbour.h>
  28. #include <net/dst.h>
  29. #include <net/flow.h>
  30. #include <net/fib_rules.h>
  31. #include <net/dn.h>
  32. #include <net/dn_fib.h>
  33. #include <net/dn_neigh.h>
  34. #include <net/dn_dev.h>
  35. #include <net/dn_route.h>
  36. static struct fib_rules_ops *dn_fib_rules_ops;
  37. struct dn_fib_rule
  38. {
  39. struct fib_rule common;
  40. unsigned char dst_len;
  41. unsigned char src_len;
  42. __le16 src;
  43. __le16 srcmask;
  44. __le16 dst;
  45. __le16 dstmask;
  46. __le16 srcmap;
  47. u8 flags;
  48. };
  49. int dn_fib_lookup(struct flowidn *flp, struct dn_fib_res *res)
  50. {
  51. struct fib_lookup_arg arg = {
  52. .result = res,
  53. };
  54. int err;
  55. err = fib_rules_lookup(dn_fib_rules_ops,
  56. flowidn_to_flowi(flp), 0, &arg);
  57. res->r = arg.rule;
  58. return err;
  59. }
  60. static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
  61. int flags, struct fib_lookup_arg *arg)
  62. {
  63. struct flowidn *fld = &flp->u.dn;
  64. int err = -EAGAIN;
  65. struct dn_fib_table *tbl;
  66. switch(rule->action) {
  67. case FR_ACT_TO_TBL:
  68. break;
  69. case FR_ACT_UNREACHABLE:
  70. err = -ENETUNREACH;
  71. goto errout;
  72. case FR_ACT_PROHIBIT:
  73. err = -EACCES;
  74. goto errout;
  75. case FR_ACT_BLACKHOLE:
  76. default:
  77. err = -EINVAL;
  78. goto errout;
  79. }
  80. tbl = dn_fib_get_table(rule->table, 0);
  81. if (tbl == NULL)
  82. goto errout;
  83. err = tbl->lookup(tbl, fld, (struct dn_fib_res *)arg->result);
  84. if (err > 0)
  85. err = -EAGAIN;
  86. errout:
  87. return err;
  88. }
  89. static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
  90. FRA_GENERIC_POLICY,
  91. };
  92. static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  93. {
  94. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  95. struct flowidn *fld = &fl->u.dn;
  96. __le16 daddr = fld->daddr;
  97. __le16 saddr = fld->saddr;
  98. if (((saddr ^ r->src) & r->srcmask) ||
  99. ((daddr ^ r->dst) & r->dstmask))
  100. return 0;
  101. return 1;
  102. }
  103. static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  104. struct fib_rule_hdr *frh,
  105. struct nlattr **tb)
  106. {
  107. int err = -EINVAL;
  108. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  109. if (frh->tos)
  110. goto errout;
  111. if (rule->table == RT_TABLE_UNSPEC) {
  112. if (rule->action == FR_ACT_TO_TBL) {
  113. struct dn_fib_table *table;
  114. table = dn_fib_empty_table();
  115. if (table == NULL) {
  116. err = -ENOBUFS;
  117. goto errout;
  118. }
  119. rule->table = table->n;
  120. }
  121. }
  122. if (frh->src_len)
  123. r->src = nla_get_le16(tb[FRA_SRC]);
  124. if (frh->dst_len)
  125. r->dst = nla_get_le16(tb[FRA_DST]);
  126. r->src_len = frh->src_len;
  127. r->srcmask = dnet_make_mask(r->src_len);
  128. r->dst_len = frh->dst_len;
  129. r->dstmask = dnet_make_mask(r->dst_len);
  130. err = 0;
  131. errout:
  132. return err;
  133. }
  134. static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  135. struct nlattr **tb)
  136. {
  137. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  138. if (frh->src_len && (r->src_len != frh->src_len))
  139. return 0;
  140. if (frh->dst_len && (r->dst_len != frh->dst_len))
  141. return 0;
  142. if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
  143. return 0;
  144. if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
  145. return 0;
  146. return 1;
  147. }
  148. unsigned int dnet_addr_type(__le16 addr)
  149. {
  150. struct flowidn fld = { .daddr = addr };
  151. struct dn_fib_res res;
  152. unsigned int ret = RTN_UNICAST;
  153. struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
  154. res.r = NULL;
  155. if (tb) {
  156. if (!tb->lookup(tb, &fld, &res)) {
  157. ret = res.type;
  158. dn_fib_res_put(&res);
  159. }
  160. }
  161. return ret;
  162. }
  163. static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  164. struct fib_rule_hdr *frh)
  165. {
  166. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  167. frh->dst_len = r->dst_len;
  168. frh->src_len = r->src_len;
  169. frh->tos = 0;
  170. if ((r->dst_len &&
  171. nla_put_le16(skb, FRA_DST, r->dst)) ||
  172. (r->src_len &&
  173. nla_put_le16(skb, FRA_SRC, r->src)))
  174. goto nla_put_failure;
  175. return 0;
  176. nla_put_failure:
  177. return -ENOBUFS;
  178. }
  179. static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
  180. {
  181. dn_rt_cache_flush(-1);
  182. }
  183. static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template = {
  184. .family = AF_DECnet,
  185. .rule_size = sizeof(struct dn_fib_rule),
  186. .addr_size = sizeof(u16),
  187. .action = dn_fib_rule_action,
  188. .match = dn_fib_rule_match,
  189. .configure = dn_fib_rule_configure,
  190. .compare = dn_fib_rule_compare,
  191. .fill = dn_fib_rule_fill,
  192. .flush_cache = dn_fib_rule_flush_cache,
  193. .nlgroup = RTNLGRP_DECnet_RULE,
  194. .policy = dn_fib_rule_policy,
  195. .owner = THIS_MODULE,
  196. .fro_net = &init_net,
  197. };
  198. void __init dn_fib_rules_init(void)
  199. {
  200. dn_fib_rules_ops =
  201. fib_rules_register(&dn_fib_rules_ops_template, &init_net);
  202. BUG_ON(IS_ERR(dn_fib_rules_ops));
  203. BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff,
  204. RT_TABLE_MAIN, 0));
  205. }
  206. void __exit dn_fib_rules_cleanup(void)
  207. {
  208. rtnl_lock();
  209. fib_rules_unregister(dn_fib_rules_ops);
  210. rtnl_unlock();
  211. rcu_barrier();
  212. }