vxlan.c 47 KB

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