rt_netlink.c 54 KB

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