fib_frontend.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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/sched.h>
  25. #include <linux/mm.h>
  26. #include <linux/string.h>
  27. #include <linux/socket.h>
  28. #include <linux/sockios.h>
  29. #include <linux/errno.h>
  30. #include <linux/in.h>
  31. #include <linux/inet.h>
  32. #include <linux/inetdevice.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/if_addr.h>
  35. #include <linux/if_arp.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/netlink.h>
  38. #include <linux/init.h>
  39. #include <linux/list.h>
  40. #include <net/ip.h>
  41. #include <net/protocol.h>
  42. #include <net/route.h>
  43. #include <net/tcp.h>
  44. #include <net/sock.h>
  45. #include <net/icmp.h>
  46. #include <net/arp.h>
  47. #include <net/ip_fib.h>
  48. #define FFprint(a...) printk(KERN_DEBUG a)
  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(u32 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(u32 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(u32 src, u32 dst, u8 tos, int oif,
  158. struct net_device *dev, u32 *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. #ifndef CONFIG_IP_NOSIOCRT
  222. /*
  223. * Handle IP routing ioctl calls. These are used to manipulate the routing tables
  224. */
  225. int ip_rt_ioctl(unsigned int cmd, void __user *arg)
  226. {
  227. int err;
  228. struct kern_rta rta;
  229. struct rtentry r;
  230. struct {
  231. struct nlmsghdr nlh;
  232. struct rtmsg rtm;
  233. } req;
  234. switch (cmd) {
  235. case SIOCADDRT: /* Add a route */
  236. case SIOCDELRT: /* Delete a route */
  237. if (!capable(CAP_NET_ADMIN))
  238. return -EPERM;
  239. if (copy_from_user(&r, arg, sizeof(struct rtentry)))
  240. return -EFAULT;
  241. rtnl_lock();
  242. err = fib_convert_rtentry(cmd, &req.nlh, &req.rtm, &rta, &r);
  243. if (err == 0) {
  244. if (cmd == SIOCDELRT) {
  245. struct fib_table *tb = fib_get_table(req.rtm.rtm_table);
  246. err = -ESRCH;
  247. if (tb)
  248. err = tb->tb_delete(tb, &req.rtm, &rta, &req.nlh, NULL);
  249. } else {
  250. struct fib_table *tb = fib_new_table(req.rtm.rtm_table);
  251. err = -ENOBUFS;
  252. if (tb)
  253. err = tb->tb_insert(tb, &req.rtm, &rta, &req.nlh, NULL);
  254. }
  255. kfree(rta.rta_mx);
  256. }
  257. rtnl_unlock();
  258. return err;
  259. }
  260. return -EINVAL;
  261. }
  262. #else
  263. int ip_rt_ioctl(unsigned int cmd, void *arg)
  264. {
  265. return -EINVAL;
  266. }
  267. #endif
  268. static int inet_check_attr(struct rtmsg *r, struct rtattr **rta)
  269. {
  270. int i;
  271. for (i=1; i<=RTA_MAX; i++, rta++) {
  272. struct rtattr *attr = *rta;
  273. if (attr) {
  274. if (RTA_PAYLOAD(attr) < 4)
  275. return -EINVAL;
  276. if (i != RTA_MULTIPATH && i != RTA_METRICS &&
  277. i != RTA_TABLE)
  278. *rta = (struct rtattr*)RTA_DATA(attr);
  279. }
  280. }
  281. return 0;
  282. }
  283. int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  284. {
  285. struct fib_table * tb;
  286. struct rtattr **rta = arg;
  287. struct rtmsg *r = NLMSG_DATA(nlh);
  288. if (inet_check_attr(r, rta))
  289. return -EINVAL;
  290. tb = fib_get_table(rtm_get_table(rta, r->rtm_table));
  291. if (tb)
  292. return tb->tb_delete(tb, r, (struct kern_rta*)rta, nlh, &NETLINK_CB(skb));
  293. return -ESRCH;
  294. }
  295. int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
  296. {
  297. struct fib_table * tb;
  298. struct rtattr **rta = arg;
  299. struct rtmsg *r = NLMSG_DATA(nlh);
  300. if (inet_check_attr(r, rta))
  301. return -EINVAL;
  302. tb = fib_new_table(rtm_get_table(rta, r->rtm_table));
  303. if (tb)
  304. return tb->tb_insert(tb, r, (struct kern_rta*)rta, nlh, &NETLINK_CB(skb));
  305. return -ENOBUFS;
  306. }
  307. int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  308. {
  309. unsigned int h, s_h;
  310. unsigned int e = 0, s_e;
  311. struct fib_table *tb;
  312. struct hlist_node *node;
  313. int dumped = 0;
  314. if (NLMSG_PAYLOAD(cb->nlh, 0) >= sizeof(struct rtmsg) &&
  315. ((struct rtmsg*)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)
  316. return ip_rt_dump(skb, cb);
  317. s_h = cb->args[0];
  318. s_e = cb->args[1];
  319. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  320. e = 0;
  321. hlist_for_each_entry(tb, node, &fib_table_hash[h], tb_hlist) {
  322. if (e < s_e)
  323. goto next;
  324. if (dumped)
  325. memset(&cb->args[2], 0, sizeof(cb->args) -
  326. 2 * sizeof(cb->args[0]));
  327. if (tb->tb_dump(tb, skb, cb) < 0)
  328. goto out;
  329. dumped = 1;
  330. next:
  331. e++;
  332. }
  333. }
  334. out:
  335. cb->args[1] = e;
  336. cb->args[0] = h;
  337. return skb->len;
  338. }
  339. /* Prepare and feed intra-kernel routing request.
  340. Really, it should be netlink message, but :-( netlink
  341. can be not configured, so that we feed it directly
  342. to fib engine. It is legal, because all events occur
  343. only when netlink is already locked.
  344. */
  345. static void fib_magic(int cmd, int type, u32 dst, int dst_len, struct in_ifaddr *ifa)
  346. {
  347. struct fib_table * tb;
  348. struct {
  349. struct nlmsghdr nlh;
  350. struct rtmsg rtm;
  351. } req;
  352. struct kern_rta rta;
  353. memset(&req.rtm, 0, sizeof(req.rtm));
  354. memset(&rta, 0, sizeof(rta));
  355. if (type == RTN_UNICAST)
  356. tb = fib_new_table(RT_TABLE_MAIN);
  357. else
  358. tb = fib_new_table(RT_TABLE_LOCAL);
  359. if (tb == NULL)
  360. return;
  361. req.nlh.nlmsg_len = sizeof(req);
  362. req.nlh.nlmsg_type = cmd;
  363. req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_APPEND;
  364. req.nlh.nlmsg_pid = 0;
  365. req.nlh.nlmsg_seq = 0;
  366. req.rtm.rtm_dst_len = dst_len;
  367. req.rtm.rtm_table = tb->tb_id;
  368. req.rtm.rtm_protocol = RTPROT_KERNEL;
  369. req.rtm.rtm_scope = (type != RTN_LOCAL ? RT_SCOPE_LINK : RT_SCOPE_HOST);
  370. req.rtm.rtm_type = type;
  371. rta.rta_dst = &dst;
  372. rta.rta_prefsrc = &ifa->ifa_local;
  373. rta.rta_oif = &ifa->ifa_dev->dev->ifindex;
  374. if (cmd == RTM_NEWROUTE)
  375. tb->tb_insert(tb, &req.rtm, &rta, &req.nlh, NULL);
  376. else
  377. tb->tb_delete(tb, &req.rtm, &rta, &req.nlh, NULL);
  378. }
  379. void fib_add_ifaddr(struct in_ifaddr *ifa)
  380. {
  381. struct in_device *in_dev = ifa->ifa_dev;
  382. struct net_device *dev = in_dev->dev;
  383. struct in_ifaddr *prim = ifa;
  384. u32 mask = ifa->ifa_mask;
  385. u32 addr = ifa->ifa_local;
  386. u32 prefix = ifa->ifa_address&mask;
  387. if (ifa->ifa_flags&IFA_F_SECONDARY) {
  388. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  389. if (prim == NULL) {
  390. printk(KERN_DEBUG "fib_add_ifaddr: bug: prim == NULL\n");
  391. return;
  392. }
  393. }
  394. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
  395. if (!(dev->flags&IFF_UP))
  396. return;
  397. /* Add broadcast address, if it is explicitly assigned. */
  398. if (ifa->ifa_broadcast && ifa->ifa_broadcast != 0xFFFFFFFF)
  399. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  400. if (!ZERONET(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) &&
  401. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  402. fib_magic(RTM_NEWROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
  403. RTN_UNICAST, prefix, ifa->ifa_prefixlen, prim);
  404. /* Add network specific broadcasts, when it takes a sense */
  405. if (ifa->ifa_prefixlen < 31) {
  406. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
  407. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix|~mask, 32, prim);
  408. }
  409. }
  410. }
  411. static void fib_del_ifaddr(struct in_ifaddr *ifa)
  412. {
  413. struct in_device *in_dev = ifa->ifa_dev;
  414. struct net_device *dev = in_dev->dev;
  415. struct in_ifaddr *ifa1;
  416. struct in_ifaddr *prim = ifa;
  417. u32 brd = ifa->ifa_address|~ifa->ifa_mask;
  418. u32 any = ifa->ifa_address&ifa->ifa_mask;
  419. #define LOCAL_OK 1
  420. #define BRD_OK 2
  421. #define BRD0_OK 4
  422. #define BRD1_OK 8
  423. unsigned ok = 0;
  424. if (!(ifa->ifa_flags&IFA_F_SECONDARY))
  425. fib_magic(RTM_DELROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
  426. RTN_UNICAST, any, ifa->ifa_prefixlen, prim);
  427. else {
  428. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  429. if (prim == NULL) {
  430. printk(KERN_DEBUG "fib_del_ifaddr: bug: prim == NULL\n");
  431. return;
  432. }
  433. }
  434. /* Deletion is more complicated than add.
  435. We should take care of not to delete too much :-)
  436. Scan address list to be sure that addresses are really gone.
  437. */
  438. for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
  439. if (ifa->ifa_local == ifa1->ifa_local)
  440. ok |= LOCAL_OK;
  441. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  442. ok |= BRD_OK;
  443. if (brd == ifa1->ifa_broadcast)
  444. ok |= BRD1_OK;
  445. if (any == ifa1->ifa_broadcast)
  446. ok |= BRD0_OK;
  447. }
  448. if (!(ok&BRD_OK))
  449. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  450. if (!(ok&BRD1_OK))
  451. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
  452. if (!(ok&BRD0_OK))
  453. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
  454. if (!(ok&LOCAL_OK)) {
  455. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
  456. /* Check, that this local address finally disappeared. */
  457. if (inet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
  458. /* And the last, but not the least thing.
  459. We must flush stray FIB entries.
  460. First of all, we scan fib_info list searching
  461. for stray nexthop entries, then ignite fib_flush.
  462. */
  463. if (fib_sync_down(ifa->ifa_local, NULL, 0))
  464. fib_flush();
  465. }
  466. }
  467. #undef LOCAL_OK
  468. #undef BRD_OK
  469. #undef BRD0_OK
  470. #undef BRD1_OK
  471. }
  472. static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb )
  473. {
  474. struct fib_result res;
  475. struct flowi fl = { .nl_u = { .ip4_u = { .daddr = frn->fl_addr,
  476. .fwmark = frn->fl_fwmark,
  477. .tos = frn->fl_tos,
  478. .scope = frn->fl_scope } } };
  479. if (tb) {
  480. local_bh_disable();
  481. frn->tb_id = tb->tb_id;
  482. frn->err = tb->tb_lookup(tb, &fl, &res);
  483. if (!frn->err) {
  484. frn->prefixlen = res.prefixlen;
  485. frn->nh_sel = res.nh_sel;
  486. frn->type = res.type;
  487. frn->scope = res.scope;
  488. }
  489. local_bh_enable();
  490. }
  491. }
  492. static void nl_fib_input(struct sock *sk, int len)
  493. {
  494. struct sk_buff *skb = NULL;
  495. struct nlmsghdr *nlh = NULL;
  496. struct fib_result_nl *frn;
  497. u32 pid;
  498. struct fib_table *tb;
  499. skb = skb_dequeue(&sk->sk_receive_queue);
  500. nlh = (struct nlmsghdr *)skb->data;
  501. if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
  502. nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn))) {
  503. kfree_skb(skb);
  504. return;
  505. }
  506. frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
  507. tb = fib_get_table(frn->tb_id_in);
  508. nl_fib_lookup(frn, tb);
  509. pid = nlh->nlmsg_pid; /*pid of sending process */
  510. NETLINK_CB(skb).pid = 0; /* from kernel */
  511. NETLINK_CB(skb).dst_pid = pid;
  512. NETLINK_CB(skb).dst_group = 0; /* unicast */
  513. netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
  514. }
  515. static void nl_fib_lookup_init(void)
  516. {
  517. netlink_kernel_create(NETLINK_FIB_LOOKUP, 0, nl_fib_input, THIS_MODULE);
  518. }
  519. static void fib_disable_ip(struct net_device *dev, int force)
  520. {
  521. if (fib_sync_down(0, dev, force))
  522. fib_flush();
  523. rt_cache_flush(0);
  524. arp_ifdown(dev);
  525. }
  526. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  527. {
  528. struct in_ifaddr *ifa = (struct in_ifaddr*)ptr;
  529. switch (event) {
  530. case NETDEV_UP:
  531. fib_add_ifaddr(ifa);
  532. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  533. fib_sync_up(ifa->ifa_dev->dev);
  534. #endif
  535. rt_cache_flush(-1);
  536. break;
  537. case NETDEV_DOWN:
  538. fib_del_ifaddr(ifa);
  539. if (ifa->ifa_dev->ifa_list == NULL) {
  540. /* Last address was deleted from this interface.
  541. Disable IP.
  542. */
  543. fib_disable_ip(ifa->ifa_dev->dev, 1);
  544. } else {
  545. rt_cache_flush(-1);
  546. }
  547. break;
  548. }
  549. return NOTIFY_DONE;
  550. }
  551. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  552. {
  553. struct net_device *dev = ptr;
  554. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  555. if (event == NETDEV_UNREGISTER) {
  556. fib_disable_ip(dev, 2);
  557. return NOTIFY_DONE;
  558. }
  559. if (!in_dev)
  560. return NOTIFY_DONE;
  561. switch (event) {
  562. case NETDEV_UP:
  563. for_ifa(in_dev) {
  564. fib_add_ifaddr(ifa);
  565. } endfor_ifa(in_dev);
  566. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  567. fib_sync_up(dev);
  568. #endif
  569. rt_cache_flush(-1);
  570. break;
  571. case NETDEV_DOWN:
  572. fib_disable_ip(dev, 0);
  573. break;
  574. case NETDEV_CHANGEMTU:
  575. case NETDEV_CHANGE:
  576. rt_cache_flush(0);
  577. break;
  578. }
  579. return NOTIFY_DONE;
  580. }
  581. static struct notifier_block fib_inetaddr_notifier = {
  582. .notifier_call =fib_inetaddr_event,
  583. };
  584. static struct notifier_block fib_netdev_notifier = {
  585. .notifier_call =fib_netdev_event,
  586. };
  587. void __init ip_fib_init(void)
  588. {
  589. unsigned int i;
  590. for (i = 0; i < FIB_TABLE_HASHSZ; i++)
  591. INIT_HLIST_HEAD(&fib_table_hash[i]);
  592. #ifndef CONFIG_IP_MULTIPLE_TABLES
  593. ip_fib_local_table = fib_hash_init(RT_TABLE_LOCAL);
  594. hlist_add_head_rcu(&ip_fib_local_table->tb_hlist, &fib_table_hash[0]);
  595. ip_fib_main_table = fib_hash_init(RT_TABLE_MAIN);
  596. hlist_add_head_rcu(&ip_fib_main_table->tb_hlist, &fib_table_hash[0]);
  597. #else
  598. fib4_rules_init();
  599. #endif
  600. register_netdevice_notifier(&fib_netdev_notifier);
  601. register_inetaddr_notifier(&fib_inetaddr_notifier);
  602. nl_fib_lookup_init();
  603. }
  604. EXPORT_SYMBOL(inet_addr_type);
  605. EXPORT_SYMBOL(ip_dev_find);