vxlan.c 42 KB

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