fib_rules.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * IPv4 Forwarding Information Base: policy rules.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. * Thomas Graf <tgraf@suug.ch>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * Fixes:
  17. * Rani Assaf : local_rule cannot be deleted
  18. * Marc Boucher : routing by fwmark
  19. */
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/netlink.h>
  24. #include <linux/inetdevice.h>
  25. #include <linux/init.h>
  26. #include <linux/list.h>
  27. #include <linux/rcupdate.h>
  28. #include <net/ip.h>
  29. #include <net/route.h>
  30. #include <net/tcp.h>
  31. #include <net/ip_fib.h>
  32. #include <net/fib_rules.h>
  33. static struct fib_rules_ops fib4_rules_ops;
  34. struct fib4_rule
  35. {
  36. struct fib_rule common;
  37. u8 dst_len;
  38. u8 src_len;
  39. u8 tos;
  40. u32 src;
  41. u32 srcmask;
  42. u32 dst;
  43. u32 dstmask;
  44. #ifdef CONFIG_IP_ROUTE_FWMARK
  45. u32 fwmark;
  46. #endif
  47. #ifdef CONFIG_NET_CLS_ROUTE
  48. u32 tclassid;
  49. #endif
  50. };
  51. static struct fib4_rule default_rule = {
  52. .common = {
  53. .refcnt = ATOMIC_INIT(2),
  54. .pref = 0x7FFF,
  55. .table = RT_TABLE_DEFAULT,
  56. .action = FR_ACT_TO_TBL,
  57. },
  58. };
  59. static struct fib4_rule main_rule = {
  60. .common = {
  61. .refcnt = ATOMIC_INIT(2),
  62. .pref = 0x7FFE,
  63. .table = RT_TABLE_MAIN,
  64. .action = FR_ACT_TO_TBL,
  65. },
  66. };
  67. static struct fib4_rule local_rule = {
  68. .common = {
  69. .refcnt = ATOMIC_INIT(2),
  70. .table = RT_TABLE_LOCAL,
  71. .action = FR_ACT_TO_TBL,
  72. .flags = FIB_RULE_PERMANENT,
  73. },
  74. };
  75. static LIST_HEAD(fib4_rules);
  76. #ifdef CONFIG_NET_CLS_ROUTE
  77. u32 fib_rules_tclass(struct fib_result *res)
  78. {
  79. return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
  80. }
  81. #endif
  82. int fib_lookup(struct flowi *flp, struct fib_result *res)
  83. {
  84. struct fib_lookup_arg arg = {
  85. .result = res,
  86. };
  87. int err;
  88. err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
  89. res->r = arg.rule;
  90. return err;
  91. }
  92. static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
  93. int flags, struct fib_lookup_arg *arg)
  94. {
  95. int err = -EAGAIN;
  96. struct fib_table *tbl;
  97. switch (rule->action) {
  98. case FR_ACT_TO_TBL:
  99. break;
  100. case FR_ACT_UNREACHABLE:
  101. err = -ENETUNREACH;
  102. goto errout;
  103. case FR_ACT_PROHIBIT:
  104. err = -EACCES;
  105. goto errout;
  106. case FR_ACT_BLACKHOLE:
  107. default:
  108. err = -EINVAL;
  109. goto errout;
  110. }
  111. if ((tbl = fib_get_table(rule->table)) == NULL)
  112. goto errout;
  113. err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
  114. if (err > 0)
  115. err = -EAGAIN;
  116. errout:
  117. return err;
  118. }
  119. void fib_select_default(const struct flowi *flp, struct fib_result *res)
  120. {
  121. if (res->r && res->r->action == FR_ACT_TO_TBL &&
  122. FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
  123. struct fib_table *tb;
  124. if ((tb = fib_get_table(res->r->table)) != NULL)
  125. tb->tb_select_default(tb, flp, res);
  126. }
  127. }
  128. static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  129. {
  130. struct fib4_rule *r = (struct fib4_rule *) rule;
  131. u32 daddr = fl->fl4_dst;
  132. u32 saddr = fl->fl4_src;
  133. if (((saddr ^ r->src) & r->srcmask) ||
  134. ((daddr ^ r->dst) & r->dstmask))
  135. return 0;
  136. if (r->tos && (r->tos != fl->fl4_tos))
  137. return 0;
  138. #ifdef CONFIG_IP_ROUTE_FWMARK
  139. if (r->fwmark && (r->fwmark != fl->fl4_fwmark))
  140. return 0;
  141. #endif
  142. return 1;
  143. }
  144. static struct fib_table *fib_empty_table(void)
  145. {
  146. u32 id;
  147. for (id = 1; id <= RT_TABLE_MAX; id++)
  148. if (fib_get_table(id) == NULL)
  149. return fib_new_table(id);
  150. return NULL;
  151. }
  152. static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = {
  153. [FRA_IFNAME] = { .type = NLA_STRING },
  154. [FRA_PRIORITY] = { .type = NLA_U32 },
  155. [FRA_SRC] = { .type = NLA_U32 },
  156. [FRA_DST] = { .type = NLA_U32 },
  157. [FRA_FWMARK] = { .type = NLA_U32 },
  158. [FRA_FLOW] = { .type = NLA_U32 },
  159. [FRA_TABLE] = { .type = NLA_U32 },
  160. };
  161. static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  162. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  163. struct nlattr **tb)
  164. {
  165. int err = -EINVAL;
  166. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  167. if (frh->src_len > 32 || frh->dst_len > 32 ||
  168. (frh->tos & ~IPTOS_TOS_MASK))
  169. goto errout;
  170. if (rule->table == RT_TABLE_UNSPEC) {
  171. if (rule->action == FR_ACT_TO_TBL) {
  172. struct fib_table *table;
  173. table = fib_empty_table();
  174. if (table == NULL) {
  175. err = -ENOBUFS;
  176. goto errout;
  177. }
  178. rule->table = table->tb_id;
  179. }
  180. }
  181. if (tb[FRA_SRC])
  182. rule4->src = nla_get_u32(tb[FRA_SRC]);
  183. if (tb[FRA_DST])
  184. rule4->dst = nla_get_u32(tb[FRA_DST]);
  185. #ifdef CONFIG_IP_ROUTE_FWMARK
  186. if (tb[FRA_FWMARK])
  187. rule4->fwmark = nla_get_u32(tb[FRA_FWMARK]);
  188. #endif
  189. #ifdef CONFIG_NET_CLS_ROUTE
  190. if (tb[FRA_FLOW])
  191. rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
  192. #endif
  193. rule4->src_len = frh->src_len;
  194. rule4->srcmask = inet_make_mask(rule4->src_len);
  195. rule4->dst_len = frh->dst_len;
  196. rule4->dstmask = inet_make_mask(rule4->dst_len);
  197. rule4->tos = frh->tos;
  198. err = 0;
  199. errout:
  200. return err;
  201. }
  202. static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  203. struct nlattr **tb)
  204. {
  205. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  206. if (frh->src_len && (rule4->src_len != frh->src_len))
  207. return 0;
  208. if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  209. return 0;
  210. if (frh->tos && (rule4->tos != frh->tos))
  211. return 0;
  212. #ifdef CONFIG_IP_ROUTE_FWMARK
  213. if (tb[FRA_FWMARK] && (rule4->fwmark != nla_get_u32(tb[FRA_FWMARK])))
  214. return 0;
  215. #endif
  216. #ifdef CONFIG_NET_CLS_ROUTE
  217. if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  218. return 0;
  219. #endif
  220. if (tb[FRA_SRC] && (rule4->src != nla_get_u32(tb[FRA_SRC])))
  221. return 0;
  222. if (tb[FRA_DST] && (rule4->dst != nla_get_u32(tb[FRA_DST])))
  223. return 0;
  224. return 1;
  225. }
  226. static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  227. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  228. {
  229. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  230. frh->family = AF_INET;
  231. frh->dst_len = rule4->dst_len;
  232. frh->src_len = rule4->src_len;
  233. frh->tos = rule4->tos;
  234. #ifdef CONFIG_IP_ROUTE_FWMARK
  235. if (rule4->fwmark)
  236. NLA_PUT_U32(skb, FRA_FWMARK, rule4->fwmark);
  237. #endif
  238. if (rule4->dst_len)
  239. NLA_PUT_U32(skb, FRA_DST, rule4->dst);
  240. if (rule4->src_len)
  241. NLA_PUT_U32(skb, FRA_SRC, rule4->src);
  242. #ifdef CONFIG_NET_CLS_ROUTE
  243. if (rule4->tclassid)
  244. NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
  245. #endif
  246. return 0;
  247. nla_put_failure:
  248. return -ENOBUFS;
  249. }
  250. int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
  251. {
  252. return fib_rules_dump(skb, cb, AF_INET);
  253. }
  254. static u32 fib4_rule_default_pref(void)
  255. {
  256. struct list_head *pos;
  257. struct fib_rule *rule;
  258. if (!list_empty(&fib4_rules)) {
  259. pos = fib4_rules.next;
  260. if (pos->next != &fib4_rules) {
  261. rule = list_entry(pos->next, struct fib_rule, list);
  262. if (rule->pref)
  263. return rule->pref - 1;
  264. }
  265. }
  266. return 0;
  267. }
  268. static struct fib_rules_ops fib4_rules_ops = {
  269. .family = AF_INET,
  270. .rule_size = sizeof(struct fib4_rule),
  271. .action = fib4_rule_action,
  272. .match = fib4_rule_match,
  273. .configure = fib4_rule_configure,
  274. .compare = fib4_rule_compare,
  275. .fill = fib4_rule_fill,
  276. .default_pref = fib4_rule_default_pref,
  277. .nlgroup = RTNLGRP_IPV4_RULE,
  278. .policy = fib4_rule_policy,
  279. .rules_list = &fib4_rules,
  280. .owner = THIS_MODULE,
  281. };
  282. void __init fib4_rules_init(void)
  283. {
  284. list_add_tail(&local_rule.common.list, &fib4_rules);
  285. list_add_tail(&main_rule.common.list, &fib4_rules);
  286. list_add_tail(&default_rule.common.list, &fib4_rules);
  287. fib_rules_register(&fib4_rules_ops);
  288. }