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

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: 
1.1.1.3 ! misho      33: #include "common.h"
1.1       misho      34: 
                     35: #include "../include/gnuc.h"
                     36: #ifdef HAVE_OS_PROTO_H
                     37: #include "../include/os-proto.h"
                     38: #endif
                     39: 
                     40: struct libnet_link_int *
                     41: libnet_open_link_interface(int8_t *device, int8_t *ebuf)
                     42: {
                     43:     register struct libnet_link_int *l;
                     44:     int16_t enmode;
                     45:     int backlog = -1;   /* request the most */
                     46:     struct enfilter Filter;
                     47:     struct endevp devparams;
                     48: 
                     49:     l = (struct libnet_link_int *)malloc(sizeof(*l));
                     50:     if (l == NULL)
                     51:     {
1.1.1.3 ! misho      52:         snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !            53:                  "libnet_open_link_int: %s", strerror(errno));
1.1       misho      54:         return (0);
                     55:     }
                     56:     memset(l, 0, sizeof(*l));
                     57:     l->fd = pfopen(device, O_RDWR);
                     58:     if (l->fd < 0)
                     59:     {
1.1.1.3 ! misho      60:         snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !            61:                  "pf open: %s: %s\nyour system may not be properly configured; see \"man packetfilter(4)\"",
1.1       misho      62:             device, strerror(errno));
                     63:         goto bad;
                     64:     }
                     65: 
                     66:     enmode = ENTSTAMP|ENBATCH|ENNONEXCL;
                     67:     if (ioctl(l->fd, EIOCMBIS, (caddr_t)&enmode) < 0)
                     68:     {
1.1.1.3 ! misho      69:         snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !            70:                  "EIOCMBIS: %s", strerror(errno));
1.1       misho      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:     {
1.1.1.3 ! misho      81:         snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !            82:                  "EIOCSETW: %s", strerror(errno));
1.1       misho      83:         goto bad;
                     84:     }
                     85:     /*
                     86:      *  discover interface type
                     87:      */
                     88:     if (ioctl(l->fd, EIOCDEVP, (caddr_t)&devparams) < 0)
                     89:     {
1.1.1.3 ! misho      90:         snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !            91:                  "EIOCDEVP: %s", strerror(errno));
1.1       misho      92:         goto bad;
                     93:     }
                     94: 
                     95:     /* HACK: to compile prior to Ultrix 4.2 */
                     96: #ifndef        ENDT_FDDI
                     97: #define        ENDT_FDDI   4
                     98: #endif
                     99:     switch (devparams.end_dev_type)
                    100:     {
                    101:         case ENDT_10MB:
                    102:             l->linktype = DLT_EN10MB;
                    103:             break;
                    104:         case ENDT_FDDI:
                    105:             l->linktype = DLT_FDDI;
                    106:             break;
                    107:         default:
                    108:             /*
                    109:              * XXX
                    110:              * Currently, the Ultrix packet filter supports only
                    111:              * Ethernet and FDDI.  Eventually, support for SLIP and PPP
                    112:              * (and possibly others: T1?) should be added.
                    113:              */
                    114:             l->linktype = DLT_EN10MB;
                    115:             break;
                    116:        }
                    117:     /*
                    118:      *  acceptag all packets
                    119:      */
                    120:     bzero((int8_t *)&Filter, sizeof(Filter));
                    121:     Filter.enf_Priority = 37;  /* anything > 2 */
                    122:     Filter.enf_FilterLen = 0;  /* means "always true" */
                    123:     if (ioctl(l->fd, EIOCSETF, (caddr_t)&Filter) < 0)
                    124:     {
1.1.1.3 ! misho     125:         snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !           126:                  "EIOCSETF: %s", strerror(errno));
1.1       misho     127:         goto bad;
                    128:     }
                    129: 
                    130:     return (l);
                    131: bad:
                    132:     free(l);
                    133:     return (NULL);
                    134: }
                    135: 
                    136: 
                    137: int
                    138: libnet_close_link_interface(struct libnet_link_int *l)
                    139: {
                    140:     if (close(l->fd) == 0)
                    141:     {
                    142:         free(l);
                    143:         return (1);
                    144:     }
                    145:     else
                    146:     {
                    147:         free(l);
                    148:         return (-1);
                    149:     }
                    150: }
                    151: 
                    152: 
                    153: int
                    154: libnet_write_link_layer(struct libnet_link_int *l, const int8_t *device,
1.1.1.2   misho     155:             const uint8_t *buf, int len)
1.1       misho     156: {
                    157:     int c;
                    158: 
                    159:     c = write(l->fd, buf, len);
                    160:     if (c != len)
                    161:     {
                    162:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
1.1.1.3 ! misho     163:             "libnet_write_link: %d bytes written (%s)", c,
1.1       misho     164:             strerror(errno));
                    165:     }
                    166:     return (c);
                    167: }
1.1.1.3 ! misho     168: 
        !           169: /**
        !           170:  * Local Variables:
        !           171:  *  indent-tabs-mode: nil
        !           172:  *  c-file-style: "stroustrup"
        !           173:  * End:
        !           174:  */

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