fib_rules.c 11 KB

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