rt_socket.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Kernel routing table updates by routing socket.
  3. * Copyright (C) 1997, 98 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "if.h"
  24. #include "prefix.h"
  25. #include "sockunion.h"
  26. #include "log.h"
  27. #include "str.h"
  28. #include "privs.h"
  29. #include "zebra/debug.h"
  30. #include "zebra/rib.h"
  31. #include "zebra/rt.h"
  32. #include "zebra/kernel_socket.h"
  33. extern struct zebra_privs_t zserv_privs;
  34. /* kernel socket export */
  35. extern int rtm_write (int message, union sockunion *dest,
  36. union sockunion *mask, union sockunion *gate,
  37. unsigned int index, int zebra_flags, int metric);
  38. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  39. /* Adjust netmask socket length. Return value is a adjusted sin_len
  40. value. */
  41. static int
  42. sin_masklen (struct in_addr mask)
  43. {
  44. char *p, *lim;
  45. int len;
  46. struct sockaddr_in sin;
  47. if (mask.s_addr == 0)
  48. return sizeof (long);
  49. sin.sin_addr = mask;
  50. len = sizeof (struct sockaddr_in);
  51. lim = (char *) &sin.sin_addr;
  52. p = lim + sizeof (sin.sin_addr);
  53. while (*--p == 0 && p >= lim)
  54. len--;
  55. return len;
  56. }
  57. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  58. /* Interface between zebra message and rtm message. */
  59. static int
  60. kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib)
  61. {
  62. struct sockaddr_in *mask = NULL;
  63. struct sockaddr_in sin_dest, sin_mask, sin_gate;
  64. struct nexthop *nexthop, *tnexthop;
  65. int recursing;
  66. int nexthop_num = 0;
  67. ifindex_t ifindex = 0;
  68. int gate = 0;
  69. int error;
  70. char prefix_buf[PREFIX_STRLEN];
  71. if (IS_ZEBRA_DEBUG_RIB)
  72. prefix2str (p, prefix_buf, sizeof(prefix_buf));
  73. memset (&sin_dest, 0, sizeof (struct sockaddr_in));
  74. sin_dest.sin_family = AF_INET;
  75. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  76. sin_dest.sin_len = sizeof (struct sockaddr_in);
  77. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  78. sin_dest.sin_addr = p->u.prefix4;
  79. memset (&sin_mask, 0, sizeof (struct sockaddr_in));
  80. memset (&sin_gate, 0, sizeof (struct sockaddr_in));
  81. sin_gate.sin_family = AF_INET;
  82. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  83. sin_gate.sin_len = sizeof (struct sockaddr_in);
  84. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  85. /* Make gateway. */
  86. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  87. {
  88. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  89. continue;
  90. gate = 0;
  91. char gate_buf[INET_ADDRSTRLEN] = "NULL";
  92. /*
  93. * XXX We need to refrain from kernel operations in some cases,
  94. * but this if statement seems overly cautious - what about
  95. * other than ADD and DELETE?
  96. */
  97. if ((cmd == RTM_ADD
  98. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  99. || (cmd == RTM_DELETE
  100. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
  101. ))
  102. {
  103. if (nexthop->type == NEXTHOP_TYPE_IPV4 ||
  104. nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  105. {
  106. sin_gate.sin_addr = nexthop->gate.ipv4;
  107. gate = 1;
  108. }
  109. if (nexthop->type == NEXTHOP_TYPE_IFINDEX
  110. || nexthop->type == NEXTHOP_TYPE_IFNAME
  111. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  112. ifindex = nexthop->ifindex;
  113. if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
  114. {
  115. struct in_addr loopback;
  116. loopback.s_addr = htonl (INADDR_LOOPBACK);
  117. sin_gate.sin_addr = loopback;
  118. gate = 1;
  119. }
  120. if (gate && p->prefixlen == 32)
  121. mask = NULL;
  122. else
  123. {
  124. masklen2ip (p->prefixlen, &sin_mask.sin_addr);
  125. sin_mask.sin_family = AF_INET;
  126. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  127. sin_mask.sin_len = sin_masklen (sin_mask.sin_addr);
  128. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  129. mask = &sin_mask;
  130. }
  131. error = rtm_write (cmd,
  132. (union sockunion *)&sin_dest,
  133. (union sockunion *)mask,
  134. gate ? (union sockunion *)&sin_gate : NULL,
  135. ifindex,
  136. rib->flags,
  137. rib->metric);
  138. if (IS_ZEBRA_DEBUG_RIB)
  139. {
  140. if (!gate)
  141. {
  142. zlog_debug ("%s: %s: attention! gate not found for rib %p",
  143. __func__, prefix_buf, rib);
  144. rib_dump (p, rib);
  145. }
  146. else
  147. inet_ntop (AF_INET, &sin_gate.sin_addr, gate_buf, INET_ADDRSTRLEN);
  148. }
  149. switch (error)
  150. {
  151. /* We only flag nexthops as being in FIB if rtm_write() did its work. */
  152. case ZEBRA_ERR_NOERROR:
  153. nexthop_num++;
  154. if (IS_ZEBRA_DEBUG_RIB)
  155. zlog_debug ("%s: %s: successfully did NH %s",
  156. __func__, prefix_buf, gate_buf);
  157. if (cmd == RTM_ADD)
  158. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  159. break;
  160. /* The only valid case for this error is kernel's failure to install
  161. * a multipath route, which is common for FreeBSD. This should be
  162. * ignored silently, but logged as an error otherwise.
  163. */
  164. case ZEBRA_ERR_RTEXIST:
  165. if (cmd != RTM_ADD)
  166. zlog_err ("%s: rtm_write() returned %d for command %d",
  167. __func__, error, cmd);
  168. continue;
  169. break;
  170. /* Given that our NEXTHOP_FLAG_FIB matches real kernel FIB, it isn't
  171. * normal to get any other messages in ANY case.
  172. */
  173. case ZEBRA_ERR_RTNOEXIST:
  174. case ZEBRA_ERR_RTUNREACH:
  175. default:
  176. zlog_err ("%s: %s: rtm_write() unexpectedly returned %d for command %s",
  177. __func__, prefix2str(p, prefix_buf, sizeof(prefix_buf)),
  178. error, lookup (rtm_type_str, cmd));
  179. break;
  180. }
  181. } /* if (cmd and flags make sense) */
  182. else
  183. if (IS_ZEBRA_DEBUG_RIB)
  184. zlog_debug ("%s: odd command %s for flags %d",
  185. __func__, lookup (rtm_type_str, cmd), nexthop->flags);
  186. } /* for (ALL_NEXTHOPS_RO(...))*/
  187. /* If there was no useful nexthop, then complain. */
  188. if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL)
  189. zlog_debug ("%s: No useful nexthops were found in RIB entry %p", __func__, rib);
  190. return 0; /*XXX*/
  191. }
  192. #ifdef HAVE_IPV6
  193. #ifdef SIN6_LEN
  194. /* Calculate sin6_len value for netmask socket value. */
  195. static int
  196. sin6_masklen (struct in6_addr mask)
  197. {
  198. struct sockaddr_in6 sin6;
  199. char *p, *lim;
  200. int len;
  201. if (IN6_IS_ADDR_UNSPECIFIED (&mask))
  202. return sizeof (long);
  203. sin6.sin6_addr = mask;
  204. len = sizeof (struct sockaddr_in6);
  205. lim = (char *) & sin6.sin6_addr;
  206. p = lim + sizeof (sin6.sin6_addr);
  207. while (*--p == 0 && p >= lim)
  208. len--;
  209. return len;
  210. }
  211. #endif /* SIN6_LEN */
  212. /* Interface between zebra message and rtm message. */
  213. static int
  214. kernel_rtm_ipv6 (int cmd, struct prefix *p, struct rib *rib)
  215. {
  216. struct sockaddr_in6 *mask;
  217. struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
  218. struct nexthop *nexthop, *tnexthop;
  219. int recursing;
  220. int nexthop_num = 0;
  221. ifindex_t ifindex = 0;
  222. int gate = 0;
  223. int error;
  224. memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
  225. sin_dest.sin6_family = AF_INET6;
  226. #ifdef SIN6_LEN
  227. sin_dest.sin6_len = sizeof (struct sockaddr_in6);
  228. #endif /* SIN6_LEN */
  229. sin_dest.sin6_addr = p->u.prefix6;
  230. memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
  231. memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
  232. sin_gate.sin6_family = AF_INET6;
  233. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  234. sin_gate.sin6_len = sizeof (struct sockaddr_in6);
  235. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  236. /* Make gateway. */
  237. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  238. {
  239. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  240. continue;
  241. gate = 0;
  242. if ((cmd == RTM_ADD
  243. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  244. || (cmd == RTM_DELETE
  245. #if 0
  246. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
  247. #endif
  248. ))
  249. {
  250. if (nexthop->type == NEXTHOP_TYPE_IPV6
  251. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  252. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  253. {
  254. sin_gate.sin6_addr = nexthop->gate.ipv6;
  255. gate = 1;
  256. }
  257. if (nexthop->type == NEXTHOP_TYPE_IFINDEX
  258. || nexthop->type == NEXTHOP_TYPE_IFNAME
  259. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  260. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  261. ifindex = nexthop->ifindex;
  262. if (cmd == RTM_ADD)
  263. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  264. }
  265. /* Under kame set interface index to link local address. */
  266. #ifdef KAME
  267. #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
  268. do { \
  269. (a).s6_addr[2] = ((i) >> 8) & 0xff; \
  270. (a).s6_addr[3] = (i) & 0xff; \
  271. } while (0)
  272. if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
  273. SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex);
  274. #endif /* KAME */
  275. if (gate && p->prefixlen == 128)
  276. mask = NULL;
  277. else
  278. {
  279. masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr);
  280. sin_mask.sin6_family = AF_INET6;
  281. #ifdef SIN6_LEN
  282. sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
  283. #endif /* SIN6_LEN */
  284. mask = &sin_mask;
  285. }
  286. error = rtm_write (cmd,
  287. (union sockunion *) &sin_dest,
  288. (union sockunion *) mask,
  289. gate ? (union sockunion *)&sin_gate : NULL,
  290. ifindex,
  291. rib->flags,
  292. rib->metric);
  293. #if 0
  294. if (error)
  295. {
  296. zlog_info ("kernel_rtm_ipv6(): nexthop %d add error=%d.",
  297. nexthop_num, error);
  298. }
  299. #else
  300. (void)error;
  301. #endif
  302. nexthop_num++;
  303. }
  304. /* If there is no useful nexthop then return. */
  305. if (nexthop_num == 0)
  306. {
  307. if (IS_ZEBRA_DEBUG_KERNEL)
  308. zlog_debug ("kernel_rtm_ipv6(): No useful nexthop.");
  309. return 0;
  310. }
  311. return 0; /*XXX*/
  312. }
  313. #endif
  314. static int
  315. kernel_rtm (int cmd, struct prefix *p, struct rib *rib)
  316. {
  317. switch (PREFIX_FAMILY(p))
  318. {
  319. case AF_INET:
  320. return kernel_rtm_ipv4 (cmd, p, rib);
  321. case AF_INET6:
  322. return kernel_rtm_ipv6 (cmd, p, rib);
  323. }
  324. return 0;
  325. }
  326. int
  327. kernel_route_rib (struct prefix *p, struct rib *old, struct rib *new)
  328. {
  329. int route = 0;
  330. if (zserv_privs.change(ZPRIVS_RAISE))
  331. zlog (NULL, LOG_ERR, "Can't raise privileges");
  332. if (old)
  333. route |= kernel_rtm (RTM_DELETE, p, old);
  334. if (new)
  335. route |= kernel_rtm (RTM_ADD, p, new);
  336. if (zserv_privs.change(ZPRIVS_LOWER))
  337. zlog (NULL, LOG_ERR, "Can't lower privileges");
  338. return route;
  339. }