Adding the possibility to listening multiple IP L2TP tunnels.
[l2tpns.git] / pppoe.c
1 /*
2 * Fernando ALVES 2013
3 * Add functionality "server pppoe" to l2tpns.
4 * inspiration pppoe.c of accel-ppp
5 * GPL licenced
6 */
7
8 #include <unistd.h>
9 #include <assert.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <pthread.h>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <sys/ioctl.h>
19 #include <net/if.h>
20 #include <net/ethernet.h>
21 #include <netpacket/packet.h>
22 #include <arpa/inet.h>
23 #include <linux/if_pppox.h>
24
25 #include "l2tpns.h"
26 #include "cluster.h"
27 #include "constants.h"
28 #include "md5.h"
29 #include "util.h"
30
31 int pppoediscfd = -1;
32 int pppoesessfd = -1;
33
34 static uint8_t bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
35
36 /* PPPoE codes */
37 #define CODE_PADI 0x09
38 #define CODE_PADO 0x07
39 #define CODE_PADR 0x19
40 #define CODE_PADS 0x65
41 #define CODE_PADT 0xA7
42 #define CODE_SESS 0x00
43
44 /* PPPoE Tags */
45 #define TAG_END_OF_LIST 0x0000
46 #define TAG_SERVICE_NAME 0x0101
47 #define TAG_AC_NAME 0x0102
48 #define TAG_HOST_UNIQ 0x0103
49 #define TAG_AC_COOKIE 0x0104
50 #define TAG_VENDOR_SPECIFIC 0x0105
51 #define TAG_RELAY_SESSION_ID 0x0110
52 #define TAG_SERVICE_NAME_ERROR 0x0201
53 #define TAG_AC_SYSTEM_ERROR 0x0202
54 #define TAG_GENERIC_ERROR 0x0203
55
56 static char *code_pad[] = {
57 "PADI",
58 "PADO",
59 "PADR",
60 "PADS",
61 "PADT",
62 "SESS",
63 NULL
64 };
65
66 enum
67 {
68 INDEX_PADI = 0,
69 INDEX_PADO,
70 INDEX_PADR,
71 INDEX_PADS,
72 INDEX_PADT,
73 INDEX_SESS
74 };
75
76 // set up pppoe discovery socket
77 static void init_pppoe_disc(void)
78 {
79 int on = 1;
80 struct ifreq ifr;
81 struct sockaddr_ll sa;
82
83 memset(&ifr, 0, sizeof(ifr));
84 memset(&sa, 0, sizeof(sa));
85
86 pppoediscfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PPP_DISC));
87 if (pppoediscfd < 0)
88 {
89 LOG(0, 0, 0, "Error pppoe: socket: %s\n", strerror(errno));
90 exit(1);
91 }
92
93 fcntl(pppoediscfd, F_SETFD, fcntl(pppoediscfd, F_GETFD) | FD_CLOEXEC);
94
95 if (setsockopt(pppoediscfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)))
96 {
97 LOG(0, 0, 0, "Error pppoe: setsockopt(SO_BROADCAST): %s\n", strerror(errno));
98 exit(1);
99 }
100
101 assert(strlen(ifr.ifr_name) < sizeof(config->pppoe_if_to_bind) - 1);
102 if (*config->pppoe_if_to_bind)
103 strncpy(ifr.ifr_name, config->pppoe_if_to_bind, IFNAMSIZ);
104
105 if (ioctl(pppoediscfd, SIOCGIFHWADDR, &ifr))
106 {
107 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFHWADDR): %s\n", strerror(errno));
108 exit(1);
109 }
110
111 if ((ifr.ifr_hwaddr.sa_data[0] & 1) != 0)
112 {
113 LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config->pppoe_if_to_bind);
114 exit(1);
115 }
116
117 memcpy(config->pppoe_hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
118
119 if (ioctl(pppoediscfd, SIOCGIFMTU, &ifr))
120 {
121 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFMTU): %s\n", strerror(errno));
122 exit(1);
123 }
124
125 if (ifr.ifr_mtu < ETH_DATA_LEN)
126 LOG(0, 0, 0, "Error pppoe: interface %s has MTU of %i, should be %i\n", config->pppoe_if_to_bind, ifr.ifr_mtu, ETH_DATA_LEN);
127
128 if (ioctl(pppoediscfd, SIOCGIFINDEX, &ifr))
129 {
130 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFINDEX): %s\n", strerror(errno));
131 exit(1);
132 }
133
134 memset(&sa, 0, sizeof(sa));
135 sa.sll_family = AF_PACKET;
136 sa.sll_protocol = htons(ETH_P_PPP_DISC);
137 sa.sll_ifindex = ifr.ifr_ifindex;
138
139 if (bind(pppoediscfd, (struct sockaddr *)&sa, sizeof(sa)))
140 {
141 LOG(0, 0, 0, "Error pppoe: bind: %s\n", strerror(errno));
142 exit(1);
143 }
144
145 if (fcntl(pppoediscfd, F_SETFL, O_NONBLOCK))
146 {
147 LOG(0, 0, 0, "Error pppoe: failed to set nonblocking mode: %s\n", strerror(errno));
148 exit(1);
149 }
150
151 }
152
153 // set up pppoe session socket
154 static void init_pppoe_sess(void)
155 {
156 int on = 1;
157 struct ifreq ifr;
158 struct sockaddr_ll sa;
159
160 memset(&ifr, 0, sizeof(ifr));
161 memset(&sa, 0, sizeof(sa));
162
163 pppoesessfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PPP_SES));
164 if (pppoesessfd < 0)
165 {
166 LOG(0, 0, 0, "Error pppoe: socket: %s\n", strerror(errno));
167 exit(1);
168 }
169
170 fcntl(pppoesessfd, F_SETFD, fcntl(pppoesessfd, F_GETFD) | FD_CLOEXEC);
171
172 if (setsockopt(pppoesessfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)))
173 {
174 LOG(0, 0, 0, "Error pppoe: setsockopt(SO_BROADCAST): %s\n", strerror(errno));
175 exit(1);
176 }
177
178 assert(strlen(ifr.ifr_name) < sizeof(config->pppoe_if_to_bind) - 1);
179 if (*config->pppoe_if_to_bind)
180 strncpy(ifr.ifr_name, config->pppoe_if_to_bind, IFNAMSIZ);
181
182 if (ioctl(pppoesessfd, SIOCGIFHWADDR, &ifr))
183 {
184 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFHWADDR): %s\n", strerror(errno));
185 exit(1);
186 }
187
188 if ((ifr.ifr_hwaddr.sa_data[0] & 1) != 0)
189 {
190 LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config->pppoe_if_to_bind);
191 exit(1);
192 }
193
194 memcpy(config->pppoe_hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
195
196 if (ioctl(pppoesessfd, SIOCGIFMTU, &ifr))
197 {
198 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFMTU): %s\n", strerror(errno));
199 exit(1);
200 }
201
202 if (ifr.ifr_mtu < ETH_DATA_LEN)
203 LOG(0, 0, 0, "Error pppoe: interface %s has MTU of %i, should be %i\n", config->pppoe_if_to_bind, ifr.ifr_mtu, ETH_DATA_LEN);
204
205 if (ioctl(pppoesessfd, SIOCGIFINDEX, &ifr))
206 {
207 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFINDEX): %s\n", strerror(errno));
208 exit(1);
209 }
210
211 memset(&sa, 0, sizeof(sa));
212 sa.sll_family = AF_PACKET;
213 sa.sll_protocol = htons(ETH_P_PPP_SES);
214 sa.sll_ifindex = ifr.ifr_ifindex;
215
216 if (bind(pppoesessfd, (struct sockaddr *)&sa, sizeof(sa)))
217 {
218 LOG(0, 0, 0, "Error pppoe: bind: %s\n", strerror(errno));
219 exit(1);
220 }
221
222 if (fcntl(pppoesessfd, F_SETFL, O_NONBLOCK))
223 {
224 LOG(0, 0, 0, "Error pppoe: failed to set nonblocking mode: %s\n", strerror(errno));
225 exit(1);
226 }
227 }
228
229 // set up pppoe discovery/session socket
230 void init_pppoe(void)
231 {
232 tunnelidt t = TUNNEL_ID_PPPOE;
233
234 init_pppoe_disc();
235 init_pppoe_sess();
236
237 // Reserve the a pseudo tunnel for pppoe server
238 if (t > config->cluster_highest_tunnelid)
239 config->cluster_highest_tunnelid = t;
240
241 memset(&tunnel[t], 0, sizeof(tunnel[t]));
242 tunnel[t].state = TUNNELOPEN;
243 STAT(tunnel_created);
244 }
245
246 char * get_string_codepad(uint8_t codepad)
247 {
248 char * ptrch = NULL;
249 switch(codepad)
250 {
251 case CODE_PADI:
252 ptrch = code_pad[INDEX_PADI];
253 break;
254
255 case CODE_PADO:
256 ptrch = code_pad[INDEX_PADO];
257 break;
258
259 case CODE_PADR:
260 ptrch = code_pad[INDEX_PADR];
261 break;
262
263 case CODE_PADS:
264 ptrch = code_pad[INDEX_PADS];
265 break;
266
267 case CODE_PADT:
268 ptrch = code_pad[INDEX_PADT];
269 break;
270
271 case CODE_SESS:
272 ptrch = code_pad[INDEX_SESS];
273 break;
274 }
275
276 return ptrch;
277 }
278
279 static uint8_t * setup_header(uint8_t *pack, const uint8_t *src, const uint8_t *dst, int code, uint16_t sid, uint16_t h_proto)
280 {
281 uint8_t * p;
282
283 // 14 bytes ethernet Header + 6 bytes header pppoe
284 struct ethhdr *ethhdr = (struct ethhdr *)pack;
285 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
286
287 memcpy(ethhdr->h_source, src, ETH_ALEN);
288 memcpy(ethhdr->h_dest, dst, ETH_ALEN);
289 ethhdr->h_proto = htons(h_proto);
290
291 hdr->ver = 1;
292 hdr->type = 1;
293 hdr->code = code;
294 hdr->sid = htons(sid);
295 hdr->length = 0;
296
297 p = (uint8_t *)(pack + ETH_HLEN + sizeof(*hdr));
298
299 return p;
300 }
301
302 static void add_tag(uint8_t *pack, int type, const uint8_t *data, int len)
303 {
304 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
305 struct pppoe_tag *tag = (struct pppoe_tag *)(pack + ETH_HLEN + sizeof(*hdr) + ntohs(hdr->length));
306
307 tag->tag_type = htons(type);
308 tag->tag_len = htons(len);
309 memcpy(tag->tag_data, data, len);
310
311 hdr->length = htons(ntohs(hdr->length) + sizeof(*tag) + len);
312 }
313
314 static void add_tag2(uint8_t *pack, const struct pppoe_tag *t)
315 {
316 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
317 struct pppoe_tag *tag = (struct pppoe_tag *)(pack + ETH_HLEN + sizeof(*hdr) + ntohs(hdr->length));
318
319 memcpy(tag, t, sizeof(*t) + ntohs(t->tag_len));
320
321 hdr->length = htons(ntohs(hdr->length) + sizeof(*tag) + ntohs(t->tag_len));
322 }
323
324 static void pppoe_disc_send(const uint8_t *pack)
325 {
326 struct ethhdr *ethhdr = (struct ethhdr *)pack;
327 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
328 int n, s;
329
330 s = ETH_HLEN + sizeof(*hdr) + ntohs(hdr->length);
331
332 LOG(3, 0, 0, "SENT pppoe_disc: Code %s to %s\n", get_string_codepad(hdr->code), fmtMacAddr(ethhdr->h_dest));
333 LOG_HEX(5, "pppoe_disc_send", pack, s);
334
335 n = write(pppoediscfd, pack, s);
336 if (n < 0 )
337 LOG(0, 0, 0, "pppoe: write: %s\n", strerror(errno));
338 else if (n != s) {
339 LOG(0, 0, 0, "pppoe: short write %i/%i\n", n,s);
340 }
341 }
342
343 void pppoe_sess_send(const uint8_t *pack, uint16_t l, tunnelidt t)
344 {
345 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
346 int n;
347 uint16_t sizeppp;
348 sessionidt s;
349
350 if (t != TUNNEL_ID_PPPOE)
351 {
352 LOG(3, 0, t, "ERROR pppoe_sess_send: Tunnel %d is not a tunnel pppoe\n", t);
353 return;
354 }
355
356 s = ntohs(hdr->sid);
357 if (session[s].tunnel != t)
358 {
359 LOG(3, s, t, "ERROR pppoe_sess_send: Session is not a session pppoe\n");
360 return;
361 }
362
363 if (l < (ETH_HLEN + sizeof(*hdr) + 3))
364 {
365 LOG(0, s, t, "ERROR pppoe_sess_send: packet too small for pppoe sent (size=%d)\n", l);
366 return;
367 }
368
369 // recalculate the ppp frame length
370 sizeppp = l - (ETH_HLEN + sizeof(*hdr));
371 hdr->length = htons(sizeppp);
372
373 LOG_HEX(5, "pppoe_sess_send", pack, l);
374
375 n = write(pppoesessfd, pack, l);
376 if (n < 0 )
377 LOG(0, s, t, "pppoe_sess_send: write: %s\n", strerror(errno));
378 else if (n != l)
379 LOG(0, s, t, "pppoe_sess_send: short write %i/%i\n", n,l);
380 }
381
382 static void pppoe_send_err(const uint8_t *addr, const struct pppoe_tag *host_uniq, const struct pppoe_tag *relay_sid, int code, int tag_type)
383 {
384 uint8_t pack[ETHER_MAX_LEN];
385
386 setup_header(pack, config->pppoe_hwaddr, addr, code, 0, ETH_P_PPP_DISC);
387
388 add_tag(pack, TAG_AC_NAME, (uint8_t *)config->pppoe_ac_name, strlen(config->pppoe_ac_name));
389 add_tag(pack, tag_type, NULL, 0);
390
391 if (host_uniq)
392 add_tag2(pack, host_uniq);
393
394 if (relay_sid)
395 add_tag2(pack, relay_sid);
396
397 pppoe_disc_send(pack);
398 }
399
400 // generate cookie
401 static void pppoe_gen_cookie(const uint8_t *serv_hwaddr, const uint8_t *client_hwaddr, uint8_t *out)
402 {
403 MD5_CTX ctx;
404
405 MD5_Init(&ctx);
406 MD5_Update(&ctx, config->l2tp_secret, strlen(config->l2tp_secret));
407 MD5_Update(&ctx, (void *) serv_hwaddr, ETH_ALEN);
408 MD5_Update(&ctx, (void *) client_hwaddr, ETH_ALEN);
409 MD5_Final(out, &ctx);
410 }
411
412 // check cookie
413 static int pppoe_check_cookie(const uint8_t *serv_hwaddr, const uint8_t *client_hwaddr, uint8_t *cookie)
414 {
415 hasht hash;
416
417 pppoe_gen_cookie(serv_hwaddr, client_hwaddr, hash);
418
419 return memcmp(hash, cookie, 16);
420 }
421
422 static void pppoe_send_PADO(const uint8_t *addr, const struct pppoe_tag *host_uniq, const struct pppoe_tag *relay_sid, const struct pppoe_tag *service_name)
423 {
424 uint8_t pack[ETHER_MAX_LEN];
425 hasht hash;
426
427 setup_header(pack, config->pppoe_hwaddr, addr, CODE_PADO, 0, ETH_P_PPP_DISC);
428
429 add_tag(pack, TAG_AC_NAME, (uint8_t *)config->pppoe_ac_name, strlen(config->pppoe_ac_name));
430
431 if (service_name)
432 add_tag2(pack, service_name);
433
434 pppoe_gen_cookie(config->pppoe_hwaddr, addr, hash);
435 add_tag(pack, TAG_AC_COOKIE, hash, 16);
436
437 if (host_uniq)
438 add_tag2(pack, host_uniq);
439
440 if (relay_sid)
441 add_tag2(pack, relay_sid);
442
443 pppoe_disc_send(pack);
444 }
445
446 static void pppoe_send_PADS(uint16_t sid, const uint8_t *addr, const struct pppoe_tag *host_uniq, const struct pppoe_tag *relay_sid, const struct pppoe_tag *service_name)
447 {
448 uint8_t pack[ETHER_MAX_LEN];
449
450 setup_header(pack, config->pppoe_hwaddr, addr, CODE_PADS, sid, ETH_P_PPP_DISC);
451
452 add_tag(pack, TAG_AC_NAME, (uint8_t *)config->pppoe_ac_name, strlen(config->pppoe_ac_name));
453
454 add_tag2(pack, service_name);
455
456 if (host_uniq)
457 add_tag2(pack, host_uniq);
458
459 if (relay_sid)
460 add_tag2(pack, relay_sid);
461
462 pppoe_disc_send(pack);
463 }
464
465 static void pppoe_send_PADT(uint16_t sid)
466 {
467 uint8_t pack[ETHER_MAX_LEN];
468
469 setup_header(pack, config->pppoe_hwaddr, session[sid].src_hwaddr, CODE_PADT, sid, ETH_P_PPP_DISC);
470
471 add_tag(pack, TAG_AC_NAME, (uint8_t *)config->pppoe_ac_name, strlen(config->pppoe_ac_name));
472
473 LOG(3, sid, session[sid].tunnel, "pppoe: Sent PADT\n");
474
475 pppoe_disc_send(pack);
476 }
477
478 void pppoe_shutdown_session(sessionidt s)
479 {
480
481 if (session[s].tunnel != TUNNEL_ID_PPPOE)
482 {
483 LOG(3, s, session[s].tunnel, "ERROR pppoe_shutdown_session: Session is not a session pppoe\n");
484 return;
485 }
486
487 pppoe_send_PADT(s);
488 }
489
490 static void pppoe_recv_PADI(uint8_t *pack, int size)
491 {
492 struct ethhdr *ethhdr = (struct ethhdr *)pack;
493 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
494 struct pppoe_tag *tag;
495 struct pppoe_tag *host_uniq_tag = NULL;
496 struct pppoe_tag *relay_sid_tag = NULL;
497 struct pppoe_tag *service_name_tag = NULL;
498 int n, service_match = 0;
499 int len;
500
501 if (hdr->sid)
502 return;
503
504 len = ntohs(hdr->length);
505 for (n = 0; n < len; n += sizeof(*tag) + ntohs(tag->tag_len)) {
506 tag = (struct pppoe_tag *)(pack + ETH_HLEN + sizeof(*hdr) + n);
507 if (n + sizeof(*tag) + ntohs(tag->tag_len) > len)
508 return;
509 switch (ntohs(tag->tag_type)) {
510 case TAG_END_OF_LIST:
511 break;
512 case TAG_SERVICE_NAME:
513 if (*config->pppoe_service_name && tag->tag_len)
514 {
515 if (ntohs(tag->tag_len) != strlen(config->pppoe_service_name))
516 break;
517 if (memcmp(tag->tag_data, config->pppoe_service_name, ntohs(tag->tag_len)))
518 break;
519 service_match = 1;
520 }
521 else
522 {
523 service_name_tag = tag;
524 service_match = 1;
525 }
526 break;
527 case TAG_HOST_UNIQ:
528 host_uniq_tag = tag;
529 break;
530 case TAG_RELAY_SESSION_ID:
531 relay_sid_tag = tag;
532 break;
533 }
534 }
535
536 if (!service_match)
537 {
538 LOG(3, 0, 0, "pppoe: discarding PADI packet (Service-Name mismatch)\n");
539 return;
540 }
541
542 pppoe_send_PADO(ethhdr->h_source, host_uniq_tag, relay_sid_tag, service_name_tag);
543 }
544
545 static void pppoe_recv_PADR(uint8_t *pack, int size)
546 {
547 struct ethhdr *ethhdr = (struct ethhdr *)pack;
548 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
549 struct pppoe_tag *tag;
550 struct pppoe_tag *host_uniq_tag = NULL;
551 struct pppoe_tag *relay_sid_tag = NULL;
552 struct pppoe_tag *ac_cookie_tag = NULL;
553 struct pppoe_tag *service_name_tag = NULL;
554 int n, service_match = 0;
555 uint16_t sid;
556
557 if (!memcmp(ethhdr->h_dest, bc_addr, ETH_ALEN))
558 {
559 LOG(1, 0, 0, "Rcv pppoe: discard PADR (destination address is broadcast)\n");
560 return;
561 }
562
563 if (hdr->sid)
564 {
565 LOG(1, 0, 0, "Rcv pppoe: discarding PADR packet (sid is not zero)\n");
566 return;
567 }
568
569 for (n = 0; n < ntohs(hdr->length); n += sizeof(*tag) + ntohs(tag->tag_len))
570 {
571 tag = (struct pppoe_tag *)(pack + ETH_HLEN + sizeof(*hdr) + n);
572 switch (ntohs(tag->tag_type))
573 {
574 case TAG_END_OF_LIST:
575 break;
576 case TAG_SERVICE_NAME:
577 service_name_tag = tag;
578 if (tag->tag_len == 0)
579 service_match = 1;
580 else if (*config->pppoe_service_name)
581 {
582 if (ntohs(tag->tag_len) != strlen(config->pppoe_service_name))
583 break;
584 if (memcmp(tag->tag_data, config->pppoe_service_name, ntohs(tag->tag_len)))
585 break;
586 service_match = 1;
587 }
588 else
589 {
590 service_match = 1;
591 }
592 break;
593 case TAG_HOST_UNIQ:
594 host_uniq_tag = tag;
595 break;
596 case TAG_AC_COOKIE:
597 ac_cookie_tag = tag;
598 break;
599 case TAG_RELAY_SESSION_ID:
600 relay_sid_tag = tag;
601 break;
602 }
603 }
604
605 if (!service_match)
606 {
607 LOG(3, 0, 0, "pppoe: Service-Name mismatch\n");
608 pppoe_send_err(ethhdr->h_source, host_uniq_tag, relay_sid_tag, CODE_PADS, TAG_SERVICE_NAME_ERROR);
609 return;
610 }
611
612 if (!ac_cookie_tag)
613 {
614 LOG(3, 0, 0, "pppoe: discard PADR packet (no AC-Cookie tag present)\n");
615 return;
616 }
617
618 if (ntohs(ac_cookie_tag->tag_len) != 16)
619 {
620 LOG(3, 0, 0, "pppoe: discard PADR packet (incorrect AC-Cookie tag length)\n");
621 return;
622 }
623
624 if (pppoe_check_cookie(ethhdr->h_dest, ethhdr->h_source, (uint8_t *) ac_cookie_tag->tag_data))
625 {
626 LOG(3, 0, 0, "pppoe: discard PADR packet (incorrect AC-Cookie)\n");
627 return;
628 }
629
630 sid = sessionfree;
631 sessionfree = session[sid].next;
632 memset(&session[sid], 0, sizeof(session[0]));
633
634 if (sid > config->cluster_highest_sessionid)
635 config->cluster_highest_sessionid = sid;
636
637 session[sid].opened = time_now;
638 session[sid].tunnel = TUNNEL_ID_PPPOE;
639 session[sid].last_packet = session[sid].last_data = time_now;
640
641 //strncpy(session[sid].called, called, sizeof(session[sid].called) - 1);
642 //strncpy(session[sid].calling, calling, sizeof(session[sid].calling) - 1);
643
644 session[sid].ppp.phase = Establish;
645 session[sid].ppp.lcp = Starting;
646
647 session[sid].magic = time_now; // set magic number
648 session[sid].mru = PPPoE_MRU; // default
649
650 // start LCP
651 sess_local[sid].lcp_authtype = config->radius_authprefer;
652 sess_local[sid].ppp_mru = MRU;
653
654 // Set multilink options before sending initial LCP packet
655 sess_local[sid].mp_mrru = 1614;
656 sess_local[sid].mp_epdis = ntohl(config->iftun_address ? config->iftun_address : my_address);
657
658 memcpy(session[sid].src_hwaddr, ethhdr->h_source, ETH_ALEN);
659 pppoe_send_PADS(sid, ethhdr->h_source, host_uniq_tag, relay_sid_tag, service_name_tag);
660
661 sendlcp(sid, session[sid].tunnel);
662 change_state(sid, lcp, RequestSent);
663
664 }
665
666 static void pppoe_recv_PADT(uint8_t *pack)
667 {
668 struct ethhdr *ethhdr = (struct ethhdr *)pack;
669 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
670
671 if (!memcmp(ethhdr->h_dest, bc_addr, ETH_ALEN))
672 {
673 LOG(3, 0, 0, "pppoe: discard PADT (destination address is broadcast)\n");
674 return;
675 }
676
677 if (hdr->sid)
678 {
679 if ((hdr->sid < MAXSESSION) && (session[hdr->sid].tunnel == TUNNEL_ID_PPPOE))
680 sessionshutdown(hdr->sid, "Client shutdown", CDN_ADMIN_DISC, 0);
681 }
682 }
683
684 // fill in a PPPOE message with a PPP frame,
685 // returns start of PPP frame
686 uint8_t *pppoe_makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tunnelidt t,
687 uint16_t mtype, uint8_t prio, bundleidt bid, uint8_t mp_bits)
688 {
689 uint16_t type = mtype;
690 uint8_t *start = b;
691 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(b + ETH_HLEN);
692
693 if (t != TUNNEL_ID_PPPOE)
694 return NULL;
695
696 if (size < 28) // Need more space than this!!
697 {
698 LOG(0, s, t, "pppoe_makeppp buffer too small for pppoe header (size=%d)\n", size);
699 return NULL;
700 }
701
702 // 14 bytes ethernet Header + 6 bytes header pppoe
703 b = setup_header(b, config->pppoe_hwaddr, session[s].src_hwaddr, CODE_SESS, s, ETH_P_PPP_SES);
704
705 // Check whether this session is part of multilink
706 if (bid)
707 {
708 if (bundle[bid].num_of_links > 1)
709 type = PPPMP; // Change PPP message type to the PPPMP
710 else
711 bid = 0;
712 }
713
714 *(uint16_t *) b = htons(type);
715 b += 2;
716 hdr->length += 2;
717
718 if (bid)
719 {
720 // Set the sequence number and (B)egin (E)nd flags
721 if (session[s].mssf)
722 {
723 // Set the multilink bits
724 uint16_t bits_send = mp_bits;
725 *(uint16_t *) b = htons((bundle[bid].seq_num_t & 0x0FFF)|bits_send);
726 b += 2;
727 hdr->length += 2;
728 }
729 else
730 {
731 *(uint32_t *) b = htonl(bundle[bid].seq_num_t);
732 // Set the multilink bits
733 *b = mp_bits;
734 b += 4;
735 hdr->length += 4;
736 }
737
738 bundle[bid].seq_num_t++;
739
740 // Add the message type if this fragment has the begin bit set
741 if (mp_bits & MP_BEGIN)
742 {
743 //*b++ = mtype; // The next two lines are instead of this
744 *(uint16_t *) b = htons(mtype); // Message type
745 b += 2;
746 hdr->length += 2;
747 }
748 }
749
750 if ((b - start) + l > size)
751 {
752 LOG(0, s, t, "pppoe_makeppp would overflow buffer (size=%d, header+payload=%td)\n", size, (b - start) + l);
753 return NULL;
754 }
755
756 // Copy the payload
757 if (p && l)
758 {
759 memcpy(b, p, l);
760 hdr->length += l;
761 }
762
763 return b;
764 }
765
766 // fill in a PPPOE message with a PPP frame,
767 // returns start of PPP frame
768 //(note: THIS ROUTINE WRITES TO p[-28]).
769 uint8_t *opt_pppoe_makeppp(uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype, uint8_t prio, bundleidt bid, uint8_t mp_bits)
770 {
771 uint16_t type = mtype;
772 uint16_t hdrlen = l;
773 uint8_t *b = p;
774 struct pppoe_hdr *hdr;
775
776 if (t != TUNNEL_ID_PPPOE)
777 return NULL;
778
779 // Check whether this session is part of multilink
780 if (bid)
781 {
782 if (bundle[bid].num_of_links > 1)
783 type = PPPMP; // Change PPP message type to the PPPMP
784 else
785 bid = 0;
786 }
787
788 if (bid)
789 {
790 // Add the message type if this fragment has the begin bit set
791 if (mp_bits & MP_BEGIN)
792 {
793 b -= 2;
794 *(uint16_t *) b = htons(mtype); // Message type
795 }
796
797 // Set the sequence number and (B)egin (E)nd flags
798 if (session[s].mssf)
799 {
800 // Set the multilink bits
801 uint16_t bits_send = mp_bits;
802 b -= 2;
803 *(uint16_t *) b = htons((bundle[bid].seq_num_t & 0x0FFF)|bits_send);
804 }
805 else
806 {
807 b -= 4;
808 *(uint32_t *) b = htonl(bundle[bid].seq_num_t);
809 // Set the multilink bits
810 *b = mp_bits;
811 }
812
813 bundle[bid].seq_num_t++;
814 }
815
816 b -= 2;
817 *(uint16_t *) b = htons(type);
818
819 // Size ppp packet
820 hdrlen += (p - b);
821
822 // 14 bytes ethernet Header + 6 bytes header pppoe
823 b -= (ETH_HLEN + sizeof(*hdr));
824 setup_header(b, config->pppoe_hwaddr, session[s].src_hwaddr, CODE_SESS, s, ETH_P_PPP_SES);
825 hdr = (struct pppoe_hdr *)(b + ETH_HLEN);
826 // Store length on header pppoe
827 hdr->length = hdrlen;
828
829 return b;
830 }
831
832 // pppoe discovery recv data
833 void process_pppoe_disc(uint8_t *pack, int size)
834 {
835 struct ethhdr *ethhdr = (struct ethhdr *)pack;
836 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
837
838 LOG(3, 0, 0, "RCV pppoe_disc: Code %s from %s\n", get_string_codepad(hdr->code), fmtMacAddr(ethhdr->h_source));
839 LOG_HEX(5, "PPPOE Disc", pack, size);
840
841 if (!config->cluster_iam_master)
842 {
843 if (hdr->code == CODE_PADI)
844 return; // Discard because the PADI is received by all (PADI is a broadcast diffusion)
845
846 master_forward_pppoe_packet(pack, size, hdr->code);
847 return;
848 }
849
850 if (size < (ETH_HLEN + sizeof(*hdr)))
851 {
852 LOG(1, 0, 0, "Error pppoe_disc: short packet received (%i)\n", size);
853 return;
854 }
855
856 if (memcmp(ethhdr->h_dest, bc_addr, ETH_ALEN) && memcmp(ethhdr->h_dest, config->pppoe_hwaddr, ETH_ALEN))
857 {
858 LOG(1, 0, 0, "Error pppoe_disc: h_dest != bc_addr and h_dest != config->pppoe_hwaddr\n");
859 return;
860 }
861
862 if (!memcmp(ethhdr->h_source, bc_addr, ETH_ALEN))
863 {
864 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (source address is broadcast)\n");
865 return;
866 }
867
868 if ((ethhdr->h_source[0] & 1) != 0)
869 {
870 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (host address is not unicast)\n");
871 return;
872 }
873
874 if (size < ETH_HLEN + sizeof(*hdr) + ntohs(hdr->length))
875 {
876 LOG(1, 0, 0, "Error pppoe_disc: short packet received\n");
877 return;
878 }
879
880 if (hdr->ver != 1)
881 {
882 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (unsupported version %i)\n", hdr->ver);
883 return;
884 }
885
886 if (hdr->type != 1)
887 {
888 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (unsupported type %i)\n", hdr->type);
889 return;
890 }
891
892 switch (hdr->code) {
893 case CODE_PADI:
894 pppoe_recv_PADI(pack, size);
895 break;
896 case CODE_PADR:
897 pppoe_recv_PADR(pack, size);
898 break;
899 case CODE_PADT:
900 pppoe_recv_PADT(pack);
901 break;
902 }
903 }
904
905 // Forward from pppoe to l2tp remote LNS
906 static void pppoe_forwardto_session_rmlns(uint8_t *pack, int size, sessionidt sess, uint16_t proto)
907 {
908 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
909 uint16_t lppp = ntohs(hdr->length);
910 uint16_t ll2tp = lppp + 6;
911 uint8_t *pppdata = (uint8_t *) hdr->tag;
912 uint8_t *pl2tp = pppdata - 6;
913 uint8_t *p = pl2tp;
914 uint16_t t = 0, s = 0;
915
916 s = session[sess].forwardtosession;
917 if (session[s].forwardtosession != sess)
918 {
919 LOG(3, sess, session[sess].tunnel, "pppoe: Link Session (%u) broken\n", s);
920 return;
921 }
922
923 t = session[s].tunnel;
924 if (t >= MAXTUNNEL)
925 {
926 LOG(1, s, t, "pppoe: Session with invalid tunnel ID\n");
927 return;
928 }
929
930 if (!tunnel[t].isremotelns)
931 {
932 LOG(3, sess, session[sess].tunnel, "pppoe: Link Tunnel/Session (%u/%u) broken\n", s, t);
933 return;
934 }
935
936 // First word L2TP options (with no options)
937 *(uint16_t *) p = htons(0x0002);
938 p += 2;
939 *(uint16_t *) p = htons(tunnel[t].far); // tunnel
940 p += 2;
941 *(uint16_t *) p = htons(session[s].far); // session
942 p += 2;
943
944 if ((proto == PPPIP) || (proto == PPPMP) ||(proto == PPPIPV6 && config->ipv6_prefix.s6_addr[0]))
945 {
946 session[sess].last_packet = session[sess].last_data = time_now;
947 // Update STAT IN
948 increment_counter(&session[sess].cin, &session[sess].cin_wrap, ll2tp);
949 session[sess].cin_delta += ll2tp;
950 session[sess].pin++;
951 sess_local[sess].cin += ll2tp;
952 sess_local[sess].pin++;
953
954 session[s].last_data = time_now;
955 // Update STAT OUT
956 increment_counter(&session[s].cout, &session[s].cout_wrap, ll2tp); // byte count
957 session[s].cout_delta += ll2tp;
958 session[s].pout++;
959 sess_local[s].cout += ll2tp;
960 sess_local[s].pout++;
961 }
962 else
963 session[sess].last_packet = time_now;
964
965 tunnelsend(pl2tp, ll2tp, t); // send it...
966 }
967
968 // Forward from l2tp to pppoe
969 // (note: THIS ROUTINE WRITES TO pack[-20]).
970 void pppoe_forwardto_session_pppoe(uint8_t *pack, int size, sessionidt sess, uint16_t proto)
971 {
972 uint16_t t = 0, s = 0;
973 uint16_t lpppoe = size - 2;
974 uint8_t *p = pack + 2; // First word L2TP options
975
976 LOG(5, sess, session[sess].tunnel, "Forwarding data session to pppoe session %u\n", session[sess].forwardtosession);
977
978 s = session[sess].forwardtosession;
979 t = session[s].tunnel;
980
981 if (*pack & 0x40)
982 { // length
983 p += 2;
984 lpppoe -= 2;
985 }
986
987 *(uint16_t *) p = htons(tunnel[t].far); // tunnel
988 p += 2;
989 *(uint16_t *) p = htons(session[s].far); // session
990 p += 2;
991 lpppoe -= 4;
992
993 if (*pack & 0x08)
994 { // ns/nr
995 *(uint16_t *) p = htons(tunnel[t].ns); // sequence
996 p += 2;
997 *(uint16_t *) p = htons(tunnel[t].nr); // sequence
998 p += 2;
999 lpppoe -= 4;
1000 }
1001
1002 if (lpppoe > 2 && p[0] == 0xFF && p[1] == 0x03)
1003 {
1004 // HDLC address header, discard in pppoe
1005 p += 2;
1006 lpppoe -= 2;
1007 }
1008
1009 lpppoe += (ETH_HLEN + sizeof(struct pppoe_hdr));
1010 p -= (ETH_HLEN + sizeof(struct pppoe_hdr));
1011
1012 // 14 bytes ethernet Header + 6 bytes header pppoe
1013 setup_header(p, config->pppoe_hwaddr, session[s].src_hwaddr, CODE_SESS, s, ETH_P_PPP_SES);
1014
1015 if ((proto == PPPIP) || (proto == PPPMP) ||(proto == PPPIPV6 && config->ipv6_prefix.s6_addr[0]))
1016 {
1017 session[sess].last_packet = session[sess].last_data = time_now;
1018 // Update STAT IN
1019 increment_counter(&session[sess].cin, &session[sess].cin_wrap, lpppoe);
1020 session[sess].cin_delta += lpppoe;
1021 session[sess].pin++;
1022 sess_local[sess].cin += lpppoe;
1023 sess_local[sess].pin++;
1024
1025 session[s].last_data = time_now;
1026 // Update STAT OUT
1027 increment_counter(&session[s].cout, &session[s].cout_wrap, lpppoe); // byte count
1028 session[s].cout_delta += lpppoe;
1029 session[s].pout++;
1030 sess_local[s].cout += lpppoe;
1031 sess_local[s].pout++;
1032 }
1033 else
1034 session[sess].last_packet = time_now;
1035
1036 tunnelsend(p, lpppoe, t); // send it....
1037 }
1038
1039 void process_pppoe_sess(uint8_t *pack, int size)
1040 {
1041 //struct ethhdr *ethhdr = (struct ethhdr *)pack;
1042 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(pack + ETH_HLEN);
1043 uint16_t lppp = ntohs(hdr->length);
1044 uint8_t *pppdata = (uint8_t *) hdr->tag;
1045 uint16_t proto, sid, t;
1046
1047 sid = ntohs(hdr->sid);
1048 t = TUNNEL_ID_PPPOE;
1049
1050 LOG_HEX(5, "RCV PPPOE Sess", pack, size);
1051
1052 if (sid >= MAXSESSION)
1053 {
1054 LOG(0, sid, t, "Received pppoe packet with invalid session ID\n");
1055 STAT(tunnel_rx_errors);
1056 return;
1057 }
1058
1059 if (session[sid].tunnel != t)
1060 {
1061 if (!config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1062
1063 LOG(1, sid, t, "ERROR process_pppoe_sess: Session is not a session pppoe\n");
1064 return;
1065 }
1066
1067 if (hdr->ver != 1)
1068 {
1069 LOG(3, sid, t, "Error process_pppoe_sess: discarding packet (unsupported version %i)\n", hdr->ver);
1070 return;
1071 }
1072
1073 if (hdr->type != 1)
1074 {
1075 LOG(3, sid, t, "Error process_pppoe_sess: discarding packet (unsupported type %i)\n", hdr->type);
1076 return;
1077 }
1078
1079 if (lppp > 2 && pppdata[0] == 0xFF && pppdata[1] == 0x03)
1080 { // HDLC address header, discard
1081 LOG(5, sid, t, "pppoe_sess: HDLC address header, discard\n");
1082 pppdata += 2;
1083 lppp -= 2;
1084 }
1085 if (lppp < 2)
1086 {
1087 LOG(3, sid, t, "Error process_pppoe_sess: Short ppp length %d\n", lppp);
1088 return;
1089 }
1090 if (*pppdata & 1)
1091 {
1092 proto = *pppdata++;
1093 lppp--;
1094 }
1095 else
1096 {
1097 proto = ntohs(*(uint16_t *) pppdata);
1098 pppdata += 2;
1099 lppp -= 2;
1100 }
1101
1102 if (session[sid].forwardtosession)
1103 { // Must be forwaded to a remote lns tunnel l2tp
1104 pppoe_forwardto_session_rmlns(pack, size, sid, proto);
1105 return;
1106 }
1107
1108 if (proto == PPPPAP)
1109 {
1110 session[sid].last_packet = time_now;
1111 if (!config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1112 processpap(sid, t, pppdata, lppp);
1113 }
1114 else if (proto == PPPCHAP)
1115 {
1116 session[sid].last_packet = time_now;
1117 if (!config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1118 processchap(sid, t, pppdata, lppp);
1119 }
1120 else if (proto == PPPLCP)
1121 {
1122 session[sid].last_packet = time_now;
1123 if (!config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1124 processlcp(sid, t, pppdata, lppp);
1125 }
1126 else if (proto == PPPIPCP)
1127 {
1128 session[sid].last_packet = time_now;
1129 if (!config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1130 processipcp(sid, t, pppdata, lppp);
1131 }
1132 else if (proto == PPPIPV6CP && config->ipv6_prefix.s6_addr[0])
1133 {
1134 session[sid].last_packet = time_now;
1135 if (!config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1136 processipv6cp(sid, t, pppdata, lppp);
1137 }
1138 else if (proto == PPPCCP)
1139 {
1140 session[sid].last_packet = time_now;
1141 if (!config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1142 processccp(sid, t, pppdata, lppp);
1143 }
1144 else if (proto == PPPIP)
1145 {
1146 session[sid].last_packet = session[sid].last_data = time_now;
1147 if (session[sid].walled_garden && !config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1148 processipin(sid, t, pppdata, lppp);
1149 }
1150 else if (proto == PPPMP)
1151 {
1152 session[sid].last_packet = session[sid].last_data = time_now;
1153 if (!config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1154 processmpin(sid, t, pppdata, lppp);
1155 }
1156 else if (proto == PPPIPV6 && config->ipv6_prefix.s6_addr[0])
1157 {
1158 session[sid].last_packet = session[sid].last_data = time_now;
1159 if (session[sid].walled_garden && !config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1160 processipv6in(sid, t, pppdata, lppp);
1161 }
1162 else if (session[sid].ppp.lcp == Opened)
1163 {
1164 session[sid].last_packet = time_now;
1165 if (!config->cluster_iam_master) { master_forward_pppoe_packet(pack, size, hdr->code); return; }
1166 protoreject(sid, t, pppdata, lppp, proto);
1167 }
1168 else
1169 {
1170 LOG(3, sid, t, "process_pppoe_sess: Unknown PPP protocol 0x%04X received in LCP %s state\n",
1171 proto, ppp_state(session[sid].ppp.lcp));
1172 }
1173 }
1174
1175 void pppoe_send_garp()
1176 {
1177 int s;
1178 struct ifreq ifr;
1179 uint8_t mac[6];
1180
1181 if (!*config->pppoe_if_to_bind)
1182 return;
1183
1184 s = socket(PF_INET, SOCK_DGRAM, 0);
1185 if (s < 0)
1186 {
1187 LOG(0, 0, 0, "Error creating socket for GARP: %s\n", strerror(errno));
1188 return;
1189 }
1190 memset(&ifr, 0, sizeof(ifr));
1191 strncpy(ifr.ifr_name, config->pppoe_if_to_bind, sizeof(ifr.ifr_name) - 1);
1192 if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0)
1193 {
1194 LOG(0, 0, 0, "Error getting eth0 hardware address for GARP: %s\n", strerror(errno));
1195 close(s);
1196 return;
1197 }
1198 memcpy(mac, &ifr.ifr_hwaddr.sa_data, 6*sizeof(char));
1199 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0)
1200 {
1201 LOG(0, 0, 0, "Error getting eth0 interface index for GARP: %s\n", strerror(errno));
1202 close(s);
1203 return;
1204 }
1205 close(s);
1206
1207 sendarp(ifr.ifr_ifindex, mac, config->iftun_address);
1208 }
1209
1210 // rcv pppoe data from slave
1211 void pppoe_process_forward(uint8_t *pack, int size, in_addr_t addr)
1212 {
1213 struct ethhdr *ethhdr = (struct ethhdr *)pack;
1214
1215 if (ethhdr->h_proto == htons(ETH_P_PPP_DISC))
1216 process_pppoe_disc(pack, size);
1217 else if (ethhdr->h_proto == htons(ETH_P_PPP_SES))
1218 process_pppoe_sess(pack, size);
1219 else
1220 LOG(0, 0, 0, "pppoe_process_forward: I got a C_PPPOE_FORWARD from %s, but not a PPPOE data?\n", fmtaddr(addr, 0));
1221 }