1 /* $OpenBSD: prune.c,v 1.2 2009/03/14 15:32:55 michele 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 <netinet/in_systm.h> 23 #include <netinet/ip.h> 24 #include <arpa/inet.h> 25 26 #include <stdlib.h> 27 #include <strings.h> 28 29 #include "igmp.h" 30 #include "dvmrpd.h" 31 #include "dvmrp.h" 32 #include "log.h" 33 #include "dvmrpe.h" 34 35 /* DVMRP prune packet handling */ 36 int 37 send_prune(struct nbr *nbr, struct prune *p) 38 { 39 struct sockaddr_in dst; 40 struct buf *buf; 41 struct dvmrp_hdr *dvmrp_hdr; 42 struct prune_hdr prune; 43 int ret = 0; 44 45 log_debug("send_prune: interface %s nbr %s", nbr->iface->name, 46 inet_ntoa(nbr->addr)); 47 48 if (nbr->iface->passive) 49 return (0); 50 51 bzero(&prune, sizeof(prune)); 52 53 dst.sin_family = AF_INET; 54 dst.sin_len = sizeof(struct sockaddr_in); 55 dst.sin_addr = nbr->addr; 56 57 if ((buf = buf_open(nbr->iface->mtu - sizeof(struct ip))) == NULL) 58 fatal("send_prune"); 59 60 /* DVMRP header */ 61 if (gen_dvmrp_hdr(buf, nbr->iface, DVMRP_CODE_PRUNE)) 62 goto fail; 63 64 prune.src_host_addr = p->origin.s_addr; 65 prune.group_addr = p->group.s_addr; 66 67 /* XXX */ 68 prune.lifetime = htonl(MAX_PRUNE_LIFETIME); 69 prune.src_netmask = p->netmask.s_addr; 70 71 buf_add(buf, &prune, sizeof(prune)); 72 73 /* update chksum */ 74 dvmrp_hdr = buf_seek(buf, 0, sizeof(dvmrp_hdr)); 75 dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); 76 77 ret = send_packet(nbr->iface, buf->buf, buf->wpos, &dst); 78 buf_free(buf); 79 80 return (ret); 81 fail: 82 log_warn("send_prune"); 83 buf_free(buf); 84 return (-1); 85 } 86 87 void 88 recv_prune(struct nbr *nbr, char *buf, u_int16_t len) 89 { 90 struct prune p; 91 struct prune_hdr *prune; 92 93 log_debug("recv_prune: neighbor ID %s", inet_ntoa(nbr->id)); 94 95 if (len < PRUNE_MIN_LEN) { 96 log_debug("recv_prune: packet malformed from %s", 97 inet_ntoa(nbr->id)); 98 return; 99 } 100 101 bzero(&p, sizeof(p)); 102 103 prune = (struct prune_hdr *)buf; 104 105 p.origin.s_addr = prune->src_host_addr; 106 p.group.s_addr = prune->group_addr; 107 p.lifetime = prune->lifetime; 108 109 if (len >= sizeof(*prune)) 110 p.netmask.s_addr = prune->src_netmask; 111 112 p.ifindex = nbr->iface->ifindex; 113 114 dvmrpe_imsg_compose_rde(IMSG_RECV_PRUNE, nbr->peerid, 0, &p, sizeof(p)); 115 116 return; 117 } 118