fib_rules.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * IPv4 Forwarding Information Base: policy rules.
  7. *
  8. * Version: $Id: fib_rules.c,v 1.17 2001/10/31 21:55:54 davem Exp $
  9. *
  10. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * Fixes:
  18. * Rani Assaf : local_rule cannot be deleted
  19. * Marc Boucher : routing by fwmark
  20. */
  21. #include <linux/config.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/system.h>
  24. #include <linux/bitops.h>
  25. #include <linux/types.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/mm.h>
  29. #include <linux/string.h>
  30. #include <linux/socket.h>
  31. #include <linux/sockios.h>
  32. #include <linux/errno.h>
  33. #include <linux/in.h>
  34. #include <linux/inet.h>
  35. #include <linux/inetdevice.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/if_arp.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/netlink.h>
  41. #include <linux/init.h>
  42. #include <linux/list.h>
  43. #include <linux/rcupdate.h>
  44. #include <net/ip.h>
  45. #include <net/protocol.h>
  46. #include <net/route.h>
  47. #include <net/tcp.h>
  48. #include <net/sock.h>
  49. #include <net/ip_fib.h>
  50. #define FRprintk(a...)
  51. struct fib_rule
  52. {
  53. struct hlist_node hlist;
  54. atomic_t r_clntref;
  55. u32 r_preference;
  56. unsigned char r_table;
  57. unsigned char r_action;
  58. unsigned char r_dst_len;
  59. unsigned char r_src_len;
  60. u32 r_src;
  61. u32 r_srcmask;
  62. u32 r_dst;
  63. u32 r_dstmask;
  64. u32 r_srcmap;
  65. u8 r_flags;
  66. u8 r_tos;
  67. #ifdef CONFIG_IP_ROUTE_FWMARK
  68. u32 r_fwmark;
  69. #endif
  70. int r_ifindex;
  71. #ifdef CONFIG_NET_CLS_ROUTE
  72. __u32 r_tclassid;
  73. #endif
  74. char r_ifname[IFNAMSIZ];
  75. int r_dead;
  76. struct rcu_head rcu;
  77. };
  78. static struct fib_rule default_rule = {
  79. .r_clntref = ATOMIC_INIT(2),
  80. .r_preference = 0x7FFF,
  81. .r_table = RT_TABLE_DEFAULT,
  82. .r_action = RTN_UNICAST,
  83. };
  84. static struct fib_rule main_rule = {
  85. .r_clntref = ATOMIC_INIT(2),
  86. .r_preference = 0x7FFE,
  87. .r_table = RT_TABLE_MAIN,
  88. .r_action = RTN_UNICAST,
  89. };
  90. static struct fib_rule local_rule = {
  91. .r_clntref = ATOMIC_INIT(2),
  92. .r_table = RT_TABLE_LOCAL,
  93. .r_action = RTN_UNICAST,
  94. };
  95. static struct hlist_head fib_rules;
  96. /* writer func called from netlink -- rtnl_sem hold*/
  97. int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  98. {
  99. struct rtattr **rta = arg;
  100. struct rtmsg *rtm = NLMSG_DATA(nlh);
  101. struct fib_rule *r;
  102. struct hlist_node *node;
  103. int err = -ESRCH;
  104. hlist_for_each_entry(r, node, &fib_rules, hlist) {
  105. if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 4) == 0) &&
  106. rtm->rtm_src_len == r->r_src_len &&
  107. rtm->rtm_dst_len == r->r_dst_len &&
  108. (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 4) == 0) &&
  109. rtm->rtm_tos == r->r_tos &&
  110. #ifdef CONFIG_IP_ROUTE_FWMARK
  111. (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
  112. #endif
  113. (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
  114. (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
  115. (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) &&
  116. (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
  117. err = -EPERM;
  118. if (r == &local_rule)
  119. break;
  120. hlist_del_rcu(&r->hlist);
  121. r->r_dead = 1;
  122. fib_rule_put(r);
  123. err = 0;
  124. break;
  125. }
  126. }
  127. return err;
  128. }
  129. /* Allocate new unique table id */
  130. static struct fib_table *fib_empty_table(void)
  131. {
  132. int id;
  133. for (id = 1; id <= RT_TABLE_MAX; id++)
  134. if (fib_tables[id] == NULL)
  135. return __fib_new_table(id);
  136. return NULL;
  137. }
  138. static inline void fib_rule_put_rcu(struct rcu_head *head)
  139. {
  140. struct fib_rule *r = container_of(head, struct fib_rule, rcu);
  141. kfree(r);
  142. }
  143. void fib_rule_put(struct fib_rule *r)
  144. {
  145. if (atomic_dec_and_test(&r->r_clntref)) {
  146. if (r->r_dead)
  147. call_rcu(&r->rcu, fib_rule_put_rcu);
  148. else
  149. printk("Freeing alive rule %p\n", r);
  150. }
  151. }
  152. /* writer func called from netlink -- rtnl_sem hold*/
  153. int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  154. {
  155. struct rtattr **rta = arg;
  156. struct rtmsg *rtm = NLMSG_DATA(nlh);
  157. struct fib_rule *r, *new_r, *last = NULL;
  158. struct hlist_node *node = NULL;
  159. unsigned char table_id;
  160. if (rtm->rtm_src_len > 32 || rtm->rtm_dst_len > 32 ||
  161. (rtm->rtm_tos & ~IPTOS_TOS_MASK))
  162. return -EINVAL;
  163. if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
  164. return -EINVAL;
  165. table_id = rtm->rtm_table;
  166. if (table_id == RT_TABLE_UNSPEC) {
  167. struct fib_table *table;
  168. if (rtm->rtm_type == RTN_UNICAST) {
  169. if ((table = fib_empty_table()) == NULL)
  170. return -ENOBUFS;
  171. table_id = table->tb_id;
  172. }
  173. }
  174. new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
  175. if (!new_r)
  176. return -ENOMEM;
  177. memset(new_r, 0, sizeof(*new_r));
  178. if (rta[RTA_SRC-1])
  179. memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 4);
  180. if (rta[RTA_DST-1])
  181. memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 4);
  182. if (rta[RTA_GATEWAY-1])
  183. memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 4);
  184. new_r->r_src_len = rtm->rtm_src_len;
  185. new_r->r_dst_len = rtm->rtm_dst_len;
  186. new_r->r_srcmask = inet_make_mask(rtm->rtm_src_len);
  187. new_r->r_dstmask = inet_make_mask(rtm->rtm_dst_len);
  188. new_r->r_tos = rtm->rtm_tos;
  189. #ifdef CONFIG_IP_ROUTE_FWMARK
  190. if (rta[RTA_PROTOINFO-1])
  191. memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
  192. #endif
  193. new_r->r_action = rtm->rtm_type;
  194. new_r->r_flags = rtm->rtm_flags;
  195. if (rta[RTA_PRIORITY-1])
  196. memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
  197. new_r->r_table = table_id;
  198. if (rta[RTA_IIF-1]) {
  199. struct net_device *dev;
  200. rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ);
  201. new_r->r_ifindex = -1;
  202. dev = __dev_get_by_name(new_r->r_ifname);
  203. if (dev)
  204. new_r->r_ifindex = dev->ifindex;
  205. }
  206. #ifdef CONFIG_NET_CLS_ROUTE
  207. if (rta[RTA_FLOW-1])
  208. memcpy(&new_r->r_tclassid, RTA_DATA(rta[RTA_FLOW-1]), 4);
  209. #endif
  210. r = container_of(fib_rules.first, struct fib_rule, hlist);
  211. if (!new_r->r_preference) {
  212. if (r && r->hlist.next != NULL) {
  213. r = container_of(r->hlist.next, struct fib_rule, hlist);
  214. if (r->r_preference)
  215. new_r->r_preference = r->r_preference - 1;
  216. }
  217. }
  218. hlist_for_each_entry(r, node, &fib_rules, hlist) {
  219. if (r->r_preference > new_r->r_preference)
  220. break;
  221. last = r;
  222. }
  223. atomic_inc(&new_r->r_clntref);
  224. if (last)
  225. hlist_add_after_rcu(&last->hlist, &new_r->hlist);
  226. else
  227. hlist_add_before_rcu(&new_r->hlist, &r->hlist);
  228. return 0;
  229. }
  230. #ifdef CONFIG_NET_CLS_ROUTE
  231. u32 fib_rules_tclass(struct fib_result *res)
  232. {
  233. if (res->r)
  234. return res->r->r_tclassid;
  235. return 0;
  236. }
  237. #endif
  238. /* callers should hold rtnl semaphore */
  239. static void fib_rules_detach(struct net_device *dev)
  240. {
  241. struct hlist_node *node;
  242. struct fib_rule *r;
  243. hlist_for_each_entry(r, node, &fib_rules, hlist) {
  244. if (r->r_ifindex == dev->ifindex)
  245. r->r_ifindex = -1;
  246. }
  247. }
  248. /* callers should hold rtnl semaphore */
  249. static void fib_rules_attach(struct net_device *dev)
  250. {
  251. struct hlist_node *node;
  252. struct fib_rule *r;
  253. hlist_for_each_entry(r, node, &fib_rules, hlist) {
  254. if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0)
  255. r->r_ifindex = dev->ifindex;
  256. }
  257. }
  258. int fib_lookup(const struct flowi *flp, struct fib_result *res)
  259. {
  260. int err;
  261. struct fib_rule *r, *policy;
  262. struct fib_table *tb;
  263. struct hlist_node *node;
  264. u32 daddr = flp->fl4_dst;
  265. u32 saddr = flp->fl4_src;
  266. FRprintk("Lookup: %u.%u.%u.%u <- %u.%u.%u.%u ",
  267. NIPQUAD(flp->fl4_dst), NIPQUAD(flp->fl4_src));
  268. rcu_read_lock();
  269. hlist_for_each_entry_rcu(r, node, &fib_rules, hlist) {
  270. if (((saddr^r->r_src) & r->r_srcmask) ||
  271. ((daddr^r->r_dst) & r->r_dstmask) ||
  272. (r->r_tos && r->r_tos != flp->fl4_tos) ||
  273. #ifdef CONFIG_IP_ROUTE_FWMARK
  274. (r->r_fwmark && r->r_fwmark != flp->fl4_fwmark) ||
  275. #endif
  276. (r->r_ifindex && r->r_ifindex != flp->iif))
  277. continue;
  278. FRprintk("tb %d r %d ", r->r_table, r->r_action);
  279. switch (r->r_action) {
  280. case RTN_UNICAST:
  281. policy = r;
  282. break;
  283. case RTN_UNREACHABLE:
  284. rcu_read_unlock();
  285. return -ENETUNREACH;
  286. default:
  287. case RTN_BLACKHOLE:
  288. rcu_read_unlock();
  289. return -EINVAL;
  290. case RTN_PROHIBIT:
  291. rcu_read_unlock();
  292. return -EACCES;
  293. }
  294. if ((tb = fib_get_table(r->r_table)) == NULL)
  295. continue;
  296. err = tb->tb_lookup(tb, flp, res);
  297. if (err == 0) {
  298. res->r = policy;
  299. if (policy)
  300. atomic_inc(&policy->r_clntref);
  301. rcu_read_unlock();
  302. return 0;
  303. }
  304. if (err < 0 && err != -EAGAIN) {
  305. rcu_read_unlock();
  306. return err;
  307. }
  308. }
  309. FRprintk("FAILURE\n");
  310. rcu_read_unlock();
  311. return -ENETUNREACH;
  312. }
  313. void fib_select_default(const struct flowi *flp, struct fib_result *res)
  314. {
  315. if (res->r && res->r->r_action == RTN_UNICAST &&
  316. FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
  317. struct fib_table *tb;
  318. if ((tb = fib_get_table(res->r->r_table)) != NULL)
  319. tb->tb_select_default(tb, flp, res);
  320. }
  321. }
  322. static int fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
  323. {
  324. struct net_device *dev = ptr;
  325. if (event == NETDEV_UNREGISTER)
  326. fib_rules_detach(dev);
  327. else if (event == NETDEV_REGISTER)
  328. fib_rules_attach(dev);
  329. return NOTIFY_DONE;
  330. }
  331. static struct notifier_block fib_rules_notifier = {
  332. .notifier_call =fib_rules_event,
  333. };
  334. static __inline__ int inet_fill_rule(struct sk_buff *skb,
  335. struct fib_rule *r,
  336. struct netlink_callback *cb,
  337. unsigned int flags)
  338. {
  339. struct rtmsg *rtm;
  340. struct nlmsghdr *nlh;
  341. unsigned char *b = skb->tail;
  342. nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags);
  343. rtm = NLMSG_DATA(nlh);
  344. rtm->rtm_family = AF_INET;
  345. rtm->rtm_dst_len = r->r_dst_len;
  346. rtm->rtm_src_len = r->r_src_len;
  347. rtm->rtm_tos = r->r_tos;
  348. #ifdef CONFIG_IP_ROUTE_FWMARK
  349. if (r->r_fwmark)
  350. RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
  351. #endif
  352. rtm->rtm_table = r->r_table;
  353. rtm->rtm_protocol = 0;
  354. rtm->rtm_scope = 0;
  355. rtm->rtm_type = r->r_action;
  356. rtm->rtm_flags = r->r_flags;
  357. if (r->r_dst_len)
  358. RTA_PUT(skb, RTA_DST, 4, &r->r_dst);
  359. if (r->r_src_len)
  360. RTA_PUT(skb, RTA_SRC, 4, &r->r_src);
  361. if (r->r_ifname[0])
  362. RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
  363. if (r->r_preference)
  364. RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
  365. if (r->r_srcmap)
  366. RTA_PUT(skb, RTA_GATEWAY, 4, &r->r_srcmap);
  367. #ifdef CONFIG_NET_CLS_ROUTE
  368. if (r->r_tclassid)
  369. RTA_PUT(skb, RTA_FLOW, 4, &r->r_tclassid);
  370. #endif
  371. nlh->nlmsg_len = skb->tail - b;
  372. return skb->len;
  373. nlmsg_failure:
  374. rtattr_failure:
  375. skb_trim(skb, b - skb->data);
  376. return -1;
  377. }
  378. /* callers should hold rtnl semaphore */
  379. int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
  380. {
  381. int idx = 0;
  382. int s_idx = cb->args[0];
  383. struct fib_rule *r;
  384. struct hlist_node *node;
  385. rcu_read_lock();
  386. hlist_for_each_entry(r, node, &fib_rules, hlist) {
  387. if (idx < s_idx)
  388. continue;
  389. if (inet_fill_rule(skb, r, cb, NLM_F_MULTI) < 0)
  390. break;
  391. idx++;
  392. }
  393. rcu_read_unlock();
  394. cb->args[0] = idx;
  395. return skb->len;
  396. }
  397. void __init fib_rules_init(void)
  398. {
  399. INIT_HLIST_HEAD(&fib_rules);
  400. hlist_add_head(&local_rule.hlist, &fib_rules);
  401. hlist_add_after(&local_rule.hlist, &main_rule.hlist);
  402. hlist_add_after(&main_rule.hlist, &default_rule.hlist);
  403. register_netdevice_notifier(&fib_rules_notifier);
  404. }