fib_frontend.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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: FIB frontend.
  7. *
  8. * Version: $Id: fib_frontend.c,v 1.1.1.1 2007/06/12 07:27:14 eyryu 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. #include <linux/module.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/system.h>
  20. #include <linux/bitops.h>
  21. #include <linux/capability.h>
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/string.h>
  26. #include <linux/socket.h>
  27. #include <linux/sockios.h>
  28. #include <linux/errno.h>
  29. #include <linux/in.h>
  30. #include <linux/inet.h>
  31. #include <linux/inetdevice.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/if_addr.h>
  34. #include <linux/if_arp.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/netlink.h>
  37. #include <linux/init.h>
  38. #include <linux/list.h>
  39. #include <net/ip.h>
  40. #include <net/protocol.h>
  41. #include <net/route.h>
  42. #include <net/tcp.h>
  43. #include <net/sock.h>
  44. #include <net/icmp.h>
  45. #include <net/arp.h>
  46. #include <net/ip_fib.h>
  47. #define FFprint(a...) printk(KERN_DEBUG a)
  48. #ifndef CONFIG_IP_MULTIPLE_TABLES
  49. struct fib_table *ip_fib_local_table;
  50. struct fib_table *ip_fib_main_table;
  51. #define FIB_TABLE_HASHSZ 1
  52. static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
  53. #else
  54. #define FIB_TABLE_HASHSZ 256
  55. static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
  56. struct fib_table *fib_new_table(u32 id)
  57. {
  58. struct fib_table *tb;
  59. unsigned int h;
  60. if (id == 0)
  61. id = RT_TABLE_MAIN;
  62. tb = fib_get_table(id);
  63. if (tb)
  64. return tb;
  65. tb = fib_hash_init(id);
  66. if (!tb)
  67. return NULL;
  68. h = id & (FIB_TABLE_HASHSZ - 1);
  69. hlist_add_head_rcu(&tb->tb_hlist, &fib_table_hash[h]);
  70. return tb;
  71. }
  72. struct fib_table *fib_get_table(u32 id)
  73. {
  74. struct fib_table *tb;
  75. struct hlist_node *node;
  76. unsigned int h;
  77. if (id == 0)
  78. id = RT_TABLE_MAIN;
  79. h = id & (FIB_TABLE_HASHSZ - 1);
  80. rcu_read_lock();
  81. hlist_for_each_entry_rcu(tb, node, &fib_table_hash[h], tb_hlist) {
  82. if (tb->tb_id == id) {
  83. rcu_read_unlock();
  84. return tb;
  85. }
  86. }
  87. rcu_read_unlock();
  88. return NULL;
  89. }
  90. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  91. static void fib_flush(void)
  92. {
  93. int flushed = 0;
  94. struct fib_table *tb;
  95. struct hlist_node *node;
  96. unsigned int h;
  97. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  98. hlist_for_each_entry(tb, node, &fib_table_hash[h], tb_hlist)
  99. flushed += tb->tb_flush(tb);
  100. }
  101. if (flushed)
  102. rt_cache_flush(-1);
  103. }
  104. /*
  105. * Find the first device with a given source address.
  106. */
  107. struct net_device * ip_dev_find(__be32 addr)
  108. {
  109. struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
  110. struct fib_result res;
  111. struct net_device *dev = NULL;
  112. #ifdef CONFIG_IP_MULTIPLE_TABLES
  113. res.r = NULL;
  114. #endif
  115. if (!ip_fib_local_table ||
  116. ip_fib_local_table->tb_lookup(ip_fib_local_table, &fl, &res))
  117. return NULL;
  118. if (res.type != RTN_LOCAL)
  119. goto out;
  120. dev = FIB_RES_DEV(res);
  121. if (dev)
  122. dev_hold(dev);
  123. out:
  124. fib_res_put(&res);
  125. return dev;
  126. }
  127. unsigned inet_addr_type(__be32 addr)
  128. {
  129. struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
  130. struct fib_result res;
  131. unsigned ret = RTN_BROADCAST;
  132. if (ZERONET(addr) || BADCLASS(addr))
  133. return RTN_BROADCAST;
  134. if (MULTICAST(addr))
  135. return RTN_MULTICAST;
  136. #ifdef CONFIG_IP_MULTIPLE_TABLES
  137. res.r = NULL;
  138. #endif
  139. if (ip_fib_local_table) {
  140. ret = RTN_UNICAST;
  141. if (!ip_fib_local_table->tb_lookup(ip_fib_local_table,
  142. &fl, &res)) {
  143. ret = res.type;
  144. fib_res_put(&res);
  145. }
  146. }
  147. return ret;
  148. }
  149. /* Given (packet source, input interface) and optional (dst, oif, tos):
  150. - (main) check, that source is valid i.e. not broadcast or our local
  151. address.
  152. - figure out what "logical" interface this packet arrived
  153. and calculate "specific destination" address.
  154. - check, that packet arrived from expected physical interface.
  155. */
  156. int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
  157. struct net_device *dev, __be32 *spec_dst, u32 *itag)
  158. {
  159. struct in_device *in_dev;
  160. struct flowi fl = { .nl_u = { .ip4_u =
  161. { .daddr = src,
  162. .saddr = dst,
  163. .tos = tos } },
  164. .iif = oif };
  165. struct fib_result res;
  166. int no_addr, rpf;
  167. int ret;
  168. no_addr = rpf = 0;
  169. rcu_read_lock();
  170. in_dev = __in_dev_get_rcu(dev);
  171. if (in_dev) {
  172. no_addr = in_dev->ifa_list == NULL;
  173. rpf = IN_DEV_RPFILTER(in_dev);
  174. }
  175. rcu_read_unlock();
  176. if (in_dev == NULL)
  177. goto e_inval;
  178. if (fib_lookup(&fl, &res))
  179. goto last_resort;
  180. if (res.type != RTN_UNICAST)
  181. goto e_inval_res;
  182. *spec_dst = FIB_RES_PREFSRC(res);
  183. fib_combine_itag(itag, &res);
  184. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  185. if (FIB_RES_DEV(res) == dev || res.fi->fib_nhs > 1)
  186. #else
  187. if (FIB_RES_DEV(res) == dev)
  188. #endif
  189. {
  190. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  191. fib_res_put(&res);
  192. return ret;
  193. }
  194. fib_res_put(&res);
  195. if (no_addr)
  196. goto last_resort;
  197. if (rpf)
  198. goto e_inval;
  199. fl.oif = dev->ifindex;
  200. ret = 0;
  201. if (fib_lookup(&fl, &res) == 0) {
  202. if (res.type == RTN_UNICAST) {
  203. *spec_dst = FIB_RES_PREFSRC(res);
  204. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  205. }
  206. fib_res_put(&res);
  207. }
  208. return ret;
  209. last_resort:
  210. if (rpf)
  211. goto e_inval;
  212. *spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
  213. *itag = 0;
  214. return 0;
  215. e_inval_res:
  216. fib_res_put(&res);
  217. e_inval:
  218. return -EINVAL;
  219. }
  220. #ifndef CONFIG_IP_NOSIOCRT
  221. static inline __be32 sk_extract_addr(struct sockaddr *addr)
  222. {
  223. return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
  224. }
  225. static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
  226. {
  227. struct nlattr *nla;
  228. nla = (struct nlattr *) ((char *) mx + len);
  229. nla->nla_type = type;
  230. nla->nla_len = nla_attr_size(4);
  231. *(u32 *) nla_data(nla) = value;
  232. return len + nla_total_size(4);
  233. }
  234. static int rtentry_to_fib_config(int cmd, struct rtentry *rt,
  235. struct fib_config *cfg)
  236. {
  237. __be32 addr;
  238. int plen;
  239. memset(cfg, 0, sizeof(*cfg));
  240. if (rt->rt_dst.sa_family != AF_INET)
  241. return -EAFNOSUPPORT;
  242. /*
  243. * Check mask for validity:
  244. * a) it must be contiguous.
  245. * b) destination must have all host bits clear.
  246. * c) if application forgot to set correct family (AF_INET),
  247. * reject request unless it is absolutely clear i.e.
  248. * both family and mask are zero.
  249. */
  250. plen = 32;
  251. addr = sk_extract_addr(&rt->rt_dst);
  252. if (!(rt->rt_flags & RTF_HOST)) {
  253. __be32 mask = sk_extract_addr(&rt->rt_genmask);
  254. if (rt->rt_genmask.sa_family != AF_INET) {
  255. if (mask || rt->rt_genmask.sa_family)
  256. return -EAFNOSUPPORT;
  257. }
  258. if (bad_mask(mask, addr))
  259. return -EINVAL;
  260. plen = inet_mask_len(mask);
  261. }
  262. cfg->fc_dst_len = plen;
  263. cfg->fc_dst = addr;
  264. if (cmd != SIOCDELRT) {
  265. cfg->fc_nlflags = NLM_F_CREATE;
  266. cfg->fc_protocol = RTPROT_BOOT;
  267. }
  268. if (rt->rt_metric)
  269. cfg->fc_priority = rt->rt_metric - 1;
  270. if (rt->rt_flags & RTF_REJECT) {
  271. cfg->fc_scope = RT_SCOPE_HOST;
  272. cfg->fc_type = RTN_UNREACHABLE;
  273. return 0;
  274. }
  275. cfg->fc_scope = RT_SCOPE_NOWHERE;
  276. cfg->fc_type = RTN_UNICAST;
  277. if (rt->rt_dev) {
  278. char *colon;
  279. struct net_device *dev;
  280. char devname[IFNAMSIZ];
  281. if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
  282. return -EFAULT;
  283. devname[IFNAMSIZ-1] = 0;
  284. colon = strchr(devname, ':');
  285. if (colon)
  286. *colon = 0;
  287. dev = __dev_get_by_name(devname);
  288. if (!dev)
  289. return -ENODEV;
  290. cfg->fc_oif = dev->ifindex;
  291. if (colon) {
  292. struct in_ifaddr *ifa;
  293. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  294. if (!in_dev)
  295. return -ENODEV;
  296. *colon = ':';
  297. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
  298. if (strcmp(ifa->ifa_label, devname) == 0)
  299. break;
  300. if (ifa == NULL)
  301. return -ENODEV;
  302. cfg->fc_prefsrc = ifa->ifa_local;
  303. }
  304. }
  305. addr = sk_extract_addr(&rt->rt_gateway);
  306. if (rt->rt_gateway.sa_family == AF_INET && addr) {
  307. cfg->fc_gw = addr;
  308. if (rt->rt_flags & RTF_GATEWAY &&
  309. inet_addr_type(addr) == RTN_UNICAST)
  310. cfg->fc_scope = RT_SCOPE_UNIVERSE;
  311. }
  312. if (cmd == SIOCDELRT)
  313. return 0;
  314. if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw)
  315. return -EINVAL;
  316. if (cfg->fc_scope == RT_SCOPE_NOWHERE)
  317. cfg->fc_scope = RT_SCOPE_LINK;
  318. if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
  319. struct nlattr *mx;
  320. int len = 0;
  321. mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL);
  322. if (mx == NULL)
  323. return -ENOMEM;
  324. if (rt->rt_flags & RTF_MTU)
  325. len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
  326. if (rt->rt_flags & RTF_WINDOW)
  327. len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
  328. if (rt->rt_flags & RTF_IRTT)
  329. len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
  330. cfg->fc_mx = mx;
  331. cfg->fc_mx_len = len;
  332. }
  333. return 0;
  334. }
  335. /*
  336. * Handle IP routing ioctl calls. These are used to manipulate the routing tables
  337. */
  338. int ip_rt_ioctl(unsigned int cmd, void __user *arg)
  339. {
  340. struct fib_config cfg;
  341. struct rtentry rt;
  342. int err;
  343. switch (cmd) {
  344. case SIOCADDRT: /* Add a route */
  345. case SIOCDELRT: /* Delete a route */
  346. if (!capable(CAP_NET_ADMIN))
  347. return -EPERM;
  348. if (copy_from_user(&rt, arg, sizeof(rt)))
  349. return -EFAULT;
  350. rtnl_lock();
  351. err = rtentry_to_fib_config(cmd, &rt, &cfg);
  352. if (err == 0) {
  353. struct fib_table *tb;
  354. if (cmd == SIOCDELRT) {
  355. tb = fib_get_table(cfg.fc_table);
  356. if (tb)
  357. err = tb->tb_delete(tb, &cfg);
  358. else
  359. err = -ESRCH;
  360. } else {
  361. tb = fib_new_table(cfg.fc_table);
  362. if (tb)
  363. err = tb->tb_insert(tb, &cfg);
  364. else
  365. err = -ENOBUFS;
  366. }
  367. /* allocated by rtentry_to_fib_config() */
  368. kfree(cfg.fc_mx);
  369. }
  370. rtnl_unlock();
  371. return err;
  372. }
  373. return -EINVAL;
  374. }
  375. #else
  376. int ip_rt_ioctl(unsigned int cmd, void *arg)
  377. {
  378. return -EINVAL;
  379. }
  380. #endif
  381. struct nla_policy rtm_ipv4_policy[RTA_MAX+1] __read_mostly = {
  382. [RTA_DST] = { .type = NLA_U32 },
  383. [RTA_SRC] = { .type = NLA_U32 },
  384. [RTA_IIF] = { .type = NLA_U32 },
  385. [RTA_OIF] = { .type = NLA_U32 },
  386. [RTA_GATEWAY] = { .type = NLA_U32 },
  387. [RTA_PRIORITY] = { .type = NLA_U32 },
  388. [RTA_PREFSRC] = { .type = NLA_U32 },
  389. [RTA_METRICS] = { .type = NLA_NESTED },
  390. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  391. [RTA_PROTOINFO] = { .type = NLA_U32 },
  392. [RTA_FLOW] = { .type = NLA_U32 },
  393. [RTA_MP_ALGO] = { .type = NLA_U32 },
  394. };
  395. static int rtm_to_fib_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  396. struct fib_config *cfg)
  397. {
  398. struct nlattr *attr;
  399. int err, remaining;
  400. struct rtmsg *rtm;
  401. err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy);
  402. if (err < 0)
  403. goto errout;
  404. memset(cfg, 0, sizeof(*cfg));
  405. rtm = nlmsg_data(nlh);
  406. cfg->fc_dst_len = rtm->rtm_dst_len;
  407. cfg->fc_tos = rtm->rtm_tos;
  408. cfg->fc_table = rtm->rtm_table;
  409. cfg->fc_protocol = rtm->rtm_protocol;
  410. cfg->fc_scope = rtm->rtm_scope;
  411. cfg->fc_type = rtm->rtm_type;
  412. cfg->fc_flags = rtm->rtm_flags;
  413. cfg->fc_nlflags = nlh->nlmsg_flags;
  414. cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
  415. cfg->fc_nlinfo.nlh = nlh;
  416. if (cfg->fc_type > RTN_MAX) {
  417. err = -EINVAL;
  418. goto errout;
  419. }
  420. nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
  421. switch (attr->nla_type) {
  422. case RTA_DST:
  423. cfg->fc_dst = nla_get_be32(attr);
  424. break;
  425. case RTA_OIF:
  426. cfg->fc_oif = nla_get_u32(attr);
  427. break;
  428. case RTA_GATEWAY:
  429. cfg->fc_gw = nla_get_be32(attr);
  430. break;
  431. case RTA_PRIORITY:
  432. cfg->fc_priority = nla_get_u32(attr);
  433. break;
  434. case RTA_PREFSRC:
  435. cfg->fc_prefsrc = nla_get_be32(attr);
  436. break;
  437. case RTA_METRICS:
  438. cfg->fc_mx = nla_data(attr);
  439. cfg->fc_mx_len = nla_len(attr);
  440. break;
  441. case RTA_MULTIPATH:
  442. cfg->fc_mp = nla_data(attr);
  443. cfg->fc_mp_len = nla_len(attr);
  444. break;
  445. case RTA_FLOW:
  446. cfg->fc_flow = nla_get_u32(attr);
  447. break;
  448. case RTA_MP_ALGO:
  449. cfg->fc_mp_alg = nla_get_u32(attr);
  450. break;
  451. case RTA_TABLE:
  452. cfg->fc_table = nla_get_u32(attr);
  453. break;
  454. }
  455. }
  456. return 0;
  457. errout:
  458. return err;
  459. }
  460. int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  461. {
  462. struct fib_config cfg;
  463. struct fib_table *tb;
  464. int err;
  465. err = rtm_to_fib_config(skb, nlh, &cfg);
  466. if (err < 0)
  467. goto errout;
  468. tb = fib_get_table(cfg.fc_table);
  469. if (tb == NULL) {
  470. err = -ESRCH;
  471. goto errout;
  472. }
  473. err = tb->tb_delete(tb, &cfg);
  474. errout:
  475. return err;
  476. }
  477. int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  478. {
  479. struct fib_config cfg;
  480. struct fib_table *tb;
  481. int err;
  482. err = rtm_to_fib_config(skb, nlh, &cfg);
  483. if (err < 0)
  484. goto errout;
  485. tb = fib_new_table(cfg.fc_table);
  486. if (tb == NULL) {
  487. err = -ENOBUFS;
  488. goto errout;
  489. }
  490. err = tb->tb_insert(tb, &cfg);
  491. errout:
  492. return err;
  493. }
  494. int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  495. {
  496. unsigned int h, s_h;
  497. unsigned int e = 0, s_e;
  498. struct fib_table *tb;
  499. struct hlist_node *node;
  500. int dumped = 0;
  501. if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
  502. ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
  503. return ip_rt_dump(skb, cb);
  504. s_h = cb->args[0];
  505. s_e = cb->args[1];
  506. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  507. e = 0;
  508. hlist_for_each_entry(tb, node, &fib_table_hash[h], tb_hlist) {
  509. if (e < s_e)
  510. goto next;
  511. if (dumped)
  512. memset(&cb->args[2], 0, sizeof(cb->args) -
  513. 2 * sizeof(cb->args[0]));
  514. if (tb->tb_dump(tb, skb, cb) < 0)
  515. goto out;
  516. dumped = 1;
  517. next:
  518. e++;
  519. }
  520. }
  521. out:
  522. cb->args[1] = e;
  523. cb->args[0] = h;
  524. return skb->len;
  525. }
  526. /* Prepare and feed intra-kernel routing request.
  527. Really, it should be netlink message, but :-( netlink
  528. can be not configured, so that we feed it directly
  529. to fib engine. It is legal, because all events occur
  530. only when netlink is already locked.
  531. */
  532. static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
  533. {
  534. struct fib_table *tb;
  535. struct fib_config cfg = {
  536. .fc_protocol = RTPROT_KERNEL,
  537. .fc_type = type,
  538. .fc_dst = dst,
  539. .fc_dst_len = dst_len,
  540. .fc_prefsrc = ifa->ifa_local,
  541. .fc_oif = ifa->ifa_dev->dev->ifindex,
  542. .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
  543. };
  544. if (type == RTN_UNICAST)
  545. tb = fib_new_table(RT_TABLE_MAIN);
  546. else
  547. tb = fib_new_table(RT_TABLE_LOCAL);
  548. if (tb == NULL)
  549. return;
  550. cfg.fc_table = tb->tb_id;
  551. if (type != RTN_LOCAL)
  552. cfg.fc_scope = RT_SCOPE_LINK;
  553. else
  554. cfg.fc_scope = RT_SCOPE_HOST;
  555. if (cmd == RTM_NEWROUTE)
  556. tb->tb_insert(tb, &cfg);
  557. else
  558. tb->tb_delete(tb, &cfg);
  559. }
  560. void fib_add_ifaddr(struct in_ifaddr *ifa)
  561. {
  562. struct in_device *in_dev = ifa->ifa_dev;
  563. struct net_device *dev = in_dev->dev;
  564. struct in_ifaddr *prim = ifa;
  565. __be32 mask = ifa->ifa_mask;
  566. __be32 addr = ifa->ifa_local;
  567. __be32 prefix = ifa->ifa_address&mask;
  568. if (ifa->ifa_flags&IFA_F_SECONDARY) {
  569. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  570. if (prim == NULL) {
  571. printk(KERN_DEBUG "fib_add_ifaddr: bug: prim == NULL\n");
  572. return;
  573. }
  574. }
  575. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
  576. if (!(dev->flags&IFF_UP))
  577. return;
  578. /* Add broadcast address, if it is explicitly assigned. */
  579. if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
  580. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  581. if (!ZERONET(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) &&
  582. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  583. fib_magic(RTM_NEWROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
  584. RTN_UNICAST, prefix, ifa->ifa_prefixlen, prim);
  585. /* Add network specific broadcasts, when it takes a sense */
  586. if (ifa->ifa_prefixlen < 31) {
  587. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
  588. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix|~mask, 32, prim);
  589. }
  590. }
  591. }
  592. static void fib_del_ifaddr(struct in_ifaddr *ifa)
  593. {
  594. struct in_device *in_dev = ifa->ifa_dev;
  595. struct net_device *dev = in_dev->dev;
  596. struct in_ifaddr *ifa1;
  597. struct in_ifaddr *prim = ifa;
  598. __be32 brd = ifa->ifa_address|~ifa->ifa_mask;
  599. __be32 any = ifa->ifa_address&ifa->ifa_mask;
  600. #define LOCAL_OK 1
  601. #define BRD_OK 2
  602. #define BRD0_OK 4
  603. #define BRD1_OK 8
  604. unsigned ok = 0;
  605. if (!(ifa->ifa_flags&IFA_F_SECONDARY))
  606. fib_magic(RTM_DELROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
  607. RTN_UNICAST, any, ifa->ifa_prefixlen, prim);
  608. else {
  609. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  610. if (prim == NULL) {
  611. printk(KERN_DEBUG "fib_del_ifaddr: bug: prim == NULL\n");
  612. return;
  613. }
  614. }
  615. /* Deletion is more complicated than add.
  616. We should take care of not to delete too much :-)
  617. Scan address list to be sure that addresses are really gone.
  618. */
  619. for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
  620. if (ifa->ifa_local == ifa1->ifa_local)
  621. ok |= LOCAL_OK;
  622. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  623. ok |= BRD_OK;
  624. if (brd == ifa1->ifa_broadcast)
  625. ok |= BRD1_OK;
  626. if (any == ifa1->ifa_broadcast)
  627. ok |= BRD0_OK;
  628. }
  629. if (!(ok&BRD_OK))
  630. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  631. if (!(ok&BRD1_OK))
  632. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
  633. if (!(ok&BRD0_OK))
  634. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
  635. if (!(ok&LOCAL_OK)) {
  636. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
  637. /* Check, that this local address finally disappeared. */
  638. if (inet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
  639. /* And the last, but not the least thing.
  640. We must flush stray FIB entries.
  641. First of all, we scan fib_info list searching
  642. for stray nexthop entries, then ignite fib_flush.
  643. */
  644. if (fib_sync_down(ifa->ifa_local, NULL, 0))
  645. fib_flush();
  646. }
  647. }
  648. #undef LOCAL_OK
  649. #undef BRD_OK
  650. #undef BRD0_OK
  651. #undef BRD1_OK
  652. }
  653. static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb )
  654. {
  655. struct fib_result res;
  656. struct flowi fl = { .mark = frn->fl_mark,
  657. .nl_u = { .ip4_u = { .daddr = frn->fl_addr,
  658. .tos = frn->fl_tos,
  659. .scope = frn->fl_scope } } };
  660. #ifdef CONFIG_IP_MULTIPLE_TABLES
  661. res.r = NULL;
  662. #endif
  663. frn->err = -ENOENT;
  664. if (tb) {
  665. local_bh_disable();
  666. frn->tb_id = tb->tb_id;
  667. frn->err = tb->tb_lookup(tb, &fl, &res);
  668. if (!frn->err) {
  669. frn->prefixlen = res.prefixlen;
  670. frn->nh_sel = res.nh_sel;
  671. frn->type = res.type;
  672. frn->scope = res.scope;
  673. fib_res_put(&res);
  674. }
  675. local_bh_enable();
  676. }
  677. }
  678. static void nl_fib_input(struct sock *sk, int len)
  679. {
  680. struct sk_buff *skb = NULL;
  681. struct nlmsghdr *nlh = NULL;
  682. struct fib_result_nl *frn;
  683. u32 pid;
  684. struct fib_table *tb;
  685. skb = skb_dequeue(&sk->sk_receive_queue);
  686. if (skb == NULL)
  687. return;
  688. nlh = (struct nlmsghdr *)skb->data;
  689. if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
  690. nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn))) {
  691. kfree_skb(skb);
  692. return;
  693. }
  694. frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
  695. tb = fib_get_table(frn->tb_id_in);
  696. nl_fib_lookup(frn, tb);
  697. pid = NETLINK_CB(skb).pid; /* pid of sending process */
  698. NETLINK_CB(skb).pid = 0; /* from kernel */
  699. NETLINK_CB(skb).dst_group = 0; /* unicast */
  700. netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
  701. }
  702. static void nl_fib_lookup_init(void)
  703. {
  704. netlink_kernel_create(NETLINK_FIB_LOOKUP, 0, nl_fib_input, THIS_MODULE);
  705. }
  706. static void fib_disable_ip(struct net_device *dev, int force)
  707. {
  708. if (fib_sync_down(0, dev, force))
  709. fib_flush();
  710. rt_cache_flush(0);
  711. arp_ifdown(dev);
  712. }
  713. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  714. {
  715. struct in_ifaddr *ifa = (struct in_ifaddr*)ptr;
  716. switch (event) {
  717. case NETDEV_UP:
  718. fib_add_ifaddr(ifa);
  719. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  720. fib_sync_up(ifa->ifa_dev->dev);
  721. #endif
  722. rt_cache_flush(-1);
  723. break;
  724. case NETDEV_DOWN:
  725. fib_del_ifaddr(ifa);
  726. if (ifa->ifa_dev->ifa_list == NULL) {
  727. /* Last address was deleted from this interface.
  728. Disable IP.
  729. */
  730. fib_disable_ip(ifa->ifa_dev->dev, 1);
  731. } else {
  732. rt_cache_flush(-1);
  733. }
  734. break;
  735. }
  736. return NOTIFY_DONE;
  737. }
  738. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  739. {
  740. struct net_device *dev = ptr;
  741. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  742. if (event == NETDEV_UNREGISTER) {
  743. fib_disable_ip(dev, 2);
  744. return NOTIFY_DONE;
  745. }
  746. if (!in_dev)
  747. return NOTIFY_DONE;
  748. switch (event) {
  749. case NETDEV_UP:
  750. for_ifa(in_dev) {
  751. fib_add_ifaddr(ifa);
  752. } endfor_ifa(in_dev);
  753. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  754. fib_sync_up(dev);
  755. #endif
  756. rt_cache_flush(-1);
  757. break;
  758. case NETDEV_DOWN:
  759. fib_disable_ip(dev, 0);
  760. break;
  761. case NETDEV_CHANGEMTU:
  762. case NETDEV_CHANGE:
  763. rt_cache_flush(0);
  764. break;
  765. }
  766. return NOTIFY_DONE;
  767. }
  768. static struct notifier_block fib_inetaddr_notifier = {
  769. .notifier_call =fib_inetaddr_event,
  770. };
  771. static struct notifier_block fib_netdev_notifier = {
  772. .notifier_call =fib_netdev_event,
  773. };
  774. void __init ip_fib_init(void)
  775. {
  776. unsigned int i;
  777. for (i = 0; i < FIB_TABLE_HASHSZ; i++)
  778. INIT_HLIST_HEAD(&fib_table_hash[i]);
  779. #ifndef CONFIG_IP_MULTIPLE_TABLES
  780. ip_fib_local_table = fib_hash_init(RT_TABLE_LOCAL);
  781. hlist_add_head_rcu(&ip_fib_local_table->tb_hlist, &fib_table_hash[0]);
  782. ip_fib_main_table = fib_hash_init(RT_TABLE_MAIN);
  783. hlist_add_head_rcu(&ip_fib_main_table->tb_hlist, &fib_table_hash[0]);
  784. #else
  785. fib4_rules_init();
  786. #endif
  787. register_netdevice_notifier(&fib_netdev_notifier);
  788. register_inetaddr_notifier(&fib_inetaddr_notifier);
  789. nl_fib_lookup_init();
  790. }
  791. EXPORT_SYMBOL(inet_addr_type);
  792. EXPORT_SYMBOL(ip_dev_find);