fib_rules.c 16 KB

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