fib_rules.c 18 KB

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