Creating socket(9) functions in FreeBSD 8 kernel

Sadish Kulasekere sadishkr at gmail.com
Sun Sep 12 04:57:00 UTC 2010


Hi,

I've  been trying to write a program to send a message to a specific host
with a specific MAC address. I used the socket(9) kernel interface socket
functions, but the kernel keeps crashing when I try to load it. I have
posted the code below. Can someone please tell me what is wrong with this
code?

Cheers,
Sadish.

#include <sys/errno.h>
//#include <sys/type.h>
//#include <netinet/in.h>
#include <sys/param.h>
//#include <sys/errno.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/types.h>
//#include <sys/socket.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/ioccom.h>
#include <sys/sysent.h>
#include <sys/sysproto.h>
#include <sys/proc.h>
#include <sys/syscall.h>
#include <sys/queue.h>
#include <machine/iodev.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/pfil.h>
//#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/tcp_var.h>
#include <netinet/in_pcb.h>
#include <netinet/in_var.h>

#include <sys/socketvar.h>

static int wake(void);
//typedef u8;

static int wake(void)
{
      unsigned int i,j;
      unsigned char k[6] = {0x1F,0x00,0xDD,0x1D,0x3B,0x2E}; /*need to fix
the correct MAC*/
      unsigned char wolBuff[136];
      unsigned char* ptr;
      //int optval = 1;
      int packet;
      //int sendint;
      struct socket *sok;
      struct sockaddr_in sap;
      //struct sockaddr *sap;
      //struct ucred *crd;
      //struct thread *trd;
      struct uio auio;
      struct iovec aiov;
      struct sockopt sokoptions;

      aiov.iov_base = (caddr_t) &wolBuff;//&wolBuff;
      aiov.iov_len = sizeof(wolBuff);//strlen(wolBuff);

      auio.uio_iov = &aiov;
      auio.uio_iovcnt = 1;
      auio.uio_offset = 0;
      auio.uio_resid = sizeof(wolBuff);//strlen(wolBuff);
      auio.uio_segflg = UIO_SYSSPACE;
      auio.uio_rw = UIO_WRITE;
      auio.uio_td = 0;

      //sokoptions.sopt_dir = SOPT_SET;
      //sokoptions.sopt_level = SOL_SOCKET;
      //sokoptions.sopt_name = packet;
      //sokoptions.sopt_val = 0;
      //sokoptions.sopt_valsize = 0;
      //sokoptions.sopt_td = NULL;


      ptr = wolBuff;
      for(i=0; i<6; i++)
      {
            *ptr++ = 0xFF;
      }
      for(j=0; j<16; j++)
      {
            for(i=0; i<6; i++)
            {
                  *ptr++ = k[i];
            }
      }

      //uprintf("print buffer %u",*ptr);

      //packet = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
      packet = socreate(AF_LINK, &sok, SOCK_DGRAM,0, 0, 0);

     if(sok==NULL)
      {
       uprintf("sok null\n");
       goto ret;
      }

      sokoptions.sopt_dir = SOPT_SET;
      sokoptions.sopt_level = SOL_SOCKET;
      sokoptions.sopt_name = packet;
      sokoptions.sopt_val = 0;
      sokoptions.sopt_valsize = 0;
      sokoptions.sopt_td = NULL;

      //sosetopt((struct socket *)packet, &sokoptions);

      /* Set up broadcast address */
        sap.sin_family = AF_INET;
        sap.sin_addr.s_addr = INADDR_BROADCAST;//htonl(0xffffffff)

        sap.sin_port = htons(6000);

      uprintf("aaaaaaaaaaaaa \n"); //just for testing

      packet = sosend(sok, (struct sockaddr *)&sap, &auio, 0, 0, 0, 0);
      if(packet <0)
      {
        uprintf("bbbbbbbbbbbbb \n");
        goto ret;
      }

      //soclose(sok);

      ret:
  return 0;

}

static int deinit(void)
{

    uprintf("module unloaded \n");
    return 0;

}

/* The function to load/unload kernel module*/
static int event_handler(struct module *module, int event, void *arg)
{
      int error =0; /* Error, 0 for normal return status*/

      switch (event)
      {
      case MOD_LOAD:
        error = wake();
        break;
      case MOD_UNLOAD:
        error = deinit();
        break;
      default:
        error = EOPNOTSUPP; /*Error operation not supported*/
        break;

      }

    return(error);

}

/*argument for DECLARE_MODULE macro*/
static moduledata_t test_conf ={
  "WOLgenerator",  /*module name */
  event_handler, /* event handler function*/
  "NULL"    /* extra data*/

};

/*int
dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
{
        int err = 0;

        if (count > 0)
                return EBUSY;
        count = 1;
        return (err);
}

int
dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
{
        int err = 0;
        count = 0;
        return (err);
}

int
dev_read(struct cdev *dev, struct uio *uio, int ioflag)
{
    int rv = 0;

    sprintf(flwbuf, "%016d, %9u \n", bytes,sport);
    rv = uiomove(flwbuf, MIN(uio->uio_resid, strlen(flwbuf)), uio);
    return rv;
}*/

DECLARE_MODULE(WOLgenerator,test_conf,SI_SUB_KLD,SI_ORDER_ANY); /* the name
of module, the moduledata_t structure,???,order of loading*/


More information about the freebsd-net mailing list