dn_rules.c 6.8 KB

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