fib_rules.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. static void flush_route_cache(struct fib_rules_ops *ops)
  40. {
  41. if (ops->flush_cache)
  42. ops->flush_cache();
  43. }
  44. int fib_rules_register(struct fib_rules_ops *ops)
  45. {
  46. int err = -EEXIST;
  47. struct fib_rules_ops *o;
  48. if (ops->rule_size < sizeof(struct fib_rule))
  49. return -EINVAL;
  50. if (ops->match == NULL || ops->configure == NULL ||
  51. ops->compare == NULL || ops->fill == NULL ||
  52. ops->action == NULL)
  53. return -EINVAL;
  54. spin_lock(&rules_mod_lock);
  55. list_for_each_entry(o, &rules_ops, list)
  56. if (ops->family == o->family)
  57. goto errout;
  58. list_add_tail_rcu(&ops->list, &rules_ops);
  59. err = 0;
  60. errout:
  61. spin_unlock(&rules_mod_lock);
  62. return err;
  63. }
  64. EXPORT_SYMBOL_GPL(fib_rules_register);
  65. static void cleanup_ops(struct fib_rules_ops *ops)
  66. {
  67. struct fib_rule *rule, *tmp;
  68. list_for_each_entry_safe(rule, tmp, ops->rules_list, list) {
  69. list_del_rcu(&rule->list);
  70. fib_rule_put(rule);
  71. }
  72. }
  73. int fib_rules_unregister(struct fib_rules_ops *ops)
  74. {
  75. int err = 0;
  76. struct fib_rules_ops *o;
  77. spin_lock(&rules_mod_lock);
  78. list_for_each_entry(o, &rules_ops, list) {
  79. if (o == ops) {
  80. list_del_rcu(&o->list);
  81. cleanup_ops(ops);
  82. goto out;
  83. }
  84. }
  85. err = -ENOENT;
  86. out:
  87. spin_unlock(&rules_mod_lock);
  88. synchronize_rcu();
  89. return err;
  90. }
  91. EXPORT_SYMBOL_GPL(fib_rules_unregister);
  92. static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
  93. struct flowi *fl, int flags)
  94. {
  95. int ret = 0;
  96. if (rule->ifindex && (rule->ifindex != fl->iif))
  97. goto out;
  98. if ((rule->mark ^ fl->mark) & rule->mark_mask)
  99. goto out;
  100. ret = ops->match(rule, fl, flags);
  101. out:
  102. return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
  103. }
  104. int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
  105. int flags, struct fib_lookup_arg *arg)
  106. {
  107. struct fib_rule *rule;
  108. int err;
  109. rcu_read_lock();
  110. list_for_each_entry_rcu(rule, ops->rules_list, list) {
  111. jumped:
  112. if (!fib_rule_match(rule, ops, fl, flags))
  113. continue;
  114. if (rule->action == FR_ACT_GOTO) {
  115. struct fib_rule *target;
  116. target = rcu_dereference(rule->ctarget);
  117. if (target == NULL) {
  118. continue;
  119. } else {
  120. rule = target;
  121. goto jumped;
  122. }
  123. } else if (rule->action == FR_ACT_NOP)
  124. continue;
  125. else
  126. err = ops->action(rule, fl, flags, arg);
  127. if (err != -EAGAIN) {
  128. fib_rule_get(rule);
  129. arg->rule = rule;
  130. goto out;
  131. }
  132. }
  133. err = -ESRCH;
  134. out:
  135. rcu_read_unlock();
  136. return err;
  137. }
  138. EXPORT_SYMBOL_GPL(fib_rules_lookup);
  139. static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
  140. struct fib_rules_ops *ops)
  141. {
  142. int err = -EINVAL;
  143. if (frh->src_len)
  144. if (tb[FRA_SRC] == NULL ||
  145. frh->src_len > (ops->addr_size * 8) ||
  146. nla_len(tb[FRA_SRC]) != ops->addr_size)
  147. goto errout;
  148. if (frh->dst_len)
  149. if (tb[FRA_DST] == NULL ||
  150. frh->dst_len > (ops->addr_size * 8) ||
  151. nla_len(tb[FRA_DST]) != ops->addr_size)
  152. goto errout;
  153. err = 0;
  154. errout:
  155. return err;
  156. }
  157. static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  158. {
  159. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  160. struct fib_rules_ops *ops = NULL;
  161. struct fib_rule *rule, *r, *last = NULL;
  162. struct nlattr *tb[FRA_MAX+1];
  163. int err = -EINVAL, unresolved = 0;
  164. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  165. goto errout;
  166. ops = lookup_rules_ops(frh->family);
  167. if (ops == NULL) {
  168. err = EAFNOSUPPORT;
  169. goto errout;
  170. }
  171. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  172. if (err < 0)
  173. goto errout;
  174. err = validate_rulemsg(frh, tb, ops);
  175. if (err < 0)
  176. goto errout;
  177. rule = kzalloc(ops->rule_size, GFP_KERNEL);
  178. if (rule == NULL) {
  179. err = -ENOMEM;
  180. goto errout;
  181. }
  182. if (tb[FRA_PRIORITY])
  183. rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
  184. if (tb[FRA_IFNAME]) {
  185. struct net_device *dev;
  186. rule->ifindex = -1;
  187. nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
  188. dev = __dev_get_by_name(rule->ifname);
  189. if (dev)
  190. rule->ifindex = dev->ifindex;
  191. }
  192. if (tb[FRA_FWMARK]) {
  193. rule->mark = nla_get_u32(tb[FRA_FWMARK]);
  194. if (rule->mark)
  195. /* compatibility: if the mark value is non-zero all bits
  196. * are compared unless a mask is explicitly specified.
  197. */
  198. rule->mark_mask = 0xFFFFFFFF;
  199. }
  200. if (tb[FRA_FWMASK])
  201. rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
  202. rule->action = frh->action;
  203. rule->flags = frh->flags;
  204. rule->table = frh_get_table(frh, tb);
  205. if (!rule->pref && ops->default_pref)
  206. rule->pref = ops->default_pref();
  207. err = -EINVAL;
  208. if (tb[FRA_GOTO]) {
  209. if (rule->action != FR_ACT_GOTO)
  210. goto errout_free;
  211. rule->target = nla_get_u32(tb[FRA_GOTO]);
  212. /* Backward jumps are prohibited to avoid endless loops */
  213. if (rule->target <= rule->pref)
  214. goto errout_free;
  215. list_for_each_entry(r, ops->rules_list, list) {
  216. if (r->pref == rule->target) {
  217. rule->ctarget = r;
  218. break;
  219. }
  220. }
  221. if (rule->ctarget == NULL)
  222. unresolved = 1;
  223. } else if (rule->action == FR_ACT_GOTO)
  224. goto errout_free;
  225. err = ops->configure(rule, skb, nlh, frh, tb);
  226. if (err < 0)
  227. goto errout_free;
  228. list_for_each_entry(r, ops->rules_list, list) {
  229. if (r->pref > rule->pref)
  230. break;
  231. last = r;
  232. }
  233. fib_rule_get(rule);
  234. if (ops->unresolved_rules) {
  235. /*
  236. * There are unresolved goto rules in the list, check if
  237. * any of them are pointing to this new rule.
  238. */
  239. list_for_each_entry(r, ops->rules_list, list) {
  240. if (r->action == FR_ACT_GOTO &&
  241. r->target == rule->pref) {
  242. BUG_ON(r->ctarget != NULL);
  243. rcu_assign_pointer(r->ctarget, rule);
  244. if (--ops->unresolved_rules == 0)
  245. break;
  246. }
  247. }
  248. }
  249. if (rule->action == FR_ACT_GOTO)
  250. ops->nr_goto_rules++;
  251. if (unresolved)
  252. ops->unresolved_rules++;
  253. if (last)
  254. list_add_rcu(&rule->list, &last->list);
  255. else
  256. list_add_rcu(&rule->list, ops->rules_list);
  257. notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
  258. flush_route_cache(ops);
  259. rules_ops_put(ops);
  260. return 0;
  261. errout_free:
  262. kfree(rule);
  263. errout:
  264. rules_ops_put(ops);
  265. return err;
  266. }
  267. static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  268. {
  269. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  270. struct fib_rules_ops *ops = NULL;
  271. struct fib_rule *rule, *tmp;
  272. struct nlattr *tb[FRA_MAX+1];
  273. int err = -EINVAL;
  274. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  275. goto errout;
  276. ops = lookup_rules_ops(frh->family);
  277. if (ops == NULL) {
  278. err = EAFNOSUPPORT;
  279. goto errout;
  280. }
  281. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  282. if (err < 0)
  283. goto errout;
  284. err = validate_rulemsg(frh, tb, ops);
  285. if (err < 0)
  286. goto errout;
  287. list_for_each_entry(rule, ops->rules_list, list) {
  288. if (frh->action && (frh->action != rule->action))
  289. continue;
  290. if (frh->table && (frh_get_table(frh, tb) != rule->table))
  291. continue;
  292. if (tb[FRA_PRIORITY] &&
  293. (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
  294. continue;
  295. if (tb[FRA_IFNAME] &&
  296. nla_strcmp(tb[FRA_IFNAME], rule->ifname))
  297. continue;
  298. if (tb[FRA_FWMARK] &&
  299. (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
  300. continue;
  301. if (tb[FRA_FWMASK] &&
  302. (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
  303. continue;
  304. if (!ops->compare(rule, frh, tb))
  305. continue;
  306. if (rule->flags & FIB_RULE_PERMANENT) {
  307. err = -EPERM;
  308. goto errout;
  309. }
  310. list_del_rcu(&rule->list);
  311. if (rule->action == FR_ACT_GOTO)
  312. ops->nr_goto_rules--;
  313. /*
  314. * Check if this rule is a target to any of them. If so,
  315. * disable them. As this operation is eventually very
  316. * expensive, it is only performed if goto rules have
  317. * actually been added.
  318. */
  319. if (ops->nr_goto_rules > 0) {
  320. list_for_each_entry(tmp, ops->rules_list, list) {
  321. if (tmp->ctarget == rule) {
  322. rcu_assign_pointer(tmp->ctarget, NULL);
  323. ops->unresolved_rules++;
  324. }
  325. }
  326. }
  327. synchronize_rcu();
  328. notify_rule_change(RTM_DELRULE, rule, ops, nlh,
  329. NETLINK_CB(skb).pid);
  330. fib_rule_put(rule);
  331. flush_route_cache(ops);
  332. rules_ops_put(ops);
  333. return 0;
  334. }
  335. err = -ENOENT;
  336. errout:
  337. rules_ops_put(ops);
  338. return err;
  339. }
  340. static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
  341. struct fib_rule *rule)
  342. {
  343. size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
  344. + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
  345. + nla_total_size(4) /* FRA_PRIORITY */
  346. + nla_total_size(4) /* FRA_TABLE */
  347. + nla_total_size(4) /* FRA_FWMARK */
  348. + nla_total_size(4); /* FRA_FWMASK */
  349. if (ops->nlmsg_payload)
  350. payload += ops->nlmsg_payload(rule);
  351. return payload;
  352. }
  353. static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
  354. u32 pid, u32 seq, int type, int flags,
  355. struct fib_rules_ops *ops)
  356. {
  357. struct nlmsghdr *nlh;
  358. struct fib_rule_hdr *frh;
  359. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
  360. if (nlh == NULL)
  361. return -EMSGSIZE;
  362. frh = nlmsg_data(nlh);
  363. frh->table = rule->table;
  364. NLA_PUT_U32(skb, FRA_TABLE, rule->table);
  365. frh->res1 = 0;
  366. frh->res2 = 0;
  367. frh->action = rule->action;
  368. frh->flags = rule->flags;
  369. if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
  370. frh->flags |= FIB_RULE_UNRESOLVED;
  371. if (rule->ifname[0]) {
  372. NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
  373. if (rule->ifindex == -1)
  374. frh->flags |= FIB_RULE_DEV_DETACHED;
  375. }
  376. if (rule->pref)
  377. NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
  378. if (rule->mark)
  379. NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
  380. if (rule->mark_mask || rule->mark)
  381. NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
  382. if (rule->target)
  383. NLA_PUT_U32(skb, FRA_GOTO, rule->target);
  384. if (ops->fill(rule, skb, nlh, frh) < 0)
  385. goto nla_put_failure;
  386. return nlmsg_end(skb, nlh);
  387. nla_put_failure:
  388. nlmsg_cancel(skb, nlh);
  389. return -EMSGSIZE;
  390. }
  391. static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
  392. struct fib_rules_ops *ops)
  393. {
  394. int idx = 0;
  395. struct fib_rule *rule;
  396. list_for_each_entry(rule, ops->rules_list, list) {
  397. if (idx < cb->args[1])
  398. goto skip;
  399. if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
  400. cb->nlh->nlmsg_seq, RTM_NEWRULE,
  401. NLM_F_MULTI, ops) < 0)
  402. break;
  403. skip:
  404. idx++;
  405. }
  406. cb->args[1] = idx;
  407. rules_ops_put(ops);
  408. return skb->len;
  409. }
  410. static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
  411. {
  412. struct fib_rules_ops *ops;
  413. int idx = 0, family;
  414. family = rtnl_msg_family(cb->nlh);
  415. if (family != AF_UNSPEC) {
  416. /* Protocol specific dump request */
  417. ops = lookup_rules_ops(family);
  418. if (ops == NULL)
  419. return -EAFNOSUPPORT;
  420. return dump_rules(skb, cb, ops);
  421. }
  422. rcu_read_lock();
  423. list_for_each_entry_rcu(ops, &rules_ops, list) {
  424. if (idx < cb->args[0] || !try_module_get(ops->owner))
  425. goto skip;
  426. if (dump_rules(skb, cb, ops) < 0)
  427. break;
  428. cb->args[1] = 0;
  429. skip:
  430. idx++;
  431. }
  432. rcu_read_unlock();
  433. cb->args[0] = idx;
  434. return skb->len;
  435. }
  436. static void notify_rule_change(int event, struct fib_rule *rule,
  437. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  438. u32 pid)
  439. {
  440. struct sk_buff *skb;
  441. int err = -ENOBUFS;
  442. skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
  443. if (skb == NULL)
  444. goto errout;
  445. err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
  446. if (err < 0) {
  447. /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
  448. WARN_ON(err == -EMSGSIZE);
  449. kfree_skb(skb);
  450. goto errout;
  451. }
  452. err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
  453. errout:
  454. if (err < 0)
  455. rtnl_set_sk_err(ops->nlgroup, err);
  456. }
  457. static void attach_rules(struct list_head *rules, struct net_device *dev)
  458. {
  459. struct fib_rule *rule;
  460. list_for_each_entry(rule, rules, list) {
  461. if (rule->ifindex == -1 &&
  462. strcmp(dev->name, rule->ifname) == 0)
  463. rule->ifindex = dev->ifindex;
  464. }
  465. }
  466. static void detach_rules(struct list_head *rules, struct net_device *dev)
  467. {
  468. struct fib_rule *rule;
  469. list_for_each_entry(rule, rules, list)
  470. if (rule->ifindex == dev->ifindex)
  471. rule->ifindex = -1;
  472. }
  473. static int fib_rules_event(struct notifier_block *this, unsigned long event,
  474. void *ptr)
  475. {
  476. struct net_device *dev = ptr;
  477. struct fib_rules_ops *ops;
  478. ASSERT_RTNL();
  479. rcu_read_lock();
  480. switch (event) {
  481. case NETDEV_REGISTER:
  482. list_for_each_entry(ops, &rules_ops, list)
  483. attach_rules(ops->rules_list, dev);
  484. break;
  485. case NETDEV_UNREGISTER:
  486. list_for_each_entry(ops, &rules_ops, list)
  487. detach_rules(ops->rules_list, dev);
  488. break;
  489. }
  490. rcu_read_unlock();
  491. return NOTIFY_DONE;
  492. }
  493. static struct notifier_block fib_rules_notifier = {
  494. .notifier_call = fib_rules_event,
  495. };
  496. static int __init fib_rules_init(void)
  497. {
  498. rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
  499. rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
  500. rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
  501. return register_netdevice_notifier(&fib_rules_notifier);
  502. }
  503. subsys_initcall(fib_rules_init);