Skip to content

Commit bf0e9ca

Browse files
committed
dhclient: support VID 0 (no vlan) decapsulation
VLAN ID 0 is supposed to be interpreted as having no VLAN with a bit of priority on the side, but the kernel is not able to decapsulate this on the fly so dhclient needs to take care of it. This is similar to the VLAN ID send use case where we latch on to a parent interface in order to be able to serve responses with the correct VLAN priority. Patch is inspired by the pfSense proposal below, but the filter was rewritten for minimal impact. PR: #114 See also: pfsense/FreeBSD-src#9
1 parent 431bbf4 commit bf0e9ca

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

sbin/dhclient/bpf.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/*-
66
* SPDX-License-Identifier: BSD-3-Clause
77
*
8-
* Copyright (c) 2018 Franco Fichtner <franco@opnsense.org>
8+
* Copyright (c) 2018-2021 Franco Fichtner <franco@opnsense.org>
99
* Copyright (c) 1995, 1996, 1998, 1999
1010
* The Internet Software Consortium. All rights reserved.
1111
*
@@ -203,20 +203,41 @@ if_register_send(struct interface_info *info)
203203
* constant offsets used in if_register_send to patch the BPF program!
204204
*/
205205
static struct bpf_insn dhcp_bpf_filter[] = {
206+
/* Set packet index for IP packet... */
207+
BPF_STMT(BPF_LDX + BPF_W + BPF_IMM, 0),
208+
209+
/* Test whether this is a VLAN packet... */
210+
BPF_STMT(BPF_LD + BPF_H + BPF_IND, 12),
211+
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_VLAN, 0, 4),
212+
213+
/* Test whether it has a VID of 0 */
214+
BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
215+
BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0xfff),
216+
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 1),
217+
218+
/* Correct the packet index for VLAN... */
219+
BPF_STMT(BPF_LDX + BPF_W + BPF_IMM, 4),
220+
206221
/* Make sure this is an IP packet... */
207-
BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
208-
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
222+
BPF_STMT(BPF_LD + BPF_H + BPF_IND, 12),
223+
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 14),
209224

210225
/* Make sure it's a UDP packet... */
211-
BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
212-
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
226+
BPF_STMT(BPF_LD + BPF_B + BPF_IND, 23),
227+
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 12),
213228

214229
/* Make sure this isn't a fragment... */
215-
BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
216-
BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
230+
BPF_STMT(BPF_LD + BPF_H + BPF_IND, 20),
231+
BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 10, 0),
217232

218233
/* Get the IP header length... */
234+
BPF_STMT(BPF_MISC + BPF_TXA, 0),
235+
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 2),
219236
BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
237+
BPF_JUMP(BPF_JMP + BPF_JA, 1, 0, 0),
238+
BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 18),
239+
BPF_STMT(BPF_ALU + BPF_ADD + BPF_X, 0),
240+
BPF_STMT(BPF_MISC + BPF_TAX, 0),
220241

221242
/* Make sure it's to the right port... */
222243
BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
@@ -279,7 +300,7 @@ if_register_receive(struct interface_info *info)
279300
* XXX: changes to filter program may require changes to the
280301
* insn number(s) used below!
281302
*/
282-
dhcp_bpf_filter[8].k = LOCAL_PORT;
303+
dhcp_bpf_filter[21].k = LOCAL_PORT;
283304

284305
if (ioctl(info->rfdesc, BIOCSETF, &p) < 0)
285306
error("Can't install packet filter program: %m");

sbin/dhclient/packet.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ decode_hw_header(unsigned char *buf, int bufix, struct hardware *from)
167167
from->htype = ARPHRD_ETHER;
168168
from->hlen = sizeof(eh.ether_shost);
169169

170-
return (sizeof(eh));
170+
return (sizeof(eh) + (ntohs(eh.ether_type) == ETHERTYPE_VLAN ?
171+
ETHER_VLAN_ENCAP_LEN : 0));
171172
}
172173

173174
ssize_t

0 commit comments

Comments
 (0)