rt_netlink.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  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 "memory.h"
  33. #include "rib.h"
  34. #include "thread.h"
  35. #include "privs.h"
  36. #include "vrf.h"
  37. #include "zebra/zserv.h"
  38. #include "zebra/rt.h"
  39. #include "zebra/redistribute.h"
  40. #include "zebra/interface.h"
  41. #include "zebra/debug.h"
  42. #include "rt_netlink.h"
  43. static const struct message nlmsg_str[] = {
  44. {RTM_NEWROUTE, "RTM_NEWROUTE"},
  45. {RTM_DELROUTE, "RTM_DELROUTE"},
  46. {RTM_GETROUTE, "RTM_GETROUTE"},
  47. {RTM_NEWLINK, "RTM_NEWLINK"},
  48. {RTM_DELLINK, "RTM_DELLINK"},
  49. {RTM_GETLINK, "RTM_GETLINK"},
  50. {RTM_NEWADDR, "RTM_NEWADDR"},
  51. {RTM_DELADDR, "RTM_DELADDR"},
  52. {RTM_GETADDR, "RTM_GETADDR"},
  53. {0, NULL}
  54. };
  55. extern struct zebra_t zebrad;
  56. extern struct zebra_privs_t zserv_privs;
  57. extern u_int32_t nl_rcvbufsize;
  58. /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
  59. names and ifindex values. */
  60. static void
  61. set_ifindex(struct interface *ifp, unsigned int ifi_index)
  62. {
  63. struct interface *oifp;
  64. if (((oifp = if_lookup_by_index(ifi_index)) != NULL) && (oifp != ifp))
  65. {
  66. if (ifi_index == IFINDEX_INTERNAL)
  67. zlog_err("Netlink is setting interface %s ifindex to reserved "
  68. "internal value %u", ifp->name, ifi_index);
  69. else
  70. {
  71. if (IS_ZEBRA_DEBUG_KERNEL)
  72. zlog_debug("interface index %d was renamed from %s to %s",
  73. ifi_index, oifp->name, ifp->name);
  74. if (if_is_up(oifp))
  75. zlog_err("interface rename detected on up interface: index %d "
  76. "was renamed from %s to %s, results are uncertain!",
  77. ifi_index, oifp->name, ifp->name);
  78. if_delete_update(oifp);
  79. }
  80. }
  81. ifp->ifindex = ifi_index;
  82. }
  83. #ifndef SO_RCVBUFFORCE
  84. #define SO_RCVBUFFORCE (33)
  85. #endif
  86. static int
  87. netlink_recvbuf (struct nlsock *nl, uint32_t newsize)
  88. {
  89. u_int32_t oldsize;
  90. socklen_t newlen = sizeof(newsize);
  91. socklen_t oldlen = sizeof(oldsize);
  92. int ret;
  93. ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);
  94. if (ret < 0)
  95. {
  96. zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name,
  97. safe_strerror (errno));
  98. return -1;
  99. }
  100. /* Try force option (linux >= 2.6.14) and fall back to normal set */
  101. if ( zserv_privs.change (ZPRIVS_RAISE) )
  102. zlog_err ("routing_socket: Can't raise privileges");
  103. ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE, &nl_rcvbufsize,
  104. sizeof(nl_rcvbufsize));
  105. if ( zserv_privs.change (ZPRIVS_LOWER) )
  106. zlog_err ("routing_socket: Can't lower privileges");
  107. if (ret < 0)
  108. ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &nl_rcvbufsize,
  109. sizeof(nl_rcvbufsize));
  110. if (ret < 0)
  111. {
  112. zlog (NULL, LOG_ERR, "Can't set %s receive buffer size: %s", nl->name,
  113. safe_strerror (errno));
  114. return -1;
  115. }
  116. ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);
  117. if (ret < 0)
  118. {
  119. zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name,
  120. safe_strerror (errno));
  121. return -1;
  122. }
  123. zlog (NULL, LOG_INFO,
  124. "Setting netlink socket receive buffer size: %u -> %u",
  125. oldsize, newsize);
  126. return 0;
  127. }
  128. /* Make socket for Linux netlink interface. */
  129. static int
  130. netlink_socket (struct nlsock *nl, unsigned long groups, vrf_id_t vrf_id)
  131. {
  132. int ret;
  133. struct sockaddr_nl snl;
  134. int sock;
  135. int namelen;
  136. int save_errno;
  137. if (zserv_privs.change (ZPRIVS_RAISE))
  138. {
  139. zlog (NULL, LOG_ERR, "Can't raise privileges");
  140. return -1;
  141. }
  142. sock = vrf_socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, vrf_id);
  143. if (sock < 0)
  144. {
  145. zlog (NULL, LOG_ERR, "Can't open %s socket: %s", nl->name,
  146. safe_strerror (errno));
  147. return -1;
  148. }
  149. memset (&snl, 0, sizeof snl);
  150. snl.nl_family = AF_NETLINK;
  151. snl.nl_groups = groups;
  152. /* Bind the socket to the netlink structure for anything. */
  153. ret = bind (sock, (struct sockaddr *) &snl, sizeof snl);
  154. save_errno = errno;
  155. if (zserv_privs.change (ZPRIVS_LOWER))
  156. zlog (NULL, LOG_ERR, "Can't lower privileges");
  157. if (ret < 0)
  158. {
  159. zlog (NULL, LOG_ERR, "Can't bind %s socket to group 0x%x: %s",
  160. nl->name, snl.nl_groups, safe_strerror (save_errno));
  161. close (sock);
  162. return -1;
  163. }
  164. /* multiple netlink sockets will have different nl_pid */
  165. namelen = sizeof snl;
  166. ret = getsockname (sock, (struct sockaddr *) &snl, (socklen_t *) &namelen);
  167. if (ret < 0 || namelen != sizeof snl)
  168. {
  169. zlog (NULL, LOG_ERR, "Can't get %s socket name: %s", nl->name,
  170. safe_strerror (errno));
  171. close (sock);
  172. return -1;
  173. }
  174. nl->snl = snl;
  175. nl->sock = sock;
  176. return ret;
  177. }
  178. /* Get type specified information from netlink. */
  179. static int
  180. netlink_request (int family, int type, struct nlsock *nl)
  181. {
  182. int ret;
  183. struct sockaddr_nl snl;
  184. int save_errno;
  185. struct
  186. {
  187. struct nlmsghdr nlh;
  188. struct rtgenmsg g;
  189. } req;
  190. /* Check netlink socket. */
  191. if (nl->sock < 0)
  192. {
  193. zlog (NULL, LOG_ERR, "%s socket isn't active.", nl->name);
  194. return -1;
  195. }
  196. memset (&snl, 0, sizeof snl);
  197. snl.nl_family = AF_NETLINK;
  198. memset (&req, 0, sizeof req);
  199. req.nlh.nlmsg_len = sizeof req;
  200. req.nlh.nlmsg_type = type;
  201. req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
  202. req.nlh.nlmsg_pid = nl->snl.nl_pid;
  203. req.nlh.nlmsg_seq = ++nl->seq;
  204. req.g.rtgen_family = family;
  205. /* linux appears to check capabilities on every message
  206. * have to raise caps for every message sent
  207. */
  208. if (zserv_privs.change (ZPRIVS_RAISE))
  209. {
  210. zlog (NULL, LOG_ERR, "Can't raise privileges");
  211. return -1;
  212. }
  213. ret = sendto (nl->sock, (void *) &req, sizeof req, 0,
  214. (struct sockaddr *) &snl, sizeof snl);
  215. save_errno = errno;
  216. if (zserv_privs.change (ZPRIVS_LOWER))
  217. zlog (NULL, LOG_ERR, "Can't lower privileges");
  218. if (ret < 0)
  219. {
  220. zlog (NULL, LOG_ERR, "%s sendto failed: %s", nl->name,
  221. safe_strerror (save_errno));
  222. return -1;
  223. }
  224. return 0;
  225. }
  226. /* Receive message from netlink interface and pass those information
  227. to the given function. */
  228. static int
  229. netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *,
  230. vrf_id_t),
  231. struct nlsock *nl, struct zebra_vrf *zvrf)
  232. {
  233. int status;
  234. int ret = 0;
  235. int error;
  236. while (1)
  237. {
  238. char buf[NL_PKT_BUF_SIZE];
  239. struct iovec iov = {
  240. .iov_base = buf,
  241. .iov_len = sizeof buf
  242. };
  243. struct sockaddr_nl snl;
  244. struct msghdr msg = {
  245. .msg_name = (void *) &snl,
  246. .msg_namelen = sizeof snl,
  247. .msg_iov = &iov,
  248. .msg_iovlen = 1
  249. };
  250. struct nlmsghdr *h;
  251. status = recvmsg (nl->sock, &msg, 0);
  252. if (status < 0)
  253. {
  254. if (errno == EINTR)
  255. continue;
  256. if (errno == EWOULDBLOCK || errno == EAGAIN)
  257. break;
  258. zlog (NULL, LOG_ERR, "%s recvmsg overrun: %s",
  259. nl->name, safe_strerror(errno));
  260. continue;
  261. }
  262. if (status == 0)
  263. {
  264. zlog (NULL, LOG_ERR, "%s EOF", nl->name);
  265. return -1;
  266. }
  267. if (msg.msg_namelen != sizeof snl)
  268. {
  269. zlog (NULL, LOG_ERR, "%s sender address length error: length %d",
  270. nl->name, msg.msg_namelen);
  271. return -1;
  272. }
  273. for (h = (struct nlmsghdr *) buf; NLMSG_OK (h, (unsigned int) status);
  274. h = NLMSG_NEXT (h, status))
  275. {
  276. /* Finish of reading. */
  277. if (h->nlmsg_type == NLMSG_DONE)
  278. return ret;
  279. /* Error handling. */
  280. if (h->nlmsg_type == NLMSG_ERROR)
  281. {
  282. struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h);
  283. int errnum = err->error;
  284. int msg_type = err->msg.nlmsg_type;
  285. /* If the error field is zero, then this is an ACK */
  286. if (err->error == 0)
  287. {
  288. if (IS_ZEBRA_DEBUG_KERNEL)
  289. {
  290. zlog_debug ("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
  291. __FUNCTION__, nl->name,
  292. lookup (nlmsg_str, err->msg.nlmsg_type),
  293. err->msg.nlmsg_type, err->msg.nlmsg_seq,
  294. err->msg.nlmsg_pid);
  295. }
  296. /* return if not a multipart message, otherwise continue */
  297. if (!(h->nlmsg_flags & NLM_F_MULTI))
  298. {
  299. return 0;
  300. }
  301. continue;
  302. }
  303. if (h->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
  304. {
  305. zlog (NULL, LOG_ERR, "%s error: message truncated",
  306. nl->name);
  307. return -1;
  308. }
  309. /* Deal with errors that occur because of races in link handling */
  310. if (nl == &zvrf->netlink_cmd
  311. && ((msg_type == RTM_DELROUTE &&
  312. (-errnum == ENODEV || -errnum == ESRCH))
  313. || (msg_type == RTM_NEWROUTE && -errnum == EEXIST)))
  314. {
  315. if (IS_ZEBRA_DEBUG_KERNEL)
  316. zlog_debug ("%s: error: %s type=%s(%u), seq=%u, pid=%u",
  317. nl->name, safe_strerror (-errnum),
  318. lookup (nlmsg_str, msg_type),
  319. msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid);
  320. return 0;
  321. }
  322. zlog_err ("%s error: %s, type=%s(%u), seq=%u, pid=%u",
  323. nl->name, safe_strerror (-errnum),
  324. lookup (nlmsg_str, msg_type),
  325. msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid);
  326. return -1;
  327. }
  328. /* OK we got netlink message. */
  329. if (IS_ZEBRA_DEBUG_KERNEL)
  330. zlog_debug ("netlink_parse_info: %s type %s(%u), seq=%u, pid=%u",
  331. nl->name,
  332. lookup (nlmsg_str, h->nlmsg_type), h->nlmsg_type,
  333. h->nlmsg_seq, h->nlmsg_pid);
  334. /* skip unsolicited messages originating from command socket
  335. * linux sets the originators port-id for {NEW|DEL}ADDR messages,
  336. * so this has to be checked here. */
  337. if (nl != &zvrf->netlink_cmd
  338. && h->nlmsg_pid == zvrf->netlink_cmd.snl.nl_pid
  339. && (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR))
  340. {
  341. if (IS_ZEBRA_DEBUG_KERNEL)
  342. zlog_debug ("netlink_parse_info: %s packet comes from %s",
  343. zvrf->netlink_cmd.name, nl->name);
  344. continue;
  345. }
  346. error = (*filter) (&snl, h, zvrf->vrf_id);
  347. if (error < 0)
  348. {
  349. zlog (NULL, LOG_ERR, "%s filter function error", nl->name);
  350. ret = error;
  351. }
  352. }
  353. /* After error care. */
  354. if (msg.msg_flags & MSG_TRUNC)
  355. {
  356. zlog (NULL, LOG_ERR, "%s error: message truncated", nl->name);
  357. continue;
  358. }
  359. if (status)
  360. {
  361. zlog (NULL, LOG_ERR, "%s error: data remnant size %d", nl->name,
  362. status);
  363. return -1;
  364. }
  365. }
  366. return ret;
  367. }
  368. /* Utility function for parse rtattr. */
  369. static void
  370. netlink_parse_rtattr (struct rtattr **tb, int max, struct rtattr *rta,
  371. int len)
  372. {
  373. while (RTA_OK (rta, len))
  374. {
  375. if (rta->rta_type <= max)
  376. tb[rta->rta_type] = rta;
  377. rta = RTA_NEXT (rta, len);
  378. }
  379. }
  380. /* Utility function to parse hardware link-layer address and update ifp */
  381. static void
  382. netlink_interface_update_hw_addr (struct rtattr **tb, struct interface *ifp)
  383. {
  384. int i;
  385. if (tb[IFLA_ADDRESS])
  386. {
  387. int hw_addr_len;
  388. hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]);
  389. if (hw_addr_len > INTERFACE_HWADDR_MAX)
  390. zlog_warn ("Hardware address is too large: %d", hw_addr_len);
  391. else
  392. {
  393. ifp->hw_addr_len = hw_addr_len;
  394. memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len);
  395. for (i = 0; i < hw_addr_len; i++)
  396. if (ifp->hw_addr[i] != 0)
  397. break;
  398. if (i == hw_addr_len)
  399. ifp->hw_addr_len = 0;
  400. else
  401. ifp->hw_addr_len = hw_addr_len;
  402. }
  403. }
  404. }
  405. /* Called from interface_lookup_netlink(). This function is only used
  406. during bootstrap. */
  407. static int
  408. netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h,
  409. vrf_id_t vrf_id)
  410. {
  411. int len;
  412. struct ifinfomsg *ifi;
  413. struct rtattr *tb[IFLA_MAX + 1];
  414. struct interface *ifp;
  415. char *name;
  416. ifi = NLMSG_DATA (h);
  417. if (h->nlmsg_type != RTM_NEWLINK)
  418. return 0;
  419. len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
  420. if (len < 0)
  421. return -1;
  422. /* Looking up interface name. */
  423. memset (tb, 0, sizeof tb);
  424. netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
  425. #ifdef IFLA_WIRELESS
  426. /* check for wireless messages to ignore */
  427. if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
  428. {
  429. if (IS_ZEBRA_DEBUG_KERNEL)
  430. zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
  431. return 0;
  432. }
  433. #endif /* IFLA_WIRELESS */
  434. if (tb[IFLA_IFNAME] == NULL)
  435. return -1;
  436. name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
  437. /* Add interface. */
  438. ifp = if_get_by_name_vrf (name, vrf_id);
  439. set_ifindex(ifp, ifi->ifi_index);
  440. ifp->flags = ifi->ifi_flags & 0x0000fffff;
  441. ifp->mtu6 = ifp->mtu = *(uint32_t *) RTA_DATA (tb[IFLA_MTU]);
  442. ifp->metric = 0;
  443. /* Hardware type and address. */
  444. ifp->hw_type = ifi->ifi_type;
  445. netlink_interface_update_hw_addr (tb, ifp);
  446. if_add_update (ifp);
  447. return 0;
  448. }
  449. /* Lookup interface IPv4/IPv6 address. */
  450. static int
  451. netlink_interface_addr (struct sockaddr_nl *snl, struct nlmsghdr *h,
  452. vrf_id_t vrf_id)
  453. {
  454. int len;
  455. struct ifaddrmsg *ifa;
  456. struct rtattr *tb[IFA_MAX + 1];
  457. struct interface *ifp;
  458. void *addr;
  459. void *broad;
  460. u_char flags = 0;
  461. char *label = NULL;
  462. ifa = NLMSG_DATA (h);
  463. if (ifa->ifa_family != AF_INET
  464. #ifdef HAVE_IPV6
  465. && ifa->ifa_family != AF_INET6
  466. #endif /* HAVE_IPV6 */
  467. )
  468. return 0;
  469. if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
  470. return 0;
  471. len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifaddrmsg));
  472. if (len < 0)
  473. return -1;
  474. memset (tb, 0, sizeof tb);
  475. netlink_parse_rtattr (tb, IFA_MAX, IFA_RTA (ifa), len);
  476. ifp = if_lookup_by_index_vrf (ifa->ifa_index, vrf_id);
  477. if (ifp == NULL)
  478. {
  479. zlog_err ("netlink_interface_addr can't find interface by index %d vrf %u",
  480. ifa->ifa_index, vrf_id);
  481. return -1;
  482. }
  483. if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
  484. {
  485. char buf[BUFSIZ];
  486. zlog_debug ("netlink_interface_addr %s %s vrf %u:",
  487. lookup (nlmsg_str, h->nlmsg_type), ifp->name, vrf_id);
  488. if (tb[IFA_LOCAL])
  489. zlog_debug (" IFA_LOCAL %s/%d",
  490. inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_LOCAL]),
  491. buf, BUFSIZ), ifa->ifa_prefixlen);
  492. if (tb[IFA_ADDRESS])
  493. zlog_debug (" IFA_ADDRESS %s/%d",
  494. inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_ADDRESS]),
  495. buf, BUFSIZ), ifa->ifa_prefixlen);
  496. if (tb[IFA_BROADCAST])
  497. zlog_debug (" IFA_BROADCAST %s/%d",
  498. inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_BROADCAST]),
  499. buf, BUFSIZ), ifa->ifa_prefixlen);
  500. if (tb[IFA_LABEL] && strcmp (ifp->name, RTA_DATA (tb[IFA_LABEL])))
  501. zlog_debug (" IFA_LABEL %s", (char *)RTA_DATA (tb[IFA_LABEL]));
  502. if (tb[IFA_CACHEINFO])
  503. {
  504. struct ifa_cacheinfo *ci = RTA_DATA (tb[IFA_CACHEINFO]);
  505. zlog_debug (" IFA_CACHEINFO pref %d, valid %d",
  506. ci->ifa_prefered, ci->ifa_valid);
  507. }
  508. }
  509. /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
  510. if (tb[IFA_LOCAL] == NULL)
  511. tb[IFA_LOCAL] = tb[IFA_ADDRESS];
  512. if (tb[IFA_ADDRESS] == NULL)
  513. tb[IFA_ADDRESS] = tb[IFA_LOCAL];
  514. /* local interface address */
  515. addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
  516. /* is there a peer address? */
  517. if (tb[IFA_ADDRESS] &&
  518. memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_ADDRESS])))
  519. {
  520. broad = RTA_DATA(tb[IFA_ADDRESS]);
  521. SET_FLAG (flags, ZEBRA_IFA_PEER);
  522. }
  523. else
  524. /* seeking a broadcast address */
  525. broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) : NULL);
  526. /* addr is primary key, SOL if we don't have one */
  527. if (addr == NULL)
  528. {
  529. zlog_debug ("%s: NULL address", __func__);
  530. return -1;
  531. }
  532. /* Flags. */
  533. if (ifa->ifa_flags & IFA_F_SECONDARY)
  534. SET_FLAG (flags, ZEBRA_IFA_SECONDARY);
  535. /* Label */
  536. if (tb[IFA_LABEL])
  537. label = (char *) RTA_DATA (tb[IFA_LABEL]);
  538. if (ifp && label && strcmp (ifp->name, label) == 0)
  539. label = NULL;
  540. /* Register interface address to the interface. */
  541. if (ifa->ifa_family == AF_INET)
  542. {
  543. if (h->nlmsg_type == RTM_NEWADDR)
  544. connected_add_ipv4 (ifp, flags,
  545. (struct in_addr *) addr, ifa->ifa_prefixlen,
  546. (struct in_addr *) broad, label);
  547. else
  548. connected_delete_ipv4 (ifp, flags,
  549. (struct in_addr *) addr, ifa->ifa_prefixlen,
  550. (struct in_addr *) broad);
  551. }
  552. #ifdef HAVE_IPV6
  553. if (ifa->ifa_family == AF_INET6)
  554. {
  555. if (h->nlmsg_type == RTM_NEWADDR)
  556. connected_add_ipv6 (ifp, flags,
  557. (struct in6_addr *) addr, ifa->ifa_prefixlen,
  558. (struct in6_addr *) broad, label);
  559. else
  560. connected_delete_ipv6 (ifp,
  561. (struct in6_addr *) addr, ifa->ifa_prefixlen,
  562. (struct in6_addr *) broad);
  563. }
  564. #endif /* HAVE_IPV6 */
  565. return 0;
  566. }
  567. /* Looking up routing table by netlink interface. */
  568. static int
  569. netlink_routing_table (struct sockaddr_nl *snl, struct nlmsghdr *h,
  570. vrf_id_t vrf_id)
  571. {
  572. int len;
  573. struct rtmsg *rtm;
  574. struct rtattr *tb[RTA_MAX + 1];
  575. u_char flags = 0;
  576. char anyaddr[16] = { 0 };
  577. int index;
  578. int table;
  579. int metric;
  580. u_int32_t mtu = 0;
  581. void *dest;
  582. void *gate;
  583. void *src;
  584. rtm = NLMSG_DATA (h);
  585. if (h->nlmsg_type != RTM_NEWROUTE)
  586. return 0;
  587. if (rtm->rtm_type != RTN_UNICAST)
  588. return 0;
  589. table = rtm->rtm_table;
  590. #if 0 /* we weed them out later in rib_weed_tables () */
  591. if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
  592. return 0;
  593. #endif
  594. len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
  595. if (len < 0)
  596. return -1;
  597. memset (tb, 0, sizeof tb);
  598. netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
  599. if (rtm->rtm_flags & RTM_F_CLONED)
  600. return 0;
  601. if (rtm->rtm_protocol == RTPROT_REDIRECT)
  602. return 0;
  603. if (rtm->rtm_protocol == RTPROT_KERNEL)
  604. return 0;
  605. if (rtm->rtm_src_len != 0)
  606. return 0;
  607. /* Route which inserted by Zebra. */
  608. if (rtm->rtm_protocol == RTPROT_ZEBRA)
  609. flags |= ZEBRA_FLAG_SELFROUTE;
  610. index = 0;
  611. metric = 0;
  612. dest = NULL;
  613. gate = NULL;
  614. src = NULL;
  615. if (tb[RTA_OIF])
  616. index = *(int *) RTA_DATA (tb[RTA_OIF]);
  617. if (tb[RTA_DST])
  618. dest = RTA_DATA (tb[RTA_DST]);
  619. else
  620. dest = anyaddr;
  621. if (tb[RTA_PREFSRC])
  622. src = RTA_DATA (tb[RTA_PREFSRC]);
  623. if (tb[RTA_GATEWAY])
  624. gate = RTA_DATA (tb[RTA_GATEWAY]);
  625. if (tb[RTA_PRIORITY])
  626. metric = *(int *) RTA_DATA(tb[RTA_PRIORITY]);
  627. if (tb[RTA_METRICS])
  628. {
  629. struct rtattr *mxrta[RTAX_MAX+1];
  630. memset (mxrta, 0, sizeof mxrta);
  631. netlink_parse_rtattr (mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
  632. RTA_PAYLOAD(tb[RTA_METRICS]));
  633. if (mxrta[RTAX_MTU])
  634. mtu = *(u_int32_t *) RTA_DATA(mxrta[RTAX_MTU]);
  635. }
  636. if (rtm->rtm_family == AF_INET)
  637. {
  638. struct prefix_ipv4 p;
  639. p.family = AF_INET;
  640. memcpy (&p.prefix, dest, 4);
  641. p.prefixlen = rtm->rtm_dst_len;
  642. if (!tb[RTA_MULTIPATH])
  643. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, src, index,
  644. vrf_id, table, metric, mtu, 0, SAFI_UNICAST);
  645. else
  646. {
  647. /* This is a multipath route */
  648. struct rib *rib;
  649. struct rtnexthop *rtnh =
  650. (struct rtnexthop *) RTA_DATA (tb[RTA_MULTIPATH]);
  651. len = RTA_PAYLOAD (tb[RTA_MULTIPATH]);
  652. rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
  653. rib->type = ZEBRA_ROUTE_KERNEL;
  654. rib->distance = 0;
  655. rib->flags = flags;
  656. rib->metric = metric;
  657. rib->mtu = mtu;
  658. rib->vrf_id = vrf_id;
  659. rib->table = table;
  660. rib->nexthop_num = 0;
  661. rib->uptime = time (NULL);
  662. for (;;)
  663. {
  664. if (len < (int) sizeof (*rtnh) || rtnh->rtnh_len > len)
  665. break;
  666. index = rtnh->rtnh_ifindex;
  667. gate = 0;
  668. if (rtnh->rtnh_len > sizeof (*rtnh))
  669. {
  670. memset (tb, 0, sizeof (tb));
  671. netlink_parse_rtattr (tb, RTA_MAX, RTNH_DATA (rtnh),
  672. rtnh->rtnh_len - sizeof (*rtnh));
  673. if (tb[RTA_GATEWAY])
  674. gate = RTA_DATA (tb[RTA_GATEWAY]);
  675. }
  676. if (gate)
  677. {
  678. if (index)
  679. nexthop_ipv4_ifindex_add (rib, gate, src, index);
  680. else
  681. nexthop_ipv4_add (rib, gate, src);
  682. }
  683. else
  684. nexthop_ifindex_add (rib, index);
  685. len -= NLMSG_ALIGN(rtnh->rtnh_len);
  686. rtnh = RTNH_NEXT(rtnh);
  687. }
  688. if (rib->nexthop_num == 0)
  689. XFREE (MTYPE_RIB, rib);
  690. else
  691. rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST);
  692. }
  693. }
  694. #ifdef HAVE_IPV6
  695. if (rtm->rtm_family == AF_INET6)
  696. {
  697. struct prefix_ipv6 p;
  698. p.family = AF_INET6;
  699. memcpy (&p.prefix, dest, 16);
  700. p.prefixlen = rtm->rtm_dst_len;
  701. rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, vrf_id,
  702. table, metric, mtu, 0, SAFI_UNICAST);
  703. }
  704. #endif /* HAVE_IPV6 */
  705. return 0;
  706. }
  707. static const struct message rtproto_str[] = {
  708. {RTPROT_REDIRECT, "redirect"},
  709. {RTPROT_KERNEL, "kernel"},
  710. {RTPROT_BOOT, "boot"},
  711. {RTPROT_STATIC, "static"},
  712. {RTPROT_GATED, "GateD"},
  713. {RTPROT_RA, "router advertisement"},
  714. {RTPROT_MRT, "MRT"},
  715. {RTPROT_ZEBRA, "Zebra"},
  716. #ifdef RTPROT_BIRD
  717. {RTPROT_BIRD, "BIRD"},
  718. #endif /* RTPROT_BIRD */
  719. {0, NULL}
  720. };
  721. /* Routing information change from the kernel. */
  722. static int
  723. netlink_route_change (struct sockaddr_nl *snl, struct nlmsghdr *h,
  724. vrf_id_t vrf_id)
  725. {
  726. int len;
  727. struct rtmsg *rtm;
  728. struct rtattr *tb[RTA_MAX + 1];
  729. char anyaddr[16] = { 0 };
  730. int index;
  731. int table;
  732. int metric;
  733. u_int32_t mtu = 0;
  734. void *dest;
  735. void *gate;
  736. void *src;
  737. rtm = NLMSG_DATA (h);
  738. if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE))
  739. {
  740. /* If this is not route add/delete message print warning. */
  741. zlog_warn ("Kernel message: %d vrf %u\n", h->nlmsg_type, vrf_id);
  742. return 0;
  743. }
  744. /* Connected route. */
  745. if (IS_ZEBRA_DEBUG_KERNEL)
  746. zlog_debug ("%s %s %s proto %s vrf %u",
  747. h->nlmsg_type ==
  748. RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
  749. rtm->rtm_family == AF_INET ? "ipv4" : "ipv6",
  750. rtm->rtm_type == RTN_UNICAST ? "unicast" : "multicast",
  751. lookup (rtproto_str, rtm->rtm_protocol),
  752. vrf_id);
  753. if (rtm->rtm_type != RTN_UNICAST)
  754. {
  755. return 0;
  756. }
  757. table = rtm->rtm_table;
  758. if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
  759. {
  760. return 0;
  761. }
  762. len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
  763. if (len < 0)
  764. return -1;
  765. memset (tb, 0, sizeof tb);
  766. netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
  767. if (rtm->rtm_flags & RTM_F_CLONED)
  768. return 0;
  769. if (rtm->rtm_protocol == RTPROT_REDIRECT)
  770. return 0;
  771. if (rtm->rtm_protocol == RTPROT_KERNEL)
  772. return 0;
  773. if (rtm->rtm_protocol == RTPROT_ZEBRA && h->nlmsg_type == RTM_NEWROUTE)
  774. return 0;
  775. if (rtm->rtm_src_len != 0)
  776. {
  777. zlog_warn ("netlink_route_change(): no src len, vrf %u", vrf_id);
  778. return 0;
  779. }
  780. index = 0;
  781. metric = 0;
  782. dest = NULL;
  783. gate = NULL;
  784. src = NULL;
  785. if (tb[RTA_OIF])
  786. index = *(int *) RTA_DATA (tb[RTA_OIF]);
  787. if (tb[RTA_DST])
  788. dest = RTA_DATA (tb[RTA_DST]);
  789. else
  790. dest = anyaddr;
  791. if (tb[RTA_GATEWAY])
  792. gate = RTA_DATA (tb[RTA_GATEWAY]);
  793. if (tb[RTA_PREFSRC])
  794. src = RTA_DATA (tb[RTA_PREFSRC]);
  795. if (h->nlmsg_type == RTM_NEWROUTE)
  796. {
  797. if (tb[RTA_PRIORITY])
  798. metric = *(int *) RTA_DATA(tb[RTA_PRIORITY]);
  799. if (tb[RTA_METRICS])
  800. {
  801. struct rtattr *mxrta[RTAX_MAX+1];
  802. memset (mxrta, 0, sizeof mxrta);
  803. netlink_parse_rtattr (mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
  804. RTA_PAYLOAD(tb[RTA_METRICS]));
  805. if (mxrta[RTAX_MTU])
  806. mtu = *(u_int32_t *) RTA_DATA(mxrta[RTAX_MTU]);
  807. }
  808. }
  809. if (rtm->rtm_family == AF_INET)
  810. {
  811. struct prefix_ipv4 p;
  812. p.family = AF_INET;
  813. memcpy (&p.prefix, dest, 4);
  814. p.prefixlen = rtm->rtm_dst_len;
  815. if (IS_ZEBRA_DEBUG_KERNEL)
  816. {
  817. char buf[PREFIX_STRLEN];
  818. zlog_debug ("%s %s vrf %u",
  819. h->nlmsg_type == RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
  820. prefix2str (&p, buf, sizeof(buf)), vrf_id);
  821. }
  822. if (h->nlmsg_type == RTM_NEWROUTE)
  823. {
  824. if (!tb[RTA_MULTIPATH])
  825. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, src, index, vrf_id,
  826. table, metric, mtu, 0, SAFI_UNICAST);
  827. else
  828. {
  829. /* This is a multipath route */
  830. struct rib *rib;
  831. struct rtnexthop *rtnh =
  832. (struct rtnexthop *) RTA_DATA (tb[RTA_MULTIPATH]);
  833. len = RTA_PAYLOAD (tb[RTA_MULTIPATH]);
  834. rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
  835. rib->type = ZEBRA_ROUTE_KERNEL;
  836. rib->distance = 0;
  837. rib->flags = 0;
  838. rib->metric = metric;
  839. rib->mtu = mtu;
  840. rib->vrf_id = vrf_id;
  841. rib->table = table;
  842. rib->nexthop_num = 0;
  843. rib->uptime = time (NULL);
  844. for (;;)
  845. {
  846. if (len < (int) sizeof (*rtnh) || rtnh->rtnh_len > len)
  847. break;
  848. index = rtnh->rtnh_ifindex;
  849. gate = 0;
  850. if (rtnh->rtnh_len > sizeof (*rtnh))
  851. {
  852. memset (tb, 0, sizeof (tb));
  853. netlink_parse_rtattr (tb, RTA_MAX, RTNH_DATA (rtnh),
  854. rtnh->rtnh_len - sizeof (*rtnh));
  855. if (tb[RTA_GATEWAY])
  856. gate = RTA_DATA (tb[RTA_GATEWAY]);
  857. }
  858. if (gate)
  859. {
  860. if (index)
  861. nexthop_ipv4_ifindex_add (rib, gate, src, index);
  862. else
  863. nexthop_ipv4_add (rib, gate, src);
  864. }
  865. else
  866. nexthop_ifindex_add (rib, index);
  867. len -= NLMSG_ALIGN(rtnh->rtnh_len);
  868. rtnh = RTNH_NEXT(rtnh);
  869. }
  870. if (rib->nexthop_num == 0)
  871. XFREE (MTYPE_RIB, rib);
  872. else
  873. rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST);
  874. }
  875. }
  876. else
  877. rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, vrf_id,
  878. SAFI_UNICAST);
  879. }
  880. #ifdef HAVE_IPV6
  881. if (rtm->rtm_family == AF_INET6)
  882. {
  883. struct prefix_ipv6 p;
  884. p.family = AF_INET6;
  885. memcpy (&p.prefix, dest, 16);
  886. p.prefixlen = rtm->rtm_dst_len;
  887. if (IS_ZEBRA_DEBUG_KERNEL)
  888. {
  889. char buf[PREFIX_STRLEN];
  890. zlog_debug ("%s %s vrf %u",
  891. h->nlmsg_type == RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
  892. prefix2str (&p, buf, sizeof(buf)), vrf_id);
  893. }
  894. if (h->nlmsg_type == RTM_NEWROUTE)
  895. rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, vrf_id, table,
  896. metric, mtu, 0, SAFI_UNICAST);
  897. else
  898. rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, vrf_id,
  899. SAFI_UNICAST);
  900. }
  901. #endif /* HAVE_IPV6 */
  902. return 0;
  903. }
  904. static int
  905. netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h,
  906. vrf_id_t vrf_id)
  907. {
  908. int len;
  909. struct ifinfomsg *ifi;
  910. struct rtattr *tb[IFLA_MAX + 1];
  911. struct interface *ifp;
  912. char *name;
  913. ifi = NLMSG_DATA (h);
  914. if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK))
  915. {
  916. /* If this is not link add/delete message so print warning. */
  917. zlog_warn ("netlink_link_change: wrong kernel message %d vrf %u\n",
  918. h->nlmsg_type, vrf_id);
  919. return 0;
  920. }
  921. len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
  922. if (len < 0)
  923. return -1;
  924. /* Looking up interface name. */
  925. memset (tb, 0, sizeof tb);
  926. netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
  927. #ifdef IFLA_WIRELESS
  928. /* check for wireless messages to ignore */
  929. if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
  930. {
  931. if (IS_ZEBRA_DEBUG_KERNEL)
  932. zlog_debug ("%s: ignoring IFLA_WIRELESS message, vrf %u", __func__,
  933. vrf_id);
  934. return 0;
  935. }
  936. #endif /* IFLA_WIRELESS */
  937. if (tb[IFLA_IFNAME] == NULL)
  938. return -1;
  939. name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
  940. /* Add interface. */
  941. if (h->nlmsg_type == RTM_NEWLINK)
  942. {
  943. ifp = if_lookup_by_name_vrf (name, vrf_id);
  944. if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  945. {
  946. if (ifp == NULL)
  947. ifp = if_get_by_name_vrf (name, vrf_id);
  948. set_ifindex(ifp, ifi->ifi_index);
  949. ifp->flags = ifi->ifi_flags & 0x0000fffff;
  950. ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
  951. ifp->metric = 0;
  952. netlink_interface_update_hw_addr (tb, ifp);
  953. /* If new link is added. */
  954. if_add_update (ifp);
  955. }
  956. else
  957. {
  958. /* Interface status change. */
  959. set_ifindex(ifp, ifi->ifi_index);
  960. ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
  961. ifp->metric = 0;
  962. netlink_interface_update_hw_addr (tb, ifp);
  963. if (if_is_operative (ifp))
  964. {
  965. ifp->flags = ifi->ifi_flags & 0x0000fffff;
  966. if (!if_is_operative (ifp))
  967. if_down (ifp);
  968. else
  969. /* Must notify client daemons of new interface status. */
  970. zebra_interface_up_update (ifp);
  971. }
  972. else
  973. {
  974. ifp->flags = ifi->ifi_flags & 0x0000fffff;
  975. if (if_is_operative (ifp))
  976. if_up (ifp);
  977. }
  978. }
  979. }
  980. else
  981. {
  982. /* RTM_DELLINK. */
  983. ifp = if_lookup_by_name_vrf (name, vrf_id);
  984. if (ifp == NULL)
  985. {
  986. zlog_warn ("interface %s vrf %u is deleted but can't find",
  987. name, vrf_id);
  988. return 0;
  989. }
  990. if_delete_update (ifp);
  991. }
  992. return 0;
  993. }
  994. static int
  995. netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h,
  996. vrf_id_t vrf_id)
  997. {
  998. /* JF: Ignore messages that aren't from the kernel */
  999. if ( snl->nl_pid != 0 )
  1000. {
  1001. zlog ( NULL, LOG_ERR, "Ignoring message from pid %u", snl->nl_pid );
  1002. return 0;
  1003. }
  1004. switch (h->nlmsg_type)
  1005. {
  1006. case RTM_NEWROUTE:
  1007. return netlink_route_change (snl, h, vrf_id);
  1008. break;
  1009. case RTM_DELROUTE:
  1010. return netlink_route_change (snl, h, vrf_id);
  1011. break;
  1012. case RTM_NEWLINK:
  1013. return netlink_link_change (snl, h, vrf_id);
  1014. break;
  1015. case RTM_DELLINK:
  1016. return netlink_link_change (snl, h, vrf_id);
  1017. break;
  1018. case RTM_NEWADDR:
  1019. return netlink_interface_addr (snl, h, vrf_id);
  1020. break;
  1021. case RTM_DELADDR:
  1022. return netlink_interface_addr (snl, h, vrf_id);
  1023. break;
  1024. default:
  1025. zlog_warn ("Unknown netlink nlmsg_type %d vrf %u\n", h->nlmsg_type,
  1026. vrf_id);
  1027. break;
  1028. }
  1029. return 0;
  1030. }
  1031. /* Interface lookup by netlink socket. */
  1032. int
  1033. interface_lookup_netlink (struct zebra_vrf *zvrf)
  1034. {
  1035. int ret;
  1036. /* Get interface information. */
  1037. ret = netlink_request (AF_PACKET, RTM_GETLINK, &zvrf->netlink_cmd);
  1038. if (ret < 0)
  1039. return ret;
  1040. ret = netlink_parse_info (netlink_interface, &zvrf->netlink_cmd, zvrf);
  1041. if (ret < 0)
  1042. return ret;
  1043. /* Get IPv4 address of the interfaces. */
  1044. ret = netlink_request (AF_INET, RTM_GETADDR, &zvrf->netlink_cmd);
  1045. if (ret < 0)
  1046. return ret;
  1047. ret = netlink_parse_info (netlink_interface_addr, &zvrf->netlink_cmd, zvrf);
  1048. if (ret < 0)
  1049. return ret;
  1050. #ifdef HAVE_IPV6
  1051. /* Get IPv6 address of the interfaces. */
  1052. ret = netlink_request (AF_INET6, RTM_GETADDR, &zvrf->netlink_cmd);
  1053. if (ret < 0)
  1054. return ret;
  1055. ret = netlink_parse_info (netlink_interface_addr, &zvrf->netlink_cmd, zvrf);
  1056. if (ret < 0)
  1057. return ret;
  1058. #endif /* HAVE_IPV6 */
  1059. return 0;
  1060. }
  1061. /* Routing table read function using netlink interface. Only called
  1062. bootstrap time. */
  1063. int
  1064. netlink_route_read (struct zebra_vrf *zvrf)
  1065. {
  1066. int ret;
  1067. /* Get IPv4 routing table. */
  1068. ret = netlink_request (AF_INET, RTM_GETROUTE, &zvrf->netlink_cmd);
  1069. if (ret < 0)
  1070. return ret;
  1071. ret = netlink_parse_info (netlink_routing_table, &zvrf->netlink_cmd, zvrf);
  1072. if (ret < 0)
  1073. return ret;
  1074. #ifdef HAVE_IPV6
  1075. /* Get IPv6 routing table. */
  1076. ret = netlink_request (AF_INET6, RTM_GETROUTE, &zvrf->netlink_cmd);
  1077. if (ret < 0)
  1078. return ret;
  1079. ret = netlink_parse_info (netlink_routing_table, &zvrf->netlink_cmd, zvrf);
  1080. if (ret < 0)
  1081. return ret;
  1082. #endif /* HAVE_IPV6 */
  1083. return 0;
  1084. }
  1085. /* Utility function comes from iproute2.
  1086. Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
  1087. int
  1088. addattr_l (struct nlmsghdr *n, size_t maxlen, int type, void *data, int alen)
  1089. {
  1090. size_t len;
  1091. struct rtattr *rta;
  1092. len = RTA_LENGTH (alen);
  1093. if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
  1094. return -1;
  1095. rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
  1096. rta->rta_type = type;
  1097. rta->rta_len = len;
  1098. memcpy (RTA_DATA (rta), data, alen);
  1099. n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
  1100. return 0;
  1101. }
  1102. int
  1103. rta_addattr_l (struct rtattr *rta, int maxlen, int type, void *data, int alen)
  1104. {
  1105. int len;
  1106. struct rtattr *subrta;
  1107. len = RTA_LENGTH (alen);
  1108. if (RTA_ALIGN (rta->rta_len) + len > maxlen)
  1109. return -1;
  1110. subrta = (struct rtattr *) (((char *) rta) + RTA_ALIGN (rta->rta_len));
  1111. subrta->rta_type = type;
  1112. subrta->rta_len = len;
  1113. memcpy (RTA_DATA (subrta), data, alen);
  1114. rta->rta_len = NLMSG_ALIGN (rta->rta_len) + len;
  1115. return 0;
  1116. }
  1117. /* Utility function comes from iproute2.
  1118. Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
  1119. int
  1120. addattr32 (struct nlmsghdr *n, size_t maxlen, int type, int data)
  1121. {
  1122. size_t len;
  1123. struct rtattr *rta;
  1124. len = RTA_LENGTH (4);
  1125. if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
  1126. return -1;
  1127. rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
  1128. rta->rta_type = type;
  1129. rta->rta_len = len;
  1130. memcpy (RTA_DATA (rta), &data, 4);
  1131. n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
  1132. return 0;
  1133. }
  1134. static int
  1135. netlink_talk_filter (struct sockaddr_nl *snl, struct nlmsghdr *h,
  1136. vrf_id_t vrf_id)
  1137. {
  1138. zlog_warn ("netlink_talk: ignoring message type 0x%04x vrf %u", h->nlmsg_type,
  1139. vrf_id);
  1140. return 0;
  1141. }
  1142. /* sendmsg() to netlink socket then recvmsg(). */
  1143. static int
  1144. netlink_talk (struct nlmsghdr *n, struct nlsock *nl, struct zebra_vrf *zvrf)
  1145. {
  1146. int status;
  1147. struct sockaddr_nl snl;
  1148. struct iovec iov = {
  1149. .iov_base = (void *) n,
  1150. .iov_len = n->nlmsg_len
  1151. };
  1152. struct msghdr msg = {
  1153. .msg_name = (void *) &snl,
  1154. .msg_namelen = sizeof snl,
  1155. .msg_iov = &iov,
  1156. .msg_iovlen = 1,
  1157. };
  1158. int save_errno;
  1159. memset (&snl, 0, sizeof snl);
  1160. snl.nl_family = AF_NETLINK;
  1161. n->nlmsg_seq = ++nl->seq;
  1162. /* Request an acknowledgement by setting NLM_F_ACK */
  1163. n->nlmsg_flags |= NLM_F_ACK;
  1164. if (IS_ZEBRA_DEBUG_KERNEL)
  1165. zlog_debug ("netlink_talk: %s type %s(%u), seq=%u", nl->name,
  1166. lookup (nlmsg_str, n->nlmsg_type), n->nlmsg_type,
  1167. n->nlmsg_seq);
  1168. /* Send message to netlink interface. */
  1169. if (zserv_privs.change (ZPRIVS_RAISE))
  1170. zlog (NULL, LOG_ERR, "Can't raise privileges");
  1171. status = sendmsg (nl->sock, &msg, 0);
  1172. save_errno = errno;
  1173. if (zserv_privs.change (ZPRIVS_LOWER))
  1174. zlog (NULL, LOG_ERR, "Can't lower privileges");
  1175. if (status < 0)
  1176. {
  1177. zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s",
  1178. safe_strerror (save_errno));
  1179. return -1;
  1180. }
  1181. /*
  1182. * Get reply from netlink socket.
  1183. * The reply should either be an acknowlegement or an error.
  1184. */
  1185. return netlink_parse_info (netlink_talk_filter, nl, zvrf);
  1186. }
  1187. /* This function takes a nexthop as argument and adds
  1188. * the appropriate netlink attributes to an existing
  1189. * netlink message.
  1190. *
  1191. * @param routedesc: Human readable description of route type
  1192. * (direct/recursive, single-/multipath)
  1193. * @param bytelen: Length of addresses in bytes.
  1194. * @param nexthop: Nexthop information
  1195. * @param nlmsg: nlmsghdr structure to fill in.
  1196. * @param req_size: The size allocated for the message.
  1197. */
  1198. static void
  1199. _netlink_route_build_singlepath(
  1200. const char *routedesc,
  1201. int bytelen,
  1202. struct nexthop *nexthop,
  1203. struct nlmsghdr *nlmsg,
  1204. struct rtmsg *rtmsg,
  1205. size_t req_size)
  1206. {
  1207. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ONLINK))
  1208. rtmsg->rtm_flags |= RTNH_F_ONLINK;
  1209. if (nexthop->type == NEXTHOP_TYPE_IPV4
  1210. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  1211. {
  1212. addattr_l (nlmsg, req_size, RTA_GATEWAY,
  1213. &nexthop->gate.ipv4, bytelen);
  1214. if (nexthop->src.ipv4.s_addr)
  1215. addattr_l (nlmsg, req_size, RTA_PREFSRC,
  1216. &nexthop->src.ipv4, bytelen);
  1217. if (IS_ZEBRA_DEBUG_KERNEL)
  1218. zlog_debug("netlink_route_multipath() (%s): "
  1219. "nexthop via %s if %u",
  1220. routedesc,
  1221. inet_ntoa (nexthop->gate.ipv4),
  1222. nexthop->ifindex);
  1223. }
  1224. #ifdef HAVE_IPV6
  1225. if (nexthop->type == NEXTHOP_TYPE_IPV6
  1226. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  1227. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  1228. {
  1229. addattr_l (nlmsg, req_size, RTA_GATEWAY,
  1230. &nexthop->gate.ipv6, bytelen);
  1231. if (IS_ZEBRA_DEBUG_KERNEL)
  1232. zlog_debug("netlink_route_multipath() (%s): "
  1233. "nexthop via %s if %u",
  1234. routedesc,
  1235. inet6_ntoa (nexthop->gate.ipv6),
  1236. nexthop->ifindex);
  1237. }
  1238. #endif /* HAVE_IPV6 */
  1239. if (nexthop->type == NEXTHOP_TYPE_IFINDEX
  1240. || nexthop->type == NEXTHOP_TYPE_IFNAME
  1241. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  1242. {
  1243. addattr32 (nlmsg, req_size, RTA_OIF, nexthop->ifindex);
  1244. if (nexthop->src.ipv4.s_addr)
  1245. addattr_l (nlmsg, req_size, RTA_PREFSRC,
  1246. &nexthop->src.ipv4, bytelen);
  1247. if (IS_ZEBRA_DEBUG_KERNEL)
  1248. zlog_debug("netlink_route_multipath() (%s): "
  1249. "nexthop via if %u", routedesc, nexthop->ifindex);
  1250. }
  1251. if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
  1252. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)
  1253. {
  1254. addattr32 (nlmsg, req_size, RTA_OIF, nexthop->ifindex);
  1255. if (IS_ZEBRA_DEBUG_KERNEL)
  1256. zlog_debug("netlink_route_multipath() (%s): "
  1257. "nexthop via if %u", routedesc, nexthop->ifindex);
  1258. }
  1259. }
  1260. /* This function takes a nexthop as argument and
  1261. * appends to the given rtattr/rtnexthop pair the
  1262. * representation of the nexthop. If the nexthop
  1263. * defines a preferred source, the src parameter
  1264. * will be modified to point to that src, otherwise
  1265. * it will be kept unmodified.
  1266. *
  1267. * @param routedesc: Human readable description of route type
  1268. * (direct/recursive, single-/multipath)
  1269. * @param bytelen: Length of addresses in bytes.
  1270. * @param nexthop: Nexthop information
  1271. * @param rta: rtnetlink attribute structure
  1272. * @param rtnh: pointer to an rtnetlink nexthop structure
  1273. * @param src: pointer pointing to a location where
  1274. * the prefsrc should be stored.
  1275. */
  1276. static void
  1277. _netlink_route_build_multipath(
  1278. const char *routedesc,
  1279. int bytelen,
  1280. struct nexthop *nexthop,
  1281. struct rtattr *rta,
  1282. struct rtnexthop *rtnh,
  1283. union g_addr **src
  1284. )
  1285. {
  1286. rtnh->rtnh_len = sizeof (*rtnh);
  1287. rtnh->rtnh_flags = 0;
  1288. rtnh->rtnh_hops = 0;
  1289. rta->rta_len += rtnh->rtnh_len;
  1290. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ONLINK))
  1291. rtnh->rtnh_flags |= RTNH_F_ONLINK;
  1292. if (nexthop->type == NEXTHOP_TYPE_IPV4
  1293. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  1294. {
  1295. rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTA_GATEWAY,
  1296. &nexthop->gate.ipv4, bytelen);
  1297. rtnh->rtnh_len += sizeof (struct rtattr) + bytelen;
  1298. if (nexthop->src.ipv4.s_addr)
  1299. *src = &nexthop->src;
  1300. if (IS_ZEBRA_DEBUG_KERNEL)
  1301. zlog_debug("netlink_route_multipath() (%s): "
  1302. "nexthop via %s if %u",
  1303. routedesc,
  1304. inet_ntoa (nexthop->gate.ipv4),
  1305. nexthop->ifindex);
  1306. }
  1307. #ifdef HAVE_IPV6
  1308. if (nexthop->type == NEXTHOP_TYPE_IPV6
  1309. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  1310. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  1311. {
  1312. rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTA_GATEWAY,
  1313. &nexthop->gate.ipv6, bytelen);
  1314. rtnh->rtnh_len += sizeof (struct rtattr) + bytelen;
  1315. if (IS_ZEBRA_DEBUG_KERNEL)
  1316. zlog_debug("netlink_route_multipath() (%s): "
  1317. "nexthop via %s if %u",
  1318. routedesc,
  1319. inet6_ntoa (nexthop->gate.ipv6),
  1320. nexthop->ifindex);
  1321. }
  1322. #endif /* HAVE_IPV6 */
  1323. /* ifindex */
  1324. if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
  1325. || nexthop->type == NEXTHOP_TYPE_IFINDEX
  1326. || nexthop->type == NEXTHOP_TYPE_IFNAME)
  1327. {
  1328. rtnh->rtnh_ifindex = nexthop->ifindex;
  1329. if (nexthop->src.ipv4.s_addr)
  1330. *src = &nexthop->src;
  1331. if (IS_ZEBRA_DEBUG_KERNEL)
  1332. zlog_debug("netlink_route_multipath() (%s): "
  1333. "nexthop via if %u", routedesc, nexthop->ifindex);
  1334. }
  1335. else if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  1336. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  1337. {
  1338. rtnh->rtnh_ifindex = nexthop->ifindex;
  1339. if (IS_ZEBRA_DEBUG_KERNEL)
  1340. zlog_debug("netlink_route_multipath() (%s): "
  1341. "nexthop via if %u", routedesc, nexthop->ifindex);
  1342. }
  1343. else
  1344. {
  1345. rtnh->rtnh_ifindex = 0;
  1346. }
  1347. }
  1348. /* Log debug information for netlink_route_multipath
  1349. * if debug logging is enabled.
  1350. *
  1351. * @param cmd: Netlink command which is to be processed
  1352. * @param p: Prefix for which the change is due
  1353. * @param nexthop: Nexthop which is currently processed
  1354. * @param routedesc: Semantic annotation for nexthop
  1355. * (recursive, multipath, etc.)
  1356. * @param family: Address family which the change concerns
  1357. */
  1358. static void
  1359. _netlink_route_debug(
  1360. int cmd,
  1361. struct prefix *p,
  1362. struct nexthop *nexthop,
  1363. const char *routedesc,
  1364. int family,
  1365. struct zebra_vrf *zvrf)
  1366. {
  1367. if (IS_ZEBRA_DEBUG_KERNEL)
  1368. {
  1369. char buf[PREFIX_STRLEN];
  1370. zlog_debug ("netlink_route_multipath() (%s): %s %s vrf %u type %s",
  1371. routedesc,
  1372. lookup (nlmsg_str, cmd),
  1373. prefix2str (p, buf, sizeof(buf)),
  1374. zvrf->vrf_id,
  1375. nexthop_type_to_str (nexthop->type));
  1376. }
  1377. }
  1378. /* Routing table change via netlink interface. */
  1379. static int
  1380. netlink_route_multipath (int cmd, struct prefix *p, struct rib *rib,
  1381. int family)
  1382. {
  1383. int bytelen;
  1384. struct sockaddr_nl snl;
  1385. struct nexthop *nexthop = NULL, *tnexthop;
  1386. int recursing;
  1387. int nexthop_num;
  1388. int discard;
  1389. const char *routedesc;
  1390. struct
  1391. {
  1392. struct nlmsghdr n;
  1393. struct rtmsg r;
  1394. char buf[NL_PKT_BUF_SIZE];
  1395. } req;
  1396. struct zebra_vrf *zvrf = vrf_info_lookup (rib->vrf_id);
  1397. memset (&req, 0, sizeof req - NL_PKT_BUF_SIZE);
  1398. bytelen = (family == AF_INET ? 4 : 16);
  1399. req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
  1400. req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
  1401. req.n.nlmsg_type = cmd;
  1402. req.r.rtm_family = family;
  1403. req.r.rtm_table = rib->table;
  1404. req.r.rtm_dst_len = p->prefixlen;
  1405. req.r.rtm_protocol = RTPROT_ZEBRA;
  1406. req.r.rtm_scope = RT_SCOPE_UNIVERSE;
  1407. if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
  1408. discard = 1;
  1409. else
  1410. discard = 0;
  1411. if (cmd == RTM_NEWROUTE)
  1412. {
  1413. if (discard)
  1414. {
  1415. if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
  1416. req.r.rtm_type = RTN_BLACKHOLE;
  1417. else if (rib->flags & ZEBRA_FLAG_REJECT)
  1418. req.r.rtm_type = RTN_UNREACHABLE;
  1419. else
  1420. assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
  1421. }
  1422. else
  1423. req.r.rtm_type = RTN_UNICAST;
  1424. }
  1425. addattr_l (&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen);
  1426. /* Metric. */
  1427. addattr32 (&req.n, sizeof req, RTA_PRIORITY, rib->metric);
  1428. if (rib->mtu || rib->nexthop_mtu)
  1429. {
  1430. char buf[NL_PKT_BUF_SIZE];
  1431. struct rtattr *rta = (void *) buf;
  1432. u_int32_t mtu = rib->mtu;
  1433. if (!mtu || (rib->nexthop_mtu && rib->nexthop_mtu < mtu))
  1434. mtu = rib->nexthop_mtu;
  1435. rta->rta_type = RTA_METRICS;
  1436. rta->rta_len = RTA_LENGTH(0);
  1437. rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTAX_MTU, &mtu, sizeof mtu);
  1438. addattr_l (&req.n, NL_PKT_BUF_SIZE, RTA_METRICS, RTA_DATA (rta),
  1439. RTA_PAYLOAD (rta));
  1440. }
  1441. if (discard)
  1442. {
  1443. if (cmd == RTM_NEWROUTE)
  1444. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  1445. {
  1446. /* We shouldn't encounter recursive nexthops on discard routes,
  1447. * but it is probably better to handle that case correctly anyway.
  1448. */
  1449. if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  1450. continue;
  1451. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1452. }
  1453. goto skip;
  1454. }
  1455. /* Count overall nexthops so we can decide whether to use singlepath
  1456. * or multipath case. */
  1457. nexthop_num = 0;
  1458. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  1459. {
  1460. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  1461. continue;
  1462. if (cmd == RTM_NEWROUTE && !CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  1463. continue;
  1464. if (cmd == RTM_DELROUTE && !CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  1465. continue;
  1466. nexthop_num++;
  1467. }
  1468. /* Singlepath case. */
  1469. if (nexthop_num == 1 || MULTIPATH_NUM == 1)
  1470. {
  1471. nexthop_num = 0;
  1472. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  1473. {
  1474. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  1475. continue;
  1476. if ((cmd == RTM_NEWROUTE
  1477. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  1478. || (cmd == RTM_DELROUTE
  1479. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
  1480. {
  1481. routedesc = recursing ? "recursive, 1 hop" : "single hop";
  1482. _netlink_route_debug(cmd, p, nexthop, routedesc, family, zvrf);
  1483. _netlink_route_build_singlepath(routedesc, bytelen,
  1484. nexthop, &req.n, &req.r,
  1485. sizeof req);
  1486. if (cmd == RTM_NEWROUTE)
  1487. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1488. nexthop_num++;
  1489. break;
  1490. }
  1491. }
  1492. }
  1493. else
  1494. {
  1495. char buf[NL_PKT_BUF_SIZE];
  1496. struct rtattr *rta = (void *) buf;
  1497. struct rtnexthop *rtnh;
  1498. union g_addr *src = NULL;
  1499. rta->rta_type = RTA_MULTIPATH;
  1500. rta->rta_len = RTA_LENGTH (0);
  1501. rtnh = RTA_DATA (rta);
  1502. nexthop_num = 0;
  1503. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  1504. {
  1505. if (MULTIPATH_NUM != 0 && nexthop_num >= MULTIPATH_NUM)
  1506. break;
  1507. if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  1508. continue;
  1509. if ((cmd == RTM_NEWROUTE
  1510. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  1511. || (cmd == RTM_DELROUTE
  1512. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
  1513. {
  1514. routedesc = recursing ? "recursive, multihop" : "multihop";
  1515. nexthop_num++;
  1516. _netlink_route_debug(cmd, p, nexthop,
  1517. routedesc, family, zvrf);
  1518. _netlink_route_build_multipath(routedesc, bytelen,
  1519. nexthop, rta, rtnh, &src);
  1520. rtnh = RTNH_NEXT (rtnh);
  1521. if (cmd == RTM_NEWROUTE)
  1522. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  1523. }
  1524. }
  1525. if (src)
  1526. addattr_l (&req.n, sizeof req, RTA_PREFSRC, &src->ipv4, bytelen);
  1527. if (rta->rta_len > RTA_LENGTH (0))
  1528. addattr_l (&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH, RTA_DATA (rta),
  1529. RTA_PAYLOAD (rta));
  1530. }
  1531. /* If there is no useful nexthop then return. */
  1532. if (nexthop_num == 0)
  1533. {
  1534. if (IS_ZEBRA_DEBUG_KERNEL)
  1535. zlog_debug ("netlink_route_multipath(): No useful nexthop.");
  1536. return 0;
  1537. }
  1538. skip:
  1539. /* Destination netlink address. */
  1540. memset (&snl, 0, sizeof snl);
  1541. snl.nl_family = AF_NETLINK;
  1542. /* Talk to netlink socket. */
  1543. return netlink_talk (&req.n, &zvrf->netlink_cmd, zvrf);
  1544. }
  1545. int
  1546. kernel_add_ipv4 (struct prefix *p, struct rib *rib)
  1547. {
  1548. return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET);
  1549. }
  1550. int
  1551. kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
  1552. {
  1553. return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET);
  1554. }
  1555. #ifdef HAVE_IPV6
  1556. int
  1557. kernel_add_ipv6 (struct prefix *p, struct rib *rib)
  1558. {
  1559. return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET6);
  1560. }
  1561. int
  1562. kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
  1563. {
  1564. return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET6);
  1565. }
  1566. #endif /* HAVE_IPV6 */
  1567. /* Interface address modification. */
  1568. static int
  1569. netlink_address (int cmd, int family, struct interface *ifp,
  1570. struct connected *ifc)
  1571. {
  1572. int bytelen;
  1573. struct prefix *p;
  1574. struct
  1575. {
  1576. struct nlmsghdr n;
  1577. struct ifaddrmsg ifa;
  1578. char buf[NL_PKT_BUF_SIZE];
  1579. } req;
  1580. struct zebra_vrf *zvrf = vrf_info_lookup (ifp->vrf_id);
  1581. p = ifc->address;
  1582. memset (&req, 0, sizeof req - NL_PKT_BUF_SIZE);
  1583. bytelen = (family == AF_INET ? 4 : 16);
  1584. req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg));
  1585. req.n.nlmsg_flags = NLM_F_REQUEST;
  1586. req.n.nlmsg_type = cmd;
  1587. req.ifa.ifa_family = family;
  1588. req.ifa.ifa_index = ifp->ifindex;
  1589. req.ifa.ifa_prefixlen = p->prefixlen;
  1590. addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
  1591. if (family == AF_INET && cmd == RTM_NEWADDR)
  1592. {
  1593. if (!CONNECTED_PEER(ifc) && ifc->destination)
  1594. {
  1595. p = ifc->destination;
  1596. addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix,
  1597. bytelen);
  1598. }
  1599. }
  1600. if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
  1601. SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY);
  1602. if (ifc->label)
  1603. addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label,
  1604. strlen (ifc->label) + 1);
  1605. return netlink_talk (&req.n, &zvrf->netlink_cmd, zvrf);
  1606. }
  1607. int
  1608. kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc)
  1609. {
  1610. return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc);
  1611. }
  1612. int
  1613. kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc)
  1614. {
  1615. return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc);
  1616. }
  1617. extern struct thread_master *master;
  1618. /* Kernel route reflection. */
  1619. static int
  1620. kernel_read (struct thread *thread)
  1621. {
  1622. struct zebra_vrf *zvrf = (struct zebra_vrf *)THREAD_ARG (thread);
  1623. netlink_parse_info (netlink_information_fetch, &zvrf->netlink, zvrf);
  1624. zvrf->t_netlink = thread_add_read (zebrad.master, kernel_read, zvrf,
  1625. zvrf->netlink.sock);
  1626. return 0;
  1627. }
  1628. /* Filter out messages from self that occur on listener socket,
  1629. caused by our actions on the command socket
  1630. */
  1631. static void netlink_install_filter (int sock, __u32 pid)
  1632. {
  1633. struct sock_filter filter[] = {
  1634. /* 0: ldh [4] */
  1635. BPF_STMT(BPF_LD|BPF_ABS|BPF_H, offsetof(struct nlmsghdr, nlmsg_type)),
  1636. /* 1: jeq 0x18 jt 3 jf 6 */
  1637. BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(RTM_NEWROUTE), 1, 0),
  1638. /* 2: jeq 0x19 jt 3 jf 6 */
  1639. BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(RTM_DELROUTE), 0, 3),
  1640. /* 3: ldw [12] */
  1641. BPF_STMT(BPF_LD|BPF_ABS|BPF_W, offsetof(struct nlmsghdr, nlmsg_pid)),
  1642. /* 4: jeq XX jt 5 jf 6 */
  1643. BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htonl(pid), 0, 1),
  1644. /* 5: ret 0 (skip) */
  1645. BPF_STMT(BPF_RET|BPF_K, 0),
  1646. /* 6: ret 0xffff (keep) */
  1647. BPF_STMT(BPF_RET|BPF_K, 0xffff),
  1648. };
  1649. struct sock_fprog prog = {
  1650. .len = array_size(filter),
  1651. .filter = filter,
  1652. };
  1653. if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)) < 0)
  1654. zlog_warn ("Can't install socket filter: %s\n", safe_strerror(errno));
  1655. }
  1656. /* Exported interface function. This function simply calls
  1657. netlink_socket (). */
  1658. void
  1659. kernel_init (struct zebra_vrf *zvrf)
  1660. {
  1661. unsigned long groups;
  1662. groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR;
  1663. #ifdef HAVE_IPV6
  1664. groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR;
  1665. #endif /* HAVE_IPV6 */
  1666. netlink_socket (&zvrf->netlink, groups, zvrf->vrf_id);
  1667. netlink_socket (&zvrf->netlink_cmd, 0, zvrf->vrf_id);
  1668. /* Register kernel socket. */
  1669. if (zvrf->netlink.sock > 0)
  1670. {
  1671. /* Only want non-blocking on the netlink event socket */
  1672. if (fcntl (zvrf->netlink.sock, F_SETFL, O_NONBLOCK) < 0)
  1673. zlog_err ("Can't set %s socket flags: %s", zvrf->netlink.name,
  1674. safe_strerror (errno));
  1675. /* Set receive buffer size if it's set from command line */
  1676. if (nl_rcvbufsize)
  1677. netlink_recvbuf (&zvrf->netlink, nl_rcvbufsize);
  1678. netlink_install_filter (zvrf->netlink.sock, zvrf->netlink_cmd.snl.nl_pid);
  1679. zvrf->t_netlink = thread_add_read (zebrad.master, kernel_read, zvrf,
  1680. zvrf->netlink.sock);
  1681. }
  1682. }
  1683. void
  1684. kernel_terminate (struct zebra_vrf *zvrf)
  1685. {
  1686. THREAD_READ_OFF (zvrf->t_netlink);
  1687. if (zvrf->netlink.sock >= 0)
  1688. {
  1689. close (zvrf->netlink.sock);
  1690. zvrf->netlink.sock = -1;
  1691. }
  1692. if (zvrf->netlink_cmd.sock >= 0)
  1693. {
  1694. close (zvrf->netlink_cmd.sock);
  1695. zvrf->netlink_cmd.sock = -1;
  1696. }
  1697. }
  1698. /*
  1699. * nl_msg_type_to_str
  1700. */
  1701. const char *
  1702. nl_msg_type_to_str (uint16_t msg_type)
  1703. {
  1704. return lookup (nlmsg_str, msg_type);
  1705. }
  1706. /*
  1707. * nl_rtproto_to_str
  1708. */
  1709. const char *
  1710. nl_rtproto_to_str (u_char rtproto)
  1711. {
  1712. return lookup (rtproto_str, rtproto);
  1713. }