vxlan.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. /*
  2. * VXLAN: Virtual eXtensible 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/arp.h>
  31. #include <net/ndisc.h>
  32. #include <net/ip.h>
  33. #include <net/icmp.h>
  34. #include <net/udp.h>
  35. #include <net/rtnetlink.h>
  36. #include <net/route.h>
  37. #include <net/dsfield.h>
  38. #include <net/inet_ecn.h>
  39. #include <net/net_namespace.h>
  40. #include <net/netns/generic.h>
  41. #define VXLAN_VERSION "0.1"
  42. #define VNI_HASH_BITS 10
  43. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  44. #define FDB_HASH_BITS 8
  45. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  46. #define FDB_AGE_DEFAULT 300 /* 5 min */
  47. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  48. #define VXLAN_N_VID (1u << 24)
  49. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  50. /* IP header + UDP + VXLAN + Ethernet header */
  51. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  52. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  53. /* VXLAN protocol header */
  54. struct vxlanhdr {
  55. __be32 vx_flags;
  56. __be32 vx_vni;
  57. };
  58. /* UDP port for VXLAN traffic. */
  59. static unsigned int vxlan_port __read_mostly = 8472;
  60. module_param_named(udp_port, vxlan_port, uint, 0444);
  61. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  62. static bool log_ecn_error = true;
  63. module_param(log_ecn_error, bool, 0644);
  64. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  65. /* per-net private data for this module */
  66. static unsigned int vxlan_net_id;
  67. struct vxlan_net {
  68. struct socket *sock; /* UDP encap socket */
  69. struct hlist_head vni_list[VNI_HASH_SIZE];
  70. };
  71. /* Forwarding table entry */
  72. struct vxlan_fdb {
  73. struct hlist_node hlist; /* linked list of entries */
  74. struct rcu_head rcu;
  75. unsigned long updated; /* jiffies */
  76. unsigned long used;
  77. __be32 remote_ip;
  78. u16 state; /* see ndm_state */
  79. u8 eth_addr[ETH_ALEN];
  80. };
  81. /* Per-cpu network traffic stats */
  82. struct vxlan_stats {
  83. u64 rx_packets;
  84. u64 rx_bytes;
  85. u64 tx_packets;
  86. u64 tx_bytes;
  87. struct u64_stats_sync syncp;
  88. };
  89. /* Pseudo network device */
  90. struct vxlan_dev {
  91. struct hlist_node hlist;
  92. struct net_device *dev;
  93. struct vxlan_stats __percpu *stats;
  94. __u32 vni; /* virtual network id */
  95. __be32 gaddr; /* multicast group */
  96. __be32 saddr; /* source address */
  97. unsigned int link; /* link to multicast over */
  98. __u16 port_min; /* source port range */
  99. __u16 port_max;
  100. __u8 tos; /* TOS override */
  101. __u8 ttl;
  102. u32 flags; /* VXLAN_F_* below */
  103. unsigned long age_interval;
  104. struct timer_list age_timer;
  105. spinlock_t hash_lock;
  106. unsigned int addrcnt;
  107. unsigned int addrmax;
  108. struct hlist_head fdb_head[FDB_HASH_SIZE];
  109. };
  110. #define VXLAN_F_LEARN 0x01
  111. #define VXLAN_F_PROXY 0x02
  112. #define VXLAN_F_RSC 0x04
  113. #define VXLAN_F_L2MISS 0x08
  114. #define VXLAN_F_L3MISS 0x10
  115. /* salt for hash table */
  116. static u32 vxlan_salt __read_mostly;
  117. static inline struct hlist_head *vni_head(struct net *net, u32 id)
  118. {
  119. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  120. return &vn->vni_list[hash_32(id, VNI_HASH_BITS)];
  121. }
  122. /* Look up VNI in a per net namespace table */
  123. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id)
  124. {
  125. struct vxlan_dev *vxlan;
  126. struct hlist_node *node;
  127. hlist_for_each_entry_rcu(vxlan, node, vni_head(net, id), hlist) {
  128. if (vxlan->vni == id)
  129. return vxlan;
  130. }
  131. return NULL;
  132. }
  133. /* Fill in neighbour message in skbuff. */
  134. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  135. const struct vxlan_fdb *fdb,
  136. u32 portid, u32 seq, int type, unsigned int flags)
  137. {
  138. unsigned long now = jiffies;
  139. struct nda_cacheinfo ci;
  140. struct nlmsghdr *nlh;
  141. struct ndmsg *ndm;
  142. bool send_ip, send_eth;
  143. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  144. if (nlh == NULL)
  145. return -EMSGSIZE;
  146. ndm = nlmsg_data(nlh);
  147. memset(ndm, 0, sizeof(*ndm));
  148. send_eth = send_ip = true;
  149. if (type == RTM_GETNEIGH) {
  150. ndm->ndm_family = AF_INET;
  151. send_ip = fdb->remote_ip != 0;
  152. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  153. } else
  154. ndm->ndm_family = AF_BRIDGE;
  155. ndm->ndm_state = fdb->state;
  156. ndm->ndm_ifindex = vxlan->dev->ifindex;
  157. ndm->ndm_flags = NTF_SELF;
  158. ndm->ndm_type = NDA_DST;
  159. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  160. goto nla_put_failure;
  161. if (send_ip && nla_put_be32(skb, NDA_DST, fdb->remote_ip))
  162. goto nla_put_failure;
  163. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  164. ci.ndm_confirmed = 0;
  165. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  166. ci.ndm_refcnt = 0;
  167. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  168. goto nla_put_failure;
  169. return nlmsg_end(skb, nlh);
  170. nla_put_failure:
  171. nlmsg_cancel(skb, nlh);
  172. return -EMSGSIZE;
  173. }
  174. static inline size_t vxlan_nlmsg_size(void)
  175. {
  176. return NLMSG_ALIGN(sizeof(struct ndmsg))
  177. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  178. + nla_total_size(sizeof(__be32)) /* NDA_DST */
  179. + nla_total_size(sizeof(struct nda_cacheinfo));
  180. }
  181. static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
  182. const struct vxlan_fdb *fdb, int type)
  183. {
  184. struct net *net = dev_net(vxlan->dev);
  185. struct sk_buff *skb;
  186. int err = -ENOBUFS;
  187. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  188. if (skb == NULL)
  189. goto errout;
  190. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0);
  191. if (err < 0) {
  192. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  193. WARN_ON(err == -EMSGSIZE);
  194. kfree_skb(skb);
  195. goto errout;
  196. }
  197. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  198. return;
  199. errout:
  200. if (err < 0)
  201. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  202. }
  203. static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
  204. {
  205. struct vxlan_dev *vxlan = netdev_priv(dev);
  206. struct vxlan_fdb f;
  207. memset(&f, 0, sizeof f);
  208. f.state = NUD_STALE;
  209. f.remote_ip = ipa; /* goes to NDA_DST */
  210. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  211. }
  212. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  213. {
  214. struct vxlan_fdb f;
  215. memset(&f, 0, sizeof f);
  216. f.state = NUD_STALE;
  217. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  218. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  219. }
  220. /* Hash Ethernet address */
  221. static u32 eth_hash(const unsigned char *addr)
  222. {
  223. u64 value = get_unaligned((u64 *)addr);
  224. /* only want 6 bytes */
  225. #ifdef __BIG_ENDIAN
  226. value >>= 16;
  227. #else
  228. value <<= 16;
  229. #endif
  230. return hash_64(value, FDB_HASH_BITS);
  231. }
  232. /* Hash chain to use given mac address */
  233. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  234. const u8 *mac)
  235. {
  236. return &vxlan->fdb_head[eth_hash(mac)];
  237. }
  238. /* Look up Ethernet address in forwarding table */
  239. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  240. const u8 *mac)
  241. {
  242. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  243. struct vxlan_fdb *f;
  244. struct hlist_node *node;
  245. hlist_for_each_entry_rcu(f, node, head, hlist) {
  246. if (compare_ether_addr(mac, f->eth_addr) == 0)
  247. return f;
  248. }
  249. return NULL;
  250. }
  251. /* Add new entry to forwarding table -- assumes lock held */
  252. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  253. const u8 *mac, __be32 ip,
  254. __u16 state, __u16 flags)
  255. {
  256. struct vxlan_fdb *f;
  257. int notify = 0;
  258. f = vxlan_find_mac(vxlan, mac);
  259. if (f) {
  260. if (flags & NLM_F_EXCL) {
  261. netdev_dbg(vxlan->dev,
  262. "lost race to create %pM\n", mac);
  263. return -EEXIST;
  264. }
  265. if (f->state != state) {
  266. f->state = state;
  267. f->updated = jiffies;
  268. notify = 1;
  269. }
  270. } else {
  271. if (!(flags & NLM_F_CREATE))
  272. return -ENOENT;
  273. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  274. return -ENOSPC;
  275. netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
  276. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  277. if (!f)
  278. return -ENOMEM;
  279. notify = 1;
  280. f->remote_ip = ip;
  281. f->state = state;
  282. f->updated = f->used = jiffies;
  283. memcpy(f->eth_addr, mac, ETH_ALEN);
  284. ++vxlan->addrcnt;
  285. hlist_add_head_rcu(&f->hlist,
  286. vxlan_fdb_head(vxlan, mac));
  287. }
  288. if (notify)
  289. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  290. return 0;
  291. }
  292. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  293. {
  294. netdev_dbg(vxlan->dev,
  295. "delete %pM\n", f->eth_addr);
  296. --vxlan->addrcnt;
  297. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  298. hlist_del_rcu(&f->hlist);
  299. kfree_rcu(f, rcu);
  300. }
  301. /* Add static entry (via netlink) */
  302. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  303. struct net_device *dev,
  304. const unsigned char *addr, u16 flags)
  305. {
  306. struct vxlan_dev *vxlan = netdev_priv(dev);
  307. __be32 ip;
  308. int err;
  309. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  310. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  311. ndm->ndm_state);
  312. return -EINVAL;
  313. }
  314. if (tb[NDA_DST] == NULL)
  315. return -EINVAL;
  316. if (nla_len(tb[NDA_DST]) != sizeof(__be32))
  317. return -EAFNOSUPPORT;
  318. ip = nla_get_be32(tb[NDA_DST]);
  319. spin_lock_bh(&vxlan->hash_lock);
  320. err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags);
  321. spin_unlock_bh(&vxlan->hash_lock);
  322. return err;
  323. }
  324. /* Delete entry (via netlink) */
  325. static int vxlan_fdb_delete(struct ndmsg *ndm, struct net_device *dev,
  326. const unsigned char *addr)
  327. {
  328. struct vxlan_dev *vxlan = netdev_priv(dev);
  329. struct vxlan_fdb *f;
  330. int err = -ENOENT;
  331. spin_lock_bh(&vxlan->hash_lock);
  332. f = vxlan_find_mac(vxlan, addr);
  333. if (f) {
  334. vxlan_fdb_destroy(vxlan, f);
  335. err = 0;
  336. }
  337. spin_unlock_bh(&vxlan->hash_lock);
  338. return err;
  339. }
  340. /* Dump forwarding table */
  341. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  342. struct net_device *dev, int idx)
  343. {
  344. struct vxlan_dev *vxlan = netdev_priv(dev);
  345. unsigned int h;
  346. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  347. struct vxlan_fdb *f;
  348. struct hlist_node *n;
  349. int err;
  350. hlist_for_each_entry_rcu(f, n, &vxlan->fdb_head[h], hlist) {
  351. if (idx < cb->args[0])
  352. goto skip;
  353. err = vxlan_fdb_info(skb, vxlan, f,
  354. NETLINK_CB(cb->skb).portid,
  355. cb->nlh->nlmsg_seq,
  356. RTM_NEWNEIGH,
  357. NLM_F_MULTI);
  358. if (err < 0)
  359. break;
  360. skip:
  361. ++idx;
  362. }
  363. }
  364. return idx;
  365. }
  366. /* Watch incoming packets to learn mapping between Ethernet address
  367. * and Tunnel endpoint.
  368. */
  369. static void vxlan_snoop(struct net_device *dev,
  370. __be32 src_ip, const u8 *src_mac)
  371. {
  372. struct vxlan_dev *vxlan = netdev_priv(dev);
  373. struct vxlan_fdb *f;
  374. int err;
  375. f = vxlan_find_mac(vxlan, src_mac);
  376. if (likely(f)) {
  377. f->used = jiffies;
  378. if (likely(f->remote_ip == src_ip))
  379. return;
  380. if (net_ratelimit())
  381. netdev_info(dev,
  382. "%pM migrated from %pI4 to %pI4\n",
  383. src_mac, &f->remote_ip, &src_ip);
  384. f->remote_ip = src_ip;
  385. f->updated = jiffies;
  386. } else {
  387. /* learned new entry */
  388. spin_lock(&vxlan->hash_lock);
  389. err = vxlan_fdb_create(vxlan, src_mac, src_ip,
  390. NUD_REACHABLE,
  391. NLM_F_EXCL|NLM_F_CREATE);
  392. spin_unlock(&vxlan->hash_lock);
  393. }
  394. }
  395. /* See if multicast group is already in use by other ID */
  396. static bool vxlan_group_used(struct vxlan_net *vn,
  397. const struct vxlan_dev *this)
  398. {
  399. const struct vxlan_dev *vxlan;
  400. struct hlist_node *node;
  401. unsigned h;
  402. for (h = 0; h < VNI_HASH_SIZE; ++h)
  403. hlist_for_each_entry(vxlan, node, &vn->vni_list[h], hlist) {
  404. if (vxlan == this)
  405. continue;
  406. if (!netif_running(vxlan->dev))
  407. continue;
  408. if (vxlan->gaddr == this->gaddr)
  409. return true;
  410. }
  411. return false;
  412. }
  413. /* kernel equivalent to IP_ADD_MEMBERSHIP */
  414. static int vxlan_join_group(struct net_device *dev)
  415. {
  416. struct vxlan_dev *vxlan = netdev_priv(dev);
  417. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  418. struct sock *sk = vn->sock->sk;
  419. struct ip_mreqn mreq = {
  420. .imr_multiaddr.s_addr = vxlan->gaddr,
  421. .imr_ifindex = vxlan->link,
  422. };
  423. int err;
  424. /* Already a member of group */
  425. if (vxlan_group_used(vn, vxlan))
  426. return 0;
  427. /* Need to drop RTNL to call multicast join */
  428. rtnl_unlock();
  429. lock_sock(sk);
  430. err = ip_mc_join_group(sk, &mreq);
  431. release_sock(sk);
  432. rtnl_lock();
  433. return err;
  434. }
  435. /* kernel equivalent to IP_DROP_MEMBERSHIP */
  436. static int vxlan_leave_group(struct net_device *dev)
  437. {
  438. struct vxlan_dev *vxlan = netdev_priv(dev);
  439. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  440. int err = 0;
  441. struct sock *sk = vn->sock->sk;
  442. struct ip_mreqn mreq = {
  443. .imr_multiaddr.s_addr = vxlan->gaddr,
  444. .imr_ifindex = vxlan->link,
  445. };
  446. /* Only leave group when last vxlan is done. */
  447. if (vxlan_group_used(vn, vxlan))
  448. return 0;
  449. /* Need to drop RTNL to call multicast leave */
  450. rtnl_unlock();
  451. lock_sock(sk);
  452. err = ip_mc_leave_group(sk, &mreq);
  453. release_sock(sk);
  454. rtnl_lock();
  455. return err;
  456. }
  457. /* Callback from net/ipv4/udp.c to receive packets */
  458. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  459. {
  460. struct iphdr *oip;
  461. struct vxlanhdr *vxh;
  462. struct vxlan_dev *vxlan;
  463. struct vxlan_stats *stats;
  464. __u32 vni;
  465. int err;
  466. /* pop off outer UDP header */
  467. __skb_pull(skb, sizeof(struct udphdr));
  468. /* Need Vxlan and inner Ethernet header to be present */
  469. if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
  470. goto error;
  471. /* Drop packets with reserved bits set */
  472. vxh = (struct vxlanhdr *) skb->data;
  473. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  474. (vxh->vx_vni & htonl(0xff))) {
  475. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  476. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  477. goto error;
  478. }
  479. __skb_pull(skb, sizeof(struct vxlanhdr));
  480. /* Is this VNI defined? */
  481. vni = ntohl(vxh->vx_vni) >> 8;
  482. vxlan = vxlan_find_vni(sock_net(sk), vni);
  483. if (!vxlan) {
  484. netdev_dbg(skb->dev, "unknown vni %d\n", vni);
  485. goto drop;
  486. }
  487. if (!pskb_may_pull(skb, ETH_HLEN)) {
  488. vxlan->dev->stats.rx_length_errors++;
  489. vxlan->dev->stats.rx_errors++;
  490. goto drop;
  491. }
  492. skb_reset_mac_header(skb);
  493. /* Re-examine inner Ethernet packet */
  494. oip = ip_hdr(skb);
  495. skb->protocol = eth_type_trans(skb, vxlan->dev);
  496. /* Ignore packet loops (and multicast echo) */
  497. if (compare_ether_addr(eth_hdr(skb)->h_source,
  498. vxlan->dev->dev_addr) == 0)
  499. goto drop;
  500. if (vxlan->flags & VXLAN_F_LEARN)
  501. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source);
  502. __skb_tunnel_rx(skb, vxlan->dev);
  503. skb_reset_network_header(skb);
  504. /* If the NIC driver gave us an encapsulated packet with
  505. * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
  506. * leave the CHECKSUM_UNNECESSARY, the device checksummed it
  507. * for us. Otherwise force the upper layers to verify it.
  508. */
  509. if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
  510. !(vxlan->dev->features & NETIF_F_RXCSUM))
  511. skb->ip_summed = CHECKSUM_NONE;
  512. skb->encapsulation = 0;
  513. err = IP_ECN_decapsulate(oip, skb);
  514. if (unlikely(err)) {
  515. if (log_ecn_error)
  516. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  517. &oip->saddr, oip->tos);
  518. if (err > 1) {
  519. ++vxlan->dev->stats.rx_frame_errors;
  520. ++vxlan->dev->stats.rx_errors;
  521. goto drop;
  522. }
  523. }
  524. stats = this_cpu_ptr(vxlan->stats);
  525. u64_stats_update_begin(&stats->syncp);
  526. stats->rx_packets++;
  527. stats->rx_bytes += skb->len;
  528. u64_stats_update_end(&stats->syncp);
  529. netif_rx(skb);
  530. return 0;
  531. error:
  532. /* Put UDP header back */
  533. __skb_push(skb, sizeof(struct udphdr));
  534. return 1;
  535. drop:
  536. /* Consume bad packet */
  537. kfree_skb(skb);
  538. return 0;
  539. }
  540. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  541. {
  542. struct vxlan_dev *vxlan = netdev_priv(dev);
  543. struct arphdr *parp;
  544. u8 *arpptr, *sha;
  545. __be32 sip, tip;
  546. struct neighbour *n;
  547. if (dev->flags & IFF_NOARP)
  548. goto out;
  549. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  550. dev->stats.tx_dropped++;
  551. goto out;
  552. }
  553. parp = arp_hdr(skb);
  554. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  555. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  556. parp->ar_pro != htons(ETH_P_IP) ||
  557. parp->ar_op != htons(ARPOP_REQUEST) ||
  558. parp->ar_hln != dev->addr_len ||
  559. parp->ar_pln != 4)
  560. goto out;
  561. arpptr = (u8 *)parp + sizeof(struct arphdr);
  562. sha = arpptr;
  563. arpptr += dev->addr_len; /* sha */
  564. memcpy(&sip, arpptr, sizeof(sip));
  565. arpptr += sizeof(sip);
  566. arpptr += dev->addr_len; /* tha */
  567. memcpy(&tip, arpptr, sizeof(tip));
  568. if (ipv4_is_loopback(tip) ||
  569. ipv4_is_multicast(tip))
  570. goto out;
  571. n = neigh_lookup(&arp_tbl, &tip, dev);
  572. if (n) {
  573. struct vxlan_dev *vxlan = netdev_priv(dev);
  574. struct vxlan_fdb *f;
  575. struct sk_buff *reply;
  576. if (!(n->nud_state & NUD_CONNECTED)) {
  577. neigh_release(n);
  578. goto out;
  579. }
  580. f = vxlan_find_mac(vxlan, n->ha);
  581. if (f && f->remote_ip == 0) {
  582. /* bridge-local neighbor */
  583. neigh_release(n);
  584. goto out;
  585. }
  586. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  587. n->ha, sha);
  588. neigh_release(n);
  589. skb_reset_mac_header(reply);
  590. __skb_pull(reply, skb_network_offset(reply));
  591. reply->ip_summed = CHECKSUM_UNNECESSARY;
  592. reply->pkt_type = PACKET_HOST;
  593. if (netif_rx_ni(reply) == NET_RX_DROP)
  594. dev->stats.rx_dropped++;
  595. } else if (vxlan->flags & VXLAN_F_L3MISS)
  596. vxlan_ip_miss(dev, tip);
  597. out:
  598. consume_skb(skb);
  599. return NETDEV_TX_OK;
  600. }
  601. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  602. {
  603. struct vxlan_dev *vxlan = netdev_priv(dev);
  604. struct neighbour *n;
  605. struct iphdr *pip;
  606. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  607. return false;
  608. n = NULL;
  609. switch (ntohs(eth_hdr(skb)->h_proto)) {
  610. case ETH_P_IP:
  611. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  612. return false;
  613. pip = ip_hdr(skb);
  614. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  615. break;
  616. default:
  617. return false;
  618. }
  619. if (n) {
  620. bool diff;
  621. diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
  622. if (diff) {
  623. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  624. dev->addr_len);
  625. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  626. }
  627. neigh_release(n);
  628. return diff;
  629. } else if (vxlan->flags & VXLAN_F_L3MISS)
  630. vxlan_ip_miss(dev, pip->daddr);
  631. return false;
  632. }
  633. /* Extract dsfield from inner protocol */
  634. static inline u8 vxlan_get_dsfield(const struct iphdr *iph,
  635. const struct sk_buff *skb)
  636. {
  637. if (skb->protocol == htons(ETH_P_IP))
  638. return iph->tos;
  639. else if (skb->protocol == htons(ETH_P_IPV6))
  640. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  641. else
  642. return 0;
  643. }
  644. /* Propogate ECN bits out */
  645. static inline u8 vxlan_ecn_encap(u8 tos,
  646. const struct iphdr *iph,
  647. const struct sk_buff *skb)
  648. {
  649. u8 inner = vxlan_get_dsfield(iph, skb);
  650. return INET_ECN_encapsulate(tos, inner);
  651. }
  652. static void vxlan_sock_free(struct sk_buff *skb)
  653. {
  654. sock_put(skb->sk);
  655. }
  656. /* On transmit, associate with the tunnel socket */
  657. static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
  658. {
  659. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  660. struct sock *sk = vn->sock->sk;
  661. skb_orphan(skb);
  662. sock_hold(sk);
  663. skb->sk = sk;
  664. skb->destructor = vxlan_sock_free;
  665. }
  666. /* Compute source port for outgoing packet
  667. * first choice to use L4 flow hash since it will spread
  668. * better and maybe available from hardware
  669. * secondary choice is to use jhash on the Ethernet header
  670. */
  671. static u16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
  672. {
  673. unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
  674. u32 hash;
  675. hash = skb_get_rxhash(skb);
  676. if (!hash)
  677. hash = jhash(skb->data, 2 * ETH_ALEN,
  678. (__force u32) skb->protocol);
  679. return (((u64) hash * range) >> 32) + vxlan->port_min;
  680. }
  681. /* Transmit local packets over Vxlan
  682. *
  683. * Outer IP header inherits ECN and DF from inner header.
  684. * Outer UDP destination is the VXLAN assigned port.
  685. * source port is based on hash of flow
  686. */
  687. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  688. {
  689. struct vxlan_dev *vxlan = netdev_priv(dev);
  690. struct rtable *rt;
  691. const struct iphdr *old_iph;
  692. struct ethhdr *eth;
  693. struct iphdr *iph;
  694. struct vxlanhdr *vxh;
  695. struct udphdr *uh;
  696. struct flowi4 fl4;
  697. unsigned int pkt_len = skb->len;
  698. __be32 dst;
  699. __u16 src_port;
  700. __be16 df = 0;
  701. __u8 tos, ttl;
  702. int err;
  703. bool did_rsc = false;
  704. const struct vxlan_fdb *f;
  705. skb_reset_mac_header(skb);
  706. eth = eth_hdr(skb);
  707. if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
  708. return arp_reduce(dev, skb);
  709. else if ((vxlan->flags&VXLAN_F_RSC) && ntohs(eth->h_proto) == ETH_P_IP)
  710. did_rsc = route_shortcircuit(dev, skb);
  711. f = vxlan_find_mac(vxlan, eth->h_dest);
  712. if (f == NULL) {
  713. did_rsc = false;
  714. dst = vxlan->gaddr;
  715. if (!dst && (vxlan->flags & VXLAN_F_L2MISS) &&
  716. !is_multicast_ether_addr(eth->h_dest))
  717. vxlan_fdb_miss(vxlan, eth->h_dest);
  718. } else
  719. dst = f->remote_ip;
  720. if (!dst) {
  721. if (did_rsc) {
  722. __skb_pull(skb, skb_network_offset(skb));
  723. skb->ip_summed = CHECKSUM_NONE;
  724. skb->pkt_type = PACKET_HOST;
  725. /* short-circuited back to local bridge */
  726. if (netif_rx(skb) == NET_RX_SUCCESS) {
  727. struct vxlan_stats *stats =
  728. this_cpu_ptr(vxlan->stats);
  729. u64_stats_update_begin(&stats->syncp);
  730. stats->tx_packets++;
  731. stats->tx_bytes += pkt_len;
  732. u64_stats_update_end(&stats->syncp);
  733. } else {
  734. dev->stats.tx_errors++;
  735. dev->stats.tx_aborted_errors++;
  736. }
  737. return NETDEV_TX_OK;
  738. }
  739. goto drop;
  740. }
  741. if (!skb->encapsulation) {
  742. skb_reset_inner_headers(skb);
  743. skb->encapsulation = 1;
  744. }
  745. /* Need space for new headers (invalidates iph ptr) */
  746. if (skb_cow_head(skb, VXLAN_HEADROOM))
  747. goto drop;
  748. old_iph = ip_hdr(skb);
  749. ttl = vxlan->ttl;
  750. if (!ttl && IN_MULTICAST(ntohl(dst)))
  751. ttl = 1;
  752. tos = vxlan->tos;
  753. if (tos == 1)
  754. tos = vxlan_get_dsfield(old_iph, skb);
  755. src_port = vxlan_src_port(vxlan, skb);
  756. memset(&fl4, 0, sizeof(fl4));
  757. fl4.flowi4_oif = vxlan->link;
  758. fl4.flowi4_tos = RT_TOS(tos);
  759. fl4.daddr = dst;
  760. fl4.saddr = vxlan->saddr;
  761. rt = ip_route_output_key(dev_net(dev), &fl4);
  762. if (IS_ERR(rt)) {
  763. netdev_dbg(dev, "no route to %pI4\n", &dst);
  764. dev->stats.tx_carrier_errors++;
  765. goto tx_error;
  766. }
  767. if (rt->dst.dev == dev) {
  768. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  769. ip_rt_put(rt);
  770. dev->stats.collisions++;
  771. goto tx_error;
  772. }
  773. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  774. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  775. IPSKB_REROUTED);
  776. skb_dst_drop(skb);
  777. skb_dst_set(skb, &rt->dst);
  778. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  779. vxh->vx_flags = htonl(VXLAN_FLAGS);
  780. vxh->vx_vni = htonl(vxlan->vni << 8);
  781. __skb_push(skb, sizeof(*uh));
  782. skb_reset_transport_header(skb);
  783. uh = udp_hdr(skb);
  784. uh->dest = htons(vxlan_port);
  785. uh->source = htons(src_port);
  786. uh->len = htons(skb->len);
  787. uh->check = 0;
  788. __skb_push(skb, sizeof(*iph));
  789. skb_reset_network_header(skb);
  790. iph = ip_hdr(skb);
  791. iph->version = 4;
  792. iph->ihl = sizeof(struct iphdr) >> 2;
  793. iph->frag_off = df;
  794. iph->protocol = IPPROTO_UDP;
  795. iph->tos = vxlan_ecn_encap(tos, old_iph, skb);
  796. iph->daddr = dst;
  797. iph->saddr = fl4.saddr;
  798. iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  799. vxlan_set_owner(dev, skb);
  800. /* See iptunnel_xmit() */
  801. if (skb->ip_summed != CHECKSUM_PARTIAL)
  802. skb->ip_summed = CHECKSUM_NONE;
  803. ip_select_ident(iph, &rt->dst, NULL);
  804. err = ip_local_out(skb);
  805. if (likely(net_xmit_eval(err) == 0)) {
  806. struct vxlan_stats *stats = this_cpu_ptr(vxlan->stats);
  807. u64_stats_update_begin(&stats->syncp);
  808. stats->tx_packets++;
  809. stats->tx_bytes += pkt_len;
  810. u64_stats_update_end(&stats->syncp);
  811. } else {
  812. dev->stats.tx_errors++;
  813. dev->stats.tx_aborted_errors++;
  814. }
  815. return NETDEV_TX_OK;
  816. drop:
  817. dev->stats.tx_dropped++;
  818. goto tx_free;
  819. tx_error:
  820. dev->stats.tx_errors++;
  821. tx_free:
  822. dev_kfree_skb(skb);
  823. return NETDEV_TX_OK;
  824. }
  825. /* Walk the forwarding table and purge stale entries */
  826. static void vxlan_cleanup(unsigned long arg)
  827. {
  828. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  829. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  830. unsigned int h;
  831. if (!netif_running(vxlan->dev))
  832. return;
  833. spin_lock_bh(&vxlan->hash_lock);
  834. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  835. struct hlist_node *p, *n;
  836. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  837. struct vxlan_fdb *f
  838. = container_of(p, struct vxlan_fdb, hlist);
  839. unsigned long timeout;
  840. if (f->state & NUD_PERMANENT)
  841. continue;
  842. timeout = f->used + vxlan->age_interval * HZ;
  843. if (time_before_eq(timeout, jiffies)) {
  844. netdev_dbg(vxlan->dev,
  845. "garbage collect %pM\n",
  846. f->eth_addr);
  847. f->state = NUD_STALE;
  848. vxlan_fdb_destroy(vxlan, f);
  849. } else if (time_before(timeout, next_timer))
  850. next_timer = timeout;
  851. }
  852. }
  853. spin_unlock_bh(&vxlan->hash_lock);
  854. mod_timer(&vxlan->age_timer, next_timer);
  855. }
  856. /* Setup stats when device is created */
  857. static int vxlan_init(struct net_device *dev)
  858. {
  859. struct vxlan_dev *vxlan = netdev_priv(dev);
  860. vxlan->stats = alloc_percpu(struct vxlan_stats);
  861. if (!vxlan->stats)
  862. return -ENOMEM;
  863. return 0;
  864. }
  865. /* Start ageing timer and join group when device is brought up */
  866. static int vxlan_open(struct net_device *dev)
  867. {
  868. struct vxlan_dev *vxlan = netdev_priv(dev);
  869. int err;
  870. if (vxlan->gaddr) {
  871. err = vxlan_join_group(dev);
  872. if (err)
  873. return err;
  874. }
  875. if (vxlan->age_interval)
  876. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  877. return 0;
  878. }
  879. /* Purge the forwarding table */
  880. static void vxlan_flush(struct vxlan_dev *vxlan)
  881. {
  882. unsigned h;
  883. spin_lock_bh(&vxlan->hash_lock);
  884. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  885. struct hlist_node *p, *n;
  886. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  887. struct vxlan_fdb *f
  888. = container_of(p, struct vxlan_fdb, hlist);
  889. vxlan_fdb_destroy(vxlan, f);
  890. }
  891. }
  892. spin_unlock_bh(&vxlan->hash_lock);
  893. }
  894. /* Cleanup timer and forwarding table on shutdown */
  895. static int vxlan_stop(struct net_device *dev)
  896. {
  897. struct vxlan_dev *vxlan = netdev_priv(dev);
  898. if (vxlan->gaddr)
  899. vxlan_leave_group(dev);
  900. del_timer_sync(&vxlan->age_timer);
  901. vxlan_flush(vxlan);
  902. return 0;
  903. }
  904. /* Merge per-cpu statistics */
  905. static struct rtnl_link_stats64 *vxlan_stats64(struct net_device *dev,
  906. struct rtnl_link_stats64 *stats)
  907. {
  908. struct vxlan_dev *vxlan = netdev_priv(dev);
  909. struct vxlan_stats tmp, sum = { 0 };
  910. unsigned int cpu;
  911. for_each_possible_cpu(cpu) {
  912. unsigned int start;
  913. const struct vxlan_stats *stats
  914. = per_cpu_ptr(vxlan->stats, cpu);
  915. do {
  916. start = u64_stats_fetch_begin_bh(&stats->syncp);
  917. memcpy(&tmp, stats, sizeof(tmp));
  918. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  919. sum.tx_bytes += tmp.tx_bytes;
  920. sum.tx_packets += tmp.tx_packets;
  921. sum.rx_bytes += tmp.rx_bytes;
  922. sum.rx_packets += tmp.rx_packets;
  923. }
  924. stats->tx_bytes = sum.tx_bytes;
  925. stats->tx_packets = sum.tx_packets;
  926. stats->rx_bytes = sum.rx_bytes;
  927. stats->rx_packets = sum.rx_packets;
  928. stats->multicast = dev->stats.multicast;
  929. stats->rx_length_errors = dev->stats.rx_length_errors;
  930. stats->rx_frame_errors = dev->stats.rx_frame_errors;
  931. stats->rx_errors = dev->stats.rx_errors;
  932. stats->tx_dropped = dev->stats.tx_dropped;
  933. stats->tx_carrier_errors = dev->stats.tx_carrier_errors;
  934. stats->tx_aborted_errors = dev->stats.tx_aborted_errors;
  935. stats->collisions = dev->stats.collisions;
  936. stats->tx_errors = dev->stats.tx_errors;
  937. return stats;
  938. }
  939. /* Stub, nothing needs to be done. */
  940. static void vxlan_set_multicast_list(struct net_device *dev)
  941. {
  942. }
  943. static const struct net_device_ops vxlan_netdev_ops = {
  944. .ndo_init = vxlan_init,
  945. .ndo_open = vxlan_open,
  946. .ndo_stop = vxlan_stop,
  947. .ndo_start_xmit = vxlan_xmit,
  948. .ndo_get_stats64 = vxlan_stats64,
  949. .ndo_set_rx_mode = vxlan_set_multicast_list,
  950. .ndo_change_mtu = eth_change_mtu,
  951. .ndo_validate_addr = eth_validate_addr,
  952. .ndo_set_mac_address = eth_mac_addr,
  953. .ndo_fdb_add = vxlan_fdb_add,
  954. .ndo_fdb_del = vxlan_fdb_delete,
  955. .ndo_fdb_dump = vxlan_fdb_dump,
  956. };
  957. /* Info for udev, that this is a virtual tunnel endpoint */
  958. static struct device_type vxlan_type = {
  959. .name = "vxlan",
  960. };
  961. static void vxlan_free(struct net_device *dev)
  962. {
  963. struct vxlan_dev *vxlan = netdev_priv(dev);
  964. free_percpu(vxlan->stats);
  965. free_netdev(dev);
  966. }
  967. /* Initialize the device structure. */
  968. static void vxlan_setup(struct net_device *dev)
  969. {
  970. struct vxlan_dev *vxlan = netdev_priv(dev);
  971. unsigned h;
  972. int low, high;
  973. eth_hw_addr_random(dev);
  974. ether_setup(dev);
  975. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  976. dev->netdev_ops = &vxlan_netdev_ops;
  977. dev->destructor = vxlan_free;
  978. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  979. dev->tx_queue_len = 0;
  980. dev->features |= NETIF_F_LLTX;
  981. dev->features |= NETIF_F_NETNS_LOCAL;
  982. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  983. dev->features |= NETIF_F_RXCSUM;
  984. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  985. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  986. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  987. spin_lock_init(&vxlan->hash_lock);
  988. init_timer_deferrable(&vxlan->age_timer);
  989. vxlan->age_timer.function = vxlan_cleanup;
  990. vxlan->age_timer.data = (unsigned long) vxlan;
  991. inet_get_local_port_range(&low, &high);
  992. vxlan->port_min = low;
  993. vxlan->port_max = high;
  994. vxlan->dev = dev;
  995. for (h = 0; h < FDB_HASH_SIZE; ++h)
  996. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  997. }
  998. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  999. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1000. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1001. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1002. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1003. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1004. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1005. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1006. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1007. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1008. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1009. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1010. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1011. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1012. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1013. };
  1014. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1015. {
  1016. if (tb[IFLA_ADDRESS]) {
  1017. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1018. pr_debug("invalid link address (not ethernet)\n");
  1019. return -EINVAL;
  1020. }
  1021. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1022. pr_debug("invalid all zero ethernet address\n");
  1023. return -EADDRNOTAVAIL;
  1024. }
  1025. }
  1026. if (!data)
  1027. return -EINVAL;
  1028. if (data[IFLA_VXLAN_ID]) {
  1029. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1030. if (id >= VXLAN_VID_MASK)
  1031. return -ERANGE;
  1032. }
  1033. if (data[IFLA_VXLAN_GROUP]) {
  1034. __be32 gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1035. if (!IN_MULTICAST(ntohl(gaddr))) {
  1036. pr_debug("group address is not IPv4 multicast\n");
  1037. return -EADDRNOTAVAIL;
  1038. }
  1039. }
  1040. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1041. const struct ifla_vxlan_port_range *p
  1042. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1043. if (ntohs(p->high) < ntohs(p->low)) {
  1044. pr_debug("port range %u .. %u not valid\n",
  1045. ntohs(p->low), ntohs(p->high));
  1046. return -EINVAL;
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1052. struct nlattr *tb[], struct nlattr *data[])
  1053. {
  1054. struct vxlan_dev *vxlan = netdev_priv(dev);
  1055. __u32 vni;
  1056. int err;
  1057. if (!data[IFLA_VXLAN_ID])
  1058. return -EINVAL;
  1059. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1060. if (vxlan_find_vni(net, vni)) {
  1061. pr_info("duplicate VNI %u\n", vni);
  1062. return -EEXIST;
  1063. }
  1064. vxlan->vni = vni;
  1065. if (data[IFLA_VXLAN_GROUP])
  1066. vxlan->gaddr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1067. if (data[IFLA_VXLAN_LOCAL])
  1068. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  1069. if (data[IFLA_VXLAN_LINK] &&
  1070. (vxlan->link = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  1071. struct net_device *lowerdev
  1072. = __dev_get_by_index(net, vxlan->link);
  1073. if (!lowerdev) {
  1074. pr_info("ifindex %d does not exist\n", vxlan->link);
  1075. return -ENODEV;
  1076. }
  1077. if (!tb[IFLA_MTU])
  1078. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1079. /* update header length based on lower device */
  1080. dev->hard_header_len = lowerdev->hard_header_len +
  1081. VXLAN_HEADROOM;
  1082. }
  1083. if (data[IFLA_VXLAN_TOS])
  1084. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  1085. if (data[IFLA_VXLAN_TTL])
  1086. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  1087. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  1088. vxlan->flags |= VXLAN_F_LEARN;
  1089. if (data[IFLA_VXLAN_AGEING])
  1090. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  1091. else
  1092. vxlan->age_interval = FDB_AGE_DEFAULT;
  1093. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  1094. vxlan->flags |= VXLAN_F_PROXY;
  1095. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  1096. vxlan->flags |= VXLAN_F_RSC;
  1097. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  1098. vxlan->flags |= VXLAN_F_L2MISS;
  1099. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  1100. vxlan->flags |= VXLAN_F_L3MISS;
  1101. if (data[IFLA_VXLAN_LIMIT])
  1102. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  1103. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1104. const struct ifla_vxlan_port_range *p
  1105. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1106. vxlan->port_min = ntohs(p->low);
  1107. vxlan->port_max = ntohs(p->high);
  1108. }
  1109. err = register_netdevice(dev);
  1110. if (!err)
  1111. hlist_add_head_rcu(&vxlan->hlist, vni_head(net, vxlan->vni));
  1112. return err;
  1113. }
  1114. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  1115. {
  1116. struct vxlan_dev *vxlan = netdev_priv(dev);
  1117. hlist_del_rcu(&vxlan->hlist);
  1118. unregister_netdevice_queue(dev, head);
  1119. }
  1120. static size_t vxlan_get_size(const struct net_device *dev)
  1121. {
  1122. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  1123. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  1124. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  1125. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  1126. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  1127. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  1128. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  1129. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  1130. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  1131. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  1132. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  1133. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  1134. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  1135. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  1136. 0;
  1137. }
  1138. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1139. {
  1140. const struct vxlan_dev *vxlan = netdev_priv(dev);
  1141. struct ifla_vxlan_port_range ports = {
  1142. .low = htons(vxlan->port_min),
  1143. .high = htons(vxlan->port_max),
  1144. };
  1145. if (nla_put_u32(skb, IFLA_VXLAN_ID, vxlan->vni))
  1146. goto nla_put_failure;
  1147. if (vxlan->gaddr && nla_put_be32(skb, IFLA_VXLAN_GROUP, vxlan->gaddr))
  1148. goto nla_put_failure;
  1149. if (vxlan->link && nla_put_u32(skb, IFLA_VXLAN_LINK, vxlan->link))
  1150. goto nla_put_failure;
  1151. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  1152. goto nla_put_failure;
  1153. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  1154. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  1155. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  1156. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  1157. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  1158. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  1159. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  1160. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  1161. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  1162. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  1163. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  1164. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  1165. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax))
  1166. goto nla_put_failure;
  1167. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  1168. goto nla_put_failure;
  1169. return 0;
  1170. nla_put_failure:
  1171. return -EMSGSIZE;
  1172. }
  1173. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  1174. .kind = "vxlan",
  1175. .maxtype = IFLA_VXLAN_MAX,
  1176. .policy = vxlan_policy,
  1177. .priv_size = sizeof(struct vxlan_dev),
  1178. .setup = vxlan_setup,
  1179. .validate = vxlan_validate,
  1180. .newlink = vxlan_newlink,
  1181. .dellink = vxlan_dellink,
  1182. .get_size = vxlan_get_size,
  1183. .fill_info = vxlan_fill_info,
  1184. };
  1185. static __net_init int vxlan_init_net(struct net *net)
  1186. {
  1187. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1188. struct sock *sk;
  1189. struct sockaddr_in vxlan_addr = {
  1190. .sin_family = AF_INET,
  1191. .sin_addr.s_addr = htonl(INADDR_ANY),
  1192. };
  1193. int rc;
  1194. unsigned h;
  1195. /* Create UDP socket for encapsulation receive. */
  1196. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
  1197. if (rc < 0) {
  1198. pr_debug("UDP socket create failed\n");
  1199. return rc;
  1200. }
  1201. /* Put in proper namespace */
  1202. sk = vn->sock->sk;
  1203. sk_change_net(sk, net);
  1204. vxlan_addr.sin_port = htons(vxlan_port);
  1205. rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
  1206. sizeof(vxlan_addr));
  1207. if (rc < 0) {
  1208. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1209. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1210. sk_release_kernel(sk);
  1211. vn->sock = NULL;
  1212. return rc;
  1213. }
  1214. /* Disable multicast loopback */
  1215. inet_sk(sk)->mc_loop = 0;
  1216. /* Mark socket as an encapsulation socket. */
  1217. udp_sk(sk)->encap_type = 1;
  1218. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1219. udp_encap_enable();
  1220. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1221. INIT_HLIST_HEAD(&vn->vni_list[h]);
  1222. return 0;
  1223. }
  1224. static __net_exit void vxlan_exit_net(struct net *net)
  1225. {
  1226. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1227. if (vn->sock) {
  1228. sk_release_kernel(vn->sock->sk);
  1229. vn->sock = NULL;
  1230. }
  1231. }
  1232. static struct pernet_operations vxlan_net_ops = {
  1233. .init = vxlan_init_net,
  1234. .exit = vxlan_exit_net,
  1235. .id = &vxlan_net_id,
  1236. .size = sizeof(struct vxlan_net),
  1237. };
  1238. static int __init vxlan_init_module(void)
  1239. {
  1240. int rc;
  1241. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  1242. rc = register_pernet_device(&vxlan_net_ops);
  1243. if (rc)
  1244. goto out1;
  1245. rc = rtnl_link_register(&vxlan_link_ops);
  1246. if (rc)
  1247. goto out2;
  1248. return 0;
  1249. out2:
  1250. unregister_pernet_device(&vxlan_net_ops);
  1251. out1:
  1252. return rc;
  1253. }
  1254. module_init(vxlan_init_module);
  1255. static void __exit vxlan_cleanup_module(void)
  1256. {
  1257. rtnl_link_unregister(&vxlan_link_ops);
  1258. unregister_pernet_device(&vxlan_net_ops);
  1259. }
  1260. module_exit(vxlan_cleanup_module);
  1261. MODULE_LICENSE("GPL");
  1262. MODULE_VERSION(VXLAN_VERSION);
  1263. MODULE_AUTHOR("Stephen Hemminger <shemminger@vyatta.com>");
  1264. MODULE_ALIAS_RTNL_LINK("vxlan");