Annotation of embedaddon/libnet/src/libnet_link_pf.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  *  $Id: libnet_link_pf.c,v 1.3 2004/01/03 20:31:02 mike Exp $
        !             3:  *
        !             4:  *  libnet
        !             5:  *  libnet_pf.c - pf routines
        !             6:  *
        !             7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
        !             8:  *  All rights reserved.
        !             9:  *
        !            10:  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
        !            11:  *     The Regents of the University of California.  All rights reserved.
        !            12:  *
        !            13:  * Redistribution and use in source and binary forms, with or without
        !            14:  * modification, are permitted provided that: (1) source code distributions
        !            15:  * retain the above copyright notice and this paragraph in its entirety, (2)
        !            16:  * distributions including binary code include the above copyright notice and
        !            17:  * this paragraph in its entirety in the documentation or other materials
        !            18:  * provided with the distribution, and (3) all advertising materials mentioning
        !            19:  * features or use of this software display the following acknowledgement:
        !            20:  * ``This product includes software developed by the University of California,
        !            21:  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
        !            22:  * the University nor the names of its contributors may be used to endorse
        !            23:  * or promote products derived from this software without specific prior
        !            24:  * written permission.
        !            25:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
        !            26:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
        !            27:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            28:  *
        !            29:  * packet filter subroutines for tcpdump
        !            30:  *     Extraction/creation by Jeffrey Mogul, DECWRL
        !            31:  */
        !            32: 
        !            33: #if (HAVE_CONFIG_H)
        !            34: #include "../include/config.h"
        !            35: #endif
        !            36: #include "../include/low_libnet.h"
        !            37: 
        !            38: #include "../include/gnuc.h"
        !            39: #ifdef HAVE_OS_PROTO_H
        !            40: #include "../include/os-proto.h"
        !            41: #endif
        !            42: 
        !            43: struct libnet_link_int *
        !            44: libnet_open_link_interface(int8_t *device, int8_t *ebuf)
        !            45: {
        !            46:     register struct libnet_link_int *l;
        !            47:     int16_t enmode;
        !            48:     int backlog = -1;   /* request the most */
        !            49:     struct enfilter Filter;
        !            50:     struct endevp devparams;
        !            51: 
        !            52:     l = (struct libnet_link_int *)malloc(sizeof(*l));
        !            53:     if (l == NULL)
        !            54:     {
        !            55:         sprintf(ebuf, "libnet_open_link_int: %s", strerror(errno));
        !            56:         return (0);
        !            57:     }
        !            58:     memset(l, 0, sizeof(*l));
        !            59:     l->fd = pfopen(device, O_RDWR);
        !            60:     if (l->fd < 0)
        !            61:     {
        !            62:         sprintf(ebuf, "pf open: %s: %s\n\your system may not be properly configured; see \"man packetfilter(4)\"\n",
        !            63:             device, strerror(errno));
        !            64:         goto bad;
        !            65:     }
        !            66: 
        !            67:     enmode = ENTSTAMP|ENBATCH|ENNONEXCL;
        !            68:     if (ioctl(l->fd, EIOCMBIS, (caddr_t)&enmode) < 0)
        !            69:     {
        !            70:         sprintf(ebuf, "EIOCMBIS: %s", strerror(errno));
        !            71:         goto bad;
        !            72:     }
        !            73: #ifdef ENCOPYALL
        !            74:     /* Try to set COPYALL mode so that we see packets to ourself */
        !            75:     enmode = ENCOPYALL;
        !            76:     ioctl(l->fd, EIOCMBIS, (caddr_t)&enmode);   /* OK if this fails */
        !            77: #endif
        !            78:        /* set the backlog */
        !            79:     if (ioctl(l->fd, EIOCSETW, (caddr_t)&backlog) < 0)
        !            80:     {
        !            81:         sprintf(ebuf, "EIOCSETW: %s", strerror(errno));
        !            82:         goto bad;
        !            83:     }
        !            84:     /*
        !            85:      *  discover interface type
        !            86:      */
        !            87:     if (ioctl(l->fd, EIOCDEVP, (caddr_t)&devparams) < 0)
        !            88:     {
        !            89:         sprintf(ebuf, "EIOCDEVP: %s", strerror(errno));
        !            90:         goto bad;
        !            91:     }
        !            92: 
        !            93:     /* HACK: to compile prior to Ultrix 4.2 */
        !            94: #ifndef        ENDT_FDDI
        !            95: #define        ENDT_FDDI   4
        !            96: #endif
        !            97:     switch (devparams.end_dev_type)
        !            98:     {
        !            99:         case ENDT_10MB:
        !           100:             l->linktype = DLT_EN10MB;
        !           101:             break;
        !           102:         case ENDT_FDDI:
        !           103:             l->linktype = DLT_FDDI;
        !           104:             break;
        !           105:         default:
        !           106:             /*
        !           107:              * XXX
        !           108:              * Currently, the Ultrix packet filter supports only
        !           109:              * Ethernet and FDDI.  Eventually, support for SLIP and PPP
        !           110:              * (and possibly others: T1?) should be added.
        !           111:              */
        !           112:             l->linktype = DLT_EN10MB;
        !           113:             break;
        !           114:        }
        !           115:     /*
        !           116:      *  acceptag all packets
        !           117:      */
        !           118:     bzero((int8_t *)&Filter, sizeof(Filter));
        !           119:     Filter.enf_Priority = 37;  /* anything > 2 */
        !           120:     Filter.enf_FilterLen = 0;  /* means "always true" */
        !           121:     if (ioctl(l->fd, EIOCSETF, (caddr_t)&Filter) < 0)
        !           122:     {
        !           123:         sprintf(ebuf, "EIOCSETF: %s", strerror(errno));
        !           124:         goto bad;
        !           125:     }
        !           126: 
        !           127:     return (l);
        !           128: bad:
        !           129:     free(l);
        !           130:     return (NULL);
        !           131: }
        !           132: 
        !           133: 
        !           134: int
        !           135: libnet_close_link_interface(struct libnet_link_int *l)
        !           136: {
        !           137:     if (close(l->fd) == 0)
        !           138:     {
        !           139:         free(l);
        !           140:         return (1);
        !           141:     }
        !           142:     else
        !           143:     {
        !           144:         free(l);
        !           145:         return (-1);
        !           146:     }
        !           147: }
        !           148: 
        !           149: 
        !           150: int
        !           151: libnet_write_link_layer(struct libnet_link_int *l, const int8_t *device,
        !           152:             const u_int8_t *buf, int len)
        !           153: {
        !           154:     int c;
        !           155: 
        !           156:     c = write(l->fd, buf, len);
        !           157:     if (c != len)
        !           158:     {
        !           159:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
        !           160:             "libnet_write_link: %d bytes written (%s)\n", c,
        !           161:             strerror(errno));
        !           162:     }
        !           163:     return (c);
        !           164: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>