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>
24 #include <linux/rtnetlink.h>
29 #include "constants.h"
36 static uint8_t bc_addr
[ETH_ALEN
] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
39 #define CODE_PADI 0x09
40 #define CODE_PADO 0x07
41 #define CODE_PADR 0x19
42 #define CODE_PADS 0x65
43 #define CODE_PADT 0xA7
44 #define CODE_SESS 0x00
47 #define TAG_END_OF_LIST 0x0000
48 #define TAG_SERVICE_NAME 0x0101
49 #define TAG_AC_NAME 0x0102
50 #define TAG_HOST_UNIQ 0x0103
51 #define TAG_AC_COOKIE 0x0104
52 #define TAG_VENDOR_SPECIFIC 0x0105
53 #define TAG_RELAY_SESSION_ID 0x0110
54 #define TAG_SERVICE_NAME_ERROR 0x0201
55 #define TAG_AC_SYSTEM_ERROR 0x0202
56 #define TAG_GENERIC_ERROR 0x0203
58 static char *code_pad
[] = {
78 // set up pppoe discovery socket
79 static void init_pppoe_disc(void)
83 struct sockaddr_ll sa
;
85 memset(&ifr
, 0, sizeof(ifr
));
86 memset(&sa
, 0, sizeof(sa
));
88 pppoediscfd
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_PPP_DISC
));
91 LOG(0, 0, 0, "Error pppoe: socket: %s\n", strerror(errno
));
95 fcntl(pppoediscfd
, F_SETFD
, fcntl(pppoediscfd
, F_GETFD
) | FD_CLOEXEC
);
97 if (setsockopt(pppoediscfd
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof(on
)))
99 LOG(0, 0, 0, "Error pppoe: setsockopt(SO_BROADCAST): %s\n", strerror(errno
));
103 assert(strlen(ifr
.ifr_name
) < sizeof(config
->pppoe_if_to_bind
) - 1);
104 if (*config
->pppoe_if_to_bind
)
105 strncpy(ifr
.ifr_name
, config
->pppoe_if_to_bind
, IFNAMSIZ
);
107 if (ioctl(pppoediscfd
, SIOCGIFHWADDR
, &ifr
))
109 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFHWADDR): %s\n", strerror(errno
));
113 if ((ifr
.ifr_hwaddr
.sa_data
[0] & 1) != 0)
115 LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config
->pppoe_if_to_bind
);
119 memcpy(config
->pppoe_hwaddr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
121 if (ioctl(pppoediscfd
, SIOCGIFMTU
, &ifr
))
123 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFMTU): %s\n", strerror(errno
));
127 if (ifr
.ifr_mtu
< ETH_DATA_LEN
)
128 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
);
130 if (ioctl(pppoediscfd
, SIOCGIFINDEX
, &ifr
))
132 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFINDEX): %s\n", strerror(errno
));
136 memset(&sa
, 0, sizeof(sa
));
137 sa
.sll_family
= AF_PACKET
;
138 sa
.sll_protocol
= htons(ETH_P_PPP_DISC
);
139 sa
.sll_ifindex
= ifr
.ifr_ifindex
;
141 if (bind(pppoediscfd
, (struct sockaddr
*)&sa
, sizeof(sa
)))
143 LOG(0, 0, 0, "Error pppoe: bind: %s\n", strerror(errno
));
147 if (fcntl(pppoediscfd
, F_SETFL
, O_NONBLOCK
))
149 LOG(0, 0, 0, "Error pppoe: failed to set nonblocking mode: %s\n", strerror(errno
));
155 // set up pppoe session socket
156 static void init_pppoe_sess(void)
160 struct sockaddr_ll sa
;
162 memset(&ifr
, 0, sizeof(ifr
));
163 memset(&sa
, 0, sizeof(sa
));
165 pppoesessfd
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_PPP_SES
));
168 LOG(0, 0, 0, "Error pppoe: socket: %s\n", strerror(errno
));
172 fcntl(pppoesessfd
, F_SETFD
, fcntl(pppoesessfd
, F_GETFD
) | FD_CLOEXEC
);
174 if (setsockopt(pppoesessfd
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof(on
)))
176 LOG(0, 0, 0, "Error pppoe: setsockopt(SO_BROADCAST): %s\n", strerror(errno
));
180 assert(strlen(ifr
.ifr_name
) < sizeof(config
->pppoe_if_to_bind
) - 1);
181 if (*config
->pppoe_if_to_bind
)
182 strncpy(ifr
.ifr_name
, config
->pppoe_if_to_bind
, IFNAMSIZ
);
184 if (ioctl(pppoesessfd
, SIOCGIFHWADDR
, &ifr
))
186 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFHWADDR): %s\n", strerror(errno
));
190 if ((ifr
.ifr_hwaddr
.sa_data
[0] & 1) != 0)
192 LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config
->pppoe_if_to_bind
);
196 memcpy(config
->pppoe_hwaddr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
198 if (ioctl(pppoesessfd
, SIOCGIFMTU
, &ifr
))
200 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFMTU): %s\n", strerror(errno
));
204 if (ifr
.ifr_mtu
< ETH_DATA_LEN
)
205 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
);
207 if (ioctl(pppoesessfd
, SIOCGIFINDEX
, &ifr
))
209 LOG(0, 0, 0, "Error pppoe: ioctl(SIOCGIFINDEX): %s\n", strerror(errno
));
213 memset(&sa
, 0, sizeof(sa
));
214 sa
.sll_family
= AF_PACKET
;
215 sa
.sll_protocol
= htons(ETH_P_PPP_SES
);
216 sa
.sll_ifindex
= ifr
.ifr_ifindex
;
218 if (bind(pppoesessfd
, (struct sockaddr
*)&sa
, sizeof(sa
)))
220 LOG(0, 0, 0, "Error pppoe: bind: %s\n", strerror(errno
));
224 if (fcntl(pppoesessfd
, F_SETFL
, O_NONBLOCK
))
226 LOG(0, 0, 0, "Error pppoe: failed to set nonblocking mode: %s\n", strerror(errno
));
231 // set up pppoe discovery/session socket
232 void init_pppoe(void)
234 tunnelidt t
= TUNNEL_ID_PPPOE
;
239 // Reserve the a pseudo tunnel for pppoe server
240 if (t
> config
->cluster_highest_tunnelid
)
241 config
->cluster_highest_tunnelid
= t
;
243 memset(&tunnel
[t
], 0, sizeof(tunnel
[t
]));
244 tunnel
[t
].state
= TUNNELOPEN
;
245 STAT(tunnel_created
);
248 char * get_string_codepad(uint8_t codepad
)
254 ptrch
= code_pad
[INDEX_PADI
];
258 ptrch
= code_pad
[INDEX_PADO
];
262 ptrch
= code_pad
[INDEX_PADR
];
266 ptrch
= code_pad
[INDEX_PADS
];
270 ptrch
= code_pad
[INDEX_PADT
];
274 ptrch
= code_pad
[INDEX_SESS
];
281 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
)
285 // 14 bytes ethernet Header + 6 bytes header pppoe
286 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
287 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
289 memcpy(ethhdr
->h_source
, src
, ETH_ALEN
);
290 memcpy(ethhdr
->h_dest
, dst
, ETH_ALEN
);
291 ethhdr
->h_proto
= htons(h_proto
);
296 hdr
->sid
= htons(sid
);
299 p
= (uint8_t *)(pack
+ ETH_HLEN
+ sizeof(*hdr
));
304 static void add_tag(uint8_t *pack
, int type
, const uint8_t *data
, int len
)
306 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
307 struct pppoe_tag
*tag
= (struct pppoe_tag
*)(pack
+ ETH_HLEN
+ sizeof(*hdr
) + ntohs(hdr
->length
));
309 tag
->tag_type
= htons(type
);
310 tag
->tag_len
= htons(len
);
311 memcpy(tag
->tag_data
, data
, len
);
313 hdr
->length
= htons(ntohs(hdr
->length
) + sizeof(*tag
) + len
);
316 static void add_tag2(uint8_t *pack
, const struct pppoe_tag
*t
)
318 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
319 struct pppoe_tag
*tag
= (struct pppoe_tag
*)(pack
+ ETH_HLEN
+ sizeof(*hdr
) + ntohs(hdr
->length
));
321 memcpy(tag
, t
, sizeof(*t
) + ntohs(t
->tag_len
));
323 hdr
->length
= htons(ntohs(hdr
->length
) + sizeof(*tag
) + ntohs(t
->tag_len
));
326 static void pppoe_disc_send(const uint8_t *pack
)
328 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
329 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
332 s
= ETH_HLEN
+ sizeof(*hdr
) + ntohs(hdr
->length
);
334 LOG(3, 0, 0, "SENT pppoe_disc: Code %s to %s\n", get_string_codepad(hdr
->code
), fmtMacAddr(ethhdr
->h_dest
));
335 LOG_HEX(5, "pppoe_disc_send", pack
, s
);
337 n
= write(pppoediscfd
, pack
, s
);
339 LOG(0, 0, 0, "pppoe: write: %s\n", strerror(errno
));
341 LOG(0, 0, 0, "pppoe: short write %i/%i\n", n
,s
);
345 void pppoe_sess_send(const uint8_t *pack
, uint16_t l
, tunnelidt t
)
347 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
352 if (t
!= TUNNEL_ID_PPPOE
)
354 LOG(3, 0, t
, "ERROR pppoe_sess_send: Tunnel %d is not a tunnel pppoe\n", t
);
359 if (session
[s
].tunnel
!= t
)
361 LOG(3, s
, t
, "ERROR pppoe_sess_send: Session is not a session pppoe\n");
365 if (l
< (ETH_HLEN
+ sizeof(*hdr
) + 3))
367 LOG(0, s
, t
, "ERROR pppoe_sess_send: packet too small for pppoe sent (size=%d)\n", l
);
371 // recalculate the ppp frame length
372 sizeppp
= l
- (ETH_HLEN
+ sizeof(*hdr
));
373 hdr
->length
= htons(sizeppp
);
375 LOG_HEX(5, "pppoe_sess_send", pack
, l
);
377 n
= write(pppoesessfd
, pack
, l
);
379 LOG(0, s
, t
, "pppoe_sess_send: write: %s\n", strerror(errno
));
381 LOG(0, s
, t
, "pppoe_sess_send: short write %i/%i\n", n
,l
);
384 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
)
386 uint8_t pack
[ETHER_MAX_LEN
];
388 setup_header(pack
, config
->pppoe_hwaddr
, addr
, code
, 0, ETH_P_PPP_DISC
);
390 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
391 add_tag(pack
, tag_type
, NULL
, 0);
394 add_tag2(pack
, host_uniq
);
397 add_tag2(pack
, relay_sid
);
399 pppoe_disc_send(pack
);
403 static void pppoe_gen_cookie(const uint8_t *serv_hwaddr
, const uint8_t *client_hwaddr
, uint8_t *out
)
408 MD5_Update(&ctx
, config
->l2tp_secret
, strlen(config
->l2tp_secret
));
409 MD5_Update(&ctx
, (void *) serv_hwaddr
, ETH_ALEN
);
410 MD5_Update(&ctx
, (void *) client_hwaddr
, ETH_ALEN
);
411 MD5_Final(out
, &ctx
);
415 static int pppoe_check_cookie(const uint8_t *serv_hwaddr
, const uint8_t *client_hwaddr
, uint8_t *cookie
)
419 pppoe_gen_cookie(serv_hwaddr
, client_hwaddr
, hash
);
421 return memcmp(hash
, cookie
, 16);
424 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
)
426 uint8_t pack
[ETHER_MAX_LEN
];
429 setup_header(pack
, config
->pppoe_hwaddr
, addr
, CODE_PADO
, 0, ETH_P_PPP_DISC
);
431 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
434 add_tag2(pack
, service_name
);
436 pppoe_gen_cookie(config
->pppoe_hwaddr
, addr
, hash
);
437 add_tag(pack
, TAG_AC_COOKIE
, hash
, 16);
440 add_tag2(pack
, host_uniq
);
443 add_tag2(pack
, relay_sid
);
445 pppoe_disc_send(pack
);
448 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
)
450 uint8_t pack
[ETHER_MAX_LEN
];
452 setup_header(pack
, config
->pppoe_hwaddr
, addr
, CODE_PADS
, sid
, ETH_P_PPP_DISC
);
454 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
456 add_tag2(pack
, service_name
);
459 add_tag2(pack
, host_uniq
);
462 add_tag2(pack
, relay_sid
);
464 pppoe_disc_send(pack
);
467 static void pppoe_send_PADT(uint16_t sid
)
469 uint8_t pack
[ETHER_MAX_LEN
];
471 setup_header(pack
, config
->pppoe_hwaddr
, session
[sid
].src_hwaddr
, CODE_PADT
, sid
, ETH_P_PPP_DISC
);
473 add_tag(pack
, TAG_AC_NAME
, (uint8_t *)config
->pppoe_ac_name
, strlen(config
->pppoe_ac_name
));
475 LOG(3, sid
, session
[sid
].tunnel
, "pppoe: Sent PADT\n");
477 pppoe_disc_send(pack
);
480 void pppoe_shutdown_session(sessionidt s
)
483 if (session
[s
].tunnel
!= TUNNEL_ID_PPPOE
)
485 LOG(3, s
, session
[s
].tunnel
, "ERROR pppoe_shutdown_session: Session is not a session pppoe\n");
492 static void pppoe_recv_PADI(uint8_t *pack
, int size
)
494 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
495 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
496 struct pppoe_tag
*tag
;
497 struct pppoe_tag
*host_uniq_tag
= NULL
;
498 struct pppoe_tag
*relay_sid_tag
= NULL
;
499 struct pppoe_tag
*service_name_tag
= NULL
;
500 int n
, service_match
= 0;
506 len
= ntohs(hdr
->length
);
507 for (n
= 0; n
< len
; n
+= sizeof(*tag
) + ntohs(tag
->tag_len
))
509 tag
= (struct pppoe_tag
*)(pack
+ ETH_HLEN
+ sizeof(*hdr
) + n
);
510 if (n
+ sizeof(*tag
) + ntohs(tag
->tag_len
) > len
)
512 switch (ntohs(tag
->tag_type
))
514 case TAG_END_OF_LIST
:
516 case TAG_SERVICE_NAME
:
517 if (config
->pppoe_only_equal_svc_name
&& *config
->pppoe_service_name
&& !tag
->tag_len
)
521 else if (*config
->pppoe_service_name
&& tag
->tag_len
)
523 if (ntohs(tag
->tag_len
) != strlen(config
->pppoe_service_name
))
525 if (memcmp(tag
->tag_data
, config
->pppoe_service_name
, ntohs(tag
->tag_len
)))
527 service_name_tag
= tag
;
532 service_name_tag
= tag
;
539 case TAG_RELAY_SESSION_ID
:
547 LOG(3, 0, 0, "pppoe: discarding PADI packet (Service-Name mismatch)\n");
551 pppoe_send_PADO(ethhdr
->h_source
, host_uniq_tag
, relay_sid_tag
, service_name_tag
);
554 static void pppoe_recv_PADR(uint8_t *pack
, int size
)
556 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
557 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
558 struct pppoe_tag
*tag
;
559 struct pppoe_tag
*host_uniq_tag
= NULL
;
560 struct pppoe_tag
*relay_sid_tag
= NULL
;
561 struct pppoe_tag
*ac_cookie_tag
= NULL
;
562 struct pppoe_tag
*service_name_tag
= NULL
;
563 int n
, service_match
= 0;
566 if (!memcmp(ethhdr
->h_dest
, bc_addr
, ETH_ALEN
))
568 LOG(1, 0, 0, "Rcv pppoe: discard PADR (destination address is broadcast)\n");
574 LOG(1, 0, 0, "Rcv pppoe: discarding PADR packet (sid is not zero)\n");
578 for (n
= 0; n
< ntohs(hdr
->length
); n
+= sizeof(*tag
) + ntohs(tag
->tag_len
))
580 tag
= (struct pppoe_tag
*)(pack
+ ETH_HLEN
+ sizeof(*hdr
) + n
);
581 switch (ntohs(tag
->tag_type
))
583 case TAG_END_OF_LIST
:
585 case TAG_SERVICE_NAME
:
586 service_name_tag
= tag
;
587 if (tag
->tag_len
== 0)
589 else if (*config
->pppoe_service_name
)
591 if (ntohs(tag
->tag_len
) != strlen(config
->pppoe_service_name
))
593 if (memcmp(tag
->tag_data
, config
->pppoe_service_name
, ntohs(tag
->tag_len
)))
608 case TAG_RELAY_SESSION_ID
:
616 LOG(3, 0, 0, "pppoe: Service-Name mismatch\n");
617 pppoe_send_err(ethhdr
->h_source
, host_uniq_tag
, relay_sid_tag
, CODE_PADS
, TAG_SERVICE_NAME_ERROR
);
623 LOG(3, 0, 0, "pppoe: discard PADR packet (no AC-Cookie tag present)\n");
627 if (ntohs(ac_cookie_tag
->tag_len
) != 16)
629 LOG(3, 0, 0, "pppoe: discard PADR packet (incorrect AC-Cookie tag length)\n");
633 if (pppoe_check_cookie(ethhdr
->h_dest
, ethhdr
->h_source
, (uint8_t *) ac_cookie_tag
->tag_data
))
635 LOG(3, 0, 0, "pppoe: discard PADR packet (incorrect AC-Cookie)\n");
640 sessionfree
= session
[sid
].next
;
641 memset(&session
[sid
], 0, sizeof(session
[0]));
643 if (sid
> config
->cluster_highest_sessionid
)
644 config
->cluster_highest_sessionid
= sid
;
646 session
[sid
].opened
= time_now
;
647 session
[sid
].tunnel
= TUNNEL_ID_PPPOE
;
648 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
650 //strncpy(session[sid].called, called, sizeof(session[sid].called) - 1);
651 //strncpy(session[sid].calling, calling, sizeof(session[sid].calling) - 1);
653 session
[sid
].ppp
.phase
= Establish
;
654 session
[sid
].ppp
.lcp
= Starting
;
656 session
[sid
].magic
= time_now
; // set magic number
657 session
[sid
].mru
= PPPoE_MRU
; // default
660 sess_local
[sid
].lcp_authtype
= config
->radius_authprefer
;
661 sess_local
[sid
].ppp_mru
= MRU
;
663 // Set multilink options before sending initial LCP packet
664 sess_local
[sid
].mp_mrru
= 1614;
665 sess_local
[sid
].mp_epdis
= ntohl(config
->iftun_address
? config
->iftun_address
: my_address
);
667 memcpy(session
[sid
].src_hwaddr
, ethhdr
->h_source
, ETH_ALEN
);
668 pppoe_send_PADS(sid
, ethhdr
->h_source
, host_uniq_tag
, relay_sid_tag
, service_name_tag
);
670 sendlcp(sid
, session
[sid
].tunnel
);
671 change_state(sid
, lcp
, RequestSent
);
675 static void pppoe_recv_PADT(uint8_t *pack
)
677 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
678 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
680 if (!memcmp(ethhdr
->h_dest
, bc_addr
, ETH_ALEN
))
682 LOG(3, 0, 0, "pppoe: discard PADT (destination address is broadcast)\n");
688 if ((hdr
->sid
< MAXSESSION
) && (session
[hdr
->sid
].tunnel
== TUNNEL_ID_PPPOE
))
689 sessionshutdown(hdr
->sid
, "Client shutdown", CDN_ADMIN_DISC
, 0);
693 // fill in a PPPOE message with a PPP frame,
694 // returns start of PPP frame
695 uint8_t *pppoe_makeppp(uint8_t *b
, int size
, uint8_t *p
, int l
, sessionidt s
, tunnelidt t
,
696 uint16_t mtype
, uint8_t prio
, bundleidt bid
, uint8_t mp_bits
)
698 uint16_t type
= mtype
;
700 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(b
+ ETH_HLEN
);
702 if (t
!= TUNNEL_ID_PPPOE
)
705 if (size
< 28) // Need more space than this!!
707 LOG(0, s
, t
, "pppoe_makeppp buffer too small for pppoe header (size=%d)\n", size
);
711 // 14 bytes ethernet Header + 6 bytes header pppoe
712 b
= setup_header(b
, config
->pppoe_hwaddr
, session
[s
].src_hwaddr
, CODE_SESS
, s
, ETH_P_PPP_SES
);
714 // Check whether this session is part of multilink
717 if (bundle
[bid
].num_of_links
> 1)
718 type
= PPPMP
; // Change PPP message type to the PPPMP
723 *(uint16_t *) b
= htons(type
);
729 // Set the sequence number and (B)egin (E)nd flags
732 // Set the multilink bits
733 uint16_t bits_send
= mp_bits
;
734 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
740 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
741 // Set the multilink bits
747 bundle
[bid
].seq_num_t
++;
749 // Add the message type if this fragment has the begin bit set
750 if (mp_bits
& MP_BEGIN
)
752 //*b++ = mtype; // The next two lines are instead of this
753 *(uint16_t *) b
= htons(mtype
); // Message type
759 if ((b
- start
) + l
> size
)
761 LOG(0, s
, t
, "pppoe_makeppp would overflow buffer (size=%d, header+payload=%td)\n", size
, (b
- start
) + l
);
775 // fill in a PPPOE message with a PPP frame,
776 // returns start of PPP frame
777 //(note: THIS ROUTINE WRITES TO p[-28]).
778 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
)
780 uint16_t type
= mtype
;
783 struct pppoe_hdr
*hdr
;
785 if (t
!= TUNNEL_ID_PPPOE
)
788 // Check whether this session is part of multilink
791 if (bundle
[bid
].num_of_links
> 1)
792 type
= PPPMP
; // Change PPP message type to the PPPMP
799 // Add the message type if this fragment has the begin bit set
800 if (mp_bits
& MP_BEGIN
)
803 *(uint16_t *) b
= htons(mtype
); // Message type
806 // Set the sequence number and (B)egin (E)nd flags
809 // Set the multilink bits
810 uint16_t bits_send
= mp_bits
;
812 *(uint16_t *) b
= htons((bundle
[bid
].seq_num_t
& 0x0FFF)|bits_send
);
817 *(uint32_t *) b
= htonl(bundle
[bid
].seq_num_t
);
818 // Set the multilink bits
822 bundle
[bid
].seq_num_t
++;
826 *(uint16_t *) b
= htons(type
);
831 // 14 bytes ethernet Header + 6 bytes header pppoe
832 b
-= (ETH_HLEN
+ sizeof(*hdr
));
833 setup_header(b
, config
->pppoe_hwaddr
, session
[s
].src_hwaddr
, CODE_SESS
, s
, ETH_P_PPP_SES
);
834 hdr
= (struct pppoe_hdr
*)(b
+ ETH_HLEN
);
835 // Store length on header pppoe
836 hdr
->length
= hdrlen
;
841 // pppoe discovery recv data
842 void process_pppoe_disc(uint8_t *pack
, int size
)
844 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
845 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
847 LOG(3, 0, 0, "RCV pppoe_disc: Code %s from %s\n", get_string_codepad(hdr
->code
), fmtMacAddr(ethhdr
->h_source
));
848 LOG_HEX(5, "PPPOE Disc", pack
, size
);
850 if (!config
->cluster_iam_master
)
852 if (hdr
->code
== CODE_PADI
)
853 return; // Discard because the PADI is received by all (PADI is a broadcast diffusion)
855 master_forward_pppoe_packet(pack
, size
, hdr
->code
);
859 if (size
< (ETH_HLEN
+ sizeof(*hdr
)))
861 LOG(1, 0, 0, "Error pppoe_disc: short packet received (%i)\n", size
);
865 if (memcmp(ethhdr
->h_dest
, bc_addr
, ETH_ALEN
) && memcmp(ethhdr
->h_dest
, config
->pppoe_hwaddr
, ETH_ALEN
))
867 LOG(1, 0, 0, "Error pppoe_disc: h_dest != bc_addr and h_dest != config->pppoe_hwaddr\n");
871 if (!memcmp(ethhdr
->h_source
, bc_addr
, ETH_ALEN
))
873 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (source address is broadcast)\n");
877 if ((ethhdr
->h_source
[0] & 1) != 0)
879 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (host address is not unicast)\n");
883 if (size
< ETH_HLEN
+ sizeof(*hdr
) + ntohs(hdr
->length
))
885 LOG(1, 0, 0, "Error pppoe_disc: short packet received\n");
891 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (unsupported version %i)\n", hdr
->ver
);
897 LOG(1, 0, 0, "Error pppoe_disc: discarding packet (unsupported type %i)\n", hdr
->type
);
903 pppoe_recv_PADI(pack
, size
);
906 pppoe_recv_PADR(pack
, size
);
909 pppoe_recv_PADT(pack
);
914 // Forward from pppoe to l2tp remote LNS
915 static void pppoe_forwardto_session_rmlns(uint8_t *pack
, int size
, sessionidt sess
, uint16_t proto
)
917 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
918 uint16_t lppp
= ntohs(hdr
->length
);
919 uint16_t ll2tp
= lppp
+ 6;
920 uint8_t *pppdata
= (uint8_t *) hdr
->tag
;
921 uint8_t *pl2tp
= pppdata
- 6;
923 uint16_t t
= 0, s
= 0;
925 s
= session
[sess
].forwardtosession
;
926 if (session
[s
].forwardtosession
!= sess
)
928 LOG(3, sess
, session
[sess
].tunnel
, "pppoe: Link Session (%u) broken\n", s
);
932 t
= session
[s
].tunnel
;
935 LOG(1, s
, t
, "pppoe: Session with invalid tunnel ID\n");
939 if (!tunnel
[t
].isremotelns
)
941 LOG(3, sess
, session
[sess
].tunnel
, "pppoe: Link Tunnel/Session (%u/%u) broken\n", s
, t
);
945 // First word L2TP options (with no options)
946 *(uint16_t *) p
= htons(0x0002);
948 *(uint16_t *) p
= htons(tunnel
[t
].far
); // tunnel
950 *(uint16_t *) p
= htons(session
[s
].far
); // session
953 if ((proto
== PPPIP
) || (proto
== PPPMP
) ||(proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0]))
955 session
[sess
].last_packet
= session
[sess
].last_data
= time_now
;
957 increment_counter(&session
[sess
].cin
, &session
[sess
].cin_wrap
, ll2tp
);
958 session
[sess
].cin_delta
+= ll2tp
;
960 sess_local
[sess
].cin
+= ll2tp
;
961 sess_local
[sess
].pin
++;
963 session
[s
].last_data
= time_now
;
965 increment_counter(&session
[s
].cout
, &session
[s
].cout_wrap
, ll2tp
); // byte count
966 session
[s
].cout_delta
+= ll2tp
;
968 sess_local
[s
].cout
+= ll2tp
;
969 sess_local
[s
].pout
++;
972 session
[sess
].last_packet
= time_now
;
974 tunnelsend(pl2tp
, ll2tp
, t
); // send it...
977 // Forward from l2tp to pppoe
978 // (note: THIS ROUTINE WRITES TO pack[-20]).
979 void pppoe_forwardto_session_pppoe(uint8_t *pack
, int size
, sessionidt sess
, uint16_t proto
)
981 uint16_t t
= 0, s
= 0;
982 uint16_t lpppoe
= size
- 2;
983 uint8_t *p
= pack
+ 2; // First word L2TP options
985 LOG(5, sess
, session
[sess
].tunnel
, "Forwarding data session to pppoe session %u\n", session
[sess
].forwardtosession
);
987 s
= session
[sess
].forwardtosession
;
988 t
= session
[s
].tunnel
;
996 *(uint16_t *) p
= htons(tunnel
[t
].far
); // tunnel
998 *(uint16_t *) p
= htons(session
[s
].far
); // session
1004 *(uint16_t *) p
= htons(tunnel
[t
].ns
); // sequence
1006 *(uint16_t *) p
= htons(tunnel
[t
].nr
); // sequence
1011 if (lpppoe
> 2 && p
[0] == 0xFF && p
[1] == 0x03)
1013 // HDLC address header, discard in pppoe
1018 lpppoe
+= (ETH_HLEN
+ sizeof(struct pppoe_hdr
));
1019 p
-= (ETH_HLEN
+ sizeof(struct pppoe_hdr
));
1021 // 14 bytes ethernet Header + 6 bytes header pppoe
1022 setup_header(p
, config
->pppoe_hwaddr
, session
[s
].src_hwaddr
, CODE_SESS
, s
, ETH_P_PPP_SES
);
1024 if ((proto
== PPPIP
) || (proto
== PPPMP
) ||(proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0]))
1026 session
[sess
].last_packet
= session
[sess
].last_data
= time_now
;
1028 increment_counter(&session
[sess
].cin
, &session
[sess
].cin_wrap
, lpppoe
);
1029 session
[sess
].cin_delta
+= lpppoe
;
1030 session
[sess
].pin
++;
1031 sess_local
[sess
].cin
+= lpppoe
;
1032 sess_local
[sess
].pin
++;
1034 session
[s
].last_data
= time_now
;
1036 increment_counter(&session
[s
].cout
, &session
[s
].cout_wrap
, lpppoe
); // byte count
1037 session
[s
].cout_delta
+= lpppoe
;
1039 sess_local
[s
].cout
+= lpppoe
;
1040 sess_local
[s
].pout
++;
1043 session
[sess
].last_packet
= time_now
;
1045 tunnelsend(p
, lpppoe
, t
); // send it....
1048 void process_pppoe_sess(uint8_t *pack
, int size
)
1050 //struct ethhdr *ethhdr = (struct ethhdr *)pack;
1051 struct pppoe_hdr
*hdr
= (struct pppoe_hdr
*)(pack
+ ETH_HLEN
);
1052 uint16_t lppp
= ntohs(hdr
->length
);
1053 uint8_t *pppdata
= (uint8_t *) hdr
->tag
;
1054 uint16_t proto
, sid
, t
;
1056 sid
= ntohs(hdr
->sid
);
1057 t
= TUNNEL_ID_PPPOE
;
1059 LOG_HEX(5, "RCV PPPOE Sess", pack
, size
);
1061 if (sid
>= MAXSESSION
)
1063 LOG(0, sid
, t
, "Received pppoe packet with invalid session ID\n");
1064 STAT(tunnel_rx_errors
);
1068 if (session
[sid
].tunnel
!= t
)
1070 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1072 LOG(1, sid
, t
, "ERROR process_pppoe_sess: Session is not a session pppoe\n");
1078 LOG(3, sid
, t
, "Error process_pppoe_sess: discarding packet (unsupported version %i)\n", hdr
->ver
);
1084 LOG(3, sid
, t
, "Error process_pppoe_sess: discarding packet (unsupported type %i)\n", hdr
->type
);
1088 if (lppp
> 2 && pppdata
[0] == 0xFF && pppdata
[1] == 0x03)
1089 { // HDLC address header, discard
1090 LOG(5, sid
, t
, "pppoe_sess: HDLC address header, discard\n");
1096 LOG(3, sid
, t
, "Error process_pppoe_sess: Short ppp length %d\n", lppp
);
1106 proto
= ntohs(*(uint16_t *) pppdata
);
1111 if (session
[sid
].forwardtosession
)
1112 { // Must be forwaded to a remote lns tunnel l2tp
1113 pppoe_forwardto_session_rmlns(pack
, size
, sid
, proto
);
1117 if (proto
== PPPPAP
)
1119 session
[sid
].last_packet
= time_now
;
1120 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1121 processpap(sid
, t
, pppdata
, lppp
);
1123 else if (proto
== PPPCHAP
)
1125 session
[sid
].last_packet
= time_now
;
1126 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1127 processchap(sid
, t
, pppdata
, lppp
);
1129 else if (proto
== PPPLCP
)
1131 session
[sid
].last_packet
= time_now
;
1132 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1133 processlcp(sid
, t
, pppdata
, lppp
);
1135 else if (proto
== PPPIPCP
)
1137 session
[sid
].last_packet
= time_now
;
1138 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1139 processipcp(sid
, t
, pppdata
, lppp
);
1141 else if (proto
== PPPIPV6CP
&& config
->ipv6_prefix
.s6_addr
[0])
1143 session
[sid
].last_packet
= time_now
;
1144 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1145 processipv6cp(sid
, t
, pppdata
, lppp
);
1147 else if (proto
== PPPCCP
)
1149 session
[sid
].last_packet
= time_now
;
1150 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1151 processccp(sid
, t
, pppdata
, lppp
);
1153 else if (proto
== PPPIP
)
1155 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
1156 if (session
[sid
].walled_garden
&& !config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1157 processipin(sid
, t
, pppdata
, lppp
);
1159 else if (proto
== PPPMP
)
1161 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
1162 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1163 processmpin(sid
, t
, pppdata
, lppp
);
1165 else if (proto
== PPPIPV6
&& config
->ipv6_prefix
.s6_addr
[0])
1167 session
[sid
].last_packet
= session
[sid
].last_data
= time_now
;
1168 if (session
[sid
].walled_garden
&& !config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1169 processipv6in(sid
, t
, pppdata
, lppp
);
1171 else if (session
[sid
].ppp
.lcp
== Opened
)
1173 session
[sid
].last_packet
= time_now
;
1174 if (!config
->cluster_iam_master
) { master_forward_pppoe_packet(pack
, size
, hdr
->code
); return; }
1175 protoreject(sid
, t
, pppdata
, lppp
, proto
);
1179 LOG(3, sid
, t
, "process_pppoe_sess: Unknown PPP protocol 0x%04X received in LCP %s state\n",
1180 proto
, ppp_state(session
[sid
].ppp
.lcp
));
1184 void pppoe_send_garp()
1190 if (!*config
->pppoe_if_to_bind
)
1193 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
1196 LOG(0, 0, 0, "Error creating socket for GARP: %s\n", strerror(errno
));
1199 memset(&ifr
, 0, sizeof(ifr
));
1200 strncpy(ifr
.ifr_name
, config
->pppoe_if_to_bind
, sizeof(ifr
.ifr_name
) - 1);
1201 if (ioctl(s
, SIOCGIFHWADDR
, &ifr
) < 0)
1203 LOG(0, 0, 0, "Error getting eth0 hardware address for GARP: %s\n", strerror(errno
));
1207 memcpy(mac
, &ifr
.ifr_hwaddr
.sa_data
, 6*sizeof(char));
1208 if (ioctl(s
, SIOCGIFINDEX
, &ifr
) < 0)
1210 LOG(0, 0, 0, "Error getting eth0 interface index for GARP: %s\n", strerror(errno
));
1216 sendarp(ifr
.ifr_ifindex
, mac
, config
->iftun_address
);
1219 // rcv pppoe data from slave
1220 void pppoe_process_forward(uint8_t *pack
, int size
, in_addr_t addr
)
1222 struct ethhdr
*ethhdr
= (struct ethhdr
*)pack
;
1224 if (ethhdr
->h_proto
== htons(ETH_P_PPP_DISC
))
1225 process_pppoe_disc(pack
, size
);
1226 else if (ethhdr
->h_proto
== htons(ETH_P_PPP_SES
))
1227 process_pppoe_sess(pack
, size
);
1229 LOG(0, 0, 0, "pppoe_process_forward: I got a C_PPPOE_FORWARD from %s, but not a PPPOE data?\n", fmtaddr(addr
, 0));