/* ARGSUSED */ void dhcp_packet_lif(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg) { dhcp_lif_t *lif = arg; PKT_LIST *plp; uchar_t recv_type; const char *pname; uint_t xid; dhcp_smach_t *dsmp; if ((plp = recv_pkt(fd, lif->lif_max, B_FALSE)) == NULL) return; recv_type = pkt_recv_type(plp); pname = pkt_type_to_string(recv_type, B_FALSE); if (!pkt_v4_match(recv_type, DHCP_PACK | DHCP_PNAK | DHCP_PUNTYPED | DHCP_POFFER)) { dhcpmsg(MSG_VERBOSE, "dhcp_packet_lif: ignored v4 %s packet " "received via LIF %s", pname, lif->lif_name); free_pkt_entry(plp); return; } /* * Find the corresponding state machine. */ xid = pkt_get_xid(plp->pkt, B_FALSE); for (dsmp = lookup_smach_by_xid(xid, NULL, B_FALSE); dsmp != NULL; dsmp = lookup_smach_by_xid(xid, dsmp, B_FALSE)) { if (dsmp->dsm_lif == lif) break; } if (dsmp == NULL) goto drop; if (pkt_v4_match(recv_type, DHCP_PACK|DHCP_PNAK)) { /* * We've got an ACK/NAK; make sure it's acceptable and cancel * the REQUEST retransmissions. */ accept_v4_acknak(dsmp, plp); } else { if (is_bound_state(dsmp->dsm_state)) goto drop; /* * Must be an OFFER or a BOOTP message: enqueue it for later * processing by select_best(). */ pkt_smach_enqueue(dsmp, plp); } return; drop: dhcpmsg(MSG_VERBOSE, "dhcp_packet_lif: ignored %s packet xid " "%x received via LIF %s; %s", pname, xid, lif->lif_name, dsmp == NULL ? "unknown state machine" : "bound"); free_pkt_entry(plp); }
static int test_ping(void) { struct pkt *pkt; struct timeval tv; printf("ping: "); fflush(stdout); ping->pkt_icmp_msg->echo.icmp_id = rand_uint16(ctx.rnd); pkt = pkt_dup(ping); pkt->pkt_ip->ip_id = rand_uint16(ctx.rnd); ip_checksum(pkt->pkt_ip, pkt->pkt_end - pkt->pkt_eth_data); pcap_filter(ctx.pcap, "icmp[0] = 0 and src %s and dst %s", addr_ntoa(&ctx.dst), addr_ntoa(&ctx.src)); send_pkt(pkt); for (tv = read_tv; (pkt = recv_pkt(&tv)) != NULL; tv = read_tv) { if (memcmp(&pkt->pkt_icmp_msg->echo, &ping->pkt_icmp_msg->echo, 8) == 0) break; } printf("%s\n", pkt ? timeval_ntoa(&tv) : "no reply"); return (0); }
int rom_recv_SIZE(CalcHandle* handle, uint32_t* size) { uint16_t cmd, len; TRYF(recv_pkt(handle, &cmd, &len, (uint8_t *)size)); ticalcs_info(" TI->PC: SIZE (0x%08x bytes)", *size); return 0; }
int rom_recv_RDY(CalcHandle* handle) { uint16_t cmd, len; TRYF(recv_pkt(handle, &cmd, &len, NULL)); ticalcs_info(" TI->PC: %s", cmd ? "OK" : "KO"); return 0; }
static int test_frag(char *overlap, int drop) { struct timeval tv, save_tv = read_tv; struct pkt *pkt; struct icmp_msg_echo *echo; char *frag_argv[4]; if (overlap != NULL) printf("frag-%s: ", overlap); else if (drop) printf("frag-timeout (please wait): "); else printf("frag: "); fflush(stdout); ping->pkt_ip->ip_id = rand_uint16(ctx.rnd); ping->pkt_icmp_msg->echo.icmp_id = rand_uint16(ctx.rnd); pkt = pkt_dup(ping); ip_checksum(pkt->pkt_ip, pkt->pkt_end - pkt->pkt_eth_data); TAILQ_INSERT_TAIL(&ctx.pktq, pkt, pkt_next); frag_argv[0] = "ip_frag"; frag_argv[1] = "8"; frag_argv[2] = overlap; frag_argv[3] = NULL; mod_ip_frag.open(overlap ? 3 : 2, frag_argv, NULL); mod_ip_frag.apply(NULL, &ctx.pktq, NULL); if (drop) { pkt = TAILQ_LAST(&ctx.pktq, pktq); TAILQ_REMOVE(&ctx.pktq, pkt, pkt_next); pkt_free(pkt); save_tv.tv_sec = FRAG_TIMEOUT; } pcap_filter(ctx.pcap, "icmp[0] = %d and src %s and dst %s", drop ? 11 : 0, addr_ntoa(&ctx.dst), addr_ntoa(&ctx.src)); send_pktq(&ctx.pktq); for (tv = save_tv; (pkt = recv_pkt(&tv)) != NULL; tv = save_tv) { if (drop) { echo = (struct icmp_msg_echo *) (pkt->pkt_icmp_msg->timexceed.icmp_ip + IP_HDR_LEN + ICMP_HDR_LEN); } else { echo = &pkt->pkt_icmp_msg->echo; } if (echo->icmp_id == ping->pkt_icmp_msg->echo.icmp_id) break; } printf("%s\n", pkt ? timeval_ntoa(&tv) : "no reply"); return (0); }
int rom_recv_EXIT(CalcHandle* handle) { uint16_t cmd, len; int err = 0; err = recv_pkt(handle, &cmd, &len, NULL); ticalcs_info(" TI->PC: EXIT"); return err; }
void key_xchg (void) { if (init_session()) // initialize session key { if (recv_pkt()) // receive public key { modexp(); // encrypt the session key send_pkt(); // send session key } } }
void key_xchg (void) { uint8_t buf[2048]; uint32_t keylen=0; // send public send_pkt (rsa->n->d, rsa->n->dmax); // wait for encrypted session key keylen=recv_pkt (buf, sizeof(buf)); RSA_private_decrypt(keylen, session_key, buf, rsa, RSA_NO_PADDING); }
int rom_recv_DATA(CalcHandle* handle, uint16_t* size, uint8_t* data) { uint16_t cmd; uint16_t rpt; TRYF(recv_pkt(handle, &cmd, size, data)); if(cmd == CMD_DATA1) { ticalcs_info(" TI->PC: BLOCK (0x%04x bytes)", *size); std_blk++; return 0; } else if(cmd == CMD_DATA2) { *size = (data[1] << 8) | data[0]; rpt = (data[3] << 8) | data[2]; memset(data, rpt, *size); ticalcs_info(" TI->PC: BLOCK (0x%04x bytes)", *size); sav_blk++; return 0; } return -1; }
/* ARGSUSED */ void dhcp_acknak_global(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg) { PKT_LIST *plp; dhcp_pif_t *pif; uchar_t recv_type; const char *pname; uint_t xid; dhcp_smach_t *dsmp; boolean_t isv6 = (fd == v6_sock_fd); struct sockaddr_in sin; const char *reason; size_t sinlen = sizeof (sin); int sock; plp = recv_pkt(fd, get_max_mtu(isv6), isv6); if (plp == NULL) return; recv_type = pkt_recv_type(plp); pname = pkt_type_to_string(recv_type, isv6); /* * Find the corresponding state machine and pif. * * Note that DHCPv6 Reconfigure would be special: it's not the reply to * any transaction, and thus we would need to search on transaction ID * zero (all state machines) to find the match. However, Reconfigure * is not yet supported. */ xid = pkt_get_xid(plp->pkt, isv6); for (dsmp = lookup_smach_by_xid(xid, NULL, isv6); dsmp != NULL; dsmp = lookup_smach_by_xid(xid, dsmp, isv6)) { pif = dsmp->dsm_lif->lif_pif; if (pif->pif_index == plp->ifindex || pif->pif_under_ipmp && pif->pif_grindex == plp->ifindex) break; } if (dsmp == NULL) { dhcpmsg(MSG_VERBOSE, "dhcp_acknak_global: ignored v%d %s packet" " on ifindex %d: unknown state machine", isv6 ? 6 : 4, pname, plp->ifindex); free_pkt_entry(plp); return; } if (!isv6 && !pkt_v4_match(recv_type, DHCP_PACK|DHCP_PNAK)) { reason = "not ACK or NAK"; goto drop; } /* * For IPv4, most packets will be handled by dhcp_packet_lif(). The * only exceptions are broadcast packets sent when lif_sock_ip_fd has * bound to something other than INADDR_ANY. */ if (!isv6) { sock = dsmp->dsm_lif->lif_sock_ip_fd; if (getsockname(sock, (struct sockaddr *)&sin, &sinlen) != -1 && sin.sin_addr.s_addr == INADDR_ANY) { reason = "handled by lif_sock_ip_fd"; goto drop; } } /* * We've got a packet; make sure it's acceptable and cancel the REQUEST * retransmissions. */ if (isv6) accept_v6_message(dsmp, plp, pname, recv_type); else accept_v4_acknak(dsmp, plp); return; drop: dhcpmsg(MSG_VERBOSE, "dhcp_acknak_global: ignored v%d %s packet for %s " "received on global socket: %s", isv6 ? 6 : 4, pname, pif->pif_name, reason); free_pkt_entry(plp); }
static int test_ip_tracert(void) { struct timeval tv; struct hop hops[IP_TTL_DEFAULT]; struct pkt *pkt; struct icmp_msg_echo *echo; int i, hopcnt, max_ttl; printf("ip-tracert: "); fflush(stdout); pcap_filter(ctx.pcap, "icmp[0] = 0 and src %s and dst %s", addr_ntoa(&ctx.dst), addr_ntoa(&ctx.src)); ping->pkt_icmp_msg->echo.icmp_id = rand_uint16(ctx.rnd); pkt = pkt_dup(ping); pkt->pkt_ip->ip_id = rand_uint16(ctx.rnd); ip_checksum(pkt->pkt_ip, pkt->pkt_end - pkt->pkt_eth_data); send_pkt(pkt); tv = read_tv; if ((pkt = recv_pkt(&tv)) == NULL) { printf("no reply\n"); return (0); } /* XXX - guess remote stack's starting TTL */ for (i = 2; pkt->pkt_ip->ip_ttl > i; i <<= 1) ; if ((max_ttl = i - pkt->pkt_ip->ip_ttl + 1) > IP_TTL_DEFAULT) max_ttl = IP_TTL_DEFAULT; printf("%s, %d hops max\n", ip_ntoa(&ping->pkt_ip->ip_dst), max_ttl); pcap_filter(ctx.pcap, "icmp and dst %s", addr_ntoa(&ctx.src)); for (i = 1; i < max_ttl + 1; i++) { pkt = pkt_dup(ping); pkt->pkt_ip->ip_id = rand_uint16(ctx.rnd); pkt->pkt_ip->ip_ttl = i; pkt->pkt_icmp_msg->echo.icmp_seq = htons(i); ip_checksum(pkt->pkt_ip, pkt->pkt_end - pkt->pkt_eth_data); send_pkt(pkt); usleep(42); /* XXX */ } memset(&hops, 0, sizeof(hops)); hopcnt = 0; for (tv = read_tv; (pkt = recv_pkt(&tv)) != NULL; tv = read_tv) { if ((pkt->pkt_icmp->icmp_type == ICMP_TIMEXCEED || pkt->pkt_icmp->icmp_type == ICMP_UNREACH) && pkt->pkt_end - pkt->pkt_eth_data >= (IP_HDR_LEN + ICMP_LEN_MIN) * 2) { echo = (struct icmp_msg_echo *) (pkt->pkt_icmp_msg->timexceed.icmp_ip + IP_HDR_LEN + ICMP_HDR_LEN); } else if (pkt->pkt_icmp->icmp_type == ICMP_ECHOREPLY) { echo = &pkt->pkt_icmp_msg->echo; } else continue; if (echo->icmp_id != ping->pkt_icmp_msg->echo.icmp_id) continue; i = ntohs(echo->icmp_seq); addr_pack(&hops[i].addr, ADDR_TYPE_IP, IP_ADDR_BITS, &pkt->pkt_ip->ip_src, IP_ADDR_LEN); memcpy(&hops[i].icmp, pkt->pkt_icmp, ICMP_HDR_LEN); hops[i].ttl = pkt->pkt_ip->ip_ttl; hopcnt++; if (pkt->pkt_ip->ip_src == ping->pkt_ip->ip_dst) break; } for (i = 1; i < hopcnt + 1; i++) { if (hops[i].addr.addr_type == ADDR_TYPE_IP) { printf("%2d %s (%d)\n", i, addr_ntoa(&hops[i].addr), hops[i].ttl); } else printf("%2d *\n", i); } return (0); }
static int test_ip_opt(void) { struct pkt *pkt; struct timeval tv; struct ip_opt opts[IP_OPT_MAX]; int i, len, max; printf("ip-opt: "); fflush(stdout); memset(&opts, 0, sizeof(opts)); max = 0; opts[max].opt_type = IP_OPT_SEC; opts[max].opt_len = IP_OPT_LEN + 9; max++; opts[max].opt_type = IP_OPT_LSRR; opts[max].opt_len = IP_OPT_LEN + 1 + 4; opts[max].opt_data.rr.ptr = 8; opts[max].opt_data.rr.iplist[0] = ping->pkt_ip->ip_src; max++; opts[max].opt_type = IP_OPT_TS; opts[max].opt_len = IP_OPT_LEN + 1 + 1 + 4; opts[max].opt_data.ts.ptr = 5; opts[max].opt_data.ts.flg = IP_OPT_TS_TSONLY; max++; opts[max].opt_type = IP_OPT_ESEC; opts[max].opt_len = IP_OPT_LEN; max++; opts[max].opt_type = IP_OPT_CIPSO; opts[max].opt_len = IP_OPT_LEN; max++; opts[max].opt_type = IP_OPT_RR; opts[max].opt_len = IP_OPT_LEN + 1 + 4; opts[max].opt_data.rr.ptr = 4; max++; opts[max].opt_type = IP_OPT_SATID; opts[max].opt_len = IP_OPT_LEN + 2; max++; opts[max].opt_type = IP_OPT_SSRR; opts[max].opt_len = IP_OPT_LEN + 1 + 4; opts[max].opt_data.rr.ptr = 8; opts[max].opt_data.rr.iplist[0] = ping->pkt_ip->ip_src; max++; pcap_filter(ctx.pcap, "icmp and src %s and dst %s", addr_ntoa(&ctx.dst), addr_ntoa(&ctx.src)); ping->pkt_icmp_msg->echo.icmp_id = rand_uint16(ctx.rnd); for (i = 0; i < max; i++) { pkt = pkt_dup(ping); pkt->pkt_ip->ip_id = rand_uint16(ctx.rnd); pkt->pkt_icmp_msg->echo.icmp_seq = opts[i].opt_type; len = ip_add_option(pkt->pkt_ip, PKT_BUF_LEN - ETH_HDR_LEN + IP_HDR_LEN, IP_PROTO_IP, &opts[i], opts[i].opt_len); pkt->pkt_end += len; ip_checksum(pkt->pkt_ip, pkt->pkt_end - pkt->pkt_eth_data); send_pkt(pkt); } i = 0; for (tv = read_tv; (pkt = recv_pkt(&tv)) != NULL; tv = read_tv) { if (pkt->pkt_icmp->icmp_type == ICMP_ECHOREPLY && pkt->pkt_icmp_msg->echo.icmp_id == ping->pkt_icmp_msg->echo.icmp_id) { i = IP_OPT_NUMBER(pkt->pkt_icmp_msg->echo.icmp_seq); printf("%s ", optnames[i]); } } printf("%s\n", i ? "" : "none"); return (0); }