3 * Add functionality "server pppoe" to l2tpns.
4 * inspiration pppoe.c of accel-ppp
17 #include <sys/socket.h>
18 #include <sys/ioctl.h>
20 #include <net/ethernet.h>
21 #include <netpacket/packet.h>
22 #include <arpa/inet.h>
23 #include <linux/if_pppox.h>
27 #include "constants.h"
34 static uint8_t bc_addr
[ETH_ALEN
] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
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
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
56 static char *code_pad
[] = {
76 // set up pppoe discovery socket
77 static void init_pppoe_disc(void)
81 struct sockaddr_ll sa
;
83 memset(&ifr
, 0, sizeof(ifr
));
84 memset(&sa
, 0, sizeof(sa
));
86 pppoediscfd
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_PPP_DISC
));
89 LOG(0, 0, 0, "Error pppoe: socket: %s\n", strerror(errno
));
93 fcntl(pppoediscfd
, F_SETFD
, fcntl(pppoediscfd
, F_GETFD
) | FD_CLOEXEC
);
95 if (setsockopt(pppoediscfd
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof(on
)))
97 LOG(0, 0, 0, "Error pppoe: setsockopt(SO_BROADCAST): %s\n", strerror(errno
));
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
);
105 if (ioctl(pppoediscfd
, SIOCGIFHWADDR
, &ifr
))
107 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFHWADDR): %s\n", strerror(errno
));
111 if ((ifr
.ifr_hwaddr
.sa_data
[0] & 1) != 0)
113 LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config
->pppoe_if_to_bind
);
117 memcpy(config
->pppoe_hwaddr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
119 if (ioctl(pppoediscfd
, SIOCGIFMTU
, &ifr
))
121 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFMTU): %s\n", strerror(errno
));
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
);
128 if (ioctl(pppoediscfd
, SIOCGIFINDEX
, &ifr
))
130 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFINDEX): %s\n", strerror(errno
));
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
;
139 if (bind(pppoediscfd
, (struct sockaddr
*)&sa
, sizeof(sa
)))
141 LOG(0, 0, 0, "Error pppoe: bind: %s\n", strerror(errno
));
145 if (fcntl(pppoediscfd
, F_SETFL
, O_NONBLOCK
))
147 LOG(0, 0, 0, "Error pppoe: failed to set nonblocking mode: %s\n", strerror(errno
));
153 // set up pppoe session socket
154 static void init_pppoe_sess(void)
158 struct sockaddr_ll sa
;
160 memset(&ifr
, 0, sizeof(ifr
));
161 memset(&sa
, 0, sizeof(sa
));
163 pppoesessfd
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_PPP_SES
));
166 LOG(0, 0, 0, "Error pppoe: socket: %s\n", strerror(errno
));
170 fcntl(pppoesessfd
, F_SETFD
, fcntl(pppoesessfd
, F_GETFD
) | FD_CLOEXEC
);
172 if (setsockopt(pppoesessfd
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof(on
)))
174 LOG(0, 0, 0, "Error pppoe: setsockopt(SO_BROADCAST): %s\n", strerror(errno
));
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
);
182 if (ioctl(pppoesessfd
, SIOCGIFHWADDR
, &ifr
))
184 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFHWADDR): %s\n", strerror(errno
));
188 if ((ifr
.ifr_hwaddr
.sa_data
[0] & 1) != 0)
190 LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config
->pppoe_if_to_bind
);
194 memcpy(config
->pppoe_hwaddr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
196 if (ioctl(pppoesessfd
, SIOCGIFMTU
, &ifr
))
198 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFMTU): %s\n", strerror(errno
));
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
);
205 if (ioctl(pppoesessfd
, SIOCGIFINDEX
, &ifr
))
207 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFINDEX): %s\n", strerror(errno
));
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
;
216 if (bind(pppoesessfd
, (struct sockaddr
*)&sa
, sizeof(sa
)))
218 LOG(0, 0, 0, "Error pppoe: bind: %s\n", strerror(errno
));
222 if (fcntl(pppoesessfd
, F_SETFL
, O_NONBLOCK
))
224 LOG(0, 0, 0, "Error pppoe: failed to set nonblocking mode: %s\n", strerror(errno
));
229 // set up pppoe discovery/session socket
230 void init_pppoe(void)
232 tunnelidt t
= TUNNEL_ID_PPPOE
;
237 // Reserve the a pseudo tunnel for pppoe server
238 if (t
> config
->cluster_highest_tunnelid
)
239 config
->cluster_highest_tunnelid
= t
;
241 memset(&tunnel
[t
], 0, sizeof(tunnel
[t
]));
242 tunnel
[t
].state
= TUNNELOPEN
;
243 STAT(tunnel_created
);
246 char * get_string_codepad(uint8_t codepad
)
252 ptrch
= code_pad
[INDEX_PADI
];
256 ptrch
= code_pad
[INDEX_PADO
];
260 ptrch
= code_pad
[INDEX_PADR
];
264 ptrch
= code_pad
[INDEX_PADS
];
268 ptrch
= code_pad
[INDEX_PADT
];
272 ptrch
= code_pad
[INDEX_SESS
];
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
)
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
);
287 memcpy(ethhdr
->h_source
, src
, ETH_ALEN
);
288 memcpy(ethhdr
->h_dest
, dst
, ETH_ALEN
);
289 ethhdr
->h_proto
= htons(h_proto
);
294 hdr
->sid
= htons(sid
);
297 p
= (uint8_t *)(pack
+ ETH_HLEN
+ sizeof(*hdr
));
302 static void add_tag(uint8_t *pack
, int type
, const uint8_t *data
, int len
)
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
));
307 tag
->tag_type
= htons(type
);
308 tag
->tag_len
= htons(len
);
309 memcpy(tag
->tag_data
, data
, len
);
311 hdr
->length
= htons(ntohs(hdr
->length
) + sizeof(*tag
) + len
);
314 static void add_tag2(uint8_t *pack
, const struct pppoe_tag
*t
)
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
));
319 memcpy(tag
, t
, sizeof(*t
) + ntohs(t
->tag_len
));
321 hdr
->length
= htons(ntohs(hdr
->length
) + sizeof(*tag
) + ntohs(t
->tag_len
));
324 static void pppoe_disc_send(const uint8_t *pack
)
326 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
327 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
330 s
= ETH_HLEN
+ sizeof(*hdr
) + ntohs(hdr
->length
);
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
);
335 n
= write(pppoediscfd
, pack
, s
);
337 LOG(0, 0, 0, "pppoe: write: %s\n", strerror(errno
));
339 LOG(0, 0, 0, "pppoe: short write %i/%i\n", n
,s
);
343 void pppoe_sess_send(const uint8_t *pack
, uint16_t l
, tunnelidt t
)
345 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
350 if (t
!= TUNNEL_ID_PPPOE
)
352 LOG(3, 0, t
, "ERROR pppoe_sess_send: Tunnel %d is not a tunnel pppoe\n", t
);
357 if (session
[s
].tunnel
!= t
)
359 LOG(3, s
, t
, "ERROR pppoe_sess_send: Session is not a session pppoe\n");
363 if (l
< (ETH_HLEN
+ sizeof(*hdr
) + 3))
365 LOG(0, s
, t
, "ERROR pppoe_sess_send: packet too small for pppoe sent (size=%d)\n", l
);
369 // recalculate the ppp frame length
370 sizeppp
= l
- (ETH_HLEN
+ sizeof(*hdr
));
371 hdr
->length
= htons(sizeppp
);
373 LOG_HEX(5, "pppoe_sess_send", pack
, l
);
375 n
= write(pppoesessfd
, pack
, l
);
377 LOG(0, s
, t
, "pppoe_sess_send: write: %s\n", strerror(errno
));
379 LOG(0, s
, t
, "pppoe_sess_send: short write %i/%i\n", n
,l
);
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
)
384 uint8_t pack
[ETHER_MAX_LEN
];
386 setup_header(pack
, config
->pppoe_hwaddr
, addr
, code
, 0, ETH_P_PPP_DISC
);
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);
392 add_tag2(pack
, host_uniq
);
395 add_tag2(pack
, relay_sid
);
397 pppoe_disc_send(pack
);
401 static void pppoe_gen_cookie(const uint8_t *serv_hwaddr
, const uint8_t *client_hwaddr
, uint8_t *out
)
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
);
413 static int pppoe_check_cookie(const uint8_t *serv_hwaddr
, const uint8_t *client_hwaddr
, uint8_t *cookie
)
417 pppoe_gen_cookie(serv_hwaddr
, client_hwaddr
, hash
);
419 return memcmp(hash
, cookie
, 16);
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
)
424 uint8_t pack
[ETHER_MAX_LEN
];
427 setup_header(pack
, config
->pppoe_hwaddr
, addr
, CODE_PADO
, 0, ETH_P_PPP_DISC
);
429 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
432 add_tag2(pack
, service_name
);
434 pppoe_gen_cookie(config
->pppoe_hwaddr
, addr
, hash
);
435 add_tag(pack
, TAG_AC_COOKIE
, hash
, 16);
438 add_tag2(pack
, host_uniq
);
441 add_tag2(pack
, relay_sid
);
443 pppoe_disc_send(pack
);
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
)
448 uint8_t pack
[ETHER_MAX_LEN
];
450 setup_header(pack
, config
->pppoe_hwaddr
, addr
, CODE_PADS
, sid
, ETH_P_PPP_DISC
);
452 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
454 add_tag2(pack
, service_name
);
457 add_tag2(pack
, host_uniq
);
460 add_tag2(pack
, relay_sid
);
462 pppoe_disc_send(pack
);
465 static void pppoe_send_PADT(uint16_t sid
)
467 uint8_t pack
[ETHER_MAX_LEN
];
469 setup_header(pack
, config
->pppoe_hwaddr
, session
[sid
].src_hwaddr
, CODE_PADT
, sid
, ETH_P_PPP_DISC
);
471 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
473 LOG(3, sid
, session
[sid
].tunnel
, "pppoe: Sent PADT\n");
475 pppoe_disc_send(pack
);
478 void pppoe_shutdown_session(sessionidt s
)
481 if (session
[s
].tunnel
!= TUNNEL_ID_PPPOE
)
483 LOG(3, s
, session
[s
].tunnel
, "ERROR pppoe_shutdown_session: Session is not a session pppoe\n");
490 static void pppoe_recv_PADI(uint8_t *pack
, int size
)
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;
504 len
= ntohs(hdr
->length
);
505 for (n
= 0; n
< len
; n
+= sizeof(*tag
) + ntohs(tag
->tag_len
))
507 tag
= (struct pppoe_tag
*)(pack
+ ETH_HLEN
+ sizeof(*hdr
) + n
);
508 if (n
+ sizeof(*tag
) + ntohs(tag
->tag_len
) > len
)
510 switch (ntohs(tag
->tag_type
))
512 case TAG_END_OF_LIST
:
514 case TAG_SERVICE_NAME
:
515 if (config
->pppoe_only_equal_svc_name
&& *config
->pppoe_service_name
&& !tag
->tag_len
)
519 else if (*config
->pppoe_service_name
&& tag
->tag_len
)
521 if (ntohs(tag
->tag_len
) != strlen(config
->pppoe_service_name
))
523 if (memcmp(tag
->tag_data
, config
->pppoe_service_name
, ntohs(tag
->tag_len
)))
525 service_name_tag
= tag
;
530 service_name_tag
= tag
;
537 case TAG_RELAY_SESSION_ID
:
545 LOG(3, 0, 0, "pppoe: discarding PADI packet (Service-Name mismatch)\n");
549 pppoe_send_PADO(ethhdr
->h_source
, host_uniq_tag
, relay_sid_tag
, service_name_tag
);
552 static void pppoe_recv_PADR(uint8_t *pack
, int size
)
554 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
555 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
556 struct pppoe_tag
*tag
;
557 struct pppoe_tag
*host_uniq_tag
= NULL
;
558 struct pppoe_tag
*relay_sid_tag
= NULL
;
559 struct pppoe_tag
*ac_cookie_tag
= NULL
;
560 struct pppoe_tag
*service_name_tag
= NULL
;
561 int n
, service_match
= 0;
564 if (!memcmp(ethhdr
->h_dest
, bc_addr
, ETH_ALEN
))
566 LOG(1, 0, 0, "Rcv pppoe: discard PADR (destination address is broadcast)\n");
572 LOG(1, 0, 0, "Rcv pppoe: discarding PADR packet (sid is not zero)\n");
576 for (n
= 0; n
< ntohs(hdr
->length
); n
+= sizeof(*tag
) + ntohs(tag
->tag_len
))
578 tag
= (struct pppoe_tag
*)(pack
+ ETH_HLEN
+ sizeof(*hdr
) + n
);
579 switch (ntohs(tag
->tag_type
))
581 case TAG_END_OF_LIST
:
583 case TAG_SERVICE_NAME
:
584 service_name_tag
= tag
;
585 if (tag
->tag_len
== 0)
587 else if (*config
->pppoe_service_name
)
589 if (ntohs(tag
->tag_len
) != strlen(config
->pppoe_service_name
))
591 if (memcmp(tag
->tag_data
, config
->pppoe_service_name
, ntohs(tag
->tag_len
)))
606 case TAG_RELAY_SESSION_ID
:
614 LOG(3, 0, 0, "pppoe: Service-Name mismatch\n");
615 pppoe_send_err(ethhdr
->h_source
, host_uniq_tag
, relay_sid_tag
, CODE_PADS
, TAG_SERVICE_NAME_ERROR
);
621 LOG(3, 0, 0, "pppoe: discard PADR packet (no AC-Cookie tag present)\n");
625 if (ntohs(ac_cookie_tag
->tag_len
) != 16)
627 LOG(3, 0, 0, "pppoe: discard PADR packet (incorrect AC-Cookie tag length)\n");
631 if (pppoe_check_cookie(ethhdr
->h_dest
, ethhdr
->h_source
, (uint8_t *) ac_cookie_tag
->tag_data
))
633 LOG(3, 0, 0, "pppoe: discard PADR packet (incorrect AC-Cookie)\n");
638 sessionfree
= session
[sid
].next
;
639 memset(&session
[sid
], 0, sizeof(session
[0]));
641 if (sid
> config
->cluster_highest_sessionid
)
642 config
->cluster_highest_sessionid
= sid
;
644 session
[sid
].opened
= time_now
;
645 session
[sid
].tunnel
= TUNNEL_ID_PPPOE
;
646 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
648 //strncpy(session[sid].called, called, sizeof(session[sid].called) - 1);
649 //strncpy(session[sid].calling, calling, sizeof(session[sid].calling) - 1);
651 session
[sid
].ppp
.phase
= Establish
;
652 session
[sid
].ppp
.lcp
= Starting
;
654 session
[sid
].magic
= time_now
; // set magic number
655 session
[sid
].mru
= PPPoE_MRU
; // default
658 sess_local
[sid
].lcp_authtype
= config
->radius_authprefer
;
659 sess_local
[sid
].ppp_mru
= MRU
;
661 // Set multilink options before sending initial LCP packet
662 sess_local
[sid
].mp_mrru
= 1614;
663 sess_local
[sid
].mp_epdis
= ntohl(config
->iftun_address
? config
->iftun_address
: my_address
);
665 memcpy(session
[sid
].src_hwaddr
, ethhdr
->h_source
, ETH_ALEN
);
666 pppoe_send_PADS(sid
, ethhdr
->h_source
, host_uniq_tag
, relay_sid_tag
, service_name_tag
);
668 sendlcp(sid
, session
[sid
].tunnel
);
669 change_state(sid
, lcp
, RequestSent
);
673 static void pppoe_recv_PADT(uint8_t *pack
)
675 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
676 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
678 if (!memcmp(ethhdr
->h_dest
, bc_addr
, ETH_ALEN
))
680 LOG(3, 0, 0, "pppoe: discard PADT (destination address is broadcast)\n");
686 if ((hdr
->sid
< MAXSESSION
) && (session
[hdr
->sid
].tunnel
== TUNNEL_ID_PPPOE
))
687 sessionshutdown(hdr
->sid
, "Client shutdown", CDN_ADMIN_DISC
, 0);
691 // fill in a PPPOE message with a PPP frame,
692 // returns start of PPP frame
693 uint8_t *pppoe_makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
,
694 uint16_t mtype
, uint8_t prio
, bundleidt bid
, uint8_t mp_bits
)
696 uint16_t type
= mtype
;
698 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(b
+ ETH_HLEN
);
700 if (t
!= TUNNEL_ID_PPPOE
)
703 if (size
< 28) // Need more space than this!!
705 LOG(0, s
, t
, "pppoe_makeppp buffer too small for pppoe header (size=%d)\n", size
);
709 // 14 bytes ethernet Header + 6 bytes header pppoe
710 b
= setup_header(b
, config
->pppoe_hwaddr
, session
[s
].src_hwaddr
, CODE_SESS
, s
, ETH_P_PPP_SES
);
712 // Check whether this session is part of multilink
715 if (bundle
[bid
].num_of_links
> 1)
716 type
= PPPMP
; // Change PPP message type to the PPPMP
721 *(uint16_t *) b
= htons(type
);
727 // Set the sequence number and (B)egin (E)nd flags
730 // Set the multilink bits
731 uint16_t bits_send
= mp_bits
;
732 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
738 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
739 // Set the multilink bits
745 bundle
[bid
].seq_num_t
++;
747 // Add the message type if this fragment has the begin bit set
748 if (mp_bits
& MP_BEGIN
)
750 //*b++ = mtype; // The next two lines are instead of this
751 *(uint16_t *) b
= htons(mtype
); // Message type
757 if ((b
- start
) + l
> size
)
759 LOG(0, s
, t
, "pppoe_makeppp would overflow buffer (size=%d, header+payload=%td)\n", size
, (b
- start
) + l
);
773 // fill in a PPPOE message with a PPP frame,
774 // returns start of PPP frame
775 //(note: THIS ROUTINE WRITES TO p[-28]).
776 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
)
778 uint16_t type
= mtype
;
781 struct pppoe_hdr
*hdr
;
783 if (t
!= TUNNEL_ID_PPPOE
)
786 // Check whether this session is part of multilink
789 if (bundle
[bid
].num_of_links
> 1)
790 type
= PPPMP
; // Change PPP message type to the PPPMP
797 // Add the message type if this fragment has the begin bit set
798 if (mp_bits
& MP_BEGIN
)
801 *(uint16_t *) b
= htons(mtype
); // Message type
804 // Set the sequence number and (B)egin (E)nd flags
807 // Set the multilink bits
808 uint16_t bits_send
= mp_bits
;
810 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
815 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
816 // Set the multilink bits
820 bundle
[bid
].seq_num_t
++;
824 *(uint16_t *) b
= htons(type
);
829 // 14 bytes ethernet Header + 6 bytes header pppoe
830 b
-= (ETH_HLEN
+ sizeof(*hdr
));
831 setup_header(b
, config
->pppoe_hwaddr
, session
[s
].src_hwaddr
, CODE_SESS
, s
, ETH_P_PPP_SES
);
832 hdr
= (struct pppoe_hdr
*)(b
+ ETH_HLEN
);
833 // Store length on header pppoe
834 hdr
->length
= hdrlen
;
839 // pppoe discovery recv data
840 void process_pppoe_disc(uint8_t *pack
, int size
)
842 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
843 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
845 LOG(3, 0, 0, "RCV pppoe_disc: Code %s from %s\n", get_string_codepad(hdr
->code
), fmtMacAddr(ethhdr
->h_source
));
846 LOG_HEX(5, "PPPOE Disc", pack
, size
);
848 if (!config
->cluster_iam_master
)
850 if (hdr
->code
== CODE_PADI
)
851 return; // Discard because the PADI is received by all (PADI is a broadcast diffusion)
853 master_forward_pppoe_packet(pack
, size
, hdr
->code
);
857 if (size
< (ETH_HLEN
+ sizeof(*hdr
)))
859 LOG(1, 0, 0, "Error pppoe_disc: short packet received (%i)\n", size
);
863 if (memcmp(ethhdr
->h_dest
, bc_addr
, ETH_ALEN
) && memcmp(ethhdr
->h_dest
, config
->pppoe_hwaddr
, ETH_ALEN
))
865 LOG(1, 0, 0, "Error pppoe_disc: h_dest != bc_addr and h_dest != config->pppoe_hwaddr\n");
869 if (!memcmp(ethhdr
->h_source
, bc_addr
, ETH_ALEN
))
871 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (source address is broadcast)\n");
875 if ((ethhdr
->h_source
[0] & 1) != 0)
877 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (host address is not unicast)\n");
881 if (size
< ETH_HLEN
+ sizeof(*hdr
) + ntohs(hdr
->length
))
883 LOG(1, 0, 0, "Error pppoe_disc: short packet received\n");
889 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (unsupported version %i)\n", hdr
->ver
);
895 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (unsupported type %i)\n", hdr
->type
);
901 pppoe_recv_PADI(pack
, size
);
904 pppoe_recv_PADR(pack
, size
);
907 pppoe_recv_PADT(pack
);
912 // Forward from pppoe to l2tp remote LNS
913 static void pppoe_forwardto_session_rmlns(uint8_t *pack
, int size
, sessionidt sess
, uint16_t proto
)
915 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
916 uint16_t lppp
= ntohs(hdr
->length
);
917 uint16_t ll2tp
= lppp
+ 6;
918 uint8_t *pppdata
= (uint8_t *) hdr
->tag
;
919 uint8_t *pl2tp
= pppdata
- 6;
921 uint16_t t
= 0, s
= 0;
923 s
= session
[sess
].forwardtosession
;
924 if (session
[s
].forwardtosession
!= sess
)
926 LOG(3, sess
, session
[sess
].tunnel
, "pppoe: Link Session (%u) broken\n", s
);
930 t
= session
[s
].tunnel
;
933 LOG(1, s
, t
, "pppoe: Session with invalid tunnel ID\n");
937 if (!tunnel
[t
].isremotelns
)
939 LOG(3, sess
, session
[sess
].tunnel
, "pppoe: Link Tunnel/Session (%u/%u) broken\n", s
, t
);
943 // First word L2TP options (with no options)
944 *(uint16_t *) p
= htons(0x0002);
946 *(uint16_t *) p
= htons(tunnel
[t
].far
); // tunnel
948 *(uint16_t *) p
= htons(session
[s
].far
); // session
951 if ((proto
== PPPIP
) || (proto
== PPPMP
) ||(proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0]))
953 session
[sess
].last_packet
= session
[sess
].last_data
= time_now
;
955 increment_counter(&session
[sess
].cin
, &session
[sess
].cin_wrap
, ll2tp
);
956 session
[sess
].cin_delta
+= ll2tp
;
958 sess_local
[sess
].cin
+= ll2tp
;
959 sess_local
[sess
].pin
++;
961 session
[s
].last_data
= time_now
;
963 increment_counter(&session
[s
].cout
, &session
[s
].cout_wrap
, ll2tp
); // byte count
964 session
[s
].cout_delta
+= ll2tp
;
966 sess_local
[s
].cout
+= ll2tp
;
967 sess_local
[s
].pout
++;
970 session
[sess
].last_packet
= time_now
;
972 tunnelsend(pl2tp
, ll2tp
, t
); // send it...
975 // Forward from l2tp to pppoe
976 // (note: THIS ROUTINE WRITES TO pack[-20]).
977 void pppoe_forwardto_session_pppoe(uint8_t *pack
, int size
, sessionidt sess
, uint16_t proto
)
979 uint16_t t
= 0, s
= 0;
980 uint16_t lpppoe
= size
- 2;
981 uint8_t *p
= pack
+ 2; // First word L2TP options
983 LOG(5, sess
, session
[sess
].tunnel
, "Forwarding data session to pppoe session %u\n", session
[sess
].forwardtosession
);
985 s
= session
[sess
].forwardtosession
;
986 t
= session
[s
].tunnel
;
994 *(uint16_t *) p
= htons(tunnel
[t
].far
); // tunnel
996 *(uint16_t *) p
= htons(session
[s
].far
); // session
1002 *(uint16_t *) p
= htons(tunnel
[t
].ns
); // sequence
1004 *(uint16_t *) p
= htons(tunnel
[t
].nr
); // sequence
1009 if (lpppoe
> 2 && p
[0] == 0xFF && p
[1] == 0x03)
1011 // HDLC address header, discard in pppoe
1016 lpppoe
+= (ETH_HLEN
+ sizeof(struct pppoe_hdr
));
1017 p
-= (ETH_HLEN
+ sizeof(struct pppoe_hdr
));
1019 // 14 bytes ethernet Header + 6 bytes header pppoe
1020 setup_header(p
, config
->pppoe_hwaddr
, session
[s
].src_hwaddr
, CODE_SESS
, s
, ETH_P_PPP_SES
);
1022 if ((proto
== PPPIP
) || (proto
== PPPMP
) ||(proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0]))
1024 session
[sess
].last_packet
= session
[sess
].last_data
= time_now
;
1026 increment_counter(&session
[sess
].cin
, &session
[sess
].cin_wrap
, lpppoe
);
1027 session
[sess
].cin_delta
+= lpppoe
;
1028 session
[sess
].pin
++;
1029 sess_local
[sess
].cin
+= lpppoe
;
1030 sess_local
[sess
].pin
++;
1032 session
[s
].last_data
= time_now
;
1034 increment_counter(&session
[s
].cout
, &session
[s
].cout_wrap
, lpppoe
); // byte count
1035 session
[s
].cout_delta
+= lpppoe
;
1037 sess_local
[s
].cout
+= lpppoe
;
1038 sess_local
[s
].pout
++;
1041 session
[sess
].last_packet
= time_now
;
1043 tunnelsend(p
, lpppoe
, t
); // send it....
1046 void process_pppoe_sess(uint8_t *pack
, int size
)
1048 //struct ethhdr *ethhdr = (struct ethhdr *)pack;
1049 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
1050 uint16_t lppp
= ntohs(hdr
->length
);
1051 uint8_t *pppdata
= (uint8_t *) hdr
->tag
;
1052 uint16_t proto
, sid
, t
;
1054 sid
= ntohs(hdr
->sid
);
1055 t
= TUNNEL_ID_PPPOE
;
1057 LOG_HEX(5, "RCV PPPOE Sess", pack
, size
);
1059 if (sid
>= MAXSESSION
)
1061 LOG(0, sid
, t
, "Received pppoe packet with invalid session ID\n");
1062 STAT(tunnel_rx_errors
);
1066 if (session
[sid
].tunnel
!= t
)
1068 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1070 LOG(1, sid
, t
, "ERROR process_pppoe_sess: Session is not a session pppoe\n");
1076 LOG(3, sid
, t
, "Error process_pppoe_sess: discarding packet (unsupported version %i)\n", hdr
->ver
);
1082 LOG(3, sid
, t
, "Error process_pppoe_sess: discarding packet (unsupported type %i)\n", hdr
->type
);
1086 if (lppp
> 2 && pppdata
[0] == 0xFF && pppdata
[1] == 0x03)
1087 { // HDLC address header, discard
1088 LOG(5, sid
, t
, "pppoe_sess: HDLC address header, discard\n");
1094 LOG(3, sid
, t
, "Error process_pppoe_sess: Short ppp length %d\n", lppp
);
1104 proto
= ntohs(*(uint16_t *) pppdata
);
1109 if (session
[sid
].forwardtosession
)
1110 { // Must be forwaded to a remote lns tunnel l2tp
1111 pppoe_forwardto_session_rmlns(pack
, size
, sid
, proto
);
1115 if (proto
== PPPPAP
)
1117 session
[sid
].last_packet
= time_now
;
1118 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1119 processpap(sid
, t
, pppdata
, lppp
);
1121 else if (proto
== PPPCHAP
)
1123 session
[sid
].last_packet
= time_now
;
1124 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1125 processchap(sid
, t
, pppdata
, lppp
);
1127 else if (proto
== PPPLCP
)
1129 session
[sid
].last_packet
= time_now
;
1130 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1131 processlcp(sid
, t
, pppdata
, lppp
);
1133 else if (proto
== PPPIPCP
)
1135 session
[sid
].last_packet
= time_now
;
1136 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1137 processipcp(sid
, t
, pppdata
, lppp
);
1139 else if (proto
== PPPIPV6CP
&& config
->ipv6_prefix
.s6_addr
[0])
1141 session
[sid
].last_packet
= time_now
;
1142 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1143 processipv6cp(sid
, t
, pppdata
, lppp
);
1145 else if (proto
== PPPCCP
)
1147 session
[sid
].last_packet
= time_now
;
1148 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1149 processccp(sid
, t
, pppdata
, lppp
);
1151 else if (proto
== PPPIP
)
1153 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
1154 if (session
[sid
].walled_garden
&& !config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1155 processipin(sid
, t
, pppdata
, lppp
);
1157 else if (proto
== PPPMP
)
1159 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
1160 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1161 processmpin(sid
, t
, pppdata
, lppp
);
1163 else if (proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0])
1165 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
1166 if (session
[sid
].walled_garden
&& !config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1167 processipv6in(sid
, t
, pppdata
, lppp
);
1169 else if (session
[sid
].ppp
.lcp
== Opened
)
1171 session
[sid
].last_packet
= time_now
;
1172 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1173 protoreject(sid
, t
, pppdata
, lppp
, proto
);
1177 LOG(3, sid
, t
, "process_pppoe_sess: Unknown PPP protocol 0x%04X received in LCP %s state\n",
1178 proto
, ppp_state(session
[sid
].ppp
.lcp
));
1182 void pppoe_send_garp()
1188 if (!*config
->pppoe_if_to_bind
)
1191 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
1194 LOG(0, 0, 0, "Error creating socket for GARP: %s\n", strerror(errno
));
1197 memset(&ifr
, 0, sizeof(ifr
));
1198 strncpy(ifr
.ifr_name
, config
->pppoe_if_to_bind
, sizeof(ifr
.ifr_name
) - 1);
1199 if (ioctl(s
, SIOCGIFHWADDR
, &ifr
) < 0)
1201 LOG(0, 0, 0, "Error getting eth0 hardware address for GARP: %s\n", strerror(errno
));
1205 memcpy(mac
, &ifr
.ifr_hwaddr
.sa_data
, 6*sizeof(char));
1206 if (ioctl(s
, SIOCGIFINDEX
, &ifr
) < 0)
1208 LOG(0, 0, 0, "Error getting eth0 interface index for GARP: %s\n", strerror(errno
));
1214 sendarp(ifr
.ifr_ifindex
, mac
, config
->iftun_address
);
1217 // rcv pppoe data from slave
1218 void pppoe_process_forward(uint8_t *pack
, int size
, in_addr_t addr
)
1220 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
1222 if (ethhdr
->h_proto
== htons(ETH_P_PPP_DISC
))
1223 process_pppoe_disc(pack
, size
);
1224 else if (ethhdr
->h_proto
== htons(ETH_P_PPP_SES
))
1225 process_pppoe_sess(pack
, size
);
1227 LOG(0, 0, 0, "pppoe_process_forward: I got a C_PPPOE_FORWARD from %s, but not a PPPOE data?\n", fmtaddr(addr
, 0));