141edb306SCy Schubert
241edb306SCy Schubert /*
341edb306SCy Schubert * Copyright (C) 2012 by Darren Reed.
441edb306SCy Schubert *
541edb306SCy Schubert * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert *
741edb306SCy Schubert * $Id$
841edb306SCy Schubert */
941edb306SCy Schubert
1041edb306SCy Schubert #include <ctype.h>
1141edb306SCy Schubert
1241edb306SCy Schubert #include "ipf.h"
1341edb306SCy Schubert #include "ipt.h"
1441edb306SCy Schubert
1541edb306SCy Schubert extern int opts;
1641edb306SCy Schubert
1741edb306SCy Schubert static char *tx_proto = "";
1841edb306SCy Schubert
1941edb306SCy Schubert static int text_open(char *), text_close(void);
2041edb306SCy Schubert static int text_readip(mb_t *, char **, int *);
2141edb306SCy Schubert static int parseline(char *, ip_t *, char **, int *);
2241edb306SCy Schubert
2341edb306SCy Schubert static char myflagset[] = "FSRPAUEC";
2441edb306SCy Schubert static u_char myflags[] = { TH_FIN, TH_SYN, TH_RST, TH_PUSH,
2541edb306SCy Schubert TH_ACK, TH_URG, TH_ECN, TH_CWR };
2641edb306SCy Schubert
2741edb306SCy Schubert struct ipread iptext = { text_open, text_close, text_readip, R_DO_CKSUM };
2841edb306SCy Schubert static FILE *tfp = NULL;
2941edb306SCy Schubert static int tfd = -1;
3041edb306SCy Schubert
3141edb306SCy Schubert static u_32_t tx_hostnum(char *, int *);
3241edb306SCy Schubert static u_short tx_portnum(char *);
3341edb306SCy Schubert
3441edb306SCy Schubert #ifdef USE_INET6
3541edb306SCy Schubert int parseipv6(char **, ip6_t *, char **, int *);
3641edb306SCy Schubert #endif
3741edb306SCy Schubert
3841edb306SCy Schubert /*
3941edb306SCy Schubert * returns an ip address as a long var as a result of either a DNS lookup or
4041edb306SCy Schubert * straight inet_addr() call
4141edb306SCy Schubert */
42efeb8bffSCy Schubert static u_32_t
tx_hostnum(char * host,int * resolved)43efeb8bffSCy Schubert tx_hostnum(char *host, int *resolved)
4441edb306SCy Schubert {
4541edb306SCy Schubert i6addr_t ipa;
4641edb306SCy Schubert
4741edb306SCy Schubert *resolved = 0;
4841edb306SCy Schubert if (!strcasecmp("any", host))
492582ae57SCy Schubert return (0L);
5041edb306SCy Schubert if (ISDIGIT(*host))
512582ae57SCy Schubert return (inet_addr(host));
5241edb306SCy Schubert
5341edb306SCy Schubert if (gethost(AF_INET, host, &ipa) == -1) {
5441edb306SCy Schubert *resolved = -1;
5541edb306SCy Schubert fprintf(stderr, "can't resolve hostname: %s\n", host);
562582ae57SCy Schubert return (0);
5741edb306SCy Schubert }
582582ae57SCy Schubert return (ipa.in4.s_addr);
5941edb306SCy Schubert }
6041edb306SCy Schubert
6141edb306SCy Schubert
6241edb306SCy Schubert /*
6341edb306SCy Schubert * find the port number given by the name, either from getservbyname() or
6441edb306SCy Schubert * straight atoi()
6541edb306SCy Schubert */
66efeb8bffSCy Schubert static u_short
tx_portnum(char * name)67efeb8bffSCy Schubert tx_portnum(char *name)
6841edb306SCy Schubert {
6941edb306SCy Schubert struct servent *sp;
7041edb306SCy Schubert
7141edb306SCy Schubert if (ISDIGIT(*name))
7241edb306SCy Schubert return (u_short)atoi(name);
7341edb306SCy Schubert sp = getservbyname(name, tx_proto);
7441edb306SCy Schubert if (sp)
752582ae57SCy Schubert return (ntohs(sp->s_port));
7641edb306SCy Schubert (void) fprintf(stderr, "unknown service \"%s\".\n", name);
772582ae57SCy Schubert return (0);
7841edb306SCy Schubert }
7941edb306SCy Schubert
8041edb306SCy Schubert
81efeb8bffSCy Schubert static int
text_open(char * fname)82efeb8bffSCy Schubert text_open(char *fname)
8341edb306SCy Schubert {
8441edb306SCy Schubert if (tfp && tfd != -1) {
8541edb306SCy Schubert rewind(tfp);
862582ae57SCy Schubert return (tfd);
8741edb306SCy Schubert }
8841edb306SCy Schubert
8941edb306SCy Schubert if (!strcmp(fname, "-")) {
9041edb306SCy Schubert tfd = 0;
9141edb306SCy Schubert tfp = stdin;
9241edb306SCy Schubert } else {
9341edb306SCy Schubert tfd = open(fname, O_RDONLY);
9441edb306SCy Schubert if (tfd != -1)
9541edb306SCy Schubert tfp = fdopen(tfd, "r");
9641edb306SCy Schubert }
972582ae57SCy Schubert return (tfd);
9841edb306SCy Schubert }
9941edb306SCy Schubert
10041edb306SCy Schubert
101efeb8bffSCy Schubert static int
text_close(void)102efeb8bffSCy Schubert text_close(void)
10341edb306SCy Schubert {
10441edb306SCy Schubert int cfd = tfd;
10541edb306SCy Schubert
10641edb306SCy Schubert tfd = -1;
1072582ae57SCy Schubert return (close(cfd));
10841edb306SCy Schubert }
10941edb306SCy Schubert
11041edb306SCy Schubert
111efeb8bffSCy Schubert static int
text_readip(mb_t * mb,char ** ifn,int * dir)112efeb8bffSCy Schubert text_readip(mb_t *mb, char **ifn, int *dir)
11341edb306SCy Schubert {
11441edb306SCy Schubert register char *s;
11541edb306SCy Schubert char line[513];
11641edb306SCy Schubert ip_t *ip;
11741edb306SCy Schubert char *buf;
11841edb306SCy Schubert
11941edb306SCy Schubert buf = (char *)mb->mb_buf;
12041edb306SCy Schubert
12141edb306SCy Schubert *ifn = NULL;
12241edb306SCy Schubert while (fgets(line, sizeof(line)-1, tfp)) {
12341edb306SCy Schubert if ((s = strchr(line, '\n')))
12441edb306SCy Schubert *s = '\0';
12541edb306SCy Schubert if ((s = strchr(line, '\r')))
12641edb306SCy Schubert *s = '\0';
12741edb306SCy Schubert if ((s = strchr(line, '#')))
12841edb306SCy Schubert *s = '\0';
12941edb306SCy Schubert if (!*line)
13041edb306SCy Schubert continue;
13141edb306SCy Schubert if ((opts & OPT_DEBUG) != 0)
13241edb306SCy Schubert printf("input: %s\n", line);
13341edb306SCy Schubert *ifn = NULL;
13441edb306SCy Schubert *dir = 0;
13541edb306SCy Schubert if (!parseline(line, (ip_t *)buf, ifn, dir)) {
13641edb306SCy Schubert ip = (ip_t *)buf;
13741edb306SCy Schubert if (IP_V(ip) == 6) {
13841edb306SCy Schubert #ifdef USE_INET6
13941edb306SCy Schubert mb->mb_len = ntohs(((ip6_t *)ip)->ip6_plen) +
14041edb306SCy Schubert sizeof(ip6_t);
14141edb306SCy Schubert #else
14241edb306SCy Schubert mb->mb_len = 0;
14341edb306SCy Schubert #endif
14441edb306SCy Schubert } else {
14541edb306SCy Schubert mb->mb_len = ntohs(ip->ip_len);
14641edb306SCy Schubert }
1472582ae57SCy Schubert return (mb->mb_len);
14841edb306SCy Schubert }
14941edb306SCy Schubert }
15041edb306SCy Schubert if (feof(tfp))
1512582ae57SCy Schubert return (0);
1522582ae57SCy Schubert return (-1);
15341edb306SCy Schubert }
15441edb306SCy Schubert
155efeb8bffSCy Schubert static int
parseline(char * line,ip_t * ip,char ** ifn,int * out)156efeb8bffSCy Schubert parseline(char *line, ip_t *ip, char **ifn, int *out)
15741edb306SCy Schubert {
15841edb306SCy Schubert tcphdr_t th, *tcp = &th;
15941edb306SCy Schubert struct icmp icmp, *ic = &icmp;
16041edb306SCy Schubert char *cps[20], **cpp, c, ipopts[68];
16141edb306SCy Schubert int i, r;
16241edb306SCy Schubert
16341edb306SCy Schubert if (*ifn)
16441edb306SCy Schubert free(*ifn);
16541edb306SCy Schubert bzero((char *)ip, MAX(sizeof(*tcp), sizeof(*ic)) + sizeof(*ip));
16641edb306SCy Schubert bzero((char *)tcp, sizeof(*tcp));
16741edb306SCy Schubert bzero((char *)ic, sizeof(*ic));
16841edb306SCy Schubert bzero(ipopts, sizeof(ipopts));
16941edb306SCy Schubert IP_HL_A(ip, sizeof(*ip) >> 2);
17041edb306SCy Schubert IP_V_A(ip, IPVERSION);
17141edb306SCy Schubert ip->ip_ttl = 63;
17241edb306SCy Schubert for (i = 0, cps[0] = strtok(line, " \b\t\r\n"); cps[i] && i < 19; )
17341edb306SCy Schubert cps[++i] = strtok(NULL, " \b\t\r\n");
17441edb306SCy Schubert
17541edb306SCy Schubert cpp = cps;
17641edb306SCy Schubert if (!*cpp)
1772582ae57SCy Schubert return (1);
17841edb306SCy Schubert
17941edb306SCy Schubert c = **cpp;
18041edb306SCy Schubert if (!ISALPHA(c) || (TOLOWER(c) != 'o' && TOLOWER(c) != 'i')) {
18141edb306SCy Schubert fprintf(stderr, "bad direction \"%s\"\n", *cpp);
1822582ae57SCy Schubert return (1);
18341edb306SCy Schubert }
18441edb306SCy Schubert
18541edb306SCy Schubert #ifdef USE_INET6
18641edb306SCy Schubert if (!strcasecmp(*cpp, "out6") || !strcasecmp(*cpp, "in6")) {
1872582ae57SCy Schubert return (parseipv6(cpp, (ip6_t *)ip, ifn, out));
18841edb306SCy Schubert }
18941edb306SCy Schubert #endif
19041edb306SCy Schubert
19141edb306SCy Schubert *out = (TOLOWER(c) == 'o') ? 1 : 0;
19241edb306SCy Schubert cpp++;
19341edb306SCy Schubert if (!*cpp)
1942582ae57SCy Schubert return (1);
19541edb306SCy Schubert
19641edb306SCy Schubert if (!strcasecmp(*cpp, "on")) {
19741edb306SCy Schubert cpp++;
19841edb306SCy Schubert if (!*cpp)
1992582ae57SCy Schubert return (1);
20041edb306SCy Schubert *ifn = strdup(*cpp++);
20141edb306SCy Schubert if (!*cpp)
2022582ae57SCy Schubert return (1);
20341edb306SCy Schubert }
20441edb306SCy Schubert
20541edb306SCy Schubert c = **cpp;
20641edb306SCy Schubert ip->ip_len = sizeof(ip_t);
20741edb306SCy Schubert if (!strcasecmp(*cpp, "tcp") || !strcasecmp(*cpp, "udp") ||
20841edb306SCy Schubert !strcasecmp(*cpp, "icmp")) {
20941edb306SCy Schubert if (c == 't') {
21041edb306SCy Schubert ip->ip_p = IPPROTO_TCP;
21141edb306SCy Schubert ip->ip_len += sizeof(struct tcphdr);
21241edb306SCy Schubert tx_proto = "tcp";
21341edb306SCy Schubert } else if (c == 'u') {
21441edb306SCy Schubert ip->ip_p = IPPROTO_UDP;
21541edb306SCy Schubert ip->ip_len += sizeof(struct udphdr);
21641edb306SCy Schubert tx_proto = "udp";
21741edb306SCy Schubert } else {
21841edb306SCy Schubert ip->ip_p = IPPROTO_ICMP;
21941edb306SCy Schubert ip->ip_len += ICMPERR_IPICMPHLEN;
22041edb306SCy Schubert tx_proto = "icmp";
22141edb306SCy Schubert }
22241edb306SCy Schubert cpp++;
22341edb306SCy Schubert } else if (ISDIGIT(**cpp) && !index(*cpp, '.')) {
22441edb306SCy Schubert ip->ip_p = atoi(*cpp);
22541edb306SCy Schubert cpp++;
22641edb306SCy Schubert } else
22741edb306SCy Schubert ip->ip_p = IPPROTO_IP;
22841edb306SCy Schubert
22941edb306SCy Schubert if (!*cpp)
2302582ae57SCy Schubert return (1);
23141edb306SCy Schubert if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) {
23241edb306SCy Schubert char *last;
23341edb306SCy Schubert
23441edb306SCy Schubert last = strchr(*cpp, ',');
23541edb306SCy Schubert if (!last) {
23641edb306SCy Schubert fprintf(stderr, "tcp/udp with no source port\n");
2372582ae57SCy Schubert return (1);
23841edb306SCy Schubert }
23941edb306SCy Schubert *last++ = '\0';
24041edb306SCy Schubert tcp->th_sport = htons(tx_portnum(last));
24141edb306SCy Schubert if (ip->ip_p == IPPROTO_TCP) {
24241edb306SCy Schubert tcp->th_win = htons(4096);
24341edb306SCy Schubert TCP_OFF_A(tcp, sizeof(*tcp) >> 2);
24441edb306SCy Schubert }
24541edb306SCy Schubert }
24641edb306SCy Schubert ip->ip_src.s_addr = tx_hostnum(*cpp, &r);
24741edb306SCy Schubert cpp++;
24841edb306SCy Schubert if (!*cpp)
2492582ae57SCy Schubert return (1);
25041edb306SCy Schubert
25141edb306SCy Schubert if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP) {
25241edb306SCy Schubert char *last;
25341edb306SCy Schubert
25441edb306SCy Schubert last = strchr(*cpp, ',');
25541edb306SCy Schubert if (!last) {
25641edb306SCy Schubert fprintf(stderr, "tcp/udp with no destination port\n");
2572582ae57SCy Schubert return (1);
25841edb306SCy Schubert }
25941edb306SCy Schubert *last++ = '\0';
26041edb306SCy Schubert tcp->th_dport = htons(tx_portnum(last));
26141edb306SCy Schubert }
26241edb306SCy Schubert ip->ip_dst.s_addr = tx_hostnum(*cpp, &r);
26341edb306SCy Schubert cpp++;
26441edb306SCy Schubert if (ip->ip_p == IPPROTO_TCP) {
26541edb306SCy Schubert if (*cpp != NULL) {
26641edb306SCy Schubert char *s, *t;
26741edb306SCy Schubert
26841edb306SCy Schubert tcp->th_flags = 0;
26941edb306SCy Schubert for (s = *cpp; *s; s++)
27041edb306SCy Schubert if ((t = strchr(myflagset, *s)))
27141edb306SCy Schubert tcp->th_flags |= myflags[t-myflagset];
27241edb306SCy Schubert if (tcp->th_flags)
27341edb306SCy Schubert cpp++;
27441edb306SCy Schubert }
27541edb306SCy Schubert
27641edb306SCy Schubert if (tcp->th_flags & TH_URG)
27741edb306SCy Schubert tcp->th_urp = htons(1);
27841edb306SCy Schubert
27941edb306SCy Schubert if (*cpp && !strncasecmp(*cpp, "seq=", 4)) {
28041edb306SCy Schubert tcp->th_seq = htonl(atoi(*cpp + 4));
28141edb306SCy Schubert cpp++;
28241edb306SCy Schubert }
28341edb306SCy Schubert
28441edb306SCy Schubert if (*cpp && !strncasecmp(*cpp, "ack=", 4)) {
28541edb306SCy Schubert tcp->th_ack = htonl(atoi(*cpp + 4));
28641edb306SCy Schubert cpp++;
28741edb306SCy Schubert }
28841edb306SCy Schubert } else if (*cpp && ip->ip_p == IPPROTO_ICMP) {
28941edb306SCy Schubert char *t;
29041edb306SCy Schubert
29141edb306SCy Schubert t = strchr(*cpp, ',');
29241edb306SCy Schubert if (t != NULL)
29341edb306SCy Schubert *t = '\0';
29441edb306SCy Schubert
29541edb306SCy Schubert ic->icmp_type = geticmptype(AF_INET, *cpp);
29641edb306SCy Schubert if (t != NULL)
29741edb306SCy Schubert ic->icmp_code = atoi(t + 1);
29841edb306SCy Schubert cpp++;
29941edb306SCy Schubert
30041edb306SCy Schubert if (ic->icmp_type == ICMP_ECHO ||
30141edb306SCy Schubert ic->icmp_type == ICMP_ECHOREPLY)
30241edb306SCy Schubert ic->icmp_id = htons(getpid());
30341edb306SCy Schubert if (t != NULL)
30441edb306SCy Schubert *t = ',';
30541edb306SCy Schubert }
30641edb306SCy Schubert
30741edb306SCy Schubert if (*cpp && !strcasecmp(*cpp, "opt")) {
30841edb306SCy Schubert u_long olen;
30941edb306SCy Schubert
31041edb306SCy Schubert cpp++;
31141edb306SCy Schubert olen = buildopts(*cpp, ipopts, (IP_HL(ip) - 5) << 2);
31241edb306SCy Schubert if (olen) {
31341edb306SCy Schubert bcopy(ipopts, (char *)(ip + 1), olen);
31441edb306SCy Schubert IP_HL_A(ip, IP_HL(ip) + (olen >> 2));
31541edb306SCy Schubert ip->ip_len += olen;
31641edb306SCy Schubert }
31741edb306SCy Schubert }
31841edb306SCy Schubert if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
31941edb306SCy Schubert bcopy((char *)tcp, ((char *)ip) + (IP_HL(ip) << 2),
32041edb306SCy Schubert sizeof(*tcp));
32141edb306SCy Schubert else if (ip->ip_p == IPPROTO_ICMP)
32241edb306SCy Schubert bcopy((char *)ic, ((char *)ip) + (IP_HL(ip) << 2),
32341edb306SCy Schubert sizeof(*ic));
32441edb306SCy Schubert ip->ip_len = htons(ip->ip_len);
3252582ae57SCy Schubert return (0);
32641edb306SCy Schubert }
32741edb306SCy Schubert
32841edb306SCy Schubert
32941edb306SCy Schubert #ifdef USE_INET6
330efeb8bffSCy Schubert int
parseipv6(char ** cpp,ip6_t * ip6,char ** ifn,int * out)331efeb8bffSCy Schubert parseipv6(char **cpp, ip6_t *ip6, char **ifn, int *out)
33241edb306SCy Schubert {
33341edb306SCy Schubert tcphdr_t th, *tcp = &th;
33441edb306SCy Schubert struct icmp6_hdr icmp, *ic6 = &icmp;
33541edb306SCy Schubert
33641edb306SCy Schubert bzero((char *)ip6, MAX(sizeof(*tcp), sizeof(*ic6)) + sizeof(*ip6));
33741edb306SCy Schubert bzero((char *)tcp, sizeof(*tcp));
33841edb306SCy Schubert bzero((char *)ic6, sizeof(*ic6));
33941edb306SCy Schubert ip6->ip6_vfc = 0x60;
34041edb306SCy Schubert
34141edb306SCy Schubert *out = (**cpp == 'o') ? 1 : 0;
34241edb306SCy Schubert cpp++;
34341edb306SCy Schubert if (!*cpp)
3442582ae57SCy Schubert return (1);
34541edb306SCy Schubert
34641edb306SCy Schubert if (!strcasecmp(*cpp, "on")) {
34741edb306SCy Schubert cpp++;
34841edb306SCy Schubert if (!*cpp)
3492582ae57SCy Schubert return (1);
35041edb306SCy Schubert *ifn = strdup(*cpp++);
35141edb306SCy Schubert if (!*cpp)
3522582ae57SCy Schubert return (1);
35341edb306SCy Schubert }
35441edb306SCy Schubert
35541edb306SCy Schubert if (!strcasecmp(*cpp, "tcp")) {
35641edb306SCy Schubert ip6->ip6_nxt = IPPROTO_TCP;
35741edb306SCy Schubert tx_proto = "tcp";
35841edb306SCy Schubert cpp++;
35941edb306SCy Schubert } else if (!strcasecmp(*cpp, "udp")) {
36041edb306SCy Schubert ip6->ip6_nxt = IPPROTO_UDP;
36141edb306SCy Schubert tx_proto = "udp";
36241edb306SCy Schubert cpp++;
36341edb306SCy Schubert } else if (!strcasecmp(*cpp, "icmpv6")) {
36441edb306SCy Schubert ip6->ip6_nxt = IPPROTO_ICMPV6;
36541edb306SCy Schubert tx_proto = "icmpv6";
36641edb306SCy Schubert cpp++;
36741edb306SCy Schubert } else if (ISDIGIT(**cpp) && !index(*cpp, ':')) {
36841edb306SCy Schubert ip6->ip6_nxt = atoi(*cpp);
36941edb306SCy Schubert cpp++;
37041edb306SCy Schubert } else
37141edb306SCy Schubert ip6->ip6_nxt = IPPROTO_IPV6;
37241edb306SCy Schubert
37341edb306SCy Schubert if (!*cpp)
3742582ae57SCy Schubert return (1);
37541edb306SCy Schubert
37641edb306SCy Schubert switch (ip6->ip6_nxt)
37741edb306SCy Schubert {
37841edb306SCy Schubert case IPPROTO_TCP :
37941edb306SCy Schubert ip6->ip6_plen = sizeof(struct tcphdr);
38041edb306SCy Schubert break;
38141edb306SCy Schubert case IPPROTO_UDP :
38241edb306SCy Schubert ip6->ip6_plen = sizeof(struct udphdr);
38341edb306SCy Schubert break;
38441edb306SCy Schubert case IPPROTO_ICMPV6 :
38541edb306SCy Schubert ip6->ip6_plen = ICMP6ERR_IPICMPHLEN;
38641edb306SCy Schubert break;
38741edb306SCy Schubert default :
38841edb306SCy Schubert break;
38941edb306SCy Schubert }
39041edb306SCy Schubert
39141edb306SCy Schubert if (ip6->ip6_nxt == IPPROTO_TCP || ip6->ip6_nxt == IPPROTO_UDP) {
39241edb306SCy Schubert char *last;
39341edb306SCy Schubert
39441edb306SCy Schubert last = strchr(*cpp, ',');
39541edb306SCy Schubert if (!last) {
39641edb306SCy Schubert fprintf(stderr, "tcp/udp with no source port\n");
3972582ae57SCy Schubert return (1);
39841edb306SCy Schubert }
39941edb306SCy Schubert *last++ = '\0';
40041edb306SCy Schubert tcp->th_sport = htons(tx_portnum(last));
40141edb306SCy Schubert if (ip6->ip6_nxt == IPPROTO_TCP) {
40241edb306SCy Schubert tcp->th_win = htons(4096);
40341edb306SCy Schubert TCP_OFF_A(tcp, sizeof(*tcp) >> 2);
40441edb306SCy Schubert }
40541edb306SCy Schubert }
40641edb306SCy Schubert
40741edb306SCy Schubert if (inet_pton(AF_INET6, *cpp, &ip6->ip6_src) != 1) {
40841edb306SCy Schubert fprintf(stderr, "cannot parse source address '%s'\n", *cpp);
4092582ae57SCy Schubert return (1);
41041edb306SCy Schubert }
41141edb306SCy Schubert
41241edb306SCy Schubert cpp++;
41341edb306SCy Schubert if (!*cpp)
4142582ae57SCy Schubert return (1);
41541edb306SCy Schubert
41641edb306SCy Schubert if (ip6->ip6_nxt == IPPROTO_TCP || ip6->ip6_nxt == IPPROTO_UDP) {
41741edb306SCy Schubert char *last;
41841edb306SCy Schubert
41941edb306SCy Schubert last = strchr(*cpp, ',');
42041edb306SCy Schubert if (!last) {
42141edb306SCy Schubert fprintf(stderr, "tcp/udp with no destination port\n");
4222582ae57SCy Schubert return (1);
42341edb306SCy Schubert }
42441edb306SCy Schubert *last++ = '\0';
42541edb306SCy Schubert tcp->th_dport = htons(tx_portnum(last));
42641edb306SCy Schubert }
42741edb306SCy Schubert
42841edb306SCy Schubert if (inet_pton(AF_INET6, *cpp, &ip6->ip6_dst) != 1) {
42941edb306SCy Schubert fprintf(stderr, "cannot parse destination address '%s'\n",
43041edb306SCy Schubert *cpp);
4312582ae57SCy Schubert return (1);
43241edb306SCy Schubert }
43341edb306SCy Schubert
43441edb306SCy Schubert cpp++;
43541edb306SCy Schubert if (ip6->ip6_nxt == IPPROTO_TCP) {
43641edb306SCy Schubert if (*cpp != NULL) {
43741edb306SCy Schubert char *s, *t;
43841edb306SCy Schubert
43941edb306SCy Schubert tcp->th_flags = 0;
44041edb306SCy Schubert for (s = *cpp; *s; s++)
44141edb306SCy Schubert if ((t = strchr(myflagset, *s)))
44241edb306SCy Schubert tcp->th_flags |= myflags[t-myflagset];
44341edb306SCy Schubert if (tcp->th_flags)
44441edb306SCy Schubert cpp++;
44541edb306SCy Schubert }
44641edb306SCy Schubert
44741edb306SCy Schubert if (tcp->th_flags & TH_URG)
44841edb306SCy Schubert tcp->th_urp = htons(1);
44941edb306SCy Schubert
45041edb306SCy Schubert if (*cpp && !strncasecmp(*cpp, "seq=", 4)) {
45141edb306SCy Schubert tcp->th_seq = htonl(atoi(*cpp + 4));
45241edb306SCy Schubert cpp++;
45341edb306SCy Schubert }
45441edb306SCy Schubert
45541edb306SCy Schubert if (*cpp && !strncasecmp(*cpp, "ack=", 4)) {
45641edb306SCy Schubert tcp->th_ack = htonl(atoi(*cpp + 4));
45741edb306SCy Schubert cpp++;
45841edb306SCy Schubert }
45941edb306SCy Schubert } else if (*cpp && ip6->ip6_nxt == IPPROTO_ICMPV6) {
46041edb306SCy Schubert char *t;
46141edb306SCy Schubert
46241edb306SCy Schubert t = strchr(*cpp, ',');
46341edb306SCy Schubert if (t != NULL)
46441edb306SCy Schubert *t = '\0';
46541edb306SCy Schubert
46641edb306SCy Schubert ic6->icmp6_type = geticmptype(AF_INET6, *cpp);
46741edb306SCy Schubert if (t != NULL)
46841edb306SCy Schubert ic6->icmp6_code = atoi(t + 1);
46941edb306SCy Schubert
47041edb306SCy Schubert if (ic6->icmp6_type == ICMP6_ECHO_REQUEST ||
47141edb306SCy Schubert ic6->icmp6_type == ICMP6_ECHO_REPLY)
47241edb306SCy Schubert ic6->icmp6_id = htons(getpid());
47341edb306SCy Schubert
47441edb306SCy Schubert if (t != NULL)
47541edb306SCy Schubert *t = ',';
47641edb306SCy Schubert }
47741edb306SCy Schubert
47841edb306SCy Schubert if (ip6->ip6_nxt == IPPROTO_TCP || ip6->ip6_nxt == IPPROTO_UDP) {
47941edb306SCy Schubert bcopy((char *)tcp, (char *)ip6 + sizeof(*ip6),
48041edb306SCy Schubert sizeof(*tcp));
48141edb306SCy Schubert } else if (ip6->ip6_nxt == IPPROTO_ICMPV6) {
48241edb306SCy Schubert bcopy((char *)ic6, (char *)ip6 + sizeof(*ip6),
48341edb306SCy Schubert sizeof(*ic6));
48441edb306SCy Schubert }
48541edb306SCy Schubert
48641edb306SCy Schubert /*
48741edb306SCy Schubert * Because a length of 0 == jumbo gram...
48841edb306SCy Schubert */
48941edb306SCy Schubert if (ip6->ip6_plen == 0) {
49041edb306SCy Schubert ip6->ip6_plen++;
49141edb306SCy Schubert }
49241edb306SCy Schubert ip6->ip6_plen = htons(ip6->ip6_plen);
4932582ae57SCy Schubert return (0);
49441edb306SCy Schubert }
49541edb306SCy Schubert #endif
496