fib_frontend.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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.26 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. #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/init.h>
  37. #include <linux/list.h>
  38. #include <net/ip.h>
  39. #include <net/protocol.h>
  40. #include <net/route.h>
  41. #include <net/tcp.h>
  42. #include <net/sock.h>
  43. #include <net/icmp.h>
  44. #include <net/arp.h>
  45. #include <net/ip_fib.h>
  46. #include <net/rtnetlink.h>
  47. #define FFprint(a...) printk(KERN_DEBUG a)
  48. static struct sock *fibnl;
  49. #ifndef CONFIG_IP_MULTIPLE_TABLES
  50. struct fib_table *ip_fib_local_table;
  51. struct fib_table *ip_fib_main_table;
  52. #define FIB_TABLE_HASHSZ 1
  53. static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
  54. #else
  55. #define FIB_TABLE_HASHSZ 256
  56. static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
  57. struct fib_table *fib_new_table(u32 id)
  58. {
  59. struct fib_table *tb;
  60. unsigned int h;
  61. if (id == 0)
  62. id = RT_TABLE_MAIN;
  63. tb = fib_get_table(id);
  64. if (tb)
  65. return tb;
  66. tb = fib_hash_init(id);
  67. if (!tb)
  68. return NULL;
  69. h = id & (FIB_TABLE_HASHSZ - 1);
  70. hlist_add_head_rcu(&tb->tb_hlist, &fib_table_hash[h]);
  71. return tb;
  72. }
  73. struct fib_table *fib_get_table(u32 id)
  74. {
  75. struct fib_table *tb;
  76. struct hlist_node *node;
  77. unsigned int h;
  78. if (id == 0)
  79. id = RT_TABLE_MAIN;
  80. h = id & (FIB_TABLE_HASHSZ - 1);
  81. rcu_read_lock();
  82. hlist_for_each_entry_rcu(tb, node, &fib_table_hash[h], tb_hlist) {
  83. if (tb->tb_id == id) {
  84. rcu_read_unlock();
  85. return tb;
  86. }
  87. }
  88. rcu_read_unlock();
  89. return NULL;
  90. }
  91. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  92. static void fib_flush(void)
  93. {
  94. int flushed = 0;
  95. struct fib_table *tb;
  96. struct hlist_node *node;
  97. unsigned int h;
  98. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  99. hlist_for_each_entry(tb, node, &fib_table_hash[h], tb_hlist)
  100. flushed += tb->tb_flush(tb);
  101. }
  102. if (flushed)
  103. rt_cache_flush(-1);
  104. }
  105. /*
  106. * Find the first device with a given source address.
  107. */
  108. struct net_device * ip_dev_find(__be32 addr)
  109. {
  110. struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
  111. struct fib_result res;
  112. struct net_device *dev = NULL;
  113. #ifdef CONFIG_IP_MULTIPLE_TABLES
  114. res.r = NULL;
  115. #endif
  116. if (!ip_fib_local_table ||
  117. ip_fib_local_table->tb_lookup(ip_fib_local_table, &fl, &res))
  118. return NULL;
  119. if (res.type != RTN_LOCAL)
  120. goto out;
  121. dev = FIB_RES_DEV(res);
  122. if (dev)
  123. dev_hold(dev);
  124. out:
  125. fib_res_put(&res);
  126. return dev;
  127. }
  128. unsigned inet_addr_type(__be32 addr)
  129. {
  130. struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
  131. struct fib_result res;
  132. unsigned ret = RTN_BROADCAST;
  133. if (ZERONET(addr) || BADCLASS(addr))
  134. return RTN_BROADCAST;
  135. if (MULTICAST(addr))
  136. return RTN_MULTICAST;
  137. #ifdef CONFIG_IP_MULTIPLE_TABLES
  138. res.r = NULL;
  139. #endif
  140. if (ip_fib_local_table) {
  141. ret = RTN_UNICAST;
  142. if (!ip_fib_local_table->tb_lookup(ip_fib_local_table,
  143. &fl, &res)) {
  144. ret = res.type;
  145. fib_res_put(&res);
  146. }
  147. }
  148. return ret;
  149. }
  150. /* Given (packet source, input interface) and optional (dst, oif, tos):
  151. - (main) check, that source is valid i.e. not broadcast or our local
  152. address.
  153. - figure out what "logical" interface this packet arrived
  154. and calculate "specific destination" address.
  155. - check, that packet arrived from expected physical interface.
  156. */
  157. int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
  158. struct net_device *dev, __be32 *spec_dst, u32 *itag)
  159. {
  160. struct in_device *in_dev;
  161. struct flowi fl = { .nl_u = { .ip4_u =
  162. { .daddr = src,
  163. .saddr = dst,
  164. .tos = tos } },
  165. .iif = oif };
  166. struct fib_result res;
  167. int no_addr, rpf;
  168. int ret;
  169. no_addr = rpf = 0;
  170. rcu_read_lock();
  171. in_dev = __in_dev_get_rcu(dev);
  172. if (in_dev) {
  173. no_addr = in_dev->ifa_list == NULL;
  174. rpf = IN_DEV_RPFILTER(in_dev);
  175. }
  176. rcu_read_unlock();
  177. if (in_dev == NULL)
  178. goto e_inval;
  179. if (fib_lookup(&fl, &res))
  180. goto last_resort;
  181. if (res.type != RTN_UNICAST)
  182. goto e_inval_res;
  183. *spec_dst = FIB_RES_PREFSRC(res);
  184. fib_combine_itag(itag, &res);
  185. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  186. if (FIB_RES_DEV(res) == dev || res.fi->fib_nhs > 1)
  187. #else
  188. if (FIB_RES_DEV(res) == dev)
  189. #endif
  190. {
  191. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  192. fib_res_put(&res);
  193. return ret;
  194. }
  195. fib_res_put(&res);
  196. if (no_addr)
  197. goto last_resort;
  198. if (rpf)
  199. goto e_inval;
  200. fl.oif = dev->ifindex;
  201. ret = 0;
  202. if (fib_lookup(&fl, &res) == 0) {
  203. if (res.type == RTN_UNICAST) {
  204. *spec_dst = FIB_RES_PREFSRC(res);
  205. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  206. }
  207. fib_res_put(&res);
  208. }
  209. return ret;
  210. last_resort:
  211. if (rpf)
  212. goto e_inval;
  213. *spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
  214. *itag = 0;
  215. return 0;
  216. e_inval_res:
  217. fib_res_put(&res);
  218. e_inval:
  219. return -EINVAL;
  220. }
  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(&init_net, 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. const struct nla_policy rtm_ipv4_policy[RTA_MAX+1] = {
  376. [RTA_DST] = { .type = NLA_U32 },
  377. [RTA_SRC] = { .type = NLA_U32 },
  378. [RTA_IIF] = { .type = NLA_U32 },
  379. [RTA_OIF] = { .type = NLA_U32 },
  380. [RTA_GATEWAY] = { .type = NLA_U32 },
  381. [RTA_PRIORITY] = { .type = NLA_U32 },
  382. [RTA_PREFSRC] = { .type = NLA_U32 },
  383. [RTA_METRICS] = { .type = NLA_NESTED },
  384. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  385. [RTA_PROTOINFO] = { .type = NLA_U32 },
  386. [RTA_FLOW] = { .type = NLA_U32 },
  387. };
  388. static int rtm_to_fib_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  389. struct fib_config *cfg)
  390. {
  391. struct nlattr *attr;
  392. int err, remaining;
  393. struct rtmsg *rtm;
  394. err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy);
  395. if (err < 0)
  396. goto errout;
  397. memset(cfg, 0, sizeof(*cfg));
  398. rtm = nlmsg_data(nlh);
  399. cfg->fc_dst_len = rtm->rtm_dst_len;
  400. cfg->fc_tos = rtm->rtm_tos;
  401. cfg->fc_table = rtm->rtm_table;
  402. cfg->fc_protocol = rtm->rtm_protocol;
  403. cfg->fc_scope = rtm->rtm_scope;
  404. cfg->fc_type = rtm->rtm_type;
  405. cfg->fc_flags = rtm->rtm_flags;
  406. cfg->fc_nlflags = nlh->nlmsg_flags;
  407. cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
  408. cfg->fc_nlinfo.nlh = nlh;
  409. if (cfg->fc_type > RTN_MAX) {
  410. err = -EINVAL;
  411. goto errout;
  412. }
  413. nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
  414. switch (nla_type(attr)) {
  415. case RTA_DST:
  416. cfg->fc_dst = nla_get_be32(attr);
  417. break;
  418. case RTA_OIF:
  419. cfg->fc_oif = nla_get_u32(attr);
  420. break;
  421. case RTA_GATEWAY:
  422. cfg->fc_gw = nla_get_be32(attr);
  423. break;
  424. case RTA_PRIORITY:
  425. cfg->fc_priority = nla_get_u32(attr);
  426. break;
  427. case RTA_PREFSRC:
  428. cfg->fc_prefsrc = nla_get_be32(attr);
  429. break;
  430. case RTA_METRICS:
  431. cfg->fc_mx = nla_data(attr);
  432. cfg->fc_mx_len = nla_len(attr);
  433. break;
  434. case RTA_MULTIPATH:
  435. cfg->fc_mp = nla_data(attr);
  436. cfg->fc_mp_len = nla_len(attr);
  437. break;
  438. case RTA_FLOW:
  439. cfg->fc_flow = nla_get_u32(attr);
  440. break;
  441. case RTA_TABLE:
  442. cfg->fc_table = nla_get_u32(attr);
  443. break;
  444. }
  445. }
  446. return 0;
  447. errout:
  448. return err;
  449. }
  450. static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  451. {
  452. struct fib_config cfg;
  453. struct fib_table *tb;
  454. int err;
  455. err = rtm_to_fib_config(skb, nlh, &cfg);
  456. if (err < 0)
  457. goto errout;
  458. tb = fib_get_table(cfg.fc_table);
  459. if (tb == NULL) {
  460. err = -ESRCH;
  461. goto errout;
  462. }
  463. err = tb->tb_delete(tb, &cfg);
  464. errout:
  465. return err;
  466. }
  467. static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  468. {
  469. struct fib_config cfg;
  470. struct fib_table *tb;
  471. int err;
  472. err = rtm_to_fib_config(skb, nlh, &cfg);
  473. if (err < 0)
  474. goto errout;
  475. tb = fib_new_table(cfg.fc_table);
  476. if (tb == NULL) {
  477. err = -ENOBUFS;
  478. goto errout;
  479. }
  480. err = tb->tb_insert(tb, &cfg);
  481. errout:
  482. return err;
  483. }
  484. static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  485. {
  486. unsigned int h, s_h;
  487. unsigned int e = 0, s_e;
  488. struct fib_table *tb;
  489. struct hlist_node *node;
  490. int dumped = 0;
  491. if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
  492. ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
  493. return ip_rt_dump(skb, cb);
  494. s_h = cb->args[0];
  495. s_e = cb->args[1];
  496. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  497. e = 0;
  498. hlist_for_each_entry(tb, node, &fib_table_hash[h], tb_hlist) {
  499. if (e < s_e)
  500. goto next;
  501. if (dumped)
  502. memset(&cb->args[2], 0, sizeof(cb->args) -
  503. 2 * sizeof(cb->args[0]));
  504. if (tb->tb_dump(tb, skb, cb) < 0)
  505. goto out;
  506. dumped = 1;
  507. next:
  508. e++;
  509. }
  510. }
  511. out:
  512. cb->args[1] = e;
  513. cb->args[0] = h;
  514. return skb->len;
  515. }
  516. /* Prepare and feed intra-kernel routing request.
  517. Really, it should be netlink message, but :-( netlink
  518. can be not configured, so that we feed it directly
  519. to fib engine. It is legal, because all events occur
  520. only when netlink is already locked.
  521. */
  522. static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
  523. {
  524. struct fib_table *tb;
  525. struct fib_config cfg = {
  526. .fc_protocol = RTPROT_KERNEL,
  527. .fc_type = type,
  528. .fc_dst = dst,
  529. .fc_dst_len = dst_len,
  530. .fc_prefsrc = ifa->ifa_local,
  531. .fc_oif = ifa->ifa_dev->dev->ifindex,
  532. .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
  533. };
  534. if (type == RTN_UNICAST)
  535. tb = fib_new_table(RT_TABLE_MAIN);
  536. else
  537. tb = fib_new_table(RT_TABLE_LOCAL);
  538. if (tb == NULL)
  539. return;
  540. cfg.fc_table = tb->tb_id;
  541. if (type != RTN_LOCAL)
  542. cfg.fc_scope = RT_SCOPE_LINK;
  543. else
  544. cfg.fc_scope = RT_SCOPE_HOST;
  545. if (cmd == RTM_NEWROUTE)
  546. tb->tb_insert(tb, &cfg);
  547. else
  548. tb->tb_delete(tb, &cfg);
  549. }
  550. void fib_add_ifaddr(struct in_ifaddr *ifa)
  551. {
  552. struct in_device *in_dev = ifa->ifa_dev;
  553. struct net_device *dev = in_dev->dev;
  554. struct in_ifaddr *prim = ifa;
  555. __be32 mask = ifa->ifa_mask;
  556. __be32 addr = ifa->ifa_local;
  557. __be32 prefix = ifa->ifa_address&mask;
  558. if (ifa->ifa_flags&IFA_F_SECONDARY) {
  559. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  560. if (prim == NULL) {
  561. printk(KERN_DEBUG "fib_add_ifaddr: bug: prim == NULL\n");
  562. return;
  563. }
  564. }
  565. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
  566. if (!(dev->flags&IFF_UP))
  567. return;
  568. /* Add broadcast address, if it is explicitly assigned. */
  569. if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
  570. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  571. if (!ZERONET(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) &&
  572. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  573. fib_magic(RTM_NEWROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
  574. RTN_UNICAST, prefix, ifa->ifa_prefixlen, prim);
  575. /* Add network specific broadcasts, when it takes a sense */
  576. if (ifa->ifa_prefixlen < 31) {
  577. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
  578. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix|~mask, 32, prim);
  579. }
  580. }
  581. }
  582. static void fib_del_ifaddr(struct in_ifaddr *ifa)
  583. {
  584. struct in_device *in_dev = ifa->ifa_dev;
  585. struct net_device *dev = in_dev->dev;
  586. struct in_ifaddr *ifa1;
  587. struct in_ifaddr *prim = ifa;
  588. __be32 brd = ifa->ifa_address|~ifa->ifa_mask;
  589. __be32 any = ifa->ifa_address&ifa->ifa_mask;
  590. #define LOCAL_OK 1
  591. #define BRD_OK 2
  592. #define BRD0_OK 4
  593. #define BRD1_OK 8
  594. unsigned ok = 0;
  595. if (!(ifa->ifa_flags&IFA_F_SECONDARY))
  596. fib_magic(RTM_DELROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
  597. RTN_UNICAST, any, ifa->ifa_prefixlen, prim);
  598. else {
  599. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  600. if (prim == NULL) {
  601. printk(KERN_DEBUG "fib_del_ifaddr: bug: prim == NULL\n");
  602. return;
  603. }
  604. }
  605. /* Deletion is more complicated than add.
  606. We should take care of not to delete too much :-)
  607. Scan address list to be sure that addresses are really gone.
  608. */
  609. for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
  610. if (ifa->ifa_local == ifa1->ifa_local)
  611. ok |= LOCAL_OK;
  612. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  613. ok |= BRD_OK;
  614. if (brd == ifa1->ifa_broadcast)
  615. ok |= BRD1_OK;
  616. if (any == ifa1->ifa_broadcast)
  617. ok |= BRD0_OK;
  618. }
  619. if (!(ok&BRD_OK))
  620. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  621. if (!(ok&BRD1_OK))
  622. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
  623. if (!(ok&BRD0_OK))
  624. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
  625. if (!(ok&LOCAL_OK)) {
  626. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
  627. /* Check, that this local address finally disappeared. */
  628. if (inet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
  629. /* And the last, but not the least thing.
  630. We must flush stray FIB entries.
  631. First of all, we scan fib_info list searching
  632. for stray nexthop entries, then ignite fib_flush.
  633. */
  634. if (fib_sync_down(ifa->ifa_local, NULL, 0))
  635. fib_flush();
  636. }
  637. }
  638. #undef LOCAL_OK
  639. #undef BRD_OK
  640. #undef BRD0_OK
  641. #undef BRD1_OK
  642. }
  643. static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb )
  644. {
  645. struct fib_result res;
  646. struct flowi fl = { .mark = frn->fl_mark,
  647. .nl_u = { .ip4_u = { .daddr = frn->fl_addr,
  648. .tos = frn->fl_tos,
  649. .scope = frn->fl_scope } } };
  650. #ifdef CONFIG_IP_MULTIPLE_TABLES
  651. res.r = NULL;
  652. #endif
  653. frn->err = -ENOENT;
  654. if (tb) {
  655. local_bh_disable();
  656. frn->tb_id = tb->tb_id;
  657. frn->err = tb->tb_lookup(tb, &fl, &res);
  658. if (!frn->err) {
  659. frn->prefixlen = res.prefixlen;
  660. frn->nh_sel = res.nh_sel;
  661. frn->type = res.type;
  662. frn->scope = res.scope;
  663. fib_res_put(&res);
  664. }
  665. local_bh_enable();
  666. }
  667. }
  668. static void nl_fib_input(struct sk_buff *skb)
  669. {
  670. struct fib_result_nl *frn;
  671. struct nlmsghdr *nlh;
  672. struct fib_table *tb;
  673. u32 pid;
  674. nlh = nlmsg_hdr(skb);
  675. if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
  676. nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn))) {
  677. kfree_skb(skb);
  678. return;
  679. }
  680. frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
  681. tb = fib_get_table(frn->tb_id_in);
  682. nl_fib_lookup(frn, tb);
  683. pid = NETLINK_CB(skb).pid; /* pid of sending process */
  684. NETLINK_CB(skb).pid = 0; /* from kernel */
  685. NETLINK_CB(skb).dst_group = 0; /* unicast */
  686. netlink_unicast(fibnl, skb, pid, MSG_DONTWAIT);
  687. }
  688. static void nl_fib_lookup_init(void)
  689. {
  690. fibnl = netlink_kernel_create(&init_net, NETLINK_FIB_LOOKUP, 0,
  691. nl_fib_input, NULL, THIS_MODULE);
  692. }
  693. static void fib_disable_ip(struct net_device *dev, int force)
  694. {
  695. if (fib_sync_down(0, dev, force))
  696. fib_flush();
  697. rt_cache_flush(0);
  698. arp_ifdown(dev);
  699. }
  700. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  701. {
  702. struct in_ifaddr *ifa = (struct in_ifaddr*)ptr;
  703. switch (event) {
  704. case NETDEV_UP:
  705. fib_add_ifaddr(ifa);
  706. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  707. fib_sync_up(ifa->ifa_dev->dev);
  708. #endif
  709. rt_cache_flush(-1);
  710. break;
  711. case NETDEV_DOWN:
  712. fib_del_ifaddr(ifa);
  713. if (ifa->ifa_dev->ifa_list == NULL) {
  714. /* Last address was deleted from this interface.
  715. Disable IP.
  716. */
  717. fib_disable_ip(ifa->ifa_dev->dev, 1);
  718. } else {
  719. rt_cache_flush(-1);
  720. }
  721. break;
  722. }
  723. return NOTIFY_DONE;
  724. }
  725. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  726. {
  727. struct net_device *dev = ptr;
  728. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  729. if (dev->nd_net != &init_net)
  730. return NOTIFY_DONE;
  731. if (event == NETDEV_UNREGISTER) {
  732. fib_disable_ip(dev, 2);
  733. return NOTIFY_DONE;
  734. }
  735. if (!in_dev)
  736. return NOTIFY_DONE;
  737. switch (event) {
  738. case NETDEV_UP:
  739. for_ifa(in_dev) {
  740. fib_add_ifaddr(ifa);
  741. } endfor_ifa(in_dev);
  742. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  743. fib_sync_up(dev);
  744. #endif
  745. rt_cache_flush(-1);
  746. break;
  747. case NETDEV_DOWN:
  748. fib_disable_ip(dev, 0);
  749. break;
  750. case NETDEV_CHANGEMTU:
  751. case NETDEV_CHANGE:
  752. rt_cache_flush(0);
  753. break;
  754. }
  755. return NOTIFY_DONE;
  756. }
  757. static struct notifier_block fib_inetaddr_notifier = {
  758. .notifier_call =fib_inetaddr_event,
  759. };
  760. static struct notifier_block fib_netdev_notifier = {
  761. .notifier_call =fib_netdev_event,
  762. };
  763. void __init ip_fib_init(void)
  764. {
  765. unsigned int i;
  766. for (i = 0; i < FIB_TABLE_HASHSZ; i++)
  767. INIT_HLIST_HEAD(&fib_table_hash[i]);
  768. #ifndef CONFIG_IP_MULTIPLE_TABLES
  769. ip_fib_local_table = fib_hash_init(RT_TABLE_LOCAL);
  770. hlist_add_head_rcu(&ip_fib_local_table->tb_hlist, &fib_table_hash[0]);
  771. ip_fib_main_table = fib_hash_init(RT_TABLE_MAIN);
  772. hlist_add_head_rcu(&ip_fib_main_table->tb_hlist, &fib_table_hash[0]);
  773. #else
  774. fib4_rules_init();
  775. #endif
  776. register_netdevice_notifier(&fib_netdev_notifier);
  777. register_inetaddr_notifier(&fib_inetaddr_notifier);
  778. nl_fib_lookup_init();
  779. rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL);
  780. rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL);
  781. rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib);
  782. }
  783. EXPORT_SYMBOL(inet_addr_type);
  784. EXPORT_SYMBOL(ip_dev_find);