1  | 
     | 
     | 
    /*	$OpenBSD: probe.c,v 1.4 2015/05/05 01:26:37 jsg Exp $ */  | 
    
    
    2  | 
     | 
     | 
     | 
    
    
    3  | 
     | 
     | 
    /*  | 
    
    
    4  | 
     | 
     | 
     * Copyright (c) 2005, 2006 Esben Norby <norby@openbsd.org>  | 
    
    
    5  | 
     | 
     | 
     *  | 
    
    
    6  | 
     | 
     | 
     * Permission to use, copy, modify, and distribute this software for any  | 
    
    
    7  | 
     | 
     | 
     * purpose with or without fee is hereby granted, provided that the above  | 
    
    
    8  | 
     | 
     | 
     * copyright notice and this permission notice appear in all copies.  | 
    
    
    9  | 
     | 
     | 
     *  | 
    
    
    10  | 
     | 
     | 
     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
    
    
    11  | 
     | 
     | 
     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
    
    
    12  | 
     | 
     | 
     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
    
    
    13  | 
     | 
     | 
     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
    
    
    14  | 
     | 
     | 
     * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN  | 
    
    
    15  | 
     | 
     | 
     * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  | 
    
    
    16  | 
     | 
     | 
     * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
    
    
    17  | 
     | 
     | 
     */  | 
    
    
    18  | 
     | 
     | 
     | 
    
    
    19  | 
     | 
     | 
    #include <sys/types.h>  | 
    
    
    20  | 
     | 
     | 
    #include <sys/socket.h>  | 
    
    
    21  | 
     | 
     | 
    #include <netinet/in.h>  | 
    
    
    22  | 
     | 
     | 
    #include <arpa/inet.h>  | 
    
    
    23  | 
     | 
     | 
    #include <sys/time.h>  | 
    
    
    24  | 
     | 
     | 
    #include <stdlib.h>  | 
    
    
    25  | 
     | 
     | 
    #include <string.h>  | 
    
    
    26  | 
     | 
     | 
    #include <event.h>  | 
    
    
    27  | 
     | 
     | 
     | 
    
    
    28  | 
     | 
     | 
    #include "igmp.h"  | 
    
    
    29  | 
     | 
     | 
    #include "dvmrpd.h"  | 
    
    
    30  | 
     | 
     | 
    #include "dvmrp.h"  | 
    
    
    31  | 
     | 
     | 
    #include "log.h"  | 
    
    
    32  | 
     | 
     | 
    #include "dvmrpe.h"  | 
    
    
    33  | 
     | 
     | 
     | 
    
    
    34  | 
     | 
     | 
    extern struct dvmrpd_conf	*deconf;  | 
    
    
    35  | 
     | 
     | 
     | 
    
    
    36  | 
     | 
     | 
    /* DVMRP probe packet handling */  | 
    
    
    37  | 
     | 
     | 
    int  | 
    
    
    38  | 
     | 
     | 
    send_probe(struct iface *iface)  | 
    
    
    39  | 
     | 
     | 
    { | 
    
    
    40  | 
     | 
     | 
    	struct sockaddr_in	 dst;  | 
    
    
    41  | 
     | 
     | 
    	struct ibuf		*buf;  | 
    
    
    42  | 
     | 
     | 
    	struct dvmrp_hdr	*dvmrp_hdr;  | 
    
    
    43  | 
     | 
     | 
    	struct nbr		*nbr;  | 
    
    
    44  | 
     | 
     | 
    	int			 ret = 0;  | 
    
    
    45  | 
     | 
     | 
     | 
    
    
    46  | 
     | 
     | 
    	if (iface->passive)  | 
    
    
    47  | 
     | 
     | 
    		return (0);  | 
    
    
    48  | 
     | 
     | 
     | 
    
    
    49  | 
     | 
     | 
    	if ((buf = ibuf_open(iface->mtu - sizeof(struct ip))) == NULL)  | 
    
    
    50  | 
     | 
     | 
    		fatal("send_probe"); | 
    
    
    51  | 
     | 
     | 
     | 
    
    
    52  | 
     | 
     | 
    	/* DVMRP header */  | 
    
    
    53  | 
     | 
     | 
    	if (gen_dvmrp_hdr(buf, iface, DVMRP_CODE_PROBE))  | 
    
    
    54  | 
     | 
     | 
    		goto fail;  | 
    
    
    55  | 
     | 
     | 
     | 
    
    
    56  | 
     | 
     | 
    	/* generation ID */  | 
    
    
    57  | 
     | 
     | 
    	ibuf_add(buf, &iface->gen_id, sizeof(iface->gen_id));  | 
    
    
    58  | 
     | 
     | 
     | 
    
    
    59  | 
     | 
     | 
    	/* generate neighbor list */  | 
    
    
    60  | 
     | 
     | 
    	LIST_FOREACH(nbr, &iface->nbr_list, entry) { | 
    
    
    61  | 
     | 
     | 
    		if (nbr->state > NBR_STA_DOWN)  | 
    
    
    62  | 
     | 
     | 
    			ibuf_add(buf, &nbr->id, sizeof(nbr->id));  | 
    
    
    63  | 
     | 
     | 
    	}  | 
    
    
    64  | 
     | 
     | 
     | 
    
    
    65  | 
     | 
     | 
    	/* set destination address */  | 
    
    
    66  | 
     | 
     | 
    	dst.sin_family = AF_INET;  | 
    
    
    67  | 
     | 
     | 
    	dst.sin_len = sizeof(struct sockaddr_in);  | 
    
    
    68  | 
     | 
     | 
    	inet_aton(AllDVMRPRouters, &dst.sin_addr);  | 
    
    
    69  | 
     | 
     | 
     | 
    
    
    70  | 
     | 
     | 
    	/* update chksum */  | 
    
    
    71  | 
     | 
     | 
    	dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr));  | 
    
    
    72  | 
     | 
     | 
    	dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos);  | 
    
    
    73  | 
     | 
     | 
     | 
    
    
    74  | 
     | 
     | 
    	ret = send_packet(iface, buf->buf, buf->wpos, &dst);  | 
    
    
    75  | 
     | 
     | 
    	ibuf_free(buf);  | 
    
    
    76  | 
     | 
     | 
    	return (ret);  | 
    
    
    77  | 
     | 
     | 
    fail:  | 
    
    
    78  | 
     | 
     | 
    	log_warn("send_probe"); | 
    
    
    79  | 
     | 
     | 
    	ibuf_free(buf);  | 
    
    
    80  | 
     | 
     | 
    	return (-1);  | 
    
    
    81  | 
     | 
     | 
    }  | 
    
    
    82  | 
     | 
     | 
     | 
    
    
    83  | 
     | 
     | 
    void  | 
    
    
    84  | 
     | 
     | 
    recv_probe(struct iface *iface, struct in_addr src, u_int32_t src_ip,  | 
    
    
    85  | 
     | 
     | 
        u_int8_t capabilities, char *buf, u_int16_t len)  | 
    
    
    86  | 
     | 
     | 
    { | 
    
    
    87  | 
     | 
     | 
    	struct nbr	*nbr = NULL;  | 
    
    
    88  | 
     | 
     | 
    	u_int32_t	 gen_id;  | 
    
    
    89  | 
     | 
     | 
    	u_int32_t	 nbr_id;  | 
    
    
    90  | 
     | 
     | 
     | 
    
    
    91  | 
     | 
     | 
    	LIST_FOREACH(nbr, &iface->nbr_list, entry) { | 
    
    
    92  | 
     | 
     | 
    		if (nbr->id.s_addr == src_ip)  | 
    
    
    93  | 
     | 
     | 
    			break;  | 
    
    
    94  | 
     | 
     | 
    	}  | 
    
    
    95  | 
     | 
     | 
     | 
    
    
    96  | 
     | 
     | 
    	memcpy(&gen_id, buf, sizeof(gen_id));  | 
    
    
    97  | 
     | 
     | 
    	len -= sizeof(gen_id);  | 
    
    
    98  | 
     | 
     | 
    	buf += sizeof(gen_id);  | 
    
    
    99  | 
     | 
     | 
     | 
    
    
    100  | 
     | 
     | 
    	if (!nbr) { | 
    
    
    101  | 
     | 
     | 
    		nbr = nbr_new(src_ip, iface, 0);  | 
    
    
    102  | 
     | 
     | 
    		nbr->gen_id = gen_id;  | 
    
    
    103  | 
     | 
     | 
    		nbr->capabilities = capabilities;  | 
    
    
    104  | 
     | 
     | 
    		nbr->addr = src;  | 
    
    
    105  | 
     | 
     | 
    	}  | 
    
    
    106  | 
     | 
     | 
     | 
    
    
    107  | 
     | 
     | 
    	nbr_fsm(nbr, NBR_EVT_PROBE_RCVD);  | 
    
    
    108  | 
     | 
     | 
     | 
    
    
    109  | 
     | 
     | 
    	if ((nbr->gen_id != gen_id) || (nbr->capabilities != capabilities)) { | 
    
    
    110  | 
     | 
     | 
    		if (!nbr->compat)  | 
    
    
    111  | 
     | 
     | 
    			nbr_fsm(nbr, NBR_EVT_1_WAY_RCVD);  | 
    
    
    112  | 
     | 
     | 
    		nbr->gen_id = gen_id;  | 
    
    
    113  | 
     | 
     | 
    		nbr->capabilities = capabilities;  | 
    
    
    114  | 
     | 
     | 
     | 
    
    
    115  | 
     | 
     | 
    		/* XXX handle nbr change! */  | 
    
    
    116  | 
     | 
     | 
    	}  | 
    
    
    117  | 
     | 
     | 
     | 
    
    
    118  | 
     | 
     | 
    	while (len >= sizeof(nbr_id)) { | 
    
    
    119  | 
     | 
     | 
    		memcpy(&nbr_id, buf, sizeof(nbr_id));  | 
    
    
    120  | 
     | 
     | 
    		if (nbr_id == iface->addr.s_addr) { | 
    
    
    121  | 
     | 
     | 
    			/* seen myself */  | 
    
    
    122  | 
     | 
     | 
    			if (nbr->state < NBR_STA_2_WAY)  | 
    
    
    123  | 
     | 
     | 
    			nbr_fsm(nbr, NBR_EVT_2_WAY_RCVD);  | 
    
    
    124  | 
     | 
     | 
    			break;  | 
    
    
    125  | 
     | 
     | 
    		}  | 
    
    
    126  | 
     | 
     | 
    		buf += sizeof(nbr_id);  | 
    
    
    127  | 
     | 
     | 
    		len -= sizeof(nbr_id);  | 
    
    
    128  | 
     | 
     | 
    	}  | 
    
    
    129  | 
     | 
     | 
     | 
    
    
    130  | 
     | 
     | 
    	if (len == 0) { | 
    
    
    131  | 
     | 
     | 
    		nbr_fsm(nbr, NBR_EVT_1_WAY_RCVD);  | 
    
    
    132  | 
     | 
     | 
    		return;  | 
    
    
    133  | 
     | 
     | 
    	}  | 
    
    
    134  | 
     | 
     | 
     | 
    
    
    135  | 
     | 
     | 
    	/* XXX len correct?? */  | 
    
    
    136  | 
     | 
     | 
     | 
    
    
    137  | 
     | 
     | 
    	return;  | 
    
    
    138  | 
     | 
     | 
    }  |