vxlan.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /*
  2. * VXLAN: Virtual eXtensiable Local Area Network
  3. *
  4. * Copyright (c) 2012 Vyatta Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * TODO
  11. * - use IANA UDP port number (when defined)
  12. * - IPv6 (not in RFC)
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/rculist.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/udp.h>
  26. #include <linux/igmp.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/if_ether.h>
  29. #include <linux/hash.h>
  30. #include <net/ip.h>
  31. #include <net/icmp.h>
  32. #include <net/udp.h>
  33. #include <net/rtnetlink.h>
  34. #include <net/route.h>
  35. #include <net/dsfield.h>
  36. #include <net/inet_ecn.h>
  37. #include <net/net_namespace.h>
  38. #include <net/netns/generic.h>
  39. #define VXLAN_VERSION "0.1"
  40. #define VNI_HASH_BITS 10
  41. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  42. #define FDB_HASH_BITS 8
  43. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  44. #define FDB_AGE_DEFAULT 300 /* 5 min */
  45. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  46. #define VXLAN_N_VID (1u << 24)
  47. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  48. /* VLAN + IP header + UDP + VXLAN */
  49. #define VXLAN_HEADROOM (4 + 20 + 8 + 8)
  50. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  51. /* VXLAN protocol header */
  52. struct vxlanhdr {
  53. __be32 vx_flags;
  54. __be32 vx_vni;
  55. };
  56. /* UDP port for VXLAN traffic. */
  57. static unsigned int vxlan_port __read_mostly = 8472;
  58. module_param_named(udp_port, vxlan_port, uint, 0444);
  59. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  60. static bool log_ecn_error = true;
  61. module_param(log_ecn_error, bool, 0644);
  62. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  63. /* per-net private data for this module */
  64. static unsigned int vxlan_net_id;
  65. struct vxlan_net {
  66. struct socket *sock; /* UDP encap socket */
  67. struct hlist_head vni_list[VNI_HASH_SIZE];
  68. };
  69. /* Forwarding table entry */
  70. struct vxlan_fdb {
  71. struct hlist_node hlist; /* linked list of entries */
  72. struct rcu_head rcu;
  73. unsigned long updated; /* jiffies */
  74. unsigned long used;
  75. __be32 remote_ip;
  76. u16 state; /* see ndm_state */
  77. u8 eth_addr[ETH_ALEN];
  78. };
  79. /* Per-cpu network traffic stats */
  80. struct vxlan_stats {
  81. u64 rx_packets;
  82. u64 rx_bytes;
  83. u64 tx_packets;
  84. u64 tx_bytes;
  85. struct u64_stats_sync syncp;
  86. };
  87. /* Pseudo network device */
  88. struct vxlan_dev {
  89. struct hlist_node hlist;
  90. struct net_device *dev;
  91. struct vxlan_stats __percpu *stats;
  92. __u32 vni; /* virtual network id */
  93. __be32 gaddr; /* multicast group */
  94. __be32 saddr; /* source address */
  95. unsigned int link; /* link to multicast over */
  96. __u8 tos; /* TOS override */
  97. __u8 ttl;
  98. bool learn;
  99. unsigned long age_interval;
  100. struct timer_list age_timer;
  101. spinlock_t hash_lock;
  102. unsigned int addrcnt;
  103. unsigned int addrmax;
  104. unsigned int addrexceeded;
  105. struct hlist_head fdb_head[FDB_HASH_SIZE];
  106. };
  107. /* salt for hash table */
  108. static u32 vxlan_salt __read_mostly;
  109. static inline struct hlist_head *vni_head(struct net *net, u32 id)
  110. {
  111. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  112. return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
  113. }
  114. /* Look up VNI in a per net namespace table */
  115. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
  116. {
  117. struct vxlan_dev *vxlan;
  118. struct hlist_node *node;
  119. hlist_for_each_entry_rcu(vxlan, node, vni_head(net, id), hlist) {
  120. if (vxlan->vni == id)
  121. return vxlan;
  122. }
  123. return NULL;
  124. }
  125. /* Fill in neighbour message in skbuff. */
  126. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  127. const struct vxlan_fdb *fdb,
  128. u32 portid, u32 seq, int type, unsigned int flags)
  129. {
  130. unsigned long now = jiffies;
  131. struct nda_cacheinfo ci;
  132. struct nlmsghdr *nlh;
  133. struct ndmsg *ndm;
  134. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  135. if (nlh == NULL)
  136. return -EMSGSIZE;
  137. ndm = nlmsg_data(nlh);
  138. memset(ndm, 0, sizeof(*ndm));
  139. ndm->ndm_family = AF_BRIDGE;
  140. ndm->ndm_state = fdb->state;
  141. ndm->ndm_ifindex = vxlan->dev->ifindex;
  142. ndm->ndm_flags = NTF_SELF;
  143. ndm->ndm_type = NDA_DST;
  144. if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  145. goto nla_put_failure;
  146. if (nla_put_be32(skb, NDA_DST, fdb->remote_ip))
  147. goto nla_put_failure;
  148. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  149. ci.ndm_confirmed = 0;
  150. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  151. ci.ndm_refcnt = 0;
  152. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  153. goto nla_put_failure;
  154. return nlmsg_end(skb, nlh);
  155. nla_put_failure:
  156. nlmsg_cancel(skb, nlh);
  157. return -EMSGSIZE;
  158. }
  159. static inline size_t vxlan_nlmsg_size(void)
  160. {
  161. return NLMSG_ALIGN(sizeof(struct ndmsg))
  162. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  163. + nla_total_size(sizeof(__be32)) /* NDA_DST */
  164. + nla_total_size(sizeof(struct nda_cacheinfo));
  165. }
  166. static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
  167. const struct vxlan_fdb *fdb, int type)
  168. {
  169. struct net *net = dev_net(vxlan->dev);
  170. struct sk_buff *skb;
  171. int err = -ENOBUFS;
  172. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  173. if (skb == NULL)
  174. goto errout;
  175. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0);
  176. if (err < 0) {
  177. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  178. WARN_ON(err == -EMSGSIZE);
  179. kfree_skb(skb);
  180. goto errout;
  181. }
  182. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  183. return;
  184. errout:
  185. if (err < 0)
  186. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  187. }
  188. /* Hash Ethernet address */
  189. static u32 eth_hash(const unsigned char *addr)
  190. {
  191. u64 value = get_unaligned((u64 *)addr);
  192. /* only want 6 bytes */
  193. #ifdef __BIG_ENDIAN
  194. value <<= 16;
  195. #else
  196. value >>= 16;
  197. #endif
  198. return hash_64(value, FDB_HASH_BITS);
  199. }
  200. /* Hash chain to use given mac address */
  201. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  202. const u8 *mac)
  203. {
  204. return &vxlan->fdb_head[eth_hash(mac)];
  205. }
  206. /* Look up Ethernet address in forwarding table */
  207. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  208. const u8 *mac)
  209. {
  210. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  211. struct vxlan_fdb *f;
  212. struct hlist_node *node;
  213. hlist_for_each_entry_rcu(f, node, head, hlist) {
  214. if (compare_ether_addr(mac, f->eth_addr) == 0)
  215. return f;
  216. }
  217. return NULL;
  218. }
  219. /* Add new entry to forwarding table -- assumes lock held */
  220. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  221. const u8 *mac, __be32 ip,
  222. __u16 state, __u16 flags)
  223. {
  224. struct vxlan_fdb *f;
  225. int notify = 0;
  226. f = vxlan_find_mac(vxlan, mac);
  227. if (f) {
  228. if (flags & NLM_F_EXCL) {
  229. netdev_dbg(vxlan->dev,
  230. "lost race to create %pM\n", mac);
  231. return -EEXIST;
  232. }
  233. if (f->state != state) {
  234. f->state = state;
  235. f->updated = jiffies;
  236. notify = 1;
  237. }
  238. } else {
  239. if (!(flags & NLM_F_CREATE))
  240. return -ENOENT;
  241. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  242. return -ENOSPC;
  243. netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
  244. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  245. if (!f)
  246. return -ENOMEM;
  247. notify = 1;
  248. f->remote_ip = ip;
  249. f->state = state;
  250. f->updated = f->used = jiffies;
  251. memcpy(f->eth_addr, mac, ETH_ALEN);
  252. ++vxlan->addrcnt;
  253. hlist_add_head_rcu(&f->hlist,
  254. vxlan_fdb_head(vxlan, mac));
  255. }
  256. if (notify)
  257. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  258. return 0;
  259. }
  260. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  261. {
  262. netdev_dbg(vxlan->dev,
  263. "delete %pM\n", f->eth_addr);
  264. --vxlan->addrcnt;
  265. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  266. hlist_del_rcu(&f->hlist);
  267. kfree_rcu(f, rcu);
  268. }
  269. /* Add static entry (via netlink) */
  270. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  271. struct net_device *dev,
  272. const unsigned char *addr, u16 flags)
  273. {
  274. struct vxlan_dev *vxlan = netdev_priv(dev);
  275. __be32 ip;
  276. int err;
  277. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  278. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  279. ndm->ndm_state);
  280. return -EINVAL;
  281. }
  282. if (tb[NDA_DST] == NULL)
  283. return -EINVAL;
  284. if (nla_len(tb[NDA_DST]) != sizeof(__be32))
  285. return -EAFNOSUPPORT;
  286. ip = nla_get_be32(tb[NDA_DST]);
  287. spin_lock_bh(&vxlan->hash_lock);
  288. err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags);
  289. spin_unlock_bh(&vxlan->hash_lock);
  290. return err;
  291. }
  292. /* Delete entry (via netlink) */
  293. static int vxlan_fdb_delete(struct ndmsg *ndm, struct net_device *dev,
  294. const unsigned char *addr)
  295. {
  296. struct vxlan_dev *vxlan = netdev_priv(dev);
  297. struct vxlan_fdb *f;
  298. int err = -ENOENT;
  299. spin_lock_bh(&vxlan->hash_lock);
  300. f = vxlan_find_mac(vxlan, addr);
  301. if (f) {
  302. vxlan_fdb_destroy(vxlan, f);
  303. err = 0;
  304. }
  305. spin_unlock_bh(&vxlan->hash_lock);
  306. return err;
  307. }
  308. /* Dump forwarding table */
  309. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  310. struct net_device *dev, int idx)
  311. {
  312. struct vxlan_dev *vxlan = netdev_priv(dev);
  313. unsigned int h;
  314. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  315. struct vxlan_fdb *f;
  316. struct hlist_node *n;
  317. int err;
  318. hlist_for_each_entry_rcu(f, n, &vxlan->fdb_head[h], hlist) {
  319. if (idx < cb->args[0])
  320. goto skip;
  321. err = vxlan_fdb_info(skb, vxlan, f,
  322. NETLINK_CB(cb->skb).portid,
  323. cb->nlh->nlmsg_seq,
  324. RTM_NEWNEIGH,
  325. NLM_F_MULTI);
  326. if (err < 0)
  327. break;
  328. skip:
  329. ++idx;
  330. }
  331. }
  332. return idx;
  333. }
  334. /* Watch incoming packets to learn mapping between Ethernet address
  335. * and Tunnel endpoint.
  336. */
  337. static void vxlan_snoop(struct net_device *dev,
  338. __be32 src_ip, const u8 *src_mac)
  339. {
  340. struct vxlan_dev *vxlan = netdev_priv(dev);
  341. struct vxlan_fdb *f;
  342. int err;
  343. f = vxlan_find_mac(vxlan, src_mac);
  344. if (likely(f)) {
  345. f->used = jiffies;
  346. if (likely(f->remote_ip == src_ip))
  347. return;
  348. if (net_ratelimit())
  349. netdev_info(dev,
  350. "%pM migrated from %pI4 to %pI4\n",
  351. src_mac, &f->remote_ip, &src_ip);
  352. f->remote_ip = src_ip;
  353. f->updated = jiffies;
  354. } else {
  355. /* learned new entry */
  356. spin_lock(&vxlan->hash_lock);
  357. err = vxlan_fdb_create(vxlan, src_mac, src_ip,
  358. NUD_REACHABLE,
  359. NLM_F_EXCL|NLM_F_CREATE);
  360. spin_unlock(&vxlan->hash_lock);
  361. }
  362. }
  363. /* See if multicast group is already in use by other ID */
  364. static bool vxlan_group_used(struct vxlan_net *vn,
  365. const struct vxlan_dev *this)
  366. {
  367. const struct vxlan_dev *vxlan;
  368. struct hlist_node *node;
  369. unsigned h;
  370. for (h = 0; h < VNI_HASH_SIZE; ++h)
  371. hlist_for_each_entry(vxlan, node, &vn->vni_list[h], hlist) {
  372. if (vxlan == this)
  373. continue;
  374. if (!netif_running(vxlan->dev))
  375. continue;
  376. if (vxlan->gaddr == this->gaddr)
  377. return true;
  378. }
  379. return false;
  380. }
  381. /* kernel equivalent to IP_ADD_MEMBERSHIP */
  382. static int vxlan_join_group(struct net_device *dev)
  383. {
  384. struct vxlan_dev *vxlan = netdev_priv(dev);
  385. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  386. struct sock *sk = vn->sock->sk;
  387. struct ip_mreqn mreq = {
  388. .imr_multiaddr.s_addr = vxlan->gaddr,
  389. };
  390. int err;
  391. /* Already a member of group */
  392. if (vxlan_group_used(vn, vxlan))
  393. return 0;
  394. /* Need to drop RTNL to call multicast join */
  395. rtnl_unlock();
  396. lock_sock(sk);
  397. err = ip_mc_join_group(sk, &mreq);
  398. release_sock(sk);
  399. rtnl_lock();
  400. return err;
  401. }
  402. /* kernel equivalent to IP_DROP_MEMBERSHIP */
  403. static int vxlan_leave_group(struct net_device *dev)
  404. {
  405. struct vxlan_dev *vxlan = netdev_priv(dev);
  406. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  407. int err = 0;
  408. struct sock *sk = vn->sock->sk;
  409. struct ip_mreqn mreq = {
  410. .imr_multiaddr.s_addr = vxlan->gaddr,
  411. };
  412. /* Only leave group when last vxlan is done. */
  413. if (vxlan_group_used(vn, vxlan))
  414. return 0;
  415. /* Need to drop RTNL to call multicast leave */
  416. rtnl_unlock();
  417. lock_sock(sk);
  418. err = ip_mc_leave_group(sk, &mreq);
  419. release_sock(sk);
  420. rtnl_lock();
  421. return err;
  422. }
  423. /* Callback from net/ipv4/udp.c to receive packets */
  424. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  425. {
  426. struct iphdr *oip;
  427. struct vxlanhdr *vxh;
  428. struct vxlan_dev *vxlan;
  429. struct vxlan_stats *stats;
  430. __u32 vni;
  431. int err;
  432. /* pop off outer UDP header */
  433. __skb_pull(skb, sizeof(struct udphdr));
  434. /* Need Vxlan and inner Ethernet header to be present */
  435. if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
  436. goto error;
  437. /* Drop packets with reserved bits set */
  438. vxh = (struct vxlanhdr *) skb->data;
  439. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  440. (vxh->vx_vni & htonl(0xff))) {
  441. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  442. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  443. goto error;
  444. }
  445. __skb_pull(skb, sizeof(struct vxlanhdr));
  446. skb_postpull_rcsum(skb, eth_hdr(skb), sizeof(struct vxlanhdr));
  447. /* Is this VNI defined? */
  448. vni = ntohl(vxh->vx_vni) >> 8;
  449. vxlan = vxlan_find_vni(sock_net(sk), vni);
  450. if (!vxlan) {
  451. netdev_dbg(skb->dev, "unknown vni %d\n", vni);
  452. goto drop;
  453. }
  454. if (!pskb_may_pull(skb, ETH_HLEN)) {
  455. vxlan->dev->stats.rx_length_errors++;
  456. vxlan->dev->stats.rx_errors++;
  457. goto drop;
  458. }
  459. /* Re-examine inner Ethernet packet */
  460. oip = ip_hdr(skb);
  461. skb->protocol = eth_type_trans(skb, vxlan->dev);
  462. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  463. /* Ignore packet loops (and multicast echo) */
  464. if (compare_ether_addr(eth_hdr(skb)->h_source,
  465. vxlan->dev->dev_addr) == 0)
  466. goto drop;
  467. if (vxlan->learn)
  468. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
  469. __skb_tunnel_rx(skb, vxlan->dev);
  470. skb_reset_network_header(skb);
  471. err = IP_ECN_decapsulate(oip, skb);
  472. if (unlikely(err)) {
  473. if (log_ecn_error)
  474. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  475. &oip->saddr, oip->tos);
  476. if (err > 1) {
  477. ++vxlan->dev->stats.rx_frame_errors;
  478. ++vxlan->dev->stats.rx_errors;
  479. goto drop;
  480. }
  481. }
  482. stats = this_cpu_ptr(vxlan->stats);
  483. u64_stats_update_begin(&stats->syncp);
  484. stats->rx_packets++;
  485. stats->rx_bytes += skb->len;
  486. u64_stats_update_end(&stats->syncp);
  487. netif_rx(skb);
  488. return 0;
  489. error:
  490. /* Put UDP header back */
  491. __skb_push(skb, sizeof(struct udphdr));
  492. return 1;
  493. drop:
  494. /* Consume bad packet */
  495. kfree_skb(skb);
  496. return 0;
  497. }
  498. /* Extract dsfield from inner protocol */
  499. static inline u8 vxlan_get_dsfield(const struct iphdr *iph,
  500. const struct sk_buff *skb)
  501. {
  502. if (skb->protocol == htons(ETH_P_IP))
  503. return iph->tos;
  504. else if (skb->protocol == htons(ETH_P_IPV6))
  505. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  506. else
  507. return 0;
  508. }
  509. /* Propogate ECN bits out */
  510. static inline u8 vxlan_ecn_encap(u8 tos,
  511. const struct iphdr *iph,
  512. const struct sk_buff *skb)
  513. {
  514. u8 inner = vxlan_get_dsfield(iph, skb);
  515. return INET_ECN_encapsulate(tos, inner);
  516. }
  517. /* Transmit local packets over Vxlan
  518. *
  519. * Outer IP header inherits ECN and DF from inner header.
  520. * Outer UDP destination is the VXLAN assigned port.
  521. * source port is based on hash of flow if available
  522. * otherwise use a random value
  523. */
  524. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  525. {
  526. struct vxlan_dev *vxlan = netdev_priv(dev);
  527. struct rtable *rt;
  528. const struct ethhdr *eth;
  529. const struct iphdr *old_iph;
  530. struct iphdr *iph;
  531. struct vxlanhdr *vxh;
  532. struct udphdr *uh;
  533. struct flowi4 fl4;
  534. struct vxlan_fdb *f;
  535. unsigned int pkt_len = skb->len;
  536. u32 hash;
  537. __be32 dst;
  538. __be16 df = 0;
  539. __u8 tos, ttl;
  540. int err;
  541. /* Need space for new headers (invalidates iph ptr) */
  542. if (skb_cow_head(skb, VXLAN_HEADROOM))
  543. goto drop;
  544. eth = (void *)skb->data;
  545. old_iph = ip_hdr(skb);
  546. if (!is_multicast_ether_addr(eth->h_dest) &&
  547. (f = vxlan_find_mac(vxlan, eth->h_dest)))
  548. dst = f->remote_ip;
  549. else if (vxlan->gaddr) {
  550. dst = vxlan->gaddr;
  551. } else
  552. goto drop;
  553. ttl = vxlan->ttl;
  554. if (!ttl && IN_MULTICAST(ntohl(dst)))
  555. ttl = 1;
  556. tos = vxlan->tos;
  557. if (tos == 1)
  558. tos = vxlan_get_dsfield(old_iph, skb);
  559. hash = skb_get_rxhash(skb);
  560. rt = ip_route_output_gre(dev_net(dev), &fl4, dst,
  561. vxlan->saddr, vxlan->vni,
  562. RT_TOS(tos), vxlan->link);
  563. if (IS_ERR(rt)) {
  564. netdev_dbg(dev, "no route to %pI4\n", &dst);
  565. dev->stats.tx_carrier_errors++;
  566. goto tx_error;
  567. }
  568. if (rt->dst.dev == dev) {
  569. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  570. ip_rt_put(rt);
  571. dev->stats.collisions++;
  572. goto tx_error;
  573. }
  574. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  575. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  576. IPSKB_REROUTED);
  577. skb_dst_drop(skb);
  578. skb_dst_set(skb, &rt->dst);
  579. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  580. vxh->vx_flags = htonl(VXLAN_FLAGS);
  581. vxh->vx_vni = htonl(vxlan->vni << 8);
  582. __skb_push(skb, sizeof(*uh));
  583. skb_reset_transport_header(skb);
  584. uh = udp_hdr(skb);
  585. uh->dest = htons(vxlan_port);
  586. uh->source = hash ? :random32();
  587. uh->len = htons(skb->len);
  588. uh->check = 0;
  589. __skb_push(skb, sizeof(*iph));
  590. skb_reset_network_header(skb);
  591. iph = ip_hdr(skb);
  592. iph->version = 4;
  593. iph->ihl = sizeof(struct iphdr) >> 2;
  594. iph->frag_off = df;
  595. iph->protocol = IPPROTO_UDP;
  596. iph->tos = vxlan_ecn_encap(tos, old_iph, skb);
  597. iph->daddr = fl4.daddr;
  598. iph->saddr = fl4.saddr;
  599. iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  600. /* See __IPTUNNEL_XMIT */
  601. skb->ip_summed = CHECKSUM_NONE;
  602. ip_select_ident(iph, &rt->dst, NULL);
  603. err = ip_local_out(skb);
  604. if (likely(net_xmit_eval(err) == 0)) {
  605. struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats);
  606. u64_stats_update_begin(&stats->syncp);
  607. stats->tx_packets++;
  608. stats->tx_bytes += pkt_len;
  609. u64_stats_update_end(&stats->syncp);
  610. } else {
  611. dev->stats.tx_errors++;
  612. dev->stats.tx_aborted_errors++;
  613. }
  614. return NETDEV_TX_OK;
  615. drop:
  616. dev->stats.tx_dropped++;
  617. goto tx_free;
  618. tx_error:
  619. dev->stats.tx_errors++;
  620. tx_free:
  621. dev_kfree_skb(skb);
  622. return NETDEV_TX_OK;
  623. }
  624. /* Walk the forwarding table and purge stale entries */
  625. static void vxlan_cleanup(unsigned long arg)
  626. {
  627. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  628. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  629. unsigned int h;
  630. if (!netif_running(vxlan->dev))
  631. return;
  632. spin_lock_bh(&vxlan->hash_lock);
  633. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  634. struct hlist_node *p, *n;
  635. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  636. struct vxlan_fdb *f
  637. = container_of(p, struct vxlan_fdb, hlist);
  638. unsigned long timeout;
  639. if (f->state == NUD_PERMANENT)
  640. continue;
  641. timeout = f->used + vxlan->age_interval * HZ;
  642. if (time_before_eq(timeout, jiffies)) {
  643. netdev_dbg(vxlan->dev,
  644. "garbage collect %pM\n",
  645. f->eth_addr);
  646. f->state = NUD_STALE;
  647. vxlan_fdb_destroy(vxlan, f);
  648. } else if (time_before(timeout, next_timer))
  649. next_timer = timeout;
  650. }
  651. }
  652. spin_unlock_bh(&vxlan->hash_lock);
  653. mod_timer(&vxlan->age_timer, next_timer);
  654. }
  655. /* Setup stats when device is created */
  656. static int vxlan_init(struct net_device *dev)
  657. {
  658. struct vxlan_dev *vxlan = netdev_priv(dev);
  659. vxlan->stats = alloc_percpu(struct vxlan_stats);
  660. if (!vxlan->stats)
  661. return -ENOMEM;
  662. return 0;
  663. }
  664. /* Start ageing timer and join group when device is brought up */
  665. static int vxlan_open(struct net_device *dev)
  666. {
  667. struct vxlan_dev *vxlan = netdev_priv(dev);
  668. int err;
  669. if (vxlan->gaddr) {
  670. err = vxlan_join_group(dev);
  671. if (err)
  672. return err;
  673. }
  674. if (vxlan->age_interval)
  675. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  676. return 0;
  677. }
  678. /* Purge the forwarding table */
  679. static void vxlan_flush(struct vxlan_dev *vxlan)
  680. {
  681. unsigned h;
  682. spin_lock_bh(&vxlan->hash_lock);
  683. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  684. struct hlist_node *p, *n;
  685. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  686. struct vxlan_fdb *f
  687. = container_of(p, struct vxlan_fdb, hlist);
  688. vxlan_fdb_destroy(vxlan, f);
  689. }
  690. }
  691. spin_unlock_bh(&vxlan->hash_lock);
  692. }
  693. /* Cleanup timer and forwarding table on shutdown */
  694. static int vxlan_stop(struct net_device *dev)
  695. {
  696. struct vxlan_dev *vxlan = netdev_priv(dev);
  697. if (vxlan->gaddr)
  698. vxlan_leave_group(dev);
  699. del_timer_sync(&vxlan->age_timer);
  700. vxlan_flush(vxlan);
  701. return 0;
  702. }
  703. /* Merge per-cpu statistics */
  704. static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev,
  705. struct rtnl_link_stats64 *stats)
  706. {
  707. struct vxlan_dev *vxlan = netdev_priv(dev);
  708. struct vxlan_stats tmp, sum = { 0 };
  709. unsigned int cpu;
  710. for_each_possible_cpu(cpu) {
  711. unsigned int start;
  712. const struct vxlan_stats *stats
  713. = per_cpu_ptr(vxlan->stats, cpu);
  714. do {
  715. start = u64_stats_fetch_begin_bh(&stats->syncp);
  716. memcpy(&tmp, stats, sizeof(tmp));
  717. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  718. sum.tx_bytes += tmp.tx_bytes;
  719. sum.tx_packets += tmp.tx_packets;
  720. sum.rx_bytes += tmp.rx_bytes;
  721. sum.rx_packets += tmp.rx_packets;
  722. }
  723. stats->tx_bytes = sum.tx_bytes;
  724. stats->tx_packets = sum.tx_packets;
  725. stats->rx_bytes = sum.rx_bytes;
  726. stats->rx_packets = sum.rx_packets;
  727. stats->multicast = dev->stats.multicast;
  728. stats->rx_length_errors = dev->stats.rx_length_errors;
  729. stats->rx_frame_errors = dev->stats.rx_frame_errors;
  730. stats->rx_errors = dev->stats.rx_errors;
  731. stats->tx_dropped = dev->stats.tx_dropped;
  732. stats->tx_carrier_errors = dev->stats.tx_carrier_errors;
  733. stats->tx_aborted_errors = dev->stats.tx_aborted_errors;
  734. stats->collisions = dev->stats.collisions;
  735. stats->tx_errors = dev->stats.tx_errors;
  736. return stats;
  737. }
  738. /* Stub, nothing needs to be done. */
  739. static void vxlan_set_multicast_list(struct net_device *dev)
  740. {
  741. }
  742. static const struct net_device_ops vxlan_netdev_ops = {
  743. .ndo_init = vxlan_init,
  744. .ndo_open = vxlan_open,
  745. .ndo_stop = vxlan_stop,
  746. .ndo_start_xmit = vxlan_xmit,
  747. .ndo_get_stats64 = vxlan_stats64,
  748. .ndo_set_rx_mode = vxlan_set_multicast_list,
  749. .ndo_change_mtu = eth_change_mtu,
  750. .ndo_validate_addr = eth_validate_addr,
  751. .ndo_set_mac_address = eth_mac_addr,
  752. .ndo_fdb_add = vxlan_fdb_add,
  753. .ndo_fdb_del = vxlan_fdb_delete,
  754. .ndo_fdb_dump = vxlan_fdb_dump,
  755. };
  756. /* Info for udev, that this is a virtual tunnel endpoint */
  757. static struct device_type vxlan_type = {
  758. .name = "vxlan",
  759. };
  760. static void vxlan_free(struct net_device *dev)
  761. {
  762. struct vxlan_dev *vxlan = netdev_priv(dev);
  763. free_percpu(vxlan->stats);
  764. free_netdev(dev);
  765. }
  766. /* Initialize the device structure. */
  767. static void vxlan_setup(struct net_device *dev)
  768. {
  769. struct vxlan_dev *vxlan = netdev_priv(dev);
  770. unsigned h;
  771. eth_hw_addr_random(dev);
  772. ether_setup(dev);
  773. dev->netdev_ops = &vxlan_netdev_ops;
  774. dev->destructor = vxlan_free;
  775. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  776. dev->tx_queue_len = 0;
  777. dev->features |= NETIF_F_LLTX;
  778. dev->features |= NETIF_F_NETNS_LOCAL;
  779. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  780. spin_lock_init(&vxlan->hash_lock);
  781. init_timer_deferrable(&vxlan->age_timer);
  782. vxlan->age_timer.function = vxlan_cleanup;
  783. vxlan->age_timer.data = (unsigned long) vxlan;
  784. vxlan->dev = dev;
  785. for (h = 0; h < FDB_HASH_SIZE; ++h)
  786. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  787. }
  788. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  789. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  790. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  791. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  792. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  793. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  794. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  795. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  796. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  797. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  798. };
  799. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  800. {
  801. if (tb[IFLA_ADDRESS]) {
  802. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  803. pr_debug("invalid link address (not ethernet)\n");
  804. return -EINVAL;
  805. }
  806. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  807. pr_debug("invalid all zero ethernet address\n");
  808. return -EADDRNOTAVAIL;
  809. }
  810. }
  811. if (!data)
  812. return -EINVAL;
  813. if (data[IFLA_VXLAN_ID]) {
  814. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  815. if (id >= VXLAN_VID_MASK)
  816. return -ERANGE;
  817. }
  818. if (data[IFLA_VXLAN_GROUP]) {
  819. __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  820. if (!IN_MULTICAST(ntohl(gaddr))) {
  821. pr_debug("group address is not IPv4 multicast\n");
  822. return -EADDRNOTAVAIL;
  823. }
  824. }
  825. return 0;
  826. }
  827. static int vxlan_newlink(struct net *net, struct net_device *dev,
  828. struct nlattr *tb[], struct nlattr *data[])
  829. {
  830. struct vxlan_dev *vxlan = netdev_priv(dev);
  831. __u32 vni;
  832. int err;
  833. if (!data[IFLA_VXLAN_ID])
  834. return -EINVAL;
  835. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  836. if (vxlan_find_vni(net, vni)) {
  837. pr_info("duplicate VNI %u\n", vni);
  838. return -EEXIST;
  839. }
  840. vxlan->vni = vni;
  841. if (data[IFLA_VXLAN_GROUP])
  842. vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  843. if (data[IFLA_VXLAN_LOCAL])
  844. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  845. if (data[IFLA_VXLAN_LINK]) {
  846. vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]);
  847. if (!tb[IFLA_MTU]) {
  848. struct net_device *lowerdev;
  849. lowerdev = __dev_get_by_index(net, vxlan->link);
  850. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  851. }
  852. }
  853. if (data[IFLA_VXLAN_TOS])
  854. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  855. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  856. vxlan->learn = true;
  857. if (data[IFLA_VXLAN_AGEING])
  858. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  859. else
  860. vxlan->age_interval = FDB_AGE_DEFAULT;
  861. if (data[IFLA_VXLAN_LIMIT])
  862. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  863. err = register_netdevice(dev);
  864. if (!err)
  865. hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
  866. return err;
  867. }
  868. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  869. {
  870. struct vxlan_dev *vxlan = netdev_priv(dev);
  871. hlist_del_rcu(&vxlan->hlist);
  872. unregister_netdevice_queue(dev, head);
  873. }
  874. static size_t vxlan_get_size(const struct net_device *dev)
  875. {
  876. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  877. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  878. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  879. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  880. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  881. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  882. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  883. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  884. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  885. 0;
  886. }
  887. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  888. {
  889. const struct vxlan_dev *vxlan = netdev_priv(dev);
  890. if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
  891. goto nla_put_failure;
  892. if (vxlan->gaddr && nla_put_u32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
  893. goto nla_put_failure;
  894. if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
  895. goto nla_put_failure;
  896. if (vxlan->saddr && nla_put_u32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  897. goto nla_put_failure;
  898. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  899. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  900. nla_put_u8(skb, IFLA_VXLAN_LEARNING, vxlan->learn) ||
  901. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  902. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
  903. goto nla_put_failure;
  904. return 0;
  905. nla_put_failure:
  906. return -EMSGSIZE;
  907. }
  908. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  909. .kind = "vxlan",
  910. .maxtype = IFLA_VXLAN_MAX,
  911. .policy = vxlan_policy,
  912. .priv_size = sizeof(struct vxlan_dev),
  913. .setup = vxlan_setup,
  914. .validate = vxlan_validate,
  915. .newlink = vxlan_newlink,
  916. .dellink = vxlan_dellink,
  917. .get_size = vxlan_get_size,
  918. .fill_info = vxlan_fill_info,
  919. };
  920. static __net_init int vxlan_init_net(struct net *net)
  921. {
  922. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  923. struct sock *sk;
  924. struct sockaddr_in vxlan_addr = {
  925. .sin_family = AF_INET,
  926. .sin_addr.s_addr = htonl(INADDR_ANY),
  927. };
  928. int rc;
  929. unsigned h;
  930. /* Create UDP socket for encapsulation receive. */
  931. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
  932. if (rc < 0) {
  933. pr_debug("UDP socket create failed\n");
  934. return rc;
  935. }
  936. /* Put in proper namespace */
  937. sk = vn->sock->sk;
  938. sk_change_net(sk, net);
  939. vxlan_addr.sin_port = htons(vxlan_port);
  940. rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
  941. sizeof(vxlan_addr));
  942. if (rc < 0) {
  943. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  944. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  945. sk_release_kernel(sk);
  946. vn->sock = NULL;
  947. return rc;
  948. }
  949. /* Disable multicast loopback */
  950. inet_sk(sk)->mc_loop = 0;
  951. /* Mark socket as an encapsulation socket. */
  952. udp_sk(sk)->encap_type = 1;
  953. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  954. udp_encap_enable();
  955. for (h = 0; h < VNI_HASH_SIZE; ++h)
  956. INIT_HLIST_HEAD(&vn->vni_list[h]);
  957. return 0;
  958. }
  959. static __net_exit void vxlan_exit_net(struct net *net)
  960. {
  961. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  962. if (vn->sock) {
  963. sk_release_kernel(vn->sock->sk);
  964. vn->sock = NULL;
  965. }
  966. }
  967. static struct pernet_operations vxlan_net_ops = {
  968. .init = vxlan_init_net,
  969. .exit = vxlan_exit_net,
  970. .id = &vxlan_net_id,
  971. .size = sizeof(struct vxlan_net),
  972. };
  973. static int __init vxlan_init_module(void)
  974. {
  975. int rc;
  976. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  977. rc = register_pernet_device(&vxlan_net_ops);
  978. if (rc)
  979. goto out1;
  980. rc = rtnl_link_register(&vxlan_link_ops);
  981. if (rc)
  982. goto out2;
  983. return 0;
  984. out2:
  985. unregister_pernet_device(&vxlan_net_ops);
  986. out1:
  987. return rc;
  988. }
  989. module_init(vxlan_init_module);
  990. static void __exit vxlan_cleanup_module(void)
  991. {
  992. rtnl_link_unregister(&vxlan_link_ops);
  993. unregister_pernet_device(&vxlan_net_ops);
  994. }
  995. module_exit(vxlan_cleanup_module);
  996. MODULE_LICENSE("GPL");
  997. MODULE_VERSION(VXLAN_VERSION);
  998. MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
  999. MODULE_ALIAS_RTNL_LINK("vxlan");