3 * Add functionality "server pppoe" to l2tpns.
16 #include <sys/socket.h>
17 #include <sys/ioctl.h>
19 #include <net/ethernet.h>
20 #include <netpacket/packet.h>
21 #include <arpa/inet.h>
22 #include <linux/if_pppox.h>
26 #include "constants.h"
33 static uint8_t bc_addr
[ETH_ALEN
] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
36 #define CODE_PADI 0x09
37 #define CODE_PADO 0x07
38 #define CODE_PADR 0x19
39 #define CODE_PADS 0x65
40 #define CODE_PADT 0xA7
41 #define CODE_SESS 0x00
44 #define TAG_END_OF_LIST 0x0000
45 #define TAG_SERVICE_NAME 0x0101
46 #define TAG_AC_NAME 0x0102
47 #define TAG_HOST_UNIQ 0x0103
48 #define TAG_AC_COOKIE 0x0104
49 #define TAG_VENDOR_SPECIFIC 0x0105
50 #define TAG_RELAY_SESSION_ID 0x0110
51 #define TAG_SERVICE_NAME_ERROR 0x0201
52 #define TAG_AC_SYSTEM_ERROR 0x0202
53 #define TAG_GENERIC_ERROR 0x0203
55 static char *code_pad
[] = {
75 // set up pppoe discovery socket
76 static void init_pppoe_disc(void)
80 struct sockaddr_ll sa
;
82 memset(&ifr
, 0, sizeof(ifr
));
83 memset(&sa
, 0, sizeof(sa
));
85 pppoediscfd
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_PPP_DISC
));
88 LOG(0, 0, 0, "Error pppoe: socket: %s\n", strerror(errno
));
92 fcntl(pppoediscfd
, F_SETFD
, fcntl(pppoediscfd
, F_GETFD
) | FD_CLOEXEC
);
94 if (setsockopt(pppoediscfd
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof(on
)))
96 LOG(0, 0, 0, "Error pppoe: setsockopt(SO_BROADCAST): %s\n", strerror(errno
));
100 assert(strlen(ifr
.ifr_name
) < sizeof(config
->pppoe_if_to_bind
) - 1);
101 if (*config
->pppoe_if_to_bind
)
102 strncpy(ifr
.ifr_name
, config
->pppoe_if_to_bind
, IFNAMSIZ
);
104 if (ioctl(pppoediscfd
, SIOCGIFHWADDR
, &ifr
))
106 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFHWADDR): %s\n", strerror(errno
));
110 if ((ifr
.ifr_hwaddr
.sa_data
[0] & 1) != 0)
112 LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config
->pppoe_if_to_bind
);
116 memcpy(config
->pppoe_hwaddr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
118 if (ioctl(pppoediscfd
, SIOCGIFMTU
, &ifr
))
120 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFMTU): %s\n", strerror(errno
));
124 if (ifr
.ifr_mtu
< ETH_DATA_LEN
)
125 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 if (ioctl(pppoediscfd
, SIOCGIFINDEX
, &ifr
))
129 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFINDEX): %s\n", strerror(errno
));
133 memset(&sa
, 0, sizeof(sa
));
134 sa
.sll_family
= AF_PACKET
;
135 sa
.sll_protocol
= htons(ETH_P_PPP_DISC
);
136 sa
.sll_ifindex
= ifr
.ifr_ifindex
;
138 if (bind(pppoediscfd
, (struct sockaddr
*)&sa
, sizeof(sa
)))
140 LOG(0, 0, 0, "Error pppoe: bind: %s\n", strerror(errno
));
144 if (fcntl(pppoediscfd
, F_SETFL
, O_NONBLOCK
))
146 LOG(0, 0, 0, "Error pppoe: failed to set nonblocking mode: %s\n", strerror(errno
));
152 // set up pppoe session socket
153 static void init_pppoe_sess(void)
157 struct sockaddr_ll sa
;
159 memset(&ifr
, 0, sizeof(ifr
));
160 memset(&sa
, 0, sizeof(sa
));
162 pppoesessfd
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_PPP_SES
));
165 LOG(0, 0, 0, "Error pppoe: socket: %s\n", strerror(errno
));
169 fcntl(pppoesessfd
, F_SETFD
, fcntl(pppoesessfd
, F_GETFD
) | FD_CLOEXEC
);
171 if (setsockopt(pppoesessfd
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof(on
)))
173 LOG(0, 0, 0, "Error pppoe: setsockopt(SO_BROADCAST): %s\n", strerror(errno
));
177 assert(strlen(ifr
.ifr_name
) < sizeof(config
->pppoe_if_to_bind
) - 1);
178 if (*config
->pppoe_if_to_bind
)
179 strncpy(ifr
.ifr_name
, config
->pppoe_if_to_bind
, IFNAMSIZ
);
181 if (ioctl(pppoesessfd
, SIOCGIFHWADDR
, &ifr
))
183 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFHWADDR): %s\n", strerror(errno
));
187 if ((ifr
.ifr_hwaddr
.sa_data
[0] & 1) != 0)
189 LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config
->pppoe_if_to_bind
);
193 memcpy(config
->pppoe_hwaddr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
195 if (ioctl(pppoesessfd
, SIOCGIFMTU
, &ifr
))
197 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFMTU): %s\n", strerror(errno
));
201 if (ifr
.ifr_mtu
< ETH_DATA_LEN
)
202 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 if (ioctl(pppoesessfd
, SIOCGIFINDEX
, &ifr
))
206 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFINDEX): %s\n", strerror(errno
));
210 memset(&sa
, 0, sizeof(sa
));
211 sa
.sll_family
= AF_PACKET
;
212 sa
.sll_protocol
= htons(ETH_P_PPP_SES
);
213 sa
.sll_ifindex
= ifr
.ifr_ifindex
;
215 if (bind(pppoesessfd
, (struct sockaddr
*)&sa
, sizeof(sa
)))
217 LOG(0, 0, 0, "Error pppoe: bind: %s\n", strerror(errno
));
221 if (fcntl(pppoesessfd
, F_SETFL
, O_NONBLOCK
))
223 LOG(0, 0, 0, "Error pppoe: failed to set nonblocking mode: %s\n", strerror(errno
));
228 // set up pppoe discovery/session socket
229 void init_pppoe(void)
231 tunnelidt t
= TUNNEL_ID_PPPOE
;
236 // Reserve the a pseudo tunnel for pppoe server
237 if (t
> config
->cluster_highest_tunnelid
)
238 config
->cluster_highest_tunnelid
= t
;
240 memset(&tunnel
[t
], 0, sizeof(tunnel
[t
]));
241 tunnel
[t
].state
= TUNNELOPEN
;
242 STAT(tunnel_created
);
245 char * get_string_codepad(uint8_t codepad
)
251 ptrch
= code_pad
[INDEX_PADI
];
255 ptrch
= code_pad
[INDEX_PADO
];
259 ptrch
= code_pad
[INDEX_PADR
];
263 ptrch
= code_pad
[INDEX_PADS
];
267 ptrch
= code_pad
[INDEX_PADT
];
271 ptrch
= code_pad
[INDEX_SESS
];
278 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
)
282 // 14 bytes ethernet Header + 6 bytes header pppoe
283 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
284 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
286 memcpy(ethhdr
->h_source
, src
, ETH_ALEN
);
287 memcpy(ethhdr
->h_dest
, dst
, ETH_ALEN
);
288 ethhdr
->h_proto
= htons(h_proto
);
293 hdr
->sid
= htons(sid
);
296 p
= (uint8_t *)(pack
+ ETH_HLEN
+ sizeof(*hdr
));
301 static void add_tag(uint8_t *pack
, int type
, const uint8_t *data
, int len
)
303 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
304 struct pppoe_tag
*tag
= (struct pppoe_tag
*)(pack
+ ETH_HLEN
+ sizeof(*hdr
) + ntohs(hdr
->length
));
306 tag
->tag_type
= htons(type
);
307 tag
->tag_len
= htons(len
);
308 memcpy(tag
->tag_data
, data
, len
);
310 hdr
->length
= htons(ntohs(hdr
->length
) + sizeof(*tag
) + len
);
313 static void add_tag2(uint8_t *pack
, const struct pppoe_tag
*t
)
315 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
316 struct pppoe_tag
*tag
= (struct pppoe_tag
*)(pack
+ ETH_HLEN
+ sizeof(*hdr
) + ntohs(hdr
->length
));
318 memcpy(tag
, t
, sizeof(*t
) + ntohs(t
->tag_len
));
320 hdr
->length
= htons(ntohs(hdr
->length
) + sizeof(*tag
) + ntohs(t
->tag_len
));
323 static void pppoe_disc_send(const uint8_t *pack
)
325 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
326 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
329 s
= ETH_HLEN
+ sizeof(*hdr
) + ntohs(hdr
->length
);
331 LOG(3, 0, 0, "SENT pppoe_disc: Code %s to %s\n", get_string_codepad(hdr
->code
), fmtMacAddr(ethhdr
->h_dest
));
332 LOG_HEX(5, "pppoe_disc_send", pack
, s
);
334 n
= write(pppoediscfd
, pack
, s
);
336 LOG(0, 0, 0, "pppoe: write: %s\n", strerror(errno
));
338 LOG(0, 0, 0, "pppoe: short write %i/%i\n", n
,s
);
342 void pppoe_sess_send(const uint8_t *pack
, uint16_t l
, tunnelidt t
)
344 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
349 if (t
!= TUNNEL_ID_PPPOE
)
351 LOG(3, 0, t
, "ERROR pppoe_sess_send: Tunnel %d is not a tunnel pppoe\n", t
);
356 if (session
[s
].tunnel
!= t
)
358 LOG(3, s
, t
, "ERROR pppoe_sess_send: Session is not a session pppoe\n");
362 if (l
< (ETH_HLEN
+ sizeof(*hdr
) + 3))
364 LOG(0, s
, t
, "ERROR pppoe_sess_send: packet too small for pppoe sent (size=%d)\n", l
);
368 // recalculate the ppp frame length
369 sizeppp
= l
- (ETH_HLEN
+ sizeof(*hdr
));
370 hdr
->length
= htons(sizeppp
);
372 LOG_HEX(5, "pppoe_sess_send", pack
, l
);
374 n
= write(pppoesessfd
, pack
, l
);
376 LOG(0, s
, t
, "pppoe_sess_send: write: %s\n", strerror(errno
));
378 LOG(0, s
, t
, "pppoe_sess_send: short write %i/%i\n", n
,l
);
381 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 uint8_t pack
[ETHER_MAX_LEN
];
385 setup_header(pack
, config
->pppoe_hwaddr
, addr
, code
, 0, ETH_P_PPP_DISC
);
387 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
388 add_tag(pack
, tag_type
, NULL
, 0);
391 add_tag2(pack
, host_uniq
);
394 add_tag2(pack
, relay_sid
);
396 pppoe_disc_send(pack
);
400 static void pppoe_gen_cookie(const uint8_t *serv_hwaddr
, const uint8_t *client_hwaddr
, uint8_t *out
)
405 MD5_Update(&ctx
, config
->l2tp_secret
, strlen(config
->l2tp_secret
));
406 MD5_Update(&ctx
, (void *) serv_hwaddr
, ETH_ALEN
);
407 MD5_Update(&ctx
, (void *) client_hwaddr
, ETH_ALEN
);
408 MD5_Final(out
, &ctx
);
412 static int pppoe_check_cookie(const uint8_t *serv_hwaddr
, const uint8_t *client_hwaddr
, uint8_t *cookie
)
416 pppoe_gen_cookie(serv_hwaddr
, client_hwaddr
, hash
);
418 return memcmp(hash
, cookie
, 16);
421 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 uint8_t pack
[ETHER_MAX_LEN
];
426 setup_header(pack
, config
->pppoe_hwaddr
, addr
, CODE_PADO
, 0, ETH_P_PPP_DISC
);
428 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
431 add_tag2(pack
, service_name
);
433 pppoe_gen_cookie(config
->pppoe_hwaddr
, addr
, hash
);
434 add_tag(pack
, TAG_AC_COOKIE
, hash
, 16);
437 add_tag2(pack
, host_uniq
);
440 add_tag2(pack
, relay_sid
);
442 pppoe_disc_send(pack
);
445 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 uint8_t pack
[ETHER_MAX_LEN
];
449 setup_header(pack
, config
->pppoe_hwaddr
, addr
, CODE_PADS
, sid
, ETH_P_PPP_DISC
);
451 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
453 add_tag2(pack
, service_name
);
456 add_tag2(pack
, host_uniq
);
459 add_tag2(pack
, relay_sid
);
461 pppoe_disc_send(pack
);
464 static void pppoe_send_PADT(uint16_t sid
)
466 uint8_t pack
[ETHER_MAX_LEN
];
468 setup_header(pack
, config
->pppoe_hwaddr
, session
[sid
].src_hwaddr
, CODE_PADT
, sid
, ETH_P_PPP_DISC
);
470 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
472 LOG(3, sid
, session
[sid
].tunnel
, "pppoe: Sent PADT\n");
474 pppoe_disc_send(pack
);
477 void pppoe_shutdown_session(sessionidt s
)
480 if (session
[s
].tunnel
!= TUNNEL_ID_PPPOE
)
482 LOG(3, s
, session
[s
].tunnel
, "ERROR pppoe_shutdown_session: Session is not a session pppoe\n");
489 static void pppoe_recv_PADI(uint8_t *pack
, int size
)
491 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
492 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
493 struct pppoe_tag
*tag
;
494 struct pppoe_tag
*host_uniq_tag
= NULL
;
495 struct pppoe_tag
*relay_sid_tag
= NULL
;
496 struct pppoe_tag
*service_name_tag
= NULL
;
497 int n
, service_match
= 0;
503 len
= ntohs(hdr
->length
);
504 for (n
= 0; n
< len
; n
+= sizeof(*tag
) + ntohs(tag
->tag_len
)) {
505 tag
= (struct pppoe_tag
*)(pack
+ ETH_HLEN
+ sizeof(*hdr
) + n
);
506 if (n
+ sizeof(*tag
) + ntohs(tag
->tag_len
) > len
)
508 switch (ntohs(tag
->tag_type
)) {
509 case TAG_END_OF_LIST
:
511 case TAG_SERVICE_NAME
:
512 if (*config
->pppoe_service_name
&& tag
->tag_len
)
514 if (ntohs(tag
->tag_len
) != strlen(config
->pppoe_service_name
))
516 if (memcmp(tag
->tag_data
, config
->pppoe_service_name
, ntohs(tag
->tag_len
)))
522 service_name_tag
= tag
;
529 case TAG_RELAY_SESSION_ID
:
537 LOG(3, 0, 0, "pppoe: discarding PADI packet (Service-Name mismatch)\n");
541 pppoe_send_PADO(ethhdr
->h_source
, host_uniq_tag
, relay_sid_tag
, service_name_tag
);
544 static void pppoe_recv_PADR(uint8_t *pack
, int size
)
546 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
547 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
548 struct pppoe_tag
*tag
;
549 struct pppoe_tag
*host_uniq_tag
= NULL
;
550 struct pppoe_tag
*relay_sid_tag
= NULL
;
551 struct pppoe_tag
*ac_cookie_tag
= NULL
;
552 struct pppoe_tag
*service_name_tag
= NULL
;
553 int n
, service_match
= 0;
556 if (!memcmp(ethhdr
->h_dest
, bc_addr
, ETH_ALEN
))
558 LOG(1, 0, 0, "Rcv pppoe: discard PADR (destination address is broadcast)\n");
564 LOG(1, 0, 0, "Rcv pppoe: discarding PADR packet (sid is not zero)\n");
568 for (n
= 0; n
< ntohs(hdr
->length
); n
+= sizeof(*tag
) + ntohs(tag
->tag_len
))
570 tag
= (struct pppoe_tag
*)(pack
+ ETH_HLEN
+ sizeof(*hdr
) + n
);
571 switch (ntohs(tag
->tag_type
))
573 case TAG_END_OF_LIST
:
575 case TAG_SERVICE_NAME
:
576 service_name_tag
= tag
;
577 if (tag
->tag_len
== 0)
579 else if (*config
->pppoe_service_name
)
581 if (ntohs(tag
->tag_len
) != strlen(config
->pppoe_service_name
))
583 if (memcmp(tag
->tag_data
, config
->pppoe_service_name
, ntohs(tag
->tag_len
)))
598 case TAG_RELAY_SESSION_ID
:
606 LOG(3, 0, 0, "pppoe: Service-Name mismatch\n");
607 pppoe_send_err(ethhdr
->h_source
, host_uniq_tag
, relay_sid_tag
, CODE_PADS
, TAG_SERVICE_NAME_ERROR
);
613 LOG(3, 0, 0, "pppoe: discard PADR packet (no AC-Cookie tag present)\n");
617 if (ntohs(ac_cookie_tag
->tag_len
) != 16)
619 LOG(3, 0, 0, "pppoe: discard PADR packet (incorrect AC-Cookie tag length)\n");
623 if (pppoe_check_cookie(ethhdr
->h_dest
, ethhdr
->h_source
, (uint8_t *) ac_cookie_tag
->tag_data
))
625 LOG(3, 0, 0, "pppoe: discard PADR packet (incorrect AC-Cookie)\n");
630 sessionfree
= session
[sid
].next
;
631 memset(&session
[sid
], 0, sizeof(session
[0]));
633 if (sid
> config
->cluster_highest_sessionid
)
634 config
->cluster_highest_sessionid
= sid
;
636 session
[sid
].opened
= time_now
;
637 session
[sid
].tunnel
= TUNNEL_ID_PPPOE
;
638 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
640 //strncpy(session[sid].called, called, sizeof(session[sid].called) - 1);
641 //strncpy(session[sid].calling, calling, sizeof(session[sid].calling) - 1);
643 session
[sid
].ppp
.phase
= Establish
;
644 session
[sid
].ppp
.lcp
= Starting
;
646 session
[sid
].magic
= time_now
; // set magic number
647 session
[sid
].mru
= PPPoE_MRU
; // default
650 sess_local
[sid
].lcp_authtype
= config
->radius_authprefer
;
651 sess_local
[sid
].ppp_mru
= MRU
;
653 // Set multilink options before sending initial LCP packet
654 sess_local
[sid
].mp_mrru
= 1614;
655 sess_local
[sid
].mp_epdis
= ntohl(config
->iftun_address
? config
->iftun_address
: my_address
);
657 memcpy(session
[sid
].src_hwaddr
, ethhdr
->h_source
, ETH_ALEN
);
658 pppoe_send_PADS(sid
, ethhdr
->h_source
, host_uniq_tag
, relay_sid_tag
, service_name_tag
);
660 sendlcp(sid
, session
[sid
].tunnel
);
661 change_state(sid
, lcp
, RequestSent
);
665 static void pppoe_recv_PADT(uint8_t *pack
)
667 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
668 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
670 if (!memcmp(ethhdr
->h_dest
, bc_addr
, ETH_ALEN
))
672 LOG(3, 0, 0, "pppoe: discard PADT (destination address is broadcast)\n");
678 if ((hdr
->sid
< MAXSESSION
) && (session
[hdr
->sid
].tunnel
== TUNNEL_ID_PPPOE
))
679 sessionshutdown(hdr
->sid
, "Client shutdown", CDN_ADMIN_DISC
, 0);
683 // fill in a PPPOE message with a PPP frame,
684 // returns start of PPP frame
685 uint8_t *pppoe_makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
,
686 uint16_t mtype
, uint8_t prio
, bundleidt bid
, uint8_t mp_bits
)
688 uint16_t type
= mtype
;
690 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(b
+ ETH_HLEN
);
692 if (t
!= TUNNEL_ID_PPPOE
)
695 if (size
< 28) // Need more space than this!!
697 LOG(0, s
, t
, "pppoe_makeppp buffer too small for pppoe header (size=%d)\n", size
);
701 // 14 bytes ethernet Header + 6 bytes header pppoe
702 b
= setup_header(b
, config
->pppoe_hwaddr
, session
[s
].src_hwaddr
, CODE_SESS
, s
, ETH_P_PPP_SES
);
704 // Check whether this session is part of multilink
707 if (bundle
[bid
].num_of_links
> 1)
708 type
= PPPMP
; // Change PPP message type to the PPPMP
713 *(uint16_t *) b
= htons(type
);
719 // Set the sequence number and (B)egin (E)nd flags
722 // Set the multilink bits
723 uint16_t bits_send
= mp_bits
;
724 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
730 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
731 // Set the multilink bits
737 bundle
[bid
].seq_num_t
++;
739 // Add the message type if this fragment has the begin bit set
740 if (mp_bits
& MP_BEGIN
)
742 //*b++ = mtype; // The next two lines are instead of this
743 *(uint16_t *) b
= htons(mtype
); // Message type
749 if ((b
- start
) + l
> size
)
751 LOG(0, s
, t
, "pppoe_makeppp would overflow buffer (size=%d, header+payload=%td)\n", size
, (b
- start
) + l
);
765 // pppoe discovery recv data
766 void process_pppoe_disc(uint8_t *pack
, int size
)
768 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
769 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
771 LOG(3, 0, 0, "RCV pppoe_disc: Code %s from %s\n", get_string_codepad(hdr
->code
), fmtMacAddr(ethhdr
->h_source
));
772 LOG_HEX(5, "PPPOE Disc", pack
, size
);
774 if (!config
->cluster_iam_master
)
776 if (hdr
->code
== CODE_PADI
)
777 return; // Discard because the PADI is received by all (PADI is a broadcast diffusion)
779 master_forward_pppoe_packet(pack
, size
, hdr
->code
);
783 if (size
< (ETH_HLEN
+ sizeof(*hdr
)))
785 LOG(1, 0, 0, "Error pppoe_disc: short packet received (%i)\n", size
);
789 if (memcmp(ethhdr
->h_dest
, bc_addr
, ETH_ALEN
) && memcmp(ethhdr
->h_dest
, config
->pppoe_hwaddr
, ETH_ALEN
))
791 LOG(1, 0, 0, "Error pppoe_disc: h_dest != bc_addr and h_dest != config->pppoe_hwaddr\n");
795 if (!memcmp(ethhdr
->h_source
, bc_addr
, ETH_ALEN
))
797 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (source address is broadcast)\n");
801 if ((ethhdr
->h_source
[0] & 1) != 0)
803 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (host address is not unicast)\n");
807 if (size
< ETH_HLEN
+ sizeof(*hdr
) + ntohs(hdr
->length
))
809 LOG(1, 0, 0, "Error pppoe_disc: short packet received\n");
815 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (unsupported version %i)\n", hdr
->ver
);
821 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (unsupported type %i)\n", hdr
->type
);
827 pppoe_recv_PADI(pack
, size
);
830 pppoe_recv_PADR(pack
, size
);
833 pppoe_recv_PADT(pack
);
839 // Forward from pppoe to l2tp remote LNS
840 static void pppoe_forwardto_session_rmlns(uint8_t *pack
, int size
, sessionidt sess
, uint16_t proto
)
842 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
843 uint16_t lppp
= ntohs(hdr
->length
);
844 uint16_t ll2tp
= lppp
+ 6;
845 uint8_t *pppdata
= (uint8_t *) hdr
->tag
;
846 uint8_t *pl2tp
= pppdata
- 6;
848 uint16_t t
= 0, s
= 0;
850 s
= session
[sess
].forwardtosession
;
851 if (session
[s
].forwardtosession
!= sess
)
853 LOG(3, sess
, session
[sess
].tunnel
, "pppoe: Link Session (%u) broken\n", s
);
857 t
= session
[s
].tunnel
;
860 LOG(1, s
, t
, "pppoe: Session with invalid tunnel ID\n");
864 if (!tunnel
[t
].isremotelns
)
866 LOG(3, sess
, session
[sess
].tunnel
, "pppoe: Link Tunnel/Session (%u/%u) broken\n", s
, t
);
870 // First word L2TP options (with no options)
871 *(uint16_t *) p
= htons(0x0002);
873 *(uint16_t *) p
= htons(tunnel
[t
].far
); // tunnel
875 *(uint16_t *) p
= htons(session
[s
].far
); // session
878 if ((proto
== PPPIP
) || (proto
== PPPMP
) ||(proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0]))
880 session
[sess
].last_packet
= session
[sess
].last_data
= time_now
;
882 increment_counter(&session
[sess
].cin
, &session
[sess
].cin_wrap
, ll2tp
);
883 session
[sess
].cin_delta
+= ll2tp
;
885 sess_local
[sess
].cin
+= ll2tp
;
886 sess_local
[sess
].pin
++;
888 session
[s
].last_data
= time_now
;
890 increment_counter(&session
[s
].cout
, &session
[s
].cout_wrap
, ll2tp
); // byte count
891 session
[s
].cout_delta
+= ll2tp
;
893 sess_local
[s
].cout
+= ll2tp
;
894 sess_local
[s
].pout
++;
897 session
[sess
].last_packet
= time_now
;
899 tunnelsend(pl2tp
, ll2tp
, t
); // send it...
902 // Forward from l2tp to pppoe
903 // (note: THIS ROUTINE WRITES TO pack[-20]).
904 void pppoe_forwardto_session_pppoe(uint8_t *pack
, int size
, sessionidt sess
, uint16_t proto
)
906 uint16_t t
= 0, s
= 0;
907 uint16_t lpppoe
= size
- 2;
908 uint8_t *p
= pack
+ 2; // First word L2TP options
910 LOG(5, sess
, session
[sess
].tunnel
, "Forwarding data session to pppoe session %u\n", session
[sess
].forwardtosession
);
912 s
= session
[sess
].forwardtosession
;
913 t
= session
[s
].tunnel
;
921 *(uint16_t *) p
= htons(tunnel
[t
].far
); // tunnel
923 *(uint16_t *) p
= htons(session
[s
].far
); // session
929 *(uint16_t *) p
= htons(tunnel
[t
].ns
); // sequence
931 *(uint16_t *) p
= htons(tunnel
[t
].nr
); // sequence
936 if (lpppoe
> 2 && p
[0] == 0xFF && p
[1] == 0x03)
938 // HDLC address header, discard in pppoe
943 lpppoe
+= (ETH_HLEN
+ sizeof(struct pppoe_hdr
));
944 p
-= (ETH_HLEN
+ sizeof(struct pppoe_hdr
));
946 // 14 bytes ethernet Header + 6 bytes header pppoe
947 setup_header(p
, config
->pppoe_hwaddr
, session
[s
].src_hwaddr
, CODE_SESS
, s
, ETH_P_PPP_SES
);
949 if ((proto
== PPPIP
) || (proto
== PPPMP
) ||(proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0]))
951 session
[sess
].last_packet
= session
[sess
].last_data
= time_now
;
953 increment_counter(&session
[sess
].cin
, &session
[sess
].cin_wrap
, lpppoe
);
954 session
[sess
].cin_delta
+= lpppoe
;
956 sess_local
[sess
].cin
+= lpppoe
;
957 sess_local
[sess
].pin
++;
959 session
[s
].last_data
= time_now
;
961 increment_counter(&session
[s
].cout
, &session
[s
].cout_wrap
, lpppoe
); // byte count
962 session
[s
].cout_delta
+= lpppoe
;
964 sess_local
[s
].cout
+= lpppoe
;
965 sess_local
[s
].pout
++;
968 session
[sess
].last_packet
= time_now
;
970 tunnelsend(p
, lpppoe
, t
); // send it....
974 void process_pppoe_sess(uint8_t *pack
, int size
)
976 //struct ethhdr *ethhdr = (struct ethhdr *)pack;
977 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
978 uint16_t lppp
= ntohs(hdr
->length
);
979 uint8_t *pppdata
= (uint8_t *) hdr
->tag
;
980 uint16_t proto
, sid
, t
;
982 sid
= ntohs(hdr
->sid
);
985 LOG_HEX(5, "RCV PPPOE Sess", pack
, size
);
987 if (sid
>= MAXSESSION
)
989 LOG(0, sid
, t
, "Received pppoe packet with invalid session ID\n");
990 STAT(tunnel_rx_errors
);
994 if (session
[sid
].tunnel
!= t
)
996 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
998 LOG(1, sid
, t
, "ERROR process_pppoe_sess: Session is not a session pppoe\n");
1004 LOG(3, sid
, t
, "Error process_pppoe_sess: discarding packet (unsupported version %i)\n", hdr
->ver
);
1010 LOG(3, sid
, t
, "Error process_pppoe_sess: discarding packet (unsupported type %i)\n", hdr
->type
);
1014 if (lppp
> 2 && pppdata
[0] == 0xFF && pppdata
[1] == 0x03)
1015 { // HDLC address header, discard
1016 LOG(5, sid
, t
, "pppoe_sess: HDLC address header, discard\n");
1022 LOG(3, sid
, t
, "Error process_pppoe_sess: Short ppp length %d\n", lppp
);
1032 proto
= ntohs(*(uint16_t *) pppdata
);
1038 if (session
[sid
].forwardtosession
)
1039 { // Must be forwaded to a remote lns tunnel l2tp
1040 pppoe_forwardto_session_rmlns(pack
, size
, sid
, proto
);
1045 if (proto
== PPPPAP
)
1047 session
[sid
].last_packet
= time_now
;
1048 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1049 processpap(sid
, t
, pppdata
, lppp
);
1051 else if (proto
== PPPCHAP
)
1053 session
[sid
].last_packet
= time_now
;
1054 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1055 processchap(sid
, t
, pppdata
, lppp
);
1057 else if (proto
== PPPLCP
)
1059 session
[sid
].last_packet
= time_now
;
1060 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1061 processlcp(sid
, t
, pppdata
, lppp
);
1063 else if (proto
== PPPIPCP
)
1065 session
[sid
].last_packet
= time_now
;
1066 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1067 processipcp(sid
, t
, pppdata
, lppp
);
1069 else if (proto
== PPPIPV6CP
&& config
->ipv6_prefix
.s6_addr
[0])
1071 session
[sid
].last_packet
= time_now
;
1072 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1073 processipv6cp(sid
, t
, pppdata
, lppp
);
1075 else if (proto
== PPPCCP
)
1077 session
[sid
].last_packet
= time_now
;
1078 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1079 processccp(sid
, t
, pppdata
, lppp
);
1081 else if (proto
== PPPIP
)
1083 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
1084 if (session
[sid
].walled_garden
&& !config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1085 processipin(sid
, t
, pppdata
, lppp
);
1087 else if (proto
== PPPMP
)
1089 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
1090 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1091 processmpin(sid
, t
, pppdata
, lppp
);
1093 else if (proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0])
1095 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
1096 if (session
[sid
].walled_garden
&& !config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1097 processipv6in(sid
, t
, pppdata
, lppp
);
1099 else if (session
[sid
].ppp
.lcp
== Opened
)
1101 session
[sid
].last_packet
= time_now
;
1102 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1103 protoreject(sid
, t
, pppdata
, lppp
, proto
);
1107 LOG(3, sid
, t
, "process_pppoe_sess: Unknown PPP protocol 0x%04X received in LCP %s state\n",
1108 proto
, ppp_state(session
[sid
].ppp
.lcp
));
1112 void pppoe_send_garp()
1118 if (!*config
->pppoe_if_to_bind
)
1121 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
1124 LOG(0, 0, 0, "Error creating socket for GARP: %s\n", strerror(errno
));
1127 memset(&ifr
, 0, sizeof(ifr
));
1128 strncpy(ifr
.ifr_name
, config
->pppoe_if_to_bind
, sizeof(ifr
.ifr_name
) - 1);
1129 if (ioctl(s
, SIOCGIFHWADDR
, &ifr
) < 0)
1131 LOG(0, 0, 0, "Error getting eth0 hardware address for GARP: %s\n", strerror(errno
));
1135 memcpy(mac
, &ifr
.ifr_hwaddr
.sa_data
, 6*sizeof(char));
1136 if (ioctl(s
, SIOCGIFINDEX
, &ifr
) < 0)
1138 LOG(0, 0, 0, "Error getting eth0 interface index for GARP: %s\n", strerror(errno
));
1144 sendarp(ifr
.ifr_ifindex
, mac
, config
->iftun_address
);
1147 // rcv pppoe data from slave
1148 void pppoe_process_forward(uint8_t *pack
, int size
, in_addr_t addr
)
1150 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
1152 if (ethhdr
->h_proto
== htons(ETH_P_PPP_DISC
))
1153 process_pppoe_disc(pack
, size
);
1154 else if (ethhdr
->h_proto
== htons(ETH_P_PPP_SES
))
1155 process_pppoe_sess(pack
, size
);
1157 LOG(0, 0, 0, "pppoe_process_forward: I got a C_PPPOE_FORWARD from %s, but not a PPPOE data?\n", fmtaddr(addr
, 0));