route.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /*
  2. route.c -- routing
  3. Copyright (C) 2000-2005 Ivo Timmermans,
  4. 2000-2017 Guus Sliepen <guus@tinc-vpn.org>
  5. 2015-2016 Vittorio Gambaletta
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "system.h"
  19. #include "avl_tree.h"
  20. #include "connection.h"
  21. #include "ethernet.h"
  22. #include "ipv4.h"
  23. #include "ipv6.h"
  24. #include "logger.h"
  25. #include "net.h"
  26. #include "protocol.h"
  27. #include "route.h"
  28. #include "subnet.h"
  29. #include "utils.h"
  30. rmode_t routing_mode = RMODE_ROUTER;
  31. fmode_t forwarding_mode = FMODE_INTERNAL;
  32. bmode_t broadcast_mode = BMODE_MST;
  33. bool decrement_ttl = false;
  34. bool directonly = false;
  35. bool priorityinheritance = false;
  36. int macexpire = 600;
  37. bool overwrite_mac = false;
  38. mac_t mymac = {{0xFE, 0xFD, 0, 0, 0, 0}};
  39. /* Sizes of various headers */
  40. static const size_t ether_size = sizeof(struct ether_header);
  41. static const size_t arp_size = sizeof(struct ether_arp);
  42. static const size_t ip_size = sizeof(struct ip);
  43. static const size_t icmp_size = sizeof(struct icmp) - sizeof(struct ip);
  44. static const size_t ip6_size = sizeof(struct ip6_hdr);
  45. static const size_t icmp6_size = sizeof(struct icmp6_hdr);
  46. static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
  47. static const size_t opt_size = sizeof(struct nd_opt_hdr);
  48. #ifndef MAX
  49. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  50. #endif
  51. /* RFC 1071 */
  52. static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
  53. uint16_t *p = data;
  54. uint32_t checksum = prevsum ^ 0xFFFF;
  55. while(len >= 2) {
  56. checksum += *p++;
  57. len -= 2;
  58. }
  59. if(len) {
  60. checksum += *(uint8_t *)p;
  61. }
  62. while(checksum >> 16) {
  63. checksum = (checksum & 0xFFFF) + (checksum >> 16);
  64. }
  65. return ~checksum;
  66. }
  67. static bool ratelimit(int frequency) {
  68. static time_t lasttime = 0;
  69. static int count = 0;
  70. if(lasttime == now) {
  71. if(count >= frequency) {
  72. return true;
  73. }
  74. } else {
  75. lasttime = now;
  76. count = 0;
  77. }
  78. count++;
  79. return false;
  80. }
  81. static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
  82. if(packet->len < length) {
  83. ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
  84. return false;
  85. } else {
  86. return true;
  87. }
  88. }
  89. static void swap_mac_addresses(vpn_packet_t *packet) {
  90. mac_t tmp;
  91. memcpy(&tmp, &packet->data[0], sizeof(tmp));
  92. memcpy(&packet->data[0], &packet->data[6], sizeof(tmp));
  93. memcpy(&packet->data[6], &tmp, sizeof(tmp));
  94. }
  95. /* RFC 792 */
  96. static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
  97. struct ip ip = {0};
  98. struct icmp icmp = {0};
  99. struct in_addr ip_src;
  100. struct in_addr ip_dst;
  101. uint32_t oldlen;
  102. if(ratelimit(3)) {
  103. return;
  104. }
  105. /* Swap Ethernet source and destination addresses */
  106. swap_mac_addresses(packet);
  107. /* Copy headers from packet into properly aligned structs on the stack */
  108. memcpy(&ip, packet->data + ether_size, ip_size);
  109. /* Remember original source and destination */
  110. ip_src = ip.ip_src;
  111. ip_dst = ip.ip_dst;
  112. /* Try to reply with an IP address assigned to the local machine */
  113. if(type == ICMP_TIME_EXCEEDED && code == ICMP_EXC_TTL) {
  114. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  115. if(sockfd != -1) {
  116. struct sockaddr_in addr;
  117. memset(&addr, 0, sizeof(addr));
  118. addr.sin_family = AF_INET;
  119. addr.sin_addr = ip.ip_src;
  120. if(!connect(sockfd, (const struct sockaddr *) &addr, sizeof(addr))) {
  121. memset(&addr, 0, sizeof(addr));
  122. addr.sin_family = AF_INET;
  123. socklen_t addrlen = sizeof(addr);
  124. if(!getsockname(sockfd, (struct sockaddr *) &addr, &addrlen) && addrlen <= sizeof(addr)) {
  125. ip_dst = addr.sin_addr;
  126. }
  127. }
  128. close(sockfd);
  129. }
  130. }
  131. oldlen = packet->len - ether_size;
  132. if(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
  133. icmp.icmp_nextmtu = htons(packet->len - ether_size);
  134. }
  135. if(oldlen >= IP_MSS - ip_size - icmp_size) {
  136. oldlen = IP_MSS - ip_size - icmp_size;
  137. }
  138. /* Copy first part of original contents to ICMP message */
  139. memmove(packet->data + ether_size + ip_size + icmp_size, packet->data + ether_size, oldlen);
  140. /* Fill in IPv4 header */
  141. ip.ip_v = 4;
  142. ip.ip_hl = ip_size / 4;
  143. ip.ip_tos = 0;
  144. ip.ip_len = htons(ip_size + icmp_size + oldlen);
  145. ip.ip_id = 0;
  146. ip.ip_off = 0;
  147. ip.ip_ttl = 255;
  148. ip.ip_p = IPPROTO_ICMP;
  149. ip.ip_sum = 0;
  150. ip.ip_src = ip_dst;
  151. ip.ip_dst = ip_src;
  152. ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
  153. /* Fill in ICMP header */
  154. icmp.icmp_type = type;
  155. icmp.icmp_code = code;
  156. icmp.icmp_cksum = 0;
  157. icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
  158. icmp.icmp_cksum = inet_checksum(packet->data + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
  159. /* Copy structs on stack back to packet */
  160. memcpy(packet->data + ether_size, &ip, ip_size);
  161. memcpy(packet->data + ether_size + ip_size, &icmp, icmp_size);
  162. packet->len = ether_size + ip_size + icmp_size + oldlen;
  163. send_packet(source, packet);
  164. }
  165. /* RFC 2463 */
  166. static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_t ether_size, uint8_t type, uint8_t code) {
  167. struct ip6_hdr ip6;
  168. struct icmp6_hdr icmp6 = {0};
  169. uint16_t checksum;
  170. struct {
  171. struct in6_addr ip6_src; /* source address */
  172. struct in6_addr ip6_dst; /* destination address */
  173. uint32_t length;
  174. uint32_t next;
  175. } pseudo;
  176. if(ratelimit(3)) {
  177. return;
  178. }
  179. /* Swap Ethernet source and destination addresses */
  180. swap_mac_addresses(packet);
  181. /* Copy headers from packet to structs on the stack */
  182. memcpy(&ip6, packet->data + ether_size, ip6_size);
  183. /* Remember original source and destination */
  184. pseudo.ip6_src = ip6.ip6_dst;
  185. pseudo.ip6_dst = ip6.ip6_src;
  186. /* Try to reply with an IP address assigned to the local machine */
  187. if(type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) {
  188. int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
  189. if(sockfd != -1) {
  190. struct sockaddr_in6 addr;
  191. memset(&addr, 0, sizeof(addr));
  192. addr.sin6_family = AF_INET6;
  193. addr.sin6_addr = ip6.ip6_src;
  194. if(!connect(sockfd, (const struct sockaddr *) &addr, sizeof(addr))) {
  195. memset(&addr, 0, sizeof(addr));
  196. addr.sin6_family = AF_INET6;
  197. socklen_t addrlen = sizeof(addr);
  198. if(!getsockname(sockfd, (struct sockaddr *) &addr, &addrlen) && addrlen <= sizeof(addr)) {
  199. pseudo.ip6_src = addr.sin6_addr;
  200. }
  201. }
  202. close(sockfd);
  203. }
  204. }
  205. pseudo.length = packet->len - ether_size;
  206. if(type == ICMP6_PACKET_TOO_BIG) {
  207. icmp6.icmp6_mtu = htonl(pseudo.length);
  208. }
  209. if(pseudo.length >= IP_MSS - ip6_size - icmp6_size) {
  210. pseudo.length = IP_MSS - ip6_size - icmp6_size;
  211. }
  212. /* Copy first part of original contents to ICMP message */
  213. memmove(packet->data + ether_size + ip6_size + icmp6_size, packet->data + ether_size, pseudo.length);
  214. /* Fill in IPv6 header */
  215. ip6.ip6_flow = htonl(0x60000000UL);
  216. ip6.ip6_plen = htons(icmp6_size + pseudo.length);
  217. ip6.ip6_nxt = IPPROTO_ICMPV6;
  218. ip6.ip6_hlim = 255;
  219. ip6.ip6_src = pseudo.ip6_src;
  220. ip6.ip6_dst = pseudo.ip6_dst;
  221. /* Fill in ICMP header */
  222. icmp6.icmp6_type = type;
  223. icmp6.icmp6_code = code;
  224. icmp6.icmp6_cksum = 0;
  225. /* Create pseudo header */
  226. pseudo.length = htonl(icmp6_size + pseudo.length);
  227. pseudo.next = htonl(IPPROTO_ICMPV6);
  228. /* Generate checksum */
  229. checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
  230. checksum = inet_checksum(&icmp6, icmp6_size, checksum);
  231. checksum = inet_checksum(packet->data + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
  232. icmp6.icmp6_cksum = checksum;
  233. /* Copy structs on stack back to packet */
  234. memcpy(packet->data + ether_size, &ip6, ip6_size);
  235. memcpy(packet->data + ether_size + ip6_size, &icmp6, icmp6_size);
  236. packet->len = ether_size + ip6_size + ntohl(pseudo.length);
  237. send_packet(source, packet);
  238. }
  239. static bool do_decrement_ttl(node_t *source, vpn_packet_t *packet) {
  240. uint16_t type = packet->data[12] << 8 | packet->data[13];
  241. length_t ethlen = ether_size;
  242. if(type == ETH_P_8021Q) {
  243. type = packet->data[16] << 8 | packet->data[17];
  244. ethlen += 4;
  245. }
  246. switch(type) {
  247. case ETH_P_IP:
  248. if(!checklength(source, packet, ethlen + ip_size)) {
  249. return false;
  250. }
  251. if(packet->data[ethlen + 8] <= 1) {
  252. if(packet->data[ethlen + 11] != IPPROTO_ICMP || packet->data[ethlen + 32] != ICMP_TIME_EXCEEDED) {
  253. route_ipv4_unreachable(source, packet, ethlen, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL);
  254. }
  255. return false;
  256. }
  257. uint16_t old = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
  258. packet->data[ethlen + 8]--;
  259. uint16_t new = packet->data[ethlen + 8] << 8 | packet->data[ethlen + 9];
  260. uint32_t checksum = packet->data[ethlen + 10] << 8 | packet->data[ethlen + 11];
  261. checksum += old + (~new & 0xFFFF);
  262. while(checksum >> 16) {
  263. checksum = (checksum & 0xFFFF) + (checksum >> 16);
  264. }
  265. packet->data[ethlen + 10] = checksum >> 8;
  266. packet->data[ethlen + 11] = checksum & 0xff;
  267. return true;
  268. case ETH_P_IPV6:
  269. if(!checklength(source, packet, ethlen + ip6_size)) {
  270. return false;
  271. }
  272. if(packet->data[ethlen + 7] <= 1) {
  273. if(packet->data[ethlen + 6] != IPPROTO_ICMPV6 || packet->data[ethlen + 40] != ICMP6_TIME_EXCEEDED) {
  274. route_ipv6_unreachable(source, packet, ethlen, ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT);
  275. }
  276. return false;
  277. }
  278. packet->data[ethlen + 7]--;
  279. return true;
  280. default:
  281. return true;
  282. }
  283. }
  284. static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *packet) {
  285. if(!source || !via || !(via->options & OPTION_CLAMP_MSS)) {
  286. return;
  287. }
  288. uint16_t mtu = source->mtu;
  289. if(via != myself && via->mtu < mtu) {
  290. mtu = via->mtu;
  291. }
  292. /* Find TCP header */
  293. int start = ether_size;
  294. uint16_t type = packet->data[12] << 8 | packet->data[13];
  295. if(type == ETH_P_8021Q) {
  296. start += 4;
  297. type = packet->data[16] << 8 | packet->data[17];
  298. }
  299. if(type == ETH_P_IP && packet->data[start + 9] == 6) {
  300. start += (packet->data[start] & 0xf) * 4;
  301. } else if(type == ETH_P_IPV6 && packet->data[start + 6] == 6) {
  302. start += 40;
  303. } else {
  304. return;
  305. }
  306. if(packet->len <= start + 20) {
  307. return;
  308. }
  309. /* Use data offset field to calculate length of options field */
  310. int len = ((packet->data[start + 12] >> 4) - 5) * 4;
  311. if(packet->len < start + 20 + len) {
  312. return;
  313. }
  314. /* Search for MSS option header */
  315. for(int i = 0; i < len;) {
  316. if(packet->data[start + 20 + i] == 0) {
  317. break;
  318. }
  319. if(packet->data[start + 20 + i] == 1) {
  320. i++;
  321. continue;
  322. }
  323. if(i > len - 2 || i > len - packet->data[start + 21 + i]) {
  324. break;
  325. }
  326. if(packet->data[start + 20 + i] != 2) {
  327. if(packet->data[start + 21 + i] < 2) {
  328. break;
  329. }
  330. i += packet->data[start + 21 + i];
  331. continue;
  332. }
  333. if(packet->data[start + 21] != 4) {
  334. break;
  335. }
  336. /* Found it */
  337. uint16_t oldmss = packet->data[start + 22 + i] << 8 | packet->data[start + 23 + i];
  338. uint16_t newmss = mtu - start - 20;
  339. uint32_t csum = packet->data[start + 16] << 8 | packet->data[start + 17];
  340. if(oldmss <= newmss) {
  341. break;
  342. }
  343. ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
  344. /* Update the MSS value and the checksum */
  345. packet->data[start + 22 + i] = newmss >> 8;
  346. packet->data[start + 23 + i] = newmss & 0xff;
  347. csum ^= 0xffff;
  348. csum += oldmss ^ 0xffff;
  349. csum += newmss;
  350. csum = (csum & 0xffff) + (csum >> 16);
  351. csum += csum >> 16;
  352. csum ^= 0xffff;
  353. packet->data[start + 16] = csum >> 8;
  354. packet->data[start + 17] = csum;
  355. break;
  356. }
  357. }
  358. static void learn_mac(mac_t *address) {
  359. subnet_t *subnet;
  360. avl_node_t *node;
  361. connection_t *c;
  362. subnet = lookup_subnet_mac(myself, address);
  363. /* If we don't know this MAC address yet, store it */
  364. if(!subnet) {
  365. ifdebug(TRAFFIC) logger(LOG_INFO, "Learned new MAC address %x:%x:%x:%x:%x:%x",
  366. address->x[0], address->x[1], address->x[2], address->x[3],
  367. address->x[4], address->x[5]);
  368. subnet = new_subnet();
  369. subnet->type = SUBNET_MAC;
  370. subnet->expires = now + macexpire;
  371. subnet->net.mac.address = *address;
  372. subnet->weight = 10;
  373. subnet_add(myself, subnet);
  374. subnet_update(myself, subnet, true);
  375. /* And tell all other tinc daemons it's our MAC */
  376. for(node = connection_tree->head; node; node = node->next) {
  377. c = node->data;
  378. if(c->status.active) {
  379. send_add_subnet(c, subnet);
  380. }
  381. }
  382. }
  383. if(subnet->expires) {
  384. subnet->expires = now + macexpire;
  385. }
  386. }
  387. void age_subnets(void) {
  388. subnet_t *s;
  389. connection_t *c;
  390. avl_node_t *node, *next, *node2;
  391. for(node = myself->subnet_tree->head; node; node = next) {
  392. next = node->next;
  393. s = node->data;
  394. if(s->expires && s->expires <= now) {
  395. ifdebug(TRAFFIC) {
  396. char netstr[MAXNETSTR];
  397. if(net2str(netstr, sizeof(netstr), s)) {
  398. logger(LOG_INFO, "Subnet %s expired", netstr);
  399. }
  400. }
  401. for(node2 = connection_tree->head; node2; node2 = node2->next) {
  402. c = node2->data;
  403. if(c->status.active) {
  404. send_del_subnet(c, s);
  405. }
  406. }
  407. subnet_update(myself, s, false);
  408. subnet_del(myself, s);
  409. }
  410. }
  411. }
  412. static void route_broadcast(node_t *source, vpn_packet_t *packet) {
  413. if(decrement_ttl && source != myself)
  414. if(!do_decrement_ttl(source, packet)) {
  415. return;
  416. }
  417. broadcast_packet(source, packet);
  418. }
  419. /* RFC 791 */
  420. static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
  421. struct ip ip;
  422. vpn_packet_t fragment;
  423. int len, maxlen, todo;
  424. uint8_t *offset;
  425. uint16_t ip_off, origf;
  426. memcpy(&ip, packet->data + ether_size, ip_size);
  427. fragment.priority = packet->priority;
  428. if(ip.ip_hl != ip_size / 4) {
  429. return;
  430. }
  431. todo = ntohs(ip.ip_len) - ip_size;
  432. if(ether_size + ip_size + todo != packet->len) {
  433. ifdebug(TRAFFIC) logger(LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%d)", packet->len, (int)(ether_size + ip_size + todo));
  434. return;
  435. }
  436. ifdebug(TRAFFIC) logger(LOG_INFO, "Fragmenting packet of %d bytes to %s (%s)", packet->len, dest->name, dest->hostname);
  437. offset = packet->data + ether_size + ip_size;
  438. maxlen = (MAX(dest->mtu, 590) - ether_size - ip_size) & ~0x7;
  439. ip_off = ntohs(ip.ip_off);
  440. origf = ip_off & ~IP_OFFMASK;
  441. ip_off &= IP_OFFMASK;
  442. while(todo) {
  443. len = todo > maxlen ? maxlen : todo;
  444. memcpy(fragment.data + ether_size + ip_size, offset, len);
  445. todo -= len;
  446. offset += len;
  447. ip.ip_len = htons(ip_size + len);
  448. ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
  449. ip.ip_sum = 0;
  450. ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
  451. memcpy(fragment.data, packet->data, ether_size);
  452. memcpy(fragment.data + ether_size, &ip, ip_size);
  453. fragment.len = ether_size + ip_size + len;
  454. send_packet(dest, &fragment);
  455. ip_off += len / 8;
  456. }
  457. }
  458. static void route_ipv4_unicast(node_t *source, vpn_packet_t *packet) {
  459. subnet_t *subnet;
  460. node_t *via;
  461. ipv4_t dest;
  462. memcpy(&dest, &packet->data[30], sizeof(dest));
  463. subnet = lookup_subnet_ipv4(&dest);
  464. if(!subnet) {
  465. ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv4 destination address %d.%d.%d.%d",
  466. source->name, source->hostname,
  467. dest.x[0],
  468. dest.x[1],
  469. dest.x[2],
  470. dest.x[3]);
  471. route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNKNOWN);
  472. return;
  473. }
  474. if(subnet->owner == source) {
  475. ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
  476. return;
  477. }
  478. if(!subnet->owner->status.reachable) {
  479. route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_UNREACH);
  480. return;
  481. }
  482. if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
  483. route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
  484. return;
  485. }
  486. if(decrement_ttl && source != myself && subnet->owner != myself)
  487. if(!do_decrement_ttl(source, packet)) {
  488. return;
  489. }
  490. if(priorityinheritance) {
  491. packet->priority = packet->data[15];
  492. }
  493. via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
  494. if(via == source) {
  495. ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
  496. return;
  497. }
  498. if(directonly && subnet->owner != via) {
  499. route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_NET_ANO);
  500. return;
  501. }
  502. if(via && packet->len > MAX(via->mtu, 590) && via != myself) {
  503. ifdebug(TRAFFIC) logger(LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
  504. if(packet->data[20] & 0x40) {
  505. packet->len = MAX(via->mtu, 590);
  506. route_ipv4_unreachable(source, packet, ether_size, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
  507. } else {
  508. fragment_ipv4_packet(via, packet, ether_size);
  509. }
  510. return;
  511. }
  512. clamp_mss(source, via, packet);
  513. send_packet(subnet->owner, packet);
  514. }
  515. static void route_ipv4(node_t *source, vpn_packet_t *packet) {
  516. if(!checklength(source, packet, ether_size + ip_size)) {
  517. return;
  518. }
  519. if(broadcast_mode && (((packet->data[30] & 0xf0) == 0xe0) || (
  520. packet->data[30] == 255 &&
  521. packet->data[31] == 255 &&
  522. packet->data[32] == 255 &&
  523. packet->data[33] == 255))) {
  524. route_broadcast(source, packet);
  525. } else {
  526. route_ipv4_unicast(source, packet);
  527. }
  528. }
  529. static void route_ipv6_unicast(node_t *source, vpn_packet_t *packet) {
  530. subnet_t *subnet;
  531. node_t *via;
  532. ipv6_t dest;
  533. memcpy(&dest, &packet->data[38], sizeof(dest));
  534. subnet = lookup_subnet_ipv6(&dest);
  535. if(!subnet) {
  536. ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
  537. source->name, source->hostname,
  538. ntohs(dest.x[0]),
  539. ntohs(dest.x[1]),
  540. ntohs(dest.x[2]),
  541. ntohs(dest.x[3]),
  542. ntohs(dest.x[4]),
  543. ntohs(dest.x[5]),
  544. ntohs(dest.x[6]),
  545. ntohs(dest.x[7]));
  546. route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR);
  547. return;
  548. }
  549. if(subnet->owner == source) {
  550. ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
  551. return;
  552. }
  553. if(!subnet->owner->status.reachable) {
  554. route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE);
  555. return;
  556. }
  557. if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
  558. route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
  559. return;
  560. }
  561. if(decrement_ttl && source != myself && subnet->owner != myself) {
  562. if(!do_decrement_ttl(source, packet)) {
  563. return;
  564. }
  565. }
  566. if(priorityinheritance) {
  567. packet->priority = ((packet->data[14] & 0x0f) << 4) | (packet->data[15] >> 4);
  568. }
  569. via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
  570. if(via == source) {
  571. ifdebug(TRAFFIC) logger(LOG_ERR, "Routing loop for packet from %s (%s)!", source->name, source->hostname);
  572. return;
  573. }
  574. if(directonly && subnet->owner != via) {
  575. route_ipv6_unreachable(source, packet, ether_size, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN);
  576. return;
  577. }
  578. if(via && packet->len > MAX(via->mtu, 1294) && via != myself) {
  579. ifdebug(TRAFFIC) logger(LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
  580. packet->len = MAX(via->mtu, 1294);
  581. route_ipv6_unreachable(source, packet, ether_size, ICMP6_PACKET_TOO_BIG, 0);
  582. return;
  583. }
  584. clamp_mss(source, via, packet);
  585. send_packet(subnet->owner, packet);
  586. }
  587. /* RFC 2461 */
  588. static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
  589. struct ip6_hdr ip6;
  590. struct nd_neighbor_solicit ns;
  591. struct nd_opt_hdr opt;
  592. subnet_t *subnet;
  593. uint16_t checksum;
  594. bool has_opt;
  595. struct {
  596. struct in6_addr ip6_src; /* source address */
  597. struct in6_addr ip6_dst; /* destination address */
  598. uint32_t length;
  599. uint32_t next;
  600. } pseudo;
  601. if(!checklength(source, packet, ether_size + ip6_size + ns_size)) {
  602. return;
  603. }
  604. has_opt = packet->len >= ether_size + ip6_size + ns_size + opt_size + ETH_ALEN;
  605. if(source != myself) {
  606. ifdebug(TRAFFIC) logger(LOG_WARNING, "Got neighbor solicitation request from %s (%s) while in router mode!", source->name, source->hostname);
  607. return;
  608. }
  609. /* Copy headers from packet to structs on the stack */
  610. memcpy(&ip6, packet->data + ether_size, ip6_size);
  611. memcpy(&ns, packet->data + ether_size + ip6_size, ns_size);
  612. if(has_opt) {
  613. memcpy(&opt, packet->data + ether_size + ip6_size + ns_size, opt_size);
  614. }
  615. /* First, snatch the source address from the neighbor solicitation packet */
  616. if(overwrite_mac) {
  617. memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
  618. }
  619. /* Check if this is a valid neighbor solicitation request */
  620. if(ns.nd_ns_hdr.icmp6_type != ND_NEIGHBOR_SOLICIT ||
  621. (has_opt && opt.nd_opt_type != ND_OPT_SOURCE_LINKADDR)) {
  622. ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type neighbor solicitation request");
  623. return;
  624. }
  625. /* Create pseudo header */
  626. pseudo.ip6_src = ip6.ip6_src;
  627. pseudo.ip6_dst = ip6.ip6_dst;
  628. if(has_opt) {
  629. pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
  630. } else {
  631. pseudo.length = htonl(ns_size);
  632. }
  633. pseudo.next = htonl(IPPROTO_ICMPV6);
  634. /* Generate checksum */
  635. checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
  636. checksum = inet_checksum(&ns, ns_size, checksum);
  637. if(has_opt) {
  638. checksum = inet_checksum(&opt, opt_size, checksum);
  639. checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
  640. }
  641. if(checksum) {
  642. ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: checksum error for neighbor solicitation request");
  643. return;
  644. }
  645. /* Check if the IPv6 address exists on the VPN */
  646. subnet = lookup_subnet_ipv6((ipv6_t *) &ns.nd_ns_target);
  647. if(!subnet) {
  648. ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: neighbor solicitation request for unknown address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
  649. ntohs(((uint16_t *) &ns.nd_ns_target)[0]),
  650. ntohs(((uint16_t *) &ns.nd_ns_target)[1]),
  651. ntohs(((uint16_t *) &ns.nd_ns_target)[2]),
  652. ntohs(((uint16_t *) &ns.nd_ns_target)[3]),
  653. ntohs(((uint16_t *) &ns.nd_ns_target)[4]),
  654. ntohs(((uint16_t *) &ns.nd_ns_target)[5]),
  655. ntohs(((uint16_t *) &ns.nd_ns_target)[6]),
  656. ntohs(((uint16_t *) &ns.nd_ns_target)[7]));
  657. return;
  658. }
  659. /* Check if it is for our own subnet */
  660. if(subnet->owner == myself) {
  661. return; /* silently ignore */
  662. }
  663. if(decrement_ttl)
  664. if(!do_decrement_ttl(source, packet)) {
  665. return;
  666. }
  667. /* Create neighbor advertation reply */
  668. memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
  669. packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
  670. ip6.ip6_dst = ip6.ip6_src; /* swap destination and source protocol address */
  671. ip6.ip6_src = ns.nd_ns_target;
  672. if(has_opt) {
  673. memcpy(packet->data + ether_size + ip6_size + ns_size + opt_size, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
  674. }
  675. ns.nd_ns_cksum = 0;
  676. ns.nd_ns_type = ND_NEIGHBOR_ADVERT;
  677. ns.nd_ns_reserved = htonl(0x40000000UL); /* Set solicited flag */
  678. opt.nd_opt_type = ND_OPT_TARGET_LINKADDR;
  679. /* Create pseudo header */
  680. pseudo.ip6_src = ip6.ip6_src;
  681. pseudo.ip6_dst = ip6.ip6_dst;
  682. if(has_opt) {
  683. pseudo.length = htonl(ns_size + opt_size + ETH_ALEN);
  684. } else {
  685. pseudo.length = htonl(ns_size);
  686. }
  687. pseudo.next = htonl(IPPROTO_ICMPV6);
  688. /* Generate checksum */
  689. checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
  690. checksum = inet_checksum(&ns, ns_size, checksum);
  691. if(has_opt) {
  692. checksum = inet_checksum(&opt, opt_size, checksum);
  693. checksum = inet_checksum(packet->data + ether_size + ip6_size + ns_size + opt_size, ETH_ALEN, checksum);
  694. }
  695. ns.nd_ns_hdr.icmp6_cksum = checksum;
  696. /* Copy structs on stack back to packet */
  697. memcpy(packet->data + ether_size, &ip6, ip6_size);
  698. memcpy(packet->data + ether_size + ip6_size, &ns, ns_size);
  699. if(has_opt) {
  700. memcpy(packet->data + ether_size + ip6_size + ns_size, &opt, opt_size);
  701. }
  702. send_packet(source, packet);
  703. }
  704. static void route_ipv6(node_t *source, vpn_packet_t *packet) {
  705. if(!checklength(source, packet, ether_size + ip6_size)) {
  706. return;
  707. }
  708. if(packet->data[20] == IPPROTO_ICMPV6 && checklength(source, packet, ether_size + ip6_size + icmp6_size) && packet->data[54] == ND_NEIGHBOR_SOLICIT) {
  709. route_neighborsol(source, packet);
  710. return;
  711. }
  712. if(broadcast_mode && packet->data[38] == 255) {
  713. route_broadcast(source, packet);
  714. } else {
  715. route_ipv6_unicast(source, packet);
  716. }
  717. }
  718. /* RFC 826 */
  719. static void route_arp(node_t *source, vpn_packet_t *packet) {
  720. struct ether_arp arp;
  721. subnet_t *subnet;
  722. struct in_addr addr;
  723. if(!checklength(source, packet, ether_size + arp_size)) {
  724. return;
  725. }
  726. if(source != myself) {
  727. ifdebug(TRAFFIC) logger(LOG_WARNING, "Got ARP request from %s (%s) while in router mode!", source->name, source->hostname);
  728. return;
  729. }
  730. /* First, snatch the source address from the ARP packet */
  731. if(overwrite_mac) {
  732. memcpy(mymac.x, packet->data + ETH_ALEN, ETH_ALEN);
  733. }
  734. /* Copy headers from packet to structs on the stack */
  735. memcpy(&arp, packet->data + ether_size, arp_size);
  736. /* Check if this is a valid ARP request */
  737. if(ntohs(arp.arp_hrd) != ARPHRD_ETHER || ntohs(arp.arp_pro) != ETH_P_IP ||
  738. arp.arp_hln != ETH_ALEN || arp.arp_pln != sizeof(addr) || ntohs(arp.arp_op) != ARPOP_REQUEST) {
  739. ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: received unknown type ARP request");
  740. return;
  741. }
  742. /* Check if the IPv4 address exists on the VPN */
  743. subnet = lookup_subnet_ipv4((ipv4_t *) &arp.arp_tpa);
  744. if(!subnet) {
  745. ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet: ARP request for unknown address %d.%d.%d.%d",
  746. arp.arp_tpa[0], arp.arp_tpa[1], arp.arp_tpa[2],
  747. arp.arp_tpa[3]);
  748. return;
  749. }
  750. /* Check if it is for our own subnet */
  751. if(subnet->owner == myself) {
  752. return; /* silently ignore */
  753. }
  754. if(decrement_ttl)
  755. if(!do_decrement_ttl(source, packet)) {
  756. return;
  757. }
  758. memcpy(packet->data, packet->data + ETH_ALEN, ETH_ALEN); /* copy destination address */
  759. packet->data[ETH_ALEN * 2 - 1] ^= 0xFF; /* mangle source address so it looks like it's not from us */
  760. memcpy(&addr, arp.arp_tpa, sizeof(addr)); /* save protocol addr */
  761. memcpy(arp.arp_tpa, arp.arp_spa, sizeof(addr)); /* swap destination and source protocol address */
  762. memcpy(arp.arp_spa, &addr, sizeof(addr)); /* ... */
  763. memcpy(arp.arp_tha, arp.arp_sha, ETH_ALEN); /* set target hard/proto addr */
  764. memcpy(arp.arp_sha, packet->data + ETH_ALEN, ETH_ALEN); /* add fake source hard addr */
  765. arp.arp_op = htons(ARPOP_REPLY);
  766. /* Copy structs on stack back to packet */
  767. memcpy(packet->data + ether_size, &arp, arp_size);
  768. send_packet(source, packet);
  769. }
  770. static void route_mac(node_t *source, vpn_packet_t *packet) {
  771. subnet_t *subnet;
  772. mac_t dest;
  773. /* Learn source address */
  774. if(source == myself) {
  775. mac_t src;
  776. memcpy(&src, &packet->data[6], sizeof(src));
  777. learn_mac(&src);
  778. }
  779. /* Lookup destination address */
  780. memcpy(&dest, &packet->data[0], sizeof(dest));
  781. subnet = lookup_subnet_mac(NULL, &dest);
  782. if(!subnet) {
  783. route_broadcast(source, packet);
  784. return;
  785. }
  786. if(subnet->owner == source) {
  787. ifdebug(TRAFFIC) logger(LOG_WARNING, "Packet looping back to %s (%s)!", source->name, source->hostname);
  788. return;
  789. }
  790. if(forwarding_mode == FMODE_OFF && source != myself && subnet->owner != myself) {
  791. return;
  792. }
  793. if(decrement_ttl && source != myself && subnet->owner != myself)
  794. if(!do_decrement_ttl(source, packet)) {
  795. return;
  796. }
  797. uint16_t type = packet->data[12] << 8 | packet->data[13];
  798. if(priorityinheritance) {
  799. if(type == ETH_P_IP && packet->len >= ether_size + ip_size) {
  800. packet->priority = packet->data[15];
  801. } else if(type == ETH_P_IPV6 && packet->len >= ether_size + ip6_size) {
  802. packet->priority = ((packet->data[14] & 0x0f) << 4) | (packet->data[15] >> 4);
  803. }
  804. }
  805. // Handle packets larger than PMTU
  806. node_t *via = (subnet->owner->via == myself) ? subnet->owner->nexthop : subnet->owner->via;
  807. if(directonly && subnet->owner != via) {
  808. return;
  809. }
  810. if(via && packet->len > via->mtu && via != myself) {
  811. ifdebug(TRAFFIC) logger(LOG_INFO, "Packet for %s (%s) length %d larger than MTU %d", subnet->owner->name, subnet->owner->hostname, packet->len, via->mtu);
  812. length_t ethlen = 14;
  813. if(type == ETH_P_8021Q) {
  814. type = packet->data[16] << 8 | packet->data[17];
  815. ethlen += 4;
  816. }
  817. if(type == ETH_P_IP && packet->len > 576 + ethlen) {
  818. if(packet->data[6 + ethlen] & 0x40) {
  819. packet->len = via->mtu;
  820. route_ipv4_unreachable(source, packet, ethlen, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED);
  821. } else {
  822. fragment_ipv4_packet(via, packet, ethlen);
  823. }
  824. return;
  825. } else if(type == ETH_P_IPV6 && packet->len > 1280 + ethlen) {
  826. packet->len = via->mtu;
  827. route_ipv6_unreachable(source, packet, ethlen, ICMP6_PACKET_TOO_BIG, 0);
  828. return;
  829. }
  830. }
  831. clamp_mss(source, via, packet);
  832. send_packet(subnet->owner, packet);
  833. }
  834. void route(node_t *source, vpn_packet_t *packet) {
  835. if(forwarding_mode == FMODE_KERNEL && source != myself) {
  836. send_packet(myself, packet);
  837. return;
  838. }
  839. if(!checklength(source, packet, ether_size)) {
  840. return;
  841. }
  842. switch(routing_mode) {
  843. case RMODE_ROUTER: {
  844. uint16_t type = packet->data[12] << 8 | packet->data[13];
  845. switch(type) {
  846. case ETH_P_ARP:
  847. route_arp(source, packet);
  848. break;
  849. case ETH_P_IP:
  850. route_ipv4(source, packet);
  851. break;
  852. case ETH_P_IPV6:
  853. route_ipv6(source, packet);
  854. break;
  855. default:
  856. ifdebug(TRAFFIC) logger(LOG_WARNING, "Cannot route packet from %s (%s): unknown type %hx", source->name, source->hostname, type);
  857. break;
  858. }
  859. }
  860. break;
  861. case RMODE_SWITCH:
  862. route_mac(source, packet);
  863. break;
  864. case RMODE_HUB:
  865. route_broadcast(source, packet);
  866. break;
  867. }
  868. }