vxlan.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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. * Return true if packet is bogus and should be droppped.
  477. */
  478. static bool vxlan_snoop(struct net_device *dev,
  479. __be32 src_ip, const u8 *src_mac)
  480. {
  481. struct vxlan_dev *vxlan = netdev_priv(dev);
  482. struct vxlan_fdb *f;
  483. f = vxlan_find_mac(vxlan, src_mac);
  484. if (likely(f)) {
  485. if (likely(f->remote.remote_ip == src_ip))
  486. return false;
  487. /* Don't migrate static entries, drop packets */
  488. if (f->state & NUD_NOARP)
  489. return true;
  490. if (net_ratelimit())
  491. netdev_info(dev,
  492. "%pM migrated from %pI4 to %pI4\n",
  493. src_mac, &f->remote.remote_ip, &src_ip);
  494. f->remote.remote_ip = src_ip;
  495. f->updated = jiffies;
  496. } else {
  497. /* learned new entry */
  498. spin_lock(&vxlan->hash_lock);
  499. /* close off race between vxlan_flush and incoming packets */
  500. if (netif_running(dev))
  501. vxlan_fdb_create(vxlan, src_mac, src_ip,
  502. NUD_REACHABLE,
  503. NLM_F_EXCL|NLM_F_CREATE,
  504. vxlan->dst_port,
  505. vxlan->default_dst.remote_vni,
  506. 0, NTF_SELF);
  507. spin_unlock(&vxlan->hash_lock);
  508. }
  509. return false;
  510. }
  511. /* See if multicast group is already in use by other ID */
  512. static bool vxlan_group_used(struct vxlan_net *vn,
  513. const struct vxlan_dev *this)
  514. {
  515. const struct vxlan_dev *vxlan;
  516. unsigned h;
  517. for (h = 0; h < VNI_HASH_SIZE; ++h)
  518. hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist) {
  519. if (vxlan == this)
  520. continue;
  521. if (!netif_running(vxlan->dev))
  522. continue;
  523. if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
  524. return true;
  525. }
  526. return false;
  527. }
  528. /* kernel equivalent to IP_ADD_MEMBERSHIP */
  529. static int vxlan_join_group(struct net_device *dev)
  530. {
  531. struct vxlan_dev *vxlan = netdev_priv(dev);
  532. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  533. struct sock *sk = vn->sock->sk;
  534. struct ip_mreqn mreq = {
  535. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  536. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  537. };
  538. int err;
  539. /* Already a member of group */
  540. if (vxlan_group_used(vn, vxlan))
  541. return 0;
  542. /* Need to drop RTNL to call multicast join */
  543. rtnl_unlock();
  544. lock_sock(sk);
  545. err = ip_mc_join_group(sk, &mreq);
  546. release_sock(sk);
  547. rtnl_lock();
  548. return err;
  549. }
  550. /* kernel equivalent to IP_DROP_MEMBERSHIP */
  551. static int vxlan_leave_group(struct net_device *dev)
  552. {
  553. struct vxlan_dev *vxlan = netdev_priv(dev);
  554. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  555. int err = 0;
  556. struct sock *sk = vn->sock->sk;
  557. struct ip_mreqn mreq = {
  558. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  559. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  560. };
  561. /* Only leave group when last vxlan is done. */
  562. if (vxlan_group_used(vn, vxlan))
  563. return 0;
  564. /* Need to drop RTNL to call multicast leave */
  565. rtnl_unlock();
  566. lock_sock(sk);
  567. err = ip_mc_leave_group(sk, &mreq);
  568. release_sock(sk);
  569. rtnl_lock();
  570. return err;
  571. }
  572. /* Callback from net/ipv4/udp.c to receive packets */
  573. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  574. {
  575. struct iphdr *oip;
  576. struct vxlanhdr *vxh;
  577. struct vxlan_dev *vxlan;
  578. struct pcpu_tstats *stats;
  579. __u32 vni;
  580. int err;
  581. /* pop off outer UDP header */
  582. __skb_pull(skb, sizeof(struct udphdr));
  583. /* Need Vxlan and inner Ethernet header to be present */
  584. if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
  585. goto error;
  586. /* Drop packets with reserved bits set */
  587. vxh = (struct vxlanhdr *) skb->data;
  588. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  589. (vxh->vx_vni & htonl(0xff))) {
  590. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  591. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  592. goto error;
  593. }
  594. __skb_pull(skb, sizeof(struct vxlanhdr));
  595. /* Is this VNI defined? */
  596. vni = ntohl(vxh->vx_vni) >> 8;
  597. vxlan = vxlan_find_vni(sock_net(sk), vni);
  598. if (!vxlan) {
  599. netdev_dbg(skb->dev, "unknown vni %d\n", vni);
  600. goto drop;
  601. }
  602. if (!pskb_may_pull(skb, ETH_HLEN)) {
  603. vxlan->dev->stats.rx_length_errors++;
  604. vxlan->dev->stats.rx_errors++;
  605. goto drop;
  606. }
  607. skb_reset_mac_header(skb);
  608. /* Re-examine inner Ethernet packet */
  609. oip = ip_hdr(skb);
  610. skb->protocol = eth_type_trans(skb, vxlan->dev);
  611. /* Ignore packet loops (and multicast echo) */
  612. if (compare_ether_addr(eth_hdr(skb)->h_source,
  613. vxlan->dev->dev_addr) == 0)
  614. goto drop;
  615. if ((vxlan->flags & VXLAN_F_LEARN) &&
  616. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
  617. goto drop;
  618. __skb_tunnel_rx(skb, vxlan->dev);
  619. skb_reset_network_header(skb);
  620. /* If the NIC driver gave us an encapsulated packet with
  621. * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
  622. * leave the CHECKSUM_UNNECESSARY, the device checksummed it
  623. * for us. Otherwise force the upper layers to verify it.
  624. */
  625. if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
  626. !(vxlan->dev->features & NETIF_F_RXCSUM))
  627. skb->ip_summed = CHECKSUM_NONE;
  628. skb->encapsulation = 0;
  629. err = IP_ECN_decapsulate(oip, skb);
  630. if (unlikely(err)) {
  631. if (log_ecn_error)
  632. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  633. &oip->saddr, oip->tos);
  634. if (err > 1) {
  635. ++vxlan->dev->stats.rx_frame_errors;
  636. ++vxlan->dev->stats.rx_errors;
  637. goto drop;
  638. }
  639. }
  640. stats = this_cpu_ptr(vxlan->dev->tstats);
  641. u64_stats_update_begin(&stats->syncp);
  642. stats->rx_packets++;
  643. stats->rx_bytes += skb->len;
  644. u64_stats_update_end(&stats->syncp);
  645. netif_rx(skb);
  646. return 0;
  647. error:
  648. /* Put UDP header back */
  649. __skb_push(skb, sizeof(struct udphdr));
  650. return 1;
  651. drop:
  652. /* Consume bad packet */
  653. kfree_skb(skb);
  654. return 0;
  655. }
  656. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  657. {
  658. struct vxlan_dev *vxlan = netdev_priv(dev);
  659. struct arphdr *parp;
  660. u8 *arpptr, *sha;
  661. __be32 sip, tip;
  662. struct neighbour *n;
  663. if (dev->flags & IFF_NOARP)
  664. goto out;
  665. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  666. dev->stats.tx_dropped++;
  667. goto out;
  668. }
  669. parp = arp_hdr(skb);
  670. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  671. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  672. parp->ar_pro != htons(ETH_P_IP) ||
  673. parp->ar_op != htons(ARPOP_REQUEST) ||
  674. parp->ar_hln != dev->addr_len ||
  675. parp->ar_pln != 4)
  676. goto out;
  677. arpptr = (u8 *)parp + sizeof(struct arphdr);
  678. sha = arpptr;
  679. arpptr += dev->addr_len; /* sha */
  680. memcpy(&sip, arpptr, sizeof(sip));
  681. arpptr += sizeof(sip);
  682. arpptr += dev->addr_len; /* tha */
  683. memcpy(&tip, arpptr, sizeof(tip));
  684. if (ipv4_is_loopback(tip) ||
  685. ipv4_is_multicast(tip))
  686. goto out;
  687. n = neigh_lookup(&arp_tbl, &tip, dev);
  688. if (n) {
  689. struct vxlan_fdb *f;
  690. struct sk_buff *reply;
  691. if (!(n->nud_state & NUD_CONNECTED)) {
  692. neigh_release(n);
  693. goto out;
  694. }
  695. f = vxlan_find_mac(vxlan, n->ha);
  696. if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
  697. /* bridge-local neighbor */
  698. neigh_release(n);
  699. goto out;
  700. }
  701. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  702. n->ha, sha);
  703. neigh_release(n);
  704. skb_reset_mac_header(reply);
  705. __skb_pull(reply, skb_network_offset(reply));
  706. reply->ip_summed = CHECKSUM_UNNECESSARY;
  707. reply->pkt_type = PACKET_HOST;
  708. if (netif_rx_ni(reply) == NET_RX_DROP)
  709. dev->stats.rx_dropped++;
  710. } else if (vxlan->flags & VXLAN_F_L3MISS)
  711. vxlan_ip_miss(dev, tip);
  712. out:
  713. consume_skb(skb);
  714. return NETDEV_TX_OK;
  715. }
  716. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  717. {
  718. struct vxlan_dev *vxlan = netdev_priv(dev);
  719. struct neighbour *n;
  720. struct iphdr *pip;
  721. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  722. return false;
  723. n = NULL;
  724. switch (ntohs(eth_hdr(skb)->h_proto)) {
  725. case ETH_P_IP:
  726. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  727. return false;
  728. pip = ip_hdr(skb);
  729. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  730. break;
  731. default:
  732. return false;
  733. }
  734. if (n) {
  735. bool diff;
  736. diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
  737. if (diff) {
  738. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  739. dev->addr_len);
  740. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  741. }
  742. neigh_release(n);
  743. return diff;
  744. } else if (vxlan->flags & VXLAN_F_L3MISS)
  745. vxlan_ip_miss(dev, pip->daddr);
  746. return false;
  747. }
  748. static void vxlan_sock_free(struct sk_buff *skb)
  749. {
  750. sock_put(skb->sk);
  751. }
  752. /* On transmit, associate with the tunnel socket */
  753. static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
  754. {
  755. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  756. struct sock *sk = vn->sock->sk;
  757. skb_orphan(skb);
  758. sock_hold(sk);
  759. skb->sk = sk;
  760. skb->destructor = vxlan_sock_free;
  761. }
  762. /* Compute source port for outgoing packet
  763. * first choice to use L4 flow hash since it will spread
  764. * better and maybe available from hardware
  765. * secondary choice is to use jhash on the Ethernet header
  766. */
  767. static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
  768. {
  769. unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
  770. u32 hash;
  771. hash = skb_get_rxhash(skb);
  772. if (!hash)
  773. hash = jhash(skb->data, 2 * ETH_ALEN,
  774. (__force u32) skb->protocol);
  775. return htons((((u64) hash * range) >> 32) + vxlan->port_min);
  776. }
  777. static int handle_offloads(struct sk_buff *skb)
  778. {
  779. if (skb_is_gso(skb)) {
  780. int err = skb_unclone(skb, GFP_ATOMIC);
  781. if (unlikely(err))
  782. return err;
  783. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  784. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  785. skb->ip_summed = CHECKSUM_NONE;
  786. return 0;
  787. }
  788. /* Bypass encapsulation if the destination is local */
  789. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  790. struct vxlan_dev *dst_vxlan)
  791. {
  792. struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  793. struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  794. skb->pkt_type = PACKET_HOST;
  795. skb->encapsulation = 0;
  796. skb->dev = dst_vxlan->dev;
  797. __skb_pull(skb, skb_network_offset(skb));
  798. if (dst_vxlan->flags & VXLAN_F_LEARN)
  799. vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
  800. eth_hdr(skb)->h_source);
  801. u64_stats_update_begin(&tx_stats->syncp);
  802. tx_stats->tx_packets++;
  803. tx_stats->tx_bytes += skb->len;
  804. u64_stats_update_end(&tx_stats->syncp);
  805. if (netif_rx(skb) == NET_RX_SUCCESS) {
  806. u64_stats_update_begin(&rx_stats->syncp);
  807. rx_stats->rx_packets++;
  808. rx_stats->rx_bytes += skb->len;
  809. u64_stats_update_end(&rx_stats->syncp);
  810. } else {
  811. skb->dev->stats.rx_dropped++;
  812. }
  813. }
  814. static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  815. struct vxlan_rdst *rdst, bool did_rsc)
  816. {
  817. struct vxlan_dev *vxlan = netdev_priv(dev);
  818. struct rtable *rt;
  819. const struct iphdr *old_iph;
  820. struct iphdr *iph;
  821. struct vxlanhdr *vxh;
  822. struct udphdr *uh;
  823. struct flowi4 fl4;
  824. __be32 dst;
  825. __be16 src_port, dst_port;
  826. u32 vni;
  827. __be16 df = 0;
  828. __u8 tos, ttl;
  829. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
  830. vni = rdst->remote_vni;
  831. dst = rdst->remote_ip;
  832. if (!dst) {
  833. if (did_rsc) {
  834. /* short-circuited back to local bridge */
  835. vxlan_encap_bypass(skb, vxlan, vxlan);
  836. return NETDEV_TX_OK;
  837. }
  838. goto drop;
  839. }
  840. if (!skb->encapsulation) {
  841. skb_reset_inner_headers(skb);
  842. skb->encapsulation = 1;
  843. }
  844. /* Need space for new headers (invalidates iph ptr) */
  845. if (skb_cow_head(skb, VXLAN_HEADROOM))
  846. goto drop;
  847. old_iph = ip_hdr(skb);
  848. ttl = vxlan->ttl;
  849. if (!ttl && IN_MULTICAST(ntohl(dst)))
  850. ttl = 1;
  851. tos = vxlan->tos;
  852. if (tos == 1)
  853. tos = ip_tunnel_get_dsfield(old_iph, skb);
  854. src_port = vxlan_src_port(vxlan, skb);
  855. memset(&fl4, 0, sizeof(fl4));
  856. fl4.flowi4_oif = rdst->remote_ifindex;
  857. fl4.flowi4_tos = RT_TOS(tos);
  858. fl4.daddr = dst;
  859. fl4.saddr = vxlan->saddr;
  860. rt = ip_route_output_key(dev_net(dev), &fl4);
  861. if (IS_ERR(rt)) {
  862. netdev_dbg(dev, "no route to %pI4\n", &dst);
  863. dev->stats.tx_carrier_errors++;
  864. goto tx_error;
  865. }
  866. if (rt->dst.dev == dev) {
  867. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  868. ip_rt_put(rt);
  869. dev->stats.collisions++;
  870. goto tx_error;
  871. }
  872. /* Bypass encapsulation if the destination is local */
  873. if (rt->rt_flags & RTCF_LOCAL &&
  874. !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  875. struct vxlan_dev *dst_vxlan;
  876. ip_rt_put(rt);
  877. dst_vxlan = vxlan_find_vni(dev_net(dev), vni);
  878. if (!dst_vxlan)
  879. goto tx_error;
  880. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  881. return NETDEV_TX_OK;
  882. }
  883. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  884. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  885. IPSKB_REROUTED);
  886. skb_dst_drop(skb);
  887. skb_dst_set(skb, &rt->dst);
  888. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  889. vxh->vx_flags = htonl(VXLAN_FLAGS);
  890. vxh->vx_vni = htonl(vni << 8);
  891. __skb_push(skb, sizeof(*uh));
  892. skb_reset_transport_header(skb);
  893. uh = udp_hdr(skb);
  894. uh->dest = dst_port;
  895. uh->source = src_port;
  896. uh->len = htons(skb->len);
  897. uh->check = 0;
  898. __skb_push(skb, sizeof(*iph));
  899. skb_reset_network_header(skb);
  900. iph = ip_hdr(skb);
  901. iph->version = 4;
  902. iph->ihl = sizeof(struct iphdr) >> 2;
  903. iph->frag_off = df;
  904. iph->protocol = IPPROTO_UDP;
  905. iph->tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  906. iph->daddr = dst;
  907. iph->saddr = fl4.saddr;
  908. iph->ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  909. tunnel_ip_select_ident(skb, old_iph, &rt->dst);
  910. nf_reset(skb);
  911. vxlan_set_owner(dev, skb);
  912. if (handle_offloads(skb))
  913. goto drop;
  914. iptunnel_xmit(skb, dev);
  915. return NETDEV_TX_OK;
  916. drop:
  917. dev->stats.tx_dropped++;
  918. goto tx_free;
  919. tx_error:
  920. dev->stats.tx_errors++;
  921. tx_free:
  922. dev_kfree_skb(skb);
  923. return NETDEV_TX_OK;
  924. }
  925. /* Transmit local packets over Vxlan
  926. *
  927. * Outer IP header inherits ECN and DF from inner header.
  928. * Outer UDP destination is the VXLAN assigned port.
  929. * source port is based on hash of flow
  930. */
  931. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  932. {
  933. struct vxlan_dev *vxlan = netdev_priv(dev);
  934. struct ethhdr *eth;
  935. bool did_rsc = false;
  936. struct vxlan_rdst *rdst0, *rdst;
  937. struct vxlan_fdb *f;
  938. int rc1, rc;
  939. skb_reset_mac_header(skb);
  940. eth = eth_hdr(skb);
  941. if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
  942. return arp_reduce(dev, skb);
  943. f = vxlan_find_mac(vxlan, eth->h_dest);
  944. did_rsc = false;
  945. if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
  946. ntohs(eth->h_proto) == ETH_P_IP) {
  947. did_rsc = route_shortcircuit(dev, skb);
  948. if (did_rsc)
  949. f = vxlan_find_mac(vxlan, eth->h_dest);
  950. }
  951. if (f == NULL) {
  952. rdst0 = &vxlan->default_dst;
  953. if (rdst0->remote_ip == htonl(INADDR_ANY) &&
  954. (vxlan->flags & VXLAN_F_L2MISS) &&
  955. !is_multicast_ether_addr(eth->h_dest))
  956. vxlan_fdb_miss(vxlan, eth->h_dest);
  957. } else
  958. rdst0 = &f->remote;
  959. rc = NETDEV_TX_OK;
  960. /* if there are multiple destinations, send copies */
  961. for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
  962. struct sk_buff *skb1;
  963. skb1 = skb_clone(skb, GFP_ATOMIC);
  964. if (skb1) {
  965. rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
  966. if (rc == NETDEV_TX_OK)
  967. rc = rc1;
  968. }
  969. }
  970. rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
  971. if (rc == NETDEV_TX_OK)
  972. rc = rc1;
  973. return rc;
  974. }
  975. /* Walk the forwarding table and purge stale entries */
  976. static void vxlan_cleanup(unsigned long arg)
  977. {
  978. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  979. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  980. unsigned int h;
  981. if (!netif_running(vxlan->dev))
  982. return;
  983. spin_lock_bh(&vxlan->hash_lock);
  984. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  985. struct hlist_node *p, *n;
  986. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  987. struct vxlan_fdb *f
  988. = container_of(p, struct vxlan_fdb, hlist);
  989. unsigned long timeout;
  990. if (f->state & NUD_PERMANENT)
  991. continue;
  992. timeout = f->used + vxlan->age_interval * HZ;
  993. if (time_before_eq(timeout, jiffies)) {
  994. netdev_dbg(vxlan->dev,
  995. "garbage collect %pM\n",
  996. f->eth_addr);
  997. f->state = NUD_STALE;
  998. vxlan_fdb_destroy(vxlan, f);
  999. } else if (time_before(timeout, next_timer))
  1000. next_timer = timeout;
  1001. }
  1002. }
  1003. spin_unlock_bh(&vxlan->hash_lock);
  1004. mod_timer(&vxlan->age_timer, next_timer);
  1005. }
  1006. /* Setup stats when device is created */
  1007. static int vxlan_init(struct net_device *dev)
  1008. {
  1009. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1010. if (!dev->tstats)
  1011. return -ENOMEM;
  1012. return 0;
  1013. }
  1014. /* Start ageing timer and join group when device is brought up */
  1015. static int vxlan_open(struct net_device *dev)
  1016. {
  1017. struct vxlan_dev *vxlan = netdev_priv(dev);
  1018. int err;
  1019. if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
  1020. err = vxlan_join_group(dev);
  1021. if (err)
  1022. return err;
  1023. }
  1024. if (vxlan->age_interval)
  1025. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  1026. return 0;
  1027. }
  1028. /* Purge the forwarding table */
  1029. static void vxlan_flush(struct vxlan_dev *vxlan)
  1030. {
  1031. unsigned h;
  1032. spin_lock_bh(&vxlan->hash_lock);
  1033. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1034. struct hlist_node *p, *n;
  1035. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1036. struct vxlan_fdb *f
  1037. = container_of(p, struct vxlan_fdb, hlist);
  1038. vxlan_fdb_destroy(vxlan, f);
  1039. }
  1040. }
  1041. spin_unlock_bh(&vxlan->hash_lock);
  1042. }
  1043. /* Cleanup timer and forwarding table on shutdown */
  1044. static int vxlan_stop(struct net_device *dev)
  1045. {
  1046. struct vxlan_dev *vxlan = netdev_priv(dev);
  1047. if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
  1048. vxlan_leave_group(dev);
  1049. del_timer_sync(&vxlan->age_timer);
  1050. vxlan_flush(vxlan);
  1051. return 0;
  1052. }
  1053. /* Stub, nothing needs to be done. */
  1054. static void vxlan_set_multicast_list(struct net_device *dev)
  1055. {
  1056. }
  1057. static const struct net_device_ops vxlan_netdev_ops = {
  1058. .ndo_init = vxlan_init,
  1059. .ndo_open = vxlan_open,
  1060. .ndo_stop = vxlan_stop,
  1061. .ndo_start_xmit = vxlan_xmit,
  1062. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1063. .ndo_set_rx_mode = vxlan_set_multicast_list,
  1064. .ndo_change_mtu = eth_change_mtu,
  1065. .ndo_validate_addr = eth_validate_addr,
  1066. .ndo_set_mac_address = eth_mac_addr,
  1067. .ndo_fdb_add = vxlan_fdb_add,
  1068. .ndo_fdb_del = vxlan_fdb_delete,
  1069. .ndo_fdb_dump = vxlan_fdb_dump,
  1070. };
  1071. /* Info for udev, that this is a virtual tunnel endpoint */
  1072. static struct device_type vxlan_type = {
  1073. .name = "vxlan",
  1074. };
  1075. static void vxlan_free(struct net_device *dev)
  1076. {
  1077. free_percpu(dev->tstats);
  1078. free_netdev(dev);
  1079. }
  1080. /* Initialize the device structure. */
  1081. static void vxlan_setup(struct net_device *dev)
  1082. {
  1083. struct vxlan_dev *vxlan = netdev_priv(dev);
  1084. unsigned h;
  1085. int low, high;
  1086. eth_hw_addr_random(dev);
  1087. ether_setup(dev);
  1088. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  1089. dev->netdev_ops = &vxlan_netdev_ops;
  1090. dev->destructor = vxlan_free;
  1091. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  1092. dev->tx_queue_len = 0;
  1093. dev->features |= NETIF_F_LLTX;
  1094. dev->features |= NETIF_F_NETNS_LOCAL;
  1095. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1096. dev->features |= NETIF_F_RXCSUM;
  1097. dev->features |= NETIF_F_GSO_SOFTWARE;
  1098. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1099. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1100. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1101. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1102. spin_lock_init(&vxlan->hash_lock);
  1103. init_timer_deferrable(&vxlan->age_timer);
  1104. vxlan->age_timer.function = vxlan_cleanup;
  1105. vxlan->age_timer.data = (unsigned long) vxlan;
  1106. inet_get_local_port_range(&low, &high);
  1107. vxlan->port_min = low;
  1108. vxlan->port_max = high;
  1109. vxlan->dst_port = htons(vxlan_port);
  1110. vxlan->dev = dev;
  1111. for (h = 0; h < FDB_HASH_SIZE; ++h)
  1112. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  1113. }
  1114. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  1115. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1116. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1117. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1118. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1119. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1120. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1121. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1122. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1123. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1124. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1125. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1126. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1127. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1128. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1129. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  1130. };
  1131. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1132. {
  1133. if (tb[IFLA_ADDRESS]) {
  1134. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1135. pr_debug("invalid link address (not ethernet)\n");
  1136. return -EINVAL;
  1137. }
  1138. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1139. pr_debug("invalid all zero ethernet address\n");
  1140. return -EADDRNOTAVAIL;
  1141. }
  1142. }
  1143. if (!data)
  1144. return -EINVAL;
  1145. if (data[IFLA_VXLAN_ID]) {
  1146. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1147. if (id >= VXLAN_VID_MASK)
  1148. return -ERANGE;
  1149. }
  1150. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1151. const struct ifla_vxlan_port_range *p
  1152. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1153. if (ntohs(p->high) < ntohs(p->low)) {
  1154. pr_debug("port range %u .. %u not valid\n",
  1155. ntohs(p->low), ntohs(p->high));
  1156. return -EINVAL;
  1157. }
  1158. }
  1159. return 0;
  1160. }
  1161. static void vxlan_get_drvinfo(struct net_device *netdev,
  1162. struct ethtool_drvinfo *drvinfo)
  1163. {
  1164. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  1165. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  1166. }
  1167. static const struct ethtool_ops vxlan_ethtool_ops = {
  1168. .get_drvinfo = vxlan_get_drvinfo,
  1169. .get_link = ethtool_op_get_link,
  1170. };
  1171. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1172. struct nlattr *tb[], struct nlattr *data[])
  1173. {
  1174. struct vxlan_dev *vxlan = netdev_priv(dev);
  1175. struct vxlan_rdst *dst = &vxlan->default_dst;
  1176. __u32 vni;
  1177. int err;
  1178. if (!data[IFLA_VXLAN_ID])
  1179. return -EINVAL;
  1180. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1181. if (vxlan_find_vni(net, vni)) {
  1182. pr_info("duplicate VNI %u\n", vni);
  1183. return -EEXIST;
  1184. }
  1185. dst->remote_vni = vni;
  1186. if (data[IFLA_VXLAN_GROUP])
  1187. dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1188. if (data[IFLA_VXLAN_LOCAL])
  1189. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  1190. if (data[IFLA_VXLAN_LINK] &&
  1191. (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  1192. struct net_device *lowerdev
  1193. = __dev_get_by_index(net, dst->remote_ifindex);
  1194. if (!lowerdev) {
  1195. pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
  1196. return -ENODEV;
  1197. }
  1198. if (!tb[IFLA_MTU])
  1199. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1200. /* update header length based on lower device */
  1201. dev->hard_header_len = lowerdev->hard_header_len +
  1202. VXLAN_HEADROOM;
  1203. }
  1204. if (data[IFLA_VXLAN_TOS])
  1205. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  1206. if (data[IFLA_VXLAN_TTL])
  1207. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  1208. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  1209. vxlan->flags |= VXLAN_F_LEARN;
  1210. if (data[IFLA_VXLAN_AGEING])
  1211. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  1212. else
  1213. vxlan->age_interval = FDB_AGE_DEFAULT;
  1214. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  1215. vxlan->flags |= VXLAN_F_PROXY;
  1216. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  1217. vxlan->flags |= VXLAN_F_RSC;
  1218. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  1219. vxlan->flags |= VXLAN_F_L2MISS;
  1220. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  1221. vxlan->flags |= VXLAN_F_L3MISS;
  1222. if (data[IFLA_VXLAN_LIMIT])
  1223. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  1224. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1225. const struct ifla_vxlan_port_range *p
  1226. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1227. vxlan->port_min = ntohs(p->low);
  1228. vxlan->port_max = ntohs(p->high);
  1229. }
  1230. if (data[IFLA_VXLAN_PORT])
  1231. vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  1232. SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
  1233. err = register_netdevice(dev);
  1234. if (!err)
  1235. hlist_add_head_rcu(&vxlan->hlist, vni_head(net, dst->remote_vni));
  1236. return err;
  1237. }
  1238. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  1239. {
  1240. struct vxlan_dev *vxlan = netdev_priv(dev);
  1241. hlist_del_rcu(&vxlan->hlist);
  1242. unregister_netdevice_queue(dev, head);
  1243. }
  1244. static size_t vxlan_get_size(const struct net_device *dev)
  1245. {
  1246. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  1247. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  1248. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  1249. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  1250. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  1251. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  1252. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  1253. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  1254. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  1255. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  1256. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  1257. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  1258. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  1259. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  1260. nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
  1261. 0;
  1262. }
  1263. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1264. {
  1265. const struct vxlan_dev *vxlan = netdev_priv(dev);
  1266. const struct vxlan_rdst *dst = &vxlan->default_dst;
  1267. struct ifla_vxlan_port_range ports = {
  1268. .low = htons(vxlan->port_min),
  1269. .high = htons(vxlan->port_max),
  1270. };
  1271. if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
  1272. goto nla_put_failure;
  1273. if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
  1274. goto nla_put_failure;
  1275. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  1276. goto nla_put_failure;
  1277. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  1278. goto nla_put_failure;
  1279. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  1280. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  1281. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  1282. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  1283. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  1284. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  1285. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  1286. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  1287. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  1288. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  1289. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  1290. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  1291. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
  1292. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
  1293. goto nla_put_failure;
  1294. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  1295. goto nla_put_failure;
  1296. return 0;
  1297. nla_put_failure:
  1298. return -EMSGSIZE;
  1299. }
  1300. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  1301. .kind = "vxlan",
  1302. .maxtype = IFLA_VXLAN_MAX,
  1303. .policy = vxlan_policy,
  1304. .priv_size = sizeof(struct vxlan_dev),
  1305. .setup = vxlan_setup,
  1306. .validate = vxlan_validate,
  1307. .newlink = vxlan_newlink,
  1308. .dellink = vxlan_dellink,
  1309. .get_size = vxlan_get_size,
  1310. .fill_info = vxlan_fill_info,
  1311. };
  1312. static __net_init int vxlan_init_net(struct net *net)
  1313. {
  1314. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1315. struct sock *sk;
  1316. struct sockaddr_in vxlan_addr = {
  1317. .sin_family = AF_INET,
  1318. .sin_addr.s_addr = htonl(INADDR_ANY),
  1319. };
  1320. int rc;
  1321. unsigned h;
  1322. /* Create UDP socket for encapsulation receive. */
  1323. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
  1324. if (rc < 0) {
  1325. pr_debug("UDP socket create failed\n");
  1326. return rc;
  1327. }
  1328. /* Put in proper namespace */
  1329. sk = vn->sock->sk;
  1330. sk_change_net(sk, net);
  1331. vxlan_addr.sin_port = htons(vxlan_port);
  1332. rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
  1333. sizeof(vxlan_addr));
  1334. if (rc < 0) {
  1335. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1336. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1337. sk_release_kernel(sk);
  1338. vn->sock = NULL;
  1339. return rc;
  1340. }
  1341. /* Disable multicast loopback */
  1342. inet_sk(sk)->mc_loop = 0;
  1343. /* Mark socket as an encapsulation socket. */
  1344. udp_sk(sk)->encap_type = 1;
  1345. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1346. udp_encap_enable();
  1347. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1348. INIT_HLIST_HEAD(&vn->vni_list[h]);
  1349. return 0;
  1350. }
  1351. static __net_exit void vxlan_exit_net(struct net *net)
  1352. {
  1353. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1354. struct vxlan_dev *vxlan;
  1355. unsigned h;
  1356. rtnl_lock();
  1357. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1358. hlist_for_each_entry(vxlan, &vn->vni_list[h], hlist)
  1359. dev_close(vxlan->dev);
  1360. rtnl_unlock();
  1361. if (vn->sock) {
  1362. sk_release_kernel(vn->sock->sk);
  1363. vn->sock = NULL;
  1364. }
  1365. }
  1366. static struct pernet_operations vxlan_net_ops = {
  1367. .init = vxlan_init_net,
  1368. .exit = vxlan_exit_net,
  1369. .id = &vxlan_net_id,
  1370. .size = sizeof(struct vxlan_net),
  1371. };
  1372. static int __init vxlan_init_module(void)
  1373. {
  1374. int rc;
  1375. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  1376. rc = register_pernet_device(&vxlan_net_ops);
  1377. if (rc)
  1378. goto out1;
  1379. rc = rtnl_link_register(&vxlan_link_ops);
  1380. if (rc)
  1381. goto out2;
  1382. return 0;
  1383. out2:
  1384. unregister_pernet_device(&vxlan_net_ops);
  1385. out1:
  1386. return rc;
  1387. }
  1388. module_init(vxlan_init_module);
  1389. static void __exit vxlan_cleanup_module(void)
  1390. {
  1391. rtnl_link_unregister(&vxlan_link_ops);
  1392. unregister_pernet_device(&vxlan_net_ops);
  1393. rcu_barrier();
  1394. }
  1395. module_exit(vxlan_cleanup_module);
  1396. MODULE_LICENSE("GPL");
  1397. MODULE_VERSION(VXLAN_VERSION);
  1398. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  1399. MODULE_ALIAS_RTNL_LINK("vxlan");