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