dn_rules.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. static struct fib_rules_ops dn_fib_rules_ops;
  34. struct dn_fib_rule
  35. {
  36. struct fib_rule common;
  37. unsigned char dst_len;
  38. unsigned char src_len;
  39. __le16 src;
  40. __le16 srcmask;
  41. __le16 dst;
  42. __le16 dstmask;
  43. __le16 srcmap;
  44. u8 flags;
  45. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  46. u32 fwmark;
  47. #endif
  48. };
  49. static struct dn_fib_rule default_rule = {
  50. .common = {
  51. .refcnt = ATOMIC_INIT(2),
  52. .pref = 0x7fff,
  53. .table = RT_TABLE_MAIN,
  54. .action = FR_ACT_TO_TBL,
  55. },
  56. };
  57. static LIST_HEAD(dn_fib_rules);
  58. int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
  59. {
  60. struct fib_lookup_arg arg = {
  61. .result = res,
  62. };
  63. int err;
  64. err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
  65. res->r = arg.rule;
  66. return err;
  67. }
  68. static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
  69. int flags, struct fib_lookup_arg *arg)
  70. {
  71. int err = -EAGAIN;
  72. struct dn_fib_table *tbl;
  73. switch(rule->action) {
  74. case FR_ACT_TO_TBL:
  75. break;
  76. case FR_ACT_UNREACHABLE:
  77. err = -ENETUNREACH;
  78. goto errout;
  79. case FR_ACT_PROHIBIT:
  80. err = -EACCES;
  81. goto errout;
  82. case FR_ACT_BLACKHOLE:
  83. default:
  84. err = -EINVAL;
  85. goto errout;
  86. }
  87. tbl = dn_fib_get_table(rule->table, 0);
  88. if (tbl == NULL)
  89. goto errout;
  90. err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
  91. if (err > 0)
  92. err = -EAGAIN;
  93. errout:
  94. return err;
  95. }
  96. static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
  97. [FRA_IFNAME] = { .type = NLA_STRING },
  98. [FRA_PRIORITY] = { .type = NLA_U32 },
  99. [FRA_SRC] = { .type = NLA_U16 },
  100. [FRA_DST] = { .type = NLA_U16 },
  101. [FRA_FWMARK] = { .type = NLA_U32 },
  102. [FRA_TABLE] = { .type = NLA_U32 },
  103. };
  104. static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  105. {
  106. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  107. u16 daddr = fl->fld_dst;
  108. u16 saddr = fl->fld_src;
  109. if (((saddr ^ r->src) & r->srcmask) ||
  110. ((daddr ^ r->dst) & r->dstmask))
  111. return 0;
  112. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  113. if (r->fwmark && (r->fwmark != fl->fld_fwmark))
  114. return 0;
  115. #endif
  116. return 1;
  117. }
  118. static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  119. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  120. struct nlattr **tb)
  121. {
  122. int err = -EINVAL;
  123. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  124. if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
  125. goto errout;
  126. if (rule->table == RT_TABLE_UNSPEC) {
  127. if (rule->action == FR_ACT_TO_TBL) {
  128. struct dn_fib_table *table;
  129. table = dn_fib_empty_table();
  130. if (table == NULL) {
  131. err = -ENOBUFS;
  132. goto errout;
  133. }
  134. rule->table = table->n;
  135. }
  136. }
  137. if (tb[FRA_SRC])
  138. r->src = nla_get_u16(tb[FRA_SRC]);
  139. if (tb[FRA_DST])
  140. r->dst = nla_get_u16(tb[FRA_DST]);
  141. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  142. if (tb[FRA_FWMARK])
  143. r->fwmark = nla_get_u32(tb[FRA_FWMARK]);
  144. #endif
  145. r->src_len = frh->src_len;
  146. r->srcmask = dnet_make_mask(r->src_len);
  147. r->dst_len = frh->dst_len;
  148. r->dstmask = dnet_make_mask(r->dst_len);
  149. err = 0;
  150. errout:
  151. return err;
  152. }
  153. static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  154. struct nlattr **tb)
  155. {
  156. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  157. if (frh->src_len && (r->src_len != frh->src_len))
  158. return 0;
  159. if (frh->dst_len && (r->dst_len != frh->dst_len))
  160. return 0;
  161. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  162. if (tb[FRA_FWMARK] && (r->fwmark != nla_get_u32(tb[FRA_FWMARK])))
  163. return 0;
  164. #endif
  165. if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC])))
  166. return 0;
  167. if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST])))
  168. return 0;
  169. return 1;
  170. }
  171. unsigned dnet_addr_type(__le16 addr)
  172. {
  173. struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
  174. struct dn_fib_res res;
  175. unsigned ret = RTN_UNICAST;
  176. struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
  177. res.r = NULL;
  178. if (tb) {
  179. if (!tb->lookup(tb, &fl, &res)) {
  180. ret = res.type;
  181. dn_fib_res_put(&res);
  182. }
  183. }
  184. return ret;
  185. }
  186. static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  187. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  188. {
  189. struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  190. frh->family = AF_DECnet;
  191. frh->dst_len = r->dst_len;
  192. frh->src_len = r->src_len;
  193. frh->tos = 0;
  194. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  195. if (r->fwmark)
  196. NLA_PUT_U32(skb, FRA_FWMARK, r->fwmark);
  197. #endif
  198. if (r->dst_len)
  199. NLA_PUT_U16(skb, FRA_DST, r->dst);
  200. if (r->src_len)
  201. NLA_PUT_U16(skb, FRA_SRC, r->src);
  202. return 0;
  203. nla_put_failure:
  204. return -ENOBUFS;
  205. }
  206. static u32 dn_fib_rule_default_pref(void)
  207. {
  208. struct list_head *pos;
  209. struct fib_rule *rule;
  210. if (!list_empty(&dn_fib_rules)) {
  211. pos = dn_fib_rules.next;
  212. if (pos->next != &dn_fib_rules) {
  213. rule = list_entry(pos->next, struct fib_rule, list);
  214. if (rule->pref)
  215. return rule->pref - 1;
  216. }
  217. }
  218. return 0;
  219. }
  220. int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
  221. {
  222. return fib_rules_dump(skb, cb, AF_DECnet);
  223. }
  224. static struct fib_rules_ops dn_fib_rules_ops = {
  225. .family = AF_DECnet,
  226. .rule_size = sizeof(struct dn_fib_rule),
  227. .action = dn_fib_rule_action,
  228. .match = dn_fib_rule_match,
  229. .configure = dn_fib_rule_configure,
  230. .compare = dn_fib_rule_compare,
  231. .fill = dn_fib_rule_fill,
  232. .default_pref = dn_fib_rule_default_pref,
  233. .nlgroup = RTNLGRP_DECnet_RULE,
  234. .policy = dn_fib_rule_policy,
  235. .rules_list = &dn_fib_rules,
  236. .owner = THIS_MODULE,
  237. };
  238. void __init dn_fib_rules_init(void)
  239. {
  240. list_add_tail(&default_rule.common.list, &dn_fib_rules);
  241. fib_rules_register(&dn_fib_rules_ops);
  242. }
  243. void __exit dn_fib_rules_cleanup(void)
  244. {
  245. fib_rules_unregister(&dn_fib_rules_ops);
  246. }