1 /*****************************************************************************
2 * Copyright (C) 2006,2007,2008 Katalix Systems Ltd
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *****************************************************************************/
20 /* pppd plugin for interfacing to openl2tpd */
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
35 #include <linux/version.h>
36 #include <linux/sockios.h>
41 #include <pppd/pppd.h>
42 #include <pppd/options.h>
46 #include <pppd/ipcp.h>
47 #include <pppd/multilink.h>
51 /* should be defined in sys/types.h */
52 #define aligned_u64 unsigned long long __attribute__((aligned(8)))
54 #include <linux/types.h>
55 #include <linux/if_ether.h>
56 #include <linux/ppp_defs.h>
57 #include <linux/if_pppox.h>
58 #include <linux/if_pppol2tp.h>
60 #include "l2tp_event.h"
62 extern int pppol2tp_tunnel_id;
63 extern int pppol2tp_session_id;
65 extern void (*pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
66 uint32_t send_accm, uint32_t recv_accm);
67 extern void (*pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, int up);
69 const char pppd_version[] = PPPD_VERSION;
71 static int openl2tp_fd = -1;
73 static void (*old_pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
75 uint32_t recv_accm) = NULL;
76 static void (*old_pppol2tp_ip_updown_hook)(int tunnel_id, int session_id,
78 #ifdef PPP_WITH_MULTILINK
79 static multilink_join_hook_fn *old_multilink_join_hook = NULL;
82 /*****************************************************************************
84 * We send a PPP_ACCM_IND to openl2tpd to report ACCM values and
85 * SESSION_PPP_UPDOWN_IND to indicate when the PPP link comes up or
87 *****************************************************************************/
89 static int openl2tp_client_create(void)
91 struct sockaddr_un addr;
94 if (openl2tp_fd < 0) {
95 openl2tp_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
96 if (openl2tp_fd < 0) {
97 error("openl2tp connection create: %m");
101 addr.sun_family = AF_UNIX;
102 strcpy(&addr.sun_path[0], OPENL2TP_EVENT_SOCKET_NAME);
104 result = connect(openl2tp_fd, (struct sockaddr *) &addr,
107 error("openl2tp connection connect: %m");
115 static void openl2tp_send_accm_ind(int tunnel_id, int session_id,
116 uint32_t send_accm, uint32_t recv_accm)
119 uint8_t buf[OPENL2TP_MSG_MAX_LEN];
120 struct openl2tp_event_msg *msg = (void *) &buf[0];
121 struct openl2tp_event_tlv *tlv;
122 uint16_t tid = tunnel_id;
123 uint16_t sid = session_id;
124 struct openl2tp_tlv_ppp_accm accm;
126 if (openl2tp_fd < 0) {
127 result = openl2tp_client_create();
133 accm.send_accm = send_accm;
134 accm.recv_accm = recv_accm;
136 msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
137 msg->msg_type = OPENL2TP_MSG_TYPE_PPP_ACCM_IND;
140 tlv = (void *) &msg->msg_data[msg->msg_len];
141 tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
142 tlv->tlv_len = sizeof(tid);
143 memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
144 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
146 tlv = (void *) &msg->msg_data[msg->msg_len];
147 tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
148 tlv->tlv_len = sizeof(sid);
149 memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
150 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
152 tlv = (void *) &msg->msg_data[msg->msg_len];
153 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_ACCM;
154 tlv->tlv_len = sizeof(accm);
155 memcpy(&tlv->tlv_value[0], &accm, tlv->tlv_len);
156 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
158 result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
161 error("openl2tp send: %m");
163 if (result != (sizeof(*msg) + msg->msg_len)) {
164 warn("openl2tp send: unexpected byte count %d, expected %d",
165 result, sizeof(msg) + msg->msg_len);
167 dbglog("openl2tp send: sent PPP_ACCM_IND, %d bytes", result);
170 if (old_pppol2tp_send_accm_hook != NULL) {
171 (*old_pppol2tp_send_accm_hook)(tunnel_id, session_id,
172 send_accm, recv_accm);
177 static void openl2tp_ppp_updown_ind(int tunnel_id, int session_id, int up)
180 uint8_t buf[OPENL2TP_MSG_MAX_LEN];
181 struct openl2tp_event_msg *msg = (void *) &buf[0];
182 struct openl2tp_event_tlv *tlv;
183 uint16_t tid = tunnel_id;
184 uint16_t sid = session_id;
187 char ifname[MAXNAMELEN];
188 char user_name[MAXNAMELEN];
191 ppp_get_ifname(ifname, sizeof(ifname));
193 if (openl2tp_fd < 0) {
194 result = openl2tp_client_create();
200 if (!ppp_peer_authname(user_name, sizeof(user_name)))
203 msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
204 msg->msg_type = OPENL2TP_MSG_TYPE_PPP_UPDOWN_IND;
207 tlv = (void *) &msg->msg_data[msg->msg_len];
208 tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
209 tlv->tlv_len = sizeof(tid);
210 memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
211 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
213 tlv = (void *) &msg->msg_data[msg->msg_len];
214 tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
215 tlv->tlv_len = sizeof(sid);
216 memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
217 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
219 tlv = (void *) &msg->msg_data[msg->msg_len];
220 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_STATE;
221 tlv->tlv_len = sizeof(state);
222 memcpy(&tlv->tlv_value[0], &state, tlv->tlv_len);
223 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
225 tlv = (void *) &msg->msg_data[msg->msg_len];
226 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_UNIT;
227 tlv->tlv_len = sizeof(unit);
228 memcpy(&tlv->tlv_value[0], &unit, tlv->tlv_len);
229 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
231 tlv = (void *) &msg->msg_data[msg->msg_len];
232 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_IFNAME;
233 tlv->tlv_len = strlen(ifname) + 1;
234 memcpy(&tlv->tlv_value[0], ifname, tlv->tlv_len);
235 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
237 if (user_name[0] != '\0') {
238 tlv = (void *) &msg->msg_data[msg->msg_len];
239 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_USER_NAME;
240 tlv->tlv_len = strlen(user_name) + 1;
241 memcpy(&tlv->tlv_value[0], user_name, tlv->tlv_len);
242 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
245 result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
248 error("openl2tp send: %m");
250 if (result != (sizeof(*msg) + msg->msg_len)) {
251 warn("openl2tp send: unexpected byte count %d, expected %d",
252 result, sizeof(msg) + msg->msg_len);
254 dbglog("openl2tp send: sent PPP_UPDOWN_IND, %d bytes", result);
257 if (old_pppol2tp_ip_updown_hook != NULL) {
258 (*old_pppol2tp_ip_updown_hook)(tunnel_id, session_id, up);
264 /*****************************************************************************
265 * When a multilink interface is created, there are 2 cases to consider.
267 * 1. The new interface is the first of a multilink bundle (master).
268 * 2. The new interface is being attached to an existing bundle.
270 * The first case is handled by existing code because the interface
271 * generates ip-up events just like standard interfaces. But in the
272 * second case, where the interface is added to an existing ppp
273 * bundle, pppd does not do IP negotiation and so as a result, no
274 * ip-up event is generated when the interface is created. Since
275 * openl2tpd needs the SESSION_PPP_UPDOWN_IND for all interfaces of a
276 * PPP bundle, we must fake the event.
278 * We use the ip_multilink_join_hook to hear when an interface joins a
280 *****************************************************************************/
282 #ifdef PPP_WITH_MULTILINK
283 static void openl2tp_multilink_join_ind(void)
285 if (mp_on() && !mp_master()) {
286 /* send event only if not master */
287 openl2tp_ppp_updown_ind(pppol2tp_tunnel_id,
288 pppol2tp_session_id, 1);
293 /*****************************************************************************
295 *****************************************************************************/
297 void plugin_init(void)
299 old_pppol2tp_send_accm_hook = pppol2tp_send_accm_hook;
300 pppol2tp_send_accm_hook = openl2tp_send_accm_ind;
302 old_pppol2tp_ip_updown_hook = pppol2tp_ip_updown_hook;
303 pppol2tp_ip_updown_hook = openl2tp_ppp_updown_ind;
305 #ifdef PPP_WITH_MULTILINK
306 old_multilink_join_hook = multilink_join_hook;
307 multilink_join_hook = openl2tp_multilink_join_ind;