fib_rules.c 19 KB

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