fib_rules.c 14 KB

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