From owner-freebsd-hackers@FreeBSD.ORG Fri Apr 25 05:42:46 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id D48CD51C for ; Fri, 25 Apr 2014 05:42:46 +0000 (UTC) Received: from mail-qc0-x234.google.com (mail-qc0-x234.google.com [IPv6:2607:f8b0:400d:c01::234]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 9391B10F5 for ; Fri, 25 Apr 2014 05:42:46 +0000 (UTC) Received: by mail-qc0-f180.google.com with SMTP id w7so3535864qcr.25 for ; Thu, 24 Apr 2014 22:42:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eitanadler.com; s=0xdeadbeef; h=mime-version:from:date:message-id:subject:to:content-type; bh=fU/3fwB/hwO3emrh3O9I3b4527qYwv83Nw3tgkkQ7CE=; b=JTcarKjKdy6pbmSpGfYacPO7y7FG3HIzAhr8+Ck9F3C0sswH1pXEkSH2K6t07fIqB4 b3IaMtmhLUiC/eAZs47V6qQq/g26+eLgkWWA8Rh5zd1UOZ6zRPYbNtlj8T9716ONw2GL Z1Z6fgEFdSzzIoOSvz47RKvuWd6It2nPA3vHI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to :content-type; bh=fU/3fwB/hwO3emrh3O9I3b4527qYwv83Nw3tgkkQ7CE=; b=d/l1CCuUv5AP5V3MYF3pJk/5cCuCw7sKIynpX5Lz+hWQ6vFVHMYw4v2U3RLhEdpmJ6 Ax63FVPOOZG0ebAPOChk0zTucxI1lizqGx5TsjFOa2iG1viiQjmCTaKh5lwEB5dRJfVZ TWOf6cdmkt2/b/gf5wWq9mRoqw3IkeetSCq5XukvQIVpjemabGLAX/eKr7ywmzv31YR0 bM4TA9ei1SrRtehK2OGotTye9yxxYo6WfwY2L6GoVbv4LP1fWMoAm2oZAevENyliJbgn y6oqr5xypa2XVAS+NOumwSEzYz4/WlbR3pHAHgiZ2vci6e56XYPMhRc22/PQOBCwzniQ v8FA== X-Gm-Message-State: ALoCoQlq27ZA3n1JZDGt2odytCCx52+kF0Mls2GBWdP182BJZFcNb9tiq1CEKbh49HYNEp3Ayyb9 X-Received: by 10.224.67.131 with SMTP id r3mr8672511qai.75.1398404564816; Thu, 24 Apr 2014 22:42:44 -0700 (PDT) MIME-Version: 1.0 Received: by 10.96.54.102 with HTTP; Thu, 24 Apr 2014 22:42:14 -0700 (PDT) From: Eitan Adler Date: Thu, 24 Apr 2014 22:42:14 -0700 Message-ID: Subject: request for help: netcat kernel module for FreeBSD To: FreeBSD Hackers Content-Type: text/plain; charset=UTF-8 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Apr 2014 05:42:46 -0000 Hi all, In an effort to teach myself how to write a kernel module I decided to port the netcat kernel module to FreeBSD. For those that havn't heard of it, the original is here: https://github.com/usrbinnc/netcat-cpi-kernel-module. You will need the original to compile this as well (for the data files) N.B. This is not intended to commit. When I load this module the system just freezes. If I put a return (0); right before make_dev_p it works. Can anyone help me figure out what else I did wrong. (read() code is untested atm) #include #include #include #include #include /* cdevsw struct */ #include #include #include "tracks/trk1.h" #include "tracks/trk2.h" #include "tracks/trk3.h" #include "tracks/trk4.h" #include "tracks/trk5.h" #include "tracks/trk6.h" /* Function prototypes */ static d_open_t netcat_open; static d_close_t netcat_close; static d_read_t netcat_read; static d_write_t netcat_write; static struct cdevsw netcat_cdevsw = { .d_version = D_VERSION, .d_open = netcat_open, .d_close = netcat_close, .d_read = netcat_read, .d_write = netcat_write, .d_name = "netcat", }; static char *tracks[] = {netcat_cpi_trk1, netcat_cpi_trk2, netcat_cpi_trk3, netcat_cpi_trk4, netcat_cpi_trk5, netcat_cpi_trk6}; static char *tracknames[] = {"Interrupt 0x7f", "The Internet is an Apt Motherfucker", "Interrupt 0x0d", "netcat", "Interrupt 0xbb", "Approximating the Circumference of the Earth"}; static unsigned long tracklens[] = {NETCAT_CPI_TRK1_LEN, NETCAT_CPI_TRK2_LEN, NETCAT_CPI_TRK3_LEN, NETCAT_CPI_TRK4_LEN, NETCAT_CPI_TRK5_LEN, NETCAT_CPI_TRK6_LEN}; static struct { char *msg; int current_track; size_t bytes; } netcatdata; static struct cdev *netcat_dev; static int netcat_loader(struct module *module, int event, void *arg) { int e = 0; switch (event) { case MOD_LOAD: uprintf("netcat - Cycles Per Instruction - Kernel Module Edition - 2014\n"); uprintf("netcat is Brandon Lucia, Andrew Olmstead, and David Balatero\n"); uprintf("On the web at http://netcat.co\n"); uprintf("'ogg123 - < /dev/netcat' to play.\n"); e = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &netcat_dev, &netcat_cdevsw, 0, UID_ROOT, GID_WHEEL, 0444, "netcat"); if (e != 0) break; break; case MOD_UNLOAD: destroy_dev(netcat_dev); break; default: e = EOPNOTSUPP; break; } return (e); } static int netcat_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused) { int error = 0; netcatdata.current_track = 0; netcatdata.msg = tracks[netcatdata.current_track]; /* track 1 */ netcatdata.bytes = 0; uprintf("Now playing %s\n", tracknames[netcatdata.current_track]); return (error); } static int netcat_close(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused) { int error = 0; return (error); } static int netcat_read(struct cdev *dev __unused, struct uio *uio, int ioflag __unused) { int error = 0; size_t totalbytesleft; totalbytesleft = uio->uio_resid; while (totalbytesleft > 0) { int amt = MIN(uio->uio_resid, tracklens[netcatdata.current_track] - netcatdata.bytes); error = uiomove(tracks[netcatdata.current_track] + uio->uio_offset, amt, uio); netcatdata.bytes += amt; if (netcatdata.bytes == tracklens[netcatdata.current_track]) { uprintf("Now playing %s\n", tracknames[netcatdata.current_track]); netcatdata.bytes = 0; netcatdata.current_track = (netcatdata.current_track + 1) % 6; } } error = uiomove(tracks[netcatdata.current_track], 1, uio); if (error != 0) uprintf("uiomove failed!\n"); return (error); } static int netcat_write(struct cdev *dev __unused, struct uio *uio, int ioflag __unused) { int error = EOPNOTSUPP; return (error); } DEV_MODULE(netcat, netcat_loader, NULL); -- Eitan Adler