vxlan.c 41 KB

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