sys/net/pfvar.h: zap duplicate signatures

2018-12-08 Thread Klemens Nanni
These are identical (see increased context) since introduction:

revision 1.240
date: 2006/10/27 13:56:51;  author: mcbride;  state: Exp;  lines: +26 -8;
Split ruleset manipulation functions out into pf_ruleset.c to allow them to
be imported into pfctl. This is a precursor to separating ruleset parsing
from loading in pfctl, and tons of good things will come from it.

2 minor changes aside from cut-n-paste and #define portability magic:

- instead of defining the global pf_main_ruleset, define pf_main_anchor
  (which contains the pf_main_ruleset)

- allow pf_find_or_create_ruleset() to return the pf_main_ruleset if it's
  passed an empty anchor name.

ok henning dhartmei


OK to remove the duplicate signatures under _KERNEL for clarity?

Index: pfvar.h
===
RCS file: /cvs/src/sys/net/pfvar.h,v
retrieving revision 1.486
diff -u -p -U11 -r1.486 pfvar.h
--- pfvar.h 13 Sep 2018 19:53:58 -  1.486
+++ pfvar.h 8 Dec 2018 21:42:31 -
@@ -1917,28 +1917,22 @@ int  pf_anchor_setup(struct pf_rule 
*,
 int pf_anchor_copyout(const struct pf_ruleset *,
const struct pf_rule *, struct pfioc_rule *);
 voidpf_anchor_remove(struct pf_rule *);
 voidpf_remove_if_empty_ruleset(struct pf_ruleset *);
 struct pf_anchor   *pf_find_anchor(const char *);
 struct pf_ruleset  *pf_find_ruleset(const char *);
 struct pf_ruleset  *pf_get_leaf_ruleset(char *, char **);
 struct pf_anchor   *pf_create_anchor(struct pf_anchor *, const char *);
 struct pf_ruleset  *pf_find_or_create_ruleset(const char *);
 voidpf_rs_initialize(void);
 
-#ifdef _KERNEL
-int pf_anchor_copyout(const struct pf_ruleset *,
-   const struct pf_rule *, struct pfioc_rule *);
-voidpf_anchor_remove(struct pf_rule *);
-#endif /* _KERNEL */
-
 /* The fingerprint functions can be linked into userland programs (tcpdump) */
 intpf_osfp_add(struct pf_osfp_ioctl *);
 #ifdef _KERNEL
 struct pf_osfp_enlist *
pf_osfp_fingerprint(struct pf_pdesc *);
 #endif /* _KERNEL */
 struct pf_osfp_enlist *
pf_osfp_fingerprint_hdr(const struct ip *, const struct ip6_hdr *,
const struct tcphdr *);
 void   pf_osfp_flush(void);
 intpf_osfp_get(struct pf_osfp_ioctl *);



Re: Importing FreeBSD eMMC code

2018-12-08 Thread Heppler, J. Scott

I'm not sure this is a bug so much as new technology being implemented
inconsistently.  I suspect the problem is due to an Replay Protected
Memory Block

https://superuser.com/questions/1039508/installing-linux-onto-emmc

My HP stream has an RPMB

dmesg | grep mmc

[2.191192] mmc0: CQHCI version 5.10
[2.191496] mmc0: SDHCI controller on PCI [:00:1c.0] using ADMA 64-bit
[2.312740] mmc0: Command Queue Engine enabled
[2.312749] mmc0: new HS400 MMC card at address 0001
[2.318993] mmcblk0: mmc0:0001 DF4032 29.1 GiB 
[2.319193] mmcblk0boot0: mmc0:0001 DF4032 partition 1 4.00 MiB

[2.319397] mmcblk0boot1: mmc0:0001 DF4032 partition 2 4.00 MiB
[2.319490] mmcblk0rpmb: mmc0:0001 DF4032 partition 3 4.00 MiB, chardev 
(242:0)

I read, but cannot confirm, that this area is messy with 3 different use
cases.  Linux/FreeBSD essentially code the 3 possibilities.

Scott

On Dec 06, 2018: 17:53, Chris Cappuccio wrote:

Heppler, J. Scott [shep...@centurylink.net] wrote:

I'll add my dmesg from an HP Stream 14 cb112wm

OpenBSD 6.4-current (RAMDISK_CD) #465: Wed Nov 28 22:26:21 MST 2018
   dera...@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/RAMDISK_CD
sdhc0 at pci0 dev 28 function 0 vendor "Intel", unknown product 0x31cc rev 
0x03: apic 1 int 39
sdhc0: SDHC 3.0, 200 MHz base clock
sdmmc0 at sdhc0: 8-bit, sd high-speed, mmc high-speed, dma

...

sdmmc0: can't enable card

...

Looks like you are hitting a bug...


--
J. Scott Heppler

Penguin Innovations

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 



NOTICE: This e-mail message and any attachments may
contain legally privileged and confidential information intended
solely for the use of the intended recipients. If you are not an
intended recipient, you are hereby notified that you have
received this message in error and any review, dissemination,
distribution, copying, or other unauthorized use of this email
and any attachment is strictly prohibited. If you have received
this email in error, please notify the sender immediately and
delete the message and any attachments from your system.



SGI O2 mec(4) cold boot issue (and workaround)

2018-12-08 Thread Miod Vallat
I have noticed, for a while, that my O2 systems were horribly slow
during installs or upgrades, when fetching sets from the network (28
*minutes* to fetch base64.tgz).

At first, I thought this was a bsd.rd specific bug, but couldn't find
anything obvious. After gathering enough data, I found out that the
problem only occurs on a cold boot. After a reboot, the network
performance is as good as it can be. That would explain why I would only
notice it during upgrades.

I also noticed that, on a warm boot, the dmesg would show:

mec0 at macebus0 base 0x0028 irq 3: MAC-110 rev 1, address 08:00:69:0e:bf:a1
nsphy0 at mec0 phy 8: DP83840 10/100 PHY, rev. 1

but on cold boots, it would show:

mec0 at macebus0 base 0x0028 irq 3: MAC-110 rev 1, address 08:00:69:0e:bf:a1
nsphy0 at mec0 phy 10: DP83840 10/100 PHY, rev. 1

Note that, in these cases, the phy seems to attach to a different
address. In these cases, after booting, "ifconfig mec0" would show:

mec0: flags=8843 mtu 1500
lladdr 08:00:69:0e:bf:a1
llprio 3
media: Ethernet autoselect
status: active
inet 10.0.1.193 netmask 0xff00 broadcast 10.255.255.255

while one would expect the "media" line to be similar to:

media: Ethernet autoselect (100baseTX full-duplex)

Investigating further, it seems that, after a cold boot, the MII bus
takes some time to initialize; the phy does not answer to address 8 but
to a larger address (10 or 11), then, after being reset, to its correct
address of 8.

So the kernel would discover the phy at a wrong address, attach it, and
after it gets reset, reading from the phy at the wrong address would
return either all bits clear or all bits set, confusing the link speed
logic without any way to recover.

What I tried but did not work:
- invoking mii_attach() twice in the mec driver. This would attach nsphy
  twice, once at the wrong address, then once at the correct address,
  but the first (wrong) attachment would be preferred.
- adding a one second delay between the Ethernet interface reset and
  mii_attach(). This would work most of the time, but not always.

What I tried and works:
- the first time the interface is reset, the mii bus is walked and all
  phys found on it are reset. Thus, by the time mii_attach() runs and
  walks the bus again, the phy will answer at the right address.

The diff below implements this (last chunk of if_mec.c), and also cleans
the mii read/write routines a bit (all the other chunks).

Tested on three different R5K family O2 systems, which have all been
exposing that problem on cold boot.

Index: dev/if_mec.c
===
RCS file: /OpenBSD/src/sys/arch/sgi/dev/if_mec.c,v
retrieving revision 1.37
diff -u -p -r1.37 if_mec.c
--- dev/if_mec.c22 Jan 2017 10:17:37 -  1.37
+++ dev/if_mec.c8 Dec 2018 15:00:04 -
@@ -264,7 +264,6 @@ struct mec_softc {
bus_dma_tag_t sc_dmat;  /* bus_dma tag. */
 
struct mii_data sc_mii; /* MII/media information. */
-   int sc_phyaddr; /* MII address. */
struct timeout sc_tick_ch;  /* Tick timeout. */
 
bus_dmamap_t sc_cddmamap;   /* bus_dma map for control data. */
@@ -329,7 +328,7 @@ voidmec_start(struct ifnet *);
 void   mec_watchdog(struct ifnet *);
 void   mec_tick(void *);
 intmec_ioctl(struct ifnet *, u_long, caddr_t);
-void   mec_reset(struct mec_softc *);
+void   mec_reset(struct mec_softc *, int);
 void   mec_iff(struct mec_softc *);
 intmec_intr(void *arg);
 void   mec_stop(struct ifnet *);
@@ -422,7 +421,7 @@ mec_attach(struct device *parent, struct
enaddr_aton(bios_enaddr, sc->sc_ac.ac_enaddr);
 
/* Reset device. */
-   mec_reset(sc);
+   mec_reset(sc, 1);
 
command = bus_space_read_8(sc->sc_st, sc->sc_sh, MEC_MAC_CONTROL);
 
@@ -451,7 +450,6 @@ mec_attach(struct device *parent, struct
ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL);
} else {
ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
-   sc->sc_phyaddr = child->mii_phy;
}
 
bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
@@ -499,21 +497,19 @@ mec_mii_readreg(struct device *self, int
struct mec_softc *sc = (void *)self;
bus_space_tag_t st = sc->sc_st;
bus_space_handle_t sh = sc->sc_sh;
-   uint32_t val;
+   uint64_t val;
int i;
 
if (mec_mii_wait(sc) != 0)
return 0;
 
-   bus_space_write_4(st, sh, MEC_PHY_ADDRESS,
+   bus_space_write_8(st, sh, MEC_PHY_ADDRESS,
(phy << MEC_PHY_ADDR_DEVSHIFT) | (reg & MEC_PHY_ADDR_REGISTER));
bus_space_write_8(st, sh, MEC_PHY_READ_INITIATE, 1);
-   delay(25);
 
for (i = 0; i < 20; i++) {
-   delay(30);
-
-   val = bus_space_read_4(st, sh, MEC_PHY_DATA);
+   delay(25);
+   val = bu

pcap_dispatch() returning zero

2018-12-08 Thread Jan Stary
The return value of pcap_dispatch() is described in pcap.3 as follows:

  The number of packets read is returned.
  Zero is returned when EOF is reached in a savefile.
  A return of -1 indicates an error in which case ...

It will also return zero on the last short read (as "EOF is reached").
So if you read the packets in chunks of 100, like

  while ((r = pcap_dispatch(src, 100, callback, NULL)) > 0) {
/* ... */
  }
  if (r == 0) {
/* EOF */
  } else if (r == -1) {
/* ERROR */
  }

and the file contains 120 packets, you will have
r == 100 the first time (reading 100 packets) and
r == 0 the second time (reading 20 packets).

It seems there is no way for the caller to know
how many packets were actually there in the last short read
besides counting the packets himself in the callback().

Is that intended? It seems more natural, and less surprising,
to have it return the actual number of packets. That would make
the sequence above 100, then 20, then 0 (like read(2) does).

Anyway, should this be explicitly mentioned in the manpage?

Jan


Index: pcap.3
===
RCS file: /cvs/src/lib/libpcap/pcap.3,v
retrieving revision 1.48
diff -u -p -r1.48 pcap.3
--- pcap.3  3 Jun 2018 10:45:15 -   1.48
+++ pcap.3  8 Dec 2018 12:53:59 -
@@ -345,7 +345,11 @@ and a
 .Fa u_char
 pointer to the packet data.
 The number of packets read is returned.
-Zero is returned when EOF is reached in a savefile.
+Zero is returned when EOF is reached in a savefile;
+this includes a short read near the end of savefile,
+when less than
+.Fa cnt
+packets are available.
 A return of \-1 indicates an error in which case
 .Fn pcap_perror
 or



add velocity sensor type, change distance unit

2018-12-08 Thread Landry Breuil
Hi,

followup on a diff from last month, updated to -current:

- adds SENSOR_VELOCITY, internal unit um/s, display unit m/s
- changes SENSOR_DISTANCE to display as meters, with 3 decimals

which gives:
hw.sensors.nmea0.distance0=335.600 m (Altitude), OK
hw.sensors.nmea0.velocity0=18.337 m/s (Ground speed), OK

should the LAST-UPDATED field in the snmp sensors mib be updated to the
commit date ?

looking for okays so that i can make progress on the nmea changes for
altitude/speed.

Landry
Index: sys/sys/sensors.h
===
RCS file: /cvs/src/sys/sys/sensors.h,v
retrieving revision 1.35
diff -u -r1.35 sensors.h
--- sys/sys/sensors.h   8 Apr 2017 04:06:01 -   1.35
+++ sys/sys/sensors.h   8 Dec 2018 09:51:00 -
@@ -52,6 +52,7 @@
SENSOR_DISTANCE,/* distance (uMeter) */
SENSOR_PRESSURE,/* pressure (mPa) */
SENSOR_ACCEL,   /* acceleration (u m/s^2) */
+   SENSOR_VELOCITY,/* velocity (u m/s) */
SENSOR_MAX_TYPES
 };
 
@@ -78,6 +79,7 @@
"distance",
"pressure",
"acceleration",
+   "velocity",
"undefined"
 };
 #endif /* !_KERNEL */
Index: share/snmp/OPENBSD-SENSORS-MIB.txt
===
RCS file: /cvs/src/share/snmp/OPENBSD-SENSORS-MIB.txt,v
retrieving revision 1.6
diff -u -r1.6 OPENBSD-SENSORS-MIB.txt
--- share/snmp/OPENBSD-SENSORS-MIB.txt  2 Sep 2016 12:17:33 -   1.6
+++ share/snmp/OPENBSD-SENSORS-MIB.txt  8 Dec 2018 09:51:00 -
@@ -26,7 +26,7 @@
FROM SNMPv2-CONF;
 
 sensorsMIBObjects MODULE-IDENTITY
-   LAST-UPDATED "20120920Z"
+   LAST-UPDATED "20181103Z"
ORGANIZATION "OpenBSD"
CONTACT-INFO
"Editor:Reyk Floeter
@@ -39,6 +39,9 @@
DESCRIPTION
"The MIB module for gathering information from
OpenBSD's kernel sensor framework."
+   REVISION "20181103Z"
+   DESCRIPTION
+   "Add new sensor types."
REVISION "20120920Z"
DESCRIPTION
"Add new sensor types."
@@ -136,7 +139,8 @@
angle(17),
distance(18),
pressure(19),
-   accel(20)
+   accel(20),
+   velocity(21)
}
MAX-ACCESS  read-only
STATUS  current
Index: usr.sbin/snmpd/mib.c
===
RCS file: /cvs/src/usr.sbin/snmpd/mib.c,v
retrieving revision 1.91
diff -u -r1.91 mib.c
--- usr.sbin/snmpd/mib.c31 Aug 2018 05:20:36 -  1.91
+++ usr.sbin/snmpd/mib.c8 Dec 2018 09:51:06 -
@@ -2658,7 +2658,7 @@
 static const char * const sensor_unit_s[SENSOR_MAX_TYPES + 1] = {
"degC", "RPM", "V DC", "V AC", "Ohm", "W", "A", "Wh", "Ah",
"", "", "%", "lx", "", "sec", "%RH", "Hz", "degree", 
-   "mm", "Pa", "m/s^2", ""
+   "m", "Pa", "m/s^2", "m/s", ""
 };
 
 const char *
@@ -2690,6 +2690,8 @@
case SENSOR_LUX:
case SENSOR_FREQ:
case SENSOR_ACCEL:
+   case SENSOR_VELOCITY:
+   case SENSOR_DISTANCE:
ret = asprintf(&v, "%.2f", s->value / 100.0);
break;
case SENSOR_INDICATOR:
@@ -2699,7 +2701,6 @@
case SENSOR_HUMIDITY:
ret = asprintf(&v, "%.2f", s->value / 1000.0);
break;
-   case SENSOR_DISTANCE:
case SENSOR_PRESSURE:
ret = asprintf(&v, "%.2f", s->value / 1000.0);
break;
Index: usr.sbin/sensorsd/sensorsd.c
===
RCS file: /cvs/src/usr.sbin/sensorsd/sensorsd.c,v
retrieving revision 1.62
diff -u -r1.62 sensorsd.c
--- usr.sbin/sensorsd/sensorsd.c22 Oct 2018 16:20:09 -  1.62
+++ usr.sbin/sensorsd/sensorsd.c8 Dec 2018 09:51:06 -
@@ -692,7 +692,7 @@
snprintf(fbuf, RFBUFSIZ, "%lld", value);
break;
case SENSOR_DISTANCE:
-   snprintf(fbuf, RFBUFSIZ, "%.2f mm", value / 1000.0);
+   snprintf(fbuf, RFBUFSIZ, "%.3f m", value / 100.0);
break;
case SENSOR_PRESSURE:
snprintf(fbuf, RFBUFSIZ, "%.2f Pa", value / 1000.0);
@@ -700,6 +700,9 @@
case SENSOR_ACCEL:
snprintf(fbuf, RFBUFSIZ, "%2.4f m/s^2", value / 100.0);
break;
+   case SENSOR_VELOCITY:
+   snprintf(fbuf, RFBUFSIZ, "%4.3f m/s", value / 100.0);
+   break;
default:
snprintf(fbuf, RFBUFSIZ, "%lld ???", value);
}
@@ -813,13 +816,14 @@
case SENSOR_LUX:
case SENSOR_FREQ:
case SENSOR_ACCEL:
+   case SENSOR_DISTANCE:
+   case SENSOR_VELOC

Re: sed(1) make -i behave a little nicer

2018-12-08 Thread Martijn van Duren
On 12/7/18 4:13 PM, patrick keshishian wrote:
> On Fri, Dec 07, 2018 at 08:41:21AM +0100, Martijn van Duren wrote:
>> I ran into a few minor nuisances with sed's -i mode, which are mostly 
>> compatible with gnu sed, but I reckon we should address.
>>
>> The problem is sed works by writing the output to a second file in the
>> same directory as the original and after completion renaming the file
>> to the original. This has two disadvantages:
>> 1) The inode number changes, resulting in loss of carefully crafted
>>hardlinks.
>> 2) We require to have write permission in the same directory as the
>>original file, even if we don't want to have a backup file.
>>
>> Diff below tries to solve this by doing the following.
>> Copy the file to the backup location (/tmp/sedXX if no
>> extension) and use the backup as the infile and the original as the
>> outfile.
>>
>> Furthermore I changed the lstat to fstat, so we can edit symlinks (gsed
>> supports symlinks by replacing the symlink by a new real file, which is
>> also fun), and I extended the warning messages in process to show the
>> backup file if we crash during operation, so people will always know
>> where to recover the file in case of disaster.
>>
>> Because process also error(FATAL, ...)s during process and we always
>> have a backup file I don't think the warning in sed.1 is worth keeping.
>>
>> The only downside to this new approach (that I can think of) is that we
>> now temporarily have a file that is in an inconsistent state, but that's
>> not much different to writing a file with any other editor.
>>
>> $ echo test > /usr/obj/test && dd if=/dev/zero of=/usr/obj/bloat
>> /usr/obj: write failed, file system is full
>> dd: /usr/obj/bloat: No space left on device
>> 614113+0 records in
>> 614112+0 records out
>> 314425344 bytes transferred in 2.206 secs (142470196 bytes/sec)
>> $ ./obj/sed -i -f /tmp/test.sed /usr/obj/test
>>   
>>
>> /usr/obj: write failed, file system is full
>> sed: /usr/obj/test: No space left on device (backup at /tmp/sedgRPSLImG9N)
>> $ cat /tmp/test.sed
>> s/test/aaa/
>> $ cat /tmp/sedgRPSLImG9N 
>> test
>> $ ls -i /tmp/test
>> 104 /tmp/test
>> $ sed -i s/test/foo/ /tmp/test
>> $ ls -i /tmp/test 
>> 104 /tmp/test
>> $ doas touch /etc/test
>> $ doas chown martijn /etc/test
>> $ echo test > /etc/test
>> $ sed -i s/test/foo/ /etc/test
>> $ cat /etc/test 
>> foo
>>
>> The diff does change quite a few mechanics, so some scrutiny is welcome.
>>
>> martijn@
>>
>> Index: main.c
>> ===
>> RCS file: /cvs/src/usr.bin/sed/main.c,v
>> retrieving revision 1.39
>> diff -u -p -r1.39 main.c
>> --- main.c   6 Dec 2018 20:16:04 -   1.39
>> +++ main.c   7 Dec 2018 07:31:54 -
>> @@ -35,6 +35,7 @@
>>  
>>  #include 
>>  #include 
>> +#include 
>>  #include 
>>  
>>  #include 

>>  void
>>  finish_file(void)
>>  {
>>  if (infile != NULL) {
>>  fclose(infile);
>> -if (*oldfname != '\0') {
>> -if (rename(fname, oldfname) != 0) {
>> -warning("rename()");
>> -unlink(tmpfname);
>> -exit(1);
>> -}
>> +if (inplace != NULL) {
>> +if (*oldfname == '\0')
>> +unlink(oldfname);
> 
> This looks a bit odd.
That's because it's a type-O. Thanks for spotting.

I also noticed I forgot to close the outfile.
Updated diff below.
> 
> 
>>  *oldfname = '\0';
>>  }
>> -if (*tmpfname != '\0') {
>> -if (outfile != NULL && outfile != stdout)
>> -fclose(outfile);
>> -outfile = NULL;
>> -rename(tmpfname, fname);
>> -*tmpfname = '\0';
>> -}
>>  outfname = NULL;
>>  }

Index: main.c
===
RCS file: /cvs/src/usr.bin/sed/main.c,v
retrieving revision 1.39
diff -u -p -r1.39 main.c
--- main.c  6 Dec 2018 20:16:04 -   1.39
+++ main.c  8 Dec 2018 11:47:28 -
@@ -35,6 +35,7 @@
 
 #include 
 #include 
+#include 
 #include 
 
 #include 
@@ -94,13 +95,13 @@ static int rval;/* Exit status */
  */
 const char *fname; /* File name. */
 const char *outfname;  /* Output file name */
-static char oldfname[PATH_MAX];/* Old file name (for in-place editing) 
*/
-static char tmpfname[PATH_MAX];/* Temporary file name (for in-place 
editing) */
+char oldfname[PATH_MAX];   /* Old file name (for in-place editing) */
 char *inplace; /* Inplace edit file extension */
 u_long linenum;
 
 static void add_compunit(enum e_cut, char *);
 static void add_file(char *);
+int copy(FILE *, FILE *);
 static int next_files_have_l

[ksh] Fix kill -s SIGNAME

2018-12-08 Thread Jeremie Courreges-Anglas


kill, whether implemented as a builtin or an external command, supports
signal numbers and signal names.  As an extension to POSIX, signal names
may be prefixed with "SIG".  Both /bin/kill and the kill ksh builtin
attempt to do that, but the test in ksh is flawed.

The minimal diff below fixes that.

Index: trap.c
===
RCS file: /d/cvs/src/bin/ksh/trap.c,v
retrieving revision 1.32
diff -u -p -p -u -r1.32 trap.c
--- trap.c  15 Mar 2018 16:51:29 -  1.32
+++ trap.c  8 Dec 2018 11:02:12 -
@@ -97,13 +97,13 @@ gettrap(const char *name, int igncase)
if (igncase) {
if (p->name && (!strcasecmp(p->name, name) ||
(strlen(name) > 3 && !strncasecmp("SIG",
-   p->name, 3) &&
+   name, 3) &&
!strcasecmp(p->name, name + 3
return p;
} else {
if (p->name && (!strcmp(p->name, name) ||
(strlen(name) > 3 && !strncmp("SIG",
-   p->name, 3) && !strcmp(p->name, name + 3
+   name, 3) && !strcmp(p->name, name + 3
return p;
}
}



Alternately, if you find that code a bit obfuscated, please find below
a version I find less redundant and more readable.

Looking for oks, preferably for the second version.


Index: trap.c
===
RCS file: /d/cvs/src/bin/ksh/trap.c,v
retrieving revision 1.32
diff -u -p -p -u -r1.32 trap.c
--- trap.c  15 Mar 2018 16:51:29 -  1.32
+++ trap.c  8 Dec 2018 11:03:46 -
@@ -92,20 +92,18 @@ gettrap(const char *name, int igncase)
return &sigtraps[n];
return NULL;
}
+
+   if (igncase && strncasecmp(name, "SIG", 3) == 0)
+   name += 3;
+   if (!igncase && strncmp(name, "SIG", 3) == 0)
+   name += 3;
+
for (p = sigtraps, i = NSIG+1; --i >= 0; p++)
if (p->name) {
-   if (igncase) {
-   if (p->name && (!strcasecmp(p->name, name) ||
-   (strlen(name) > 3 && !strncasecmp("SIG",
-   p->name, 3) &&
-   !strcasecmp(p->name, name + 3
-   return p;
-   } else {
-   if (p->name && (!strcmp(p->name, name) ||
-   (strlen(name) > 3 && !strncmp("SIG",
-   p->name, 3) && !strcmp(p->name, name + 3
-   return p;
-   }
+   if (igncase && strcasecmp(p->name, name) == 0)
+   return p;
+   if (!igncase && strcmp(p->name, name) == 0)
+   return p;
}
return NULL;
 }

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE



libpcap: add basic MPLS support

2018-12-08 Thread Denis Fondras
Add basic MPLS support in libpcap.

Index: gencode.c
===
RCS file: /cvs/src/lib/libpcap/gencode.c,v
retrieving revision 1.51
diff -u -p -r1.51 gencode.c
--- gencode.c   10 Nov 2018 10:17:37 -  1.51
+++ gencode.c   14 Nov 2018 12:50:46 -
@@ -37,6 +37,8 @@ struct rtentry;
 #include 
 #include 
 
+#include 
+
 #include 
 #include 
 
@@ -69,6 +71,7 @@ static pcap_t *bpf_pcap;
 
 /* Hack for updating VLAN offsets. */
 static u_int   orig_linktype = -1, orig_nl = -1, orig_nl_nosnap = -1;
+static u_int   mpls_stack = 0;
 
 /* XXX */
 #ifdef PCAP_FDDIPAD
@@ -837,11 +840,11 @@ gen_linktype(proto)
struct block *b0, *b1;
 
/* If we're not using encapsulation and checking for IP, we're done */
-   if (off_linktype == -1 && proto == ETHERTYPE_IP)
+   if ((off_linktype == -1 || mpls_stack > 0) && proto == ETHERTYPE_IP)
return gen_true();
 #ifdef INET6
/* this isn't the right thing to do, but sometimes necessary */
-   if (off_linktype == -1 && proto == ETHERTYPE_IPV6)
+   if ((off_linktype == -1 || mpls_stack > 0) && proto == ETHERTYPE_IPV6)
return gen_true();
 #endif
 
@@ -3348,6 +3351,34 @@ gen_acode(eaddr, q)
}
bpf_error("ARCnet address used in non-arc expression");
/* NOTREACHED */
+}
+
+struct block *
+gen_mpls(label)
+   int label;
+{
+   struct block*b0;
+
+   if (label > MPLS_LABEL_MAX)
+   bpf_error("invalid MPLS label : %d", label);
+
+   if (mpls_stack > 0) /* Bottom-Of-Label-Stack bit ? */
+   b0 = gen_mcmp(off_nl-2, BPF_B, (bpf_int32)0, 0x1);
+   else 
+   b0 = gen_linktype(ETHERTYPE_MPLS);
+
+   if (label >= 0) {
+   struct block *b1;
+
+   b1 = gen_mcmp(off_nl, BPF_W, (bpf_int32)(label << 12),
+   MPLS_LABEL_MASK);
+   gen_and(b0, b1);
+   b0 = b1;
+   }
+   off_nl += 4;
+   off_linktype += 4;
+   mpls_stack++;
+   return (b0);
 }
 
 /*
Index: gencode.h
===
RCS file: /cvs/src/lib/libpcap/gencode.h,v
retrieving revision 1.18
diff -u -p -r1.18 gencode.h
--- gencode.h   3 Jun 2018 10:29:28 -   1.18
+++ gencode.h   14 Nov 2018 12:50:46 -
@@ -179,6 +179,7 @@ struct block *gen_multicast(int);
 struct block *gen_inbound(int);
 
 struct block *gen_vlan(int);
+struct block *gen_mpls(int);
 
 struct block *gen_pf_ifname(char *);
 struct block *gen_pf_rnr(int);
Index: grammar.y
===
RCS file: /cvs/src/lib/libpcap/grammar.y,v
retrieving revision 1.19
diff -u -p -r1.19 grammar.y
--- grammar.y   27 Oct 2009 23:59:30 -  1.19
+++ grammar.y   14 Nov 2018 12:50:46 -
@@ -115,7 +115,7 @@ pcap_parse()
 %token LSH RSH
 %token  LEN
 %token  IPV6 ICMPV6 AH ESP
-%token VLAN
+%token VLAN MPLS
 
 %type   ID
 %type   EID
@@ -279,6 +279,8 @@ other:pqual TK_BROADCAST{ $$ = gen_b
| OUTBOUND  { $$ = gen_inbound(1); }
| VLAN pnum { $$ = gen_vlan($2); }
| VLAN  { $$ = gen_vlan(-1); }
+   | MPLS pnum { $$ = gen_mpls($2); }
+   | MPLS  { $$ = gen_mpls(-1); }
| pfvar { $$ = $1; }
| pqual p80211  { $$ = $2; }
;
Index: pcap-filter.3
===
RCS file: /cvs/src/lib/libpcap/pcap-filter.3,v
retrieving revision 1.2
diff -u -p -r1.2 pcap-filter.3
--- pcap-filter.3   19 Feb 2014 04:51:32 -  1.2
+++ pcap-filter.3   14 Nov 2018 12:50:46 -
@@ -522,6 +522,31 @@ filters on VLAN 200 encapsulated within 
 .in -.5i
 filters IPv4 protocols encapsulated in VLAN 300 encapsulated within any
 higher order VLAN.
+.IP "\fBmpls \fI[label]\fR"
+True if the packet is an MPLS (Multi-Protocol Label Switching) packet.
+If \fIlabel\fR is specified, only true if the packet has the specified
+\fIlabel\fR.
+Note that the first \fBmpls\fR keyword encountered in \fIexpression\fR
+changes the decoding offsets for the remainder of \fIexpression\fR on
+the assumption that the packet is an MPLS packet.  The \fBmpls
+\fI[label]\fR expression may be used more than once, to filter on MPLS
+labels stack.  Each use of that expression increments the filter offsets
+by 4.
+.IP
+For example:
+.in +.5i
+.nf
+\fBmpls 42 && mpls 12\fR
+.fi
+.in -.5i
+filters on MPLS label 42 on top of MPLS label 12, and
+.in +.5i
+.nf
+\fBmpls 42 && mpls 12 && net 192.0.2.0/24\fR
+.fi
+.in -.5i
+filters network 192.0.2.0/24 transported inside packet with label 42 on
+top of label 12.
 .IP  "\fBtcp\fR, \fBudp\fR, \fBicmp\fR"
 Abbreviations for:
 .in +.5i
Index: scanner.l
===
RCS file: /cvs/src/lib/libpcap/scanner.l,v
retrieving revision 1.25
diff -u

vmd: set dhcp hostname option during netboot

2018-12-08 Thread Anton Lindqvist
Hi,
I've been trying out the new fake netboot feature in vmd. Overall, a
great addition that removed the need for me to run dhcpd/rebound locally
to achieve auto install. It would be convenient if the DHCP lease
included a hostname inferred from the VM name in order to use dedicated
response files for different VMs. Maybe this is a behavior that
shouldn't be limited to just netboot? The res_hnok() validation is
borrowed from dhclient.

Comments? OK?

Index: dhcp.c
===
RCS file: /cvs/src/usr.sbin/vmd/dhcp.c,v
retrieving revision 1.7
diff -u -p -r1.7 dhcp.c
--- dhcp.c  6 Dec 2018 09:20:06 -   1.7
+++ dhcp.c  8 Dec 2018 09:04:33 -
@@ -24,6 +24,7 @@
 #include 
 #include 
 
+#include 
 #include 
 #include 
 #include 
@@ -44,8 +45,10 @@ dhcp_request(struct vionet_dev *dev, cha
struct packet_ctxpc;
struct dhcp_packet   req, resp;
struct in_addr   server_addr, mask, client_addr, requested_addr;
-   size_t   resplen, o;
+   size_t   len, resplen, o;
uint32_t ltime;
+   struct vmd_vm   *vm;
+   const char  *hostname = NULL;
 
if (buflen < (ssize_t)(BOOTP_MIN_LEN + sizeof(struct ether_header)))
return (-1);
@@ -108,8 +111,12 @@ dhcp_request(struct vionet_dev *dev, cha
resp.hlen = req.hlen;
resp.xid = req.xid;
 
-   if (dev->pxeboot)
+   if (dev->pxeboot) {
strlcpy(resp.file, "auto_install", sizeof resp.file);
+   vm = vm_getbyvmid(dev->vm_vmid);
+   if (vm && res_hnok(vm->vm_params.vmc_params.vcp_name))
+   hostname = vm->vm_params.vmc_params.vcp_name;
+   }
 
if ((client_addr.s_addr =
vm_priv_addr(&env->vmd_cfg,
@@ -205,6 +212,14 @@ dhcp_request(struct vionet_dev *dev, cha
resp.options[o++] = sizeof(server_addr);
memcpy(&resp.options[o], &server_addr, sizeof(server_addr));
o += sizeof(server_addr);
+
+   if (hostname != NULL) {
+   len = strlen(hostname);
+   resp.options[o++] = DHO_HOST_NAME;
+   resp.options[o++] = len;
+   memcpy(&resp.options[o], hostname, len);
+   o += len;
+   }
 
resp.options[o++] = DHO_END;
 



sys/net/pf*.[ch]: remove useless macros

2018-12-08 Thread Klemens Nanni
All they do is case conversion^Wconfusion, so remove them.

Relevant pfvar.h diff at the top, all other hunks were done with sed(1).

Feedback? Objections? OK?

Index: net/pfvar.h
===
RCS file: /cvs/src/sys/net/pfvar.h,v
retrieving revision 1.486
diff -u -p -r1.486 pfvar.h
--- net/pfvar.h 13 Sep 2018 19:53:58 -  1.486
+++ net/pfvar.h 8 Dec 2018 08:12:30 -
@@ -278,19 +278,6 @@ struct pfi_dynaddr {
!(a)->addr32[0] && !(a)->addr32[1] && \
!(a)->addr32[2] && !(a)->addr32[3] )) \
 
-#define PF_MATCHA(n, a, m, b, f) \
-   pf_match_addr(n, a, m, b, f)
-
-#define PF_ACPY(a, b, f) \
-   pf_addrcpy(a, b, f)
-
-#define PF_AINC(a, f) \
-   pf_addr_inc(a, f)
-
-#define PF_POOLMASK(a, b, c, d, f) \
-   pf_poolmask(a, b, c, d, f)
-
-
 #definePF_MISMATCHAW(aw, x, af, neg, ifp, rtid)
\
(   \
(((aw)->type == PF_ADDR_NOROUTE &&  \
@@ -308,7 +295,7 @@ struct pfi_dynaddr {
&(aw)->v.a.mask, (x), (af))) || \
((aw)->type == PF_ADDR_ADDRMASK &&  \
!PF_AZERO(&(aw)->v.a.mask, (af)) && \
-   !PF_MATCHA(0, &(aw)->v.a.addr,  \
+   !pf_match_addr(0, &(aw)->v.a.addr,  \
&(aw)->v.a.mask, (x), (af) !=   \
(neg)   \
)
Index: net/if_pflog.c
===
RCS file: /cvs/src/sys/net/if_pflog.c,v
retrieving revision 1.81
diff -u -p -r1.81 if_pflog.c
--- net/if_pflog.c  9 Jan 2018 15:24:24 -   1.81
+++ net/if_pflog.c  8 Dec 2018 08:04:34 -
@@ -267,8 +267,8 @@ pflog_packet(struct pf_pdesc *pd, u_int8
hdr.rule_pid = rm->cpid;
hdr.dir = pd->dir;
 
-   PF_ACPY(&hdr.saddr, &pd->nsaddr, pd->naf);
-   PF_ACPY(&hdr.daddr, &pd->ndaddr, pd->naf);
+   pf_addrcpy(&hdr.saddr, &pd->nsaddr, pd->naf);
+   pf_addrcpy(&hdr.daddr, &pd->ndaddr, pd->naf);
hdr.af = pd->af;
hdr.naf = pd->naf;
hdr.sport = pd->nsport;
@@ -402,8 +402,8 @@ pflog_bpfcopy(const void *src_arg, void 
goto copy;
pd.naf = pfloghdr->naf;
 
-   PF_ACPY(&osaddr, pd.src, pd.af);
-   PF_ACPY(&odaddr, pd.dst, pd.af);
+   pf_addrcpy(&osaddr, pd.src, pd.af);
+   pf_addrcpy(&odaddr, pd.dst, pd.af);
if (pd.sport)
osport = *pd.sport;
if (pd.dport)
@@ -417,12 +417,12 @@ pflog_bpfcopy(const void *src_arg, void 
&pd.hdr, M_NOWAIT);
 #ifdef INET6
if (afto) {
-   PF_ACPY(&pd.nsaddr, &pfloghdr->saddr, pd.naf);
-   PF_ACPY(&pd.ndaddr, &pfloghdr->daddr, pd.naf);
+   pf_addrcpy(&pd.nsaddr, &pfloghdr->saddr, pd.naf);
+   pf_addrcpy(&pd.ndaddr, &pfloghdr->daddr, pd.naf);
}
 #endif /* INET6 */
-   PF_ACPY(&pfloghdr->saddr, &osaddr, pd.af);
-   PF_ACPY(&pfloghdr->daddr, &odaddr, pd.af);
+   pf_addrcpy(&pfloghdr->saddr, &osaddr, pd.af);
+   pf_addrcpy(&pfloghdr->daddr, &odaddr, pd.af);
pfloghdr->sport = osport;
pfloghdr->dport = odport;
}
Index: net/pf.c
===
RCS file: /cvs/src/sys/net/pf.c,v
retrieving revision 1.1078
diff -u -p -r1.1078 pf.c
--- net/pf.c15 Nov 2018 13:16:37 -  1.1078
+++ net/pf.c8 Dec 2018 08:04:34 -
@@ -549,7 +549,7 @@ pf_insert_src_node(struct pf_src_node **
if (*sn == NULL) {
k.af = af;
k.type = type;
-   PF_ACPY(&k.addr, src, af);
+   pf_addrcpy(&k.addr, src, af);
k.rule.ptr = rule;
pf_status.scounters[SCNT_SRC_NODE_SEARCH]++;
*sn = RB_FIND(pf_src_tree, &tree_src_tracking, &k);
@@ -570,9 +570,9 @@ pf_insert_src_node(struct pf_src_node **
(*sn)->type = type;
(*sn)->af = af;
(*sn)->rule.ptr = rule;
-   PF_ACPY(&(*sn)->addr, src, af);
+   pf_addrcpy(&(*sn)->addr, src, af);
if (raddr)
-   PF_ACPY(&(*sn)->raddr, raddr, af);
+   pf_addrcpy(&(*sn)->raddr, raddr, af);
if (RB_INSERT(pf_src_tree,
&tree_src_tracking, *sn) != NULL) {
if (pf_status.debug >= LOG_NOTICE) {
@@ -855,9 +855,9 @@ pf_state_key_addr_setup(struct pf_pdesc 
  copy:
 #endif /* INET6 */
if (saddr)
-   PF_ACPY(&key->addr[sidx], saddr, af);
+   pf_addrcpy(&key->addr[sidx], saddr, af);