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>
32 #include <netinet/in.h>
34 #include <linux/version.h>
35 #include <linux/sockios.h>
37 #include <pppd/pppd.h>
38 #include <pppd/pathnames.h>
42 #include <pppd/ipcp.h>
46 /* should be defined in sys/types.h */
47 #define aligned_u64 unsigned long long __attribute__((aligned(8)))
49 #include <linux/types.h>
50 #include <linux/if_ether.h>
51 #include <linux/ppp_defs.h>
52 #include <linux/if_ppp.h>
53 #include <linux/if_pppox.h>
54 #include <linux/if_pppol2tp.h>
56 #include "l2tp_event.h"
58 extern int pppol2tp_tunnel_id;
59 extern int pppol2tp_session_id;
61 extern void (*pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
62 uint32_t send_accm, uint32_t recv_accm);
63 extern void (*pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, int up);
65 const char pppd_version[] = PPPD_VERSION;
67 static int openl2tp_fd = -1;
69 static void (*old_pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
71 uint32_t recv_accm) = NULL;
72 static void (*old_pppol2tp_ip_updown_hook)(int tunnel_id, int session_id,
74 static void (*old_multilink_join_hook)(void) = NULL;
76 /*****************************************************************************
78 * We send a PPP_ACCM_IND to openl2tpd to report ACCM values and
79 * SESSION_PPP_UPDOWN_IND to indicate when the PPP link comes up or
81 *****************************************************************************/
83 static int openl2tp_client_create(void)
85 struct sockaddr_un addr;
88 if (openl2tp_fd < 0) {
89 openl2tp_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
90 if (openl2tp_fd < 0) {
91 error("openl2tp connection create: %m");
95 addr.sun_family = AF_UNIX;
96 strcpy(&addr.sun_path[0], OPENL2TP_EVENT_SOCKET_NAME);
98 result = connect(openl2tp_fd, (struct sockaddr *) &addr,
101 error("openl2tp connection connect: %m");
109 static void openl2tp_send_accm_ind(int tunnel_id, int session_id,
110 uint32_t send_accm, uint32_t recv_accm)
113 uint8_t buf[OPENL2TP_MSG_MAX_LEN];
114 struct openl2tp_event_msg *msg = (void *) &buf[0];
115 struct openl2tp_event_tlv *tlv;
116 uint16_t tid = tunnel_id;
117 uint16_t sid = session_id;
118 struct openl2tp_tlv_ppp_accm accm;
120 if (openl2tp_fd < 0) {
121 result = openl2tp_client_create();
127 accm.send_accm = send_accm;
128 accm.recv_accm = recv_accm;
130 msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
131 msg->msg_type = OPENL2TP_MSG_TYPE_PPP_ACCM_IND;
134 tlv = (void *) &msg->msg_data[msg->msg_len];
135 tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
136 tlv->tlv_len = sizeof(tid);
137 memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
138 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
140 tlv = (void *) &msg->msg_data[msg->msg_len];
141 tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
142 tlv->tlv_len = sizeof(sid);
143 memcpy(&tlv->tlv_value[0], &sid, 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_PPP_ACCM;
148 tlv->tlv_len = sizeof(accm);
149 memcpy(&tlv->tlv_value[0], &accm, tlv->tlv_len);
150 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
152 result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
155 error("openl2tp send: %m");
157 if (result != (sizeof(*msg) + msg->msg_len)) {
158 warn("openl2tp send: unexpected byte count %d, expected %d",
159 result, sizeof(msg) + msg->msg_len);
161 dbglog("openl2tp send: sent PPP_ACCM_IND, %d bytes", result);
164 if (old_pppol2tp_send_accm_hook != NULL) {
165 (*old_pppol2tp_send_accm_hook)(tunnel_id, session_id,
166 send_accm, recv_accm);
171 static void openl2tp_ppp_updown_ind(int tunnel_id, int session_id, int up)
174 uint8_t buf[OPENL2TP_MSG_MAX_LEN];
175 struct openl2tp_event_msg *msg = (void *) &buf[0];
176 struct openl2tp_event_tlv *tlv;
177 uint16_t tid = tunnel_id;
178 uint16_t sid = session_id;
181 char *user_name = NULL;
183 if (openl2tp_fd < 0) {
184 result = openl2tp_client_create();
190 if (peer_authname[0] != '\0') {
191 user_name = strdup(peer_authname);
194 msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
195 msg->msg_type = OPENL2TP_MSG_TYPE_PPP_UPDOWN_IND;
198 tlv = (void *) &msg->msg_data[msg->msg_len];
199 tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
200 tlv->tlv_len = sizeof(tid);
201 memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
202 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
204 tlv = (void *) &msg->msg_data[msg->msg_len];
205 tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
206 tlv->tlv_len = sizeof(sid);
207 memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
208 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
210 tlv = (void *) &msg->msg_data[msg->msg_len];
211 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_STATE;
212 tlv->tlv_len = sizeof(state);
213 memcpy(&tlv->tlv_value[0], &state, tlv->tlv_len);
214 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
216 tlv = (void *) &msg->msg_data[msg->msg_len];
217 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_UNIT;
218 tlv->tlv_len = sizeof(unit);
219 memcpy(&tlv->tlv_value[0], &unit, tlv->tlv_len);
220 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
222 tlv = (void *) &msg->msg_data[msg->msg_len];
223 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_IFNAME;
224 tlv->tlv_len = strlen(ifname) + 1;
225 memcpy(&tlv->tlv_value[0], ifname, tlv->tlv_len);
226 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
228 if (user_name != NULL) {
229 tlv = (void *) &msg->msg_data[msg->msg_len];
230 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_USER_NAME;
231 tlv->tlv_len = strlen(user_name) + 1;
232 memcpy(&tlv->tlv_value[0], user_name, tlv->tlv_len);
233 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
236 result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
239 error("openl2tp send: %m");
241 if (result != (sizeof(*msg) + msg->msg_len)) {
242 warn("openl2tp send: unexpected byte count %d, expected %d",
243 result, sizeof(msg) + msg->msg_len);
245 dbglog("openl2tp send: sent PPP_UPDOWN_IND, %d bytes", result);
248 if (old_pppol2tp_ip_updown_hook != NULL) {
249 (*old_pppol2tp_ip_updown_hook)(tunnel_id, session_id, up);
252 if (user_name != NULL)
258 /*****************************************************************************
259 * When a multilink interface is created, there are 2 cases to consider.
261 * 1. The new interface is the first of a multilink bundle (master).
262 * 2. The new interface is being attached to an existing bundle.
264 * The first case is handled by existing code because the interface
265 * generates ip-up events just like standard interfaces. But in the
266 * second case, where the interface is added to an existing ppp
267 * bundle, pppd does not do IP negotiation and so as a result, no
268 * ip-up event is generated when the interface is created. Since
269 * openl2tpd needs the SESSION_PPP_UPDOWN_IND for all interfaces of a
270 * PPP bundle, we must fake the event.
272 * We use the ip_multilink_join_hook to hear when an interface joins a
274 *****************************************************************************/
276 static void openl2tp_multilink_join_ind(void)
278 if (doing_multilink && !multilink_master) {
279 /* send event only if not master */
280 openl2tp_ppp_updown_ind(pppol2tp_tunnel_id,
281 pppol2tp_session_id, 1);
285 /*****************************************************************************
287 *****************************************************************************/
289 void plugin_init(void)
291 old_pppol2tp_send_accm_hook = pppol2tp_send_accm_hook;
292 pppol2tp_send_accm_hook = openl2tp_send_accm_ind;
294 old_pppol2tp_ip_updown_hook = pppol2tp_ip_updown_hook;
295 pppol2tp_ip_updown_hook = openl2tp_ppp_updown_ind;
297 old_multilink_join_hook = multilink_join_hook;
298 multilink_join_hook = openl2tp_multilink_join_ind;