fib_rules.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. 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. r->fr_net = hold_net(ops->fro_net);
  31. /* The lock is not required here, the list in unreacheable
  32. * at the moment this function is called */
  33. list_add_tail(&r->list, &ops->rules_list);
  34. return 0;
  35. }
  36. EXPORT_SYMBOL(fib_default_rule_add);
  37. u32 fib_default_rule_pref(struct fib_rules_ops *ops)
  38. {
  39. struct list_head *pos;
  40. struct fib_rule *rule;
  41. if (!list_empty(&ops->rules_list)) {
  42. pos = ops->rules_list.next;
  43. if (pos->next != &ops->rules_list) {
  44. rule = list_entry(pos->next, struct fib_rule, list);
  45. if (rule->pref)
  46. return rule->pref - 1;
  47. }
  48. }
  49. return 0;
  50. }
  51. EXPORT_SYMBOL(fib_default_rule_pref);
  52. static void notify_rule_change(int event, struct fib_rule *rule,
  53. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  54. u32 pid);
  55. static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
  56. {
  57. struct fib_rules_ops *ops;
  58. rcu_read_lock();
  59. list_for_each_entry_rcu(ops, &net->rules_ops, list) {
  60. if (ops->family == family) {
  61. if (!try_module_get(ops->owner))
  62. ops = NULL;
  63. rcu_read_unlock();
  64. return ops;
  65. }
  66. }
  67. rcu_read_unlock();
  68. return NULL;
  69. }
  70. static void rules_ops_put(struct fib_rules_ops *ops)
  71. {
  72. if (ops)
  73. module_put(ops->owner);
  74. }
  75. static void flush_route_cache(struct fib_rules_ops *ops)
  76. {
  77. if (ops->flush_cache)
  78. ops->flush_cache(ops);
  79. }
  80. static int __fib_rules_register(struct fib_rules_ops *ops)
  81. {
  82. int err = -EEXIST;
  83. struct fib_rules_ops *o;
  84. struct net *net;
  85. net = ops->fro_net;
  86. if (ops->rule_size < sizeof(struct fib_rule))
  87. return -EINVAL;
  88. if (ops->match == NULL || ops->configure == NULL ||
  89. ops->compare == NULL || ops->fill == NULL ||
  90. ops->action == NULL)
  91. return -EINVAL;
  92. spin_lock(&net->rules_mod_lock);
  93. list_for_each_entry(o, &net->rules_ops, list)
  94. if (ops->family == o->family)
  95. goto errout;
  96. hold_net(net);
  97. list_add_tail_rcu(&ops->list, &net->rules_ops);
  98. err = 0;
  99. errout:
  100. spin_unlock(&net->rules_mod_lock);
  101. return err;
  102. }
  103. struct fib_rules_ops *
  104. fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
  105. {
  106. struct fib_rules_ops *ops;
  107. int err;
  108. ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
  109. if (ops == NULL)
  110. return ERR_PTR(-ENOMEM);
  111. INIT_LIST_HEAD(&ops->rules_list);
  112. ops->fro_net = net;
  113. err = __fib_rules_register(ops);
  114. if (err) {
  115. kfree(ops);
  116. ops = ERR_PTR(err);
  117. }
  118. return ops;
  119. }
  120. EXPORT_SYMBOL_GPL(fib_rules_register);
  121. static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
  122. {
  123. struct fib_rule *rule, *tmp;
  124. list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
  125. list_del_rcu(&rule->list);
  126. fib_rule_put(rule);
  127. }
  128. }
  129. static void fib_rules_put_rcu(struct rcu_head *head)
  130. {
  131. struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
  132. struct net *net = ops->fro_net;
  133. release_net(net);
  134. kfree(ops);
  135. }
  136. void fib_rules_unregister(struct fib_rules_ops *ops)
  137. {
  138. struct net *net = ops->fro_net;
  139. spin_lock(&net->rules_mod_lock);
  140. list_del_rcu(&ops->list);
  141. fib_rules_cleanup_ops(ops);
  142. spin_unlock(&net->rules_mod_lock);
  143. call_rcu(&ops->rcu, fib_rules_put_rcu);
  144. }
  145. EXPORT_SYMBOL_GPL(fib_rules_unregister);
  146. static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
  147. struct flowi *fl, int flags)
  148. {
  149. int ret = 0;
  150. if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
  151. goto out;
  152. if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
  153. goto out;
  154. if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
  155. goto out;
  156. ret = ops->match(rule, fl, flags);
  157. out:
  158. return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
  159. }
  160. int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
  161. int flags, struct fib_lookup_arg *arg)
  162. {
  163. struct fib_rule *rule;
  164. int err;
  165. rcu_read_lock();
  166. list_for_each_entry_rcu(rule, &ops->rules_list, list) {
  167. jumped:
  168. if (!fib_rule_match(rule, ops, fl, flags))
  169. continue;
  170. if (rule->action == FR_ACT_GOTO) {
  171. struct fib_rule *target;
  172. target = rcu_dereference(rule->ctarget);
  173. if (target == NULL) {
  174. continue;
  175. } else {
  176. rule = target;
  177. goto jumped;
  178. }
  179. } else if (rule->action == FR_ACT_NOP)
  180. continue;
  181. else
  182. err = ops->action(rule, fl, flags, arg);
  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, void *arg)
  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 = hold_net(net);
  243. if (tb[FRA_PRIORITY])
  244. rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
  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. rule->action = frh->action;
  272. rule->flags = frh->flags;
  273. rule->table = frh_get_table(frh, tb);
  274. if (!tb[FRA_PRIORITY] && ops->default_pref)
  275. rule->pref = ops->default_pref(ops);
  276. err = -EINVAL;
  277. if (tb[FRA_GOTO]) {
  278. if (rule->action != FR_ACT_GOTO)
  279. goto errout_free;
  280. rule->target = nla_get_u32(tb[FRA_GOTO]);
  281. /* Backward jumps are prohibited to avoid endless loops */
  282. if (rule->target <= rule->pref)
  283. goto errout_free;
  284. list_for_each_entry(r, &ops->rules_list, list) {
  285. if (r->pref == rule->target) {
  286. RCU_INIT_POINTER(rule->ctarget, r);
  287. break;
  288. }
  289. }
  290. if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
  291. unresolved = 1;
  292. } else if (rule->action == FR_ACT_GOTO)
  293. goto errout_free;
  294. err = ops->configure(rule, skb, frh, tb);
  295. if (err < 0)
  296. goto errout_free;
  297. list_for_each_entry(r, &ops->rules_list, list) {
  298. if (r->pref > rule->pref)
  299. break;
  300. last = r;
  301. }
  302. fib_rule_get(rule);
  303. if (last)
  304. list_add_rcu(&rule->list, &last->list);
  305. else
  306. list_add_rcu(&rule->list, &ops->rules_list);
  307. if (ops->unresolved_rules) {
  308. /*
  309. * There are unresolved goto rules in the list, check if
  310. * any of them are pointing to this new rule.
  311. */
  312. list_for_each_entry(r, &ops->rules_list, list) {
  313. if (r->action == FR_ACT_GOTO &&
  314. r->target == rule->pref &&
  315. rtnl_dereference(r->ctarget) == NULL) {
  316. rcu_assign_pointer(r->ctarget, rule);
  317. if (--ops->unresolved_rules == 0)
  318. break;
  319. }
  320. }
  321. }
  322. if (rule->action == FR_ACT_GOTO)
  323. ops->nr_goto_rules++;
  324. if (unresolved)
  325. ops->unresolved_rules++;
  326. notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
  327. flush_route_cache(ops);
  328. rules_ops_put(ops);
  329. return 0;
  330. errout_free:
  331. release_net(rule->fr_net);
  332. kfree(rule);
  333. errout:
  334. rules_ops_put(ops);
  335. return err;
  336. }
  337. static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  338. {
  339. struct net *net = sock_net(skb->sk);
  340. struct fib_rule_hdr *frh = nlmsg_data(nlh);
  341. struct fib_rules_ops *ops = NULL;
  342. struct fib_rule *rule, *tmp;
  343. struct nlattr *tb[FRA_MAX+1];
  344. int err = -EINVAL;
  345. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
  346. goto errout;
  347. ops = lookup_rules_ops(net, frh->family);
  348. if (ops == NULL) {
  349. err = -EAFNOSUPPORT;
  350. goto errout;
  351. }
  352. err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
  353. if (err < 0)
  354. goto errout;
  355. err = validate_rulemsg(frh, tb, ops);
  356. if (err < 0)
  357. goto errout;
  358. list_for_each_entry(rule, &ops->rules_list, list) {
  359. if (frh->action && (frh->action != rule->action))
  360. continue;
  361. if (frh->table && (frh_get_table(frh, tb) != rule->table))
  362. continue;
  363. if (tb[FRA_PRIORITY] &&
  364. (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
  365. continue;
  366. if (tb[FRA_IIFNAME] &&
  367. nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
  368. continue;
  369. if (tb[FRA_OIFNAME] &&
  370. nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
  371. continue;
  372. if (tb[FRA_FWMARK] &&
  373. (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
  374. continue;
  375. if (tb[FRA_FWMASK] &&
  376. (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
  377. continue;
  378. if (!ops->compare(rule, frh, tb))
  379. continue;
  380. if (rule->flags & FIB_RULE_PERMANENT) {
  381. err = -EPERM;
  382. goto errout;
  383. }
  384. list_del_rcu(&rule->list);
  385. if (rule->action == FR_ACT_GOTO) {
  386. ops->nr_goto_rules--;
  387. if (rtnl_dereference(rule->ctarget) == NULL)
  388. ops->unresolved_rules--;
  389. }
  390. /*
  391. * Check if this rule is a target to any of them. If so,
  392. * disable them. As this operation is eventually very
  393. * expensive, it is only performed if goto rules have
  394. * actually been added.
  395. */
  396. if (ops->nr_goto_rules > 0) {
  397. list_for_each_entry(tmp, &ops->rules_list, list) {
  398. if (rtnl_dereference(tmp->ctarget) == rule) {
  399. RCU_INIT_POINTER(tmp->ctarget, NULL);
  400. ops->unresolved_rules++;
  401. }
  402. }
  403. }
  404. notify_rule_change(RTM_DELRULE, rule, ops, nlh,
  405. NETLINK_CB(skb).pid);
  406. fib_rule_put(rule);
  407. flush_route_cache(ops);
  408. rules_ops_put(ops);
  409. return 0;
  410. }
  411. err = -ENOENT;
  412. errout:
  413. rules_ops_put(ops);
  414. return err;
  415. }
  416. static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
  417. struct fib_rule *rule)
  418. {
  419. size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
  420. + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
  421. + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
  422. + nla_total_size(4) /* FRA_PRIORITY */
  423. + nla_total_size(4) /* FRA_TABLE */
  424. + nla_total_size(4) /* FRA_FWMARK */
  425. + nla_total_size(4); /* FRA_FWMASK */
  426. if (ops->nlmsg_payload)
  427. payload += ops->nlmsg_payload(rule);
  428. return payload;
  429. }
  430. static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
  431. u32 pid, u32 seq, int type, int flags,
  432. struct fib_rules_ops *ops)
  433. {
  434. struct nlmsghdr *nlh;
  435. struct fib_rule_hdr *frh;
  436. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
  437. if (nlh == NULL)
  438. return -EMSGSIZE;
  439. frh = nlmsg_data(nlh);
  440. frh->family = ops->family;
  441. frh->table = rule->table;
  442. NLA_PUT_U32(skb, FRA_TABLE, rule->table);
  443. frh->res1 = 0;
  444. frh->res2 = 0;
  445. frh->action = rule->action;
  446. frh->flags = rule->flags;
  447. if (rule->action == FR_ACT_GOTO &&
  448. rcu_access_pointer(rule->ctarget) == NULL)
  449. frh->flags |= FIB_RULE_UNRESOLVED;
  450. if (rule->iifname[0]) {
  451. NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname);
  452. if (rule->iifindex == -1)
  453. frh->flags |= FIB_RULE_IIF_DETACHED;
  454. }
  455. if (rule->oifname[0]) {
  456. NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname);
  457. if (rule->oifindex == -1)
  458. frh->flags |= FIB_RULE_OIF_DETACHED;
  459. }
  460. if (rule->pref)
  461. NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
  462. if (rule->mark)
  463. NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
  464. if (rule->mark_mask || rule->mark)
  465. NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
  466. if (rule->target)
  467. NLA_PUT_U32(skb, FRA_GOTO, rule->target);
  468. if (ops->fill(rule, skb, frh) < 0)
  469. goto nla_put_failure;
  470. return nlmsg_end(skb, nlh);
  471. nla_put_failure:
  472. nlmsg_cancel(skb, nlh);
  473. return -EMSGSIZE;
  474. }
  475. static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
  476. struct fib_rules_ops *ops)
  477. {
  478. int idx = 0;
  479. struct fib_rule *rule;
  480. rcu_read_lock();
  481. list_for_each_entry_rcu(rule, &ops->rules_list, list) {
  482. if (idx < cb->args[1])
  483. goto skip;
  484. if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
  485. cb->nlh->nlmsg_seq, RTM_NEWRULE,
  486. NLM_F_MULTI, ops) < 0)
  487. break;
  488. skip:
  489. idx++;
  490. }
  491. rcu_read_unlock();
  492. cb->args[1] = idx;
  493. rules_ops_put(ops);
  494. return skb->len;
  495. }
  496. static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
  497. {
  498. struct net *net = sock_net(skb->sk);
  499. struct fib_rules_ops *ops;
  500. int idx = 0, family;
  501. family = rtnl_msg_family(cb->nlh);
  502. if (family != AF_UNSPEC) {
  503. /* Protocol specific dump request */
  504. ops = lookup_rules_ops(net, family);
  505. if (ops == NULL)
  506. return -EAFNOSUPPORT;
  507. return dump_rules(skb, cb, ops);
  508. }
  509. rcu_read_lock();
  510. list_for_each_entry_rcu(ops, &net->rules_ops, list) {
  511. if (idx < cb->args[0] || !try_module_get(ops->owner))
  512. goto skip;
  513. if (dump_rules(skb, cb, ops) < 0)
  514. break;
  515. cb->args[1] = 0;
  516. skip:
  517. idx++;
  518. }
  519. rcu_read_unlock();
  520. cb->args[0] = idx;
  521. return skb->len;
  522. }
  523. static void notify_rule_change(int event, struct fib_rule *rule,
  524. struct fib_rules_ops *ops, struct nlmsghdr *nlh,
  525. u32 pid)
  526. {
  527. struct net *net;
  528. struct sk_buff *skb;
  529. int err = -ENOBUFS;
  530. net = ops->fro_net;
  531. skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
  532. if (skb == NULL)
  533. goto errout;
  534. err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
  535. if (err < 0) {
  536. /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
  537. WARN_ON(err == -EMSGSIZE);
  538. kfree_skb(skb);
  539. goto errout;
  540. }
  541. rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
  542. return;
  543. errout:
  544. if (err < 0)
  545. rtnl_set_sk_err(net, ops->nlgroup, err);
  546. }
  547. static void attach_rules(struct list_head *rules, struct net_device *dev)
  548. {
  549. struct fib_rule *rule;
  550. list_for_each_entry(rule, rules, list) {
  551. if (rule->iifindex == -1 &&
  552. strcmp(dev->name, rule->iifname) == 0)
  553. rule->iifindex = dev->ifindex;
  554. if (rule->oifindex == -1 &&
  555. strcmp(dev->name, rule->oifname) == 0)
  556. rule->oifindex = dev->ifindex;
  557. }
  558. }
  559. static void detach_rules(struct list_head *rules, struct net_device *dev)
  560. {
  561. struct fib_rule *rule;
  562. list_for_each_entry(rule, rules, list) {
  563. if (rule->iifindex == dev->ifindex)
  564. rule->iifindex = -1;
  565. if (rule->oifindex == dev->ifindex)
  566. rule->oifindex = -1;
  567. }
  568. }
  569. static int fib_rules_event(struct notifier_block *this, unsigned long event,
  570. void *ptr)
  571. {
  572. struct net_device *dev = ptr;
  573. struct net *net = dev_net(dev);
  574. struct fib_rules_ops *ops;
  575. ASSERT_RTNL();
  576. switch (event) {
  577. case NETDEV_REGISTER:
  578. list_for_each_entry(ops, &net->rules_ops, list)
  579. attach_rules(&ops->rules_list, dev);
  580. break;
  581. case NETDEV_UNREGISTER:
  582. list_for_each_entry(ops, &net->rules_ops, list)
  583. detach_rules(&ops->rules_list, dev);
  584. break;
  585. }
  586. return NOTIFY_DONE;
  587. }
  588. static struct notifier_block fib_rules_notifier = {
  589. .notifier_call = fib_rules_event,
  590. };
  591. static int __net_init fib_rules_net_init(struct net *net)
  592. {
  593. INIT_LIST_HEAD(&net->rules_ops);
  594. spin_lock_init(&net->rules_mod_lock);
  595. return 0;
  596. }
  597. static struct pernet_operations fib_rules_net_ops = {
  598. .init = fib_rules_net_init,
  599. };
  600. static int __init fib_rules_init(void)
  601. {
  602. int err;
  603. rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
  604. rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
  605. rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
  606. err = register_pernet_subsys(&fib_rules_net_ops);
  607. if (err < 0)
  608. goto fail;
  609. err = register_netdevice_notifier(&fib_rules_notifier);
  610. if (err < 0)
  611. goto fail_unregister;
  612. return 0;
  613. fail_unregister:
  614. unregister_pernet_subsys(&fib_rules_net_ops);
  615. fail:
  616. rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
  617. rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
  618. rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
  619. return err;
  620. }
  621. subsys_initcall(fib_rules_init);