ipaux.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. #include "ip.h"
  8. #include "ipv6.h"
  9. /*
  10. * well known IP addresses
  11. */
  12. uchar IPv4bcast[IPaddrlen] = {
  13. 0, 0, 0, 0,
  14. 0, 0, 0, 0,
  15. 0, 0, 0xff, 0xff,
  16. 0xff, 0xff, 0xff, 0xff
  17. };
  18. uchar IPv4allsys[IPaddrlen] = {
  19. 0, 0, 0, 0,
  20. 0, 0, 0, 0,
  21. 0, 0, 0xff, 0xff,
  22. 0xe0, 0, 0, 0x01
  23. };
  24. uchar IPv4allrouter[IPaddrlen] = {
  25. 0, 0, 0, 0,
  26. 0, 0, 0, 0,
  27. 0, 0, 0xff, 0xff,
  28. 0xe0, 0, 0, 0x02
  29. };
  30. uchar IPallbits[IPaddrlen] = {
  31. 0xff, 0xff, 0xff, 0xff,
  32. 0xff, 0xff, 0xff, 0xff,
  33. 0xff, 0xff, 0xff, 0xff,
  34. 0xff, 0xff, 0xff, 0xff
  35. };
  36. uchar IPnoaddr[IPaddrlen];
  37. /*
  38. * prefix of all v4 addresses
  39. */
  40. uchar v4prefix[IPaddrlen] = {
  41. 0, 0, 0, 0,
  42. 0, 0, 0, 0,
  43. 0, 0, 0xff, 0xff,
  44. 0, 0, 0, 0
  45. };
  46. char *v6hdrtypes[Maxhdrtype] =
  47. {
  48. [HBH] "HopbyHop",
  49. [ICMP] "ICMP",
  50. [IGMP] "IGMP",
  51. [GGP] "GGP",
  52. [IPINIP] "IP",
  53. [ST] "ST",
  54. [TCP] "TCP",
  55. [UDP] "UDP",
  56. [ISO_TP4] "ISO_TP4",
  57. [RH] "Routinghdr",
  58. [FH] "Fraghdr",
  59. [IDRP] "IDRP",
  60. [RSVP] "RSVP",
  61. [AH] "Authhdr",
  62. [ESP] "ESP",
  63. [ICMPv6] "ICMPv6",
  64. [NNH] "Nonexthdr",
  65. [ISO_IP] "ISO_IP",
  66. [IGRP] "IGRP",
  67. [OSPF] "OSPF",
  68. };
  69. /*
  70. * well known IPv6 addresses
  71. */
  72. uchar v6Unspecified[IPaddrlen] = {
  73. 0, 0, 0, 0,
  74. 0, 0, 0, 0,
  75. 0, 0, 0, 0,
  76. 0, 0, 0, 0
  77. };
  78. uchar v6loopback[IPaddrlen] = {
  79. 0, 0, 0, 0,
  80. 0, 0, 0, 0,
  81. 0, 0, 0, 0,
  82. 0, 0, 0, 0x01
  83. };
  84. uchar v6linklocal[IPaddrlen] = {
  85. 0xfe, 0x80, 0, 0,
  86. 0, 0, 0, 0,
  87. 0, 0, 0, 0,
  88. 0, 0, 0, 0
  89. };
  90. uchar v6linklocalmask[IPaddrlen] = {
  91. 0xff, 0xff, 0xff, 0xff,
  92. 0xff, 0xff, 0xff, 0xff,
  93. 0, 0, 0, 0,
  94. 0, 0, 0, 0
  95. };
  96. int v6linklocalprefix = 8;
  97. uchar v6sitelocal[IPaddrlen] = {
  98. 0xfe, 0xc0, 0, 0,
  99. 0, 0, 0, 0,
  100. 0, 0, 0, 0,
  101. 0, 0, 0, 0
  102. };
  103. uchar v6sitelocalmask[IPaddrlen] = {
  104. 0xff, 0xff, 0xff, 0xff,
  105. 0xff, 0xff, 0xff, 0xff,
  106. 0, 0, 0, 0,
  107. 0, 0, 0, 0
  108. };
  109. int v6sitelocalprefix = 6;
  110. uchar v6glunicast[IPaddrlen] = {
  111. 0x08, 0, 0, 0,
  112. 0, 0, 0, 0,
  113. 0, 0, 0, 0,
  114. 0, 0, 0, 0
  115. };
  116. uchar v6multicast[IPaddrlen] = {
  117. 0xff, 0, 0, 0,
  118. 0, 0, 0, 0,
  119. 0, 0, 0, 0,
  120. 0, 0, 0, 0
  121. };
  122. uchar v6multicastmask[IPaddrlen] = {
  123. 0xff, 0, 0, 0,
  124. 0, 0, 0, 0,
  125. 0, 0, 0, 0,
  126. 0, 0, 0, 0
  127. };
  128. int v6multicastprefix = 1;
  129. uchar v6allnodesN[IPaddrlen] = {
  130. 0xff, 0x01, 0, 0,
  131. 0, 0, 0, 0,
  132. 0, 0, 0, 0,
  133. 0, 0, 0, 0x01
  134. };
  135. uchar v6allnodesNmask[IPaddrlen] = {
  136. 0xff, 0xff, 0, 0,
  137. 0, 0, 0, 0,
  138. 0, 0, 0, 0,
  139. 0, 0, 0, 0
  140. };
  141. int v6allnodesprefix = 2;
  142. uchar v6allnodesL[IPaddrlen] = {
  143. 0xff, 0x02, 0, 0,
  144. 0, 0, 0, 0,
  145. 0, 0, 0, 0,
  146. 0, 0, 0, 0x01
  147. };
  148. uchar v6allnodesLmask[IPaddrlen] = {
  149. 0xff, 0xff, 0, 0,
  150. 0, 0, 0, 0,
  151. 0, 0, 0, 0,
  152. 0, 0, 0, 0
  153. };
  154. int v6allnodesLprefix = 2;
  155. uchar v6allroutersN[IPaddrlen] = {
  156. 0xff, 0x01, 0, 0,
  157. 0, 0, 0, 0,
  158. 0, 0, 0, 0,
  159. 0, 0, 0, 0x02
  160. };
  161. uchar v6allroutersL[IPaddrlen] = {
  162. 0xff, 0x02, 0, 0,
  163. 0, 0, 0, 0,
  164. 0, 0, 0, 0,
  165. 0, 0, 0, 0x02
  166. };
  167. uchar v6allroutersS[IPaddrlen] = {
  168. 0xff, 0x05, 0, 0,
  169. 0, 0, 0, 0,
  170. 0, 0, 0, 0,
  171. 0, 0, 0, 0x02
  172. };
  173. uchar v6solicitednode[IPaddrlen] = {
  174. 0xff, 0x02, 0, 0,
  175. 0, 0, 0, 0,
  176. 0, 0, 0, 0x01,
  177. 0xff, 0, 0, 0
  178. };
  179. uchar v6solicitednodemask[IPaddrlen] = {
  180. 0xff, 0xff, 0xff, 0xff,
  181. 0xff, 0xff, 0xff, 0xff,
  182. 0xff, 0xff, 0xff, 0xff,
  183. 0xff, 0x0, 0x0, 0x0
  184. };
  185. int v6solicitednodeprefix = 13;
  186. ushort
  187. ptclcsum(Block *bp, int offset, int len)
  188. {
  189. uchar *addr;
  190. ulong losum, hisum;
  191. ushort csum;
  192. int odd, blocklen, x;
  193. /* Correct to front of data area */
  194. while(bp != nil && offset && offset >= BLEN(bp)) {
  195. offset -= BLEN(bp);
  196. bp = bp->next;
  197. }
  198. if(bp == nil)
  199. return 0;
  200. addr = bp->rp + offset;
  201. blocklen = BLEN(bp) - offset;
  202. if(bp->next == nil) {
  203. if(blocklen < len)
  204. len = blocklen;
  205. return ~ptclbsum(addr, len) & 0xffff;
  206. }
  207. losum = 0;
  208. hisum = 0;
  209. odd = 0;
  210. while(len) {
  211. x = blocklen;
  212. if(len < x)
  213. x = len;
  214. csum = ptclbsum(addr, x);
  215. if(odd)
  216. hisum += csum;
  217. else
  218. losum += csum;
  219. odd = (odd+x) & 1;
  220. len -= x;
  221. bp = bp->next;
  222. if(bp == nil)
  223. break;
  224. blocklen = BLEN(bp);
  225. addr = bp->rp;
  226. }
  227. losum += hisum>>8;
  228. losum += (hisum&0xff)<<8;
  229. while((csum = losum>>16) != 0)
  230. losum = csum + (losum & 0xffff);
  231. return ~losum & 0xffff;
  232. }
  233. enum
  234. {
  235. Isprefix= 16,
  236. };
  237. static uchar prefixvals[256] =
  238. {
  239. [0x00] 0 | Isprefix,
  240. [0x80] 1 | Isprefix,
  241. [0xC0] 2 | Isprefix,
  242. [0xE0] 3 | Isprefix,
  243. [0xF0] 4 | Isprefix,
  244. [0xF8] 5 | Isprefix,
  245. [0xFC] 6 | Isprefix,
  246. [0xFE] 7 | Isprefix,
  247. [0xFF] 8 | Isprefix,
  248. };
  249. int
  250. eipfmt(Fmt *f)
  251. {
  252. char buf[5*8];
  253. static char *efmt = "%.2lux%.2lux%.2lux%.2lux%.2lux%.2lux";
  254. static char *ifmt = "%d.%d.%d.%d";
  255. uchar *p, ip[16];
  256. ulong *lp;
  257. ushort s;
  258. int i, j, n, eln, eli;
  259. switch(f->r) {
  260. case 'E': /* Ethernet address */
  261. p = va_arg(f->args, uchar*);
  262. return fmtprint(f, efmt, p[0], p[1], p[2], p[3], p[4], p[5]);
  263. case 'I': /* Ip address */
  264. p = va_arg(f->args, uchar*);
  265. common:
  266. if(memcmp(p, v4prefix, 12) == 0)
  267. return fmtprint(f, ifmt, p[12], p[13], p[14], p[15]);
  268. /* find longest elision */
  269. eln = eli = -1;
  270. for(i = 0; i < 16; i += 2){
  271. for(j = i; j < 16; j += 2)
  272. if(p[j] != 0 || p[j+1] != 0)
  273. break;
  274. if(j > i && j - i > eln){
  275. eli = i;
  276. eln = j - i;
  277. }
  278. }
  279. /* print with possible elision */
  280. n = 0;
  281. for(i = 0; i < 16; i += 2){
  282. if(i == eli){
  283. n += sprint(buf+n, "::");
  284. i += eln;
  285. if(i >= 16)
  286. break;
  287. } else if(i != 0)
  288. n += sprint(buf+n, ":");
  289. s = (p[i]<<8) + p[i+1];
  290. n += sprint(buf+n, "%ux", s);
  291. }
  292. return fmtstrcpy(f, buf);
  293. case 'i': /* v6 address as 4 longs */
  294. lp = va_arg(f->args, ulong*);
  295. for(i = 0; i < 4; i++)
  296. hnputl(ip+4*i, *lp++);
  297. p = ip;
  298. goto common;
  299. case 'V': /* v4 ip address */
  300. p = va_arg(f->args, uchar*);
  301. return fmtprint(f, ifmt, p[0], p[1], p[2], p[3]);
  302. case 'M': /* ip mask */
  303. p = va_arg(f->args, uchar*);
  304. /* look for a prefix mask */
  305. for(i = 0; i < 16; i++)
  306. if(p[i] != 0xff)
  307. break;
  308. if(i < 16){
  309. if((prefixvals[p[i]] & Isprefix) == 0)
  310. goto common;
  311. for(j = i+1; j < 16; j++)
  312. if(p[j] != 0)
  313. goto common;
  314. n = 8*i + (prefixvals[p[i]] & ~Isprefix);
  315. } else
  316. n = 8*16;
  317. /* got one, use /xx format */
  318. return fmtprint(f, "/%d", n);
  319. }
  320. return fmtprint(f, "(eipfmt)");
  321. }
  322. #define CLASS(p) ((*(uchar*)(p))>>6)
  323. extern char*
  324. v4parseip(uchar *to, char *from)
  325. {
  326. int i;
  327. char *p;
  328. p = from;
  329. for(i = 0; i < 4 && *p; i++){
  330. to[i] = strtoul(p, &p, 0);
  331. if(*p == '.')
  332. p++;
  333. }
  334. switch(CLASS(to)){
  335. case 0: /* class A - 1 uchar net */
  336. case 1:
  337. if(i == 3){
  338. to[3] = to[2];
  339. to[2] = to[1];
  340. to[1] = 0;
  341. } else if(i == 2){
  342. to[3] = to[1];
  343. to[1] = 0;
  344. }
  345. break;
  346. case 2: /* class B - 2 uchar net */
  347. if(i == 3){
  348. to[3] = to[2];
  349. to[2] = 0;
  350. }
  351. break;
  352. }
  353. return p;
  354. }
  355. int
  356. isv4(uchar *ip)
  357. {
  358. return memcmp(ip, v4prefix, IPv4off) == 0;
  359. }
  360. /*
  361. * the following routines are unrolled with no memset's to speed
  362. * up the usual case
  363. */
  364. void
  365. v4tov6(uchar *v6, uchar *v4)
  366. {
  367. v6[0] = 0;
  368. v6[1] = 0;
  369. v6[2] = 0;
  370. v6[3] = 0;
  371. v6[4] = 0;
  372. v6[5] = 0;
  373. v6[6] = 0;
  374. v6[7] = 0;
  375. v6[8] = 0;
  376. v6[9] = 0;
  377. v6[10] = 0xff;
  378. v6[11] = 0xff;
  379. v6[12] = v4[0];
  380. v6[13] = v4[1];
  381. v6[14] = v4[2];
  382. v6[15] = v4[3];
  383. }
  384. int
  385. v6tov4(uchar *v4, uchar *v6)
  386. {
  387. if(v6[0] == 0
  388. && v6[1] == 0
  389. && v6[2] == 0
  390. && v6[3] == 0
  391. && v6[4] == 0
  392. && v6[5] == 0
  393. && v6[6] == 0
  394. && v6[7] == 0
  395. && v6[8] == 0
  396. && v6[9] == 0
  397. && v6[10] == 0xff
  398. && v6[11] == 0xff)
  399. {
  400. v4[0] = v6[12];
  401. v4[1] = v6[13];
  402. v4[2] = v6[14];
  403. v4[3] = v6[15];
  404. return 0;
  405. } else {
  406. memset(v4, 0, 4);
  407. return -1;
  408. }
  409. }
  410. ulong
  411. parseip(uchar *to, char *from)
  412. {
  413. int i, elipsis = 0, v4 = 1;
  414. ulong x;
  415. char *p, *op;
  416. memset(to, 0, IPaddrlen);
  417. p = from;
  418. for(i = 0; i < 16 && *p; i+=2){
  419. op = p;
  420. x = strtoul(p, &p, 16);
  421. if(*p == '.' || (*p == 0 && i == 0)){
  422. p = v4parseip(to+i, op);
  423. i += 4;
  424. break;
  425. } else {
  426. to[i] = x>>8;
  427. to[i+1] = x;
  428. }
  429. if(*p == ':'){
  430. v4 = 0;
  431. if(*++p == ':'){
  432. elipsis = i+2;
  433. p++;
  434. }
  435. }
  436. }
  437. if(i < 16){
  438. memmove(&to[elipsis+16-i], &to[elipsis], i-elipsis);
  439. memset(&to[elipsis], 0, 16-i);
  440. }
  441. if(v4){
  442. to[10] = to[11] = 0xff;
  443. return nhgetl(to+12);
  444. } else
  445. return 6;
  446. }
  447. /*
  448. * hack to allow ip v4 masks to be entered in the old
  449. * style
  450. */
  451. ulong
  452. parseipmask(uchar *to, char *from)
  453. {
  454. ulong x;
  455. int i;
  456. uchar *p;
  457. if(*from == '/'){
  458. /* as a number of prefix bits */
  459. i = atoi(from+1);
  460. if(i < 0)
  461. i = 0;
  462. if(i > 128)
  463. i = 128;
  464. memset(to, 0, IPaddrlen);
  465. for(p = to; i >= 8; i -= 8)
  466. *p++ = 0xff;
  467. if(i > 0)
  468. *p = ~((1<<(8-i))-1);
  469. x = nhgetl(to+IPv4off);
  470. } else {
  471. /* as a straight bit mask */
  472. x = parseip(to, from);
  473. if(memcmp(to, v4prefix, IPv4off) == 0)
  474. memset(to, 0xff, IPv4off);
  475. }
  476. return x;
  477. }
  478. void
  479. maskip(uchar *from, uchar *mask, uchar *to)
  480. {
  481. int i;
  482. for(i = 0; i < IPaddrlen; i++)
  483. to[i] = from[i] & mask[i];
  484. }
  485. uchar classmask[4][16] = {
  486. 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0x00,0x00,0x00,
  487. 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0x00,0x00,0x00,
  488. 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0x00,0x00,
  489. 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0x00,
  490. };
  491. uchar*
  492. defmask(uchar *ip)
  493. {
  494. if(isv4(ip))
  495. return classmask[ip[IPv4off]>>6];
  496. else {
  497. if(ipcmp(ip, v6loopback) == 0)
  498. return IPallbits;
  499. else if(memcmp(ip, v6linklocal, v6linklocalprefix) == 0)
  500. return v6linklocalmask;
  501. else if(memcmp(ip, v6sitelocal, v6sitelocalprefix) == 0)
  502. return v6sitelocalmask;
  503. else if(memcmp(ip, v6solicitednode, v6solicitednodeprefix) == 0)
  504. return v6solicitednodemask;
  505. else if(memcmp(ip, v6multicast, v6multicastprefix) == 0)
  506. return v6multicastmask;
  507. return IPallbits;
  508. }
  509. }
  510. void
  511. ipv62smcast(uchar *smcast, uchar *a)
  512. {
  513. assert(IPaddrlen == 16);
  514. memmove(smcast, v6solicitednode, IPaddrlen);
  515. smcast[13] = a[13];
  516. smcast[14] = a[14];
  517. smcast[15] = a[15];
  518. }
  519. /*
  520. * parse a hex mac address
  521. */
  522. int
  523. parsemac(uchar *to, char *from, int len)
  524. {
  525. char nip[4];
  526. char *p;
  527. int i;
  528. p = from;
  529. memset(to, 0, len);
  530. for(i = 0; i < len; i++){
  531. if(p[0] == '\0' || p[1] == '\0')
  532. break;
  533. nip[0] = p[0];
  534. nip[1] = p[1];
  535. nip[2] = '\0';
  536. p += 2;
  537. to[i] = strtoul(nip, 0, 16);
  538. if(*p == ':')
  539. p++;
  540. }
  541. return i;
  542. }
  543. /*
  544. * hashing tcp, udp, ... connections
  545. */
  546. ulong
  547. iphash(uchar *sa, ushort sp, uchar *da, ushort dp)
  548. {
  549. return ((sa[IPaddrlen-1]<<24) ^ (sp << 16) ^ (da[IPaddrlen-1]<<8) ^ dp ) % Nhash;
  550. }
  551. void
  552. iphtadd(Ipht *ht, Conv *c)
  553. {
  554. ulong hv;
  555. Iphash *h;
  556. hv = iphash(c->raddr, c->rport, c->laddr, c->lport);
  557. h = smalloc(sizeof(*h));
  558. if(ipcmp(c->raddr, IPnoaddr) != 0)
  559. h->match = IPmatchexact;
  560. else {
  561. if(ipcmp(c->laddr, IPnoaddr) != 0){
  562. if(c->lport == 0)
  563. h->match = IPmatchaddr;
  564. else
  565. h->match = IPmatchpa;
  566. } else {
  567. if(c->lport == 0)
  568. h->match = IPmatchany;
  569. else
  570. h->match = IPmatchport;
  571. }
  572. }
  573. h->c = c;
  574. lock(ht);
  575. h->next = ht->tab[hv];
  576. ht->tab[hv] = h;
  577. unlock(ht);
  578. }
  579. void
  580. iphtrem(Ipht *ht, Conv *c)
  581. {
  582. ulong hv;
  583. Iphash **l, *h;
  584. hv = iphash(c->raddr, c->rport, c->laddr, c->lport);
  585. lock(ht);
  586. for(l = &ht->tab[hv]; (*l) != nil; l = &(*l)->next)
  587. if((*l)->c == c){
  588. h = *l;
  589. (*l) = h->next;
  590. free(h);
  591. break;
  592. }
  593. unlock(ht);
  594. }
  595. /* look for a matching conversation with the following precedence
  596. * connected && raddr,rport,laddr,lport
  597. * announced && laddr,lport
  598. * announced && *,lport
  599. * announced && laddr,*
  600. * announced && *,*
  601. */
  602. Conv*
  603. iphtlook(Ipht *ht, uchar *sa, ushort sp, uchar *da, ushort dp)
  604. {
  605. ulong hv;
  606. Iphash *h;
  607. Conv *c;
  608. /* exact 4 pair match (connection) */
  609. hv = iphash(sa, sp, da, dp);
  610. lock(ht);
  611. for(h = ht->tab[hv]; h != nil; h = h->next){
  612. if(h->match != IPmatchexact)
  613. continue;
  614. c = h->c;
  615. if(sp == c->rport && dp == c->lport
  616. && ipcmp(sa, c->raddr) == 0 && ipcmp(da, c->laddr) == 0){
  617. unlock(ht);
  618. return c;
  619. }
  620. }
  621. /* match local address and port */
  622. hv = iphash(IPnoaddr, 0, da, dp);
  623. for(h = ht->tab[hv]; h != nil; h = h->next){
  624. if(h->match != IPmatchpa)
  625. continue;
  626. c = h->c;
  627. if(dp == c->lport && ipcmp(da, c->laddr) == 0){
  628. unlock(ht);
  629. return c;
  630. }
  631. }
  632. /* match just port */
  633. hv = iphash(IPnoaddr, 0, IPnoaddr, dp);
  634. for(h = ht->tab[hv]; h != nil; h = h->next){
  635. if(h->match != IPmatchport)
  636. continue;
  637. c = h->c;
  638. if(dp == c->lport){
  639. unlock(ht);
  640. return c;
  641. }
  642. }
  643. /* match local address */
  644. hv = iphash(IPnoaddr, 0, da, 0);
  645. for(h = ht->tab[hv]; h != nil; h = h->next){
  646. if(h->match != IPmatchaddr)
  647. continue;
  648. c = h->c;
  649. if(ipcmp(da, c->laddr) == 0){
  650. unlock(ht);
  651. return c;
  652. }
  653. }
  654. /* look for something that matches anything */
  655. hv = iphash(IPnoaddr, 0, IPnoaddr, 0);
  656. for(h = ht->tab[hv]; h != nil; h = h->next){
  657. if(h->match != IPmatchany)
  658. continue;
  659. c = h->c;
  660. unlock(ht);
  661. return c;
  662. }
  663. unlock(ht);
  664. return nil;
  665. }