blob: ebcc975c1b2be3ea49cb76efd71069e4660a6791 [file] [log] [blame]
Bharath M Kumar0d87e912013-08-12 18:32:57 +05301/*
2 **************************************************************************
Zac Livingston866b0e22013-10-23 18:14:17 -06003 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
Bharath M Kumar0d87e912013-08-12 18:32:57 +05304 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 **************************************************************************
15 */
16
17/*
18 * nss_tun6rd.c
19 *
20 * This file is the NSS 6rd tunnel module
21 * ------------------------REVISION HISTORY-----------------------------
22 * Qualcomm Atheros 15/sep/2013 Created
23 */
24
25#include <linux/types.h>
26#include <linux/ip.h>
27#include <linux/tcp.h>
28#include <linux/module.h>
29#include <linux/skbuff.h>
30#include <net/ipv6.h>
31#include <net/ipip.h>
32#include <linux/if_arp.h>
33#include "nss_api_if.h"
34#include "nss_hlos_if.h"
35
Bharath M Kumar0d87e912013-08-12 18:32:57 +053036/*
37 * NSS tun6rd debug macros
38 */
39#if (NSS_TUN6RD_DEBUG_LEVEL < 1)
40#define nss_tun6rd_assert(fmt, args...)
41#else
42#define nss_tun6d_assert(c) if (!(c)) { BUG_ON(!(c)); }
43#endif
44
45#if (NSS_TUN6RD_DEBUG_LEVEL < 2)
46#define nss_tun6rd_error(fmt, args...)
47#else
48#define nss_tun6rd_error(fmt, args...) printk(KERN_WARNING "nss tun6rd:"fmt, ##args)
49#endif
50
51#if (NSS_TUN6RD_DEBUG_LEVEL < 3)
52#define nss_tun6rd_warning(fmt, args...)
53#else
54#define nss_tun6rd_warning(fmt, args...) printk(KERN_WARNING "nss tun6rd:"fmt, ##args)
55#endif
56
57#if (NSS_TUN6RD_DEBUG_LEVEL < 4)
58#define nss_tun6rd_info(fmt, args...)
59#else
60#define nss_tun6rd_info(fmt, args...) printk(KERN_INFO "nss tun6rd :"fmt, ##args)
61#endif
62
63#if (NSS_TUN6RD_DEBUG_LEVEL < 5)
64#define nss_tun6rd_trace(fmt, args...)
65#else
66#define nss_tun6rd_trace(fmt, args...) printk(KERN_DEBUG "nss tun6rd :"fmt, ##args)
67#endif
68
69void nss_tun6rd_exception(void *ctx, void *buf);
Radhakrishna Jiguru5ad493d2013-11-25 20:17:54 +053070void nss_tun6rd_event_receive(void *ctx, nss_tun6rd_event_t ev_type,
71 void *os_buf, uint32_t len);
Bharath M Kumar0d87e912013-08-12 18:32:57 +053072
73enum tun6rd_metadata_types {
74 TUN6RD_METADATA_TYPE_IF_UP,
75 TUN6RD_METADATA_TYPE_IF_DOWN
76};
77
78/*
79 * 6rd configuration command structure
80 */
81struct nss_tunnel_6rd_cfg{
82 uint32_t prefix[4]; /*6rd prefix */
83 uint32_t relay_prefix; /* Relay prefix */
84 uint16_t prefixlen; /* 6rd prefix len */
85 uint16_t relay_prefixlen; /* Relay prefix length*/
86 uint32_t saddr; /* Tunnel source address */
87 uint32_t daddr; /* Tunnel destination addresss */
88 uint8_t tos; /* Tunnel tos field */
89 uint8_t ttl; /* Tunnel ttl field */
90
91};
92
93/*
94 * 6rd tunnel interface down command structure
95 */
96struct tun6rd_if_down_param{
97 uint32_t prefix[4]; /*Tunnel 6rd prefix */
98};
99
100/*
101 * 6rd Tunnel generic param
102 */
103struct nss_tunnel_6rd_param {
104 enum tun6rd_metadata_types type;
105 union {
106 struct nss_tunnel_6rd_cfg cfg;
107 struct tun6rd_if_down_param ifdown_param;
108 }sub;
109};
110
111/*
112 * 6rd tunnel host instance
113 */
114struct nss_tun6rd_tunnel{
115 void *nss_ctx;
116 uint32_t if_num;
117 struct net_device *netdev;
118 uint32_t device_up;
119};
120
Radhakrishna Jiguru5ad493d2013-11-25 20:17:54 +0530121/*
122 * 6rd tunnel stats
123 */
124struct nss_tun6rd_stats{
125 uint32_t rx_packets;
126 uint32_t rx_bytes;
127 uint32_t tx_packets;
128 uint32_t tx_bytes;
129};
130
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530131struct nss_tun6rd_tunnel g_tun6rd;
132
133/*
134 * Internal function
135 */
136static int
137nss_tun6rd_dev_event(struct notifier_block *nb,
138 unsigned long event,
139 void *dev);
140
141/*
142 * Linux Net device Notifier
143 */
144struct notifier_block nss_tun6rd_notifier = {
145 .notifier_call = nss_tun6rd_dev_event,
146};
147
148/*
149 * nss_tun6rd_dev_up()
150 * 6RD Tunnel device i/f up handler
151 */
152void nss_tun6rd_dev_up( struct net_device * netdev)
153{
154 struct ip_tunnel *tunnel;
155 struct ip_tunnel_6rd_parm *ip6rd;
156 const struct iphdr *tiph;
157 struct nss_tunnel_6rd_param tun6rdparam;
158 struct nss_tunnel_6rd_cfg *tun6rdcfg;
159 nss_tx_status_t status;
160
161 /*
162 * Validate netdev for ipv6-in-ipv4 Tunnel
163 */
164 if (netdev->type != ARPHRD_SIT ) {
165 return;
166 }
167
168 tunnel = (struct ip_tunnel*)netdev_priv(netdev);
169 ip6rd = &tunnel->ip6rd;
170
171 /*
172 * Valid 6rd Tunnel Check
173 * 1. 6rd Prefix len should be non zero
174 * 2. Relay prefix length should not be greater then 32
175 * 3. To allow for stateless address auto-configuration on the CE LAN side,
176 * 6rd delegated prefix SHOULD be /64 or shorter.
177 */
178 if ((ip6rd->prefixlen == 0 )
179 || (ip6rd->relay_prefixlen > 32)
180 || (ip6rd->prefixlen
181 + (32 - ip6rd->relay_prefixlen) > 64)){
182
183 nss_tun6rd_error("Invalid 6rd argument prefix len %d \
184 relayprefix len %d \n",
185 ip6rd->prefixlen,ip6rd->relay_prefixlen);
186 return;
187 }
188
189 nss_tun6rd_info(" Valid 6rd Tunnel Prefix %x %x %x %x \n \
190 prefix len %d relay_prefix %d relay_prefixlen %d \n",
191 ip6rd->prefix.s6_addr32[0],ip6rd->prefix.s6_addr32[1],
192 ip6rd->prefix.s6_addr32[2],ip6rd->prefix.s6_addr32[3],
193 ip6rd->prefixlen, ip6rd->relay_prefix,
194 ip6rd->relay_prefixlen);
195
196 /*
197 * Prepare The Tunnel configuration parameter to send to nss
198 */
199 memset( &tun6rdparam, 0, sizeof(struct nss_tunnel_6rd_param));
200 tun6rdparam.type = TUN6RD_METADATA_TYPE_IF_UP;
201 tun6rdcfg = (struct nss_tunnel_6rd_cfg *)&tun6rdparam.sub.cfg;
202
203 /*
204 * Find the Tunnel device ipHeader info
205 */
206 tiph = &tunnel->parms.iph ;
207 nss_tun6rd_trace(" Tunnel Param srcaddr %x daddr %x ttl %d tos %x\n",
208 tiph->saddr, tiph->daddr,tiph->ttl,tiph->tos);
209
210 if(tiph->saddr == 0) {
211 nss_tun6rd_error("Tunnel src address not configured %x\n",
212 tiph->saddr);
213 return;
214 }
215
216 if (tiph->daddr == 0) {
217 nss_tun6rd_error("Tunnel dest address not configured %x\n",
218 tiph->daddr);
219 return;
220 }
221
222 tun6rdcfg->prefixlen = ip6rd->prefixlen;
223 tun6rdcfg->relay_prefix = ip6rd->relay_prefix;
224 tun6rdcfg->relay_prefixlen = ip6rd->relay_prefixlen;
225 tun6rdcfg->saddr = ntohl(tiph->saddr);
226 tun6rdcfg->daddr = ntohl(tiph->daddr);
227 tun6rdcfg->prefix[0] = ntohl(ip6rd->prefix.s6_addr32[0]);
228 tun6rdcfg->prefix[1] = ntohl(ip6rd->prefix.s6_addr32[1]);
229 tun6rdcfg->prefix[2] = ntohl(ip6rd->prefix.s6_addr32[2]);
230 tun6rdcfg->prefix[3] = ntohl(ip6rd->prefix.s6_addr32[3]);
231 tun6rdcfg->ttl = tiph->ttl;
232 tun6rdcfg->tos = tiph->tos;
233
234 nss_tun6rd_trace(" 6rd Tunnel info \n");
235 nss_tun6rd_trace(" saddr %x daddr %d ttl %x tos %x \n",
236 tiph->saddr, tiph->daddr, tiph->ttl, tiph->tos);
237 nss_tun6rd_trace(" Prefix %x:%x:%x:%x Prefix len %d \n",
238 ip6rd->prefix.s6_addr32[0], ip6rd->prefix.s6_addr32[1],
239 ip6rd->prefix.s6_addr32[2], ip6rd->prefix.s6_addr32[3],
240 ip6rd->prefixlen);
241 nss_tun6rd_trace("Relay Prefix %x Len %d\n",
242 ip6rd->relay_prefix, ip6rd->relay_prefixlen);
243
244 /*
245 * Register 6rd tunnel with NSS
246 */
247 g_tun6rd.nss_ctx = nss_register_tun6rd_if(g_tun6rd.if_num,
248 nss_tun6rd_exception,
Radhakrishna Jiguru5ad493d2013-11-25 20:17:54 +0530249 nss_tun6rd_event_receive,
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530250 netdev);
251 if (g_tun6rd.nss_ctx == NULL) {
252 nss_tun6rd_trace("nss_register_tun6rd_if Failed \n");
253 return;
254 } else {
255 nss_tun6rd_trace("nss_register_tun6rd_if Success \n");
256 }
257
258 nss_tun6rd_trace("Sending 6rd tunnel i/f up command to NSS %x \n",
259 (int)g_tun6rd.nss_ctx);
260
261 /*
262 * Send 6rd Tunnel UP command to NSS
263 */
264 status = nss_tx_generic_if_buf(g_tun6rd.nss_ctx,
265 g_tun6rd.if_num,
Radhakrishna Jiguru5ad493d2013-11-25 20:17:54 +0530266 (uint8_t *)&tun6rdparam,
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530267 sizeof(struct nss_tunnel_6rd_param));
268
269 if (status != NSS_TX_SUCCESS) {
270 nss_tun6rd_error("Tunnel up command error %d \n", status);
271 return;
272 }
273
274 g_tun6rd.device_up = 1;
275}
276
277/*
278 * nss_tun6rd_dev_down()
279 * 6RD Tunnel device i/f down handler
280 */
281void nss_tun6rd_dev_down( struct net_device * netdev)
282{
283 struct ip_tunnel *tunnel;
284 struct ip_tunnel_6rd_parm *ip6rd;
285 struct nss_tunnel_6rd_param tun6rdparam;
286 struct tun6rd_if_down_param *ifdown;
287 nss_tx_status_t status;
288
289 /*
290 * Check if tunnel 6rd is registered ?
291 */
292 if (g_tun6rd.nss_ctx == NULL) {
293 return;
294 }
295
296 /*
297 * Validate netdev for ipv6-in-ipv4 Tunnel
298 */
299 if (netdev->type != ARPHRD_SIT ) {
300 return;
301 }
302
303 tunnel = (struct ip_tunnel*)netdev_priv(netdev);
304 ip6rd = &tunnel->ip6rd;
305
306 /*
307 * Valid 6rd Tunnel Check
308 */
309 if ((ip6rd->prefixlen == 0 )
310 || (ip6rd->relay_prefixlen > 32 )
311 || (ip6rd->prefixlen
312 + (32 - ip6rd->relay_prefixlen) > 64)){
313
314 nss_tun6rd_error("Invalid 6rd argument prefix len %d \
315 relayprefix len %d \n",
316 ip6rd->prefixlen,ip6rd->relay_prefixlen);
317 return;
318 }
319
320 memset( &tun6rdparam, 0, sizeof(struct nss_tunnel_6rd_param));
321 tun6rdparam.type = TUN6RD_METADATA_TYPE_IF_DOWN;
322 ifdown = (struct tun6rd_if_down_param *)&tun6rdparam.sub.ifdown_param;
323 ifdown->prefix[0] = ntohl(ip6rd->prefix.s6_addr32[0]);
324 ifdown->prefix[1] = ntohl(ip6rd->prefix.s6_addr32[1]);
325 ifdown->prefix[2] = ntohl(ip6rd->prefix.s6_addr32[2]);
326 ifdown->prefix[3] = ntohl(ip6rd->prefix.s6_addr32[3]);
327
328 nss_tun6rd_trace(" Prefix %x:%x:%x:%x Prefix len %d \n",
329 ip6rd->prefix.s6_addr32[0], ip6rd->prefix.s6_addr32[1],
330 ip6rd->prefix.s6_addr32[2], ip6rd->prefix.s6_addr32[3],
331 ip6rd->prefixlen);
332
333
334 nss_tun6rd_trace("Sending Tunnle 6rd Down command %x \n",g_tun6rd.if_num);
335 status = nss_tx_generic_if_buf(g_tun6rd.nss_ctx,
336 g_tun6rd.if_num,
337 (uint8_t *)&tun6rdparam,
338 sizeof(struct nss_tunnel_6rd_param));
339
340 if (status != NSS_TX_SUCCESS) {
341 nss_tun6rd_error("Tunnel down command error %d \n", status);
342 return;
343 }
344
345 /*
346 * Un-Register 6rd tunnel with NSS
347 */
348 nss_unregister_tun6rd_if(g_tun6rd.if_num);
349 g_tun6rd.nss_ctx = NULL;
350 g_tun6rd.device_up = 0;
351 return;
352}
353
354/*
355 * nss_tun6rd_dev_event()
356 * Net device notifier for 6rd module
357 */
358static int nss_tun6rd_dev_event(struct notifier_block *nb,
359 unsigned long event, void *dev)
360{
361 struct net_device *netdev = (struct net_device *)dev;
362
363 nss_tun6rd_trace("%s\n",__FUNCTION__);
364 switch (event) {
365 case NETDEV_UP:
366 nss_tun6rd_trace(" NETDEV_UP :event %lu name %s \n",
367 event,netdev->name);
368 nss_tun6rd_dev_up(netdev);
369 break;
370
371 case NETDEV_DOWN:
372 nss_tun6rd_trace(" NETDEV_DOWN :event %lu name %s \n",
373 event,netdev->name);
374 nss_tun6rd_dev_down(netdev);
375 break;
376
377 default:
378 nss_tun6rd_trace("Unhandled notifier dev %s event %x \n",
379 netdev->name,(int)event);
380 break;
381 }
382
383 return NOTIFY_DONE;
384}
385
386/*
387 * nss_tun6rd_exception()
388 * Exception handler registered to NSS driver
389 */
390void nss_tun6rd_exception(void *ctx, void *buf)
391{
392 struct net_device *dev = (struct net_device *)ctx;
393 struct sk_buff *skb = (struct sk_buff *)buf;
394 const struct iphdr *iph;
395
396 skb->dev = dev;
397 nss_tun6rd_info("received - %d bytes name %s ver %x \n",
398 skb->len,dev->name,skb->data[0]);
399
400 iph = (const struct iphdr *)skb->data;
401
402 /*
403 * Packet after Decap/Encap Did not find the Rule.
404 */
405 if (iph->version == 4) {
Bharath M Kumara0194272013-12-16 16:11:32 +0530406 if(iph->protocol == IPPROTO_IPV6){
407 skb_pull(skb, sizeof(struct iphdr));
408 skb->protocol = htons(ETH_P_IPV6);
409 skb_reset_network_header(skb);
410 skb->pkt_type = PACKET_HOST;
411 skb->ip_summed = CHECKSUM_NONE;
412 dev_queue_xmit(skb);
413 return;
414 }
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530415 skb->protocol = htons(ETH_P_IP);
416 } else {
417 skb->protocol = htons(ETH_P_IPV6);
418 }
419
420 skb_reset_network_header(skb);
421 skb->pkt_type = PACKET_HOST;
422 skb->skb_iif = dev->ifindex;
423 skb->ip_summed = CHECKSUM_NONE;
424 netif_receive_skb(skb);
425}
426
427/*
Radhakrishna Jiguru5ad493d2013-11-25 20:17:54 +0530428 * nss_tun6rd_update_dev_stats
429 * Update the Dev stats received from NetAp
430 */
431static void nss_tun6rd_update_dev_stats(struct net_device *dev,
432 struct nss_tun6rd_stats_sync *tun6rdstats)
433{
434 void *ptr;
435 struct nss_tun6rd_stats stats;
436
437 stats.rx_packets = tun6rdstats->rx_packets;
438 stats.rx_bytes = tun6rdstats->rx_bytes;
439 stats.tx_packets = tun6rdstats->tx_packets;
440 stats.tx_bytes = tun6rdstats->tx_bytes;
441 ptr = (void *)&stats;
442 ipip6_update_offload_stats(dev, ptr);
443}
444
445/**
446 * @brief Event Callback to receive events from NSS
447 * @param[in] pointer to net device context
448 * @param[in] event type
449 * @param[in] pointer to buffer
450 * @param[in] length of buffer
451 * @return Returns void
452 */
453void nss_tun6rd_event_receive(void *if_ctx, nss_tun6rd_event_t ev_type,
454 void *os_buf, uint32_t len)
455{
456 struct net_device *netdev = NULL;
457 netdev = (struct net_device *)if_ctx;
458
459 switch (ev_type) {
460 case NSS_TUN6RD_EVENT_STATS:
461 nss_tun6rd_update_dev_stats(netdev, (struct nss_tun6rd_stats_sync *)os_buf );
462 break;
463
464 default:
465 nss_tun6rd_info("%s: Unknown Event from NSS",
466 __FUNCTION__);
467 break;
468 }
469}
470
471/*
Bharath M Kumar0d87e912013-08-12 18:32:57 +0530472 * nss_tun6rd_init_module()
473 * Tunnel 6rd module init function
474 */
475int __init nss_tun6rd_init_module(void)
476{
477 nss_tun6rd_info("module (platform - IPQ806x , Build - %s:%s) loaded\n",
478 __DATE__, __TIME__);
479
480 register_netdevice_notifier(&nss_tun6rd_notifier);
481 nss_tun6rd_trace("Netdev Notifier registerd \n");
482
483 g_tun6rd.if_num = NSS_TUNRD_IF_NUMBER;
484 g_tun6rd.netdev = NULL;
485 g_tun6rd.device_up = 0;
486 g_tun6rd.nss_ctx = NULL;
487
488 return 0;
489}
490
491/*
492 * nss_tun6rd_exit_module()
493 * Tunnel 6rd module exit function
494 */
495void __exit nss_tun6rd_exit_module(void)
496{
497
498 unregister_netdevice_notifier(&nss_tun6rd_notifier);
499 nss_tun6rd_info("module unloaded\n");
500}
501
502module_init(nss_tun6rd_init_module);
503module_exit(nss_tun6rd_exit_module);
504
505MODULE_LICENSE("Dual BSD/GPL");
506MODULE_DESCRIPTION("NSS tun6rd offload manager");