fib_frontend.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/system.h>
  18. #include <linux/bitops.h>
  19. #include <linux/capability.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/string.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/errno.h>
  27. #include <linux/in.h>
  28. #include <linux/inet.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/if_addr.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/init.h>
  35. #include <linux/list.h>
  36. #include <linux/slab.h>
  37. #include <net/ip.h>
  38. #include <net/protocol.h>
  39. #include <net/route.h>
  40. #include <net/tcp.h>
  41. #include <net/sock.h>
  42. #include <net/arp.h>
  43. #include <net/ip_fib.h>
  44. #include <net/rtnetlink.h>
  45. #ifndef CONFIG_IP_MULTIPLE_TABLES
  46. static int __net_init fib4_rules_init(struct net *net)
  47. {
  48. struct fib_table *local_table, *main_table;
  49. local_table = fib_hash_table(RT_TABLE_LOCAL);
  50. if (local_table == NULL)
  51. return -ENOMEM;
  52. main_table = fib_hash_table(RT_TABLE_MAIN);
  53. if (main_table == NULL)
  54. goto fail;
  55. hlist_add_head_rcu(&local_table->tb_hlist,
  56. &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]);
  57. hlist_add_head_rcu(&main_table->tb_hlist,
  58. &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]);
  59. return 0;
  60. fail:
  61. kfree(local_table);
  62. return -ENOMEM;
  63. }
  64. #else
  65. struct fib_table *fib_new_table(struct net *net, u32 id)
  66. {
  67. struct fib_table *tb;
  68. unsigned int h;
  69. if (id == 0)
  70. id = RT_TABLE_MAIN;
  71. tb = fib_get_table(net, id);
  72. if (tb)
  73. return tb;
  74. tb = fib_hash_table(id);
  75. if (!tb)
  76. return NULL;
  77. h = id & (FIB_TABLE_HASHSZ - 1);
  78. hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
  79. return tb;
  80. }
  81. struct fib_table *fib_get_table(struct net *net, u32 id)
  82. {
  83. struct fib_table *tb;
  84. struct hlist_node *node;
  85. struct hlist_head *head;
  86. unsigned int h;
  87. if (id == 0)
  88. id = RT_TABLE_MAIN;
  89. h = id & (FIB_TABLE_HASHSZ - 1);
  90. rcu_read_lock();
  91. head = &net->ipv4.fib_table_hash[h];
  92. hlist_for_each_entry_rcu(tb, node, head, tb_hlist) {
  93. if (tb->tb_id == id) {
  94. rcu_read_unlock();
  95. return tb;
  96. }
  97. }
  98. rcu_read_unlock();
  99. return NULL;
  100. }
  101. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  102. void fib_select_default(struct net *net,
  103. const struct flowi *flp, struct fib_result *res)
  104. {
  105. struct fib_table *tb;
  106. int table = RT_TABLE_MAIN;
  107. #ifdef CONFIG_IP_MULTIPLE_TABLES
  108. if (res->r == NULL || res->r->action != FR_ACT_TO_TBL)
  109. return;
  110. table = res->r->table;
  111. #endif
  112. tb = fib_get_table(net, table);
  113. if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
  114. fib_table_select_default(tb, flp, res);
  115. }
  116. static void fib_flush(struct net *net)
  117. {
  118. int flushed = 0;
  119. struct fib_table *tb;
  120. struct hlist_node *node;
  121. struct hlist_head *head;
  122. unsigned int h;
  123. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  124. head = &net->ipv4.fib_table_hash[h];
  125. hlist_for_each_entry(tb, node, head, tb_hlist)
  126. flushed += fib_table_flush(tb);
  127. }
  128. if (flushed)
  129. rt_cache_flush(net, -1);
  130. }
  131. /**
  132. * __ip_dev_find - find the first device with a given source address.
  133. * @net: the net namespace
  134. * @addr: the source address
  135. * @devref: if true, take a reference on the found device
  136. *
  137. * If a caller uses devref=false, it should be protected by RCU
  138. */
  139. struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
  140. {
  141. struct flowi fl = {
  142. .nl_u = {
  143. .ip4_u = {
  144. .daddr = addr
  145. }
  146. },
  147. .flags = FLOWI_FLAG_MATCH_ANY_IIF
  148. };
  149. struct fib_result res = { 0 };
  150. struct net_device *dev = NULL;
  151. if (fib_lookup(net, &fl, &res))
  152. return NULL;
  153. if (res.type != RTN_LOCAL)
  154. goto out;
  155. dev = FIB_RES_DEV(res);
  156. if (dev && devref)
  157. dev_hold(dev);
  158. out:
  159. fib_res_put(&res);
  160. return dev;
  161. }
  162. EXPORT_SYMBOL(__ip_dev_find);
  163. /*
  164. * Find address type as if only "dev" was present in the system. If
  165. * on_dev is NULL then all interfaces are taken into consideration.
  166. */
  167. static inline unsigned __inet_dev_addr_type(struct net *net,
  168. const struct net_device *dev,
  169. __be32 addr)
  170. {
  171. struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
  172. struct fib_result res;
  173. unsigned ret = RTN_BROADCAST;
  174. struct fib_table *local_table;
  175. if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
  176. return RTN_BROADCAST;
  177. if (ipv4_is_multicast(addr))
  178. return RTN_MULTICAST;
  179. #ifdef CONFIG_IP_MULTIPLE_TABLES
  180. res.r = NULL;
  181. #endif
  182. local_table = fib_get_table(net, RT_TABLE_LOCAL);
  183. if (local_table) {
  184. ret = RTN_UNICAST;
  185. if (!fib_table_lookup(local_table, &fl, &res)) {
  186. if (!dev || dev == res.fi->fib_dev)
  187. ret = res.type;
  188. fib_res_put(&res);
  189. }
  190. }
  191. return ret;
  192. }
  193. unsigned int inet_addr_type(struct net *net, __be32 addr)
  194. {
  195. return __inet_dev_addr_type(net, NULL, addr);
  196. }
  197. EXPORT_SYMBOL(inet_addr_type);
  198. unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
  199. __be32 addr)
  200. {
  201. return __inet_dev_addr_type(net, dev, addr);
  202. }
  203. EXPORT_SYMBOL(inet_dev_addr_type);
  204. /* Given (packet source, input interface) and optional (dst, oif, tos):
  205. * - (main) check, that source is valid i.e. not broadcast or our local
  206. * address.
  207. * - figure out what "logical" interface this packet arrived
  208. * and calculate "specific destination" address.
  209. * - check, that packet arrived from expected physical interface.
  210. */
  211. int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
  212. struct net_device *dev, __be32 *spec_dst,
  213. u32 *itag, u32 mark)
  214. {
  215. struct in_device *in_dev;
  216. struct flowi fl = {
  217. .nl_u = {
  218. .ip4_u = {
  219. .daddr = src,
  220. .saddr = dst,
  221. .tos = tos
  222. }
  223. },
  224. .mark = mark,
  225. .iif = oif
  226. };
  227. struct fib_result res;
  228. int no_addr, rpf, accept_local;
  229. bool dev_match;
  230. int ret;
  231. struct net *net;
  232. no_addr = rpf = accept_local = 0;
  233. rcu_read_lock();
  234. in_dev = __in_dev_get_rcu(dev);
  235. if (in_dev) {
  236. no_addr = in_dev->ifa_list == NULL;
  237. rpf = IN_DEV_RPFILTER(in_dev);
  238. accept_local = IN_DEV_ACCEPT_LOCAL(in_dev);
  239. if (mark && !IN_DEV_SRC_VMARK(in_dev))
  240. fl.mark = 0;
  241. }
  242. rcu_read_unlock();
  243. if (in_dev == NULL)
  244. goto e_inval;
  245. net = dev_net(dev);
  246. if (fib_lookup(net, &fl, &res))
  247. goto last_resort;
  248. if (res.type != RTN_UNICAST) {
  249. if (res.type != RTN_LOCAL || !accept_local)
  250. goto e_inval_res;
  251. }
  252. *spec_dst = FIB_RES_PREFSRC(res);
  253. fib_combine_itag(itag, &res);
  254. dev_match = false;
  255. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  256. for (ret = 0; ret < res.fi->fib_nhs; ret++) {
  257. struct fib_nh *nh = &res.fi->fib_nh[ret];
  258. if (nh->nh_dev == dev) {
  259. dev_match = true;
  260. break;
  261. }
  262. }
  263. #else
  264. if (FIB_RES_DEV(res) == dev)
  265. dev_match = true;
  266. #endif
  267. if (dev_match) {
  268. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  269. fib_res_put(&res);
  270. return ret;
  271. }
  272. fib_res_put(&res);
  273. if (no_addr)
  274. goto last_resort;
  275. if (rpf == 1)
  276. goto e_rpf;
  277. fl.oif = dev->ifindex;
  278. ret = 0;
  279. if (fib_lookup(net, &fl, &res) == 0) {
  280. if (res.type == RTN_UNICAST) {
  281. *spec_dst = FIB_RES_PREFSRC(res);
  282. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  283. }
  284. fib_res_put(&res);
  285. }
  286. return ret;
  287. last_resort:
  288. if (rpf)
  289. goto e_rpf;
  290. *spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
  291. *itag = 0;
  292. return 0;
  293. e_inval_res:
  294. fib_res_put(&res);
  295. e_inval:
  296. return -EINVAL;
  297. e_rpf:
  298. return -EXDEV;
  299. }
  300. static inline __be32 sk_extract_addr(struct sockaddr *addr)
  301. {
  302. return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
  303. }
  304. static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
  305. {
  306. struct nlattr *nla;
  307. nla = (struct nlattr *) ((char *) mx + len);
  308. nla->nla_type = type;
  309. nla->nla_len = nla_attr_size(4);
  310. *(u32 *) nla_data(nla) = value;
  311. return len + nla_total_size(4);
  312. }
  313. static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
  314. struct fib_config *cfg)
  315. {
  316. __be32 addr;
  317. int plen;
  318. memset(cfg, 0, sizeof(*cfg));
  319. cfg->fc_nlinfo.nl_net = net;
  320. if (rt->rt_dst.sa_family != AF_INET)
  321. return -EAFNOSUPPORT;
  322. /*
  323. * Check mask for validity:
  324. * a) it must be contiguous.
  325. * b) destination must have all host bits clear.
  326. * c) if application forgot to set correct family (AF_INET),
  327. * reject request unless it is absolutely clear i.e.
  328. * both family and mask are zero.
  329. */
  330. plen = 32;
  331. addr = sk_extract_addr(&rt->rt_dst);
  332. if (!(rt->rt_flags & RTF_HOST)) {
  333. __be32 mask = sk_extract_addr(&rt->rt_genmask);
  334. if (rt->rt_genmask.sa_family != AF_INET) {
  335. if (mask || rt->rt_genmask.sa_family)
  336. return -EAFNOSUPPORT;
  337. }
  338. if (bad_mask(mask, addr))
  339. return -EINVAL;
  340. plen = inet_mask_len(mask);
  341. }
  342. cfg->fc_dst_len = plen;
  343. cfg->fc_dst = addr;
  344. if (cmd != SIOCDELRT) {
  345. cfg->fc_nlflags = NLM_F_CREATE;
  346. cfg->fc_protocol = RTPROT_BOOT;
  347. }
  348. if (rt->rt_metric)
  349. cfg->fc_priority = rt->rt_metric - 1;
  350. if (rt->rt_flags & RTF_REJECT) {
  351. cfg->fc_scope = RT_SCOPE_HOST;
  352. cfg->fc_type = RTN_UNREACHABLE;
  353. return 0;
  354. }
  355. cfg->fc_scope = RT_SCOPE_NOWHERE;
  356. cfg->fc_type = RTN_UNICAST;
  357. if (rt->rt_dev) {
  358. char *colon;
  359. struct net_device *dev;
  360. char devname[IFNAMSIZ];
  361. if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
  362. return -EFAULT;
  363. devname[IFNAMSIZ-1] = 0;
  364. colon = strchr(devname, ':');
  365. if (colon)
  366. *colon = 0;
  367. dev = __dev_get_by_name(net, devname);
  368. if (!dev)
  369. return -ENODEV;
  370. cfg->fc_oif = dev->ifindex;
  371. if (colon) {
  372. struct in_ifaddr *ifa;
  373. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  374. if (!in_dev)
  375. return -ENODEV;
  376. *colon = ':';
  377. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
  378. if (strcmp(ifa->ifa_label, devname) == 0)
  379. break;
  380. if (ifa == NULL)
  381. return -ENODEV;
  382. cfg->fc_prefsrc = ifa->ifa_local;
  383. }
  384. }
  385. addr = sk_extract_addr(&rt->rt_gateway);
  386. if (rt->rt_gateway.sa_family == AF_INET && addr) {
  387. cfg->fc_gw = addr;
  388. if (rt->rt_flags & RTF_GATEWAY &&
  389. inet_addr_type(net, addr) == RTN_UNICAST)
  390. cfg->fc_scope = RT_SCOPE_UNIVERSE;
  391. }
  392. if (cmd == SIOCDELRT)
  393. return 0;
  394. if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw)
  395. return -EINVAL;
  396. if (cfg->fc_scope == RT_SCOPE_NOWHERE)
  397. cfg->fc_scope = RT_SCOPE_LINK;
  398. if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
  399. struct nlattr *mx;
  400. int len = 0;
  401. mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL);
  402. if (mx == NULL)
  403. return -ENOMEM;
  404. if (rt->rt_flags & RTF_MTU)
  405. len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
  406. if (rt->rt_flags & RTF_WINDOW)
  407. len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
  408. if (rt->rt_flags & RTF_IRTT)
  409. len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
  410. cfg->fc_mx = mx;
  411. cfg->fc_mx_len = len;
  412. }
  413. return 0;
  414. }
  415. /*
  416. * Handle IP routing ioctl calls.
  417. * These are used to manipulate the routing tables
  418. */
  419. int ip_rt_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  420. {
  421. struct fib_config cfg;
  422. struct rtentry rt;
  423. int err;
  424. switch (cmd) {
  425. case SIOCADDRT: /* Add a route */
  426. case SIOCDELRT: /* Delete a route */
  427. if (!capable(CAP_NET_ADMIN))
  428. return -EPERM;
  429. if (copy_from_user(&rt, arg, sizeof(rt)))
  430. return -EFAULT;
  431. rtnl_lock();
  432. err = rtentry_to_fib_config(net, cmd, &rt, &cfg);
  433. if (err == 0) {
  434. struct fib_table *tb;
  435. if (cmd == SIOCDELRT) {
  436. tb = fib_get_table(net, cfg.fc_table);
  437. if (tb)
  438. err = fib_table_delete(tb, &cfg);
  439. else
  440. err = -ESRCH;
  441. } else {
  442. tb = fib_new_table(net, cfg.fc_table);
  443. if (tb)
  444. err = fib_table_insert(tb, &cfg);
  445. else
  446. err = -ENOBUFS;
  447. }
  448. /* allocated by rtentry_to_fib_config() */
  449. kfree(cfg.fc_mx);
  450. }
  451. rtnl_unlock();
  452. return err;
  453. }
  454. return -EINVAL;
  455. }
  456. const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
  457. [RTA_DST] = { .type = NLA_U32 },
  458. [RTA_SRC] = { .type = NLA_U32 },
  459. [RTA_IIF] = { .type = NLA_U32 },
  460. [RTA_OIF] = { .type = NLA_U32 },
  461. [RTA_GATEWAY] = { .type = NLA_U32 },
  462. [RTA_PRIORITY] = { .type = NLA_U32 },
  463. [RTA_PREFSRC] = { .type = NLA_U32 },
  464. [RTA_METRICS] = { .type = NLA_NESTED },
  465. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  466. [RTA_FLOW] = { .type = NLA_U32 },
  467. };
  468. static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
  469. struct nlmsghdr *nlh, struct fib_config *cfg)
  470. {
  471. struct nlattr *attr;
  472. int err, remaining;
  473. struct rtmsg *rtm;
  474. err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy);
  475. if (err < 0)
  476. goto errout;
  477. memset(cfg, 0, sizeof(*cfg));
  478. rtm = nlmsg_data(nlh);
  479. cfg->fc_dst_len = rtm->rtm_dst_len;
  480. cfg->fc_tos = rtm->rtm_tos;
  481. cfg->fc_table = rtm->rtm_table;
  482. cfg->fc_protocol = rtm->rtm_protocol;
  483. cfg->fc_scope = rtm->rtm_scope;
  484. cfg->fc_type = rtm->rtm_type;
  485. cfg->fc_flags = rtm->rtm_flags;
  486. cfg->fc_nlflags = nlh->nlmsg_flags;
  487. cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
  488. cfg->fc_nlinfo.nlh = nlh;
  489. cfg->fc_nlinfo.nl_net = net;
  490. if (cfg->fc_type > RTN_MAX) {
  491. err = -EINVAL;
  492. goto errout;
  493. }
  494. nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
  495. switch (nla_type(attr)) {
  496. case RTA_DST:
  497. cfg->fc_dst = nla_get_be32(attr);
  498. break;
  499. case RTA_OIF:
  500. cfg->fc_oif = nla_get_u32(attr);
  501. break;
  502. case RTA_GATEWAY:
  503. cfg->fc_gw = nla_get_be32(attr);
  504. break;
  505. case RTA_PRIORITY:
  506. cfg->fc_priority = nla_get_u32(attr);
  507. break;
  508. case RTA_PREFSRC:
  509. cfg->fc_prefsrc = nla_get_be32(attr);
  510. break;
  511. case RTA_METRICS:
  512. cfg->fc_mx = nla_data(attr);
  513. cfg->fc_mx_len = nla_len(attr);
  514. break;
  515. case RTA_MULTIPATH:
  516. cfg->fc_mp = nla_data(attr);
  517. cfg->fc_mp_len = nla_len(attr);
  518. break;
  519. case RTA_FLOW:
  520. cfg->fc_flow = nla_get_u32(attr);
  521. break;
  522. case RTA_TABLE:
  523. cfg->fc_table = nla_get_u32(attr);
  524. break;
  525. }
  526. }
  527. return 0;
  528. errout:
  529. return err;
  530. }
  531. static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  532. {
  533. struct net *net = sock_net(skb->sk);
  534. struct fib_config cfg;
  535. struct fib_table *tb;
  536. int err;
  537. err = rtm_to_fib_config(net, skb, nlh, &cfg);
  538. if (err < 0)
  539. goto errout;
  540. tb = fib_get_table(net, cfg.fc_table);
  541. if (tb == NULL) {
  542. err = -ESRCH;
  543. goto errout;
  544. }
  545. err = fib_table_delete(tb, &cfg);
  546. errout:
  547. return err;
  548. }
  549. static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  550. {
  551. struct net *net = sock_net(skb->sk);
  552. struct fib_config cfg;
  553. struct fib_table *tb;
  554. int err;
  555. err = rtm_to_fib_config(net, skb, nlh, &cfg);
  556. if (err < 0)
  557. goto errout;
  558. tb = fib_new_table(net, cfg.fc_table);
  559. if (tb == NULL) {
  560. err = -ENOBUFS;
  561. goto errout;
  562. }
  563. err = fib_table_insert(tb, &cfg);
  564. errout:
  565. return err;
  566. }
  567. static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  568. {
  569. struct net *net = sock_net(skb->sk);
  570. unsigned int h, s_h;
  571. unsigned int e = 0, s_e;
  572. struct fib_table *tb;
  573. struct hlist_node *node;
  574. struct hlist_head *head;
  575. int dumped = 0;
  576. if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
  577. ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
  578. return ip_rt_dump(skb, cb);
  579. s_h = cb->args[0];
  580. s_e = cb->args[1];
  581. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  582. e = 0;
  583. head = &net->ipv4.fib_table_hash[h];
  584. hlist_for_each_entry(tb, node, head, tb_hlist) {
  585. if (e < s_e)
  586. goto next;
  587. if (dumped)
  588. memset(&cb->args[2], 0, sizeof(cb->args) -
  589. 2 * sizeof(cb->args[0]));
  590. if (fib_table_dump(tb, skb, cb) < 0)
  591. goto out;
  592. dumped = 1;
  593. next:
  594. e++;
  595. }
  596. }
  597. out:
  598. cb->args[1] = e;
  599. cb->args[0] = h;
  600. return skb->len;
  601. }
  602. /* Prepare and feed intra-kernel routing request.
  603. * Really, it should be netlink message, but :-( netlink
  604. * can be not configured, so that we feed it directly
  605. * to fib engine. It is legal, because all events occur
  606. * only when netlink is already locked.
  607. */
  608. static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
  609. {
  610. struct net *net = dev_net(ifa->ifa_dev->dev);
  611. struct fib_table *tb;
  612. struct fib_config cfg = {
  613. .fc_protocol = RTPROT_KERNEL,
  614. .fc_type = type,
  615. .fc_dst = dst,
  616. .fc_dst_len = dst_len,
  617. .fc_prefsrc = ifa->ifa_local,
  618. .fc_oif = ifa->ifa_dev->dev->ifindex,
  619. .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
  620. .fc_nlinfo = {
  621. .nl_net = net,
  622. },
  623. };
  624. if (type == RTN_UNICAST)
  625. tb = fib_new_table(net, RT_TABLE_MAIN);
  626. else
  627. tb = fib_new_table(net, RT_TABLE_LOCAL);
  628. if (tb == NULL)
  629. return;
  630. cfg.fc_table = tb->tb_id;
  631. if (type != RTN_LOCAL)
  632. cfg.fc_scope = RT_SCOPE_LINK;
  633. else
  634. cfg.fc_scope = RT_SCOPE_HOST;
  635. if (cmd == RTM_NEWROUTE)
  636. fib_table_insert(tb, &cfg);
  637. else
  638. fib_table_delete(tb, &cfg);
  639. }
  640. void fib_add_ifaddr(struct in_ifaddr *ifa)
  641. {
  642. struct in_device *in_dev = ifa->ifa_dev;
  643. struct net_device *dev = in_dev->dev;
  644. struct in_ifaddr *prim = ifa;
  645. __be32 mask = ifa->ifa_mask;
  646. __be32 addr = ifa->ifa_local;
  647. __be32 prefix = ifa->ifa_address & mask;
  648. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  649. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  650. if (prim == NULL) {
  651. printk(KERN_WARNING "fib_add_ifaddr: bug: prim == NULL\n");
  652. return;
  653. }
  654. }
  655. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
  656. if (!(dev->flags & IFF_UP))
  657. return;
  658. /* Add broadcast address, if it is explicitly assigned. */
  659. if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
  660. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  661. if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
  662. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  663. fib_magic(RTM_NEWROUTE,
  664. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  665. prefix, ifa->ifa_prefixlen, prim);
  666. /* Add network specific broadcasts, when it takes a sense */
  667. if (ifa->ifa_prefixlen < 31) {
  668. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
  669. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
  670. 32, prim);
  671. }
  672. }
  673. }
  674. static void fib_del_ifaddr(struct in_ifaddr *ifa)
  675. {
  676. struct in_device *in_dev = ifa->ifa_dev;
  677. struct net_device *dev = in_dev->dev;
  678. struct in_ifaddr *ifa1;
  679. struct in_ifaddr *prim = ifa;
  680. __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
  681. __be32 any = ifa->ifa_address & ifa->ifa_mask;
  682. #define LOCAL_OK 1
  683. #define BRD_OK 2
  684. #define BRD0_OK 4
  685. #define BRD1_OK 8
  686. unsigned ok = 0;
  687. if (!(ifa->ifa_flags & IFA_F_SECONDARY))
  688. fib_magic(RTM_DELROUTE,
  689. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  690. any, ifa->ifa_prefixlen, prim);
  691. else {
  692. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  693. if (prim == NULL) {
  694. printk(KERN_WARNING "fib_del_ifaddr: bug: prim == NULL\n");
  695. return;
  696. }
  697. }
  698. /* Deletion is more complicated than add.
  699. * We should take care of not to delete too much :-)
  700. *
  701. * Scan address list to be sure that addresses are really gone.
  702. */
  703. for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
  704. if (ifa->ifa_local == ifa1->ifa_local)
  705. ok |= LOCAL_OK;
  706. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  707. ok |= BRD_OK;
  708. if (brd == ifa1->ifa_broadcast)
  709. ok |= BRD1_OK;
  710. if (any == ifa1->ifa_broadcast)
  711. ok |= BRD0_OK;
  712. }
  713. if (!(ok & BRD_OK))
  714. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  715. if (!(ok & BRD1_OK))
  716. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
  717. if (!(ok & BRD0_OK))
  718. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
  719. if (!(ok & LOCAL_OK)) {
  720. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
  721. /* Check, that this local address finally disappeared. */
  722. if (inet_addr_type(dev_net(dev), ifa->ifa_local) != RTN_LOCAL) {
  723. /* And the last, but not the least thing.
  724. * We must flush stray FIB entries.
  725. *
  726. * First of all, we scan fib_info list searching
  727. * for stray nexthop entries, then ignite fib_flush.
  728. */
  729. if (fib_sync_down_addr(dev_net(dev), ifa->ifa_local))
  730. fib_flush(dev_net(dev));
  731. }
  732. }
  733. #undef LOCAL_OK
  734. #undef BRD_OK
  735. #undef BRD0_OK
  736. #undef BRD1_OK
  737. }
  738. static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb)
  739. {
  740. struct fib_result res;
  741. struct flowi fl = {
  742. .mark = frn->fl_mark,
  743. .nl_u = {
  744. .ip4_u = {
  745. .daddr = frn->fl_addr,
  746. .tos = frn->fl_tos,
  747. .scope = frn->fl_scope
  748. }
  749. }
  750. };
  751. #ifdef CONFIG_IP_MULTIPLE_TABLES
  752. res.r = NULL;
  753. #endif
  754. frn->err = -ENOENT;
  755. if (tb) {
  756. local_bh_disable();
  757. frn->tb_id = tb->tb_id;
  758. frn->err = fib_table_lookup(tb, &fl, &res);
  759. if (!frn->err) {
  760. frn->prefixlen = res.prefixlen;
  761. frn->nh_sel = res.nh_sel;
  762. frn->type = res.type;
  763. frn->scope = res.scope;
  764. fib_res_put(&res);
  765. }
  766. local_bh_enable();
  767. }
  768. }
  769. static void nl_fib_input(struct sk_buff *skb)
  770. {
  771. struct net *net;
  772. struct fib_result_nl *frn;
  773. struct nlmsghdr *nlh;
  774. struct fib_table *tb;
  775. u32 pid;
  776. net = sock_net(skb->sk);
  777. nlh = nlmsg_hdr(skb);
  778. if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
  779. nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn)))
  780. return;
  781. skb = skb_clone(skb, GFP_KERNEL);
  782. if (skb == NULL)
  783. return;
  784. nlh = nlmsg_hdr(skb);
  785. frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
  786. tb = fib_get_table(net, frn->tb_id_in);
  787. nl_fib_lookup(frn, tb);
  788. pid = NETLINK_CB(skb).pid; /* pid of sending process */
  789. NETLINK_CB(skb).pid = 0; /* from kernel */
  790. NETLINK_CB(skb).dst_group = 0; /* unicast */
  791. netlink_unicast(net->ipv4.fibnl, skb, pid, MSG_DONTWAIT);
  792. }
  793. static int __net_init nl_fib_lookup_init(struct net *net)
  794. {
  795. struct sock *sk;
  796. sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, 0,
  797. nl_fib_input, NULL, THIS_MODULE);
  798. if (sk == NULL)
  799. return -EAFNOSUPPORT;
  800. net->ipv4.fibnl = sk;
  801. return 0;
  802. }
  803. static void nl_fib_lookup_exit(struct net *net)
  804. {
  805. netlink_kernel_release(net->ipv4.fibnl);
  806. net->ipv4.fibnl = NULL;
  807. }
  808. static void fib_disable_ip(struct net_device *dev, int force, int delay)
  809. {
  810. if (fib_sync_down_dev(dev, force))
  811. fib_flush(dev_net(dev));
  812. rt_cache_flush(dev_net(dev), delay);
  813. arp_ifdown(dev);
  814. }
  815. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  816. {
  817. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  818. struct net_device *dev = ifa->ifa_dev->dev;
  819. switch (event) {
  820. case NETDEV_UP:
  821. fib_add_ifaddr(ifa);
  822. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  823. fib_sync_up(dev);
  824. #endif
  825. rt_cache_flush(dev_net(dev), -1);
  826. break;
  827. case NETDEV_DOWN:
  828. fib_del_ifaddr(ifa);
  829. if (ifa->ifa_dev->ifa_list == NULL) {
  830. /* Last address was deleted from this interface.
  831. * Disable IP.
  832. */
  833. fib_disable_ip(dev, 1, 0);
  834. } else {
  835. rt_cache_flush(dev_net(dev), -1);
  836. }
  837. break;
  838. }
  839. return NOTIFY_DONE;
  840. }
  841. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  842. {
  843. struct net_device *dev = ptr;
  844. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  845. if (event == NETDEV_UNREGISTER) {
  846. fib_disable_ip(dev, 2, -1);
  847. return NOTIFY_DONE;
  848. }
  849. if (!in_dev)
  850. return NOTIFY_DONE;
  851. switch (event) {
  852. case NETDEV_UP:
  853. for_ifa(in_dev) {
  854. fib_add_ifaddr(ifa);
  855. } endfor_ifa(in_dev);
  856. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  857. fib_sync_up(dev);
  858. #endif
  859. rt_cache_flush(dev_net(dev), -1);
  860. break;
  861. case NETDEV_DOWN:
  862. fib_disable_ip(dev, 0, 0);
  863. break;
  864. case NETDEV_CHANGEMTU:
  865. case NETDEV_CHANGE:
  866. rt_cache_flush(dev_net(dev), 0);
  867. break;
  868. case NETDEV_UNREGISTER_BATCH:
  869. rt_cache_flush_batch();
  870. break;
  871. }
  872. return NOTIFY_DONE;
  873. }
  874. static struct notifier_block fib_inetaddr_notifier = {
  875. .notifier_call = fib_inetaddr_event,
  876. };
  877. static struct notifier_block fib_netdev_notifier = {
  878. .notifier_call = fib_netdev_event,
  879. };
  880. static int __net_init ip_fib_net_init(struct net *net)
  881. {
  882. int err;
  883. unsigned int i;
  884. net->ipv4.fib_table_hash = kzalloc(
  885. sizeof(struct hlist_head)*FIB_TABLE_HASHSZ, GFP_KERNEL);
  886. if (net->ipv4.fib_table_hash == NULL)
  887. return -ENOMEM;
  888. for (i = 0; i < FIB_TABLE_HASHSZ; i++)
  889. INIT_HLIST_HEAD(&net->ipv4.fib_table_hash[i]);
  890. err = fib4_rules_init(net);
  891. if (err < 0)
  892. goto fail;
  893. return 0;
  894. fail:
  895. kfree(net->ipv4.fib_table_hash);
  896. return err;
  897. }
  898. static void ip_fib_net_exit(struct net *net)
  899. {
  900. unsigned int i;
  901. #ifdef CONFIG_IP_MULTIPLE_TABLES
  902. fib4_rules_exit(net);
  903. #endif
  904. for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
  905. struct fib_table *tb;
  906. struct hlist_head *head;
  907. struct hlist_node *node, *tmp;
  908. head = &net->ipv4.fib_table_hash[i];
  909. hlist_for_each_entry_safe(tb, node, tmp, head, tb_hlist) {
  910. hlist_del(node);
  911. fib_table_flush(tb);
  912. kfree(tb);
  913. }
  914. }
  915. kfree(net->ipv4.fib_table_hash);
  916. }
  917. static int __net_init fib_net_init(struct net *net)
  918. {
  919. int error;
  920. error = ip_fib_net_init(net);
  921. if (error < 0)
  922. goto out;
  923. error = nl_fib_lookup_init(net);
  924. if (error < 0)
  925. goto out_nlfl;
  926. error = fib_proc_init(net);
  927. if (error < 0)
  928. goto out_proc;
  929. out:
  930. return error;
  931. out_proc:
  932. nl_fib_lookup_exit(net);
  933. out_nlfl:
  934. ip_fib_net_exit(net);
  935. goto out;
  936. }
  937. static void __net_exit fib_net_exit(struct net *net)
  938. {
  939. fib_proc_exit(net);
  940. nl_fib_lookup_exit(net);
  941. ip_fib_net_exit(net);
  942. }
  943. static struct pernet_operations fib_net_ops = {
  944. .init = fib_net_init,
  945. .exit = fib_net_exit,
  946. };
  947. void __init ip_fib_init(void)
  948. {
  949. rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL);
  950. rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL);
  951. rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib);
  952. register_pernet_subsys(&fib_net_ops);
  953. register_netdevice_notifier(&fib_netdev_notifier);
  954. register_inetaddr_notifier(&fib_inetaddr_notifier);
  955. fib_hash_init();
  956. }