fib_rules.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
  88. int flags, struct fib_lookup_arg *arg)
  89. {
  90. struct fib_rule *rule;
  91. int err;
  92. rcu_read_lock();
  93. list_for_each_entry_rcu(rule, ops->rules_list, list) {
  94. if (rule->ifindex && (rule->ifindex != fl->iif))
  95. continue;
  96. if (!ops->match(rule, fl, flags))
  97. continue;
  98. err = ops->action(rule, fl, flags, arg);
  99. if (err != -EAGAIN) {
  100. fib_rule_get(rule);
  101. arg->rule = rule;
  102. goto out;
  103. }
  104. }
  105. err = -ENETUNREACH;
  106. out:
  107. rcu_read_unlock();
  108. return err;
  109. }
  110. EXPORT_SYMBOL_GPL(fib_rules_lookup);
  111. int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  112. {
  113. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  114. struct fib_rules_ops *ops = NULL;
  115. struct fib_rule *rule, *r, *last = NULL;
  116. struct nlattr *tb[FRA_MAX+1];
  117. int err = -EINVAL;
  118. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  119. goto errout;
  120. ops = lookup_rules_ops(frh->family);
  121. if (ops == NULL) {
  122. err = EAFNOSUPPORT;
  123. goto errout;
  124. }
  125. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  126. if (err < 0)
  127. goto errout;
  128. rule = kzalloc(ops->rule_size, GFP_KERNEL);
  129. if (rule == NULL) {
  130. err = -ENOMEM;
  131. goto errout;
  132. }
  133. if (tb[FRA_PRIORITY])
  134. rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
  135. if (tb[FRA_IFNAME]) {
  136. struct net_device *dev;
  137. rule->ifindex = -1;
  138. nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
  139. dev = __dev_get_by_name(rule->ifname);
  140. if (dev)
  141. rule->ifindex = dev->ifindex;
  142. }
  143. rule->action = frh->action;
  144. rule->flags = frh->flags;
  145. rule->table = frh_get_table(frh, tb);
  146. if (!rule->pref && ops->default_pref)
  147. rule->pref = ops->default_pref();
  148. err = ops->configure(rule, skb, nlh, frh, tb);
  149. if (err < 0)
  150. goto errout_free;
  151. list_for_each_entry(r, ops->rules_list, list) {
  152. if (r->pref > rule->pref)
  153. break;
  154. last = r;
  155. }
  156. fib_rule_get(rule);
  157. if (last)
  158. list_add_rcu(&rule->list, &last->list);
  159. else
  160. list_add_rcu(&rule->list, ops->rules_list);
  161. notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
  162. rules_ops_put(ops);
  163. return 0;
  164. errout_free:
  165. kfree(rule);
  166. errout:
  167. rules_ops_put(ops);
  168. return err;
  169. }
  170. int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  171. {
  172. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  173. struct fib_rules_ops *ops = NULL;
  174. struct fib_rule *rule;
  175. struct nlattr *tb[FRA_MAX+1];
  176. int err = -EINVAL;
  177. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  178. goto errout;
  179. ops = lookup_rules_ops(frh->family);
  180. if (ops == NULL) {
  181. err = EAFNOSUPPORT;
  182. goto errout;
  183. }
  184. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  185. if (err < 0)
  186. goto errout;
  187. list_for_each_entry(rule, ops->rules_list, list) {
  188. if (frh->action && (frh->action != rule->action))
  189. continue;
  190. if (frh->table && (frh_get_table(frh, tb) != rule->table))
  191. continue;
  192. if (tb[FRA_PRIORITY] &&
  193. (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
  194. continue;
  195. if (tb[FRA_IFNAME] &&
  196. nla_strcmp(tb[FRA_IFNAME], rule->ifname))
  197. continue;
  198. if (!ops->compare(rule, frh, tb))
  199. continue;
  200. if (rule->flags & FIB_RULE_PERMANENT) {
  201. err = -EPERM;
  202. goto errout;
  203. }
  204. list_del_rcu(&rule->list);
  205. synchronize_rcu();
  206. notify_rule_change(RTM_DELRULE, rule, ops, nlh,
  207. NETLINK_CB(skb).pid);
  208. fib_rule_put(rule);
  209. rules_ops_put(ops);
  210. return 0;
  211. }
  212. err = -ENOENT;
  213. errout:
  214. rules_ops_put(ops);
  215. return err;
  216. }
  217. static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
  218. u32 pid, u32 seq, int type, int flags,
  219. struct fib_rules_ops *ops)
  220. {
  221. struct nlmsghdr *nlh;
  222. struct fib_rule_hdr *frh;
  223. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
  224. if (nlh == NULL)
  225. return -1;
  226. frh = nlmsg_data(nlh);
  227. frh->table = rule->table;
  228. NLA_PUT_U32(skb, FRA_TABLE, rule->table);
  229. frh->res1 = 0;
  230. frh->res2 = 0;
  231. frh->action = rule->action;
  232. frh->flags = rule->flags;
  233. if (rule->ifname[0])
  234. NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
  235. if (rule->pref)
  236. NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
  237. if (ops->fill(rule, skb, nlh, frh) < 0)
  238. goto nla_put_failure;
  239. return nlmsg_end(skb, nlh);
  240. nla_put_failure:
  241. return nlmsg_cancel(skb, nlh);
  242. }
  243. int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
  244. {
  245. int idx = 0;
  246. struct fib_rule *rule;
  247. struct fib_rules_ops *ops;
  248. ops = lookup_rules_ops(family);
  249. if (ops == NULL)
  250. return -EAFNOSUPPORT;
  251. rcu_read_lock();
  252. list_for_each_entry(rule, ops->rules_list, list) {
  253. if (idx < cb->args[0])
  254. goto skip;
  255. if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
  256. cb->nlh->nlmsg_seq, RTM_NEWRULE,
  257. NLM_F_MULTI, ops) < 0)
  258. break;
  259. skip:
  260. idx++;
  261. }
  262. rcu_read_unlock();
  263. cb->args[0] = idx;
  264. rules_ops_put(ops);
  265. return skb->len;
  266. }
  267. EXPORT_SYMBOL_GPL(fib_rules_dump);
  268. static void notify_rule_change(int event, struct fib_rule *rule,
  269. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  270. u32 pid)
  271. {
  272. struct sk_buff *skb;
  273. int err = -ENOBUFS;
  274. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  275. if (skb == NULL)
  276. goto errout;
  277. err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
  278. if (err < 0) {
  279. kfree_skb(skb);
  280. goto errout;
  281. }
  282. err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
  283. errout:
  284. if (err < 0)
  285. rtnl_set_sk_err(ops->nlgroup, err);
  286. }
  287. static void attach_rules(struct list_head *rules, struct net_device *dev)
  288. {
  289. struct fib_rule *rule;
  290. list_for_each_entry(rule, rules, list) {
  291. if (rule->ifindex == -1 &&
  292. strcmp(dev->name, rule->ifname) == 0)
  293. rule->ifindex = dev->ifindex;
  294. }
  295. }
  296. static void detach_rules(struct list_head *rules, struct net_device *dev)
  297. {
  298. struct fib_rule *rule;
  299. list_for_each_entry(rule, rules, list)
  300. if (rule->ifindex == dev->ifindex)
  301. rule->ifindex = -1;
  302. }
  303. static int fib_rules_event(struct notifier_block *this, unsigned long event,
  304. void *ptr)
  305. {
  306. struct net_device *dev = ptr;
  307. struct fib_rules_ops *ops;
  308. ASSERT_RTNL();
  309. rcu_read_lock();
  310. switch (event) {
  311. case NETDEV_REGISTER:
  312. list_for_each_entry(ops, &rules_ops, list)
  313. attach_rules(ops->rules_list, dev);
  314. break;
  315. case NETDEV_UNREGISTER:
  316. list_for_each_entry(ops, &rules_ops, list)
  317. detach_rules(ops->rules_list, dev);
  318. break;
  319. }
  320. rcu_read_unlock();
  321. return NOTIFY_DONE;
  322. }
  323. static struct notifier_block fib_rules_notifier = {
  324. .notifier_call = fib_rules_event,
  325. };
  326. static int __init fib_rules_init(void)
  327. {
  328. return register_netdevice_notifier(&fib_rules_notifier);
  329. }
  330. subsys_initcall(fib_rules_init);