vxlan.c 41 KB

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