rt_netlink.c 55 KB

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