af_mpls.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/netconf.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/percpu.h>
  13. #include <net/ip.h>
  14. #include <net/dst.h>
  15. #include <net/sock.h>
  16. #include <net/arp.h>
  17. #include <net/ip_fib.h>
  18. #include <net/netevent.h>
  19. #include <net/netns/generic.h>
  20. #if IS_ENABLED(CONFIG_IPV6)
  21. #include <net/ipv6.h>
  22. #endif
  23. #include <net/addrconf.h>
  24. #include <net/nexthop.h>
  25. #include "internal.h"
  26. /* Maximum number of labels to look ahead at when selecting a path of
  27. * a multipath route
  28. */
  29. #define MAX_MP_SELECT_LABELS 4
  30. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  31. static int zero = 0;
  32. static int label_limit = (1 << 20) - 1;
  33. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  34. struct nlmsghdr *nlh, struct net *net, u32 portid,
  35. unsigned int nlm_flags);
  36. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  37. {
  38. struct mpls_route *rt = NULL;
  39. if (index < net->mpls.platform_labels) {
  40. struct mpls_route __rcu **platform_label =
  41. rcu_dereference(net->mpls.platform_label);
  42. rt = rcu_dereference(platform_label[index]);
  43. }
  44. return rt;
  45. }
  46. bool mpls_output_possible(const struct net_device *dev)
  47. {
  48. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  49. }
  50. EXPORT_SYMBOL_GPL(mpls_output_possible);
  51. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  52. {
  53. u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
  54. int nh_index = nh - rt->rt_nh;
  55. return nh0_via + rt->rt_max_alen * nh_index;
  56. }
  57. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  58. const struct mpls_nh *nh)
  59. {
  60. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  61. }
  62. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  63. {
  64. /* The size of the layer 2.5 labels to be added for this route */
  65. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  66. }
  67. unsigned int mpls_dev_mtu(const struct net_device *dev)
  68. {
  69. /* The amount of data the layer 2 frame can hold */
  70. return dev->mtu;
  71. }
  72. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  73. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  74. {
  75. if (skb->len <= mtu)
  76. return false;
  77. if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
  78. return false;
  79. return true;
  80. }
  81. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  82. void mpls_stats_inc_outucastpkts(struct net_device *dev,
  83. const struct sk_buff *skb)
  84. {
  85. struct mpls_dev *mdev;
  86. if (skb->protocol == htons(ETH_P_MPLS_UC)) {
  87. mdev = mpls_dev_get(dev);
  88. if (mdev)
  89. MPLS_INC_STATS_LEN(mdev, skb->len,
  90. tx_packets,
  91. tx_bytes);
  92. } else if (skb->protocol == htons(ETH_P_IP)) {
  93. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
  94. #if IS_ENABLED(CONFIG_IPV6)
  95. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  96. struct inet6_dev *in6dev = __in6_dev_get(dev);
  97. if (in6dev)
  98. IP6_UPD_PO_STATS(dev_net(dev), in6dev,
  99. IPSTATS_MIB_OUT, skb->len);
  100. #endif
  101. }
  102. }
  103. EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
  104. static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
  105. {
  106. struct mpls_entry_decoded dec;
  107. unsigned int mpls_hdr_len = 0;
  108. struct mpls_shim_hdr *hdr;
  109. bool eli_seen = false;
  110. int label_index;
  111. u32 hash = 0;
  112. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
  113. label_index++) {
  114. mpls_hdr_len += sizeof(*hdr);
  115. if (!pskb_may_pull(skb, mpls_hdr_len))
  116. break;
  117. /* Read and decode the current label */
  118. hdr = mpls_hdr(skb) + label_index;
  119. dec = mpls_entry_decode(hdr);
  120. /* RFC6790 - reserved labels MUST NOT be used as keys
  121. * for the load-balancing function
  122. */
  123. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  124. hash = jhash_1word(dec.label, hash);
  125. /* The entropy label follows the entropy label
  126. * indicator, so this means that the entropy
  127. * label was just added to the hash - no need to
  128. * go any deeper either in the label stack or in the
  129. * payload
  130. */
  131. if (eli_seen)
  132. break;
  133. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  134. eli_seen = true;
  135. }
  136. if (!dec.bos)
  137. continue;
  138. /* found bottom label; does skb have room for a header? */
  139. if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
  140. const struct iphdr *v4hdr;
  141. v4hdr = (const struct iphdr *)(hdr + 1);
  142. if (v4hdr->version == 4) {
  143. hash = jhash_3words(ntohl(v4hdr->saddr),
  144. ntohl(v4hdr->daddr),
  145. v4hdr->protocol, hash);
  146. } else if (v4hdr->version == 6 &&
  147. pskb_may_pull(skb, mpls_hdr_len +
  148. sizeof(struct ipv6hdr))) {
  149. const struct ipv6hdr *v6hdr;
  150. v6hdr = (const struct ipv6hdr *)(hdr + 1);
  151. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  152. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  153. hash = jhash_1word(v6hdr->nexthdr, hash);
  154. }
  155. }
  156. break;
  157. }
  158. return hash;
  159. }
  160. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  161. struct sk_buff *skb)
  162. {
  163. int alive = ACCESS_ONCE(rt->rt_nhn_alive);
  164. u32 hash = 0;
  165. int nh_index = 0;
  166. int n = 0;
  167. /* No need to look further into packet if there's only
  168. * one path
  169. */
  170. if (rt->rt_nhn == 1)
  171. goto out;
  172. if (alive <= 0)
  173. return NULL;
  174. hash = mpls_multipath_hash(rt, skb);
  175. nh_index = hash % alive;
  176. if (alive == rt->rt_nhn)
  177. goto out;
  178. for_nexthops(rt) {
  179. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  180. continue;
  181. if (n == nh_index)
  182. return nh;
  183. n++;
  184. } endfor_nexthops(rt);
  185. out:
  186. return &rt->rt_nh[nh_index];
  187. }
  188. static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
  189. struct mpls_entry_decoded dec)
  190. {
  191. enum mpls_payload_type payload_type;
  192. bool success = false;
  193. /* The IPv4 code below accesses through the IPv4 header
  194. * checksum, which is 12 bytes into the packet.
  195. * The IPv6 code below accesses through the IPv6 hop limit
  196. * which is 8 bytes into the packet.
  197. *
  198. * For all supported cases there should always be at least 12
  199. * bytes of packet data present. The IPv4 header is 20 bytes
  200. * without options and the IPv6 header is always 40 bytes
  201. * long.
  202. */
  203. if (!pskb_may_pull(skb, 12))
  204. return false;
  205. payload_type = rt->rt_payload_type;
  206. if (payload_type == MPT_UNSPEC)
  207. payload_type = ip_hdr(skb)->version;
  208. switch (payload_type) {
  209. case MPT_IPV4: {
  210. struct iphdr *hdr4 = ip_hdr(skb);
  211. skb->protocol = htons(ETH_P_IP);
  212. csum_replace2(&hdr4->check,
  213. htons(hdr4->ttl << 8),
  214. htons(dec.ttl << 8));
  215. hdr4->ttl = dec.ttl;
  216. success = true;
  217. break;
  218. }
  219. case MPT_IPV6: {
  220. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  221. skb->protocol = htons(ETH_P_IPV6);
  222. hdr6->hop_limit = dec.ttl;
  223. success = true;
  224. break;
  225. }
  226. case MPT_UNSPEC:
  227. break;
  228. }
  229. return success;
  230. }
  231. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  232. struct packet_type *pt, struct net_device *orig_dev)
  233. {
  234. struct net *net = dev_net(dev);
  235. struct mpls_shim_hdr *hdr;
  236. struct mpls_route *rt;
  237. struct mpls_nh *nh;
  238. struct mpls_entry_decoded dec;
  239. struct net_device *out_dev;
  240. struct mpls_dev *out_mdev;
  241. struct mpls_dev *mdev;
  242. unsigned int hh_len;
  243. unsigned int new_header_size;
  244. unsigned int mtu;
  245. int err;
  246. /* Careful this entire function runs inside of an rcu critical section */
  247. mdev = mpls_dev_get(dev);
  248. if (!mdev)
  249. goto drop;
  250. MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
  251. rx_bytes);
  252. if (!mdev->input_enabled) {
  253. MPLS_INC_STATS(mdev, rx_dropped);
  254. goto drop;
  255. }
  256. if (skb->pkt_type != PACKET_HOST)
  257. goto err;
  258. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  259. goto err;
  260. if (!pskb_may_pull(skb, sizeof(*hdr)))
  261. goto err;
  262. /* Read and decode the label */
  263. hdr = mpls_hdr(skb);
  264. dec = mpls_entry_decode(hdr);
  265. rt = mpls_route_input_rcu(net, dec.label);
  266. if (!rt) {
  267. MPLS_INC_STATS(mdev, rx_noroute);
  268. goto drop;
  269. }
  270. nh = mpls_select_multipath(rt, skb);
  271. if (!nh)
  272. goto err;
  273. /* Pop the label */
  274. skb_pull(skb, sizeof(*hdr));
  275. skb_reset_network_header(skb);
  276. skb_orphan(skb);
  277. if (skb_warn_if_lro(skb))
  278. goto err;
  279. skb_forward_csum(skb);
  280. /* Verify ttl is valid */
  281. if (dec.ttl <= 1)
  282. goto err;
  283. dec.ttl -= 1;
  284. /* Find the output device */
  285. out_dev = rcu_dereference(nh->nh_dev);
  286. if (!mpls_output_possible(out_dev))
  287. goto tx_err;
  288. /* Verify the destination can hold the packet */
  289. new_header_size = mpls_nh_header_size(nh);
  290. mtu = mpls_dev_mtu(out_dev);
  291. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  292. goto tx_err;
  293. hh_len = LL_RESERVED_SPACE(out_dev);
  294. if (!out_dev->header_ops)
  295. hh_len = 0;
  296. /* Ensure there is enough space for the headers in the skb */
  297. if (skb_cow(skb, hh_len + new_header_size))
  298. goto tx_err;
  299. skb->dev = out_dev;
  300. skb->protocol = htons(ETH_P_MPLS_UC);
  301. if (unlikely(!new_header_size && dec.bos)) {
  302. /* Penultimate hop popping */
  303. if (!mpls_egress(rt, skb, dec))
  304. goto err;
  305. } else {
  306. bool bos;
  307. int i;
  308. skb_push(skb, new_header_size);
  309. skb_reset_network_header(skb);
  310. /* Push the new labels */
  311. hdr = mpls_hdr(skb);
  312. bos = dec.bos;
  313. for (i = nh->nh_labels - 1; i >= 0; i--) {
  314. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  315. dec.ttl, 0, bos);
  316. bos = false;
  317. }
  318. }
  319. mpls_stats_inc_outucastpkts(out_dev, skb);
  320. /* If via wasn't specified then send out using device address */
  321. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  322. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  323. out_dev->dev_addr, skb);
  324. else
  325. err = neigh_xmit(nh->nh_via_table, out_dev,
  326. mpls_nh_via(rt, nh), skb);
  327. if (err)
  328. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  329. __func__, err);
  330. return 0;
  331. tx_err:
  332. out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
  333. if (out_mdev)
  334. MPLS_INC_STATS(out_mdev, tx_errors);
  335. goto drop;
  336. err:
  337. MPLS_INC_STATS(mdev, rx_errors);
  338. drop:
  339. kfree_skb(skb);
  340. return NET_RX_DROP;
  341. }
  342. static struct packet_type mpls_packet_type __read_mostly = {
  343. .type = cpu_to_be16(ETH_P_MPLS_UC),
  344. .func = mpls_forward,
  345. };
  346. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  347. [RTA_DST] = { .type = NLA_U32 },
  348. [RTA_OIF] = { .type = NLA_U32 },
  349. };
  350. struct mpls_route_config {
  351. u32 rc_protocol;
  352. u32 rc_ifindex;
  353. u8 rc_via_table;
  354. u8 rc_via_alen;
  355. u8 rc_via[MAX_VIA_ALEN];
  356. u32 rc_label;
  357. u8 rc_output_labels;
  358. u32 rc_output_label[MAX_NEW_LABELS];
  359. u32 rc_nlflags;
  360. enum mpls_payload_type rc_payload_type;
  361. struct nl_info rc_nlinfo;
  362. struct rtnexthop *rc_mp;
  363. int rc_mp_len;
  364. };
  365. static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
  366. {
  367. u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
  368. struct mpls_route *rt;
  369. rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
  370. VIA_ALEN_ALIGN) +
  371. num_nh * max_alen_aligned,
  372. GFP_KERNEL);
  373. if (rt) {
  374. rt->rt_nhn = num_nh;
  375. rt->rt_nhn_alive = num_nh;
  376. rt->rt_max_alen = max_alen_aligned;
  377. }
  378. return rt;
  379. }
  380. static void mpls_rt_free(struct mpls_route *rt)
  381. {
  382. if (rt)
  383. kfree_rcu(rt, rt_rcu);
  384. }
  385. static void mpls_notify_route(struct net *net, unsigned index,
  386. struct mpls_route *old, struct mpls_route *new,
  387. const struct nl_info *info)
  388. {
  389. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  390. unsigned portid = info ? info->portid : 0;
  391. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  392. struct mpls_route *rt = new ? new : old;
  393. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  394. /* Ignore reserved labels for now */
  395. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  396. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  397. }
  398. static void mpls_route_update(struct net *net, unsigned index,
  399. struct mpls_route *new,
  400. const struct nl_info *info)
  401. {
  402. struct mpls_route __rcu **platform_label;
  403. struct mpls_route *rt;
  404. ASSERT_RTNL();
  405. platform_label = rtnl_dereference(net->mpls.platform_label);
  406. rt = rtnl_dereference(platform_label[index]);
  407. rcu_assign_pointer(platform_label[index], new);
  408. mpls_notify_route(net, index, rt, new, info);
  409. /* If we removed a route free it now */
  410. mpls_rt_free(rt);
  411. }
  412. static unsigned find_free_label(struct net *net)
  413. {
  414. struct mpls_route __rcu **platform_label;
  415. size_t platform_labels;
  416. unsigned index;
  417. platform_label = rtnl_dereference(net->mpls.platform_label);
  418. platform_labels = net->mpls.platform_labels;
  419. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  420. index++) {
  421. if (!rtnl_dereference(platform_label[index]))
  422. return index;
  423. }
  424. return LABEL_NOT_SPECIFIED;
  425. }
  426. #if IS_ENABLED(CONFIG_INET)
  427. static struct net_device *inet_fib_lookup_dev(struct net *net,
  428. const void *addr)
  429. {
  430. struct net_device *dev;
  431. struct rtable *rt;
  432. struct in_addr daddr;
  433. memcpy(&daddr, addr, sizeof(struct in_addr));
  434. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  435. if (IS_ERR(rt))
  436. return ERR_CAST(rt);
  437. dev = rt->dst.dev;
  438. dev_hold(dev);
  439. ip_rt_put(rt);
  440. return dev;
  441. }
  442. #else
  443. static struct net_device *inet_fib_lookup_dev(struct net *net,
  444. const void *addr)
  445. {
  446. return ERR_PTR(-EAFNOSUPPORT);
  447. }
  448. #endif
  449. #if IS_ENABLED(CONFIG_IPV6)
  450. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  451. const void *addr)
  452. {
  453. struct net_device *dev;
  454. struct dst_entry *dst;
  455. struct flowi6 fl6;
  456. int err;
  457. if (!ipv6_stub)
  458. return ERR_PTR(-EAFNOSUPPORT);
  459. memset(&fl6, 0, sizeof(fl6));
  460. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  461. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  462. if (err)
  463. return ERR_PTR(err);
  464. dev = dst->dev;
  465. dev_hold(dev);
  466. dst_release(dst);
  467. return dev;
  468. }
  469. #else
  470. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  471. const void *addr)
  472. {
  473. return ERR_PTR(-EAFNOSUPPORT);
  474. }
  475. #endif
  476. static struct net_device *find_outdev(struct net *net,
  477. struct mpls_route *rt,
  478. struct mpls_nh *nh, int oif)
  479. {
  480. struct net_device *dev = NULL;
  481. if (!oif) {
  482. switch (nh->nh_via_table) {
  483. case NEIGH_ARP_TABLE:
  484. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  485. break;
  486. case NEIGH_ND_TABLE:
  487. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  488. break;
  489. case NEIGH_LINK_TABLE:
  490. break;
  491. }
  492. } else {
  493. dev = dev_get_by_index(net, oif);
  494. }
  495. if (!dev)
  496. return ERR_PTR(-ENODEV);
  497. if (IS_ERR(dev))
  498. return dev;
  499. /* The caller is holding rtnl anyways, so release the dev reference */
  500. dev_put(dev);
  501. return dev;
  502. }
  503. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  504. struct mpls_nh *nh, int oif)
  505. {
  506. struct net_device *dev = NULL;
  507. int err = -ENODEV;
  508. dev = find_outdev(net, rt, nh, oif);
  509. if (IS_ERR(dev)) {
  510. err = PTR_ERR(dev);
  511. dev = NULL;
  512. goto errout;
  513. }
  514. /* Ensure this is a supported device */
  515. err = -EINVAL;
  516. if (!mpls_dev_get(dev))
  517. goto errout;
  518. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  519. (dev->addr_len != nh->nh_via_alen))
  520. goto errout;
  521. RCU_INIT_POINTER(nh->nh_dev, dev);
  522. if (!(dev->flags & IFF_UP)) {
  523. nh->nh_flags |= RTNH_F_DEAD;
  524. } else {
  525. unsigned int flags;
  526. flags = dev_get_flags(dev);
  527. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  528. nh->nh_flags |= RTNH_F_LINKDOWN;
  529. }
  530. return 0;
  531. errout:
  532. return err;
  533. }
  534. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  535. struct mpls_route *rt)
  536. {
  537. struct net *net = cfg->rc_nlinfo.nl_net;
  538. struct mpls_nh *nh = rt->rt_nh;
  539. int err;
  540. int i;
  541. if (!nh)
  542. return -ENOMEM;
  543. err = -EINVAL;
  544. /* Ensure only a supported number of labels are present */
  545. if (cfg->rc_output_labels > MAX_NEW_LABELS)
  546. goto errout;
  547. nh->nh_labels = cfg->rc_output_labels;
  548. for (i = 0; i < nh->nh_labels; i++)
  549. nh->nh_label[i] = cfg->rc_output_label[i];
  550. nh->nh_via_table = cfg->rc_via_table;
  551. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  552. nh->nh_via_alen = cfg->rc_via_alen;
  553. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  554. if (err)
  555. goto errout;
  556. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  557. rt->rt_nhn_alive--;
  558. return 0;
  559. errout:
  560. return err;
  561. }
  562. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  563. struct mpls_nh *nh, int oif, struct nlattr *via,
  564. struct nlattr *newdst)
  565. {
  566. int err = -ENOMEM;
  567. if (!nh)
  568. goto errout;
  569. if (newdst) {
  570. err = nla_get_labels(newdst, MAX_NEW_LABELS,
  571. &nh->nh_labels, nh->nh_label);
  572. if (err)
  573. goto errout;
  574. }
  575. if (via) {
  576. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  577. __mpls_nh_via(rt, nh));
  578. if (err)
  579. goto errout;
  580. } else {
  581. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  582. }
  583. err = mpls_nh_assign_dev(net, rt, nh, oif);
  584. if (err)
  585. goto errout;
  586. return 0;
  587. errout:
  588. return err;
  589. }
  590. static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  591. u8 cfg_via_alen, u8 *max_via_alen)
  592. {
  593. int nhs = 0;
  594. int remaining = len;
  595. if (!rtnh) {
  596. *max_via_alen = cfg_via_alen;
  597. return 1;
  598. }
  599. *max_via_alen = 0;
  600. while (rtnh_ok(rtnh, remaining)) {
  601. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  602. int attrlen;
  603. attrlen = rtnh_attrlen(rtnh);
  604. nla = nla_find(attrs, attrlen, RTA_VIA);
  605. if (nla && nla_len(nla) >=
  606. offsetof(struct rtvia, rtvia_addr)) {
  607. int via_alen = nla_len(nla) -
  608. offsetof(struct rtvia, rtvia_addr);
  609. if (via_alen <= MAX_VIA_ALEN)
  610. *max_via_alen = max_t(u16, *max_via_alen,
  611. via_alen);
  612. }
  613. nhs++;
  614. rtnh = rtnh_next(rtnh, &remaining);
  615. }
  616. /* leftover implies invalid nexthop configuration, discard it */
  617. return remaining > 0 ? 0 : nhs;
  618. }
  619. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  620. struct mpls_route *rt)
  621. {
  622. struct rtnexthop *rtnh = cfg->rc_mp;
  623. struct nlattr *nla_via, *nla_newdst;
  624. int remaining = cfg->rc_mp_len;
  625. int nhs = 0;
  626. int err = 0;
  627. change_nexthops(rt) {
  628. int attrlen;
  629. nla_via = NULL;
  630. nla_newdst = NULL;
  631. err = -EINVAL;
  632. if (!rtnh_ok(rtnh, remaining))
  633. goto errout;
  634. /* neither weighted multipath nor any flags
  635. * are supported
  636. */
  637. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  638. goto errout;
  639. attrlen = rtnh_attrlen(rtnh);
  640. if (attrlen > 0) {
  641. struct nlattr *attrs = rtnh_attrs(rtnh);
  642. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  643. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  644. }
  645. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  646. rtnh->rtnh_ifindex, nla_via, nla_newdst);
  647. if (err)
  648. goto errout;
  649. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  650. rt->rt_nhn_alive--;
  651. rtnh = rtnh_next(rtnh, &remaining);
  652. nhs++;
  653. } endfor_nexthops(rt);
  654. rt->rt_nhn = nhs;
  655. return 0;
  656. errout:
  657. return err;
  658. }
  659. static int mpls_route_add(struct mpls_route_config *cfg)
  660. {
  661. struct mpls_route __rcu **platform_label;
  662. struct net *net = cfg->rc_nlinfo.nl_net;
  663. struct mpls_route *rt, *old;
  664. int err = -EINVAL;
  665. u8 max_via_alen;
  666. unsigned index;
  667. int nhs;
  668. index = cfg->rc_label;
  669. /* If a label was not specified during insert pick one */
  670. if ((index == LABEL_NOT_SPECIFIED) &&
  671. (cfg->rc_nlflags & NLM_F_CREATE)) {
  672. index = find_free_label(net);
  673. }
  674. /* Reserved labels may not be set */
  675. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  676. goto errout;
  677. /* The full 20 bit range may not be supported. */
  678. if (index >= net->mpls.platform_labels)
  679. goto errout;
  680. /* Append makes no sense with mpls */
  681. err = -EOPNOTSUPP;
  682. if (cfg->rc_nlflags & NLM_F_APPEND)
  683. goto errout;
  684. err = -EEXIST;
  685. platform_label = rtnl_dereference(net->mpls.platform_label);
  686. old = rtnl_dereference(platform_label[index]);
  687. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  688. goto errout;
  689. err = -EEXIST;
  690. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  691. goto errout;
  692. err = -ENOENT;
  693. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  694. goto errout;
  695. err = -EINVAL;
  696. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  697. cfg->rc_via_alen, &max_via_alen);
  698. if (nhs == 0)
  699. goto errout;
  700. err = -ENOMEM;
  701. rt = mpls_rt_alloc(nhs, max_via_alen);
  702. if (!rt)
  703. goto errout;
  704. rt->rt_protocol = cfg->rc_protocol;
  705. rt->rt_payload_type = cfg->rc_payload_type;
  706. if (cfg->rc_mp)
  707. err = mpls_nh_build_multi(cfg, rt);
  708. else
  709. err = mpls_nh_build_from_cfg(cfg, rt);
  710. if (err)
  711. goto freert;
  712. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  713. return 0;
  714. freert:
  715. mpls_rt_free(rt);
  716. errout:
  717. return err;
  718. }
  719. static int mpls_route_del(struct mpls_route_config *cfg)
  720. {
  721. struct net *net = cfg->rc_nlinfo.nl_net;
  722. unsigned index;
  723. int err = -EINVAL;
  724. index = cfg->rc_label;
  725. /* Reserved labels may not be removed */
  726. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  727. goto errout;
  728. /* The full 20 bit range may not be supported */
  729. if (index >= net->mpls.platform_labels)
  730. goto errout;
  731. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  732. err = 0;
  733. errout:
  734. return err;
  735. }
  736. static void mpls_get_stats(struct mpls_dev *mdev,
  737. struct mpls_link_stats *stats)
  738. {
  739. struct mpls_pcpu_stats *p;
  740. int i;
  741. memset(stats, 0, sizeof(*stats));
  742. for_each_possible_cpu(i) {
  743. struct mpls_link_stats local;
  744. unsigned int start;
  745. p = per_cpu_ptr(mdev->stats, i);
  746. do {
  747. start = u64_stats_fetch_begin(&p->syncp);
  748. local = p->stats;
  749. } while (u64_stats_fetch_retry(&p->syncp, start));
  750. stats->rx_packets += local.rx_packets;
  751. stats->rx_bytes += local.rx_bytes;
  752. stats->tx_packets += local.tx_packets;
  753. stats->tx_bytes += local.tx_bytes;
  754. stats->rx_errors += local.rx_errors;
  755. stats->tx_errors += local.tx_errors;
  756. stats->rx_dropped += local.rx_dropped;
  757. stats->tx_dropped += local.tx_dropped;
  758. stats->rx_noroute += local.rx_noroute;
  759. }
  760. }
  761. static int mpls_fill_stats_af(struct sk_buff *skb,
  762. const struct net_device *dev)
  763. {
  764. struct mpls_link_stats *stats;
  765. struct mpls_dev *mdev;
  766. struct nlattr *nla;
  767. mdev = mpls_dev_get(dev);
  768. if (!mdev)
  769. return -ENODATA;
  770. nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
  771. sizeof(struct mpls_link_stats),
  772. MPLS_STATS_UNSPEC);
  773. if (!nla)
  774. return -EMSGSIZE;
  775. stats = nla_data(nla);
  776. mpls_get_stats(mdev, stats);
  777. return 0;
  778. }
  779. static size_t mpls_get_stats_af_size(const struct net_device *dev)
  780. {
  781. struct mpls_dev *mdev;
  782. mdev = mpls_dev_get(dev);
  783. if (!mdev)
  784. return 0;
  785. return nla_total_size_64bit(sizeof(struct mpls_link_stats));
  786. }
  787. static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
  788. u32 portid, u32 seq, int event,
  789. unsigned int flags, int type)
  790. {
  791. struct nlmsghdr *nlh;
  792. struct netconfmsg *ncm;
  793. bool all = false;
  794. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  795. flags);
  796. if (!nlh)
  797. return -EMSGSIZE;
  798. if (type == NETCONFA_ALL)
  799. all = true;
  800. ncm = nlmsg_data(nlh);
  801. ncm->ncm_family = AF_MPLS;
  802. if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
  803. goto nla_put_failure;
  804. if ((all || type == NETCONFA_INPUT) &&
  805. nla_put_s32(skb, NETCONFA_INPUT,
  806. mdev->input_enabled) < 0)
  807. goto nla_put_failure;
  808. nlmsg_end(skb, nlh);
  809. return 0;
  810. nla_put_failure:
  811. nlmsg_cancel(skb, nlh);
  812. return -EMSGSIZE;
  813. }
  814. static int mpls_netconf_msgsize_devconf(int type)
  815. {
  816. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  817. + nla_total_size(4); /* NETCONFA_IFINDEX */
  818. bool all = false;
  819. if (type == NETCONFA_ALL)
  820. all = true;
  821. if (all || type == NETCONFA_INPUT)
  822. size += nla_total_size(4);
  823. return size;
  824. }
  825. static void mpls_netconf_notify_devconf(struct net *net, int type,
  826. struct mpls_dev *mdev)
  827. {
  828. struct sk_buff *skb;
  829. int err = -ENOBUFS;
  830. skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
  831. if (!skb)
  832. goto errout;
  833. err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, RTM_NEWNETCONF,
  834. 0, type);
  835. if (err < 0) {
  836. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  837. WARN_ON(err == -EMSGSIZE);
  838. kfree_skb(skb);
  839. goto errout;
  840. }
  841. rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
  842. return;
  843. errout:
  844. if (err < 0)
  845. rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
  846. }
  847. static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
  848. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  849. };
  850. static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
  851. struct nlmsghdr *nlh)
  852. {
  853. struct net *net = sock_net(in_skb->sk);
  854. struct nlattr *tb[NETCONFA_MAX + 1];
  855. struct netconfmsg *ncm;
  856. struct net_device *dev;
  857. struct mpls_dev *mdev;
  858. struct sk_buff *skb;
  859. int ifindex;
  860. int err;
  861. err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
  862. devconf_mpls_policy);
  863. if (err < 0)
  864. goto errout;
  865. err = -EINVAL;
  866. if (!tb[NETCONFA_IFINDEX])
  867. goto errout;
  868. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  869. dev = __dev_get_by_index(net, ifindex);
  870. if (!dev)
  871. goto errout;
  872. mdev = mpls_dev_get(dev);
  873. if (!mdev)
  874. goto errout;
  875. err = -ENOBUFS;
  876. skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  877. if (!skb)
  878. goto errout;
  879. err = mpls_netconf_fill_devconf(skb, mdev,
  880. NETLINK_CB(in_skb).portid,
  881. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  882. NETCONFA_ALL);
  883. if (err < 0) {
  884. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  885. WARN_ON(err == -EMSGSIZE);
  886. kfree_skb(skb);
  887. goto errout;
  888. }
  889. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  890. errout:
  891. return err;
  892. }
  893. static int mpls_netconf_dump_devconf(struct sk_buff *skb,
  894. struct netlink_callback *cb)
  895. {
  896. struct net *net = sock_net(skb->sk);
  897. struct hlist_head *head;
  898. struct net_device *dev;
  899. struct mpls_dev *mdev;
  900. int idx, s_idx;
  901. int h, s_h;
  902. s_h = cb->args[0];
  903. s_idx = idx = cb->args[1];
  904. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  905. idx = 0;
  906. head = &net->dev_index_head[h];
  907. rcu_read_lock();
  908. cb->seq = net->dev_base_seq;
  909. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  910. if (idx < s_idx)
  911. goto cont;
  912. mdev = mpls_dev_get(dev);
  913. if (!mdev)
  914. goto cont;
  915. if (mpls_netconf_fill_devconf(skb, mdev,
  916. NETLINK_CB(cb->skb).portid,
  917. cb->nlh->nlmsg_seq,
  918. RTM_NEWNETCONF,
  919. NLM_F_MULTI,
  920. NETCONFA_ALL) < 0) {
  921. rcu_read_unlock();
  922. goto done;
  923. }
  924. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  925. cont:
  926. idx++;
  927. }
  928. rcu_read_unlock();
  929. }
  930. done:
  931. cb->args[0] = h;
  932. cb->args[1] = idx;
  933. return skb->len;
  934. }
  935. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  936. (&((struct mpls_dev *)0)->field)
  937. static int mpls_conf_proc(struct ctl_table *ctl, int write,
  938. void __user *buffer,
  939. size_t *lenp, loff_t *ppos)
  940. {
  941. int oval = *(int *)ctl->data;
  942. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  943. if (write) {
  944. struct mpls_dev *mdev = ctl->extra1;
  945. int i = (int *)ctl->data - (int *)mdev;
  946. struct net *net = ctl->extra2;
  947. int val = *(int *)ctl->data;
  948. if (i == offsetof(struct mpls_dev, input_enabled) &&
  949. val != oval) {
  950. mpls_netconf_notify_devconf(net,
  951. NETCONFA_INPUT,
  952. mdev);
  953. }
  954. }
  955. return ret;
  956. }
  957. static const struct ctl_table mpls_dev_table[] = {
  958. {
  959. .procname = "input",
  960. .maxlen = sizeof(int),
  961. .mode = 0644,
  962. .proc_handler = mpls_conf_proc,
  963. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  964. },
  965. { }
  966. };
  967. static int mpls_dev_sysctl_register(struct net_device *dev,
  968. struct mpls_dev *mdev)
  969. {
  970. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  971. struct net *net = dev_net(dev);
  972. struct ctl_table *table;
  973. int i;
  974. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  975. if (!table)
  976. goto out;
  977. /* Table data contains only offsets relative to the base of
  978. * the mdev at this point, so make them absolute.
  979. */
  980. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
  981. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  982. table[i].extra1 = mdev;
  983. table[i].extra2 = net;
  984. }
  985. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  986. mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
  987. if (!mdev->sysctl)
  988. goto free;
  989. return 0;
  990. free:
  991. kfree(table);
  992. out:
  993. return -ENOBUFS;
  994. }
  995. static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
  996. {
  997. struct ctl_table *table;
  998. table = mdev->sysctl->ctl_table_arg;
  999. unregister_net_sysctl_table(mdev->sysctl);
  1000. kfree(table);
  1001. }
  1002. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  1003. {
  1004. struct mpls_dev *mdev;
  1005. int err = -ENOMEM;
  1006. int i;
  1007. ASSERT_RTNL();
  1008. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  1009. if (!mdev)
  1010. return ERR_PTR(err);
  1011. mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
  1012. if (!mdev->stats)
  1013. goto free;
  1014. for_each_possible_cpu(i) {
  1015. struct mpls_pcpu_stats *mpls_stats;
  1016. mpls_stats = per_cpu_ptr(mdev->stats, i);
  1017. u64_stats_init(&mpls_stats->syncp);
  1018. }
  1019. err = mpls_dev_sysctl_register(dev, mdev);
  1020. if (err)
  1021. goto free;
  1022. mdev->dev = dev;
  1023. rcu_assign_pointer(dev->mpls_ptr, mdev);
  1024. return mdev;
  1025. free:
  1026. free_percpu(mdev->stats);
  1027. kfree(mdev);
  1028. return ERR_PTR(err);
  1029. }
  1030. static void mpls_dev_destroy_rcu(struct rcu_head *head)
  1031. {
  1032. struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
  1033. free_percpu(mdev->stats);
  1034. kfree(mdev);
  1035. }
  1036. static void mpls_ifdown(struct net_device *dev, int event)
  1037. {
  1038. struct mpls_route __rcu **platform_label;
  1039. struct net *net = dev_net(dev);
  1040. unsigned index;
  1041. platform_label = rtnl_dereference(net->mpls.platform_label);
  1042. for (index = 0; index < net->mpls.platform_labels; index++) {
  1043. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1044. if (!rt)
  1045. continue;
  1046. change_nexthops(rt) {
  1047. if (rtnl_dereference(nh->nh_dev) != dev)
  1048. continue;
  1049. switch (event) {
  1050. case NETDEV_DOWN:
  1051. case NETDEV_UNREGISTER:
  1052. nh->nh_flags |= RTNH_F_DEAD;
  1053. /* fall through */
  1054. case NETDEV_CHANGE:
  1055. nh->nh_flags |= RTNH_F_LINKDOWN;
  1056. ACCESS_ONCE(rt->rt_nhn_alive) = rt->rt_nhn_alive - 1;
  1057. break;
  1058. }
  1059. if (event == NETDEV_UNREGISTER)
  1060. RCU_INIT_POINTER(nh->nh_dev, NULL);
  1061. } endfor_nexthops(rt);
  1062. }
  1063. }
  1064. static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
  1065. {
  1066. struct mpls_route __rcu **platform_label;
  1067. struct net *net = dev_net(dev);
  1068. unsigned index;
  1069. int alive;
  1070. platform_label = rtnl_dereference(net->mpls.platform_label);
  1071. for (index = 0; index < net->mpls.platform_labels; index++) {
  1072. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1073. if (!rt)
  1074. continue;
  1075. alive = 0;
  1076. change_nexthops(rt) {
  1077. struct net_device *nh_dev =
  1078. rtnl_dereference(nh->nh_dev);
  1079. if (!(nh->nh_flags & nh_flags)) {
  1080. alive++;
  1081. continue;
  1082. }
  1083. if (nh_dev != dev)
  1084. continue;
  1085. alive++;
  1086. nh->nh_flags &= ~nh_flags;
  1087. } endfor_nexthops(rt);
  1088. ACCESS_ONCE(rt->rt_nhn_alive) = alive;
  1089. }
  1090. }
  1091. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  1092. void *ptr)
  1093. {
  1094. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1095. struct mpls_dev *mdev;
  1096. unsigned int flags;
  1097. if (event == NETDEV_REGISTER) {
  1098. /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
  1099. if (dev->type == ARPHRD_ETHER ||
  1100. dev->type == ARPHRD_LOOPBACK ||
  1101. dev->type == ARPHRD_IPGRE ||
  1102. dev->type == ARPHRD_SIT ||
  1103. dev->type == ARPHRD_TUNNEL) {
  1104. mdev = mpls_add_dev(dev);
  1105. if (IS_ERR(mdev))
  1106. return notifier_from_errno(PTR_ERR(mdev));
  1107. }
  1108. return NOTIFY_OK;
  1109. }
  1110. mdev = mpls_dev_get(dev);
  1111. if (!mdev)
  1112. return NOTIFY_OK;
  1113. switch (event) {
  1114. case NETDEV_DOWN:
  1115. mpls_ifdown(dev, event);
  1116. break;
  1117. case NETDEV_UP:
  1118. flags = dev_get_flags(dev);
  1119. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1120. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1121. else
  1122. mpls_ifup(dev, RTNH_F_DEAD);
  1123. break;
  1124. case NETDEV_CHANGE:
  1125. flags = dev_get_flags(dev);
  1126. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1127. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1128. else
  1129. mpls_ifdown(dev, event);
  1130. break;
  1131. case NETDEV_UNREGISTER:
  1132. mpls_ifdown(dev, event);
  1133. mdev = mpls_dev_get(dev);
  1134. if (mdev) {
  1135. mpls_dev_sysctl_unregister(mdev);
  1136. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  1137. call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
  1138. }
  1139. break;
  1140. case NETDEV_CHANGENAME:
  1141. mdev = mpls_dev_get(dev);
  1142. if (mdev) {
  1143. int err;
  1144. mpls_dev_sysctl_unregister(mdev);
  1145. err = mpls_dev_sysctl_register(dev, mdev);
  1146. if (err)
  1147. return notifier_from_errno(err);
  1148. }
  1149. break;
  1150. }
  1151. return NOTIFY_OK;
  1152. }
  1153. static struct notifier_block mpls_dev_notifier = {
  1154. .notifier_call = mpls_dev_notify,
  1155. };
  1156. static int nla_put_via(struct sk_buff *skb,
  1157. u8 table, const void *addr, int alen)
  1158. {
  1159. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  1160. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  1161. };
  1162. struct nlattr *nla;
  1163. struct rtvia *via;
  1164. int family = AF_UNSPEC;
  1165. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1166. if (!nla)
  1167. return -EMSGSIZE;
  1168. if (table <= NEIGH_NR_TABLES)
  1169. family = table_to_family[table];
  1170. via = nla_data(nla);
  1171. via->rtvia_family = family;
  1172. memcpy(via->rtvia_addr, addr, alen);
  1173. return 0;
  1174. }
  1175. int nla_put_labels(struct sk_buff *skb, int attrtype,
  1176. u8 labels, const u32 label[])
  1177. {
  1178. struct nlattr *nla;
  1179. struct mpls_shim_hdr *nla_label;
  1180. bool bos;
  1181. int i;
  1182. nla = nla_reserve(skb, attrtype, labels*4);
  1183. if (!nla)
  1184. return -EMSGSIZE;
  1185. nla_label = nla_data(nla);
  1186. bos = true;
  1187. for (i = labels - 1; i >= 0; i--) {
  1188. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  1189. bos = false;
  1190. }
  1191. return 0;
  1192. }
  1193. EXPORT_SYMBOL_GPL(nla_put_labels);
  1194. int nla_get_labels(const struct nlattr *nla,
  1195. u32 max_labels, u8 *labels, u32 label[])
  1196. {
  1197. unsigned len = nla_len(nla);
  1198. unsigned nla_labels;
  1199. struct mpls_shim_hdr *nla_label;
  1200. bool bos;
  1201. int i;
  1202. /* len needs to be an even multiple of 4 (the label size) */
  1203. if (len & 3)
  1204. return -EINVAL;
  1205. /* Limit the number of new labels allowed */
  1206. nla_labels = len/4;
  1207. if (nla_labels > max_labels)
  1208. return -EINVAL;
  1209. nla_label = nla_data(nla);
  1210. bos = true;
  1211. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  1212. struct mpls_entry_decoded dec;
  1213. dec = mpls_entry_decode(nla_label + i);
  1214. /* Ensure the bottom of stack flag is properly set
  1215. * and ttl and tc are both clear.
  1216. */
  1217. if ((dec.bos != bos) || dec.ttl || dec.tc)
  1218. return -EINVAL;
  1219. switch (dec.label) {
  1220. case MPLS_LABEL_IMPLNULL:
  1221. /* RFC3032: This is a label that an LSR may
  1222. * assign and distribute, but which never
  1223. * actually appears in the encapsulation.
  1224. */
  1225. return -EINVAL;
  1226. }
  1227. label[i] = dec.label;
  1228. }
  1229. *labels = nla_labels;
  1230. return 0;
  1231. }
  1232. EXPORT_SYMBOL_GPL(nla_get_labels);
  1233. int nla_get_via(const struct nlattr *nla, u8 *via_alen,
  1234. u8 *via_table, u8 via_addr[])
  1235. {
  1236. struct rtvia *via = nla_data(nla);
  1237. int err = -EINVAL;
  1238. int alen;
  1239. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  1240. goto errout;
  1241. alen = nla_len(nla) -
  1242. offsetof(struct rtvia, rtvia_addr);
  1243. if (alen > MAX_VIA_ALEN)
  1244. goto errout;
  1245. /* Validate the address family */
  1246. switch (via->rtvia_family) {
  1247. case AF_PACKET:
  1248. *via_table = NEIGH_LINK_TABLE;
  1249. break;
  1250. case AF_INET:
  1251. *via_table = NEIGH_ARP_TABLE;
  1252. if (alen != 4)
  1253. goto errout;
  1254. break;
  1255. case AF_INET6:
  1256. *via_table = NEIGH_ND_TABLE;
  1257. if (alen != 16)
  1258. goto errout;
  1259. break;
  1260. default:
  1261. /* Unsupported address family */
  1262. goto errout;
  1263. }
  1264. memcpy(via_addr, via->rtvia_addr, alen);
  1265. *via_alen = alen;
  1266. err = 0;
  1267. errout:
  1268. return err;
  1269. }
  1270. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  1271. struct mpls_route_config *cfg)
  1272. {
  1273. struct rtmsg *rtm;
  1274. struct nlattr *tb[RTA_MAX+1];
  1275. int index;
  1276. int err;
  1277. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
  1278. if (err < 0)
  1279. goto errout;
  1280. err = -EINVAL;
  1281. rtm = nlmsg_data(nlh);
  1282. memset(cfg, 0, sizeof(*cfg));
  1283. if (rtm->rtm_family != AF_MPLS)
  1284. goto errout;
  1285. if (rtm->rtm_dst_len != 20)
  1286. goto errout;
  1287. if (rtm->rtm_src_len != 0)
  1288. goto errout;
  1289. if (rtm->rtm_tos != 0)
  1290. goto errout;
  1291. if (rtm->rtm_table != RT_TABLE_MAIN)
  1292. goto errout;
  1293. /* Any value is acceptable for rtm_protocol */
  1294. /* As mpls uses destination specific addresses
  1295. * (or source specific address in the case of multicast)
  1296. * all addresses have universal scope.
  1297. */
  1298. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  1299. goto errout;
  1300. if (rtm->rtm_type != RTN_UNICAST)
  1301. goto errout;
  1302. if (rtm->rtm_flags != 0)
  1303. goto errout;
  1304. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1305. cfg->rc_protocol = rtm->rtm_protocol;
  1306. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1307. cfg->rc_nlflags = nlh->nlmsg_flags;
  1308. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1309. cfg->rc_nlinfo.nlh = nlh;
  1310. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1311. for (index = 0; index <= RTA_MAX; index++) {
  1312. struct nlattr *nla = tb[index];
  1313. if (!nla)
  1314. continue;
  1315. switch (index) {
  1316. case RTA_OIF:
  1317. cfg->rc_ifindex = nla_get_u32(nla);
  1318. break;
  1319. case RTA_NEWDST:
  1320. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1321. &cfg->rc_output_labels,
  1322. cfg->rc_output_label))
  1323. goto errout;
  1324. break;
  1325. case RTA_DST:
  1326. {
  1327. u8 label_count;
  1328. if (nla_get_labels(nla, 1, &label_count,
  1329. &cfg->rc_label))
  1330. goto errout;
  1331. /* Reserved labels may not be set */
  1332. if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
  1333. goto errout;
  1334. break;
  1335. }
  1336. case RTA_VIA:
  1337. {
  1338. if (nla_get_via(nla, &cfg->rc_via_alen,
  1339. &cfg->rc_via_table, cfg->rc_via))
  1340. goto errout;
  1341. break;
  1342. }
  1343. case RTA_MULTIPATH:
  1344. {
  1345. cfg->rc_mp = nla_data(nla);
  1346. cfg->rc_mp_len = nla_len(nla);
  1347. break;
  1348. }
  1349. default:
  1350. /* Unsupported attribute */
  1351. goto errout;
  1352. }
  1353. }
  1354. err = 0;
  1355. errout:
  1356. return err;
  1357. }
  1358. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1359. {
  1360. struct mpls_route_config cfg;
  1361. int err;
  1362. err = rtm_to_route_config(skb, nlh, &cfg);
  1363. if (err < 0)
  1364. return err;
  1365. return mpls_route_del(&cfg);
  1366. }
  1367. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1368. {
  1369. struct mpls_route_config cfg;
  1370. int err;
  1371. err = rtm_to_route_config(skb, nlh, &cfg);
  1372. if (err < 0)
  1373. return err;
  1374. return mpls_route_add(&cfg);
  1375. }
  1376. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1377. u32 label, struct mpls_route *rt, int flags)
  1378. {
  1379. struct net_device *dev;
  1380. struct nlmsghdr *nlh;
  1381. struct rtmsg *rtm;
  1382. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1383. if (nlh == NULL)
  1384. return -EMSGSIZE;
  1385. rtm = nlmsg_data(nlh);
  1386. rtm->rtm_family = AF_MPLS;
  1387. rtm->rtm_dst_len = 20;
  1388. rtm->rtm_src_len = 0;
  1389. rtm->rtm_tos = 0;
  1390. rtm->rtm_table = RT_TABLE_MAIN;
  1391. rtm->rtm_protocol = rt->rt_protocol;
  1392. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1393. rtm->rtm_type = RTN_UNICAST;
  1394. rtm->rtm_flags = 0;
  1395. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1396. goto nla_put_failure;
  1397. if (rt->rt_nhn == 1) {
  1398. const struct mpls_nh *nh = rt->rt_nh;
  1399. if (nh->nh_labels &&
  1400. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1401. nh->nh_label))
  1402. goto nla_put_failure;
  1403. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1404. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1405. nh->nh_via_alen))
  1406. goto nla_put_failure;
  1407. dev = rtnl_dereference(nh->nh_dev);
  1408. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1409. goto nla_put_failure;
  1410. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1411. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1412. if (nh->nh_flags & RTNH_F_DEAD)
  1413. rtm->rtm_flags |= RTNH_F_DEAD;
  1414. } else {
  1415. struct rtnexthop *rtnh;
  1416. struct nlattr *mp;
  1417. int dead = 0;
  1418. int linkdown = 0;
  1419. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1420. if (!mp)
  1421. goto nla_put_failure;
  1422. for_nexthops(rt) {
  1423. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1424. if (!rtnh)
  1425. goto nla_put_failure;
  1426. dev = rtnl_dereference(nh->nh_dev);
  1427. if (dev)
  1428. rtnh->rtnh_ifindex = dev->ifindex;
  1429. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1430. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1431. linkdown++;
  1432. }
  1433. if (nh->nh_flags & RTNH_F_DEAD) {
  1434. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1435. dead++;
  1436. }
  1437. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1438. nh->nh_labels,
  1439. nh->nh_label))
  1440. goto nla_put_failure;
  1441. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1442. nla_put_via(skb, nh->nh_via_table,
  1443. mpls_nh_via(rt, nh),
  1444. nh->nh_via_alen))
  1445. goto nla_put_failure;
  1446. /* length of rtnetlink header + attributes */
  1447. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1448. } endfor_nexthops(rt);
  1449. if (linkdown == rt->rt_nhn)
  1450. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1451. if (dead == rt->rt_nhn)
  1452. rtm->rtm_flags |= RTNH_F_DEAD;
  1453. nla_nest_end(skb, mp);
  1454. }
  1455. nlmsg_end(skb, nlh);
  1456. return 0;
  1457. nla_put_failure:
  1458. nlmsg_cancel(skb, nlh);
  1459. return -EMSGSIZE;
  1460. }
  1461. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1462. {
  1463. struct net *net = sock_net(skb->sk);
  1464. struct mpls_route __rcu **platform_label;
  1465. size_t platform_labels;
  1466. unsigned int index;
  1467. ASSERT_RTNL();
  1468. index = cb->args[0];
  1469. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1470. index = MPLS_LABEL_FIRST_UNRESERVED;
  1471. platform_label = rtnl_dereference(net->mpls.platform_label);
  1472. platform_labels = net->mpls.platform_labels;
  1473. for (; index < platform_labels; index++) {
  1474. struct mpls_route *rt;
  1475. rt = rtnl_dereference(platform_label[index]);
  1476. if (!rt)
  1477. continue;
  1478. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1479. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1480. index, rt, NLM_F_MULTI) < 0)
  1481. break;
  1482. }
  1483. cb->args[0] = index;
  1484. return skb->len;
  1485. }
  1486. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1487. {
  1488. size_t payload =
  1489. NLMSG_ALIGN(sizeof(struct rtmsg))
  1490. + nla_total_size(4); /* RTA_DST */
  1491. if (rt->rt_nhn == 1) {
  1492. struct mpls_nh *nh = rt->rt_nh;
  1493. if (nh->nh_dev)
  1494. payload += nla_total_size(4); /* RTA_OIF */
  1495. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1496. payload += nla_total_size(2 + nh->nh_via_alen);
  1497. if (nh->nh_labels) /* RTA_NEWDST */
  1498. payload += nla_total_size(nh->nh_labels * 4);
  1499. } else {
  1500. /* each nexthop is packed in an attribute */
  1501. size_t nhsize = 0;
  1502. for_nexthops(rt) {
  1503. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1504. /* RTA_VIA */
  1505. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1506. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1507. if (nh->nh_labels)
  1508. nhsize += nla_total_size(nh->nh_labels * 4);
  1509. } endfor_nexthops(rt);
  1510. /* nested attribute */
  1511. payload += nla_total_size(nhsize);
  1512. }
  1513. return payload;
  1514. }
  1515. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1516. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1517. unsigned int nlm_flags)
  1518. {
  1519. struct sk_buff *skb;
  1520. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1521. int err = -ENOBUFS;
  1522. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1523. if (skb == NULL)
  1524. goto errout;
  1525. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1526. if (err < 0) {
  1527. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1528. WARN_ON(err == -EMSGSIZE);
  1529. kfree_skb(skb);
  1530. goto errout;
  1531. }
  1532. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1533. return;
  1534. errout:
  1535. if (err < 0)
  1536. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1537. }
  1538. static int resize_platform_label_table(struct net *net, size_t limit)
  1539. {
  1540. size_t size = sizeof(struct mpls_route *) * limit;
  1541. size_t old_limit;
  1542. size_t cp_size;
  1543. struct mpls_route __rcu **labels = NULL, **old;
  1544. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1545. unsigned index;
  1546. if (size) {
  1547. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1548. if (!labels)
  1549. labels = vzalloc(size);
  1550. if (!labels)
  1551. goto nolabels;
  1552. }
  1553. /* In case the predefined labels need to be populated */
  1554. if (limit > MPLS_LABEL_IPV4NULL) {
  1555. struct net_device *lo = net->loopback_dev;
  1556. rt0 = mpls_rt_alloc(1, lo->addr_len);
  1557. if (!rt0)
  1558. goto nort0;
  1559. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1560. rt0->rt_protocol = RTPROT_KERNEL;
  1561. rt0->rt_payload_type = MPT_IPV4;
  1562. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1563. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1564. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1565. lo->addr_len);
  1566. }
  1567. if (limit > MPLS_LABEL_IPV6NULL) {
  1568. struct net_device *lo = net->loopback_dev;
  1569. rt2 = mpls_rt_alloc(1, lo->addr_len);
  1570. if (!rt2)
  1571. goto nort2;
  1572. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1573. rt2->rt_protocol = RTPROT_KERNEL;
  1574. rt2->rt_payload_type = MPT_IPV6;
  1575. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1576. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1577. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1578. lo->addr_len);
  1579. }
  1580. rtnl_lock();
  1581. /* Remember the original table */
  1582. old = rtnl_dereference(net->mpls.platform_label);
  1583. old_limit = net->mpls.platform_labels;
  1584. /* Free any labels beyond the new table */
  1585. for (index = limit; index < old_limit; index++)
  1586. mpls_route_update(net, index, NULL, NULL);
  1587. /* Copy over the old labels */
  1588. cp_size = size;
  1589. if (old_limit < limit)
  1590. cp_size = old_limit * sizeof(struct mpls_route *);
  1591. memcpy(labels, old, cp_size);
  1592. /* If needed set the predefined labels */
  1593. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1594. (limit > MPLS_LABEL_IPV6NULL)) {
  1595. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1596. rt2 = NULL;
  1597. }
  1598. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1599. (limit > MPLS_LABEL_IPV4NULL)) {
  1600. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1601. rt0 = NULL;
  1602. }
  1603. /* Update the global pointers */
  1604. net->mpls.platform_labels = limit;
  1605. rcu_assign_pointer(net->mpls.platform_label, labels);
  1606. rtnl_unlock();
  1607. mpls_rt_free(rt2);
  1608. mpls_rt_free(rt0);
  1609. if (old) {
  1610. synchronize_rcu();
  1611. kvfree(old);
  1612. }
  1613. return 0;
  1614. nort2:
  1615. mpls_rt_free(rt0);
  1616. nort0:
  1617. kvfree(labels);
  1618. nolabels:
  1619. return -ENOMEM;
  1620. }
  1621. static int mpls_platform_labels(struct ctl_table *table, int write,
  1622. void __user *buffer, size_t *lenp, loff_t *ppos)
  1623. {
  1624. struct net *net = table->data;
  1625. int platform_labels = net->mpls.platform_labels;
  1626. int ret;
  1627. struct ctl_table tmp = {
  1628. .procname = table->procname,
  1629. .data = &platform_labels,
  1630. .maxlen = sizeof(int),
  1631. .mode = table->mode,
  1632. .extra1 = &zero,
  1633. .extra2 = &label_limit,
  1634. };
  1635. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1636. if (write && ret == 0)
  1637. ret = resize_platform_label_table(net, platform_labels);
  1638. return ret;
  1639. }
  1640. static const struct ctl_table mpls_table[] = {
  1641. {
  1642. .procname = "platform_labels",
  1643. .data = NULL,
  1644. .maxlen = sizeof(int),
  1645. .mode = 0644,
  1646. .proc_handler = mpls_platform_labels,
  1647. },
  1648. { }
  1649. };
  1650. static int mpls_net_init(struct net *net)
  1651. {
  1652. struct ctl_table *table;
  1653. net->mpls.platform_labels = 0;
  1654. net->mpls.platform_label = NULL;
  1655. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  1656. if (table == NULL)
  1657. return -ENOMEM;
  1658. table[0].data = net;
  1659. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  1660. if (net->mpls.ctl == NULL) {
  1661. kfree(table);
  1662. return -ENOMEM;
  1663. }
  1664. return 0;
  1665. }
  1666. static void mpls_net_exit(struct net *net)
  1667. {
  1668. struct mpls_route __rcu **platform_label;
  1669. size_t platform_labels;
  1670. struct ctl_table *table;
  1671. unsigned int index;
  1672. table = net->mpls.ctl->ctl_table_arg;
  1673. unregister_net_sysctl_table(net->mpls.ctl);
  1674. kfree(table);
  1675. /* An rcu grace period has passed since there was a device in
  1676. * the network namespace (and thus the last in flight packet)
  1677. * left this network namespace. This is because
  1678. * unregister_netdevice_many and netdev_run_todo has completed
  1679. * for each network device that was in this network namespace.
  1680. *
  1681. * As such no additional rcu synchronization is necessary when
  1682. * freeing the platform_label table.
  1683. */
  1684. rtnl_lock();
  1685. platform_label = rtnl_dereference(net->mpls.platform_label);
  1686. platform_labels = net->mpls.platform_labels;
  1687. for (index = 0; index < platform_labels; index++) {
  1688. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1689. RCU_INIT_POINTER(platform_label[index], NULL);
  1690. mpls_rt_free(rt);
  1691. }
  1692. rtnl_unlock();
  1693. kvfree(platform_label);
  1694. }
  1695. static struct pernet_operations mpls_net_ops = {
  1696. .init = mpls_net_init,
  1697. .exit = mpls_net_exit,
  1698. };
  1699. static struct rtnl_af_ops mpls_af_ops __read_mostly = {
  1700. .family = AF_MPLS,
  1701. .fill_stats_af = mpls_fill_stats_af,
  1702. .get_stats_af_size = mpls_get_stats_af_size,
  1703. };
  1704. static int __init mpls_init(void)
  1705. {
  1706. int err;
  1707. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1708. err = register_pernet_subsys(&mpls_net_ops);
  1709. if (err)
  1710. goto out;
  1711. err = register_netdevice_notifier(&mpls_dev_notifier);
  1712. if (err)
  1713. goto out_unregister_pernet;
  1714. dev_add_pack(&mpls_packet_type);
  1715. rtnl_af_register(&mpls_af_ops);
  1716. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1717. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1718. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1719. rtnl_register(PF_MPLS, RTM_GETNETCONF, mpls_netconf_get_devconf,
  1720. mpls_netconf_dump_devconf, NULL);
  1721. err = 0;
  1722. out:
  1723. return err;
  1724. out_unregister_pernet:
  1725. unregister_pernet_subsys(&mpls_net_ops);
  1726. goto out;
  1727. }
  1728. module_init(mpls_init);
  1729. static void __exit mpls_exit(void)
  1730. {
  1731. rtnl_unregister_all(PF_MPLS);
  1732. rtnl_af_unregister(&mpls_af_ops);
  1733. dev_remove_pack(&mpls_packet_type);
  1734. unregister_netdevice_notifier(&mpls_dev_notifier);
  1735. unregister_pernet_subsys(&mpls_net_ops);
  1736. }
  1737. module_exit(mpls_exit);
  1738. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1739. MODULE_LICENSE("GPL v2");
  1740. MODULE_ALIAS_NETPROTO(PF_MPLS);