dn_rules.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. struct netlink_ext_ack *extack)
  107. {
  108. int err = -EINVAL;
  109. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  110. if (frh->tos) {
  111. NL_SET_ERR_MSG(extack, "Invalid tos value");
  112. goto errout;
  113. }
  114. if (rule->table == RT_TABLE_UNSPEC) {
  115. if (rule->action == FR_ACT_TO_TBL) {
  116. struct dn_fib_table *table;
  117. table = dn_fib_empty_table();
  118. if (table == NULL) {
  119. err = -ENOBUFS;
  120. goto errout;
  121. }
  122. rule->table = table->n;
  123. }
  124. }
  125. if (frh->src_len)
  126. r->src = nla_get_le16(tb[FRA_SRC]);
  127. if (frh->dst_len)
  128. r->dst = nla_get_le16(tb[FRA_DST]);
  129. r->src_len = frh->src_len;
  130. r->srcmask = dnet_make_mask(r->src_len);
  131. r->dst_len = frh->dst_len;
  132. r->dstmask = dnet_make_mask(r->dst_len);
  133. err = 0;
  134. errout:
  135. return err;
  136. }
  137. static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  138. struct nlattr **tb)
  139. {
  140. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  141. if (frh->src_len && (r->src_len != frh->src_len))
  142. return 0;
  143. if (frh->dst_len && (r->dst_len != frh->dst_len))
  144. return 0;
  145. if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
  146. return 0;
  147. if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
  148. return 0;
  149. return 1;
  150. }
  151. unsigned int dnet_addr_type(__le16 addr)
  152. {
  153. struct flowidn fld = { .daddr = addr };
  154. struct dn_fib_res res;
  155. unsigned int ret = RTN_UNICAST;
  156. struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
  157. res.r = NULL;
  158. if (tb) {
  159. if (!tb->lookup(tb, &fld, &res)) {
  160. ret = res.type;
  161. dn_fib_res_put(&res);
  162. }
  163. }
  164. return ret;
  165. }
  166. static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  167. struct fib_rule_hdr *frh)
  168. {
  169. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  170. frh->dst_len = r->dst_len;
  171. frh->src_len = r->src_len;
  172. frh->tos = 0;
  173. if ((r->dst_len &&
  174. nla_put_le16(skb, FRA_DST, r->dst)) ||
  175. (r->src_len &&
  176. nla_put_le16(skb, FRA_SRC, r->src)))
  177. goto nla_put_failure;
  178. return 0;
  179. nla_put_failure:
  180. return -ENOBUFS;
  181. }
  182. static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
  183. {
  184. dn_rt_cache_flush(-1);
  185. }
  186. static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template = {
  187. .family = AF_DECnet,
  188. .rule_size = sizeof(struct dn_fib_rule),
  189. .addr_size = sizeof(u16),
  190. .action = dn_fib_rule_action,
  191. .match = dn_fib_rule_match,
  192. .configure = dn_fib_rule_configure,
  193. .compare = dn_fib_rule_compare,
  194. .fill = dn_fib_rule_fill,
  195. .flush_cache = dn_fib_rule_flush_cache,
  196. .nlgroup = RTNLGRP_DECnet_RULE,
  197. .policy = dn_fib_rule_policy,
  198. .owner = THIS_MODULE,
  199. .fro_net = &init_net,
  200. };
  201. void __init dn_fib_rules_init(void)
  202. {
  203. dn_fib_rules_ops =
  204. fib_rules_register(&dn_fib_rules_ops_template, &init_net);
  205. BUG_ON(IS_ERR(dn_fib_rules_ops));
  206. BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff,
  207. RT_TABLE_MAIN, 0));
  208. }
  209. void __exit dn_fib_rules_cleanup(void)
  210. {
  211. rtnl_lock();
  212. fib_rules_unregister(dn_fib_rules_ops);
  213. rtnl_unlock();
  214. rcu_barrier();
  215. }