rt_netlink.c 58 KB

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