datapath: Move find_route() to compat.h
[sliver-openvswitch.git] / datapath / compat.h
1 /*
2  * Copyright (c) 2007-2012 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #ifndef COMPAT_H
20 #define COMPAT_H 1
21
22 #include <linux/in.h>
23 #include <linux/in_route.h>
24 #include <linux/netlink.h>
25 #include <net/route.h>
26 #include <net/xfrm.h>
27
28
29 #ifndef HAVE_NLA_NUL_STRING
30 static inline int CHECK_NUL_STRING(struct nlattr *attr, int maxlen)
31 {
32         char *s;
33         int len;
34         if (!attr)
35                 return 0;
36
37         len = nla_len(attr);
38         if (len >= maxlen)
39                 return -EINVAL;
40
41         s = nla_data(attr);
42         if (s[len - 1] != '\0')
43                 return -EINVAL;
44
45         return 0;
46 }
47 #else
48 static inline int CHECK_NUL_STRING(struct nlattr *attr, int maxlen)
49 {
50         return 0;
51 }
52 #endif  /* !HAVE_NLA_NUL_STRING */
53
54 static inline void skb_clear_rxhash(struct sk_buff *skb)
55 {
56 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
57         skb->rxhash = 0;
58 #endif
59 }
60
61 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
62 #define GENL_SOCK(net) (genl_sock)
63 #define SET_NETNSOK
64 #else
65 #define GENL_SOCK(net) ((net)->genl_sock)
66 #define SET_NETNSOK    .netnsok = true,
67 #endif
68
69 #ifdef HAVE_PARALLEL_OPS
70 #define SET_PARALLEL_OPS        .parallel_ops = true,
71 #else
72 #define SET_PARALLEL_OPS
73 #endif
74
75
76 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
77 #ifdef CONFIG_NETFILTER
78 static inline u32 skb_get_mark(struct sk_buff *skb)
79 {
80         return skb->nfmark;
81 }
82
83 static inline void skb_set_mark(struct sk_buff *skb, u32 mark)
84 {
85         skb->nfmark = mark;
86 }
87 #else /* CONFIG_NETFILTER */
88 static inline u32 skb_get_mark(struct sk_buff *skb)
89 {
90         return 0;
91 }
92
93 static inline void skb_set_mark(struct sk_buff *skb, u32 mark)
94 {
95 }
96 #endif
97 #else /* before 2.6.20 */
98 static inline u32 skb_get_mark(struct sk_buff *skb)
99 {
100         return skb->mark;
101 }
102
103 static inline void skb_set_mark(struct sk_buff *skb, u32 mark)
104 {
105         skb->mark = mark;
106 }
107 #endif /* after 2.6.20 */
108
109 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
110 #define rt_dst(rt) (rt->dst)
111 #else
112 #define rt_dst(rt) (rt->u.dst)
113 #endif
114
115 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
116 #define inet_sport(sk)  (inet_sk(sk)->sport)
117 #else
118 #define inet_sport(sk)  (inet_sk(sk)->inet_sport)
119 #endif
120
121 static inline struct rtable *find_route(struct net *net,
122                                         __be32 *saddr, __be32 daddr,
123                                         u8 ipproto, u8 tos, u32 skb_mark)
124 {
125         struct rtable *rt;
126         /* Tunnel configuration keeps DSCP part of TOS bits, But Linux
127          * router expect RT_TOS bits only. */
128
129 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
130         struct flowi fl = { .nl_u = { .ip4_u = {
131                                         .daddr = daddr,
132                                         .saddr = *saddr,
133 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
134                                         .fwmark = skb_mark,
135 #endif
136                                         .tos   = RT_TOS(tos) } },
137 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
138                                         .mark = skb_mark,
139 #endif
140                                         .proto = ipproto };
141
142         if (unlikely(ip_route_output_key(net, &rt, &fl)))
143                 return ERR_PTR(-EADDRNOTAVAIL);
144         *saddr = fl.nl_u.ip4_u.saddr;
145         return rt;
146 #else
147         struct flowi4 fl = { .daddr = daddr,
148                              .saddr = *saddr,
149                              .flowi4_tos = RT_TOS(tos),
150                              .flowi4_mark = skb_mark,
151                              .flowi4_proto = ipproto };
152
153         rt = ip_route_output_key(net, &fl);
154         *saddr = fl.saddr;
155         return rt;
156 #endif
157 }
158 #endif /* compat.h */