fib_rules.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <net/ip.h>
  43. #include <net/protocol.h>
  44. #include <net/route.h>
  45. #include <net/tcp.h>
  46. #include <net/sock.h>
  47. #include <net/ip_fib.h>
  48. #define FRprintk(a...)
  49. struct fib_rule
  50. {
  51. struct fib_rule *r_next;
  52. atomic_t r_clntref;
  53. u32 r_preference;
  54. unsigned char r_table;
  55. unsigned char r_action;
  56. unsigned char r_dst_len;
  57. unsigned char r_src_len;
  58. u32 r_src;
  59. u32 r_srcmask;
  60. u32 r_dst;
  61. u32 r_dstmask;
  62. u32 r_srcmap;
  63. u8 r_flags;
  64. u8 r_tos;
  65. #ifdef CONFIG_IP_ROUTE_FWMARK
  66. u32 r_fwmark;
  67. #endif
  68. int r_ifindex;
  69. #ifdef CONFIG_NET_CLS_ROUTE
  70. __u32 r_tclassid;
  71. #endif
  72. char r_ifname[IFNAMSIZ];
  73. int r_dead;
  74. };
  75. static struct fib_rule default_rule = {
  76. .r_clntref = ATOMIC_INIT(2),
  77. .r_preference = 0x7FFF,
  78. .r_table = RT_TABLE_DEFAULT,
  79. .r_action = RTN_UNICAST,
  80. };
  81. static struct fib_rule main_rule = {
  82. .r_next = &default_rule,
  83. .r_clntref = ATOMIC_INIT(2),
  84. .r_preference = 0x7FFE,
  85. .r_table = RT_TABLE_MAIN,
  86. .r_action = RTN_UNICAST,
  87. };
  88. static struct fib_rule local_rule = {
  89. .r_next = &main_rule,
  90. .r_clntref = ATOMIC_INIT(2),
  91. .r_table = RT_TABLE_LOCAL,
  92. .r_action = RTN_UNICAST,
  93. };
  94. static struct fib_rule *fib_rules = &local_rule;
  95. static DEFINE_RWLOCK(fib_rules_lock);
  96. int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  97. {
  98. struct rtattr **rta = arg;
  99. struct rtmsg *rtm = NLMSG_DATA(nlh);
  100. struct fib_rule *r, **rp;
  101. int err = -ESRCH;
  102. for (rp=&fib_rules; (r=*rp) != NULL; rp=&r->r_next) {
  103. if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 4) == 0) &&
  104. rtm->rtm_src_len == r->r_src_len &&
  105. rtm->rtm_dst_len == r->r_dst_len &&
  106. (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 4) == 0) &&
  107. rtm->rtm_tos == r->r_tos &&
  108. #ifdef CONFIG_IP_ROUTE_FWMARK
  109. (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
  110. #endif
  111. (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
  112. (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
  113. (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) &&
  114. (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
  115. err = -EPERM;
  116. if (r == &local_rule)
  117. break;
  118. write_lock_bh(&fib_rules_lock);
  119. *rp = r->r_next;
  120. r->r_dead = 1;
  121. write_unlock_bh(&fib_rules_lock);
  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. void fib_rule_put(struct fib_rule *r)
  139. {
  140. if (atomic_dec_and_test(&r->r_clntref)) {
  141. if (r->r_dead)
  142. kfree(r);
  143. else
  144. printk("Freeing alive rule %p\n", r);
  145. }
  146. }
  147. int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  148. {
  149. struct rtattr **rta = arg;
  150. struct rtmsg *rtm = NLMSG_DATA(nlh);
  151. struct fib_rule *r, *new_r, **rp;
  152. unsigned char table_id;
  153. if (rtm->rtm_src_len > 32 || rtm->rtm_dst_len > 32 ||
  154. (rtm->rtm_tos & ~IPTOS_TOS_MASK))
  155. return -EINVAL;
  156. if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
  157. return -EINVAL;
  158. table_id = rtm->rtm_table;
  159. if (table_id == RT_TABLE_UNSPEC) {
  160. struct fib_table *table;
  161. if (rtm->rtm_type == RTN_UNICAST) {
  162. if ((table = fib_empty_table()) == NULL)
  163. return -ENOBUFS;
  164. table_id = table->tb_id;
  165. }
  166. }
  167. new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
  168. if (!new_r)
  169. return -ENOMEM;
  170. memset(new_r, 0, sizeof(*new_r));
  171. if (rta[RTA_SRC-1])
  172. memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 4);
  173. if (rta[RTA_DST-1])
  174. memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 4);
  175. if (rta[RTA_GATEWAY-1])
  176. memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 4);
  177. new_r->r_src_len = rtm->rtm_src_len;
  178. new_r->r_dst_len = rtm->rtm_dst_len;
  179. new_r->r_srcmask = inet_make_mask(rtm->rtm_src_len);
  180. new_r->r_dstmask = inet_make_mask(rtm->rtm_dst_len);
  181. new_r->r_tos = rtm->rtm_tos;
  182. #ifdef CONFIG_IP_ROUTE_FWMARK
  183. if (rta[RTA_PROTOINFO-1])
  184. memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
  185. #endif
  186. new_r->r_action = rtm->rtm_type;
  187. new_r->r_flags = rtm->rtm_flags;
  188. if (rta[RTA_PRIORITY-1])
  189. memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
  190. new_r->r_table = table_id;
  191. if (rta[RTA_IIF-1]) {
  192. struct net_device *dev;
  193. rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ);
  194. new_r->r_ifindex = -1;
  195. dev = __dev_get_by_name(new_r->r_ifname);
  196. if (dev)
  197. new_r->r_ifindex = dev->ifindex;
  198. }
  199. #ifdef CONFIG_NET_CLS_ROUTE
  200. if (rta[RTA_FLOW-1])
  201. memcpy(&new_r->r_tclassid, RTA_DATA(rta[RTA_FLOW-1]), 4);
  202. #endif
  203. rp = &fib_rules;
  204. if (!new_r->r_preference) {
  205. r = fib_rules;
  206. if (r && (r = r->r_next) != NULL) {
  207. rp = &fib_rules->r_next;
  208. if (r->r_preference)
  209. new_r->r_preference = r->r_preference - 1;
  210. }
  211. }
  212. while ( (r = *rp) != NULL ) {
  213. if (r->r_preference > new_r->r_preference)
  214. break;
  215. rp = &r->r_next;
  216. }
  217. new_r->r_next = r;
  218. atomic_inc(&new_r->r_clntref);
  219. write_lock_bh(&fib_rules_lock);
  220. *rp = new_r;
  221. write_unlock_bh(&fib_rules_lock);
  222. return 0;
  223. }
  224. #ifdef CONFIG_NET_CLS_ROUTE
  225. u32 fib_rules_tclass(struct fib_result *res)
  226. {
  227. if (res->r)
  228. return res->r->r_tclassid;
  229. return 0;
  230. }
  231. #endif
  232. static void fib_rules_detach(struct net_device *dev)
  233. {
  234. struct fib_rule *r;
  235. for (r=fib_rules; r; r=r->r_next) {
  236. if (r->r_ifindex == dev->ifindex) {
  237. write_lock_bh(&fib_rules_lock);
  238. r->r_ifindex = -1;
  239. write_unlock_bh(&fib_rules_lock);
  240. }
  241. }
  242. }
  243. static void fib_rules_attach(struct net_device *dev)
  244. {
  245. struct fib_rule *r;
  246. for (r=fib_rules; r; r=r->r_next) {
  247. if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) {
  248. write_lock_bh(&fib_rules_lock);
  249. r->r_ifindex = dev->ifindex;
  250. write_unlock_bh(&fib_rules_lock);
  251. }
  252. }
  253. }
  254. int fib_lookup(const struct flowi *flp, struct fib_result *res)
  255. {
  256. int err;
  257. struct fib_rule *r, *policy;
  258. struct fib_table *tb;
  259. u32 daddr = flp->fl4_dst;
  260. u32 saddr = flp->fl4_src;
  261. FRprintk("Lookup: %u.%u.%u.%u <- %u.%u.%u.%u ",
  262. NIPQUAD(flp->fl4_dst), NIPQUAD(flp->fl4_src));
  263. read_lock(&fib_rules_lock);
  264. for (r = fib_rules; r; r=r->r_next) {
  265. if (((saddr^r->r_src) & r->r_srcmask) ||
  266. ((daddr^r->r_dst) & r->r_dstmask) ||
  267. (r->r_tos && r->r_tos != flp->fl4_tos) ||
  268. #ifdef CONFIG_IP_ROUTE_FWMARK
  269. (r->r_fwmark && r->r_fwmark != flp->fl4_fwmark) ||
  270. #endif
  271. (r->r_ifindex && r->r_ifindex != flp->iif))
  272. continue;
  273. FRprintk("tb %d r %d ", r->r_table, r->r_action);
  274. switch (r->r_action) {
  275. case RTN_UNICAST:
  276. policy = r;
  277. break;
  278. case RTN_UNREACHABLE:
  279. read_unlock(&fib_rules_lock);
  280. return -ENETUNREACH;
  281. default:
  282. case RTN_BLACKHOLE:
  283. read_unlock(&fib_rules_lock);
  284. return -EINVAL;
  285. case RTN_PROHIBIT:
  286. read_unlock(&fib_rules_lock);
  287. return -EACCES;
  288. }
  289. if ((tb = fib_get_table(r->r_table)) == NULL)
  290. continue;
  291. err = tb->tb_lookup(tb, flp, res);
  292. if (err == 0) {
  293. res->r = policy;
  294. if (policy)
  295. atomic_inc(&policy->r_clntref);
  296. read_unlock(&fib_rules_lock);
  297. return 0;
  298. }
  299. if (err < 0 && err != -EAGAIN) {
  300. read_unlock(&fib_rules_lock);
  301. return err;
  302. }
  303. }
  304. FRprintk("FAILURE\n");
  305. read_unlock(&fib_rules_lock);
  306. return -ENETUNREACH;
  307. }
  308. void fib_select_default(const struct flowi *flp, struct fib_result *res)
  309. {
  310. if (res->r && res->r->r_action == RTN_UNICAST &&
  311. FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
  312. struct fib_table *tb;
  313. if ((tb = fib_get_table(res->r->r_table)) != NULL)
  314. tb->tb_select_default(tb, flp, res);
  315. }
  316. }
  317. static int fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
  318. {
  319. struct net_device *dev = ptr;
  320. if (event == NETDEV_UNREGISTER)
  321. fib_rules_detach(dev);
  322. else if (event == NETDEV_REGISTER)
  323. fib_rules_attach(dev);
  324. return NOTIFY_DONE;
  325. }
  326. static struct notifier_block fib_rules_notifier = {
  327. .notifier_call =fib_rules_event,
  328. };
  329. static __inline__ int inet_fill_rule(struct sk_buff *skb,
  330. struct fib_rule *r,
  331. struct netlink_callback *cb,
  332. unsigned int flags)
  333. {
  334. struct rtmsg *rtm;
  335. struct nlmsghdr *nlh;
  336. unsigned char *b = skb->tail;
  337. nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags);
  338. rtm = NLMSG_DATA(nlh);
  339. rtm->rtm_family = AF_INET;
  340. rtm->rtm_dst_len = r->r_dst_len;
  341. rtm->rtm_src_len = r->r_src_len;
  342. rtm->rtm_tos = r->r_tos;
  343. #ifdef CONFIG_IP_ROUTE_FWMARK
  344. if (r->r_fwmark)
  345. RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
  346. #endif
  347. rtm->rtm_table = r->r_table;
  348. rtm->rtm_protocol = 0;
  349. rtm->rtm_scope = 0;
  350. rtm->rtm_type = r->r_action;
  351. rtm->rtm_flags = r->r_flags;
  352. if (r->r_dst_len)
  353. RTA_PUT(skb, RTA_DST, 4, &r->r_dst);
  354. if (r->r_src_len)
  355. RTA_PUT(skb, RTA_SRC, 4, &r->r_src);
  356. if (r->r_ifname[0])
  357. RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
  358. if (r->r_preference)
  359. RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
  360. if (r->r_srcmap)
  361. RTA_PUT(skb, RTA_GATEWAY, 4, &r->r_srcmap);
  362. #ifdef CONFIG_NET_CLS_ROUTE
  363. if (r->r_tclassid)
  364. RTA_PUT(skb, RTA_FLOW, 4, &r->r_tclassid);
  365. #endif
  366. nlh->nlmsg_len = skb->tail - b;
  367. return skb->len;
  368. nlmsg_failure:
  369. rtattr_failure:
  370. skb_trim(skb, b - skb->data);
  371. return -1;
  372. }
  373. int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
  374. {
  375. int idx;
  376. int s_idx = cb->args[0];
  377. struct fib_rule *r;
  378. read_lock(&fib_rules_lock);
  379. for (r=fib_rules, idx=0; r; r = r->r_next, idx++) {
  380. if (idx < s_idx)
  381. continue;
  382. if (inet_fill_rule(skb, r, cb, NLM_F_MULTI) < 0)
  383. break;
  384. }
  385. read_unlock(&fib_rules_lock);
  386. cb->args[0] = idx;
  387. return skb->len;
  388. }
  389. void __init fib_rules_init(void)
  390. {
  391. register_netdevice_notifier(&fib_rules_notifier);
  392. }