]>
Commit | Line | Data |
---|---|---|
d342894c | 1 | /* |
eb5ce439 | 2 | * VXLAN: Virtual eXtensible Local Area Network |
d342894c | 3 | * |
4 | * Copyright (c) 2012 Vyatta Inc. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * TODO | |
11 | * - use IANA UDP port number (when defined) | |
12 | * - IPv6 (not in RFC) | |
13 | */ | |
14 | ||
15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
16 | ||
17 | #include <linux/kernel.h> | |
18 | #include <linux/types.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/errno.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/skbuff.h> | |
23 | #include <linux/rculist.h> | |
24 | #include <linux/netdevice.h> | |
25 | #include <linux/in.h> | |
26 | #include <linux/ip.h> | |
27 | #include <linux/udp.h> | |
28 | #include <linux/igmp.h> | |
29 | #include <linux/etherdevice.h> | |
30 | #include <linux/if_ether.h> | |
d342894c | 31 | #include <linux/hash.h> |
1b13c97f | 32 | #include <linux/ethtool.h> |
e4f67add DS |
33 | #include <net/arp.h> |
34 | #include <net/ndisc.h> | |
d342894c | 35 | #include <net/ip.h> |
c5441932 | 36 | #include <net/ip_tunnels.h> |
d342894c | 37 | #include <net/icmp.h> |
38 | #include <net/udp.h> | |
39 | #include <net/rtnetlink.h> | |
40 | #include <net/route.h> | |
41 | #include <net/dsfield.h> | |
42 | #include <net/inet_ecn.h> | |
43 | #include <net/net_namespace.h> | |
44 | #include <net/netns/generic.h> | |
45 | ||
46 | #define VXLAN_VERSION "0.1" | |
47 | ||
48 | #define VNI_HASH_BITS 10 | |
49 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) | |
50 | #define FDB_HASH_BITS 8 | |
51 | #define FDB_HASH_SIZE (1<<FDB_HASH_BITS) | |
52 | #define FDB_AGE_DEFAULT 300 /* 5 min */ | |
53 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ | |
54 | ||
55 | #define VXLAN_N_VID (1u << 24) | |
56 | #define VXLAN_VID_MASK (VXLAN_N_VID - 1) | |
52b702ff AD |
57 | /* IP header + UDP + VXLAN + Ethernet header */ |
58 | #define VXLAN_HEADROOM (20 + 8 + 8 + 14) | |
d342894c | 59 | |
60 | #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */ | |
61 | ||
62 | /* VXLAN protocol header */ | |
63 | struct vxlanhdr { | |
64 | __be32 vx_flags; | |
65 | __be32 vx_vni; | |
66 | }; | |
67 | ||
68 | /* UDP port for VXLAN traffic. */ | |
69 | static unsigned int vxlan_port __read_mostly = 8472; | |
70 | module_param_named(udp_port, vxlan_port, uint, 0444); | |
71 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); | |
72 | ||
73 | static bool log_ecn_error = true; | |
74 | module_param(log_ecn_error, bool, 0644); | |
75 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
76 | ||
77 | /* per-net private data for this module */ | |
78 | static unsigned int vxlan_net_id; | |
79 | struct vxlan_net { | |
80 | struct socket *sock; /* UDP encap socket */ | |
81 | struct hlist_head vni_list[VNI_HASH_SIZE]; | |
82 | }; | |
83 | ||
6681712d DS |
84 | struct vxlan_rdst { |
85 | struct rcu_head rcu; | |
86 | __be32 remote_ip; | |
87 | __be16 remote_port; | |
88 | u32 remote_vni; | |
89 | u32 remote_ifindex; | |
90 | struct vxlan_rdst *remote_next; | |
91 | }; | |
92 | ||
d342894c | 93 | /* Forwarding table entry */ |
94 | struct vxlan_fdb { | |
95 | struct hlist_node hlist; /* linked list of entries */ | |
96 | struct rcu_head rcu; | |
97 | unsigned long updated; /* jiffies */ | |
98 | unsigned long used; | |
6681712d | 99 | struct vxlan_rdst remote; |
d342894c | 100 | u16 state; /* see ndm_state */ |
101 | u8 eth_addr[ETH_ALEN]; | |
102 | }; | |
103 | ||
d342894c | 104 | /* Pseudo network device */ |
105 | struct vxlan_dev { | |
106 | struct hlist_node hlist; | |
107 | struct net_device *dev; | |
d342894c | 108 | __u32 vni; /* virtual network id */ |
109 | __be32 gaddr; /* multicast group */ | |
110 | __be32 saddr; /* source address */ | |
111 | unsigned int link; /* link to multicast over */ | |
05f47d69 | 112 | __u16 port_min; /* source port range */ |
113 | __u16 port_max; | |
d342894c | 114 | __u8 tos; /* TOS override */ |
115 | __u8 ttl; | |
e4f67add | 116 | u32 flags; /* VXLAN_F_* below */ |
d342894c | 117 | |
118 | unsigned long age_interval; | |
119 | struct timer_list age_timer; | |
120 | spinlock_t hash_lock; | |
121 | unsigned int addrcnt; | |
122 | unsigned int addrmax; | |
d342894c | 123 | |
124 | struct hlist_head fdb_head[FDB_HASH_SIZE]; | |
125 | }; | |
126 | ||
e4f67add DS |
127 | #define VXLAN_F_LEARN 0x01 |
128 | #define VXLAN_F_PROXY 0x02 | |
129 | #define VXLAN_F_RSC 0x04 | |
130 | #define VXLAN_F_L2MISS 0x08 | |
131 | #define VXLAN_F_L3MISS 0x10 | |
132 | ||
d342894c | 133 | /* salt for hash table */ |
134 | static u32 vxlan_salt __read_mostly; | |
135 | ||
136 | static inline struct hlist_head *vni_head(struct net *net, u32 id) | |
137 | { | |
138 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
139 | ||
140 | return &vn->vni_list[hash_32(id, VNI_HASH_BITS)]; | |
141 | } | |
142 | ||
143 | /* Look up VNI in a per net namespace table */ | |
144 | static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id) | |
145 | { | |
146 | struct vxlan_dev *vxlan; | |
d342894c | 147 | |
b67bfe0d | 148 | hlist_for_each_entry_rcu(vxlan, vni_head(net, id), hlist) { |
d342894c | 149 | if (vxlan->vni == id) |
150 | return vxlan; | |
151 | } | |
152 | ||
153 | return NULL; | |
154 | } | |
155 | ||
156 | /* Fill in neighbour message in skbuff. */ | |
157 | static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, | |
158 | const struct vxlan_fdb *fdb, | |
6681712d DS |
159 | u32 portid, u32 seq, int type, unsigned int flags, |
160 | const struct vxlan_rdst *rdst) | |
d342894c | 161 | { |
162 | unsigned long now = jiffies; | |
163 | struct nda_cacheinfo ci; | |
164 | struct nlmsghdr *nlh; | |
165 | struct ndmsg *ndm; | |
e4f67add | 166 | bool send_ip, send_eth; |
d342894c | 167 | |
168 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); | |
169 | if (nlh == NULL) | |
170 | return -EMSGSIZE; | |
171 | ||
172 | ndm = nlmsg_data(nlh); | |
173 | memset(ndm, 0, sizeof(*ndm)); | |
e4f67add DS |
174 | |
175 | send_eth = send_ip = true; | |
176 | ||
177 | if (type == RTM_GETNEIGH) { | |
178 | ndm->ndm_family = AF_INET; | |
6681712d | 179 | send_ip = rdst->remote_ip != htonl(INADDR_ANY); |
e4f67add DS |
180 | send_eth = !is_zero_ether_addr(fdb->eth_addr); |
181 | } else | |
182 | ndm->ndm_family = AF_BRIDGE; | |
d342894c | 183 | ndm->ndm_state = fdb->state; |
184 | ndm->ndm_ifindex = vxlan->dev->ifindex; | |
185 | ndm->ndm_flags = NTF_SELF; | |
186 | ndm->ndm_type = NDA_DST; | |
187 | ||
e4f67add | 188 | if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr)) |
d342894c | 189 | goto nla_put_failure; |
190 | ||
6681712d DS |
191 | if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip)) |
192 | goto nla_put_failure; | |
193 | ||
194 | if (rdst->remote_port && rdst->remote_port != vxlan_port && | |
195 | nla_put_be16(skb, NDA_PORT, rdst->remote_port)) | |
196 | goto nla_put_failure; | |
197 | if (rdst->remote_vni != vxlan->vni && | |
198 | nla_put_be32(skb, NDA_VNI, rdst->remote_vni)) | |
199 | goto nla_put_failure; | |
200 | if (rdst->remote_ifindex && | |
201 | nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) | |
d342894c | 202 | goto nla_put_failure; |
203 | ||
204 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); | |
205 | ci.ndm_confirmed = 0; | |
206 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); | |
207 | ci.ndm_refcnt = 0; | |
208 | ||
209 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) | |
210 | goto nla_put_failure; | |
211 | ||
212 | return nlmsg_end(skb, nlh); | |
213 | ||
214 | nla_put_failure: | |
215 | nlmsg_cancel(skb, nlh); | |
216 | return -EMSGSIZE; | |
217 | } | |
218 | ||
219 | static inline size_t vxlan_nlmsg_size(void) | |
220 | { | |
221 | return NLMSG_ALIGN(sizeof(struct ndmsg)) | |
222 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ | |
223 | + nla_total_size(sizeof(__be32)) /* NDA_DST */ | |
6681712d DS |
224 | + nla_total_size(sizeof(__be32)) /* NDA_PORT */ |
225 | + nla_total_size(sizeof(__be32)) /* NDA_VNI */ | |
226 | + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ | |
d342894c | 227 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
228 | } | |
229 | ||
230 | static void vxlan_fdb_notify(struct vxlan_dev *vxlan, | |
231 | const struct vxlan_fdb *fdb, int type) | |
232 | { | |
233 | struct net *net = dev_net(vxlan->dev); | |
234 | struct sk_buff *skb; | |
235 | int err = -ENOBUFS; | |
236 | ||
237 | skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); | |
238 | if (skb == NULL) | |
239 | goto errout; | |
240 | ||
6681712d | 241 | err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote); |
d342894c | 242 | if (err < 0) { |
243 | /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ | |
244 | WARN_ON(err == -EMSGSIZE); | |
245 | kfree_skb(skb); | |
246 | goto errout; | |
247 | } | |
248 | ||
249 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); | |
250 | return; | |
251 | errout: | |
252 | if (err < 0) | |
253 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); | |
254 | } | |
255 | ||
e4f67add DS |
256 | static void vxlan_ip_miss(struct net_device *dev, __be32 ipa) |
257 | { | |
258 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
259 | struct vxlan_fdb f; | |
260 | ||
261 | memset(&f, 0, sizeof f); | |
262 | f.state = NUD_STALE; | |
6681712d DS |
263 | f.remote.remote_ip = ipa; /* goes to NDA_DST */ |
264 | f.remote.remote_vni = VXLAN_N_VID; | |
e4f67add DS |
265 | |
266 | vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); | |
267 | } | |
268 | ||
269 | static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) | |
270 | { | |
271 | struct vxlan_fdb f; | |
272 | ||
273 | memset(&f, 0, sizeof f); | |
274 | f.state = NUD_STALE; | |
275 | memcpy(f.eth_addr, eth_addr, ETH_ALEN); | |
276 | ||
277 | vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH); | |
278 | } | |
279 | ||
d342894c | 280 | /* Hash Ethernet address */ |
281 | static u32 eth_hash(const unsigned char *addr) | |
282 | { | |
283 | u64 value = get_unaligned((u64 *)addr); | |
284 | ||
285 | /* only want 6 bytes */ | |
286 | #ifdef __BIG_ENDIAN | |
d342894c | 287 | value >>= 16; |
321fb991 | 288 | #else |
289 | value <<= 16; | |
d342894c | 290 | #endif |
291 | return hash_64(value, FDB_HASH_BITS); | |
292 | } | |
293 | ||
294 | /* Hash chain to use given mac address */ | |
295 | static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, | |
296 | const u8 *mac) | |
297 | { | |
298 | return &vxlan->fdb_head[eth_hash(mac)]; | |
299 | } | |
300 | ||
301 | /* Look up Ethernet address in forwarding table */ | |
302 | static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, | |
303 | const u8 *mac) | |
304 | ||
305 | { | |
306 | struct hlist_head *head = vxlan_fdb_head(vxlan, mac); | |
307 | struct vxlan_fdb *f; | |
d342894c | 308 | |
b67bfe0d | 309 | hlist_for_each_entry_rcu(f, head, hlist) { |
d342894c | 310 | if (compare_ether_addr(mac, f->eth_addr) == 0) |
311 | return f; | |
312 | } | |
313 | ||
314 | return NULL; | |
315 | } | |
316 | ||
6681712d DS |
317 | /* Add/update destinations for multicast */ |
318 | static int vxlan_fdb_append(struct vxlan_fdb *f, | |
319 | __be32 ip, __u32 port, __u32 vni, __u32 ifindex) | |
320 | { | |
321 | struct vxlan_rdst *rd_prev, *rd; | |
322 | ||
323 | rd_prev = NULL; | |
324 | for (rd = &f->remote; rd; rd = rd->remote_next) { | |
325 | if (rd->remote_ip == ip && | |
326 | rd->remote_port == port && | |
327 | rd->remote_vni == vni && | |
328 | rd->remote_ifindex == ifindex) | |
329 | return 0; | |
330 | rd_prev = rd; | |
331 | } | |
332 | rd = kmalloc(sizeof(*rd), GFP_ATOMIC); | |
333 | if (rd == NULL) | |
334 | return -ENOBUFS; | |
335 | rd->remote_ip = ip; | |
336 | rd->remote_port = port; | |
337 | rd->remote_vni = vni; | |
338 | rd->remote_ifindex = ifindex; | |
339 | rd->remote_next = NULL; | |
340 | rd_prev->remote_next = rd; | |
341 | return 1; | |
342 | } | |
343 | ||
d342894c | 344 | /* Add new entry to forwarding table -- assumes lock held */ |
345 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, | |
346 | const u8 *mac, __be32 ip, | |
6681712d DS |
347 | __u16 state, __u16 flags, |
348 | __u32 port, __u32 vni, __u32 ifindex) | |
d342894c | 349 | { |
350 | struct vxlan_fdb *f; | |
351 | int notify = 0; | |
352 | ||
353 | f = vxlan_find_mac(vxlan, mac); | |
354 | if (f) { | |
355 | if (flags & NLM_F_EXCL) { | |
356 | netdev_dbg(vxlan->dev, | |
357 | "lost race to create %pM\n", mac); | |
358 | return -EEXIST; | |
359 | } | |
360 | if (f->state != state) { | |
361 | f->state = state; | |
362 | f->updated = jiffies; | |
363 | notify = 1; | |
364 | } | |
6681712d DS |
365 | if ((flags & NLM_F_APPEND) && |
366 | is_multicast_ether_addr(f->eth_addr)) { | |
367 | int rc = vxlan_fdb_append(f, ip, port, vni, ifindex); | |
368 | ||
369 | if (rc < 0) | |
370 | return rc; | |
371 | notify |= rc; | |
372 | } | |
d342894c | 373 | } else { |
374 | if (!(flags & NLM_F_CREATE)) | |
375 | return -ENOENT; | |
376 | ||
377 | if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax) | |
378 | return -ENOSPC; | |
379 | ||
380 | netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip); | |
381 | f = kmalloc(sizeof(*f), GFP_ATOMIC); | |
382 | if (!f) | |
383 | return -ENOMEM; | |
384 | ||
385 | notify = 1; | |
6681712d DS |
386 | f->remote.remote_ip = ip; |
387 | f->remote.remote_port = port; | |
388 | f->remote.remote_vni = vni; | |
389 | f->remote.remote_ifindex = ifindex; | |
390 | f->remote.remote_next = NULL; | |
d342894c | 391 | f->state = state; |
392 | f->updated = f->used = jiffies; | |
393 | memcpy(f->eth_addr, mac, ETH_ALEN); | |
394 | ||
395 | ++vxlan->addrcnt; | |
396 | hlist_add_head_rcu(&f->hlist, | |
397 | vxlan_fdb_head(vxlan, mac)); | |
398 | } | |
399 | ||
400 | if (notify) | |
401 | vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); | |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
6681712d DS |
406 | void vxlan_fdb_free(struct rcu_head *head) |
407 | { | |
408 | struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); | |
409 | ||
410 | while (f->remote.remote_next) { | |
411 | struct vxlan_rdst *rd = f->remote.remote_next; | |
412 | ||
413 | f->remote.remote_next = rd->remote_next; | |
414 | kfree(rd); | |
415 | } | |
416 | kfree(f); | |
417 | } | |
418 | ||
d342894c | 419 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) |
420 | { | |
421 | netdev_dbg(vxlan->dev, | |
422 | "delete %pM\n", f->eth_addr); | |
423 | ||
424 | --vxlan->addrcnt; | |
425 | vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH); | |
426 | ||
427 | hlist_del_rcu(&f->hlist); | |
6681712d | 428 | call_rcu(&f->rcu, vxlan_fdb_free); |
d342894c | 429 | } |
430 | ||
431 | /* Add static entry (via netlink) */ | |
432 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], | |
433 | struct net_device *dev, | |
434 | const unsigned char *addr, u16 flags) | |
435 | { | |
436 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
6681712d | 437 | struct net *net = dev_net(vxlan->dev); |
d342894c | 438 | __be32 ip; |
6681712d | 439 | u32 port, vni, ifindex; |
d342894c | 440 | int err; |
441 | ||
442 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { | |
443 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", | |
444 | ndm->ndm_state); | |
445 | return -EINVAL; | |
446 | } | |
447 | ||
448 | if (tb[NDA_DST] == NULL) | |
449 | return -EINVAL; | |
450 | ||
451 | if (nla_len(tb[NDA_DST]) != sizeof(__be32)) | |
452 | return -EAFNOSUPPORT; | |
453 | ||
454 | ip = nla_get_be32(tb[NDA_DST]); | |
455 | ||
6681712d DS |
456 | if (tb[NDA_PORT]) { |
457 | if (nla_len(tb[NDA_PORT]) != sizeof(u32)) | |
458 | return -EINVAL; | |
459 | port = nla_get_u32(tb[NDA_PORT]); | |
460 | } else | |
461 | port = vxlan_port; | |
462 | ||
463 | if (tb[NDA_VNI]) { | |
464 | if (nla_len(tb[NDA_VNI]) != sizeof(u32)) | |
465 | return -EINVAL; | |
466 | vni = nla_get_u32(tb[NDA_VNI]); | |
467 | } else | |
468 | vni = vxlan->vni; | |
469 | ||
470 | if (tb[NDA_IFINDEX]) { | |
5abb0029 | 471 | struct net_device *tdev; |
6681712d DS |
472 | |
473 | if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) | |
474 | return -EINVAL; | |
475 | ifindex = nla_get_u32(tb[NDA_IFINDEX]); | |
5abb0029 PS |
476 | tdev = dev_get_by_index(net, ifindex); |
477 | if (!tdev) | |
6681712d | 478 | return -EADDRNOTAVAIL; |
5abb0029 | 479 | dev_put(tdev); |
6681712d DS |
480 | } else |
481 | ifindex = 0; | |
482 | ||
d342894c | 483 | spin_lock_bh(&vxlan->hash_lock); |
6681712d DS |
484 | err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags, port, |
485 | vni, ifindex); | |
d342894c | 486 | spin_unlock_bh(&vxlan->hash_lock); |
487 | ||
488 | return err; | |
489 | } | |
490 | ||
491 | /* Delete entry (via netlink) */ | |
1690be63 VY |
492 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
493 | struct net_device *dev, | |
d342894c | 494 | const unsigned char *addr) |
495 | { | |
496 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
497 | struct vxlan_fdb *f; | |
498 | int err = -ENOENT; | |
499 | ||
500 | spin_lock_bh(&vxlan->hash_lock); | |
501 | f = vxlan_find_mac(vxlan, addr); | |
502 | if (f) { | |
503 | vxlan_fdb_destroy(vxlan, f); | |
504 | err = 0; | |
505 | } | |
506 | spin_unlock_bh(&vxlan->hash_lock); | |
507 | ||
508 | return err; | |
509 | } | |
510 | ||
511 | /* Dump forwarding table */ | |
512 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, | |
513 | struct net_device *dev, int idx) | |
514 | { | |
515 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
516 | unsigned int h; | |
517 | ||
518 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
519 | struct vxlan_fdb *f; | |
d342894c | 520 | int err; |
521 | ||
b67bfe0d | 522 | hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { |
6681712d DS |
523 | struct vxlan_rdst *rd; |
524 | for (rd = &f->remote; rd; rd = rd->remote_next) { | |
525 | if (idx < cb->args[0]) | |
526 | goto skip; | |
527 | ||
528 | err = vxlan_fdb_info(skb, vxlan, f, | |
529 | NETLINK_CB(cb->skb).portid, | |
530 | cb->nlh->nlmsg_seq, | |
531 | RTM_NEWNEIGH, | |
532 | NLM_F_MULTI, rd); | |
533 | if (err < 0) | |
534 | break; | |
d342894c | 535 | skip: |
6681712d DS |
536 | ++idx; |
537 | } | |
d342894c | 538 | } |
539 | } | |
540 | ||
541 | return idx; | |
542 | } | |
543 | ||
544 | /* Watch incoming packets to learn mapping between Ethernet address | |
545 | * and Tunnel endpoint. | |
546 | */ | |
547 | static void vxlan_snoop(struct net_device *dev, | |
548 | __be32 src_ip, const u8 *src_mac) | |
549 | { | |
550 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
551 | struct vxlan_fdb *f; | |
552 | int err; | |
553 | ||
554 | f = vxlan_find_mac(vxlan, src_mac); | |
555 | if (likely(f)) { | |
556 | f->used = jiffies; | |
6681712d | 557 | if (likely(f->remote.remote_ip == src_ip)) |
d342894c | 558 | return; |
559 | ||
560 | if (net_ratelimit()) | |
561 | netdev_info(dev, | |
562 | "%pM migrated from %pI4 to %pI4\n", | |
6681712d | 563 | src_mac, &f->remote.remote_ip, &src_ip); |
d342894c | 564 | |
6681712d | 565 | f->remote.remote_ip = src_ip; |
d342894c | 566 | f->updated = jiffies; |
567 | } else { | |
568 | /* learned new entry */ | |
569 | spin_lock(&vxlan->hash_lock); | |
570 | err = vxlan_fdb_create(vxlan, src_mac, src_ip, | |
571 | NUD_REACHABLE, | |
6681712d DS |
572 | NLM_F_EXCL|NLM_F_CREATE, |
573 | vxlan_port, vxlan->vni, 0); | |
d342894c | 574 | spin_unlock(&vxlan->hash_lock); |
575 | } | |
576 | } | |
577 | ||
578 | ||
579 | /* See if multicast group is already in use by other ID */ | |
580 | static bool vxlan_group_used(struct vxlan_net *vn, | |
581 | const struct vxlan_dev *this) | |
582 | { | |
583 | const struct vxlan_dev *vxlan; | |
d342894c | 584 | unsigned h; |
585 | ||
586 | for (h = 0; h < VNI_HASH_SIZE; ++h) | |
b67bfe0d | 587 | hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist) { |
d342894c | 588 | if (vxlan == this) |
589 | continue; | |
590 | ||
591 | if (!netif_running(vxlan->dev)) | |
592 | continue; | |
593 | ||
594 | if (vxlan->gaddr == this->gaddr) | |
595 | return true; | |
596 | } | |
597 | ||
598 | return false; | |
599 | } | |
600 | ||
601 | /* kernel equivalent to IP_ADD_MEMBERSHIP */ | |
602 | static int vxlan_join_group(struct net_device *dev) | |
603 | { | |
604 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
605 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); | |
606 | struct sock *sk = vn->sock->sk; | |
607 | struct ip_mreqn mreq = { | |
af9b078e YB |
608 | .imr_multiaddr.s_addr = vxlan->gaddr, |
609 | .imr_ifindex = vxlan->link, | |
d342894c | 610 | }; |
611 | int err; | |
612 | ||
613 | /* Already a member of group */ | |
614 | if (vxlan_group_used(vn, vxlan)) | |
615 | return 0; | |
616 | ||
617 | /* Need to drop RTNL to call multicast join */ | |
618 | rtnl_unlock(); | |
619 | lock_sock(sk); | |
620 | err = ip_mc_join_group(sk, &mreq); | |
621 | release_sock(sk); | |
622 | rtnl_lock(); | |
623 | ||
624 | return err; | |
625 | } | |
626 | ||
627 | ||
628 | /* kernel equivalent to IP_DROP_MEMBERSHIP */ | |
629 | static int vxlan_leave_group(struct net_device *dev) | |
630 | { | |
631 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
632 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); | |
633 | int err = 0; | |
634 | struct sock *sk = vn->sock->sk; | |
635 | struct ip_mreqn mreq = { | |
af9b078e YB |
636 | .imr_multiaddr.s_addr = vxlan->gaddr, |
637 | .imr_ifindex = vxlan->link, | |
d342894c | 638 | }; |
639 | ||
640 | /* Only leave group when last vxlan is done. */ | |
641 | if (vxlan_group_used(vn, vxlan)) | |
642 | return 0; | |
643 | ||
644 | /* Need to drop RTNL to call multicast leave */ | |
645 | rtnl_unlock(); | |
646 | lock_sock(sk); | |
647 | err = ip_mc_leave_group(sk, &mreq); | |
648 | release_sock(sk); | |
649 | rtnl_lock(); | |
650 | ||
651 | return err; | |
652 | } | |
653 | ||
654 | /* Callback from net/ipv4/udp.c to receive packets */ | |
655 | static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) | |
656 | { | |
657 | struct iphdr *oip; | |
658 | struct vxlanhdr *vxh; | |
659 | struct vxlan_dev *vxlan; | |
e8171045 | 660 | struct pcpu_tstats *stats; |
d342894c | 661 | __u32 vni; |
662 | int err; | |
663 | ||
664 | /* pop off outer UDP header */ | |
665 | __skb_pull(skb, sizeof(struct udphdr)); | |
666 | ||
667 | /* Need Vxlan and inner Ethernet header to be present */ | |
668 | if (!pskb_may_pull(skb, sizeof(struct vxlanhdr))) | |
669 | goto error; | |
670 | ||
671 | /* Drop packets with reserved bits set */ | |
672 | vxh = (struct vxlanhdr *) skb->data; | |
673 | if (vxh->vx_flags != htonl(VXLAN_FLAGS) || | |
674 | (vxh->vx_vni & htonl(0xff))) { | |
675 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", | |
676 | ntohl(vxh->vx_flags), ntohl(vxh->vx_vni)); | |
677 | goto error; | |
678 | } | |
679 | ||
680 | __skb_pull(skb, sizeof(struct vxlanhdr)); | |
d342894c | 681 | |
682 | /* Is this VNI defined? */ | |
683 | vni = ntohl(vxh->vx_vni) >> 8; | |
684 | vxlan = vxlan_find_vni(sock_net(sk), vni); | |
685 | if (!vxlan) { | |
686 | netdev_dbg(skb->dev, "unknown vni %d\n", vni); | |
687 | goto drop; | |
688 | } | |
689 | ||
690 | if (!pskb_may_pull(skb, ETH_HLEN)) { | |
691 | vxlan->dev->stats.rx_length_errors++; | |
692 | vxlan->dev->stats.rx_errors++; | |
693 | goto drop; | |
694 | } | |
695 | ||
e4f67add DS |
696 | skb_reset_mac_header(skb); |
697 | ||
d342894c | 698 | /* Re-examine inner Ethernet packet */ |
699 | oip = ip_hdr(skb); | |
700 | skb->protocol = eth_type_trans(skb, vxlan->dev); | |
d342894c | 701 | |
702 | /* Ignore packet loops (and multicast echo) */ | |
703 | if (compare_ether_addr(eth_hdr(skb)->h_source, | |
704 | vxlan->dev->dev_addr) == 0) | |
705 | goto drop; | |
706 | ||
e4f67add | 707 | if (vxlan->flags & VXLAN_F_LEARN) |
d342894c | 708 | vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source); |
709 | ||
710 | __skb_tunnel_rx(skb, vxlan->dev); | |
711 | skb_reset_network_header(skb); | |
0afb1666 JG |
712 | |
713 | /* If the NIC driver gave us an encapsulated packet with | |
714 | * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled, | |
715 | * leave the CHECKSUM_UNNECESSARY, the device checksummed it | |
716 | * for us. Otherwise force the upper layers to verify it. | |
717 | */ | |
718 | if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation || | |
719 | !(vxlan->dev->features & NETIF_F_RXCSUM)) | |
720 | skb->ip_summed = CHECKSUM_NONE; | |
721 | ||
722 | skb->encapsulation = 0; | |
d342894c | 723 | |
724 | err = IP_ECN_decapsulate(oip, skb); | |
725 | if (unlikely(err)) { | |
726 | if (log_ecn_error) | |
727 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", | |
728 | &oip->saddr, oip->tos); | |
729 | if (err > 1) { | |
730 | ++vxlan->dev->stats.rx_frame_errors; | |
731 | ++vxlan->dev->stats.rx_errors; | |
732 | goto drop; | |
733 | } | |
734 | } | |
735 | ||
e8171045 | 736 | stats = this_cpu_ptr(vxlan->dev->tstats); |
d342894c | 737 | u64_stats_update_begin(&stats->syncp); |
738 | stats->rx_packets++; | |
739 | stats->rx_bytes += skb->len; | |
740 | u64_stats_update_end(&stats->syncp); | |
741 | ||
742 | netif_rx(skb); | |
743 | ||
744 | return 0; | |
745 | error: | |
746 | /* Put UDP header back */ | |
747 | __skb_push(skb, sizeof(struct udphdr)); | |
748 | ||
749 | return 1; | |
750 | drop: | |
751 | /* Consume bad packet */ | |
752 | kfree_skb(skb); | |
753 | return 0; | |
754 | } | |
755 | ||
e4f67add DS |
756 | static int arp_reduce(struct net_device *dev, struct sk_buff *skb) |
757 | { | |
758 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
759 | struct arphdr *parp; | |
760 | u8 *arpptr, *sha; | |
761 | __be32 sip, tip; | |
762 | struct neighbour *n; | |
763 | ||
764 | if (dev->flags & IFF_NOARP) | |
765 | goto out; | |
766 | ||
767 | if (!pskb_may_pull(skb, arp_hdr_len(dev))) { | |
768 | dev->stats.tx_dropped++; | |
769 | goto out; | |
770 | } | |
771 | parp = arp_hdr(skb); | |
772 | ||
773 | if ((parp->ar_hrd != htons(ARPHRD_ETHER) && | |
774 | parp->ar_hrd != htons(ARPHRD_IEEE802)) || | |
775 | parp->ar_pro != htons(ETH_P_IP) || | |
776 | parp->ar_op != htons(ARPOP_REQUEST) || | |
777 | parp->ar_hln != dev->addr_len || | |
778 | parp->ar_pln != 4) | |
779 | goto out; | |
780 | arpptr = (u8 *)parp + sizeof(struct arphdr); | |
781 | sha = arpptr; | |
782 | arpptr += dev->addr_len; /* sha */ | |
783 | memcpy(&sip, arpptr, sizeof(sip)); | |
784 | arpptr += sizeof(sip); | |
785 | arpptr += dev->addr_len; /* tha */ | |
786 | memcpy(&tip, arpptr, sizeof(tip)); | |
787 | ||
788 | if (ipv4_is_loopback(tip) || | |
789 | ipv4_is_multicast(tip)) | |
790 | goto out; | |
791 | ||
792 | n = neigh_lookup(&arp_tbl, &tip, dev); | |
793 | ||
794 | if (n) { | |
e4f67add DS |
795 | struct vxlan_fdb *f; |
796 | struct sk_buff *reply; | |
797 | ||
798 | if (!(n->nud_state & NUD_CONNECTED)) { | |
799 | neigh_release(n); | |
800 | goto out; | |
801 | } | |
802 | ||
803 | f = vxlan_find_mac(vxlan, n->ha); | |
6681712d | 804 | if (f && f->remote.remote_ip == htonl(INADDR_ANY)) { |
e4f67add DS |
805 | /* bridge-local neighbor */ |
806 | neigh_release(n); | |
807 | goto out; | |
808 | } | |
809 | ||
810 | reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, | |
811 | n->ha, sha); | |
812 | ||
813 | neigh_release(n); | |
814 | ||
815 | skb_reset_mac_header(reply); | |
816 | __skb_pull(reply, skb_network_offset(reply)); | |
817 | reply->ip_summed = CHECKSUM_UNNECESSARY; | |
818 | reply->pkt_type = PACKET_HOST; | |
819 | ||
820 | if (netif_rx_ni(reply) == NET_RX_DROP) | |
821 | dev->stats.rx_dropped++; | |
822 | } else if (vxlan->flags & VXLAN_F_L3MISS) | |
823 | vxlan_ip_miss(dev, tip); | |
824 | out: | |
825 | consume_skb(skb); | |
826 | return NETDEV_TX_OK; | |
827 | } | |
828 | ||
829 | static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) | |
830 | { | |
831 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
832 | struct neighbour *n; | |
833 | struct iphdr *pip; | |
834 | ||
835 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) | |
836 | return false; | |
837 | ||
838 | n = NULL; | |
839 | switch (ntohs(eth_hdr(skb)->h_proto)) { | |
840 | case ETH_P_IP: | |
841 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) | |
842 | return false; | |
843 | pip = ip_hdr(skb); | |
844 | n = neigh_lookup(&arp_tbl, &pip->daddr, dev); | |
845 | break; | |
846 | default: | |
847 | return false; | |
848 | } | |
849 | ||
850 | if (n) { | |
851 | bool diff; | |
852 | ||
853 | diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0; | |
854 | if (diff) { | |
855 | memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, | |
856 | dev->addr_len); | |
857 | memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); | |
858 | } | |
859 | neigh_release(n); | |
860 | return diff; | |
861 | } else if (vxlan->flags & VXLAN_F_L3MISS) | |
862 | vxlan_ip_miss(dev, pip->daddr); | |
863 | return false; | |
864 | } | |
865 | ||
1cad8715 | 866 | static void vxlan_sock_free(struct sk_buff *skb) |
867 | { | |
868 | sock_put(skb->sk); | |
869 | } | |
870 | ||
871 | /* On transmit, associate with the tunnel socket */ | |
872 | static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb) | |
873 | { | |
874 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); | |
875 | struct sock *sk = vn->sock->sk; | |
876 | ||
877 | skb_orphan(skb); | |
878 | sock_hold(sk); | |
879 | skb->sk = sk; | |
880 | skb->destructor = vxlan_sock_free; | |
881 | } | |
882 | ||
05f47d69 | 883 | /* Compute source port for outgoing packet |
884 | * first choice to use L4 flow hash since it will spread | |
885 | * better and maybe available from hardware | |
886 | * secondary choice is to use jhash on the Ethernet header | |
887 | */ | |
888 | static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb) | |
889 | { | |
890 | unsigned int range = (vxlan->port_max - vxlan->port_min) + 1; | |
891 | u32 hash; | |
892 | ||
893 | hash = skb_get_rxhash(skb); | |
894 | if (!hash) | |
895 | hash = jhash(skb->data, 2 * ETH_ALEN, | |
896 | (__force u32) skb->protocol); | |
897 | ||
898 | return (((u64) hash * range) >> 32) + vxlan->port_min; | |
899 | } | |
900 | ||
05c0db08 PS |
901 | static int handle_offloads(struct sk_buff *skb) |
902 | { | |
903 | if (skb_is_gso(skb)) { | |
904 | int err = skb_unclone(skb, GFP_ATOMIC); | |
905 | if (unlikely(err)) | |
906 | return err; | |
907 | ||
908 | skb_shinfo(skb)->gso_type |= (SKB_GSO_UDP_TUNNEL | SKB_GSO_UDP); | |
909 | } else if (skb->ip_summed != CHECKSUM_PARTIAL) | |
910 | skb->ip_summed = CHECKSUM_NONE; | |
911 | ||
912 | return 0; | |
913 | } | |
914 | ||
9dcc71e1 SS |
915 | /* Bypass encapsulation if the destination is local */ |
916 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, | |
917 | struct vxlan_dev *dst_vxlan) | |
918 | { | |
919 | struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); | |
920 | struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); | |
921 | ||
922 | skb->pkt_type = PACKET_HOST; | |
923 | skb->encapsulation = 0; | |
924 | skb->dev = dst_vxlan->dev; | |
925 | __skb_pull(skb, skb_network_offset(skb)); | |
926 | ||
927 | if (dst_vxlan->flags & VXLAN_F_LEARN) | |
928 | vxlan_snoop(skb->dev, INADDR_LOOPBACK, eth_hdr(skb)->h_source); | |
929 | ||
930 | u64_stats_update_begin(&tx_stats->syncp); | |
931 | tx_stats->tx_packets++; | |
932 | tx_stats->tx_bytes += skb->len; | |
933 | u64_stats_update_end(&tx_stats->syncp); | |
934 | ||
935 | if (netif_rx(skb) == NET_RX_SUCCESS) { | |
936 | u64_stats_update_begin(&rx_stats->syncp); | |
937 | rx_stats->rx_packets++; | |
938 | rx_stats->rx_bytes += skb->len; | |
939 | u64_stats_update_end(&rx_stats->syncp); | |
940 | } else { | |
941 | skb->dev->stats.rx_dropped++; | |
942 | } | |
943 | } | |
944 | ||
6681712d DS |
945 | static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, |
946 | struct vxlan_rdst *rdst, bool did_rsc) | |
d342894c | 947 | { |
948 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
949 | struct rtable *rt; | |
d342894c | 950 | const struct iphdr *old_iph; |
951 | struct iphdr *iph; | |
952 | struct vxlanhdr *vxh; | |
953 | struct udphdr *uh; | |
954 | struct flowi4 fl4; | |
d342894c | 955 | __be32 dst; |
6681712d DS |
956 | __u16 src_port, dst_port; |
957 | u32 vni; | |
d342894c | 958 | __be16 df = 0; |
959 | __u8 tos, ttl; | |
d342894c | 960 | |
6681712d DS |
961 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan_port; |
962 | vni = rdst->remote_vni; | |
963 | dst = rdst->remote_ip; | |
e4f67add DS |
964 | |
965 | if (!dst) { | |
966 | if (did_rsc) { | |
e4f67add | 967 | /* short-circuited back to local bridge */ |
9dcc71e1 | 968 | vxlan_encap_bypass(skb, vxlan, vxlan); |
e4f67add DS |
969 | return NETDEV_TX_OK; |
970 | } | |
ef59febe | 971 | goto drop; |
e4f67add | 972 | } |
ef59febe | 973 | |
d6727fe3 JG |
974 | if (!skb->encapsulation) { |
975 | skb_reset_inner_headers(skb); | |
976 | skb->encapsulation = 1; | |
977 | } | |
978 | ||
d342894c | 979 | /* Need space for new headers (invalidates iph ptr) */ |
980 | if (skb_cow_head(skb, VXLAN_HEADROOM)) | |
981 | goto drop; | |
982 | ||
d342894c | 983 | old_iph = ip_hdr(skb); |
984 | ||
d342894c | 985 | ttl = vxlan->ttl; |
986 | if (!ttl && IN_MULTICAST(ntohl(dst))) | |
987 | ttl = 1; | |
988 | ||
989 | tos = vxlan->tos; | |
990 | if (tos == 1) | |
206aaafc | 991 | tos = ip_tunnel_get_dsfield(old_iph, skb); |
d342894c | 992 | |
05f47d69 | 993 | src_port = vxlan_src_port(vxlan, skb); |
d342894c | 994 | |
ca78f181 | 995 | memset(&fl4, 0, sizeof(fl4)); |
6681712d | 996 | fl4.flowi4_oif = rdst->remote_ifindex; |
ca78f181 | 997 | fl4.flowi4_tos = RT_TOS(tos); |
998 | fl4.daddr = dst; | |
999 | fl4.saddr = vxlan->saddr; | |
1000 | ||
1001 | rt = ip_route_output_key(dev_net(dev), &fl4); | |
d342894c | 1002 | if (IS_ERR(rt)) { |
1003 | netdev_dbg(dev, "no route to %pI4\n", &dst); | |
1004 | dev->stats.tx_carrier_errors++; | |
1005 | goto tx_error; | |
1006 | } | |
1007 | ||
1008 | if (rt->dst.dev == dev) { | |
1009 | netdev_dbg(dev, "circular route to %pI4\n", &dst); | |
1010 | ip_rt_put(rt); | |
1011 | dev->stats.collisions++; | |
1012 | goto tx_error; | |
1013 | } | |
1014 | ||
9dcc71e1 SS |
1015 | /* Bypass encapsulation if the destination is local */ |
1016 | if (rt->rt_flags & RTCF_LOCAL) { | |
1017 | struct vxlan_dev *dst_vxlan; | |
1018 | ||
1019 | ip_rt_put(rt); | |
1020 | dst_vxlan = vxlan_find_vni(dev_net(dev), vni); | |
1021 | if (!dst_vxlan) | |
1022 | goto tx_error; | |
1023 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); | |
1024 | return NETDEV_TX_OK; | |
1025 | } | |
1026 | ||
d342894c | 1027 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
1028 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | | |
1029 | IPSKB_REROUTED); | |
1030 | skb_dst_drop(skb); | |
1031 | skb_dst_set(skb, &rt->dst); | |
1032 | ||
1033 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); | |
1034 | vxh->vx_flags = htonl(VXLAN_FLAGS); | |
6681712d | 1035 | vxh->vx_vni = htonl(vni << 8); |
d342894c | 1036 | |
1037 | __skb_push(skb, sizeof(*uh)); | |
1038 | skb_reset_transport_header(skb); | |
1039 | uh = udp_hdr(skb); | |
1040 | ||
6681712d | 1041 | uh->dest = htons(dst_port); |
05f47d69 | 1042 | uh->source = htons(src_port); |
d342894c | 1043 | |
1044 | uh->len = htons(skb->len); | |
1045 | uh->check = 0; | |
1046 | ||
1047 | __skb_push(skb, sizeof(*iph)); | |
1048 | skb_reset_network_header(skb); | |
1049 | iph = ip_hdr(skb); | |
1050 | iph->version = 4; | |
1051 | iph->ihl = sizeof(struct iphdr) >> 2; | |
1052 | iph->frag_off = df; | |
1053 | iph->protocol = IPPROTO_UDP; | |
206aaafc | 1054 | iph->tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
ca78f181 | 1055 | iph->daddr = dst; |
d342894c | 1056 | iph->saddr = fl4.saddr; |
1057 | iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); | |
8dc98eb2 | 1058 | tunnel_ip_select_ident(skb, old_iph, &rt->dst); |
d342894c | 1059 | |
88c4c066 ZM |
1060 | nf_reset(skb); |
1061 | ||
1cad8715 | 1062 | vxlan_set_owner(dev, skb); |
1063 | ||
05c0db08 PS |
1064 | if (handle_offloads(skb)) |
1065 | goto drop; | |
d342894c | 1066 | |
6aed0c8b | 1067 | iptunnel_xmit(skb, dev); |
d342894c | 1068 | return NETDEV_TX_OK; |
1069 | ||
1070 | drop: | |
1071 | dev->stats.tx_dropped++; | |
1072 | goto tx_free; | |
1073 | ||
1074 | tx_error: | |
1075 | dev->stats.tx_errors++; | |
1076 | tx_free: | |
1077 | dev_kfree_skb(skb); | |
1078 | return NETDEV_TX_OK; | |
1079 | } | |
1080 | ||
6681712d DS |
1081 | /* Transmit local packets over Vxlan |
1082 | * | |
1083 | * Outer IP header inherits ECN and DF from inner header. | |
1084 | * Outer UDP destination is the VXLAN assigned port. | |
1085 | * source port is based on hash of flow | |
1086 | */ | |
1087 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) | |
1088 | { | |
1089 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1090 | struct ethhdr *eth; | |
1091 | bool did_rsc = false; | |
1092 | struct vxlan_rdst group, *rdst0, *rdst; | |
1093 | struct vxlan_fdb *f; | |
1094 | int rc1, rc; | |
1095 | ||
1096 | skb_reset_mac_header(skb); | |
1097 | eth = eth_hdr(skb); | |
1098 | ||
1099 | if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP) | |
1100 | return arp_reduce(dev, skb); | |
1101 | else if ((vxlan->flags&VXLAN_F_RSC) && ntohs(eth->h_proto) == ETH_P_IP) | |
1102 | did_rsc = route_shortcircuit(dev, skb); | |
1103 | ||
1104 | f = vxlan_find_mac(vxlan, eth->h_dest); | |
1105 | if (f == NULL) { | |
1106 | did_rsc = false; | |
1107 | group.remote_port = vxlan_port; | |
1108 | group.remote_vni = vxlan->vni; | |
1109 | group.remote_ip = vxlan->gaddr; | |
1110 | group.remote_ifindex = vxlan->link; | |
1111 | group.remote_next = 0; | |
1112 | rdst0 = &group; | |
1113 | ||
1114 | if (group.remote_ip == htonl(INADDR_ANY) && | |
1115 | (vxlan->flags & VXLAN_F_L2MISS) && | |
1116 | !is_multicast_ether_addr(eth->h_dest)) | |
1117 | vxlan_fdb_miss(vxlan, eth->h_dest); | |
1118 | } else | |
1119 | rdst0 = &f->remote; | |
1120 | ||
1121 | rc = NETDEV_TX_OK; | |
1122 | ||
1123 | /* if there are multiple destinations, send copies */ | |
1124 | for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) { | |
1125 | struct sk_buff *skb1; | |
1126 | ||
1127 | skb1 = skb_clone(skb, GFP_ATOMIC); | |
1128 | rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc); | |
1129 | if (rc == NETDEV_TX_OK) | |
1130 | rc = rc1; | |
1131 | } | |
1132 | ||
1133 | rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc); | |
1134 | if (rc == NETDEV_TX_OK) | |
1135 | rc = rc1; | |
1136 | return rc; | |
1137 | } | |
1138 | ||
d342894c | 1139 | /* Walk the forwarding table and purge stale entries */ |
1140 | static void vxlan_cleanup(unsigned long arg) | |
1141 | { | |
1142 | struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; | |
1143 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; | |
1144 | unsigned int h; | |
1145 | ||
1146 | if (!netif_running(vxlan->dev)) | |
1147 | return; | |
1148 | ||
1149 | spin_lock_bh(&vxlan->hash_lock); | |
1150 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
1151 | struct hlist_node *p, *n; | |
1152 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { | |
1153 | struct vxlan_fdb *f | |
1154 | = container_of(p, struct vxlan_fdb, hlist); | |
1155 | unsigned long timeout; | |
1156 | ||
3c172868 | 1157 | if (f->state & NUD_PERMANENT) |
d342894c | 1158 | continue; |
1159 | ||
1160 | timeout = f->used + vxlan->age_interval * HZ; | |
1161 | if (time_before_eq(timeout, jiffies)) { | |
1162 | netdev_dbg(vxlan->dev, | |
1163 | "garbage collect %pM\n", | |
1164 | f->eth_addr); | |
1165 | f->state = NUD_STALE; | |
1166 | vxlan_fdb_destroy(vxlan, f); | |
1167 | } else if (time_before(timeout, next_timer)) | |
1168 | next_timer = timeout; | |
1169 | } | |
1170 | } | |
1171 | spin_unlock_bh(&vxlan->hash_lock); | |
1172 | ||
1173 | mod_timer(&vxlan->age_timer, next_timer); | |
1174 | } | |
1175 | ||
1176 | /* Setup stats when device is created */ | |
1177 | static int vxlan_init(struct net_device *dev) | |
1178 | { | |
e8171045 PS |
1179 | dev->tstats = alloc_percpu(struct pcpu_tstats); |
1180 | if (!dev->tstats) | |
d342894c | 1181 | return -ENOMEM; |
1182 | ||
1183 | return 0; | |
1184 | } | |
1185 | ||
1186 | /* Start ageing timer and join group when device is brought up */ | |
1187 | static int vxlan_open(struct net_device *dev) | |
1188 | { | |
1189 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1190 | int err; | |
1191 | ||
1192 | if (vxlan->gaddr) { | |
1193 | err = vxlan_join_group(dev); | |
1194 | if (err) | |
1195 | return err; | |
1196 | } | |
1197 | ||
1198 | if (vxlan->age_interval) | |
1199 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); | |
1200 | ||
1201 | return 0; | |
1202 | } | |
1203 | ||
1204 | /* Purge the forwarding table */ | |
1205 | static void vxlan_flush(struct vxlan_dev *vxlan) | |
1206 | { | |
1207 | unsigned h; | |
1208 | ||
1209 | spin_lock_bh(&vxlan->hash_lock); | |
1210 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
1211 | struct hlist_node *p, *n; | |
1212 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { | |
1213 | struct vxlan_fdb *f | |
1214 | = container_of(p, struct vxlan_fdb, hlist); | |
1215 | vxlan_fdb_destroy(vxlan, f); | |
1216 | } | |
1217 | } | |
1218 | spin_unlock_bh(&vxlan->hash_lock); | |
1219 | } | |
1220 | ||
1221 | /* Cleanup timer and forwarding table on shutdown */ | |
1222 | static int vxlan_stop(struct net_device *dev) | |
1223 | { | |
1224 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1225 | ||
1226 | if (vxlan->gaddr) | |
1227 | vxlan_leave_group(dev); | |
1228 | ||
1229 | del_timer_sync(&vxlan->age_timer); | |
1230 | ||
1231 | vxlan_flush(vxlan); | |
1232 | ||
1233 | return 0; | |
1234 | } | |
1235 | ||
d342894c | 1236 | /* Stub, nothing needs to be done. */ |
1237 | static void vxlan_set_multicast_list(struct net_device *dev) | |
1238 | { | |
1239 | } | |
1240 | ||
1241 | static const struct net_device_ops vxlan_netdev_ops = { | |
1242 | .ndo_init = vxlan_init, | |
1243 | .ndo_open = vxlan_open, | |
1244 | .ndo_stop = vxlan_stop, | |
1245 | .ndo_start_xmit = vxlan_xmit, | |
e8171045 | 1246 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
d342894c | 1247 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
1248 | .ndo_change_mtu = eth_change_mtu, | |
1249 | .ndo_validate_addr = eth_validate_addr, | |
1250 | .ndo_set_mac_address = eth_mac_addr, | |
1251 | .ndo_fdb_add = vxlan_fdb_add, | |
1252 | .ndo_fdb_del = vxlan_fdb_delete, | |
1253 | .ndo_fdb_dump = vxlan_fdb_dump, | |
1254 | }; | |
1255 | ||
1256 | /* Info for udev, that this is a virtual tunnel endpoint */ | |
1257 | static struct device_type vxlan_type = { | |
1258 | .name = "vxlan", | |
1259 | }; | |
1260 | ||
1261 | static void vxlan_free(struct net_device *dev) | |
1262 | { | |
e8171045 | 1263 | free_percpu(dev->tstats); |
d342894c | 1264 | free_netdev(dev); |
1265 | } | |
1266 | ||
1267 | /* Initialize the device structure. */ | |
1268 | static void vxlan_setup(struct net_device *dev) | |
1269 | { | |
1270 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1271 | unsigned h; | |
05f47d69 | 1272 | int low, high; |
d342894c | 1273 | |
1274 | eth_hw_addr_random(dev); | |
1275 | ether_setup(dev); | |
2840bf22 | 1276 | dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM; |
d342894c | 1277 | |
1278 | dev->netdev_ops = &vxlan_netdev_ops; | |
1279 | dev->destructor = vxlan_free; | |
1280 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); | |
1281 | ||
1282 | dev->tx_queue_len = 0; | |
1283 | dev->features |= NETIF_F_LLTX; | |
1284 | dev->features |= NETIF_F_NETNS_LOCAL; | |
d6727fe3 | 1285 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
0afb1666 | 1286 | dev->features |= NETIF_F_RXCSUM; |
05c0db08 | 1287 | dev->features |= NETIF_F_GSO_SOFTWARE; |
0afb1666 JG |
1288 | |
1289 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; | |
05c0db08 | 1290 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
d342894c | 1291 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
6602d007 | 1292 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
d342894c | 1293 | |
1294 | spin_lock_init(&vxlan->hash_lock); | |
1295 | ||
1296 | init_timer_deferrable(&vxlan->age_timer); | |
1297 | vxlan->age_timer.function = vxlan_cleanup; | |
1298 | vxlan->age_timer.data = (unsigned long) vxlan; | |
1299 | ||
05f47d69 | 1300 | inet_get_local_port_range(&low, &high); |
1301 | vxlan->port_min = low; | |
1302 | vxlan->port_max = high; | |
1303 | ||
d342894c | 1304 | vxlan->dev = dev; |
1305 | ||
1306 | for (h = 0; h < FDB_HASH_SIZE; ++h) | |
1307 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); | |
1308 | } | |
1309 | ||
1310 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { | |
1311 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, | |
1312 | [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, | |
1313 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, | |
1314 | [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, | |
1315 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, | |
1316 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, | |
1317 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, | |
1318 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, | |
1319 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, | |
05f47d69 | 1320 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
e4f67add DS |
1321 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, |
1322 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, | |
1323 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, | |
1324 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, | |
d342894c | 1325 | }; |
1326 | ||
1327 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1328 | { | |
1329 | if (tb[IFLA_ADDRESS]) { | |
1330 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { | |
1331 | pr_debug("invalid link address (not ethernet)\n"); | |
1332 | return -EINVAL; | |
1333 | } | |
1334 | ||
1335 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { | |
1336 | pr_debug("invalid all zero ethernet address\n"); | |
1337 | return -EADDRNOTAVAIL; | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | if (!data) | |
1342 | return -EINVAL; | |
1343 | ||
1344 | if (data[IFLA_VXLAN_ID]) { | |
1345 | __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); | |
1346 | if (id >= VXLAN_VID_MASK) | |
1347 | return -ERANGE; | |
1348 | } | |
1349 | ||
1350 | if (data[IFLA_VXLAN_GROUP]) { | |
1351 | __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]); | |
1352 | if (!IN_MULTICAST(ntohl(gaddr))) { | |
1353 | pr_debug("group address is not IPv4 multicast\n"); | |
1354 | return -EADDRNOTAVAIL; | |
1355 | } | |
1356 | } | |
05f47d69 | 1357 | |
1358 | if (data[IFLA_VXLAN_PORT_RANGE]) { | |
1359 | const struct ifla_vxlan_port_range *p | |
1360 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
1361 | ||
1362 | if (ntohs(p->high) < ntohs(p->low)) { | |
1363 | pr_debug("port range %u .. %u not valid\n", | |
1364 | ntohs(p->low), ntohs(p->high)); | |
1365 | return -EINVAL; | |
1366 | } | |
1367 | } | |
1368 | ||
d342894c | 1369 | return 0; |
1370 | } | |
1371 | ||
1b13c97f YB |
1372 | static void vxlan_get_drvinfo(struct net_device *netdev, |
1373 | struct ethtool_drvinfo *drvinfo) | |
1374 | { | |
1375 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); | |
1376 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); | |
1377 | } | |
1378 | ||
1379 | static const struct ethtool_ops vxlan_ethtool_ops = { | |
1380 | .get_drvinfo = vxlan_get_drvinfo, | |
1381 | .get_link = ethtool_op_get_link, | |
1382 | }; | |
1383 | ||
d342894c | 1384 | static int vxlan_newlink(struct net *net, struct net_device *dev, |
1385 | struct nlattr *tb[], struct nlattr *data[]) | |
1386 | { | |
1387 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1388 | __u32 vni; | |
1389 | int err; | |
1390 | ||
1391 | if (!data[IFLA_VXLAN_ID]) | |
1392 | return -EINVAL; | |
1393 | ||
1394 | vni = nla_get_u32(data[IFLA_VXLAN_ID]); | |
1395 | if (vxlan_find_vni(net, vni)) { | |
1396 | pr_info("duplicate VNI %u\n", vni); | |
1397 | return -EEXIST; | |
1398 | } | |
1399 | vxlan->vni = vni; | |
1400 | ||
1401 | if (data[IFLA_VXLAN_GROUP]) | |
1402 | vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]); | |
1403 | ||
1404 | if (data[IFLA_VXLAN_LOCAL]) | |
1405 | vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]); | |
1406 | ||
34e02aa1 | 1407 | if (data[IFLA_VXLAN_LINK] && |
1408 | (vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]))) { | |
1409 | struct net_device *lowerdev | |
1410 | = __dev_get_by_index(net, vxlan->link); | |
1411 | ||
1412 | if (!lowerdev) { | |
1413 | pr_info("ifindex %d does not exist\n", vxlan->link); | |
1414 | return -ENODEV; | |
1415 | } | |
d342894c | 1416 | |
34e02aa1 | 1417 | if (!tb[IFLA_MTU]) |
d342894c | 1418 | dev->mtu = lowerdev->mtu - VXLAN_HEADROOM; |
1ba56fb4 AD |
1419 | |
1420 | /* update header length based on lower device */ | |
1421 | dev->hard_header_len = lowerdev->hard_header_len + | |
1422 | VXLAN_HEADROOM; | |
d342894c | 1423 | } |
1424 | ||
1425 | if (data[IFLA_VXLAN_TOS]) | |
1426 | vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); | |
1427 | ||
afb97186 VB |
1428 | if (data[IFLA_VXLAN_TTL]) |
1429 | vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); | |
1430 | ||
d342894c | 1431 | if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING])) |
e4f67add | 1432 | vxlan->flags |= VXLAN_F_LEARN; |
d342894c | 1433 | |
1434 | if (data[IFLA_VXLAN_AGEING]) | |
1435 | vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); | |
1436 | else | |
1437 | vxlan->age_interval = FDB_AGE_DEFAULT; | |
1438 | ||
e4f67add DS |
1439 | if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY])) |
1440 | vxlan->flags |= VXLAN_F_PROXY; | |
1441 | ||
1442 | if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC])) | |
1443 | vxlan->flags |= VXLAN_F_RSC; | |
1444 | ||
1445 | if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS])) | |
1446 | vxlan->flags |= VXLAN_F_L2MISS; | |
1447 | ||
1448 | if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS])) | |
1449 | vxlan->flags |= VXLAN_F_L3MISS; | |
1450 | ||
d342894c | 1451 | if (data[IFLA_VXLAN_LIMIT]) |
1452 | vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); | |
1453 | ||
05f47d69 | 1454 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
1455 | const struct ifla_vxlan_port_range *p | |
1456 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
1457 | vxlan->port_min = ntohs(p->low); | |
1458 | vxlan->port_max = ntohs(p->high); | |
1459 | } | |
1460 | ||
1b13c97f YB |
1461 | SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops); |
1462 | ||
d342894c | 1463 | err = register_netdevice(dev); |
1464 | if (!err) | |
1465 | hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni)); | |
1466 | ||
1467 | return err; | |
1468 | } | |
1469 | ||
1470 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) | |
1471 | { | |
1472 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1473 | ||
1474 | hlist_del_rcu(&vxlan->hlist); | |
1475 | ||
1476 | unregister_netdevice_queue(dev, head); | |
1477 | } | |
1478 | ||
1479 | static size_t vxlan_get_size(const struct net_device *dev) | |
1480 | { | |
1481 | ||
1482 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ | |
1483 | nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */ | |
1484 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ | |
1485 | nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */ | |
1486 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ | |
1487 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ | |
1488 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ | |
e4f67add DS |
1489 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ |
1490 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ | |
1491 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ | |
1492 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ | |
d342894c | 1493 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
1494 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ | |
05f47d69 | 1495 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
d342894c | 1496 | 0; |
1497 | } | |
1498 | ||
1499 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
1500 | { | |
1501 | const struct vxlan_dev *vxlan = netdev_priv(dev); | |
05f47d69 | 1502 | struct ifla_vxlan_port_range ports = { |
1503 | .low = htons(vxlan->port_min), | |
1504 | .high = htons(vxlan->port_max), | |
1505 | }; | |
d342894c | 1506 | |
1507 | if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni)) | |
1508 | goto nla_put_failure; | |
1509 | ||
7c41c42c | 1510 | if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr)) |
d342894c | 1511 | goto nla_put_failure; |
1512 | ||
1513 | if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link)) | |
1514 | goto nla_put_failure; | |
1515 | ||
7c41c42c | 1516 | if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr)) |
d342894c | 1517 | goto nla_put_failure; |
1518 | ||
1519 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) || | |
1520 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) || | |
e4f67add DS |
1521 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, |
1522 | !!(vxlan->flags & VXLAN_F_LEARN)) || | |
1523 | nla_put_u8(skb, IFLA_VXLAN_PROXY, | |
1524 | !!(vxlan->flags & VXLAN_F_PROXY)) || | |
1525 | nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) || | |
1526 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, | |
1527 | !!(vxlan->flags & VXLAN_F_L2MISS)) || | |
1528 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, | |
1529 | !!(vxlan->flags & VXLAN_F_L3MISS)) || | |
d342894c | 1530 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) || |
1531 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax)) | |
1532 | goto nla_put_failure; | |
1533 | ||
05f47d69 | 1534 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
1535 | goto nla_put_failure; | |
1536 | ||
d342894c | 1537 | return 0; |
1538 | ||
1539 | nla_put_failure: | |
1540 | return -EMSGSIZE; | |
1541 | } | |
1542 | ||
1543 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { | |
1544 | .kind = "vxlan", | |
1545 | .maxtype = IFLA_VXLAN_MAX, | |
1546 | .policy = vxlan_policy, | |
1547 | .priv_size = sizeof(struct vxlan_dev), | |
1548 | .setup = vxlan_setup, | |
1549 | .validate = vxlan_validate, | |
1550 | .newlink = vxlan_newlink, | |
1551 | .dellink = vxlan_dellink, | |
1552 | .get_size = vxlan_get_size, | |
1553 | .fill_info = vxlan_fill_info, | |
1554 | }; | |
1555 | ||
1556 | static __net_init int vxlan_init_net(struct net *net) | |
1557 | { | |
1558 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
1559 | struct sock *sk; | |
1560 | struct sockaddr_in vxlan_addr = { | |
1561 | .sin_family = AF_INET, | |
1562 | .sin_addr.s_addr = htonl(INADDR_ANY), | |
1563 | }; | |
1564 | int rc; | |
1565 | unsigned h; | |
1566 | ||
1567 | /* Create UDP socket for encapsulation receive. */ | |
1568 | rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock); | |
1569 | if (rc < 0) { | |
1570 | pr_debug("UDP socket create failed\n"); | |
1571 | return rc; | |
1572 | } | |
bfe1b9b1 | 1573 | /* Put in proper namespace */ |
1574 | sk = vn->sock->sk; | |
1575 | sk_change_net(sk, net); | |
d342894c | 1576 | |
1577 | vxlan_addr.sin_port = htons(vxlan_port); | |
1578 | ||
1579 | rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr, | |
1580 | sizeof(vxlan_addr)); | |
1581 | if (rc < 0) { | |
1582 | pr_debug("bind for UDP socket %pI4:%u (%d)\n", | |
1583 | &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc); | |
bfe1b9b1 | 1584 | sk_release_kernel(sk); |
d342894c | 1585 | vn->sock = NULL; |
1586 | return rc; | |
1587 | } | |
1588 | ||
1589 | /* Disable multicast loopback */ | |
d342894c | 1590 | inet_sk(sk)->mc_loop = 0; |
1591 | ||
1592 | /* Mark socket as an encapsulation socket. */ | |
1593 | udp_sk(sk)->encap_type = 1; | |
1594 | udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv; | |
1595 | udp_encap_enable(); | |
1596 | ||
1597 | for (h = 0; h < VNI_HASH_SIZE; ++h) | |
1598 | INIT_HLIST_HEAD(&vn->vni_list[h]); | |
1599 | ||
1600 | return 0; | |
1601 | } | |
1602 | ||
1603 | static __net_exit void vxlan_exit_net(struct net *net) | |
1604 | { | |
1605 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
9cb6cb7e ZM |
1606 | struct vxlan_dev *vxlan; |
1607 | unsigned h; | |
1608 | ||
1609 | rtnl_lock(); | |
1610 | for (h = 0; h < VNI_HASH_SIZE; ++h) | |
1611 | hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist) | |
1612 | dev_close(vxlan->dev); | |
1613 | rtnl_unlock(); | |
d342894c | 1614 | |
1615 | if (vn->sock) { | |
bfe1b9b1 | 1616 | sk_release_kernel(vn->sock->sk); |
d342894c | 1617 | vn->sock = NULL; |
1618 | } | |
1619 | } | |
1620 | ||
1621 | static struct pernet_operations vxlan_net_ops = { | |
1622 | .init = vxlan_init_net, | |
1623 | .exit = vxlan_exit_net, | |
1624 | .id = &vxlan_net_id, | |
1625 | .size = sizeof(struct vxlan_net), | |
1626 | }; | |
1627 | ||
1628 | static int __init vxlan_init_module(void) | |
1629 | { | |
1630 | int rc; | |
1631 | ||
1632 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); | |
1633 | ||
1634 | rc = register_pernet_device(&vxlan_net_ops); | |
1635 | if (rc) | |
1636 | goto out1; | |
1637 | ||
1638 | rc = rtnl_link_register(&vxlan_link_ops); | |
1639 | if (rc) | |
1640 | goto out2; | |
1641 | ||
1642 | return 0; | |
1643 | ||
1644 | out2: | |
1645 | unregister_pernet_device(&vxlan_net_ops); | |
1646 | out1: | |
1647 | return rc; | |
1648 | } | |
1649 | module_init(vxlan_init_module); | |
1650 | ||
1651 | static void __exit vxlan_cleanup_module(void) | |
1652 | { | |
1653 | rtnl_link_unregister(&vxlan_link_ops); | |
1654 | unregister_pernet_device(&vxlan_net_ops); | |
6681712d | 1655 | rcu_barrier(); |
d342894c | 1656 | } |
1657 | module_exit(vxlan_cleanup_module); | |
1658 | ||
1659 | MODULE_LICENSE("GPL"); | |
1660 | MODULE_VERSION(VXLAN_VERSION); | |
1661 | MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>"); | |
1662 | MODULE_ALIAS_RTNL_LINK("vxlan"); |