fib_rules.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 = -ESRCH;
  116. out:
  117. rcu_read_unlock();
  118. return err;
  119. }
  120. EXPORT_SYMBOL_GPL(fib_rules_lookup);
  121. static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
  122. struct fib_rules_ops *ops)
  123. {
  124. int err = -EINVAL;
  125. if (frh->src_len)
  126. if (tb[FRA_SRC] == NULL ||
  127. frh->src_len > (ops->addr_size * 8) ||
  128. nla_len(tb[FRA_SRC]) != ops->addr_size)
  129. goto errout;
  130. if (frh->dst_len)
  131. if (tb[FRA_DST] == NULL ||
  132. frh->dst_len > (ops->addr_size * 8) ||
  133. nla_len(tb[FRA_DST]) != ops->addr_size)
  134. goto errout;
  135. err = 0;
  136. errout:
  137. return err;
  138. }
  139. int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  140. {
  141. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  142. struct fib_rules_ops *ops = NULL;
  143. struct fib_rule *rule, *r, *last = NULL;
  144. struct nlattr *tb[FRA_MAX+1];
  145. int err = -EINVAL;
  146. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  147. goto errout;
  148. ops = lookup_rules_ops(frh->family);
  149. if (ops == NULL) {
  150. err = EAFNOSUPPORT;
  151. goto errout;
  152. }
  153. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  154. if (err < 0)
  155. goto errout;
  156. err = validate_rulemsg(frh, tb, ops);
  157. if (err < 0)
  158. goto errout;
  159. rule = kzalloc(ops->rule_size, GFP_KERNEL);
  160. if (rule == NULL) {
  161. err = -ENOMEM;
  162. goto errout;
  163. }
  164. if (tb[FRA_PRIORITY])
  165. rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
  166. if (tb[FRA_IFNAME]) {
  167. struct net_device *dev;
  168. rule->ifindex = -1;
  169. nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
  170. dev = __dev_get_by_name(rule->ifname);
  171. if (dev)
  172. rule->ifindex = dev->ifindex;
  173. }
  174. if (tb[FRA_FWMARK]) {
  175. rule->mark = nla_get_u32(tb[FRA_FWMARK]);
  176. if (rule->mark)
  177. /* compatibility: if the mark value is non-zero all bits
  178. * are compared unless a mask is explicitly specified.
  179. */
  180. rule->mark_mask = 0xFFFFFFFF;
  181. }
  182. if (tb[FRA_FWMASK])
  183. rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
  184. rule->action = frh->action;
  185. rule->flags = frh->flags;
  186. rule->table = frh_get_table(frh, tb);
  187. if (!rule->pref && ops->default_pref)
  188. rule->pref = ops->default_pref();
  189. err = ops->configure(rule, skb, nlh, frh, tb);
  190. if (err < 0)
  191. goto errout_free;
  192. list_for_each_entry(r, ops->rules_list, list) {
  193. if (r->pref > rule->pref)
  194. break;
  195. last = r;
  196. }
  197. fib_rule_get(rule);
  198. if (last)
  199. list_add_rcu(&rule->list, &last->list);
  200. else
  201. list_add_rcu(&rule->list, ops->rules_list);
  202. notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
  203. rules_ops_put(ops);
  204. return 0;
  205. errout_free:
  206. kfree(rule);
  207. errout:
  208. rules_ops_put(ops);
  209. return err;
  210. }
  211. int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  212. {
  213. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  214. struct fib_rules_ops *ops = NULL;
  215. struct fib_rule *rule;
  216. struct nlattr *tb[FRA_MAX+1];
  217. int err = -EINVAL;
  218. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  219. goto errout;
  220. ops = lookup_rules_ops(frh->family);
  221. if (ops == NULL) {
  222. err = EAFNOSUPPORT;
  223. goto errout;
  224. }
  225. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  226. if (err < 0)
  227. goto errout;
  228. err = validate_rulemsg(frh, tb, ops);
  229. if (err < 0)
  230. goto errout;
  231. list_for_each_entry(rule, ops->rules_list, list) {
  232. if (frh->action && (frh->action != rule->action))
  233. continue;
  234. if (frh->table && (frh_get_table(frh, tb) != rule->table))
  235. continue;
  236. if (tb[FRA_PRIORITY] &&
  237. (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
  238. continue;
  239. if (tb[FRA_IFNAME] &&
  240. nla_strcmp(tb[FRA_IFNAME], rule->ifname))
  241. continue;
  242. if (tb[FRA_FWMARK] &&
  243. (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
  244. continue;
  245. if (tb[FRA_FWMASK] &&
  246. (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
  247. continue;
  248. if (!ops->compare(rule, frh, tb))
  249. continue;
  250. if (rule->flags & FIB_RULE_PERMANENT) {
  251. err = -EPERM;
  252. goto errout;
  253. }
  254. list_del_rcu(&rule->list);
  255. synchronize_rcu();
  256. notify_rule_change(RTM_DELRULE, rule, ops, nlh,
  257. NETLINK_CB(skb).pid);
  258. fib_rule_put(rule);
  259. rules_ops_put(ops);
  260. return 0;
  261. }
  262. err = -ENOENT;
  263. errout:
  264. rules_ops_put(ops);
  265. return err;
  266. }
  267. static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
  268. struct fib_rule *rule)
  269. {
  270. size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
  271. + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
  272. + nla_total_size(4) /* FRA_PRIORITY */
  273. + nla_total_size(4) /* FRA_TABLE */
  274. + nla_total_size(4) /* FRA_FWMARK */
  275. + nla_total_size(4); /* FRA_FWMASK */
  276. if (ops->nlmsg_payload)
  277. payload += ops->nlmsg_payload(rule);
  278. return payload;
  279. }
  280. static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
  281. u32 pid, u32 seq, int type, int flags,
  282. struct fib_rules_ops *ops)
  283. {
  284. struct nlmsghdr *nlh;
  285. struct fib_rule_hdr *frh;
  286. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
  287. if (nlh == NULL)
  288. return -EMSGSIZE;
  289. frh = nlmsg_data(nlh);
  290. frh->table = rule->table;
  291. NLA_PUT_U32(skb, FRA_TABLE, rule->table);
  292. frh->res1 = 0;
  293. frh->res2 = 0;
  294. frh->action = rule->action;
  295. frh->flags = rule->flags;
  296. if (rule->ifname[0])
  297. NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
  298. if (rule->pref)
  299. NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
  300. if (rule->mark)
  301. NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
  302. if (rule->mark_mask || rule->mark)
  303. NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
  304. if (ops->fill(rule, skb, nlh, frh) < 0)
  305. goto nla_put_failure;
  306. return nlmsg_end(skb, nlh);
  307. nla_put_failure:
  308. nlmsg_cancel(skb, nlh);
  309. return -EMSGSIZE;
  310. }
  311. int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
  312. {
  313. int idx = 0;
  314. struct fib_rule *rule;
  315. struct fib_rules_ops *ops;
  316. ops = lookup_rules_ops(family);
  317. if (ops == NULL)
  318. return -EAFNOSUPPORT;
  319. rcu_read_lock();
  320. list_for_each_entry_rcu(rule, ops->rules_list, list) {
  321. if (idx < cb->args[0])
  322. goto skip;
  323. if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
  324. cb->nlh->nlmsg_seq, RTM_NEWRULE,
  325. NLM_F_MULTI, ops) < 0)
  326. break;
  327. skip:
  328. idx++;
  329. }
  330. rcu_read_unlock();
  331. cb->args[0] = idx;
  332. rules_ops_put(ops);
  333. return skb->len;
  334. }
  335. EXPORT_SYMBOL_GPL(fib_rules_dump);
  336. static void notify_rule_change(int event, struct fib_rule *rule,
  337. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  338. u32 pid)
  339. {
  340. struct sk_buff *skb;
  341. int err = -ENOBUFS;
  342. skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
  343. if (skb == NULL)
  344. goto errout;
  345. err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
  346. if (err < 0) {
  347. /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
  348. WARN_ON(err == -EMSGSIZE);
  349. kfree_skb(skb);
  350. goto errout;
  351. }
  352. err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
  353. errout:
  354. if (err < 0)
  355. rtnl_set_sk_err(ops->nlgroup, err);
  356. }
  357. static void attach_rules(struct list_head *rules, struct net_device *dev)
  358. {
  359. struct fib_rule *rule;
  360. list_for_each_entry(rule, rules, list) {
  361. if (rule->ifindex == -1 &&
  362. strcmp(dev->name, rule->ifname) == 0)
  363. rule->ifindex = dev->ifindex;
  364. }
  365. }
  366. static void detach_rules(struct list_head *rules, struct net_device *dev)
  367. {
  368. struct fib_rule *rule;
  369. list_for_each_entry(rule, rules, list)
  370. if (rule->ifindex == dev->ifindex)
  371. rule->ifindex = -1;
  372. }
  373. static int fib_rules_event(struct notifier_block *this, unsigned long event,
  374. void *ptr)
  375. {
  376. struct net_device *dev = ptr;
  377. struct fib_rules_ops *ops;
  378. ASSERT_RTNL();
  379. rcu_read_lock();
  380. switch (event) {
  381. case NETDEV_REGISTER:
  382. list_for_each_entry(ops, &rules_ops, list)
  383. attach_rules(ops->rules_list, dev);
  384. break;
  385. case NETDEV_UNREGISTER:
  386. list_for_each_entry(ops, &rules_ops, list)
  387. detach_rules(ops->rules_list, dev);
  388. break;
  389. }
  390. rcu_read_unlock();
  391. return NOTIFY_DONE;
  392. }
  393. static struct notifier_block fib_rules_notifier = {
  394. .notifier_call = fib_rules_event,
  395. };
  396. static int __init fib_rules_init(void)
  397. {
  398. return register_netdevice_notifier(&fib_rules_notifier);
  399. }
  400. subsys_initcall(fib_rules_init);