Add PPPOE-SERVER functionality
[l2tpns.git] / icmp.c
1 // L2TPNS: icmp
2
3 #include <arpa/inet.h>
4 #include <netdb.h>
5 #include <netinet/in.h>
6 #include <asm/types.h>
7 #include <linux/ip.h>
8 #include <linux/icmp.h>
9 #include <netinet/icmp6.h>
10 #include <stdio.h>
11 #include <sys/socket.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <memory.h>
16
17 #include "l2tpns.h"
18 #include "pppoe.h"
19
20 static uint16_t _checksum(uint8_t *addr, int count);
21
22 struct ipv6_pseudo_hdr {
23 struct in6_addr src;
24 struct in6_addr dest;
25 uint32_t ulp_length;
26 uint32_t zero : 24;
27 uint32_t nexthdr : 8;
28 };
29
30 void host_unreachable(in_addr_t destination, uint16_t id, in_addr_t source, uint8_t *packet, int packet_len)
31 {
32 char buf[128] = {0};
33 struct iphdr *iph;
34 struct icmphdr *icmp;
35 int len = 0, on = 1, icmp_socket;
36 struct sockaddr_in whereto = {0};
37
38 if ((icmp_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
39 return;
40
41 setsockopt(icmp_socket, IPPROTO_IP, IP_HDRINCL, (char *)&on, sizeof(on));
42
43 whereto.sin_addr.s_addr = destination;
44 whereto.sin_family = AF_INET;
45
46 iph = (struct iphdr *)(buf);
47 len = sizeof(struct iphdr);
48 icmp = (struct icmphdr *)(buf + len);
49 len += sizeof(struct icmphdr);
50
51 /* ip header + first 8 bytes of payload */
52 if (packet_len > (sizeof(struct iphdr) + 8))
53 packet_len = sizeof(struct iphdr) + 8;
54
55 memcpy(buf + len, packet, packet_len);
56 len += packet_len;
57
58 iph->tos = 0;
59 iph->id = id;
60 iph->frag_off = 0;
61 iph->ttl = 30;
62 iph->check = 0;
63 iph->version = 4;
64 iph->ihl = 5;
65 iph->protocol = 1;
66 iph->check = 0;
67 iph->daddr = destination;
68 iph->saddr = source;
69
70 iph->tot_len = ntohs(len);
71
72 icmp->type = ICMP_DEST_UNREACH;
73 icmp->code = ICMP_HOST_UNREACH;
74 icmp->checksum = _checksum((uint8_t *) icmp, sizeof(struct icmphdr) + packet_len);
75
76 iph->check = _checksum((uint8_t *) iph, sizeof(struct iphdr));
77
78 sendto(icmp_socket, buf, len, 0, (struct sockaddr *)&whereto, sizeof(struct sockaddr));
79 close(icmp_socket);
80 }
81
82 static uint16_t _checksum(uint8_t *addr, int count)
83 {
84 register long sum = 0;
85
86 for (; count > 1; count -= 2)
87 {
88 sum += ntohs(*(uint32_t *) addr);
89 addr += 2;
90 }
91
92 if (count > 1) sum += *(unsigned char *)addr;
93
94 // take only 16 bits out of the 32 bit sum and add up the carries
95 while (sum >> 16)
96 sum = (sum & 0xFFFF) + (sum >> 16);
97
98 // one's complement the result
99 sum = ~sum;
100
101 return htons((uint16_t) sum);
102 }
103
104 void send_ipv6_ra(sessionidt s, tunnelidt t, struct in6_addr *ip)
105 {
106 struct nd_opt_prefix_info *pinfo;
107 struct ipv6_pseudo_hdr *phdr;
108 uint8_t b[MAXETHER + 20];
109 uint8_t c[MAXETHER + 20];
110 int l;
111 uint8_t *o;
112
113 LOG(3, s, t, "Sending IPv6 RA\n");
114
115 memset(b, 0, sizeof(b));
116 o = makeppp(b, sizeof(b), 0, 0, s, t, PPPIPV6, 0, 0, 0);
117
118 if (!o)
119 {
120 LOG(3, s, t, "failed to send IPv6 RA\n");
121 return;
122 }
123
124 *o = 0x60; // IPv6
125 *(o+1) = 0;
126 *(o+5) = 48; // Length of payload (not header)
127 *(o+6) = 58; // icmp6 is next
128 *(o+7) = 255; // Hop limit
129 memset(o+8, 0, 16); // source = FE80::1
130 *(o+8) = 0xFE;
131 *(o+9) = 0x80;
132 *(o+23) = 1;
133 if (ip != NULL)
134 {
135 memcpy(o+24, ip, 16); // dest = ip
136 }
137 else
138 {
139 // FF02::1 - all hosts
140 *(o+24) = 0xFF;
141 *(o+25) = 2;
142 *(o+39) = 1;
143 }
144 *(o+40) = 134; // RA message
145 *(o+41) = 0; // Code
146 *(o+42) = *(o+43) = 0; // Checksum
147 *(o+44) = 64; // Hop count
148 *(o+45) = 0; // Flags
149 *(o+46) = *(o+47) = 255; // Lifetime
150 *(uint32_t *)(o+48) = 0; // Reachable time
151 *(uint32_t *)(o+52) = 0; // Retrans timer
152 pinfo = (struct nd_opt_prefix_info *)(o+56);
153 pinfo->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
154 pinfo->nd_opt_pi_len = 4;
155 pinfo->nd_opt_pi_prefix_len = 64; // prefix length
156 pinfo->nd_opt_pi_flags_reserved = ND_OPT_PI_FLAG_ONLINK;
157 pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
158 pinfo->nd_opt_pi_valid_time = htonl(2592000);
159 pinfo->nd_opt_pi_preferred_time = htonl(604800);
160 pinfo->nd_opt_pi_reserved2 = 0;
161 pinfo->nd_opt_pi_prefix = config->ipv6_prefix;
162 l = sizeof(*pinfo) + 56;
163
164 memset(c, 0, sizeof(c));
165 phdr = (struct ipv6_pseudo_hdr *) c;
166 memcpy(&phdr->src, o+8, 16);
167 memcpy(&phdr->dest, o+24, 16);
168 phdr->ulp_length = htonl(l - 40);
169 phdr->nexthdr = IPPROTO_ICMPV6;
170
171 memcpy(c + sizeof(*phdr), o + 40, l - 40);
172
173 // Checksum is over the icmp6 payload plus the pseudo header
174 *(uint16_t *)(o+42) = _checksum(c, l - 40 + sizeof(*phdr));
175
176 tunnelsend(b, l + (o-b), t); // send it...
177 return;
178 }