vxlan.c 38 KB

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