public inbox for developer@lists.illumos.org (since 2011-08)
 help / color / mirror / Atom feed
From: Marcel Telka <marcel@telka.sk>
To: illumos-developer <developer@lists.illumos.org>
Subject: Re: [developer] Raw ethernet packets
Date: Sun, 2 Jun 2024 11:05:40 +0200	[thread overview]
Message-ID: <Zlw15EaVyk7PArM7@telcontar> (raw)
In-Reply-To: <ZlmNl2B7u36Iyz7a@telcontar>

[-- Attachment #1: Type: text/plain, Size: 1460 bytes --]

On Fri, May 31, 2024 at 10:43:03AM +0200, Marcel Telka wrote:
> I'm trying to send a raw ethernet packet from an userland application
> but all my attempts so far were unsuccessful.  I basically tried two

Attached is the testing program ethersend.c that uses both bpf and
socket approaches to send the raw ethernet frame.  In both cases the
frame is silently dropped in mac_tx() because fe_tx_srs is NULL:

https://src.illumos.org/source/xref/illumos-gate/usr/src/uts/common/io/mac/mac_client.c?r=119d61cc&mo=100885&fi=3519#3576

There is attached the ethersend.d dtrace script that shows the issue:

# gcc -Wall -lsocket -o ethersend ethersend.c
# ./ethersend.d -c './ethersend e1000g0' 2>&1 | grep mac_tx
  1              -> mac_tx                    
  1               | mac_tx:entry                              0
  1              <- mac_tx                                    0
  1            -> mac_tx                      
  1             | mac_tx:entry                                0
  1            <- mac_tx                                      0
#

Since something similar reportedly works on dilos (I assume it is a fork
of old illumos-gate) then it looks like the issue is in new illumos.

I'll try to do some archaeology...

-- 
+-------------------------------------------+
| Marcel Telka   e-mail:   marcel@telka.sk  |
|                homepage: http://telka.sk/ |
+-------------------------------------------+

[-- Attachment #2: ethersend.c --]
[-- Type: text/plain, Size: 2727 bytes --]

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <net/if.h>
#include <net/bpf.h>
#include <sys/socket.h>
#include <sys/sockio.h>

char packet[] =
	"\xff\xff\xff\xff\xff\xff"
	"\x00\x1c\x25\xa0\xb7\x2e"
	"\x08\x06\x00\x01"
	"\x08\x00\x06\x04\x00\x01"
	"\x00\x1c\x25\xa0\xb7\x2e"
	"\x0a\x00\x00\x0d"
	"\xff\xff\xff\xff\xff\xff"
	"\x0a\x00\x00\x3d"
	"\x00\x00\x00\x00\x00\x00"
	"\x00\x00\x00\x00\x00\x00"
	"\x00\x00\x00\x00\x00\x00"
;

int
send_bpf(const char *iface, char *data, size_t len)
{
	int fd = open("/dev/bpf", O_RDWR);
	if (fd < 0) {
		perror("open");
		return -1;
	}

	struct ifreq ifr;

	(void) memset(&ifr, 0, sizeof ifr);
	(void) memcpy(&ifr.ifr_name, iface, strlen(iface));
	if (ioctl(fd, BIOCSETIF, &ifr) < 0) {
		perror("BIOCSETIF");
		return -2;
	}

	int enable;
       
	enable = 1;
	if (ioctl(fd, BIOCIMMEDIATE, &enable) < 0) {
		perror("BIOCIMMEDIATE");
		return -3;
	}

	enable = 1;
	if (ioctl(fd, BIOCSHDRCMPLT, &enable) < 0) {
		perror("BIOCSHDRCMPLT");
		return -4;
	}

	size_t ret = write(fd, data, len);
	if (ret >= 0)
		printf("send_bpf sent %ld bytes\n", ret);
	if (ret != len) {
		fprintf(stderr, "write returned %ld, expected %ld\n", ret, len);
		return -5;
	}

	if (close(fd) < 0) {
		perror("close");
		return -6;
	}

	return 0;
}

int
send_socket(const char *iface, char *data, size_t len)
{
	int s = socket(AF_PACKET, SOCK_RAW, 0);
	if (s < 0) {
		perror("socket");
		return -1;
	}

	struct ifreq ifr;

	(void) memset(&ifr, 0, sizeof ifr);
	(void) memcpy(&ifr.ifr_name, iface, strlen(iface));
	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
		perror("SIOCGIFINDEX");
		return -2;
	}

	struct sockaddr_ll lls;
	(void) memset(&lls, 0, sizeof lls);
	lls.sll_family = AF_PACKET;
	lls.sll_protocol = 0;
	lls.sll_ifindex = ifr.ifr_index;

	if (bind(s, (struct sockaddr *)&lls, sizeof lls) < 0) {
		perror("bind");
		return -3;
	}

	size_t ret = send(s, data, len, 0);
	if (ret >= 0)
		printf("send_socket sent %ld bytes\n", ret);
	if (ret != len) {
		fprintf(stderr, "send returned %ld, expected %ld\n", ret, len);
		return -4;
	}

	if (close(s) < 0) {
		perror("close");
		return -5;
	}

	return 0;
}

int main(int argc, char *argv[])
{
	if (argc != 2) {
		fprintf(stderr, "Missing interface\n");
		return 1;
	}

	int ret;

	ret = send_bpf(argv[1], packet, sizeof packet - 1);
	if (ret != 0)
		fprintf(stderr, "send_bpf: %d\n", ret);

	ret = send_socket(argv[1], packet, sizeof packet - 1);
	if (ret != 0)
		fprintf(stderr, "send_socket: %d\n", ret);

	return 0;
}

[-- Attachment #3: ethersend.d --]
[-- Type: text/plain, Size: 430 bytes --]

#!/usr/sbin/dtrace -s

#pragma D option flowindent

fbt::send:entry
{
	self->t = 1;
}

fbt::write:entry
/execname == "ethersend" && arg0 > 2/
{
	self->t = 1;
}

fbt:::entry
/self->t/
{
}

fbt:::return
/self->t/
{
	trace(arg1);
}

mac_tx:entry
/self->t/
{
	trace(((mac_client_impl_t *)arg0)->mci_flent->fe_tx_srs);
}

fbt::send:return
{
	self->t = 0;
}

fbt::write:return
{
	self->t = 0;
}

  parent reply	other threads:[~2024-06-02  9:05 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-31  8:43 Marcel Telka
2024-05-31  8:54 ` [developer] " Marcel Telka
2024-05-31  9:13   ` Denis Kozadaev
2024-05-31  9:51     ` Marcel Telka
2024-05-31 11:08       ` Marcel Telka
2024-05-31 11:23         ` Denis Kozadaev
2024-05-31 12:07           ` Marcel Telka
2024-05-31 12:18             ` Denis Kozadaev
2024-05-31 13:33               ` Marcel Telka
2024-05-31 14:15                 ` Denis Kozadaev
2024-05-31 15:04                   ` Marcel Telka
2024-05-31 15:20                     ` Denis Kozadaev
2024-05-31  9:03 ` Joshua M. Clulow
2024-05-31  9:50   ` Marcel Telka
2024-05-31 13:45     ` Marcel Telka
2024-05-31  9:43 ` Pramod Batni
2024-05-31  9:49   ` Marcel Telka
2024-06-02  9:05 ` Marcel Telka [this message]
2024-06-06 12:54   ` Marcel Telka
2024-06-06 16:51     ` Alan Coopersmith

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Zlw15EaVyk7PArM7@telcontar \
    --to=marcel@telka.sk \
    --cc=developer@lists.illumos.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).