fib_rules.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. __be32 src;
  41. __be32 srcmask;
  42. __be32 dst;
  43. __be32 dstmask;
  44. #ifdef CONFIG_IP_ROUTE_FWMARK
  45. u32 fwmark;
  46. u32 fwmask;
  47. #endif
  48. #ifdef CONFIG_NET_CLS_ROUTE
  49. u32 tclassid;
  50. #endif
  51. };
  52. static struct fib4_rule default_rule = {
  53. .common = {
  54. .refcnt = ATOMIC_INIT(2),
  55. .pref = 0x7FFF,
  56. .table = RT_TABLE_DEFAULT,
  57. .action = FR_ACT_TO_TBL,
  58. },
  59. };
  60. static struct fib4_rule main_rule = {
  61. .common = {
  62. .refcnt = ATOMIC_INIT(2),
  63. .pref = 0x7FFE,
  64. .table = RT_TABLE_MAIN,
  65. .action = FR_ACT_TO_TBL,
  66. },
  67. };
  68. static struct fib4_rule local_rule = {
  69. .common = {
  70. .refcnt = ATOMIC_INIT(2),
  71. .table = RT_TABLE_LOCAL,
  72. .action = FR_ACT_TO_TBL,
  73. .flags = FIB_RULE_PERMANENT,
  74. },
  75. };
  76. static LIST_HEAD(fib4_rules);
  77. #ifdef CONFIG_NET_CLS_ROUTE
  78. u32 fib_rules_tclass(struct fib_result *res)
  79. {
  80. return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
  81. }
  82. #endif
  83. int fib_lookup(struct flowi *flp, struct fib_result *res)
  84. {
  85. struct fib_lookup_arg arg = {
  86. .result = res,
  87. };
  88. int err;
  89. err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
  90. res->r = arg.rule;
  91. return err;
  92. }
  93. static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
  94. int flags, struct fib_lookup_arg *arg)
  95. {
  96. int err = -EAGAIN;
  97. struct fib_table *tbl;
  98. switch (rule->action) {
  99. case FR_ACT_TO_TBL:
  100. break;
  101. case FR_ACT_UNREACHABLE:
  102. err = -ENETUNREACH;
  103. goto errout;
  104. case FR_ACT_PROHIBIT:
  105. err = -EACCES;
  106. goto errout;
  107. case FR_ACT_BLACKHOLE:
  108. default:
  109. err = -EINVAL;
  110. goto errout;
  111. }
  112. if ((tbl = fib_get_table(rule->table)) == NULL)
  113. goto errout;
  114. err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
  115. if (err > 0)
  116. err = -EAGAIN;
  117. errout:
  118. return err;
  119. }
  120. void fib_select_default(const struct flowi *flp, struct fib_result *res)
  121. {
  122. if (res->r && res->r->action == FR_ACT_TO_TBL &&
  123. FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
  124. struct fib_table *tb;
  125. if ((tb = fib_get_table(res->r->table)) != NULL)
  126. tb->tb_select_default(tb, flp, res);
  127. }
  128. }
  129. static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  130. {
  131. struct fib4_rule *r = (struct fib4_rule *) rule;
  132. __be32 daddr = fl->fl4_dst;
  133. __be32 saddr = fl->fl4_src;
  134. if (((saddr ^ r->src) & r->srcmask) ||
  135. ((daddr ^ r->dst) & r->dstmask))
  136. return 0;
  137. if (r->tos && (r->tos != fl->fl4_tos))
  138. return 0;
  139. #ifdef CONFIG_IP_ROUTE_FWMARK
  140. if ((r->fwmark ^ fl->fl4_fwmark) & r->fwmask)
  141. return 0;
  142. #endif
  143. return 1;
  144. }
  145. static struct fib_table *fib_empty_table(void)
  146. {
  147. u32 id;
  148. for (id = 1; id <= RT_TABLE_MAX; id++)
  149. if (fib_get_table(id) == NULL)
  150. return fib_new_table(id);
  151. return NULL;
  152. }
  153. static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = {
  154. [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
  155. [FRA_PRIORITY] = { .type = NLA_U32 },
  156. [FRA_SRC] = { .type = NLA_U32 },
  157. [FRA_DST] = { .type = NLA_U32 },
  158. [FRA_FWMARK] = { .type = NLA_U32 },
  159. [FRA_FWMASK] = { .type = NLA_U32 },
  160. [FRA_FLOW] = { .type = NLA_U32 },
  161. [FRA_TABLE] = { .type = NLA_U32 },
  162. };
  163. static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  164. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  165. struct nlattr **tb)
  166. {
  167. int err = -EINVAL;
  168. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  169. if (frh->src_len > 32 || frh->dst_len > 32 ||
  170. (frh->tos & ~IPTOS_TOS_MASK))
  171. goto errout;
  172. if (rule->table == RT_TABLE_UNSPEC) {
  173. if (rule->action == FR_ACT_TO_TBL) {
  174. struct fib_table *table;
  175. table = fib_empty_table();
  176. if (table == NULL) {
  177. err = -ENOBUFS;
  178. goto errout;
  179. }
  180. rule->table = table->tb_id;
  181. }
  182. }
  183. if (tb[FRA_SRC])
  184. rule4->src = nla_get_be32(tb[FRA_SRC]);
  185. if (tb[FRA_DST])
  186. rule4->dst = nla_get_be32(tb[FRA_DST]);
  187. #ifdef CONFIG_IP_ROUTE_FWMARK
  188. if (tb[FRA_FWMARK]) {
  189. rule4->fwmark = nla_get_u32(tb[FRA_FWMARK]);
  190. if (rule4->fwmark)
  191. /* compatibility: if the mark value is non-zero all bits
  192. * are compared unless a mask is explicitly specified.
  193. */
  194. rule4->fwmask = 0xFFFFFFFF;
  195. }
  196. if (tb[FRA_FWMASK])
  197. rule4->fwmask = nla_get_u32(tb[FRA_FWMASK]);
  198. #endif
  199. #ifdef CONFIG_NET_CLS_ROUTE
  200. if (tb[FRA_FLOW])
  201. rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
  202. #endif
  203. rule4->src_len = frh->src_len;
  204. rule4->srcmask = inet_make_mask(rule4->src_len);
  205. rule4->dst_len = frh->dst_len;
  206. rule4->dstmask = inet_make_mask(rule4->dst_len);
  207. rule4->tos = frh->tos;
  208. err = 0;
  209. errout:
  210. return err;
  211. }
  212. static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  213. struct nlattr **tb)
  214. {
  215. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  216. if (frh->src_len && (rule4->src_len != frh->src_len))
  217. return 0;
  218. if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  219. return 0;
  220. if (frh->tos && (rule4->tos != frh->tos))
  221. return 0;
  222. #ifdef CONFIG_IP_ROUTE_FWMARK
  223. if (tb[FRA_FWMARK] && (rule4->fwmark != nla_get_u32(tb[FRA_FWMARK])))
  224. return 0;
  225. if (tb[FRA_FWMASK] && (rule4->fwmask != nla_get_u32(tb[FRA_FWMASK])))
  226. return 0;
  227. #endif
  228. #ifdef CONFIG_NET_CLS_ROUTE
  229. if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  230. return 0;
  231. #endif
  232. if (tb[FRA_SRC] && (rule4->src != nla_get_be32(tb[FRA_SRC])))
  233. return 0;
  234. if (tb[FRA_DST] && (rule4->dst != nla_get_be32(tb[FRA_DST])))
  235. return 0;
  236. return 1;
  237. }
  238. static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  239. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  240. {
  241. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  242. frh->family = AF_INET;
  243. frh->dst_len = rule4->dst_len;
  244. frh->src_len = rule4->src_len;
  245. frh->tos = rule4->tos;
  246. #ifdef CONFIG_IP_ROUTE_FWMARK
  247. if (rule4->fwmark)
  248. NLA_PUT_U32(skb, FRA_FWMARK, rule4->fwmark);
  249. if (rule4->fwmask || rule4->fwmark)
  250. NLA_PUT_U32(skb, FRA_FWMASK, rule4->fwmask);
  251. #endif
  252. if (rule4->dst_len)
  253. NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
  254. if (rule4->src_len)
  255. NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
  256. #ifdef CONFIG_NET_CLS_ROUTE
  257. if (rule4->tclassid)
  258. NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
  259. #endif
  260. return 0;
  261. nla_put_failure:
  262. return -ENOBUFS;
  263. }
  264. int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
  265. {
  266. return fib_rules_dump(skb, cb, AF_INET);
  267. }
  268. static u32 fib4_rule_default_pref(void)
  269. {
  270. struct list_head *pos;
  271. struct fib_rule *rule;
  272. if (!list_empty(&fib4_rules)) {
  273. pos = fib4_rules.next;
  274. if (pos->next != &fib4_rules) {
  275. rule = list_entry(pos->next, struct fib_rule, list);
  276. if (rule->pref)
  277. return rule->pref - 1;
  278. }
  279. }
  280. return 0;
  281. }
  282. static struct fib_rules_ops fib4_rules_ops = {
  283. .family = AF_INET,
  284. .rule_size = sizeof(struct fib4_rule),
  285. .action = fib4_rule_action,
  286. .match = fib4_rule_match,
  287. .configure = fib4_rule_configure,
  288. .compare = fib4_rule_compare,
  289. .fill = fib4_rule_fill,
  290. .default_pref = fib4_rule_default_pref,
  291. .nlgroup = RTNLGRP_IPV4_RULE,
  292. .policy = fib4_rule_policy,
  293. .rules_list = &fib4_rules,
  294. .owner = THIS_MODULE,
  295. };
  296. void __init fib4_rules_init(void)
  297. {
  298. list_add_tail(&local_rule.common.list, &fib4_rules);
  299. list_add_tail(&main_rule.common.list, &fib4_rules);
  300. list_add_tail(&default_rule.common.list, &fib4_rules);
  301. fib_rules_register(&fib4_rules_ops);
  302. }