| 1 |
/***************************************************************************** |
| 2 |
* Copyright (C) 2006,2007,2008 Katalix Systems Ltd |
| 3 |
* |
| 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. |
| 8 |
* |
| 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. |
| 13 |
* |
| 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 |
| 17 |
* |
| 18 |
*****************************************************************************/ |
| 19 |
|
| 20 |
/* pppd plugin for interfacing to openl2tpd */ |
| 21 |
|
| 22 |
#include <unistd.h> |
| 23 |
#include <string.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <errno.h> |
| 26 |
|
| 27 |
#include <sys/stat.h> |
| 28 |
#include <net/if.h> |
| 29 |
#include <sys/ioctl.h> |
| 30 |
#include <sys/socket.h> |
| 31 |
#include <sys/un.h> |
| 32 |
#include <sys/time.h> |
| 33 |
#include <netinet/in.h> |
| 34 |
#include <signal.h> |
| 35 |
#include <linux/version.h> |
| 36 |
#include <linux/sockios.h> |
| 37 |
#include <stdarg.h> |
| 38 |
#include <stdbool.h> |
| 39 |
#include <stdio.h> |
| 40 |
|
| 41 |
#include <pppd/pppd.h> |
| 42 |
#include <pppd/options.h> |
| 43 |
#include <pppd/fsm.h> |
| 44 |
#include <pppd/lcp.h> |
| 45 |
#include <pppd/ccp.h> |
| 46 |
#include <pppd/ipcp.h> |
| 47 |
#include <pppd/multilink.h> |
| 48 |
|
| 49 |
|
| 50 |
#ifndef aligned_u64 |
| 51 |
/* should be defined in sys/types.h */ |
| 52 |
#define aligned_u64 unsigned long long __attribute__((aligned(8))) |
| 53 |
#endif |
| 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> |
| 59 |
|
| 60 |
#include "l2tp_event.h" |
| 61 |
|
| 62 |
extern int pppol2tp_tunnel_id; |
| 63 |
extern int pppol2tp_session_id; |
| 64 |
|
| 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); |
| 68 |
|
| 69 |
const char pppd_version[] = PPPD_VERSION; |
| 70 |
|
| 71 |
static int openl2tp_fd = -1; |
| 72 |
|
| 73 |
static void (*old_pppol2tp_send_accm_hook)(int tunnel_id, int session_id, |
| 74 |
uint32_t send_accm, |
| 75 |
uint32_t recv_accm) = NULL; |
| 76 |
static void (*old_pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, |
| 77 |
int up) = NULL; |
| 78 |
#ifdef PPP_WITH_MULTILINK |
| 79 |
static multilink_join_hook_fn *old_multilink_join_hook = NULL; |
| 80 |
#endif |
| 81 |
|
| 82 |
/***************************************************************************** |
| 83 |
* OpenL2TP interface. |
| 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 |
| 86 |
* goes down. |
| 87 |
*****************************************************************************/ |
| 88 |
|
| 89 |
static int openl2tp_client_create(void) |
| 90 |
{ |
| 91 |
struct sockaddr_un addr; |
| 92 |
int result; |
| 93 |
|
| 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"); |
| 98 |
return -ENOTCONN; |
| 99 |
} |
| 100 |
|
| 101 |
addr.sun_family = AF_UNIX; |
| 102 |
strcpy(&addr.sun_path[0], OPENL2TP_EVENT_SOCKET_NAME); |
| 103 |
|
| 104 |
result = connect(openl2tp_fd, (struct sockaddr *) &addr, |
| 105 |
sizeof(addr)); |
| 106 |
if (result < 0) { |
| 107 |
error("openl2tp connection connect: %m"); |
| 108 |
return -ENOTCONN; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
static void openl2tp_send_accm_ind(int tunnel_id, int session_id, |
| 116 |
uint32_t send_accm, uint32_t recv_accm) |
| 117 |
{ |
| 118 |
int result; |
| 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; |
| 125 |
|
| 126 |
if (openl2tp_fd < 0) { |
| 127 |
result = openl2tp_client_create(); |
| 128 |
if (result < 0) { |
| 129 |
goto out; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
accm.send_accm = send_accm; |
| 134 |
accm.recv_accm = recv_accm; |
| 135 |
|
| 136 |
msg->msg_signature = OPENL2TP_MSG_SIGNATURE; |
| 137 |
msg->msg_type = OPENL2TP_MSG_TYPE_PPP_ACCM_IND; |
| 138 |
msg->msg_len = 0; |
| 139 |
|
| 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); |
| 145 |
|
| 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); |
| 151 |
|
| 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); |
| 157 |
|
| 158 |
result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len, |
| 159 |
MSG_NOSIGNAL); |
| 160 |
if (result < 0) { |
| 161 |
error("openl2tp send: %m"); |
| 162 |
} |
| 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); |
| 166 |
} |
| 167 |
dbglog("openl2tp send: sent PPP_ACCM_IND, %d bytes", result); |
| 168 |
|
| 169 |
out: |
| 170 |
if (old_pppol2tp_send_accm_hook != NULL) { |
| 171 |
(*old_pppol2tp_send_accm_hook)(tunnel_id, session_id, |
| 172 |
send_accm, recv_accm); |
| 173 |
} |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
static void openl2tp_ppp_updown_ind(int tunnel_id, int session_id, int up) |
| 178 |
{ |
| 179 |
int result; |
| 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; |
| 185 |
uint8_t state = up; |
| 186 |
int unit = 0; |
| 187 |
char ifname[MAXNAMELEN]; |
| 188 |
char user_name[MAXNAMELEN]; |
| 189 |
|
| 190 |
unit = ppp_ifunit(); |
| 191 |
ppp_get_ifname(ifname, sizeof(ifname)); |
| 192 |
|
| 193 |
if (openl2tp_fd < 0) { |
| 194 |
result = openl2tp_client_create(); |
| 195 |
if (result < 0) { |
| 196 |
goto out; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if (!ppp_peer_authname(user_name, sizeof(user_name))) |
| 201 |
user_name[0] = '\0'; |
| 202 |
|
| 203 |
msg->msg_signature = OPENL2TP_MSG_SIGNATURE; |
| 204 |
msg->msg_type = OPENL2TP_MSG_TYPE_PPP_UPDOWN_IND; |
| 205 |
msg->msg_len = 0; |
| 206 |
|
| 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); |
| 212 |
|
| 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); |
| 218 |
|
| 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); |
| 224 |
|
| 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); |
| 230 |
|
| 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); |
| 236 |
|
| 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); |
| 243 |
} |
| 244 |
|
| 245 |
result = send(openl2tp_fd, msg, sizeof(*msg) + msg->msg_len, |
| 246 |
MSG_NOSIGNAL); |
| 247 |
if (result < 0) { |
| 248 |
error("openl2tp send: %m"); |
| 249 |
} |
| 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); |
| 253 |
} |
| 254 |
dbglog("openl2tp send: sent PPP_UPDOWN_IND, %d bytes", result); |
| 255 |
|
| 256 |
out: |
| 257 |
if (old_pppol2tp_ip_updown_hook != NULL) { |
| 258 |
(*old_pppol2tp_ip_updown_hook)(tunnel_id, session_id, up); |
| 259 |
} |
| 260 |
|
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
/***************************************************************************** |
| 265 |
* When a multilink interface is created, there are 2 cases to consider. |
| 266 |
* |
| 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. |
| 269 |
* |
| 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. |
| 277 |
* |
| 278 |
* We use the ip_multilink_join_hook to hear when an interface joins a |
| 279 |
* multilink bundle. |
| 280 |
*****************************************************************************/ |
| 281 |
|
| 282 |
#ifdef PPP_WITH_MULTILINK |
| 283 |
static void openl2tp_multilink_join_ind(void) |
| 284 |
{ |
| 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); |
| 289 |
} |
| 290 |
} |
| 291 |
#endif |
| 292 |
|
| 293 |
/***************************************************************************** |
| 294 |
* Application init |
| 295 |
*****************************************************************************/ |
| 296 |
|
| 297 |
void plugin_init(void) |
| 298 |
{ |
| 299 |
old_pppol2tp_send_accm_hook = pppol2tp_send_accm_hook; |
| 300 |
pppol2tp_send_accm_hook = openl2tp_send_accm_ind; |
| 301 |
|
| 302 |
old_pppol2tp_ip_updown_hook = pppol2tp_ip_updown_hook; |
| 303 |
pppol2tp_ip_updown_hook = openl2tp_ppp_updown_ind; |
| 304 |
|
| 305 |
#ifdef PPP_WITH_MULTILINK |
| 306 |
old_multilink_join_hook = multilink_join_hook; |
| 307 |
multilink_join_hook = openl2tp_multilink_join_ind; |
| 308 |
#endif |
| 309 |
} |
| 310 |
|