fib_rules.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * net/core/fib_rules.c Generic Routing Rules
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation, version 2.
  7. *
  8. * Authors: Thomas Graf <tgraf@suug.ch>
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/list.h>
  13. #include <net/fib_rules.h>
  14. static LIST_HEAD(rules_ops);
  15. static DEFINE_SPINLOCK(rules_mod_lock);
  16. static void notify_rule_change(int event, struct fib_rule *rule,
  17. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  18. u32 pid);
  19. static struct fib_rules_ops *lookup_rules_ops(int family)
  20. {
  21. struct fib_rules_ops *ops;
  22. rcu_read_lock();
  23. list_for_each_entry_rcu(ops, &rules_ops, list) {
  24. if (ops->family == family) {
  25. if (!try_module_get(ops->owner))
  26. ops = NULL;
  27. rcu_read_unlock();
  28. return ops;
  29. }
  30. }
  31. rcu_read_unlock();
  32. return NULL;
  33. }
  34. static void rules_ops_put(struct fib_rules_ops *ops)
  35. {
  36. if (ops)
  37. module_put(ops->owner);
  38. }
  39. int fib_rules_register(struct fib_rules_ops *ops)
  40. {
  41. int err = -EEXIST;
  42. struct fib_rules_ops *o;
  43. if (ops->rule_size < sizeof(struct fib_rule))
  44. return -EINVAL;
  45. if (ops->match == NULL || ops->configure == NULL ||
  46. ops->compare == NULL || ops->fill == NULL ||
  47. ops->action == NULL)
  48. return -EINVAL;
  49. spin_lock(&rules_mod_lock);
  50. list_for_each_entry(o, &rules_ops, list)
  51. if (ops->family == o->family)
  52. goto errout;
  53. list_add_tail_rcu(&ops->list, &rules_ops);
  54. err = 0;
  55. errout:
  56. spin_unlock(&rules_mod_lock);
  57. return err;
  58. }
  59. EXPORT_SYMBOL_GPL(fib_rules_register);
  60. static void cleanup_ops(struct fib_rules_ops *ops)
  61. {
  62. struct fib_rule *rule, *tmp;
  63. list_for_each_entry_safe(rule, tmp, ops->rules_list, list) {
  64. list_del_rcu(&rule->list);
  65. fib_rule_put(rule);
  66. }
  67. }
  68. int fib_rules_unregister(struct fib_rules_ops *ops)
  69. {
  70. int err = 0;
  71. struct fib_rules_ops *o;
  72. spin_lock(&rules_mod_lock);
  73. list_for_each_entry(o, &rules_ops, list) {
  74. if (o == ops) {
  75. list_del_rcu(&o->list);
  76. cleanup_ops(ops);
  77. goto out;
  78. }
  79. }
  80. err = -ENOENT;
  81. out:
  82. spin_unlock(&rules_mod_lock);
  83. synchronize_rcu();
  84. return err;
  85. }
  86. EXPORT_SYMBOL_GPL(fib_rules_unregister);
  87. static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
  88. struct flowi *fl, int flags)
  89. {
  90. int ret = 0;
  91. if (rule->ifindex && (rule->ifindex != fl->iif))
  92. goto out;
  93. if ((rule->mark ^ fl->mark) & rule->mark_mask)
  94. goto out;
  95. ret = ops->match(rule, fl, flags);
  96. out:
  97. return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
  98. }
  99. int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
  100. int flags, struct fib_lookup_arg *arg)
  101. {
  102. struct fib_rule *rule;
  103. int err;
  104. rcu_read_lock();
  105. list_for_each_entry_rcu(rule, ops->rules_list, list) {
  106. if (!fib_rule_match(rule, ops, fl, flags))
  107. continue;
  108. err = ops->action(rule, fl, flags, arg);
  109. if (err != -EAGAIN) {
  110. fib_rule_get(rule);
  111. arg->rule = rule;
  112. goto out;
  113. }
  114. }
  115. err = -ENETUNREACH;
  116. out:
  117. rcu_read_unlock();
  118. return err;
  119. }
  120. EXPORT_SYMBOL_GPL(fib_rules_lookup);
  121. int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  122. {
  123. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  124. struct fib_rules_ops *ops = NULL;
  125. struct fib_rule *rule, *r, *last = NULL;
  126. struct nlattr *tb[FRA_MAX+1];
  127. int err = -EINVAL;
  128. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  129. goto errout;
  130. ops = lookup_rules_ops(frh->family);
  131. if (ops == NULL) {
  132. err = EAFNOSUPPORT;
  133. goto errout;
  134. }
  135. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  136. if (err < 0)
  137. goto errout;
  138. rule = kzalloc(ops->rule_size, GFP_KERNEL);
  139. if (rule == NULL) {
  140. err = -ENOMEM;
  141. goto errout;
  142. }
  143. if (tb[FRA_PRIORITY])
  144. rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
  145. if (tb[FRA_IFNAME]) {
  146. struct net_device *dev;
  147. rule->ifindex = -1;
  148. nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
  149. dev = __dev_get_by_name(rule->ifname);
  150. if (dev)
  151. rule->ifindex = dev->ifindex;
  152. }
  153. if (tb[FRA_FWMARK]) {
  154. rule->mark = nla_get_u32(tb[FRA_FWMARK]);
  155. if (rule->mark)
  156. /* compatibility: if the mark value is non-zero all bits
  157. * are compared unless a mask is explicitly specified.
  158. */
  159. rule->mark_mask = 0xFFFFFFFF;
  160. }
  161. if (tb[FRA_FWMASK])
  162. rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
  163. rule->action = frh->action;
  164. rule->flags = frh->flags;
  165. rule->table = frh_get_table(frh, tb);
  166. if (!rule->pref && ops->default_pref)
  167. rule->pref = ops->default_pref();
  168. err = ops->configure(rule, skb, nlh, frh, tb);
  169. if (err < 0)
  170. goto errout_free;
  171. list_for_each_entry(r, ops->rules_list, list) {
  172. if (r->pref > rule->pref)
  173. break;
  174. last = r;
  175. }
  176. fib_rule_get(rule);
  177. if (last)
  178. list_add_rcu(&rule->list, &last->list);
  179. else
  180. list_add_rcu(&rule->list, ops->rules_list);
  181. notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
  182. rules_ops_put(ops);
  183. return 0;
  184. errout_free:
  185. kfree(rule);
  186. errout:
  187. rules_ops_put(ops);
  188. return err;
  189. }
  190. int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  191. {
  192. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  193. struct fib_rules_ops *ops = NULL;
  194. struct fib_rule *rule;
  195. struct nlattr *tb[FRA_MAX+1];
  196. int err = -EINVAL;
  197. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  198. goto errout;
  199. ops = lookup_rules_ops(frh->family);
  200. if (ops == NULL) {
  201. err = EAFNOSUPPORT;
  202. goto errout;
  203. }
  204. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  205. if (err < 0)
  206. goto errout;
  207. list_for_each_entry(rule, ops->rules_list, list) {
  208. if (frh->action && (frh->action != rule->action))
  209. continue;
  210. if (frh->table && (frh_get_table(frh, tb) != rule->table))
  211. continue;
  212. if (tb[FRA_PRIORITY] &&
  213. (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
  214. continue;
  215. if (tb[FRA_IFNAME] &&
  216. nla_strcmp(tb[FRA_IFNAME], rule->ifname))
  217. continue;
  218. if (tb[FRA_FWMARK] &&
  219. (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
  220. continue;
  221. if (tb[FRA_FWMASK] &&
  222. (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
  223. continue;
  224. if (!ops->compare(rule, frh, tb))
  225. continue;
  226. if (rule->flags & FIB_RULE_PERMANENT) {
  227. err = -EPERM;
  228. goto errout;
  229. }
  230. list_del_rcu(&rule->list);
  231. synchronize_rcu();
  232. notify_rule_change(RTM_DELRULE, rule, ops, nlh,
  233. NETLINK_CB(skb).pid);
  234. fib_rule_put(rule);
  235. rules_ops_put(ops);
  236. return 0;
  237. }
  238. err = -ENOENT;
  239. errout:
  240. rules_ops_put(ops);
  241. return err;
  242. }
  243. static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
  244. u32 pid, u32 seq, int type, int flags,
  245. struct fib_rules_ops *ops)
  246. {
  247. struct nlmsghdr *nlh;
  248. struct fib_rule_hdr *frh;
  249. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
  250. if (nlh == NULL)
  251. return -1;
  252. frh = nlmsg_data(nlh);
  253. frh->table = rule->table;
  254. NLA_PUT_U32(skb, FRA_TABLE, rule->table);
  255. frh->res1 = 0;
  256. frh->res2 = 0;
  257. frh->action = rule->action;
  258. frh->flags = rule->flags;
  259. if (rule->ifname[0])
  260. NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
  261. if (rule->pref)
  262. NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
  263. if (rule->mark)
  264. NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
  265. if (rule->mark_mask || rule->mark)
  266. NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
  267. if (ops->fill(rule, skb, nlh, frh) < 0)
  268. goto nla_put_failure;
  269. return nlmsg_end(skb, nlh);
  270. nla_put_failure:
  271. return nlmsg_cancel(skb, nlh);
  272. }
  273. int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
  274. {
  275. int idx = 0;
  276. struct fib_rule *rule;
  277. struct fib_rules_ops *ops;
  278. ops = lookup_rules_ops(family);
  279. if (ops == NULL)
  280. return -EAFNOSUPPORT;
  281. rcu_read_lock();
  282. list_for_each_entry(rule, ops->rules_list, list) {
  283. if (idx < cb->args[0])
  284. goto skip;
  285. if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
  286. cb->nlh->nlmsg_seq, RTM_NEWRULE,
  287. NLM_F_MULTI, ops) < 0)
  288. break;
  289. skip:
  290. idx++;
  291. }
  292. rcu_read_unlock();
  293. cb->args[0] = idx;
  294. rules_ops_put(ops);
  295. return skb->len;
  296. }
  297. EXPORT_SYMBOL_GPL(fib_rules_dump);
  298. static void notify_rule_change(int event, struct fib_rule *rule,
  299. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  300. u32 pid)
  301. {
  302. struct sk_buff *skb;
  303. int err = -ENOBUFS;
  304. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  305. if (skb == NULL)
  306. goto errout;
  307. err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
  308. if (err < 0) {
  309. kfree_skb(skb);
  310. goto errout;
  311. }
  312. err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
  313. errout:
  314. if (err < 0)
  315. rtnl_set_sk_err(ops->nlgroup, err);
  316. }
  317. static void attach_rules(struct list_head *rules, struct net_device *dev)
  318. {
  319. struct fib_rule *rule;
  320. list_for_each_entry(rule, rules, list) {
  321. if (rule->ifindex == -1 &&
  322. strcmp(dev->name, rule->ifname) == 0)
  323. rule->ifindex = dev->ifindex;
  324. }
  325. }
  326. static void detach_rules(struct list_head *rules, struct net_device *dev)
  327. {
  328. struct fib_rule *rule;
  329. list_for_each_entry(rule, rules, list)
  330. if (rule->ifindex == dev->ifindex)
  331. rule->ifindex = -1;
  332. }
  333. static int fib_rules_event(struct notifier_block *this, unsigned long event,
  334. void *ptr)
  335. {
  336. struct net_device *dev = ptr;
  337. struct fib_rules_ops *ops;
  338. ASSERT_RTNL();
  339. rcu_read_lock();
  340. switch (event) {
  341. case NETDEV_REGISTER:
  342. list_for_each_entry(ops, &rules_ops, list)
  343. attach_rules(ops->rules_list, dev);
  344. break;
  345. case NETDEV_UNREGISTER:
  346. list_for_each_entry(ops, &rules_ops, list)
  347. detach_rules(ops->rules_list, dev);
  348. break;
  349. }
  350. rcu_read_unlock();
  351. return NOTIFY_DONE;
  352. }
  353. static struct notifier_block fib_rules_notifier = {
  354. .notifier_call = fib_rules_event,
  355. };
  356. static int __init fib_rules_init(void)
  357. {
  358. return register_netdevice_notifier(&fib_rules_notifier);
  359. }
  360. subsys_initcall(fib_rules_init);