2 * The mrouted program is covered by the license in the accompanying file
3 * named "LICENSE". Use of the mrouted program represents acceptance of
4 * the terms and conditions listed in that file.
6 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
7 * Leland Stanford Junior University.
10 * ipip.c,v 3.8.4.6 1998/01/06 01:57:45 fenner Exp
17 static char rcsid[] = "@(#) $Id: \
18 ipip.c,v 3.8.4.6 1998/01/06 01:57:45 fenner Exp $";
25 int raw_socket; /* socket for raw network I/O */
28 *XXX For now, we just use the IGMP socket to send packets.
29 * This is legal in BSD, because the protocol # is not checked
30 * on raw sockets. The k_* interfaces need to gain a socket
31 * argument so that we can call them on the raw_socket also.
33 #define raw_socket igmp_socket
41 * Open and initialize the raw socket.
47 if ((raw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
48 log(LOG_ERR, errno, "Raw IP socket");
53 * Allocate and fill in static IP header for encapsulating on a tunnel.
61 ip = v->uv_encap_hdr = (struct ip *)malloc(sizeof(struct ip));
63 log(LOG_ERR, 0, "out of memory");
64 bzero(ip, sizeof(struct ip));
66 * Fields zeroed that aren't filled in later:
67 * - IP ID (let the kernel fill it in)
68 * - Offset (we don't send fragments)
69 * - Checksum (let the kernel fill it in)
72 ip->ip_hl = sizeof(struct ip) >> 2;
73 ip->ip_tos = 0xc0; /* Internet Control */
74 ip->ip_ttl = MAXTTL; /* applies to unicasts only */
75 ip->ip_p = IPPROTO_IPIP;
76 ip->ip_src.s_addr = v->uv_lcl_addr;
77 ip->ip_dst.s_addr = v->uv_rmt_addr;
81 * Call build_igmp() to build an IGMP message in the output packet buffer.
82 * Then fill in the fields of the IP packet that build_igmp() left for the
83 * kernel to fill in, and encapsulate the original packet with the
84 * pre-created ip header for this vif.
87 send_ipip(src, dst, type, code, group, datalen, v)
96 struct sockaddr_in sdst;
99 build_igmp(src, dst, type, code, group, datalen);
100 ip = (struct ip *)send_buf;
101 #ifndef RAW_OUTPUT_IS_RAW
102 ip->ip_len = htons(ip->ip_len);
104 ip->ip_id = htons(rawid++);
106 ip->ip_sum = inet_cksum((u_short *)ip, ip->ip_hl << 2);
108 ip = v->uv_encap_hdr;
109 ip->ip_len = 2 * MIN_IP_HEADER_LEN + IGMP_MINLEN + datalen;
110 #ifdef RAW_OUTPUT_IS_RAW
111 ip->ip_len = htons(ip->ip_len);
114 bzero(&sdst, sizeof(sdst));
115 sdst.sin_family = AF_INET;
117 sdst.sin_len = sizeof(sdst);
119 sdst.sin_addr = ip->ip_dst;
121 iov[0].iov_base = (caddr_t)v->uv_encap_hdr;
122 iov[0].iov_len = sizeof(struct ip);
123 iov[1].iov_base = (caddr_t)send_buf;
124 iov[1].iov_len = MIN_IP_HEADER_LEN + IGMP_MINLEN + datalen;
126 bzero(&msg, sizeof(msg));
127 msg.msg_name = (caddr_t)&sdst;
128 msg.msg_namelen = sizeof(sdst);
131 if (sendmsg(raw_socket, &msg, 0) < 0) {
132 if (errno == ENETDOWN)
135 log(LOG_WARNING, errno,
136 "sendmsg to %s on %s",
137 inet_fmt(sdst.sin_addr.s_addr, s1), inet_fmt(src, s2));
140 IF_DEBUG(DEBUG_PKT|igmp_debug_kind(type, code))
141 log(LOG_DEBUG, 0, "SENT %s from %-15s to %s encaped to %s",
142 igmp_packet_kind(type, code), src == INADDR_ANY ? "INADDR_ANY" :
143 inet_fmt(src, s1), inet_fmt(dst, s2),
144 inet_fmt(sdst.sin_addr.s_addr, s3));