dn_rules.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. static LIST_HEAD(dn_fib_rules);
  56. int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
  57. {
  58. struct fib_lookup_arg arg = {
  59. .result = res,
  60. };
  61. int err;
  62. err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
  63. res->r = arg.rule;
  64. return err;
  65. }
  66. static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
  67. int flags, struct fib_lookup_arg *arg)
  68. {
  69. int err = -EAGAIN;
  70. struct dn_fib_table *tbl;
  71. switch(rule->action) {
  72. case FR_ACT_TO_TBL:
  73. break;
  74. case FR_ACT_UNREACHABLE:
  75. err = -ENETUNREACH;
  76. goto errout;
  77. case FR_ACT_PROHIBIT:
  78. err = -EACCES;
  79. goto errout;
  80. case FR_ACT_BLACKHOLE:
  81. default:
  82. err = -EINVAL;
  83. goto errout;
  84. }
  85. tbl = dn_fib_get_table(rule->table, 0);
  86. if (tbl == NULL)
  87. goto errout;
  88. err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
  89. if (err > 0)
  90. err = -EAGAIN;
  91. errout:
  92. return err;
  93. }
  94. static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
  95. FRA_GENERIC_POLICY,
  96. };
  97. static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  98. {
  99. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  100. __le16 daddr = fl->fld_dst;
  101. __le16 saddr = fl->fld_src;
  102. if (((saddr ^ r->src) & r->srcmask) ||
  103. ((daddr ^ r->dst) & r->dstmask))
  104. return 0;
  105. return 1;
  106. }
  107. static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  108. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  109. struct nlattr **tb)
  110. {
  111. int err = -EINVAL;
  112. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  113. if (frh->tos)
  114. goto errout;
  115. if (rule->table == RT_TABLE_UNSPEC) {
  116. if (rule->action == FR_ACT_TO_TBL) {
  117. struct dn_fib_table *table;
  118. table = dn_fib_empty_table();
  119. if (table == NULL) {
  120. err = -ENOBUFS;
  121. goto errout;
  122. }
  123. rule->table = table->n;
  124. }
  125. }
  126. if (frh->src_len)
  127. r->src = nla_get_le16(tb[FRA_SRC]);
  128. if (frh->dst_len)
  129. r->dst = nla_get_le16(tb[FRA_DST]);
  130. r->src_len = frh->src_len;
  131. r->srcmask = dnet_make_mask(r->src_len);
  132. r->dst_len = frh->dst_len;
  133. r->dstmask = dnet_make_mask(r->dst_len);
  134. err = 0;
  135. errout:
  136. return err;
  137. }
  138. static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  139. struct nlattr **tb)
  140. {
  141. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  142. if (frh->src_len && (r->src_len != frh->src_len))
  143. return 0;
  144. if (frh->dst_len && (r->dst_len != frh->dst_len))
  145. return 0;
  146. if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
  147. return 0;
  148. if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
  149. return 0;
  150. return 1;
  151. }
  152. unsigned dnet_addr_type(__le16 addr)
  153. {
  154. struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
  155. struct dn_fib_res res;
  156. unsigned ret = RTN_UNICAST;
  157. struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
  158. res.r = NULL;
  159. if (tb) {
  160. if (!tb->lookup(tb, &fl, &res)) {
  161. ret = res.type;
  162. dn_fib_res_put(&res);
  163. }
  164. }
  165. return ret;
  166. }
  167. static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  168. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  169. {
  170. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  171. frh->family = AF_DECnet;
  172. frh->dst_len = r->dst_len;
  173. frh->src_len = r->src_len;
  174. frh->tos = 0;
  175. if (r->dst_len)
  176. NLA_PUT_LE16(skb, FRA_DST, r->dst);
  177. if (r->src_len)
  178. NLA_PUT_LE16(skb, FRA_SRC, r->src);
  179. return 0;
  180. nla_put_failure:
  181. return -ENOBUFS;
  182. }
  183. static u32 dn_fib_rule_default_pref(void)
  184. {
  185. struct list_head *pos;
  186. struct fib_rule *rule;
  187. if (!list_empty(&dn_fib_rules)) {
  188. pos = dn_fib_rules.next;
  189. if (pos->next != &dn_fib_rules) {
  190. rule = list_entry(pos->next, struct fib_rule, list);
  191. if (rule->pref)
  192. return rule->pref - 1;
  193. }
  194. }
  195. return 0;
  196. }
  197. static void dn_fib_rule_flush_cache(void)
  198. {
  199. dn_rt_cache_flush(-1);
  200. }
  201. static struct fib_rules_ops dn_fib_rules_ops = {
  202. .family = AF_DECnet,
  203. .rule_size = sizeof(struct dn_fib_rule),
  204. .addr_size = sizeof(u16),
  205. .action = dn_fib_rule_action,
  206. .match = dn_fib_rule_match,
  207. .configure = dn_fib_rule_configure,
  208. .compare = dn_fib_rule_compare,
  209. .fill = dn_fib_rule_fill,
  210. .default_pref = dn_fib_rule_default_pref,
  211. .flush_cache = dn_fib_rule_flush_cache,
  212. .nlgroup = RTNLGRP_DECnet_RULE,
  213. .policy = dn_fib_rule_policy,
  214. .rules_list = &dn_fib_rules,
  215. .owner = THIS_MODULE,
  216. };
  217. void __init dn_fib_rules_init(void)
  218. {
  219. list_add_tail(&default_rule.common.list, &dn_fib_rules);
  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. }