rt_netlink.c 56 KB

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