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