rt_netlink.c 58 KB

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