How to detect Openwrt kern.info and daemon.info events? - linux

My background is mostly Windows programming in C and C++. Recently I've had the chance to work with some embedded Linux systems also, but I'm still new at this.
Right now I'm working on a utility for Openwrt that needs to react to network and system events that occur during normal operation.
I've been able to use Hotplug for some events, but others still elude me. I can parse the output of the system log using logread, but that seems primitive and hackish.
In particular I'd like to get a callback similar to what hotplug does for some of the 'kern.info kernel' and 'daemon.info' events. For example:
Mar 31 19:42:32 OpenWrt kern.info kernel: [ 369.540000] device wlan0 left promiscuous mode
Mar 31 19:42:32 OpenWrt kern.info kernel: [ 369.540000] br-lan: port 2(wlan0) entered disabled state
Mar 31 19:42:32 OpenWrt kern.info kernel: [ 369.730000] device wlan1 left promiscuous mode
Mar 31 19:42:32 OpenWrt kern.info kernel: [ 369.730000] br-lan: port 3(wlan1) entered disabled state
Mar 31 19:42:34 OpenWrt kern.info kernel: [ 371.360000] device wlan0 entered promiscuous mode
Mar 31 19:45:56 OpenWrt daemon.info hostapd: wlan0: STA 04:f7:e4:00:00:00 IEEE 802.11: authenticated
Mar 31 19:45:56 OpenWrt daemon.info hostapd: wlan0: STA 04:f7:e4:00:00:00 IEEE 802.11: associated (aid 1)
Mar 31 19:45:56 OpenWrt daemon.info hostapd: wlan0: STA 04:f7:e4:00:00:00 WPA: pairwise key handshake completed (WPA)
Mar 31 19:45:56 OpenWrt daemon.info hostapd: wlan0: STA 04:f7:e4:00:00:00 WPA: group key handshake completed (WPA)
Mar 31 19:45:56 OpenWrt daemon.info dnsmasq-dhcp[5005]: DHCPREQUEST(br-lan) 10.1.1.51 04:f7:e4:00:00:00
Mar 31 19:45:56 OpenWrt daemon.info dnsmasq-dhcp[5005]: DHCPNAK(br-lan) 10.1.1.51 04:f7:e4:00:00:00 wrong network
Mar 31 19:46:00 OpenWrt daemon.info dnsmasq-dhcp[5005]: DHCPDISCOVER(br-lan) 04:f7:e4:00:00:00
Mar 31 19:46:00 OpenWrt daemon.info dnsmasq-dhcp[5005]: DHCPOFFER(br-lan) 192.168.1.198 04:f7:e4:1c:09:00
Mar 31 19:46:00 OpenWrt daemon.info dnsmasq-dhcp[5005]: DHCPDISCOVER(br-lan) 04:f7:e4:1c:09:00
Mar 31 19:46:00 OpenWrt daemon.info dnsmasq-dhcp[5005]: DHCPOFFER(br-lan) 192.168.1.198 04:f7:e4:1c:09:00
Mar 31 19:46:01 OpenWrt daemon.info dnsmasq-dhcp[5005]: DHCPREQUEST(br-lan) 192.168.1.198 04:f7:e4:1c:09:00
Mar 31 19:46:01 OpenWrt daemon.info dnsmasq-dhcp[5005]: DHCPACK(br-lan) 192.168.1.198 04:f7:e4:1c:09:00 My-iPhone

Log entries like the DHCPOFFER (as seen in your example) are generated individually by the corresponding process (for example, by udhcpc) using the Unix syslog mechanism (kind-of like the Windows Event Logging API)
By default on OpenWRT logging is handled by the syslogd process provided by the busybox package. This is fairly primitive and simply sends messages to the circular buffer you see using logread and/or to a UDP socket.
You can upgrade logging on OpenWRT to use the syslog-ng package. This has a much more advanced configuration and you should be able to use this to send filtered log events to a script that you can write to do what you need with them.
opkg install syslog-ng
syslog-ng is a GPL product but the documentation is now buried beneath a commercial web site, one would hope you can get it from the source code , via http://freecode.com/projects/syslog-ng. Note that OpenWRT seems to provide version 1.6.12 which I had trouble finding the documentation for when I implemented it on my OpenWRT devices, but eventually I found it via the wayback machine: https://web.archive.org/web/20070406054439/http://www.balabit.com/products/syslog_ng/reference-1.6/syslog-ng.html/x731.html (for example)
A configuration file fragment that would pull out those DHCP messages and send them to a standalone log file would look a bit like:
source src { unix-stream("/dev/log"); internal(); };
destination dhcp_messages { file("/var/log/dhcpmessages"); };
filter f_dhcp { match("dnsmasq-dhcp"); };
log {
source(src);
filter(f_dhcp);
destination(dhcp_messages);
};
You might probably find the pipe() or program() destination drivers the most useful for your application. For example, using a program() driver you could send selected messages to a shell script that parses them and saves them into a sqlite database.

Related

Why does chromiuim trigger DRM (Direct Rendering Manager) on startup on Linux?

I was wondering if anyone knows why chromium based browsers trigger direct rendering manager on startup on Linux whereas Firefox for example doesn't seem to do that? This is what I see when starting chromium:
Dec 19 11:02:30 hp kernel: [drm] PCIE GART of 256M enabled (table at 0x000000F400000000).
Dec 19 11:02:31 hp kernel: [drm] UVD and UVD ENC initialized successfully.
Dec 19 11:02:31 hp kernel: [drm] VCE initialized successfully.

Error in Bluetooth Status - Raspberry Pi - "Failed to set privacy: Rejected (0x0b)"

I'm in the very early stages of doing some Bluetooth Mesh work on my Raspberry Pi 4.
From the instructions I have found that a rebuild of the Kernel is required to enable the Crypto APIs that Mesh requires.
I am starting with
pi#pi-4:~ $ uname -a
Linux pi-4 5.10.63-v7l+ #1459 SMP Wed Oct 6 16:41:57 BST 2021 armv7l GNU/Linux
pi#pi-4:~ $ bluetoothd -v
5.55
And rebuilt the kernel using
git clone --depth=1 https://github.com/raspberrypi/linux
cd linux
KERNEL=kernel7l
make bcm2711_defconfig
vi .config # to customize the local name of the kernel build
make menuconfig
Of note, I have seen varying documents on which kernel options to set so I went with the superset of options. Set via a combination of vi and the UI. The resulting edits were
CONFIG_EXPERT=y
CONFIG_TIMERFD=y
CONFIG_EVENTFD=y
CONFIG_NET=y
CONFIG_BT=y
CONFIG_BT_RFCOMM=y
CONFIG_BT_RFCOMM_TTY=y
CONFIG_BT_BNEP=y
CONFIG_BT_BNEP_MC_FILTER=y
CONFIG_BT_BNEP_PROTO_FILTER=y
CONFIG_BT_HIDP=y
CONFIG_RFKILL=y
CONFIG_CRYPTO_USER=y
CONFIG_CRYPTO_USER_API_HASH=y
CONFIG_CRYPTO_USER_API_SKCIPHER=y
CONFIG_CRYPTO_MD5=y
CONFIG_CRYPTO_SHA1=y
CONFIG_KEY_DH_OPERATIONS=y
CONFIG_CRYPTO_USER_API_AEAD=y
CONFIG_CRYPTO_USER_API_HASH=y
CONFIG_CRYPTO_AES=y
CONFIG_CRYPTO_CCM=y
CONFIG_CRYPTO_AEAD=y
CONFIG_CRYPTO_CMAC=y
I also edited the statup command line option to turn off the SAP plug-in to get rid of that startup error.
KERNEL=kernel7l
make -j4 zImage modules dtbs
sudo make modules_install
sudo cp arch/arm/boot/dts/*.dtb /boot/
sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/
sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/
sudo cp arch/arm/boot/zImage /boot/$KERNEL.img
pi#pi-4-x:~/ell-0.9/linux $ sudo reboot now
Connection to pi-4-x closed by remote host.
Connection to pi-4-x closed.
.
. reconnect
.
pi#pi-4:~ $ uname -a
Linux pi-4 5.10.90-v7l-With-BlueZ-Crypto+ #2 SMP Fri Jan 14 16:00:58 EST 2022 armv7l GNU/Linux
Now I am looking at this error when I check the status. This error is the SAME when using the BlueZ shipped with the distribution (5.55) and when rebuilding from source (5.63).
Any ideas?
Failed to set privacy: Rejected (0x0b)
pi#pi-4:~/linux $ sudo service bluetooth status
● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2022-01-18 11:32:44 EST; 11min ago
Docs: man:bluetoothd(8)
Main PID: 1726 (bluetoothd)
Status: "Running"
Tasks: 1 (limit: 3715)
CPU: 59ms
CGroup: /system.slice/bluetooth.service
└─1726 /usr/libexec/bluetooth/bluetoothd --noplugin=sap
Jan 18 11:32:44 pi-4 systemd[1]: Starting Bluetooth service...
Jan 18 11:32:44 pi-4 bluetoothd[1726]: Bluetooth daemon 5.55
Jan 18 11:32:44 pi-4 systemd[1]: Started Bluetooth service.
Jan 18 11:32:44 pi-4 bluetoothd[1726]: Starting SDP server
Jan 18 11:32:44 pi-4 bluetoothd[1726]: Excluding (cli) sap
Jan 18 11:32:44 pi-4 bluetoothd[1726]: Bluetooth management interface 1.18 initialized
Jan 18 11:32:44 pi-4 bluetoothd[1726]: Failed to set privacy: Rejected (0x0b)
Jan 18 11:32:44 pi-4 bluetoothd[1726]: Endpoint registered: sender=:1.21 path=/MediaEndpoint/A2DPSink/sbc
Jan 18 11:32:44 pi-4 bluetoothd[1726]: Endpoint registered: sender=:1.21 path=/MediaEndpoint/A2DPSource/sbc

Linux Bluez RFCOMM: Connection Refused

The connection to a Bluetooth device via RFCOMM fails on Linux/Bluez with Connection refused at the call of
connect(s, (struct sockaddr *)&addr, sizeof(addr));.
The device was successfully paired. An RFCOMM connection to that device from Android or Windows can be successfully established, so the problem seems to be locaed with Bluez diver and/or blueotoothd.
With Linux/Bluez the bluetoothctl and Wireshark traces show that it fist connects and then after about 2 seconds a disconnection is done. The reason for the disconnection is not clear.
The same problem happens with different Linux releases, on PC with USB Bluetooth (Linux ubuntu 4.15.0-33-generic #36~16.04.1-Ubuntu SMP Wed Aug 15 17:21:05 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux) or Raspberry Pi 3 (Jessie, Stretch).
I have checked numerous other thread having the same/similar problem. Most have no or no clear answer.
The Wireshark trace screenshot shows the disconnect after 2.2 seconds.
The corresponding bluetoothd syslog output:
Aug 31 16:43:54 ubuntu bluetoothd[926]: src/adapter.c:connected_callback() hci0 device F6:65:0A:E5:DE:E1 connected eir_len 22
Aug 31 16:43:54 ubuntu bluetoothd[926]: src/device.c:device_create() dst F6:65:0A:E5:DE:E1
Aug 31 16:43:54 ubuntu bluetoothd[926]: src/device.c:device_new() address F6:65:0A:E5:DE:E1
Aug 31 16:43:55 ubuntu bluetoothd[926]: src/device.c:device_new() Creating device /org/bluez/hci0/dev_F6_65_0A_E5_DE_E1
Aug 31 16:43:57 ubuntu bluetoothd[926]: src/adapter.c:dev_disconnected() Device F6:65:0A:E5:DE:E1 disconnected, reason 3
Aug 31 16:43:57 ubuntu bluetoothd[926]: src/adapter.c:adapter_remove_connection()
Aug 31 16:43:57 ubuntu bluetoothd[926]: src/adapter.c:adapter_remove_connection() Removing temporary device /org/bluez/hci0/dev_F6_65_0A_E5_DE_E1
Aug 31 16:43:57 ubuntu bluetoothd[926]: src/device.c:device_remove() Removing device /org/bluez/hci0/dev_F6_65_0A_E5_DE_E1
Aug 31 16:43:57 ubuntu bluetoothd[926]: src/device.c:btd_device_unref() Freeing device /org/bluez/hci0/dev_F6_65_0A_E5_DE_E1
Aug 31 16:43:57 ubuntu bluetoothd[926]: src/device.c:device_free() 0x563aa2a270a0
Aug 31 16:43:57 ubuntu bluetoothd[926]: plugins/policy.c:disconnect_cb() reason 3
Aug 31 16:43:57 ubuntu bluetoothd[926]: src/adapter.c:bonding_attempt_complete() hci0 bdaddr F6:65:0A:E5:DE:E1 type 0 status 0xe
Aug 31 16:43:57 ubuntu bluetoothd[926]: src/adapter.c:resume_discovery()
reason 3 points to MGMT_DEV_DISCONN_REMOTE in include/net/bluetooth/mgmt.h of the kernel sources. This would mean that it is the device who initiates the disconnect. But the highlighted line in the Wireshark trace shows that it is the host that initiates the disconnection.
Many thanks for any help in advance.
The incorrect RFCOMM channel was used. It instantly works when the correct RFCOMM channel is used.
sdptool records F6:65:0A:E5:DE:E1 shows on which channel the RFCOMM is:
Service Name: Serial Port
Service RecHandle: 0x10000
Service Class ID List:
"Serial Port" (0x1101)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 5

Windows 7/8 cannot connect to g_ether in Linux 3.4

I have an embedded computer that is running Linux 3.4, and there is not a way for me to upgrade to 4.x.y at the moment unfortunately.
I have compiled g_ether as a kernel module using the following configuration options:
CONFIG_USB_ETH=m
CONFIG_USB_ETH_RNDIS=y
The module compiles fine and I can load it once the system has booted with no issues:
[ 7.160000] DWC_otg: dwc_udc_start: g_ether
[ 7.168000] DWC_otg: bind to driver g_ether
[ 7.176000] DWC_otg: dwc_otg_pcd_alloc_request(e30171a4,208)
[ 7.184000] g_ether gadget: using random self ethernet address
[ 7.196000] usb0: MAC 52:e9:07:c2:0f:23
[ 7.204000] usb0: HOST MAC 82:cf:ce:fa:44:18
[ 7.212000] rndis_bind
[ 7.224000] DWC_otg: dwc_otg_pcd_alloc_request(e30171ec,208)
[ 7.232000] rndis_register: configNr = 0
[ 7.236000] rndis_set_param_medium: 0 0
[ 7.244000] DWC_otg: dwc_otg_pcd_alloc_request(e30171ec,208)
[ 7.252000] g_ether gadget: Ethernet Gadget, version: Memorial Day 2008
[ 7.264000] g_ether gadget: g_ether ready
[ 7.268000] DWC_otg: dwc_udc_start: registered gadget driver 'g_ether'
My goal is to connect this embedded computer to a Windows computer. When I connect the embedded computer, Windows initially shows the "device" in the Device Manager as working properly and then updates to show that it is not working with the error:
This device cannot start. (Code 10)
FWIW I have tried several different drivers on the Windows side, including the linux.inf referenced in the Linux USG Gadget documentation, several of the built-in drivers, and one driver I found on a forum.
From the Linux side, when I plug in the USB cord, I see the following output:
Aug 29 05:13:54 dot-8f2wktlxah kernel: [ 31.840000] g_ether gadget: init rndis
Aug 29 05:13:54 dot-8f2wktlxah kernel: [ 31.840000] g_ether gadget: RNDIS RX/TX early activation ...
Aug 29 05:13:54 dot-8f2wktlxah kernel: [ 32.116000] usb0: qlen 10
Aug 29 05:13:56 dot-8f2wktlxah kernel: [ 34.128000] g_ether gadget: rndis req21.00 v0000 i0000 l24
Aug 29 05:13:56 dot-8f2wktlxah kernel: [ 34.540000] g_ether gadget: rndis reqa1.01 v0000 i0000 l4096
This corresponds on the Windows side (via USBPcap):
1226 29734.081932 host 2.1.0 USBCOM 36 SEND ENCAPSULATED COMMAND Request
1227 29735.362932 2.1.0 host USBCOM 52 SEND ENCAPSULATED COMMAND Response
1228 29735.362932 2.1.0 host USB 28 GET STATUS Status
1229 29735.810932 2.1.2 host USBCOM 35 NETWORK CONNECTION
1230 29735.810932 host 2.1.0 USBCOM 36 GET ENCAPSULATED RESPONSE Request
The rndis req21 on the Linux side corresponds to the SEND ENCAPSULATED COMMAND on the Windows side, and the same is true for the rndis reqa1 and GET ENCAPSULATED RESPONSE.
There is no other output on either after those last response/requests.
I understand that Linux 3.4 is very out of date, but I don't have the option to upgrade since this is an embedded computer and thus I am beholden to the chip-maker to provide an update.
Has anyone successfully used the g_ether kernel module with Windows 7/8 and Linux 3.4, or know why these request/responses seem to just stop after that last GET ENCAPSULATED RESPONSE?

Failing to start systemd service

I have written the following systemd service to login at the wireless at boot:
[Unit]
Description=Wireless network connectivity (%i)
Wants=network.target
Before=network.target
BindsTo=sys-subsystem-net-devices-%i-device
After=sys-subsystem-net-devices-%i-device
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/ip link set dev %i up
ExecStart=/usr/bin/wpa_supplicant -B -i %i -c /etc/wpa_supplicant/wpa_supplicant.conf
ExecStart=/usr/bin/dhcpcd %i
ExecStop=/usr/bin/ip link set dev %i down
[Install]
WantedBy=multi-user.target
I then enable it but I get the following error every time I boot my computer:
[abc#arch ~]$ systemctl --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● network-wireless#wlp3s0.service loaded failed failed Wireless network connectivity (wlp3s0)
However if I manually start this service after boot with:
systemctl start network-wireless#xlp3s0
the service starts as expected.
This is the content of wpa_supplicant.conf
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=wheel
network={
ssid="TeliaGateway30-91-8F-1C-B2-29"
#psk="A80871A90A"
psk=b4d8a1e9ad665eed0178fea6f141134e795e15183a661848b371a41bb73a6844
}
Why is this services starting ok when starting it manually but not at boot and how can I change it to start at boot?
EDIT: Added error output:
This is what error im getting:
[abc#arch ~]$ journalctl -b -u network-wireless#wlp3s0.service
-- Logs begin at Sat 2015-08-22 12:50:42 CEST, end at Sun 2015-08-23 22:15:26 CEST. --
Aug 23 21:23:36 arch systemd[1]: Starting Wireless network connectivity (wlp3s0)...
Aug 23 21:23:36 arch ip[274]: Cannot find device "wlp3s0"
Aug 23 21:23:36 arch systemd[1]: network-wireless#wlp3s0.service: Main process exited, code=exited, status=1/FAILURE
Aug 23 21:23:36 arch systemd[1]: Failed to start Wireless network connectivity (wlp3s0).
Aug 23 21:23:37 arch systemd[1]: network-wireless#wlp3s0.service: Unit entered failed state.
Aug 23 21:23:37 arch systemd[1]: network-wireless#wlp3s0.service: Failed with result 'exit-code'.
Aug 23 21:25:11 arch systemd[1]: Starting Wireless network connectivity (wlp3s0)...
Aug 23 21:25:11 arch dhcpcd[424]: wlp3s0: waiting for carrier
Aug 23 21:25:16 arch dhcpcd[424]: wlp3s0: carrier acquired
Aug 23 21:25:16 arch dhcpcd[424]: DUID 00:01:00:01:1d:6b:6b:e6:10:0d:7f:b7:30:f3
Aug 23 21:25:16 arch dhcpcd[424]: wlp3s0: IAID c1:c4:73:e0
Aug 23 21:25:16 arch dhcpcd[424]: wlp3s0: soliciting an IPv6 router
Aug 23 21:25:16 arch dhcpcd[424]: wlp3s0: rebinding lease of 192.168.1.85
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: leased 192.168.1.85 for 3600 seconds
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: adding route to 192.168.1.0/24
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: adding default route via 192.168.1.1
Aug 23 21:25:21 arch dhcpcd[424]: forked to background, child pid 477
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: waiting for carrier
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: carrier acquired
Aug 23 21:25:21 arch dhcpcd[424]: DUID 00:01:00:01:1d:6b:6b:e6:10:0d:7f:b7:30:f3
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: IAID c1:c4:73:e0
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: soliciting an IPv6 router
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: rebinding lease of 192.168.1.85
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: leased 192.168.1.85 for 3600 seconds
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: adding route to 192.168.1.0/24
Aug 23 21:25:21 arch dhcpcd[424]: wlp3s0: adding default route via 192.168.1.1
Aug 23 21:25:21 arch dhcpcd[424]: forked to background, child pid 477
Aug 23 21:25:21 arch systemd[1]: Started Wireless network connectivity (wlp3s0).
Aug 23 21:25:28 arch dhcpcd[477]: wlp3s0: no IPv6 Routers available
Aug 23 22:15:09 arch dhcpcd[477]: wlp3s0: carrier lost
Aug 23 22:15:09 arch dhcpcd[477]: wlp3s0: deleting route to 192.168.1.0/24
Aug 23 22:15:09 arch dhcpcd[477]: wlp3s0: deleting default route via 192.168.1.1
Aug 23 22:15:13 arch dhcpcd[477]: wlp3s0: carrier acquired
Aug 23 22:15:14 arch dhcpcd[477]: wlp3s0: IAID c1:c4:73:e0
Aug 23 22:15:14 arch dhcpcd[477]: wlp3s0: soliciting an IPv6 router
Aug 23 22:15:14 arch dhcpcd[477]: wlp3s0: rebinding lease of 192.168.1.85
Aug 23 22:15:19 arch dhcpcd[477]: wlp3s0: leased 192.168.1.85 for 3600 seconds
Aug 23 22:15:19 arch dhcpcd[477]: wlp3s0: adding route to 192.168.1.0/24
Aug 23 22:15:19 arch dhcpcd[477]: wlp3s0: adding default route via 192.168.1.1
Aug 23 22:15:19 arch dhcpcd[477]: wlp3s0: removing route to 192.168.1.0/24
Aug 23 22:15:26 arch dhcpcd[477]: wlp3s0: no IPv6 Routers available
EDIT:
I have found one potetential error, it seems as if the network interface changes name from wlan0 during boot, however i have tried starting the service with wlan0 but with no change in the result.
The reason the service stops at boot is because the ip command cannot find the interface. According to the man page for systemd services that is enough reason to fail the service.
As you noticed yourself the interface gets renamed or is not ready yet at boot.
You can check if you need to add an After= statement
You can check with the systemd-analyze command if the ordering at boot is correct
You could split up the service and make it more robust. Most daemons can start fine even if the interface is not ready yet.
Personally I would make dhcpd and wpa_supplicant separate services and use systemd's networkd or an udev rule to bring up the interface (if that is even needed). There are a lot examples of unit files for wpa_supplicant and dhcpd online maybe have look at those?

Resources