fib_rules.c 18 KB

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