dn_rules.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 Forwarding Information Base (Rules)
  7. *
  8. * Author: Steve Whitehouse <SteveW@ACM.org>
  9. * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
  10. *
  11. *
  12. * Changes:
  13. * Steve Whitehouse <steve@chygwyn.com>
  14. * Updated for Thomas Graf's generic rules
  15. *
  16. */
  17. #include <linux/net.h>
  18. #include <linux/init.h>
  19. #include <linux/netlink.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/list.h>
  24. #include <linux/rcupdate.h>
  25. #include <net/neighbour.h>
  26. #include <net/dst.h>
  27. #include <net/flow.h>
  28. #include <net/fib_rules.h>
  29. #include <net/dn.h>
  30. #include <net/dn_fib.h>
  31. #include <net/dn_neigh.h>
  32. #include <net/dn_dev.h>
  33. #include <net/dn_route.h>
  34. static struct fib_rules_ops dn_fib_rules_ops;
  35. struct dn_fib_rule
  36. {
  37. struct fib_rule common;
  38. unsigned char dst_len;
  39. unsigned char src_len;
  40. __le16 src;
  41. __le16 srcmask;
  42. __le16 dst;
  43. __le16 dstmask;
  44. __le16 srcmap;
  45. u8 flags;
  46. };
  47. static struct dn_fib_rule default_rule = {
  48. .common = {
  49. .refcnt = ATOMIC_INIT(2),
  50. .pref = 0x7fff,
  51. .table = RT_TABLE_MAIN,
  52. .action = FR_ACT_TO_TBL,
  53. },
  54. };
  55. int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
  56. {
  57. struct fib_lookup_arg arg = {
  58. .result = res,
  59. };
  60. int err;
  61. err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
  62. res->r = arg.rule;
  63. return err;
  64. }
  65. static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
  66. int flags, struct fib_lookup_arg *arg)
  67. {
  68. int err = -EAGAIN;
  69. struct dn_fib_table *tbl;
  70. switch(rule->action) {
  71. case FR_ACT_TO_TBL:
  72. break;
  73. case FR_ACT_UNREACHABLE:
  74. err = -ENETUNREACH;
  75. goto errout;
  76. case FR_ACT_PROHIBIT:
  77. err = -EACCES;
  78. goto errout;
  79. case FR_ACT_BLACKHOLE:
  80. default:
  81. err = -EINVAL;
  82. goto errout;
  83. }
  84. tbl = dn_fib_get_table(rule->table, 0);
  85. if (tbl == NULL)
  86. goto errout;
  87. err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
  88. if (err > 0)
  89. err = -EAGAIN;
  90. errout:
  91. return err;
  92. }
  93. static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
  94. FRA_GENERIC_POLICY,
  95. };
  96. static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  97. {
  98. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  99. __le16 daddr = fl->fld_dst;
  100. __le16 saddr = fl->fld_src;
  101. if (((saddr ^ r->src) & r->srcmask) ||
  102. ((daddr ^ r->dst) & r->dstmask))
  103. return 0;
  104. return 1;
  105. }
  106. static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  107. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  108. struct nlattr **tb)
  109. {
  110. int err = -EINVAL;
  111. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  112. if (frh->tos)
  113. goto errout;
  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 dnet_addr_type(__le16 addr)
  152. {
  153. struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
  154. struct dn_fib_res res;
  155. unsigned 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, &fl, &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 nlmsghdr *nlh, struct fib_rule_hdr *frh)
  168. {
  169. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  170. frh->family = AF_DECnet;
  171. frh->dst_len = r->dst_len;
  172. frh->src_len = r->src_len;
  173. frh->tos = 0;
  174. if (r->dst_len)
  175. NLA_PUT_LE16(skb, FRA_DST, r->dst);
  176. if (r->src_len)
  177. NLA_PUT_LE16(skb, FRA_SRC, r->src);
  178. return 0;
  179. nla_put_failure:
  180. return -ENOBUFS;
  181. }
  182. static u32 dn_fib_rule_default_pref(void)
  183. {
  184. struct list_head *pos;
  185. struct fib_rule *rule;
  186. if (!list_empty(&dn_fib_rules_ops.rules_list)) {
  187. pos = dn_fib_rules_ops.rules_list.next;
  188. if (pos->next != &dn_fib_rules_ops.rules_list) {
  189. rule = list_entry(pos->next, struct fib_rule, list);
  190. if (rule->pref)
  191. return rule->pref - 1;
  192. }
  193. }
  194. return 0;
  195. }
  196. static void dn_fib_rule_flush_cache(void)
  197. {
  198. dn_rt_cache_flush(-1);
  199. }
  200. static struct fib_rules_ops dn_fib_rules_ops = {
  201. .family = AF_DECnet,
  202. .rule_size = sizeof(struct dn_fib_rule),
  203. .addr_size = sizeof(u16),
  204. .action = dn_fib_rule_action,
  205. .match = dn_fib_rule_match,
  206. .configure = dn_fib_rule_configure,
  207. .compare = dn_fib_rule_compare,
  208. .fill = dn_fib_rule_fill,
  209. .default_pref = dn_fib_rule_default_pref,
  210. .flush_cache = dn_fib_rule_flush_cache,
  211. .nlgroup = RTNLGRP_DECnet_RULE,
  212. .policy = dn_fib_rule_policy,
  213. .rules_list = LIST_HEAD_INIT(dn_fib_rules_ops.rules_list),
  214. .owner = THIS_MODULE,
  215. };
  216. void __init dn_fib_rules_init(void)
  217. {
  218. list_add_tail(&default_rule.common.list,
  219. &dn_fib_rules_ops.rules_list);
  220. fib_rules_register(&dn_fib_rules_ops);
  221. }
  222. void __exit dn_fib_rules_cleanup(void)
  223. {
  224. fib_rules_unregister(&dn_fib_rules_ops);
  225. }