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 */
31 #include "pathnames.h"
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
41 #include <netinet/in.h>
43 #include <linux/version.h>
44 #include <linux/sockios.h>
47 /* should be defined in sys/types.h */
48 #define aligned_u64 unsigned long long __attribute__((aligned(8)))
50 #include <linux/types.h>
51 #include <linux/if_ether.h>
52 #include <linux/ppp_defs.h>
53 #include <linux/if_ppp.h>
54 #include <linux/if_pppox.h>
55 #include <linux/if_pppol2tp.h>
57 #include "l2tp_event.h"
59 extern int pppol2tp_tunnel_id;
60 extern int pppol2tp_session_id;
62 extern void (*pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
63 uint32_t send_accm, uint32_t recv_accm);
64 extern void (*pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, int up);
66 const char pppd_version[] = VERSION;
68 static int openl2tp_fd = -1;
70 static void (*old_pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
72 uint32_t recv_accm) = NULL;
73 static void (*old_pppol2tp_ip_updown_hook)(int tunnel_id, int session_id,
75 static void (*old_multilink_join_hook)(void) = NULL;
77 /*****************************************************************************
79 * We send a PPP_ACCM_IND to openl2tpd to report ACCM values and
80 * SESSION_PPP_UPDOWN_IND to indicate when the PPP link comes up or
82 *****************************************************************************/
84 static int openl2tp_client_create(void)
86 struct sockaddr_un addr;
89 if (openl2tp_fd < 0) {
90 openl2tp_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
91 if (openl2tp_fd < 0) {
92 error("openl2tp connection create: %m");
96 addr.sun_family = AF_UNIX;
97 strcpy(&addr.sun_path[0], OPENL2TP_EVENT_SOCKET_NAME);
99 result = connect(openl2tp_fd, (struct sockaddr *) &addr,
102 error("openl2tp connection connect: %m");
110 static void openl2tp_send_accm_ind(int tunnel_id, int session_id,
111 uint32_t send_accm, uint32_t recv_accm)
114 uint8_t buf[OPENL2TP_MSG_MAX_LEN];
115 struct openl2tp_event_msg *msg = (void *) &buf[0];
116 struct openl2tp_event_tlv *tlv;
117 uint16_t tid = tunnel_id;
118 uint16_t sid = session_id;
119 struct openl2tp_tlv_ppp_accm accm;
121 if (openl2tp_fd < 0) {
122 result = openl2tp_client_create();
128 accm.send_accm = send_accm;
129 accm.recv_accm = recv_accm;
131 msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
132 msg->msg_type = OPENL2TP_MSG_TYPE_PPP_ACCM_IND;
135 tlv = (void *) &msg->msg_data[msg->msg_len];
136 tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
137 tlv->tlv_len = sizeof(tid);
138 memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
139 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
141 tlv = (void *) &msg->msg_data[msg->msg_len];
142 tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
143 tlv->tlv_len = sizeof(sid);
144 memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
145 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
147 tlv = (void *) &msg->msg_data[msg->msg_len];
148 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_ACCM;
149 tlv->tlv_len = sizeof(accm);
150 memcpy(&tlv->tlv_value[0], &accm, tlv->tlv_len);
151 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
153 result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
156 error("openl2tp send: %m");
158 if (result != (sizeof(*msg) + msg->msg_len)) {
159 warn("openl2tp send: unexpected byte count %d, expected %d",
160 result, sizeof(msg) + msg->msg_len);
162 dbglog("openl2tp send: sent PPP_ACCM_IND, %d bytes", result);
165 if (old_pppol2tp_send_accm_hook != NULL) {
166 (*old_pppol2tp_send_accm_hook)(tunnel_id, session_id,
167 send_accm, recv_accm);
172 static void openl2tp_ppp_updown_ind(int tunnel_id, int session_id, int up)
175 uint8_t buf[OPENL2TP_MSG_MAX_LEN];
176 struct openl2tp_event_msg *msg = (void *) &buf[0];
177 struct openl2tp_event_tlv *tlv;
178 uint16_t tid = tunnel_id;
179 uint16_t sid = session_id;
182 char *user_name = NULL;
184 if (openl2tp_fd < 0) {
185 result = openl2tp_client_create();
191 if (peer_authname[0] != '\0') {
192 user_name = strdup(peer_authname);
195 msg->msg_signature = OPENL2TP_MSG_SIGNATURE;
196 msg->msg_type = OPENL2TP_MSG_TYPE_PPP_UPDOWN_IND;
199 tlv = (void *) &msg->msg_data[msg->msg_len];
200 tlv->tlv_type = OPENL2TP_TLV_TYPE_TUNNEL_ID;
201 tlv->tlv_len = sizeof(tid);
202 memcpy(&tlv->tlv_value[0], &tid, tlv->tlv_len);
203 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
205 tlv = (void *) &msg->msg_data[msg->msg_len];
206 tlv->tlv_type = OPENL2TP_TLV_TYPE_SESSION_ID;
207 tlv->tlv_len = sizeof(sid);
208 memcpy(&tlv->tlv_value[0], &sid, tlv->tlv_len);
209 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
211 tlv = (void *) &msg->msg_data[msg->msg_len];
212 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_STATE;
213 tlv->tlv_len = sizeof(state);
214 memcpy(&tlv->tlv_value[0], &state, tlv->tlv_len);
215 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
217 tlv = (void *) &msg->msg_data[msg->msg_len];
218 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_UNIT;
219 tlv->tlv_len = sizeof(unit);
220 memcpy(&tlv->tlv_value[0], &unit, tlv->tlv_len);
221 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
223 tlv = (void *) &msg->msg_data[msg->msg_len];
224 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_IFNAME;
225 tlv->tlv_len = strlen(ifname) + 1;
226 memcpy(&tlv->tlv_value[0], ifname, tlv->tlv_len);
227 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
229 if (user_name != NULL) {
230 tlv = (void *) &msg->msg_data[msg->msg_len];
231 tlv->tlv_type = OPENL2TP_TLV_TYPE_PPP_USER_NAME;
232 tlv->tlv_len = strlen(user_name) + 1;
233 memcpy(&tlv->tlv_value[0], user_name, tlv->tlv_len);
234 msg->msg_len += sizeof(*tlv) + ALIGN32(tlv->tlv_len);
237 result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len,
240 error("openl2tp send: %m");
242 if (result != (sizeof(*msg) + msg->msg_len)) {
243 warn("openl2tp send: unexpected byte count %d, expected %d",
244 result, sizeof(msg) + msg->msg_len);
246 dbglog("openl2tp send: sent PPP_UPDOWN_IND, %d bytes", result);
249 if (old_pppol2tp_ip_updown_hook != NULL) {
250 (*old_pppol2tp_ip_updown_hook)(tunnel_id, session_id, up);
253 if (user_name != NULL)
259 /*****************************************************************************
260 * When a multilink interface is created, there are 2 cases to consider.
262 * 1. The new interface is the first of a multilink bundle (master).
263 * 2. The new interface is being attached to an existing bundle.
265 * The first case is handled by existing code because the interface
266 * generates ip-up events just like standard interfaces. But in the
267 * second case, where the interface is added to an existing ppp
268 * bundle, pppd does not do IP negotiation and so as a result, no
269 * ip-up event is generated when the interface is created. Since
270 * openl2tpd needs the SESSION_PPP_UPDOWN_IND for all interfaces of a
271 * PPP bundle, we must fake the event.
273 * We use the ip_multilink_join_hook to hear when an interface joins a
275 *****************************************************************************/
277 static void openl2tp_multilink_join_ind(void)
279 if (doing_multilink && !multilink_master) {
280 /* send event only if not master */
281 openl2tp_ppp_updown_ind(pppol2tp_tunnel_id,
282 pppol2tp_session_id, 1);
286 /*****************************************************************************
288 *****************************************************************************/
290 void plugin_init(void)
292 old_pppol2tp_send_accm_hook = pppol2tp_send_accm_hook;
293 pppol2tp_send_accm_hook = openl2tp_send_accm_ind;
295 old_pppol2tp_ip_updown_hook = pppol2tp_ip_updown_hook;
296 pppol2tp_ip_updown_hook = openl2tp_ppp_updown_ind;
298 old_multilink_join_hook = multilink_join_hook;
299 multilink_join_hook = openl2tp_multilink_join_ind;