rt_netlink.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  1. /* Kernel routing table updates using netlink over GNU/Linux system.
  2. * Copyright (C) 1997, 98, 99 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. /* Hack for GNU libc version 2. */
  23. #ifndef MSG_TRUNC
  24. #define MSG_TRUNC 0x20
  25. #endif /* MSG_TRUNC */
  26. #include "linklist.h"
  27. #include "if.h"
  28. #include "log.h"
  29. #include "prefix.h"
  30. #include "connected.h"
  31. #include "table.h"
  32. #include "rib.h"
  33. #include "thread.h"
  34. #include "privs.h"
  35. #include "zebra/zserv.h"
  36. #include "zebra/redistribute.h"
  37. #include "zebra/interface.h"
  38. #include "zebra/debug.h"
  39. /* Socket interface to kernel */
  40. struct nlsock
  41. {
  42. int sock;
  43. int seq;
  44. struct sockaddr_nl snl;
  45. char *name;
  46. } netlink = { -1, 0, {0}, "netlink-listen"}, /* kernel messages */
  47. netlink_cmd = { -1, 0, {0}, "netlink-cmd"}, /* command channel */
  48. netlink_addr = { -1, 0, {0}, "netlink-addr"}; /* address channel */
  49. struct message nlmsg_str[] = {
  50. {RTM_NEWROUTE, "RTM_NEWROUTE"},
  51. {RTM_DELROUTE, "RTM_DELROUTE"},
  52. {RTM_GETROUTE, "RTM_GETROUTE"},
  53. {RTM_NEWLINK, "RTM_NEWLINK"},
  54. {RTM_DELLINK, "RTM_DELLINK"},
  55. {RTM_GETLINK, "RTM_GETLINK"},
  56. {RTM_NEWADDR, "RTM_NEWADDR"},
  57. {RTM_DELADDR, "RTM_DELADDR"},
  58. {RTM_GETADDR, "RTM_GETADDR"},
  59. {0, NULL}
  60. };
  61. char *nexthop_types_desc[] =
  62. {
  63. "none",
  64. "Directly connected",
  65. "Interface route",
  66. "IPv4 nexthop",
  67. "IPv4 nexthop with ifindex",
  68. "IPv4 nexthop with ifname",
  69. "IPv6 nexthop"
  70. "IPv6 nexthop with ifindex",
  71. "IPv6 nexthop with ifname",
  72. "Null0 nexthop",
  73. };
  74. extern struct zebra_t zebrad;
  75. extern struct zebra_privs_t zserv_privs;
  76. /* Make socket for Linux netlink interface. */
  77. static int
  78. netlink_socket (struct nlsock *nl, unsigned long groups)
  79. {
  80. int ret;
  81. struct sockaddr_nl snl;
  82. int sock;
  83. int namelen;
  84. sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  85. if (sock < 0)
  86. {
  87. zlog (NULL, LOG_ERR, "Can't open %s socket: %s", nl->name,
  88. strerror (errno));
  89. return -1;
  90. }
  91. ret = fcntl (sock, F_SETFL, O_NONBLOCK);
  92. if (ret < 0)
  93. {
  94. zlog (NULL, LOG_ERR, "Can't set %s socket flags: %s", nl->name,
  95. strerror (errno));
  96. close (sock);
  97. return -1;
  98. }
  99. memset (&snl, 0, sizeof snl);
  100. snl.nl_family = AF_NETLINK;
  101. snl.nl_groups = groups;
  102. /* Bind the socket to the netlink structure for anything. */
  103. if (zserv_privs.change (ZPRIVS_RAISE))
  104. {
  105. zlog (NULL, LOG_ERR, "Can't raise privileges");
  106. return -1;
  107. }
  108. ret = bind (sock, (struct sockaddr *) &snl, sizeof snl);
  109. if (ret < 0)
  110. {
  111. if (zserv_privs.change (ZPRIVS_LOWER))
  112. zlog (NULL, LOG_ERR, "Can't lower privileges");
  113. zlog (NULL, LOG_ERR, "Can't bind %s socket to group 0x%x: %s",
  114. nl->name, snl.nl_groups, strerror (errno));
  115. close (sock);
  116. return -1;
  117. }
  118. if (zserv_privs.change (ZPRIVS_LOWER))
  119. zlog (NULL, LOG_ERR, "Can't lower privileges");
  120. /* multiple netlink sockets will have different nl_pid */
  121. namelen = sizeof snl;
  122. ret = getsockname (sock, (struct sockaddr *) &snl, &namelen);
  123. if (ret < 0 || namelen != sizeof snl)
  124. {
  125. zlog (NULL, LOG_ERR, "Can't get %s socket name: %s", nl->name,
  126. strerror (errno));
  127. close (sock);
  128. return -1;
  129. }
  130. nl->snl = snl;
  131. nl->sock = sock;
  132. return ret;
  133. }
  134. int
  135. set_netlink_blocking (struct nlsock *nl, int *flags)
  136. {
  137. /* Change socket flags for blocking I/O. */
  138. if ((*flags = fcntl (nl->sock, F_GETFL, 0)) < 0)
  139. {
  140. zlog (NULL, LOG_ERR, "%s:%i F_GETFL error: %s",
  141. __FUNCTION__, __LINE__, strerror (errno));
  142. return -1;
  143. }
  144. *flags &= ~O_NONBLOCK;
  145. if (fcntl (nl->sock, F_SETFL, *flags) < 0)
  146. {
  147. zlog (NULL, LOG_ERR, "%s:%i F_SETFL error: %s",
  148. __FUNCTION__, __LINE__, strerror (errno));
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. int
  154. set_netlink_nonblocking (struct nlsock *nl, int *flags)
  155. {
  156. /* Restore socket flags for nonblocking I/O */
  157. *flags |= O_NONBLOCK;
  158. if (fcntl (nl->sock, F_SETFL, *flags) < 0)
  159. {
  160. zlog (NULL, LOG_ERR, "%s:%i F_SETFL error: %s",
  161. __FUNCTION__, __LINE__, strerror (errno));
  162. return -1;
  163. }
  164. return 0;
  165. }
  166. /* Get type specified information from netlink. */
  167. static int
  168. netlink_request (int family, int type, struct nlsock *nl)
  169. {
  170. int ret;
  171. struct sockaddr_nl snl;
  172. struct
  173. {
  174. struct nlmsghdr nlh;
  175. struct rtgenmsg g;
  176. } req;
  177. /* Check netlink socket. */
  178. if (nl->sock < 0)
  179. {
  180. zlog (NULL, LOG_ERR, "%s socket isn't active.", nl->name);
  181. return -1;
  182. }
  183. memset (&snl, 0, sizeof snl);
  184. snl.nl_family = AF_NETLINK;
  185. req.nlh.nlmsg_len = sizeof req;
  186. req.nlh.nlmsg_type = type;
  187. req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
  188. req.nlh.nlmsg_pid = 0;
  189. req.nlh.nlmsg_seq = ++nl->seq;
  190. req.g.rtgen_family = family;
  191. /* linux appears to check capabilities on every message
  192. * have to raise caps for every message sent
  193. */
  194. if (zserv_privs.change (ZPRIVS_RAISE))
  195. {
  196. zlog (NULL, LOG_ERR, "Can't raise privileges");
  197. return -1;
  198. }
  199. ret = sendto (nl->sock, (void *) &req, sizeof req, 0,
  200. (struct sockaddr *) &snl, sizeof snl);
  201. if (zserv_privs.change (ZPRIVS_LOWER))
  202. zlog (NULL, LOG_ERR, "Can't lower privileges");
  203. if (ret < 0)
  204. {
  205. zlog (NULL, LOG_ERR, "%s sendto failed: %s", nl->name,
  206. strerror (errno));
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. /* Receive message from netlink interface and pass those information
  212. to the given function. */
  213. static int
  214. netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *),
  215. struct nlsock *nl)
  216. {
  217. int status;
  218. int ret = 0;
  219. int error;
  220. while (1)
  221. {
  222. char buf[4096];
  223. struct iovec iov = { buf, sizeof buf };
  224. struct sockaddr_nl snl;
  225. struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 };
  226. struct nlmsghdr *h;
  227. if (zserv_privs.change (ZPRIVS_RAISE))
  228. zlog (NULL, LOG_ERR, "Can't raise privileges");
  229. status = recvmsg (nl->sock, &msg, 0);
  230. if (zserv_privs.change (ZPRIVS_LOWER))
  231. zlog (NULL, LOG_ERR, "Can't lower privileges");
  232. if (status < 0)
  233. {
  234. if (errno == EINTR)
  235. continue;
  236. if (errno == EWOULDBLOCK || errno == EAGAIN)
  237. break;
  238. zlog (NULL, LOG_ERR, "%s recvmsg overrun", nl->name);
  239. continue;
  240. }
  241. if (status == 0)
  242. {
  243. zlog (NULL, LOG_ERR, "%s EOF", nl->name);
  244. return -1;
  245. }
  246. if (msg.msg_namelen != sizeof snl)
  247. {
  248. zlog (NULL, LOG_ERR, "%s sender address length error: length %d",
  249. nl->name, msg.msg_namelen);
  250. return -1;
  251. }
  252. /* JF: Ignore messages that aren't from the kernel */
  253. if ( snl.nl_pid != 0 )
  254. {
  255. zlog ( NULL, LOG_ERR, "Ignoring message from pid %u", snl.nl_pid );
  256. continue;
  257. }
  258. for (h = (struct nlmsghdr *) buf; NLMSG_OK (h, status);
  259. h = NLMSG_NEXT (h, status))
  260. {
  261. /* Finish of reading. */
  262. if (h->nlmsg_type == NLMSG_DONE)
  263. return ret;
  264. /* Error handling. */
  265. if (h->nlmsg_type == NLMSG_ERROR)
  266. {
  267. struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h);
  268. /* If the error field is zero, then this is an ACK */
  269. if (err->error == 0)
  270. {
  271. if (IS_ZEBRA_DEBUG_KERNEL)
  272. {
  273. zlog_info ("%s: %s ACK: type=%s(%u), seq=%u, pid=%d",
  274. __FUNCTION__, nl->name,
  275. lookup (nlmsg_str, err->msg.nlmsg_type),
  276. err->msg.nlmsg_type, err->msg.nlmsg_seq,
  277. err->msg.nlmsg_pid);
  278. }
  279. /* return if not a multipart message, otherwise continue */
  280. if (!(h->nlmsg_flags & NLM_F_MULTI))
  281. {
  282. return 0;
  283. }
  284. continue;
  285. }
  286. if (h->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
  287. {
  288. zlog (NULL, LOG_ERR, "%s error: message truncated",
  289. nl->name);
  290. return -1;
  291. }
  292. /* Deal with Error Noise - MAG */
  293. {
  294. int loglvl = LOG_ERR;
  295. int errnum = err->error;
  296. int msg_type = err->msg.nlmsg_type;
  297. if (nl == &netlink_cmd
  298. && (-errnum == ENODEV || -errnum == ESRCH)
  299. && (msg_type == RTM_NEWROUTE || msg_type == RTM_DELROUTE))
  300. loglvl = LOG_DEBUG;
  301. zlog (NULL, loglvl, "%s error: %s, type=%s(%u), "
  302. "seq=%u, pid=%d",
  303. nl->name, strerror (-errnum),
  304. lookup (nlmsg_str, msg_type),
  305. msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid);
  306. }
  307. /*
  308. ret = -1;
  309. continue;
  310. */
  311. return -1;
  312. }
  313. /* OK we got netlink message. */
  314. if (IS_ZEBRA_DEBUG_KERNEL)
  315. zlog_info ("netlink_parse_info: %s type %s(%u), seq=%u, pid=%d",
  316. nl->name,
  317. lookup (nlmsg_str, h->nlmsg_type), h->nlmsg_type,
  318. h->nlmsg_seq, h->nlmsg_pid);
  319. /* skip unsolicited messages originating from command socket */
  320. if (nl != &netlink_cmd && h->nlmsg_pid == netlink_cmd.snl.nl_pid)
  321. {
  322. if (IS_ZEBRA_DEBUG_KERNEL)
  323. zlog_info ("netlink_parse_info: %s packet comes from %s",
  324. nl->name, netlink_cmd.name);
  325. continue;
  326. }
  327. error = (*filter) (&snl, h);
  328. if (error < 0)
  329. {
  330. zlog (NULL, LOG_ERR, "%s filter function error", nl->name);
  331. ret = error;
  332. }
  333. }
  334. /* After error care. */
  335. if (msg.msg_flags & MSG_TRUNC)
  336. {
  337. zlog (NULL, LOG_ERR, "%s error: message truncated", nl->name);
  338. continue;
  339. }
  340. if (status)
  341. {
  342. zlog (NULL, LOG_ERR, "%s error: data remnant size %d", nl->name,
  343. status);
  344. return -1;
  345. }
  346. }
  347. return ret;
  348. }
  349. /* Utility function for parse rtattr. */
  350. static void
  351. netlink_parse_rtattr (struct rtattr **tb, int max, struct rtattr *rta,
  352. int len)
  353. {
  354. while (RTA_OK (rta, len))
  355. {
  356. if (rta->rta_type <= max)
  357. tb[rta->rta_type] = rta;
  358. rta = RTA_NEXT (rta, len);
  359. }
  360. }
  361. /* Called from interface_lookup_netlink(). This function is only used
  362. during bootstrap. */
  363. int
  364. netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h)
  365. {
  366. int len;
  367. struct ifinfomsg *ifi;
  368. struct rtattr *tb[IFLA_MAX + 1];
  369. struct interface *ifp;
  370. char *name;
  371. int i;
  372. ifi = NLMSG_DATA (h);
  373. if (h->nlmsg_type != RTM_NEWLINK)
  374. return 0;
  375. len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
  376. if (len < 0)
  377. return -1;
  378. /* Looking up interface name. */
  379. memset (tb, 0, sizeof tb);
  380. netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
  381. if (tb[IFLA_IFNAME] == NULL)
  382. return -1;
  383. name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
  384. /* Add interface. */
  385. ifp = if_get_by_name (name);
  386. ifp->ifindex = ifi->ifi_index;
  387. ifp->flags = ifi->ifi_flags & 0x0000fffff;
  388. ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
  389. ifp->metric = 1;
  390. /* Hardware type and address. */
  391. ifp->hw_type = ifi->ifi_type;
  392. if (tb[IFLA_ADDRESS])
  393. {
  394. int hw_addr_len;
  395. hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]);
  396. if (hw_addr_len > INTERFACE_HWADDR_MAX)
  397. zlog_warn ("Hardware address is too large: %d", hw_addr_len);
  398. else
  399. {
  400. ifp->hw_addr_len = hw_addr_len;
  401. memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len);
  402. for (i = 0; i < hw_addr_len; i++)
  403. if (ifp->hw_addr[i] != 0)
  404. break;
  405. if (i == hw_addr_len)
  406. ifp->hw_addr_len = 0;
  407. else
  408. ifp->hw_addr_len = hw_addr_len;
  409. }
  410. }
  411. if_add_update (ifp);
  412. return 0;
  413. }
  414. /* Lookup interface IPv4/IPv6 address. */
  415. int
  416. netlink_interface_addr (struct sockaddr_nl *snl, struct nlmsghdr *h)
  417. {
  418. int len;
  419. struct ifaddrmsg *ifa;
  420. struct rtattr *tb[IFA_MAX + 1];
  421. struct interface *ifp;
  422. void *addr = NULL;
  423. void *broad = NULL;
  424. u_char flags = 0;
  425. char *label = NULL;
  426. ifa = NLMSG_DATA (h);
  427. if (ifa->ifa_family != AF_INET
  428. #ifdef HAVE_IPV6
  429. && ifa->ifa_family != AF_INET6
  430. #endif /* HAVE_IPV6 */
  431. )
  432. return 0;
  433. if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
  434. return 0;
  435. len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifaddrmsg));
  436. if (len < 0)
  437. return -1;
  438. memset (tb, 0, sizeof tb);
  439. netlink_parse_rtattr (tb, IFA_MAX, IFA_RTA (ifa), len);
  440. ifp = if_lookup_by_index (ifa->ifa_index);
  441. if (ifp == NULL)
  442. {
  443. zlog_err ("netlink_interface_addr can't find interface by index %d",
  444. ifa->ifa_index);
  445. return -1;
  446. }
  447. if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
  448. {
  449. char buf[BUFSIZ];
  450. zlog_info ("netlink_interface_addr %s %s/%d:",
  451. lookup (nlmsg_str, h->nlmsg_type),
  452. ifp->name, ifa->ifa_prefixlen);
  453. if (tb[IFA_LOCAL])
  454. zlog_info (" IFA_LOCAL %s", inet_ntop (ifa->ifa_family,
  455. RTA_DATA (tb[IFA_LOCAL]),
  456. buf, BUFSIZ));
  457. if (tb[IFA_ADDRESS])
  458. zlog_info (" IFA_ADDRESS %s", inet_ntop (ifa->ifa_family,
  459. RTA_DATA (tb
  460. [IFA_ADDRESS]),
  461. buf, BUFSIZ));
  462. if (tb[IFA_BROADCAST])
  463. zlog_info (" IFA_BROADCAST %s", inet_ntop (ifa->ifa_family,
  464. RTA_DATA (tb
  465. [IFA_BROADCAST]),
  466. buf, BUFSIZ));
  467. if (tb[IFA_LABEL] && strcmp (ifp->name, RTA_DATA (tb[IFA_LABEL])))
  468. zlog_info (" IFA_LABEL %s", (char *)RTA_DATA (tb[IFA_LABEL]));
  469. }
  470. if (tb[IFA_ADDRESS] == NULL)
  471. tb[IFA_ADDRESS] = tb[IFA_LOCAL];
  472. if (ifp->flags & IFF_POINTOPOINT)
  473. {
  474. if (tb[IFA_LOCAL])
  475. {
  476. addr = RTA_DATA (tb[IFA_LOCAL]);
  477. if (tb[IFA_ADDRESS])
  478. broad = RTA_DATA (tb[IFA_ADDRESS]);
  479. else
  480. broad = NULL;
  481. }
  482. else
  483. {
  484. if (tb[IFA_ADDRESS])
  485. addr = RTA_DATA (tb[IFA_ADDRESS]);
  486. else
  487. addr = NULL;
  488. }
  489. }
  490. else
  491. {
  492. if (tb[IFA_ADDRESS])
  493. addr = RTA_DATA (tb[IFA_ADDRESS]);
  494. else
  495. addr = NULL;
  496. if (tb[IFA_BROADCAST])
  497. broad = RTA_DATA(tb[IFA_BROADCAST]);
  498. else
  499. broad = NULL;
  500. }
  501. /* Flags. */
  502. if (ifa->ifa_flags & IFA_F_SECONDARY)
  503. SET_FLAG (flags, ZEBRA_IFA_SECONDARY);
  504. /* Label */
  505. if (tb[IFA_LABEL])
  506. label = (char *) RTA_DATA (tb[IFA_LABEL]);
  507. if (ifp && label && strcmp (ifp->name, label) == 0)
  508. label = NULL;
  509. /* Register interface address to the interface. */
  510. if (ifa->ifa_family == AF_INET)
  511. {
  512. if (h->nlmsg_type == RTM_NEWADDR)
  513. connected_add_ipv4 (ifp, flags,
  514. (struct in_addr *) addr, ifa->ifa_prefixlen,
  515. (struct in_addr *) broad, label);
  516. else
  517. connected_delete_ipv4 (ifp, flags,
  518. (struct in_addr *) addr, ifa->ifa_prefixlen,
  519. (struct in_addr *) broad, label);
  520. }
  521. #ifdef HAVE_IPV6
  522. if (ifa->ifa_family == AF_INET6)
  523. {
  524. if (h->nlmsg_type == RTM_NEWADDR)
  525. connected_add_ipv6 (ifp,
  526. (struct in6_addr *) addr, ifa->ifa_prefixlen,
  527. (struct in6_addr *) broad);
  528. else
  529. connected_delete_ipv6 (ifp,
  530. (struct in6_addr *) addr, ifa->ifa_prefixlen,
  531. (struct in6_addr *) broad);
  532. }
  533. #endif /* HAVE_IPV6 */
  534. return 0;
  535. }
  536. /* Looking up routing table by netlink interface. */
  537. int
  538. netlink_routing_table (struct sockaddr_nl *snl, struct nlmsghdr *h)
  539. {
  540. int len;
  541. struct rtmsg *rtm;
  542. struct rtattr *tb[RTA_MAX + 1];
  543. u_char flags = 0;
  544. char anyaddr[16] = { 0 };
  545. int index;
  546. int table;
  547. int metric;
  548. void *dest;
  549. void *gate;
  550. rtm = NLMSG_DATA (h);
  551. if (h->nlmsg_type != RTM_NEWROUTE)
  552. return 0;
  553. if (rtm->rtm_type != RTN_UNICAST)
  554. return 0;
  555. table = rtm->rtm_table;
  556. #if 0 /* we weed them out later in rib_weed_tables () */
  557. if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
  558. return 0;
  559. #endif
  560. len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
  561. if (len < 0)
  562. return -1;
  563. memset (tb, 0, sizeof tb);
  564. netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
  565. if (rtm->rtm_flags & RTM_F_CLONED)
  566. return 0;
  567. if (rtm->rtm_protocol == RTPROT_REDIRECT)
  568. return 0;
  569. if (rtm->rtm_protocol == RTPROT_KERNEL)
  570. return 0;
  571. if (rtm->rtm_src_len != 0)
  572. return 0;
  573. /* Route which inserted by Zebra. */
  574. if (rtm->rtm_protocol == RTPROT_ZEBRA)
  575. flags |= ZEBRA_FLAG_SELFROUTE;
  576. index = 0;
  577. metric = 0;
  578. dest = NULL;
  579. gate = NULL;
  580. if (tb[RTA_OIF])
  581. index = *(int *) RTA_DATA (tb[RTA_OIF]);
  582. if (tb[RTA_DST])
  583. dest = RTA_DATA (tb[RTA_DST]);
  584. else
  585. dest = anyaddr;
  586. /* Multipath treatment is needed. */
  587. if (tb[RTA_GATEWAY])
  588. gate = RTA_DATA (tb[RTA_GATEWAY]);
  589. if (tb[RTA_PRIORITY])
  590. metric = *(int *) RTA_DATA(tb[RTA_PRIORITY]);
  591. if (rtm->rtm_family == AF_INET)
  592. {
  593. struct prefix_ipv4 p;
  594. p.family = AF_INET;
  595. memcpy (&p.prefix, dest, 4);
  596. p.prefixlen = rtm->rtm_dst_len;
  597. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table, metric, 0);
  598. }
  599. #ifdef HAVE_IPV6
  600. if (rtm->rtm_family == AF_INET6)
  601. {
  602. struct prefix_ipv6 p;
  603. p.family = AF_INET6;
  604. memcpy (&p.prefix, dest, 16);
  605. p.prefixlen = rtm->rtm_dst_len;
  606. rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table);
  607. }
  608. #endif /* HAVE_IPV6 */
  609. return 0;
  610. }
  611. struct message rtproto_str[] = {
  612. {RTPROT_REDIRECT, "redirect"},
  613. {RTPROT_KERNEL, "kernel"},
  614. {RTPROT_BOOT, "boot"},
  615. {RTPROT_STATIC, "static"},
  616. {RTPROT_GATED, "GateD"},
  617. {RTPROT_RA, "router advertisement"},
  618. {RTPROT_MRT, "MRT"},
  619. {RTPROT_ZEBRA, "Zebra"},
  620. #ifdef RTPROT_BIRD
  621. {RTPROT_BIRD, "BIRD"},
  622. #endif /* RTPROT_BIRD */
  623. {0, NULL}
  624. };
  625. /* Routing information change from the kernel. */
  626. int
  627. netlink_route_change (struct sockaddr_nl *snl, struct nlmsghdr *h)
  628. {
  629. int len;
  630. struct rtmsg *rtm;
  631. struct rtattr *tb[RTA_MAX + 1];
  632. char anyaddr[16] = { 0 };
  633. int index;
  634. int table;
  635. void *dest;
  636. void *gate;
  637. rtm = NLMSG_DATA (h);
  638. if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE))
  639. {
  640. /* If this is not route add/delete message print warning. */
  641. zlog_warn ("Kernel message: %d\n", h->nlmsg_type);
  642. return 0;
  643. }
  644. /* Connected route. */
  645. if (IS_ZEBRA_DEBUG_KERNEL)
  646. zlog_info ("%s %s %s proto %s",
  647. h->nlmsg_type ==
  648. RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
  649. rtm->rtm_family == AF_INET ? "ipv4" : "ipv6",
  650. rtm->rtm_type == RTN_UNICAST ? "unicast" : "multicast",
  651. lookup (rtproto_str, rtm->rtm_protocol));
  652. if (rtm->rtm_type != RTN_UNICAST)
  653. {
  654. return 0;
  655. }
  656. table = rtm->rtm_table;
  657. if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
  658. {
  659. return 0;
  660. }
  661. len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
  662. if (len < 0)
  663. return -1;
  664. memset (tb, 0, sizeof tb);
  665. netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
  666. if (rtm->rtm_flags & RTM_F_CLONED)
  667. return 0;
  668. if (rtm->rtm_protocol == RTPROT_REDIRECT)
  669. return 0;
  670. if (rtm->rtm_protocol == RTPROT_KERNEL)
  671. return 0;
  672. if (rtm->rtm_protocol == RTPROT_ZEBRA && h->nlmsg_type == RTM_NEWROUTE)
  673. return 0;
  674. if (rtm->rtm_src_len != 0)
  675. {
  676. zlog_warn ("netlink_route_change(): no src len");
  677. return 0;
  678. }
  679. index = 0;
  680. dest = NULL;
  681. gate = NULL;
  682. if (tb[RTA_OIF])
  683. index = *(int *) RTA_DATA (tb[RTA_OIF]);
  684. if (tb[RTA_DST])
  685. dest = RTA_DATA (tb[RTA_DST]);
  686. else
  687. dest = anyaddr;
  688. if (tb[RTA_GATEWAY])
  689. gate = RTA_DATA (tb[RTA_GATEWAY]);
  690. if (rtm->rtm_family == AF_INET)
  691. {
  692. struct prefix_ipv4 p;
  693. p.family = AF_INET;
  694. memcpy (&p.prefix, dest, 4);
  695. p.prefixlen = rtm->rtm_dst_len;
  696. if (IS_ZEBRA_DEBUG_KERNEL)
  697. {
  698. if (h->nlmsg_type == RTM_NEWROUTE)
  699. zlog_info ("RTM_NEWROUTE %s/%d",
  700. inet_ntoa (p.prefix), p.prefixlen);
  701. else
  702. zlog_info ("RTM_DELROUTE %s/%d",
  703. inet_ntoa (p.prefix), p.prefixlen);
  704. }
  705. if (h->nlmsg_type == RTM_NEWROUTE)
  706. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table, 0, 0);
  707. else
  708. rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table);
  709. }
  710. #ifdef HAVE_IPV6
  711. if (rtm->rtm_family == AF_INET6)
  712. {
  713. struct prefix_ipv6 p;
  714. char buf[BUFSIZ];
  715. p.family = AF_INET6;
  716. memcpy (&p.prefix, dest, 16);
  717. p.prefixlen = rtm->rtm_dst_len;
  718. if (IS_ZEBRA_DEBUG_KERNEL)
  719. {
  720. if (h->nlmsg_type == RTM_NEWROUTE)
  721. zlog_info ("RTM_NEWROUTE %s/%d",
  722. inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ),
  723. p.prefixlen);
  724. else
  725. zlog_info ("RTM_DELROUTE %s/%d",
  726. inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ),
  727. p.prefixlen);
  728. }
  729. if (h->nlmsg_type == RTM_NEWROUTE)
  730. rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, 0);
  731. else
  732. rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, 0);
  733. }
  734. #endif /* HAVE_IPV6 */
  735. return 0;
  736. }
  737. int
  738. netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h)
  739. {
  740. int len;
  741. struct ifinfomsg *ifi;
  742. struct rtattr *tb[IFLA_MAX + 1];
  743. struct interface *ifp;
  744. char *name;
  745. ifi = NLMSG_DATA (h);
  746. if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK))
  747. {
  748. /* If this is not link add/delete message so print warning. */
  749. zlog_warn ("netlink_link_change: wrong kernel message %d\n",
  750. h->nlmsg_type);
  751. return 0;
  752. }
  753. len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
  754. if (len < 0)
  755. return -1;
  756. /* Looking up interface name. */
  757. memset (tb, 0, sizeof tb);
  758. netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
  759. if (tb[IFLA_IFNAME] == NULL)
  760. return -1;
  761. name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
  762. /* Add interface. */
  763. if (h->nlmsg_type == RTM_NEWLINK)
  764. {
  765. ifp = if_lookup_by_name (name);
  766. if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  767. {
  768. if (ifp == NULL)
  769. ifp = if_get_by_name (name);
  770. ifp->ifindex = ifi->ifi_index;
  771. ifp->flags = ifi->ifi_flags & 0x0000fffff;
  772. ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
  773. ifp->metric = 1;
  774. /* If new link is added. */
  775. if_add_update (ifp);
  776. }
  777. else
  778. {
  779. /* Interface status change. */
  780. ifp->ifindex = ifi->ifi_index;
  781. ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
  782. ifp->metric = 1;
  783. if (if_is_operative (ifp))
  784. {
  785. ifp->flags = ifi->ifi_flags & 0x0000fffff;
  786. if (!if_is_operative (ifp))
  787. if_down (ifp);
  788. }
  789. else
  790. {
  791. ifp->flags = ifi->ifi_flags & 0x0000fffff;
  792. if (if_is_operative (ifp))
  793. if_up (ifp);
  794. }
  795. }
  796. }
  797. else
  798. {
  799. /* RTM_DELLINK. */
  800. ifp = if_lookup_by_name (name);
  801. if (ifp == NULL)
  802. {
  803. zlog (NULL, LOG_WARNING, "interface %s is deleted but can't find",
  804. name);
  805. return 0;
  806. }
  807. if_delete_update (ifp);
  808. }
  809. return 0;
  810. }
  811. int
  812. netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h)
  813. {
  814. switch (h->nlmsg_type)
  815. {
  816. case RTM_NEWROUTE:
  817. return netlink_route_change (snl, h);
  818. break;
  819. case RTM_DELROUTE:
  820. return netlink_route_change (snl, h);
  821. break;
  822. case RTM_NEWLINK:
  823. return netlink_link_change (snl, h);
  824. break;
  825. case RTM_DELLINK:
  826. return netlink_link_change (snl, h);
  827. break;
  828. case RTM_NEWADDR:
  829. return netlink_interface_addr (snl, h);
  830. break;
  831. case RTM_DELADDR:
  832. return netlink_interface_addr (snl, h);
  833. break;
  834. default:
  835. zlog_warn ("Unknown netlink nlmsg_type %d\n", h->nlmsg_type);
  836. break;
  837. }
  838. return 0;
  839. }
  840. /* Interface lookup by netlink socket. */
  841. int
  842. interface_lookup_netlink ()
  843. {
  844. int ret;
  845. int flags;
  846. int snb_ret;
  847. /*
  848. * Change netlink socket flags to blocking to ensure we get
  849. * a reply via nelink_parse_info
  850. */
  851. snb_ret = set_netlink_blocking (&netlink_cmd, &flags);
  852. if (snb_ret < 0)
  853. zlog (NULL, LOG_WARNING,
  854. "%s:%i Warning: Could not set netlink socket to blocking.",
  855. __FUNCTION__, __LINE__);
  856. /* Get interface information. */
  857. ret = netlink_request (AF_PACKET, RTM_GETLINK, &netlink_cmd);
  858. if (ret < 0)
  859. return ret;
  860. ret = netlink_parse_info (netlink_interface, &netlink_cmd);
  861. if (ret < 0)
  862. return ret;
  863. /* Get IPv4 address of the interfaces. */
  864. ret = netlink_request (AF_INET, RTM_GETADDR, &netlink_cmd);
  865. if (ret < 0)
  866. return ret;
  867. ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd);
  868. if (ret < 0)
  869. return ret;
  870. #ifdef HAVE_IPV6
  871. /* Get IPv6 address of the interfaces. */
  872. ret = netlink_request (AF_INET6, RTM_GETADDR, &netlink_cmd);
  873. if (ret < 0)
  874. return ret;
  875. ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd);
  876. if (ret < 0)
  877. return ret;
  878. #endif /* HAVE_IPV6 */
  879. /* restore socket flags */
  880. if (snb_ret == 0)
  881. set_netlink_nonblocking (&netlink_cmd, &flags);
  882. return 0;
  883. }
  884. /* Routing table read function using netlink interface. Only called
  885. bootstrap time. */
  886. int
  887. netlink_route_read ()
  888. {
  889. int ret;
  890. int flags;
  891. int snb_ret;
  892. /*
  893. * Change netlink socket flags to blocking to ensure we get
  894. * a reply via nelink_parse_info
  895. */
  896. snb_ret = set_netlink_blocking (&netlink_cmd, &flags);
  897. if (snb_ret < 0)
  898. zlog (NULL, LOG_WARNING,
  899. "%s:%i Warning: Could not set netlink socket to blocking.",
  900. __FUNCTION__, __LINE__);
  901. /* Get IPv4 routing table. */
  902. ret = netlink_request (AF_INET, RTM_GETROUTE, &netlink_cmd);
  903. if (ret < 0)
  904. return ret;
  905. ret = netlink_parse_info (netlink_routing_table, &netlink_cmd);
  906. if (ret < 0)
  907. return ret;
  908. #ifdef HAVE_IPV6
  909. /* Get IPv6 routing table. */
  910. ret = netlink_request (AF_INET6, RTM_GETROUTE, &netlink_cmd);
  911. if (ret < 0)
  912. return ret;
  913. ret = netlink_parse_info (netlink_routing_table, &netlink_cmd);
  914. if (ret < 0)
  915. return ret;
  916. #endif /* HAVE_IPV6 */
  917. /* restore flags */
  918. if (snb_ret == 0)
  919. set_netlink_nonblocking (&netlink_cmd, &flags);
  920. return 0;
  921. }
  922. /* Utility function comes from iproute2.
  923. Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
  924. int
  925. addattr_l (struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
  926. {
  927. int len;
  928. struct rtattr *rta;
  929. len = RTA_LENGTH (alen);
  930. if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
  931. return -1;
  932. rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
  933. rta->rta_type = type;
  934. rta->rta_len = len;
  935. memcpy (RTA_DATA (rta), data, alen);
  936. n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
  937. return 0;
  938. }
  939. int
  940. rta_addattr_l (struct rtattr *rta, int maxlen, int type, void *data, int alen)
  941. {
  942. int len;
  943. struct rtattr *subrta;
  944. len = RTA_LENGTH (alen);
  945. if (RTA_ALIGN (rta->rta_len) + len > maxlen)
  946. return -1;
  947. subrta = (struct rtattr *) (((char *) rta) + RTA_ALIGN (rta->rta_len));
  948. subrta->rta_type = type;
  949. subrta->rta_len = len;
  950. memcpy (RTA_DATA (subrta), data, alen);
  951. rta->rta_len = NLMSG_ALIGN (rta->rta_len) + len;
  952. return 0;
  953. }
  954. /* Utility function comes from iproute2.
  955. Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
  956. int
  957. addattr32 (struct nlmsghdr *n, int maxlen, int type, int data)
  958. {
  959. int len;
  960. struct rtattr *rta;
  961. len = RTA_LENGTH (4);
  962. if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
  963. return -1;
  964. rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
  965. rta->rta_type = type;
  966. rta->rta_len = len;
  967. memcpy (RTA_DATA (rta), &data, 4);
  968. n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
  969. return 0;
  970. }
  971. static int
  972. netlink_talk_filter (struct sockaddr_nl *snl, struct nlmsghdr *h)
  973. {
  974. zlog_warn ("netlink_talk: ignoring message type 0x%04x", h->nlmsg_type);
  975. return 0;
  976. }
  977. /* sendmsg() to netlink socket then recvmsg(). */
  978. int
  979. netlink_talk (struct nlmsghdr *n, struct nlsock *nl)
  980. {
  981. int status;
  982. struct sockaddr_nl snl;
  983. struct iovec iov = { (void *) n, n->nlmsg_len };
  984. struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 };
  985. int flags = 0;
  986. int snb_ret;
  987. memset (&snl, 0, sizeof snl);
  988. snl.nl_family = AF_NETLINK;
  989. n->nlmsg_seq = ++netlink_cmd.seq;
  990. /* Request an acknowledgement by setting NLM_F_ACK */
  991. n->nlmsg_flags |= NLM_F_ACK;
  992. if (IS_ZEBRA_DEBUG_KERNEL)
  993. zlog_info ("netlink_talk: %s type %s(%u), seq=%u", netlink_cmd.name,
  994. lookup (nlmsg_str, n->nlmsg_type), n->nlmsg_type,
  995. n->nlmsg_seq);
  996. /* Send message to netlink interface. */
  997. if (zserv_privs.change (ZPRIVS_RAISE))
  998. zlog (NULL, LOG_ERR, "Can't raise privileges");
  999. status = sendmsg (nl->sock, &msg, 0);
  1000. if (zserv_privs.change (ZPRIVS_LOWER))
  1001. zlog (NULL, LOG_ERR, "Can't lower privileges");
  1002. if (status < 0)
  1003. {
  1004. zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s",
  1005. strerror (errno));
  1006. return -1;
  1007. }
  1008. /*
  1009. * Change socket flags for blocking I/O.
  1010. * This ensures we wait for a reply in netlink_parse_info().
  1011. */
  1012. snb_ret = set_netlink_blocking (nl, &flags);
  1013. if (snb_ret < 0)
  1014. zlog (NULL, LOG_WARNING,
  1015. "%s:%i Warning: Could not set netlink socket to blocking.",
  1016. __FUNCTION__, __LINE__);
  1017. /*
  1018. * Get reply from netlink socket.
  1019. * The reply should either be an acknowlegement or an error.
  1020. */
  1021. status = netlink_parse_info (netlink_talk_filter, nl);
  1022. /* Restore socket flags for nonblocking I/O */
  1023. if (snb_ret == 0)
  1024. set_netlink_nonblocking (nl, &flags);
  1025. return status;
  1026. }
  1027. /* Routing table change via netlink interface. */
  1028. int
  1029. netlink_route (int cmd, int family, void *dest, int length, void *gate,
  1030. int index, int zebra_flags, int table)
  1031. {
  1032. int ret;
  1033. int bytelen;
  1034. struct sockaddr_nl snl;
  1035. int discard;
  1036. struct
  1037. {
  1038. struct nlmsghdr n;
  1039. struct rtmsg r;
  1040. char buf[1024];
  1041. } req;
  1042. memset (&req, 0, sizeof req);
  1043. bytelen = (family == AF_INET ? 4 : 16);
  1044. req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
  1045. req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
  1046. req.n.nlmsg_type = cmd;
  1047. req.r.rtm_family = family;
  1048. req.r.rtm_table = table;
  1049. req.r.rtm_dst_len = length;
  1050. if ((zebra_flags & ZEBRA_FLAG_BLACKHOLE)
  1051. || (zebra_flags & ZEBRA_FLAG_REJECT))
  1052. discard = 1;
  1053. else
  1054. discard = 0;
  1055. if (cmd == RTM_NEWROUTE)
  1056. {
  1057. req.r.rtm_protocol = RTPROT_ZEBRA;
  1058. req.r.rtm_scope = RT_SCOPE_UNIVERSE;
  1059. if (discard)
  1060. {
  1061. if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
  1062. req.r.rtm_type = RTN_BLACKHOLE;
  1063. else if (zebra_flags & ZEBRA_FLAG_REJECT)
  1064. req.r.rtm_type = RTN_UNREACHABLE;
  1065. else
  1066. assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
  1067. }
  1068. else
  1069. req.r.rtm_type = RTN_UNICAST;
  1070. }
  1071. if (dest)
  1072. addattr_l (&req.n, sizeof req, RTA_DST, dest, bytelen);
  1073. if (!discard)
  1074. {
  1075. if (gate)
  1076. addattr_l (&req.n, sizeof req, RTA_GATEWAY, gate, bytelen);
  1077. if (index > 0)
  1078. addattr32 (&req.n, sizeof req, RTA_OIF, index);
  1079. }
  1080. /* Destination netlink address. */
  1081. memset (&snl, 0, sizeof snl);
  1082. snl.nl_family = AF_NETLINK;
  1083. /* Talk to netlink socket. */
  1084. ret = netlink_talk (&req.n, &netlink);
  1085. if (ret < 0)
  1086. return -1;
  1087. return 0;
  1088. }
  1089. /* Routing table change via netlink interface. */
  1090. int
  1091. netlink_route_multipath (int cmd, struct prefix *p, struct rib *rib,
  1092. int family)
  1093. {
  1094. int bytelen;
  1095. struct sockaddr_nl snl;
  1096. struct nexthop *nexthop = NULL;
  1097. int nexthop_num = 0;
  1098. struct nlsock *nl;
  1099. int discard;
  1100. struct
  1101. {
  1102. struct nlmsghdr n;
  1103. struct rtmsg r;
  1104. char buf[1024];
  1105. } req;
  1106. memset (&req, 0, sizeof req);
  1107. bytelen = (family == AF_INET ? 4 : 16);
  1108. req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
  1109. req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
  1110. req.n.nlmsg_type = cmd;
  1111. req.r.rtm_family = family;
  1112. req.r.rtm_table = rib->table;
  1113. req.r.rtm_dst_len = p->prefixlen;
  1114. #ifdef RTM_F_EQUALIZE
  1115. req.r.rtm_flags |= RTM_F_EQUALIZE;
  1116. #endif /* RTM_F_EQUALIZE */
  1117. if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
  1118. discard = 1;
  1119. else
  1120. discard = 0;
  1121. if (cmd == RTM_NEWROUTE)
  1122. {
  1123. req.r.rtm_protocol = RTPROT_ZEBRA;
  1124. req.r.rtm_scope = RT_SCOPE_UNIVERSE;
  1125. if (discard)
  1126. {
  1127. if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
  1128. req.r.rtm_type = RTN_BLACKHOLE;
  1129. else if (rib->flags & ZEBRA_FLAG_REJECT)
  1130. req.r.rtm_type = RTN_UNREACHABLE;
  1131. else
  1132. assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
  1133. }
  1134. else
  1135. req.r.rtm_type = RTN_UNICAST;
  1136. }
  1137. addattr_l (&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen);
  1138. /* Metric. */
  1139. addattr32 (&req.n, sizeof req, RTA_PRIORITY, rib->metric);
  1140. if (discard)
  1141. {
  1142. if (cmd == RTM_NEWROUTE)
  1143. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  1144. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1145. goto skip;
  1146. }
  1147. /* Multipath case. */
  1148. if (rib->nexthop_active_num == 1 || MULTIPATH_NUM == 1)
  1149. {
  1150. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  1151. {
  1152. if ((cmd == RTM_NEWROUTE
  1153. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  1154. || (cmd == RTM_DELROUTE
  1155. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
  1156. {
  1157. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  1158. {
  1159. if (IS_ZEBRA_DEBUG_KERNEL)
  1160. {
  1161. zlog_info
  1162. ("netlink_route_multipath() (recursive, 1 hop): "
  1163. "%s %s/%d via %s if %u, type %s",
  1164. lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4),
  1165. p->prefixlen, inet_ntoa (nexthop->rgate.ipv4),
  1166. nexthop->rifindex,
  1167. nexthop_types_desc[nexthop->rtype]);
  1168. }
  1169. if (nexthop->rtype == NEXTHOP_TYPE_IPV4
  1170. || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
  1171. addattr_l (&req.n, sizeof req, RTA_GATEWAY,
  1172. &nexthop->rgate.ipv4, bytelen);
  1173. #ifdef HAVE_IPV6
  1174. if (nexthop->rtype == NEXTHOP_TYPE_IPV6
  1175. || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
  1176. || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
  1177. addattr_l (&req.n, sizeof req, RTA_GATEWAY,
  1178. &nexthop->rgate.ipv6, bytelen);
  1179. #endif /* HAVE_IPV6 */
  1180. if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
  1181. || nexthop->rtype == NEXTHOP_TYPE_IFNAME
  1182. || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
  1183. || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
  1184. || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
  1185. addattr32 (&req.n, sizeof req, RTA_OIF,
  1186. nexthop->rifindex);
  1187. }
  1188. else
  1189. {
  1190. if (IS_ZEBRA_DEBUG_KERNEL)
  1191. {
  1192. zlog_info
  1193. ("netlink_route_multipath(): (single hop)"
  1194. "%s %s/%d via %s if %u, type %s",
  1195. lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4),
  1196. p->prefixlen, inet_ntoa (nexthop->gate.ipv4),
  1197. nexthop->ifindex,
  1198. nexthop_types_desc[nexthop->type]);
  1199. }
  1200. if (nexthop->type == NEXTHOP_TYPE_IPV4
  1201. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  1202. addattr_l (&req.n, sizeof req, RTA_GATEWAY,
  1203. &nexthop->gate.ipv4, bytelen);
  1204. #ifdef HAVE_IPV6
  1205. if (nexthop->type == NEXTHOP_TYPE_IPV6
  1206. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  1207. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  1208. addattr_l (&req.n, sizeof req, RTA_GATEWAY,
  1209. &nexthop->gate.ipv6, bytelen);
  1210. #endif /* HAVE_IPV6 */
  1211. if (nexthop->type == NEXTHOP_TYPE_IFINDEX
  1212. || nexthop->type == NEXTHOP_TYPE_IFNAME
  1213. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
  1214. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
  1215. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)
  1216. addattr32 (&req.n, sizeof req, RTA_OIF, nexthop->ifindex);
  1217. }
  1218. if (cmd == RTM_NEWROUTE)
  1219. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1220. nexthop_num++;
  1221. break;
  1222. }
  1223. }
  1224. }
  1225. else
  1226. {
  1227. char buf[1024];
  1228. struct rtattr *rta = (void *) buf;
  1229. struct rtnexthop *rtnh;
  1230. rta->rta_type = RTA_MULTIPATH;
  1231. rta->rta_len = RTA_LENGTH (0);
  1232. rtnh = RTA_DATA (rta);
  1233. nexthop_num = 0;
  1234. for (nexthop = rib->nexthop;
  1235. nexthop && (MULTIPATH_NUM == 0 || nexthop_num < MULTIPATH_NUM);
  1236. nexthop = nexthop->next)
  1237. {
  1238. if ((cmd == RTM_NEWROUTE
  1239. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  1240. || (cmd == RTM_DELROUTE
  1241. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
  1242. {
  1243. nexthop_num++;
  1244. rtnh->rtnh_len = sizeof (*rtnh);
  1245. rtnh->rtnh_flags = 0;
  1246. rtnh->rtnh_hops = 0;
  1247. rta->rta_len += rtnh->rtnh_len;
  1248. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  1249. {
  1250. if (IS_ZEBRA_DEBUG_KERNEL)
  1251. {
  1252. zlog_info ("netlink_route_multipath() "
  1253. "(recursive, multihop): "
  1254. "%s %s/%d via %s if %u, type %s",
  1255. lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4),
  1256. p->prefixlen, inet_ntoa (nexthop->rgate.ipv4),
  1257. nexthop->rifindex,
  1258. nexthop_types_desc[nexthop->type]);
  1259. }
  1260. if (nexthop->rtype == NEXTHOP_TYPE_IPV4
  1261. || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
  1262. {
  1263. rta_addattr_l (rta, 4096, RTA_GATEWAY,
  1264. &nexthop->rgate.ipv4, bytelen);
  1265. rtnh->rtnh_len += sizeof (struct rtattr) + 4;
  1266. }
  1267. #ifdef HAVE_IPV6
  1268. if (nexthop->rtype == NEXTHOP_TYPE_IPV6
  1269. || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
  1270. || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
  1271. rta_addattr_l (rta, 4096, RTA_GATEWAY,
  1272. &nexthop->rgate.ipv6, bytelen);
  1273. #endif /* HAVE_IPV6 */
  1274. /* ifindex */
  1275. if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
  1276. || nexthop->rtype == NEXTHOP_TYPE_IFNAME
  1277. || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
  1278. || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
  1279. || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
  1280. rtnh->rtnh_ifindex = nexthop->rifindex;
  1281. else
  1282. rtnh->rtnh_ifindex = 0;
  1283. }
  1284. else
  1285. {
  1286. if (IS_ZEBRA_DEBUG_KERNEL)
  1287. {
  1288. zlog_info ("netlink_route_multipath() "
  1289. "(multihop): "
  1290. "%s %s/%d via %s if %u, type %s",
  1291. lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4),
  1292. p->prefixlen, inet_ntoa (nexthop->rgate.ipv4),
  1293. nexthop->rifindex,
  1294. nexthop_types_desc[nexthop->type]);
  1295. }
  1296. if (nexthop->type == NEXTHOP_TYPE_IPV4
  1297. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  1298. {
  1299. rta_addattr_l (rta, 4096, RTA_GATEWAY,
  1300. &nexthop->gate.ipv4, bytelen);
  1301. rtnh->rtnh_len += sizeof (struct rtattr) + 4;
  1302. }
  1303. #ifdef HAVE_IPV6
  1304. if (nexthop->type == NEXTHOP_TYPE_IPV6
  1305. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  1306. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  1307. rta_addattr_l (rta, 4096, RTA_GATEWAY,
  1308. &nexthop->gate.ipv6, bytelen);
  1309. #endif /* HAVE_IPV6 */
  1310. /* ifindex */
  1311. if (nexthop->type == NEXTHOP_TYPE_IFINDEX
  1312. || nexthop->type == NEXTHOP_TYPE_IFNAME
  1313. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
  1314. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  1315. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  1316. rtnh->rtnh_ifindex = nexthop->ifindex;
  1317. else
  1318. rtnh->rtnh_ifindex = 0;
  1319. }
  1320. rtnh = RTNH_NEXT (rtnh);
  1321. if (cmd == RTM_NEWROUTE)
  1322. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1323. }
  1324. }
  1325. if (rta->rta_len > RTA_LENGTH (0))
  1326. addattr_l (&req.n, 1024, RTA_MULTIPATH, RTA_DATA (rta),
  1327. RTA_PAYLOAD (rta));
  1328. }
  1329. /* If there is no useful nexthop then return. */
  1330. if (nexthop_num == 0)
  1331. {
  1332. if (IS_ZEBRA_DEBUG_KERNEL)
  1333. zlog_info ("netlink_route_multipath(): No useful nexthop.");
  1334. return 0;
  1335. }
  1336. skip:
  1337. /* Destination netlink address. */
  1338. memset (&snl, 0, sizeof snl);
  1339. snl.nl_family = AF_NETLINK;
  1340. if (family == AF_INET)
  1341. nl = &netlink_cmd;
  1342. else
  1343. nl = &netlink;
  1344. /* Talk to netlink socket. */
  1345. return netlink_talk (&req.n, nl);
  1346. }
  1347. int
  1348. kernel_add_ipv4 (struct prefix *p, struct rib *rib)
  1349. {
  1350. return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET);
  1351. }
  1352. int
  1353. kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
  1354. {
  1355. return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET);
  1356. }
  1357. #ifdef HAVE_IPV6
  1358. int
  1359. kernel_add_ipv6 (struct prefix *p, struct rib *rib)
  1360. {
  1361. return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET6);
  1362. }
  1363. int
  1364. kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
  1365. {
  1366. return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET6);
  1367. }
  1368. /* Delete IPv6 route from the kernel. */
  1369. int
  1370. kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate,
  1371. int index, int flags, int table)
  1372. {
  1373. return netlink_route (RTM_DELROUTE, AF_INET6, &dest->prefix,
  1374. dest->prefixlen, gate, index, flags, table);
  1375. }
  1376. #endif /* HAVE_IPV6 */
  1377. /* Interface address modification. */
  1378. int
  1379. netlink_address (int cmd, int family, struct interface *ifp,
  1380. struct connected *ifc)
  1381. {
  1382. int bytelen;
  1383. struct prefix *p;
  1384. struct
  1385. {
  1386. struct nlmsghdr n;
  1387. struct ifaddrmsg ifa;
  1388. char buf[1024];
  1389. } req;
  1390. p = ifc->address;
  1391. memset (&req, 0, sizeof req);
  1392. bytelen = (family == AF_INET ? 4 : 16);
  1393. req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg));
  1394. req.n.nlmsg_flags = NLM_F_REQUEST;
  1395. req.n.nlmsg_type = cmd;
  1396. req.ifa.ifa_family = family;
  1397. req.ifa.ifa_index = ifp->ifindex;
  1398. req.ifa.ifa_prefixlen = p->prefixlen;
  1399. addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
  1400. if (family == AF_INET && cmd == RTM_NEWADDR)
  1401. {
  1402. if (if_is_broadcast (ifp) && ifc->destination)
  1403. {
  1404. p = ifc->destination;
  1405. addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix,
  1406. bytelen);
  1407. }
  1408. }
  1409. if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
  1410. SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY);
  1411. if (ifc->label)
  1412. addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label,
  1413. strlen (ifc->label) + 1);
  1414. return netlink_talk (&req.n, &netlink_cmd);
  1415. }
  1416. int
  1417. kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc)
  1418. {
  1419. return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc);
  1420. }
  1421. int
  1422. kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc)
  1423. {
  1424. return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc);
  1425. }
  1426. extern struct thread_master *master;
  1427. /* Kernel route reflection. */
  1428. int
  1429. kernel_read (struct thread *thread)
  1430. {
  1431. int ret;
  1432. int sock;
  1433. sock = THREAD_FD (thread);
  1434. ret = netlink_parse_info (netlink_information_fetch, &netlink);
  1435. thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
  1436. return 0;
  1437. }
  1438. /* Exported interface function. This function simply calls
  1439. netlink_socket (). */
  1440. void
  1441. kernel_init ()
  1442. {
  1443. unsigned long groups;
  1444. groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR;
  1445. #ifdef HAVE_IPV6
  1446. groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR;
  1447. #endif /* HAVE_IPV6 */
  1448. netlink_socket (&netlink, groups);
  1449. netlink_socket (&netlink_cmd, 0);
  1450. /* Register kernel socket. */
  1451. if (netlink.sock > 0)
  1452. thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
  1453. }