kernel_socket.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /* Kernel communication using routing socket.
  2. * Copyright (C) 1999 Kunihiro Ishiguro
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "if.h"
  23. #include "prefix.h"
  24. #include "sockunion.h"
  25. #include "connected.h"
  26. #include "memory.h"
  27. #include "ioctl.h"
  28. #include "log.h"
  29. #include "str.h"
  30. #include "table.h"
  31. #include "rib.h"
  32. #include "privs.h"
  33. #include "zebra/interface.h"
  34. #include "zebra/zserv.h"
  35. #include "zebra/debug.h"
  36. extern struct zebra_privs_t zserv_privs;
  37. extern struct zebra_t zebrad;
  38. /*
  39. * Given a sockaddr length, round it up to include pad bytes following
  40. * it. Assumes the kernel pads to sizeof(long).
  41. *
  42. * XXX: why is ROUNDUP(0) sizeof(long)? 0 is an illegal sockaddr
  43. * length anyway (< sizeof (struct sockaddr)), so this shouldn't
  44. * matter.
  45. */
  46. #define ROUNDUP(a) \
  47. ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
  48. /*
  49. * Given a pointer (sockaddr or void *), return the number of bytes
  50. * taken up by the sockaddr and any padding needed for alignment.
  51. */
  52. #if defined(HAVE_SA_LEN)
  53. #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
  54. #elif defined(HAVE_IPV6)
  55. /*
  56. * One would hope all fixed-size structure definitions are aligned,
  57. * but round them up nonetheless.
  58. */
  59. #define SAROUNDUP(X) \
  60. (((struct sockaddr *)(X))->sa_family == AF_INET ? \
  61. ROUNDUP(sizeof(struct sockaddr_in)):\
  62. (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
  63. ROUNDUP(sizeof(struct sockaddr_in6)) : \
  64. (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
  65. ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
  66. #else /* HAVE_IPV6 */
  67. #define SAROUNDUP(X) \
  68. (((struct sockaddr *)(X))->sa_family == AF_INET ? \
  69. ROUNDUP(sizeof(struct sockaddr_in)):\
  70. (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
  71. ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
  72. #endif /* HAVE_SA_LEN */
  73. /* Routing socket message types. */
  74. struct message rtm_type_str[] =
  75. {
  76. {RTM_ADD, "RTM_ADD"},
  77. {RTM_DELETE, "RTM_DELETE"},
  78. {RTM_CHANGE, "RTM_CHANGE"},
  79. {RTM_GET, "RTM_GET"},
  80. {RTM_LOSING, "RTM_LOSING"},
  81. {RTM_REDIRECT, "RTM_REDIRECT"},
  82. {RTM_MISS, "RTM_MISS"},
  83. {RTM_LOCK, "RTM_LOCK"},
  84. {RTM_OLDADD, "RTM_OLDADD"},
  85. {RTM_OLDDEL, "RTM_OLDDEL"},
  86. {RTM_RESOLVE, "RTM_RESOLVE"},
  87. {RTM_NEWADDR, "RTM_NEWADDR"},
  88. {RTM_DELADDR, "RTM_DELADDR"},
  89. {RTM_IFINFO, "RTM_IFINFO"},
  90. #ifdef RTM_OIFINFO
  91. {RTM_OIFINFO, "RTM_OIFINFO"},
  92. #endif /* RTM_OIFINFO */
  93. #ifdef RTM_NEWMADDR
  94. {RTM_NEWMADDR, "RTM_NEWMADDR"},
  95. #endif /* RTM_NEWMADDR */
  96. #ifdef RTM_DELMADDR
  97. {RTM_DELMADDR, "RTM_DELMADDR"},
  98. #endif /* RTM_DELMADDR */
  99. #ifdef RTM_IFANNOUNCE
  100. {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
  101. #endif /* RTM_IFANNOUNCE */
  102. {0, NULL}
  103. };
  104. struct message rtm_flag_str[] =
  105. {
  106. {RTF_UP, "UP"},
  107. {RTF_GATEWAY, "GATEWAY"},
  108. {RTF_HOST, "HOST"},
  109. {RTF_REJECT, "REJECT"},
  110. {RTF_DYNAMIC, "DYNAMIC"},
  111. {RTF_MODIFIED, "MODIFIED"},
  112. {RTF_DONE, "DONE"},
  113. #ifdef RTF_MASK
  114. {RTF_MASK, "MASK"},
  115. #endif /* RTF_MASK */
  116. {RTF_CLONING, "CLONING"},
  117. {RTF_XRESOLVE, "XRESOLVE"},
  118. {RTF_LLINFO, "LLINFO"},
  119. {RTF_STATIC, "STATIC"},
  120. {RTF_BLACKHOLE, "BLACKHOLE"},
  121. {RTF_PROTO1, "PROTO1"},
  122. {RTF_PROTO2, "PROTO2"},
  123. #ifdef RTF_PRCLONING
  124. {RTF_PRCLONING, "PRCLONING"},
  125. #endif /* RTF_PRCLONING */
  126. #ifdef RTF_WASCLONED
  127. {RTF_WASCLONED, "WASCLONED"},
  128. #endif /* RTF_WASCLONED */
  129. #ifdef RTF_PROTO3
  130. {RTF_PROTO3, "PROTO3"},
  131. #endif /* RTF_PROTO3 */
  132. #ifdef RTF_PINNED
  133. {RTF_PINNED, "PINNED"},
  134. #endif /* RTF_PINNED */
  135. #ifdef RTF_LOCAL
  136. {RTF_LOCAL, "LOCAL"},
  137. #endif /* RTF_LOCAL */
  138. #ifdef RTF_BROADCAST
  139. {RTF_BROADCAST, "BROADCAST"},
  140. #endif /* RTF_BROADCAST */
  141. #ifdef RTF_MULTICAST
  142. {RTF_MULTICAST, "MULTICAST"},
  143. #endif /* RTF_MULTICAST */
  144. {0, NULL}
  145. };
  146. /* Kernel routing update socket. */
  147. int routing_sock = -1;
  148. /* Yes I'm checking ugly routing socket behavior. */
  149. /* #define DEBUG */
  150. /* Supported address family check. */
  151. static int
  152. af_check (int family)
  153. {
  154. if (family == AF_INET)
  155. return 1;
  156. #ifdef HAVE_IPV6
  157. if (family == AF_INET6)
  158. return 1;
  159. #endif /* HAVE_IPV6 */
  160. return 0;
  161. }
  162. /* Dump routing table flag for debug purpose. */
  163. void
  164. rtm_flag_dump (int flag)
  165. {
  166. struct message *mes;
  167. static char buf[BUFSIZ];
  168. buf[0] = '\0';
  169. for (mes = rtm_flag_str; mes->key != 0; mes++)
  170. {
  171. if (mes->key & flag)
  172. {
  173. strlcat (buf, mes->str, BUFSIZ);
  174. strlcat (buf, " ", BUFSIZ);
  175. }
  176. }
  177. zlog_info ("Kernel: %s", buf);
  178. }
  179. #ifdef RTM_IFANNOUNCE
  180. /* Interface adding function */
  181. int
  182. ifan_read (struct if_announcemsghdr *ifan)
  183. {
  184. struct interface *ifp;
  185. ifp = if_lookup_by_index (ifan->ifan_index);
  186. if (ifp == NULL && ifan->ifan_what == IFAN_ARRIVAL)
  187. {
  188. /* Create Interface */
  189. ifp = if_get_by_name (ifan->ifan_name);
  190. ifp->ifindex = ifan->ifan_index;
  191. if_add_update (ifp);
  192. }
  193. else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
  194. {
  195. if_delete_update (ifp);
  196. if_delete (ifp);
  197. }
  198. if_get_flags (ifp);
  199. if_get_mtu (ifp);
  200. if_get_metric (ifp);
  201. if (IS_ZEBRA_DEBUG_KERNEL)
  202. zlog_info ("interface %s index %d", ifp->name, ifp->ifindex);
  203. return 0;
  204. }
  205. #endif /* RTM_IFANNOUNCE */
  206. /*
  207. * Handle struct if_msghdr obtained from reading routing socket or
  208. * sysctl (from interface_list). There may or may not be sockaddrs
  209. * present after the header.
  210. */
  211. int
  212. ifm_read (struct if_msghdr *ifm)
  213. {
  214. struct interface *ifp = NULL;
  215. struct sockaddr_dl *sdl = NULL;
  216. void *cp;
  217. unsigned int i;
  218. char ifname[IFNAMSIZ];
  219. /* paranoia: sanity check structure */
  220. if (ifm->ifm_msglen < sizeof(struct if_msghdr))
  221. {
  222. zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
  223. ifm->ifm_msglen);
  224. return -1;
  225. }
  226. /*
  227. * Check for a sockaddr_dl following the message. First, point to
  228. * where a socakddr might be if one follows the message.
  229. */
  230. cp = (void *)(ifm + 1);
  231. #ifdef SUNOS_5
  232. /*
  233. * XXX This behavior should be narrowed to only the kernel versions
  234. * for which the structures returned do not match the headers.
  235. *
  236. * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
  237. * is 12 bytes larger than the 32 bit version.
  238. */
  239. if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
  240. cp = cp + 12;
  241. #endif
  242. /*
  243. * Check for each sockaddr in turn, advancing over it. After this
  244. * loop, sdl should point to a sockaddr_dl iff one was present.
  245. */
  246. for (i = 1; i != 0; i <<= 1)
  247. {
  248. if (i & ifm->ifm_addrs)
  249. {
  250. if (i == RTA_IFP)
  251. {
  252. sdl = (struct sockaddr_dl *)cp;
  253. break;
  254. }
  255. cp += SAROUNDUP(cp);
  256. }
  257. }
  258. /* Ensure that sdl, if present, is actually a sockaddr_dl. */
  259. if (sdl != NULL && sdl->sdl_family != AF_LINK)
  260. {
  261. zlog_err ("ifm_read: sockaddr_dl bad AF %d\n",
  262. sdl->sdl_family);
  263. return -1;
  264. }
  265. /*
  266. * Look up on ifindex first, because ifindices are the primary
  267. * handle for interfaces across the user/kernel boundary. (Some
  268. * messages, such as up/down status changes on NetBSD, do not
  269. * include a sockaddr_dl).
  270. */
  271. ifp = if_lookup_by_index (ifm->ifm_index);
  272. /*
  273. * If lookup by index was unsuccessful and we have a name, try
  274. * looking up by name. Interfaces specified in the configuration
  275. * file for which the ifindex has not been determined will have
  276. * ifindex == -1, and such interfaces are found by this search, and
  277. * then their ifindex values can be filled in.
  278. */
  279. if (ifp == NULL && sdl != NULL)
  280. {
  281. /*
  282. * paranoia: sanity check name length. nlen does not include
  283. * trailing zero, but IFNAMSIZ max length does.
  284. */
  285. if (sdl->sdl_nlen >= IFNAMSIZ)
  286. {
  287. zlog_err ("ifm_read: illegal sdl_nlen %d\n", sdl->sdl_nlen);
  288. return -1;
  289. }
  290. memcpy (ifname, sdl->sdl_data, sdl->sdl_nlen);
  291. ifname[sdl->sdl_nlen] = '\0';
  292. ifp = if_lookup_by_name (ifname);
  293. }
  294. /*
  295. * If ifp does not exist or has an invalid index (-1), create or
  296. * fill in an interface.
  297. */
  298. if ((ifp == NULL) || (ifp->ifindex == -1))
  299. {
  300. /*
  301. * To create or fill in an interface, a sockaddr_dl (via
  302. * RTA_IFP) is required.
  303. */
  304. if (sdl == NULL)
  305. {
  306. zlog_warn ("Interface index %d (new) missing RTA_IFP sockaddr_dl\n",
  307. ifm->ifm_index);
  308. return -1;
  309. }
  310. if (ifp == NULL)
  311. /* Interface that zebra was not previously aware of, so create. */
  312. ifp = if_create (sdl->sdl_data, sdl->sdl_nlen);
  313. /*
  314. * Fill in newly created interface structure, or larval
  315. * structure with ifindex -1.
  316. */
  317. ifp->ifindex = ifm->ifm_index;
  318. ifp->flags = ifm->ifm_flags;
  319. #if defined(__bsdi__)
  320. if_kvm_get_mtu (ifp);
  321. #else
  322. if_get_mtu (ifp);
  323. #endif /* __bsdi__ */
  324. if_get_metric (ifp);
  325. /*
  326. * XXX sockaddr_dl contents can be larger than the structure
  327. * definition, so the user of the stored structure must be
  328. * careful not to read off the end.
  329. */
  330. memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
  331. if_add_update (ifp);
  332. }
  333. else
  334. /*
  335. * Interface structure exists. Adjust stored flags from
  336. * notification. If interface has up->down or down->up
  337. * transition, call state change routines (to adjust routes,
  338. * notify routing daemons, etc.). (Other flag changes are stored
  339. * but apparently do not trigger action.)
  340. */
  341. {
  342. if (if_is_up (ifp))
  343. {
  344. ifp->flags = ifm->ifm_flags;
  345. if (! if_is_up (ifp))
  346. if_down (ifp);
  347. }
  348. else
  349. {
  350. ifp->flags = ifm->ifm_flags;
  351. if (if_is_up (ifp))
  352. if_up (ifp);
  353. }
  354. }
  355. #ifdef HAVE_NET_RT_IFLIST
  356. ifp->stats = ifm->ifm_data;
  357. #endif /* HAVE_NET_RT_IFLIST */
  358. if (IS_ZEBRA_DEBUG_KERNEL)
  359. zlog_info ("interface %s index %d", ifp->name, ifp->ifindex);
  360. return 0;
  361. }
  362. /* Address read from struct ifa_msghdr. */
  363. void
  364. ifam_read_mesg (struct ifa_msghdr *ifm,
  365. union sockunion *addr,
  366. union sockunion *mask,
  367. union sockunion *dest)
  368. {
  369. caddr_t pnt, end;
  370. pnt = (caddr_t)(ifm + 1);
  371. end = ((caddr_t)ifm) + ifm->ifam_msglen;
  372. #define IFAMADDRGET(X,R) \
  373. if (ifm->ifam_addrs & (R)) \
  374. { \
  375. int len = SAROUNDUP(pnt); \
  376. if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
  377. memcpy ((caddr_t)(X), pnt, len); \
  378. pnt += len; \
  379. }
  380. #define IFAMMASKGET(X,R) \
  381. if (ifm->ifam_addrs & (R)) \
  382. { \
  383. int len = SAROUNDUP(pnt); \
  384. if ((X) != NULL) \
  385. memcpy ((caddr_t)(X), pnt, len); \
  386. pnt += len; \
  387. }
  388. /* Be sure structure is cleared */
  389. memset (mask, 0, sizeof (union sockunion));
  390. memset (addr, 0, sizeof (union sockunion));
  391. memset (dest, 0, sizeof (union sockunion));
  392. /* We fetch each socket variable into sockunion. */
  393. IFAMADDRGET (NULL, RTA_DST);
  394. IFAMADDRGET (NULL, RTA_GATEWAY);
  395. IFAMMASKGET (mask, RTA_NETMASK);
  396. IFAMADDRGET (NULL, RTA_GENMASK);
  397. IFAMADDRGET (NULL, RTA_IFP);
  398. IFAMADDRGET (addr, RTA_IFA);
  399. IFAMADDRGET (NULL, RTA_AUTHOR);
  400. IFAMADDRGET (dest, RTA_BRD);
  401. /* Assert read up end point matches to end point */
  402. if (pnt != end)
  403. zlog_warn ("ifam_read() does't read all socket data");
  404. }
  405. /* Interface's address information get. */
  406. int
  407. ifam_read (struct ifa_msghdr *ifam)
  408. {
  409. struct interface *ifp;
  410. union sockunion addr, mask, gate;
  411. /* Check does this interface exist or not. */
  412. ifp = if_lookup_by_index (ifam->ifam_index);
  413. if (ifp == NULL)
  414. {
  415. zlog_warn ("no interface for index %d", ifam->ifam_index);
  416. return -1;
  417. }
  418. /* Allocate and read address information. */
  419. ifam_read_mesg (ifam, &addr, &mask, &gate);
  420. /* Check interface flag for implicit up of the interface. */
  421. if_refresh (ifp);
  422. /* Add connected address. */
  423. switch (sockunion_family (&addr))
  424. {
  425. case AF_INET:
  426. if (ifam->ifam_type == RTM_NEWADDR)
  427. connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr,
  428. ip_masklen (mask.sin.sin_addr),
  429. &gate.sin.sin_addr, NULL);
  430. else
  431. connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr,
  432. ip_masklen (mask.sin.sin_addr),
  433. &gate.sin.sin_addr, NULL);
  434. break;
  435. #ifdef HAVE_IPV6
  436. case AF_INET6:
  437. /* Unset interface index from link-local address when IPv6 stack
  438. is KAME. */
  439. if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
  440. SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
  441. if (ifam->ifam_type == RTM_NEWADDR)
  442. connected_add_ipv6 (ifp,
  443. &addr.sin6.sin6_addr,
  444. ip6_masklen (mask.sin6.sin6_addr),
  445. &gate.sin6.sin6_addr);
  446. else
  447. connected_delete_ipv6 (ifp,
  448. &addr.sin6.sin6_addr,
  449. ip6_masklen (mask.sin6.sin6_addr),
  450. &gate.sin6.sin6_addr);
  451. break;
  452. #endif /* HAVE_IPV6 */
  453. default:
  454. /* Unsupported family silently ignore... */
  455. break;
  456. }
  457. return 0;
  458. }
  459. /* Interface function for reading kernel routing table information. */
  460. int
  461. rtm_read_mesg (struct rt_msghdr *rtm,
  462. union sockunion *dest,
  463. union sockunion *mask,
  464. union sockunion *gate)
  465. {
  466. caddr_t pnt, end;
  467. /* Pnt points out socket data start point. */
  468. pnt = (caddr_t)(rtm + 1);
  469. end = ((caddr_t)rtm) + rtm->rtm_msglen;
  470. /* rt_msghdr version check. */
  471. if (rtm->rtm_version != RTM_VERSION)
  472. zlog (NULL, LOG_WARNING,
  473. "Routing message version different %d should be %d."
  474. "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
  475. #define RTMADDRGET(X,R) \
  476. if (rtm->rtm_addrs & (R)) \
  477. { \
  478. int len = SAROUNDUP (pnt); \
  479. if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
  480. memcpy ((caddr_t)(X), pnt, len); \
  481. pnt += len; \
  482. }
  483. #define RTMMASKGET(X,R) \
  484. if (rtm->rtm_addrs & (R)) \
  485. { \
  486. int len = SAROUNDUP (pnt); \
  487. if ((X) != NULL) \
  488. memcpy ((caddr_t)(X), pnt, len); \
  489. pnt += len; \
  490. }
  491. /* Be sure structure is cleared */
  492. memset (dest, 0, sizeof (union sockunion));
  493. memset (gate, 0, sizeof (union sockunion));
  494. memset (mask, 0, sizeof (union sockunion));
  495. /* We fetch each socket variable into sockunion. */
  496. RTMADDRGET (dest, RTA_DST);
  497. RTMADDRGET (gate, RTA_GATEWAY);
  498. RTMMASKGET (mask, RTA_NETMASK);
  499. RTMADDRGET (NULL, RTA_GENMASK);
  500. RTMADDRGET (NULL, RTA_IFP);
  501. RTMADDRGET (NULL, RTA_IFA);
  502. RTMADDRGET (NULL, RTA_AUTHOR);
  503. RTMADDRGET (NULL, RTA_BRD);
  504. /* If there is netmask information set it's family same as
  505. destination family*/
  506. if (rtm->rtm_addrs & RTA_NETMASK)
  507. mask->sa.sa_family = dest->sa.sa_family;
  508. /* Assert read up to the end of pointer. */
  509. if (pnt != end)
  510. zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
  511. return rtm->rtm_flags;
  512. }
  513. void
  514. rtm_read (struct rt_msghdr *rtm)
  515. {
  516. int flags;
  517. u_char zebra_flags;
  518. union sockunion dest, mask, gate;
  519. zebra_flags = 0;
  520. /* Discard self send message. */
  521. if (rtm->rtm_type != RTM_GET
  522. && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid))
  523. return;
  524. /* Read destination and netmask and gateway from rtm message
  525. structure. */
  526. flags = rtm_read_mesg (rtm, &dest, &mask, &gate);
  527. #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
  528. if (flags & RTF_CLONED)
  529. return;
  530. #endif
  531. #ifdef RTF_WASCLONED /*freebsd*/
  532. if (flags & RTF_WASCLONED)
  533. return;
  534. #endif
  535. if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
  536. return;
  537. /* This is connected route. */
  538. if (! (flags & RTF_GATEWAY))
  539. return;
  540. if (flags & RTF_PROTO1)
  541. SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
  542. /* This is persistent route. */
  543. if (flags & RTF_STATIC)
  544. SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
  545. /* This is a reject or blackhole route */
  546. if (flags & RTF_REJECT)
  547. SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
  548. if (flags & RTF_BLACKHOLE)
  549. SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
  550. if (dest.sa.sa_family == AF_INET)
  551. {
  552. struct prefix_ipv4 p;
  553. p.family = AF_INET;
  554. p.prefix = dest.sin.sin_addr;
  555. if (flags & RTF_HOST)
  556. p.prefixlen = IPV4_MAX_PREFIXLEN;
  557. else
  558. p.prefixlen = ip_masklen (mask.sin.sin_addr);
  559. if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
  560. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  561. &p, &gate.sin.sin_addr, 0, 0, 0, 0);
  562. else
  563. rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  564. &p, &gate.sin.sin_addr, 0, 0);
  565. }
  566. #ifdef HAVE_IPV6
  567. if (dest.sa.sa_family == AF_INET6)
  568. {
  569. struct prefix_ipv6 p;
  570. unsigned int ifindex = 0;
  571. p.family = AF_INET6;
  572. p.prefix = dest.sin6.sin6_addr;
  573. if (flags & RTF_HOST)
  574. p.prefixlen = IPV6_MAX_PREFIXLEN;
  575. else
  576. p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
  577. #ifdef KAME
  578. if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
  579. {
  580. ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
  581. SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
  582. }
  583. #endif /* KAME */
  584. if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
  585. rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  586. &p, &gate.sin6.sin6_addr, ifindex, 0);
  587. else
  588. rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  589. &p, &gate.sin6.sin6_addr, ifindex, 0);
  590. }
  591. #endif /* HAVE_IPV6 */
  592. }
  593. /* Interface function for the kernel routing table updates. Support
  594. for RTM_CHANGE will be needed. */
  595. int
  596. rtm_write (int message,
  597. union sockunion *dest,
  598. union sockunion *mask,
  599. union sockunion *gate,
  600. unsigned int index,
  601. int zebra_flags,
  602. int metric)
  603. {
  604. int ret;
  605. caddr_t pnt;
  606. struct interface *ifp;
  607. struct sockaddr_in tmp_gate;
  608. #ifdef HAVE_IPV6
  609. struct sockaddr_in6 tmp_gate6;
  610. #endif /* HAVE_IPV6 */
  611. /* Sequencial number of routing message. */
  612. static int msg_seq = 0;
  613. /* Struct of rt_msghdr and buffer for storing socket's data. */
  614. struct
  615. {
  616. struct rt_msghdr rtm;
  617. char buf[512];
  618. } msg;
  619. memset (&tmp_gate, 0, sizeof (struct sockaddr_in));
  620. tmp_gate.sin_family = AF_INET;
  621. #ifdef HAVE_SIN_LEN
  622. tmp_gate.sin_len = sizeof (struct sockaddr_in);
  623. #endif /* HAVE_SIN_LEN */
  624. #ifdef HAVE_IPV6
  625. memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6));
  626. tmp_gate6.sin6_family = AF_INET6;
  627. #ifdef SIN6_LEN
  628. tmp_gate6.sin6_len = sizeof (struct sockaddr_in6);
  629. #endif /* SIN6_LEN */
  630. #endif /* HAVE_IPV6 */
  631. if (routing_sock < 0)
  632. return ZEBRA_ERR_EPERM;
  633. /* Clear and set rt_msghdr values */
  634. memset (&msg, 0, sizeof (struct rt_msghdr));
  635. msg.rtm.rtm_version = RTM_VERSION;
  636. msg.rtm.rtm_type = message;
  637. msg.rtm.rtm_seq = msg_seq++;
  638. msg.rtm.rtm_addrs = RTA_DST;
  639. msg.rtm.rtm_addrs |= RTA_GATEWAY;
  640. msg.rtm.rtm_flags = RTF_UP;
  641. msg.rtm.rtm_index = index;
  642. if (metric != 0)
  643. {
  644. msg.rtm.rtm_rmx.rmx_hopcount = metric;
  645. msg.rtm.rtm_inits |= RTV_HOPCOUNT;
  646. }
  647. ifp = if_lookup_by_index (index);
  648. if (gate && message == RTM_ADD)
  649. msg.rtm.rtm_flags |= RTF_GATEWAY;
  650. if (! gate && message == RTM_ADD && ifp &&
  651. (ifp->flags & IFF_POINTOPOINT) == 0)
  652. msg.rtm.rtm_flags |= RTF_CLONING;
  653. /* If no protocol specific gateway is specified, use link
  654. address for gateway. */
  655. if (! gate)
  656. {
  657. if (!ifp)
  658. {
  659. zlog_warn ("no gateway found for interface index %d", index);
  660. return -1;
  661. }
  662. gate = (union sockunion *) & ifp->sdl;
  663. }
  664. if (mask)
  665. msg.rtm.rtm_addrs |= RTA_NETMASK;
  666. else if (message == RTM_ADD)
  667. msg.rtm.rtm_flags |= RTF_HOST;
  668. /* Tagging route with flags */
  669. msg.rtm.rtm_flags |= (RTF_PROTO1);
  670. /* Additional flags. */
  671. if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
  672. msg.rtm.rtm_flags |= RTF_BLACKHOLE;
  673. if (zebra_flags & ZEBRA_FLAG_REJECT)
  674. msg.rtm.rtm_flags |= RTF_REJECT;
  675. #ifdef HAVE_SIN_LEN
  676. #define SOCKADDRSET(X,R) \
  677. if (msg.rtm.rtm_addrs & (R)) \
  678. { \
  679. int len = ROUNDUP ((X)->sa.sa_len); \
  680. memcpy (pnt, (caddr_t)(X), len); \
  681. pnt += len; \
  682. }
  683. #else
  684. #define SOCKADDRSET(X,R) \
  685. if (msg.rtm.rtm_addrs & (R)) \
  686. { \
  687. int len = ROUNDUP (sizeof((X)->sa)); \
  688. memcpy (pnt, (caddr_t)(X), len); \
  689. pnt += len; \
  690. }
  691. #endif /* HAVE_SIN_LEN */
  692. pnt = (caddr_t) msg.buf;
  693. /* Write each socket data into rtm message buffer */
  694. SOCKADDRSET (dest, RTA_DST);
  695. SOCKADDRSET (gate, RTA_GATEWAY);
  696. SOCKADDRSET (mask, RTA_NETMASK);
  697. msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
  698. ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
  699. if (ret != msg.rtm.rtm_msglen)
  700. {
  701. if (errno == EEXIST)
  702. return ZEBRA_ERR_RTEXIST;
  703. if (errno == ENETUNREACH)
  704. return ZEBRA_ERR_RTUNREACH;
  705. zlog_warn ("write : %s (%d)", strerror (errno), errno);
  706. return -1;
  707. }
  708. return 0;
  709. }
  710. #include "thread.h"
  711. #include "zebra/zserv.h"
  712. /* For debug purpose. */
  713. void
  714. rtmsg_debug (struct rt_msghdr *rtm)
  715. {
  716. char *type = "Unknown";
  717. struct message *mes;
  718. for (mes = rtm_type_str; mes->str; mes++)
  719. if (mes->key == rtm->rtm_type)
  720. {
  721. type = mes->str;
  722. break;
  723. }
  724. zlog_info ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type);
  725. rtm_flag_dump (rtm->rtm_flags);
  726. zlog_info ("Kernel: message seq %d", rtm->rtm_seq);
  727. zlog_info ("Kernel: pid %d", rtm->rtm_pid);
  728. }
  729. /* This is pretty gross, better suggestions welcome -- mhandler */
  730. #ifndef RTAX_MAX
  731. #ifdef RTA_NUMBITS
  732. #define RTAX_MAX RTA_NUMBITS
  733. #else
  734. #define RTAX_MAX 8
  735. #endif /* RTA_NUMBITS */
  736. #endif /* RTAX_MAX */
  737. /* Kernel routing table and interface updates via routing socket. */
  738. int
  739. kernel_read (struct thread *thread)
  740. {
  741. int sock;
  742. int nbytes;
  743. struct rt_msghdr *rtm;
  744. /*
  745. * This must be big enough for any message the kernel might send.
  746. * Rather than determining how many sockaddrs of what size might be
  747. * in each particular message, just use RTAX_MAX of sockaddr_storage
  748. * for each. Note that the sockaddrs must be after each message
  749. * definition, or rather after whichever happens to be the largest,
  750. * since the buffer needs to be big enough for a message and the
  751. * sockaddrs together.
  752. */
  753. union
  754. {
  755. /* Routing information. */
  756. struct
  757. {
  758. struct rt_msghdr rtm;
  759. struct sockaddr_storage addr[RTAX_MAX];
  760. } r;
  761. /* Interface information. */
  762. struct
  763. {
  764. struct if_msghdr ifm;
  765. struct sockaddr_storage addr[RTAX_MAX];
  766. } im;
  767. /* Interface address information. */
  768. struct
  769. {
  770. struct ifa_msghdr ifa;
  771. struct sockaddr_storage addr[RTAX_MAX];
  772. } ia;
  773. #ifdef RTM_IFANNOUNCE
  774. /* Interface arrival/departure */
  775. struct
  776. {
  777. struct if_announcemsghdr ifan;
  778. struct sockaddr_storage addr[RTAX_MAX];
  779. } ian;
  780. #endif /* RTM_IFANNOUNCE */
  781. } buf;
  782. /* Fetch routing socket. */
  783. sock = THREAD_FD (thread);
  784. nbytes= read (sock, &buf, sizeof buf);
  785. if (nbytes <= 0)
  786. {
  787. if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
  788. zlog_warn ("routing socket error: %s", strerror (errno));
  789. return 0;
  790. }
  791. thread_add_read (zebrad.master, kernel_read, NULL, sock);
  792. if (IS_ZEBRA_DEBUG_KERNEL)
  793. rtmsg_debug (&buf.r.rtm);
  794. rtm = &buf.r.rtm;
  795. /*
  796. * Ensure that we didn't drop any data, so that processing routines
  797. * can assume they have the whole message.
  798. */
  799. if (rtm->rtm_msglen != nbytes)
  800. {
  801. zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
  802. rtm->rtm_msglen, nbytes, rtm->rtm_type);
  803. return -1;
  804. }
  805. switch (rtm->rtm_type)
  806. {
  807. case RTM_ADD:
  808. case RTM_DELETE:
  809. rtm_read (rtm);
  810. break;
  811. case RTM_IFINFO:
  812. ifm_read (&buf.im.ifm);
  813. break;
  814. case RTM_NEWADDR:
  815. case RTM_DELADDR:
  816. ifam_read (&buf.ia.ifa);
  817. break;
  818. #ifdef RTM_IFANNOUNCE
  819. case RTM_IFANNOUNCE:
  820. ifan_read (&buf.ian.ifan);
  821. break;
  822. #endif /* RTM_IFANNOUNCE */
  823. default:
  824. if (IS_ZEBRA_DEBUG_KERNEL)
  825. zlog_info("Unprocessed RTM_type: %d", rtm->rtm_type);
  826. break;
  827. }
  828. return 0;
  829. }
  830. /* Make routing socket. */
  831. void
  832. routing_socket ()
  833. {
  834. if ( zserv_privs.change (ZPRIVS_RAISE) )
  835. zlog_err ("routing_socket: Can't raise privileges");
  836. routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
  837. if (routing_sock < 0)
  838. {
  839. if ( zserv_privs.change (ZPRIVS_LOWER) )
  840. zlog_err ("routing_socket: Can't lower privileges");
  841. zlog_warn ("Can't init kernel routing socket");
  842. return;
  843. }
  844. if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
  845. zlog_warn ("Can't set O_NONBLOCK to routing socket");
  846. if ( zserv_privs.change (ZPRIVS_LOWER) )
  847. zlog_err ("routing_socket: Can't lower privileges");
  848. /* kernel_read needs rewrite. */
  849. thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
  850. }
  851. /* Exported interface function. This function simply calls
  852. routing_socket (). */
  853. void
  854. kernel_init ()
  855. {
  856. routing_socket ();
  857. }