fib_rules.c 14 KB

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