fib_rules.c 13 KB

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