dn_rules.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * DECnet An implementation of the DECnet protocol suite for the LINUX
  3. * operating system. DECnet is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * DECnet Routing Forwarding Information Base (Rules)
  7. *
  8. * Author: Steve Whitehouse <SteveW@ACM.org>
  9. * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
  10. *
  11. *
  12. * Changes:
  13. *
  14. */
  15. #include <linux/config.h>
  16. #include <linux/string.h>
  17. #include <linux/net.h>
  18. #include <linux/socket.h>
  19. #include <linux/sockios.h>
  20. #include <linux/init.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/netlink.h>
  23. #include <linux/rtnetlink.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/timer.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/in_route.h>
  29. #include <asm/atomic.h>
  30. #include <asm/uaccess.h>
  31. #include <net/neighbour.h>
  32. #include <net/dst.h>
  33. #include <net/flow.h>
  34. #include <net/dn.h>
  35. #include <net/dn_fib.h>
  36. #include <net/dn_neigh.h>
  37. #include <net/dn_dev.h>
  38. struct dn_fib_rule
  39. {
  40. struct dn_fib_rule *r_next;
  41. atomic_t r_clntref;
  42. u32 r_preference;
  43. unsigned char r_table;
  44. unsigned char r_action;
  45. unsigned char r_dst_len;
  46. unsigned char r_src_len;
  47. dn_address r_src;
  48. dn_address r_srcmask;
  49. dn_address r_dst;
  50. dn_address r_dstmask;
  51. dn_address r_srcmap;
  52. u8 r_flags;
  53. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  54. u32 r_fwmark;
  55. #endif
  56. int r_ifindex;
  57. char r_ifname[IFNAMSIZ];
  58. int r_dead;
  59. };
  60. static struct dn_fib_rule default_rule = {
  61. .r_clntref = ATOMIC_INIT(2),
  62. .r_preference = 0x7fff,
  63. .r_table = RT_TABLE_MAIN,
  64. .r_action = RTN_UNICAST
  65. };
  66. static struct dn_fib_rule *dn_fib_rules = &default_rule;
  67. static DEFINE_RWLOCK(dn_fib_rules_lock);
  68. int dn_fib_rtm_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  69. {
  70. struct rtattr **rta = arg;
  71. struct rtmsg *rtm = NLMSG_DATA(nlh);
  72. struct dn_fib_rule *r, **rp;
  73. int err = -ESRCH;
  74. for(rp=&dn_fib_rules; (r=*rp) != NULL; rp = &r->r_next) {
  75. if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 2) == 0) &&
  76. rtm->rtm_src_len == r->r_src_len &&
  77. rtm->rtm_dst_len == r->r_dst_len &&
  78. (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 2) == 0) &&
  79. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  80. (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
  81. #endif
  82. (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
  83. (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
  84. (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) &&
  85. (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
  86. err = -EPERM;
  87. if (r == &default_rule)
  88. break;
  89. write_lock_bh(&dn_fib_rules_lock);
  90. *rp = r->r_next;
  91. r->r_dead = 1;
  92. write_unlock_bh(&dn_fib_rules_lock);
  93. dn_fib_rule_put(r);
  94. err = 0;
  95. break;
  96. }
  97. }
  98. return err;
  99. }
  100. void dn_fib_rule_put(struct dn_fib_rule *r)
  101. {
  102. if (atomic_dec_and_test(&r->r_clntref)) {
  103. if (r->r_dead)
  104. kfree(r);
  105. else
  106. printk(KERN_DEBUG "Attempt to free alive dn_fib_rule\n");
  107. }
  108. }
  109. int dn_fib_rtm_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  110. {
  111. struct rtattr **rta = arg;
  112. struct rtmsg *rtm = NLMSG_DATA(nlh);
  113. struct dn_fib_rule *r, *new_r, **rp;
  114. unsigned char table_id;
  115. if (rtm->rtm_src_len > 16 || rtm->rtm_dst_len > 16)
  116. return -EINVAL;
  117. if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
  118. return -EINVAL;
  119. if (rtm->rtm_type == RTN_NAT)
  120. return -EINVAL;
  121. table_id = rtm->rtm_table;
  122. if (table_id == RT_TABLE_UNSPEC) {
  123. struct dn_fib_table *tb;
  124. if (rtm->rtm_type == RTN_UNICAST) {
  125. if ((tb = dn_fib_empty_table()) == NULL)
  126. return -ENOBUFS;
  127. table_id = tb->n;
  128. }
  129. }
  130. new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
  131. if (!new_r)
  132. return -ENOMEM;
  133. memset(new_r, 0, sizeof(*new_r));
  134. if (rta[RTA_SRC-1])
  135. memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 2);
  136. if (rta[RTA_DST-1])
  137. memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 2);
  138. if (rta[RTA_GATEWAY-1])
  139. memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 2);
  140. new_r->r_src_len = rtm->rtm_src_len;
  141. new_r->r_dst_len = rtm->rtm_dst_len;
  142. new_r->r_srcmask = dnet_make_mask(rtm->rtm_src_len);
  143. new_r->r_dstmask = dnet_make_mask(rtm->rtm_dst_len);
  144. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  145. if (rta[RTA_PROTOINFO-1])
  146. memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
  147. #endif
  148. new_r->r_action = rtm->rtm_type;
  149. new_r->r_flags = rtm->rtm_flags;
  150. if (rta[RTA_PRIORITY-1])
  151. memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
  152. new_r->r_table = table_id;
  153. if (rta[RTA_IIF-1]) {
  154. struct net_device *dev;
  155. rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ);
  156. new_r->r_ifindex = -1;
  157. dev = dev_get_by_name(new_r->r_ifname);
  158. if (dev) {
  159. new_r->r_ifindex = dev->ifindex;
  160. dev_put(dev);
  161. }
  162. }
  163. rp = &dn_fib_rules;
  164. if (!new_r->r_preference) {
  165. r = dn_fib_rules;
  166. if (r && (r = r->r_next) != NULL) {
  167. rp = &dn_fib_rules->r_next;
  168. if (r->r_preference)
  169. new_r->r_preference = r->r_preference - 1;
  170. }
  171. }
  172. while((r=*rp) != NULL) {
  173. if (r->r_preference > new_r->r_preference)
  174. break;
  175. rp = &r->r_next;
  176. }
  177. new_r->r_next = r;
  178. atomic_inc(&new_r->r_clntref);
  179. write_lock_bh(&dn_fib_rules_lock);
  180. *rp = new_r;
  181. write_unlock_bh(&dn_fib_rules_lock);
  182. return 0;
  183. }
  184. int dn_fib_lookup(const struct flowi *flp, struct dn_fib_res *res)
  185. {
  186. struct dn_fib_rule *r, *policy;
  187. struct dn_fib_table *tb;
  188. dn_address saddr = flp->fld_src;
  189. dn_address daddr = flp->fld_dst;
  190. int err;
  191. read_lock(&dn_fib_rules_lock);
  192. for(r = dn_fib_rules; r; r = r->r_next) {
  193. if (((saddr^r->r_src) & r->r_srcmask) ||
  194. ((daddr^r->r_dst) & r->r_dstmask) ||
  195. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  196. (r->r_fwmark && r->r_fwmark != flp->fld_fwmark) ||
  197. #endif
  198. (r->r_ifindex && r->r_ifindex != flp->iif))
  199. continue;
  200. switch(r->r_action) {
  201. case RTN_UNICAST:
  202. case RTN_NAT:
  203. policy = r;
  204. break;
  205. case RTN_UNREACHABLE:
  206. read_unlock(&dn_fib_rules_lock);
  207. return -ENETUNREACH;
  208. default:
  209. case RTN_BLACKHOLE:
  210. read_unlock(&dn_fib_rules_lock);
  211. return -EINVAL;
  212. case RTN_PROHIBIT:
  213. read_unlock(&dn_fib_rules_lock);
  214. return -EACCES;
  215. }
  216. if ((tb = dn_fib_get_table(r->r_table, 0)) == NULL)
  217. continue;
  218. err = tb->lookup(tb, flp, res);
  219. if (err == 0) {
  220. res->r = policy;
  221. if (policy)
  222. atomic_inc(&policy->r_clntref);
  223. read_unlock(&dn_fib_rules_lock);
  224. return 0;
  225. }
  226. if (err < 0 && err != -EAGAIN) {
  227. read_unlock(&dn_fib_rules_lock);
  228. return err;
  229. }
  230. }
  231. read_unlock(&dn_fib_rules_lock);
  232. return -ESRCH;
  233. }
  234. unsigned dnet_addr_type(__u16 addr)
  235. {
  236. struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
  237. struct dn_fib_res res;
  238. unsigned ret = RTN_UNICAST;
  239. struct dn_fib_table *tb = dn_fib_tables[RT_TABLE_LOCAL];
  240. res.r = NULL;
  241. if (tb) {
  242. if (!tb->lookup(tb, &fl, &res)) {
  243. ret = res.type;
  244. dn_fib_res_put(&res);
  245. }
  246. }
  247. return ret;
  248. }
  249. __u16 dn_fib_rules_policy(__u16 saddr, struct dn_fib_res *res, unsigned *flags)
  250. {
  251. struct dn_fib_rule *r = res->r;
  252. if (r->r_action == RTN_NAT) {
  253. int addrtype = dnet_addr_type(r->r_srcmap);
  254. if (addrtype == RTN_NAT) {
  255. saddr = (saddr&~r->r_srcmask)|r->r_srcmap;
  256. *flags |= RTCF_SNAT;
  257. } else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) {
  258. saddr = r->r_srcmap;
  259. *flags |= RTCF_MASQ;
  260. }
  261. }
  262. return saddr;
  263. }
  264. static void dn_fib_rules_detach(struct net_device *dev)
  265. {
  266. struct dn_fib_rule *r;
  267. for(r = dn_fib_rules; r; r = r->r_next) {
  268. if (r->r_ifindex == dev->ifindex) {
  269. write_lock_bh(&dn_fib_rules_lock);
  270. r->r_ifindex = -1;
  271. write_unlock_bh(&dn_fib_rules_lock);
  272. }
  273. }
  274. }
  275. static void dn_fib_rules_attach(struct net_device *dev)
  276. {
  277. struct dn_fib_rule *r;
  278. for(r = dn_fib_rules; r; r = r->r_next) {
  279. if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) {
  280. write_lock_bh(&dn_fib_rules_lock);
  281. r->r_ifindex = dev->ifindex;
  282. write_unlock_bh(&dn_fib_rules_lock);
  283. }
  284. }
  285. }
  286. static int dn_fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
  287. {
  288. struct net_device *dev = ptr;
  289. switch(event) {
  290. case NETDEV_UNREGISTER:
  291. dn_fib_rules_detach(dev);
  292. dn_fib_sync_down(0, dev, 1);
  293. case NETDEV_REGISTER:
  294. dn_fib_rules_attach(dev);
  295. dn_fib_sync_up(dev);
  296. }
  297. return NOTIFY_DONE;
  298. }
  299. static struct notifier_block dn_fib_rules_notifier = {
  300. .notifier_call = dn_fib_rules_event,
  301. };
  302. static int dn_fib_fill_rule(struct sk_buff *skb, struct dn_fib_rule *r,
  303. struct netlink_callback *cb, unsigned int flags)
  304. {
  305. struct rtmsg *rtm;
  306. struct nlmsghdr *nlh;
  307. unsigned char *b = skb->tail;
  308. nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags);
  309. rtm = NLMSG_DATA(nlh);
  310. rtm->rtm_family = AF_DECnet;
  311. rtm->rtm_dst_len = r->r_dst_len;
  312. rtm->rtm_src_len = r->r_src_len;
  313. rtm->rtm_tos = 0;
  314. #ifdef CONFIG_DECNET_ROUTE_FWMARK
  315. if (r->r_fwmark)
  316. RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
  317. #endif
  318. rtm->rtm_table = r->r_table;
  319. rtm->rtm_protocol = 0;
  320. rtm->rtm_scope = 0;
  321. rtm->rtm_type = r->r_action;
  322. rtm->rtm_flags = r->r_flags;
  323. if (r->r_dst_len)
  324. RTA_PUT(skb, RTA_DST, 2, &r->r_dst);
  325. if (r->r_src_len)
  326. RTA_PUT(skb, RTA_SRC, 2, &r->r_src);
  327. if (r->r_ifname[0])
  328. RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
  329. if (r->r_preference)
  330. RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
  331. if (r->r_srcmap)
  332. RTA_PUT(skb, RTA_GATEWAY, 2, &r->r_srcmap);
  333. nlh->nlmsg_len = skb->tail - b;
  334. return skb->len;
  335. nlmsg_failure:
  336. rtattr_failure:
  337. skb_trim(skb, b - skb->data);
  338. return -1;
  339. }
  340. int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
  341. {
  342. int idx;
  343. int s_idx = cb->args[0];
  344. struct dn_fib_rule *r;
  345. read_lock(&dn_fib_rules_lock);
  346. for(r = dn_fib_rules, idx = 0; r; r = r->r_next, idx++) {
  347. if (idx < s_idx)
  348. continue;
  349. if (dn_fib_fill_rule(skb, r, cb, NLM_F_MULTI) < 0)
  350. break;
  351. }
  352. read_unlock(&dn_fib_rules_lock);
  353. cb->args[0] = idx;
  354. return skb->len;
  355. }
  356. void __init dn_fib_rules_init(void)
  357. {
  358. register_netdevice_notifier(&dn_fib_rules_notifier);
  359. }
  360. void __exit dn_fib_rules_cleanup(void)
  361. {
  362. unregister_netdevice_notifier(&dn_fib_rules_notifier);
  363. }