fib_rules.c 15 KB

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