codec: 3028a & 3026 record enable
[firefly-linux-kernel-4.4.55.git] / net / ipv4 / gre.c
1 /*
2  *      GRE over IPv4 demultiplexer driver
3  *
4  *      Authors: Dmitry Kozlov (xeb@mail.ru)
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  *
11  */
12
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/kmod.h>
16 #include <linux/skbuff.h>
17 #include <linux/in.h>
18 #include <linux/ip.h>
19 #include <linux/netdevice.h>
20 #include <linux/version.h>
21 #include <linux/spinlock.h>
22 #include <net/protocol.h>
23 #include <net/gre.h>
24
25
26 static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
27 static DEFINE_SPINLOCK(gre_proto_lock);
28
29 int gre_add_protocol(const struct gre_protocol *proto, u8 version)
30 {
31         if (version >= GREPROTO_MAX)
32                 goto err_out;
33
34         spin_lock(&gre_proto_lock);
35         if (gre_proto[version])
36                 goto err_out_unlock;
37
38         rcu_assign_pointer(gre_proto[version], proto);
39         spin_unlock(&gre_proto_lock);
40         return 0;
41
42 err_out_unlock:
43         spin_unlock(&gre_proto_lock);
44 err_out:
45         return -1;
46 }
47 EXPORT_SYMBOL_GPL(gre_add_protocol);
48
49 int gre_del_protocol(const struct gre_protocol *proto, u8 version)
50 {
51         if (version >= GREPROTO_MAX)
52                 goto err_out;
53
54         spin_lock(&gre_proto_lock);
55         if (rcu_dereference_protected(gre_proto[version],
56                         lockdep_is_held(&gre_proto_lock)) != proto)
57                 goto err_out_unlock;
58         rcu_assign_pointer(gre_proto[version], NULL);
59         spin_unlock(&gre_proto_lock);
60         synchronize_rcu();
61         return 0;
62
63 err_out_unlock:
64         spin_unlock(&gre_proto_lock);
65 err_out:
66         return -1;
67 }
68 EXPORT_SYMBOL_GPL(gre_del_protocol);
69
70 static int gre_rcv(struct sk_buff *skb)
71 {
72         const struct gre_protocol *proto;
73         u8 ver;
74         int ret;
75
76         if (!pskb_may_pull(skb, 12))
77                 goto drop;
78
79         ver = skb->data[1]&0x7f;
80         if (ver >= GREPROTO_MAX)
81                 goto drop;
82
83         rcu_read_lock();
84         proto = rcu_dereference(gre_proto[ver]);
85         if (!proto || !proto->handler)
86                 goto drop_unlock;
87         ret = proto->handler(skb);
88         rcu_read_unlock();
89         return ret;
90
91 drop_unlock:
92         rcu_read_unlock();
93 drop:
94         kfree_skb(skb);
95         return NET_RX_DROP;
96 }
97
98 static void gre_err(struct sk_buff *skb, u32 info)
99 {
100         const struct gre_protocol *proto;
101         const struct iphdr *iph = (const struct iphdr *)skb->data;
102         u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
103
104         if (ver >= GREPROTO_MAX)
105                 return;
106
107         rcu_read_lock();
108         proto = rcu_dereference(gre_proto[ver]);
109         if (proto && proto->err_handler)
110                 proto->err_handler(skb, info);
111         rcu_read_unlock();
112 }
113
114 static const struct net_protocol net_gre_protocol = {
115         .handler     = gre_rcv,
116         .err_handler = gre_err,
117         .netns_ok    = 1,
118 };
119
120 static int __init gre_init(void)
121 {
122         pr_info("GRE over IPv4 demultiplexor driver");
123
124         if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
125                 pr_err("gre: can't add protocol\n");
126                 return -EAGAIN;
127         }
128
129         return 0;
130 }
131
132 static void __exit gre_exit(void)
133 {
134         inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
135 }
136
137 module_init(gre_init);
138 module_exit(gre_exit);
139
140 MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
141 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
142 MODULE_LICENSE("GPL");
143