rt_netlink.c 58 KB

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