From fb7d32c7eef45be7e95254b65d2cd2ca3c86f305 Mon Sep 17 00:00:00 2001 From: Maxim Konovalov Date: Thu, 23 Jan 2003 13:04:42 +0000 Subject: [PATCH] Better error handling for -s. Submitted by: bde Do not constantify maximum payload size. It is 65467 with -R (record route), and 65507 without it. Reviewed by: silence on -net Proposed by: bde I am going to MFC rev.1.77 - 1.81 ping.c and rev.1.39 and 1.40 ping.8: MFC after: 6 months --- sbin/ping/ping.8 | 2 +- sbin/ping/ping.c | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/sbin/ping/ping.8 b/sbin/ping/ping.8 index 105e5f1f1fda..5aa26fa0f263 100644 --- a/sbin/ping/ping.8 +++ b/sbin/ping/ping.8 @@ -235,7 +235,7 @@ data bytes when combined with the 8 bytes of .Tn ICMP header data. -Only the super-user may use this option. +Only the super-user may specify values more than default. .It Fl S Ar src_addr Use the following IP address as the source address in outgoing packets. On hosts with more than one IP address, this option can be used to diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c index 96757bef32fd..7953649056ff 100644 --- a/sbin/ping/ping.c +++ b/sbin/ping/ping.c @@ -104,7 +104,6 @@ static const char rcsid[] = #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) #define MINICMPLEN ICMP_MINLEN -#define MAXPAYLOAD (IP_MAXPACKET - MAXIPLEN - MINICMPLEN) #define MAXWAIT 10 /* max seconds to wait for response */ #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ #define MAXTOS 255 @@ -151,6 +150,7 @@ int mx_dup_ck = MAX_DUP_CHK; char rcvd_tbl[MAX_DUP_CHK / 8]; struct sockaddr_in whereto; /* who to ping */ +long maxpayload; int datalen = DEFDATALEN; int s; /* socket file descriptor */ u_char outpackhdr[IP_MAXPACKET], *outpack; @@ -351,18 +351,16 @@ main(argc, argv) options |= F_SO_DONTROUTE; break; case 's': /* size of packet to send */ - if (uid) { - errno = EPERM; - err(EX_NOPERM, "-s flag"); - } ultmp = strtoul(optarg, &ep, 0); - if (ultmp > MAXPAYLOAD) - errx(EX_USAGE, - "packet size too large: %lu > %u", - ultmp, MAXPAYLOAD); if (*ep || ep == optarg) errx(EX_USAGE, "invalid packet size: `%s'", optarg); + if (uid != 0 && ultmp > DEFDATALEN) { + errno = EPERM; + err(EX_NOPERM, + "packet size too large: %lu > %u", + ultmp, DEFDATALEN); + } datalen = ultmp; break; case 'S': @@ -418,6 +416,12 @@ main(argc, argv) usage(); target = argv[optind]; + maxpayload = IP_MAXPACKET - sizeof(struct ip) - MINICMPLEN; + if (options & F_RROUTE) + maxpayload -= MAX_IPOPTLEN; + if (datalen > maxpayload) + errx(EX_USAGE, "packet size too large: %lu > %u", datalen, + maxpayload); if (source) { bzero((char *)&sin, sizeof(sin)); sin.sin_family = AF_INET; @@ -1477,7 +1481,7 @@ fill(bp, patp) &pat[13], &pat[14], &pat[15]); if (ii > 0) - for (kk = 0; kk <= MAXPAYLOAD - (PHDR_LEN + ii); kk += ii) + for (kk = 0; kk <= maxpayload - (PHDR_LEN + ii); kk += ii) for (jj = 0; jj < ii; ++jj) bp[jj + kk] = pat[jj]; if (!(options & F_QUIET)) {