Commit Graph
2037 Commits
Author SHA1 Message Date
Yu Watanabe 3b4e01637f ip-util: define IPV4_MIN_REASSEMBLY_SIZE
The number will be later used at several places.
2026-04-25 07:26:02 +09:00
Yu Watanabe 11def23206 ip-util: introduce udp_packet_verify()
This is mostly equivalent to dhcp_packet_verify_headers(), but
- it optionally returns the UDP payload as iovec, and
- supports IP header with options,
- check packet length more strictly.
2026-04-25 07:25:18 +09:00
Yu Watanabe e8497d6825 ip-util: introduce udp_packet_build()
Then make dhcp_packet_append_ip_headers() just a wrapper of the new
function. Currently, the wrapper is inefficient, but will be removed in
a later commit.
2026-04-25 07:22:28 +09:00
Yu Watanabe 7c87767d14 ip-util: introduce ip_checksum()
It is equivalent to dhcp_packet_checksum(). It is generic
and not specific to DHCP. Hence, renamed to ip_checksum().
2026-04-25 05:23:37 +09:00
Yu Watanabe e06115cb8b sd-dhcp-client: add FIXME comment about the state callback
At least currently, it is a theoretical concern, as networkd does not
change the client state in the callback.
2026-04-24 07:40:08 +09:00
Yu Watanabe 6ee065ed99 sd-dhcp-client: rework discover/request_attempts counter
discover_attempts should be reset only when
- the client is stopped, to make the counter starts from zero on
  the next invocation.
- we acquire a bound lease, to make the counter starts from zero
  when the lease is expired.

request_attempts should be reset only when the client enter a new state
that sends DHCPREQUEST, that is, when enter one of the REBOOTING,
REQUESTING, RENEWING, and REBINDING state.

This moves resetting counter to client_set_state() as it should happen
only when the state transition.
2026-04-24 07:40:08 +09:00
Yu Watanabe 2dff63a31c sd-dhcp-client: notify SD_DHCP_CLIENT_EVENT_EXPIRED only when we already have a bound lease
Otherwise, if we emit the notification without a valid bound lease, networkd
may be confused (of course should not, but for safety).

Also, increment the delay before calling client_start_delayed().
Otherwise, the first reboot is done instantaneously.
2026-04-24 07:40:08 +09:00
Yu Watanabe b831cd00e6 sd-dhcp-client: propagate failure in setting timer and stop the client
If we fail to setup timer event sources about the lease lifetime or
T1/T2, then the lease will be never updated, and the user (networkd)
will not receive any notification about the expire. The situation is
terrible. Let's stop the client with error code earlier, and notify the
failure to networkd.
2026-04-24 07:40:08 +09:00
Yu Watanabe bb5b2f9e5a sd-dhcp-client: simplify the implementation of IPv6 Only mode support
This drop delay after ACK, as it has many problems. See comment in
sd_dhcp_client_is_waiting_for_ipv6_connectivity() for more details.
This way, the logic becomes much much simpler.

Also, do not restart the client if we lost IPv6 connectivity in
sd_dhcp_client side, but restart the client by networkd. As,
sd_dhcp_client does not know if we can start the client or not,
e.g., the interface may be currently down.
2026-04-24 07:40:08 +09:00
Yu Watanabe f0b9b679ff sd-dhcp-client: simply enter renewing/rebinding state send DHCPREQUEST on T1/T2
It is not necessary to enable another timer event source to send
DHCPREQUEST from the T1/T2 timer event source. Just call the callback
function for sending message.

Also, T1 hits only we have a bound lease. Drop spurious conditions.
2026-04-24 07:40:07 +09:00
Yu Watanabe ffc3d5f812 sd-dhcp-client: initialize event source and so on in client_start_delayed()
When we start the client, any previous state/configuration should be cleaned.
Let's effectively do the same thing as client_initialize() in that
function.

This also several assertions in client_start_delayed() to
sd_dhcp_client_start(). These kind of checks should be done earlier.
2026-04-24 07:40:07 +09:00
Yu Watanabe 0b3cbfd1bc sd-dhcp-client: enter the SELECTING state before sending DHCPDISCOVER
Similarly, enter the REBOOTING state before sending DHCPREQUEST on reboot.

Also, this makes DHCPREQUEST message is sent several times also in REBOOTING
state. Previously, we wait about 4 seconds after DHCPREQUEST on reboot, and
entered the init state if no response. Now we wait 1 second after the first
DHCPREQUEST, resend another DHCPREQUEST, wait 2 seconds, then enter the
init state if no response. So, even in the worst case, we have slight
speed up.
2026-04-24 07:40:07 +09:00
Yu Watanabe ffafefbcce sd-dhcp-client: replace max_request_attempts with constant macro 2026-04-24 07:40:07 +09:00
Yu Watanabe c32105081f sd-dhcp-client: introduce client_send_discover()
No functional change, just refactoring.
2026-04-24 07:40:07 +09:00
Yu Watanabe 16d08897bb sd-dhcp-client: open socket when necessary and close it when unnecessary
To make gracefully ignore unexpected messages from outside at unexpected timing.
This potentially reduces work load to handle such messages, and slightly
reduces attack surface by malicious DHCP messages.

This also makes the socket fd is owned by the relevant IO event source.

Except for the performance optimization and security hardening, this
should not change any behaviors. So, just refactoring.
2026-04-24 07:40:07 +09:00
Yu Watanabe 3817ab9e14 sd-dhcp-client: add one missing assertion
Found and suggested by Claude. Nice!
2026-04-24 07:40:07 +09:00
Yu Watanabe 6a3b0e8475 sd-dhcp-client: move the object definition to the header
Then, we can split the long sd-dhcp-client.c into small pieces later.

This also drops redundant typedef, which is also in sd-dhcp-client.h.
2026-04-24 07:40:07 +09:00
Yu Watanabe 7aa8eabc8e dhcp-protocol: introduce several constants, string table lookups, and so on (#41710) 2026-04-24 03:54:55 +09:00
Zbigniew Jędrzejewski-Szmek 53ea569d8a test-ndisc-send: convert to the new option parser
Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-22 14:19:01 +02:00
Lennart Poettering 812aa57d2c string-util: beef up string_is_safe()
This tightens the checks of string_is_safe() and then adds flags to
relax certain aspects of it.

This does alter the rules on certain strings we pass a bit. We mostly
tighten the rules (but I think it's find and good) but we relax them on
others.

I let claude review the changes in behaviour for the various call sites
that I made. It summarized things in this table:

  ╭───────────────────────────────────────────────────┬──────────────────────────────────────────────╮
  │ CALL SITE                                         │ EFFECTIVE DELTA                              │
  ├───────────────────────────────────────────────────┼──────────────────────────────────────────────┤
  │ src/basic/syslog-util  log_namespace_name_valid   │ +UTF-8 required (globs already blocked)      │
  │ src/bootctl  --efi-boot-option-description        │ RELAXED: '\' and quotes now permitted        │
  │ src/core/dbus-manager  pretimeout governor        │ +UTF-8, +no-globs                            │
  │ src/core/load-fragment  ExecStart= path           │ +UTF-8, +no-globs                            │
  │ src/core/main  pretimeout governor (kcmdline)     │ +UTF-8, +no-globs                            │
  │ src/core/service  sd_notify STATUS=               │ +no-globs (ASCII-only preserved)             │
  │ src/home/homectl  --<identity field>=             │ empty now REJECTED; +UTF-8                   │
  │ src/libsystemd-network  dhcp_option_parse_string  │ (equivalent, just explicit)                  │
  │ src/libsystemd-network  sd_dhcp_server boot_fname │ ""→NULL coerced; else equivalent             │
  │ src/libsystemd/journal  SYSLOG_IDENTIFIER fb      │ +UTF-8, +no-globs                            │
  │ src/libsystemd/sd-json  SD_JSON_STRICT strings    │ +UTF-8 required                              │
  │ src/login/logind  session desktop=                │ +UTF-8 required                              │
  │ src/pcrlock  EFI variable string                  │ +UTF-8                                       │
  │ src/pcrlock  EFI action string                    │ RELAXED: empty + '\' now ok; +UTF-8          │
  │ src/resolve  dns-delegate id (from filename)      │ +UTF-8, +no-globs                            │
  │ src/shared/boot-entry  boot_entry_token_valid     │ (equivalent)                                 │
  │ src/shared/conf-parser  section header            │ +UTF-8, +no-globs                            │
  │ src/shared/conf-parser  CONFIG_PARSE_STRING_SAFE  │ +UTF-8 required                              │
  │ src/shared/kbd-util  keymap_is_valid              │ (equivalent; folded into STRING_FILENAME)    │
  │ src/shared/tpm2  nvpcr name                       │ +UTF-8 required                              │
  │ src/shared/vconsole  x11 layout/model/variant/opt │ +UTF-8, +no-globs                            │
  │ src/systemctl  --kernel-cmdline=                  │ +0x7f DEL rejected; empty path split out     │
  │ src/veritysetup  salt=                            │ RELAXED: safety check removed entirely       │
  │ src/vmspawn  --ssh-key-type=                      │ +UTF-8 required                              │
  ╰───────────────────────────────────────────────────┴──────────────────────────────────────────────╯
2026-04-21 17:07:53 +02:00
Yu Watanabe 06188d2663 dhcp-protocol: introduce dhcp_option_code_to_string() 2026-04-19 08:31:07 +09:00
Yu Watanabe 5c907b2b2b dhcp-protocol: introduce {bootp,dhcp}_message_type_to_string() 2026-04-19 08:31:07 +09:00
Yu Watanabe 6fd89f305a dhcp-protocol: rename several dhcp message types
These are unused, hence we can freely rename them.
2026-04-19 08:31:07 +09:00
Yu Watanabe 19a4b5184e dhcp-protocol: define BOOTP_MESSAGE_SIZE
It will be used later.
2026-04-19 08:31:07 +09:00
Yu Watanabe 68553bb03a dhcp-protocol: introduce DHCPMessageHeader struct
It is similar to DHCPMessage, but does not have the tail 'options'
variable array. Then, we can use it in another struct.
2026-04-19 08:31:07 +09:00
Yu Watanabe c651876cc9 dhcp-protocol: Option Overload (52) DHCP option value takes flags 2026-04-19 08:31:07 +09:00
Yu Watanabe 366e1d264a sd-dhcp-client: fix memleak of sd_dhcp_client.timeout_ipv6_only_mode
This also drops unnecessary zero assignments.
2026-04-19 03:25:27 +09:00
Yu Watanabe 8bb669712e string-util: make make_cstring() take void* rather than char*
It is typically used for making C string embedded in a binary data.
Hence, the input pointer may not be char*.
2026-04-17 19:24:53 +02:00
Zbigniew Jędrzejewski-Szmek b8514cff52 libsystemd-network: use DEFINE_POINTER_ARRAY_FREE_FUNC, rename cleanup function 2026-04-10 15:08:02 +02:00
Zbigniew Jędrzejewski-Szmek 954243f2bc libsystemd-network: use DEFINE_ARRAY_FREE_FUNC, rename cleanup func 2026-04-10 15:08:02 +02:00
Michael Vogt 5d82ecc0e3 libsystemd-network: make check-pointer-deref clean
Add the needed assert changes to make the code clean
for the new check-pointer-deref script.
2026-03-27 11:23:40 +01:00
Daan De Meyer 9d844012bc sd-dhcp-client: several trivial cleanups (#41256) 2026-03-22 20:00:33 +01:00
Yu Watanabe 55f2fdd508 dhcp: fix user class and vendor specific option assignment
The commit 6d7cb9a6b8 fixes the assignment
of the these options when specified through SendOption=. However, it
breaks when specified through UserClass= or SendVendorOption=.

When UserClass= or SendVendorOption= is specified, the option length is
calculated from the sd_dhcp_client.user_class or .vendor_options. Hence,
we can use 0 for the length in that case.

Follow-up for 6d7cb9a6b8.
2026-03-22 19:40:54 +01:00
Yu Watanabe 26f769bea3 sd-dhcp-client: propagate errors in client_initialize_{io,time}_events()
Call client_stop() on error and return 0 only on callback.
Normal non-callback functions should propagate errors.

This also makes client_initialize_time_events() use
event_reset_time_relative().
2026-03-22 03:34:43 +09:00
Yu Watanabe ad4d2ae619 sd-dhcp-client: voidify client_initialize()
It never fails.
2026-03-22 03:34:43 +09:00
Yu Watanabe 2fb192d237 sd-dhcp-client: drop disabled FORCERENEW message support
FORCERENEW message support has been disabled so long time for security
concern. Most other implementations of DHCP server/client neither
support FORCERENEW. Let's completely drop relevant code.
2026-03-22 03:34:43 +09:00
Yu Watanabe ddded65d41 sd-dhcp-client: add missing error checks 2026-03-22 03:34:43 +09:00
Yu Watanabe 0c68816c56 sd-dhcp-client: add missing assertion 2026-03-22 03:34:43 +09:00
Yu Watanabe ced607506d sd-dhcp-client: coding style fix 2026-03-22 03:34:43 +09:00
Yu Watanabe bd1d1e61eb fuzz-dhcp-client: modernize test code 2026-03-22 02:26:18 +09:00
Yu Watanabe f0387aa666 test-dhcp-client: modernize test code 2026-03-22 02:26:15 +09:00
Yu Watanabe fce03832fa test-dhcp-client: fix packet length and checksum in IP header 2026-03-21 05:21:28 +09:00
Lennart Poettering 2c74a91cb8 sd-json: when parsing optionally insist top-level variant is object or array
Typically, the top-level JSON object has to be an object, in any json
document we parse, hence let's add a simple way to enforce that.

Make use of this in various places.

(Note, various other JSON parsers insist on this logic right from the
beginning, but I actually thinking making this insisting optional like
this patch does it is the cleaner approach)
2026-03-20 15:34:56 +01:00
Lennart Poettering 1b364f8b53 sd-ndisc: fix address family check
Issue reported by zhengg-research
2026-03-11 18:25:28 +01:00
Yu Watanabe 6eaa5ba322 sd-dhcp-client: several fixlets for sending RELEASE or DECLINE
- Extract common logic to client_send_release_or_decline().
- Do not send DECLINE message on BOOTP protocol.
- Drop redundant assignment of chaddr, as it is already set by
  client_message_init() -> dhcp_message_init().
- Do not assign acquired address in ciaddr field of DECLINE message,
  but use Requested IP Address option.
- Broadcast DECLINE message, rather than unicast.
- Set server identifier in both cases.

Fixes #39299.
2026-03-11 08:45:24 +09:00
Nandakumar Raghavan 6d7cb9a6b8 networkd: fix for networkd crash when client sends Option 82 via SendOption=
When a DHCP client uses SendOption=82:string:..., option_append() calls
the SD_DHCP_OPTION_RELAY_AGENT_INFORMATION case which was written for
the server relay path. It casts optval to sd_dhcp_server* and calls
strlen() on its members, but optval is actually raw binary data from
the client, causing SIGSEGV. The same is applicable when option 43 and
option 77 are passed to SendOption.

Fix by checking optlen > 0 and appending the option as a plain TLV,
skipping the server-specific relay agent logic.
2026-03-10 18:29:59 +01:00
Yu Watanabe 8903e6b5f7 tree-wide: suppress misc-use-internal-linkage warnings
Suppress warnings like the following from clang tidy:
```
../src/boot/addon.c:11:19: error: function 'efi_main' can be made static to enforce internal linkage [misc-use-internal-linkage,-warnings-as-errors]
   11 | EFIAPI EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *system_table);
      |                   ^
```

Some warnings are suppressed simply by setting comments to ignore the warning,
some are by making global variables static, or include a suitable header.
2026-03-05 20:23:24 +01:00
Frantisek Sumsal 90f5d2a202 tree-wide: various fixlets suggested by Coccinelle 2026-03-05 10:27:58 +01:00
Skye Soss 13db63e920 network: add DHCPv6 message types to string table (#40912)
Adds the DHCPv6 message types ADDR-REG-INFORM and
ADDR-REG-REPLY to the DHCPv6 message types string table.

Follow-up for 1e55da38aa.
2026-03-03 01:50:12 +09:00
Mike Yuan 41615d59fa various: port manual goto cleanup to CLEANUP_TMPFILE_AT 2026-02-11 02:39:26 +01:00