main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License v2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. */
  18. #include <sys/ioctl.h>
  19. #include <sys/socket.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <stddef.h>
  24. #include <stdlib.h>
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include <errno.h>
  28. #include <signal.h>
  29. #include <string.h>
  30. #include "relayd.h"
  31. static LIST_HEAD(pending_routes);
  32. LIST_HEAD(interfaces);
  33. int debug;
  34. static int host_timeout;
  35. static int host_ping_tries;
  36. static int inet_sock;
  37. static int forward_bcast;
  38. static int forward_dhcp;
  39. static int parse_dhcp;
  40. uint8_t local_addr[4];
  41. int local_route_table;
  42. struct relayd_pending_route {
  43. struct relayd_route rt;
  44. struct uloop_timeout timeout;
  45. uint8_t gateway[4];
  46. };
  47. static struct relayd_host *find_host_by_ipaddr(struct relayd_interface *rif, const uint8_t *ipaddr)
  48. {
  49. struct relayd_host *host;
  50. if (!rif) {
  51. list_for_each_entry(rif, &interfaces, list) {
  52. host = find_host_by_ipaddr(rif, ipaddr);
  53. if (!host)
  54. continue;
  55. return host;
  56. }
  57. return NULL;
  58. }
  59. list_for_each_entry(host, &rif->hosts, list) {
  60. if (memcmp(ipaddr, host->ipaddr, sizeof(host->ipaddr)) != 0)
  61. continue;
  62. return host;
  63. }
  64. return NULL;
  65. }
  66. static void add_arp(struct relayd_host *host)
  67. {
  68. struct sockaddr_in *sin;
  69. struct arpreq arp;
  70. strncpy(arp.arp_dev, host->rif->ifname, sizeof(arp.arp_dev));
  71. arp.arp_flags = ATF_COM;
  72. arp.arp_ha.sa_family = ARPHRD_ETHER;
  73. memcpy(arp.arp_ha.sa_data, host->lladdr, ETH_ALEN);
  74. sin = (struct sockaddr_in *) &arp.arp_pa;
  75. sin->sin_family = AF_INET;
  76. memcpy(&sin->sin_addr, host->ipaddr, sizeof(host->ipaddr));
  77. ioctl(inet_sock, SIOCSARP, &arp);
  78. }
  79. static void timeout_host_route(struct uloop_timeout *timeout)
  80. {
  81. struct relayd_pending_route *rt;
  82. rt = container_of(timeout, struct relayd_pending_route, timeout);
  83. list_del(&rt->rt.list);
  84. free(rt);
  85. }
  86. void relayd_add_host_route(struct relayd_host *host, const uint8_t *dest, uint8_t mask)
  87. {
  88. struct relayd_route *rt;
  89. list_for_each_entry(rt, &host->routes, list) {
  90. if (!memcmp(rt->dest, dest, sizeof(rt->dest)) && rt->mask == mask)
  91. return;
  92. }
  93. rt = calloc(1, sizeof(*rt));
  94. if (!rt)
  95. return;
  96. list_add(&rt->list, &host->routes);
  97. memcpy(rt->dest, dest, sizeof(rt->dest));
  98. rt->mask = mask;
  99. relayd_add_route(host, rt);
  100. }
  101. static void del_host(struct relayd_host *host)
  102. {
  103. struct relayd_route *route, *tmp;
  104. DPRINTF(1, "%s: deleting host "IP_FMT" ("MAC_FMT")\n", host->rif->ifname,
  105. IP_BUF(host->ipaddr), MAC_BUF(host->lladdr));
  106. list_for_each_entry_safe(route, tmp, &host->routes, list) {
  107. relayd_del_route(host, route);
  108. list_del(&route->list);
  109. free(route);
  110. }
  111. if (host->rif->managed)
  112. relayd_del_route(host, NULL);
  113. uloop_timeout_cancel(&host->timeout);
  114. list_del(&host->list);
  115. free(host);
  116. }
  117. static void fill_arp_packet(struct arp_packet *pkt, struct relayd_interface *rif,
  118. const uint8_t spa[4], const uint8_t tpa[4])
  119. {
  120. memset(pkt, 0, sizeof(*pkt));
  121. pkt->eth.ether_type = htons(ETHERTYPE_ARP);
  122. memcpy(pkt->eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
  123. memcpy(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
  124. memcpy(pkt->arp.arp_spa, spa, 4);
  125. memcpy(pkt->arp.arp_tpa, tpa, 4);
  126. pkt->arp.arp_hrd = htons(ARPHRD_ETHER);
  127. pkt->arp.arp_pro = htons(ETH_P_IP);
  128. pkt->arp.arp_hln = ETH_ALEN;
  129. pkt->arp.arp_pln = 4;
  130. }
  131. static void send_arp_request(struct relayd_interface *rif, const uint8_t *ipaddr)
  132. {
  133. struct arp_packet pkt;
  134. fill_arp_packet(&pkt, rif, rif->src_ip, ipaddr);
  135. pkt.arp.arp_op = htons(ARPOP_REQUEST);
  136. memcpy(pkt.arp.arp_spa, rif->src_ip, sizeof(pkt.arp.arp_spa));
  137. memset(pkt.arp.arp_tha, 0, ETH_ALEN);
  138. memset(pkt.eth.ether_dhost, 0xff, ETH_ALEN);
  139. DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
  140. rif->ifname, IP_BUF(pkt.arp.arp_tpa),
  141. IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
  142. sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
  143. (struct sockaddr *) &rif->sll, sizeof(rif->sll));
  144. }
  145. void relayd_add_pending_route(const uint8_t *gateway, const uint8_t *dest, uint8_t mask, int timeout)
  146. {
  147. struct relayd_pending_route *rt;
  148. struct relayd_interface *rif;
  149. struct relayd_host *host;
  150. host = find_host_by_ipaddr(NULL, gateway);
  151. if (host) {
  152. relayd_add_host_route(host, dest, mask);
  153. return;
  154. }
  155. rt = calloc(1, sizeof(*rt));
  156. if (!rt)
  157. return;
  158. memcpy(rt->gateway, gateway, sizeof(rt->gateway));
  159. memcpy(rt->rt.dest, dest, sizeof(rt->rt.dest));
  160. rt->rt.mask = mask;
  161. list_add(&rt->rt.list, &pending_routes);
  162. if (timeout <= 0)
  163. return;
  164. rt->timeout.cb = timeout_host_route;
  165. uloop_timeout_set(&rt->timeout, 10000);
  166. list_for_each_entry(rif, &interfaces, list) {
  167. send_arp_request(rif, gateway);
  168. }
  169. }
  170. static void send_arp_reply(struct relayd_interface *rif, const uint8_t spa[4],
  171. const uint8_t tha[ETH_ALEN], const uint8_t tpa[4])
  172. {
  173. struct arp_packet pkt;
  174. fill_arp_packet(&pkt, rif, spa, tpa);
  175. if (tha) {
  176. pkt.arp.arp_op = htons(ARPOP_REPLY);
  177. memcpy(pkt.eth.ether_dhost, tha, ETH_ALEN);
  178. memcpy(pkt.arp.arp_tha, tha, ETH_ALEN);
  179. DPRINTF(2, "%s: sending ARP reply to "IP_FMT", "IP_FMT" is at ("MAC_FMT")\n",
  180. rif->ifname, IP_BUF(pkt.arp.arp_tpa),
  181. IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
  182. } else {
  183. pkt.arp.arp_op = htons(ARPOP_REQUEST);
  184. memset(pkt.eth.ether_dhost, 0xff, ETH_ALEN);
  185. memset(pkt.arp.arp_tha, 0xff, ETH_ALEN);
  186. DPRINTF(2, "%s: sending gratuitous ARP: "IP_FMT" is at ("MAC_FMT")\n",
  187. rif->ifname, IP_BUF(pkt.arp.arp_tpa),
  188. MAC_BUF(pkt.eth.ether_shost));
  189. }
  190. sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
  191. (struct sockaddr *) &rif->sll, sizeof(rif->sll));
  192. if (tha)
  193. return;
  194. /*
  195. * Gratuitous ARP comes in two flavours, request and reply.
  196. * Some operating systems only accept request, some only reply.
  197. * Let's just send both...
  198. */
  199. pkt.arp.arp_op = htons(ARPOP_REPLY);
  200. sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
  201. (struct sockaddr *) &rif->sll, sizeof(rif->sll));
  202. }
  203. static void host_entry_timeout(struct uloop_timeout *timeout)
  204. {
  205. struct relayd_host *host = container_of(timeout, struct relayd_host, timeout);
  206. struct relayd_interface *rif;
  207. /*
  208. * When a host is behind a managed interface, we must not expire its host
  209. * entry prematurely, as this will cause routes to the node to expire,
  210. * leading to loss of connectivity from the other side.
  211. * When the timeout is reached, try pinging the host a few times before
  212. * giving up on it.
  213. */
  214. if (host->rif->managed && host->cleanup_pending < host_ping_tries) {
  215. list_for_each_entry(rif, &interfaces, list) {
  216. send_arp_request(rif, host->ipaddr);
  217. }
  218. host->cleanup_pending++;
  219. uloop_timeout_set(&host->timeout, 1000);
  220. return;
  221. }
  222. del_host(host);
  223. }
  224. static struct relayd_host *add_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
  225. {
  226. struct relayd_host *host;
  227. struct relayd_pending_route *route, *rtmp;
  228. DPRINTF(1, "%s: adding host "IP_FMT" ("MAC_FMT")\n", rif->ifname,
  229. IP_BUF(ipaddr), MAC_BUF(lladdr));
  230. host = calloc(1, sizeof(*host));
  231. INIT_LIST_HEAD(&host->routes);
  232. host->rif = rif;
  233. memcpy(host->ipaddr, ipaddr, sizeof(host->ipaddr));
  234. memcpy(host->lladdr, lladdr, sizeof(host->lladdr));
  235. list_add(&host->list, &rif->hosts);
  236. host->timeout.cb = host_entry_timeout;
  237. uloop_timeout_set(&host->timeout, host_timeout * 1000);
  238. add_arp(host);
  239. if (rif->managed)
  240. relayd_add_route(host, NULL);
  241. list_for_each_entry_safe(route, rtmp, &pending_routes, rt.list) {
  242. if (memcmp(route->gateway, ipaddr, 4) != 0)
  243. continue;
  244. relayd_add_host_route(host, route->rt.dest, route->rt.mask);
  245. if (!route->timeout.pending)
  246. continue;
  247. uloop_timeout_cancel(&route->timeout);
  248. list_del(&route->rt.list);
  249. free(route);
  250. }
  251. return host;
  252. }
  253. static void send_gratuitous_arp(struct relayd_interface *rif, const uint8_t *spa)
  254. {
  255. struct relayd_interface *to_rif;
  256. list_for_each_entry(to_rif, &interfaces, list) {
  257. if (rif == to_rif)
  258. continue;
  259. send_arp_reply(to_rif, spa, NULL, spa);
  260. }
  261. }
  262. struct relayd_host *relayd_refresh_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
  263. {
  264. struct relayd_host *host;
  265. host = find_host_by_ipaddr(rif, ipaddr);
  266. if (!host) {
  267. host = find_host_by_ipaddr(NULL, ipaddr);
  268. /*
  269. * When we suddenly see the host appearing on a different interface,
  270. * reduce the timeout to make the old entry expire faster, in case the
  271. * host has moved.
  272. * If the old entry is behind a managed interface, it will be pinged
  273. * before we expire it
  274. */
  275. if (host && !host->cleanup_pending) {
  276. uloop_timeout_set(&host->timeout, 1);
  277. return NULL;
  278. }
  279. host = add_host(rif, lladdr, ipaddr);
  280. } else {
  281. host->cleanup_pending = false;
  282. uloop_timeout_set(&host->timeout, host_timeout * 1000);
  283. send_gratuitous_arp(rif, ipaddr);
  284. }
  285. return host;
  286. }
  287. static void relay_arp_request(struct relayd_interface *from_rif, struct arp_packet *pkt)
  288. {
  289. struct relayd_interface *rif;
  290. struct arp_packet reqpkt;
  291. memcpy(&reqpkt, pkt, sizeof(reqpkt));
  292. list_for_each_entry(rif, &interfaces, list) {
  293. if (rif == from_rif)
  294. continue;
  295. memcpy(reqpkt.eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
  296. memset(reqpkt.eth.ether_dhost, 0xff, ETH_ALEN);
  297. memcpy(reqpkt.arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
  298. memset(reqpkt.arp.arp_tha, 0, ETH_ALEN);
  299. DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
  300. rif->ifname, IP_BUF(reqpkt.arp.arp_tpa),
  301. IP_BUF(reqpkt.arp.arp_spa), MAC_BUF(reqpkt.eth.ether_shost));
  302. sendto(rif->fd.fd, &reqpkt, sizeof(reqpkt), 0,
  303. (struct sockaddr *) &rif->sll, sizeof(rif->sll));
  304. }
  305. }
  306. static void recv_arp_request(struct relayd_interface *rif, struct arp_packet *pkt)
  307. {
  308. struct relayd_host *host;
  309. DPRINTF(2, "%s: ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
  310. rif->ifname,
  311. IP_BUF(pkt->arp.arp_tpa),
  312. IP_BUF(pkt->arp.arp_spa),
  313. MAC_BUF(pkt->eth.ether_shost));
  314. if (!memcmp(pkt->arp.arp_spa, "\x00\x00\x00\x00", 4))
  315. return;
  316. host = find_host_by_ipaddr(NULL, pkt->arp.arp_spa);
  317. if (!host || host->rif != rif)
  318. relayd_refresh_host(rif, pkt->eth.ether_shost, pkt->arp.arp_spa);
  319. if (local_route_table && !memcmp(pkt->arp.arp_tpa, local_addr, sizeof(local_addr))) {
  320. send_arp_reply(rif, local_addr, pkt->arp.arp_sha, pkt->arp.arp_spa);
  321. return;
  322. }
  323. host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
  324. /*
  325. * If a host is being pinged because of a timeout, do not use the cached
  326. * entry here. That way we can avoid giving out stale data in case the node
  327. * has moved. We shouldn't relay requests here either, as we might miss our
  328. * chance to create a host route.
  329. */
  330. if (host && host->cleanup_pending)
  331. return;
  332. relay_arp_request(rif, pkt);
  333. }
  334. static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt)
  335. {
  336. struct relayd_host *host;
  337. DPRINTF(2, "%s: received ARP reply for "IP_FMT" from "MAC_FMT", deliver to "IP_FMT"\n",
  338. rif->ifname,
  339. IP_BUF(pkt->arp.arp_spa),
  340. MAC_BUF(pkt->eth.ether_shost),
  341. IP_BUF(pkt->arp.arp_tpa));
  342. if (memcmp(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN) != 0)
  343. relayd_refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
  344. host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
  345. if (!host)
  346. return;
  347. if (host->rif == rif)
  348. return;
  349. send_arp_reply(host->rif, pkt->arp.arp_spa, host->lladdr, host->ipaddr);
  350. }
  351. static void recv_packet(struct uloop_fd *fd, unsigned int events)
  352. {
  353. struct relayd_interface *rif = container_of(fd, struct relayd_interface, fd);
  354. struct arp_packet *pkt;
  355. static char pktbuf[4096];
  356. int pktlen;
  357. do {
  358. if (rif->fd.error)
  359. uloop_end();
  360. pktlen = recv(rif->fd.fd, pktbuf, sizeof(pktbuf), 0);
  361. if (pktlen < 0) {
  362. if (errno == EINTR)
  363. continue;
  364. break;
  365. }
  366. if (!pktlen)
  367. break;
  368. pkt = (void *)pktbuf;
  369. if (pkt->arp.arp_op == htons(ARPOP_REPLY))
  370. recv_arp_reply(rif, pkt);
  371. else if (pkt->arp.arp_op == htons(ARPOP_REQUEST))
  372. recv_arp_request(rif, pkt);
  373. else
  374. DPRINTF(1, "received unknown packet type: %04x\n", ntohs(pkt->arp.arp_op));
  375. } while (1);
  376. }
  377. void relayd_forward_bcast_packet(struct relayd_interface *from_rif, void *packet, int len)
  378. {
  379. struct relayd_interface *rif;
  380. struct ether_header *eth = packet;
  381. list_for_each_entry(rif, &interfaces, list) {
  382. if (rif == from_rif)
  383. continue;
  384. DPRINTF(3, "%s: forwarding broadcast packet to %s\n", from_rif->ifname, rif->ifname);
  385. memcpy(eth->ether_shost, rif->sll.sll_addr, ETH_ALEN);
  386. send(rif->bcast_fd.fd, packet, len, 0);
  387. }
  388. }
  389. static void recv_bcast_packet(struct uloop_fd *fd, unsigned int events)
  390. {
  391. struct relayd_interface *rif = container_of(fd, struct relayd_interface, bcast_fd);
  392. static char pktbuf[4096];
  393. int pktlen;
  394. do {
  395. if (rif->fd.error)
  396. uloop_end();
  397. pktlen = recv(rif->bcast_fd.fd, pktbuf, sizeof(pktbuf), 0);
  398. if (pktlen < 0) {
  399. if (errno == EINTR)
  400. continue;
  401. break;
  402. }
  403. if (!pktlen)
  404. break;
  405. if (!forward_bcast && !forward_dhcp)
  406. continue;
  407. if (relayd_handle_dhcp_packet(rif, pktbuf, pktlen, forward_dhcp, parse_dhcp))
  408. continue;
  409. if (forward_bcast)
  410. relayd_forward_bcast_packet(rif, pktbuf, pktlen);
  411. } while (1);
  412. }
  413. static int init_interface(struct relayd_interface *rif)
  414. {
  415. struct sockaddr_ll *sll = &rif->sll;
  416. struct sockaddr_in *sin;
  417. struct ifreq ifr;
  418. int fd = rif->fd.fd;
  419. #ifdef PACKET_RECV_TYPE
  420. unsigned int pkt_type;
  421. #endif
  422. fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
  423. if (fd < 0)
  424. return -1;
  425. rif->fd.fd = fd;
  426. memset(&ifr, 0, sizeof(ifr));
  427. strcpy(ifr.ifr_name, rif->ifname);
  428. if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
  429. perror("ioctl(SIOCGIFHWADDR)");
  430. return -1;
  431. }
  432. memcpy(sll->sll_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  433. sll->sll_family = AF_PACKET;
  434. sll->sll_protocol = htons(ETH_P_ARP);
  435. sll->sll_pkttype = PACKET_BROADCAST;
  436. sll->sll_hatype = ARPHRD_ETHER;
  437. sll->sll_halen = ETH_ALEN;
  438. if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
  439. perror("ioctl(SIOCGIFINDEX)");
  440. return -1;
  441. }
  442. sll->sll_ifindex = ifr.ifr_ifindex;
  443. if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
  444. memcpy(rif->src_ip, DUMMY_IP, sizeof(rif->src_ip));
  445. } else {
  446. sin = (struct sockaddr_in *) &ifr.ifr_addr;
  447. memcpy(rif->src_ip, &sin->sin_addr.s_addr, sizeof(rif->src_ip));
  448. }
  449. if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
  450. perror("bind(ETH_P_ARP)");
  451. return -1;
  452. }
  453. rif->fd.cb = recv_packet;
  454. uloop_fd_add(&rif->fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  455. if (!forward_bcast && !forward_dhcp)
  456. return 0;
  457. fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
  458. if (fd < 0)
  459. return 0;
  460. rif->bcast_fd.fd = fd;
  461. rif->bcast_fd.cb = recv_bcast_packet;
  462. memcpy(&rif->bcast_sll, &rif->sll, sizeof(rif->bcast_sll));
  463. sll = &rif->bcast_sll;
  464. sll->sll_protocol = htons(ETH_P_IP);
  465. if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
  466. perror("bind(ETH_P_IP)");
  467. return 0;
  468. }
  469. #ifdef PACKET_RECV_TYPE
  470. pkt_type = (1 << PACKET_BROADCAST) | (1 << PACKET_MULTICAST);
  471. setsockopt(fd, SOL_PACKET, PACKET_RECV_TYPE, &pkt_type, sizeof(pkt_type));
  472. #endif
  473. uloop_fd_add(&rif->bcast_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  474. relayd_add_interface_routes(rif);
  475. return 0;
  476. }
  477. static void ping_static_routes(void)
  478. {
  479. struct relayd_pending_route *rt;
  480. struct relayd_interface *rif;
  481. list_for_each_entry(rt, &pending_routes, rt.list)
  482. list_for_each_entry(rif, &interfaces, list)
  483. send_arp_request(rif, rt->gateway);
  484. }
  485. static int init_interfaces(void)
  486. {
  487. struct relayd_interface *rif;
  488. int ret;
  489. list_for_each_entry(rif, &interfaces, list) {
  490. ret = init_interface(rif);
  491. if (ret < 0)
  492. return ret;
  493. }
  494. return 0;
  495. }
  496. static void cleanup_hosts(void)
  497. {
  498. struct relayd_interface *rif;
  499. struct relayd_host *host, *tmp;
  500. list_for_each_entry(rif, &interfaces, list) {
  501. list_for_each_entry_safe(host, tmp, &rif->hosts, list) {
  502. del_host(host);
  503. }
  504. }
  505. }
  506. static void free_interfaces(void)
  507. {
  508. struct relayd_interface *rif, *rtmp;
  509. list_for_each_entry_safe(rif, rtmp, &interfaces, list) {
  510. relayd_del_interface_routes(rif);
  511. list_del(&rif->list);
  512. free(rif);
  513. }
  514. }
  515. static struct relayd_interface *alloc_interface(const char *ifname, bool managed)
  516. {
  517. struct relayd_interface *rif;
  518. if (strlen(ifname) >= IFNAMSIZ)
  519. return NULL;
  520. list_for_each_entry(rif, &interfaces, list) {
  521. if (!strncmp(rif->ifname, ifname, IFNAMSIZ))
  522. return rif;
  523. }
  524. rif = calloc(1, sizeof(*rif));
  525. if (!rif)
  526. return NULL;
  527. INIT_LIST_HEAD(&rif->hosts);
  528. strcpy(rif->ifname, ifname);
  529. list_add(&rif->list, &interfaces);
  530. rif->managed = managed;
  531. return rif;
  532. }
  533. static void die(int signo)
  534. {
  535. /*
  536. * When we hit SIGTERM, clean up interfaces directly, so that we
  537. * won't leave our routing in an invalid state.
  538. */
  539. uloop_end();
  540. }
  541. static int usage(const char *progname)
  542. {
  543. fprintf(stderr, "Usage: %s <options>\n"
  544. "\n"
  545. "Options:\n"
  546. " -d Enable debug messages\n"
  547. " -i <ifname> Add an interface for relaying\n"
  548. " -I <ifname> Same as -i, except with ARP cache and host route management\n"
  549. " You need to specify at least two interfaces\n"
  550. " -G <ip> Set a gateway IP for clients\n"
  551. " -R <gateway>:<net>/<mask>\n"
  552. " Add a static route for <net>/<mask> via <gateway>\n"
  553. " -t <timeout> Host entry expiry timeout\n"
  554. " -p <tries> Number of ARP ping attempts before considering a host dead\n"
  555. " -T <table> Set routing table number for automatically added routes\n"
  556. " -B Enable broadcast forwarding\n"
  557. " -D Enable DHCP forwarding\n"
  558. " -P Disable DHCP options parsing\n"
  559. " -L <ipaddr> Enable local access using <ipaddr> as source address\n"
  560. "\n",
  561. progname);
  562. return -1;
  563. }
  564. int main(int argc, char **argv)
  565. {
  566. struct relayd_interface *rif = NULL;
  567. struct in_addr addr, addr2;
  568. bool local_addr_valid = false;
  569. bool managed = false;
  570. int ifnum = 0;
  571. char *s, *s2;
  572. int mask;
  573. int ch;
  574. debug = 0;
  575. inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
  576. if (inet_sock < 0) {
  577. perror("socket(AF_INET)");
  578. return 1;
  579. }
  580. host_timeout = 30;
  581. host_ping_tries = 5;
  582. forward_bcast = 0;
  583. local_route_table = 0;
  584. parse_dhcp = 1;
  585. uloop_init();
  586. while ((ch = getopt(argc, argv, "I:i:t:p:BDPdT:G:R:L:")) != -1) {
  587. switch(ch) {
  588. case 'I':
  589. managed = true;
  590. /* fall through */
  591. case 'i':
  592. ifnum++;
  593. rif = alloc_interface(optarg, managed);
  594. if (!rif)
  595. return 1;
  596. managed = false;
  597. break;
  598. case 't':
  599. host_timeout = atoi(optarg);
  600. if (host_timeout <= 0)
  601. return usage(argv[0]);
  602. break;
  603. case 'p':
  604. host_ping_tries = atoi(optarg);
  605. if (host_ping_tries <= 0)
  606. return usage(argv[0]);
  607. break;
  608. case 'd':
  609. debug++;
  610. break;
  611. case 'B':
  612. forward_bcast = 1;
  613. break;
  614. case 'D':
  615. forward_dhcp = 1;
  616. break;
  617. case 'P':
  618. parse_dhcp = 0;
  619. break;
  620. case 'T':
  621. route_table = atoi(optarg);
  622. if (route_table <= 0)
  623. return usage(argv[0]);
  624. break;
  625. case 'G':
  626. if (!inet_aton(optarg, &addr)) {
  627. fprintf(stderr, "Address '%s' not found\n", optarg);
  628. return 1;
  629. }
  630. relayd_add_pending_route((uint8_t *) &addr.s_addr, (const uint8_t *) "\x00\x00\x00\x00", 0, 0);
  631. break;
  632. case 'L':
  633. if (!inet_aton(optarg, &addr)) {
  634. fprintf(stderr, "Address '%s' not found\n", optarg);
  635. return 1;
  636. }
  637. memcpy(&local_addr, &addr.s_addr, sizeof(local_addr));
  638. local_addr_valid = true;
  639. break;
  640. case 'R':
  641. s = strchr(optarg, ':');
  642. if (!s)
  643. return usage(argv[0]);
  644. *(s++) = 0;
  645. if (!inet_aton(optarg, &addr)) {
  646. fprintf(stderr, "Address '%s' not found\n", optarg);
  647. return 1;
  648. }
  649. s2 = strchr(s, '/');
  650. if (!s2)
  651. return usage(argv[0]);
  652. *(s2++) = 0;
  653. if (!inet_aton(s, &addr2)) {
  654. fprintf(stderr, "Address '%s' not found\n", s);
  655. return 1;
  656. }
  657. mask = atoi(s2);
  658. if (mask < 0 || mask > 32)
  659. return usage(argv[0]);
  660. relayd_add_pending_route((uint8_t *) &addr.s_addr, (uint8_t *) &addr2.s_addr, mask, 0);
  661. break;
  662. case '?':
  663. default:
  664. return usage(argv[0]);
  665. }
  666. }
  667. if (list_empty(&interfaces))
  668. return usage(argv[0]);
  669. if (ifnum < 2) {
  670. fprintf(stderr, "ERROR: Need at least 2 interfaces for relaying\n");
  671. return -1;
  672. }
  673. argc -= optind;
  674. argv += optind;
  675. signal(SIGTERM, die);
  676. signal(SIGHUP, die);
  677. signal(SIGUSR1, die);
  678. signal(SIGUSR2, die);
  679. if (local_addr_valid)
  680. local_route_table = route_table++;
  681. if (relayd_rtnl_init() < 0)
  682. return 1;
  683. if (init_interfaces() < 0)
  684. return 1;
  685. ping_static_routes();
  686. uloop_run();
  687. uloop_done();
  688. cleanup_hosts();
  689. free_interfaces();
  690. relayd_rtnl_done();
  691. close(inet_sock);
  692. return 0;
  693. }