Annotation of ansh/src/utils.c, revision 1.1.1.1.2.5

1.1       misho       1: /*************************************************************************
                      2:  * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
                      3:  *  by Michael Pounov <misho@elwix.org>
                      4:  *
                      5:  * $Author: misho $
1.1.1.1.2.5! misho       6:  * $Id: utils.c,v 1.1.1.1.2.4 2011/10/10 14:15:40 misho Exp $
1.1       misho       7:  *
                      8:  *************************************************************************/
                      9: #include "global.h"
                     10: 
                     11: 
                     12: void
                     13: Get1stEth(char *psDev, int devlen)
                     14: {
                     15:        struct ifaddrs *ifa;
                     16: 
                     17:        assert(psDev);
                     18:        assert(devlen > 0);
                     19: 
                     20:        getifaddrs(&ifa);
                     21:        strlcpy(psDev, ifa->ifa_name, devlen);
                     22:        freeifaddrs(ifa);
                     23: }
                     24: 
                     25: int
                     26: PrepareL2(const char *psDev, int *bpflen)
                     27: {
                     28:        int h, n = 1;
                     29:        register int i;
                     30:        char szStr[STRSIZ];
                     31:        struct ifreq ifr;
1.1.1.1.2.3  misho      32:        struct bpf_program fcode = { 0 };
                     33:        struct bpf_insn insns[] = {
                     34:                BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
                     35:                BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ANSH_ID, 0, 1),
                     36:                BPF_STMT(BPF_RET + BPF_K, -1),
                     37:                BPF_STMT(BPF_RET + BPF_K, 0),
                     38:        };
1.1       misho      39: 
                     40:        FTRACE(3);
                     41:        assert(psDev);
                     42: 
1.1.1.1.2.3  misho      43:        fcode.bf_len = sizeof(insns) / sizeof(struct bpf_insn);
                     44:        fcode.bf_insns = insns;
                     45: 
1.1       misho      46:        for (i = 0; i < 10; i++) {
                     47:                memset(szStr, 0, sizeof szStr);
                     48:                snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
                     49:                h = open(szStr, O_RDWR);
                     50:                if (h > 2)
                     51:                        break;
                     52:        }
                     53:        if (h < 3) {
                     54:                printf("Error:: open bpf %s #%d - %s\n", szStr, errno, strerror(errno));
                     55:                return -1;
                     56:        }
                     57: 
1.1.1.1.2.5! misho      58:        if (ioctl(h, BIOCIMMEDIATE, &n) == -1) {
1.1.1.1.2.3  misho      59:                printf("Error:: set interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno));
                     60:                close(h);
                     61:                return -1;
                     62:        }
1.1.1.1.2.5! misho      63:        strlcpy(ifr.ifr_name, psDev, sizeof ifr.ifr_name);
        !            64:        if (ioctl(h, BIOCSETIF, &ifr) == -1) {
        !            65:                printf("Error:: bind interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno));
1.1       misho      66:                close(h);
                     67:                return -1;
                     68:        }
1.1.1.1.2.3  misho      69:        if (ioctl(h, BIOCSETF, &fcode) == -1) {
                     70:                printf("Error:: set filter interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno));
                     71:                close(h);
                     72:                return -1;
                     73:        }
1.1       misho      74:        if (ioctl(h, BIOCGBLEN, bpflen) == -1) {
                     75:                printf("Error:: get interface %s buffer length #%d - %s\n", psDev, errno, strerror(errno));
                     76:                close(h);
                     77:                return -1;
                     78:        }
                     79: 
1.1.1.1.2.4  misho      80:        /*
1.1.1.1.2.3  misho      81:        n = fcntl(h, F_GETFL);
                     82:        fcntl(h, F_SETFL, n | O_NONBLOCK);
1.1.1.1.2.4  misho      83:        */
1.1.1.1.2.3  misho      84: 
1.1       misho      85:        VERB(3) LOG("Openned device handle %d with bpf buflen %d", h, *bpflen);
                     86:        return h;
                     87: }
                     88: 
                     89: int
                     90: PrepareL3(const struct sockaddr *sa, int *bpflen)
                     91: {
                     92:        int h, n = 1;
                     93: 
                     94:        FTRACE(3);
                     95:        assert(sa);
                     96: 
                     97:        h = socket(sa->sa_family, SOCK_RAW, IPPROTO_ICMP);
                     98:        if (h == -1) {
                     99:                printf("Error:: Cant open raw socket #%d - %s\n", errno, strerror(errno));
                    100:                return -1;
                    101:        }
                    102:        /*
                    103:        if (setsockopt(h, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
                    104:                printf("Error:: Cant set raw socket #%d - %s\n", errno, strerror(errno));
                    105:                close(h);
                    106:                return -1;
                    107:        }
                    108:        */
                    109:        if (bind(h, sa, sizeof(struct sockaddr)) == -1) {
                    110:                printf("Error:: Cant bind to raw socket #%d - %s\n", errno, strerror(errno));
                    111:                close(h);
                    112:                return -1;
                    113:        }
                    114: 
                    115:        n = fcntl(h, F_GETFL);
                    116:        fcntl(h, F_SETFL, n | O_NONBLOCK);
                    117: 
                    118:        *bpflen = USHRT_MAX;
                    119:        VERB(3) LOG("Openned socket handle %d", h);
                    120:        return h;
                    121: }
                    122: 
                    123: char
1.1.1.1.2.1  misho     124: icmpRecv(int s, u_short * __restrict id, u_int * __restrict crypted, u_char * __restrict data, 
1.1       misho     125:                int * __restrict datlen, struct sockaddr *sa, socklen_t *salen)
                    126: {
                    127:        int ret = 0;
                    128:        struct icmp *icmp;
                    129:        struct ansh_hdr *hdr;
                    130:        u_char buf[USHRT_MAX] = { 0 };
                    131:        u_int crc;
                    132: 
                    133:        ret = recvfrom(s, buf, sizeof buf, 0, sa, salen);
                    134:        if (ret == -1) {
                    135:                ERR("Receive recvfrom() #%d - %s", errno, strerror(errno));
                    136:                return ANSH_FLG_ERR;
                    137:        } else
                    138:                VERB(4) LOG("Get packet with len=%d", ret);
                    139: 
                    140:        /* check header len */
                    141:        if (ret < (sizeof(struct ip) + sizeof(struct icmp) + sizeof(struct ansh_hdr))) {
                    142:                VERB(1) LOG("Discard packet too short %d ...", ret);
                    143:                return ANSH_FLG_ERR;
                    144:        } else
                    145:                icmp = (struct icmp*) (buf + sizeof(struct ip));
                    146: 
                    147:        /* check echo magic ansh code */
                    148:        if (icmp->icmp_type != ICMP_ECHOREPLY || icmp->icmp_code != ANSH_CODE) {
                    149:                VERB(3) LOG("Packet isnt for me %d ... icmp_code=%d", ret, icmp->icmp_code);
                    150:                return ANSH_FLG_ERR;
                    151:        } else
                    152:                hdr = (struct ansh_hdr*) (buf + sizeof(struct ip) + sizeof(struct icmp));
                    153: 
                    154:        /* check version and total size of packet */
                    155:        if (hdr->ansh_ver != ANSH_VERSION) {
                    156:                VERB(3) LOG("Packet with wrong version ...");
                    157:                return ANSH_FLG_ERR;
                    158:        }
1.1.1.1.2.1  misho     159:        if (crypted) {
                    160:                if (hdr->ansh_nonce && !*crypted) {
                    161:                        VERB(3) LOG("Channel INSECURED:: Crypted communication not supported at this moment ...");
                    162:                        return ANSH_FLG_ERR;
                    163:                }
                    164:                if (!hdr->ansh_nonce && *crypted) {
                    165:                        VERB(3) LOG("Channel SECURED:: Plain text communication not supported at this moment ...");
                    166:                        return ANSH_FLG_ERR;
                    167:                }
                    168: 
                    169:                *crypted = ntohl(hdr->ansh_nonce);
                    170:        }
1.1       misho     171: 
                    172:        /* check crc of packet */
                    173:        crc = hdr->ansh_crc;
                    174:        hdr->ansh_crc ^= hdr->ansh_crc;
                    175:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    176:        if (crc != hdr->ansh_crc) {
                    177:                VERB(3) LOG("Packet with wrong crc ...");
                    178:                return ANSH_FLG_ERR;
                    179:        }
                    180: 
                    181:        /* copy data */
                    182:        if (data && datlen) {
                    183:                memset(data, 0, *datlen);
                    184:                *datlen = ntohs(hdr->ansh_len) - sizeof(struct ansh_hdr);
                    185:                memcpy(data, buf + sizeof(struct ip) + sizeof(struct icmp) + sizeof(struct ansh_hdr), *datlen);
                    186:        }
                    187: 
                    188:        if (id)
                    189:                *id = ntohs(icmp->icmp_id);
                    190:        return hdr->ansh_flg;
                    191: }
                    192: 
                    193: int
1.1.1.1.2.1  misho     194: icmpSend(int s, u_short id, char flg, u_int crypted, u_char *data, int datlen, struct sockaddr *sa, socklen_t salen)
1.1       misho     195: {
                    196:        u_char *pos, buf[USHRT_MAX] = { 0 };
                    197:        struct icmp *icmp;
                    198:        struct ansh_hdr *hdr;
                    199:        int ret = 0;
                    200: 
                    201:        assert(data);
                    202:        if ((sizeof buf - sizeof(struct icmp) + sizeof(struct ansh_hdr)) < datlen)
                    203:                return ANSH_FLG_ERR;
                    204: 
                    205:        icmp = (struct icmp*) buf;
                    206:        hdr = (struct ansh_hdr*) (buf + sizeof(struct icmp));
                    207:        pos = buf + sizeof(struct icmp) + sizeof(struct ansh_hdr);
                    208: 
                    209:        memcpy(pos, data, datlen);
                    210: 
                    211:        hdr->ansh_ver = ANSH_VERSION;
                    212:        hdr->ansh_flg = flg;
                    213:        hdr->ansh_len = htons(datlen + sizeof(struct ansh_hdr));
1.1.1.1.2.1  misho     214:        hdr->ansh_nonce = htonl(crypted);
1.1       misho     215:        hdr->ansh_crc = 0;
                    216:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    217: 
                    218:        icmp->icmp_type = ICMP_ECHOREPLY;
                    219:        icmp->icmp_code = ANSH_CODE;
                    220:        icmp->icmp_id = htons(id);
                    221:        icmp->icmp_seq = htons(datlen);
                    222:        icmp->icmp_cksum = 0;
                    223:        icmp->icmp_cksum = crcIP(buf, sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen);
                    224: 
                    225:        if ((ret = sendto(s, buf, sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen, 
                    226:                                        0, sa, salen)) == -1) {
                    227:                ERR("Send sendto() #%d - %s", errno, strerror(errno));
                    228:                return ANSH_FLG_ERR;
                    229:        } else
                    230:                VERB(4) LOG("Put packet with len=%d", ret);
                    231:        if (ret != sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen) {
                    232:                VERB(3) LOG("Sended data %d is different from source data len %d", ret, 
                    233:                                sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen);
                    234:                return ANSH_FLG_ERR;
                    235:        }
                    236: 
                    237:        return ret;
                    238: }
                    239: 
1.1.1.1.2.5! misho     240: static int
        !           241: _pkt_Send(int s, char flg, u_int crypted, u_char *data, int datlen, struct ether_addr *ea)
1.1       misho     242: {
                    243:        u_char *pos, buf[USHRT_MAX] = { 0 };
                    244:        struct ether_header *e = (struct ether_header*) buf;
                    245:        struct ansh_hdr *hdr;
                    246:        int ret = 0;
                    247: 
                    248:        assert(data);
                    249:        if ((sizeof buf - ETHER_HDR_LEN + sizeof(struct ansh_hdr)) < datlen)
                    250:                return ANSH_FLG_ERR;
                    251: 
1.1.1.1.2.3  misho     252:        e->ether_type = ntohs(ANSH_ID);
1.1       misho     253:        memcpy(e->ether_dhost, ea->octet, ETHER_ADDR_LEN);
                    254:        hdr = (struct ansh_hdr*) (buf + ETHER_HDR_LEN);
                    255:        pos = ((u_char*) hdr) + sizeof(struct ansh_hdr);
                    256: 
                    257:        memcpy(pos, data, datlen);
                    258: 
                    259:        hdr->ansh_ver = ANSH_VERSION;
                    260:        hdr->ansh_flg = flg;
                    261:        hdr->ansh_len = htons(datlen + sizeof(struct ansh_hdr));
1.1.1.1.2.2  misho     262:        hdr->ansh_nonce = htonl(crypted);
1.1       misho     263:        hdr->ansh_crc = 0;
                    264:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    265: 
                    266:        if ((ret = write(s, buf, ETHER_HDR_LEN + sizeof(struct ansh_hdr) + datlen)) == -1) {
                    267:                ERR("Send packet() #%d - %s", errno, strerror(errno));
                    268:                return ANSH_FLG_ERR;
                    269:        } else
                    270:                VERB(4) LOG("Put packet with len=%d", ret);
                    271:        if (ret != ETHER_HDR_LEN + sizeof(struct ansh_hdr) + datlen) {
                    272:                VERB(3) LOG("Sended data %d is different from source data len %d", ret, 
                    273:                                ETHER_HDR_LEN + sizeof(struct ansh_hdr) + datlen);
                    274:                return ANSH_FLG_ERR;
                    275:        }
                    276: 
                    277:        return ret;
                    278: }
                    279: 
1.1.1.1.2.5! misho     280: int
        !           281: pktSend(int s, char flg, u_int crypted, u_char *data, int datlen, struct ether_addr *ea)
1.1       misho     282: {
1.1.1.1.2.5! misho     283:        int wlen, ret = 0;
        !           284:        u_char *pos = data;
1.1       misho     285: 
1.1.1.1.2.5! misho     286:        while (datlen > -1) {
        !           287:                wlen = _pkt_Send(s, flg, crypted, pos, (datlen > 512) ? 512 : datlen, ea);
        !           288:                if (wlen == -1)
        !           289:                        return -1;
        !           290:                else {
        !           291:                        pos += wlen;
        !           292:                        datlen -= wlen;
        !           293:                        ret += wlen;
        !           294:                }
1.1       misho     295:        }
                    296: 
1.1.1.1.2.5! misho     297:        return ret;
        !           298: }
1.1       misho     299: 
1.1.1.1.2.5! misho     300: static char
        !           301: _pkt_Recv(u_char * __restrict buf, int rlen, u_int * __restrict crypted, 
        !           302:                u_char * __restrict data, int * __restrict datlen, 
        !           303:                u_char ** __restrict next, int * __restrict nextlen)
        !           304: {
        !           305:        int bias;
        !           306:        struct bpf_hdr *bpf;
        !           307:        struct ansh_hdr *hdr;
        !           308:        u_int crc;
        !           309: 
        !           310:        if (rlen < (sizeof(struct bpf_hdr) + ETHER_HDR_LEN + sizeof(struct ansh_hdr))) {
        !           311:                VERB(1) LOG("Discard packet too short %d ...", rlen);
1.1       misho     312:                return ANSH_FLG_ERR;
                    313:        } else {
                    314:                bpf = (struct bpf_hdr*) buf;
                    315:                hdr = (struct ansh_hdr*) (buf + bpf->bh_hdrlen + ETHER_HDR_LEN);
                    316:        }
                    317: 
1.1.1.1.2.5! misho     318:        /* slice readed data to packets */
        !           319:        if ((bias = BPF_WORDALIGN(bpf->bh_hdrlen + bpf->bh_caplen)) < rlen) {
        !           320:                *next = buf + bias;
        !           321:                *nextlen = rlen - bias;
        !           322:        } else {
        !           323:                *next = NULL;
        !           324:                *nextlen = 0;
        !           325:        }
        !           326: 
1.1       misho     327:        /* check version and total size of packet */
                    328:        if (hdr->ansh_ver != ANSH_VERSION) {
                    329:                VERB(3) LOG("Packet with wrong version ... %d", hdr->ansh_ver);
                    330:                return ANSH_FLG_ERR;
                    331:        }
1.1.1.1.2.2  misho     332:        if (crypted) {
                    333:                if (hdr->ansh_nonce && !*crypted) {
                    334:                        VERB(3) LOG("Channel INSECURED:: Crypted communication not supported at this moment ...");
                    335:                        return ANSH_FLG_ERR;
                    336:                }
                    337:                if (!hdr->ansh_nonce && *crypted) {
                    338:                        VERB(3) LOG("Channel SECURED:: Plain text communication not supported at this moment ...");
                    339:                        return ANSH_FLG_ERR;
                    340:                }
                    341: 
                    342:                *crypted = ntohl(hdr->ansh_nonce);
                    343:        }
                    344: 
1.1       misho     345:        /* check crc of packet */
                    346:        crc = hdr->ansh_crc;
                    347:        hdr->ansh_crc ^= hdr->ansh_crc;
                    348:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    349:        if (crc != hdr->ansh_crc) {
                    350:                VERB(3) LOG("Packet with wrong crc ...");
                    351:                return ANSH_FLG_ERR;
                    352:        }
                    353: 
1.1.1.1.2.5! misho     354:        /* select data */
1.1       misho     355:        if (data) {
                    356:                *datlen = ntohs(hdr->ansh_len) - sizeof(struct ansh_hdr);
1.1.1.1.2.4  misho     357:                memcpy(data, buf + bpf->bh_hdrlen + ETHER_HDR_LEN + sizeof(struct ansh_hdr), *datlen);
1.1       misho     358:        }
                    359: 
1.1.1.1.2.5! misho     360:        return hdr->ansh_flg;
        !           361: }
        !           362: 
        !           363: char
        !           364: pktRecv(int s, u_int * __restrict crypted, u_char * __restrict data, int * __restrict datlen, 
        !           365:                struct ether_header *eth)
        !           366: {
        !           367:        u_char *buf, *next, *pos, *ptr;
        !           368:        int nextlen, rlen, buflen, ptrlen;
        !           369:        char flg;
        !           370:        struct bpf_hdr *bpf;
        !           371:        struct ether_header *e;
        !           372: 
        !           373:        if (!eth || !datlen)
        !           374:                return ANSH_FLG_ERR;
        !           375: 
        !           376:        if (!(buf = malloc(*datlen))) {
        !           377:                ERR("malloc() #%d - %s", errno, strerror(errno));
        !           378:                return ANSH_FLG_ERR;
        !           379:        }
        !           380: 
        !           381:        rlen = read(s, buf, *datlen);
        !           382:        if (rlen == -1) {
        !           383:                ERR("Receive packet() #%d - %s", errno, strerror(errno));
        !           384:                free(buf);
        !           385:                return ANSH_FLG_ERR;
        !           386:        } else
        !           387:                VERB(4) LOG("Get packet with len=%d", rlen);
        !           388: 
        !           389:        /* check header len */
        !           390:        if (rlen < (sizeof(struct bpf_hdr) + ETHER_HDR_LEN + sizeof(struct ansh_hdr))) {
        !           391:                VERB(1) LOG("Discard packet too short %d ...", rlen);
        !           392:                free(buf);
        !           393:                return ANSH_FLG_ERR;
        !           394:        } else {
        !           395:                bpf = (struct bpf_hdr*) buf;
        !           396:                e = (struct ether_header*) (buf + bpf->bh_hdrlen);
        !           397:                memcpy(eth, e, ETHER_HDR_LEN);
        !           398:        }
        !           399: 
        !           400:        ptr = next = buf;
        !           401:        ptrlen = nextlen = rlen;
        !           402:        pos = data;     
        !           403:        buflen = *datlen;
        !           404:        if ((flg = _pkt_Recv(ptr, ptrlen, crypted, pos, &buflen, &next, &nextlen)) == -1) {
        !           405:                free(buf);
        !           406:                return ANSH_FLG_ERR;
        !           407:        } else {
        !           408:                pos += buflen;
        !           409:                *datlen = buflen;
        !           410:                ptr = next;
        !           411:                ptrlen = nextlen;
        !           412:        }
        !           413:        while (next && nextlen > 0)
        !           414:                if (_pkt_Recv(ptr, ptrlen, crypted, pos, &buflen, &next, &nextlen) == -1)
        !           415:                        break;
        !           416:                else {
        !           417:                        pos += buflen;
        !           418:                        *datlen += buflen;
        !           419:                        ptr = next;
        !           420:                        ptrlen = nextlen;
        !           421:                }
        !           422: 
1.1       misho     423:        free(buf);
1.1.1.1.2.5! misho     424:        return flg;
1.1       misho     425: }
                    426: 
                    427: void *
                    428: TOfunc(sched_task_t *task)
                    429: {
                    430:        struct tagProc *proc;
                    431: 
                    432:        FTRACE(3);
                    433: 
                    434:        /* not found argument, drop data */
                    435:        if (!(proc = TASK_ARG(task)))
                    436:                return (void*) -1;
                    437: 
                    438:        if (proc->proc_pid)
                    439:                kill(proc->proc_pid, SIGTERM);
                    440: 
                    441:        return NULL;
                    442: }
                    443: 
1.1.1.1.2.1  misho     444: u_char *
                    445: cryptBuffer(u_char *buf, int rlen, u_int ctr)
                    446: {
                    447:        u_char *str, ivec[AES_BLOCK_SIZE] = { 0 };
                    448:        u_int rctr = htonl(ctr);
                    449: 
                    450:        FTRACE(3);
                    451: 
                    452:        if (!buf)
                    453:                return NULL;
                    454: 
                    455:        memcpy(ivec, &ctr, sizeof ctr);
                    456:        memcpy(ivec + 4, &rctr, sizeof rctr);
                    457:        memcpy(ivec + 8, &ctr, sizeof ctr);
                    458:        memcpy(ivec + 12, &rctr, sizeof rctr);
                    459: 
                    460:        if (io_ctr_AES(buf, rlen, &str, (u_char*) "_ansh_ELWIX_", ivec) == -1)
                    461:                return NULL;
                    462: 
                    463:        return str;
                    464: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>